Skip to content

Commit 1592a0a

Browse files
committed
Changes to localization handling on client side. Will need to do more cleanup.
1 parent e545f8d commit 1592a0a

37 files changed

+163
-155
lines changed

Tooling/README.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

Tooling/adbStarter.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

Tooling/adbStopper.sh

Lines changed: 0 additions & 1 deletion
This file was deleted.

VoiceCraft.Client/VoiceCraft.Client.Android/Audio/AudioPlayer.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Android.Media;
55
using VoiceCraft.Core;
66
using VoiceCraft.Core.Interfaces;
7+
using VoiceCraft.Core.Locales;
78
using AudioFormat = Android.Media.AudioFormat;
89

910
namespace VoiceCraft.Client.Android.Audio;
@@ -38,7 +39,8 @@ public AudioPlayer(AudioManager audioManager, int sampleRate, int channels, Core
3839

3940
public AudioContentType ContentType { get; set; } = AudioContentType.Music;
4041

41-
public int SessionId => _nativePlayer?.AudioSessionId ?? throw new InvalidOperationException(Locales.Locales.Audio_Player_Init);
42+
public int SessionId => _nativePlayer?.AudioSessionId ??
43+
throw new InvalidOperationException(Localizer.Get("Audio.Player.Init"));
4244

4345
//Public Properties
4446
public int SampleRate
@@ -47,7 +49,8 @@ public int SampleRate
4749
set
4850
{
4951
if (value < 0)
50-
throw new ArgumentOutOfRangeException(nameof(value), value, "Sample rate must be greater than or equal to zero!");
52+
throw new ArgumentOutOfRangeException(nameof(value), value,
53+
"Sample rate must be greater than or equal to zero!");
5154

5255
_sampleRate = value;
5356
}
@@ -59,7 +62,8 @@ public int Channels
5962
set
6063
{
6164
if (value < 1)
62-
throw new ArgumentOutOfRangeException(nameof(value), value, "Channels must be greater than or equal to one!");
65+
throw new ArgumentOutOfRangeException(nameof(value), value,
66+
"Channels must be greater than or equal to one!");
6367

6468
_channels = value;
6569
}
@@ -87,7 +91,8 @@ public int BufferMilliseconds
8791
set
8892
{
8993
if (value < 0)
90-
throw new ArgumentOutOfRangeException(nameof(value), value, "Buffer milliseconds must be greater than or equal to zero!");
94+
throw new ArgumentOutOfRangeException(nameof(value), value,
95+
"Buffer milliseconds must be greater than or equal to zero!");
9196

9297
_bufferMilliseconds = value;
9398
}
@@ -110,7 +115,7 @@ public void Initialize(Func<byte[], int, int> playerCallback)
110115

111116
//Check if already playing.
112117
if (PlaybackState != PlaybackState.Stopped)
113-
throw new InvalidOperationException(Locales.Locales.Audio_Player_InitFailed);
118+
throw new InvalidOperationException(Localizer.Get("Audio.Player.InitFailed"));
114119

115120
//Cleanup previous player.
116121
CleanupPlayer();
@@ -132,37 +137,41 @@ public void Initialize(Func<byte[], int, int> playerCallback)
132137
2 => ChannelOut.Stereo,
133138
_ => throw new NotSupportedException()
134139
};
135-
140+
136141
//Determine the buffer size
137142
var blockAlign = Channels * (BitDepth / 8);
138143
var bytesPerSecond = _sampleRate * blockAlign;
139144

140145
var audioAttributes = new AudioAttributes.Builder().SetUsage(Usage)?.SetContentType(ContentType)?.Build();
141-
var audioFormat = new AudioFormat.Builder().SetEncoding(encoding)?.SetSampleRate(SampleRate)?.SetChannelMask(channelMask).Build();
146+
var audioFormat = new AudioFormat.Builder().SetEncoding(encoding)?.SetSampleRate(SampleRate)
147+
?.SetChannelMask(channelMask).Build();
142148

143149
if (audioAttributes == null || audioFormat == null)
144150
throw new InvalidOperationException();
145151

146152
//Calculate total buffer bytes.
147153
var totalBufferBytes = (int)(bytesPerSecond / 1000.0 * BufferMilliseconds);
148-
if (totalBufferBytes % blockAlign != 0) totalBufferBytes = totalBufferBytes + blockAlign - totalBufferBytes % blockAlign;
149-
totalBufferBytes = Math.Max(totalBufferBytes, AudioTrack.GetMinBufferSize(SampleRate, channelMask, encoding));
154+
if (totalBufferBytes % blockAlign != 0)
155+
totalBufferBytes = totalBufferBytes + blockAlign - totalBufferBytes % blockAlign;
156+
totalBufferBytes = Math.Max(totalBufferBytes,
157+
AudioTrack.GetMinBufferSize(SampleRate, channelMask, encoding));
150158

151159
_nativePlayer = new AudioTrack.Builder().SetAudioAttributes(audioAttributes).SetAudioFormat(audioFormat)
152160
.SetBufferSizeInBytes(totalBufferBytes).SetTransferMode(AudioTrackMode.Stream).Build();
153-
161+
154162
_bufferBytes = (_nativePlayer.BufferSizeInFrames + NumberOfBuffers - 1) / NumberOfBuffers * blockAlign;
155163
_bufferBytes = (_bufferBytes + 3) & ~3;
156164
_byteBuffer = new byte[_bufferBytes];
157165
_shortBuffer = new short[_bufferBytes / sizeof(short)];
158166
_floatBuffer = new float[_bufferBytes / sizeof(float)];
159-
167+
160168
if (_nativePlayer.State != AudioTrackState.Initialized)
161-
throw new InvalidOperationException(Locales.Locales.Audio_Player_InitFailed);
169+
throw new InvalidOperationException(Localizer.Get("Audio.Player.InitFailed"));
162170

163171
_nativePlayer.SetVolume(1.0f);
164172
var selectedDevice = _audioManager.GetDevices(GetDevicesTargets.Outputs)
165-
?.FirstOrDefault(x => $"{x.ProductName.Truncate(8)} - {x.Type}" == SelectedDevice);
173+
?.FirstOrDefault(x =>
174+
x.ProductName != null && $"{x.ProductName.Truncate(8)} - {x.Type}" == SelectedDevice);
166175
_nativePlayer.SetPreferredDevice(selectedDevice);
167176
}
168177
catch
@@ -295,7 +304,7 @@ private void ThrowIfDisposed()
295304
private void ThrowIfNotInitialized()
296305
{
297306
if (_nativePlayer == null)
298-
throw new InvalidOperationException(Locales.Locales.Audio_Player_Init);
307+
throw new InvalidOperationException(Localizer.Get("Audio.Player.Init"));
299308
}
300309

301310
private void Resume()
@@ -385,7 +394,7 @@ private void PlaybackLogic()
385394
throw new ArgumentOutOfRangeException();
386395
}
387396
}
388-
397+
389398
_nativePlayer?.Flush();
390399
}
391400

VoiceCraft.Client/VoiceCraft.Client.Android/Audio/AudioRecorder.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Android.Media;
55
using VoiceCraft.Core;
66
using VoiceCraft.Core.Interfaces;
7+
using VoiceCraft.Core.Locales;
78
using AudioFormat = VoiceCraft.Core.AudioFormat;
89

910
namespace VoiceCraft.Client.Android.Audio;
@@ -34,7 +35,7 @@ public AudioRecorder(AudioManager audioManager, int sampleRate, int channels, Au
3435

3536
public AudioSource AudioSource { get; set; } = AudioSource.VoiceCommunication;
3637

37-
public int SessionId => _nativeRecorder?.AudioSessionId ?? throw new InvalidOperationException(Locales.Locales.Audio_Recorder_Init);
38+
public int SessionId => _nativeRecorder?.AudioSessionId ?? throw new InvalidOperationException(Localizer.Get("Audio.Player.Init"));
3839

3940
//Public Properties
4041
public int SampleRate
@@ -106,7 +107,7 @@ public void Initialize()
106107
ThrowIfDisposed();
107108

108109
if (CaptureState != CaptureState.Stopped)
109-
throw new InvalidOperationException(Locales.Locales.Audio_Recorder_InitFailed);
110+
throw new InvalidOperationException(Localizer.Get("Audio.Player.InitFailed"));
110111

111112
//Cleanup previous recorder.
112113
CleanupRecorder();
@@ -141,7 +142,7 @@ public void Initialize()
141142
//Create the AudioRecord Object.
142143
_nativeRecorder = new AudioRecord(AudioSource, SampleRate, channelMask, encoding, _bufferBytes);
143144
if (_nativeRecorder.State != State.Initialized)
144-
throw new InvalidOperationException(Locales.Locales.Audio_Recorder_InitFailed);
145+
throw new InvalidOperationException(Localizer.Get("Audio.Player.InitFailed"));
145146

146147
var device = _audioManager.GetDevices(GetDevicesTargets.Inputs)
147148
?.FirstOrDefault(x => $"{x.ProductName.Truncate(8)} - {x.Type}" == SelectedDevice);
@@ -240,7 +241,7 @@ private void ThrowIfDisposed()
240241
private void ThrowIfNotInitialized()
241242
{
242243
if (_nativeRecorder == null)
243-
throw new InvalidOperationException(Locales.Locales.Audio_Recorder_Init);
244+
throw new InvalidOperationException(Localizer.Get("Audio.Player.Init"));
244245
}
245246

246247
private void InvokeDataAvailable(byte[] buffer, int bytesRecorded)

VoiceCraft.Client/VoiceCraft.Client.Android/Audio/NativeAutomaticGainController.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Android.Media.Audiofx;
33
using VoiceCraft.Core.Interfaces;
4+
using VoiceCraft.Core.Locales;
45

56
namespace VoiceCraft.Client.Android.Audio;
67

@@ -16,13 +17,13 @@ public void Initialize(IAudioRecorder recorder)
1617
ThrowIfDisposed();
1718

1819
if (recorder is not AudioRecorder audioRecorder)
19-
throw new InvalidOperationException(Locales.Locales.Audio_AGC_InitFailed);
20+
throw new InvalidOperationException(Localizer.Get("Audio.AGC.InitFailed"));
2021

2122
CleanupGainController();
2223
_gainController = AutomaticGainControl.Create(audioRecorder.SessionId);
2324

2425
if (_gainController == null || _gainController.SetEnabled(true) != AudioEffectStatus.Success)
25-
throw new InvalidOperationException(Locales.Locales.Audio_AEC_InitFailed);
26+
throw new InvalidOperationException(Localizer.Get("Audio.AGC.InitFailed"));
2627
}
2728

2829
public void Process(byte[] buffer)
@@ -63,7 +64,7 @@ private void ThrowIfDisposed()
6364
private void ThrowIfNotInitialized()
6465
{
6566
if (_gainController == null)
66-
throw new InvalidOperationException(Locales.Locales.Audio_AGC_Init);
67+
throw new InvalidOperationException(Localizer.Get("Audio.AGC.Init"));
6768
}
6869

6970
private void Dispose(bool disposing)

VoiceCraft.Client/VoiceCraft.Client.Android/Audio/NativeDenoiser.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Android.Media.Audiofx;
33
using VoiceCraft.Core.Interfaces;
4+
using VoiceCraft.Core.Locales;
45

56
namespace VoiceCraft.Client.Android.Audio;
67

@@ -15,13 +16,13 @@ public void Initialize(IAudioRecorder recorder)
1516
ThrowIfDisposed();
1617

1718
if (recorder is not AudioRecorder audioRecorder)
18-
throw new InvalidOperationException(Locales.Locales.Audio_DN_InitFailed);
19+
throw new InvalidOperationException(Localizer.Get("Audio.DN.InitFailed"));
1920

2021
CleanupDenoiser();
2122
_denoiser = NoiseSuppressor.Create(audioRecorder.SessionId);
2223

2324
if (_denoiser == null || _denoiser.SetEnabled(true) != AudioEffectStatus.Success)
24-
throw new InvalidOperationException(Locales.Locales.Audio_DN_InitFailed);
25+
throw new InvalidOperationException(Localizer.Get("Audio.DN.InitFailed"));
2526
}
2627

2728
public void Denoise(byte[] buffer)
@@ -62,7 +63,7 @@ private void ThrowIfDisposed()
6263
private void ThrowIfNotInitialized()
6364
{
6465
if (_denoiser == null)
65-
throw new InvalidOperationException(Locales.Locales.Audio_DN_Init);
66+
throw new InvalidOperationException(Localizer.Get("Audio.DN.Init"));
6667
}
6768

6869
private void Dispose(bool disposing)

VoiceCraft.Client/VoiceCraft.Client.Android/Audio/NativeEchoCanceler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using Android.Media.Audiofx;
33
using VoiceCraft.Core.Interfaces;
4+
using VoiceCraft.Core.Locales;
45

56
namespace VoiceCraft.Client.Android.Audio;
67

@@ -17,13 +18,13 @@ public void
1718
ThrowIfDisposed();
1819

1920
if (recorder is not AudioRecorder audioRecorder)
20-
throw new InvalidOperationException(Locales.Locales.Audio_AEC_InitFailed);
21+
throw new InvalidOperationException(Localizer.Get("Audio.AEC.InitFailed"));
2122

2223
CleanupEchoCanceler();
2324
_echoCanceler = AcousticEchoCanceler.Create(audioRecorder.SessionId);
2425

2526
if (_echoCanceler == null || _echoCanceler.SetEnabled(true) != AudioEffectStatus.Success)
26-
throw new InvalidOperationException(Locales.Locales.Audio_AEC_InitFailed);
27+
throw new InvalidOperationException(Localizer.Get("Audio.AEC.InitFailed"));
2728
}
2829

2930
public void EchoPlayback(Span<byte> buffer, int count)
@@ -75,7 +76,7 @@ private void ThrowIfDisposed()
7576
private void ThrowIfNotInitialized()
7677
{
7778
if (_echoCanceler == null)
78-
throw new InvalidOperationException(Locales.Locales.Audio_AEC_Init);
79+
throw new InvalidOperationException(Localizer.Get("Audio.AEC.Init"));
7980
}
8081

8182
private void Dispose(bool disposing)

VoiceCraft.Client/VoiceCraft.Client.Android/Audio/SpeexDspAutomaticGainController.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using SpeexDSPSharp.Core;
33
using VoiceCraft.Core.Interfaces;
4+
using VoiceCraft.Core.Locales;
45

56
namespace VoiceCraft.Client.Android.Audio;
67

@@ -15,7 +16,7 @@ public void Initialize(IAudioRecorder recorder)
1516
ThrowIfDisposed();
1617

1718
if (recorder.Channels != 1)
18-
throw new InvalidOperationException(Locales.Locales.Audio_AGC_InitFailed);
19+
throw new InvalidOperationException(Localizer.Get("Audio.AGC.InitFailed"));
1920

2021
CleanupGainController();
2122

@@ -70,7 +71,7 @@ private void ThrowIfDisposed()
7071
private void ThrowIfNotInitialized()
7172
{
7273
if (_gainController == null)
73-
throw new InvalidOperationException(Locales.Locales.Audio_AGC_Init);
74+
throw new InvalidOperationException(Localizer.Get("Audio.AGC.Init"));
7475
}
7576

7677
private void Dispose(bool disposing)

VoiceCraft.Client/VoiceCraft.Client.Android/Audio/SpeexDspDenoiser.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using SpeexDSPSharp.Core;
33
using VoiceCraft.Core.Interfaces;
4+
using VoiceCraft.Core.Locales;
45

56
namespace VoiceCraft.Client.Android.Audio;
67

@@ -15,7 +16,7 @@ public void Initialize(IAudioRecorder recorder)
1516
ThrowIfDisposed();
1617

1718
if (recorder.Channels != 1)
18-
throw new InvalidOperationException(Locales.Locales.Audio_DN_InitFailed);
19+
throw new InvalidOperationException(Localizer.Get("Audio.DN.InitFailed"));
1920

2021
CleanupDenoiser();
2122

@@ -68,7 +69,7 @@ private void ThrowIfDisposed()
6869
private void ThrowIfNotInitialized()
6970
{
7071
if (_denoiser == null)
71-
throw new InvalidOperationException(Locales.Locales.Audio_DN_Init);
72+
throw new InvalidOperationException(Localizer.Get("Audio.DN.Init"));
7273
}
7374

7475
private void Dispose(bool disposing)

0 commit comments

Comments
 (0)