blob: f178bdedda3c957c79096963b0a136a6d99e1253 [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>
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -070030#include <android-base/macros.h>
Glenn Kastenc52b0332014-02-21 11:35:14 -080031#include <utils/Vector.h>
Andy Hung3348e362014-07-07 10:21:44 -070032#include <media/AudioBufferProvider.h>
Andy Hung068561c2017-01-03 17:09:32 -080033#include <media/AudioResampler.h>
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070034
35using namespace android;
36
Andy Hung78136682014-04-08 17:59:59 -070037static bool gVerbose = false;
Glenn Kastene00eefe2013-12-17 13:54:29 -080038
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070039static int usage(const char* name) {
Andy Hungdf383a52014-04-09 19:10:15 -070040 fprintf(stderr,"Usage: %s [-p] [-f] [-F] [-v] [-c channels]"
41 " [-q {dq|lq|mq|hq|vhq|dlq|dmq|dhq}]"
Andy Hung78136682014-04-08 17:59:59 -070042 " [-i input-sample-rate] [-o output-sample-rate]"
43 " [-O csv] [-P csv] [<input-file>]"
Andy Hung86eae0e2013-12-09 12:12:46 -080044 " <output-file>\n", name);
Mathias Agopian3f717612012-11-04 18:49:14 -080045 fprintf(stderr," -p enable profiling\n");
Andy Hung78136682014-04-08 17:59:59 -070046 fprintf(stderr," -f enable filter profiling\n");
47 fprintf(stderr," -F enable floating point -q {dlq|dmq|dhq} only");
Glenn Kastene00eefe2013-12-17 13:54:29 -080048 fprintf(stderr," -v verbose : log buffer provider calls\n");
Andy Hungdf383a52014-04-09 19:10:15 -070049 fprintf(stderr," -c # channels (1-2 for lq|mq|hq; 1-8 for dlq|dmq|dhq)\n");
Mathias Agopian3f717612012-11-04 18:49:14 -080050 fprintf(stderr," -q resampler quality\n");
51 fprintf(stderr," dq : default quality\n");
52 fprintf(stderr," lq : low quality\n");
53 fprintf(stderr," mq : medium quality\n");
54 fprintf(stderr," hq : high quality\n");
55 fprintf(stderr," vhq : very high quality\n");
Andy Hung86eae0e2013-12-09 12:12:46 -080056 fprintf(stderr," dlq : dynamic low quality\n");
57 fprintf(stderr," dmq : dynamic medium quality\n");
58 fprintf(stderr," dhq : dynamic high quality\n");
Glenn Kastenbd72d222013-12-17 15:22:08 -080059 fprintf(stderr," -i input file sample rate (ignored if input file is specified)\n");
Mathias Agopian3f717612012-11-04 18:49:14 -080060 fprintf(stderr," -o output file sample rate\n");
Glenn Kastenc52b0332014-02-21 11:35:14 -080061 fprintf(stderr," -O # frames output per call to resample() in CSV format\n");
62 fprintf(stderr," -P # frames provided per call to resample() in CSV format\n");
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070063 return -1;
64}
65
Glenn Kastenc52b0332014-02-21 11:35:14 -080066// Convert a list of integers in CSV format to a Vector of those values.
67// Returns the number of elements in the list, or -1 on error.
68int parseCSV(const char *string, Vector<int>& values)
69{
70 // pass 1: count the number of values and do syntax check
71 size_t numValues = 0;
72 bool hadDigit = false;
73 for (const char *p = string; ; ) {
74 switch (*p++) {
75 case '0': case '1': case '2': case '3': case '4':
76 case '5': case '6': case '7': case '8': case '9':
77 hadDigit = true;
78 break;
79 case '\0':
80 if (hadDigit) {
81 // pass 2: allocate and initialize vector of values
82 values.resize(++numValues);
83 values.editItemAt(0) = atoi(p = optarg);
84 for (size_t i = 1; i < numValues; ) {
85 if (*p++ == ',') {
86 values.editItemAt(i++) = atoi(p);
87 }
88 }
89 return numValues;
90 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -070091 FALLTHROUGH_INTENDED;
Glenn Kastenc52b0332014-02-21 11:35:14 -080092 case ',':
93 if (hadDigit) {
94 hadDigit = false;
95 numValues++;
96 break;
97 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -070098 FALLTHROUGH_INTENDED;
Glenn Kastenc52b0332014-02-21 11:35:14 -080099 default:
100 return -1;
101 }
102 }
103}
104
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700105int main(int argc, char* argv[]) {
Mathias Agopian3f717612012-11-04 18:49:14 -0800106 const char* const progname = argv[0];
Andy Hung6582f2b2014-01-03 12:30:41 -0800107 bool profileResample = false;
108 bool profileFilter = false;
Andy Hung78136682014-04-08 17:59:59 -0700109 bool useFloat = false;
Mathias Agopian3f717612012-11-04 18:49:14 -0800110 int channels = 1;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700111 int input_freq = 0;
112 int output_freq = 0;
113 AudioResampler::src_quality quality = AudioResampler::DEFAULT_QUALITY;
Glenn Kastenc52b0332014-02-21 11:35:14 -0800114 Vector<int> Ovalues;
115 Vector<int> Pvalues;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700116
117 int ch;
Andy Hungdf383a52014-04-09 19:10:15 -0700118 while ((ch = getopt(argc, argv, "pfFvc:q:i:o:O:P:")) != -1) {
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700119 switch (ch) {
120 case 'p':
Andy Hung6582f2b2014-01-03 12:30:41 -0800121 profileResample = true;
122 break;
123 case 'f':
124 profileFilter = true;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700125 break;
Andy Hung78136682014-04-08 17:59:59 -0700126 case 'F':
127 useFloat = true;
128 break;
Glenn Kastene00eefe2013-12-17 13:54:29 -0800129 case 'v':
130 gVerbose = true;
131 break;
Andy Hungdf383a52014-04-09 19:10:15 -0700132 case 'c':
133 channels = atoi(optarg);
Mathias Agopian3f717612012-11-04 18:49:14 -0800134 break;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700135 case 'q':
136 if (!strcmp(optarg, "dq"))
137 quality = AudioResampler::DEFAULT_QUALITY;
138 else if (!strcmp(optarg, "lq"))
139 quality = AudioResampler::LOW_QUALITY;
140 else if (!strcmp(optarg, "mq"))
141 quality = AudioResampler::MED_QUALITY;
142 else if (!strcmp(optarg, "hq"))
143 quality = AudioResampler::HIGH_QUALITY;
144 else if (!strcmp(optarg, "vhq"))
145 quality = AudioResampler::VERY_HIGH_QUALITY;
Andy Hung86eae0e2013-12-09 12:12:46 -0800146 else if (!strcmp(optarg, "dlq"))
147 quality = AudioResampler::DYN_LOW_QUALITY;
148 else if (!strcmp(optarg, "dmq"))
149 quality = AudioResampler::DYN_MED_QUALITY;
150 else if (!strcmp(optarg, "dhq"))
151 quality = AudioResampler::DYN_HIGH_QUALITY;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700152 else {
Mathias Agopian3f717612012-11-04 18:49:14 -0800153 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700154 return -1;
155 }
156 break;
157 case 'i':
158 input_freq = atoi(optarg);
159 break;
160 case 'o':
161 output_freq = atoi(optarg);
162 break;
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800163 case 'O':
Glenn Kastenc52b0332014-02-21 11:35:14 -0800164 if (parseCSV(optarg, Ovalues) < 0) {
165 fprintf(stderr, "incorrect syntax for -O option\n");
166 return -1;
167 }
168 break;
169 case 'P':
170 if (parseCSV(optarg, Pvalues) < 0) {
171 fprintf(stderr, "incorrect syntax for -P option\n");
172 return -1;
173 }
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800174 break;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700175 case '?':
176 default:
Mathias Agopian3f717612012-11-04 18:49:14 -0800177 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700178 return -1;
179 }
180 }
Andy Hung78136682014-04-08 17:59:59 -0700181
Andy Hungdf383a52014-04-09 19:10:15 -0700182 if (channels < 1
183 || channels > (quality < AudioResampler::DYN_LOW_QUALITY ? 2 : 8)) {
184 fprintf(stderr, "invalid number of audio channels %d\n", channels);
185 return -1;
186 }
Andy Hung78136682014-04-08 17:59:59 -0700187 if (useFloat && quality < AudioResampler::DYN_LOW_QUALITY) {
188 fprintf(stderr, "float processing is only possible for dynamic resamplers\n");
189 return -1;
190 }
191
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700192 argc -= optind;
Mathias Agopian3f717612012-11-04 18:49:14 -0800193 argv += optind;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700194
Mathias Agopian3f717612012-11-04 18:49:14 -0800195 const char* file_in = NULL;
196 const char* file_out = NULL;
197 if (argc == 1) {
198 file_out = argv[0];
199 } else if (argc == 2) {
200 file_in = argv[0];
201 file_out = argv[1];
202 } else {
203 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700204 return -1;
205 }
206
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700207 // ----------------------------------------------------------
208
Mathias Agopian3f717612012-11-04 18:49:14 -0800209 size_t input_size;
210 void* input_vaddr;
211 if (argc == 2) {
Glenn Kastenbd72d222013-12-17 15:22:08 -0800212 SF_INFO info;
213 info.format = 0;
214 SNDFILE *sf = sf_open(file_in, SFM_READ, &info);
215 if (sf == NULL) {
216 perror(file_in);
217 return EXIT_FAILURE;
Mathias Agopian3f717612012-11-04 18:49:14 -0800218 }
Glenn Kastenbd72d222013-12-17 15:22:08 -0800219 input_size = info.frames * info.channels * sizeof(short);
220 input_vaddr = malloc(input_size);
221 (void) sf_readf_short(sf, (short *) input_vaddr, info.frames);
222 sf_close(sf);
223 channels = info.channels;
224 input_freq = info.samplerate;
Mathias Agopian3f717612012-11-04 18:49:14 -0800225 } else {
Andy Hung86eae0e2013-12-09 12:12:46 -0800226 // data for testing is exactly (input sampling rate/1000)/2 seconds
227 // so 44.1khz input is 22.05 seconds
Mathias Agopian3f717612012-11-04 18:49:14 -0800228 double k = 1000; // Hz / s
229 double time = (input_freq / 2) / k;
230 size_t input_frames = size_t(input_freq * time);
231 input_size = channels * sizeof(int16_t) * input_frames;
232 input_vaddr = malloc(input_size);
233 int16_t* in = (int16_t*)input_vaddr;
234 for (size_t i=0 ; i<input_frames ; i++) {
235 double t = double(i) / input_freq;
236 double y = sin(M_PI * k * t * t);
237 int16_t yi = floor(y * 32767.0 + 0.5);
Andy Hungdf383a52014-04-09 19:10:15 -0700238 for (int j = 0; j < channels; j++) {
239 in[i*channels + j] = yi / (1 + j);
Mathias Agopian3f717612012-11-04 18:49:14 -0800240 }
241 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700242 }
Andy Hungdf383a52014-04-09 19:10:15 -0700243 size_t input_framesize = channels * sizeof(int16_t);
244 size_t input_frames = input_size / input_framesize;
Andy Hung78136682014-04-08 17:59:59 -0700245
246 // For float processing, convert input int16_t to float array
247 if (useFloat) {
248 void *new_vaddr;
249
Andy Hungdf383a52014-04-09 19:10:15 -0700250 input_framesize = channels * sizeof(float);
251 input_size = input_frames * input_framesize;
Andy Hung78136682014-04-08 17:59:59 -0700252 new_vaddr = malloc(input_size);
253 memcpy_to_float_from_i16(reinterpret_cast<float*>(new_vaddr),
254 reinterpret_cast<int16_t*>(input_vaddr), input_frames * channels);
255 free(input_vaddr);
256 input_vaddr = new_vaddr;
257 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700258
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700259 // ----------------------------------------------------------
260
261 class Provider: public AudioBufferProvider {
Andy Hung78136682014-04-08 17:59:59 -0700262 const void* mAddr; // base address
Glenn Kastene00eefe2013-12-17 13:54:29 -0800263 const size_t mNumFrames; // total frames
Andy Hung78136682014-04-08 17:59:59 -0700264 const size_t mFrameSize; // size of each frame in bytes
Glenn Kastene00eefe2013-12-17 13:54:29 -0800265 size_t mNextFrame; // index of next frame to provide
266 size_t mUnrel; // number of frames not yet released
Glenn Kastenc52b0332014-02-21 11:35:14 -0800267 const Vector<int> mPvalues; // number of frames provided per call
268 size_t mNextPidx; // index of next entry in mPvalues to use
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700269 public:
Andy Hung78136682014-04-08 17:59:59 -0700270 Provider(const void* addr, size_t frames, size_t frameSize, const Vector<int>& Pvalues)
271 : mAddr(addr),
272 mNumFrames(frames),
273 mFrameSize(frameSize),
Glenn Kastenc52b0332014-02-21 11:35:14 -0800274 mNextFrame(0), mUnrel(0), mPvalues(Pvalues), mNextPidx(0) {
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700275 }
Glenn Kastend79072e2016-01-06 08:41:20 -0800276 virtual status_t getNextBuffer(Buffer* buffer) {
Glenn Kastene00eefe2013-12-17 13:54:29 -0800277 size_t requestedFrames = buffer->frameCount;
278 if (requestedFrames > mNumFrames - mNextFrame) {
279 buffer->frameCount = mNumFrames - mNextFrame;
280 }
Glenn Kastenc52b0332014-02-21 11:35:14 -0800281 if (!mPvalues.isEmpty()) {
282 size_t provided = mPvalues[mNextPidx++];
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700283 printf("mPvalue[%zu]=%zu not %zu\n", mNextPidx-1, provided, buffer->frameCount);
Glenn Kastenc52b0332014-02-21 11:35:14 -0800284 if (provided < buffer->frameCount) {
285 buffer->frameCount = provided;
286 }
287 if (mNextPidx >= mPvalues.size()) {
288 mNextPidx = 0;
289 }
290 }
Glenn Kastene00eefe2013-12-17 13:54:29 -0800291 if (gVerbose) {
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700292 printf("getNextBuffer() requested %zu frames out of %zu frames available,"
293 " and returned %zu frames\n",
294 requestedFrames, (size_t) (mNumFrames - mNextFrame), buffer->frameCount);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800295 }
296 mUnrel = buffer->frameCount;
297 if (buffer->frameCount > 0) {
Andy Hung78136682014-04-08 17:59:59 -0700298 buffer->raw = (char *)mAddr + mFrameSize * mNextFrame;
Glenn Kastene00eefe2013-12-17 13:54:29 -0800299 return NO_ERROR;
300 } else {
Andy Hung78136682014-04-08 17:59:59 -0700301 buffer->raw = NULL;
Glenn Kastene00eefe2013-12-17 13:54:29 -0800302 return NOT_ENOUGH_DATA;
303 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700304 }
305 virtual void releaseBuffer(Buffer* buffer) {
Glenn Kastene00eefe2013-12-17 13:54:29 -0800306 if (buffer->frameCount > mUnrel) {
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700307 fprintf(stderr, "ERROR releaseBuffer() released %zu frames but only %zu available "
Glenn Kastene00eefe2013-12-17 13:54:29 -0800308 "to release\n", buffer->frameCount, mUnrel);
309 mNextFrame += mUnrel;
310 mUnrel = 0;
311 } else {
312 if (gVerbose) {
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700313 printf("releaseBuffer() released %zu frames out of %zu frames available "
Glenn Kastene00eefe2013-12-17 13:54:29 -0800314 "to release\n", buffer->frameCount, mUnrel);
315 }
316 mNextFrame += buffer->frameCount;
317 mUnrel -= buffer->frameCount;
318 }
Glenn Kasten47f3f5a2013-12-17 16:14:04 -0800319 buffer->frameCount = 0;
Andy Hung78136682014-04-08 17:59:59 -0700320 buffer->raw = NULL;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700321 }
Andy Hung86eae0e2013-12-09 12:12:46 -0800322 void reset() {
323 mNextFrame = 0;
324 }
Andy Hungdf383a52014-04-09 19:10:15 -0700325 } provider(input_vaddr, input_frames, input_framesize, Pvalues);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700326
Glenn Kastene00eefe2013-12-17 13:54:29 -0800327 if (gVerbose) {
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700328 printf("%zu input frames\n", input_frames);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800329 }
Andy Hung78136682014-04-08 17:59:59 -0700330
Andy Hung3348e362014-07-07 10:21:44 -0700331 audio_format_t format = useFloat ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT;
Andy Hungdf383a52014-04-09 19:10:15 -0700332 int output_channels = channels > 2 ? channels : 2; // output is at least stereo samples
333 size_t output_framesize = output_channels * (useFloat ? sizeof(float) : sizeof(int32_t));
334 size_t output_frames = ((int64_t) input_frames * output_freq) / input_freq;
335 size_t output_size = output_frames * output_framesize;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700336
Andy Hung6582f2b2014-01-03 12:30:41 -0800337 if (profileFilter) {
338 // Check how fast sample rate changes are that require filter changes.
339 // The delta sample rate changes must indicate a downsampling ratio,
340 // and must be larger than 10% changes.
341 //
342 // On fast devices, filters should be generated between 0.1ms - 1ms.
343 // (single threaded).
Andy Hung3348e362014-07-07 10:21:44 -0700344 AudioResampler* resampler = AudioResampler::create(format, channels,
Andy Hung6582f2b2014-01-03 12:30:41 -0800345 8000, quality);
346 int looplimit = 100;
Andy Hung86eae0e2013-12-09 12:12:46 -0800347 timespec start, end;
348 clock_gettime(CLOCK_MONOTONIC, &start);
349 for (int i = 0; i < looplimit; ++i) {
Andy Hung6582f2b2014-01-03 12:30:41 -0800350 resampler->setSampleRate(9000);
351 resampler->setSampleRate(12000);
352 resampler->setSampleRate(20000);
353 resampler->setSampleRate(30000);
Andy Hung86eae0e2013-12-09 12:12:46 -0800354 }
355 clock_gettime(CLOCK_MONOTONIC, &end);
356 int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
357 int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
358 int64_t time = end_ns - start_ns;
Andy Hung6582f2b2014-01-03 12:30:41 -0800359 printf("%.2f sample rate changes with filter calculation/sec\n",
360 looplimit * 4 / (time / 1e9));
361
362 // Check how fast sample rate changes are without filter changes.
363 // This should be very fast, probably 0.1us - 1us per sample rate
364 // change.
365 resampler->setSampleRate(1000);
366 looplimit = 1000;
367 clock_gettime(CLOCK_MONOTONIC, &start);
368 for (int i = 0; i < looplimit; ++i) {
369 resampler->setSampleRate(1000+i);
370 }
371 clock_gettime(CLOCK_MONOTONIC, &end);
372 start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
373 end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
374 time = end_ns - start_ns;
375 printf("%.2f sample rate changes without filter calculation/sec\n",
376 looplimit / (time / 1e9));
377 resampler->reset();
378 delete resampler;
379 }
380
381 void* output_vaddr = malloc(output_size);
Andy Hung3348e362014-07-07 10:21:44 -0700382 AudioResampler* resampler = AudioResampler::create(format, channels,
Andy Hung6582f2b2014-01-03 12:30:41 -0800383 output_freq, quality);
Andy Hungdf383a52014-04-09 19:10:15 -0700384
Andy Hung6582f2b2014-01-03 12:30:41 -0800385 resampler->setSampleRate(input_freq);
Andy Hung5e58b0a2014-06-23 19:07:29 -0700386 resampler->setVolume(AudioResampler::UNITY_GAIN_FLOAT, AudioResampler::UNITY_GAIN_FLOAT);
Andy Hung6582f2b2014-01-03 12:30:41 -0800387
388 if (profileResample) {
389 /*
390 * For profiling on mobile devices, upon experimentation
391 * it is better to run a few trials with a shorter loop limit,
392 * and take the minimum time.
393 *
394 * Long tests can cause CPU temperature to build up and thermal throttling
395 * to reduce CPU frequency.
396 *
397 * For frequency checks (index=0, or 1, etc.):
398 * "cat /sys/devices/system/cpu/cpu${index}/cpufreq/scaling_*_freq"
399 *
400 * For temperature checks (index=0, or 1, etc.):
401 * "cat /sys/class/thermal/thermal_zone${index}/temp"
402 *
403 * Another way to avoid thermal throttling is to fix the CPU frequency
404 * at a lower level which prevents excessive temperatures.
405 */
406 const int trials = 4;
407 const int looplimit = 4;
408 timespec start, end;
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700409 int64_t time = 0;
Andy Hung6582f2b2014-01-03 12:30:41 -0800410
411 for (int n = 0; n < trials; ++n) {
412 clock_gettime(CLOCK_MONOTONIC, &start);
413 for (int i = 0; i < looplimit; ++i) {
Andy Hungdf383a52014-04-09 19:10:15 -0700414 resampler->resample((int*) output_vaddr, output_frames, &provider);
Andy Hung6582f2b2014-01-03 12:30:41 -0800415 provider.reset(); // during benchmarking reset only the provider
416 }
417 clock_gettime(CLOCK_MONOTONIC, &end);
418 int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
419 int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
420 int64_t diff_ns = end_ns - start_ns;
421 if (n == 0 || diff_ns < time) {
422 time = diff_ns; // save the best out of our trials.
423 }
424 }
425 // Mfrms/s is "Millions of output frames per second".
Glenn Kasten1e4e4f42014-04-08 10:41:02 -0700426 printf("quality: %d channels: %d msec: %" PRId64 " Mfrms/s: %.2lf\n",
Andy Hungdf383a52014-04-09 19:10:15 -0700427 quality, channels, time/1000000, output_frames * looplimit / (time / 1e9) / 1e6);
Andy Hung86eae0e2013-12-09 12:12:46 -0800428 resampler->reset();
Andy Hung6b667dd2015-02-06 15:05:37 -0800429
430 // TODO fix legacy bug: reset does not clear buffers.
431 // delete and recreate resampler here.
432 delete resampler;
433 resampler = AudioResampler::create(format, channels,
434 output_freq, quality);
435 resampler->setSampleRate(input_freq);
436 resampler->setVolume(AudioResampler::UNITY_GAIN_FLOAT, AudioResampler::UNITY_GAIN_FLOAT);
Andy Hung86eae0e2013-12-09 12:12:46 -0800437 }
438
Mathias Agopian3f717612012-11-04 18:49:14 -0800439 memset(output_vaddr, 0, output_size);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800440 if (gVerbose) {
Andy Hungdf383a52014-04-09 19:10:15 -0700441 printf("resample() %zu output frames\n", output_frames);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800442 }
Glenn Kastenc52b0332014-02-21 11:35:14 -0800443 if (Ovalues.isEmpty()) {
Andy Hungdf383a52014-04-09 19:10:15 -0700444 Ovalues.push(output_frames);
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800445 }
Andy Hungdf383a52014-04-09 19:10:15 -0700446 for (size_t i = 0, j = 0; i < output_frames; ) {
Glenn Kastenc52b0332014-02-21 11:35:14 -0800447 size_t thisFrames = Ovalues[j++];
448 if (j >= Ovalues.size()) {
449 j = 0;
450 }
Andy Hungdf383a52014-04-09 19:10:15 -0700451 if (thisFrames == 0 || thisFrames > output_frames - i) {
452 thisFrames = output_frames - i;
Glenn Kastenc52b0332014-02-21 11:35:14 -0800453 }
Andy Hungdf383a52014-04-09 19:10:15 -0700454 resampler->resample((int*) output_vaddr + output_channels*i, thisFrames, &provider);
Glenn Kasten56df9ff2014-02-17 14:57:27 -0800455 i += thisFrames;
456 }
Glenn Kastene00eefe2013-12-17 13:54:29 -0800457 if (gVerbose) {
458 printf("resample() complete\n");
459 }
460 resampler->reset();
461 if (gVerbose) {
462 printf("reset() complete\n");
463 }
Andy Hung6582f2b2014-01-03 12:30:41 -0800464 delete resampler;
465 resampler = NULL;
Mathias Agopian3f717612012-11-04 18:49:14 -0800466
Andy Hung78136682014-04-08 17:59:59 -0700467 // For float processing, convert output format from float to Q4.27,
468 // which is then converted to int16_t for final storage.
469 if (useFloat) {
470 memcpy_to_q4_27_from_float(reinterpret_cast<int32_t*>(output_vaddr),
Andy Hungdf383a52014-04-09 19:10:15 -0700471 reinterpret_cast<float*>(output_vaddr), output_frames * output_channels);
Andy Hung78136682014-04-08 17:59:59 -0700472 }
473
Andy Hungdf383a52014-04-09 19:10:15 -0700474 // mono takes left channel only (out of stereo output pair)
475 // stereo and multichannel preserve all channels.
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700476 int32_t* out = (int32_t*) output_vaddr;
Andy Hungdf383a52014-04-09 19:10:15 -0700477 int16_t* convert = (int16_t*) malloc(output_frames * channels * sizeof(int16_t));
Andy Hung86eae0e2013-12-09 12:12:46 -0800478
Andy Hung5e58b0a2014-06-23 19:07:29 -0700479 const int volumeShift = 12; // shift requirement for Q4.27 to Q.15
Andy Hung6582f2b2014-01-03 12:30:41 -0800480 // round to half towards zero and saturate at int16 (non-dithered)
Andy Hung5e58b0a2014-06-23 19:07:29 -0700481 const int roundVal = (1<<(volumeShift-1)) - 1; // volumePrecision > 0
Andy Hung6582f2b2014-01-03 12:30:41 -0800482
Andy Hungdf383a52014-04-09 19:10:15 -0700483 for (size_t i = 0; i < output_frames; i++) {
Andy Hung86eae0e2013-12-09 12:12:46 -0800484 for (int j = 0; j < channels; j++) {
Andy Hungdf383a52014-04-09 19:10:15 -0700485 int32_t s = out[i * output_channels + j] + roundVal; // add offset here
Andy Hung6582f2b2014-01-03 12:30:41 -0800486 if (s < 0) {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700487 s = (s + 1) >> volumeShift; // round to 0
Andy Hung6582f2b2014-01-03 12:30:41 -0800488 if (s < -32768) {
489 s = -32768;
490 }
491 } else {
Andy Hung5e58b0a2014-06-23 19:07:29 -0700492 s = s >> volumeShift;
Andy Hung6582f2b2014-01-03 12:30:41 -0800493 if (s > 32767) {
494 s = 32767;
495 }
496 }
Mathias Agopian3f717612012-11-04 18:49:14 -0800497 convert[i * channels + j] = int16_t(s);
498 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700499 }
500
501 // write output to disk
Andy Hungdf383a52014-04-09 19:10:15 -0700502 SF_INFO info;
503 info.frames = 0;
504 info.samplerate = output_freq;
505 info.channels = channels;
506 info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
507 SNDFILE *sf = sf_open(file_out, SFM_WRITE, &info);
508 if (sf == NULL) {
509 perror(file_out);
510 return EXIT_FAILURE;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700511 }
Andy Hungdf383a52014-04-09 19:10:15 -0700512 (void) sf_writef_short(sf, convert, output_frames);
513 sf_close(sf);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700514
Glenn Kastenf5293642013-12-17 14:49:17 -0800515 return EXIT_SUCCESS;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700516}