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@0: #include "../../include/RTAudio.h" andrewm@0: andrewm@0: using namespace std; 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@0: cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f frequency]" << endl; andrewm@0: cerr << " -h: Print this menu\n"; andrewm@0: cerr << " -v: Enable verbose messages\n"; andrewm@0: cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n"; andrewm@0: cerr << " -f frequency: Set the frequency of the oscillator\n"; andrewm@0: cerr << " -m: Enable the matrix (ADC and DAC) as well as audio\n"; andrewm@0: } andrewm@0: andrewm@0: int main(int argc, char *argv[]) andrewm@0: { andrewm@0: int periodSize = 8; // Period size in sensor frames andrewm@0: int verbose = 0; // Verbose printing level andrewm@0: float frequency = 440.0; // Frequency of oscillator andrewm@0: int useMatrix = 0; // Whether to use the matrix or just audio andrewm@0: andrewm@0: // Parse command-line arguments andrewm@0: while (1) { andrewm@0: int c; andrewm@0: if ((c = getopt(argc, argv, "hp:vf:m")) < 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 'p': andrewm@0: periodSize = atoi(optarg); andrewm@0: if(periodSize < 1) andrewm@0: periodSize = 1; andrewm@0: break; andrewm@0: case 'v': andrewm@0: verbose = 1; andrewm@0: break; andrewm@0: case 'f': andrewm@0: frequency = atof(optarg); andrewm@0: break; andrewm@0: case 'm': andrewm@0: useMatrix = 1; 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@0: andrewm@0: // Set verbose logging information (optional by using value > 0; default is 0) andrewm@0: setVerboseLevel(verbose); andrewm@0: andrewm@0: if(verbose) { andrewm@0: cout << "Starting with period size " << periodSize << " and frequency " << frequency << endl; andrewm@0: if(useMatrix) andrewm@0: cout << "Matrix enabled\n"; andrewm@0: else andrewm@0: cout << "Matrix disabled\n"; andrewm@0: } andrewm@0: andrewm@0: // Initialise the PRU audio device andrewm@0: if(initAudio(periodSize, useMatrix, &frequency) != 0) { andrewm@0: cout << "Error: unable to initialise audio" << endl; andrewm@0: return -1; andrewm@0: } andrewm@0: andrewm@0: // Start the audio device running andrewm@0: if(startAudio()) { andrewm@0: cout << "Error: unable to start real-time audio" << endl; andrewm@0: return -1; andrewm@0: } andrewm@0: andrewm@0: // Set up interrupt handler to catch Control-C andrewm@0: signal(SIGINT, 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@0: stopAudio(); andrewm@0: andrewm@0: if(verbose) { andrewm@0: cout << "Cleaning up..." << endl; andrewm@0: } andrewm@0: andrewm@0: // Clean up any resources allocated for audio andrewm@0: cleanupAudio(); andrewm@0: andrewm@0: // All done! andrewm@0: return 0; andrewm@0: }