Mercurial > hg > beaglert
comparison core/ReceiveAudioThread.cpp @ 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 | ff28e56e5b7e |
comparison
equal
deleted
inserted
replaced
127:6c8fb6f07b47 | 128:2696a7f00053 |
---|---|
1 #include "ReceiveAudioThread.h" | 1 #include "ReceiveAudioThread.h" |
2 | 2 |
3 #ifdef JUCE | 3 #ifdef USE_JUCE |
4 #else | 4 #else |
5 //initialise static members | 5 //initialise static members |
6 bool ReceiveAudioThread::staticConstructed=false; | 6 bool ReceiveAudioThread::staticConstructed=false; |
7 AuxiliaryTask ReceiveAudioThread::receiveDataTask=NULL; | 7 AuxiliaryTask ReceiveAudioThread::receiveDataTask=NULL; |
8 std::vector<ReceiveAudioThread *> ReceiveAudioThread::objAddrs(0); | 8 std::vector<ReceiveAudioThread *> ReceiveAudioThread::objAddrs(0); |
18 return; | 18 return; |
19 staticConstructed=true; | 19 staticConstructed=true; |
20 threadIsExiting=false; | 20 threadIsExiting=false; |
21 receiveDataTask=BeagleRT_createAuxiliaryTask(receiveData, 90, "receiveDataTask"); //TODO: allow different priorities | 21 receiveDataTask=BeagleRT_createAuxiliaryTask(receiveData, 90, "receiveDataTask"); //TODO: allow different priorities |
22 } | 22 } |
23 #endif /* JUCE */ | 23 #endif /* USE_JUCE */ |
24 | 24 |
25 void ReceiveAudioThread::dealloc(){ | 25 void ReceiveAudioThread::dealloc(){ |
26 free(buffer); | 26 free(buffer); |
27 buffer=NULL; | 27 buffer=NULL; |
28 free(stackBuffer); | 28 free(stackBuffer); |
98 wrapWritePointer(); | 98 wrapWritePointer(); |
99 return numBytes; | 99 return numBytes; |
100 } | 100 } |
101 return 0; //timeout occurred | 101 return 0; //timeout occurred |
102 } | 102 } |
103 //JUCE Thread(threadName), | 103 //USE_JUCE Thread(threadName), |
104 #ifdef JUCE | 104 #ifdef USE_JUCE |
105 ReceiveAudioThread::ReceiveAudioThread(const String &threadName) : | 105 ReceiveAudioThread::ReceiveAudioThread(const String &threadName) : |
106 Thread(threadName), | 106 Thread(threadName), |
107 #else | 107 #else |
108 ReceiveAudioThread::ReceiveAudioThread() : | 108 ReceiveAudioThread::ReceiveAudioThread() : |
109 #endif /* JUCE */ | 109 #endif /* USE_JUCE */ |
110 socket(0), | 110 socket(0), |
111 listening(false), | 111 listening(false), |
112 bufferReady(false), | 112 bufferReady(false), |
113 buffer(NULL), | 113 buffer(NULL), |
114 stackBuffer(NULL), | 114 stackBuffer(NULL), |
115 bufferLength(0), | 115 bufferLength(0), |
116 lastValidPointer(0), | 116 lastValidPointer(0), |
117 waitForSocketTime(5), | 117 waitForSocketTime(5), |
118 #ifdef JUCE | 118 #ifdef USE_JUCE |
119 threadPriority(5) | 119 threadPriority(5) |
120 #else | 120 #else |
121 threadPriority(88) | 121 threadPriority(88) |
122 #endif /* JUCE */ | 122 #endif /* USE_JUCE */ |
123 {}; | 123 {}; |
124 ReceiveAudioThread::~ReceiveAudioThread(){ | 124 ReceiveAudioThread::~ReceiveAudioThread(){ |
125 #ifdef JUCE | 125 #ifdef USE_JUCE |
126 stopThread(1000); | 126 stopThread(1000); |
127 #else | 127 #else |
128 while(threadRunning){ | 128 while(threadRunning){ |
129 usleep(sleepTime*2); //wait for thread to stop | 129 usleep(sleepTime*2); //wait for thread to stop |
130 std::cout<< "Waiting for receiveAudioTask to stop" << std::endl; | 130 std::cout<< "Waiting for receiveAudioTask to stop" << std::endl; |
131 } | 131 } |
132 #endif /* JUCE */ | 132 #endif /* USE_JUCE */ |
133 //TODO: check if thread stopped, otherwise kill it before dealloc | 133 //TODO: check if thread stopped, otherwise kill it before dealloc |
134 dealloc(); | 134 dealloc(); |
135 } | 135 } |
136 void ReceiveAudioThread::init(int aPort, int aSamplesPerBlock, int aChannel){ | 136 void ReceiveAudioThread::init(int aPort, int aSamplesPerBlock, int aChannel){ |
137 dealloc(); | 137 dealloc(); |
138 #ifdef JUCE | 138 #ifdef USE_JUCE |
139 #else | 139 #else |
140 staticConstructor(); | 140 staticConstructor(); |
141 objAddrs.push_back(this);//TODO: this line should be in the constructor | 141 objAddrs.push_back(this);//TODO: this line should be in the constructor |
142 #endif /* JUCE */ | 142 #endif /* USE_JUCE */ |
143 bindToPort(aPort); | 143 bindToPort(aPort); |
144 channel=aChannel; | 144 channel=aChannel; |
145 printf("Channel %d is receiving on port %d\n",aChannel, aPort); | 145 printf("Channel %d is receiving on port %d\n",aChannel, aPort); |
146 // fd=fopen("output.m","w"); //DEBUG | 146 // fd=fopen("output.m","w"); //DEBUG |
147 // fprintf(fd,"var=["); //DEBUG | 147 // fprintf(fd,"var=["); //DEBUG |
160 bufferReady=true; | 160 bufferReady=true; |
161 bytesToRead=sizeof(float)*(payloadLength + headerLength); | 161 bytesToRead=sizeof(float)*(payloadLength + headerLength); |
162 writePointer=-1; | 162 writePointer=-1; |
163 readPointer=0; | 163 readPointer=0; |
164 sleepTime=payloadLength/(float)44100 /4.0; //set sleepTime so that you do not check too often or too infrequently | 164 sleepTime=payloadLength/(float)44100 /4.0; //set sleepTime so that you do not check too often or too infrequently |
165 #ifdef JUCE | 165 #ifdef USE_JUCE |
166 startThread(threadPriority); | 166 startThread(threadPriority); |
167 #else | 167 #else |
168 //TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled | 168 //TODO: the thread cannot be started here at the moment because init() is called in setup(), where tasks cannot be scheduled |
169 #endif /* JUCE */ | 169 #endif /* USE_JUCE */ |
170 } | 170 } |
171 | 171 |
172 void ReceiveAudioThread::bindToPort(int aPort){ | 172 void ReceiveAudioThread::bindToPort(int aPort){ |
173 listening=socket.bindToPort(aPort); | 173 listening=socket.bindToPort(aPort); |
174 #ifdef JUCE | 174 #ifdef USE_JUCE |
175 #else | 175 #else |
176 if(listening==false) //this condition is valid also for JUCE, but we do not printf in JUCE | 176 if(listening==false) //this condition is valid also for USE_JUCE, but we do not printf in USE_JUCE |
177 printf("Could not bind to port %d\n",aPort); | 177 printf("Could not bind to port %d\n",aPort); |
178 #endif /* JUCE */ | 178 #endif /* USE_JUCE */ |
179 } | 179 } |
180 bool ReceiveAudioThread::isListening(){ | 180 bool ReceiveAudioThread::isListening(){ |
181 return listening; | 181 return listening; |
182 } | 182 } |
183 float* ReceiveAudioThread::getCurrentBuffer(int length){ // NOTE: this cannot work all the time unless samplesPerBuffer and payloadLength are multiples | 183 float* ReceiveAudioThread::getCurrentBuffer(int length){ // NOTE: this cannot work all the time unless samplesPerBuffer and payloadLength are multiples |
229 // for each sample. | 229 // for each sample. |
230 } | 230 } |
231 bool ReceiveAudioThread::isBufferReady(){ | 231 bool ReceiveAudioThread::isBufferReady(){ |
232 return bufferReady; | 232 return bufferReady; |
233 } | 233 } |
234 #ifdef JUCE | 234 #ifdef USE_JUCE |
235 #else | 235 #else |
236 void ReceiveAudioThread::startThread(){ | 236 void ReceiveAudioThread::startThread(){ |
237 printf("receivedata is going to be scheduled\n"); | |
238 BeagleRT_scheduleAuxiliaryTask(receiveDataTask); | 237 BeagleRT_scheduleAuxiliaryTask(receiveDataTask); |
239 printf("receivedata has been scheduled\n"); | |
240 } | 238 } |
241 void ReceiveAudioThread::stopThread(){ | 239 void ReceiveAudioThread::stopThread(){ |
242 threadIsExiting=true; | 240 threadIsExiting=true; |
243 } | 241 } |
244 bool ReceiveAudioThread::threadShouldExit(){ | 242 bool ReceiveAudioThread::threadShouldExit(){ |
245 return(gShouldStop || threadIsExiting ); | 243 return(gShouldStop || threadIsExiting ); |
246 } | 244 } |
247 #endif /* JUCE */ | 245 #endif /* USE_JUCE */ |
248 void ReceiveAudioThread::run(){ | 246 void ReceiveAudioThread::run(){ |
249 // fd2=fopen("buffer.m","w"); //DEBUG | 247 // fd2=fopen("buffer.m","w"); //DEBUG |
250 // fprintf(fd2, "buf=["); //DEBUG | 248 // fprintf(fd2, "buf=["); //DEBUG |
251 threadRunning=true; | 249 threadRunning=true; |
252 while(!threadShouldExit()){ //TODO: check that the socket buffer is empty before starting | 250 while(!threadShouldExit()){ //TODO: check that the socket buffer is empty before starting |
253 #ifdef JUCE | 251 #ifdef USE_JUCE |
254 readUdpToBuffer(); // read into the oldBuffer | 252 readUdpToBuffer(); // read into the oldBuffer |
255 usleep(sleepTime); | 253 sleep(sleepTime); |
256 #else | 254 #else |
257 for(unsigned int n=0; n<ReceiveAudioThread::objAddrs.size(); n++){ | 255 for(unsigned int n=0; n<ReceiveAudioThread::objAddrs.size(); n++){ |
258 // printf("%d\n", n); | 256 // printf("%d\n", n); |
259 ReceiveAudioThread::objAddrs[n]->readUdpToBuffer(); | 257 ReceiveAudioThread::objAddrs[n]->readUdpToBuffer(); |
260 } | 258 } |
261 usleep(sleepTime); //TODO: use rt_task_sleep instead | 259 usleep(sleepTime); //TODO: use rt_task_sleep instead |
262 #endif /* JUCE */ | 260 #endif /* USE_JUCE */ |
263 } | 261 } |
264 threadRunning=false; | 262 threadRunning=false; |
265 printf("Thread is not running \n"); | 263 printf("Thread is not running \n"); |
266 // fprintf(fd,"];readPointer,writePointer,lastValidPointer,destination]=deal(var(:,1), var(:,2), var(:,3), var(:,4));"); //DEBUG | 264 // fprintf(fd,"];readPointer,writePointer,lastValidPointer,destination]=deal(var(:,1), var(:,2), var(:,3), var(:,4));"); //DEBUG |
267 // fclose(fd);//DEBUG | 265 // fclose(fd);//DEBUG |