comparison projects/basic/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 frequency]" << 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 the frequency of the oscillator\n"; 30 cerr << " --frequency [-f] frequency: Set the frequency of the oscillator\n";
30 cerr << " -m: Enable the matrix (ADC and DAC) as well as audio\n"; 31 cerr << " --help [-h]: Print this menu\n";
31 } 32 }
32 33
33 int main(int argc, char *argv[]) 34 int main(int argc, char *argv[])
34 { 35 {
35 int periodSize = 8; // Period size in sensor frames 36 RTAudioSettings settings; // Standard audio settings
36 int verbose = 0; // Verbose printing level
37 float frequency = 440.0; // Frequency of oscillator 37 float frequency = 440.0; // Frequency of oscillator
38 int useMatrix = 0; // Whether to use the matrix or just audio 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);
39 48
40 // Parse command-line arguments 49 // Parse command-line arguments
41 while (1) { 50 while (1) {
42 int c; 51 int c;
43 if ((c = getopt(argc, argv, "hp:vf:m")) < 0) 52 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
44 break; 53 break;
45 switch (c) { 54 switch (c) {
46 case 'h': 55 case 'h':
47 usage(basename(argv[0])); 56 usage(basename(argv[0]));
48 exit(0); 57 exit(0);
49 case 'p':
50 periodSize = atoi(optarg);
51 if(periodSize < 1)
52 periodSize = 1;
53 break;
54 case 'v':
55 verbose = 1;
56 break;
57 case 'f': 58 case 'f':
58 frequency = atof(optarg); 59 frequency = atof(optarg);
59 break;
60 case 'm':
61 useMatrix = 1;
62 break; 60 break;
63 case '?': 61 case '?':
64 default: 62 default:
65 usage(basename(argv[0])); 63 usage(basename(argv[0]));
66 exit(1); 64 exit(1);
67 } 65 }
68 } 66 }
69 67
70
71 // Set verbose logging information (optional by using value > 0; default is 0)
72 setVerboseLevel(verbose);
73
74 if(verbose) {
75 cout << "Starting with period size " << periodSize << " and frequency " << frequency << endl;
76 if(useMatrix)
77 cout << "Matrix enabled\n";
78 else
79 cout << "Matrix disabled\n";
80 }
81
82 // Initialise the PRU audio device 68 // Initialise the PRU audio device
83 if(initAudio(periodSize, useMatrix, &frequency) != 0) { 69 if(BeagleRT_initAudio(&settings, &frequency) != 0) {
84 cout << "Error: unable to initialise audio" << endl; 70 cout << "Error: unable to initialise audio" << endl;
85 return -1; 71 return -1;
86 } 72 }
87 73
88 // Start the audio device running 74 // Start the audio device running
89 if(startAudio()) { 75 if(BeagleRT_startAudio()) {
90 cout << "Error: unable to start real-time audio" << endl; 76 cout << "Error: unable to start real-time audio" << endl;
91 return -1; 77 return -1;
92 } 78 }
93 79
94 // Set up interrupt handler to catch Control-C 80 // Set up interrupt handler to catch Control-C
98 while(!gShouldStop) { 84 while(!gShouldStop) {
99 usleep(100000); 85 usleep(100000);
100 } 86 }
101 87
102 // Stop the audio device 88 // Stop the audio device
103 stopAudio(); 89 BeagleRT_stopAudio();
104
105 if(verbose) {
106 cout << "Cleaning up..." << endl;
107 }
108 90
109 // Clean up any resources allocated for audio 91 // Clean up any resources allocated for audio
110 cleanupAudio(); 92 BeagleRT_cleanupAudio();
111 93
112 // All done! 94 // All done!
113 return 0; 95 return 0;
114 } 96 }