2022-08-13 00:52:34 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
|
|
using NAudio.Wave;
|
|
|
|
|
|
|
|
|
|
namespace RageCoop.Client.Sync
|
|
|
|
|
{
|
|
|
|
|
internal static class Voice
|
|
|
|
|
{
|
2022-08-13 02:19:40 +02:00
|
|
|
|
public static bool IsRecording = false;
|
|
|
|
|
|
2022-08-13 00:52:34 +02:00
|
|
|
|
private static WaveInEvent _waveIn;
|
|
|
|
|
private static BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(16000, 16, 1));
|
|
|
|
|
|
|
|
|
|
private static Thread _thread;
|
|
|
|
|
|
|
|
|
|
public static void StopRecording()
|
|
|
|
|
{
|
2022-08-13 02:19:40 +02:00
|
|
|
|
if (!IsRecording)
|
|
|
|
|
return;
|
|
|
|
|
|
2022-08-13 00:52:34 +02:00
|
|
|
|
_waveIn.StopRecording();
|
|
|
|
|
GTA.UI.Notification.Show("STOPPED [1]");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void InitRecording()
|
|
|
|
|
{
|
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();
|
|
|
|
|
|
|
|
|
|
_waveIn = new WaveInEvent
|
|
|
|
|
{
|
|
|
|
|
DeviceNumber = 0,
|
|
|
|
|
BufferMilliseconds = 20,
|
|
|
|
|
NumberOfBuffers = 1,
|
|
|
|
|
WaveFormat = _waveProvider.WaveFormat
|
|
|
|
|
};
|
|
|
|
|
_waveIn.DataAvailable += WaveInDataAvailable;
|
2022-08-13 02:19:40 +02:00
|
|
|
|
_waveIn.RecordingStopped += (object sender, StoppedEventArgs e) =>
|
|
|
|
|
{
|
|
|
|
|
IsRecording = false;
|
|
|
|
|
};
|
2022-08-13 00:52:34 +02:00
|
|
|
|
GTA.UI.Notification.Show("INIT");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void StartRecording()
|
|
|
|
|
{
|
2022-08-13 02:19:40 +02:00
|
|
|
|
if (IsRecording)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
IsRecording = true;
|
2022-08-13 00:52:34 +02:00
|
|
|
|
_waveIn.StartRecording();
|
|
|
|
|
GTA.UI.Notification.Show("STARTED");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void WaveInDataAvailable(object sender, WaveInEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (_waveIn == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_waveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
|
2022-08-13 02:39:18 +02:00
|
|
|
|
|
2022-08-13 02:19:40 +02:00
|
|
|
|
Networking.SendVoiceMessage(e.Buffer);
|
2022-08-13 00:52:34 +02:00
|
|
|
|
} catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
// if some happens along the way...
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|