comparison src/testApp.cpp @ 0:b299a65a3ad0

start project
author Andrew N Robertson <andrew.robertson@eecs.qmul.ac.uk>
date Tue, 16 Aug 2011 11:29:59 +0100
parents
children 1a32ce016bb9
comparison
equal deleted inserted replaced
-1:000000000000 0:b299a65a3ad0
1 #include "testApp.h"
2
3 //--------------------------------------------------------------
4 void testApp::setup(){
5 midiFileName = "../../../data/entertainer.mid";
6 int retVal = cannamMainFunction();
7
8 playing = false;
9 /*
10 bayesStruct.resetSize(1000);
11 bayesStruct.resetSpeedSize(200);
12 bayesStruct.setRelativeSpeedScalar(0.01);
13 bayesStruct.simpleExample();
14 */
15
16 receiver.setup( PORT );
17
18 screenWidth = ofGetWidth();
19 screenHeight = ofGetHeight();
20 midiEvents.screenWidth = &screenWidth;
21 midiEvents.screenHeight = &screenHeight;
22 ofSetFrameRate(100);
23 }
24
25 //--------------------------------------------------------------
26 void testApp::update(){
27 if (playing){
28 midiEvents.updatePlayPosition();
29 midiEvents.bayesStruct.updateBestEstimate();
30 }
31 // drawer.tickLocation+=20;
32
33 // check for waiting messages
34 while( receiver.hasWaitingMessages() )
35 {
36 ofxOscMessage m;
37 receiver.getNextMessage( &m );
38
39 if ( m.getAddress() == "/midinoteon" )
40 {
41 int newMidiOnPitch = m.getArgAsInt32(0);
42 int velocity = m.getArgAsInt32(1);
43 double time = m.getArgAsFloat(2);
44
45 if (velocity != 0)
46 midiEvents.newNoteOnEvent(newMidiOnPitch, velocity, time);
47
48 }
49
50 if ( m.getAddress() == "/startplaying" )
51 {
52 startPlaying();
53 }
54
55 if ( m.getAddress() == "/stopplaying" )
56 {
57 stopPlaying();
58 }
59 }//end while osc
60
61 }
62
63 //--------------------------------------------------------------
64 void testApp::draw(){
65
66 midiEvents.drawFile();
67
68 }
69
70 //--------------------------------------------------------------
71 void testApp::keyPressed(int key){
72
73
74 if (key == ' '){
75 startPlaying();
76 }
77
78 if (key == 'c'){
79 double timenow = ofGetElapsedTimeMillis();
80 midiEvents.exampleCrossUpdate();
81 timenow *= -1;
82 timenow += ofGetElapsedTimeMillis();
83 printf("CROSS UPDATE TOOK %f", timenow);
84 }
85
86 if (key == OF_KEY_RETURN)
87 stopPlaying();
88
89 if (key == OF_KEY_UP){
90 if (midiEvents.ticksPerScreen >= 4000)
91 midiEvents.ticksPerScreen += 2000;
92 else
93 midiEvents.ticksPerScreen += 500;
94 }
95
96 if (key == 'm'){
97 // midiEvents.findMatch(84, 0, 10000);
98 }
99
100 if (key == OF_KEY_DOWN){
101 if (midiEvents.ticksPerScreen >= 4000)
102 midiEvents.ticksPerScreen -= 2000;
103 else if (midiEvents.ticksPerScreen > 500)
104 midiEvents.ticksPerScreen -= 500;
105 }
106
107 if (key == 'w')
108 midiEvents.printMatchMatrix();
109
110 if (key == 'p'){
111 midiEvents.printNotes();
112 }
113
114 if (key == 'l')
115 midiEvents.bayesStruct.decaySpeedDistribution(100);
116
117 if (key == 'o'){
118 //open audio file
119 string *filePtr;
120 filePtr = &midiFileName;
121
122 if (getFilenameFromDialogBox(filePtr)){
123 printf("Midifile: Loaded name okay :\n'%s' \n", midiFileName.c_str());
124 cannamMainFunction();
125 }
126
127 }
128 }
129
130 //--------------------------------------------------------------
131 void testApp::keyReleased(int key){
132
133 }
134
135 //--------------------------------------------------------------
136 void testApp::mouseMoved(int x, int y ){
137
138 }
139
140 //--------------------------------------------------------------
141 void testApp::mouseDragged(int x, int y, int button){
142
143 }
144
145 //--------------------------------------------------------------
146 void testApp::mousePressed(int x, int y, int button){
147
148 }
149
150 //--------------------------------------------------------------
151 void testApp::mouseReleased(int x, int y, int button){
152
153 }
154
155 //--------------------------------------------------------------
156 void testApp::windowResized(int w, int h){
157 screenWidth = w;
158 screenHeight = h;
159 midiEvents.noteHeight = screenHeight / (float)(midiEvents.noteMaximum - midiEvents.noteMinimum);
160
161 }
162
163
164
165 void testApp::startPlaying(){
166 playing = !playing;
167 midiEvents.reset();
168 midiEvents.setStartPlayingTimes();
169 //this is where we stop and start playing
170 }
171
172 void testApp::stopPlaying(){
173 playing = false;
174 }
175
176 bool testApp::getFilenameFromDialogBox(string* fileNameToSave){
177 //this uses a pointer structure within the loader and returns true if the dialogue box was used successfully
178 // first, create a string that will hold the URL
179 string URL;
180
181 // openFile(string& URL) returns 1 if a file was picked
182 // returns 0 when something went wrong or the user pressed 'cancel'
183 int response = ofxFileDialogOSX::openFile(URL);
184 if(response){
185 // now you can use the URL
186 *fileNameToSave = URL;
187 //printf("\n filename is %s \n", soundFileName.c_str());
188 return true;
189 }
190 else {
191 // soundFileName = "OPEN canceled. ";
192 printf("\n open file cancelled \n");
193 return false;
194 }
195
196
197
198 }
199
200
201
202
203 int testApp::cannamMainFunction(){
204
205
206 midiEvents.recordedNoteOnMatrix.clear();//where we store the note ons
207
208 //int main(int argc, char **argv)
209 //{
210 // if (argc != 2) {
211 // cerr << "Usage: midifile <file.mid>" << endl;
212 // return 1;
213 // }
214
215 std::string filename = midiFileName;//argv[1];
216
217 MIDIFileReader fr(filename);
218 if (!fr.isOK()) {
219 std::cerr << "Error: " << fr.getError().c_str() << std::endl;
220 return 1;
221 }
222
223 MIDIComposition c = fr.load();
224
225 switch (fr.getFormat()) {
226 case MIDI_SINGLE_TRACK_FILE: cout << "Format: MIDI Single Track File" << endl; break;
227 case MIDI_SIMULTANEOUS_TRACK_FILE: cout << "Format: MIDI Simultaneous Track File" << endl; break;
228 case MIDI_SEQUENTIAL_TRACK_FILE: cout << "Format: MIDI Sequential Track File" << endl; break;
229 default: cout << "Format: Unknown MIDI file format?" << endl; break;
230 }
231
232 cout << "Tracks: " << c.size() << endl;
233
234 int td = fr.getTimingDivision();
235 if (td < 32768) {
236 cout << "Timing division: " << fr.getTimingDivision() << " ppq" << endl;
237
238 midiEvents.pulsesPerQuarternote = fr.getTimingDivision();
239 } else {
240 int frames = 256 - (td >> 8);
241 int subframes = td & 0xff;
242 cout << "SMPTE timing: " << frames << " fps, " << subframes << " subframes" << endl;
243 }
244
245 for (MIDIComposition::const_iterator i = c.begin(); i != c.end(); ++i) {
246
247 cout << "Start of track: " << i->first+1 << endl;
248
249 for (MIDITrack::const_iterator j = i->second.begin(); j != i->second.end(); ++j) {
250
251 unsigned int t = j->getTime();
252 int ch = j->getChannelNumber();
253
254 if (j->isMeta()) {
255 int code = j->getMetaEventCode();
256 string name;
257 bool printable = true;
258 switch (code) {
259
260 case MIDI_END_OF_TRACK:
261 cout << t << ": End of track" << endl;
262 break;
263
264 case MIDI_TEXT_EVENT: name = "Text"; break;
265 case MIDI_COPYRIGHT_NOTICE: name = "Copyright"; break;
266 case MIDI_TRACK_NAME: name = "Track name"; break;
267 case MIDI_INSTRUMENT_NAME: name = "Instrument name"; break;
268 case MIDI_LYRIC: name = "Lyric"; break;
269 case MIDI_TEXT_MARKER: name = "Text marker"; break;
270 case MIDI_SEQUENCE_NUMBER: name = "Sequence number"; printable = false; break;
271 case MIDI_CHANNEL_PREFIX_OR_PORT: name = "Channel prefix or port"; printable = false; break;
272 case MIDI_CUE_POINT: name = "Cue point"; break;
273 case MIDI_CHANNEL_PREFIX: name = "Channel prefix"; printable = false; break;
274 case MIDI_SEQUENCER_SPECIFIC: name = "Sequencer specific"; printable = false; break;
275 case MIDI_SMPTE_OFFSET: name = "SMPTE offset"; printable = false; break;
276
277 case MIDI_SET_TEMPO:
278 {
279 int m0 = j->getMetaMessage()[0];
280 int m1 = j->getMetaMessage()[1];
281 int m2 = j->getMetaMessage()[2];
282 long tempo = (((m0 << 8) + m1) << 8) + m2;
283
284 cout << t << ": Tempo: " << 60000000.0 / double(tempo) << endl;
285 midiEvents.tempo = 60000000.0 / double(tempo);
286 midiEvents.period = double(tempo)/1000.0;
287
288 printf("period double is %f\n", midiEvents.period);
289 }
290 break;
291
292 case MIDI_TIME_SIGNATURE:
293 {
294 int numerator = j->getMetaMessage()[0];
295 int denominator = 1 << (int)j->getMetaMessage()[1];
296
297 cout << t << ": Time signature: " << numerator << "/" << denominator << endl;
298 }
299
300 case MIDI_KEY_SIGNATURE:
301 {
302 int accidentals = j->getMetaMessage()[0];
303 int isMinor = j->getMetaMessage()[1];
304 bool isSharp = accidentals < 0 ? false : true;
305 accidentals = accidentals < 0 ? -accidentals : accidentals;
306 cout << t << ": Key signature: " << accidentals << " "
307 << (isSharp ?
308 (accidentals > 1 ? "sharps" : "sharp") :
309 (accidentals > 1 ? "flats" : "flat"))
310 << (isMinor ? ", minor" : ", major") << endl;
311 }
312
313 }
314
315
316 if (name != "") {
317 if (printable) {
318 cout << t << ": File meta event: code " << code
319 << ": " << name << ": \"" << j->getMetaMessage()
320 << "\"" << endl;
321 } else {
322 cout << t << ": File meta event: code " << code
323 << ": " << name << ": ";
324 for (int k = 0; k < j->getMetaMessage().length(); ++k) {
325 cout << (int)j->getMetaMessage()[k] << " ";
326 }
327 }
328 }
329 continue;
330 }
331
332 switch (j->getMessageType()) {
333
334 case MIDI_NOTE_ON:
335 cout << t << ": Note: channel " << ch
336 << " duration " << j->getDuration()
337 << " pitch " << j->getPitch()
338 << " velocity " << j->getVelocity()
339 << "event time " << midiEvents.getEventTimeMillis(t) << endl;
340 v.clear();
341 v.push_back(t);
342 v.push_back(j->getPitch());
343 v.push_back(j->getVelocity());
344 v.push_back(j->getDuration());
345 midiEvents.recordedNoteOnMatrix.push_back(v);
346 midiEvents.recordedEventTimes.push_back(midiEvents.getEventTimeMillis(t));
347 break;
348
349 case MIDI_POLY_AFTERTOUCH:
350 cout << t << ": Polyphonic aftertouch: channel " << ch
351 << " pitch " << j->getPitch()
352 << " pressure " << j->getData2() << endl;
353 break;
354
355 case MIDI_CTRL_CHANGE:
356 {
357 int controller = j->getData1();
358 string name;
359 switch (controller) {
360 case MIDI_CONTROLLER_BANK_MSB: name = "Bank select MSB"; break;
361 case MIDI_CONTROLLER_VOLUME: name = "Volume"; break;
362 case MIDI_CONTROLLER_BANK_LSB: name = "Bank select LSB"; break;
363 case MIDI_CONTROLLER_MODULATION: name = "Modulation wheel"; break;
364 case MIDI_CONTROLLER_PAN: name = "Pan"; break;
365 case MIDI_CONTROLLER_SUSTAIN: name = "Sustain"; break;
366 case MIDI_CONTROLLER_RESONANCE: name = "Resonance"; break;
367 case MIDI_CONTROLLER_RELEASE: name = "Release"; break;
368 case MIDI_CONTROLLER_ATTACK: name = "Attack"; break;
369 case MIDI_CONTROLLER_FILTER: name = "Filter"; break;
370 case MIDI_CONTROLLER_REVERB: name = "Reverb"; break;
371 case MIDI_CONTROLLER_CHORUS: name = "Chorus"; break;
372 case MIDI_CONTROLLER_NRPN_1: name = "NRPN 1"; break;
373 case MIDI_CONTROLLER_NRPN_2: name = "NRPN 2"; break;
374 case MIDI_CONTROLLER_RPN_1: name = "RPN 1"; break;
375 case MIDI_CONTROLLER_RPN_2: name = "RPN 2"; break;
376 case MIDI_CONTROLLER_SOUNDS_OFF: name = "All sounds off"; break;
377 case MIDI_CONTROLLER_RESET: name = "Reset"; break;
378 case MIDI_CONTROLLER_LOCAL: name = "Local"; break;
379 case MIDI_CONTROLLER_ALL_NOTES_OFF: name = "All notes off"; break;
380 }
381 cout << t << ": Controller change: channel " << ch
382 << " controller " << j->getData1();
383 if (name != "") cout << " (" << name << ")";
384 cout << " value " << j->getData2() << endl;
385 }
386 break;
387
388 case MIDI_PROG_CHANGE:
389 cout << t << ": Program change: channel " << ch
390 << " program " << j->getData1() << endl;
391 break;
392
393 case MIDI_CHNL_AFTERTOUCH:
394 cout << t << ": Channel aftertouch: channel " << ch
395 << " pressure " << j->getData1() << endl;
396 break;
397
398 case MIDI_PITCH_BEND:
399 cout << t << ": Pitch bend: channel " << ch
400 << " value " << (int)j->getData2() * 128 + (int)j->getData1() << endl;
401 break;
402
403 case MIDI_SYSTEM_EXCLUSIVE:
404 cout << t << ": System exclusive: code "
405 << (int)j->getMessageType() << " message length " <<
406 j->getMetaMessage().length() << endl;
407 break;
408
409
410 }
411
412
413 }
414
415
416 }
417
418 //}
419
420
421
422
423 }//end cannam midi main
424