changeset 340:69d86429a1cf prerelease

More on libpd support for threads
author Giulio Moro <giuliomoro@yahoo.it>
date Mon, 06 Jun 2016 02:37:30 +0100
parents 02e6f96466f8
children 7af9c5be3434
files examples/basic_libpd/render.cpp
diffstat 1 files changed, 23 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/examples/basic_libpd/render.cpp	Sun Jun 05 22:20:17 2016 +0100
+++ b/examples/basic_libpd/render.cpp	Mon Jun 06 02:37:30 2016 +0100
@@ -25,7 +25,7 @@
 // in from the call to initAudio().
 //
 // Return true on success; returning false halts the program.
-unsigned int gLibPdBlockSize;  //make sure this matches the one used to compile libpd
+unsigned int gLibpdBlockSize;  //make sure this matches the one used to compile libpd
 
 unsigned int gChannelsInUse = 10;
 int gBufLength;
@@ -43,7 +43,7 @@
 
 UdpServer udpServer;
 
-void udpRead(){
+void libpdReadFilesLoop(){
     while(!gShouldStop){
     	// check for modified sockets/file descriptors
     	// (libpd would normally do this every block WITHIN the audio thread)
@@ -52,8 +52,11 @@
         usleep(1000);
     }
 }
+
 #define PARSE_MIDI
-AuxiliaryTask udpReadTask;
+AuxiliaryTask libpdReadFilesTask;
+AuxiliaryTask libpdProcessMessageQueueTask;
+AuxiliaryTask libpdProcessMidiQueueTask;
 Midi midi;
 
 bool setup(BelaContext *context, void *userData)
@@ -68,25 +71,26 @@
 	gChannelsInUse = min((int)(context->analogChannels+context->audioChannels), (int)gChannelsInUse);
 	udpServer.bindToPort(1234);
 
-	gLibPdBlockSize = libpd_blocksize();
+	gLibpdBlockSize = libpd_blocksize();
 	// check that we are not running with a blocksize smaller than gLibPdBlockSize
 	// it would still work, but the load would be executed unevenly between calls to render
-	if(context->audioFrames < gLibPdBlockSize){
-		fprintf(stderr, "Error: minimum block size must be %d\n", gLibPdBlockSize);
+	if(context->audioFrames < gLibpdBlockSize){
+		fprintf(stderr, "Error: minimum block size must be %d\n", gLibpdBlockSize);
 		return false;
 	}
 	// init pd
 	libpd_set_queued_printhook(Bela_printHook); // set this before calling libpd_init
 	libpd_set_queued_noteonhook(pdnoteon);
+	//TODO: add hooks for other midi events and generate MIDI output appropriately
 	libpd_queued_init();
-	//TODO: analyse the ASCII of the patch file and find the in/outs to use
+	//TODO: ideally, we would analyse the ASCII of the patch file and find the in/outs to use
 	libpd_init_audio(gChannelsInUse, gChannelsInUse, context->audioSampleRate);
 
 	libpd_start_message(1); // one entry in list
 	libpd_add_float(1.0f);
 	libpd_finish_message("pd", "dsp");
 
-	gBufLength = max((uint32_t)gLibPdBlockSize, context->audioFrames);
+	gBufLength = max(gLibpdBlockSize, context->audioFrames);
 	unsigned int bufferSize = sizeof(float)*gChannelsInUse*gBufLength;
 	gInBuf = (float*)malloc(bufferSize);
 	gOutBuf = (float*)malloc(bufferSize);
@@ -97,8 +101,12 @@
 	// open patch       [; pd open file folder(
 	libpd_openfile(file, folder);
     
-	udpReadTask = Bela_createAuxiliaryTask(udpRead, 60, "udpReadTask");
-	Bela_scheduleAuxiliaryTask(udpReadTask);
+	libpdReadFilesTask = Bela_createAuxiliaryTask(libpdReadFilesLoop, 60, "libpdReadFiles");
+	Bela_scheduleAuxiliaryTask(libpdReadFilesTask);
+
+	// Higher priority for the midi queue and lower priority for the message queue. Adjust to taste
+	libpdProcessMidiQueueTask = Bela_createAuxiliaryTask(libpd_queued_receive_midi_messages, 80, "libpdProcessMidiQueue");
+	libpdProcessMessageQueueTask = Bela_createAuxiliaryTask(libpd_queued_receive_pd_messages, 70, "libpdProcessMessageQueue");
 	return true;
 }
 
@@ -182,9 +190,9 @@
 	int input;
 	while((input = midi.getInput()) >= 0){
 		libpd_midibyte(0, input);
-		rt_printf("input: %d\n", input);
 	}
 #endif /* PARSE_MIDI */
+
 	static unsigned int inW = 0;
 	static unsigned int outR = 0;
 /*
@@ -220,9 +228,9 @@
 	}
 	// rt_printf("inW %d\n", inW);
 	if(inW == 0){ //if the buffer is full, process it
-		static int numberOfPdBlocksToProcess = gBufLength/gLibPdBlockSize;
+		static int numberOfPdBlocksToProcess = gBufLength/gLibpdBlockSize;
 		// TODO: see if we can rewrite libpd_process_float so that it takes a buffer
-		// of interleaved channels of arbitrary length channels rather than a series of
+		// of interleaved channels of arbitrary length rather than a series of
 		// stacked buffers of size gLibPdBlockSize as it currently does.
 		libpd_process_float(numberOfPdBlocksToProcess, gInBuf, gOutBuf);
 		outR = 0; // reset the read pointer. NOTE: hopefully this is needed only the first time
@@ -260,6 +268,8 @@
 	}
 	// rt_printf("outR %d, analogChannelsInUse %d, channelsInUse %d\n", 
 	// 	outR , analogChannelsInUse, gChannelsInUse);
+	Bela_scheduleAuxiliaryTask(libpdProcessMidiQueueTask);
+	Bela_scheduleAuxiliaryTask(libpdProcessMessageQueueTask);
 }
 
 // cleanup() is called once at the end, after the audio has stopped.