victor@3
|
1 /*
|
victor@3
|
2 * main.cpp
|
victor@3
|
3 *
|
victor@3
|
4 * Created on: Oct 24, 2014
|
victor@3
|
5 * Author: Andrew McPherson and Victor Zappi
|
victor@3
|
6 */
|
victor@3
|
7
|
victor@3
|
8 #include <iostream>
|
victor@3
|
9 #include <cstdlib>
|
victor@3
|
10 #include <libgen.h>
|
victor@3
|
11 #include <signal.h>
|
victor@3
|
12 #include <string>
|
victor@3
|
13 #include <sndfile.h> // to load audio files
|
victor@3
|
14 #include "../../include/RTAudio.h"
|
victor@3
|
15 #include "SampleData.h"
|
victor@3
|
16
|
victor@3
|
17 using namespace std;
|
victor@3
|
18
|
victor@3
|
19 float gCutFreq = 100;
|
victor@3
|
20
|
victor@3
|
21 // Load samples from file
|
victor@3
|
22 int initFile(string file, SampleData *smp)//float *& smp)
|
victor@3
|
23 {
|
victor@3
|
24 SNDFILE *sndfile ;
|
victor@3
|
25 SF_INFO sfinfo ;
|
victor@3
|
26
|
victor@3
|
27 if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) {
|
victor@3
|
28 cout << "Couldn't open file " << file << endl;
|
victor@3
|
29 return 1;
|
victor@3
|
30 }
|
victor@3
|
31
|
victor@3
|
32 int numChan = sfinfo.channels;
|
victor@3
|
33 if(numChan != 1)
|
victor@3
|
34 {
|
victor@3
|
35 cout << "Error: " << file << " is not a mono file" << endl;
|
victor@3
|
36 return 1;
|
victor@3
|
37 }
|
victor@3
|
38
|
victor@3
|
39 smp->sampleLen = sfinfo.frames * numChan;
|
victor@3
|
40 smp->samples = new float[smp->sampleLen];
|
victor@3
|
41 if(smp == NULL){
|
victor@3
|
42 cout << "Could not allocate buffer" << endl;
|
victor@3
|
43 return 1;
|
victor@3
|
44 }
|
victor@3
|
45
|
victor@3
|
46 int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
|
victor@3
|
47 int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen);
|
victor@3
|
48
|
victor@3
|
49 // Pad with zeros in case we couldn't read whole file
|
victor@3
|
50 for(int k = readcount; k <smp->sampleLen; k++)
|
victor@3
|
51 smp->samples[k] = 0;
|
victor@3
|
52
|
victor@3
|
53 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) {
|
victor@3
|
54 double scale ;
|
victor@3
|
55 int m ;
|
victor@3
|
56
|
victor@3
|
57 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
|
victor@3
|
58 if (scale < 1e-10)
|
victor@3
|
59 scale = 1.0 ;
|
victor@3
|
60 else
|
victor@3
|
61 scale = 32700.0 / scale ;
|
victor@3
|
62 cout << "File samples scale = " << scale << endl;
|
victor@3
|
63
|
victor@3
|
64 for (m = 0; m < smp->sampleLen; m++)
|
victor@3
|
65 smp->samples[m] *= scale;
|
victor@3
|
66 }
|
victor@3
|
67
|
victor@3
|
68 sf_close(sndfile);
|
victor@3
|
69
|
victor@3
|
70 return 0;
|
victor@3
|
71 }
|
victor@3
|
72
|
victor@3
|
73
|
victor@3
|
74 // Handle Ctrl-C by requesting that the audio rendering stop
|
victor@3
|
75 void interrupt_handler(int var)
|
victor@3
|
76 {
|
victor@3
|
77 //rt_task_delete ((RT_TASK *) &gTriggerSamplesTask);
|
victor@3
|
78 gShouldStop = true;
|
victor@3
|
79 }
|
victor@3
|
80
|
victor@3
|
81 // Print usage information
|
victor@3
|
82 void usage(const char * processName)
|
victor@3
|
83 {
|
victor@3
|
84 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f frequency]" << endl;
|
victor@3
|
85 cerr << " -h: Print this menu\n";
|
victor@3
|
86 cerr << " -v: Enable verbose messages\n";
|
victor@3
|
87 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n";
|
victor@3
|
88 cerr << " -m: Enable the matrix (ADC and DAC) as well as audio\n";
|
victor@3
|
89 cerr << " -f filename: Name of the file to load (default is \"sample.wav\")\n";
|
victor@3
|
90 cerr << " -c freq: Set the cut off frequency of the filter in Hz\n";
|
victor@3
|
91 }
|
victor@3
|
92
|
victor@3
|
93 int main(int argc, char *argv[])
|
victor@3
|
94 {
|
victor@3
|
95 int verbose = 0; // Verbose printing level
|
victor@3
|
96 int periodSize = 8; // Period size in sensor frames
|
victor@3
|
97 int useMatrix = 0; // Whether to use the matrix or just audio
|
victor@3
|
98 string fileName; // Name of the sample to load
|
victor@3
|
99
|
victor@3
|
100 SampleData sampleData; // User define structure to pass data retrieved from file to render function
|
victor@3
|
101 sampleData.samples = 0;
|
victor@3
|
102 sampleData.sampleLen = -1;
|
victor@3
|
103
|
victor@3
|
104 // Parse command-line arguments
|
victor@3
|
105 while (1) {
|
victor@3
|
106 int c;
|
victor@3
|
107 if ((c = getopt(argc, argv, "hp:vms:")) < 0)
|
victor@3
|
108 break;
|
victor@3
|
109 switch (c) {
|
victor@3
|
110 case 'h':
|
victor@3
|
111 usage(basename(argv[0]));
|
victor@3
|
112 exit(0);
|
victor@3
|
113 case 'p':
|
victor@3
|
114 periodSize = atoi(optarg);
|
victor@3
|
115 if(periodSize < 1)
|
victor@3
|
116 periodSize = 1;
|
victor@3
|
117 break;
|
victor@3
|
118 case 'v':
|
victor@3
|
119 verbose = 1;
|
victor@3
|
120 break;
|
victor@3
|
121 case 'm':
|
victor@3
|
122 useMatrix = 1;
|
victor@3
|
123 break;
|
victor@3
|
124 case 'f':
|
victor@3
|
125 fileName = string((char *)optarg);
|
victor@3
|
126 break;
|
victor@3
|
127 case 'c':
|
victor@3
|
128 gCutFreq = atof(optarg);
|
victor@3
|
129 break;
|
victor@3
|
130 case '?':
|
victor@3
|
131 default:
|
victor@3
|
132 usage(basename(argv[0]));
|
victor@3
|
133 exit(1);
|
victor@3
|
134 }
|
victor@3
|
135 }
|
victor@3
|
136
|
victor@3
|
137 if(fileName.empty()){
|
victor@3
|
138 fileName = "filter/longsample.wav";
|
victor@3
|
139 }
|
victor@3
|
140
|
victor@3
|
141 // Set verbose logging information (optional by using value > 0; default is 0)
|
victor@3
|
142 setVerboseLevel(verbose);
|
victor@3
|
143
|
victor@3
|
144 if(verbose) {
|
victor@3
|
145 cout << "Starting with period size " << periodSize << endl;
|
victor@3
|
146 if(useMatrix)
|
victor@3
|
147 cout << "Matrix enabled\n";
|
victor@3
|
148 else
|
victor@3
|
149 cout << "Matrix disabled\n";
|
victor@3
|
150 cout << "Loading file " << fileName << endl;
|
victor@3
|
151 }
|
victor@3
|
152
|
victor@3
|
153 // Load file
|
victor@3
|
154 if(initFile(fileName, &sampleData) != 0)
|
victor@3
|
155 {
|
victor@3
|
156 cout << "Error: unable to load samples " << endl;
|
victor@3
|
157 return -1;
|
victor@3
|
158 }
|
victor@3
|
159
|
victor@3
|
160 if(verbose)
|
victor@3
|
161 cout << "File contains " << sampleData.sampleLen << " samples" << endl;
|
victor@3
|
162
|
victor@3
|
163 // Initialise the PRU audio device
|
victor@3
|
164 if(initAudio(periodSize, useMatrix, &sampleData) != 0) {
|
victor@3
|
165 cout << "Error: unable to initialise audio" << endl;
|
victor@3
|
166 return -1;
|
victor@3
|
167 }
|
victor@3
|
168
|
victor@3
|
169 // Start the audio device running
|
victor@3
|
170 if(startAudio()) {
|
victor@3
|
171 cout << "Error: unable to start real-time audio" << endl;
|
victor@3
|
172 return -1;
|
victor@3
|
173 }
|
victor@3
|
174
|
victor@3
|
175 // Set up interrupt handler to catch Control-C
|
victor@3
|
176 signal(SIGINT, interrupt_handler);
|
victor@3
|
177
|
victor@3
|
178 // Run until told to stop
|
victor@3
|
179 while(!gShouldStop) {
|
victor@3
|
180 usleep(100000);
|
victor@3
|
181 }
|
victor@3
|
182
|
victor@3
|
183 // Stop the audio device
|
victor@3
|
184 stopAudio();
|
victor@3
|
185
|
victor@3
|
186 if(verbose) {
|
victor@3
|
187 cout << "Cleaning up..." << endl;
|
victor@3
|
188 }
|
victor@3
|
189
|
victor@3
|
190 // Clean up any resources allocated for audio
|
victor@3
|
191 cleanupAudio();
|
victor@3
|
192
|
victor@3
|
193 // All done!
|
victor@3
|
194 return 0;
|
victor@3
|
195 }
|