comparison examples/03-Analog/analog-output/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
25 #include <iostream>
26 #include <cstdlib>
27 #include <libgen.h>
28 #include <signal.h>
29 #include <getopt.h>
30 #include <Bela.h>
31
32 using namespace std;
33
34 // Handle Ctrl-C by requesting that the audio rendering stop
35 void interrupt_handler(int var)
36 {
37 gShouldStop = true;
38 }
39
40 // Print usage information
41 void usage(const char * processName)
42 {
43 cerr << "Usage: " << processName << " [options]" << endl;
44
45 Bela_usage();
46
47 cerr << " --frequency [-f] frequency: Set the frequency of the LED fade (default: 1.0)\n";
48 cerr << " --help [-h]: Print this menu\n";
49 }
50
51 int main(int argc, char *argv[])
52 {
53 BelaInitSettings settings; // Standard audio settings
54 float frequency = 1.0; // Frequency of LED fades
55
56 struct option customOptions[] =
57 {
58 {"help", 0, NULL, 'h'},
59 {"frequency", 1, NULL, 'f'},
60 {NULL, 0, NULL, 0}
61 };
62
63 // Set default settings
64 Bela_defaultSettings(&settings);
65
66 // In this example, audio isn't used so might as well leave speaker muted
67 settings.beginMuted = 1;
68
69 // Parse command-line arguments
70 while (1) {
71 int c;
72 if ((c = Bela_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
73 break;
74 switch (c) {
75 case 'h':
76 usage(basename(argv[0]));
77 exit(0);
78 case 'f':
79 frequency = atof(optarg);
80 if(frequency < 0)
81 frequency = 0;
82 if(frequency > 11025.0)
83 frequency = 11025.0;
84 break;
85 case '?':
86 default:
87 usage(basename(argv[0]));
88 exit(1);
89 }
90 }
91
92 // Initialise the PRU audio device
93 if(Bela_initAudio(&settings, &frequency) != 0) {
94 cout << "Error: unable to initialise audio" << endl;
95 return -1;
96 }
97
98 // Start the audio device running
99 if(Bela_startAudio()) {
100 cout << "Error: unable to start real-time audio" << endl;
101 return -1;
102 }
103
104 // Set up interrupt handler to catch Control-C and SIGTERM
105 signal(SIGINT, interrupt_handler);
106 signal(SIGTERM, interrupt_handler);
107
108 // Run until told to stop
109 while(!gShouldStop) {
110 usleep(100000);
111 }
112
113 // Stop the audio device
114 Bela_stopAudio();
115
116 // Clean up any resources allocated for audio
117 Bela_cleanupAudio();
118
119 // All done!
120 return 0;
121 }