Mercurial > hg > beaglert
comparison examples/filter_IIR/main.cpp @ 300:dbeed520b014 prerelease
Renamed projects to examples
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Fri, 27 May 2016 13:58:20 +0100 |
parents | projects/filter_IIR/main.cpp@3c3a1357657d |
children | e4392164b458 |
comparison
equal
deleted
inserted
replaced
297:a3d83ebdf49b | 300:dbeed520b014 |
---|---|
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 <getopt.h> | |
14 #include <sndfile.h> // to load audio files | |
15 | |
16 #include <BeagleRT.h> | |
17 #include "SampleData.h" | |
18 | |
19 using namespace std; | |
20 | |
21 float gCutFreq = 100; | |
22 | |
23 // Load samples from file | |
24 int initFile(string file, SampleData *smp)//float *& smp) | |
25 { | |
26 SNDFILE *sndfile ; | |
27 SF_INFO sfinfo ; | |
28 | |
29 if (!(sndfile = sf_open (file.c_str(), SFM_READ, &sfinfo))) { | |
30 cout << "Couldn't open file " << file << endl; | |
31 return 1; | |
32 } | |
33 | |
34 int numChan = sfinfo.channels; | |
35 if(numChan != 1) | |
36 { | |
37 cout << "Error: " << file << " is not a mono file" << endl; | |
38 return 1; | |
39 } | |
40 | |
41 smp->sampleLen = sfinfo.frames * numChan; | |
42 smp->samples = new float[smp->sampleLen]; | |
43 if(smp == NULL){ | |
44 cout << "Could not allocate buffer" << endl; | |
45 return 1; | |
46 } | |
47 | |
48 int subformat = sfinfo.format & SF_FORMAT_SUBMASK; | |
49 int readcount = sf_read_float(sndfile, smp->samples, smp->sampleLen); | |
50 | |
51 // Pad with zeros in case we couldn't read whole file | |
52 for(int k = readcount; k <smp->sampleLen; k++) | |
53 smp->samples[k] = 0; | |
54 | |
55 if (subformat == SF_FORMAT_FLOAT || subformat == SF_FORMAT_DOUBLE) { | |
56 double scale ; | |
57 int m ; | |
58 | |
59 sf_command (sndfile, SFC_CALC_SIGNAL_MAX, &scale, sizeof (scale)) ; | |
60 if (scale < 1e-10) | |
61 scale = 1.0 ; | |
62 else | |
63 scale = 32700.0 / scale ; | |
64 cout << "File samples scale = " << scale << endl; | |
65 | |
66 for (m = 0; m < smp->sampleLen; m++) | |
67 smp->samples[m] *= scale; | |
68 } | |
69 | |
70 sf_close(sndfile); | |
71 | |
72 return 0; | |
73 } | |
74 | |
75 // Handle Ctrl-C by requesting that the audio rendering stop | |
76 void interrupt_handler(int var) | |
77 { | |
78 //rt_task_delete ((RT_TASK *) &gTriggerSamplesTask); | |
79 gShouldStop = true; | |
80 } | |
81 | |
82 // Print usage information | |
83 void usage(const char * processName) | |
84 { | |
85 cerr << "Usage: " << processName << " [options]" << endl; | |
86 | |
87 BeagleRT_usage(); | |
88 | |
89 cerr << " --file [-f] filename: Name of the file to load (default is \"longsample.wav\")\n"; | |
90 cerr << " --cutfreq [-c] freq: Set the cut off frequency of the filter in Hz\n"; | |
91 cerr << " --help [-h]: Print this menu\n"; | |
92 } | |
93 | |
94 int main(int argc, char *argv[]) | |
95 { | |
96 BeagleRTInitSettings settings; // Standard audio settings | |
97 string fileName; // Name of the sample to load | |
98 | |
99 SampleData sampleData; // User define structure to pass data retrieved from file to render function | |
100 sampleData.samples = 0; | |
101 sampleData.sampleLen = -1; | |
102 | |
103 | |
104 struct option customOptions[] = | |
105 { | |
106 {"help", 0, NULL, 'h'}, | |
107 {"cutfreq", 1, NULL, 'c'}, | |
108 {"file", 1, NULL, 'f'}, | |
109 {NULL, 0, NULL, 0} | |
110 }; | |
111 | |
112 // Set default settings | |
113 BeagleRT_defaultSettings(&settings); | |
114 | |
115 // Parse command-line arguments | |
116 while (1) { | |
117 int c; | |
118 if ((c = BeagleRT_getopt_long(argc, argv, "hf:c:", customOptions, &settings)) < 0) | |
119 break; | |
120 switch (c) { | |
121 case 'h': | |
122 usage(basename(argv[0])); | |
123 exit(0); | |
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 if(settings.verbose) { | |
142 cout << "Loading file " << fileName << endl; | |
143 } | |
144 | |
145 // Load file | |
146 if(initFile(fileName, &sampleData) != 0) | |
147 { | |
148 cout << "Error: unable to load samples " << endl; | |
149 return -1; | |
150 } | |
151 | |
152 if(settings.verbose) | |
153 cout << "File contains " << sampleData.sampleLen << " samples" << endl; | |
154 | |
155 | |
156 // Initialise the PRU audio device | |
157 if(BeagleRT_initAudio(&settings, &sampleData) != 0) { | |
158 cout << "Error: unable to initialise audio" << endl; | |
159 return -1; | |
160 } | |
161 | |
162 // Start the audio device running | |
163 if(BeagleRT_startAudio()) { | |
164 cout << "Error: unable to start real-time audio" << endl; | |
165 return -1; | |
166 } | |
167 | |
168 // Set up interrupt handler to catch Control-C and SIGTERM | |
169 signal(SIGINT, interrupt_handler); | |
170 signal(SIGTERM, interrupt_handler); | |
171 | |
172 // Run until told to stop | |
173 while(!gShouldStop) { | |
174 usleep(100000); | |
175 } | |
176 | |
177 // Stop the audio device | |
178 BeagleRT_stopAudio(); | |
179 | |
180 // Clean up any resources allocated for audio | |
181 BeagleRT_cleanupAudio(); | |
182 | |
183 // All done! | |
184 return 0; | |
185 } | |
186 |