/// <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); } }
/// <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); } }
public MusicXmlDecoder(string fileName) : base() { scorepartwise score; using (var reader = new StreamReader(fileName)) { score = (scorepartwise)_xmlSerializer.Deserialize(reader); } Events = new List <MusicalEvent>(); double divisions = 1; Beats = 4; BeatType = 4; Tempo = 120; int currentEventNumber = 0; foreach (var meause in score.part[0].measure) { foreach (var item in meause.Items) { var attributes = item as attributes; if (attributes != null) { if (attributes.divisions != 0) { divisions = (double)(attributes.divisions); } if (attributes.time != null) { Beats = Convert.ToInt32(attributes.time[0].Items[0]); } if (attributes.time != null) { BeatType = Convert.ToInt32(attributes.time[0].Items[1]); } } var direction = item as direction; if (direction != null) { var sound = direction.sound; if (sound != null && sound.tempo != 0.0m) { Tempo = (int)sound.tempo; } } var note = item as note; if (note != null && (note.staff == null || int.Parse(note.staff) == 1)) { var pitch = note.Items[0] as pitch; if (pitch != null) { Events.Add(new MusicalEvent { Number = currentEventNumber++, MidiNote = MusicalTemperament.NoteNameToMidiNote(pitch.step.ToString(), (int)pitch.alter, int.Parse(pitch.octave)), Duration = Convert.ToDouble(note.Items[1]) / divisions }); } else { var rest = note.Items[0] as rest; if (rest != null) { Events.Add(new MusicalEvent { Number = currentEventNumber++, MidiNote = -1, Duration = Convert.ToDouble(note.Items[1]) / divisions }); } } } } } }