Mercurial > hg > beaglert
comparison examples/03-Analog/analog-input/main.cpp @ 464:8fcfbfb32aa0 prerelease
Examples reorder with subdirectories. Added header to each project. Moved Doxygen to bottom of render.cpp.
author | Robert Jack <robert.h.jack@gmail.com> |
---|---|
date | Mon, 20 Jun 2016 16:20:38 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
463:c47709e8b5c9 | 464:8fcfbfb32aa0 |
---|---|
1 /* | |
2 ____ _____ _ _ | |
3 | __ )| ____| | / \ | |
4 | _ \| _| | | / _ \ | |
5 | |_) | |___| |___ / ___ \ | |
6 |____/|_____|_____/_/ \_\ | |
7 | |
8 The platform for ultra-low latency audio and sensor processing | |
9 | |
10 http://bela.io | |
11 | |
12 A project of the Augmented Instruments Laboratory within the | |
13 Centre for Digital Music at Queen Mary University of London. | |
14 http://www.eecs.qmul.ac.uk/~andrewm | |
15 | |
16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson, | |
17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack, | |
18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved. | |
19 | |
20 The Bela software is distributed under the GNU Lesser General Public License | |
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt | |
22 */ | |
23 | |
24 #include <iostream> | |
25 #include <cstdlib> | |
26 #include <libgen.h> | |
27 #include <signal.h> | |
28 #include <getopt.h> | |
29 #include <Bela.h> | |
30 | |
31 using namespace std; | |
32 | |
33 int gSensorInputFrequency = 0; | |
34 int gSensorInputAmplitude = 1; | |
35 | |
36 // Handle Ctrl-C by requesting that the audio rendering stop | |
37 void interrupt_handler(int var) | |
38 { | |
39 gShouldStop = true; | |
40 } | |
41 | |
42 // Print usage information | |
43 void usage(const char * processName) | |
44 { | |
45 cerr << "Usage: " << processName << " [options]" << endl; | |
46 | |
47 Bela_usage(); | |
48 | |
49 cerr << " --frequency [-f] input: Choose the analog input controlling frequency (0-7; default 0)\n"; | |
50 cerr << " --amplitude [-a] input: Choose the analog input controlling amplitude (0-7; default 1)\n"; | |
51 cerr << " --help [-h]: Print this menu\n"; | |
52 } | |
53 | |
54 int main(int argc, char *argv[]) | |
55 { | |
56 BelaInitSettings settings; // Standard audio settings | |
57 | |
58 struct option customOptions[] = | |
59 { | |
60 {"help", 0, NULL, 'h'}, | |
61 {"frequency", 1, NULL, 'f'}, | |
62 {"amplitude", 1, NULL, 'a'}, | |
63 {NULL, 0, NULL, 0} | |
64 }; | |
65 | |
66 // Set default settings | |
67 Bela_defaultSettings(&settings); | |
68 | |
69 // Parse command-line arguments | |
70 while (1) { | |
71 int c; | |
72 if ((c = Bela_getopt_long(argc, argv, "hf:a:", customOptions, &settings)) < 0) | |
73 break; | |
74 switch (c) { | |
75 case 'h': | |
76 usage(basename(argv[0])); | |
77 exit(0); | |
78 case 'f': | |
79 gSensorInputFrequency = atoi(optarg); | |
80 if(gSensorInputFrequency < 0 || gSensorInputFrequency > 7) { | |
81 usage(basename(argv[0])); | |
82 exit(0); | |
83 } | |
84 break; | |
85 case 'a': | |
86 gSensorInputAmplitude = atoi(optarg); | |
87 if(gSensorInputAmplitude < 0 || gSensorInputAmplitude > 7) { | |
88 usage(basename(argv[0])); | |
89 exit(0); | |
90 } | |
91 break; | |
92 case '?': | |
93 default: | |
94 usage(basename(argv[0])); | |
95 exit(1); | |
96 } | |
97 } | |
98 | |
99 // Initialise the PRU audio device | |
100 if(Bela_initAudio(&settings, 0) != 0) { | |
101 cout << "Error: unable to initialise audio" << endl; | |
102 return -1; | |
103 } | |
104 | |
105 if(settings.verbose) { | |
106 cout << "--> Frequency on input " << gSensorInputFrequency << endl; | |
107 cout << "--> Amplitude on input " << gSensorInputAmplitude << endl; | |
108 } | |
109 | |
110 // Start the audio device running | |
111 if(Bela_startAudio()) { | |
112 cout << "Error: unable to start real-time audio" << endl; | |
113 return -1; | |
114 } | |
115 | |
116 // Set up interrupt handler to catch Control-C and SIGTERM | |
117 signal(SIGINT, interrupt_handler); | |
118 signal(SIGTERM, interrupt_handler); | |
119 | |
120 // Run until told to stop | |
121 while(!gShouldStop) { | |
122 usleep(100000); | |
123 } | |
124 | |
125 // Stop the audio device | |
126 Bela_stopAudio(); | |
127 | |
128 // Clean up any resources allocated for audio | |
129 Bela_cleanupAudio(); | |
130 | |
131 // All done! | |
132 return 0; | |
133 } |