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@12
|
97
|
Chris@12
|
98 int ibs = 1024;
|
Chris@12
|
99 int obs = int(ceil((double(ibs) * targetRate) / sourceRate));
|
Chris@12
|
100
|
Chris@12
|
101 int channels = sfinfo.channels;
|
Chris@12
|
102 float *ibuf = new float[channels * ibs];
|
Chris@12
|
103 float *obuf = new float[channels * obs];
|
Chris@12
|
104
|
Chris@12
|
105 double **prebuf = new double*[channels];
|
Chris@12
|
106 double **postbuf = new double*[channels];
|
Chris@12
|
107 vector<Resampler *> resamplers; // one per channel
|
Chris@12
|
108
|
Chris@12
|
109 for (int c = 0; c < channels; ++c) {
|
Chris@12
|
110 prebuf[c] = new double[ibs];
|
Chris@12
|
111 postbuf[c] = new double[obs];
|
Chris@12
|
112 resamplers.push_back(new Resampler(sourceRate, targetRate));
|
Chris@12
|
113 }
|
Chris@12
|
114
|
Chris@12
|
115 int latency = resamplers[0]->getLatency();
|
Chris@12
|
116 int n = 0;
|
Chris@12
|
117
|
Chris@12
|
118 while (true) {
|
Chris@12
|
119
|
Chris@12
|
120 int count = sf_readf_float(sndfile, ibuf, ibs);
|
Chris@12
|
121 if (count <= 0) break;
|
Chris@12
|
122
|
Chris@12
|
123 int rcount = 0;
|
Chris@12
|
124 int ocount = 0;
|
Chris@12
|
125
|
Chris@12
|
126 for (int c = 0; c < channels; ++c) {
|
Chris@12
|
127
|
Chris@12
|
128 for (int i = 0; i < count; ++i) {
|
Chris@12
|
129 prebuf[c][i] = ibuf[i * channels + c];
|
Chris@12
|
130 }
|
Chris@12
|
131
|
Chris@12
|
132 rcount = resamplers[c]->process(prebuf[c], postbuf[c], count);
|
Chris@12
|
133
|
Chris@12
|
134 if (n + rcount > latency) {
|
Chris@12
|
135 int off = 0;
|
Chris@12
|
136 if (latency > n) {
|
Chris@12
|
137 off = latency - n;
|
Chris@12
|
138 }
|
Chris@12
|
139 for (int i = off; i < rcount; ++i) {
|
Chris@12
|
140 obuf[(i - off) * channels + c] = postbuf[c][i];
|
Chris@12
|
141 }
|
Chris@12
|
142 ocount = rcount - off;
|
Chris@12
|
143 }
|
Chris@12
|
144 }
|
Chris@12
|
145
|
Chris@12
|
146 if (ocount > 0) {
|
Chris@12
|
147 sf_writef_float(sndfileOut, obuf, ocount);
|
Chris@12
|
148 }
|
Chris@12
|
149
|
Chris@12
|
150 n += rcount;
|
Chris@12
|
151 }
|
Chris@12
|
152
|
Chris@12
|
153 //!!! todo: tail (latency)
|
Chris@12
|
154
|
Chris@12
|
155 sf_close(sndfile);
|
Chris@12
|
156 sf_close(sndfileOut);
|
Chris@12
|
157
|
Chris@12
|
158 for (int c = 0; c < channels; ++c) {
|
Chris@12
|
159 delete[] prebuf[c];
|
Chris@12
|
160 delete[] postbuf[c];
|
Chris@12
|
161 delete resamplers[c];
|
Chris@12
|
162 }
|
Chris@12
|
163
|
Chris@12
|
164 delete[] prebuf;
|
Chris@12
|
165 delete[] postbuf;
|
Chris@12
|
166 delete[] ibuf;
|
Chris@12
|
167 delete[] obuf;
|
Chris@12
|
168
|
Chris@12
|
169 timeval etv;
|
Chris@12
|
170 (void)gettimeofday(&etv, 0);
|
Chris@12
|
171
|
Chris@12
|
172 etv.tv_sec -= tv.tv_sec;
|
Chris@12
|
173 if (etv.tv_usec < tv.tv_usec) {
|
Chris@12
|
174 etv.tv_usec += 1000000;
|
Chris@12
|
175 etv.tv_sec -= 1;
|
Chris@12
|
176 }
|
Chris@12
|
177 etv.tv_usec -= tv.tv_usec;
|
Chris@12
|
178
|
Chris@12
|
179 double sec = double(etv.tv_sec) + (double(etv.tv_usec) / 1000000.0);
|
Chris@12
|
180 cerr << "elapsed time: " << sec << " sec, in frames/sec: " << sfinfo.frames/sec << ", out frames/sec: " << n/sec << endl;
|
Chris@12
|
181
|
Chris@12
|
182 return 0;
|
Chris@12
|
183 }
|
Chris@12
|
184
|
Chris@12
|
185
|
Chris@12
|
186
|
Chris@12
|
187
|
Chris@12
|
188
|
Chris@12
|
189
|