Exemplo n.º 1
0
        IEnumerator ReadRawAudio()
        {
            int loops      = 0;
            int readAbsPos = Microphone.GetPosition(CurrentDeviceName);
            int prevPos    = readAbsPos;

            float[] temp = new float[Sample.Length];

            while (AudioClip != null && Microphone.IsRecording(CurrentDeviceName) && IsRecording)
            {
                bool isNewDataAvailable = true;

                while (isNewDataAvailable && AudioClip)
                {
                    int currPos = Microphone.GetPosition(CurrentDeviceName);
                    if (currPos < prevPos)
                    {
                        loops++;
                    }
                    prevPos = currPos;

                    var   currAbsPos     = loops * AudioClip.samples + currPos;
                    var   nextReadAbsPos = readAbsPos + temp.Length;
                    float levelMax       = 0;

                    if (nextReadAbsPos < currAbsPos)
                    {
                        AudioClip.GetData(temp, readAbsPos % AudioClip.samples);

                        for (int i = 0; i < temp.Length; i++)
                        {
                            float wavePeak = temp[i] * temp[i];
                            if (levelMax < wavePeak)
                            {
                                levelMax = wavePeak;
                            }
                        }

                        Sample = temp;
                        m_SampleCount++;
                        OnSampleReady?.Invoke(m_SampleCount, Sample, levelMax);

                        readAbsPos         = nextReadAbsPos;
                        isNewDataAvailable = true;
                    }
                    else
                    {
                        isNewDataAvailable = false;
                    }
                }

                yield return(null);
            }
        }
Exemplo n.º 2
0
        private IEnumerator ReadRawAudio()
        {
            int loops       = 0;
            int readAbsPos  = 0;
            int prevPos     = 0;
            int sampleCount = 0;
            var temp        = new float[samples.Length];

            while (micClip != null && Microphone.IsRecording(null))
            {
                bool isNewDataAvailable = true;

                while (isNewDataAvailable)
                {
                    int currPos = Microphone.GetPosition(null);
                    if (currPos < prevPos)
                    {
                        loops++;
                    }
                    prevPos = currPos;

                    var currAbsPos     = loops * micClip.samples + currPos;
                    var nextReadAbsPos = readAbsPos + temp.Length;

                    if (nextReadAbsPos < currAbsPos)
                    {
                        micClip.GetData(temp, readAbsPos % micClip.samples);
                        samples = temp;
                        sampleCount++;

                        //プッシュトゥトーク
                        if (inputDevices[0].DownSidePadPressing() || inputDevices[1].DownSidePadPressing() || Input.GetKey(KeyCode.E))
                        {
                            OnSampleReady?.Invoke(sampleCount, samples);
//                    Debug.Log("send");
                        }

                        readAbsPos         = nextReadAbsPos;
                        isNewDataAvailable = true;
                    }
                    else
                    {
                        isNewDataAvailable = false;
                    }
                }

                yield return(null);
            }
        }
Exemplo n.º 3
0
        IEnumerator ReadRawAudio()
        {
            int loops      = 0;
            int readAbsPos = 0;
            int prevPos    = 0;

            float[] temp = new float[Sample.Length];

            while (Clip != null && Microphone.IsRecording(CurrentDeviceName))
            {
                bool isNewDataAvailable = true;

                while (isNewDataAvailable)
                {
                    int currPos = Microphone.GetPosition(CurrentDeviceName);
                    if (currPos < prevPos)
                    {
                        loops++;
                    }
                    prevPos = currPos;

                    var currAbsPos     = loops * Clip.samples + currPos;
                    var nextReadAbsPos = readAbsPos + temp.Length;

                    if (nextReadAbsPos < currAbsPos)
                    {
                        Clip.GetData(temp, readAbsPos % Clip.samples);

                        Sample = temp;
                        m_SampleCount++;
                        if (OnSampleReady != null)
                        {
                            OnSampleReady.Invoke(m_SampleCount, Sample);
                        }

                        readAbsPos         = nextReadAbsPos;
                        isNewDataAvailable = true;
                    }
                    else
                    {
                        isNewDataAvailable = false;
                    }
                }
                yield return(null);
            }
        }
Exemplo n.º 4
0
        // Read raw audio
        protected virtual IEnumerator ReadRawAudio(int sampleDurationMS)
        {
            // Start recording
            OnStartRecording?.Invoke();

            // Get data
            AudioClip micClip       = GetMicClip();
            string    micDevice     = GetMicName();
            int       micSampleRate = GetMicSampleRate();

            // Setup sample
            int sampleTotal = AudioEncoding.samplerate / 1000 * sampleDurationMS * micClip.channels;

            float[] sample = new float[sampleTotal];

            // All needed data
            int loops        = 0;
            int readAbsPos   = Microphone.GetPosition(micDevice);
            int prevPos      = readAbsPos;
            int micTempTotal = micSampleRate / 1000 * sampleDurationMS * micClip.channels;
            int micDif       = micTempTotal / sampleTotal;

            float[] temp = new float[micTempTotal];

            // Continue reading
            while (micClip != null && Microphone.IsRecording(micDevice) && IsRecording)
            {
                bool isNewDataAvailable = true;

                while (isNewDataAvailable && micClip != null)
                {
                    int currPos = Microphone.GetPosition(micDevice);
                    if (currPos < prevPos)
                    {
                        loops++;
                    }
                    prevPos = currPos;

                    var currAbsPos     = loops * micClip.samples + currPos;
                    var nextReadAbsPos = readAbsPos + micTempTotal;

                    if (nextReadAbsPos < currAbsPos)
                    {
                        micClip.GetData(temp, readAbsPos % micClip.samples);

                        // Fill sample & get level max
                        float levelMax    = 0;
                        int   sampleIndex = 0;
                        for (int i = 0; i < temp.Length; i++)
                        {
                            float wavePeak = temp[i] * temp[i];
                            if (levelMax < wavePeak)
                            {
                                levelMax = wavePeak;
                            }
                            if (i % micDif == 0 && sampleIndex < sample.Length)
                            {
                                sample[sampleIndex] = temp[i];
                                sampleIndex++;
                            }
                        }

                        _sampleCount++;
                        OnSampleReady?.Invoke(_sampleCount, sample, levelMax);

                        readAbsPos = nextReadAbsPos;
                    }
                    else
                    {
                        isNewDataAvailable = false;
                    }
                }

                // Wait a moment
                yield return(null);
            }

            // Stop
            if (IsRecording)
            {
                StopRecording();
            }
        }