Small update for voice chat

This commit is contained in:
Nick I. A
2022-08-14 02:26:59 +02:00
parent 609fd90561
commit f9609afd8a
5 changed files with 25 additions and 29 deletions

View File

@ -204,7 +204,7 @@ namespace RageCoop.Client
Sync.Voice.StartRecording(); Sync.Voice.StartRecording();
return; return;
} }
else if (Sync.Voice.IsRecording) else if (Sync.Voice.IsRecording())
{ {
Sync.Voice.StopRecording(); Sync.Voice.StopRecording();
return; return;

View File

@ -17,7 +17,7 @@ namespace RageCoop.Client.Menus
private static readonly NativeCheckboxItem _disableTrafficItem = new NativeCheckboxItem("Disable Traffic (NPCs/Vehicles)", "Local traffic only", Main.Settings.DisableTraffic); private static readonly NativeCheckboxItem _disableTrafficItem = new NativeCheckboxItem("Disable Traffic (NPCs/Vehicles)", "Local traffic only", Main.Settings.DisableTraffic);
private static readonly NativeCheckboxItem _flipMenuItem = new NativeCheckboxItem("Flip menu", Main.Settings.FlipMenu); private static readonly NativeCheckboxItem _flipMenuItem = new NativeCheckboxItem("Flip menu", Main.Settings.FlipMenu);
private static readonly NativeCheckboxItem _disablePauseAlt = new NativeCheckboxItem("Disable Alternate Pause", "Don't freeze game time when Esc pressed", Main.Settings.DisableTraffic); private static readonly NativeCheckboxItem _disablePauseAlt = new NativeCheckboxItem("Disable Alternate Pause", "Don't freeze game time when Esc pressed", Main.Settings.DisableTraffic);
private static readonly NativeCheckboxItem _disableVoice = new NativeCheckboxItem("Enable/Disable the voice", Main.Settings.Voice); private static readonly NativeCheckboxItem _disableVoice = new NativeCheckboxItem("Enable voice", "Check your GTA:V settings to find the right key on your keyboard for PushToTalk and talk to your friends", Main.Settings.Voice);
private static NativeItem _menuKey = new NativeItem("Menu Key", "The key to open menu", Main.Settings.MenuKey.ToString()); private static NativeItem _menuKey = new NativeItem("Menu Key", "The key to open menu", Main.Settings.MenuKey.ToString());
private static NativeItem _passengerKey = new NativeItem("Passenger Key", "The key to enter a vehicle as passenger", Main.Settings.PassengerKey.ToString()); private static NativeItem _passengerKey = new NativeItem("Passenger Key", "The key to enter a vehicle as passenger", Main.Settings.PassengerKey.ToString());
@ -47,9 +47,12 @@ namespace RageCoop.Client.Menus
private static void DisableVoiceCheckboxChanged(object sender, EventArgs e) private static void DisableVoiceCheckboxChanged(object sender, EventArgs e)
{ {
if (_disableVoice.Checked && !Sync.Voice.WasInitialized()) if (_disableVoice.Checked)
{ {
Sync.Voice.InitRecording(); if (Networking.IsOnServer && !Sync.Voice.WasInitialized())
{
Sync.Voice.Init();
}
} else { } else {
Sync.Voice.ClearAll(); Sync.Voice.ClearAll();
} }

View File

@ -79,7 +79,7 @@ namespace RageCoop.Client
Main.MainChat.Init(); Main.MainChat.Init();
if (Main.Settings.Voice && !Sync.Voice.WasInitialized()) if (Main.Settings.Voice && !Sync.Voice.WasInitialized())
{ {
Sync.Voice.InitRecording(); Sync.Voice.Init();
} }
GTA.UI.Notification.Show("~g~Connected!"); GTA.UI.Notification.Show("~g~Connected!");
}); });
@ -95,7 +95,7 @@ namespace RageCoop.Client
if (PlayerList.Players.TryGetValue(p.ID,out var player)) if (PlayerList.Players.TryGetValue(p.ID,out var player))
{ {
player.Connection=message.SenderConnection; player.Connection=message.SenderConnection;
Main.Logger.Debug($"Direct connectionn to {player.Username} established"); Main.Logger.Debug($"Direct connection to {player.Username} established");
} }
else else
{ {

View File

@ -6,15 +6,13 @@ namespace RageCoop.Client.Sync
{ {
internal static class Voice internal static class Voice
{ {
private static bool _initialized = false;
public static bool IsRecording = false;
private static WaveInEvent _waveIn; private static WaveInEvent _waveIn;
private static BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(16000, 16, 1)); private static readonly BufferedWaveProvider _waveProvider = new BufferedWaveProvider(new WaveFormat(16000, 16, 1));
private static Thread _thread; private static Thread _thread;
public static bool WasInitialized() => _initialized; public static bool WasInitialized() => _thread != null;
public static bool IsRecording() => _waveIn != null;
public static void ClearAll() public static void ClearAll()
{ {
_waveProvider.ClearBuffer(); _waveProvider.ClearBuffer();
@ -26,25 +24,21 @@ namespace RageCoop.Client.Sync
_thread.Abort(); _thread.Abort();
_thread = null; _thread = null;
} }
_initialized = false;
} }
public static void StopRecording() public static void StopRecording()
{ {
if (_waveIn != null) if (!IsRecording())
{ return;
_waveIn.StopRecording();
_waveIn.Dispose();
_waveIn = null;
}
IsRecording = false; _waveIn.StopRecording();
_waveIn.Dispose();
_waveIn = null;
} }
public static void InitRecording() public static void Init()
{ {
if (_initialized) if (WasInitialized())
return; return;
// I tried without thread but the game will lag without // I tried without thread but the game will lag without
@ -65,17 +59,13 @@ namespace RageCoop.Client.Sync
} }
})); }));
_thread.Start(); _thread.Start();
_initialized = true;
} }
public static void StartRecording() public static void StartRecording()
{ {
if (IsRecording) if (IsRecording())
return; return;
IsRecording = true;
_waveIn = new WaveInEvent _waveIn = new WaveInEvent
{ {
DeviceNumber = 0, DeviceNumber = 0,
@ -95,7 +85,7 @@ namespace RageCoop.Client.Sync
private static void WaveInDataAvailable(object sender, WaveInEventArgs e) private static void WaveInDataAvailable(object sender, WaveInEventArgs e)
{ {
if (_waveIn == null || !IsRecording) if (!IsRecording())
return; return;
Networking.SendVoiceMessage(e.Buffer, e.BytesRecorded); Networking.SendVoiceMessage(e.Buffer, e.BytesRecorded);

View File

@ -486,7 +486,10 @@ namespace RageCoop.Server
case PacketType.Voice: case PacketType.Voice:
{ {
Forward(data.GetPacket<Packets.Voice>(),sender,ConnectionChannel.Voice); if (Settings.UseVoice)
{
Forward(data.GetPacket<Packets.Voice>(), sender, ConnectionChannel.Voice);
}
} }
break; break;