blob: bae3c5b7ed32f672921d28f298188a47112cbc40 [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
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070017#include <unistd.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <fcntl.h>
21#include <string.h>
22#include <sys/mman.h>
23#include <sys/stat.h>
24#include <errno.h>
Glenn Kasten1e4e4f42014-04-08 10:41:02 -070025#include <inttypes.h>
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070026#include <time.h>
Mathias Agopian3f717612012-11-04 18:49:14 -080027#include <math.h>
Andy Hung78136682014-04-08 17:59:59 -070028#include <audio_utils/primitives.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>
Andy Hung3348e362014-07-07 10:21:44 -070031#include <media/AudioBufferProvider.h>
32#include "AudioResampler.h"
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070033
34using namespace android;
35
Andy Hung78136682014-04-08 17:59:59 -070036static bool gVerbose = false;
Glenn Kastene00eefe2013-12-17 13:54:29 -080037
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070038static int usage(const char* name) {
Andy Hungdf383a52014-04-09 19:10:15 -070039 fprintf(stderr,"Usage: %s [-p] [-f] [-F] [-v] [-c channels]"
40 " [-q {dq|lq|mq|hq|vhq|dlq|dmq|dhq}]"
Andy Hung78136682014-04-08 17:59:59 -070041 " [-i input-sample-rate] [-o output-sample-rate]"
42 " [-O csv] [-P csv] [<input-file>]"
Andy Hung86eae0e2013-12-09 12:12:46 -080043 " <output-file>\n", name);
Mathias Agopian3f717612012-11-04 18:49:14 -080044 fprintf(stderr," -p enable profiling\n");
Andy Hung78136682014-04-08 17:59:59 -070045 fprintf(stderr," -f enable filter profiling\n");
46 fprintf(stderr," -F enable floating point -q {dlq|dmq|dhq} only");
Glenn Kastene00eefe2013-12-17 13:54:29 -080047 fprintf(stderr," -v verbose : log buffer provider calls\n");
Andy Hungdf383a52014-04-09 19:10:15 -070048 fprintf(stderr," -c # channels (1-2 for lq|mq|hq; 1-8 for dlq|dmq|dhq)\n");
Mathias Agopian3f717612012-11-04 18:49:14 -080049 fprintf(stderr," -q resampler quality\n");
50 fprintf(stderr," dq : default quality\n");
51 fprintf(stderr," lq : low quality\n");
52 fprintf(stderr," mq : medium quality\n");
53 fprintf(stderr," hq : high quality\n");
54 fprintf(stderr," vhq : very high quality\n");
Andy Hung86eae0e2013-12-09 12:12:46 -080055 fprintf(stderr," dlq : dynamic low quality\n");
56 fprintf(stderr," dmq : dynamic medium quality\n");
57 fprintf(stderr," dhq : dynamic high quality\n");
Glenn Kastenbd72d222013-12-17 15:22:08 -080058 fprintf(stderr," -i input file sample rate (ignored if input file is specified)\n");
Mathias Agopian3f717612012-11-04 18:49:14 -080059 fprintf(stderr," -o output file sample rate\n");
Glenn Kastenc52b0332014-02-21 11:35:14 -080060 fprintf(stderr," -O # frames output per call to resample() in CSV format\n");
61 fprintf(stderr," -P # frames provided per call to resample() in CSV format\n");
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070062 return -1;
63}
64
Glenn Kastenc52b0332014-02-21 11:35:14 -080065// Convert a list of integers in CSV format to a Vector of those values.
66// Returns the number of elements in the list, or -1 on error.
67int parseCSV(const char *string, Vector<int>& values)
68{
69 // pass 1: count the number of values and do syntax check
70 size_t numValues = 0;
71 bool hadDigit = false;
72 for (const char *p = string; ; ) {
73 switch (*p++) {
74 case '0': case '1': case '2': case '3': case '4':
75 case '5': case '6': case '7': case '8': case '9':
76 hadDigit = true;
77 break;
78 case '\0':
79 if (hadDigit) {
80 // pass 2: allocate and initialize vector of values
81 values.resize(++numValues);
82 values.editItemAt(0) = atoi(p = optarg);
83 for (size_t i = 1; i < numValues; ) {
84 if (*p++ == ',') {
85 values.editItemAt(i++) = atoi(p);
86 }
87 }
88 return numValues;
89 }
90 // fall through
91 case ',':
92 if (hadDigit) {
93 hadDigit = false;
94 numValues++;
95 break;
96 }
97 // fall through
98 default:
99 return -1;
100 }
101 }
102}
103
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700104int main(int argc, char* argv[]) {
Mathias Agopian3f717612012-11-04 18:49:14 -0800105 const char* const progname = argv[0];
Andy Hung6582f2b2014-01-03 12:30:41 -0800106 bool profileResample = false;
107 bool profileFilter = false;
Andy Hung78136682014-04-08 17:59:59 -0700108 bool useFloat = false;
Mathias Agopian3f717612012-11-04 18:49:14 -0800109 int channels = 1;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700110 int input_freq = 0;
111 int output_freq = 0;
112 AudioResampler::src_quality quality = AudioResampler::DEFAULT_QUALITY;
Glenn Kastenc52b0332014-02-21 11:35:14 -0800113 Vector<int> Ovalues;
114 Vector<int> Pvalues;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700115
116 int ch;
Andy Hungdf383a52014-04-09 19:10:15 -0700117 while ((ch = getopt(argc, argv, "pfFvc:q:i:o:O:P:")) != -1) {
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700118 switch (ch) {
119 case 'p':
Andy Hung6582f2b2014-01-03 12:30:41 -0800120 profileResample = true;
121 break;
122 case 'f':
123 profileFilter = true;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700124 break;
Andy Hung78136682014-04-08 17:59:59 -0700125 case 'F':
126 useFloat = true;
127 break;
Glenn Kastene00eefe2013-12-17 13:54:29 -0800128 case 'v':
129 gVerbose = true;
130 break;
Andy Hungdf383a52014-04-09 19:10:15 -0700131 case 'c':
132 channels = atoi(optarg);
Mathias Agopian3f717612012-11-04 18:49:14 -0800133 break;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700134 case 'q':
135 if (!strcmp(optarg, "dq"))
136 quality = AudioResampler::DEFAULT_QUALITY;
137 else if (!strcmp(optarg, "lq"))
138 quality = AudioResampler::LOW_QUALITY;
139 else if (!strcmp(optarg, "mq"))
140 quality = AudioResampler::MED_QUALITY;
141 else if (!strcmp(optarg, "hq"))
142 quality = AudioResampler::HIGH_QUALITY;
143 else if (!strcmp(optarg, "vhq"))
144 quality = AudioResampler::VERY_HIGH_QUALITY;
Andy Hung86eae0e2013-12-09 12:12:46 -0800145 else if (!strcmp(optarg, "dlq"))
146 quality = AudioResampler::DYN_LOW_QUALITY;
147 else if (!strcmp(optarg, "dmq"))
148 quality = AudioResampler::DYN_MED_QUALITY;
149 else if (!strcmp(optarg, "dhq"))
150 quality = AudioResampler::DYN_HIGH_QUALITY;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700151 else {
Mathias Agopian3f717612012-11-04 18:49:14 -0800152 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700153 return -1;
154 }
155 break;
156 case 'i':
157 input_freq = atoi(optarg);
158 break;
159 case 'o':
160 output_freq = atoi(optarg);
161 break;
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800162 case 'O':
Glenn Kastenc52b0332014-02-21 11:35:14 -0800163 if (parseCSV(optarg, Ovalues) < 0) {
164 fprintf(stderr, "incorrect syntax for -O option\n");
165 return -1;
166 }
167 break;
168 case 'P':
169 if (parseCSV(optarg, Pvalues) < 0) {
170 fprintf(stderr, "incorrect syntax for -P option\n");
171 return -1;
172 }
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800173 break;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700174 case '?':
175 default:
Mathias Agopian3f717612012-11-04 18:49:14 -0800176 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700177 return -1;
178 }
179 }
Andy Hung78136682014-04-08 17:59:59 -0700180
Andy Hungdf383a52014-04-09 19:10:15 -0700181 if (channels < 1
182 || channels > (quality < AudioResampler::DYN_LOW_QUALITY ? 2 : 8)) {
183 fprintf(stderr, "invalid number of audio channels %d\n", channels);
184 return -1;
185 }
Andy Hung78136682014-04-08 17:59:59 -0700186 if (useFloat && quality < AudioResampler::DYN_LOW_QUALITY) {
187 fprintf(stderr, "float processing is only possible for dynamic resamplers\n");
188 return -1;
189 }
190
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700191 argc -= optind;
Mathias Agopian3f717612012-11-04 18:49:14 -0800192 argv += optind;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700193
Mathias Agopian3f717612012-11-04 18:49:14 -0800194 const char* file_in = NULL;
195 const char* file_out = NULL;
196 if (argc == 1) {
197 file_out = argv[0];
198 } else if (argc == 2) {
199 file_in = argv[0];
200 file_out = argv[1];
201 } else {
202 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700203 return -1;
204 }
205
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700206 // ----------------------------------------------------------
207
Mathias Agopian3f717612012-11-04 18:49:14 -0800208 size_t input_size;
209 void* input_vaddr;
210 if (argc == 2) {
Glenn Kastenbd72d222013-12-17 15:22:08 -0800211 SF_INFO info;
212 info.format = 0;
213 SNDFILE *sf = sf_open(file_in, SFM_READ, &info);
214 if (sf == NULL) {
215 perror(file_in);
216 return EXIT_FAILURE;
Mathias Agopian3f717612012-11-04 18:49:14 -0800217 }
Glenn Kastenbd72d222013-12-17 15:22:08 -0800218 input_size = info.frames * info.channels * sizeof(short);
219 input_vaddr = malloc(input_size);
220 (void) sf_readf_short(sf, (short *) input_vaddr, info.frames);
221 sf_close(sf);
222 channels = info.channels;
223 input_freq = info.samplerate;
Mathias Agopian3f717612012-11-04 18:49:14 -0800224 } else {
Andy Hung86eae0e2013-12-09 12:12:46 -0800225 // data for testing is exactly (input sampling rate/1000)/2 seconds
226 // so 44.1khz input is 22.05 seconds
Mathias Agopian3f717612012-11-04 18:49:14 -0800227 double k = 1000; // Hz / s
228 double time = (input_freq / 2) / k;
229 size_t input_frames = size_t(input_freq * time);
230 input_size = channels * sizeof(int16_t) * input_frames;
231 input_vaddr = malloc(input_size);
232 int16_t* in = (int16_t*)input_vaddr;
233 for (size_t i=0 ; i<input_frames ; i++) {
234 double t = double(i) / input_freq;
235 double y = sin(M_PI * k * t * t);
236 int16_t yi = floor(y * 32767.0 + 0.5);
Andy Hungdf383a52014-04-09 19:10:15 -0700237 for (int j = 0; j < channels; j++) {
238 in[i*channels + j] = yi / (1 + j);
Mathias Agopian3f717612012-11-04 18:49:14 -0800239 }
240 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700241 }
Andy Hungdf383a52014-04-09 19:10:15 -0700242 size_t input_framesize = channels * sizeof(int16_t);
243 size_t input_frames = input_size / input_framesize;
Andy Hung78136682014-04-08 17:59:59 -0700244
245 // For float processing, convert input int16_t to float array
246 if (useFloat) {
247 void *new_vaddr;
248
Andy Hungdf383a52014-04-09 19:10:15 -0700249 input_framesize = channels * sizeof(float);
250 input_size = input_frames * input_framesize;
Andy Hung78136682014-04-08 17:59:59 -0700251 new_vaddr = malloc(input_size);
252 memcpy_to_float_from_i16(reinterpret_cast<float*>(new_vaddr),
253 reinterpret_cast<int16_t*>(input_vaddr), input_frames * channels);
254 free(input_vaddr);
255 input_vaddr = new_vaddr;
256 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700257
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700258 // ----------------------------------------------------------
259
260 class Provider: public AudioBufferProvider {
Andy Hung78136682014-04-08 17:59:59 -0700261 const void* mAddr; // base address
Glenn Kastene00eefe2013-12-17 13:54:29 -0800262 const size_t mNumFrames; // total frames
Andy Hung78136682014-04-08 17:59:59 -0700263 const size_t mFrameSize; // size of each frame in bytes
Glenn Kastene00eefe2013-12-17 13:54:29 -0800264 size_t mNextFrame; // index of next frame to provide
265 size_t mUnrel; // number of frames not yet released
Glenn Kastenc52b0332014-02-21 11:35:14 -0800266 const Vector<int> mPvalues; // number of frames provided per call
267 size_t mNextPidx; // index of next entry in mPvalues to use
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700268 public:
Andy Hung78136682014-04-08 17:59:59 -0700269 Provider(const void* addr, size_t frames, size_t frameSize, const Vector<int>& Pvalues)
270 : mAddr(addr),
271 mNumFrames(frames),
272 mFrameSize(frameSize),
Glenn Kastenc52b0332014-02-21 11:35:14 -0800273 mNextFrame(0), mUnrel(0), mPvalues(Pvalues), mNextPidx(0) {
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700274 }
Glenn Kastend79072e2016-01-06 08:41:20 -0800275 virtual status_t getNextBuffer(Buffer* buffer) {
Glenn Kastene00eefe2013-12-17 13:54:29 -0800276 size_t requestedFrames = buffer->frameCount;
277 if (requestedFrames > mNumFrames - mNextFrame) {
278 buffer->frameCount = mNumFrames - mNextFrame;
279 }
Glenn Kastenc52b0332014-02-21 11:35:14 -0800280 if (!mPvalues.isEmpty()) {
281 size_t provided = mPvalues[mNextPidx++];
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700282 printf("mPvalue[%zu]=%zu not %zu\n", mNextPidx-1, provided, buffer->frameCount);
Glenn Kastenc52b0332014-02-21 11:35:14 -0800283 if (provided < buffer->frameCount) {
284 buffer->frameCount = provided;
285 }
286 if (mNextPidx >= mPvalues.size()) {
287 mNextPidx = 0;
288 }
289 }
Glenn Kastene00eefe2013-12-17 13:54:29 -0800290 if (gVerbose) {
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700291 printf("getNextBuffer() requested %zu frames out of %zu frames available,"
292 " and returned %zu frames\n",
293 requestedFrames, (size_t) (mNumFrames - mNextFrame), buffer->frameCount);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800294 }
295 mUnrel = buffer->frameCount;
296 if (buffer->frameCount > 0) {
Andy Hung78136682014-04-08 17:59:59 -0700297 buffer->raw = (char *)mAddr + mFrameSize * mNextFrame;
Glenn Kastene00eefe2013-12-17 13:54:29 -0800298 return NO_ERROR;
299 } else {
Andy Hung78136682014-04-08 17:59:59 -0700300 buffer->raw = NULL;
Glenn Kastene00eefe2013-12-17 13:54:29 -0800301 return NOT_ENOUGH_DATA;
302 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700303 }
304 virtual void releaseBuffer(Buffer* buffer) {
Glenn Kastene00eefe2013-12-17 13:54:29 -0800305 if (buffer->frameCount > mUnrel) {
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700306 fprintf(stderr, "ERROR releaseBuffer() released %zu frames but only %zu available "
Glenn Kastene00eefe2013-12-17 13:54:29 -0800307 "to release\n", buffer->frameCount, mUnrel);
308 mNextFrame += mUnrel;
309 mUnrel = 0;
310 } else {
311 if (gVerbose) {
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700312 printf("releaseBuffer() released %zu frames out of %zu frames available "
Glenn Kastene00eefe2013-12-17 13:54:29 -0800313 "to release\n", buffer->frameCount, mUnrel);
314 }
315 mNextFrame += buffer->frameCount;
316 mUnrel -= buffer->frameCount;
317 }
Glenn Kasten47f3f5a2013-12-17 16:14:04 -0800318 buffer->frameCount = 0;
Andy Hung78136682014-04-08 17:59:59 -0700319 buffer->raw = NULL;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700320 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800321 void reset() {
322 mNextFrame = 0;
323 }
Andy Hungdf383a52014-04-09 19:10:15 -0700324 } provider(input_vaddr, input_frames, input_framesize, Pvalues);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700325
Glenn Kastene00eefe2013-12-17 13:54:29 -0800326 if (gVerbose) {
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700327 printf("%zu input frames\n", input_frames);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800328 }
Andy Hung78136682014-04-08 17:59:59 -0700329
Andy Hung3348e362014-07-07 10:21:44 -0700330 audio_format_t format = useFloat ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
Andy Hungdf383a52014-04-09 19:10:15 -0700331 int output_channels = channels > 2 ? channels : 2; // output is at least stereo samples
332 size_t output_framesize = output_channels * (useFloat ? sizeof(float) : sizeof(int32_t));
333 size_t output_frames = ((int64_t) input_frames * output_freq) / input_freq;
334 size_t output_size = output_frames * output_framesize;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700335
Andy Hung6582f2b2014-01-03 12:30:41 -0800336 if (profileFilter) {
337 // Check how fast sample rate changes are that require filter changes.
338 // The delta sample rate changes must indicate a downsampling ratio,
339 // and must be larger than 10% changes.
340 //
341 // On fast devices, filters should be generated between 0.1ms - 1ms.
342 // (single threaded).
Andy Hung3348e362014-07-07 10:21:44 -0700343 AudioResampler* resampler = AudioResampler::create(format, channels,
Andy Hung6582f2b2014-01-03 12:30:41 -0800344 8000, quality);
345 int looplimit = 100;
Andy Hung86eae0e2013-12-09 12:12:46 -0800346 timespec start, end;
347 clock_gettime(CLOCK_MONOTONIC, &start);
348 for (int i = 0; i < looplimit; ++i) {
Andy Hung6582f2b2014-01-03 12:30:41 -0800349 resampler->setSampleRate(9000);
350 resampler->setSampleRate(12000);
351 resampler->setSampleRate(20000);
352 resampler->setSampleRate(30000);
Andy Hung86eae0e2013-12-09 12:12:46 -0800353 }
354 clock_gettime(CLOCK_MONOTONIC, &end);
355 int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
356 int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
357 int64_t time = end_ns - start_ns;
Andy Hung6582f2b2014-01-03 12:30:41 -0800358 printf("%.2f sample rate changes with filter calculation/sec\n",
359 looplimit * 4 / (time / 1e9));
360
361 // Check how fast sample rate changes are without filter changes.
362 // This should be very fast, probably 0.1us - 1us per sample rate
363 // change.
364 resampler->setSampleRate(1000);
365 looplimit = 1000;
366 clock_gettime(CLOCK_MONOTONIC, &start);
367 for (int i = 0; i < looplimit; ++i) {
368 resampler->setSampleRate(1000+i);
369 }
370 clock_gettime(CLOCK_MONOTONIC, &end);
371 start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
372 end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
373 time = end_ns - start_ns;
374 printf("%.2f sample rate changes without filter calculation/sec\n",
375 looplimit / (time / 1e9));
376 resampler->reset();
377 delete resampler;
378 }
379
380 void* output_vaddr = malloc(output_size);
Andy Hung3348e362014-07-07 10:21:44 -0700381 AudioResampler* resampler = AudioResampler::create(format, channels,
Andy Hung6582f2b2014-01-03 12:30:41 -0800382 output_freq, quality);
Andy Hungdf383a52014-04-09 19:10:15 -0700383
Andy Hung6582f2b2014-01-03 12:30:41 -0800384 resampler->setSampleRate(input_freq);
Andy Hung5e58b0a2014-06-23 19:07:29 -0700385 resampler->setVolume(AudioResampler::UNITY_GAIN_FLOAT, AudioResampler::UNITY_GAIN_FLOAT);
Andy Hung6582f2b2014-01-03 12:30:41 -0800386
387 if (profileResample) {
388 /*
389 * For profiling on mobile devices, upon experimentation
390 * it is better to run a few trials with a shorter loop limit,
391 * and take the minimum time.
392 *
393 * Long tests can cause CPU temperature to build up and thermal throttling
394 * to reduce CPU frequency.
395 *
396 * For frequency checks (index=0, or 1, etc.):
397 * "cat /sys/devices/system/cpu/cpu${index}/cpufreq/scaling_*_freq"
398 *
399 * For temperature checks (index=0, or 1, etc.):
400 * "cat /sys/class/thermal/thermal_zone${index}/temp"
401 *
402 * Another way to avoid thermal throttling is to fix the CPU frequency
403 * at a lower level which prevents excessive temperatures.
404 */
405 const int trials = 4;
406 const int looplimit = 4;
407 timespec start, end;
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700408 int64_t time = 0;
Andy Hung6582f2b2014-01-03 12:30:41 -0800409
410 for (int n = 0; n < trials; ++n) {
411 clock_gettime(CLOCK_MONOTONIC, &start);
412 for (int i = 0; i < looplimit; ++i) {
Andy Hungdf383a52014-04-09 19:10:15 -0700413 resampler->resample((int*) output_vaddr, output_frames, &provider);
Andy Hung6582f2b2014-01-03 12:30:41 -0800414 provider.reset(); // during benchmarking reset only the provider
415 }
416 clock_gettime(CLOCK_MONOTONIC, &end);
417 int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
418 int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
419 int64_t diff_ns = end_ns - start_ns;
420 if (n == 0 || diff_ns < time) {
421 time = diff_ns; // save the best out of our trials.
422 }
423 }
424 // Mfrms/s is "Millions of output frames per second".
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700425 printf("quality: %d channels: %d msec: %" PRId64 " Mfrms/s: %.2lf\n",
Andy Hungdf383a52014-04-09 19:10:15 -0700426 quality, channels, time/1000000, output_frames * looplimit / (time / 1e9) / 1e6);
Andy Hung86eae0e2013-12-09 12:12:46 -0800427 resampler->reset();
Andy Hung6b667dd2015-02-06 15:05:37 -0800428
429 // TODO fix legacy bug: reset does not clear buffers.
430 // delete and recreate resampler here.
431 delete resampler;
432 resampler = AudioResampler::create(format, channels,
433 output_freq, quality);
434 resampler->setSampleRate(input_freq);
435 resampler->setVolume(AudioResampler::UNITY_GAIN_FLOAT, AudioResampler::UNITY_GAIN_FLOAT);
Andy Hung86eae0e2013-12-09 12:12:46 -0800436 }
437
Mathias Agopian3f717612012-11-04 18:49:14 -0800438 memset(output_vaddr, 0, output_size);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800439 if (gVerbose) {
Andy Hungdf383a52014-04-09 19:10:15 -0700440 printf("resample() %zu output frames\n", output_frames);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800441 }
Glenn Kastenc52b0332014-02-21 11:35:14 -0800442 if (Ovalues.isEmpty()) {
Andy Hungdf383a52014-04-09 19:10:15 -0700443 Ovalues.push(output_frames);
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800444 }
Andy Hungdf383a52014-04-09 19:10:15 -0700445 for (size_t i = 0, j = 0; i < output_frames; ) {
Glenn Kastenc52b0332014-02-21 11:35:14 -0800446 size_t thisFrames = Ovalues[j++];
447 if (j >= Ovalues.size()) {
448 j = 0;
449 }
Andy Hungdf383a52014-04-09 19:10:15 -0700450 if (thisFrames == 0 || thisFrames > output_frames - i) {
451 thisFrames = output_frames - i;
Glenn Kastenc52b0332014-02-21 11:35:14 -0800452 }
Andy Hungdf383a52014-04-09 19:10:15 -0700453 resampler->resample((int*) output_vaddr + output_channels*i, thisFrames, &provider);
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800454 i += thisFrames;
455 }
Glenn Kastene00eefe2013-12-17 13:54:29 -0800456 if (gVerbose) {
457 printf("resample() complete\n");
458 }
459 resampler->reset();
460 if (gVerbose) {
461 printf("reset() complete\n");
462 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800463 delete resampler;
464 resampler = NULL;
Mathias Agopian3f717612012-11-04 18:49:14 -0800465
Andy Hung78136682014-04-08 17:59:59 -0700466 // For float processing, convert output format from float to Q4.27,
467 // which is then converted to int16_t for final storage.
468 if (useFloat) {
469 memcpy_to_q4_27_from_float(reinterpret_cast<int32_t*>(output_vaddr),
Andy Hungdf383a52014-04-09 19:10:15 -0700470 reinterpret_cast<float*>(output_vaddr), output_frames * output_channels);
Andy Hung78136682014-04-08 17:59:59 -0700471 }
472
Andy Hungdf383a52014-04-09 19:10:15 -0700473 // mono takes left channel only (out of stereo output pair)
474 // stereo and multichannel preserve all channels.
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700475 int32_t* out = (int32_t*) output_vaddr;
Andy Hungdf383a52014-04-09 19:10:15 -0700476 int16_t* convert = (int16_t*) malloc(output_frames * channels * sizeof(int16_t));
Andy Hung86eae0e2013-12-09 12:12:46 -0800477
Andy Hung5e58b0a2014-06-23 19:07:29 -0700478 const int volumeShift = 12; // shift requirement for Q4.27 to Q.15
Andy Hung6582f2b2014-01-03 12:30:41 -0800479 // round to half towards zero and saturate at int16 (non-dithered)
Andy Hung5e58b0a2014-06-23 19:07:29 -0700480 const int roundVal = (1<<(volumeShift-1)) - 1; // volumePrecision > 0
Andy Hung6582f2b2014-01-03 12:30:41 -0800481
Andy Hungdf383a52014-04-09 19:10:15 -0700482 for (size_t i = 0; i < output_frames; i++) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800483 for (int j = 0; j < channels; j++) {
Andy Hungdf383a52014-04-09 19:10:15 -0700484 int32_t s = out[i * output_channels + j] + roundVal; // add offset here
Andy Hung6582f2b2014-01-03 12:30:41 -0800485 if (s < 0) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700486 s = (s + 1) >> volumeShift; // round to 0
Andy Hung6582f2b2014-01-03 12:30:41 -0800487 if (s < -32768) {
488 s = -32768;
489 }
490 } else {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700491 s = s >> volumeShift;
Andy Hung6582f2b2014-01-03 12:30:41 -0800492 if (s > 32767) {
493 s = 32767;
494 }
495 }
Mathias Agopian3f717612012-11-04 18:49:14 -0800496 convert[i * channels + j] = int16_t(s);
497 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700498 }
499
500 // write output to disk
Andy Hungdf383a52014-04-09 19:10:15 -0700501 SF_INFO info;
502 info.frames = 0;
503 info.samplerate = output_freq;
504 info.channels = channels;
505 info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
506 SNDFILE *sf = sf_open(file_out, SFM_WRITE, &info);
507 if (sf == NULL) {
508 perror(file_out);
509 return EXIT_FAILURE;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700510 }
Andy Hungdf383a52014-04-09 19:10:15 -0700511 (void) sf_writef_short(sf, convert, output_frames);
512 sf_close(sf);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700513
Glenn Kastenf5293642013-12-17 14:49:17 -0800514 return EXIT_SUCCESS;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700515}