changeset 199:b128e3ea84ff

Fixed midi docs, implentation and example
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 05 Feb 2016 06:49:33 +0000
parents 62f6269f4b3e
children c3a34eaef0cf e122cddc1e49
files core/Midi.cpp include/Midi.h projects/basic_midi/render.cpp
diffstat 3 files changed, 27 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/core/Midi.cpp	Fri Feb 05 06:17:35 2016 +0000
+++ b/core/Midi.cpp	Fri Feb 05 06:49:33 2016 +0000
@@ -134,26 +134,10 @@
 		}
 		if(parserEnabled == true && ret > 0){ // if the parser is enabled and there is new data, send the data to it
 			int input;
-			while((input=getInput()) >= 0){
+			while((input=_getInput()) >= 0){
 				midi_byte_t inputByte = (midi_byte_t)(input);
-//				printf("__0x%x\n", input);
 				inputParser->parse(&inputByte, 1);
 			}
-//			if(inputBytesReadPointer == inputBytesWritePointer){
-				// this should never be the case, it should only happen when ret == 0
-//			} else {
-
-//				unsigned int length = (inputBytesWritePointer - inputBytesReadPointer + inputBytes.size())
-//						% inputBytes.size();
-//				rt_printf("inputBytes: 0x%x 0x%x 0x%x", inputBytes[inputBytesReadPointer],
-//						inputBytes[inputBytesReadPointer+1], inputBytes[inputBytesReadPointer+2]);
-//				length = inputParser->parse(&inputBytes[inputBytesReadPointer], length);
-//				inputBytesReadPointer += length; //if parserEnabled, the readPointer is incremented here,
-//											     //so calls to getInput() should be avoided
-//				if(inputBytesReadPointer == inputBytes.size()){
-//					inputBytesReadPointer = 0;
-//				}
-//			}
 		}
 		if(ret < maxBytesToRead){ //no more data to retrieve at the moment
 			usleep(1000);
@@ -206,10 +190,7 @@
 	}
 }
 
-int Midi::getInput(){
-//	if(parserEnabled == true) {
-//		return -3;
-//	}
+int Midi::_getInput(){
 	if(inputPort < 0)
 		return -2;
 	if(inputBytesReadPointer == inputBytesWritePointer){
@@ -222,6 +203,13 @@
 	return inputMessage;
 }
 
+int Midi::getInput(){
+	if(parserEnabled == true) {
+		return -3;
+	}
+	return _getInput();
+}
+
 MidiParser* Midi::getParser(){
 	if(parserEnabled == false){
 		return 0;
--- a/include/Midi.h	Fri Feb 05 06:17:35 2016 +0000
+++ b/include/Midi.h	Fri Feb 05 06:49:33 2016 +0000
@@ -147,7 +147,8 @@
 	/**
 	 * Get the oldest channel message in the buffer.
 	 *
-	 * You should not call this function when numAvailableMessages() returns 0.
+	 * If this method is called when numAvailableMessages()==0, then
+	 * a message with all fields set to zero is returned.
 	 * @param type the type of the message to retrieve
 	 * @return a copy of the oldest message of the give type in the buffer
 	 */
@@ -187,7 +188,7 @@
 	/**
 	 * Enable the input MidiParser.
 	 *
-	 * If the parser is enabled, readFrom() will return an error code.
+	 * If the parser is enabled, getInput() will return an error code.
 	 * Midi messages should instead be retrieved via, e.g.: getMidiParser()->getNextChannelMessage();
 	 *
 	 * @param enable true to enable the input MidiParser, false to disable it.
@@ -247,6 +248,7 @@
     static bool staticConstructed;
 	static void staticConstructor();
 private:
+	int _getInput();
 	void readInputLoop();
 	void writeOutputLoop();
 	int outputPort;
--- 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++;
 	}
 }