comparison projects/audio_in_FFT/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 f34c63568523
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 << " -s fftsize: Set the fSize of the FFT, in samples\n"; 30 cerr << " --fftsize [-s] size: Set the fSize of the FFT, in samples\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 int fftSize = 64; // Size of the FFT, in samples 37 int fftSize = 64; // Size of the FFT, in samples
38 int useMatrix = 0; // Whether to use the matrix or just audio 38
39 struct option customOptions[] =
40 {
41 {"help", 0, NULL, 'h'},
42 {"fftsize", 1, NULL, 's'},
43 {NULL, 0, NULL, 0}
44 };
45
46 // Set default settings
47 BeagleRT_defaultSettings(&settings);
48
49 settings.useMatrix = 0; // No matrix usage by default
39 50
40 // Parse command-line arguments 51 // Parse command-line arguments
41 while (1) { 52 while (1) {
42 int c; 53 int c;
43 if ((c = getopt(argc, argv, "hp:vf:m")) < 0) 54 if ((c = BeagleRT_getopt_long(argc, argv, "hs:", customOptions, &settings)) < 0)
44 break; 55 break;
45 switch (c) { 56 switch (c) {
46 case 'h': 57 case 'h':
47 usage(basename(argv[0])); 58 usage(basename(argv[0]));
48 exit(0); 59 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 's': 60 case 's':
58 fftSize = atof(optarg); 61 fftSize = atof(optarg);
59 break;
60 case 'm':
61 useMatrix = 1;
62 break; 62 break;
63 case '?': 63 case '?':
64 default: 64 default:
65 usage(basename(argv[0])); 65 usage(basename(argv[0]));
66 exit(1); 66 exit(1);
67 } 67 }
68 } 68 }
69 69
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 FFT size " << fftSize << endl;
76 if(useMatrix)
77 cout << "Matrix enabled\n";
78 else
79 cout << "Matrix disabled\n";
80 }
81
82 // Initialise the PRU audio device 70 // Initialise the PRU audio device
83 if(initAudio(periodSize, useMatrix, &fftSize) != 0) { 71 if(BeagleRT_initAudio(&settings, &fftSize) != 0) {
84 cout << "Error: unable to initialise audio" << endl; 72 cout << "Error: unable to initialise audio" << endl;
85 return -1; 73 return -1;
86 } 74 }
87 75
88 // Start the audio device running 76 // Start the audio device running
89 if(startAudio()) { 77 if(BeagleRT_startAudio()) {
90 cout << "Error: unable to start real-time audio" << endl; 78 cout << "Error: unable to start real-time audio" << endl;
91 return -1; 79 return -1;
92 } 80 }
93 81
94 // Set up interrupt handler to catch Control-C 82 // Set up interrupt handler to catch Control-C
98 while(!gShouldStop) { 86 while(!gShouldStop) {
99 usleep(100000); 87 usleep(100000);
100 } 88 }
101 89
102 // Stop the audio device 90 // Stop the audio device
103 stopAudio(); 91 BeagleRT_stopAudio();
104
105 if(verbose) {
106 cout << "Cleaning up..." << endl;
107 }
108 92
109 // Clean up any resources allocated for audio 93 // Clean up any resources allocated for audio
110 cleanupAudio(); 94 BeagleRT_cleanupAudio();
111 95
112 // All done! 96 // All done!
113 return 0; 97 return 0;
114 } 98 }
99