andrewm@0
|
1 /*
|
andrewm@0
|
2 * main.cpp
|
andrewm@0
|
3 *
|
andrewm@0
|
4 * Created on: Oct 24, 2014
|
andrewm@0
|
5 * Author: parallels
|
andrewm@0
|
6 */
|
andrewm@0
|
7
|
andrewm@0
|
8 #include <iostream>
|
andrewm@0
|
9 #include <cstdlib>
|
andrewm@0
|
10 #include <libgen.h>
|
andrewm@0
|
11 #include <signal.h>
|
andrewm@0
|
12 #include "../../include/RTAudio.h"
|
andrewm@0
|
13
|
andrewm@0
|
14 using namespace std;
|
andrewm@0
|
15
|
andrewm@0
|
16 int gNumOscillators = 32;
|
andrewm@0
|
17 int gWavetableLength = 1024;
|
andrewm@0
|
18
|
andrewm@0
|
19
|
andrewm@0
|
20 // Handle Ctrl-C by requesting that the audio rendering stop
|
andrewm@0
|
21 void interrupt_handler(int var)
|
andrewm@0
|
22 {
|
andrewm@0
|
23 gShouldStop = true;
|
andrewm@0
|
24 }
|
andrewm@0
|
25
|
andrewm@0
|
26 // Print usage information
|
andrewm@0
|
27 void usage(const char * processName)
|
andrewm@0
|
28 {
|
andrewm@0
|
29 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f input] [-a input]" << endl;
|
andrewm@0
|
30 cerr << " -h: Print this menu\n";
|
andrewm@0
|
31 cerr << " -v: Enable verbose messages\n";
|
andrewm@0
|
32 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n";
|
andrewm@0
|
33 cerr << " -n oscs: Set the number of oscillators to use (default: 32)\n";
|
andrewm@0
|
34 cerr << " -w length: Set the wavetable length in samples (default: 1024)\n";
|
andrewm@0
|
35 cerr << " -m: Enable the matrix (ADC and DAC) for controlling parameters\n";
|
andrewm@0
|
36 }
|
andrewm@0
|
37
|
andrewm@0
|
38 int main(int argc, char *argv[])
|
andrewm@0
|
39 {
|
andrewm@0
|
40 int periodSize = 8; // Period size in sensor frames
|
andrewm@0
|
41 int verbose = 0; // Verbose printing level
|
andrewm@0
|
42 int useMatrix = 0;
|
andrewm@0
|
43
|
andrewm@0
|
44 // Parse command-line arguments
|
andrewm@0
|
45 while (1) {
|
andrewm@0
|
46 int c;
|
andrewm@0
|
47 if ((c = getopt(argc, argv, "hp:vn:w:m")) < 0)
|
andrewm@0
|
48 break;
|
andrewm@0
|
49 switch (c) {
|
andrewm@0
|
50 case 'h':
|
andrewm@0
|
51 usage(basename(argv[0]));
|
andrewm@0
|
52 exit(0);
|
andrewm@0
|
53 case 'p':
|
andrewm@0
|
54 periodSize = atoi(optarg);
|
andrewm@0
|
55 if(periodSize < 1)
|
andrewm@0
|
56 periodSize = 1;
|
andrewm@0
|
57 break;
|
andrewm@0
|
58 case 'v':
|
andrewm@0
|
59 verbose = 1;
|
andrewm@0
|
60 break;
|
andrewm@0
|
61 case 'n':
|
andrewm@0
|
62 gNumOscillators = atoi(optarg);
|
andrewm@0
|
63 if(gNumOscillators <= 0) {
|
andrewm@0
|
64 usage(basename(argv[0]));
|
andrewm@0
|
65 exit(0);
|
andrewm@0
|
66 }
|
andrewm@0
|
67 break;
|
andrewm@0
|
68 case 'w':
|
andrewm@0
|
69 gWavetableLength = atoi(optarg);
|
andrewm@0
|
70 if(gWavetableLength < 4)
|
andrewm@0
|
71 gWavetableLength = 4;
|
andrewm@0
|
72 if(gWavetableLength > 16384)
|
andrewm@0
|
73 gWavetableLength = 16384;
|
andrewm@0
|
74 break;
|
andrewm@0
|
75 case 'm':
|
andrewm@0
|
76 useMatrix = 1;
|
andrewm@0
|
77 break;
|
andrewm@0
|
78 case '?':
|
andrewm@0
|
79 default:
|
andrewm@0
|
80 usage(basename(argv[0]));
|
andrewm@0
|
81 exit(1);
|
andrewm@0
|
82 }
|
andrewm@0
|
83 }
|
andrewm@0
|
84
|
andrewm@0
|
85
|
andrewm@0
|
86 // Set verbose logging information (optional by using value > 0; default is 0)
|
andrewm@0
|
87 setVerboseLevel(verbose);
|
andrewm@0
|
88
|
andrewm@0
|
89 if(verbose) {
|
andrewm@0
|
90 cout << "Starting with period size " << periodSize << endl;
|
andrewm@0
|
91 cout << "--> Using " << gNumOscillators << " oscillators and wavetable of " << gWavetableLength << " samples\n";
|
andrewm@0
|
92 cout << "--> Matrix ";
|
andrewm@0
|
93 if(useMatrix) cout << "enabled\n";
|
andrewm@0
|
94 else cout << "disabled\n";
|
andrewm@0
|
95 }
|
andrewm@0
|
96
|
andrewm@0
|
97 // Initialise the PRU audio device
|
andrewm@0
|
98 if(initAudio(periodSize, useMatrix, 0) != 0) {
|
andrewm@0
|
99 cout << "Error: unable to initialise audio" << endl;
|
andrewm@0
|
100 return -1;
|
andrewm@0
|
101 }
|
andrewm@0
|
102
|
andrewm@0
|
103 // Start the audio device running
|
andrewm@0
|
104 if(startAudio()) {
|
andrewm@0
|
105 cout << "Error: unable to start real-time audio" << endl;
|
andrewm@0
|
106 return -1;
|
andrewm@0
|
107 }
|
andrewm@0
|
108
|
andrewm@0
|
109 // Set up interrupt handler to catch Control-C
|
andrewm@0
|
110 signal(SIGINT, interrupt_handler);
|
andrewm@0
|
111
|
andrewm@0
|
112 // Run until told to stop
|
andrewm@0
|
113 while(!gShouldStop) {
|
andrewm@0
|
114 usleep(100000);
|
andrewm@0
|
115 }
|
andrewm@0
|
116
|
andrewm@0
|
117 // Stop the audio device
|
andrewm@0
|
118 stopAudio();
|
andrewm@0
|
119
|
andrewm@0
|
120 if(verbose) {
|
andrewm@0
|
121 cout << "Cleaning up..." << endl;
|
andrewm@0
|
122 }
|
andrewm@0
|
123
|
andrewm@0
|
124 // Clean up any resources allocated for audio
|
andrewm@0
|
125 cleanupAudio();
|
andrewm@0
|
126
|
andrewm@0
|
127 // All done!
|
andrewm@0
|
128 return 0;
|
andrewm@0
|
129 }
|