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