victor@4: /* victor@4: * main.cpp victor@4: * victor@4: * Created on: Oct 24, 2014 victor@4: * Author: parallels victor@4: */ victor@4: victor@4: #include victor@4: #include victor@4: #include victor@4: #include victor@4: #include "../../include/RTAudio.h" victor@4: victor@4: using namespace std; victor@4: victor@4: // Handle Ctrl-C by requesting that the audio rendering stop victor@4: void interrupt_handler(int var) victor@4: { victor@4: gShouldStop = true; victor@4: } victor@4: victor@4: // Print usage information victor@4: void usage(const char * processName) victor@4: { victor@4: cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f frequency]" << endl; victor@4: cerr << " -h: Print this menu\n"; victor@4: cerr << " -v: Enable verbose messages\n"; victor@4: cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n"; victor@4: cerr << " -s fftsize: Set the fSize of the FFT, in samples\n"; victor@4: cerr << " -m: Enable the matrix (ADC and DAC) as well as audio\n"; victor@4: } victor@4: victor@4: int main(int argc, char *argv[]) victor@4: { victor@4: int periodSize = 8; // Period size in sensor frames victor@4: int verbose = 0; // Verbose printing level victor@4: int fftSize = 64; // Size of the FFT, in samples victor@4: int useMatrix = 0; // Whether to use the matrix or just audio victor@4: victor@4: // Parse command-line arguments victor@4: while (1) { victor@4: int c; victor@4: if ((c = getopt(argc, argv, "hp:vf:m")) < 0) victor@4: break; victor@4: switch (c) { victor@4: case 'h': victor@4: usage(basename(argv[0])); victor@4: exit(0); victor@4: case 'p': victor@4: periodSize = atoi(optarg); victor@4: if(periodSize < 1) victor@4: periodSize = 1; victor@4: break; victor@4: case 'v': victor@4: verbose = 1; victor@4: break; victor@4: case 's': victor@4: fftSize = atof(optarg); victor@4: break; victor@4: case 'm': victor@4: useMatrix = 1; victor@4: break; victor@4: case '?': victor@4: default: victor@4: usage(basename(argv[0])); victor@4: exit(1); victor@4: } victor@4: } victor@4: victor@4: victor@4: // Set verbose logging information (optional by using value > 0; default is 0) victor@4: setVerboseLevel(verbose); victor@4: victor@4: if(verbose) { victor@4: cout << "Starting with period size " << periodSize << " and FFT size " << fftSize << endl; victor@4: if(useMatrix) victor@4: cout << "Matrix enabled\n"; victor@4: else victor@4: cout << "Matrix disabled\n"; victor@4: } victor@4: victor@4: // Initialise the PRU audio device victor@4: if(initAudio(periodSize, useMatrix, &fftSize) != 0) { victor@4: cout << "Error: unable to initialise audio" << endl; victor@4: return -1; victor@4: } victor@4: victor@4: // Start the audio device running victor@4: if(startAudio()) { victor@4: cout << "Error: unable to start real-time audio" << endl; victor@4: return -1; victor@4: } victor@4: victor@4: // Set up interrupt handler to catch Control-C victor@4: signal(SIGINT, interrupt_handler); victor@4: victor@4: // Run until told to stop victor@4: while(!gShouldStop) { victor@4: usleep(100000); victor@4: } victor@4: victor@4: // Stop the audio device victor@4: stopAudio(); victor@4: victor@4: if(verbose) { victor@4: cout << "Cleaning up..." << endl; victor@4: } victor@4: victor@4: // Clean up any resources allocated for audio victor@4: cleanupAudio(); victor@4: victor@4: // All done! victor@4: return 0; victor@4: }