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