/// <summary> /// The pitch was detected - add the record /// </summary> /// <param name="pitch"></param> private void AddPitchRecord(float pitch) { var midiNote = 0; var midiCents = 0; PitchDsp.PitchToMidiNote(pitch, out midiNote, out midiCents); var record = new PitchRecord(); record.RecordIndex = curPitchIndex; record.Pitch = pitch; record.MidiNote = midiNote; record.MidiCents = midiCents; curPitchRecord = record; if (recordPitchRecords) { if (pitchRecordHistorySize > 0 && pitchRecords.Count >= pitchRecordHistorySize) { pitchRecords.RemoveAt(0); } pitchRecords.Add(record); } if (this.PitchDetected != null) { this.PitchDetected(this, record); } }
/// <summary> /// The pitch was detected - add the record /// </summary> /// <param name="pitch"></param> private PitchRecord AddPitchRecord(float pitch) { var midiNote = 0; var midiCents = 0; PitchDsp.PitchToMidiNote(pitch, out midiNote, out midiCents); var record = new PitchRecord(); record.RecordIndex = m_curPitchIndex; record.Pitch = pitch; record.MidiNote = midiNote; record.MidiCents = midiCents; m_curPitchRecord = record; if (m_recordPitchRecords) { if (m_pitchRecordHistorySize > 0 && m_pitchRecords.Count >= m_pitchRecordHistorySize) { m_pitchRecords.RemoveAt(0); } m_pitchRecords.Add(record); } //if (this.PitchDetected != null) // this.PitchDetected(this, record); return(record); }
/// <summary> /// The pitch was detected - add the record /// </summary> /// <param name="pitch"></param> private void AddPitchRecord(float pitch) { var midiNote = 0; var midiCents = 0; PitchDsp.PitchToMidiNote(pitch, out midiNote, out midiCents); var record = new PitchRecord { RecordIndex = m_curPitchIndex, Pitch = pitch, MidiNote = midiNote, MidiCents = midiCents }; m_curPitchRecord = record; if (m_recordPitchRecords) { if (m_pitchRecordHistorySize > 0 && m_pitchRecords.Count >= m_pitchRecordHistorySize) { m_pitchRecords.RemoveAt(0); } m_pitchRecords.Add(record); } if (this.PitchDetected != null) { this.PitchDetected(this, record); } }
/// <summary> /// Setup /// </summary> private void Setup() { if (m_sampleRate < 1.0f) { return; } m_dsp = new PitchDsp(m_sampleRate, kMinFreq, kMaxFreq, m_detectLevelThreshold); m_iirFilterLoLo = new IIRFilter { Proto = IIRFilter.ProtoType.Butterworth, Type = IIRFilter.FilterType.HP, Order = 5, FreqLow = 45.0f, SampleRate = (float)m_sampleRate }; m_iirFilterLoHi = new IIRFilter { Proto = IIRFilter.ProtoType.Butterworth, Type = IIRFilter.FilterType.LP, Order = 5, FreqHigh = 280.0f, SampleRate = (float)m_sampleRate }; m_iirFilterHiLo = new IIRFilter { Proto = IIRFilter.ProtoType.Butterworth, Type = IIRFilter.FilterType.HP, Order = 5, FreqLow = 45.0f, SampleRate = (float)m_sampleRate }; m_iirFilterHiHi = new IIRFilter { Proto = IIRFilter.ProtoType.Butterworth, Type = IIRFilter.FilterType.LP, Order = 5, FreqHigh = 1500.0f, SampleRate = (float)m_sampleRate }; m_detectOverlapSamples = (int)(kDetectOverlapSec * m_sampleRate); m_maxOverlapDiff = kMaxOctaveSecRate * kDetectOverlapSec; m_pitchBufSize = (int)(((1.0f / (float)kMinFreq) * 2.0f + ((kAvgCount - 1) * kAvgOffset)) * m_sampleRate) + 16; m_pitchBufLo = new float[m_pitchBufSize + m_detectOverlapSamples]; m_pitchBufHi = new float[m_pitchBufSize + m_detectOverlapSamples]; m_samplesPerPitchBlock = (int)Math.Round(m_sampleRate / m_pitchRecordsPerSecond); m_circularBufferLo = new CircularBuffer <float>((int)(kCircularBufSaveTime * m_sampleRate + 0.5f) + 10000); m_circularBufferHi = new CircularBuffer <float>((int)(kCircularBufSaveTime * m_sampleRate + 0.5f) + 10000); }
private void setNumbers(float pitch) { if (pitch < 0) { pitch = 0; } // Set pitch Pitch = pitch; MidiNote = PitchDsp.PitchToMidiNote(pitch); Note = PitchDsp.GetNoteName((int)MidiNote, true, true); }
/// <summary> /// Setup /// </summary> private void Setup() { if (sampleRate < 1.0f) { return; } dsp = new PitchDsp(sampleRate, kMinFreq, kMaxFreq, detectLevelThreshold); iirFilterLoLo = new IIRFilter(); iirFilterLoLo.Proto = IIRFilter.ProtoType.Butterworth; iirFilterLoLo.Type = IIRFilter.FilterType.HP; iirFilterLoLo.Order = 5; iirFilterLoLo.FreqLow = 45.0f; iirFilterLoLo.SampleRate = (float)sampleRate; iirFilterLoHi = new IIRFilter(); iirFilterLoHi.Proto = IIRFilter.ProtoType.Butterworth; iirFilterLoHi.Type = IIRFilter.FilterType.LP; iirFilterLoHi.Order = 5; iirFilterLoHi.FreqHigh = 280.0f; iirFilterLoHi.SampleRate = (float)sampleRate; iirFilterHiLo = new IIRFilter(); iirFilterHiLo.Proto = IIRFilter.ProtoType.Butterworth; iirFilterHiLo.Type = IIRFilter.FilterType.HP; iirFilterHiLo.Order = 5; iirFilterHiLo.FreqLow = 45.0f; iirFilterHiLo.SampleRate = (float)sampleRate; iirFilterHiHi = new IIRFilter(); iirFilterHiHi.Proto = IIRFilter.ProtoType.Butterworth; iirFilterHiHi.Type = IIRFilter.FilterType.LP; iirFilterHiHi.Order = 5; iirFilterHiHi.FreqHigh = 1500.0f; iirFilterHiHi.SampleRate = (float)sampleRate; detectOverlapSamples = (int)(kDetectOverlapSec * sampleRate); maxOverlapDiff = kMaxOctaveSecRate * kDetectOverlapSec; pitchBufSize = (int)(((1.0f / (float)kMinFreq) * 2.0f + ((kAvgCount - 1) * kAvgOffset)) * sampleRate) + 16; pitchBufLo = new float[pitchBufSize + detectOverlapSamples]; pitchBufHi = new float[pitchBufSize + detectOverlapSamples]; samplesPerPitchBlock = (int)Math.Round(sampleRate / pitchRecordsPerSecond); circularBufferLo = new CircularBuffer <float>((int)(kCircularBufSaveTime * sampleRate + 0.5f) + 10000); circularBufferHi = new CircularBuffer <float>((int)(kCircularBufSaveTime * sampleRate + 0.5f) + 10000); }
/// <summary> /// Write data into the buffer /// </summary> /// <param name="m_pInBuffer"></param> /// <param name="count"></param> /// <returns></returns> public int WriteBuffer(T[] m_pInBuffer, int count) { count = Math.Min(count, m_bufSize); var startPos = m_availBuf != m_bufSize ? m_availBuf : m_begBufOffset; var pass1Count = Math.Min(count, m_bufSize - startPos); var pass2Count = count - pass1Count; PitchDsp.CopyBuffer(m_pInBuffer, 0, m_buffer, startPos, pass1Count); if (pass2Count > 0) { PitchDsp.CopyBuffer(m_pInBuffer, pass1Count, m_buffer, 0, pass2Count); } if (pass2Count == 0) { // did not wrap around if (m_availBuf != m_bufSize) { m_availBuf += count; // have never wrapped around } else { m_begBufOffset += count; m_startPos += count; } } else { // wrapped around if (m_availBuf != m_bufSize) { m_startPos += pass2Count; // first time wrap-around } else { m_startPos += count; } m_begBufOffset = pass2Count; m_availBuf = m_bufSize; } return(count); }
/// <summary> /// Write data into the buffer /// </summary> /// <param name="m_pInBuffer"></param> /// <param name="count"></param> /// <returns></returns> public int WriteBuffer(T[] m_pInBuffer, int count) { count = Math.Min(count, bufSize); var startPos = availBuf != bufSize ? availBuf : begBufOffset; var pass1Count = Math.Min(count, bufSize - startPos); var pass2Count = count - pass1Count; PitchDsp.CopyBuffer(m_pInBuffer, 0, buffer, startPos, pass1Count); if (pass2Count > 0) { PitchDsp.CopyBuffer(m_pInBuffer, pass1Count, buffer, 0, pass2Count); } if (pass2Count == 0) { // did not wrap around if (availBuf != bufSize) { availBuf += count; // have never wrapped around } else { begBufOffset += count; startPosition += count; } } else { // wrapped around if (availBuf != bufSize) { startPosition += pass2Count; // first time wrap-around } else { startPosition += count; } begBufOffset = pass2Count; availBuf = bufSize; } return(count); }
/// <summary> /// Read from the buffer /// </summary> /// <param name="outBuffer"></param> /// <param name="startRead"></param> /// <param name="readCount"></param> /// <returns></returns> public bool ReadBuffer(T[] outBuffer, long startRead, int readCount) { var endRead = (int)(startRead + readCount); var endAvail = (int)(m_startPos + m_availBuf); if (startRead < m_startPos || endRead > endAvail) { return(false); } var startReadPos = (int)(((startRead - m_startPos) + m_begBufOffset) % m_bufSize); var block1Samples = Math.Min(readCount, m_bufSize - startReadPos); var block2Samples = readCount - block1Samples; PitchDsp.CopyBuffer(m_buffer, startReadPos, outBuffer, 0, block1Samples); if (block2Samples > 0) { PitchDsp.CopyBuffer(m_buffer, 0, outBuffer, block1Samples, block2Samples); } return(true); }