Mercurial > hg > midi-score-follower
comparison src/CannamMidiFileLoader.cpp @ 2:5581023e0de4
Added separate CannamMidiFileLoader class to handle the loading in.
author | Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk> |
---|---|
date | Fri, 19 Aug 2011 01:26:40 +0100 |
parents | |
children | de86d77f2612 |
comparison
equal
deleted
inserted
replaced
1:1a32ce016bb9 | 2:5581023e0de4 |
---|---|
1 /* | |
2 * CannamMidiFileLoader.cpp | |
3 * midi-score-follower | |
4 * | |
5 * Created by Andrew on 19/08/2011. | |
6 * Copyright 2011 QMUL. All rights reserved. | |
7 * | |
8 */ | |
9 | |
10 #include "MIDIFileReader.h" | |
11 #include "CannamMidiFileLoader.h" | |
12 | |
13 int CannamMidiFileLoader::loadFile(std::string& filename, midiEventHolder& myMidiEvents){ | |
14 | |
15 | |
16 myMidiEvents.clearAllEvents(); | |
17 | |
18 //int main(int argc, char **argv) | |
19 //{ | |
20 // if (argc != 2) { | |
21 // cerr << "Usage: midifile <file.mid>" << endl; | |
22 // return 1; | |
23 // } | |
24 | |
25 // std::string filename = midiFileName;//argv[1]; | |
26 | |
27 MIDIFileReader fr(filename); | |
28 | |
29 if (!fr.isOK()) { | |
30 std::cerr << "Error: " << fr.getError().c_str() << std::endl; | |
31 return 1; | |
32 } | |
33 | |
34 MIDIComposition c = fr.load(); | |
35 | |
36 switch (fr.getFormat()) { | |
37 case MIDI_SINGLE_TRACK_FILE: cout << "Format: MIDI Single Track File" << endl; break; | |
38 case MIDI_SIMULTANEOUS_TRACK_FILE: cout << "Format: MIDI Simultaneous Track File" << endl; break; | |
39 case MIDI_SEQUENTIAL_TRACK_FILE: cout << "Format: MIDI Sequential Track File" << endl; break; | |
40 default: cout << "Format: Unknown MIDI file format?" << endl; break; | |
41 } | |
42 | |
43 cout << "Tracks: " << c.size() << endl; | |
44 | |
45 int td = fr.getTimingDivision(); | |
46 if (td < 32768) { | |
47 cout << "Timing division: " << fr.getTimingDivision() << " ppq" << endl; | |
48 | |
49 myMidiEvents.pulsesPerQuarternote = fr.getTimingDivision(); | |
50 } else { | |
51 int frames = 256 - (td >> 8); | |
52 int subframes = td & 0xff; | |
53 cout << "SMPTE timing: " << frames << " fps, " << subframes << " subframes" << endl; | |
54 } | |
55 | |
56 for (MIDIComposition::const_iterator i = c.begin(); i != c.end(); ++i) { | |
57 | |
58 cout << "Start of track: " << i->first+1 << endl; | |
59 | |
60 for (MIDITrack::const_iterator j = i->second.begin(); j != i->second.end(); ++j) { | |
61 | |
62 unsigned int t = j->getTime(); | |
63 int ch = j->getChannelNumber(); | |
64 | |
65 if (j->isMeta()) { | |
66 int code = j->getMetaEventCode(); | |
67 string name; | |
68 bool printable = true; | |
69 switch (code) { | |
70 | |
71 case MIDI_END_OF_TRACK: | |
72 cout << t << ": End of track" << endl; | |
73 break; | |
74 | |
75 case MIDI_TEXT_EVENT: name = "Text"; break; | |
76 case MIDI_COPYRIGHT_NOTICE: name = "Copyright"; break; | |
77 case MIDI_TRACK_NAME: name = "Track name"; break; | |
78 case MIDI_INSTRUMENT_NAME: name = "Instrument name"; break; | |
79 case MIDI_LYRIC: name = "Lyric"; break; | |
80 case MIDI_TEXT_MARKER: name = "Text marker"; break; | |
81 case MIDI_SEQUENCE_NUMBER: name = "Sequence number"; printable = false; break; | |
82 case MIDI_CHANNEL_PREFIX_OR_PORT: name = "Channel prefix or port"; printable = false; break; | |
83 case MIDI_CUE_POINT: name = "Cue point"; break; | |
84 case MIDI_CHANNEL_PREFIX: name = "Channel prefix"; printable = false; break; | |
85 case MIDI_SEQUENCER_SPECIFIC: name = "Sequencer specific"; printable = false; break; | |
86 case MIDI_SMPTE_OFFSET: name = "SMPTE offset"; printable = false; break; | |
87 | |
88 case MIDI_SET_TEMPO: | |
89 { | |
90 int m0 = j->getMetaMessage()[0]; | |
91 int m1 = j->getMetaMessage()[1]; | |
92 int m2 = j->getMetaMessage()[2]; | |
93 long tempo = (((m0 << 8) + m1) << 8) + m2; | |
94 | |
95 cout << t << ": Tempo: " << 60000000.0 / double(tempo) << endl; | |
96 myMidiEvents.tempo = 60000000.0 / double(tempo); | |
97 myMidiEvents.period = double(tempo)/1000.0; | |
98 | |
99 printf("period double is %f\n", myMidiEvents.period); | |
100 } | |
101 break; | |
102 | |
103 case MIDI_TIME_SIGNATURE: | |
104 { | |
105 int numerator = j->getMetaMessage()[0]; | |
106 int denominator = 1 << (int)j->getMetaMessage()[1]; | |
107 | |
108 cout << t << ": Time signature: " << numerator << "/" << denominator << endl; | |
109 } | |
110 | |
111 case MIDI_KEY_SIGNATURE: | |
112 { | |
113 int accidentals = j->getMetaMessage()[0]; | |
114 int isMinor = j->getMetaMessage()[1]; | |
115 bool isSharp = accidentals < 0 ? false : true; | |
116 accidentals = accidentals < 0 ? -accidentals : accidentals; | |
117 cout << t << ": Key signature: " << accidentals << " " | |
118 << (isSharp ? | |
119 (accidentals > 1 ? "sharps" : "sharp") : | |
120 (accidentals > 1 ? "flats" : "flat")) | |
121 << (isMinor ? ", minor" : ", major") << endl; | |
122 } | |
123 | |
124 } | |
125 | |
126 | |
127 if (name != "") { | |
128 if (printable) { | |
129 cout << t << ": File meta event: code " << code | |
130 << ": " << name << ": \"" << j->getMetaMessage() | |
131 << "\"" << endl; | |
132 } else { | |
133 cout << t << ": File meta event: code " << code | |
134 << ": " << name << ": "; | |
135 for (int k = 0; k < j->getMetaMessage().length(); ++k) { | |
136 cout << (int)j->getMetaMessage()[k] << " "; | |
137 } | |
138 } | |
139 } | |
140 continue; | |
141 } | |
142 | |
143 switch (j->getMessageType()) { | |
144 | |
145 case MIDI_NOTE_ON: | |
146 cout << t << ": Note: channel " << ch | |
147 << " duration " << j->getDuration() | |
148 << " pitch " << j->getPitch() | |
149 << " velocity " << j->getVelocity() | |
150 << "event time " << myMidiEvents.getEventTimeMillis(t) << endl; | |
151 v.clear(); | |
152 v.push_back(t); | |
153 v.push_back(j->getPitch()); | |
154 v.push_back(j->getVelocity()); | |
155 v.push_back(j->getDuration()); | |
156 myMidiEvents.recordedNoteOnMatrix.push_back(v); | |
157 myMidiEvents.recordedEventTimes.push_back(myMidiEvents.getEventTimeMillis(t)); | |
158 break; | |
159 | |
160 case MIDI_POLY_AFTERTOUCH: | |
161 cout << t << ": Polyphonic aftertouch: channel " << ch | |
162 << " pitch " << j->getPitch() | |
163 << " pressure " << j->getData2() << endl; | |
164 break; | |
165 | |
166 case MIDI_CTRL_CHANGE: | |
167 { | |
168 int controller = j->getData1(); | |
169 string name; | |
170 switch (controller) { | |
171 case MIDI_CONTROLLER_BANK_MSB: name = "Bank select MSB"; break; | |
172 case MIDI_CONTROLLER_VOLUME: name = "Volume"; break; | |
173 case MIDI_CONTROLLER_BANK_LSB: name = "Bank select LSB"; break; | |
174 case MIDI_CONTROLLER_MODULATION: name = "Modulation wheel"; break; | |
175 case MIDI_CONTROLLER_PAN: name = "Pan"; break; | |
176 case MIDI_CONTROLLER_SUSTAIN: name = "Sustain"; break; | |
177 case MIDI_CONTROLLER_RESONANCE: name = "Resonance"; break; | |
178 case MIDI_CONTROLLER_RELEASE: name = "Release"; break; | |
179 case MIDI_CONTROLLER_ATTACK: name = "Attack"; break; | |
180 case MIDI_CONTROLLER_FILTER: name = "Filter"; break; | |
181 case MIDI_CONTROLLER_REVERB: name = "Reverb"; break; | |
182 case MIDI_CONTROLLER_CHORUS: name = "Chorus"; break; | |
183 case MIDI_CONTROLLER_NRPN_1: name = "NRPN 1"; break; | |
184 case MIDI_CONTROLLER_NRPN_2: name = "NRPN 2"; break; | |
185 case MIDI_CONTROLLER_RPN_1: name = "RPN 1"; break; | |
186 case MIDI_CONTROLLER_RPN_2: name = "RPN 2"; break; | |
187 case MIDI_CONTROLLER_SOUNDS_OFF: name = "All sounds off"; break; | |
188 case MIDI_CONTROLLER_RESET: name = "Reset"; break; | |
189 case MIDI_CONTROLLER_LOCAL: name = "Local"; break; | |
190 case MIDI_CONTROLLER_ALL_NOTES_OFF: name = "All notes off"; break; | |
191 } | |
192 cout << t << ": Controller change: channel " << ch | |
193 << " controller " << j->getData1(); | |
194 if (name != "") cout << " (" << name << ")"; | |
195 cout << " value " << j->getData2() << endl; | |
196 } | |
197 break; | |
198 | |
199 case MIDI_PROG_CHANGE: | |
200 cout << t << ": Program change: channel " << ch | |
201 << " program " << j->getData1() << endl; | |
202 break; | |
203 | |
204 case MIDI_CHNL_AFTERTOUCH: | |
205 cout << t << ": Channel aftertouch: channel " << ch | |
206 << " pressure " << j->getData1() << endl; | |
207 break; | |
208 | |
209 case MIDI_PITCH_BEND: | |
210 cout << t << ": Pitch bend: channel " << ch | |
211 << " value " << (int)j->getData2() * 128 + (int)j->getData1() << endl; | |
212 break; | |
213 | |
214 case MIDI_SYSTEM_EXCLUSIVE: | |
215 cout << t << ": System exclusive: code " | |
216 << (int)j->getMessageType() << " message length " << | |
217 j->getMetaMessage().length() << endl; | |
218 break; | |
219 | |
220 | |
221 } | |
222 | |
223 | |
224 } | |
225 | |
226 | |
227 } | |
228 | |
229 | |
230 | |
231 | |
232 }//end cannam midi main | |
233 | |
234 | |
235 | |
236 |