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