comparison core/ReceiveAudioThread.cpp @ 120:cdd441a304a9 scope-refactoring

Added read to interleaved buffer
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 21 Aug 2015 15:52:37 +0100
parents c692827083e1
children bc514f29c3aa
comparison
equal deleted inserted replaced
119:c692827083e1 120:cdd441a304a9
130 staticConstructor(); 130 staticConstructor();
131 objAddrs.push_back(this);//TODO: this line should be in the constructor 131 objAddrs.push_back(this);//TODO: this line should be in the constructor
132 #endif 132 #endif
133 bindToPort(aPort); 133 bindToPort(aPort);
134 channel=aChannel; 134 channel=aChannel;
135 printf("Channel %d is receiving on port %d\n",aChannel, aPort);
135 // fd=fopen("output.m","w"); //DEBUG 136 // fd=fopen("output.m","w"); //DEBUG
136 // fprintf(fd,"var=["); //DEBUG 137 // fprintf(fd,"var=["); //DEBUG
137 headerLength=2; 138 headerLength=2;
138 payloadLength=300; //TODO: make sure that payloadLength and headerLength are the same as the client is sending. 139 payloadLength=300; //TODO: make sure that payloadLength and headerLength are the same as the client is sending.
139 bufferLength=std::max(headerLength+(payloadLength*4), headerLength+(aSamplesPerBlock*4)); //there are many considerations that can be done here ... 140 bufferLength=std::max(headerLength+(payloadLength*4), headerLength+(aSamplesPerBlock*4)); //there are many considerations that can be done here ...
145 lastValidPointer=headerLength+ ((bufferLength-headerLength)/payloadLength)*payloadLength; 146 lastValidPointer=headerLength+ ((bufferLength-headerLength)/payloadLength)*payloadLength;
146 memset(buffer,0,bufferLength*sizeof(float)); 147 memset(buffer,0,bufferLength*sizeof(float));
147 stackBuffer=(float*)malloc(sizeof(float)*headerLength); 148 stackBuffer=(float*)malloc(sizeof(float)*headerLength);
148 bytesToRead=sizeof(float)*(payloadLength + headerLength); 149 bytesToRead=sizeof(float)*(payloadLength + headerLength);
149 writePointer=-1; 150 writePointer=-1;
150 readPointer=0; //TODO: this *4 is sortof a security margin 151 readPointer=0;
151 sleepTime=payloadLength/(float)44100 /4.0; //set sleepTime so that you do not check too often or too infrequently 152 sleepTime=payloadLength/(float)44100 /4.0; //set sleepTime so that you do not check too often or too infrequently
152 //JUCE startThread(threadPriority); 153 //JUCE startThread(threadPriority);
153 } 154 }
154 155
155 void ReceiveAudioThread::bindToPort(int aPort){ 156 void ReceiveAudioThread::bindToPort(int aPort){
171 if(readPointer>lastValidPointer){ 172 if(readPointer>lastValidPointer){
172 readPointer=headerLength; 173 readPointer=headerLength;
173 } 174 }
174 return buffer+(int)readPointer; 175 return buffer+(int)readPointer;
175 }; 176 };
176 int ReceiveAudioThread::getSamplesSrc(float *destination, int length, float samplingRateRatio){//TODO: add interleaved version 177 int ReceiveAudioThread::getSamplesSrc(float *destination, int length,
178 float samplingRateRatio, int numChannelsInDestination,
179 int channelToWriteTo)
180 {
177 if (!(samplingRateRatio>0 && samplingRateRatio<=2)) 181 if (!(samplingRateRatio>0 && samplingRateRatio<=2))
178 return -2; 182 return -2;
179 if(isListening()==false) 183 if(isListening()==false)
180 return -1; 184 return -1;
181 if(writePointer<0){ //if writePointer has not been initalized yet ... 185 if(writePointer<0){ //if writePointer has not been initalized yet ...
192 length=lastValidPointer-payloadLength-headerLength; 196 length=lastValidPointer-payloadLength-headerLength;
193 if(length<0) //no samples available at all! 197 if(length<0) //no samples available at all!
194 return 0; 198 return 0;
195 } 199 }
196 for(int n=0; n<length; n++){ 200 for(int n=0; n<length; n++){
197 destination[n]=buffer[(int)(0.5+readPointer)];//simple ZOH non-interpolation (nearest neighbour) 201 destination[n*numChannelsInDestination+channelToWriteTo]=buffer[(int)(0.5+readPointer)];//simple ZOH non-interpolation (nearest neighbour)
198 // fprintf(fd,"%f, %d, %f;\n",readPointer,writePointer,destination[n]); //DEBUG 202 // fprintf(fd,"%f, %d, %f;\n",readPointer,writePointer,destination[n]); //DEBUG
199 readPointer+=samplingRateRatio; 203 readPointer+=samplingRateRatio;
200 if((int)(0.5+readPointer)>=lastValidPointer){ 204 if((int)(0.5+readPointer)>=lastValidPointer){
201 readPointer=readPointer-lastValidPointer+headerLength; 205 readPointer=readPointer-lastValidPointer+headerLength;
202 } 206 }
203 } 207 }
204 return readPointer; 208 return readPointer;
205 } 209 }
206 210 int ReceiveAudioThread::getSamplesSrc(float *destination, int length, float samplingRateRatio){
211 return getSamplesSrc(destination, length, samplingRateRatio, 1,0);
212 // TODO: rewriting this so that it does not call the override method we can save a multiply and add
213 // for each sample.
214 }
207 bool ReceiveAudioThread::isBufferReady(){ 215 bool ReceiveAudioThread::isBufferReady(){
208 return bufferReady; 216 return bufferReady;
209 } 217 }
210 void ReceiveAudioThread::startThread(){ 218 void ReceiveAudioThread::startThread(){
211 BeagleRT_scheduleAuxiliaryTask(receiveDataTask); 219 BeagleRT_scheduleAuxiliaryTask(receiveDataTask);