2022-08-13 04:03:01 +02:00
|
|
|
|
using System.Threading;
|
2022-08-13 00:52:34 +02:00
|
|
|
|
|
|
|
|
|
using NAudio.Wave;
|
|
|
|
|
|
2022-08-15 16:13:53 +08:00
|
|
|
|
namespace RageCoop.Client
|
2022-08-13 00:52:34 +02:00
|
|
|
|
{
|
|
|
|
|
internal static class Voice
|
|
|
|
|
{
|
|
|
|
|
private static WaveInEvent _waveIn;
|
2022-08-14 02:26:59 +02:00
|
|
|
|
private static readonly BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(16000, 16, 1));
|
2022-08-13 00:52:34 +02:00
|
|
|
|
|
|
|
|
|
private static Thread _thread;
|
|
|
|
|
|
2022-08-14 02:26:59 +02:00
|
|
|
|
public static bool WasInitialized() => _thread != null;
|
|
|
|
|
public static bool IsRecording() => _waveIn != null;
|
2022-08-13 04:03:01 +02:00
|
|
|
|
public static void ClearAll()
|
|
|
|
|
{
|
|
|
|
|
_waveProvider.ClearBuffer();
|
|
|
|
|
|
|
|
|
|
StopRecording();
|
|
|
|
|
|
|
|
|
|
if (_thread != null && _thread.IsAlive)
|
|
|
|
|
{
|
|
|
|
|
_thread.Abort();
|
|
|
|
|
_thread = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-13 03:39:11 +02:00
|
|
|
|
|
2022-08-13 00:52:34 +02:00
|
|
|
|
public static void StopRecording()
|
|
|
|
|
{
|
2022-08-14 02:26:59 +02:00
|
|
|
|
if (!IsRecording())
|
|
|
|
|
return;
|
2022-08-13 03:39:11 +02:00
|
|
|
|
|
2022-08-14 02:26:59 +02:00
|
|
|
|
_waveIn.StopRecording();
|
|
|
|
|
_waveIn.Dispose();
|
|
|
|
|
_waveIn = null;
|
2022-08-13 00:52:34 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-14 02:26:59 +02:00
|
|
|
|
public static void Init()
|
2022-08-13 00:52:34 +02:00
|
|
|
|
{
|
2022-08-14 02:26:59 +02:00
|
|
|
|
if (WasInitialized())
|
2022-08-13 03:39:11 +02:00
|
|
|
|
return;
|
|
|
|
|
|
2022-08-13 02:39:18 +02:00
|
|
|
|
// I tried without thread but the game will lag without
|
2022-08-13 00:52:34 +02:00
|
|
|
|
_thread = new Thread(new ThreadStart(() =>
|
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
using (var wo = new WaveOutEvent())
|
|
|
|
|
{
|
|
|
|
|
wo.Init(_waveProvider);
|
|
|
|
|
wo.Play();
|
|
|
|
|
|
|
|
|
|
while (wo.PlaybackState == PlaybackState.Playing)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
_thread.Start();
|
2022-08-13 03:39:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void StartRecording()
|
|
|
|
|
{
|
2022-08-14 02:26:59 +02:00
|
|
|
|
if (IsRecording())
|
2022-08-13 03:39:11 +02:00
|
|
|
|
return;
|
|
|
|
|
|
2022-08-13 00:52:34 +02:00
|
|
|
|
_waveIn = new WaveInEvent
|
|
|
|
|
{
|
|
|
|
|
DeviceNumber = 0,
|
|
|
|
|
BufferMilliseconds = 20,
|
|
|
|
|
NumberOfBuffers = 1,
|
|
|
|
|
WaveFormat = _waveProvider.WaveFormat
|
|
|
|
|
};
|
|
|
|
|
_waveIn.DataAvailable += WaveInDataAvailable;
|
2022-08-13 02:19:40 +02:00
|
|
|
|
|
2022-08-13 00:52:34 +02:00
|
|
|
|
_waveIn.StartRecording();
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 03:39:11 +02:00
|
|
|
|
public static void AddVoiceData(byte[] buffer, int recorded)
|
|
|
|
|
{
|
|
|
|
|
_waveProvider.AddSamples(buffer, 0, recorded);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-13 00:52:34 +02:00
|
|
|
|
private static void WaveInDataAvailable(object sender, WaveInEventArgs e)
|
|
|
|
|
{
|
2022-08-14 02:26:59 +02:00
|
|
|
|
if (!IsRecording())
|
2022-08-13 00:52:34 +02:00
|
|
|
|
return;
|
|
|
|
|
|
2022-08-13 03:39:11 +02:00
|
|
|
|
Networking.SendVoiceMessage(e.Buffer, e.BytesRecorded);
|
2022-08-13 00:52:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|