comparison examples/04-Audio/samples/main.cpp @ 464:8fcfbfb32aa0 prerelease

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