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