giuliomoro@18
|
1 /*
|
giuliomoro@18
|
2 * assignment1_crossover
|
giuliomoro@18
|
3 * RTDSP 2015
|
giuliomoro@18
|
4 *
|
giuliomoro@18
|
5 * First assignment for ECS732 RTDSP, to implement a 2-way audio crossover
|
giuliomoro@18
|
6 * using the BeagleBone Black.
|
giuliomoro@18
|
7 *
|
giuliomoro@18
|
8 * Andrew McPherson and Victor Zappi
|
giuliomoro@18
|
9 * Queen Mary, University of London
|
giuliomoro@18
|
10 */
|
giuliomoro@18
|
11
|
giuliomoro@18
|
12 #include <iostream>
|
giuliomoro@18
|
13 #include <cstdlib>
|
giuliomoro@18
|
14 #include <libgen.h>
|
giuliomoro@18
|
15 #include <signal.h>
|
giuliomoro@18
|
16 #include <getopt.h>
|
giuliomoro@18
|
17 #include "../include/RTAudio.h"
|
giuliomoro@18
|
18 #include <unistd.h>
|
giuliomoro@18
|
19 #include <stdlib.h>
|
giuliomoro@18
|
20 #include <fcntl.h>
|
giuliomoro@18
|
21
|
giuliomoro@18
|
22 using namespace std;
|
giuliomoro@18
|
23
|
giuliomoro@18
|
24 // Handle Ctrl-C by requesting that the audio rendering stop
|
giuliomoro@18
|
25 void interrupt_handler(int var)
|
giuliomoro@18
|
26 {
|
giuliomoro@18
|
27 gShouldStop = true;
|
giuliomoro@18
|
28 }
|
giuliomoro@18
|
29
|
giuliomoro@18
|
30 // Print usage information
|
giuliomoro@18
|
31 void usage(const char * processName)
|
giuliomoro@18
|
32 {
|
giuliomoro@18
|
33 cerr << "Usage: " << processName << " [options]" << endl;
|
giuliomoro@18
|
34
|
giuliomoro@18
|
35 BeagleRT_usage();
|
giuliomoro@18
|
36
|
giuliomoro@18
|
37 cerr << " --help [-h]: Print this menu\n";
|
giuliomoro@18
|
38 }
|
giuliomoro@18
|
39
|
giuliomoro@18
|
40 int main(int argc, char *argv[])
|
giuliomoro@18
|
41 {
|
giuliomoro@18
|
42 RTAudioSettings settings; // Standard audio settings
|
giuliomoro@18
|
43 float frequency = 1000.0; // Frequency of crossover
|
giuliomoro@18
|
44
|
giuliomoro@18
|
45 struct option customOptions[] =
|
giuliomoro@18
|
46 {
|
giuliomoro@18
|
47 {"help", 0, NULL, 'h'},
|
giuliomoro@18
|
48 {"frequency", 1, NULL, 'f'},
|
giuliomoro@18
|
49 {NULL, 0, NULL, 0}
|
giuliomoro@18
|
50 };
|
giuliomoro@18
|
51
|
giuliomoro@18
|
52 // Set default settings
|
giuliomoro@18
|
53 BeagleRT_defaultSettings(&settings);
|
giuliomoro@18
|
54
|
giuliomoro@18
|
55 // Parse command-line arguments
|
giuliomoro@18
|
56 while (1) {
|
giuliomoro@18
|
57 int c;
|
giuliomoro@18
|
58 if ((c = BeagleRT_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
|
giuliomoro@18
|
59 break;
|
giuliomoro@18
|
60 switch (c) {
|
giuliomoro@18
|
61 case 'h':
|
giuliomoro@18
|
62 usage(basename(argv[0]));
|
giuliomoro@18
|
63 exit(0);
|
giuliomoro@18
|
64 case 'f':
|
giuliomoro@18
|
65 frequency = atof(optarg);
|
giuliomoro@18
|
66 if(frequency < 20.0)
|
giuliomoro@18
|
67 frequency = 20.0;
|
giuliomoro@18
|
68 if(frequency > 5000.0)
|
giuliomoro@18
|
69 frequency = 5000.0;
|
giuliomoro@18
|
70 break;
|
giuliomoro@18
|
71 case '?':
|
giuliomoro@18
|
72 default:
|
giuliomoro@18
|
73 usage(basename(argv[0]));
|
giuliomoro@18
|
74 exit(1);
|
giuliomoro@18
|
75 }
|
giuliomoro@18
|
76 }
|
giuliomoro@18
|
77
|
giuliomoro@18
|
78 // Initialise the PRU audio device
|
giuliomoro@18
|
79 if(BeagleRT_initAudio(&settings, &frequency) != 0) {
|
giuliomoro@18
|
80 cout << "Error: unable to initialise audio" << endl;
|
giuliomoro@18
|
81 return -1;
|
giuliomoro@18
|
82 }
|
giuliomoro@18
|
83
|
giuliomoro@18
|
84 // Start the audio device running
|
giuliomoro@18
|
85 if(BeagleRT_startAudio()) {
|
giuliomoro@18
|
86 cout << "Error: unable to start real-time audio" << endl;
|
giuliomoro@18
|
87 return -1;
|
giuliomoro@18
|
88 }
|
giuliomoro@18
|
89
|
giuliomoro@18
|
90 // Set up interrupt handler to catch Control-C
|
giuliomoro@18
|
91 signal(SIGINT, interrupt_handler);
|
giuliomoro@18
|
92 signal(SIGTERM, interrupt_handler);
|
giuliomoro@18
|
93
|
giuliomoro@18
|
94 // Run until told to stop
|
giuliomoro@18
|
95 while(!gShouldStop) {
|
giuliomoro@18
|
96 usleep(100000);
|
giuliomoro@18
|
97 }
|
giuliomoro@18
|
98
|
giuliomoro@18
|
99 // Stop the audio device
|
giuliomoro@18
|
100 BeagleRT_stopAudio();
|
giuliomoro@18
|
101
|
giuliomoro@18
|
102 // Clean up any resources allocated for audio
|
giuliomoro@18
|
103 BeagleRT_cleanupAudio();
|
giuliomoro@18
|
104
|
giuliomoro@18
|
105 // All done!
|
giuliomoro@18
|
106 return 0;
|
giuliomoro@18
|
107 }
|