comparison projects/measure_noisefloor/main.cpp @ 89:d41631e0fe0e

Added noise floor measurement project; also added option to run script to run without screen
author andrewm
date Sun, 19 Jul 2015 16:15:28 +0100
parents
children
comparison
equal deleted inserted replaced
88:3a5823f7a11f 89:d41631e0fe0e
1 /*
2 * main.cpp
3 *
4 * Created on: Oct 24, 2014
5 * Author: parallels
6 */
7
8 #include <iostream>
9 #include <cstdlib>
10 #include <libgen.h>
11 #include <signal.h>
12 #include <getopt.h>
13 #include <BeagleRT.h>
14
15 extern int gBufferSize;
16
17 using namespace std;
18
19 // Handle Ctrl-C by requesting that the audio rendering stop
20 void interrupt_handler(int var)
21 {
22 gShouldStop = true;
23 }
24
25 // Print usage information
26 void usage(const char * processName)
27 {
28 cerr << "Usage: " << processName << " [options]" << endl;
29
30 BeagleRT_usage();
31
32 cerr << " --buffer-size [-b] size Set the analysis buffer size\n";
33 cerr << " --help [-h]: Print this menu\n";
34 }
35
36 int main(int argc, char *argv[])
37 {
38 BeagleRTInitSettings settings; // Standard audio settings
39
40 struct option customOptions[] =
41 {
42 {"help", 0, NULL, 'h'},
43 {"buffer-size", 1, NULL, 'b'},
44 {NULL, 0, NULL, 0}
45 };
46
47 // Set default settings
48 BeagleRT_defaultSettings(&settings);
49
50 // By default use a longer period size because latency is not an issue
51 settings.periodSize = 32;
52
53 // Parse command-line arguments
54 while (1) {
55 int c;
56 if ((c = BeagleRT_getopt_long(argc, argv, "hb:", customOptions, &settings)) < 0)
57 break;
58 switch (c) {
59 case 'b':
60 gBufferSize = atoi(optarg);
61 break;
62 case 'h':
63 usage(basename(argv[0]));
64 exit(0);
65 case '?':
66 default:
67 usage(basename(argv[0]));
68 exit(1);
69 }
70 }
71
72 if(gBufferSize < settings.periodSize)
73 gBufferSize = settings.periodSize;
74
75 // Initialise the PRU audio device
76 if(BeagleRT_initAudio(&settings, 0) != 0) {
77 cout << "Error: unable to initialise audio" << endl;
78 return -1;
79 }
80
81 // Start the audio device running
82 if(BeagleRT_startAudio()) {
83 cout << "Error: unable to start real-time audio" << endl;
84 return -1;
85 }
86
87 // Set up interrupt handler to catch Control-C and SIGTERM
88 signal(SIGINT, interrupt_handler);
89 signal(SIGTERM, interrupt_handler);
90
91 // Run until told to stop
92 while(!gShouldStop) {
93 usleep(100000);
94 }
95
96 // Stop the audio device
97 BeagleRT_stopAudio();
98
99 // Clean up any resources allocated for audio
100 BeagleRT_cleanupAudio();
101
102 // All done!
103 return 0;
104 }