changeset 226:af1e662400fc mergingClockSync

Added argument to callback
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 13 Mar 2016 03:31:19 +0000
parents 444f6028d6b1
children 97f5219429b3
files core/Midi.cpp include/Midi.h projects/basic_midi/render.cpp
diffstat 3 files changed, 26 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/core/Midi.cpp	Tue Mar 08 16:03:14 2016 +0000
+++ b/core/Midi.cpp	Sun Mar 13 03:31:19 2016 +0000
@@ -61,7 +61,7 @@
 				// done with the current message
 				// call the callback if available
 				if(isCallbackEnabled() == true){
-					messageReadyCallback(getNextChannelMessage());
+					messageReadyCallback(getNextChannelMessage(), callbackArg);
 				}
 				waitingForStatus = true;
 				writePointer++;
--- a/include/Midi.h	Tue Mar 08 16:03:14 2016 +0000
+++ b/include/Midi.h	Sun Mar 13 03:31:19 2016 +0000
@@ -115,8 +115,9 @@
 	unsigned int readPointer;
 	unsigned int elapsedDataBytes;
 	bool waitingForStatus;
-	void (*messageReadyCallback)(MidiChannelMessage);
+	void (*messageReadyCallback)(MidiChannelMessage,void*);
 	bool callbackEnabled;
+	void* callbackArg;
 public:
 	MidiParser(){
 		waitingForStatus = true;
@@ -126,6 +127,7 @@
 		readPointer = 0;
 		callbackEnabled = false;
 		messageReadyCallback = NULL;
+		callbackArg = NULL;
 	}
 
 	/**
@@ -142,17 +144,21 @@
 	 * Sets the callback to call when a new MidiChannelMessage is available
 	 * from the input port.
 	 *
-	 * In order to deactivate the callback, call this method with NULL as an
-	 * argument.
+	 * The callback will be called with two arguments:
+	 *   callback(MidiChannelMessage newMessage, void* arg)
+	 *
+	 * In order to deactivate the callback, call this method with NULL as the
+	 * first argument.
 	 * While the callback is enabled, calling numAvailableMessages() and
 	 * getNextChannelMessage() is still possible, but it will probably always
 	 * return 0 as the callback is called as soon as a new message is available.
 	 *
-	 * @param newCallback the callback function. This function
-	 * should takes one argument: the MidiChannelMessage .
+	 * @param newCallback the callback function.
+	 * @param arg the second argument to be passed to the callback function.
 	 *
 	 */
-	void setCallback(void (*newCallback)(MidiChannelMessage)){
+	void setCallback(void (*newCallback)(MidiChannelMessage, void*), void* arg=NULL){
+		callbackArg = arg;
 		messageReadyCallback = newCallback;
 		if(newCallback != NULL){
 			callbackEnabled = true;
@@ -251,13 +257,13 @@
 	 *
 	 * Internally, it calls enableParser() and the MidiParser::setCallback();
 	 *
-	 * @param newCallback the callback function. This function
-	 * should takes one argument: the MidiChannelMessage .
+	 * @param newCallback the callback function.
+	 * @param arg the second argument to be passed to the callback function.
 	 */
-	void setParserCallback(void (*callback)(MidiChannelMessage)){
+	void setParserCallback(void (*callback)(MidiChannelMessage, void*), void* arg=NULL){
 		// if callback is not NULL, also enable the parser
 		enableParser(callback != NULL); //this needs to be first, as it deletes the parser(if exists)
-		getParser()->setCallback(callback);
+		getParser()->setCallback(callback, arg);
 	}
 
 	/**
--- a/projects/basic_midi/render.cpp	Tue Mar 08 16:03:14 2016 +0000
+++ b/projects/basic_midi/render.cpp	Sun Mar 13 03:31:19 2016 +0000
@@ -18,7 +18,10 @@
 int gVelocity = 0;
 float gSamplingPeriod = 0;
 
-void midiMessageCallback(MidiChannelMessage message){
+void midiMessageCallback(MidiChannelMessage message, void* arg){
+	if(arg != NULL){
+		rt_printf("Message from midi port %d: ", *(int*)arg);
+	}
 	message.prettyPrint();
 	if(message.getType() == kmmNoteOn){
 		gFreq = powf(2, (message.getDataByte(0)-69)/12.0f) * 440;
@@ -37,14 +40,15 @@
 //
 // Return true on success; returning false halts the program.
 Midi midi;
+int gMidiPort0 = 0;
 bool setup(BeagleRTContext *context, void *userData)
 {
-	midi.readFrom(0);
-	midi.writeTo(0);
+	midi.readFrom(gMidiPort0);
+	midi.writeTo(gMidiPort0);
 	midi.enableParser(true);
-	midi.setParserCallback(midiMessageCallback);
+	midi.setParserCallback(midiMessageCallback, &gMidiPort0);
 	if(context->analogFrames == 0) {
-		rt_printf("Error: this example needs the matrix enabled\n");
+		rt_printf("Error: this example needs the analog I/O to be enabled\n");
 		return false;
 	}
 	gSamplingPeriod = 1/context->audioSampleRate;