comparison projects/d-box/main.cpp @ 5:09f03ac40fcc

API improvements and cleanups. Now all common audio command-line options can be parsed automatically.
author andrewm
date Sat, 08 Nov 2014 16:16:55 +0100
parents 8a575ba3ab52
children 901d205d1a3c
comparison
equal deleted inserted replaced
4:f34c63568523 5:09f03ac40fcc
260 260
261 gShouldStop = true; 261 gShouldStop = true;
262 } 262 }
263 263
264 264
265 void parseArguments(arg_data args) 265 void parseArguments(arg_data args, RTAudioSettings *settings)
266 { 266 {
267 // Default filename; 267 // Default filename;
268 gPartialFilename = strdup("D-Box_sound_250_60_40_h88_2.txt"); 268 gPartialFilename = strdup("D-Box_sound_250_60_40_h88_2.txt");
269 269
270 // TODO: complete this 270 // TODO: complete this
271 struct option long_option[] = 271 struct option long_option[] =
272 { 272 {
273 {"help", 0, NULL, 'h'}, 273 {"help", 0, NULL, 'h'},
274 {"period", 1, NULL, 'p'},
275 {"verbose", 1, NULL, 'v'},
276 {"audioin", 1, NULL, 'i'}, 274 {"audioin", 1, NULL, 'i'},
277 {"file", 1, NULL, 'f'}, 275 {"file", 1, NULL, 'f'},
278 {"keyboard", 1, NULL, 'k'}, 276 {"keyboard", 1, NULL, 'k'},
279 {"audio-test", 0, NULL, 'A'}, 277 {"audio-test", 0, NULL, 'T'},
280 {"new-sensors", 0, NULL, 'S'}, 278 {"new-sensors", 0, NULL, 'S'},
281 {"sensor0", 1, NULL, 'Q'}, 279 {"sensor0", 1, NULL, 'Q'},
282 {"sensor1", 1, NULL, 'R'}, 280 {"sensor1", 1, NULL, 'R'},
283 {"log", 1, NULL, 'l'}, 281 {"log", 1, NULL, 'l'},
284 {"usesd", 1, NULL, 'u'}, 282 {"usesd", 1, NULL, 'u'},
288 {NULL, 0, NULL, 0}, 286 {NULL, 0, NULL, 0},
289 }; 287 };
290 int morehelp = 0; 288 int morehelp = 0;
291 int tmp = -1; 289 int tmp = -1;
292 290
291 BeagleRT_defaultSettings(settings);
292
293 while (1) 293 while (1)
294 { 294 {
295 int c; 295 int c;
296 if ((c = getopt_long(args.argc, args.argv, "hp:vf:ki:sAQ:R:Sl:u:o:n:g:", long_option, NULL)) < 0) 296 if ((c = BeagleRT_getopt_long(args.argc, args.argv, "hf:ki:sTQ:R:Sl:u:o:n:g:", long_option, settings)) < 0)
297 break; 297 break;
298 switch (c) 298 switch (c)
299 { 299 {
300 case 'h': 300 case 'h':
301 morehelp++; 301 morehelp++;
302 break; 302 break;
303 case 'p':
304 gPeriodSize = atoi(optarg);
305 break;
306 case 'v':
307 gVerbose = 1;
308 break;
309 case 'f': 303 case 'f':
310 free(gPartialFilename); 304 free(gPartialFilename);
311 gPartialFilename = strdup(optarg); 305 gPartialFilename = strdup(optarg);
312 break; 306 break;
313 case 'k': 307 case 'k':
317 gAudioIn = (atoi(optarg)==0) ? false : true; 311 gAudioIn = (atoi(optarg)==0) ? false : true;
318 break; 312 break;
319 case 's': 313 case 's':
320 forceSensors = true; 314 forceSensors = true;
321 break; 315 break;
322 case 'A': 316 case 'T':
323 useAudioTest = true; 317 useAudioTest = true;
324 break; 318 break;
325 case 'S': 319 case 'S':
326 useNewSensors = true; 320 useNewSensors = true;
327 break; 321 break;
358 break; 352 break;
359 default: 353 default:
360 break; 354 break;
361 } 355 }
362 } 356 }
357
358 gPeriodSize = settings->periodSize;
359 gVerbose = settings->verbose;
363 } 360 }
364 361
365 int main(int argc, char *argv[]) 362 int main(int argc, char *argv[])
366 { 363 {
364 RTAudioSettings settings; // Standard audio settings
367 RT_TASK rtSensorThread; 365 RT_TASK rtSensorThread;
368 const char rtSensorThreadName[] = "dbox-sensor"; 366 const char rtSensorThreadName[] = "dbox-sensor";
369 int oscBankHopSize; 367 int oscBankHopSize;
370 368
371 // Parse command-line arguments 369 // Parse command-line arguments
372 args.argc = argc; 370 args.argc = argc;
373 args.argv = argv; 371 args.argv = argv;
374 parseArguments(args); 372 parseArguments(args, &settings);
375 373
376 setVerboseLevel(gVerbose); 374 setVerboseLevel(gVerbose);
377 if(gVerbose == 1 && useAudioTest) 375 if(gVerbose == 1 && useAudioTest)
378 cout << "main() : running in audio test mode" << endl; 376 cout << "main() : running in audio test mode" << endl;
379 377
382 return -1; 380 return -1;
383 381
384 oscBankHopSize = gOscBanks[gCurrentOscBank]->getHopSize()/gOscBanks[gCurrentOscBank]->getMinSpeed(); 382 oscBankHopSize = gOscBanks[gCurrentOscBank]->getHopSize()/gOscBanks[gCurrentOscBank]->getMinSpeed();
385 383
386 // Initialise the audio device 384 // Initialise the audio device
387 if(initAudio(gPeriodSize, 1, &oscBankHopSize) != 0) 385 if(BeagleRT_initAudio(&settings, &oscBankHopSize) != 0)
388 return -1; 386 return -1;
389 387
390 // Initialise the status LED 388 // Initialise the status LED
391 if(!gStatusLED.init(kStatusLEDPin)) { 389 if(!gStatusLED.init(kStatusLEDPin)) {
392 if(gVerbose) 390 if(gVerbose)
403 } 401 }
404 402
405 if(gVerbose == 1) 403 if(gVerbose == 1)
406 cout << "main() : creating audio thread" << endl; 404 cout << "main() : creating audio thread" << endl;
407 405
408 if(startAudio()) { 406 if(BeagleRT_startAudio()) {
409 cout << "Error: unable to start real-time audio" << endl; 407 cout << "Error: unable to start real-time audio" << endl;
410 return -1; 408 return -1;
411 } 409 }
412 410
413 // LED on... 411 // LED on...
463 // Run until told to stop 461 // Run until told to stop
464 while(!gShouldStop) { 462 while(!gShouldStop) {
465 usleep(100000); 463 usleep(100000);
466 } 464 }
467 465
468 stopAudio(); 466 BeagleRT_stopAudio();
469 467
470 if(!useAudioTest) 468 if(!useAudioTest)
471 rt_task_join(&rtSensorThread); 469 rt_task_join(&rtSensorThread);
472 470
473 cleanupAudio(); 471 BeagleRT_cleanupAudio();
474 472
475 pthread_join( keyboardThread, NULL); 473 pthread_join( keyboardThread, NULL);
476 pthread_join( logThread, NULL); 474 pthread_join( logThread, NULL);
477 475
478 for(unsigned int i = 0; i < gOscBanks.size(); i++) 476 for(unsigned int i = 0; i < gOscBanks.size(); i++)