-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVoiceUtilities.cs
64 lines (58 loc) · 2.55 KB
/
VoiceUtilities.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
namespace OpenVoiceSharp
{
public static class VoiceUtilities
{
/// <summary>
/// Gets the sample size for a frame.
/// </summary>
/// <param name="channels">Set 1 for mono and 2 for stereo</param>
/// <param name="float32">Float32 size is half</param>
/// <returns></returns>
public static int GetSampleSize(int sampleRate, int timeLengthMs, int channels)
=> ((int)(sampleRate * 16f / 8f * (timeLengthMs / 1000f) * channels));
/// <summary>
/// Gets the sample size for a frame.
/// </summary>
/// <param name="channels">Set 1 for mono and 2 for stereo</param>
/// <param name="float32">Float32 size is half</param>
/// <returns></returns>
public static int GetSampleSize(int channels)
=> GetSampleSize(VoiceChatInterface.SampleRate, VoiceChatInterface.FrameLength, channels);
/// <summary>
/// Converts 16 bit PCM data into float 32.
/// Note that the float array must be half the size of the byte array.
/// </summary>
/// <param name="input">The 16 bit PCM data according to your needs.</param>
/// <param name="output">The output data in which the result will be returned.</param>
/// <returns>The 16 bit byte array.</returns>
public static void Convert16BitToFloat(byte[] input, float[] output)
{
int outputIndex = 0;
short sample;
for (int n = 0; n < output.Length; n++)
{
sample = BitConverter.ToInt16(input, n * 2);
output[outputIndex++] = sample / 32768f;
}
}
/// <summary>
/// Converts float 32 PCM data into 16 bit.
/// Note that the byte array must be double the size of the float array.
/// </summary>
/// <param name="input">The float 32 PCM data according to your needs.</param>
/// <param name="output">The output data in which the result will be returned.</param>
/// <returns>The float32 PCM array.</returns>
public static void ConvertFloatTo16Bit(float[] input, byte[] output)
{
int sampleIndex = 0, pcmIndex = 0;
while (sampleIndex < input.Length)
{
short outsample = (short)(input[sampleIndex] * short.MaxValue);
output[pcmIndex] = (byte)(outsample & 0xff);
output[pcmIndex + 1] = (byte)((outsample >> 8) & 0xff);
sampleIndex++;
pcmIndex += 2;
}
}
}
}