annotate projects/oscillator_bank/main.cpp @ 15:901d205d1a3c

Updated to latest PRU library; external PRU file no longer needed. Also catch SIGTERM as well as SIGINT to clean up gracefully.
author andrewm
date Sat, 07 Feb 2015 16:41:56 +0000
parents 09f03ac40fcc
children a6d223473ea2
rev   line source
andrewm@0 1 /*
andrewm@0 2 * main.cpp
andrewm@0 3 *
andrewm@0 4 * Created on: Oct 24, 2014
andrewm@0 5 * Author: parallels
andrewm@0 6 */
andrewm@0 7
andrewm@0 8 #include <iostream>
andrewm@0 9 #include <cstdlib>
andrewm@0 10 #include <libgen.h>
andrewm@0 11 #include <signal.h>
andrewm@5 12 #include <getopt.h>
andrewm@0 13 #include "../../include/RTAudio.h"
andrewm@0 14
andrewm@0 15 using namespace std;
andrewm@0 16
andrewm@0 17 int gNumOscillators = 32;
andrewm@0 18 int gWavetableLength = 1024;
andrewm@0 19
andrewm@0 20 // Handle Ctrl-C by requesting that the audio rendering stop
andrewm@0 21 void interrupt_handler(int var)
andrewm@0 22 {
andrewm@0 23 gShouldStop = true;
andrewm@0 24 }
andrewm@0 25
andrewm@0 26 // Print usage information
andrewm@0 27 void usage(const char * processName)
andrewm@0 28 {
andrewm@5 29 cerr << "Usage: " << processName << " [options]" << endl;
andrewm@5 30
andrewm@5 31 BeagleRT_usage();
andrewm@5 32
andrewm@5 33 cerr << " --num-oscillators [-n] oscs: Set the number of oscillators to use (default: 32)\n";
andrewm@5 34 cerr << " --wavetable [-w] length: Set the wavetable length in samples (default: 1024)\n";
andrewm@5 35 cerr << " --help [-h]: Print this menu\n";
andrewm@0 36 }
andrewm@0 37
andrewm@0 38 int main(int argc, char *argv[])
andrewm@0 39 {
andrewm@5 40 RTAudioSettings settings; // Standard audio settings
andrewm@5 41
andrewm@5 42 struct option customOptions[] =
andrewm@5 43 {
andrewm@5 44 {"help", 0, NULL, 'h'},
andrewm@5 45 {"num-oscillators", 1, NULL, 'n'},
andrewm@5 46 {"wavetable", 1, NULL, 'w'},
andrewm@5 47 {NULL, 0, NULL, 0}
andrewm@5 48 };
andrewm@5 49
andrewm@5 50 // Set default settings
andrewm@5 51 BeagleRT_defaultSettings(&settings);
andrewm@0 52
andrewm@0 53 // Parse command-line arguments
andrewm@0 54 while (1) {
andrewm@0 55 int c;
andrewm@5 56 if ((c = BeagleRT_getopt_long(argc, argv, "hn:w:", customOptions, &settings)) < 0)
andrewm@0 57 break;
andrewm@0 58 switch (c) {
andrewm@0 59 case 'h':
andrewm@0 60 usage(basename(argv[0]));
andrewm@0 61 exit(0);
andrewm@0 62 case 'n':
andrewm@0 63 gNumOscillators = atoi(optarg);
andrewm@0 64 if(gNumOscillators <= 0) {
andrewm@0 65 usage(basename(argv[0]));
andrewm@0 66 exit(0);
andrewm@0 67 }
andrewm@0 68 break;
andrewm@0 69 case 'w':
andrewm@0 70 gWavetableLength = atoi(optarg);
andrewm@0 71 if(gWavetableLength < 4)
andrewm@0 72 gWavetableLength = 4;
andrewm@0 73 if(gWavetableLength > 16384)
andrewm@0 74 gWavetableLength = 16384;
andrewm@0 75 break;
andrewm@0 76 case '?':
andrewm@0 77 default:
andrewm@0 78 usage(basename(argv[0]));
andrewm@0 79 exit(1);
andrewm@0 80 }
andrewm@0 81 }
andrewm@0 82
andrewm@0 83 // Initialise the PRU audio device
andrewm@5 84 if(BeagleRT_initAudio(&settings, 0) != 0) {
andrewm@0 85 cout << "Error: unable to initialise audio" << endl;
andrewm@0 86 return -1;
andrewm@0 87 }
andrewm@0 88
andrewm@5 89 if(settings.verbose) {
andrewm@5 90 cout << "--> Using " << gNumOscillators << " oscillators and wavetable of " << gWavetableLength << " samples\n";
andrewm@5 91 }
andrewm@5 92
andrewm@0 93 // Start the audio device running
andrewm@5 94 if(BeagleRT_startAudio()) {
andrewm@0 95 cout << "Error: unable to start real-time audio" << endl;
andrewm@0 96 return -1;
andrewm@0 97 }
andrewm@0 98
andrewm@15 99 // Set up interrupt handler to catch Control-C and SIGTERM
andrewm@0 100 signal(SIGINT, interrupt_handler);
andrewm@15 101 signal(SIGTERM, interrupt_handler);
andrewm@0 102
andrewm@0 103 // Run until told to stop
andrewm@0 104 while(!gShouldStop) {
andrewm@0 105 usleep(100000);
andrewm@0 106 }
andrewm@0 107
andrewm@0 108 // Stop the audio device
andrewm@5 109 BeagleRT_stopAudio();
andrewm@0 110
andrewm@0 111 // Clean up any resources allocated for audio
andrewm@5 112 BeagleRT_cleanupAudio();
andrewm@0 113
andrewm@0 114 // All done!
andrewm@0 115 return 0;
andrewm@0 116 }