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 giuliomoro@301: #include andrewm@0: andrewm@0: using namespace std; andrewm@0: andrewm@0: int gNumOscillators = 32; andrewm@0: int gWavetableLength = 1024; 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: giuliomoro@301: Bela_usage(); andrewm@5: andrewm@5: cerr << " --num-oscillators [-n] oscs: Set the number of oscillators to use (default: 32)\n"; andrewm@5: cerr << " --wavetable [-w] length: Set the wavetable length in samples (default: 1024)\n"; andrewm@5: cerr << " --help [-h]: Print this menu\n"; andrewm@0: } andrewm@0: andrewm@0: int main(int argc, char *argv[]) andrewm@0: { giuliomoro@301: BelaInitSettings settings; // Standard audio settings andrewm@5: andrewm@5: struct option customOptions[] = andrewm@5: { andrewm@5: {"help", 0, NULL, 'h'}, andrewm@5: {"num-oscillators", 1, NULL, 'n'}, andrewm@5: {"wavetable", 1, NULL, 'w'}, andrewm@5: {NULL, 0, NULL, 0} andrewm@5: }; andrewm@5: andrewm@5: // Set default settings giuliomoro@301: Bela_defaultSettings(&settings); andrewm@0: andrewm@0: // Parse command-line arguments andrewm@0: while (1) { andrewm@0: int c; giuliomoro@301: if ((c = Bela_getopt_long(argc, argv, "hn:w:", 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 '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 '?': andrewm@0: default: andrewm@0: usage(basename(argv[0])); andrewm@0: exit(1); andrewm@0: } andrewm@0: } andrewm@0: andrewm@0: // Initialise the PRU audio device giuliomoro@301: if(Bela_initAudio(&settings, 0) != 0) { andrewm@0: cout << "Error: unable to initialise audio" << endl; andrewm@0: return -1; andrewm@0: } andrewm@0: andrewm@5: if(settings.verbose) { andrewm@5: cout << "--> Using " << gNumOscillators << " oscillators and wavetable of " << gWavetableLength << " samples\n"; andrewm@5: } andrewm@5: andrewm@0: // Start the audio device running giuliomoro@301: if(Bela_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 giuliomoro@301: Bela_stopAudio(); andrewm@0: andrewm@0: // Clean up any resources allocated for audio giuliomoro@301: Bela_cleanupAudio(); andrewm@0: andrewm@0: // All done! andrewm@0: return 0; andrewm@0: }