More voice changes

This commit is contained in:
Nick I. A 2022-08-13 03:39:11 +02:00
parent 4dc3cc5c28
commit 8f5deaa33b
6 changed files with 80 additions and 42 deletions

View File

@ -20,7 +20,6 @@ namespace RageCoop.Client
/// </summary>
internal class Main : Script
{
private bool _gameLoaded = false;
internal static readonly string CurrentVersion = "V0_5_0";
@ -47,8 +46,6 @@ namespace RageCoop.Client
/// </summary>
public Main()
{
Sync.Voice.InitRecording();
Worker = new Worker("RageCoop.Client.Main.Worker", Logger);
try
{
@ -94,6 +91,10 @@ namespace RageCoop.Client
#if !NON_INTERACTIVE
#endif
MainChat = new Chat();
if (Settings.Voice && !Sync.Voice.WasInitialized())
{
Sync.Voice.InitRecording();
}
Tick += OnTick;
Tick += (s, e) => { Scripting.API.Events.InvokeTick(); };
KeyDown += OnKeyDown;
@ -224,16 +225,20 @@ namespace RageCoop.Client
}
if (Networking.IsOnServer)
{
if (Game.IsControlPressed(GTA.Control.PushToTalk))
if (Sync.Voice.WasInitialized())
{
Sync.Voice.StartRecording();
return;
if (Game.IsControlPressed(GTA.Control.PushToTalk))
{
Sync.Voice.StartRecording();
return;
}
else if (Sync.Voice.IsRecording)
{
Sync.Voice.StopRecording();
return;
}
}
else if (Sync.Voice.IsRecording)
{
Sync.Voice.StopRecording();
}
if (Game.IsControlPressed(GTA.Control.FrontendPause))
{
Function.Call(Hash.ACTIVATE_FRONTEND_MENU, Function.Call<int>(Hash.GET_HASH_KEY, "FE_MENU_VERSION_SP_PAUSE"), false, 0);
@ -304,10 +309,10 @@ namespace RageCoop.Client
public static void CleanUp()
{
MainChat.Clear();
Sync.Voice.ClearBuffer();
EntityPool.Cleanup();
PlayerList.Cleanup();
Main.LocalPlayerID=default;
}
private static void DoQueuedActions()
{

View File

@ -30,6 +30,7 @@ namespace RageCoop.Client.Menus
_disableTrafficItem.CheckboxChanged += DisableTrafficCheckboxChanged;
_disablePauseAlt.CheckboxChanged+= DisablePauseAltCheckboxChanged;
_disableVoice.CheckboxChanged += DisableVoiceCheckboxChanged;
_flipMenuItem.CheckboxChanged += FlipMenuCheckboxChanged;
_menuKey.Activated+= ChaneMenuKey;
_passengerKey.Activated+= ChangePassengerKey;
@ -44,6 +45,17 @@ namespace RageCoop.Client.Menus
Menu.Add(_vehicleSoftLimit);
}
private static void DisableVoiceCheckboxChanged(object sender, EventArgs e)
{
if (_disableVoice.Checked && !Sync.Voice.WasInitialized())
{
Sync.Voice.InitRecording();
}
Main.Settings.Voice = _disableVoice.Checked;
Util.SaveSettings();
}
private static void DisablePauseAltCheckboxChanged(object sender, EventArgs e)
{
Main.Settings.DisableAlternatePause=_disablePauseAlt.Checked;

View File

@ -250,7 +250,16 @@ namespace RageCoop.Client
case PacketType.Voice:
{
Main.QueueAction(() => GTA.UI.Notification.Show("VOICE RECEIVED!"));
if (Main.Settings.Voice)
{
Packets.Voice packet = new Packets.Voice();
packet.Deserialize(data);
Main.QueueAction(() =>
{
Sync.Voice.AddVoiceData(packet.Buffer, packet.Recorded);
});
}
}
break;

View File

@ -173,9 +173,9 @@ namespace RageCoop.Client
{ Username = Main.Settings.Username, Message = message },ServerConnection, ConnectionChannel.Chat, NetDeliveryMethod.ReliableOrdered);
Peer.FlushSendQueue();
}
public static void SendVoiceMessage(byte[] buffer)
public static void SendVoiceMessage(byte[] buffer, int recorded)
{
SendSync(new Packets.Voice() { Buffer = buffer }, ConnectionChannel.Voice, NetDeliveryMethod.ReliableOrdered);
SendSync(new Packets.Voice() { Buffer = buffer, Recorded = recorded }, ConnectionChannel.Voice, NetDeliveryMethod.ReliableOrdered);
}
}
}

View File

@ -9,6 +9,7 @@ namespace RageCoop.Client.Sync
{
internal static class Voice
{
private static bool _initialized = false;
public static bool IsRecording = false;
private static WaveInEvent _waveIn;
@ -16,17 +17,27 @@ namespace RageCoop.Client.Sync
private static Thread _thread;
public static bool WasInitialized() => _initialized;
public static void ClearBuffer() => _waveProvider.ClearBuffer();
public static void StopRecording()
{
if (!IsRecording)
if (!IsRecording || _waveIn == null)
return;
_waveIn.StopRecording();
GTA.UI.Notification.Show("STOPPED [1]");
_waveIn.Dispose();
_waveIn = null;
IsRecording = false;
GTA.UI.Notification.Show("STOPPED");
}
public static void InitRecording()
{
if (_initialized)
return;
// I tried without thread but the game will lag without
_thread = new Thread(new ThreadStart(() =>
{
@ -46,19 +57,7 @@ namespace RageCoop.Client.Sync
}));
_thread.Start();
_waveIn = new WaveInEvent
{
DeviceNumber = 0,
BufferMilliseconds = 20,
NumberOfBuffers = 1,
WaveFormat = _waveProvider.WaveFormat
};
_waveIn.DataAvailable += WaveInDataAvailable;
_waveIn.RecordingStopped += (object sender, StoppedEventArgs e) =>
{
IsRecording = false;
};
GTA.UI.Notification.Show("INIT");
_initialized = true;
}
public static void StartRecording()
@ -67,24 +66,31 @@ namespace RageCoop.Client.Sync
return;
IsRecording = true;
_waveIn = new WaveInEvent
{
DeviceNumber = 0,
BufferMilliseconds = 20,
NumberOfBuffers = 1,
WaveFormat = _waveProvider.WaveFormat
};
_waveIn.DataAvailable += WaveInDataAvailable;
_waveIn.StartRecording();
GTA.UI.Notification.Show("STARTED");
}
public static void AddVoiceData(byte[] buffer, int recorded)
{
_waveProvider.AddSamples(buffer, 0, recorded);
}
private static void WaveInDataAvailable(object sender, WaveInEventArgs e)
{
if (_waveIn == null)
if (_waveIn == null || !IsRecording)
return;
try
{
_waveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
Networking.SendVoiceMessage(e.Buffer);
} catch (Exception ex)
{
// if some happens along the way...
}
Networking.SendVoiceMessage(e.Buffer, e.BytesRecorded);
}
}
}

View File

@ -7,14 +7,20 @@ namespace RageCoop.Core
internal class Voice : Packet
{
public byte[] Buffer { get; set; }
public int Recorded { get; set; }
public override PacketType Type => PacketType.Voice;
public override byte[] Serialize()
{
return Buffer;
var data = new List<byte>();
data.AddArray(Buffer);
data.AddInt(Recorded);
return data.ToArray();
}
public override void Deserialize(byte[] array)
{
Buffer = array;
var reader = new BitReader(array);
Buffer = reader.ReadByteArray();
Recorded = reader.ReadInt32();
}
}
}