changeset 125:850a4a9bd832 scope-refactoring

Added ifdefs and unified the code with udpioplugin ... the latter has not been tested (or committed). TODO: still it hangs after ctrl-c BeagleRT (auxiliary tasks do not terminate). TODO: sometimes you can hear dropouts in the transmission. Maybe it is due to pointer drifting. Rebooting BBB fixes/affects this issue.
author Giulio Moro <giuliomoro@yahoo.it>
date Sat, 22 Aug 2015 02:53:36 +0100
parents 23137a333c93
children 719119fb2905
files core/ReceiveAudioThread.cpp include/ReceiveAudioThread.h
diffstat 2 files changed, 55 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/core/ReceiveAudioThread.cpp	Sat Aug 22 02:51:39 2015 +0100
+++ b/core/ReceiveAudioThread.cpp	Sat Aug 22 02:53:36 2015 +0100
@@ -20,7 +20,7 @@
     threadIsExiting=false;
 	receiveDataTask=BeagleRT_createAuxiliaryTask(receiveData, 90, "receiveDataTask"); //TODO: allow different priorities
 }
-#endif
+#endif /* JUCE */
 
 void ReceiveAudioThread::dealloc(){
     free(buffer);
@@ -63,13 +63,13 @@
         pushPayload(writePointer); //backup headerLength samples. This could be skipped if writePointer==0
         //read header+payload
 //JUCE        int numBytes=socket.read(buffer+writePointer, bytesToRead,1);
-        int numBytes=socket.read(buffer+writePointer, bytesToRead, false); //read without waiting.
+        int numBytes=socket.read(buffer+writePointer, bytesToRead, true); //read without waiting.
             //TODO: (if using variable-length payload) validate the actual numBytes read against the size declared in the header
         if(numBytes<0){
             printf("error numBytes1\n");
             return -3; //TODO: something went wrong, you have to discard the rest of the packet!
         }
-        if(numBytes==0){//TODO: when inplementing waitUntilReady, this should not happen unless you actually receive a packate of size zero (is it at all possible?)
+        if(numBytes==0){//TODO: this should not happen unless you actually receive a packet of size zero (is it at all possible?)
 //        	printf("received 0 bytes\n");
         	return 0;
         }
@@ -101,9 +101,14 @@
     }
     return 0; //timeout occurred
 }
+//JUCE    Thread(threadName),
+#ifdef JUCE
+ReceiveAudioThread::ReceiveAudioThread(const String &threadName) :
+			Thread(threadName),
+#else
 ReceiveAudioThread::ReceiveAudioThread() :
-//JUCE    Thread(threadName),
-    socket(0),
+#endif /* JUCE */
+	socket(NULL),
     listening(false),
     bufferReady(false),
     buffer(NULL),
@@ -111,15 +116,25 @@
     bufferLength(0),
     lastValidPointer(0),
     waitForSocketTime(100),
-    threadPriority(95)
+#ifdef JUCE
+    threadPriority(5)
+#else
+    threadPriority(88)
+#endif /* JUCE */
 {};
 ReceiveAudioThread::~ReceiveAudioThread(){
-//JUCE    stopThread(1000);
+#ifdef JUCE
+	stopThread(1000);
+#else
+	printf("inside the destructor\n");
 	while(threadRunning){
+		printf("while in the destructor\n");
 		usleep(sleepTime*2);	//wait for thread to stop
 		std::cout<< "Waiting for receiveAudioTask to stop" << std::endl;
 	}
+#endif /* JUCE */
 	//TODO: check if thread stopped, otherwise kill it before dealloc
+	printf("dealloc\n");
     dealloc();
 }
 void ReceiveAudioThread::init(int aPort, int aSamplesPerBlock, int aChannel){
@@ -128,7 +143,7 @@
 #else
   staticConstructor();
   objAddrs.push_back(this);//TODO: this line should be in the constructor
-#endif
+#endif /* JUCE */
   bindToPort(aPort);
   channel=aChannel;
   printf("Channel %d is receiving on port %d\n",aChannel, aPort);
@@ -141,24 +156,30 @@
   buffer=(float*)malloc(sizeof(float)*bufferLength);
   if(buffer==NULL) // something wrong
     return;
-  bufferReady=true;
   lastValidPointer=headerLength+ ((bufferLength-headerLength)/payloadLength)*payloadLength;
   memset(buffer,0,bufferLength*sizeof(float));
   stackBuffer=(float*)malloc(sizeof(float)*headerLength);
+  if(stackBuffer==NULL) // something wrong
+      return;
+  bufferReady=true;
   bytesToRead=sizeof(float)*(payloadLength + headerLength);
   writePointer=-1;
   readPointer=0;
   sleepTime=payloadLength/(float)44100 /4.0; //set sleepTime so that you do not check too often or too infrequently
-//JUCE    startThread(threadPriority);
+#ifdef JUCE
+  startThread(threadPriority);
+#else
+  //TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled
+#endif /* JUCE */
 }
 
 void ReceiveAudioThread::bindToPort(int aPort){
     listening=socket.bindToPort(aPort);
 #ifdef JUCE
 #else
-    if(listening==false)
+    if(listening==false) //this condition is valid also for JUCE, but we do not printf in JUCE
     	printf("Could not bind to port %d\n",aPort);
-#endif
+#endif /* JUCE */
 }
 bool ReceiveAudioThread::isListening(){
     return listening;
@@ -204,7 +225,7 @@
             readPointer=readPointer-lastValidPointer+headerLength;
         }
     }
-    return readPointer; 
+    return length;
 }
 int ReceiveAudioThread::getSamplesSrc(float *destination, int length, float samplingRateRatio){
 	return getSamplesSrc(destination, length, samplingRateRatio, 1,0);
@@ -214,6 +235,8 @@
 bool ReceiveAudioThread::isBufferReady(){
     return bufferReady;
 }
+#ifdef JUCE
+#else
 void ReceiveAudioThread::startThread(){
 	printf("receivedata is going to be  scheduled\n");
 	BeagleRT_scheduleAuxiliaryTask(receiveDataTask);
@@ -225,6 +248,7 @@
 bool ReceiveAudioThread::threadShouldExit(){
 	return(gShouldStop || threadIsExiting );
 }
+#endif /* JUCE */
 void ReceiveAudioThread::run(){
     //  fd2=fopen("buffer.m","w"); //DEBUG
     //  fprintf(fd2, "buf=["); //DEBUG
@@ -239,9 +263,10 @@
 			ReceiveAudioThread::objAddrs[n]->readUdpToBuffer();
 		}
 		usleep(sleepTime); //TODO: use rt_task_sleep instead
-#endif
+#endif /* JUCE */
     }
     threadRunning=false;
+    printf("Thread is not running \n");
     //  fprintf(fd,"];readPointer,writePointer,lastValidPointer,destination]=deal(var(:,1), var(:,2), var(:,3), var(:,4));"); //DEBUG
     //  fclose(fd);//DEBUG
     //  fprintf(fd2,"];");//DEBUG
--- a/include/ReceiveAudioThread.h	Sat Aug 22 02:51:39 2015 +0100
+++ b/include/ReceiveAudioThread.h	Sat Aug 22 02:53:36 2015 +0100
@@ -1,21 +1,31 @@
 #ifndef RECEIVEAUDIOTHREAD_H_INCLUDED
 #define RECEIVEAUDIOTHREAD_H_INCLUDED
+#ifdef JUCE
+#else
 #include <vector>
 #include <iostream>
 #include <UdpServer.h>
 #include <BeagleRT.h>
 #include <native/task.h>
+#endif /*JUCE*/
 
+#ifdef JUCE
+class ReceiveAudioThread : public Thread {
+#else
 class ReceiveAudioThread{
+#endif /* JUCE */
 private:
     //  FILE *fd; //DEBUG
     //  FILE *fd2; //DEBUG
+#ifdef JUCE
+	DatagramSocket socket;
+#else
     UdpServer socket;
-//JUCE    DatagramSocket socket;
+#endif /* JUCE */
     bool listening;
     bool bufferReady;
 #ifdef JUCE
-    bool threadRunning;
+    bool threadRunning; //do we really need this ?
 #else
     static bool threadRunning;
     static bool threadIsExiting;
@@ -51,10 +61,13 @@
     static std::vector<ReceiveAudioThread *> objAddrs;
 #endif
 public:
+#ifdef JUCE
+    ReceiveAudioThread(const String &threadName);
+#else
     ReceiveAudioThread();
+#endif
     ~ReceiveAudioThread();
     void init(int port, int aSamplesPerBlock, int channel);
-    void setup();
     void bindToPort(int aPort);
     bool isListening();
     float* getCurrentBuffer(int length);