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