示例#1
1
 public G722ChatCodec()
 {
     bitrate = 64000;
     encoderState = new G722CodecState(bitrate, G722Flags.None);
     decoderState = new G722CodecState(bitrate, G722Flags.None);
     codec = new G722Codec();
     recordingFormat = new WaveFormat(16000, 1);
 }
示例#2
0
 public G722ChatCodec()
 {
     this.bitrate = 64000;
     this.encoderState = new G722CodecState(bitrate, G722Flags.None);
     this.decoderState = new G722CodecState(bitrate, G722Flags.None);
     this.codec = new G722Codec();
     this.recordingFormat = new WaveFormat(16000, 1);
 }
示例#3
0
 public G722ChatCodec()
 {
     _bitrate = 64000;
     _encoderState = new G722CodecState(_bitrate, G722Flags.None);
     _decoderState = new G722CodecState(_bitrate, G722Flags.None);
     _codec = new G722Codec();
     RecordFormat = new WaveFormat(16000, 1);
 }
示例#4
0
        private static byte[] EncodeG722(byte[] pcmMonoData, int sampleRate)
        {
            var codec = new G722Codec();
            var state = new G722CodecState(sampleRate, G722Flags.SampleRate8000);

            var wb = new WaveBuffer(pcmMonoData);
            var encodedLength = pcmMonoData.Length / 2;
            var outputBuffer = new byte[encodedLength];
            var length = codec.Encode(state, outputBuffer, wb.ShortBuffer, pcmMonoData.Length / 2);

            if (length != outputBuffer.Length)
            {
                var outputBuffer2 = new byte[length];
                Buffer.BlockCopy(outputBuffer, 0, outputBuffer2, 0, length);
                outputBuffer = outputBuffer2;
            }

            return outputBuffer;
        }
示例#5
0
        private static byte[] DecodeG722(byte[] g722Data, int sampleRate)
        {
            var codec = new G722Codec();
            var state = new G722CodecState(sampleRate, G722Flags.SampleRate8000);

            var decodedLength = g722Data.Length * 2;
            var outputBuffer = new byte[decodedLength];
            var wb = new WaveBuffer(outputBuffer);
            var length = codec.Decode(state, wb.ShortBuffer, g722Data, g722Data.Length) * 2;

            if (length != outputBuffer.Length)
            {
                var outputBuffer2 = new byte[length];
                Buffer.BlockCopy(outputBuffer, 0, outputBuffer2, 0, length);
                outputBuffer = outputBuffer2;
            }

            return outputBuffer;
        }
示例#6
0
        public int Decode(G722CodecState state, short[] outputBuffer, byte[] inputG722Data, int inputLength)
        {
            int result = 0;
            int num    = 0;
            int i      = 0;

            while (i < inputLength)
            {
                int num2;
                if (state.Packed)
                {
                    if (state.InBits < state.BitsPerSample)
                    {
                        state.InBuffer |= (uint)((uint)inputG722Data[i++] << state.InBits);
                        state.InBits   += 8;
                    }
                    num2             = (int)(state.InBuffer & (1u << state.BitsPerSample) - 1u);
                    state.InBuffer >>= state.BitsPerSample;
                    state.InBits    -= state.BitsPerSample;
                }
                else
                {
                    num2 = (int)inputG722Data[i++];
                }
                int num3;
                int num4;
                int num5;
                switch (state.BitsPerSample)
                {
                case 6:
                    num3 = (num2 & 15);
                    num4 = (num2 >> 4 & 3);
                    num5 = G722Codec.qm4[num3];
                    break;

                case 7:
                    num3   = (num2 & 31);
                    num4   = (num2 >> 5 & 3);
                    num5   = G722Codec.qm5[num3];
                    num3 >>= 1;
                    break;

                default:
                    num3   = (num2 & 63);
                    num4   = (num2 >> 6 & 3);
                    num5   = G722Codec.qm6[num3];
                    num3 >>= 2;
                    break;
                }
                num5 = state.Band[0].det * num5 >> 15;
                int num6 = state.Band[0].s + num5;
                if (num6 > 16383)
                {
                    num6 = 16383;
                }
                else if (num6 < -16384)
                {
                    num6 = -16384;
                }
                num5 = G722Codec.qm4[num3];
                int d = state.Band[0].det * num5 >> 15;
                num5  = G722Codec.rl42[num3];
                num3  = state.Band[0].nb * 127 >> 7;
                num3 += G722Codec.wl[num5];
                if (num3 < 0)
                {
                    num3 = 0;
                }
                else if (num3 > 18432)
                {
                    num3 = 18432;
                }
                state.Band[0].nb = num3;
                num3             = (state.Band[0].nb >> 6 & 31);
                num5             = 8 - (state.Band[0].nb >> 11);
                int num7 = (num5 < 0) ? (G722Codec.ilb[num3] << -num5) : (G722Codec.ilb[num3] >> num5);
                state.Band[0].det = num7 << 2;
                G722Codec.Block4(state, 0, d);
                if (!state.EncodeFrom8000Hz)
                {
                    num5 = G722Codec.qm2[num4];
                    int num8 = state.Band[1].det * num5 >> 15;
                    num = num8 + state.Band[1].s;
                    if (num > 16383)
                    {
                        num = 16383;
                    }
                    else if (num < -16384)
                    {
                        num = -16384;
                    }
                    num5  = G722Codec.rh2[num4];
                    num3  = state.Band[1].nb * 127 >> 7;
                    num3 += G722Codec.wh[num5];
                    if (num3 < 0)
                    {
                        num3 = 0;
                    }
                    else if (num3 > 22528)
                    {
                        num3 = 22528;
                    }
                    state.Band[1].nb  = num3;
                    num3              = (state.Band[1].nb >> 6 & 31);
                    num5              = 10 - (state.Band[1].nb >> 11);
                    num7              = ((num5 < 0) ? (G722Codec.ilb[num3] << -num5) : (G722Codec.ilb[num3] >> num5));
                    state.Band[1].det = num7 << 2;
                    G722Codec.Block4(state, 1, num8);
                }
                if (state.ItuTestMode)
                {
                    outputBuffer[result++] = (short)(num6 << 1);
                    outputBuffer[result++] = (short)(num << 1);
                }
                else if (state.EncodeFrom8000Hz)
                {
                    outputBuffer[result++] = (short)(num6 << 1);
                }
                else
                {
                    for (int j = 0; j < 22; j++)
                    {
                        state.QmfSignalHistory[j] = state.QmfSignalHistory[j + 2];
                    }
                    state.QmfSignalHistory[22] = num6 + num;
                    state.QmfSignalHistory[23] = num6 - num;
                    int num9  = 0;
                    int num10 = 0;
                    for (int j = 0; j < 12; j++)
                    {
                        num10 += state.QmfSignalHistory[2 * j] * G722Codec.qmf_coeffs[j];
                        num9  += state.QmfSignalHistory[2 * j + 1] * G722Codec.qmf_coeffs[11 - j];
                    }
                    outputBuffer[result++] = (short)(num9 >> 11);
                    outputBuffer[result++] = (short)(num10 >> 11);
                }
            }
            return(result);
        }
示例#7
0
        public int Encode(G722CodecState state, byte[] outputBuffer, short[] inputBuffer, int inputBufferCount)
        {
            int result = 0;
            int num    = 0;
            int i      = 0;

            while (i < inputBufferCount)
            {
                int num2;
                int j;
                if (state.ItuTestMode)
                {
                    num = (num2 = inputBuffer[i++] >> 1);
                }
                else if (state.EncodeFrom8000Hz)
                {
                    num2 = inputBuffer[i++] >> 1;
                }
                else
                {
                    for (j = 0; j < 22; j++)
                    {
                        state.QmfSignalHistory[j] = state.QmfSignalHistory[j + 2];
                    }
                    state.QmfSignalHistory[22] = (int)inputBuffer[i++];
                    state.QmfSignalHistory[23] = (int)inputBuffer[i++];
                    int num3 = 0;
                    int num4 = 0;
                    for (j = 0; j < 12; j++)
                    {
                        num4 += state.QmfSignalHistory[2 * j] * G722Codec.qmf_coeffs[j];
                        num3 += state.QmfSignalHistory[2 * j + 1] * G722Codec.qmf_coeffs[11 - j];
                    }
                    num2 = num3 + num4 >> 14;
                    num  = num3 - num4 >> 14;
                }
                int num5 = (int)G722Codec.Saturate(num2 - state.Band[0].s);
                int num6 = (num5 >= 0) ? num5 : (-(num5 + 1));
                int num7;
                for (j = 1; j < 30; j++)
                {
                    num7 = G722Codec.q6[j] * state.Band[0].det >> 12;
                    if (num6 < num7)
                    {
                        break;
                    }
                }
                int num8  = (num5 < 0) ? G722Codec.iln[j] : G722Codec.ilp[j];
                int num9  = num8 >> 2;
                int num10 = G722Codec.qm4[num9];
                int d     = state.Band[0].det * num10 >> 15;
                int num11 = G722Codec.rl42[num9];
                num6             = state.Band[0].nb * 127 >> 7;
                state.Band[0].nb = num6 + G722Codec.wl[num11];
                if (state.Band[0].nb < 0)
                {
                    state.Band[0].nb = 0;
                }
                else if (state.Band[0].nb > 18432)
                {
                    state.Band[0].nb = 18432;
                }
                num7  = (state.Band[0].nb >> 6 & 31);
                num10 = 8 - (state.Band[0].nb >> 11);
                int num12 = (num10 < 0) ? (G722Codec.ilb[num7] << -num10) : (G722Codec.ilb[num7] >> num10);
                state.Band[0].det = num12 << 2;
                G722Codec.Block4(state, 0, d);
                int num13;
                if (state.EncodeFrom8000Hz)
                {
                    num13 = (192 | num8) >> 8 - state.BitsPerSample;
                }
                else
                {
                    int num14 = (int)G722Codec.Saturate(num - state.Band[1].s);
                    num6 = ((num14 >= 0) ? num14 : (-(num14 + 1)));
                    num7 = 564 * state.Band[1].det >> 12;
                    int num15 = (num6 >= num7) ? 2 : 1;
                    int num16 = (num14 < 0) ? G722Codec.ihn[num15] : G722Codec.ihp[num15];
                    num10 = G722Codec.qm2[num16];
                    int d2    = state.Band[1].det * num10 >> 15;
                    int num17 = G722Codec.rh2[num16];
                    num6             = state.Band[1].nb * 127 >> 7;
                    state.Band[1].nb = num6 + G722Codec.wh[num17];
                    if (state.Band[1].nb < 0)
                    {
                        state.Band[1].nb = 0;
                    }
                    else if (state.Band[1].nb > 22528)
                    {
                        state.Band[1].nb = 22528;
                    }
                    num7              = (state.Band[1].nb >> 6 & 31);
                    num10             = 10 - (state.Band[1].nb >> 11);
                    num12             = ((num10 < 0) ? (G722Codec.ilb[num7] << -num10) : (G722Codec.ilb[num7] >> num10));
                    state.Band[1].det = num12 << 2;
                    G722Codec.Block4(state, 1, d2);
                    num13 = (num16 << 6 | num8) >> 8 - state.BitsPerSample;
                }
                if (state.Packed)
                {
                    state.OutBuffer |= (uint)((uint)num13 << state.OutBits);
                    state.OutBits   += state.BitsPerSample;
                    if (state.OutBits >= 8)
                    {
                        outputBuffer[result++] = (byte)(state.OutBuffer & 255u);
                        state.OutBits         -= 8;
                        state.OutBuffer      >>= 8;
                    }
                }
                else
                {
                    outputBuffer[result++] = (byte)num13;
                }
            }
            return(result);
        }
示例#8
0
        private static void Block4(G722CodecState s, int band, int d)
        {
            s.Band[band].d[0] = d;
            s.Band[band].r[0] = (int)G722Codec.Saturate(s.Band[band].s + d);
            s.Band[band].p[0] = (int)G722Codec.Saturate(s.Band[band].sz + d);
            for (int i = 0; i < 3; i++)
            {
                s.Band[band].sg[i] = s.Band[band].p[i] >> 15;
            }
            int num  = (int)G722Codec.Saturate(s.Band[band].a[1] << 2);
            int num2 = (s.Band[band].sg[0] == s.Band[band].sg[1]) ? (-num) : num;

            if (num2 > 32767)
            {
                num2 = 32767;
            }
            int num3 = (s.Band[band].sg[0] == s.Band[band].sg[2]) ? 128 : -128;

            num3 += num2 >> 7;
            num3 += s.Band[band].a[2] * 32512 >> 15;
            if (num3 > 12288)
            {
                num3 = 12288;
            }
            else if (num3 < -12288)
            {
                num3 = -12288;
            }
            s.Band[band].ap[2] = num3;
            s.Band[band].sg[0] = s.Band[band].p[0] >> 15;
            s.Band[band].sg[1] = s.Band[band].p[1] >> 15;
            num  = ((s.Band[band].sg[0] == s.Band[band].sg[1]) ? 192 : -192);
            num2 = s.Band[band].a[1] * 32640 >> 15;
            s.Band[band].ap[1] = (int)G722Codec.Saturate(num + num2);
            num3 = (int)G722Codec.Saturate(15360 - s.Band[band].ap[2]);
            if (s.Band[band].ap[1] > num3)
            {
                s.Band[band].ap[1] = num3;
            }
            else if (s.Band[band].ap[1] < -num3)
            {
                s.Band[band].ap[1] = -num3;
            }
            num = ((d == 0) ? 0 : 128);
            s.Band[band].sg[0] = d >> 15;
            for (int i = 1; i < 7; i++)
            {
                s.Band[band].sg[i] = s.Band[band].d[i] >> 15;
                num2 = ((s.Band[band].sg[i] == s.Band[band].sg[0]) ? num : (-num));
                num3 = s.Band[band].b[i] * 32640 >> 15;
                s.Band[band].bp[i] = (int)G722Codec.Saturate(num2 + num3);
            }
            for (int 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 (int 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];
            }
            num             = (int)G722Codec.Saturate(s.Band[band].r[1] + s.Band[band].r[1]);
            num             = s.Band[band].a[1] * num >> 15;
            num2            = (int)G722Codec.Saturate(s.Band[band].r[2] + s.Band[band].r[2]);
            num2            = s.Band[band].a[2] * num2 >> 15;
            s.Band[band].sp = (int)G722Codec.Saturate(num + num2);
            s.Band[band].sz = 0;
            for (int i = 6; i > 0; i--)
            {
                num              = (int)G722Codec.Saturate(s.Band[band].d[i] + s.Band[band].d[i]);
                s.Band[band].sz += s.Band[band].b[i] * num >> 15;
            }
            s.Band[band].sz = (int)G722Codec.Saturate(s.Band[band].sz);
            s.Band[band].s  = (int)G722Codec.Saturate(s.Band[band].sp + s.Band[band].sz);
        }