Beispiel #1
0
        public byte[] Encode(short[] data, BandMode mode)
        {
            this.Init();
            int length = (int)data.Length / 2;

            if (length % 2 != 0)
            {
                length++;
            }
            byte[] num  = USpeakPoolUtils.GetByte(length);
            int    num1 = 0;

            while (num1 < (int)num.Length)
            {
                if (num1 * 2 < (int)data.Length)
                {
                    byte num2 = this.ADPCM_Encode(data[num1 * 2]);
                    byte num3 = 0;
                    if (num1 * 2 + 1 < (int)data.Length)
                    {
                        num3 = this.ADPCM_Encode(data[num1 * 2 + 1]);
                    }
                    num[num1] = (byte)(num3 << 4 | num2);
                    num1++;
                }
                else
                {
                    break;
                }
            }
            return(num);
        }
Beispiel #2
0
        public byte[] Encode(short[] data, BandMode mode)
        {
            Init();
            int len = data.Length / 2;

            if (len % 2 != 0)
            {
                len++;
            }
            byte[] temp = USpeakPoolUtils.GetByte(len);
            for (int i = 0; i < temp.Length; i++)
            {
                if ((i * 2) >= data.Length)
                {
                    break;
                }

                byte a = ADPCM_Encode(data[i * 2]);
                byte b = 0;
                if (((i * 2) + 1) < data.Length)
                {
                    b = ADPCM_Encode(data[(i * 2) + 1]);
                }
                byte c = (byte)((b << 4) | a);

                temp[i] = c;
            }
            return(temp);
        }
Beispiel #3
0
        public byte[] Encode(short[] data, BandMode mode)
        {
            this.Init();
            int length = data.Length / 2;

            if ((length % 2) != 0)
            {
                length++;
            }
            byte[] @byte = USpeakPoolUtils.GetByte(length);
            for (int i = 0; i < @byte.Length; i++)
            {
                if ((i * 2) >= data.Length)
                {
                    return(@byte);
                }
                byte num3 = this.ADPCM_Encode(data[i * 2]);
                byte num4 = 0;
                if (((i * 2) + 1) < data.Length)
                {
                    num4 = this.ADPCM_Encode(data[(i * 2) + 1]);
                }
                @byte[i] = (byte)((num4 << 4) | num3);
            }
            return(@byte);
        }
Beispiel #4
0
        // Token: 0x06004C1C RID: 19484 RVA: 0x00197518 File Offset: 0x00195918
        public byte[] Encode(short[] data, BandMode mode)
        {
            this.Init();
            int num = data.Length / 2;

            if (num % 2 != 0)
            {
                num++;
            }
            byte[] @byte = USpeakPoolUtils.GetByte(num);
            for (int i = 0; i < @byte.Length; i++)
            {
                if (i * 2 >= data.Length)
                {
                    break;
                }
                byte b  = this.ADPCM_Encode(data[i * 2]);
                byte b2 = 0;
                if (i * 2 + 1 < data.Length)
                {
                    b2 = this.ADPCM_Encode(data[i * 2 + 1]);
                }
                byte b3 = (byte)((int)b2 << 4 | (int)b);
                @byte[i] = b3;
            }
            return(@byte);
        }
        /// <summary>
        /// Decompress the given encoded audio data
        /// </summary>
        /// <param name="data">The encoded audio</param>
        /// <param name="samples">The number of encoded samples</param>
        /// <param name="channels">The number of channels</param>
        /// <param name="threeD">Whether the audio is 3D</param>
        /// <param name="mode">The bandwidth mode used to encode the data</param>
        /// <param name="Codec">The codec to decode the data with</param>
        /// <param name="gain">The gain to apply to the decoded audio</param>
        /// <returns>32bit raw audio data</returns>
        public static float[] DecompressAudio(byte[] data, int samples, int channels, bool threeD, BandMode mode, ICodec Codec, float gain)
        {
            int frequency = 4000;

            if (mode == BandMode.Narrow)
            {
                frequency = 8000;
            }
            else if (mode == BandMode.Wide)
            {
                frequency = 16000;
            }

            byte[] d;
            //d = unzip( data );
            d = data;

            short[] pcm = Codec.Decode(d, mode);

            tmp.Clear();
            tmp.AddRange(pcm);

            USpeakPoolUtils.Return(pcm);

            return(USpeakAudioClipConverter.ShortsToAudioData(tmp.ToArray(), channels, frequency, threeD, gain));
        }
        /// <summary>
        /// Convert audio data to a short array
        /// </summary>
        /// <param name="clip">The audio clip to convert</param>
        /// <returns>A short array</returns>
        public static short[] AudioDataToShorts(float[] samples, int channels, float gain = 1.0f)
        {
            short[] data = USpeakPoolUtils.GetShort(samples.Length * channels);
            for (int i = 0; i < samples.Length; i++)
            {
                //convert to the -3267 to +3267 range
                float g = samples[i] * gain;
                if (Mathf.Abs(g) > 1.0f)
                {
                    if (g > 0)
                    {
                        g = 1.0f;
                    }
                    else
                    {
                        g = -1.0f;
                    }
                }
                float conv = g * 3267.0f;
                //int c = Mathf.RoundToInt( conv );

                data[i] = (short)conv;
            }

            return(data);
        }
Beispiel #7
0
        // Token: 0x06004C3F RID: 19519 RVA: 0x00197D4C File Offset: 0x0019614C
        private byte[] SpeexEncode(short[] input, global::BandMode mode)
        {
            SpeexEncoder speexEncoder = null;
            int          num          = 320;

            if (mode != global::BandMode.Narrow)
            {
                if (mode != global::BandMode.Wide)
                {
                    if (mode == global::BandMode.UltraWide)
                    {
                        speexEncoder = this.m_ultrawide_enc;
                        num          = 1280;
                    }
                }
                else
                {
                    speexEncoder = this.m_wide_enc;
                    num          = 640;
                }
            }
            else
            {
                speexEncoder = this.m_narrow_enc;
                num          = 320;
            }
            byte[] @byte = USpeakPoolUtils.GetByte(num + 4);
            int    value = speexEncoder.Encode(input, 0, input.Length, @byte, 4, @byte.Length);

            byte[] bytes = BitConverter.GetBytes(value);
            Array.Copy(bytes, @byte, 4);
            return(@byte);
        }
Beispiel #8
0
        private byte[] SpeexEncode(short[] input, BandMode mode)
        {
            NSpeex.SpeexEncoder speexEnc = null;
            int byteLen = 320;

            switch (mode)
            {
            case BandMode.Narrow:
                speexEnc = m_narrow_enc;
                byteLen  = 320;
                break;

            case BandMode.Wide:
                speexEnc = m_wide_enc;
                byteLen  = 640;
                break;

            case BandMode.UltraWide:
                speexEnc = m_ultrawide_enc;
                byteLen  = 1280;
                break;
            }

            byte[] encoded = USpeakPoolUtils.GetByte(byteLen + 4);

            int length = speexEnc.Encode(input, 0, input.Length, encoded, 4, encoded.Length);

            // first 4 bytes contains length
            byte[] len_bytes = BitConverter.GetBytes(length);

            System.Array.Copy(len_bytes, encoded, 4);

            return(encoded);
        }
        // Token: 0x06004CA7 RID: 19623 RVA: 0x0019B4BC File Offset: 0x001998BC
        public static float[] DecompressAudio(byte[] data, int samples, int channels, bool threeD, BandMode mode, ICodec Codec, float gain)
        {
            int frequency = 4000;

            if (mode == BandMode.Narrow)
            {
                frequency = 8000;
            }
            else if (mode == BandMode.Wide)
            {
                frequency = 16000;
            }
            else if (mode == BandMode.UltraWide)
            {
                frequency = 32000;
            }
            else if (mode == BandMode.Opus48k)
            {
                frequency = 48000;
            }
            short[] array  = Codec.Decode(data, mode);
            float[] result = USpeakAudioClipConverter.ShortsToAudioData(array, channels, frequency, threeD, gain);
            USpeakPoolUtils.Return(array);
            return(result);
        }
Beispiel #10
0
        // Token: 0x06003B05 RID: 15109 RVA: 0x00129978 File Offset: 0x00127D78
        public unsafe byte[] Encode(byte[] inputPcmSamples, int sampleLength, out int encodedLength)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException("OpusEncoder");
            }
            int frame_size = this.FrameCount(inputPcmSamples);

            byte[] @byte = USpeakPoolUtils.GetByte(this.MaxDataBytes);
            int    num;

            fixed(byte *value = (@byte != null && @byte.Length != 0)? @byte : null)
            {
                IntPtr data = new IntPtr((void *)value);

                num = API.opus_encode(this._encoder, inputPcmSamples, frame_size, data, sampleLength);
            }

            encodedLength = num;
            if (num < 0)
            {
                USpeakPoolUtils.Return(@byte);
                string str    = "Encoding failed - ";
                Errors errors = (Errors)num;
                throw new Exception(str + errors.ToString());
            }
            byte[] byte2 = USpeakPoolUtils.GetByte(encodedLength);
            Buffer.BlockCopy(@byte, 0, byte2, 0, encodedLength);
            USpeakPoolUtils.Return(@byte);
            return(byte2);
        }
 // Token: 0x06004CA6 RID: 19622 RVA: 0x0019B47C File Offset: 0x0019987C
 public static byte[] CompressAudioData(float[] samples, int channels, BandMode mode, ICodec Codec, float gain = 1f)
 {
     short[] array  = USpeakAudioClipConverter.AudioDataToShorts(samples, channels, gain);
     byte[]  array2 = Codec.Encode(array, mode);
     USpeakPoolUtils.Return(array);
     byte[] array3 = new byte[array2.Length];
     Array.Copy(array2, array3, array2.Length);
     USpeakPoolUtils.Return(array2);
     return(array3);
 }
Beispiel #12
0
 public static float[] ShortsToAudioData(short[] data, int channels, int frequency, bool threedimensional, float gain)
 {
     float[] @float = USpeakPoolUtils.GetFloat(data.Length);
     for (int i = 0; i < @float.Length; i++)
     {
         int num2 = data[i];
         @float[i] = (((float)num2) / 3267f) * gain;
     }
     return(@float);
 }
 // Token: 0x06004CAA RID: 19626 RVA: 0x0019B5A4 File Offset: 0x001999A4
 public static float[] ShortsToAudioData(short[] data, int channels, int frequency, bool threedimensional, float gain)
 {
     float[] @float = USpeakPoolUtils.GetFloat(data.Length);
     for (int i = 0; i < @float.Length; i++)
     {
         float num = (float)data[i] / 3267f * gain;
         @float[i] = ((num >= -1f) ? ((num <= 1f) ? num : 1f) : -1f);
     }
     return(@float);
 }
 // Token: 0x06004CA9 RID: 19625 RVA: 0x0019B53C File Offset: 0x0019993C
 public static short[] AudioDataToShorts(float[] samples, int channels, float gain = 1f)
 {
     short[] @short = USpeakPoolUtils.GetShort(samples.Length * channels);
     for (int i = 0; i < samples.Length; i++)
     {
         float num = samples[i] * gain * 3267f;
         @short[i] = (short)((num >= -3267f) ? ((num <= 3267f) ? num : 3267f) : -3267f);
     }
     return(@short);
 }
Beispiel #15
0
 public static float[] ShortsToAudioData(short[] data, int channels, int frequency, bool threedimensional, float gain)
 {
     float[] num = USpeakPoolUtils.GetFloat((int)data.Length);
     for (int i = 0; i < (int)num.Length; i++)
     {
         int num1 = data[i];
         num[i] = (float)num1 / 3267f * gain;
     }
     return(num);
 }
Beispiel #16
0
            public static byte[] MuLawEncode(short[] pcm)
            {
                int length = (int)pcm.Length;

                byte[] num = USpeakPoolUtils.GetByte(length);
                for (int i = 0; i < length; i++)
                {
                    num[i] = MuLawCodec.MuLawEncoder.MuLawEncode(pcm[i]);
                }
                return(num);
            }
Beispiel #17
0
            public static short[] MuLawDecode(byte[] data)
            {
                var size    = data.Length;
                var decoded = USpeakPoolUtils.GetShort(size);

                for (var i = 0; i < size; i++)
                {
                    decoded[i] = muLawToPcmMap[data[i]];
                }
                return(decoded);
            }
Beispiel #18
0
 public static byte[] CompressAudioData(float[] samples, int channels, out int sample_count, BandMode mode, ICodec Codec, float gain = 1f)
 {
     USpeakAudioClipCompressor.data.Clear();
     sample_count = 0;
     short[] shorts   = USpeakAudioClipConverter.AudioDataToShorts(samples, channels, gain);
     byte[]  numArray = Codec.Encode(shorts, mode);
     USpeakPoolUtils.Return(shorts);
     USpeakAudioClipCompressor.data.AddRange(numArray);
     USpeakPoolUtils.Return(numArray);
     return(USpeakAudioClipCompressor.data.ToArray());
 }
Beispiel #19
0
            public static byte[] MuLawEncode(int[] pcm)
            {
                int length = pcm.Length;

                byte[] @byte = USpeakPoolUtils.GetByte(length);
                for (int i = 0; i < length; i++)
                {
                    @byte[i] = MuLawEncode(pcm[i]);
                }
                return(@byte);
            }
Beispiel #20
0
            public static short[] MuLawDecode(byte[] data)
            {
                int length = data.Length;

                short[] @short = USpeakPoolUtils.GetShort(length);
                for (int i = 0; i < length; i++)
                {
                    @short[i] = muLawToPcmMap[data[i]];
                }
                return(@short);
            }
Beispiel #21
0
            // Token: 0x06004C34 RID: 19508 RVA: 0x00197840 File Offset: 0x00195C40
            public static short[] MuLawDecode(byte[] data)
            {
                int num = data.Length;

                short[] @short = USpeakPoolUtils.GetShort(num);
                for (int i = 0; i < num; i++)
                {
                    @short[i] = MuLawCodec.MuLawDecoder.muLawToPcmMap[(int)data[i]];
                }
                return(@short);
            }
Beispiel #22
0
            // Token: 0x06004C30 RID: 19504 RVA: 0x0019774C File Offset: 0x00195B4C
            public static byte[] MuLawEncode(short[] pcm)
            {
                int num = pcm.Length;

                byte[] @byte = USpeakPoolUtils.GetByte(num);
                for (int i = 0; i < num; i++)
                {
                    @byte[i] = MuLawCodec.MuLawEncoder.MuLawEncode(pcm[i]);
                }
                return(@byte);
            }
Beispiel #23
0
            public static short[] MuLawDecode(byte[] data)
            {
                int length = (int)data.Length;

                short[] num = USpeakPoolUtils.GetShort(length);
                for (int i = 0; i < length; i++)
                {
                    num[i] = MuLawCodec.MuLawDecoder.muLawToPcmMap[data[i]];
                }
                return(num);
            }
Beispiel #24
0
            public static byte[] MuLawEncode(short[] pcm)
            {
                var size    = pcm.Length;
                var encoded = USpeakPoolUtils.GetByte(size);

                for (int i = 0; i < size; i++)
                {
                    encoded[i] = MuLawEncode(pcm[i]);
                }
                return(encoded);
            }
        /// <summary>
        /// Convert a short array to a PCM array
        /// </summary>
        /// <param name="data">The short array representing an audio clip</param>
        /// <param name="channels">How many channels in the audio data</param>
        /// <param name="frequency">The recording frequency of the audio data</param>
        /// <param name="threedimensional">Whether the audio clip should be 3D</param>
        /// <param name="gain">How much to boost the volume (1.0 = unchanged)</param>
        /// <returns>An array of float samples</returns>
        public static float[] ShortsToAudioData(short[] data, int channels, int frequency, bool threedimensional, float gain)
        {
            float[] samples = USpeakPoolUtils.GetFloat(data.Length);

            for (int i = 0; i < samples.Length; i++)
            {
                //convert to float in the -1 to 1 range
                int c = (int)data[i];
                samples[i] = ((float)c / 3267.0f) * gain;
            }

            return(samples);
        }
Beispiel #26
0
 public short[] Decode(byte[] data, BandMode mode)
 {
     Init();
     short[] temp = USpeakPoolUtils.GetShort(data.Length * 2);
     for (int i = 0; i < data.Length; i++)
     {
         byte c = data[i];
         byte d = (byte)(c & 0x0f);
         byte e = (byte)(c >> 4);
         temp[i * 2]       = ADPCM_Decode(d);
         temp[(i * 2) + 1] = ADPCM_Decode(e);
     }
     return(temp);
 }
Beispiel #27
0
 public static short[] AudioDataToShorts(float[] samples, int channels, float gain = 1f)
 {
     short[] num = USpeakPoolUtils.GetShort((int)samples.Length * channels);
     for (int i = 0; i < (int)samples.Length; i++)
     {
         float single = samples[i] * gain;
         if (Mathf.Abs(single) > 1f)
         {
             single = (single <= 0f ? -1f : 1f);
         }
         num[i] = (short)(single * 3267f);
     }
     return(num);
 }
Beispiel #28
0
 private static void CopyStream(Stream input, Stream output)
 {
     byte[] num = USpeakPoolUtils.GetByte(32768);
     while (true)
     {
         int num1 = input.Read(num, 0, (int)num.Length);
         if (num1 <= 0)
         {
             break;
         }
         output.Write(num, 0, num1);
     }
     USpeakPoolUtils.Return(num);
 }
Beispiel #29
0
 public short[] Decode(byte[] data, BandMode mode)
 {
     this.Init();
     short[] @short = USpeakPoolUtils.GetShort(data.Length * 2);
     for (int i = 0; i < data.Length; i++)
     {
         byte num2           = data[i];
         byte originalSample = (byte)(num2 & 15);
         byte num4           = (byte)(num2 >> 4);
         @short[i * 2]       = this.ADPCM_Decode(originalSample);
         @short[(i * 2) + 1] = this.ADPCM_Decode(num4);
     }
     return(@short);
 }
Beispiel #30
0
 private static void CopyStream(Stream input, Stream output)
 {
     byte[] @byte = USpeakPoolUtils.GetByte(0x8000);
     while (true)
     {
         int count = input.Read(@byte, 0, @byte.Length);
         if (count <= 0)
         {
             break;
         }
         output.Write(@byte, 0, count);
     }
     USpeakPoolUtils.Return(@byte);
 }