Mercurial > hg > beaglert
comparison projects/basic/main.cpp @ 0:8a575ba3ab52
Initial commit.
author | andrewm |
---|---|
date | Fri, 31 Oct 2014 19:10:17 +0100 |
parents | |
children | 09f03ac40fcc |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:8a575ba3ab52 |
---|---|
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 "../../include/RTAudio.h" | |
13 | |
14 using namespace std; | |
15 | |
16 // Handle Ctrl-C by requesting that the audio rendering stop | |
17 void interrupt_handler(int var) | |
18 { | |
19 gShouldStop = true; | |
20 } | |
21 | |
22 // Print usage information | |
23 void usage(const char * processName) | |
24 { | |
25 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f frequency]" << endl; | |
26 cerr << " -h: Print this menu\n"; | |
27 cerr << " -v: Enable verbose messages\n"; | |
28 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n"; | |
29 cerr << " -f frequency: Set the frequency of the oscillator\n"; | |
30 cerr << " -m: Enable the matrix (ADC and DAC) as well as audio\n"; | |
31 } | |
32 | |
33 int main(int argc, char *argv[]) | |
34 { | |
35 int periodSize = 8; // Period size in sensor frames | |
36 int verbose = 0; // Verbose printing level | |
37 float frequency = 440.0; // Frequency of oscillator | |
38 int useMatrix = 0; // Whether to use the matrix or just audio | |
39 | |
40 // Parse command-line arguments | |
41 while (1) { | |
42 int c; | |
43 if ((c = getopt(argc, argv, "hp:vf:m")) < 0) | |
44 break; | |
45 switch (c) { | |
46 case 'h': | |
47 usage(basename(argv[0])); | |
48 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 frequency = atof(optarg); | |
59 break; | |
60 case 'm': | |
61 useMatrix = 1; | |
62 break; | |
63 case '?': | |
64 default: | |
65 usage(basename(argv[0])); | |
66 exit(1); | |
67 } | |
68 } | |
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 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 | |
83 if(initAudio(periodSize, useMatrix, &frequency) != 0) { | |
84 cout << "Error: unable to initialise audio" << endl; | |
85 return -1; | |
86 } | |
87 | |
88 // Start the audio device running | |
89 if(startAudio()) { | |
90 cout << "Error: unable to start real-time audio" << endl; | |
91 return -1; | |
92 } | |
93 | |
94 // Set up interrupt handler to catch Control-C | |
95 signal(SIGINT, interrupt_handler); | |
96 | |
97 // Run until told to stop | |
98 while(!gShouldStop) { | |
99 usleep(100000); | |
100 } | |
101 | |
102 // Stop the audio device | |
103 stopAudio(); | |
104 | |
105 if(verbose) { | |
106 cout << "Cleaning up..." << endl; | |
107 } | |
108 | |
109 // Clean up any resources allocated for audio | |
110 cleanupAudio(); | |
111 | |
112 // All done! | |
113 return 0; | |
114 } |