andrewm@0: /* andrewm@0: * main.cpp andrewm@0: * andrewm@0: * Created on: Oct 24, 2014 andrewm@0: * Author: parallels andrewm@0: */ andrewm@0: andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@0: #include andrewm@5: #include andrewm@0: #include "../../include/RTAudio.h" andrewm@0: andrewm@0: using namespace std; andrewm@0: andrewm@0: int gSensorInputFrequency = 0; andrewm@0: int gSensorInputAmplitude = 1; andrewm@0: andrewm@0: // Handle Ctrl-C by requesting that the audio rendering stop andrewm@0: void interrupt_handler(int var) andrewm@0: { andrewm@0: gShouldStop = true; andrewm@0: } andrewm@0: andrewm@0: // Print usage information andrewm@0: void usage(const char * processName) andrewm@0: { andrewm@5: cerr << "Usage: " << processName << " [options]" << endl; andrewm@5: andrewm@5: BeagleRT_usage(); andrewm@5: andrewm@5: cerr << " --frequency [-f] input: Choose the analog input controlling frequency (0-7; default 0)\n"; andrewm@5: cerr << " --amplitude [-a] input: Choose the analog input controlling amplitude (0-7; default 1)\n"; andrewm@5: cerr << " --help [-h]: Print this menu\n"; andrewm@0: } andrewm@0: andrewm@0: int main(int argc, char *argv[]) andrewm@0: { andrewm@5: RTAudioSettings settings; // Standard audio settings andrewm@5: andrewm@5: struct option customOptions[] = andrewm@5: { andrewm@5: {"help", 0, NULL, 'h'}, andrewm@5: {"frequency", 1, NULL, 'f'}, andrewm@5: {"amplitude", 1, NULL, 'a'}, andrewm@5: {NULL, 0, NULL, 0} andrewm@5: }; andrewm@5: andrewm@5: // Set default settings andrewm@5: BeagleRT_defaultSettings(&settings); andrewm@0: andrewm@0: // Parse command-line arguments andrewm@0: while (1) { andrewm@0: int c; andrewm@5: if ((c = BeagleRT_getopt_long(argc, argv, "hf:a:", customOptions, &settings)) < 0) andrewm@0: break; andrewm@0: switch (c) { andrewm@0: case 'h': andrewm@0: usage(basename(argv[0])); andrewm@0: exit(0); andrewm@0: case 'f': andrewm@0: gSensorInputFrequency = atoi(optarg); andrewm@0: if(gSensorInputFrequency < 0 || gSensorInputFrequency > 7) { andrewm@0: usage(basename(argv[0])); andrewm@0: exit(0); andrewm@0: } andrewm@0: break; andrewm@0: case 'a': andrewm@0: gSensorInputAmplitude = atoi(optarg); andrewm@0: if(gSensorInputAmplitude < 0 || gSensorInputAmplitude > 7) { andrewm@0: usage(basename(argv[0])); andrewm@0: exit(0); andrewm@0: } andrewm@0: break; andrewm@0: case '?': andrewm@0: default: andrewm@0: usage(basename(argv[0])); andrewm@0: exit(1); andrewm@0: } andrewm@0: } andrewm@0: andrewm@5: // Initialise the PRU audio device andrewm@5: if(BeagleRT_initAudio(&settings, 0) != 0) { andrewm@5: cout << "Error: unable to initialise audio" << endl; andrewm@5: return -1; andrewm@5: } andrewm@0: andrewm@5: if(settings.verbose) { andrewm@0: cout << "--> Frequency on input " << gSensorInputFrequency << endl; andrewm@0: cout << "--> Amplitude on input " << gSensorInputAmplitude << endl; andrewm@0: } andrewm@0: andrewm@0: // Start the audio device running andrewm@5: if(BeagleRT_startAudio()) { andrewm@0: cout << "Error: unable to start real-time audio" << endl; andrewm@0: return -1; andrewm@0: } andrewm@0: andrewm@15: // Set up interrupt handler to catch Control-C and SIGTERM andrewm@0: signal(SIGINT, interrupt_handler); andrewm@15: signal(SIGTERM, interrupt_handler); andrewm@0: andrewm@0: // Run until told to stop andrewm@0: while(!gShouldStop) { andrewm@0: usleep(100000); andrewm@0: } andrewm@0: andrewm@0: // Stop the audio device andrewm@5: BeagleRT_stopAudio(); andrewm@0: andrewm@0: // Clean up any resources allocated for audio andrewm@5: BeagleRT_cleanupAudio(); andrewm@0: andrewm@0: // All done! andrewm@0: return 0; andrewm@0: }