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