diff scripts/hvresources/render.cpp @ 198:62f6269f4b3e

Added support for MidiIn to heavy (with example patch and subpatches).
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 05 Feb 2016 06:17:35 +0000
parents 3b8a28edae41
children c768ed1055b0
line wrap: on
line diff
--- a/scripts/hvresources/render.cpp	Fri Feb 05 06:16:35 2016 +0000
+++ b/scripts/hvresources/render.cpp	Fri Feb 05 06:17:35 2016 +0000
@@ -12,13 +12,13 @@
  */
 
 #include <BeagleRT.h>
+#include <Midi.h>
 #include <cmath>
 #include "../include/Utilities.h"
 #include "Heavy_bbb.h"
 #include <string.h>
 #include <stdlib.h>
 #include <string.h>
-
 /*
  *	HEAVY CONTEXT & BUFFERS
  */
@@ -53,6 +53,7 @@
  * SETUP, RENDER LOOP & CLEANUP
  */
 
+Midi midi;
 bool setup(BeagleRTContext *context, void *userData)	{
 
 	/* HEAVY */
@@ -79,6 +80,9 @@
 	// Set heavy send hook
 	hv_setSendHook(gHeavyContext, sendHook);
 
+	midi.readFrom(0);
+	midi.writeTo(0);
+	midi.enableParser(true);
 	return true;
 }
 
@@ -111,6 +115,35 @@
 
 	// replacement for bang~ object
 	//hv_vscheduleMessageForReceiver(gHeavyContext, "bbb_bang", 0.0f, "b");
+	{
+		int num;
+		while((num = midi.getParser()->numAvailableMessages()) > 0){
+			static MidiChannelMessage message;
+			message = midi.getParser()->getNextChannelMessage();
+			switch(message.getType()){
+			case kmmNoteOn: {
+//					message.prettyPrint();
+					float noteNumber = message.getDataByte(0);
+					float velocity = message.getDataByte(1);
+					float channel = message.getChannel();
+//					rt_printf("message: noteNumber: %f, velocity: %f, channel: %f\n", noteNumber, velocity, channel);
+					hv_vscheduleMessageForReceiver(gHeavyContext, "hv_notein", 0, "fff", noteNumber, velocity, channel);
+				}
+				break;
+			case kmmControlChange: {
+				hv_vscheduleMessageForReceiver(gHeavyContext, "hv_ctlin", 0, "fff",
+						(float)message.getDataByte(1), (float)message.getDataByte(0), (float)message.getChannel());
+				}
+				break;
+			case kmmProgramChange:
+				hv_vscheduleMessageForReceiver(gHeavyContext, "hv_pgmin", 0, "ff",
+						(float)message.getDataByte(0), (float)message.getChannel());
+				break;
+			}
+		}
+	}
+//	hv_sendFloatToReceiver(gHeavyContext, "notein", 1.123f);
+
 
 	hv_bbb_process_inline(gHeavyContext, gHvInputBuffers, gHvOutputBuffers, context->audioFrames);