private void TestPitchDetection(Func <int, IAudioSamplesAnalyzer> audioSamplesAnalyzerProvider)
    {
        MicProfile micProfile = CreateDummyMicProfile();

        string assetsPath      = Application.dataPath;
        string sineWaveToneDir = assetsPath + "/Common/Audio/SineWaveTones/";
        Dictionary <string, string> pathToExpectedMidiNoteNameMap = new();

        pathToExpectedMidiNoteNameMap.Add(sineWaveToneDir + "sine-wave-a3-220hz.ogg", "A3");
        pathToExpectedMidiNoteNameMap.Add(sineWaveToneDir + "sine-wave-a4-440hz.ogg", "A4");
        pathToExpectedMidiNoteNameMap.Add(sineWaveToneDir + "sine-wave-a5-880hz.ogg", "A5");
        pathToExpectedMidiNoteNameMap.Add(sineWaveToneDir + "sine-wave-c2-61,74hz.ogg", "C2");
        pathToExpectedMidiNoteNameMap.Add(sineWaveToneDir + "sine-wave-c4-261,64hz.ogg", "C4");
        pathToExpectedMidiNoteNameMap.Add(sineWaveToneDir + "sine-wave-c6-1046,50hz.ogg", "C6");

        foreach (KeyValuePair <string, string> pathAndNoteName in pathToExpectedMidiNoteNameMap)
        {
            // Load the audio clip
            string    uri       = "file://" + pathAndNoteName.Key;
            AudioClip audioClip = AudioUtils.GetAudioClipUncached(uri, false);
            float[]   samples   = new float[audioClip.samples];
            audioClip.GetData(samples, 0);

            // Analyze the samples
            IAudioSamplesAnalyzer audioSamplesAnalyzer = audioSamplesAnalyzerProvider(audioClip.frequency);
            PitchEvent            pitchEvent           = audioSamplesAnalyzer.ProcessAudioSamples(samples, 0, samples.Length - 1, micProfile);

            // Check result
            Assert.NotNull(pitchEvent, $"No pitch detected when analyzing audio resource {uri}");
            string expectedName = pathAndNoteName.Value;
            string analyzedName = MidiUtils.GetAbsoluteName(pitchEvent.MidiNote);
            Assert.AreEqual(expectedName, analyzedName,
                            $"Expected {expectedName} but was {analyzedName} when analyzing audio resource {uri}");
        }
    }
Esempio n. 2
0
    void OnEnable()
    {
        audioSource = GetComponent <AudioSource>();

        audioSamplesAnalyzer = CreateAudioSamplesAnalyzer();
        audioSamplesAnalyzer.Enable();
    }
Esempio n. 3
0
    void Start()
    {
        // Restart recording if companion app for mic input has reconnected
        serverSideConnectRequestManager.ClientConnectedEventStream
        .Where(clientConnectionEvent => clientConnectionEvent.IsConnected &&
               !micSampleRecorder.IsRecording &&
               micProfile != null &&
               micProfile.ConnectedClientId == clientConnectionEvent.ConnectedClientHandler.ClientId)
        .Subscribe(_ => micSampleRecorder.StartRecording())
        .AddTo(gameObject);

        // Find first sentence to analyze
        SetRecordingSentence(recordingSentenceIndex);

        if (micProfile != null)
        {
            roundingDistance             = playerProfile.Difficulty.GetRoundingDistance();
            micSampleRecorder.MicProfile = micProfile;
            micSampleRecorder.StartRecording();

            // The AudioSampleAnalyzer uses the MicSampleRecorder's sampleRateHz. Thus, it must be initialized after the MicSampleRecorder.
            audioSamplesAnalyzer = MicPitchTracker.CreateAudioSamplesAnalyzer(settings.AudioSettings.pitchDetectionAlgorithm, micSampleRecorder.SampleRateHz);
            audioSamplesAnalyzer.Enable();
        }
        else
        {
            Debug.LogWarning($"No mic for player {playerProfile.Name}. Not recording player notes.");
            gameObject.SetActive(false);
        }
    }
Esempio n. 4
0
 private void OnPitchDetectionAlgorithmChanged(EPitchDetectionAlgorithm newValue)
 {
     if (MicProfile != null)
     {
         audioSamplesAnalyzer = CreateAudioSamplesAnalyzer(newValue, MicSampleRecorder.SampleRateHz);
         audioSamplesAnalyzer.Enable();
     }
 }
Esempio n. 5
0
    void Start()
    {
        // Update label in inspector for debugging.
        pitchEventStream.Subscribe(UpdateLastMidiNoteFields);
        MicSampleRecorder.RecordingEventStream.Subscribe(OnRecordingEvent);

        audioSamplesAnalyzer = CreateAudioSamplesAnalyzer(settings.AudioSettings.pitchDetectionAlgorithm, MicSampleRecorder.SampleRateHz);
        settings.AudioSettings.ObserveEveryValueChanged(it => it.pitchDetectionAlgorithm)
        .Subscribe(OnPitchDetectionAlgorithmChanged);
    }
Esempio n. 6
0
    void Start()
    {
        // Find first sentence to analyze
        SetRecordingSentence(recordingSentenceIndex);

        if (micProfile != null)
        {
            roundingDistance     = playerProfile.Difficulty.GetRoundingDistance();
            audioSamplesAnalyzer = MicPitchTracker.CreateAudioSamplesAnalyzer(settings.AudioSettings.pitchDetectionAlgorithm, micSampleRecorder.SampleRateHz);
            audioSamplesAnalyzer.Enable();
            micSampleRecorder.MicProfile = micProfile;
            micSampleRecorder.StartRecording();
        }
        else
        {
            Debug.LogWarning($"No mic for player {playerProfile.Name}. Not recording player notes.");
            gameObject.SetActive(false);
        }
    }
Esempio n. 7
0
 void OnEnable()
 {
     audioSource          = GetComponent <AudioSource>();
     audioSamplesAnalyzer = new CamdAudioSamplesAnalyzer(SampleRateHz);
     audioSamplesAnalyzer.Enable();
 }