comparison projects/basic_midi/render.cpp @ 184:9108a0a34cb8

Better(non ideal) monophonic behaviour of the basic midi implementation. Would require to remember what notes are being held down and release either the most recent or low/high priority.
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 17 Jan 2016 21:56:13 +0000
parents 391ad036557d
children d7148d21aaa5
comparison
equal deleted inserted replaced
183:2bdb48d1fca6 184:9108a0a34cb8
47 int message; 47 int message;
48 static bool noteOn = 0; 48 static bool noteOn = 0;
49 static int velocity = 0; 49 static int velocity = 0;
50 static int noteNumber = 0; 50 static int noteNumber = 0;
51 static int waitingFor = kNoteOn; 51 static int waitingFor = kNoteOn;
52 static int playingNote = -1;
52 while ((message = midi.getInput()) >= 0){ 53 while ((message = midi.getInput()) >= 0){
53 switch(waitingFor){ 54 switch(waitingFor){
54 case kNoteOn: 55 case kNoteOn:
55 if(message == noteOnStatus){ 56 if(message == noteOnStatus){
56 noteOn = true;
57 waitingFor = kNoteNumber; 57 waitingFor = kNoteNumber;
58 } 58 }
59 break; 59 break;
60 case kNoteNumber: 60 case kNoteNumber:
61 if((message & (1<<8)) == 0){ 61 if((message & (1<<8)) == 0){
62 noteNumber = message; 62 noteNumber = message;
63 waitingFor = kVelocity; 63 waitingFor = kVelocity;
64 f0 = powf(2, (noteNumber-69)/12.0f) * 440;
65 phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate;
66 } 64 }
67 break; 65 break;
68 case kVelocity: 66 case kVelocity:
69 if((message & (1<<8)) == 0){ 67 if((message & (1<<8)) == 0){
70 velocity = message; 68 int _velocity = message;
71 waitingFor = kNoteOn; 69 waitingFor = kNoteOn;
72 if(velocity == 0) 70 // "monophonic" behaviour, with priority to the latest note on
71 // i.e.: a note off from a previous note does not stop the current note
72 // still you might end up having a key down and no note being played if you pressed and released another
73 // key in the meantime
74 if(_velocity == 0 && noteNumber == playingNote){
73 noteOn = false; 75 noteOn = false;
76 playingNote = -1;
77 velocity = _velocity;
78 } else if (_velocity > 0) {
79 noteOn = true;
80 velocity = _velocity;
81 playingNote = noteNumber;
82 f0 = powf(2, (playingNote-69)/12.0f) * 440;
83 phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate;
84 }
74 rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity); 85 rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity);
75 } 86 }
76 break; 87 break;
77 } 88 }
78 } 89 }