comparison garage-resampler/resample.cpp @ 16:66abf86c864d

Add bandwidth, snr parameters
author Chris Cannam
date Fri, 18 Oct 2013 14:57:48 +0100
parents 8c87484e6d79
children
comparison
equal deleted inserted replaced
15:e35d83f7b4f0 16:66abf86c864d
18 int main(int argc, char **argv) 18 int main(int argc, char **argv)
19 { 19 {
20 int targetRate = 0; 20 int targetRate = 0;
21 bool version = false; 21 bool version = false;
22 bool help = false; 22 bool help = false;
23 double snr = 100;
24 double bandwidth = 0.02;
23 int c = 0; 25 int c = 0;
24 26
25 while (1) { 27 while (1) {
26 int optionIndex = 0; 28 int optionIndex = 0;
27 29
28 static struct option longOpts[] = { 30 static struct option longOpts[] = {
29 { "help", 0, 0, 'h' }, 31 { "help", 0, 0, 'h' },
30 { "version", 0, 0, 'V' }, 32 { "version", 0, 0, 'V' },
31 { "to", 1, 0, 't' }, 33 { "to", 1, 0, 't' },
34 { "snr", 1, 0, 's' },
35 { "bandwidth", 1, 0, 'b' },
32 { 0, 0, 0, 0 } 36 { 0, 0, 0, 0 }
33 }; 37 };
34 38
35 c = getopt_long(argc, argv, 39 c = getopt_long(argc, argv,
36 "t:hV", 40 "t:hVs:b:",
37 longOpts, &optionIndex); 41 longOpts, &optionIndex);
38 if (c == -1) break; 42 if (c == -1) break;
39 43
40 switch (c) { 44 switch (c) {
41 case 'h': help = true; break; 45 case 'h': help = true; break;
42 case 'V': version = true; break; 46 case 'V': version = true; break;
43 case 't': targetRate = atoi(optarg); break; 47 case 't': targetRate = atoi(optarg); break;
48 case 's': snr = atof(optarg); break;
49 case 'b': bandwidth = atof(optarg); break;
44 default: help = true; break; 50 default: help = true; break;
45 } 51 }
46 } 52 }
47 53
48 if (version) { 54 if (version) {
50 return 0; 56 return 0;
51 } 57 }
52 58
53 if (help || targetRate == 0 || optind + 2 != argc) { 59 if (help || targetRate == 0 || optind + 2 != argc) {
54 cerr << endl; 60 cerr << endl;
55 cerr << "usage: " << argv[0] << " --to <rate> <infile.wav> <outfile.wav>" << endl; 61 cerr << "usage: " << argv[0] << " --to <rate> [--snr <s>] [--bandwidth <b>] <infile.wav> <outfile.wav>" << endl;
56 cerr << endl; 62 cerr << endl;
57 return 2; 63 return 2;
58 } 64 }
59 65
60 timeval tv; 66 timeval tv;
94 } 100 }
95 101
96 int channels = sfinfo.channels; 102 int channels = sfinfo.channels;
97 vector<Resampler *> resamplers; // one per channel 103 vector<Resampler *> resamplers; // one per channel
98 104
99 for (int c = 0; c < channels; ++c) { 105 cerr << "Resampling from " << sourceRate << " to " << targetRate
100 resamplers.push_back(new Resampler(sourceRate, targetRate)); 106 << " Hz [with snr " << snr << ", bandwidth " << bandwidth << "]"
107 << endl;
108
109 for (int c = 0; c < channels; ++c) {
110 resamplers.push_back
111 (new Resampler(sourceRate, targetRate, snr, bandwidth));
101 } 112 }
102 113
103 int outputLatency = resamplers[0]->getLatency(); 114 int outputLatency = resamplers[0]->getLatency();
104 int inputLatency = int(ceil((double(outputLatency) * sourceRate) / 115 int inputLatency = int(ceil((double(outputLatency) * sourceRate) /
105 targetRate)); 116 targetRate));
197 etv.tv_sec -= 1; 208 etv.tv_sec -= 1;
198 } 209 }
199 etv.tv_usec -= tv.tv_usec; 210 etv.tv_usec -= tv.tv_usec;
200 211
201 double sec = double(etv.tv_sec) + (double(etv.tv_usec) / 1000000.0); 212 double sec = double(etv.tv_sec) + (double(etv.tv_usec) / 1000000.0);
202 cerr << "elapsed time: " << sec << " sec, in frames/sec: " << sfinfo.frames/sec << ", out frames/sec: " << n/sec << endl; 213 cerr << sfinfo.frames << " frames in, " << n << " frames out"
214 << ", nominal ratio " << double(targetRate)/double(sourceRate)
215 << ", actual " << double(n)/double(sfinfo.frames)
216 << endl << "Elapsed time: " << sec << " sec, in frames/sec: "
217 << int(sfinfo.frames/sec) << ", out frames/sec: " << int(n/sec) << endl;
203 218
204 return 0; 219 return 0;
205 } 220 }
206 221
207 222