comparison runner/MIDIFeatureWriter.cpp @ 141:d7a91e07ca57 midi

Flesh out MIDI writer
author Chris Cannam
date Mon, 13 Oct 2014 13:37:31 +0100
parents 9b94545a7fdc
children b3d73c08b6ce
comparison
equal deleted inserted replaced
140:9b94545a7fdc 141:d7a91e07ca57
56 void 56 void
57 MIDIFeatureWriter::write(QString trackId, 57 MIDIFeatureWriter::write(QString trackId,
58 const Transform &transform, 58 const Transform &transform,
59 const Plugin::OutputDescriptor& output, 59 const Plugin::OutputDescriptor& output,
60 const Plugin::FeatureList& features, 60 const Plugin::FeatureList& features,
61 std::string summaryType) 61 std::string /* summaryType */)
62 { 62 {
63 QString transformId = transform.getIdentifier(); 63 QString transformId = transform.getIdentifier();
64 64
65 QString filename = getOutputFilename(trackId, transformId); 65 QString filename = getOutputFilename(trackId, transformId);
66 if (filename == "") { 66 if (filename == "") {
67 throw FailedToOpenOutputStream(trackId, transformId); 67 throw FailedToOpenOutputStream(trackId, transformId);
68 } 68 }
69 69
70 int sampleRate = transform.getSampleRate();
71
70 if (m_rates.find(filename) == m_rates.end()) { 72 if (m_rates.find(filename) == m_rates.end()) {
71 // If the output is FixedSampleRate, we draw the sample rate
72 // from the output descriptor; otherwise from the transform
73 float sampleRate;
74 if (output.sampleType == Plugin::OutputDescriptor::FixedSampleRate) {
75 sampleRate = output.sampleRate;
76 } else {
77 sampleRate = transform.getSampleRate();
78 }
79 m_rates[filename] = sampleRate; 73 m_rates[filename] = sampleRate;
80 } 74 }
81 75
82 if (m_fileTransforms[filename].find(transformId) == 76 if (m_fileTransforms[filename].find(transformId) ==
83 m_fileTransforms[filename].end()) { 77 m_fileTransforms[filename].end()) {
91 m_channels[transformId] = channel; 85 m_channels[transformId] = channel;
92 } 86 }
93 87
94 NoteList notes = m_notes[filename]; 88 NoteList notes = m_notes[filename];
95 89
96 90 bool freq = (output.unit == "Hz" ||
91 output.unit == "hz" ||
92 output.unit == "HZ");
93
94 for (int i = 0; i < (int)features.size(); ++i) {
95
96 const Plugin::Feature &feature(features[i]);
97
98 Vamp::RealTime timestamp = feature.timestamp;
99 int frame = Vamp::RealTime::realTime2Frame(timestamp, sampleRate);
100
101 int duration = 1;
102 if (feature.hasDuration) {
103 duration = Vamp::RealTime::realTime2Frame(feature.duration, sampleRate);
104 }
105
106 int pitch = 60;
107 if (feature.values.size() > 0) {
108 float pval = feature.values[0];
109 if (freq) {
110 pitch = Pitch::getPitchForFrequency(pval);
111 } else {
112 pitch = int(pval + 0.5);
113 }
114 }
115
116 int velocity = 100;
117 if (feature.values.size() > 1) {
118 float vval = feature.values[1];
119 if (vval < 128) {
120 velocity = int(vval + 0.5);
121 }
122 }
123
124 NoteData note(frame, duration, pitch, velocity);
125
126 note.channel = m_channels[transformId];
127
128 notes.push_back(note);
129 }
97 130
98 m_notes[filename] = notes; 131 m_notes[filename] = notes;
99 } 132 }
100 133
101 void 134 void