Mercurial > hg > beaglert
comparison projects/filter_IIR/main.cpp @ 3:6810f166482f
_new IIR filter example
author | Victor Zappi <victor.zappi@qmul.ac.uk> |
---|---|
date | Thu, 06 Nov 2014 17:55:05 +0000 |
parents | |
children | f34c63568523 |
comparison
equal
deleted
inserted
replaced
2:021ac8a1a4f9 | 3:6810f166482f |
---|---|
1 /* | |
2 * main.cpp | |
3 * | |
4 * Created on: Oct 24, 2014 | |
5 * Author: Andrew McPherson and Victor Zappi | |
6 */ | |
7 | |
8 #include <iostream> | |
9 #include <cstdlib> | |
10 #include <libgen.h> | |
11 #include <signal.h> | |
12 #include <string> | |
13 #include <sndfile.h> // to load audio files | |
14 #include "../../include/RTAudio.h" | |
15 #include "SampleData.h" | |
16 | |
17 using namespace std; | |
18 | |
19 float gCutFreq = 100; | |
20 | |
21 // Load samples from file | |
22 int initFile(string file, SampleData *smp)//float *& smp) | |
23 { | |
24 SNDFILE *sndfile ; | |
25 SF_INFO sfinfo ; | |
26 | |
27 if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) { | |
28 cout << "Couldn't open file " << file << endl; | |
29 return 1; | |
30 } | |
31 | |
32 int numChan = sfinfo.channels; | |
33 if(numChan != 1) | |
34 { | |
35 cout << "Error: " << file << " is not a mono file" << endl; | |
36 return 1; | |
37 } | |
38 | |
39 smp->sampleLen = sfinfo.frames * numChan; | |
40 smp->samples = new float[smp->sampleLen]; | |
41 if(smp == NULL){ | |
42 cout << "Could not allocate buffer" << endl; | |
43 return 1; | |
44 } | |
45 | |
46 int subformat = sfinfo.format & SF_FORMAT_SUBMASK; | |
47 int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen); | |
48 | |
49 // Pad with zeros in case we couldn't read whole file | |
50 for(int k = readcount; k <smp->sampleLen; k++) | |
51 smp->samples[k] = 0; | |
52 | |
53 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) { | |
54 double scale ; | |
55 int m ; | |
56 | |
57 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; | |
58 if (scale < 1e-10) | |
59 scale = 1.0 ; | |
60 else | |
61 scale = 32700.0 / scale ; | |
62 cout << "File samples scale = " << scale << endl; | |
63 | |
64 for (m = 0; m < smp->sampleLen; m++) | |
65 smp->samples[m] *= scale; | |
66 } | |
67 | |
68 sf_close(sndfile); | |
69 | |
70 return 0; | |
71 } | |
72 | |
73 | |
74 // Handle Ctrl-C by requesting that the audio rendering stop | |
75 void interrupt_handler(int var) | |
76 { | |
77 //rt_task_delete ((RT_TASK *) &gTriggerSamplesTask); | |
78 gShouldStop = true; | |
79 } | |
80 | |
81 // Print usage information | |
82 void usage(const char * processName) | |
83 { | |
84 cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f frequency]" << endl; | |
85 cerr << " -h: Print this menu\n"; | |
86 cerr << " -v: Enable verbose messages\n"; | |
87 cerr << " -p period: Set the period (hardware buffer) size in sensor frames\n"; | |
88 cerr << " -m: Enable the matrix (ADC and DAC) as well as audio\n"; | |
89 cerr << " -f filename: Name of the file to load (default is \"sample.wav\")\n"; | |
90 cerr << " -c freq: Set the cut off frequency of the filter in Hz\n"; | |
91 } | |
92 | |
93 int main(int argc, char *argv[]) | |
94 { | |
95 int verbose = 0; // Verbose printing level | |
96 int periodSize = 8; // Period size in sensor frames | |
97 int useMatrix = 0; // Whether to use the matrix or just audio | |
98 string fileName; // Name of the sample to load | |
99 | |
100 SampleData sampleData; // User define structure to pass data retrieved from file to render function | |
101 sampleData.samples = 0; | |
102 sampleData.sampleLen = -1; | |
103 | |
104 // Parse command-line arguments | |
105 while (1) { | |
106 int c; | |
107 if ((c = getopt(argc, argv, "hp:vms:")) < 0) | |
108 break; | |
109 switch (c) { | |
110 case 'h': | |
111 usage(basename(argv[0])); | |
112 exit(0); | |
113 case 'p': | |
114 periodSize = atoi(optarg); | |
115 if(periodSize < 1) | |
116 periodSize = 1; | |
117 break; | |
118 case 'v': | |
119 verbose = 1; | |
120 break; | |
121 case 'm': | |
122 useMatrix = 1; | |
123 break; | |
124 case 'f': | |
125 fileName = string((char *)optarg); | |
126 break; | |
127 case 'c': | |
128 gCutFreq = atof(optarg); | |
129 break; | |
130 case '?': | |
131 default: | |
132 usage(basename(argv[0])); | |
133 exit(1); | |
134 } | |
135 } | |
136 | |
137 if(fileName.empty()){ | |
138 fileName = "filter/longsample.wav"; | |
139 } | |
140 | |
141 // Set verbose logging information (optional by using value > 0; default is 0) | |
142 setVerboseLevel(verbose); | |
143 | |
144 if(verbose) { | |
145 cout << "Starting with period size " << periodSize << endl; | |
146 if(useMatrix) | |
147 cout << "Matrix enabled\n"; | |
148 else | |
149 cout << "Matrix disabled\n"; | |
150 cout << "Loading file " << fileName << endl; | |
151 } | |
152 | |
153 // Load file | |
154 if(initFile(fileName, &sampleData) != 0) | |
155 { | |
156 cout << "Error: unable to load samples " << endl; | |
157 return -1; | |
158 } | |
159 | |
160 if(verbose) | |
161 cout << "File contains " << sampleData.sampleLen << " samples" << endl; | |
162 | |
163 // Initialise the PRU audio device | |
164 if(initAudio(periodSize, useMatrix, &sampleData) != 0) { | |
165 cout << "Error: unable to initialise audio" << endl; | |
166 return -1; | |
167 } | |
168 | |
169 // Start the audio device running | |
170 if(startAudio()) { | |
171 cout << "Error: unable to start real-time audio" << endl; | |
172 return -1; | |
173 } | |
174 | |
175 // Set up interrupt handler to catch Control-C | |
176 signal(SIGINT, interrupt_handler); | |
177 | |
178 // Run until told to stop | |
179 while(!gShouldStop) { | |
180 usleep(100000); | |
181 } | |
182 | |
183 // Stop the audio device | |
184 stopAudio(); | |
185 | |
186 if(verbose) { | |
187 cout << "Cleaning up..." << endl; | |
188 } | |
189 | |
190 // Clean up any resources allocated for audio | |
191 cleanupAudio(); | |
192 | |
193 // All done! | |
194 return 0; | |
195 } |