diff projects/basic_midi/render.cpp @ 199:b128e3ea84ff

Fixed midi docs, implentation and example
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 05 Feb 2016 06:49:33 +0000
parents 265a527f8be8
children 97faaf985164
line wrap: on
line diff
--- a/projects/basic_midi/render.cpp	Fri Feb 05 06:17:35 2016 +0000
+++ b/projects/basic_midi/render.cpp	Fri Feb 05 06:49:33 2016 +0000
@@ -25,7 +25,7 @@
 {
 	midi.readFrom(0);
 	midi.writeTo(0);
-	midi.enableParser(true);
+//	midi.enableParser(true);
 	if(context->analogFrames == 0) {
 		rt_printf("Error: this example needs the matrix enabled\n");
 		return false;
@@ -50,9 +50,9 @@
 	static int noteNumber = 0;
 	static int waitingFor = kNoteOn;
 	static int playingNote = -1;
-///*
+/*
 	int message;
-	if(0) // one way of getting the midi data is to parse them yourself (you should set midi.enableParser(false) above):
+	// one way of getting the midi data is to parse them yourself (you should set midi.enableParser(false) above):
 	while ((message = midi.getInput()) >= 0){
 		rt_printf("%d\n", message);
 		switch(waitingFor){
@@ -91,13 +91,20 @@
 			break;
 		}
 	}
-//*/
+*/
 	int num;
 	//alternatively, you can use the built-in parser (only processes channel messages at the moment).
 	while((num = midi.getParser()->numAvailableMessages()) > 0){
 		static MidiChannelMessage message;
 		message = midi.getParser()->getNextChannelMessage();
 		message.prettyPrint();
+		if(message.getType() == kmmNoteOn){
+			f0 = powf(2, (message.getDataByte(0)-69)/12.0f) * 440;
+			velocity = message.getDataByte(1);
+			phaseIncrement = 2 * M_PI * f0 / context->audioSampleRate;
+			noteOn = velocity > 0;
+			rt_printf("v0:%f, ph: %6.5f, velocity: %d\n", f0, phaseIncrement, velocity);
+		}
 	}
 
 	for(unsigned int n = 0; n < context->analogFrames; n++){
@@ -109,7 +116,9 @@
 			midi_byte_t bytes[6] = {176, 30, state*127, 176, 67, 30}; // toggle the OWL led and ask for the led status
 			midi.writeOutput(bytes, 6);
 		}
-
+		count++;
+	}
+	for(unsigned int n = 0; n < context->audioFrames; n++){
 		if(noteOn == 1){
 			static float phase = 0;
 			phase += phaseIncrement;
@@ -122,7 +131,6 @@
 			audioWriteFrame(context, n, 0, 0);
 			audioWriteFrame(context, n, 1, 0);
 		}
-		count++;
 	}
 }