コード例 #1
0
        private void Update()
        {
            if (AudioSource.mute && !Input.Use)
            {
                return;
            }

            if (Input.Use)
            {
                if (!AudioSource.isPlaying)
                {
                    int microphoneSamples = Microphone.GetPosition(Input.CurrentAudioInput);

                    if (microphoneSamples / AudioReactInput.Frequency > Input.Delay)
                    {
                        AudioSource.timeSamples = (int)(microphoneSamples - (Input.Delay * AudioReactInput.Frequency));
                        AudioSource.Play();
                    }
                }

                audioMultiplier = Input.AudioMultiplier;
            }
            else
            {
                audioMultiplier = AudioSourceMultiplier;
            }

            AudioSource.GetSpectrumData(frequencyData, 0, FFTWindow.BlackmanHarris);
            AudioSource.GetOutputData(outputData, 0);
            audioVolume = AudioSource.volume;

            for (int i = 0; i < FrequencySamples.Length; i++)
            {
                Action         action;
                FrequencyRange frequencyRange = (FrequencyRange)i;

                if (frequencyRange != FrequencyRange.Decibel)
                {
                    // Get Frequency Volume
                    action = new Action(() =>
                    {
                        float[] range       = GetFrequencyRange(frequencyRange);
                        float frequencyLow  = range[0];
                        float frequencyHigh = range[1];

                        int n1 = (int)Mathf.Floor(frequencyLow * samplesAmount / frequencyMax);
                        int n2 = (int)Mathf.Floor(frequencyHigh * samplesAmount / frequencyMax);

                        float sum = 0;

                        for (int j = n1; j <= n2; j++)
                        {
                            if (j < frequencyData.Length && j >= 0)
                            {
                                sum += Mathf.Abs(frequencyData[j]);
                            }
                        }

                        if (Input.Use)
                        {
                            if (sum < Input.VolumeThreshold)
                            {
                                sum = 0;
                            }
                        }
                        else
                        {
                            sum = sum * audioVolume;
                        }

                        lock (FrequencySamples)
                        {
                            FrequencySamples[i] = sum / (n2 - n1 + 1);
                        }
                    });
                }
                else
                {
                    // get RMS
                    action = new Action(() =>
                    {
                        float sum = 0;

                        for (int j = 0; j < outputData.Length; j++)
                        {
                            sum += outputData[j] * outputData[j];
                        }

                        float rmsValue = Mathf.Sqrt(sum / samplesAmount);

                        if (Input.Use)
                        {
                            if (rmsValue < Input.VolumeThreshold)
                            {
                                rmsValue = 0;
                            }
                        }
                        else
                        {
                            rmsValue = rmsValue * audioVolume;
                        }

                        lock (FrequencySamples)
                        {
                            FrequencySamples[i] = rmsValue;
                        }
                    });
                }

                if (threadPool.MaxThreads > 1)
                {
                    threadPool.CreateThread(action, "GetFrequencySample[" + i + "]");
                }
                else
                {
                    action();
                }
            }

            if (threadPool.MaxThreads > 1)
            {
                for (int i = 0; i < FrequencySamples.Length; i++)
                {
                    threadPool.JoinThread(i);
                }

                threadPool.OnUpdate();
            }

            for (int i = 0; i < FrequencySamples.Length; i++)
            {
                FrequencySamples[i] *= audioMultiplier;
            }
        }