-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
272 lines (219 loc) · 6.09 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***** main.cpp *****/
// main.cpp adapted from https://github.com/BelaPlatform/Bela/blob/master/examples/Audio/FFT-phase-vocoder/render.cpp
/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
The platform for ultra-low latency audio and sensor processing
http://bela.io
A project of the Augmented Instruments Laboratory within the
Centre for Digital Music at Queen Mary University of London.
http://www.eecs.qmul.ac.uk/~andrewm
(c) 2016 Augmented Instruments Laboratory: Andrew McPherson,
Astrid Bin, Liam Donovan, Christian Heinrichs, Robert Jack,
Giulio Moro, Laurel Pardue, Victor Zappi. All rights reserved.
The Bela software is distributed under the GNU Lesser General Public License
(LGPL 3.0), available here: https://www.gnu.org/licenses/lgpl-3.0.txt
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string.h>
#include <libgen.h>
#include <signal.h>
#include <getopt.h>
#include <unistd.h>
#include <sys/time.h>
#include <libraries/sndfile/sndfile.h> // to load audio files
#include "SampleData.h"
#include <Bela.h>
#include "Globals.h"
using namespace std;
// Global variables used by getCurrentTime()
unsigned long long gFirstSeconds, gFirstMicroseconds;
int FILE_LENGTH1;
int FILE_LENGTH2;
int FILE_LENGTH3;
// Load samples from file
int initFile(string file, SampleData *smp)
{
SNDFILE *sndfile;
SF_INFO sfinfo;
sfinfo.format = 0;
if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) {
cout << "Couldn't open file " << file << ": " << sf_strerror(sndfile) << endl;
return 1;
}
int numChan = sfinfo.channels;
if(numChan != 1)
{
cout << "Error: " << file << " is not a mono file" << endl;
return 1;
}
smp->sampleLen = sfinfo.frames * numChan;
smp->samples = new float[smp->sampleLen];
if(smp == NULL){
cout << "Could not allocate buffer" << endl;
return 1;
}
int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen);
// Pad with zeros in case we couldn't read whole file
for(int k = readcount; k <smp->sampleLen; k++)
smp->samples[k] = 0;
if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) {
double scale ;
int m ;
sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
if (scale < 1e-10)
scale = 1.0 ;
else
scale = 32700.0 / scale ;
cout << "File samples scale = " << scale << endl;
for (m = 0; m < smp->sampleLen; m++)
smp->samples[m] *= scale;
}
sf_close(sndfile);
return 0;
}
// Handle Ctrl-C by requesting that the audio rendering stop
void interrupt_handler(int var)
{
gShouldStop = true;
}
// Print usage information
void usage(const char * processName)
{
cerr << "Usage: " << processName << " [options]" << endl;
Bela_usage();
cerr << " --help [-h]: Print this menu\n";
}
/* Function which returns the time since start of the program
* in (fractional) seconds.
*/
double getCurrentTime(void) {
unsigned long long result;
struct timeval tv;
gettimeofday(&tv, NULL);
result = (tv.tv_sec - gFirstSeconds) * 1000000ULL + (tv.tv_usec - gFirstMicroseconds);
return (double)result / 1000000.0;
}
// Array of all available source songs
SampleData songs[3] = {};
// First song
string fileName1 = "betti.wav";
SampleData gSampleData1;
// Second song
string fileName2 = "nicefornothing.wav";
SampleData gSampleData2;
// Third song
string fileName3 = "jazzo.wav";
SampleData gSampleData3;
int main(int argc, char *argv[])
{
BelaInitSettings* settings = Bela_InitSettings_alloc(); // Standard audio settings
struct timeval tv;
string fileName; // Name of the sample to load
struct option customOptions[] =
{
{"help", 0, NULL, 'h'},
{"file", 1, NULL, 'f'},
{NULL, 0, NULL, 0}
};
gSampleData1.samples = 0;
gSampleData1.sampleLen = -1;
gSampleData2.samples = 0;
gSampleData2.sampleLen = -1;
gSampleData3.samples = 0;
gSampleData3.sampleLen = -1;
// Set default settings
Bela_defaultSettings(settings);
settings->setup = setup;
settings->render = render;
settings->cleanup = cleanup;
settings->periodSize = 32; // Larger period size by default, for testing
// Set projectName
if(argc > 1 && argv[0]){
settings->projectName = strrchr(argv[0], '/') + 1;
}
// Parse command-line arguments
while (1) {
int c = Bela_getopt_long(argc, argv, "hf:", customOptions, settings);
if (c < 0)
break;
int ret = -1;
switch (c) {
case 'h':
usage(basename(argv[0]));
ret = 0;
break;
case 'f':
fileName = string((char *)optarg);
break;
default:
usage(basename(argv[0]));
ret = 1;
break;
}
if(ret >= 0)
{
Bela_InitSettings_free(settings);
return ret;
}
}
// Load files
if(initFile(fileName1, &gSampleData1) != 0)
{
cout << "Error: unable to load samples " << endl;
return 1;
}
if(initFile(fileName2, &gSampleData2) != 0)
{
cout << "Error: unable to load samples " << endl;
return 1;
}
if(initFile(fileName3, &gSampleData3) != 0)
{
cout << "Error: unable to load samples " << endl;
return 1;
}
// Assign FILE_LENGTH for use in render.cpp
FILE_LENGTH1 = gSampleData1.sampleLen;
FILE_LENGTH2 = gSampleData2.sampleLen;
FILE_LENGTH3 = gSampleData3.sampleLen;
// Assign songs to Array
songs[0] = gSampleData1;
songs[1] = gSampleData2;
songs[2] = gSampleData3;
// Initialise the PRU audio device
if(Bela_initAudio(settings, &songs) != 0) {
Bela_InitSettings_free(settings);
cout << "Error: unable to initialise audio" << endl;
return 1;
}
Bela_InitSettings_free(settings);
// Initialise time
gettimeofday(&tv, NULL);
gFirstSeconds = tv.tv_sec;
gFirstMicroseconds = tv.tv_usec;
// Start the audio device running
if(Bela_startAudio()) {
cout << "Error: unable to start real-time audio" << endl;
return 1;
}
// Set up interrupt handler to catch Control-C
signal(SIGINT, interrupt_handler);
signal(SIGTERM, interrupt_handler);
// Run until told to stop
while(!gShouldStop) {
usleep(100000);
}
// Stop the audio device
Bela_stopAudio();
// Clean up any resources allocated for audio
Bela_cleanupAudio();
// All done!
return 0;
}