comparison examples/oscillator_bank/main.cpp @ 300:dbeed520b014 prerelease

Renamed projects to examples
author Giulio Moro <giuliomoro@yahoo.it>
date Fri, 27 May 2016 13:58:20 +0100
parents projects/oscillator_bank/main.cpp@3c3a1357657d
children e4392164b458
comparison
equal deleted inserted replaced
297:a3d83ebdf49b 300:dbeed520b014
1 /*
2 * main.cpp
3 *
4 * Created on: Oct 24, 2014
5 * Author: parallels
6 */
7
8 #include <iostream>
9 #include <cstdlib>
10 #include <libgen.h>
11 #include <signal.h>
12 #include <getopt.h>
13 #include <BeagleRT.h>
14
15 using namespace std;
16
17 int gNumOscillators = 32;
18 int gWavetableLength = 1024;
19
20 // Handle Ctrl-C by requesting that the audio rendering stop
21 void interrupt_handler(int var)
22 {
23 gShouldStop = true;
24 }
25
26 // Print usage information
27 void usage(const char * processName)
28 {
29 cerr << "Usage: " << processName << " [options]" << endl;
30
31 BeagleRT_usage();
32
33 cerr << " --num-oscillators [-n] oscs: Set the number of oscillators to use (default: 32)\n";
34 cerr << " --wavetable [-w] length: Set the wavetable length in samples (default: 1024)\n";
35 cerr << " --help [-h]: Print this menu\n";
36 }
37
38 int main(int argc, char *argv[])
39 {
40 BeagleRTInitSettings settings; // Standard audio settings
41
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);
52
53 // Parse command-line arguments
54 while (1) {
55 int c;
56 if ((c = BeagleRT_getopt_long(argc, argv, "hn:w:", customOptions, &settings)) < 0)
57 break;
58 switch (c) {
59 case 'h':
60 usage(basename(argv[0]));
61 exit(0);
62 case 'n':
63 gNumOscillators = atoi(optarg);
64 if(gNumOscillators <= 0) {
65 usage(basename(argv[0]));
66 exit(0);
67 }
68 break;
69 case 'w':
70 gWavetableLength = atoi(optarg);
71 if(gWavetableLength < 4)
72 gWavetableLength = 4;
73 if(gWavetableLength > 16384)
74 gWavetableLength = 16384;
75 break;
76 case '?':
77 default:
78 usage(basename(argv[0]));
79 exit(1);
80 }
81 }
82
83 // Initialise the PRU audio device
84 if(BeagleRT_initAudio(&settings, 0) != 0) {
85 cout << "Error: unable to initialise audio" << endl;
86 return -1;
87 }
88
89 if(settings.verbose) {
90 cout << "--> Using " << gNumOscillators << " oscillators and wavetable of " << gWavetableLength << " samples\n";
91 }
92
93 // Start the audio device running
94 if(BeagleRT_startAudio()) {
95 cout << "Error: unable to start real-time audio" << endl;
96 return -1;
97 }
98
99 // Set up interrupt handler to catch Control-C and SIGTERM
100 signal(SIGINT, interrupt_handler);
101 signal(SIGTERM, interrupt_handler);
102
103 // Run until told to stop
104 while(!gShouldStop) {
105 usleep(100000);
106 }
107
108 // Stop the audio device
109 BeagleRT_stopAudio();
110
111 // Clean up any resources allocated for audio
112 BeagleRT_cleanupAudio();
113
114 // All done!
115 return 0;
116 }