Chris@12
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@12
|
2
|
Chris@12
|
3 #include "Resampler.h"
|
Chris@12
|
4
|
Chris@12
|
5 #include <iostream>
|
Chris@12
|
6 #include <sndfile.h>
|
Chris@12
|
7 #include <time.h>
|
Chris@12
|
8 #include <string>
|
Chris@12
|
9 #include <cstdlib>
|
Chris@12
|
10 #include <cstring>
|
Chris@12
|
11 #include <cmath>
|
Chris@12
|
12 #include <getopt.h>
|
Chris@12
|
13 #include <unistd.h>
|
Chris@12
|
14 #include <sys/time.h>
|
Chris@12
|
15
|
Chris@12
|
16 using namespace std;
|
Chris@12
|
17
|
Chris@12
|
18 int main(int argc, char **argv)
|
Chris@12
|
19 {
|
Chris@12
|
20 int targetRate = 0;
|
Chris@12
|
21 bool version = false;
|
Chris@12
|
22 bool help = false;
|
Chris@12
|
23 int c = 0;
|
Chris@12
|
24
|
Chris@12
|
25 while (1) {
|
Chris@12
|
26 int optionIndex = 0;
|
Chris@12
|
27
|
Chris@12
|
28 static struct option longOpts[] = {
|
Chris@12
|
29 { "help", 0, 0, 'h' },
|
Chris@12
|
30 { "version", 0, 0, 'V' },
|
Chris@12
|
31 { "to", 1, 0, 't' },
|
Chris@12
|
32 { 0, 0, 0, 0 }
|
Chris@12
|
33 };
|
Chris@12
|
34
|
Chris@12
|
35 c = getopt_long(argc, argv,
|
Chris@12
|
36 "t:hV",
|
Chris@12
|
37 longOpts, &optionIndex);
|
Chris@12
|
38 if (c == -1) break;
|
Chris@12
|
39
|
Chris@12
|
40 switch (c) {
|
Chris@12
|
41 case 'h': help = true; break;
|
Chris@12
|
42 case 'V': version = true; break;
|
Chris@12
|
43 case 't': targetRate = atoi(optarg); break;
|
Chris@12
|
44 default: help = true; break;
|
Chris@12
|
45 }
|
Chris@12
|
46 }
|
Chris@12
|
47
|
Chris@12
|
48 if (version) {
|
Chris@12
|
49 cerr << "resample v0.1" << endl; //!!!
|
Chris@12
|
50 return 0;
|
Chris@12
|
51 }
|
Chris@12
|
52
|
Chris@12
|
53 cerr << "help = " << help << ", targetRate = " << targetRate << ", optionIndex = " << optind << ", argc = " << argc << endl;
|
Chris@12
|
54
|
Chris@12
|
55 if (help || targetRate == 0 || optind + 2 != argc) {
|
Chris@12
|
56 cerr << endl;
|
Chris@12
|
57 cerr << "usage: " << argv[0] << " --to <rate> <infile.wav> <outfile.wav>" << endl;
|
Chris@12
|
58 cerr << endl;
|
Chris@12
|
59 return 2;
|
Chris@12
|
60 }
|
Chris@12
|
61
|
Chris@12
|
62 timeval tv;
|
Chris@12
|
63 (void)gettimeofday(&tv, 0);
|
Chris@12
|
64
|
Chris@12
|
65 char *fileName = strdup(argv[optind++]);
|
Chris@12
|
66 char *fileNameOut = strdup(argv[optind++]);
|
Chris@12
|
67
|
Chris@12
|
68 SNDFILE *sndfile;
|
Chris@12
|
69 SNDFILE *sndfileOut;
|
Chris@12
|
70 SF_INFO sfinfo;
|
Chris@12
|
71 SF_INFO sfinfoOut;
|
Chris@12
|
72 memset(&sfinfo, 0, sizeof(SF_INFO));
|
Chris@12
|
73
|
Chris@12
|
74 sndfile = sf_open(fileName, SFM_READ, &sfinfo);
|
Chris@12
|
75 if (!sndfile) {
|
Chris@12
|
76 cerr << "ERROR: Failed to open input file \"" << fileName << "\": "
|
Chris@12
|
77 << sf_strerror(sndfile) << endl;
|
Chris@12
|
78 return 1;
|
Chris@12
|
79 }
|
Chris@12
|
80
|
Chris@12
|
81 int sourceRate = sfinfo.samplerate;
|
Chris@12
|
82
|
Chris@12
|
83 sfinfoOut.channels = sfinfo.channels;
|
Chris@12
|
84 sfinfoOut.format = sfinfo.format;
|
Chris@12
|
85 sfinfoOut.frames = int(ceil(double(sfinfo.frames) * targetRate) /
|
Chris@12
|
86 sourceRate);
|
Chris@12
|
87 sfinfoOut.samplerate = targetRate;
|
Chris@12
|
88 sfinfoOut.sections = sfinfo.sections;
|
Chris@12
|
89 sfinfoOut.seekable = sfinfo.seekable;
|
Chris@12
|
90
|
Chris@12
|
91 sndfileOut = sf_open(fileNameOut, SFM_WRITE, &sfinfoOut);
|
Chris@12
|
92 if (!sndfileOut) {
|
Chris@12
|
93 cerr << "ERROR: Failed to open output file \"" << fileNameOut << "\" for writing: "
|
Chris@12
|
94 << sf_strerror(sndfileOut) << endl;
|
Chris@12
|
95 return 1;
|
Chris@12
|
96 }
|
Chris@13
|
97
|
Chris@13
|
98 int channels = sfinfo.channels;
|
Chris@13
|
99 vector<Resampler *> resamplers; // one per channel
|
Chris@13
|
100
|
Chris@13
|
101 for (int c = 0; c < channels; ++c) {
|
Chris@13
|
102 resamplers.push_back(new Resampler(sourceRate, targetRate));
|
Chris@13
|
103 }
|
Chris@13
|
104
|
Chris@13
|
105 int outputLatency = resamplers[0]->getLatency();
|
Chris@13
|
106 int inputLatency = int(ceil((double(outputLatency) * sourceRate) /
|
Chris@13
|
107 targetRate));
|
Chris@12
|
108
|
Chris@12
|
109 int ibs = 1024;
|
Chris@13
|
110 if (ibs < inputLatency) {
|
Chris@13
|
111 ibs = inputLatency;
|
Chris@13
|
112 }
|
Chris@13
|
113
|
Chris@12
|
114 int obs = int(ceil((double(ibs) * targetRate) / sourceRate));
|
Chris@12
|
115
|
Chris@12
|
116 float *ibuf = new float[channels * ibs];
|
Chris@12
|
117 float *obuf = new float[channels * obs];
|
Chris@12
|
118
|
Chris@12
|
119 double **prebuf = new double*[channels];
|
Chris@12
|
120 double **postbuf = new double*[channels];
|
Chris@12
|
121
|
Chris@12
|
122 for (int c = 0; c < channels; ++c) {
|
Chris@12
|
123 prebuf[c] = new double[ibs];
|
Chris@12
|
124 postbuf[c] = new double[obs];
|
Chris@12
|
125 }
|
Chris@12
|
126
|
Chris@12
|
127 int n = 0;
|
Chris@13
|
128 int rcount = 0;
|
Chris@13
|
129 int ocount = 0;
|
Chris@12
|
130
|
Chris@12
|
131 while (true) {
|
Chris@12
|
132
|
Chris@12
|
133 int count = sf_readf_float(sndfile, ibuf, ibs);
|
Chris@12
|
134 if (count <= 0) break;
|
Chris@12
|
135
|
Chris@13
|
136 rcount = 0;
|
Chris@13
|
137 ocount = 0;
|
Chris@12
|
138
|
Chris@12
|
139 for (int c = 0; c < channels; ++c) {
|
Chris@12
|
140
|
Chris@12
|
141 for (int i = 0; i < count; ++i) {
|
Chris@12
|
142 prebuf[c][i] = ibuf[i * channels + c];
|
Chris@12
|
143 }
|
Chris@12
|
144
|
Chris@12
|
145 rcount = resamplers[c]->process(prebuf[c], postbuf[c], count);
|
Chris@12
|
146
|
Chris@13
|
147 if (n + rcount > outputLatency) {
|
Chris@12
|
148 int off = 0;
|
Chris@13
|
149 if (outputLatency > n) {
|
Chris@13
|
150 off = outputLatency - n;
|
Chris@12
|
151 }
|
Chris@12
|
152 for (int i = off; i < rcount; ++i) {
|
Chris@12
|
153 obuf[(i - off) * channels + c] = postbuf[c][i];
|
Chris@12
|
154 }
|
Chris@12
|
155 ocount = rcount - off;
|
Chris@12
|
156 }
|
Chris@12
|
157 }
|
Chris@12
|
158
|
Chris@12
|
159 if (ocount > 0) {
|
Chris@12
|
160 sf_writef_float(sndfileOut, obuf, ocount);
|
Chris@12
|
161 }
|
Chris@12
|
162
|
Chris@12
|
163 n += rcount;
|
Chris@12
|
164 }
|
Chris@12
|
165
|
Chris@13
|
166 // latency tail
|
Chris@13
|
167 for (int c = 0; c < channels; ++c) {
|
Chris@13
|
168 for (int i = 0; i < inputLatency; ++i) {
|
Chris@13
|
169 prebuf[c][i] = 0.0;
|
Chris@13
|
170 }
|
Chris@13
|
171 rcount = resamplers[c]->process(prebuf[c], postbuf[c], inputLatency);
|
Chris@13
|
172 for (int i = 0; i < rcount; ++i) {
|
Chris@13
|
173 obuf[i * channels + c] = postbuf[c][i];
|
Chris@13
|
174 }
|
Chris@13
|
175 }
|
Chris@13
|
176 sf_writef_float(sndfileOut, obuf, rcount);
|
Chris@13
|
177 n += rcount;
|
Chris@12
|
178
|
Chris@12
|
179 sf_close(sndfile);
|
Chris@12
|
180 sf_close(sndfileOut);
|
Chris@12
|
181
|
Chris@12
|
182 for (int c = 0; c < channels; ++c) {
|
Chris@12
|
183 delete[] prebuf[c];
|
Chris@12
|
184 delete[] postbuf[c];
|
Chris@12
|
185 delete resamplers[c];
|
Chris@12
|
186 }
|
Chris@12
|
187
|
Chris@12
|
188 delete[] prebuf;
|
Chris@12
|
189 delete[] postbuf;
|
Chris@12
|
190 delete[] ibuf;
|
Chris@12
|
191 delete[] obuf;
|
Chris@12
|
192
|
Chris@12
|
193 timeval etv;
|
Chris@12
|
194 (void)gettimeofday(&etv, 0);
|
Chris@12
|
195
|
Chris@12
|
196 etv.tv_sec -= tv.tv_sec;
|
Chris@12
|
197 if (etv.tv_usec < tv.tv_usec) {
|
Chris@12
|
198 etv.tv_usec += 1000000;
|
Chris@12
|
199 etv.tv_sec -= 1;
|
Chris@12
|
200 }
|
Chris@12
|
201 etv.tv_usec -= tv.tv_usec;
|
Chris@12
|
202
|
Chris@12
|
203 double sec = double(etv.tv_sec) + (double(etv.tv_usec) / 1000000.0);
|
Chris@12
|
204 cerr << "elapsed time: " << sec << " sec, in frames/sec: " << sfinfo.frames/sec << ", out frames/sec: " << n/sec << endl;
|
Chris@12
|
205
|
Chris@12
|
206 return 0;
|
Chris@12
|
207 }
|
Chris@12
|
208
|
Chris@12
|
209
|
Chris@12
|
210
|
Chris@12
|
211
|
Chris@12
|
212
|
Chris@12
|
213
|