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: int gNumOscillators = 32; andrewm@0: int gWavetableLength = 1024; andrewm@0: 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 input] [-a input]" << 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 << " -n oscs: Set the number of oscillators to use (default: 32)\n"; andrewm@0: cerr << " -w length: Set the wavetable length in samples (default: 1024)\n"; andrewm@0: cerr << " -m: Enable the matrix (ADC and DAC) for controlling parameters\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: int useMatrix = 0; andrewm@0: andrewm@0: // Parse command-line arguments andrewm@0: while (1) { andrewm@0: int c; andrewm@0: if ((c = getopt(argc, argv, "hp:vn:w: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 'n': andrewm@0: gNumOscillators = atoi(optarg); andrewm@0: if(gNumOscillators <= 0) { andrewm@0: usage(basename(argv[0])); andrewm@0: exit(0); andrewm@0: } andrewm@0: break; andrewm@0: case 'w': andrewm@0: gWavetableLength = atoi(optarg); andrewm@0: if(gWavetableLength < 4) andrewm@0: gWavetableLength = 4; andrewm@0: if(gWavetableLength > 16384) andrewm@0: gWavetableLength = 16384; 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 << endl; andrewm@0: cout << "--> Using " << gNumOscillators << " oscillators and wavetable of " << gWavetableLength << " samples\n"; andrewm@0: cout << "--> Matrix "; andrewm@0: if(useMatrix) cout << "enabled\n"; andrewm@0: else cout << "disabled\n"; andrewm@0: } andrewm@0: andrewm@0: // Initialise the PRU audio device andrewm@0: if(initAudio(periodSize, useMatrix, 0) != 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: }