andrewm@89: /* andrewm@89: * main.cpp andrewm@89: * andrewm@89: * Created on: Oct 24, 2014 andrewm@89: * Author: parallels andrewm@89: */ andrewm@89: andrewm@89: #include andrewm@89: #include andrewm@89: #include andrewm@89: #include andrewm@89: #include giuliomoro@301: #include andrewm@89: andrewm@89: extern int gBufferSize; andrewm@89: andrewm@89: using namespace std; andrewm@89: andrewm@89: // Handle Ctrl-C by requesting that the audio rendering stop andrewm@89: void interrupt_handler(int var) andrewm@89: { andrewm@89: gShouldStop = true; andrewm@89: } andrewm@89: andrewm@89: // Print usage information andrewm@89: void usage(const char * processName) andrewm@89: { andrewm@89: cerr << "Usage: " << processName << " [options]" << endl; andrewm@89: giuliomoro@301: Bela_usage(); andrewm@89: andrewm@89: cerr << " --buffer-size [-b] size Set the analysis buffer size\n"; andrewm@89: cerr << " --help [-h]: Print this menu\n"; andrewm@89: } andrewm@89: andrewm@89: int main(int argc, char *argv[]) andrewm@89: { giuliomoro@301: BelaInitSettings settings; // Standard audio settings andrewm@89: andrewm@89: struct option customOptions[] = andrewm@89: { andrewm@89: {"help", 0, NULL, 'h'}, andrewm@89: {"buffer-size", 1, NULL, 'b'}, andrewm@89: {NULL, 0, NULL, 0} andrewm@89: }; andrewm@89: andrewm@89: // Set default settings giuliomoro@301: Bela_defaultSettings(&settings); andrewm@89: andrewm@89: // By default use a longer period size because latency is not an issue andrewm@89: settings.periodSize = 32; andrewm@89: andrewm@89: // Parse command-line arguments andrewm@89: while (1) { andrewm@89: int c; giuliomoro@301: if ((c = Bela_getopt_long(argc, argv, "hb:", customOptions, &settings)) < 0) andrewm@89: break; andrewm@89: switch (c) { andrewm@89: case 'b': andrewm@89: gBufferSize = atoi(optarg); andrewm@89: break; andrewm@89: case 'h': andrewm@89: usage(basename(argv[0])); andrewm@89: exit(0); andrewm@89: case '?': andrewm@89: default: andrewm@89: usage(basename(argv[0])); andrewm@89: exit(1); andrewm@89: } andrewm@89: } andrewm@89: andrewm@89: if(gBufferSize < settings.periodSize) andrewm@89: gBufferSize = settings.periodSize; andrewm@89: andrewm@89: // Initialise the PRU audio device giuliomoro@301: if(Bela_initAudio(&settings, 0) != 0) { andrewm@89: cout << "Error: unable to initialise audio" << endl; andrewm@89: return -1; andrewm@89: } andrewm@89: andrewm@89: // Start the audio device running giuliomoro@301: if(Bela_startAudio()) { andrewm@89: cout << "Error: unable to start real-time audio" << endl; andrewm@89: return -1; andrewm@89: } andrewm@89: andrewm@89: // Set up interrupt handler to catch Control-C and SIGTERM andrewm@89: signal(SIGINT, interrupt_handler); andrewm@89: signal(SIGTERM, interrupt_handler); andrewm@89: andrewm@89: // Run until told to stop andrewm@89: while(!gShouldStop) { andrewm@89: usleep(100000); andrewm@89: } andrewm@89: andrewm@89: // Stop the audio device giuliomoro@301: Bela_stopAudio(); andrewm@89: andrewm@89: // Clean up any resources allocated for audio giuliomoro@301: Bela_cleanupAudio(); andrewm@89: andrewm@89: // All done! andrewm@89: return 0; andrewm@89: }