diff --git a/RageCoop.Client/Main.cs b/RageCoop.Client/Main.cs index 6b7ea67..5733165 100644 --- a/RageCoop.Client/Main.cs +++ b/RageCoop.Client/Main.cs @@ -20,7 +20,6 @@ namespace RageCoop.Client /// internal class Main : Script { - private bool _gameLoaded = false; internal static readonly string CurrentVersion = "V0_5_0"; @@ -47,8 +46,6 @@ namespace RageCoop.Client /// 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(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() { diff --git a/RageCoop.Client/Menus/Sub/SettingsMenu.cs b/RageCoop.Client/Menus/Sub/SettingsMenu.cs index 530ac69..25e5096 100644 --- a/RageCoop.Client/Menus/Sub/SettingsMenu.cs +++ b/RageCoop.Client/Menus/Sub/SettingsMenu.cs @@ -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; diff --git a/RageCoop.Client/Networking/Receive.cs b/RageCoop.Client/Networking/Receive.cs index 531f865..b559ed4 100644 --- a/RageCoop.Client/Networking/Receive.cs +++ b/RageCoop.Client/Networking/Receive.cs @@ -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; diff --git a/RageCoop.Client/Networking/Send.cs b/RageCoop.Client/Networking/Send.cs index 97f0f34..088e1a1 100644 --- a/RageCoop.Client/Networking/Send.cs +++ b/RageCoop.Client/Networking/Send.cs @@ -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); } } } diff --git a/RageCoop.Client/Sync/Voice.cs b/RageCoop.Client/Sync/Voice.cs index fd828fe..518f974 100644 --- a/RageCoop.Client/Sync/Voice.cs +++ b/RageCoop.Client/Sync/Voice.cs @@ -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); } } } diff --git a/RageCoop.Core/Packets/Voice.cs b/RageCoop.Core/Packets/Voice.cs index 67e3fa8..a2e82d9 100644 --- a/RageCoop.Core/Packets/Voice.cs +++ b/RageCoop.Core/Packets/Voice.cs @@ -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(); + 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(); } } }