Esempio n. 1
0
    public void StartPitchDetection()
    {
        if (startedPitchDetection)
        {
            Debug.Log("Mic recoding already started.");
            return;
        }

        startedPitchDetection = true;
        List <string> soundcards = new List <string>(UnityEngine.Microphone.devices);

        // Check for microphone existence.
        if (!soundcards.Contains(MicProfile.Name))
        {
            string micDevicesCsv = string.Join(",", soundcards);
            Debug.LogError($"Did not find mic '{MicProfile.Name}'. Available mic devices: {micDevicesCsv}");
            startedPitchDetection = false;
            return;
        }
        Debug.Log($"Start recording with '{MicProfile.Name}'");

        micAmplifyMultiplier = micProfile.AmplificationMultiplier();

        // Code for low-latency microphone input taken from
        // https://support.unity3d.com/hc/en-us/articles/206485253-How-do-I-get-Unity-to-playback-a-Microphone-input-in-real-time-
        // It seems that there is still a latency of more than 200ms, which is too much for real-time processing.
        micAudioClip = UnityEngine.Microphone.Start(MicProfile.Name, true, 1, SampleRateHz);
        while (UnityEngine.Microphone.GetPosition(MicProfile.Name) <= 0) /* Busy waiting */ } {
    public void SetMicProfile(MicProfile micProfile)
    {
        micPitchTracker.MicProfile = micProfile;
        if (!string.IsNullOrEmpty(micProfile.Name))
        {
            micPitchTracker.MicSampleRecorder.StartRecording();
        }
        micAmplifyMultiplier = micProfile.AmplificationMultiplier();

        if (disposable != null)
        {
            disposable.Dispose();
        }
        disposable = micProfile.ObserveEveryValueChanged(it => it.Amplification)
                     .Subscribe(newAmplification => micAmplifyMultiplier = micProfile.AmplificationMultiplier());
    }