Beispiel #1
0
        /// <summary>
        /// The pitch was detected - add the record
        /// </summary>
        /// <param name="pitch"></param>
        private void AddPitchRecord(float pitch)
        {
            var midiNote  = 0;
            var midiCents = 0;

            MusicalTemperament.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 (PitchDetected != null)
            {
                PitchDetected(this, record);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="fSampleRate"></param>
        /// <param name="minFreq"></param>
        /// <param name="maxFreq"></param>
        /// <param name="fFreqStep"></param>
        public PitchDsp(double sampleRate, float minPitch, float maxPitch, float detectLevelThreshold)
        {
            m_sampleRate           = sampleRate;
            m_minPitch             = minPitch;
            m_maxPitch             = maxPitch;
            m_detectLevelThreshold = detectLevelThreshold;

            m_minNote = (int)(MusicalTemperament.PitchToMidiNote(m_minPitch) + 0.5f) + 2;
            m_maxNote = (int)(MusicalTemperament.PitchToMidiNote(m_maxPitch) + 0.5f) - 2;

            m_blockLen44 = (int)(m_sampleRate / m_minPitch + 0.5f);
            m_blockLen34 = (m_blockLen44 * 3) / 4;
            m_blockLen24 = m_blockLen44 / 2;
            m_blockLen14 = m_blockLen44 / 4;

            m_numCourseSteps = (int)((Math.Log((double)m_maxPitch / (double)m_minPitch) / Math.Log(2.0)) * kCourseOctaveSteps + 0.5) + 3;

            m_pCourseFreqOffset = new float[m_numCourseSteps + 10000];
            m_pCourseFreq       = new float[m_numCourseSteps + 10000];

            m_detectCurve = new float[m_numCourseSteps];

            var freqStep = 1.0 / Math.Pow(2.0, 1.0 / kCourseOctaveSteps);
            var curFreq  = m_maxPitch / freqStep;

            // frequency is stored from high to low
            for (int idx = 0; idx < m_numCourseSteps; idx++)
            {
                m_pCourseFreq[idx]       = (float)curFreq;
                m_pCourseFreqOffset[idx] = (float)(m_sampleRate / curFreq);
                curFreq *= freqStep;
            }

            for (int idx = 0; idx < kScanHiSize; idx++)
            {
                m_scanHiOffset[idx] = (float)Math.Pow(kScanHiFreqStep, (kScanHiSize / 2) - idx);
            }
        }