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 <libgen.h>
|
robert@464
|
27 #include <signal.h>
|
robert@464
|
28 #include <string>
|
robert@464
|
29 #include <getopt.h>
|
robert@464
|
30 #include <sndfile.h> // to load audio files
|
robert@464
|
31
|
robert@464
|
32 #include <Bela.h>
|
robert@464
|
33 #include "SampleData.h"
|
robert@464
|
34
|
robert@464
|
35 using namespace std;
|
robert@464
|
36
|
robert@464
|
37 // Load samples from file
|
robert@464
|
38 int initFile(string file, SampleData *smp)//float *& smp)
|
robert@464
|
39 {
|
robert@464
|
40 SNDFILE *sndfile ;
|
robert@464
|
41 SF_INFO sfinfo ;
|
robert@464
|
42
|
robert@464
|
43 if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) {
|
robert@464
|
44 cout << "Couldn't open file " << file << endl;
|
robert@464
|
45 return 1;
|
robert@464
|
46 }
|
robert@464
|
47
|
robert@464
|
48 int numChan = sfinfo.channels;
|
robert@464
|
49 if(numChan != 1)
|
robert@464
|
50 {
|
robert@464
|
51 cout << "Error: " << file << " is not a mono file" << endl;
|
robert@464
|
52 return 1;
|
robert@464
|
53 }
|
robert@464
|
54
|
robert@464
|
55 smp->sampleLen = sfinfo.frames * numChan;
|
robert@464
|
56 smp->samples = new float[smp->sampleLen];
|
robert@464
|
57 if(smp == NULL){
|
robert@464
|
58 cout << "Could not allocate buffer" << endl;
|
robert@464
|
59 return 1;
|
robert@464
|
60 }
|
robert@464
|
61
|
robert@464
|
62 int subformat = sfinfo.format & SF_FORMAT_SUBMASK;
|
robert@464
|
63 int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen);
|
robert@464
|
64
|
robert@464
|
65 // Pad with zeros in case we couldn't read whole file
|
robert@464
|
66 for(int k = readcount; k <smp->sampleLen; k++)
|
robert@464
|
67 smp->samples[k] = 0;
|
robert@464
|
68
|
robert@464
|
69 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) {
|
robert@464
|
70 double scale ;
|
robert@464
|
71 int m ;
|
robert@464
|
72
|
robert@464
|
73 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ;
|
robert@464
|
74 if (scale < 1e-10)
|
robert@464
|
75 scale = 1.0 ;
|
robert@464
|
76 else
|
robert@464
|
77 scale = 32700.0 / scale ;
|
robert@464
|
78 cout << "File samples scale = " << scale << endl;
|
robert@464
|
79
|
robert@464
|
80 for (m = 0; m < smp->sampleLen; m++)
|
robert@464
|
81 smp->samples[m] *= scale;
|
robert@464
|
82 }
|
robert@464
|
83
|
robert@464
|
84 sf_close(sndfile);
|
robert@464
|
85
|
robert@464
|
86 return 0;
|
robert@464
|
87 }
|
robert@464
|
88
|
robert@464
|
89
|
robert@464
|
90 // Handle Ctrl-C by requesting that the audio rendering stop
|
robert@464
|
91 void interrupt_handler(int var)
|
robert@464
|
92 {
|
robert@464
|
93 //rt_task_delete ((RT_TASK *) &gTriggerSamplesTask);
|
robert@464
|
94 gShouldStop = true;
|
robert@464
|
95 }
|
robert@464
|
96
|
robert@464
|
97 // Print usage information
|
robert@464
|
98 void usage(const char * processName)
|
robert@464
|
99 {
|
robert@464
|
100 cerr << "Usage: " << processName << " [options]" << endl;
|
robert@464
|
101
|
robert@464
|
102 Bela_usage();
|
robert@464
|
103
|
robert@464
|
104 cerr << " --file [-f] filename: Name of the file to load (default is \"sample.wav\")\n";
|
robert@464
|
105 cerr << " --help [-h]: Print this menu\n";
|
robert@464
|
106 }
|
robert@464
|
107
|
robert@464
|
108 int main(int argc, char *argv[])
|
robert@464
|
109 {
|
robert@464
|
110 BelaInitSettings settings; // Standard audio settings
|
robert@464
|
111 string fileName; // Name of the sample to load
|
robert@464
|
112
|
robert@464
|
113 SampleData sampleData; // User define structure to pass data retrieved from file to render function
|
robert@464
|
114 sampleData.samples = 0;
|
robert@464
|
115 sampleData.sampleLen = -1;
|
robert@464
|
116
|
robert@464
|
117
|
robert@464
|
118 struct option customOptions[] =
|
robert@464
|
119 {
|
robert@464
|
120 {"help", 0, NULL, 'h'},
|
robert@464
|
121 {"file", 1, NULL, 'f'},
|
robert@464
|
122 {NULL, 0, NULL, 0}
|
robert@464
|
123 };
|
robert@464
|
124
|
robert@464
|
125 // Set default settings
|
robert@464
|
126 Bela_defaultSettings(&settings);
|
robert@464
|
127
|
robert@464
|
128 // Parse command-line arguments
|
robert@464
|
129 while (1) {
|
robert@464
|
130 int c;
|
robert@464
|
131 if ((c = Bela_getopt_long(argc, argv, "hf:", customOptions, &settings)) < 0)
|
robert@464
|
132 break;
|
robert@464
|
133 switch (c) {
|
robert@464
|
134 case 'h':
|
robert@464
|
135 usage(basename(argv[0]));
|
robert@464
|
136 exit(0);
|
robert@464
|
137 case 'f':
|
robert@464
|
138 fileName = string((char *)optarg);
|
robert@464
|
139 break;
|
robert@464
|
140 case '?':
|
robert@464
|
141 default:
|
robert@464
|
142 usage(basename(argv[0]));
|
robert@464
|
143 exit(1);
|
robert@464
|
144 }
|
robert@464
|
145 }
|
robert@464
|
146
|
robert@464
|
147 if(fileName.empty()){
|
robert@544
|
148 fileName = "sample.wav";
|
robert@464
|
149 }
|
robert@464
|
150
|
robert@464
|
151 if(settings.verbose) {
|
robert@464
|
152 cout << "Loading file " << fileName << endl;
|
robert@464
|
153 }
|
robert@464
|
154
|
robert@464
|
155 // Load file
|
robert@464
|
156 if(initFile(fileName, &sampleData) != 0)
|
robert@464
|
157 {
|
robert@464
|
158 cout << "Error: unable to load samples " << endl;
|
robert@464
|
159 return -1;
|
robert@464
|
160 }
|
robert@464
|
161
|
robert@464
|
162 if(settings.verbose)
|
robert@464
|
163 cout << "File contains " << sampleData.sampleLen << " samples" << endl;
|
robert@464
|
164
|
robert@464
|
165
|
robert@464
|
166 // Initialise the PRU audio device
|
robert@464
|
167 if(Bela_initAudio(&settings, &sampleData) != 0) {
|
robert@464
|
168 cout << "Error: unable to initialise audio" << endl;
|
robert@464
|
169 return -1;
|
robert@464
|
170 }
|
robert@464
|
171
|
robert@464
|
172 // Start the audio device running
|
robert@464
|
173 if(Bela_startAudio()) {
|
robert@464
|
174 cout << "Error: unable to start real-time audio" << endl;
|
robert@464
|
175 return -1;
|
robert@464
|
176 }
|
robert@464
|
177
|
robert@464
|
178 // Set up interrupt handler to catch Control-C and SIGTERM
|
robert@464
|
179 signal(SIGINT, interrupt_handler);
|
robert@464
|
180 signal(SIGTERM, interrupt_handler);
|
robert@464
|
181
|
robert@464
|
182 // Run until told to stop
|
robert@464
|
183 while(!gShouldStop) {
|
robert@464
|
184 usleep(100000);
|
robert@464
|
185 }
|
robert@464
|
186
|
robert@464
|
187 // Stop the audio device
|
robert@464
|
188 Bela_stopAudio();
|
robert@464
|
189
|
robert@464
|
190 // Clean up any resources allocated for audio
|
robert@464
|
191 Bela_cleanupAudio();
|
robert@464
|
192
|
robert@464
|
193 // All done!
|
robert@464
|
194 return 0;
|
robert@464
|
195 }
|