25 using System.Runtime.InteropServices;
27 using SiliconStudio.Core;
29 namespace SiliconStudio.
Paradox.Audio.Wave
36 internal class WaveFormatAdpcm : WaveFormat
41 internal WaveFormatAdpcm()
51 public WaveFormatAdpcm(
int rate,
int channels,
int blockAlign = 0)
52 : base(rate, 4, channels)
54 waveFormatTag = WaveFormatEncoding.Adpcm;
55 this.blockAlign = (short)blockAlign;
63 else if (rate <= 22050)
73 SamplesPerBlock = (ushort)(blockAlign * 2 / channels - 12);
74 averageBytesPerSecond = (SampleRate * blockAlign) / SamplesPerBlock;
77 Coefficients1 =
new short[] { 256, 512, 0, 192, 240, 460, 392 };
78 Coefficients2 =
new short[] { 0, -256, 0, 64, 0, -208, -232 };
88 public ushort SamplesPerBlock {
get;
private set; }
96 public short[] Coefficients1 {
get; set; }
104 public short[] Coefficients2 {
get; set; }
106 protected unsafe
override IntPtr MarshalToPtr()
108 var result = Marshal.AllocHGlobal(Utilities.SizeOf<WaveFormat.__Native>() +
sizeof(
int) +
sizeof(int) * Coefficients1.Length);
109 __MarshalTo(ref *(__Native*)result);
113 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 2)]
114 internal new struct __Native
116 public WaveFormat.__Native waveFormat;
117 public ushort samplesPerBlock;
118 public ushort numberOfCoefficients;
119 public short coefficients;
122 internal void __MarshalFree()
124 waveFormat.__MarshalFree();
128 internal unsafe
void __MarshalFrom(ref __Native @ref)
130 __MarshalFrom(ref @ref.waveFormat);
131 SamplesPerBlock = @ref.samplesPerBlock;
132 Coefficients1 =
new short[@ref.numberOfCoefficients];
133 Coefficients2 =
new short[@ref.numberOfCoefficients];
134 if (@ref.numberOfCoefficients > 7)
135 throw new InvalidOperationException(
"Unable to read Adpcm format. Too may coefficients (max 7)");
136 fixed (
short* pCoefs = &@ref.coefficients)
137 for (
int i = 0; i < @ref.numberOfCoefficients; i++)
139 Coefficients1[i] = pCoefs[i * 2];
140 Coefficients2[i] = pCoefs[i * 2 + 1];
142 extraSize = (short)(
sizeof(
int) +
sizeof(int) * @ref.numberOfCoefficients);
145 private unsafe
void __MarshalTo(ref __Native @ref)
147 if (Coefficients1.Length > 7)
148 throw new InvalidOperationException(
"Unable to encode Adpcm format. Too may coefficients (max 7)");
150 extraSize = (short)(
sizeof(
int) +
sizeof(int) * Coefficients1.Length);
151 __MarshalTo(ref @ref.waveFormat);
152 @ref.samplesPerBlock = SamplesPerBlock;
153 @ref.numberOfCoefficients = (ushort)Coefficients1.Length;
154 fixed (
short* pCoefs = &@ref.coefficients)
155 for (
int i = 0; i < @ref.numberOfCoefficients; i++)
157 pCoefs[i * 2] = Coefficients1[i];
158 pCoefs[i * 2 + 1] = Coefficients2[i];