robert@464
|
1 /*
|
robert@464
|
2 ____ _____ _ _
|
robert@464
|
3 | __ )| ____| | / \
|
robert@464
|
4 | _ \| _| | | / _ \
|
robert@464
|
5 | |_) | |___| |___ / ___ \
|
robert@464
|
6 |____/|_____|_____/_/ \_\
|
robert@464
|
7
|
robert@464
|
8 The platform for ultra-low latency audio and sensor processing
|
robert@464
|
9
|
robert@464
|
10 http://bela.io
|
robert@464
|
11
|
robert@464
|
12 A project of the Augmented Instruments Laboratory within the
|
robert@464
|
13 Centre for Digital Music at Queen Mary University of London.
|
robert@464
|
14 http://www.eecs.qmul.ac.uk/~andrewm
|
robert@464
|
15
|
robert@464
|
16 (c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
|
robert@464
|
17 Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
|
robert@464
|
18 Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
|
robert@464
|
19
|
robert@464
|
20 The Bela software is distributed under the GNU Lesser General Public License
|
robert@464
|
21 (LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
|
robert@464
|
22 */
|
robert@464
|
23
|
robert@464
|
24 #include <iostream>
|
robert@464
|
25 #include <cstdlib>
|
robert@464
|
26 #include <cstdio>
|
robert@464
|
27 #include <libgen.h>
|
robert@464
|
28 #include <signal.h>
|
robert@464
|
29 #include <getopt.h>
|
robert@464
|
30 #include <unistd.h>
|
robert@464
|
31 #include <sys/time.h>
|
robert@464
|
32 #include <sndfile.h> // to load audio files
|
robert@464
|
33 #include "SampleData.h"
|
robert@464
|
34 #include <Bela.h>
|
robert@464
|
35
|
robert@464
|
36 using namespace std;
|
robert@464
|
37
|
robert@464
|
38 // Global variables used by getCurrentTime()
|
robert@464
|
39 unsigned long long gFirstSeconds, gFirstMicroseconds;
|
robert@464
|
40
|
robert@464
|
41 // Load samples from file
|
robert@464
|
42 int initFile(string file, SampleData *smp)//float *& smp)
|
robert@464
|
43 {
|
robert@464
|
44 SNDFILE *sndfile ;
|
robert@464
|
45 SF_INFO sfinfo ;
|
robert@464
|
46
|
robert@464
|
47 if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) {
|
robert@464
|
48 cout << "Couldn't open file " << file << endl;
|
robert@464
|
49 return 1;
|
robert@464
|
50 }
|
robert@464
|
51
|
robert@464
|
52 int numChan = sfinfo.channels;
|
robert@464
|
53 if(numChan != 1)
|
robert@464
|
54 {
|
robert@464
|
55 cout << "Error: " << file << " is not a mono file" << endl;
|
robert@464
|
56 return 1;
|
robert@464
|
57 }
|
robert@464
|
58
|
robert@464
|
59 smp->sampleLen = sfinfo.frames * numChan;
|
robert@464
|
60 smp->samples = new float[smp->sampleLen];
|
robert@464
|
61 if(smp == NULL){
|
robert@464
|
62 cout << "Could not allocate buffer" << endl;
|
robert@464
|
63 return 1;
|
robert@464
|
64 }
|
robert@464
|
65
|
robert@464
|
66 int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
|
robert@464
|
67 int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen);
|
robert@464
|
68
|
robert@464
|
69 // Pad with zeros in case we couldn't read whole file
|
robert@464
|
70 for(int k = readcount; k <smp->sampleLen; k++)
|
robert@464
|
71 smp->samples[k] = 0;
|
robert@464
|
72
|
robert@464
|
73 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) {
|
robert@464
|
74 double scale ;
|
robert@464
|
75 int m ;
|
robert@464
|
76
|
robert@464
|
77 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
|
robert@464
|
78 if (scale < 1e-10)
|
robert@464
|
79 scale = 1.0 ;
|
robert@464
|
80 else
|
robert@464
|
81 scale = 32700.0 / scale ;
|
robert@464
|
82 cout << "File samples scale = " << scale << endl;
|
robert@464
|
83
|
robert@464
|
84 for (m = 0; m < smp->sampleLen; m++)
|
robert@464
|
85 smp->samples[m] *= scale;
|
robert@464
|
86 }
|
robert@464
|
87
|
robert@464
|
88 sf_close(sndfile);
|
robert@464
|
89
|
robert@464
|
90 return 0;
|
robert@464
|
91 }
|
robert@464
|
92
|
robert@464
|
93
|
robert@464
|
94 // Handle Ctrl-C by requesting that the audio rendering stop
|
robert@464
|
95 void interrupt_handler(int var)
|
robert@464
|
96 {
|
robert@464
|
97 gShouldStop = true;
|
robert@464
|
98 }
|
robert@464
|
99
|
robert@464
|
100 // Print usage information
|
robert@464
|
101 void usage(const char * processName)
|
robert@464
|
102 {
|
robert@464
|
103 cerr << "Usage: " << processName << " [options]" << endl;
|
robert@464
|
104
|
robert@464
|
105 Bela_usage();
|
robert@464
|
106
|
robert@464
|
107 cerr << " --help [-h]: Print this menu\n";
|
robert@464
|
108 }
|
robert@464
|
109
|
robert@464
|
110 /* Function which returns the time since start of the program
|
robert@464
|
111 * in (fractional) seconds.
|
robert@464
|
112 */
|
robert@464
|
113 double getCurrentTime(void) {
|
robert@464
|
114 unsigned long long result;
|
robert@464
|
115 struct timeval tv;
|
robert@464
|
116
|
robert@464
|
117 gettimeofday(&tv, NULL);
|
robert@464
|
118 result = (tv.tv_sec - gFirstSeconds) * 1000000ULL + (tv.tv_usec - gFirstMicroseconds);
|
robert@464
|
119 return (double)result / 1000000.0;
|
robert@464
|
120 }
|
robert@464
|
121 extern SampleData gSampleData;
|
robert@464
|
122 int main(int argc, char *argv[])
|
robert@464
|
123 {
|
robert@464
|
124 BelaInitSettings settings; // Standard audio settings
|
robert@464
|
125 struct timeval tv;
|
robert@464
|
126 string fileName; // Name of the sample to load
|
robert@464
|
127
|
robert@464
|
128 struct option customOptions[] =
|
robert@464
|
129 {
|
robert@464
|
130 {"help", 0, NULL, 'h'},
|
robert@464
|
131 {"file", 1, NULL, 'f'},
|
robert@464
|
132 {NULL, 0, NULL, 0}
|
robert@464
|
133 };
|
robert@464
|
134
|
robert@464
|
135 gSampleData.samples = 0;
|
robert@464
|
136 gSampleData.sampleLen = -1;
|
robert@464
|
137
|
robert@464
|
138 // Set default settings
|
robert@464
|
139 Bela_defaultSettings(&settings);
|
robert@464
|
140
|
robert@464
|
141 settings.periodSize = 32; // Larger period size by default, for testing
|
robert@464
|
142
|
robert@464
|
143 // Parse command-line arguments
|
robert@464
|
144 while (1) {
|
robert@464
|
145 int c;
|
robert@464
|
146 if ((c = Bela_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
|
robert@464
|
147 break;
|
robert@464
|
148 switch (c) {
|
robert@464
|
149 case 'h':
|
robert@464
|
150 usage(basename(argv[0]));
|
robert@464
|
151 exit(0);
|
robert@464
|
152 case 'f':
|
robert@464
|
153 fileName = string((char *)optarg);
|
robert@464
|
154 break;
|
robert@464
|
155 case '?':
|
robert@464
|
156 default:
|
robert@464
|
157 usage(basename(argv[0]));
|
robert@464
|
158 exit(1);
|
robert@464
|
159 }
|
robert@464
|
160 }
|
robert@464
|
161
|
robert@464
|
162 if(fileName.empty()){
|
robert@464
|
163 fileName = "sample.wav";
|
robert@464
|
164 }
|
robert@464
|
165
|
robert@464
|
166
|
robert@464
|
167 // Load file
|
robert@464
|
168 if(initFile(fileName, &gSampleData) != 0)
|
robert@464
|
169 {
|
robert@464
|
170 cout << "Error: unable to load samples " << endl;
|
robert@464
|
171 return -1;
|
robert@464
|
172 }
|
robert@464
|
173
|
robert@464
|
174 if(settings.verbose)
|
robert@464
|
175 cout << "File contains " << gSampleData.sampleLen << " samples" << endl;
|
robert@464
|
176
|
robert@464
|
177
|
robert@464
|
178 // Initialise the PRU audio device
|
robert@464
|
179 if(Bela_initAudio(&settings, &gSampleData) != 0) {
|
robert@464
|
180 cout << "Error: unable to initialise audio" << endl;
|
robert@464
|
181 return -1;
|
robert@464
|
182 }
|
robert@464
|
183
|
robert@464
|
184 // Initialise time
|
robert@464
|
185 gettimeofday(&tv, NULL);
|
robert@464
|
186 gFirstSeconds = tv.tv_sec;
|
robert@464
|
187 gFirstMicroseconds = tv.tv_usec;
|
robert@464
|
188
|
robert@464
|
189 // Start the audio device running
|
robert@464
|
190 if(Bela_startAudio()) {
|
robert@464
|
191 cout << "Error: unable to start real-time audio" << endl;
|
robert@464
|
192 return -1;
|
robert@464
|
193 }
|
robert@464
|
194
|
robert@464
|
195 // Set up interrupt handler to catch Control-C
|
robert@464
|
196 signal(SIGINT, interrupt_handler);
|
robert@464
|
197 signal(SIGTERM, interrupt_handler);
|
robert@464
|
198
|
robert@464
|
199 // Run until told to stop
|
robert@464
|
200 while(!gShouldStop) {
|
robert@464
|
201 usleep(100000);
|
robert@464
|
202 }
|
robert@464
|
203
|
robert@464
|
204 // Stop the audio device
|
robert@464
|
205 Bela_stopAudio();
|
robert@464
|
206
|
robert@464
|
207 // Clean up any resources allocated for audio
|
robert@464
|
208 Bela_cleanupAudio();
|
robert@464
|
209
|
robert@464
|
210 // All done!
|
robert@464
|
211 return 0;
|
robert@464
|
212 }
|