Exemple #1
0
    // - - -

    void FlushToListeners()
    {
        int writeHead = Microphone.GetPosition(micDeviceName);

        if (readHead == writeHead || potBuffers == null)
        {
            return;
        }

        // Say audio.clip.samples (S)  = 100
        // if w=1, r=0, we want 1 sample.  ( S + 1 - 0 ) % S = 1 YES
        // if w=0, r=99, we want 1 sample.  ( S + 0 - 99 ) % S = 1 YES
        int nFloatsToGet = (audioSource.clip.samples + writeHead - readHead) % audioSource.clip.samples;

        // Get buffer values
        for (int k = POTBuf.POT_max; k >= POTBuf.POT_min; k--)
        {
            POTBuf B = potBuffers[k];

            int n = B.buf.Length; // i.e.  1 << k;

            while (nFloatsToGet >= n)
            {
                // If the read length from the offset is longer than the clip length,
                //   the read will wrap around and read the remaining samples
                //   from the start of the clip.
                audioSource.clip.GetData(B.buf, readHead);
                readHead = (readHead + n) % audioSource.clip.samples;

                if (countdownToFFTAnalysis <= 0)
                {
                    if (processNewMicrophoneBuffer != null)
                    {
                        processNewMicrophoneBuffer(B.buf);
                    }
                    ProcessAudioBufferFFT(B.buf);
                    countdownToFFTAnalysis = FFT_INTERVAL;
                }

                ProcessAudioBufferRMS(B.buf);

                // moving away from other programs processing raw mic input
                //if (processMicBufferDelegate != null)
                //    processMicBufferDelegate(B.buf);

                B.Cycle();
                nFloatsToGet -= n;
            }
        }
    }
Exemple #2
0
    void FlushToListeners()
    {
        var audio     = GetComponent <AudioSource>();
        int writeHead = Microphone.GetPosition("");

        if (readHead == writeHead || potBuffers == null)
        {
            return;
        }

        // Say audio.clip.samples (S)  = 100
        // if w=1, r=0, we want 1 sample.  ( S + 1 - 0 ) % S = 1 YES
        // if w=0, r=99, we want 1 sample.  ( S + 0 - 99 ) % S = 1 YES
        int nFloatsToGet = (audio.clip.samples + writeHead - readHead) % audio.clip.samples;

        for (int k = POTBuf.POT_max; k >= POTBuf.POT_min; k--)
        {
            POTBuf B = potBuffers[k];

            int n = B.buf.Length; // i.e.  1 << k;

            while (nFloatsToGet >= n)
            {
                // If the read length from the offset is longer than the clip length,
                //   the read will wrap around and read the remaining samples
                //   from the start of the clip.
                audio.clip.GetData(B.buf, readHead);
                readHead = (readHead + n) % audio.clip.samples;

                if (floatsInDelegate != null)
                {
                    floatsInDelegate(B.buf);
                }

                B.Cycle();
                nFloatsToGet -= n;
            }
        }
    }