/// <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); }
/// <summary> /// In the calibration test window the user moves the pitch bar with his pitch into areas corresponding to the calibration. If the user can move the bar comfortably he can proceed /// to finish the calibration. /// </summary> private void ShowCalibrationTestWindow() { StopCalibration(); CalibrationWindow.SetActive(false); CalibrationTestWindow.SetActive(true); ShowCalibrationButton.gameObject.SetActive(true); ShowCalibrationTestButton.gameObject.SetActive(false); MicrophoneManager.Instance.StartRecording(); // LowPitchArea and visualization start from the same point but area can only be max half the size so // we have to multiply the area by factor 2 and add a small offset so the threshold for a successful calibration is smaller than the lowest pitch LowPitchArea.value = (LowPitchVisualization.value * 2f) + 0.1f; // HighPitchArea and visualization start from the opposite point but area can only be max half the size so // we have to multiply the area by factor 2 and add a small offset so the threshold for a successful calibration is smaller than the higher pitch HighPitchArea.value = (1f - HighPitchVisualization.value) * 2f + 0.1f; 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; } PitchVisualization.value = MathUtility.ConvertRange(m_CalibratedLowPitch, m_CalibratedHighPitch, 0f, 1f, pitchRecord.Pitch); }; m_PitchTracker.PitchDetected += PitchDetected; m_GetPitchCoroutine = StartCoroutine(ListenForPitches()); }