changeset 337:5e2780bfbfed prerelease

Clean up of basic_libpd
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 05 Jun 2016 20:28:43 +0100
parents 6599a9978ac4
children 1802f99cd77f
files examples/basic_libpd/render.cpp
diffstat 1 files changed, 24 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/examples/basic_libpd/render.cpp	Sun Jun 05 20:22:55 2016 +0100
+++ b/examples/basic_libpd/render.cpp	Sun Jun 05 20:28:43 2016 +0100
@@ -24,9 +24,9 @@
 // in from the call to initAudio().
 //
 // Return true on success; returning false halts the program.
-#define DEFDACBLKSIZE 8u  //make sure this matches the one used to compile libpd
+unsigned int gLibPdBlockSize;  //make sure this matches the one used to compile libpd
 
-int gChannelsInUse = 10;
+unsigned int gChannelsInUse = 10;
 int gBufLength;
 
 float* gInBuf;
@@ -43,15 +43,18 @@
 UdpServer udpServer;
 
 void udpRead(){
-    char dest[100] = {0};
     while(!gShouldStop){
+    	// check for modified sockets/file descriptors
+    	// (libpd would normally do this every block WITHIN the audio thread)
+    	// not sure if this is thread-safe at the moment
     	libpd_sys_microsleep(0);
         usleep(1000);
     }
 }
 #define PARSE_MIDI
-AuxiliaryTask  udpReadTask;
+AuxiliaryTask udpReadTask;
 Midi midi;
+
 bool setup(BelaContext *context, void *userData)
 {
 	midi.readFrom(0);
@@ -64,21 +67,13 @@
 	gChannelsInUse = min((int)(context->analogChannels+context->audioChannels), (int)gChannelsInUse);
 	udpServer.bindToPort(1234);
 
-	// check that we are not running with a blocksize smaller than DEFDACBLKSIZE
+	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 < DEFDACBLKSIZE){
-		fprintf(stderr, "Error: minimum block size must be %d\n", DEFDACBLKSIZE);
+	if(context->audioFrames < gLibPdBlockSize){
+		fprintf(stderr, "Error: minimum block size must be %d\n", gLibPdBlockSize);
 		return false;
 	}
-
-	// check that the sampling rate of the analogs is the same as audio if running with
-	// more than 2 channels (that is with analog). If we fix the TODO in render, then
-	// this test is not needed.
-//	if(context->analogFrames != context->audioFrames){
-//		fprintf(stderr, "Error: analog and audio sampling rates must be the same\n");
-//		return false;
-//	}
-	//following lines borrowed from libpd/samples/c/pdtest/pdtest.c
 	// init pd
 	libpd_set_printhook(Bela_printHook); // set this before calling libpd_init
 	libpd_set_noteonhook(pdnoteon);
@@ -90,7 +85,7 @@
 	libpd_add_float(1.0f);
 	libpd_finish_message("pd", "dsp");
 
-	gBufLength = max(DEFDACBLKSIZE, context->audioFrames);
+	gBufLength = max((uint32_t)gLibPdBlockSize, context->audioFrames);
 	unsigned int bufferSize = sizeof(float)*gChannelsInUse*gBufLength;
 	gInBuf = (float*)malloc(bufferSize);
 	gOutBuf = (float*)malloc(bufferSize);
@@ -177,6 +172,9 @@
 				libpd_pitchbend(channel, value);
 				break;
 			}
+			case kmmNone:
+			case kmmAny:
+				break;
 		}
 	}
 #else
@@ -186,15 +184,15 @@
 		rt_printf("input: %d\n", input);
 	}
 #endif /* PARSE_MIDI */
-	static int inW = 0;
-	static int outR = 0;
+	static unsigned int inW = 0;
+	static unsigned int outR = 0;
 /*
  *	NOTE: if you are only using audio (or only analogs) and you are using interleaved buffers
- *	 and the blocksize of Bela is the same as DEFDACBLKSIZE, then you probably
+ *	 and the blocksize of Bela is the same as gLibPdBlockSize, then you probably
  *	 do not need the for loops before and after libpd_process_float, so you can save quite some
  *	 memory operations.
  */
-	static int analogChannelsInUse = min(context->analogChannels, gChannelsInUse - context->audioChannels);
+	static unsigned int analogChannelsInUse = min(context->analogChannels, gChannelsInUse - context->audioChannels);
 	// rt_printf("channelsInUse: %d, analogChannels in Use: %d\n", gChannelsInUse, analogChannelsInUse);
 	for(unsigned int n = 0; n < context->audioFrames; ++n){ //pd buffers are interleaved
 		for(unsigned int ch = 0; ch < context->audioChannels; ++ch){ //first two channels are audio
@@ -203,15 +201,15 @@
 		// then analogs
 		// this loop resamples by ZOH, as needed, using m
 		if(context->analogChannels == 8 ){ //hold the value for two frames
-			for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
+			for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
 				gInBuf[inW++] = analogRead(context, n/2, analogCh); // n/2 wil be the same for n and n+1 when n is even
 			}
 		} else if(context->analogChannels == 4){ //write every frame
-			for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
+			for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
 				gInBuf[inW++] = analogRead(context, n, analogCh);
 			}
 		} else if(context->analogChannels == 2){ //drop every other frame
-			for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
+			for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
 				gInBuf[inW++] = analogRead(context, n*2, analogCh);
 			}
 		}
@@ -221,7 +219,7 @@
 	}
 	// rt_printf("inW %d\n", inW);
 	if(inW == 0){ //if the buffer is full, process it
-		static int numberOfPdBlocksToProcess = gBufLength/DEFDACBLKSIZE;
+		static int numberOfPdBlocksToProcess = gBufLength/gLibPdBlockSize;
 		libpd_process_float(numberOfPdBlocksToProcess, gInBuf, gOutBuf);
 		outR = 0; // reset the read pointer. NOTE: hopefully this is needed only the first time
 	}
@@ -241,7 +239,7 @@
 				}
 			}
 		} else if(context->analogChannels == 4){ //write every frame
-			for(int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
+			for(unsigned int analogCh = 0; analogCh < analogChannelsInUse; ++analogCh){
 				float analogOut = gOutBuf[outR++];
 				analogWrite(context, n, analogCh, analogOut);
 			}