Mercurial > hg > beaglert
changeset 128:2696a7f00053 scope-refactoring
ReceiveAudioThread is now the same with/without JUCE
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Mon, 24 Aug 2015 18:55:19 +0100 |
parents | 6c8fb6f07b47 |
children | cce58e6ec2a2 |
files | core/ReceiveAudioThread.cpp include/ReceiveAudioThread.h projects/scope/render.cpp |
diffstat | 3 files changed, 54 insertions(+), 53 deletions(-) [+] |
line wrap: on
line diff
--- a/core/ReceiveAudioThread.cpp Mon Aug 24 15:36:09 2015 +0100 +++ b/core/ReceiveAudioThread.cpp Mon Aug 24 18:55:19 2015 +0100 @@ -1,6 +1,6 @@ #include "ReceiveAudioThread.h" -#ifdef JUCE +#ifdef USE_JUCE #else //initialise static members bool ReceiveAudioThread::staticConstructed=false; @@ -20,7 +20,7 @@ threadIsExiting=false; receiveDataTask=BeagleRT_createAuxiliaryTask(receiveData, 90, "receiveDataTask"); //TODO: allow different priorities } -#endif /* JUCE */ +#endif /* USE_JUCE */ void ReceiveAudioThread::dealloc(){ free(buffer); @@ -100,13 +100,13 @@ } return 0; //timeout occurred } -//JUCE Thread(threadName), -#ifdef JUCE +//USE_JUCE Thread(threadName), +#ifdef USE_JUCE ReceiveAudioThread::ReceiveAudioThread(const String &threadName) : Thread(threadName), #else ReceiveAudioThread::ReceiveAudioThread() : -#endif /* JUCE */ +#endif /* USE_JUCE */ socket(0), listening(false), bufferReady(false), @@ -115,31 +115,31 @@ bufferLength(0), lastValidPointer(0), waitForSocketTime(5), -#ifdef JUCE +#ifdef USE_JUCE threadPriority(5) #else threadPriority(88) -#endif /* JUCE */ +#endif /* USE_JUCE */ {}; ReceiveAudioThread::~ReceiveAudioThread(){ -#ifdef JUCE +#ifdef USE_JUCE stopThread(1000); #else while(threadRunning){ usleep(sleepTime*2); //wait for thread to stop std::cout<< "Waiting for receiveAudioTask to stop" << std::endl; } -#endif /* JUCE */ +#endif /* USE_JUCE */ //TODO: check if thread stopped, otherwise kill it before dealloc dealloc(); } void ReceiveAudioThread::init(int aPort, int aSamplesPerBlock, int aChannel){ dealloc(); -#ifdef JUCE +#ifdef USE_JUCE #else staticConstructor(); objAddrs.push_back(this);//TODO: this line should be in the constructor -#endif /* JUCE */ +#endif /* USE_JUCE */ bindToPort(aPort); channel=aChannel; printf("Channel %d is receiving on port %d\n",aChannel, aPort); @@ -162,20 +162,20 @@ writePointer=-1; readPointer=0; sleepTime=payloadLength/(float)44100 /4.0; //set sleepTime so that you do not check too often or too infrequently -#ifdef JUCE +#ifdef USE_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 */ +#endif /* USE_JUCE */ } void ReceiveAudioThread::bindToPort(int aPort){ listening=socket.bindToPort(aPort); -#ifdef JUCE +#ifdef USE_JUCE #else - if(listening==false) //this condition is valid also for JUCE, but we do not printf in JUCE + if(listening==false) //this condition is valid also for USE_JUCE, but we do not printf in USE_JUCE printf("Could not bind to port %d\n",aPort); -#endif /* JUCE */ +#endif /* USE_JUCE */ } bool ReceiveAudioThread::isListening(){ return listening; @@ -231,12 +231,10 @@ bool ReceiveAudioThread::isBufferReady(){ return bufferReady; } -#ifdef JUCE +#ifdef USE_JUCE #else void ReceiveAudioThread::startThread(){ - printf("receivedata is going to be scheduled\n"); BeagleRT_scheduleAuxiliaryTask(receiveDataTask); - printf("receivedata has been scheduled\n"); } void ReceiveAudioThread::stopThread(){ threadIsExiting=true; @@ -244,22 +242,22 @@ bool ReceiveAudioThread::threadShouldExit(){ return(gShouldStop || threadIsExiting ); } -#endif /* JUCE */ +#endif /* USE_JUCE */ void ReceiveAudioThread::run(){ // fd2=fopen("buffer.m","w"); //DEBUG // fprintf(fd2, "buf=["); //DEBUG threadRunning=true; while(!threadShouldExit()){ //TODO: check that the socket buffer is empty before starting -#ifdef JUCE +#ifdef USE_JUCE readUdpToBuffer(); // read into the oldBuffer - usleep(sleepTime); + sleep(sleepTime); #else for(unsigned int n=0; n<ReceiveAudioThread::objAddrs.size(); n++){ // printf("%d\n", n); ReceiveAudioThread::objAddrs[n]->readUdpToBuffer(); } usleep(sleepTime); //TODO: use rt_task_sleep instead -#endif /* JUCE */ +#endif /* USE_JUCE */ } threadRunning=false; printf("Thread is not running \n");
--- a/include/ReceiveAudioThread.h Mon Aug 24 15:36:09 2015 +0100 +++ b/include/ReceiveAudioThread.h Mon Aug 24 18:55:19 2015 +0100 @@ -1,30 +1,32 @@ #ifndef RECEIVEAUDIOTHREAD_H_INCLUDED #define RECEIVEAUDIOTHREAD_H_INCLUDED -#ifdef JUCE + +#ifdef USE_JUCE +#include <JuceHeader.h> #else #include <vector> #include <iostream> #include <UdpServer.h> #include <BeagleRT.h> #include <native/task.h> -#endif /*JUCE*/ +#endif /*USE_JUCE*/ -#ifdef JUCE +#ifdef USE_JUCE class ReceiveAudioThread : public Thread { #else class ReceiveAudioThread{ -#endif /* JUCE */ +#endif /* USE_JUCE */ private: // FILE *fd; //DEBUG // FILE *fd2; //DEBUG -#ifdef JUCE +#ifdef USE_JUCE DatagramSocket socket; #else UdpServer socket; -#endif /* JUCE */ +#endif /* USE_JUCE */ bool listening; bool bufferReady; -#ifdef JUCE +#ifdef USE_JUCE bool threadRunning; //do we really need this ? #else static bool threadRunning; @@ -36,7 +38,7 @@ float readPointer; int writePointer; int lastValidPointer; -#ifdef JUCE +#ifdef USE_JUCE int sleepTime; #else static int sleepTime; @@ -52,7 +54,7 @@ void pushPayload(int startIndex); void popPayload(int startIndex); int readUdpToBuffer(); -#ifdef JUCE +#ifdef USE_JUCE #else static bool threadShouldExit(); static bool staticConstructed; @@ -61,7 +63,7 @@ static std::vector<ReceiveAudioThread *> objAddrs; #endif public: -#ifdef JUCE +#ifdef USE_JUCE ReceiveAudioThread(const String &threadName); #else ReceiveAudioThread(); @@ -82,7 +84,7 @@ float samplingRateRatio, int numChannelsInDestination, int channelToWriteTo); bool isBufferReady(); -#ifdef JUCE // if we are in Juce, then we run a separate thread for each receiver +#ifdef USE_JUCE // if we are in Juce, then we run a separate thread for each receiver // (as each of them are typically receiving on a mono or stereo track) void run(); #else @@ -91,6 +93,6 @@ static void startThread(); static void stopThread(); static int getNumInstances(); -#endif // JUCE +#endif // USE_JUCE }; #endif // RECEIVEAUDIOTHREAD_H_INCLUDED
--- a/projects/scope/render.cpp Mon Aug 24 15:36:09 2015 +0100 +++ b/projects/scope/render.cpp Mon Aug 24 18:55:19 2015 +0100 @@ -7,7 +7,7 @@ float gFrequency1, gFrequency2; float gInverseSampleRate; -Scope scope(2); //create a scope object with 2 channels +//Scope scope(2); //create a scope object with 2 channels NetworkSend networkSend; // initialise_render() is called once before the audio rendering starts. @@ -18,17 +18,17 @@ // in from the call to initAudio(). // // Return true on success; returning false halts the program. -ReceiveAudioThread receiveAudio0; -ReceiveAudioThread receiveAudio1; +//ReceiveAudioThread receiveAudio0; +//ReceiveAudioThread receiveAudio1; bool setup(BeagleRTContext *context, void *userData) { - receiveAudio0.init(9999, context->audioFrames, 0); - receiveAudio1.init(10000, context->audioFrames, 1); - - scope.setup(); //call this once in setup to initialise the scope - scope.setPort(0, 9999); - scope.setPort(1, 10000); -// networkSend.setup(context->audioSampleRate, 0, 9999, "192.168.7.1"); +// receiveAudio0.init(9999, context->audioFrames, 0); +// receiveAudio1.init(10000, context->audioFrames, 1); +// +// scope.setup(); //call this once in setup to initialise the scope +// scope.setPort(0, 9999); +// scope.setPort(1, 10000); + networkSend.setup(context->audioSampleRate, 0, 9999, "192.168.7.1"); gInverseSampleRate = 1.0/context->audioSampleRate; @@ -49,10 +49,10 @@ void render(BeagleRTContext *context, void *userData) { static int count=0; - if(count==0){ - printf("startHread\n"); - ReceiveAudioThread::startThread(); - } +// if(count==0){ +// printf("startHread\n"); +// ReceiveAudioThread::startThread(); +// } for(unsigned int n = 0; n < context->audioFrames; n++) { float chn0 = sinf(gPhase1); @@ -63,8 +63,9 @@ // float chn4 = context->analogIn[(int)n/2*8 + 0]; // float chn5 = context->analogIn[(int)n/2*8 + 1]; - scope.log(0, chn0); - scope.log(1, chn1); + networkSend.log(chn0); +// scope.log(0, chn0); +// scope.log(1, chn1); // scope.log(2, chn2); // scope.log(3, chn3); // scope.log(4, chn4); @@ -86,8 +87,8 @@ } if(count>0){ - int readPointer0=receiveAudio0.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 0); - int readPointer1=receiveAudio1.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 1); +// int readPointer0=receiveAudio0.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 0); +// int readPointer1=receiveAudio1.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 1); } count++; }