giuliomoro@117: #include "ReceiveAudioThread.h" giuliomoro@119: giuliomoro@119: #ifdef JUCE giuliomoro@119: #else giuliomoro@117: //initialise static members giuliomoro@119: bool ReceiveAudioThread::staticConstructed=false; giuliomoro@117: AuxiliaryTask ReceiveAudioThread::receiveDataTask=NULL; giuliomoro@119: std::vector ReceiveAudioThread::objAddrs(0); giuliomoro@119: bool ReceiveAudioThread::threadRunning; giuliomoro@119: bool ReceiveAudioThread::threadIsExiting; giuliomoro@119: int ReceiveAudioThread::sleepTime; giuliomoro@119: giuliomoro@119: void receiveData(){ giuliomoro@119: ReceiveAudioThread::run(); giuliomoro@119: } giuliomoro@119: void ReceiveAudioThread::staticConstructor(){ giuliomoro@119: if(staticConstructed==true) giuliomoro@119: return; giuliomoro@119: staticConstructed=true; giuliomoro@119: threadIsExiting=false; giuliomoro@119: receiveDataTask=BeagleRT_createAuxiliaryTask(receiveData, 90, "receiveDataTask"); //TODO: allow different priorities giuliomoro@119: } giuliomoro@119: #endif giuliomoro@117: giuliomoro@117: void ReceiveAudioThread::dealloc(){ giuliomoro@117: free(buffer); giuliomoro@117: buffer=NULL; giuliomoro@117: free(stackBuffer); giuliomoro@117: stackBuffer=NULL; giuliomoro@117: } giuliomoro@117: void ReceiveAudioThread::wrapWritePointer(){ giuliomoro@117: //this is not quite a simple wrapping as you wold in a circular buffer, giuliomoro@117: //as there is no guarantee the buffer will be full at all times, given that there must alwas be enough space at the end of it giuliomoro@117: //to hold a full payload giuliomoro@117: // lastValidPointer indicates the last pointer in the buffer containing valid data giuliomoro@117: // giuliomoro@117: if(writePointer+payloadLength+headerLength>bufferLength){ //if we are going to exceed the length of the buffer with the next reading giuliomoro@117: // lastValidPointer=writePointer+headerLength; //remember where the last valid data are giuliomoro@117: // for(int n=headerLength;nlastValidPointer){ giuliomoro@117: // lastValidPointer=writePointer+headerLength; giuliomoro@117: } giuliomoro@117: wrapWritePointer(); giuliomoro@117: return numBytes; giuliomoro@117: } giuliomoro@117: return 0; //timeout occurred giuliomoro@117: } giuliomoro@117: ReceiveAudioThread::ReceiveAudioThread() : giuliomoro@117: //JUCE Thread(threadName), giuliomoro@117: socket(0), giuliomoro@117: listening(false), giuliomoro@117: bufferReady(false), giuliomoro@117: buffer(NULL), giuliomoro@117: stackBuffer(NULL), giuliomoro@117: bufferLength(0), giuliomoro@117: lastValidPointer(0), giuliomoro@117: waitForSocketTime(100), giuliomoro@117: threadPriority(95) giuliomoro@117: {}; giuliomoro@117: ReceiveAudioThread::~ReceiveAudioThread(){ giuliomoro@117: //JUCE stopThread(1000); giuliomoro@117: while(threadRunning){ giuliomoro@119: usleep(sleepTime*2); //wait for thread to stop giuliomoro@117: std::cout<< "Waiting for receiveAudioTask to stop" << std::endl; giuliomoro@117: } giuliomoro@117: //TODO: check if thread stopped, otherwise kill it before dealloc giuliomoro@117: dealloc(); giuliomoro@117: } giuliomoro@119: void ReceiveAudioThread::init(int aPort, int aSamplesPerBlock, int aChannel){ giuliomoro@117: dealloc(); giuliomoro@119: #ifdef JUCE giuliomoro@119: #else giuliomoro@119: staticConstructor(); giuliomoro@119: objAddrs.push_back(this);//TODO: this line should be in the constructor giuliomoro@119: #endif giuliomoro@119: bindToPort(aPort); giuliomoro@119: channel=aChannel; giuliomoro@120: printf("Channel %d is receiving on port %d\n",aChannel, aPort); giuliomoro@117: // fd=fopen("output.m","w"); //DEBUG giuliomoro@117: // fprintf(fd,"var=["); //DEBUG giuliomoro@117: headerLength=2; giuliomoro@117: payloadLength=300; //TODO: make sure that payloadLength and headerLength are the same as the client is sending. giuliomoro@117: bufferLength=std::max(headerLength+(payloadLength*4), headerLength+(aSamplesPerBlock*4)); //there are many considerations that can be done here ... giuliomoro@117: //We keep a headerLength padding at the beginning of the array to allow full reads from the socket giuliomoro@117: buffer=(float*)malloc(sizeof(float)*bufferLength); giuliomoro@117: if(buffer==NULL) // something wrong giuliomoro@117: return; giuliomoro@117: bufferReady=true; giuliomoro@117: lastValidPointer=headerLength+ ((bufferLength-headerLength)/payloadLength)*payloadLength; giuliomoro@117: memset(buffer,0,bufferLength*sizeof(float)); giuliomoro@117: stackBuffer=(float*)malloc(sizeof(float)*headerLength); giuliomoro@117: bytesToRead=sizeof(float)*(payloadLength + headerLength); giuliomoro@117: writePointer=-1; giuliomoro@120: readPointer=0; giuliomoro@117: sleepTime=payloadLength/(float)44100 /4.0; //set sleepTime so that you do not check too often or too infrequently giuliomoro@117: //JUCE startThread(threadPriority); giuliomoro@117: } giuliomoro@117: giuliomoro@117: void ReceiveAudioThread::bindToPort(int aPort){ giuliomoro@117: listening=socket.bindToPort(aPort); giuliomoro@119: #ifdef JUCE giuliomoro@119: #else giuliomoro@119: if(listening==false) giuliomoro@119: printf("Could not bind to port %d\n",aPort); giuliomoro@119: #endif giuliomoro@117: } giuliomoro@117: bool ReceiveAudioThread::isListening(){ giuliomoro@117: return listening; giuliomoro@117: } giuliomoro@117: float* ReceiveAudioThread::getCurrentBuffer(int length){ // NOTE: this cannot work all the time unless samplesPerBuffer and payloadLength are multiples giuliomoro@117: //TODO: make it return the number of samples actually available at the specified location giuliomoro@117: if(isListening()==false || length>bufferLength) giuliomoro@117: return NULL; giuliomoro@117: readPointer+=length; giuliomoro@117: if(readPointer>lastValidPointer){ giuliomoro@117: readPointer=headerLength; giuliomoro@117: } giuliomoro@117: return buffer+(int)readPointer; giuliomoro@117: }; giuliomoro@120: int ReceiveAudioThread::getSamplesSrc(float *destination, int length, giuliomoro@120: float samplingRateRatio, int numChannelsInDestination, giuliomoro@120: int channelToWriteTo) giuliomoro@120: { giuliomoro@117: if (!(samplingRateRatio>0 && samplingRateRatio<=2)) giuliomoro@117: return -2; giuliomoro@117: if(isListening()==false) giuliomoro@117: return -1; giuliomoro@117: if(writePointer<0){ //if writePointer has not been initalized yet ... giuliomoro@117: writePointer=2*length; // do it, so that it starts writing at a safety margin from where we write. giuliomoro@117: // This will help keeping them in sync. giuliomoro@117: //TODO: handle what happens when the remote stream is interrupted and then restarted giuliomoro@117: } giuliomoro@117: if(length>lastValidPointer) { giuliomoro@117: //not enough samples available, we fill the buffer with what is available, but the destination buffer will not be filled completely giuliomoro@117: //at this very moment the other thread might be writing at most one payload into the buffer. giuliomoro@117: //To avoid a race condition, we need to let alone the buffer where we are currently writing giuliomoro@117: //as writing the payload also temporarily overwrites the previous headerLength samples, we need to account for them as well giuliomoro@117: //TODO: This assumes that the writePointer and readPointer do not drift. When doing clock synchronization we will find out that it is not true! giuliomoro@117: length=lastValidPointer-payloadLength-headerLength; giuliomoro@117: if(length<0) //no samples available at all! giuliomoro@117: return 0; giuliomoro@117: } giuliomoro@117: for(int n=0; n=lastValidPointer){ giuliomoro@117: readPointer=readPointer-lastValidPointer+headerLength; giuliomoro@117: } giuliomoro@117: } giuliomoro@117: return readPointer; giuliomoro@117: } giuliomoro@120: int ReceiveAudioThread::getSamplesSrc(float *destination, int length, float samplingRateRatio){ giuliomoro@120: return getSamplesSrc(destination, length, samplingRateRatio, 1,0); giuliomoro@120: // TODO: rewriting this so that it does not call the override method we can save a multiply and add giuliomoro@120: // for each sample. giuliomoro@120: } giuliomoro@117: bool ReceiveAudioThread::isBufferReady(){ giuliomoro@117: return bufferReady; giuliomoro@117: } giuliomoro@117: void ReceiveAudioThread::startThread(){ giuliomoro@117: BeagleRT_scheduleAuxiliaryTask(receiveDataTask); giuliomoro@117: } giuliomoro@117: void ReceiveAudioThread::stopThread(){ giuliomoro@117: threadIsExiting=true; giuliomoro@117: } giuliomoro@117: bool ReceiveAudioThread::threadShouldExit(){ giuliomoro@117: return(gShouldStop || threadIsExiting ); giuliomoro@117: } giuliomoro@117: void ReceiveAudioThread::run(){ giuliomoro@117: // fd2=fopen("buffer.m","w"); //DEBUG giuliomoro@117: // fprintf(fd2, "buf=["); //DEBUG giuliomoro@117: threadRunning=true; giuliomoro@117: while(!threadShouldExit()){ //TODO: check that the socket buffer is empty before starting giuliomoro@119: #ifdef JUCE giuliomoro@117: readUdpToBuffer(); // read into the oldBuffer giuliomoro@117: usleep(sleepTime); giuliomoro@119: #else giuliomoro@119: for(unsigned int n=0; nreadUdpToBuffer(); giuliomoro@119: } giuliomoro@119: usleep(sleepTime); //TODO: use rt_task_sleep instead giuliomoro@119: #endif giuliomoro@117: } giuliomoro@117: threadRunning=false; giuliomoro@117: // fprintf(fd,"];readPointer,writePointer,lastValidPointer,destination]=deal(var(:,1), var(:,2), var(:,3), var(:,4));"); //DEBUG giuliomoro@117: // fclose(fd);//DEBUG giuliomoro@117: // fprintf(fd2,"];");//DEBUG giuliomoro@117: // fclose(fd2); //DEBUG giuliomoro@117: }