robert@493: /* robert@493: * main.cpp robert@493: * robert@493: * Created on: Oct 24, 2014 robert@493: * Author: parallels robert@493: */ robert@493: robert@493: #include robert@493: #include robert@493: #include robert@493: #include robert@493: #include robert@493: #include robert@493: robert@493: using namespace std; robert@493: robert@493: int gNumOscillators = 32; robert@493: int gWavetableLength = 1024; robert@493: robert@493: // Handle Ctrl-C by requesting that the audio rendering stop robert@493: void interrupt_handler(int var) robert@493: { robert@493: gShouldStop = true; robert@493: } robert@493: robert@493: // Print usage information robert@493: void usage(const char * processName) robert@493: { robert@493: cerr << "Usage: " << processName << " [options]" << endl; robert@493: robert@493: Bela_usage(); robert@493: robert@493: cerr << " --num-oscillators [-n] oscs: Set the number of oscillators to use (default: 32)\n"; robert@493: cerr << " --wavetable [-w] length: Set the wavetable length in samples (default: 1024)\n"; robert@493: cerr << " --help [-h]: Print this menu\n"; robert@493: } robert@493: robert@493: int main(int argc, char *argv[]) robert@493: { robert@493: BelaInitSettings settings; // Standard audio settings robert@493: robert@493: struct option customOptions[] = robert@493: { robert@493: {"help", 0, NULL, 'h'}, robert@493: {"num-oscillators", 1, NULL, 'n'}, robert@493: {"wavetable", 1, NULL, 'w'}, robert@493: {NULL, 0, NULL, 0} robert@493: }; robert@493: robert@493: // Set default settings robert@493: Bela_defaultSettings(&settings); robert@493: robert@493: // Parse command-line arguments robert@493: while (1) { robert@493: int c; robert@493: if ((c = Bela_getopt_long(argc, argv, "hn:w:", customOptions, &settings)) < 0) robert@493: break; robert@493: switch (c) { robert@493: case 'h': robert@493: usage(basename(argv[0])); robert@493: exit(0); robert@493: case 'n': robert@493: gNumOscillators = atoi(optarg); robert@493: if(gNumOscillators <= 0) { robert@493: usage(basename(argv[0])); robert@493: exit(0); robert@493: } robert@493: break; robert@493: case 'w': robert@493: gWavetableLength = atoi(optarg); robert@493: if(gWavetableLength < 4) robert@493: gWavetableLength = 4; robert@493: if(gWavetableLength > 16384) robert@493: gWavetableLength = 16384; robert@493: break; robert@493: case '?': robert@493: default: robert@493: usage(basename(argv[0])); robert@493: exit(1); robert@493: } robert@493: } robert@493: robert@493: // Initialise the PRU audio device robert@493: if(Bela_initAudio(&settings, 0) != 0) { robert@493: cout << "Error: unable to initialise audio" << endl; robert@493: return -1; robert@493: } robert@493: robert@493: if(settings.verbose) { robert@493: cout << "--> Using " << gNumOscillators << " oscillators and wavetable of " << gWavetableLength << " samples\n"; robert@493: } robert@493: robert@493: // Start the audio device running robert@493: if(Bela_startAudio()) { robert@493: cout << "Error: unable to start real-time audio" << endl; robert@493: return -1; robert@493: } robert@493: robert@493: // Set up interrupt handler to catch Control-C and SIGTERM robert@493: signal(SIGINT, interrupt_handler); robert@493: signal(SIGTERM, interrupt_handler); robert@493: robert@493: // Run until told to stop robert@493: while(!gShouldStop) { robert@493: usleep(100000); robert@493: } robert@493: robert@493: // Stop the audio device robert@493: Bela_stopAudio(); robert@493: robert@493: // Clean up any resources allocated for audio robert@493: Bela_cleanupAudio(); robert@493: robert@493: // All done! robert@493: return 0; robert@493: }