Voice recording

This commit is contained in:
Garry Newman 2016-07-07 14:41:28 +01:00
parent 98fe62e4b7
commit f84aba045c
3 changed files with 133 additions and 0 deletions

View File

@ -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 );
}
}
}
}

View File

@ -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<byte[]> OnCompressedData;
public Action<byte[]> OnUncompressedData;
/// <summary>
/// Returns the optimal sample rate for voice - according to Steam
/// </summary>
public uint OptimalSampleRate
{
get { return client._user.GetVoiceOptimalSampleRate(); }
}
private bool _wantsrecording = false;
/// <summary>
/// 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.
/// </summary>
public bool WantsRecording
{
get { return _wantsrecording; }
set
{
_wantsrecording = value;
if ( value )
{
client._user.StartVoiceRecording();
}
else
{
client._user.StopVoiceRecording();
}
}
}
public bool IsRecording = false;
/// <summary>
/// If set we will capture the audio at this rate. If unset (set to 0) will capture at OptimalSampleRate
/// </summary>
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;
}
}
}
}

View File

@ -94,6 +94,15 @@ private void OnWarning( int nSeverity, System.Text.StringBuilder text )
Console.Write( text.ToString() );
}
/// <summary>
/// Should be called at least once every frame
/// </summary>
public void Update()
{
Valve.Steamworks.SteamAPI.RunCallbacks();
Voice.Update();
}
public bool Valid
{
get { return _client != null; }