/
Celt.cs
189 lines (167 loc) · 8.34 KB
/
Celt.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
#pragma warning disable SA1300 // Element must begin with upper-case letter
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace Xenko.Audio
{
/// <summary>
/// Wrapper around Celt
/// </summary>
internal class Celt : IDisposable
{
public int SampleRate { get; set; }
public int BufferSize { get; set; }
public int Channels { get; set; }
private IntPtr celtPtr;
static Celt()
{
NativeInvoke.PreLoad();
}
/// <summary>
/// Initialize the Celt encoder/decoder
/// </summary>
/// <param name="sampleRate">Required sample rate</param>
/// <param name="bufferSize">Required buffer size</param>
/// <param name="channels">Required channels</param>
/// <param name="decoderOnly">If we desire only to decode set this to true</param>
public Celt(int sampleRate, int bufferSize, int channels, bool decoderOnly)
{
SampleRate = sampleRate;
BufferSize = bufferSize;
Channels = channels;
celtPtr = xnCeltCreate(sampleRate, bufferSize, channels, decoderOnly);
if (celtPtr == IntPtr.Zero)
{
throw new Exception("Failed to create an instance of the celt encoder/decoder.");
}
}
/// <summary>
/// Dispose the Celt encoder/decoder
/// Do not call Encode or Decode after disposal!
/// </summary>
public void Dispose()
{
if (celtPtr != IntPtr.Zero)
{
xnCeltDestroy(celtPtr);
celtPtr = IntPtr.Zero;
}
}
/// <summary>
/// Decodes compressed celt data into PCM 16 bit shorts
/// </summary>
/// <param name="inputBuffer">The input buffer</param>
/// <param name="inputBufferSize">The size of the valid bytes in the input buffer</param>
/// <param name="outputSamples">The output buffer, the size of frames should be the same amount that is contained in the input buffer</param>
/// <returns></returns>
public unsafe int Decode(byte[] inputBuffer, int inputBufferSize, short[] outputSamples)
{
fixed (short* samplesPtr = outputSamples)
fixed (byte* bufferPtr = inputBuffer)
{
return xnCeltDecodeShort(celtPtr, bufferPtr, inputBufferSize, samplesPtr, outputSamples.Length / Channels);
}
}
/// <summary>
/// Decodes compressed celt data into PCM 16 bit shorts
/// </summary>
/// <param name="inputBuffer">The input buffer</param>
/// <param name="inputBufferSize">The size of the valid bytes in the input buffer</param>
/// <param name="outputSamples">The output buffer, the size of frames should be the same amount that is contained in the input buffer</param>
/// <returns></returns>
public unsafe int Decode(byte[] inputBuffer, int inputBufferSize, short* outputSamples)
{
fixed (byte* bufferPtr = inputBuffer)
{
return xnCeltDecodeShort(celtPtr, bufferPtr, inputBufferSize, outputSamples, BufferSize);
}
}
/// <summary>
/// Reset decoder state.
/// </summary>
public void ResetDecoder()
{
xnCeltResetDecoder(celtPtr);
}
/// <summary>
/// Gets the delay between encoder and decoder (in number of samples). This should be skipped at the beginning of a decoded stream.
/// </summary>
/// <returns></returns>
public int GetDecoderSampleDelay()
{
var delay = 0;
if (xnCeltGetDecoderSampleDelay(celtPtr, ref delay) != 0)
delay = 0;
return delay;
}
/// <summary>
/// Encode PCM audio into celt compressed format
/// </summary>
/// <param name="audioSamples">A buffer containing interleaved channels (as from constructor channels) and samples (can be any number of samples)</param>
/// <param name="outputBuffer">An array of bytes, the size of the array will be the max possible size of the compressed packet</param>
/// <returns></returns>
public unsafe int Encode(short[] audioSamples, byte[] outputBuffer)
{
fixed (short* samplesPtr = audioSamples)
fixed (byte* bufferPtr = outputBuffer)
{
return xnCeltEncodeShort(celtPtr, samplesPtr, audioSamples.Length / Channels, bufferPtr, outputBuffer.Length);
}
}
/// <summary>
/// Decodes compressed celt data into PCM 32 bit floats
/// </summary>
/// <param name="inputBuffer">The input buffer</param>
/// <param name="inputBufferSize">The size of the valid bytes in the input buffer</param>
/// <param name="outputSamples">The output buffer, the size of frames should be the same amount that is contained in the input buffer</param>
/// <returns></returns>
public unsafe int Decode(byte[] inputBuffer, int inputBufferSize, float[] outputSamples)
{
fixed (float* samplesPtr = outputSamples)
fixed (byte* bufferPtr = inputBuffer)
{
return xnCeltDecodeFloat(celtPtr, bufferPtr, inputBufferSize, samplesPtr, outputSamples.Length / Channels);
}
}
/// <summary>
/// Encode PCM audio into celt compressed format
/// </summary>
/// <param name="audioSamples">A buffer containing interleaved channels (as from constructor channels) and samples (can be any number of samples)</param>
/// <param name="outputBuffer">An array of bytes, the size of the array will be the max possible size of the compressed packet</param>
/// <returns></returns>
public unsafe int Encode(float[] audioSamples, byte[] outputBuffer)
{
fixed (float* samplesPtr = audioSamples)
fixed (byte* bufferPtr = outputBuffer)
{
return xnCeltEncodeFloat(celtPtr, samplesPtr, audioSamples.Length / Channels, bufferPtr, outputBuffer.Length);
}
}
[SuppressUnmanagedCodeSecurity]
[DllImport(NativeInvoke.Library, CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr xnCeltCreate(int sampleRate, int bufferSize, int channels, bool decoderOnly);
[SuppressUnmanagedCodeSecurity]
[DllImport(NativeInvoke.Library, CallingConvention = CallingConvention.Cdecl)]
private static extern void xnCeltDestroy(IntPtr celt);
[SuppressUnmanagedCodeSecurity]
[DllImport(NativeInvoke.Library, CallingConvention = CallingConvention.Cdecl)]
private static extern int xnCeltResetDecoder(IntPtr celt);
[SuppressUnmanagedCodeSecurity]
[DllImport(NativeInvoke.Library, CallingConvention = CallingConvention.Cdecl)]
private static extern int xnCeltGetDecoderSampleDelay(IntPtr celt, ref int delay);
[SuppressUnmanagedCodeSecurity]
[DllImport(NativeInvoke.Library, CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe int xnCeltEncodeFloat(IntPtr celt, float* inputSamples, int numberOfInputSamples, byte* outputBuffer, int maxOutputSize);
[SuppressUnmanagedCodeSecurity]
[DllImport(NativeInvoke.Library, CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe int xnCeltDecodeFloat(IntPtr celt, byte* inputBuffer, int inputBufferSize, float* outputBuffer, int numberOfOutputSamples);
[SuppressUnmanagedCodeSecurity]
[DllImport(NativeInvoke.Library, CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe int xnCeltEncodeShort(IntPtr celt, short* inputSamples, int numberOfInputSamples, byte* outputBuffer, int maxOutputSize);
[SuppressUnmanagedCodeSecurity]
[DllImport(NativeInvoke.Library, CallingConvention = CallingConvention.Cdecl)]
private static extern unsafe int xnCeltDecodeShort(IntPtr celt, byte* inputBuffer, int inputBufferSize, short* outputBuffer, int numberOfOutputSamples);
}
}