diff --git a/Facepunch.Steamworks.Test/Client.cs b/Facepunch.Steamworks.Test/Client.cs index c20e95e..b9063f8 100644 --- a/Facepunch.Steamworks.Test/Client.cs +++ b/Facepunch.Steamworks.Test/Client.cs @@ -67,5 +67,48 @@ public void ClientVoiceOptimalSampleRate() Assert.AreNotEqual( rate, 0 ); } } + + [TestMethod] + public void ClientUpdate() + { + using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) + { + for( int i=0; i<32; i++ ) + { + client.Update(); + System.Threading.Thread.Sleep( 10 ); + } + } + } + + [TestMethod] + public void ClientGetVoice() + { + using ( var client = new Facepunch.Steamworks.Client( 252490 ) ) + { + int dataRead = 0; + + client.Voice.OnCompressedData = ( data ) => + { + dataRead += data.Length; + }; + + client.Voice.OnUncompressedData = ( data ) => + { + dataRead += data.Length; + }; + + client.Voice.WantsRecording = true; + + for ( int i = 0; i < 32; i++ ) + { + client.Update(); + System.Threading.Thread.Sleep( 10 ); + } + + // Should really be > 0 if the mic was getting audio + Console.Write( dataRead ); + } + } } } diff --git a/Facepunch.Steamworks/Client.Voice.cs b/Facepunch.Steamworks/Client.Voice.cs index 10d8f8e..cc003ea 100644 --- a/Facepunch.Steamworks/Client.Voice.cs +++ b/Facepunch.Steamworks/Client.Voice.cs @@ -24,10 +24,91 @@ public Voice Voice public class Voice { internal Client client; + internal byte[] bufferRegular = new byte[ 1024 * 8 ]; + internal uint bufferRegularLastWrite = 0; + internal byte[] bufferCompressed = new byte[ 1024 * 8 ]; + internal uint bufferCompressedLastWrite = 0; + + public Action OnCompressedData; + public Action OnUncompressedData; + + + /// + /// Returns the optimal sample rate for voice - according to Steam + /// public uint OptimalSampleRate { get { return client._user.GetVoiceOptimalSampleRate(); } } + + private bool _wantsrecording = false; + + + /// + /// If set to true we are listening to the mic. + /// You should usually toggle this with the press of a key for push to talk. + /// + public bool WantsRecording + { + get { return _wantsrecording; } + set + { + _wantsrecording = value; + + if ( value ) + { + client._user.StartVoiceRecording(); + } + else + { + client._user.StopVoiceRecording(); + } + } + } + + + public bool IsRecording = false; + + /// + /// If set we will capture the audio at this rate. If unset (set to 0) will capture at OptimalSampleRate + /// + public uint DesiredSampleRate = 0; + + internal unsafe void Update() + { + if ( OnCompressedData == null && OnUncompressedData == null ) + return; + + fixed ( byte* pbufferRegular = bufferRegular ) + fixed ( byte* pbufferCompressed = bufferCompressed ) + { + bufferRegularLastWrite = 0; + bufferCompressedLastWrite = 0; + + Valve.Steamworks.EVoiceResult result = (Valve.Steamworks.EVoiceResult) client._user.GetVoice( OnUncompressedData != null, (IntPtr) pbufferCompressed, (uint) bufferCompressed.Length, ref bufferCompressedLastWrite, + OnCompressedData != null, (IntPtr) pbufferRegular, (uint) bufferRegular.Length, ref bufferRegularLastWrite, + DesiredSampleRate == 0 ? OptimalSampleRate : DesiredSampleRate ); + + IsRecording = true; + + if ( result == Valve.Steamworks.EVoiceResult.k_EVoiceResultOK ) + { + if ( OnCompressedData != null && bufferCompressedLastWrite > 0 ) + { + OnCompressedData( bufferRegular.Take( (int)bufferCompressedLastWrite ).ToArray() ); + } + + if ( OnUncompressedData != null && bufferRegularLastWrite > 0 ) + { + OnUncompressedData( bufferRegular.Take( (int)bufferRegularLastWrite ).ToArray() ); + } + } + + if ( result == Valve.Steamworks.EVoiceResult.k_EVoiceResultNotRecording || + result == Valve.Steamworks.EVoiceResult.k_EVoiceResultNotInitialized ) + IsRecording = false; + } + } } } diff --git a/Facepunch.Steamworks/Client.cs b/Facepunch.Steamworks/Client.cs index af072b9..58b66ba 100644 --- a/Facepunch.Steamworks/Client.cs +++ b/Facepunch.Steamworks/Client.cs @@ -94,6 +94,15 @@ private void OnWarning( int nSeverity, System.Text.StringBuilder text ) Console.Write( text.ToString() ); } + /// + /// Should be called at least once every frame + /// + public void Update() + { + Valve.Steamworks.SteamAPI.RunCallbacks(); + Voice.Update(); + } + public bool Valid { get { return _client != null; }