Exemplo n.º 1
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);
 }
Exemplo n.º 2
0
 // 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);
 }
Exemplo n.º 3
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);
 }
        /// <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);
        }
Exemplo n.º 5
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());
            }
        }
    }
Exemplo n.º 6
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());
            }
        }
    }