blob: 3ab3ba9dcf0fa8065a33d64853fa9dba96ac784d [file] [log] [blame]
Mathias Agopian0fc2cb52012-10-21 01:01:38 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "AudioResampler.h"
18#include <media/AudioBufferProvider.h>
19#include <unistd.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <fcntl.h>
23#include <string.h>
24#include <sys/mman.h>
25#include <sys/stat.h>
26#include <errno.h>
27#include <time.h>
Mathias Agopian3f717612012-11-04 18:49:14 -080028#include <math.h>
Glenn Kastenf5293642013-12-17 14:49:17 -080029#include <audio_utils/sndfile.h>
Glenn Kastenc52b0332014-02-21 11:35:14 -080030#include <utils/Vector.h>
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070031
32using namespace android;
33
Glenn Kastene00eefe2013-12-17 13:54:29 -080034bool gVerbose = false;
35
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070036static int usage(const char* name) {
Andy Hung86eae0e2013-12-09 12:12:46 -080037 fprintf(stderr,"Usage: %s [-p] [-h] [-v] [-s] [-q {dq|lq|mq|hq|vhq|dlq|dmq|dhq}]"
Glenn Kastenc52b0332014-02-21 11:35:14 -080038 " [-i input-sample-rate] [-o output-sample-rate] [-O csv] [-P csv] [<input-file>]"
Andy Hung86eae0e2013-12-09 12:12:46 -080039 " <output-file>\n", name);
Mathias Agopian3f717612012-11-04 18:49:14 -080040 fprintf(stderr," -p enable profiling\n");
41 fprintf(stderr," -h create wav file\n");
Glenn Kastene00eefe2013-12-17 13:54:29 -080042 fprintf(stderr," -v verbose : log buffer provider calls\n");
Glenn Kastenbd72d222013-12-17 15:22:08 -080043 fprintf(stderr," -s stereo (ignored if input file is specified)\n");
Mathias Agopian3f717612012-11-04 18:49:14 -080044 fprintf(stderr," -q resampler quality\n");
45 fprintf(stderr," dq : default quality\n");
46 fprintf(stderr," lq : low quality\n");
47 fprintf(stderr," mq : medium quality\n");
48 fprintf(stderr," hq : high quality\n");
49 fprintf(stderr," vhq : very high quality\n");
Andy Hung86eae0e2013-12-09 12:12:46 -080050 fprintf(stderr," dlq : dynamic low quality\n");
51 fprintf(stderr," dmq : dynamic medium quality\n");
52 fprintf(stderr," dhq : dynamic high quality\n");
Glenn Kastenbd72d222013-12-17 15:22:08 -080053 fprintf(stderr," -i input file sample rate (ignored if input file is specified)\n");
Mathias Agopian3f717612012-11-04 18:49:14 -080054 fprintf(stderr," -o output file sample rate\n");
Glenn Kastenc52b0332014-02-21 11:35:14 -080055 fprintf(stderr," -O # frames output per call to resample() in CSV format\n");
56 fprintf(stderr," -P # frames provided per call to resample() in CSV format\n");
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070057 return -1;
58}
59
Glenn Kastenc52b0332014-02-21 11:35:14 -080060// Convert a list of integers in CSV format to a Vector of those values.
61// Returns the number of elements in the list, or -1 on error.
62int parseCSV(const char *string, Vector<int>& values)
63{
64 // pass 1: count the number of values and do syntax check
65 size_t numValues = 0;
66 bool hadDigit = false;
67 for (const char *p = string; ; ) {
68 switch (*p++) {
69 case '0': case '1': case '2': case '3': case '4':
70 case '5': case '6': case '7': case '8': case '9':
71 hadDigit = true;
72 break;
73 case '\0':
74 if (hadDigit) {
75 // pass 2: allocate and initialize vector of values
76 values.resize(++numValues);
77 values.editItemAt(0) = atoi(p = optarg);
78 for (size_t i = 1; i < numValues; ) {
79 if (*p++ == ',') {
80 values.editItemAt(i++) = atoi(p);
81 }
82 }
83 return numValues;
84 }
85 // fall through
86 case ',':
87 if (hadDigit) {
88 hadDigit = false;
89 numValues++;
90 break;
91 }
92 // fall through
93 default:
94 return -1;
95 }
96 }
97}
98
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070099int main(int argc, char* argv[]) {
100
Mathias Agopian3f717612012-11-04 18:49:14 -0800101 const char* const progname = argv[0];
Andy Hung6582f2b2014-01-03 12:30:41 -0800102 bool profileResample = false;
103 bool profileFilter = false;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700104 bool writeHeader = false;
Mathias Agopian3f717612012-11-04 18:49:14 -0800105 int channels = 1;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700106 int input_freq = 0;
107 int output_freq = 0;
108 AudioResampler::src_quality quality = AudioResampler::DEFAULT_QUALITY;
Glenn Kastenc52b0332014-02-21 11:35:14 -0800109 Vector<int> Ovalues;
110 Vector<int> Pvalues;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700111
112 int ch;
Glenn Kastenc52b0332014-02-21 11:35:14 -0800113 while ((ch = getopt(argc, argv, "pfhvsq:i:o:O:P:")) != -1) {
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700114 switch (ch) {
115 case 'p':
Andy Hung6582f2b2014-01-03 12:30:41 -0800116 profileResample = true;
117 break;
118 case 'f':
119 profileFilter = true;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700120 break;
121 case 'h':
122 writeHeader = true;
123 break;
Glenn Kastene00eefe2013-12-17 13:54:29 -0800124 case 'v':
125 gVerbose = true;
126 break;
Mathias Agopian3f717612012-11-04 18:49:14 -0800127 case 's':
128 channels = 2;
129 break;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700130 case 'q':
131 if (!strcmp(optarg, "dq"))
132 quality = AudioResampler::DEFAULT_QUALITY;
133 else if (!strcmp(optarg, "lq"))
134 quality = AudioResampler::LOW_QUALITY;
135 else if (!strcmp(optarg, "mq"))
136 quality = AudioResampler::MED_QUALITY;
137 else if (!strcmp(optarg, "hq"))
138 quality = AudioResampler::HIGH_QUALITY;
139 else if (!strcmp(optarg, "vhq"))
140 quality = AudioResampler::VERY_HIGH_QUALITY;
Andy Hung86eae0e2013-12-09 12:12:46 -0800141 else if (!strcmp(optarg, "dlq"))
142 quality = AudioResampler::DYN_LOW_QUALITY;
143 else if (!strcmp(optarg, "dmq"))
144 quality = AudioResampler::DYN_MED_QUALITY;
145 else if (!strcmp(optarg, "dhq"))
146 quality = AudioResampler::DYN_HIGH_QUALITY;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700147 else {
Mathias Agopian3f717612012-11-04 18:49:14 -0800148 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700149 return -1;
150 }
151 break;
152 case 'i':
153 input_freq = atoi(optarg);
154 break;
155 case 'o':
156 output_freq = atoi(optarg);
157 break;
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800158 case 'O':
Glenn Kastenc52b0332014-02-21 11:35:14 -0800159 if (parseCSV(optarg, Ovalues) < 0) {
160 fprintf(stderr, "incorrect syntax for -O option\n");
161 return -1;
162 }
163 break;
164 case 'P':
165 if (parseCSV(optarg, Pvalues) < 0) {
166 fprintf(stderr, "incorrect syntax for -P option\n");
167 return -1;
168 }
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800169 break;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700170 case '?':
171 default:
Mathias Agopian3f717612012-11-04 18:49:14 -0800172 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700173 return -1;
174 }
175 }
176 argc -= optind;
Mathias Agopian3f717612012-11-04 18:49:14 -0800177 argv += optind;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700178
Mathias Agopian3f717612012-11-04 18:49:14 -0800179 const char* file_in = NULL;
180 const char* file_out = NULL;
181 if (argc == 1) {
182 file_out = argv[0];
183 } else if (argc == 2) {
184 file_in = argv[0];
185 file_out = argv[1];
186 } else {
187 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700188 return -1;
189 }
190
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700191 // ----------------------------------------------------------
192
Mathias Agopian3f717612012-11-04 18:49:14 -0800193 size_t input_size;
194 void* input_vaddr;
195 if (argc == 2) {
Glenn Kastenbd72d222013-12-17 15:22:08 -0800196 SF_INFO info;
197 info.format = 0;
198 SNDFILE *sf = sf_open(file_in, SFM_READ, &info);
199 if (sf == NULL) {
200 perror(file_in);
201 return EXIT_FAILURE;
Mathias Agopian3f717612012-11-04 18:49:14 -0800202 }
Glenn Kastenbd72d222013-12-17 15:22:08 -0800203 input_size = info.frames * info.channels * sizeof(short);
204 input_vaddr = malloc(input_size);
205 (void) sf_readf_short(sf, (short *) input_vaddr, info.frames);
206 sf_close(sf);
207 channels = info.channels;
208 input_freq = info.samplerate;
Mathias Agopian3f717612012-11-04 18:49:14 -0800209 } else {
Andy Hung86eae0e2013-12-09 12:12:46 -0800210 // data for testing is exactly (input sampling rate/1000)/2 seconds
211 // so 44.1khz input is 22.05 seconds
Mathias Agopian3f717612012-11-04 18:49:14 -0800212 double k = 1000; // Hz / s
213 double time = (input_freq / 2) / k;
214 size_t input_frames = size_t(input_freq * time);
215 input_size = channels * sizeof(int16_t) * input_frames;
216 input_vaddr = malloc(input_size);
217 int16_t* in = (int16_t*)input_vaddr;
218 for (size_t i=0 ; i<input_frames ; i++) {
219 double t = double(i) / input_freq;
220 double y = sin(M_PI * k * t * t);
221 int16_t yi = floor(y * 32767.0 + 0.5);
Glenn Kastenb26e3e92012-11-14 08:32:08 -0800222 for (size_t j=0 ; j<(size_t)channels ; j++) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800223 in[i*channels + j] = yi / (1+j); // right ch. 1/2 left ch.
Mathias Agopian3f717612012-11-04 18:49:14 -0800224 }
225 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700226 }
227
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700228 // ----------------------------------------------------------
229
230 class Provider: public AudioBufferProvider {
Glenn Kastene00eefe2013-12-17 13:54:29 -0800231 int16_t* const mAddr; // base address
232 const size_t mNumFrames; // total frames
233 const int mChannels;
234 size_t mNextFrame; // index of next frame to provide
235 size_t mUnrel; // number of frames not yet released
Glenn Kastenc52b0332014-02-21 11:35:14 -0800236 const Vector<int> mPvalues; // number of frames provided per call
237 size_t mNextPidx; // index of next entry in mPvalues to use
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700238 public:
Glenn Kastenc52b0332014-02-21 11:35:14 -0800239 Provider(const void* addr, size_t size, int channels, const Vector<int>& Pvalues)
Glenn Kastene00eefe2013-12-17 13:54:29 -0800240 : mAddr((int16_t*) addr),
241 mNumFrames(size / (channels*sizeof(int16_t))),
242 mChannels(channels),
Glenn Kastenc52b0332014-02-21 11:35:14 -0800243 mNextFrame(0), mUnrel(0), mPvalues(Pvalues), mNextPidx(0) {
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700244 }
245 virtual status_t getNextBuffer(Buffer* buffer,
246 int64_t pts = kInvalidPTS) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800247 (void)pts; // suppress warning
Glenn Kastene00eefe2013-12-17 13:54:29 -0800248 size_t requestedFrames = buffer->frameCount;
249 if (requestedFrames > mNumFrames - mNextFrame) {
250 buffer->frameCount = mNumFrames - mNextFrame;
251 }
Glenn Kastenc52b0332014-02-21 11:35:14 -0800252 if (!mPvalues.isEmpty()) {
253 size_t provided = mPvalues[mNextPidx++];
254 printf("mPvalue[%d]=%u not %u\n", mNextPidx-1, provided, buffer->frameCount);
255 if (provided < buffer->frameCount) {
256 buffer->frameCount = provided;
257 }
258 if (mNextPidx >= mPvalues.size()) {
259 mNextPidx = 0;
260 }
261 }
Glenn Kastene00eefe2013-12-17 13:54:29 -0800262 if (gVerbose) {
263 printf("getNextBuffer() requested %u frames out of %u frames available,"
264 " and returned %u frames\n",
265 requestedFrames, mNumFrames - mNextFrame, buffer->frameCount);
266 }
267 mUnrel = buffer->frameCount;
268 if (buffer->frameCount > 0) {
269 buffer->i16 = &mAddr[mChannels * mNextFrame];
270 return NO_ERROR;
271 } else {
272 buffer->i16 = NULL;
273 return NOT_ENOUGH_DATA;
274 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700275 }
276 virtual void releaseBuffer(Buffer* buffer) {
Glenn Kastene00eefe2013-12-17 13:54:29 -0800277 if (buffer->frameCount > mUnrel) {
278 fprintf(stderr, "ERROR releaseBuffer() released %u frames but only %u available "
279 "to release\n", buffer->frameCount, mUnrel);
280 mNextFrame += mUnrel;
281 mUnrel = 0;
282 } else {
283 if (gVerbose) {
284 printf("releaseBuffer() released %u frames out of %u frames available "
285 "to release\n", buffer->frameCount, mUnrel);
286 }
287 mNextFrame += buffer->frameCount;
288 mUnrel -= buffer->frameCount;
289 }
Glenn Kasten47f3f5a2013-12-17 16:14:04 -0800290 buffer->frameCount = 0;
291 buffer->i16 = NULL;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700292 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800293 void reset() {
294 mNextFrame = 0;
295 }
Glenn Kastenc52b0332014-02-21 11:35:14 -0800296 } provider(input_vaddr, input_size, channels, Pvalues);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700297
Mathias Agopian3f717612012-11-04 18:49:14 -0800298 size_t input_frames = input_size / (channels * sizeof(int16_t));
Glenn Kastene00eefe2013-12-17 13:54:29 -0800299 if (gVerbose) {
300 printf("%u input frames\n", input_frames);
301 }
Mathias Agopian3f717612012-11-04 18:49:14 -0800302 size_t output_size = 2 * 4 * ((int64_t) input_frames * output_freq) / input_freq;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700303 output_size &= ~7; // always stereo, 32-bits
304
Andy Hung6582f2b2014-01-03 12:30:41 -0800305 if (profileFilter) {
306 // Check how fast sample rate changes are that require filter changes.
307 // The delta sample rate changes must indicate a downsampling ratio,
308 // and must be larger than 10% changes.
309 //
310 // On fast devices, filters should be generated between 0.1ms - 1ms.
311 // (single threaded).
312 AudioResampler* resampler = AudioResampler::create(16, channels,
313 8000, quality);
314 int looplimit = 100;
Andy Hung86eae0e2013-12-09 12:12:46 -0800315 timespec start, end;
316 clock_gettime(CLOCK_MONOTONIC, &start);
317 for (int i = 0; i < looplimit; ++i) {
Andy Hung6582f2b2014-01-03 12:30:41 -0800318 resampler->setSampleRate(9000);
319 resampler->setSampleRate(12000);
320 resampler->setSampleRate(20000);
321 resampler->setSampleRate(30000);
Andy Hung86eae0e2013-12-09 12:12:46 -0800322 }
323 clock_gettime(CLOCK_MONOTONIC, &end);
324 int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
325 int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
326 int64_t time = end_ns - start_ns;
Andy Hung6582f2b2014-01-03 12:30:41 -0800327 printf("%.2f sample rate changes with filter calculation/sec\n",
328 looplimit * 4 / (time / 1e9));
329
330 // Check how fast sample rate changes are without filter changes.
331 // This should be very fast, probably 0.1us - 1us per sample rate
332 // change.
333 resampler->setSampleRate(1000);
334 looplimit = 1000;
335 clock_gettime(CLOCK_MONOTONIC, &start);
336 for (int i = 0; i < looplimit; ++i) {
337 resampler->setSampleRate(1000+i);
338 }
339 clock_gettime(CLOCK_MONOTONIC, &end);
340 start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
341 end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
342 time = end_ns - start_ns;
343 printf("%.2f sample rate changes without filter calculation/sec\n",
344 looplimit / (time / 1e9));
345 resampler->reset();
346 delete resampler;
347 }
348
349 void* output_vaddr = malloc(output_size);
350 AudioResampler* resampler = AudioResampler::create(16, channels,
351 output_freq, quality);
352 size_t out_frames = output_size/8;
353
354 /* set volume precision to 12 bits, so the volume scale is 1<<12.
355 * This means the "integer" part fits in the Q19.12 precision
356 * representation of output int32_t.
357 *
358 * Generally 0 < volumePrecision <= 14 (due to the limits of
359 * int16_t values for Volume). volumePrecision cannot be 0 due
360 * to rounding and shifts.
361 */
362 const int volumePrecision = 12; // in bits
363
364 resampler->setSampleRate(input_freq);
365 resampler->setVolume(1 << volumePrecision, 1 << volumePrecision);
366
367 if (profileResample) {
368 /*
369 * For profiling on mobile devices, upon experimentation
370 * it is better to run a few trials with a shorter loop limit,
371 * and take the minimum time.
372 *
373 * Long tests can cause CPU temperature to build up and thermal throttling
374 * to reduce CPU frequency.
375 *
376 * For frequency checks (index=0, or 1, etc.):
377 * "cat /sys/devices/system/cpu/cpu${index}/cpufreq/scaling_*_freq"
378 *
379 * For temperature checks (index=0, or 1, etc.):
380 * "cat /sys/class/thermal/thermal_zone${index}/temp"
381 *
382 * Another way to avoid thermal throttling is to fix the CPU frequency
383 * at a lower level which prevents excessive temperatures.
384 */
385 const int trials = 4;
386 const int looplimit = 4;
387 timespec start, end;
388 int64_t time;
389
390 for (int n = 0; n < trials; ++n) {
391 clock_gettime(CLOCK_MONOTONIC, &start);
392 for (int i = 0; i < looplimit; ++i) {
393 resampler->resample((int*) output_vaddr, out_frames, &provider);
394 provider.reset(); // during benchmarking reset only the provider
395 }
396 clock_gettime(CLOCK_MONOTONIC, &end);
397 int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
398 int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
399 int64_t diff_ns = end_ns - start_ns;
400 if (n == 0 || diff_ns < time) {
401 time = diff_ns; // save the best out of our trials.
402 }
403 }
404 // Mfrms/s is "Millions of output frames per second".
405 printf("quality: %d channels: %d msec: %lld Mfrms/s: %.2lf\n",
406 quality, channels, time/1000000, out_frames * looplimit / (time / 1e9) / 1e6);
Andy Hung86eae0e2013-12-09 12:12:46 -0800407 resampler->reset();
408 }
409
Mathias Agopian3f717612012-11-04 18:49:14 -0800410 memset(output_vaddr, 0, output_size);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800411 if (gVerbose) {
412 printf("resample() %u output frames\n", out_frames);
413 }
Glenn Kastenc52b0332014-02-21 11:35:14 -0800414 if (Ovalues.isEmpty()) {
415 Ovalues.push(out_frames);
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800416 }
Glenn Kastenc52b0332014-02-21 11:35:14 -0800417 for (size_t i = 0, j = 0; i < out_frames; ) {
418 size_t thisFrames = Ovalues[j++];
419 if (j >= Ovalues.size()) {
420 j = 0;
421 }
422 if (thisFrames == 0 || thisFrames > out_frames - i) {
423 thisFrames = out_frames - i;
424 }
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800425 resampler->resample((int*) output_vaddr + 2*i, thisFrames, &provider);
426 i += thisFrames;
427 }
Glenn Kastene00eefe2013-12-17 13:54:29 -0800428 if (gVerbose) {
429 printf("resample() complete\n");
430 }
431 resampler->reset();
432 if (gVerbose) {
433 printf("reset() complete\n");
434 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800435 delete resampler;
436 resampler = NULL;
Mathias Agopian3f717612012-11-04 18:49:14 -0800437
Andy Hung86eae0e2013-12-09 12:12:46 -0800438 // mono takes left channel only
439 // stereo right channel is half amplitude of stereo left channel (due to input creation)
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700440 int32_t* out = (int32_t*) output_vaddr;
Mathias Agopian3f717612012-11-04 18:49:14 -0800441 int16_t* convert = (int16_t*) malloc(out_frames * channels * sizeof(int16_t));
Andy Hung86eae0e2013-12-09 12:12:46 -0800442
Andy Hung6582f2b2014-01-03 12:30:41 -0800443 // round to half towards zero and saturate at int16 (non-dithered)
444 const int roundVal = (1<<(volumePrecision-1)) - 1; // volumePrecision > 0
445
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700446 for (size_t i = 0; i < out_frames; i++) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800447 for (int j = 0; j < channels; j++) {
Andy Hung6582f2b2014-01-03 12:30:41 -0800448 int32_t s = out[i * 2 + j] + roundVal; // add offset here
449 if (s < 0) {
450 s = (s + 1) >> volumePrecision; // round to 0
451 if (s < -32768) {
452 s = -32768;
453 }
454 } else {
455 s = s >> volumePrecision;
456 if (s > 32767) {
457 s = 32767;
458 }
459 }
Mathias Agopian3f717612012-11-04 18:49:14 -0800460 convert[i * channels + j] = int16_t(s);
461 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700462 }
463
464 // write output to disk
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700465 if (writeHeader) {
Glenn Kastenf5293642013-12-17 14:49:17 -0800466 SF_INFO info;
467 info.frames = 0;
468 info.samplerate = output_freq;
469 info.channels = channels;
470 info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
471 SNDFILE *sf = sf_open(file_out, SFM_WRITE, &info);
472 if (sf == NULL) {
473 perror(file_out);
474 return EXIT_FAILURE;
475 }
476 (void) sf_writef_short(sf, convert, out_frames);
477 sf_close(sf);
478 } else {
479 int output_fd = open(file_out, O_WRONLY | O_CREAT | O_TRUNC,
480 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
481 if (output_fd < 0) {
482 perror(file_out);
483 return EXIT_FAILURE;
484 }
485 write(output_fd, convert, out_frames * channels * sizeof(int16_t));
486 close(output_fd);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700487 }
488
Glenn Kastenf5293642013-12-17 14:49:17 -0800489 return EXIT_SUCCESS;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700490}