# HG changeset patch # User Giulio Moro # Date 1440550930 -3600 # Node ID ff28e56e5b7e58c552e10494382459973e2ebd33 # Parent da1c61aa97eac62035ebcb650883ce42c65d68a5 Updated Network files to match Udpioplugin 18:fb5a61b10223 diff -r da1c61aa97ea -r ff28e56e5b7e core/NetworkSend.cpp --- a/core/NetworkSend.cpp Tue Aug 25 11:14:25 2015 +0100 +++ b/core/NetworkSend.cpp Wed Aug 26 02:02:10 2015 +0100 @@ -1,19 +1,24 @@ -#include +#include "NetworkSend.h" #ifdef USE_JUCE #else //initialize the static members of NetworkSend bool NetworkSend::staticConstructed=false; +int NetworkSend::sleepTimeMs; +bool NetworkSend::threadIsExiting; +bool NetworkSend::threadRunning; std::vector NetworkSend::objAddrs(0); AuxiliaryTask NetworkSend::sendDataTask=NULL; void sendData(){ - NetworkSend::sendAllData(); + NetworkSend::run(); } void NetworkSend::staticConstructor(){ if(staticConstructed==true) return; staticConstructed=true; + threadIsExiting=false; + threadRunning=false; sendDataTask = BeagleRT_createAuxiliaryTask(::sendData, 95, "sendDataTask"); //TODO: allow variable priority } void NetworkSend::sendAllData(){ @@ -24,31 +29,71 @@ int NetworkSend::getNumInstances(){ return objAddrs.size(); } +void NetworkSend::startThread(){ + BeagleRT_scheduleAuxiliaryTask(sendDataTask); +} +void NetworkSend::stopThread(){ + threadIsExiting=true; +} +bool NetworkSend::threadShouldExit(){ + return(gShouldStop || threadIsExiting); +} +bool NetworkSend::isThreadRunning(){ + return threadRunning; +} #endif /* USE_JUCE */ +#ifdef USE_JUCE +NetworkSend::NetworkSend(const String &threadName): + Thread(threadName) +#else NetworkSend::NetworkSend() +#endif /* USE_JUCE */ { - sampleCount = 0; - channel.doneOnTime=true; - channel.index=channel.headerLength; //leave space for the heading message (channel, timestamp) - channel.activeBuffer=0; - channel.readyToBeSent=false; + channel.buffers=NULL; + channel.doneOnTime=NULL; + channel.readyToBeSent=NULL; + channel.enabled=false; + sleepTimeMs=2; //This should actually be initialized in the staticConstructor for non-Juce code, but doing it here makes it more portable + channel.sampleCount=0; } NetworkSend::~NetworkSend(){ +#ifdef USE_JUCE + stopThread(1000); +#else + stopThread(); for(unsigned int n=0; n4*channel.bufferLength ? blockSize*4 : 4*channel.bufferLength; + channel.numBuffers= 1+numSamples/channel.bufferLength; //the +1 takes the ceil() of the division + channel.buffers=(float**)malloc(channel.numBuffers*sizeof(float*)); + printf("NumBuffers: %d\n", channel.numBuffers); + if(channel.buffers==NULL) + return; + for(int n=0; n( t3 - t1 ).count(); + // auto duration2 = std::chrono::duration_cast( t2 - t1 ).count(); + // if(duration2>0) + // std::cout << "Duration is: " << duration2 <<". Whole loop is: " << duration1 << "\n"; + sleep(1); + } +#else + threadRunning=true; + while(threadShouldExit()==false){ + sendAllData(); + usleep(sleepTimeMs*1000); + } + threadRunning=false; +#endif +} #ifdef USE_JUCE #else Scope::Scope(int aNumChannels): @@ -132,7 +266,7 @@ void Scope::setup(float sampleRate, int aPort, const char* aServer){ for(int n=0; nlastValidPointer){ - // lastValidPointer=writePointer+headerLength; - } wrapWritePointer(); return numBytes; } @@ -125,6 +123,7 @@ #ifdef USE_JUCE stopThread(1000); #else + stopThread(); while(threadRunning){ usleep(sleepTime*2); //wait for thread to stop std::cout<< "Waiting for receiveAudioTask to stop" << std::endl; @@ -247,6 +246,31 @@ // fd2=fopen("buffer.m","w"); //DEBUG // fprintf(fd2, "buf=["); //DEBUG threadRunning=true; + int maxCount=10; + int count=0; + // Clean the socket from anything that is currently in it. +#ifdef USE_JUCE + // this is borrowed from BeagleRT's UdpServer class. + int n; + do { + float waste; + if(socket.waitUntilReady(true, 0)==0) + break; + n=socket.read((void*)&waste, sizeof(float), false); + count++; + if(n<0){ + printf("error\n"); + break; + } + printf("n: %d\n",n); + } while (n>0 && (maxCount<=0 || countsocket.empty(maxCount); + } +#endif /* USE_JUCE */ + printf("socket emptied with %d reads\n", count); + while(!threadShouldExit()){ //TODO: check that the socket buffer is empty before starting #ifdef USE_JUCE readUdpToBuffer(); // read into the oldBuffer diff -r da1c61aa97ea -r ff28e56e5b7e core/UdpServer.cpp --- a/core/UdpServer.cpp Tue Aug 25 11:14:25 2015 +0100 +++ b/core/UdpServer.cpp Wed Aug 26 02:02:10 2015 +0100 @@ -82,7 +82,7 @@ return -1; FD_ZERO(&stReadFDS); FD_SET(inSocket, &stReadFDS); - int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stZeroTimeOut); + int descriptorReady= select(inSocket+1, &stReadFDS, NULL, NULL, &stZeroTimeOut); //TODO: this is not JUCE-compliant if(descriptorReady<0){ //an error occurred return -1; } @@ -100,24 +100,19 @@ // while (blockUntilSpecifiedAmountHasArrived && numberOfBytes==maxBytesToRead); return numberOfBytes; } -int UdpServer::emptySocket(){ - return emptySocket(0); +int UdpServer::empty(){ + return empty(0); } -int UdpServer::emptySocket(int maxBytes){//discards up to maxBytes from the socket. Returns the number of bytes discarded. - if(wasteBuffer==NULL) - return -1; - int numberOfBytes=0; - while(int n=read(wasteBuffer, wasteBufferSize, false)){// calls the read function until it does not return any more bytes (i.e.: socket is empty) - if(n>0) - numberOfBytes+=n; - if(n<0) - return -1; - if(maxBytes>0 && numberOfBytes>=maxBytes) - break; - }; - return numberOfBytes; +int UdpServer::empty(int maxCount){ + int count=0; + int n; + do { + if(waitUntilReady(true, 0)==0) + return 0; + float waste; + n=read(&waste, sizeof(float), false); + count++; + } while (n>0 && (maxCount<=0 || maxCount #else #include #include #include #include #include +#include +extern bool gShouldStop; #endif /* USE_JUCE */ #define NETWORK_AUDIO_BUFFER_SIZE 302 +#define UDP_BUFFER_HEADER_CHANNEL_INDEX 0 +#define UDP_BUFFER_HEADER_TIMESTAMP_INDEX 1 +#define UDP_BUFFER_HEADER_LENGTH 2 struct NetworkBuffer{ int channelNumber; - int activeBuffer; - int index; - float buffers[2][NETWORK_AUDIO_BUFFER_SIZE]; - bool doneOnTime; - bool readyToBeSent; - static const int headerLength=2; + int numBuffers; + int writeBuffer; + int readBuffer; + int writePointer; + float** buffers; + bool* doneOnTime; + bool* readyToBeSent; + bool enabled; + int sampleCount; + static const int bufferLength=NETWORK_AUDIO_BUFFER_SIZE; + static const int headerLength=UDP_BUFFER_HEADER_LENGTH; + static const int headerChannelIndex=UDP_BUFFER_HEADER_CHANNEL_INDEX; + static const int headerTimestampIndex=UDP_BUFFER_HEADER_TIMESTAMP_INDEX; }; #ifdef USE_JUCE @@ -28,23 +41,35 @@ #else class NetworkSend { #endif /* USE_JUCE */ - int sampleCount; float sampleRate; #ifdef USE_JUCE DatagramSocket udpClient; + int sleepTimeMs; + String remoteHostname; + int remotePortNumber; #else UdpClient udpClient; + bool isThreadRunning(); + static int sleepTimeMs; + static bool threadShouldExit(); + static bool threadIsExiting; + static bool threadRunning; static bool staticConstructed; static void staticConstructor(); static AuxiliaryTask sendDataTask; //TODO: allow different AuxiliaryTasks for different priorities (e.g.: audio vs scope) static std::vector objAddrs; #endif /* USE_JUCE */ - public: + void dealloc(); +public: NetworkBuffer channel; +#ifdef USE_JUCE + NetworkSend(const String &threadName); +#else NetworkSend(); +#endif ~NetworkSend(); - void setup(float aSampleRate); - void setup(float aSampleRate, int aChannelNumber, int aPort, const char *aServer); + void setup(float aSampleRate, int blockSize, int aChannelNumber, int aPort, const char *aServer); + void cleanup(); void sendData(); void log(float value); void setPort(int aPort); @@ -56,6 +81,8 @@ #else static int getNumInstances(); static void sendAllData(); + static void startThread(); + static void stopThread(); static void run(); #endif /* USE_JUCE */ }; diff -r da1c61aa97ea -r ff28e56e5b7e include/ReceiveAudioThread.h --- a/include/ReceiveAudioThread.h Tue Aug 25 11:14:25 2015 +0100 +++ b/include/ReceiveAudioThread.h Wed Aug 26 02:02:10 2015 +0100 @@ -9,6 +9,7 @@ #include #include #include +#include #endif /*USE_JUCE*/ #ifdef USE_JUCE diff -r da1c61aa97ea -r ff28e56e5b7e include/UdpServer.h --- a/include/UdpServer.h Tue Aug 25 11:14:25 2015 +0100 +++ b/include/UdpServer.h Wed Aug 26 02:02:10 2015 +0100 @@ -52,9 +52,8 @@ */ int read(void* destBuffer, int maxBytesToRead, bool blockUntilSpecifiedAmountHasArrived); void close(); - int emptySocket(); - int emptySocket(int maxBytes); - void *getWaste(); + int empty(); + int empty(int maxCount); /* * Waits until the socket is ready for reading or writing. * diff -r da1c61aa97ea -r ff28e56e5b7e projects/scope/render.cpp --- a/projects/scope/render.cpp Tue Aug 25 11:14:25 2015 +0100 +++ b/projects/scope/render.cpp Wed Aug 26 02:02:10 2015 +0100 @@ -18,17 +18,17 @@ // in from the call to initAudio(). // // Return true on success; returning false halts the program. -//ReceiveAudioThread receiveAudio0; +ReceiveAudioThread receiveAudio0; //ReceiveAudioThread receiveAudio1; bool setup(BeagleRTContext *context, void *userData) { -// receiveAudio0.init(9999, context->audioFrames, 0); + receiveAudio0.init(10000, 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"); + networkSend.setup(context->audioSampleRate, context->audioFrames, 0, 9999, "192.168.7.1"); gInverseSampleRate = 1.0/context->audioSampleRate; @@ -49,23 +49,24 @@ 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); - float chn1 = sinf(gPhase2); +// float chn1 = sinf(gPhase2); // float chn2 = context->audioIn[n*2 + 0]; // float chn3 = context->audioIn[n*2 + 1]; // float chn4 = context->analogIn[(int)n/2*8 + 0]; // float chn5 = context->analogIn[(int)n/2*8 + 1]; - networkSend.log(chn0); -// scope.log(0, chn0); -// scope.log(1, chn1); + networkSend.log(context->audioIn[n]); +// networkSend.log(chn0); +// scope.log(0, chn0); +// scope.log(1, chn1); // scope.log(2, chn2); // scope.log(3, chn3); // scope.log(4, chn4); @@ -87,9 +88,19 @@ } if(count>0){ -// int readPointer0=receiveAudio0.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 0); + float samplingRateRatio=1; + int channelsInDestinationBuffer=2; + int channelToWriteTo=0; + int length=receiveAudio0.getSamplesSrc(context->audioOut, context->audioFrames, + samplingRateRatio, channelsInDestinationBuffer, channelToWriteTo); + if(length!=context->audioFrames){ + rt_printf("Length mismatch: %d\n", length); + } // int readPointer1=receiveAudio1.getSamplesSrc(context->audioOut, context->audioFrames, 1, 2, 1); } + for(int n=0; naudioFrames; n++){ + context->audioOut[n*2+1]=context->audioOut[n*2]; + } count++; }