Skip to content

Commit

Permalink
Experimental: Add toggle for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry221060 committed Oct 24, 2023
1 parent 87455ac commit db95fac
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/Audio/AudioData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class AudioData
public ALFormat format;
public double amplitude;
public VoiceLevel voiceLevel;
public string codec;

public AudioData() { }

Expand All @@ -21,6 +22,7 @@ public static AudioData FromPacket(AudioPacket audioPacket)
frequency = audioPacket.Frequency,
format = audioPacket.Format,
voiceLevel = audioPacket.VoiceLevel,
codec = audioPacket.Codec
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Audio/Codecs/IAudioCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
public interface IAudioCodec
{
public string Name { get; }
public int SampleRate { get; }
public int Channels { get; }
public int FrameSize { get; }
Expand Down
4 changes: 3 additions & 1 deletion src/Audio/Codecs/OpusCodec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace RPVoiceChat.Audio
{
public class OpusCodec : IAudioCodec
{
public const string _Name = "Opus";
public string Name { get; } = _Name;
public int SampleRate { get; }
public int Channels { get; }
public int FrameSize { get; }
Expand Down Expand Up @@ -85,7 +87,7 @@ public byte[] Decode(byte[] encodedData)
return decoded.ToArray();
}

private byte[] ShortsToBytes(short[] audio, int offset, int length)
public static byte[] ShortsToBytes(short[] audio, int offset, int length)
{
byte[] byteBuffer = new byte[length * sizeof(short)];
int bytesToCopy = (length - offset) * sizeof(short);
Expand Down
9 changes: 7 additions & 2 deletions src/Audio/Input/MicrophoneManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,20 @@ private AudioData ProcessAudio(byte[] rawSamples)

var amplitude = Math.Sqrt(sampleSquareSum / pcmCount);

byte[] opusEncodedAudio = codec.Encode(pcms);
byte[] audio;
bool shouldEncode = WorldConfig.GetBool("encode-audio");
if (shouldEncode) audio = codec.Encode(pcms);
else audio = OpusCodec.ShortsToBytes(pcms, 0, pcms.Length);
string codecName = shouldEncode ? codec.Name : "None";

return new AudioData()
{
data = opusEncodedAudio,
data = audio,
frequency = Frequency,
format = OutputFormat,
amplitude = amplitude,
voiceLevel = voiceLevel,
codec = codecName
};
}

Expand Down
3 changes: 2 additions & 1 deletion src/Audio/Output/AudioOutputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ public void HandleAudioPacket(AudioPacket packet)

public void HandleAudioPacket(AudioPacket packet, PlayerAudioSource source)
{
string codec = packet.Codec;
int frequency = packet.Frequency;
int channels = AudioUtils.ChannelsPerFormat(packet.Format);
AudioData audioData = AudioData.FromPacket(packet);

if (source.voiceLevel != packet.VoiceLevel)
source.UpdateVoiceLevel(packet.VoiceLevel);
source.UpdatePlayer();
source.UpdateAudioFormat(frequency, channels);
source.UpdateAudioFormat(codec, frequency, channels);
source.EnqueueAudio(audioData, packet.SequenceNumber);
}

Expand Down
10 changes: 7 additions & 3 deletions src/Audio/Output/PlayerAudioSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,14 @@ public void UpdateVoiceLevel(VoiceLevel voiceLevel)
OALW.Source(source, ALSourcef.RolloffFactor, rolloffFactor);
}

public void UpdateAudioFormat(int frequency, int channels)
public void UpdateAudioFormat(string codecName, int frequency, int channels)
{
if (codec?.SampleRate == frequency && codec?.Channels == channels) return;
codec = new OpusCodec(frequency, channels);
if (codec?.Name == codecName && codec?.SampleRate == frequency && codec?.Channels == channels) return;
codec = codecName switch {
OpusCodec._Name => new OpusCodec(frequency, channels),
"None" => null,
_ => null
};
}

public void UpdatePlayer()
Expand Down
2 changes: 2 additions & 0 deletions src/Networking/Packets/AudioPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class AudioPacket : NetworkPacket
public int Frequency { get; set; }
public ALFormat Format { get; set; }
public long SequenceNumber { get; set; }
public string Codec { get; set; }
protected override PacketType Code { get => PacketType.Audio; }

public AudioPacket() { }
Expand All @@ -27,6 +28,7 @@ public AudioPacket(string playerId, AudioData audioData, long sequenceNumber)
Frequency = audioData.frequency;
Format = audioData.format;
SequenceNumber = sequenceNumber;
Codec = audioData.codec;
}
}
}
15 changes: 15 additions & 0 deletions src/RPVoiceChatServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public override void StartServerSide(ICoreServerAPI api)
WorldConfig.Set(VoiceLevel.Talking, WorldConfig.GetInt(VoiceLevel.Talking));
WorldConfig.Set(VoiceLevel.Shouting, WorldConfig.GetInt(VoiceLevel.Shouting));
WorldConfig.Set("force-render-name-tags", WorldConfig.GetBool("force-render-name-tags", true));
WorldConfig.Set("encode-audio", WorldConfig.GetBool("encode-audio", true));

// Register commands
registerCommands();
Expand Down Expand Up @@ -94,6 +95,11 @@ private void registerCommands()
.WithArgs(parsers.Bool("toggle"))
.HandleWith(ToggleVoip)
.EndSub()
.BeginSub("encodingtoggle")
.WithDesc("Toggles audio encoding on the server")
.WithArgs(parsers.Bool("toggle"))
.HandleWith(ToggleEncoding)
.EndSub()
.BeginSub("forcenametags")
.WithDesc(UIUtils.I18n("Command.ForceNameTags.Desc"))
.WithAdditionalInformation(UIUtils.I18n("Command.ForceNameTags.Help"))
Expand All @@ -113,6 +119,15 @@ private TextCommandResult ToggleForceNameTags(TextCommandCallingArgs args)
return TextCommandResult.Success(UIUtils.I18n($"{i18nPrefix}.{stateAsText}"));
}

private TextCommandResult ToggleEncoding(TextCommandCallingArgs args) {
bool state = (bool)args[0];

WorldConfig.Set("encode-audio", state);

string stateAsText = state ? "Enabled" : "Disabled";
return TextCommandResult.Success($"Encoding set to {stateAsText}");
}

private TextCommandResult ToggleVoip(TextCommandCallingArgs args)
{
bool toggle = (bool)args[0];
Expand Down

0 comments on commit db95fac

Please sign in to comment.