Example #1
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);
        }
Example #2
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);
        }
Example #3
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);
        }
Example #4
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);
        }
Example #5
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);
        }
Example #6
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);
        }
Example #7
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);
        }
Example #8
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);
            }
Example #9
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);
            }
Example #10
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);
            }
Example #11
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);
            }
Example #12
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);
 }
Example #13
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);
 }
Example #14
0
 public void ReceiveAudio(byte[] data)
 {
     byte[] num = null;
     if (this.settings == null)
     {
         UnityEngine.Debug.LogWarning("Trying to receive remote audio data without calling InitializeSettings!\nIncoming packet will be ignored");
         return;
     }
     if (USpeaker.MuteAll || this.Mute || this.SpeakerMode == SpeakerMode.Local && !this.DebugPlayback)
     {
         return;
     }
     if (this.SpeakerMode == SpeakerMode.Remote)
     {
         this.talkTimer = 1f;
     }
     for (int i = 0; i < (int)data.Length; i = i + (int)num.Length)
     {
         int num1 = BitConverter.ToInt32(data, i);
         num = USpeakPoolUtils.GetByte(num1 + 6);
         Array.Copy(data, i, num, 0, (int)num.Length);
         USpeakFrameContainer uSpeakFrameContainer = new USpeakFrameContainer();
         uSpeakFrameContainer.LoadFrom(num);
         USpeakPoolUtils.Return(num);
         float[]  singleArray = USpeakAudioClipCompressor.DecompressAudio(uSpeakFrameContainer.encodedData, (int)uSpeakFrameContainer.Samples, 1, false, this.settings.bandMode, this.codecMgr.Codecs[this.Codec], USpeaker.RemoteGain);
         float    length      = (float)((int)singleArray.Length) / (float)this.audioFrequency;
         USpeaker uSpeaker    = this;
         uSpeaker.received = uSpeaker.received + (double)length;
         Array.Copy(singleArray, 0, this.receivedData, this.index, (int)singleArray.Length);
         USpeakPoolUtils.Return(singleArray);
         USpeaker length1 = this;
         length1.index = length1.index + (int)singleArray.Length;
         if (this.index >= base.audio.clip.samples)
         {
             this.index = 0;
         }
         base.audio.clip.SetData(this.receivedData, 0);
         if (!base.audio.isPlaying)
         {
             this.shouldPlay = true;
             if (this.playDelay <= 0f)
             {
                 this.playDelay = length * 2f;
             }
         }
     }
 }
Example #15
0
 public void ReceiveAudio(byte[] data)
 {
     if (this.settings == null)
     {
         UnityEngine.Debug.LogWarning("Trying to receive remote audio data without calling InitializeSettings!\nIncoming packet will be ignored");
     }
     else if ((!MuteAll && !this.Mute) && ((this.SpeakerMode != SpeakerMode.Local) || this.DebugPlayback))
     {
         byte[] @byte;
         if (this.SpeakerMode == SpeakerMode.Remote)
         {
             this.talkTimer = 1f;
         }
         for (int i = 0; i < data.Length; i += @byte.Length)
         {
             @byte = USpeakPoolUtils.GetByte(BitConverter.ToInt32(data, i) + 6);
             Array.Copy(data, i, @byte, 0, @byte.Length);
             USpeakFrameContainer container = new USpeakFrameContainer();
             container.LoadFrom(@byte);
             USpeakPoolUtils.Return(@byte);
             float[] sourceArray = USpeakAudioClipCompressor.DecompressAudio(container.encodedData, container.Samples, 1, false, this.settings.bandMode, this.codecMgr.Codecs[this.Codec], RemoteGain);
             float   num3        = ((float)sourceArray.Length) / ((float)this.audioFrequency);
             this.received += num3;
             Array.Copy(sourceArray, 0, this.receivedData, this.index, sourceArray.Length);
             USpeakPoolUtils.Return(sourceArray);
             this.index += sourceArray.Length;
             if (this.index >= base.audio.clip.samples)
             {
                 this.index = 0;
             }
             base.audio.clip.SetData(this.receivedData, 0);
             if (!base.audio.isPlaying)
             {
                 this.shouldPlay = true;
                 if (this.playDelay <= 0f)
                 {
                     this.playDelay = num3 * 2f;
                 }
             }
         }
     }
 }
Example #16
0
        private short[] SpeexDecode(byte[] input, BandMode mode)
        {
            NSpeex.SpeexDecoder speexDec = null;
            int shortLen = 320;

            switch (mode)
            {
            case BandMode.Narrow:
                speexDec = m_narrow_dec;
                shortLen = 320;
                break;

            case BandMode.Wide:
                speexDec = m_wide_dec;
                shortLen = 640;
                break;

            case BandMode.UltraWide:
                speexDec = m_ultrawide_dec;
                shortLen = 1280;
                break;
            }

            byte[] len_bytes = USpeakPoolUtils.GetByte(4);
            System.Array.Copy(input, len_bytes, 4);

            int dataLength = BitConverter.ToInt32(len_bytes, 0);

            USpeakPoolUtils.Return(len_bytes);

            byte[] actual_bytes = USpeakPoolUtils.GetByte(input.Length - 4);
            Buffer.BlockCopy(input, 4, actual_bytes, 0, input.Length - 4);

            short[] decoded = USpeakPoolUtils.GetShort(shortLen);

            speexDec.Decode(actual_bytes, 0, dataLength, decoded, 0, false);

            USpeakPoolUtils.Return(actual_bytes);

            return(decoded);
        }
Example #17
0
        // Token: 0x06004C40 RID: 19520 RVA: 0x00197DDC File Offset: 0x001961DC
        private short[] SpeexDecode(byte[] input, global::BandMode mode)
        {
            SpeexDecoder speexDecoder = null;
            int          length       = 320;

            if (mode != global::BandMode.Narrow)
            {
                if (mode != global::BandMode.Wide)
                {
                    if (mode == global::BandMode.UltraWide)
                    {
                        speexDecoder = this.m_ultrawide_dec;
                        length       = 1280;
                    }
                }
                else
                {
                    speexDecoder = this.m_wide_dec;
                    length       = 640;
                }
            }
            else
            {
                speexDecoder = this.m_narrow_dec;
                length       = 320;
            }
            byte[] @byte = USpeakPoolUtils.GetByte(4);
            Array.Copy(input, @byte, 4);
            int inCount = BitConverter.ToInt32(@byte, 0);

            USpeakPoolUtils.Return(@byte);
            byte[] byte2 = USpeakPoolUtils.GetByte(input.Length - 4);
            Buffer.BlockCopy(input, 4, byte2, 0, input.Length - 4);
            short[] @short = USpeakPoolUtils.GetShort(length);
            speexDecoder.Decode(byte2, 0, inCount, @short, 0, false);
            USpeakPoolUtils.Return(byte2);
            return(@short);
        }
Example #18
0
        // Token: 0x06003AF6 RID: 15094 RVA: 0x001296A0 File Offset: 0x00127AA0
        public unsafe byte[] Decode(byte[] inputOpusData, int dataLength, out int decodedLength)
        {
            if (this.disposed)
            {
                throw new ObjectDisposedException("OpusDecoder");
            }
            byte[] @byte      = USpeakPoolUtils.GetByte(this.MaxDataBytes);
            int    frame_size = this.FrameCount(this.MaxDataBytes);
            int    num;

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

                if (inputOpusData != null)
                {
                    num = API.opus_decode(this._decoder, inputOpusData, dataLength, pcm, frame_size, 0);
                }
                else
                {
                    num = API.opus_decode(this._decoder, null, 0, pcm, this.FrameCount(this._expectedBytesPerSegment), (!this.ForwardErrorCorrection) ? 0 : 1);
                }
            }

            decodedLength = num * 2;
            if (num < 0)
            {
                USpeakPoolUtils.Return(@byte);
                string str    = "Decoding failed - ";
                Errors errors = (Errors)num;
                throw new Exception(str + errors.ToString());
            }
            byte[] byte2 = USpeakPoolUtils.GetByte(decodedLength);
            Buffer.BlockCopy(@byte, 0, byte2, 0, decodedLength);
            USpeakPoolUtils.Return(@byte);
            return(byte2);
        }
Example #19
0
    // Token: 0x06004C3A RID: 19514 RVA: 0x00197A78 File Offset: 0x00195E78
    public byte[] Encode(short[] data, BandMode mode)
    {
        if (!this.isInitialized)
        {
            this.CreateEncoders();
        }
        if (mode != BandMode.Opus48k)
        {
            Debug.LogError(string.Concat(new string[]
            {
                "OpusCodec: Encode: bandwidth mode must be ",
                BandMode.Opus48k.ToString(),
                "! (set to ",
                mode.ToString(),
                ")"
            }));
        }
        if (data.Length != this._segmentFrames)
        {
            Debug.LogError(string.Concat(new object[]
            {
                "OpusCodec: Encode failed! Input PCM data is ",
                data.Length,
                " frames, expected ",
                this._segmentFrames
            }));
            return(new byte[0]);
        }
        byte[] @byte = USpeakPoolUtils.GetByte(data.Length * 2);
        Buffer.BlockCopy(data, 0, @byte, 0, data.Length * 2);
        int num = 0;

        byte[] result = this._encoder.Encode(@byte, this._bytesPerSegment, out num);
        USpeakPoolUtils.Return(@byte);
        return(result);
    }
Example #20
0
    /// <summary>
    /// Decode and buffer audio data to be played
    /// </summary>
    /// <param name="data">The data passed to USpeakOnSerializeAudio()</param>
    public void ReceiveAudio(byte[] data)
    {
        if (settings == null)
        {
            Debug.LogWarning("Trying to receive remote audio data without calling InitializeSettings!\nIncoming packet will be ignored");
            return;
        }

        if (MuteAll || Mute || (SpeakerMode == SpeakerMode.Local && !DebugPlayback))
        {
            return;
        }

        if (SpeakerMode == SpeakerMode.Remote)
        {
            talkTimer = 1.0f;
        }

        int offset = 0;

        while (offset < data.Length)
        {
            int    len   = System.BitConverter.ToInt32(data, offset);
            byte[] frame = USpeakPoolUtils.GetByte(len + 6);
            System.Array.Copy(data, offset, frame, 0, frame.Length);

            USpeakFrameContainer cont = default(USpeakFrameContainer);
            cont.LoadFrom(frame);

            USpeakPoolUtils.Return(frame);

            float[] sample = USpeakAudioClipCompressor.DecompressAudio(cont.encodedData, (int)cont.Samples, 1, false, settings.bandMode, codecMgr.Codecs[Codec], RemoteGain);

            float sampleTime = ((float)sample.Length / (float)audioFrequency);
            received += sampleTime;

            System.Array.Copy(sample, 0, receivedData, index, sample.Length);

            USpeakPoolUtils.Return(sample);

            // advance the write position into the audio clip
            index += sample.Length;

            // if the write position extends beyond the clip length, wrap around
            if (index >= audio.clip.samples)
            {
                index = 0;
            }

            // write received data to audio clip
            audio.clip.SetData(receivedData, 0);

            // not already playing audio, schedule audio to be played
            if (!audio.isPlaying)
            {
                shouldPlay = true;

                //Debug.Log( "Started receiving at time: " + Time.time );

                // no play delay set, advance play delay to allow more data to arrive (deal with network latency)
                if (playDelay <= 0)
                {
                    playDelay = sampleTime * 5f;
                }
            }

            offset += frame.Length;
        }
    }