Replace naudio.dll with code
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace NAudio.Codecs
|
||||
{
|
||||
/// <summary>
|
||||
/// a-law decoder
|
||||
/// based on code from:
|
||||
/// http://hazelware.luggle.com/tutorials/mulawcompression.html
|
||||
/// </summary>
|
||||
public class ALawDecoder
|
||||
{
|
||||
/// <summary>
|
||||
/// only 512 bytes required, so just use a lookup
|
||||
/// </summary>
|
||||
private static readonly short[] ALawDecompressTable = new short[256]
|
||||
{
|
||||
-5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736,
|
||||
-7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784,
|
||||
-2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368,
|
||||
-3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392,
|
||||
-22016,-20992,-24064,-23040,-17920,-16896,-19968,-18944,
|
||||
-30208,-29184,-32256,-31232,-26112,-25088,-28160,-27136,
|
||||
-11008,-10496,-12032,-11520,-8960, -8448, -9984, -9472,
|
||||
-15104,-14592,-16128,-15616,-13056,-12544,-14080,-13568,
|
||||
-344, -328, -376, -360, -280, -264, -312, -296,
|
||||
-472, -456, -504, -488, -408, -392, -440, -424,
|
||||
-88, -72, -120, -104, -24, -8, -56, -40,
|
||||
-216, -200, -248, -232, -152, -136, -184, -168,
|
||||
-1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184,
|
||||
-1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696,
|
||||
-688, -656, -752, -720, -560, -528, -624, -592,
|
||||
-944, -912, -1008, -976, -816, -784, -880, -848,
|
||||
5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736,
|
||||
7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784,
|
||||
2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368,
|
||||
3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392,
|
||||
22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944,
|
||||
30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136,
|
||||
11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472,
|
||||
15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568,
|
||||
344, 328, 376, 360, 280, 264, 312, 296,
|
||||
472, 456, 504, 488, 408, 392, 440, 424,
|
||||
88, 72, 120, 104, 24, 8, 56, 40,
|
||||
216, 200, 248, 232, 152, 136, 184, 168,
|
||||
1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184,
|
||||
1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696,
|
||||
688, 656, 752, 720, 560, 528, 624, 592,
|
||||
944, 912, 1008, 976, 816, 784, 880, 848
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Converts an a-law encoded byte to a 16 bit linear sample
|
||||
/// </summary>
|
||||
/// <param name="aLaw">a-law encoded byte</param>
|
||||
/// <returns>Linear sample</returns>
|
||||
public static short ALawToLinearSample(byte aLaw)
|
||||
{
|
||||
return ALawDecompressTable[aLaw];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9c7731fd64041bc4ca69a7004c59d6c9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace NAudio.Codecs
|
||||
{
|
||||
/// <summary>
|
||||
/// A-law encoder
|
||||
/// </summary>
|
||||
public static class ALawEncoder
|
||||
{
|
||||
private const int cBias = 0x84;
|
||||
private const int cClip = 32635;
|
||||
private static readonly byte[] ALawCompressTable = new byte[128]
|
||||
{
|
||||
1,1,2,2,3,3,3,3,
|
||||
4,4,4,4,4,4,4,4,
|
||||
5,5,5,5,5,5,5,5,
|
||||
5,5,5,5,5,5,5,5,
|
||||
6,6,6,6,6,6,6,6,
|
||||
6,6,6,6,6,6,6,6,
|
||||
6,6,6,6,6,6,6,6,
|
||||
6,6,6,6,6,6,6,6,
|
||||
7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Encodes a single 16 bit sample to a-law
|
||||
/// </summary>
|
||||
/// <param name="sample">16 bit PCM sample</param>
|
||||
/// <returns>a-law encoded byte</returns>
|
||||
public static byte LinearToALawSample(short sample)
|
||||
{
|
||||
int sign;
|
||||
int exponent;
|
||||
int mantissa;
|
||||
byte compressedByte;
|
||||
|
||||
sign = ((~sample) >> 8) & 0x80;
|
||||
if (sign == 0)
|
||||
sample = (short)-sample;
|
||||
if (sample > cClip)
|
||||
sample = cClip;
|
||||
if (sample >= 256)
|
||||
{
|
||||
exponent = (int)ALawCompressTable[(sample >> 8) & 0x7F];
|
||||
mantissa = (sample >> (exponent + 3)) & 0x0F;
|
||||
compressedByte = (byte)((exponent << 4) | mantissa);
|
||||
}
|
||||
else
|
||||
{
|
||||
compressedByte = (byte)(sample >> 4);
|
||||
}
|
||||
compressedByte ^= (byte)(sign ^ 0x55);
|
||||
return compressedByte;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 49601f5ceda3d7d4abc37b0c09532fb7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,628 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace NAudio.Codecs
|
||||
{
|
||||
/// <summary>
|
||||
/// SpanDSP - a series of DSP components for telephony
|
||||
///
|
||||
/// g722_decode.c - The ITU G.722 codec, decode part.
|
||||
///
|
||||
/// Written by Steve Underwood <steveu@coppice.org>
|
||||
///
|
||||
/// Copyright (C) 2005 Steve Underwood
|
||||
/// Ported to C# by Mark Heath 2011
|
||||
///
|
||||
/// Despite my general liking of the GPL, I place my own contributions
|
||||
/// to this code in the public domain for the benefit of all mankind -
|
||||
/// even the slimy ones who might try to proprietize my work and use it
|
||||
/// to my detriment.
|
||||
///
|
||||
/// Based in part on a single channel G.722 codec which is:
|
||||
/// Copyright (c) CMU 1993
|
||||
/// Computer Science, Speech Group
|
||||
/// Chengxiang Lu and Alex Hauptmann
|
||||
/// </summary>
|
||||
public class G722Codec
|
||||
{
|
||||
/// <summary>
|
||||
/// hard limits to 16 bit samples
|
||||
/// </summary>
|
||||
static short Saturate(int amp)
|
||||
{
|
||||
short amp16;
|
||||
|
||||
// Hopefully this is optimised for the common case - not clipping
|
||||
amp16 = (short)amp;
|
||||
if (amp == amp16)
|
||||
return amp16;
|
||||
if (amp > Int16.MaxValue)
|
||||
return Int16.MaxValue;
|
||||
return Int16.MinValue;
|
||||
}
|
||||
|
||||
static void Block4(G722CodecState s, int band, int d)
|
||||
{
|
||||
int wd1;
|
||||
int wd2;
|
||||
int wd3;
|
||||
int i;
|
||||
|
||||
// Block 4, RECONS
|
||||
s.Band[band].d[0] = d;
|
||||
s.Band[band].r[0] = Saturate(s.Band[band].s + d);
|
||||
|
||||
// Block 4, PARREC
|
||||
s.Band[band].p[0] = Saturate(s.Band[band].sz + d);
|
||||
|
||||
// Block 4, UPPOL2
|
||||
for (i = 0; i < 3; i++)
|
||||
s.Band[band].sg[i] = s.Band[band].p[i] >> 15;
|
||||
wd1 = Saturate(s.Band[band].a[1] << 2);
|
||||
|
||||
wd2 = (s.Band[band].sg[0] == s.Band[band].sg[1]) ? -wd1 : wd1;
|
||||
if (wd2 > 32767)
|
||||
wd2 = 32767;
|
||||
wd3 = (s.Band[band].sg[0] == s.Band[band].sg[2]) ? 128 : -128;
|
||||
wd3 += (wd2 >> 7);
|
||||
wd3 += (s.Band[band].a[2] * 32512) >> 15;
|
||||
if (wd3 > 12288)
|
||||
wd3 = 12288;
|
||||
else if (wd3 < -12288)
|
||||
wd3 = -12288;
|
||||
s.Band[band].ap[2] = wd3;
|
||||
|
||||
// Block 4, UPPOL1
|
||||
s.Band[band].sg[0] = s.Band[band].p[0] >> 15;
|
||||
s.Band[band].sg[1] = s.Band[band].p[1] >> 15;
|
||||
wd1 = (s.Band[band].sg[0] == s.Band[band].sg[1]) ? 192 : -192;
|
||||
wd2 = (s.Band[band].a[1] * 32640) >> 15;
|
||||
|
||||
s.Band[band].ap[1] = Saturate(wd1 + wd2);
|
||||
wd3 = Saturate(15360 - s.Band[band].ap[2]);
|
||||
if (s.Band[band].ap[1] > wd3)
|
||||
s.Band[band].ap[1] = wd3;
|
||||
else if (s.Band[band].ap[1] < -wd3)
|
||||
s.Band[band].ap[1] = -wd3;
|
||||
|
||||
// Block 4, UPZERO
|
||||
wd1 = (d == 0) ? 0 : 128;
|
||||
s.Band[band].sg[0] = d >> 15;
|
||||
for (i = 1; i < 7; i++)
|
||||
{
|
||||
s.Band[band].sg[i] = s.Band[band].d[i] >> 15;
|
||||
wd2 = (s.Band[band].sg[i] == s.Band[band].sg[0]) ? wd1 : -wd1;
|
||||
wd3 = (s.Band[band].b[i] * 32640) >> 15;
|
||||
s.Band[band].bp[i] = Saturate(wd2 + wd3);
|
||||
}
|
||||
|
||||
// Block 4, DELAYA
|
||||
for (i = 6; i > 0; i--)
|
||||
{
|
||||
s.Band[band].d[i] = s.Band[band].d[i - 1];
|
||||
s.Band[band].b[i] = s.Band[band].bp[i];
|
||||
}
|
||||
|
||||
for (i = 2; i > 0; i--)
|
||||
{
|
||||
s.Band[band].r[i] = s.Band[band].r[i - 1];
|
||||
s.Band[band].p[i] = s.Band[band].p[i - 1];
|
||||
s.Band[band].a[i] = s.Band[band].ap[i];
|
||||
}
|
||||
|
||||
// Block 4, FILTEP
|
||||
wd1 = Saturate(s.Band[band].r[1] + s.Band[band].r[1]);
|
||||
wd1 = (s.Band[band].a[1] * wd1) >> 15;
|
||||
wd2 = Saturate(s.Band[band].r[2] + s.Band[band].r[2]);
|
||||
wd2 = (s.Band[band].a[2] * wd2) >> 15;
|
||||
s.Band[band].sp = Saturate(wd1 + wd2);
|
||||
|
||||
// Block 4, FILTEZ
|
||||
s.Band[band].sz = 0;
|
||||
for (i = 6; i > 0; i--)
|
||||
{
|
||||
wd1 = Saturate(s.Band[band].d[i] + s.Band[band].d[i]);
|
||||
s.Band[band].sz += (s.Band[band].b[i] * wd1) >> 15;
|
||||
}
|
||||
s.Band[band].sz = Saturate(s.Band[band].sz);
|
||||
|
||||
// Block 4, PREDIC
|
||||
s.Band[band].s = Saturate(s.Band[band].sp + s.Band[band].sz);
|
||||
}
|
||||
|
||||
static readonly int[] wl = { -60, -30, 58, 172, 334, 538, 1198, 3042 };
|
||||
static readonly int[] rl42 = { 0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0 };
|
||||
static readonly int[] ilb = { 2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383, 2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834, 2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371, 3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008 };
|
||||
static readonly int[] wh = { 0, -214, 798 };
|
||||
static readonly int[] rh2 = { 2, 1, 2, 1 };
|
||||
static readonly int[] qm2 = { -7408, -1616, 7408, 1616 };
|
||||
static readonly int[] qm4 = { 0, -20456, -12896, -8968, -6288, -4240, -2584, -1200, 20456, 12896, 8968, 6288, 4240, 2584, 1200, 0 };
|
||||
static readonly int[] qm5 = { -280, -280, -23352, -17560, -14120, -11664, -9752, -8184, -6864, -5712, -4696, -3784, -2960, -2208, -1520, -880, 23352, 17560, 14120, 11664, 9752, 8184, 6864, 5712, 4696, 3784, 2960, 2208, 1520, 880, 280, -280 };
|
||||
static readonly int[] qm6 = { -136, -136, -136, -136, -24808, -21904, -19008, -16704, -14984, -13512, -12280, -11192, -10232, -9360, -8576, -7856, -7192, -6576, -6000, -5456, -4944, -4464, -4008, -3576, -3168, -2776, -2400, -2032, -1688, -1360, -1040, -728, 24808, 21904, 19008, 16704, 14984, 13512, 12280, 11192, 10232, 9360, 8576, 7856, 7192, 6576, 6000, 5456, 4944, 4464, 4008, 3576, 3168, 2776, 2400, 2032, 1688, 1360, 1040, 728, 432, 136, -432, -136 };
|
||||
static readonly int[] qmf_coeffs = { 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11, };
|
||||
static readonly int[] q6 = { 0, 35, 72, 110, 150, 190, 233, 276, 323, 370, 422, 473, 530, 587, 650, 714, 786, 858, 940, 1023, 1121, 1219, 1339, 1458, 1612, 1765, 1980, 2195, 2557, 2919, 0, 0 };
|
||||
static readonly int[] iln = { 0, 63, 62, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 0 };
|
||||
static readonly int[] ilp = { 0, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 0 };
|
||||
static readonly int[] ihn = { 0, 1, 0 };
|
||||
static readonly int[] ihp = { 0, 3, 2 };
|
||||
|
||||
/// <summary>
|
||||
/// Decodes a buffer of G722
|
||||
/// </summary>
|
||||
/// <param name="state">Codec state</param>
|
||||
/// <param name="outputBuffer">Output buffer (to contain decompressed PCM samples)</param>
|
||||
/// <param name="inputG722Data"></param>
|
||||
/// <param name="inputLength">Number of bytes in input G722 data to decode</param>
|
||||
/// <returns>Number of samples written into output buffer</returns>
|
||||
public int Decode(G722CodecState state, short[] outputBuffer, byte[] inputG722Data, int inputLength)
|
||||
{
|
||||
int dlowt;
|
||||
int rlow;
|
||||
int ihigh;
|
||||
int dhigh;
|
||||
int rhigh;
|
||||
int xout1;
|
||||
int xout2;
|
||||
int wd1;
|
||||
int wd2;
|
||||
int wd3;
|
||||
int code;
|
||||
int outlen;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
outlen = 0;
|
||||
rhigh = 0;
|
||||
for (j = 0; j < inputLength; )
|
||||
{
|
||||
if (state.Packed)
|
||||
{
|
||||
// Unpack the code bits
|
||||
if (state.InBits < state.BitsPerSample)
|
||||
{
|
||||
state.InBuffer |= (uint)(inputG722Data[j++] << state.InBits);
|
||||
state.InBits += 8;
|
||||
}
|
||||
code = (int)state.InBuffer & ((1 << state.BitsPerSample) - 1);
|
||||
state.InBuffer >>= state.BitsPerSample;
|
||||
state.InBits -= state.BitsPerSample;
|
||||
}
|
||||
else
|
||||
{
|
||||
code = inputG722Data[j++];
|
||||
}
|
||||
|
||||
switch (state.BitsPerSample)
|
||||
{
|
||||
default:
|
||||
case 8:
|
||||
wd1 = code & 0x3F;
|
||||
ihigh = (code >> 6) & 0x03;
|
||||
wd2 = qm6[wd1];
|
||||
wd1 >>= 2;
|
||||
break;
|
||||
case 7:
|
||||
wd1 = code & 0x1F;
|
||||
ihigh = (code >> 5) & 0x03;
|
||||
wd2 = qm5[wd1];
|
||||
wd1 >>= 1;
|
||||
break;
|
||||
case 6:
|
||||
wd1 = code & 0x0F;
|
||||
ihigh = (code >> 4) & 0x03;
|
||||
wd2 = qm4[wd1];
|
||||
break;
|
||||
}
|
||||
|
||||
// Block 5L, LOW BAND INVQBL
|
||||
wd2 = (state.Band[0].det * wd2) >> 15;
|
||||
|
||||
// Block 5L, RECONS
|
||||
rlow = state.Band[0].s + wd2;
|
||||
|
||||
// Block 6L, LIMIT
|
||||
if (rlow > 16383)
|
||||
rlow = 16383;
|
||||
else if (rlow < -16384)
|
||||
rlow = -16384;
|
||||
|
||||
// Block 2L, INVQAL
|
||||
wd2 = qm4[wd1];
|
||||
dlowt = (state.Band[0].det * wd2) >> 15;
|
||||
|
||||
// Block 3L, LOGSCL
|
||||
wd2 = rl42[wd1];
|
||||
wd1 = (state.Band[0].nb * 127) >> 7;
|
||||
wd1 += wl[wd2];
|
||||
if (wd1 < 0)
|
||||
wd1 = 0;
|
||||
else if (wd1 > 18432)
|
||||
wd1 = 18432;
|
||||
state.Band[0].nb = wd1;
|
||||
|
||||
// Block 3L, SCALEL
|
||||
wd1 = (state.Band[0].nb >> 6) & 31;
|
||||
wd2 = 8 - (state.Band[0].nb >> 11);
|
||||
wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
|
||||
state.Band[0].det = wd3 << 2;
|
||||
|
||||
Block4(state, 0, dlowt);
|
||||
|
||||
if (!state.EncodeFrom8000Hz)
|
||||
{
|
||||
// Block 2H, INVQAH
|
||||
wd2 = qm2[ihigh];
|
||||
dhigh = (state.Band[1].det * wd2) >> 15;
|
||||
|
||||
// Block 5H, RECONS
|
||||
rhigh = dhigh + state.Band[1].s;
|
||||
|
||||
// Block 6H, LIMIT
|
||||
if (rhigh > 16383)
|
||||
rhigh = 16383;
|
||||
else if (rhigh < -16384)
|
||||
rhigh = -16384;
|
||||
|
||||
// Block 2H, INVQAH
|
||||
wd2 = rh2[ihigh];
|
||||
wd1 = (state.Band[1].nb * 127) >> 7;
|
||||
wd1 += wh[wd2];
|
||||
if (wd1 < 0)
|
||||
wd1 = 0;
|
||||
else if (wd1 > 22528)
|
||||
wd1 = 22528;
|
||||
state.Band[1].nb = wd1;
|
||||
|
||||
// Block 3H, SCALEH
|
||||
wd1 = (state.Band[1].nb >> 6) & 31;
|
||||
wd2 = 10 - (state.Band[1].nb >> 11);
|
||||
wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
|
||||
state.Band[1].det = wd3 << 2;
|
||||
|
||||
Block4(state, 1, dhigh);
|
||||
}
|
||||
|
||||
if (state.ItuTestMode)
|
||||
{
|
||||
outputBuffer[outlen++] = (short)(rlow << 1);
|
||||
outputBuffer[outlen++] = (short)(rhigh << 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (state.EncodeFrom8000Hz)
|
||||
{
|
||||
outputBuffer[outlen++] = (short)(rlow << 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Apply the receive QMF
|
||||
for (i = 0; i < 22; i++)
|
||||
state.QmfSignalHistory[i] = state.QmfSignalHistory[i + 2];
|
||||
state.QmfSignalHistory[22] = rlow + rhigh;
|
||||
state.QmfSignalHistory[23] = rlow - rhigh;
|
||||
|
||||
xout1 = 0;
|
||||
xout2 = 0;
|
||||
for (i = 0; i < 12; i++)
|
||||
{
|
||||
xout2 += state.QmfSignalHistory[2 * i] * qmf_coeffs[i];
|
||||
xout1 += state.QmfSignalHistory[2 * i + 1] * qmf_coeffs[11 - i];
|
||||
}
|
||||
outputBuffer[outlen++] = (short)(xout1 >> 11);
|
||||
outputBuffer[outlen++] = (short)(xout2 >> 11);
|
||||
}
|
||||
}
|
||||
}
|
||||
return outlen;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encodes a buffer of G722
|
||||
/// </summary>
|
||||
/// <param name="state">Codec state</param>
|
||||
/// <param name="outputBuffer">Output buffer (to contain encoded G722)</param>
|
||||
/// <param name="inputBuffer">PCM 16 bit samples to encode</param>
|
||||
/// <param name="inputBufferCount">Number of samples in the input buffer to encode</param>
|
||||
/// <returns>Number of encoded bytes written into output buffer</returns>
|
||||
public int Encode(G722CodecState state, byte[] outputBuffer, short[] inputBuffer, int inputBufferCount)
|
||||
{
|
||||
int dlow;
|
||||
int dhigh;
|
||||
int el;
|
||||
int wd;
|
||||
int wd1;
|
||||
int ril;
|
||||
int wd2;
|
||||
int il4;
|
||||
int ih2;
|
||||
int wd3;
|
||||
int eh;
|
||||
int mih;
|
||||
int i;
|
||||
int j;
|
||||
// Low and high band PCM from the QMF
|
||||
int xlow;
|
||||
int xhigh;
|
||||
int g722_bytes;
|
||||
// Even and odd tap accumulators
|
||||
int sumeven;
|
||||
int sumodd;
|
||||
int ihigh;
|
||||
int ilow;
|
||||
int code;
|
||||
|
||||
g722_bytes = 0;
|
||||
xhigh = 0;
|
||||
for (j = 0; j < inputBufferCount; )
|
||||
{
|
||||
if (state.ItuTestMode)
|
||||
{
|
||||
xlow =
|
||||
xhigh = inputBuffer[j++] >> 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (state.EncodeFrom8000Hz)
|
||||
{
|
||||
xlow = inputBuffer[j++] >> 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Apply the transmit QMF
|
||||
// Shuffle the buffer down
|
||||
for (i = 0; i < 22; i++)
|
||||
state.QmfSignalHistory[i] = state.QmfSignalHistory[i + 2];
|
||||
state.QmfSignalHistory[22] = inputBuffer[j++];
|
||||
state.QmfSignalHistory[23] = inputBuffer[j++];
|
||||
|
||||
// Discard every other QMF output
|
||||
sumeven = 0;
|
||||
sumodd = 0;
|
||||
for (i = 0; i < 12; i++)
|
||||
{
|
||||
sumodd += state.QmfSignalHistory[2 * i] * qmf_coeffs[i];
|
||||
sumeven += state.QmfSignalHistory[2 * i + 1] * qmf_coeffs[11 - i];
|
||||
}
|
||||
xlow = (sumeven + sumodd) >> 14;
|
||||
xhigh = (sumeven - sumodd) >> 14;
|
||||
}
|
||||
}
|
||||
// Block 1L, SUBTRA
|
||||
el = Saturate(xlow - state.Band[0].s);
|
||||
|
||||
// Block 1L, QUANTL
|
||||
wd = (el >= 0) ? el : -(el + 1);
|
||||
|
||||
for (i = 1; i < 30; i++)
|
||||
{
|
||||
wd1 = (q6[i] * state.Band[0].det) >> 12;
|
||||
if (wd < wd1)
|
||||
break;
|
||||
}
|
||||
ilow = (el < 0) ? iln[i] : ilp[i];
|
||||
|
||||
// Block 2L, INVQAL
|
||||
ril = ilow >> 2;
|
||||
wd2 = qm4[ril];
|
||||
dlow = (state.Band[0].det * wd2) >> 15;
|
||||
|
||||
// Block 3L, LOGSCL
|
||||
il4 = rl42[ril];
|
||||
wd = (state.Band[0].nb * 127) >> 7;
|
||||
state.Band[0].nb = wd + wl[il4];
|
||||
if (state.Band[0].nb < 0)
|
||||
state.Band[0].nb = 0;
|
||||
else if (state.Band[0].nb > 18432)
|
||||
state.Band[0].nb = 18432;
|
||||
|
||||
// Block 3L, SCALEL
|
||||
wd1 = (state.Band[0].nb >> 6) & 31;
|
||||
wd2 = 8 - (state.Band[0].nb >> 11);
|
||||
wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
|
||||
state.Band[0].det = wd3 << 2;
|
||||
|
||||
Block4(state, 0, dlow);
|
||||
|
||||
if (state.EncodeFrom8000Hz)
|
||||
{
|
||||
// Just leave the high bits as zero
|
||||
code = (0xC0 | ilow) >> (8 - state.BitsPerSample);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Block 1H, SUBTRA
|
||||
eh = Saturate(xhigh - state.Band[1].s);
|
||||
|
||||
// Block 1H, QUANTH
|
||||
wd = (eh >= 0) ? eh : -(eh + 1);
|
||||
wd1 = (564 * state.Band[1].det) >> 12;
|
||||
mih = (wd >= wd1) ? 2 : 1;
|
||||
ihigh = (eh < 0) ? ihn[mih] : ihp[mih];
|
||||
|
||||
// Block 2H, INVQAH
|
||||
wd2 = qm2[ihigh];
|
||||
dhigh = (state.Band[1].det * wd2) >> 15;
|
||||
|
||||
// Block 3H, LOGSCH
|
||||
ih2 = rh2[ihigh];
|
||||
wd = (state.Band[1].nb * 127) >> 7;
|
||||
state.Band[1].nb = wd + wh[ih2];
|
||||
if (state.Band[1].nb < 0)
|
||||
state.Band[1].nb = 0;
|
||||
else if (state.Band[1].nb > 22528)
|
||||
state.Band[1].nb = 22528;
|
||||
|
||||
// Block 3H, SCALEH
|
||||
wd1 = (state.Band[1].nb >> 6) & 31;
|
||||
wd2 = 10 - (state.Band[1].nb >> 11);
|
||||
wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2);
|
||||
state.Band[1].det = wd3 << 2;
|
||||
|
||||
Block4(state, 1, dhigh);
|
||||
code = ((ihigh << 6) | ilow) >> (8 - state.BitsPerSample);
|
||||
}
|
||||
|
||||
if (state.Packed)
|
||||
{
|
||||
// Pack the code bits
|
||||
state.OutBuffer |= (uint)(code << state.OutBits);
|
||||
state.OutBits += state.BitsPerSample;
|
||||
if (state.OutBits >= 8)
|
||||
{
|
||||
outputBuffer[g722_bytes++] = (byte)(state.OutBuffer & 0xFF);
|
||||
state.OutBits -= 8;
|
||||
state.OutBuffer >>= 8;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
outputBuffer[g722_bytes++] = (byte)code;
|
||||
}
|
||||
}
|
||||
return g722_bytes;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stores state to be used between calls to Encode or Decode
|
||||
/// </summary>
|
||||
public class G722CodecState
|
||||
{
|
||||
/// <summary>
|
||||
/// ITU Test Mode
|
||||
/// TRUE if the operating in the special ITU test mode, with the band split filters disabled.
|
||||
/// </summary>
|
||||
public bool ItuTestMode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// TRUE if the G.722 data is packed
|
||||
/// </summary>
|
||||
public bool Packed { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 8kHz Sampling
|
||||
/// TRUE if encode from 8k samples/second
|
||||
/// </summary>
|
||||
public bool EncodeFrom8000Hz { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Bits Per Sample
|
||||
/// 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps.
|
||||
/// </summary>
|
||||
public int BitsPerSample { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Signal history for the QMF (x)
|
||||
/// </summary>
|
||||
public int[] QmfSignalHistory { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Band
|
||||
/// </summary>
|
||||
public Band[] Band { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// In bit buffer
|
||||
/// </summary>
|
||||
public uint InBuffer { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Number of bits in InBuffer
|
||||
/// </summary>
|
||||
public int InBits { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Out bit buffer
|
||||
/// </summary>
|
||||
public uint OutBuffer { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Number of bits in OutBuffer
|
||||
/// </summary>
|
||||
public int OutBits { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new instance of G722 Codec State for a
|
||||
/// new encode or decode session
|
||||
/// </summary>
|
||||
/// <param name="rate">Bitrate (typically 64000)</param>
|
||||
/// <param name="options">Special options</param>
|
||||
public G722CodecState(int rate, G722Flags options)
|
||||
{
|
||||
this.Band = new Band[2] { new Band(), new Band() };
|
||||
this.QmfSignalHistory = new int[24];
|
||||
this.ItuTestMode = false;
|
||||
|
||||
if (rate == 48000)
|
||||
this.BitsPerSample = 6;
|
||||
else if (rate == 56000)
|
||||
this.BitsPerSample = 7;
|
||||
else if (rate == 64000)
|
||||
this.BitsPerSample = 8;
|
||||
else
|
||||
throw new ArgumentException("Invalid rate, should be 48000, 56000 or 64000");
|
||||
if ((options & G722Flags.SampleRate8000) == G722Flags.SampleRate8000)
|
||||
this.EncodeFrom8000Hz = true;
|
||||
if (((options & G722Flags.Packed) == G722Flags.Packed) && this.BitsPerSample != 8)
|
||||
this.Packed = true;
|
||||
else
|
||||
this.Packed = false;
|
||||
this.Band[0].det = 32;
|
||||
this.Band[1].det = 8;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Band data for G722 Codec
|
||||
/// </summary>
|
||||
public class Band
|
||||
{
|
||||
/// <summary>s</summary>
|
||||
public int s;
|
||||
/// <summary>sp</summary>
|
||||
public int sp;
|
||||
/// <summary>sz</summary>
|
||||
public int sz;
|
||||
/// <summary>r</summary>
|
||||
public int[] r = new int[3];
|
||||
/// <summary>a</summary>
|
||||
public int[] a = new int[3];
|
||||
/// <summary>ap</summary>
|
||||
public int[] ap = new int[3];
|
||||
/// <summary>p</summary>
|
||||
public int[] p = new int[3];
|
||||
/// <summary>d</summary>
|
||||
public int[] d = new int[7];
|
||||
/// <summary>b</summary>
|
||||
public int[] b = new int[7];
|
||||
/// <summary>bp</summary>
|
||||
public int[] bp = new int[7];
|
||||
/// <summary>sg</summary>
|
||||
public int[] sg = new int[7];
|
||||
/// <summary>nb</summary>
|
||||
public int nb;
|
||||
/// <summary>det</summary>
|
||||
public int det;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// G722 Flags
|
||||
/// </summary>
|
||||
[Flags]
|
||||
public enum G722Flags
|
||||
{
|
||||
/// <summary>
|
||||
/// None
|
||||
/// </summary>
|
||||
None = 0,
|
||||
/// <summary>
|
||||
/// Using a G722 sample rate of 8000
|
||||
/// </summary>
|
||||
SampleRate8000 = 0x0001,
|
||||
/// <summary>
|
||||
/// Packed
|
||||
/// </summary>
|
||||
Packed = 0x0002
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c0905d4344a800341aae597e92687bb7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,60 @@
|
||||
namespace NAudio.Codecs
|
||||
{
|
||||
/// <summary>
|
||||
/// mu-law decoder
|
||||
/// based on code from:
|
||||
/// http://hazelware.luggle.com/tutorials/mulawcompression.html
|
||||
/// </summary>
|
||||
public static class MuLawDecoder
|
||||
{
|
||||
/// <summary>
|
||||
/// only 512 bytes required, so just use a lookup
|
||||
/// </summary>
|
||||
private static readonly short[] MuLawDecompressTable = new short[256]
|
||||
{
|
||||
-32124,-31100,-30076,-29052,-28028,-27004,-25980,-24956,
|
||||
-23932,-22908,-21884,-20860,-19836,-18812,-17788,-16764,
|
||||
-15996,-15484,-14972,-14460,-13948,-13436,-12924,-12412,
|
||||
-11900,-11388,-10876,-10364, -9852, -9340, -8828, -8316,
|
||||
-7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140,
|
||||
-5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092,
|
||||
-3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004,
|
||||
-2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980,
|
||||
-1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436,
|
||||
-1372, -1308, -1244, -1180, -1116, -1052, -988, -924,
|
||||
-876, -844, -812, -780, -748, -716, -684, -652,
|
||||
-620, -588, -556, -524, -492, -460, -428, -396,
|
||||
-372, -356, -340, -324, -308, -292, -276, -260,
|
||||
-244, -228, -212, -196, -180, -164, -148, -132,
|
||||
-120, -112, -104, -96, -88, -80, -72, -64,
|
||||
-56, -48, -40, -32, -24, -16, -8, -1,
|
||||
32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956,
|
||||
23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764,
|
||||
15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412,
|
||||
11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316,
|
||||
7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140,
|
||||
5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092,
|
||||
3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004,
|
||||
2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980,
|
||||
1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436,
|
||||
1372, 1308, 1244, 1180, 1116, 1052, 988, 924,
|
||||
876, 844, 812, 780, 748, 716, 684, 652,
|
||||
620, 588, 556, 524, 492, 460, 428, 396,
|
||||
372, 356, 340, 324, 308, 292, 276, 260,
|
||||
244, 228, 212, 196, 180, 164, 148, 132,
|
||||
120, 112, 104, 96, 88, 80, 72, 64,
|
||||
56, 48, 40, 32, 24, 16, 8, 0
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Converts a mu-law encoded byte to a 16 bit linear sample
|
||||
/// </summary>
|
||||
/// <param name="muLaw">mu-law encoded byte</param>
|
||||
/// <returns>Linear sample</returns>
|
||||
public static short MuLawToLinearSample(byte muLaw)
|
||||
{
|
||||
return MuLawDecompressTable[muLaw];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7e8f8f3cc467423488b6e7267d3e29cf
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,53 @@
|
||||
namespace NAudio.Codecs
|
||||
{
|
||||
/// <summary>
|
||||
/// mu-law encoder
|
||||
/// based on code from:
|
||||
/// http://hazelware.luggle.com/tutorials/mulawcompression.html
|
||||
/// </summary>
|
||||
public static class MuLawEncoder
|
||||
{
|
||||
private const int cBias = 0x84;
|
||||
private const int cClip = 32635;
|
||||
|
||||
private static readonly byte[] MuLawCompressTable = new byte[256]
|
||||
{
|
||||
0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,
|
||||
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
|
||||
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
||||
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
|
||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||
6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
|
||||
7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Encodes a single 16 bit sample to mu-law
|
||||
/// </summary>
|
||||
/// <param name="sample">16 bit PCM sample</param>
|
||||
/// <returns>mu-law encoded byte</returns>
|
||||
public static byte LinearToMuLawSample(short sample)
|
||||
{
|
||||
int sign = (sample >> 8) & 0x80;
|
||||
if (sign != 0)
|
||||
sample = (short)-sample;
|
||||
if (sample > cClip)
|
||||
sample = cClip;
|
||||
sample = (short)(sample + cBias);
|
||||
int exponent = (int)MuLawCompressTable[(sample >> 7) & 0xFF];
|
||||
int mantissa = (sample >> (exponent + 3)) & 0x0F;
|
||||
int compressedByte = ~(sign | (exponent << 4) | mantissa);
|
||||
|
||||
return (byte)compressedByte;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fd985b3b8cb2c2544afb6cfdda7804d7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user