public void StartDetect(int inputDevice) { this.Dispatcher.BeginInvoke((ThreadStart) delegate() { waveIn = new WaveInEvent(); waveIn.DeviceNumber = inputDevice; waveIn.WaveFormat = new WaveFormat(44100, 1); waveIn.DataAvailable += sound.WaveIn_DataAvailable; sound.bufferedWaveProvider = new BufferedWaveProvider(waveIn.WaveFormat); // begin record waveIn.StartRecording(); IWaveProvider stream = new Wave16ToFloatProvider(sound.bufferedWaveProvider); Pitch pitch = new Pitch(stream); byte[] buffer = new byte[8192]; int bytesRead; bytesRead = stream.Read(buffer, 0, buffer.Length); freq = pitch.Get(buffer); if (freq != 0) { ReturnFreq(); } }); }
IEnumerator UpdateReading() { while (true) { bytesRead = stream.Read(buffer, 0, buffer.Length); Frequency = pitch.Get(buffer); Tuple <string, string> notes = GetNote(Frequency); if (notes != null) { Note = notes.Item1; NoteWithoutOctave = notes.Item2; } if (Frequency != 0) { noteSampleDelta = Time.fixedTime - fixedTimeSinceLastNote; fixedTimeSinceLastNote = Time.fixedTime; // if the same note has been played if (!lastFrameHadNote || Note != lastNote) { OnNewNote?.Invoke(Note, Frequency); print("nova nota!!"); CurrentNote = NoteWithoutOctave; } else { CurrentNote = string.Empty; } begin = Time.fixedTime; lastFrameHadNote = true; lastNote = Note; } else if (Time.fixedTime - begin > 0.4) { lastFrameHadNote = false; print("falta nota"); begin = Time.fixedTime; CurrentNote = string.Empty; } yield return(null); } }