# HG changeset patch # User Chris Cannam # Date 1413203851 -3600 # Node ID d7a91e07ca57458e00b41c33ea38a90110986e9a # Parent 9b94545a7fdc9e77e5e676998686073ee39d3808 Flesh out MIDI writer diff -r 9b94545a7fdc -r d7a91e07ca57 runner/MIDIFeatureWriter.cpp --- a/runner/MIDIFeatureWriter.cpp Mon Oct 13 12:00:22 2014 +0100 +++ b/runner/MIDIFeatureWriter.cpp Mon Oct 13 13:37:31 2014 +0100 @@ -58,7 +58,7 @@ const Transform &transform, const Plugin::OutputDescriptor& output, const Plugin::FeatureList& features, - std::string summaryType) + std::string /* summaryType */) { QString transformId = transform.getIdentifier(); @@ -67,15 +67,9 @@ throw FailedToOpenOutputStream(trackId, transformId); } + int sampleRate = transform.getSampleRate(); + if (m_rates.find(filename) == m_rates.end()) { - // If the output is FixedSampleRate, we draw the sample rate - // from the output descriptor; otherwise from the transform - float sampleRate; - if (output.sampleType == Plugin::OutputDescriptor::FixedSampleRate) { - sampleRate = output.sampleRate; - } else { - sampleRate = transform.getSampleRate(); - } m_rates[filename] = sampleRate; } @@ -93,7 +87,46 @@ NoteList notes = m_notes[filename]; - + bool freq = (output.unit == "Hz" || + output.unit == "hz" || + output.unit == "HZ"); + + for (int i = 0; i < (int)features.size(); ++i) { + + const Plugin::Feature &feature(features[i]); + + Vamp::RealTime timestamp = feature.timestamp; + int frame = Vamp::RealTime::realTime2Frame(timestamp, sampleRate); + + int duration = 1; + if (feature.hasDuration) { + duration = Vamp::RealTime::realTime2Frame(feature.duration, sampleRate); + } + + int pitch = 60; + if (feature.values.size() > 0) { + float pval = feature.values[0]; + if (freq) { + pitch = Pitch::getPitchForFrequency(pval); + } else { + pitch = int(pval + 0.5); + } + } + + int velocity = 100; + if (feature.values.size() > 1) { + float vval = feature.values[1]; + if (vval < 128) { + velocity = int(vval + 0.5); + } + } + + NoteData note(frame, duration, pitch, velocity); + + note.channel = m_channels[transformId]; + + notes.push_back(note); + } m_notes[filename] = notes; }