public SpeexAudioEncoder(bool encodeOgg, BandMode bandMode, bool isVbr, int quality)
 {
     this.encodeOgg  = encodeOgg;
     this.bandMode   = bandMode;
     this.isVbr      = isVbr;
     this.quality    = quality < 0 ? 10 : quality;
 }
Example #2
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 #3
0
 public SpeexChatCodec(BandMode bandMode, int sampleRate, string description)
 {
     decoder = new SpeexDecoder(bandMode);
     encoder = new SpeexEncoder(bandMode);
     recordingFormat = new WaveFormat(sampleRate, 16, 2);
     this.description = description;
     encoderInputBuffer = new WaveBuffer(recordingFormat.AverageBytesPerSecond); // more than enough
 }
Example #4
0
 public SpeexCodec(BandMode mode)
     : base("Speex")
 {
     Mode = mode;
     Encoder = new SpeexEncoder(mode);
     Encoder.VBR = false;
     Encoder.Quality = 10;
     Decoder = new SpeexDecoder(mode);
 }
 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();
 }
Example #6
0
 public short[] Decode(byte[] data, BandMode mode)
 {
     this.Init();
     short[] num = USpeakPoolUtils.GetShort((int)data.Length * 2);
     for (int i = 0; i < (int)data.Length; i++)
     {
         byte num1 = data[i];
         byte num2 = (byte)(num1 & 15);
         byte num3 = (byte)(num1 >> 4);
         num[i * 2] = this.ADPCM_Decode(num2);
         num[i * 2 + 1] = this.ADPCM_Decode(num3);
     }
     return num;
 }
        public VoipCodecSpeex(BandMode bandMode, int sampleRate, string description, VoipCodecMode mode = VoipCodecMode.Both)
        {
            _bandMode = bandMode;
            _recordingFormat = new WaveFormat(sampleRate, 16, 1);
            _description = description;

            if (mode.HasFlag(VoipCodecMode.Decode))
                _decoder = new SpeexDecoder(bandMode);
            if (mode.HasFlag(VoipCodecMode.Encode))
            {
                _encoder = new SpeexEncoder(bandMode);
                _outputBufferTemp = new byte[_recordingFormat.AverageBytesPerSecond];
                _encoderInputBuffer = new WaveBuffer(_recordingFormat.AverageBytesPerSecond); // more than enough
            }
        }
Example #8
0
 public USpeakSettingsData(byte src)
 {
     if ((src & 1) == 1)
     {
         this.bandMode = BandMode.Narrow;
     }
     else if ((src & 2) != 2)
     {
         this.bandMode = BandMode.UltraWide;
     }
     else
     {
         this.bandMode = BandMode.Wide;
     }
     this.Codec = src >> 2;
 }
 public static float[] DecompressAudio(byte[] data, int samples, int channels, bool threeD, BandMode mode, ICodec Codec, float gain)
 {
     int num = 4000;
     if (mode == BandMode.Narrow)
     {
         num = 8000;
     }
     else if (mode == BandMode.Wide)
     {
         num = 16000;
     }
     short[] numArray = Codec.Decode(data, mode);
     USpeakAudioClipCompressor.tmp.Clear();
     USpeakAudioClipCompressor.tmp.AddRange(numArray);
     USpeakPoolUtils.Return(numArray);
     return USpeakAudioClipConverter.ShortsToAudioData(USpeakAudioClipCompressor.tmp.ToArray(), channels, num, threeD, gain);
 }
Example #10
0
 public short[] Decode(byte[] data, BandMode mode)
 {
     short[] result = new short[data.Length / sizeof(short)];
     System.Buffer.BlockCopy(data, 0, result, 0, data.Length);
     return(result);
 }
Example #11
0
 public short[] Decode(byte[] data, BandMode mode)
 {
     return(DecodeSpeech(data, mode));
 }
Example #12
0
 public byte[] Encode(short[] data, BandMode mode)
 {
     return(EncodeSpeech(data, mode));
 }
Example #13
0
 public SpeexAudioDecoder(BandMode bandMode)
 {
     this.bandMode = bandMode;
 }
    private float CalculateSpectrum(int[] bands, float[] weights, float scale, float max, float offset, BandMode bandMode)
    {
        float value = 0;

        for (int i = 0; i < bands.Length; ++i)
        {
            if (bandMode == BandMode.Band8)
            {
                value += spectrum.FrequencyBand8Ave[bands[i]] * (weights.Length > i?weights[i]:1);
            }
            else if (bandMode == BandMode.Band512)
            {
                int   bandNum      = bands[i];
                float steroClamped = Mathf.Clamp01(spectrum.stero);
                value += ((1 - steroClamped) * spectrum.SamplesLeft[bandNum] + steroClamped * spectrum.SamplesRight[bandNum]) * (weights.Length > i ? weights[i] : 1);
            }
        }
        value = value * scale - offset;

        value = Mathf.Max(0, Mathf.Min(value, max));

        return(value);
    }
Example #15
0
 public static SpeexCodex Create(BandMode mode)
 {
     return(new SpeexCodex(mode));
 }
Example #16
0
 public short[] Decode(byte[] data, BandMode mode)
 {
     return(MuLawCodec.MuLawDecoder.MuLawDecode(data));
 }
Example #17
0
 private void Awake()
 {
     USpeaker.USpeakerList.Add(this);
     if (base.audio == null)
     {
         base.gameObject.AddComponent<AudioSource>();
     }
     base.audio.clip = AudioClip.Create("vc", this.audioFrequency * 10, 1, this.audioFrequency, this._3DMode == ThreeDMode.Full3D, false);
     base.audio.loop = true;
     this.receivedData = new float[this.audioFrequency * 10];
     this.codecMgr = USpeakCodecManager.Instance;
     this.lastBandMode = this.BandwidthMode;
     this.lastCodec = this.Codec;
     this.last3DMode = this._3DMode;
 }
Example #18
0
 public VoipFragment(int index, byte[] data, BandMode mode)
 {
     this.index = index;
     this.data  = data;
     this.mode  = mode;
 }
Example #19
0
 public EncodeSubject(BandMode mode)
 {
     _encoder = new SpeexEncoder(mode);
 }
Example #20
0
 public USpeakSettingsData()
 {
     bandMode = BandMode.Narrow;
     Is3D     = false;
 }
Example #21
0
 public byte[] Encode(short[] data, BandMode mode)
 {
     return MuLawCodec.MuLawEncoder.MuLawEncode(data);
 }
Example #22
0
 public short[] Decode(byte[] data, BandMode mode)
 {
     return MuLawCodec.MuLawDecoder.MuLawDecode(data);
 }
        // 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);
        }
Example #24
0
 public USpeakSettingsData()
 {
     this.bandMode = BandMode.Narrow;
     this.Codec = 0;
 }
Example #25
0
 private void Update()
 {
     bool value;
     int num;
     USpeaker uSpeaker = this;
     uSpeaker.talkTimer = uSpeaker.talkTimer - Time.deltaTime;
     base.audio.volume = this.SpeakerVolume;
     if (this.last3DMode != this._3DMode)
     {
         this.last3DMode = this._3DMode;
         this.StopPlaying();
         base.audio.clip = AudioClip.Create("vc", this.audioFrequency * 10, 1, this.audioFrequency, this._3DMode == ThreeDMode.Full3D, false);
         base.audio.loop = true;
     }
     if (this._3DMode == ThreeDMode.SpeakerPan)
     {
         Transform transforms = Camera.main.transform;
         Vector3 vector3 = Vector3.Cross(transforms.up, transforms.forward);
         vector3.Normalize();
         float single = Vector3.Dot(base.transform.position - transforms.position, vector3);
         float single1 = Vector3.Dot(base.transform.position - transforms.position, transforms.forward);
         float single2 = Mathf.Sin(Mathf.Atan2(single, single1));
         base.audio.pan = single2;
     }
     if (base.audio.isPlaying)
     {
         if (this.lastTime > base.audio.time)
         {
             USpeaker uSpeaker1 = this;
             uSpeaker1.played = uSpeaker1.played + (double)base.audio.clip.length;
         }
         this.lastTime = base.audio.time;
         if (this.played + (double)base.audio.time >= this.received)
         {
             this.StopPlaying();
             this.shouldPlay = false;
         }
     }
     else if (this.shouldPlay)
     {
         USpeaker uSpeaker2 = this;
         uSpeaker2.playDelay = uSpeaker2.playDelay - Time.deltaTime;
         if (this.playDelay <= 0f)
         {
             base.audio.Play();
         }
     }
     if (this.SpeakerMode == SpeakerMode.Remote)
     {
         return;
     }
     if (this.audioHandler == null)
     {
         return;
     }
     if (this.devicesCached == null)
     {
         this.devicesCached = Microphone.devices;
         base.InvokeRepeating("RefreshDevices", 4.2f, 4.2f);
     }
     string[] strArrays = this.devicesCached;
     if ((int)strArrays.Length == 0)
     {
         return;
     }
     if (strArrays[Mathf.Min(USpeaker.InputDeviceID, (int)strArrays.Length - 1)] != this.currentDeviceName)
     {
         this.currentDeviceName = strArrays[Mathf.Min(USpeaker.InputDeviceID, (int)strArrays.Length - 1)];
         MonoBehaviour.print(string.Concat("Using input device: ", this.currentDeviceName));
         this.recording = Microphone.Start(this.currentDeviceName, false, 21, this.audioFrequency);
         this.lastReadPos = 0;
     }
     if (this.lastBandMode != this.BandwidthMode || this.lastCodec != this.Codec)
     {
         this.UpdateSettings();
         this.lastBandMode = this.BandwidthMode;
         this.lastCodec = this.Codec;
     }
     int position = Microphone.GetPosition(null);
     if (position >= this.audioFrequency * 20)
     {
         position = 0;
         this.lastReadPos = 0;
         UnityEngine.Object.DestroyImmediate(this.recording);
         Microphone.End(null);
         this.recording = Microphone.Start(this.currentDeviceName, false, 21, this.audioFrequency);
     }
     if (position <= this.overlap)
     {
         return;
     }
     bool? nullable = null;
     try
     {
         int num1 = position - this.lastReadPos;
         int sampleSize = this.codecMgr.Codecs[this.Codec].GetSampleSize(this.audioFrequency);
         if (sampleSize == 0)
         {
             sampleSize = 100;
         }
         if (sampleSize != 0)
         {
             int num2 = this.lastReadPos;
             int num3 = Mathf.FloorToInt((float)(num1 / sampleSize));
             for (int i = 0; i < num3; i++)
             {
                 float[] singleArray = USpeakPoolUtils.GetFloat(sampleSize);
                 this.recording.GetData(singleArray, num2);
                 if (!nullable.HasValue)
                 {
                     bool? nullable1 = new bool?((this.talkController == null ? false : this.talkController.ShouldSend()));
                     nullable = nullable1;
                     value = nullable1.Value;
                 }
                 else
                 {
                     value = nullable.Value;
                 }
                 if (value)
                 {
                     this.talkTimer = 1f;
                     this.OnAudioAvailable(singleArray);
                 }
                 USpeakPoolUtils.Return(singleArray);
                 num2 = num2 + sampleSize;
             }
             this.lastReadPos = num2;
         }
         else
         {
             if (num1 > sampleSize)
             {
                 float[] singleArray1 = new float[num1 - 1];
                 this.recording.GetData(singleArray1, this.lastReadPos);
                 if (this.talkController == null || this.talkController.ShouldSend())
                 {
                     this.talkTimer = 1f;
                     this.OnAudioAvailable(singleArray1);
                 }
             }
             this.lastReadPos = position;
         }
     }
     catch (Exception exception)
     {
     }
     this.ProcessPendingEncodeBuffer();
     bool flag = true;
     if (this.SendingMode == SendBehavior.RecordThenSend && this.talkController != null)
     {
         if (!nullable.HasValue)
         {
             bool? nullable2 = new bool?(this.talkController.ShouldSend());
             nullable = nullable2;
             num = (int)nullable2.Value;
         }
         else
         {
             num = (int)nullable.Value;
         }
         flag = num == 0;
     }
     USpeaker uSpeaker3 = this;
     uSpeaker3.sendTimer = uSpeaker3.sendTimer + Time.deltaTime;
     if (this.sendTimer >= this.sendt && flag)
     {
         this.sendTimer = 0f;
         this.tempSendBytes.Clear();
         foreach (USpeakFrameContainer uSpeakFrameContainer in this.sendBuffer)
         {
             this.tempSendBytes.AddRange(uSpeakFrameContainer.ToByteArray());
         }
         this.sendBuffer.Clear();
         if (this.tempSendBytes.Count > 0)
         {
             this.audioHandler.USpeakOnSerializeAudio(this.tempSendBytes.ToArray());
         }
     }
 }
        public static AudioClip DecompressAudioClip(byte[] data, int samples, int channels, bool threeD, BandMode mode, float gain)
        {
            int frequency = 4000;

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

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

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

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

            //while( tmp.Count > 1 && Mathf.Abs( tmp[ tmp.Count - 1 ] ) <= 10 )
            //{
            //    tmp.RemoveAt( tmp.Count - 1 );
            //}

            //while( tmp.Count > 1 && Mathf.Abs( tmp[ 0 ] ) <= 10 )
            //{
            //    tmp.RemoveAt( 0 );
            //}

            return(USpeakAudioClipConverter.ShortsToAudioClip(tmp.ToArray(), channels, frequency, threeD, gain));
        }
Example #27
0
 public DecodeSubject(BandMode mode)
 {
     _decoder = new SpeexDecoder(mode);
 }
Example #28
0
 public USpeakSettingsData()
 {
     this.bandMode = BandMode.Narrow;
     this.Codec    = 0;
 }
//      public static byte[] CompressAudioClip( AudioClip clip, out int samples, BandMode mode, float gain = 1.0f )
//      {
//          data.Clear();
//          samples = 0;
//          short[] b = USpeakAudioClipConverter.AudioClipToShorts( clip, gain );
//          byte[] mlaw = Codec.Encode( b, mode );
//          data.AddRange( mlaw );
//          return zip( data.ToArray() );
//      }

        public static AudioClip DecompressAudioClip(byte[] data, int samples, int channels, bool threeD, BandMode mode, float gain)
        {
//          byte[] d;
//          d = unzip( data );
            short[] pcm = Codec.Decode(data, mode);
            tmp.Clear();
            tmp.AddRange(pcm);
            return(USpeakAudioClipConverter.ShortsToAudioClip(tmp.ToArray(), channels, WSpeak.getFrequency(mode), threeD, gain));
        }
Example #30
0
 public byte[] Encode(short[] data, BandMode mode)
 {
     byte[] result = new byte[data.Length * sizeof(short)];
     System.Buffer.BlockCopy(data, 0, result, 0, result.Length);
     return(result);
 }
        /// <summary>
        /// Compress the given audio data
        /// </summary>
        /// <param name="samples">The raw 32-bit audio data</param>
        /// <param name="channels">The number of channels (nearly always 1)</param>
        /// <param name="sample_count">The number of samples that were encoded</param>
        /// <param name="mode">The chosen bandwidth mode (recording frequency)</param>
        /// <param name="Codec">The codec to encode the audio with</param>
        /// <param name="gain">The gain to apply to the audio</param>
        /// <returns>An encoded byte array</returns>
        public static byte[] CompressAudioData(float[] samples, int channels, out int sample_count, BandMode mode, ICodec Codec, float gain = 1.0f)
        {
            data.Clear();
            sample_count = 0;

            short[] b = USpeakAudioClipConverter.AudioDataToShorts(samples, channels, gain);

            byte[] mlaw = Codec.Encode(b, mode);

            USpeakPoolUtils.Return(b);

            data.AddRange(mlaw);

            USpeakPoolUtils.Return(mlaw);

            //byte[] zipped = zip( data.ToArray() );

            return(data.ToArray());
        }
 /// <summary>
 ///     コンストラクタ
 /// </summary>
 public ObserverSpeexDecoder(BandMode mode)
 {
     _decoder = new SpeexDecoder(mode);
 }
Example #33
0
    private void Update()
    {
        bool     value;
        int      num;
        USpeaker uSpeaker = this;

        uSpeaker.talkTimer = uSpeaker.talkTimer - Time.deltaTime;
        base.audio.volume  = this.SpeakerVolume;
        if (this.last3DMode != this._3DMode)
        {
            this.last3DMode = this._3DMode;
            this.StopPlaying();
            base.audio.clip = AudioClip.Create("vc", this.audioFrequency * 10, 1, this.audioFrequency, this._3DMode == ThreeDMode.Full3D, false);
            base.audio.loop = true;
        }
        if (this._3DMode == ThreeDMode.SpeakerPan)
        {
            Transform transforms = Camera.main.transform;
            Vector3   vector3    = Vector3.Cross(transforms.up, transforms.forward);
            vector3.Normalize();
            float single  = Vector3.Dot(base.transform.position - transforms.position, vector3);
            float single1 = Vector3.Dot(base.transform.position - transforms.position, transforms.forward);
            float single2 = Mathf.Sin(Mathf.Atan2(single, single1));
            base.audio.pan = single2;
        }
        if (base.audio.isPlaying)
        {
            if (this.lastTime > base.audio.time)
            {
                USpeaker uSpeaker1 = this;
                uSpeaker1.played = uSpeaker1.played + (double)base.audio.clip.length;
            }
            this.lastTime = base.audio.time;
            if (this.played + (double)base.audio.time >= this.received)
            {
                this.StopPlaying();
                this.shouldPlay = false;
            }
        }
        else if (this.shouldPlay)
        {
            USpeaker uSpeaker2 = this;
            uSpeaker2.playDelay = uSpeaker2.playDelay - Time.deltaTime;
            if (this.playDelay <= 0f)
            {
                base.audio.Play();
            }
        }
        if (this.SpeakerMode == SpeakerMode.Remote)
        {
            return;
        }
        if (this.audioHandler == null)
        {
            return;
        }
        if (this.devicesCached == null)
        {
            this.devicesCached = Microphone.devices;
            base.InvokeRepeating("RefreshDevices", 4.2f, 4.2f);
        }
        string[] strArrays = this.devicesCached;
        if ((int)strArrays.Length == 0)
        {
            return;
        }
        if (strArrays[Mathf.Min(USpeaker.InputDeviceID, (int)strArrays.Length - 1)] != this.currentDeviceName)
        {
            this.currentDeviceName = strArrays[Mathf.Min(USpeaker.InputDeviceID, (int)strArrays.Length - 1)];
            MonoBehaviour.print(string.Concat("Using input device: ", this.currentDeviceName));
            this.recording   = Microphone.Start(this.currentDeviceName, false, 21, this.audioFrequency);
            this.lastReadPos = 0;
        }
        if (this.lastBandMode != this.BandwidthMode || this.lastCodec != this.Codec)
        {
            this.UpdateSettings();
            this.lastBandMode = this.BandwidthMode;
            this.lastCodec    = this.Codec;
        }
        int position = Microphone.GetPosition(null);

        if (position >= this.audioFrequency * 20)
        {
            position         = 0;
            this.lastReadPos = 0;
            UnityEngine.Object.DestroyImmediate(this.recording);
            Microphone.End(null);
            this.recording = Microphone.Start(this.currentDeviceName, false, 21, this.audioFrequency);
        }
        if (position <= this.overlap)
        {
            return;
        }
        bool?nullable = null;

        try
        {
            int num1       = position - this.lastReadPos;
            int sampleSize = this.codecMgr.Codecs[this.Codec].GetSampleSize(this.audioFrequency);
            if (sampleSize == 0)
            {
                sampleSize = 100;
            }
            if (sampleSize != 0)
            {
                int num2 = this.lastReadPos;
                int num3 = Mathf.FloorToInt((float)(num1 / sampleSize));
                for (int i = 0; i < num3; i++)
                {
                    float[] singleArray = USpeakPoolUtils.GetFloat(sampleSize);
                    this.recording.GetData(singleArray, num2);
                    if (!nullable.HasValue)
                    {
                        bool?nullable1 = new bool?((this.talkController == null ? false : this.talkController.ShouldSend()));
                        nullable = nullable1;
                        value    = nullable1.Value;
                    }
                    else
                    {
                        value = nullable.Value;
                    }
                    if (value)
                    {
                        this.talkTimer = 1f;
                        this.OnAudioAvailable(singleArray);
                    }
                    USpeakPoolUtils.Return(singleArray);
                    num2 = num2 + sampleSize;
                }
                this.lastReadPos = num2;
            }
            else
            {
                if (num1 > sampleSize)
                {
                    float[] singleArray1 = new float[num1 - 1];
                    this.recording.GetData(singleArray1, this.lastReadPos);
                    if (this.talkController == null || this.talkController.ShouldSend())
                    {
                        this.talkTimer = 1f;
                        this.OnAudioAvailable(singleArray1);
                    }
                }
                this.lastReadPos = position;
            }
        }
        catch (Exception exception)
        {
        }
        this.ProcessPendingEncodeBuffer();
        bool flag = true;

        if (this.SendingMode == SendBehavior.RecordThenSend && this.talkController != null)
        {
            if (!nullable.HasValue)
            {
                bool?nullable2 = new bool?(this.talkController.ShouldSend());
                nullable = nullable2;
                num      = (int)nullable2.Value;
            }
            else
            {
                num = (int)nullable.Value;
            }
            flag = num == 0;
        }
        USpeaker uSpeaker3 = this;

        uSpeaker3.sendTimer = uSpeaker3.sendTimer + Time.deltaTime;
        if (this.sendTimer >= this.sendt && flag)
        {
            this.sendTimer = 0f;
            this.tempSendBytes.Clear();
            foreach (USpeakFrameContainer uSpeakFrameContainer in this.sendBuffer)
            {
                this.tempSendBytes.AddRange(uSpeakFrameContainer.ToByteArray());
            }
            this.sendBuffer.Clear();
            if (this.tempSendBytes.Count > 0)
            {
                this.audioHandler.USpeakOnSerializeAudio(this.tempSendBytes.ToArray());
            }
        }
    }
Example #34
0
        private byte[] encode(short[] rawData, BandMode mode)
        {
            //var encoder = new NSpeex.SpeexEncoder( NSpeex.BandMode.Narrow );

            rawData = ExpandShortArray(rawData);

            if (mode == BandMode.Wide)
            {
                rawData = PadShortArray(rawData, 223);
            }
            else
            {
                rawData = PadShortArray(rawData, 80);
            }

            var encoder = m_wide_enc;

            if (mode == BandMode.Narrow)
            {
                encoder = m_narrow_enc;
            }

            encoder.Quality = 1;
            encoder.VBR     = true;

            var inDataSize = rawData.Length;
            //var padding = ( inDataSize % encoder.FrameSize );
            int mult_factor      = encoder.FrameSize;
            int half_mult_factor = mult_factor / 2;

            //var last_data_size = inDataSize;
            //var indatasize2 = next_multiple( inDataSize, mult_factor );
            //Debug.Log( inDataSize );
            inDataSize = mult_factor * ((inDataSize + half_mult_factor) / mult_factor);                 //find the next multiple of frame size
            //inDataSize = indatasize2;

            //if( inDataSize < last_data_size )
            //{
            //    Debug.LogWarning( last_data_size + ", " + inDataSize );
            //}

            while (inDataSize < rawData.Length)
            {
                inDataSize += encoder.FrameSize * 2;
            }

            int padding = inDataSize - rawData.Length;

            var inData = new short[inDataSize];

            System.Array.Copy(rawData, 0, inData, 0, rawData.Length);

            // inData = ExpandShortArray( inData );

            //var encodingFrameSize = encoder.FrameSize;
            var encodedBuffer = new byte[rawData.Length * 2];

            try
            {
                var byte_count = encoder.Encode(inData, 0, inData.Length, encodedBuffer, 0, encodedBuffer.Length);
                var encd       = new byte[byte_count + 8];
                System.Array.Copy(encodedBuffer, 0, encd, 8, byte_count);
                byte[] sample_count = BitConverter.GetBytes(rawData.Length);
                byte[] pad_bc       = BitConverter.GetBytes(padding);
                System.Array.Copy(sample_count, 0, encd, 0, 4);
                System.Array.Copy(pad_bc, 0, encd, 4, 4);
                return(encd);
            }
            catch (Exception e)
            {
                Debug.Log(e.Message);
                throw e;
            }
        }
Example #35
0
        public static float[] DecompressAudio(byte[] data, int samples, int channels, bool threeD, BandMode mode, ICodec Codec, float gain)
        {
            int frequency = 0xfa0;

            if (mode == BandMode.Narrow)
            {
                frequency = 0x1f40;
            }
            else if (mode == BandMode.Wide)
            {
                frequency = 0x3e80;
            }
            byte[]  buffer     = data;
            short[] collection = Codec.Decode(buffer, mode);
            tmp.Clear();
            tmp.AddRange(collection);
            USpeakPoolUtils.Return(collection);
            return(USpeakAudioClipConverter.ShortsToAudioData(tmp.ToArray(), channels, frequency, threeD, gain));
        }
        /// <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));
        }
Example #37
0
 public ObservableSpeexEncoder(int bufferSize, BandMode mode)
 {
     _encoder            = new SpeexEncoder(mode);
     _encoderInputBuffer = new WaveBuffer(bufferSize);
 }
Example #38
0
 // Token: 0x06004C1B RID: 19483 RVA: 0x00197516 File Offset: 0x00195916
 public void Initialize(BandMode bandMode, BitRate bitrate, FrameDuration duration)
 {
 }
Example #39
0
    void Update()
    {
        // only update device list if this USpeaker is going to be recording
        if (SpeakerMode == SpeakerMode.Local)
        {
            // update microphone device list
            if (Time.time >= lastDeviceUpdate)
            {
                lastDeviceUpdate = Time.time + 2f;
                micDeviceList    = Microphone.devices;
            }
        }

        talkTimer -= Time.deltaTime;

        audio.volume = SpeakerVolume;

        if (last3DMode != _3DMode)
        {
            last3DMode = _3DMode;

            StopPlaying();
            audio.clip = AudioClip.Create("vc", audioFrequency * 10, 1, audioFrequency, (_3DMode == ThreeDMode.Full3D), false);
            audio.loop = true;
        }

        //speaker pan mode? Calculate it
        if (_3DMode == ThreeDMode.SpeakerPan)
        {
            Transform listener = Camera.main.transform;
            Vector3   side     = Vector3.Cross(listener.up, listener.forward);
            side.Normalize();

            float x = Vector3.Dot(transform.position - listener.position, side);
            float z = Vector3.Dot(transform.position - listener.position, listener.forward);

            float angle = Mathf.Atan2(x, z);

            float pan = Mathf.Sin(angle);

            audio.pan = pan;
        }

        // currently playing audio
        if (audio.isPlaying)
        {
            // last played time exceeded audio length - add play time
            if (lastTime > audio.time)
            {
                played += audio.clip.length;
            }

            // update last played time
            lastTime = audio.time;

            // we've played past the audio we received - stop playing and wait for more data
            if (played + audio.time >= received)
            {
                StopPlaying();
                shouldPlay = false;
            }
        }
        else
        {
            // should play audio? Play audio after countdown
            if (shouldPlay)
            {
                playDelay -= Time.deltaTime;

                if (playDelay <= 0)
                {
                    audio.Play();
                    // Debug.Log( "started playing at time: " + Time.time );
                }
            }
        }

        if (SpeakerMode == SpeakerMode.Remote)
        {
            return;
        }

        if (audioHandler == null)
        {
            return;
        }

        if (micDeviceList.Length == 0)
        {
            return;
        }
        else
        {
            if (string.IsNullOrEmpty(InputDeviceName))
            {
                InputDeviceName = currentDeviceName;
            }

            if (string.IsNullOrEmpty(currentDeviceName))
            {
                if (waitingToStartRec)
                {
                    micFoundDelay--;
                    if (micFoundDelay <= 0)
                    {
                        micFoundDelay     = 0;
                        waitingToStartRec = false;

                        print("New device found: " + currentDeviceName);
                        InputDeviceID     = 0;
                        InputDeviceName   = micDeviceList[0];
                        currentDeviceName = micDeviceList[0];

                        recording = Microphone.Start(currentDeviceName, true, 5, audioFrequency);

                        lastReadPos = 0;
                        sendBuffer.Clear();
                        recordedChunkCount = 0;

                        UpdateSettings();
                    }
                }
                else
                {
                    waitingToStartRec = true;
                    micFoundDelay     = 5;
                }
            }
            else
            {
                // switch to new device
                if (InputDeviceName != currentDeviceName)
                {
                    Microphone.End(currentDeviceName);
                    print("Using input device: " + InputDeviceName);
                    currentDeviceName = InputDeviceName;

                    recording = Microphone.Start(currentDeviceName, true, 5, audioFrequency);

                    lastReadPos = 0;
                    sendBuffer.Clear();
                    recordedChunkCount = 0;
                }

                // the device list changed
                if (micDeviceList[Mathf.Min(InputDeviceID, micDeviceList.Length - 1)] != currentDeviceName)
                {
                    // attempt to find the existing device
                    bool found = false;
                    for (int i = 0; i < Microphone.devices.Length; i++)
                    {
                        if (micDeviceList[i] == currentDeviceName)
                        {
                            InputDeviceID = i;
                            found         = true;
                        }
                    }

                    // existing device must have been unplugged, switch to the default audio device
                    if (!found)
                    {
                        InputDeviceID     = 0;
                        InputDeviceName   = micDeviceList[0];
                        currentDeviceName = micDeviceList[0];

                        print("Device unplugged, switching to: " + currentDeviceName);

                        recording = Microphone.Start(currentDeviceName, true, 5, audioFrequency);

                        lastReadPos = 0;
                        sendBuffer.Clear();
                        recordedChunkCount = 0;
                    }
                }
            }
        }

        if (lastBandMode != BandwidthMode || lastCodec != Codec)
        {
            UpdateSettings();

            lastBandMode = BandwidthMode;
            lastCodec    = Codec;
        }

        if (recording == null)
        {
            return;
        }

        int readPos = Microphone.GetPosition(currentDeviceName);

        int realReadPos = readPos + recording.samples * recordedChunkCount;

        if (realReadPos < lastReadPos)
        {
            recordedChunkCount++;
        }

        readPos += recording.samples * recordedChunkCount;

        if (readPos <= overlap)
        {
            return;
        }

        bool talkController_shouldSend = (talkController == null || talkController.ShouldSend());

        //read in the latest chunk(s) of audio
        try
        {
            int sz      = readPos - lastReadPos;
            int minSize = codecMgr.Codecs[Codec].GetSampleSize(audioFrequency);

            if (minSize == 0)
            {
                minSize = 100;
            }

            int currentIDX = lastReadPos;
            int numClips   = Mathf.FloorToInt(sz / minSize);

            for (int i = 0; i < numClips; i++)
            {
                float[] d = USpeakPoolUtils.GetFloat(minSize);

                recording.GetData(d, currentIDX % recording.samples);
                if (talkController_shouldSend)
                {
                    talkTimer = 1f;
                    OnAudioAvailable(d);
                }

                USpeakPoolUtils.Return(d);

                currentIDX += minSize;
            }

            lastReadPos = currentIDX;
        }
        catch (System.Exception) { }

        ProcessPendingEncodeBuffer();

        bool allowSend = true;

        if (SendingMode == SendBehavior.RecordThenSend && talkController != null)
        {
            allowSend = !talkController_shouldSend;
        }

        sendTimer += Time.deltaTime;
        if (sendTimer >= sendt && allowSend)
        {
            sendTimer = 0.0f;

            //flush the send buffer
            tempSendBytes.Clear();
            foreach (USpeakFrameContainer frame in sendBuffer)
            {
                tempSendBytes.AddRange(frame.ToByteArray());
            }
            sendBuffer.Clear();

            if (tempSendBytes.Count > 0)
            {
                // Debug.Log( "Sending at time: " + Time.time );
                audioHandler.USpeakOnSerializeAudio(tempSendBytes.ToArray());
            }
        }
    }
Example #40
0
 public byte[] Encode(short[] data, BandMode mode)
 {
     return(MuLawCodec.MuLawEncoder.MuLawEncode(data));
 }
Example #41
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());
 }
Example #42
0
        public static float[] DecompressAudio(byte[] data, int samples, int channels, bool threeD, BandMode mode, ICodec Codec, float gain)
        {
            int num = 4000;

            if (mode == BandMode.Narrow)
            {
                num = 8000;
            }
            else if (mode == BandMode.Wide)
            {
                num = 16000;
            }
            short[] numArray = Codec.Decode(data, mode);
            USpeakAudioClipCompressor.tmp.Clear();
            USpeakAudioClipCompressor.tmp.AddRange(numArray);
            USpeakPoolUtils.Return(numArray);
            return(USpeakAudioClipConverter.ShortsToAudioData(USpeakAudioClipCompressor.tmp.ToArray(), channels, num, threeD, gain));
        }
Example #43
0
 public USpeakSettingsData()
 {
     bandMode = BandMode.Narrow;
     Codec    = 0;
 }