private void TestPitchDetection(float[] buffer, IPitchDetector pitchDetector) { for (int midiNoteNumber = 45; midiNoteNumber < 63; midiNoteNumber++) { float freq = (float)(8.175 * Math.Pow(1.05946309, midiNoteNumber)); SetFrequency(buffer, freq); float detectedPitch = pitchDetector.DetectPitch(buffer, buffer.Length); // since the autocorrelator works with a lag, give it two shots at the same buffer detectedPitch = pitchDetector.DetectPitch(buffer, buffer.Length); Console.WriteLine("Testing for {0:F2}Hz, got {1:F2}Hz", freq, detectedPitch); //Assert.AreEqual(detectedPitch, freq, 0.5); } }
private void TestPitchDetection(float[] buffer, IPitchDetector pitchDetector) { for (int midiNoteNumber = 45; midiNoteNumber < 63; midiNoteNumber++) { float freq = (float)(8.175 * Math.Pow(1.05946309, midiNoteNumber)); SetFrequency(buffer, freq); float detectedPitch = pitchDetector.DetectPitch(buffer, buffer.Length); // since the autocorrelator works with a lag, give it two shots at the same buffer detectedPitch = pitchDetector.DetectPitch(buffer, buffer.Length); Console.WriteLine("Testing for {0:F2}Hz, got {1:F2}Hz", freq, detectedPitch); //Assert.AreEqual(detectedPitch, freq, 0.5); } }
public int Read(byte[] buffer, int offset, int count) { if (waveBuffer == null || waveBuffer.MaxSize < count) { waveBuffer = new WaveBuffer(count); } int bytesRead = source.Read(waveBuffer, 0, count); //Debug.Assert(bytesRead == count); // the last bit sometimes needs to be rounded up: if (bytesRead > 0) { bytesRead = count; } //pitchsource->getPitches(); int frames = bytesRead / sizeof(float); // MRH: was count float pitch = pitchDetector.DetectPitch(waveBuffer.FloatBuffer, frames); // MRH: an attempt to make it less "warbly" by holding onto the pitch for at least one more buffer if (pitch == 0f && release < maxHold) { pitch = previousPitch; release++; } else { this.previousPitch = pitch; release = 0; } int midiNoteNumber = 40; float targetPitch = (float)(8.175 * Math.Pow(1.05946309, midiNoteNumber)); WaveBuffer outBuffer = new WaveBuffer(buffer); pitchShifter.ShiftPitch(waveBuffer.FloatBuffer, pitch, targetPitch, outBuffer.FloatBuffer, frames); return(frames * 4); }
/// <summary> /// Read the buffer and launch the Pitch Detection that will be saved in the Pitch Class. /// </summary> /// <param name="buffer">Byte buffer of the raw signal</param> /// <param name="offset">Offset for the buffer</param> /// <param name="count">Lengh of the buffer</param> /// <returns></returns> public int Read(byte[] buffer, int offset, int count) { if (waveBuffer == null || waveBuffer.MaxSize < count) { waveBuffer = new WaveBuffer(count); } int bytesRead = source.Read(waveBuffer, 0, count); //The last bit sometimes needs to be rounded up: if (bytesRead > 0) { bytesRead = count; } int frames = bytesRead / sizeof(float); //Launch the pitch detection float pitch = pitchDetector.DetectPitch(waveBuffer.FloatBuffer, frames); if (pitch == 0f && release < maxHold) { pitch = previousPitch; release++; } else { this.previousPitch = pitch; release = 0; } //Save the pitch in an array in the Pitch Class lock (AR.Lock) { AR.getPitch().Add(pitch); if (AR.isRecording) { AR.getPitchRecorded().Add(pitch); } } return(bytesRead); }