Exemplo n.º 1
0
        /// <summary>
        /// Calculates the pitch average proccessed by PitchTracker and adjusts the pitch bar.
        /// </summary>
        /// <param name="onComplete"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <param name="durationSlider"></param>
        /// <returns></returns>
        IEnumerator GetPitchAverage(Action <float> onComplete, float min, float max, Slider durationSlider)
        {
            float pitches    = 0f;
            int   pitchCount = 0;

            PitchTracker.PitchDetectedHandler PitchDetected = (sender, pitchRecord) =>
            {
                if (pitchRecord.Pitch < 20.0f) // pitches under 20Hz should not occur normally, as it is already not a MIDI note
                {
                    return;
                }

                pitches += pitchRecord.Pitch;
                pitchCount++;
            };

            m_PitchTracker.PitchDetected += PitchDetected;

            float currentDuration = 0f;

            while (currentDuration < m_CalibrationDuration)
            {
                durationSlider.value = Mathf.Lerp(0f, 1f, currentDuration / m_CalibrationDuration);
                m_PitchTracker.ProcessBuffer(MicrophoneManager.Instance.GetSamples());
                if (m_PitchTracker.CurrentPitchRecord.Pitch > 19f)
                {
                    PitchVisualization.value = MathUtility.ConvertRange(min, max, 0f, 1f, pitches / pitchCount);
                }
                currentDuration += Time.deltaTime;
                yield return(null);
            }

            m_PitchTracker.PitchDetected -= PitchDetected;
            onComplete(pitches / pitchCount);
        }