changeset 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 2bdb48d1fca6
children 31e898185ae8
files core/Midi.cpp projects/basic_midi/render.cpp
diffstat 2 files changed, 16 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/core/Midi.cpp	Fri Jan 15 23:48:08 2016 +0000
+++ b/core/Midi.cpp	Sun Jan 17 21:56:13 2016 +0000
@@ -37,7 +37,6 @@
 void Midi::midiInputLoop(){
 	printf("Midi input loop %d\n", objAddrs.size());
 	for(unsigned int n = 0; n < objAddrs.size(); n++){
-		printf("In the for\n");
 		objAddrs[n] -> readInputLoop();
 	}
 }
--- a/projects/basic_midi/render.cpp	Fri Jan 15 23:48:08 2016 +0000
+++ b/projects/basic_midi/render.cpp	Sun Jan 17 21:56:13 2016 +0000
@@ -49,11 +49,11 @@
 	static int velocity = 0;
 	static int noteNumber = 0;
 	static int waitingFor = kNoteOn;
+	static int playingNote = -1;
 	while ((message = midi.getInput()) >= 0){
 		switch(waitingFor){
 		case kNoteOn:
 			if(message == noteOnStatus){
-				noteOn = true;
 				waitingFor = kNoteNumber;
 			}
 			break;
@@ -61,16 +61,27 @@
 			if((message & (1<<8)) == 0){
 				noteNumber = message;
 				waitingFor = kVelocity;
-				f0 = powf(2, (noteNumber-69)/12.0f) * 440;
-				phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate;
 			}
 			break;
 		case kVelocity:
 			if((message & (1<<8)) == 0){
-				velocity = message;
+				int _velocity = message;
 				waitingFor = kNoteOn;
-				if(velocity == 0)
+				// "monophonic" behaviour, with priority to the latest note on
+				// i.e.: a note off from a previous note does not stop the current note
+				// still you might end up having a key down and no note being played if you pressed and released another
+				// key in the meantime
+				if(_velocity == 0 && noteNumber == playingNote){
 					noteOn = false;
+					playingNote = -1;
+					velocity = _velocity;
+				} else if (_velocity > 0) {
+					noteOn = true;
+					velocity = _velocity;
+					playingNote = noteNumber;
+					f0 = powf(2, (playingNote-69)/12.0f) * 440;
+					phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate;
+				}
 				rt_printf("NoteOn: %d, NoteNumber: %d, velocity: %d\n", noteOn, noteNumber, velocity);
 			}
 			break;