comparison projects/oscillator_bank/main.cpp @ 5:09f03ac40fcc

API improvements and cleanups. Now all common audio command-line options can be parsed automatically.
author andrewm
date Sat, 08 Nov 2014 16:16:55 +0100
parents 8a575ba3ab52
children 901d205d1a3c
comparison
equal deleted inserted replaced
4:f34c63568523 5:09f03ac40fcc
7 7
8 #include <iostream> 8 #include <iostream>
9 #include <cstdlib> 9 #include <cstdlib>
10 #include <libgen.h> 10 #include <libgen.h>
11 #include <signal.h> 11 #include <signal.h>
12 #include <getopt.h>
12 #include "../../include/RTAudio.h" 13 #include "../../include/RTAudio.h"
13 14
14 using namespace std; 15 using namespace std;
15 16
16 int gNumOscillators = 32; 17 int gNumOscillators = 32;
17 int gWavetableLength = 1024; 18 int gWavetableLength = 1024;
18
19 19
20 // Handle Ctrl-C by requesting that the audio rendering stop 20 // Handle Ctrl-C by requesting that the audio rendering stop
21 void interrupt_handler(int var) 21 void interrupt_handler(int var)
22 { 22 {
23 gShouldStop = true; 23 gShouldStop = true;
24 } 24 }
25 25
26 // Print usage information 26 // Print usage information
27 void usage(const char * processName) 27 void usage(const char * processName)
28 { 28 {
29 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f input] [-a input]" << endl; 29 cerr << "Usage: " << processName << " [options]" << endl;
30 cerr << " -h: Print this menu\n"; 30
31 cerr << " -v: Enable verbose messages\n"; 31 BeagleRT_usage();
32 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n"; 32
33 cerr << " -n oscs: Set the number of oscillators to use (default: 32)\n"; 33 cerr << " --num-oscillators [-n] oscs: Set the number of oscillators to use (default: 32)\n";
34 cerr << " -w length: Set the wavetable length in samples (default: 1024)\n"; 34 cerr << " --wavetable [-w] length: Set the wavetable length in samples (default: 1024)\n";
35 cerr << " -m: Enable the matrix (ADC and DAC) for controlling parameters\n"; 35 cerr << " --help [-h]: Print this menu\n";
36 } 36 }
37 37
38 int main(int argc, char *argv[]) 38 int main(int argc, char *argv[])
39 { 39 {
40 int periodSize = 8; // Period size in sensor frames 40 RTAudioSettings settings; // Standard audio settings
41 int verbose = 0; // Verbose printing level 41
42 int useMatrix = 0; 42 struct option customOptions[] =
43 {
44 {"help", 0, NULL, 'h'},
45 {"num-oscillators", 1, NULL, 'n'},
46 {"wavetable", 1, NULL, 'w'},
47 {NULL, 0, NULL, 0}
48 };
49
50 // Set default settings
51 BeagleRT_defaultSettings(&settings);
43 52
44 // Parse command-line arguments 53 // Parse command-line arguments
45 while (1) { 54 while (1) {
46 int c; 55 int c;
47 if ((c = getopt(argc, argv, "hp:vn:w:m")) < 0) 56 if ((c = BeagleRT_getopt_long(argc, argv, "hn:w:", customOptions, &settings)) < 0)
48 break; 57 break;
49 switch (c) { 58 switch (c) {
50 case 'h': 59 case 'h':
51 usage(basename(argv[0])); 60 usage(basename(argv[0]));
52 exit(0); 61 exit(0);
53 case 'p':
54 periodSize = atoi(optarg);
55 if(periodSize < 1)
56 periodSize = 1;
57 break;
58 case 'v':
59 verbose = 1;
60 break;
61 case 'n': 62 case 'n':
62 gNumOscillators = atoi(optarg); 63 gNumOscillators = atoi(optarg);
63 if(gNumOscillators <= 0) { 64 if(gNumOscillators <= 0) {
64 usage(basename(argv[0])); 65 usage(basename(argv[0]));
65 exit(0); 66 exit(0);
70 if(gWavetableLength < 4) 71 if(gWavetableLength < 4)
71 gWavetableLength = 4; 72 gWavetableLength = 4;
72 if(gWavetableLength > 16384) 73 if(gWavetableLength > 16384)
73 gWavetableLength = 16384; 74 gWavetableLength = 16384;
74 break; 75 break;
75 case 'm':
76 useMatrix = 1;
77 break;
78 case '?': 76 case '?':
79 default: 77 default:
80 usage(basename(argv[0])); 78 usage(basename(argv[0]));
81 exit(1); 79 exit(1);
82 } 80 }
83 } 81 }
84 82
85
86 // Set verbose logging information (optional by using value > 0; default is 0)
87 setVerboseLevel(verbose);
88
89 if(verbose) {
90 cout << "Starting with period size " << periodSize << endl;
91 cout << "--> Using " << gNumOscillators << " oscillators and wavetable of " << gWavetableLength << " samples\n";
92 cout << "--> Matrix ";
93 if(useMatrix) cout << "enabled\n";
94 else cout << "disabled\n";
95 }
96
97 // Initialise the PRU audio device 83 // Initialise the PRU audio device
98 if(initAudio(periodSize, useMatrix, 0) != 0) { 84 if(BeagleRT_initAudio(&settings, 0) != 0) {
99 cout << "Error: unable to initialise audio" << endl; 85 cout << "Error: unable to initialise audio" << endl;
100 return -1; 86 return -1;
101 } 87 }
102 88
89 if(settings.verbose) {
90 cout << "--> Using " << gNumOscillators << " oscillators and wavetable of " << gWavetableLength << " samples\n";
91 }
92
103 // Start the audio device running 93 // Start the audio device running
104 if(startAudio()) { 94 if(BeagleRT_startAudio()) {
105 cout << "Error: unable to start real-time audio" << endl; 95 cout << "Error: unable to start real-time audio" << endl;
106 return -1; 96 return -1;
107 } 97 }
108 98
109 // Set up interrupt handler to catch Control-C 99 // Set up interrupt handler to catch Control-C
113 while(!gShouldStop) { 103 while(!gShouldStop) {
114 usleep(100000); 104 usleep(100000);
115 } 105 }
116 106
117 // Stop the audio device 107 // Stop the audio device
118 stopAudio(); 108 BeagleRT_stopAudio();
119
120 if(verbose) {
121 cout << "Cleaning up..." << endl;
122 }
123 109
124 // Clean up any resources allocated for audio 110 // Clean up any resources allocated for audio
125 cleanupAudio(); 111 BeagleRT_cleanupAudio();
126 112
127 // All done! 113 // All done!
128 return 0; 114 return 0;
129 } 115 }