comparison projects/basic_analog_output/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 // Handle Ctrl-C by requesting that the audio rendering stop 17 // Handle Ctrl-C by requesting that the audio rendering stop
20 } 21 }
21 22
22 // Print usage information 23 // Print usage information
23 void usage(const char * processName) 24 void usage(const char * processName)
24 { 25 {
25 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f input] [-a input]" << endl; 26 cerr << "Usage: " << processName << " [options]" << endl;
26 cerr << " -h: Print this menu\n"; 27
27 cerr << " -v: Enable verbose messages\n"; 28 BeagleRT_usage();
28 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n"; 29
29 cerr << " -f frequency: Set frequency of LED fade (default: 1.0)\n"; 30 cerr << " --frequency [-f] frequency: Set the frequency of the LED fade (default: 1.0)\n";
31 cerr << " --help [-h]: Print this menu\n";
30 } 32 }
31 33
32 int main(int argc, char *argv[]) 34 int main(int argc, char *argv[])
33 { 35 {
34 int periodSize = 8; // Period size in sensor frames 36 RTAudioSettings settings; // Standard audio settings
35 int verbose = 0; // Verbose printing level 37 float frequency = 1.0; // Frequency of LED fades
36 float frequency = 1.0; // Frequency of LED fades 38
39 struct option customOptions[] =
40 {
41 {"help", 0, NULL, 'h'},
42 {"frequency", 1, NULL, 'f'},
43 {NULL, 0, NULL, 0}
44 };
45
46 // Set default settings
47 BeagleRT_defaultSettings(&settings);
48
49 // In this example, audio isn't used so might as well leave speaker muted
50 settings.beginMuted = 1;
37 51
38 // Parse command-line arguments 52 // Parse command-line arguments
39 while (1) { 53 while (1) {
40 int c; 54 int c;
41 if ((c = getopt(argc, argv, "hp:vf:")) < 0) 55 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
42 break; 56 break;
43 switch (c) { 57 switch (c) {
44 case 'h': 58 case 'h':
45 usage(basename(argv[0])); 59 usage(basename(argv[0]));
46 exit(0); 60 exit(0);
47 case 'p':
48 periodSize = atoi(optarg);
49 if(periodSize < 1)
50 periodSize = 1;
51 break;
52 case 'v':
53 verbose = 1;
54 break;
55 case 'f': 61 case 'f':
56 frequency = atof(optarg); 62 frequency = atof(optarg);
57 if(frequency < 0) 63 if(frequency < 0)
58 frequency = 0; 64 frequency = 0;
59 if(frequency > 11025.0) 65 if(frequency > 11025.0)
64 usage(basename(argv[0])); 70 usage(basename(argv[0]));
65 exit(1); 71 exit(1);
66 } 72 }
67 } 73 }
68 74
69
70 // Set verbose logging information (optional by using value > 0; default is 0)
71 setVerboseLevel(verbose);
72
73 if(verbose) {
74 cout << "Starting with period size " << periodSize << " and frequency " << frequency << endl;
75 }
76
77 // Initialise the PRU audio device 75 // Initialise the PRU audio device
78 if(initAudio(periodSize, 1, &frequency) != 0) { 76 if(BeagleRT_initAudio(&settings, &frequency) != 0) {
79 cout << "Error: unable to initialise audio" << endl; 77 cout << "Error: unable to initialise audio" << endl;
80 return -1; 78 return -1;
81 } 79 }
82 80
83 // Start the audio device running 81 // Start the audio device running
84 if(startAudio()) { 82 if(BeagleRT_startAudio()) {
85 cout << "Error: unable to start real-time audio" << endl; 83 cout << "Error: unable to start real-time audio" << endl;
86 return -1; 84 return -1;
87 } 85 }
88 86
89 // Set up interrupt handler to catch Control-C 87 // Set up interrupt handler to catch Control-C
93 while(!gShouldStop) { 91 while(!gShouldStop) {
94 usleep(100000); 92 usleep(100000);
95 } 93 }
96 94
97 // Stop the audio device 95 // Stop the audio device
98 stopAudio(); 96 BeagleRT_stopAudio();
99
100 if(verbose) {
101 cout << "Cleaning up..." << endl;
102 }
103 97
104 // Clean up any resources allocated for audio 98 // Clean up any resources allocated for audio
105 cleanupAudio(); 99 BeagleRT_cleanupAudio();
106 100
107 // All done! 101 // All done!
108 return 0; 102 return 0;
109 } 103 }