blob: dcd00fcc567a826b3a0fc15c5b9d26d0861ca3a8 [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>
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070030
31using namespace android;
32
Glenn Kastene00eefe2013-12-17 13:54:29 -080033bool gVerbose = false;
34
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070035static int usage(const char* name) {
Glenn Kastene00eefe2013-12-17 13:54:29 -080036 fprintf(stderr,"Usage: %s [-p] [-h] [-v] [-s] [-q {dq|lq|mq|hq|vhq}] [-i input-sample-rate] "
Mathias Agopian3f717612012-11-04 18:49:14 -080037 "[-o output-sample-rate] [<input-file>] <output-file>\n", name);
38 fprintf(stderr," -p enable profiling\n");
39 fprintf(stderr," -h create wav file\n");
Glenn Kastene00eefe2013-12-17 13:54:29 -080040 fprintf(stderr," -v verbose : log buffer provider calls\n");
Mathias Agopian3f717612012-11-04 18:49:14 -080041 fprintf(stderr," -s stereo\n");
42 fprintf(stderr," -q resampler quality\n");
43 fprintf(stderr," dq : default quality\n");
44 fprintf(stderr," lq : low quality\n");
45 fprintf(stderr," mq : medium quality\n");
46 fprintf(stderr," hq : high quality\n");
47 fprintf(stderr," vhq : very high quality\n");
48 fprintf(stderr," -i input file sample rate\n");
49 fprintf(stderr," -o output file sample rate\n");
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070050 return -1;
51}
52
53int main(int argc, char* argv[]) {
54
Mathias Agopian3f717612012-11-04 18:49:14 -080055 const char* const progname = argv[0];
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070056 bool profiling = false;
57 bool writeHeader = false;
Mathias Agopian3f717612012-11-04 18:49:14 -080058 int channels = 1;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070059 int input_freq = 0;
60 int output_freq = 0;
61 AudioResampler::src_quality quality = AudioResampler::DEFAULT_QUALITY;
62
63 int ch;
Glenn Kastene00eefe2013-12-17 13:54:29 -080064 while ((ch = getopt(argc, argv, "phvsq:i:o:")) != -1) {
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070065 switch (ch) {
66 case 'p':
67 profiling = true;
68 break;
69 case 'h':
70 writeHeader = true;
71 break;
Glenn Kastene00eefe2013-12-17 13:54:29 -080072 case 'v':
73 gVerbose = true;
74 break;
Mathias Agopian3f717612012-11-04 18:49:14 -080075 case 's':
76 channels = 2;
77 break;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070078 case 'q':
79 if (!strcmp(optarg, "dq"))
80 quality = AudioResampler::DEFAULT_QUALITY;
81 else if (!strcmp(optarg, "lq"))
82 quality = AudioResampler::LOW_QUALITY;
83 else if (!strcmp(optarg, "mq"))
84 quality = AudioResampler::MED_QUALITY;
85 else if (!strcmp(optarg, "hq"))
86 quality = AudioResampler::HIGH_QUALITY;
87 else if (!strcmp(optarg, "vhq"))
88 quality = AudioResampler::VERY_HIGH_QUALITY;
89 else {
Mathias Agopian3f717612012-11-04 18:49:14 -080090 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070091 return -1;
92 }
93 break;
94 case 'i':
95 input_freq = atoi(optarg);
96 break;
97 case 'o':
98 output_freq = atoi(optarg);
99 break;
100 case '?':
101 default:
Mathias Agopian3f717612012-11-04 18:49:14 -0800102 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700103 return -1;
104 }
105 }
106 argc -= optind;
Mathias Agopian3f717612012-11-04 18:49:14 -0800107 argv += optind;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700108
Mathias Agopian3f717612012-11-04 18:49:14 -0800109 const char* file_in = NULL;
110 const char* file_out = NULL;
111 if (argc == 1) {
112 file_out = argv[0];
113 } else if (argc == 2) {
114 file_in = argv[0];
115 file_out = argv[1];
116 } else {
117 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700118 return -1;
119 }
120
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700121 // ----------------------------------------------------------
122
Mathias Agopian3f717612012-11-04 18:49:14 -0800123 size_t input_size;
124 void* input_vaddr;
125 if (argc == 2) {
126 struct stat st;
127 if (stat(file_in, &st) < 0) {
128 fprintf(stderr, "stat: %s\n", strerror(errno));
129 return -1;
130 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700131
Mathias Agopian3f717612012-11-04 18:49:14 -0800132 int input_fd = open(file_in, O_RDONLY);
133 if (input_fd < 0) {
134 fprintf(stderr, "open: %s\n", strerror(errno));
135 return -1;
136 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700137
Mathias Agopian3f717612012-11-04 18:49:14 -0800138 input_size = st.st_size;
139 input_vaddr = mmap(0, input_size, PROT_READ, MAP_PRIVATE, input_fd, 0);
140 if (input_vaddr == MAP_FAILED ) {
141 fprintf(stderr, "mmap: %s\n", strerror(errno));
142 return -1;
143 }
144 } else {
145 double k = 1000; // Hz / s
146 double time = (input_freq / 2) / k;
147 size_t input_frames = size_t(input_freq * time);
148 input_size = channels * sizeof(int16_t) * input_frames;
149 input_vaddr = malloc(input_size);
150 int16_t* in = (int16_t*)input_vaddr;
151 for (size_t i=0 ; i<input_frames ; i++) {
152 double t = double(i) / input_freq;
153 double y = sin(M_PI * k * t * t);
154 int16_t yi = floor(y * 32767.0 + 0.5);
Glenn Kastenb26e3e92012-11-14 08:32:08 -0800155 for (size_t j=0 ; j<(size_t)channels ; j++) {
Mathias Agopianad9af032012-11-04 15:16:13 -0800156 in[i*channels + j] = yi / (1+j);
Mathias Agopian3f717612012-11-04 18:49:14 -0800157 }
158 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700159 }
160
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700161 // ----------------------------------------------------------
162
163 class Provider: public AudioBufferProvider {
Glenn Kastene00eefe2013-12-17 13:54:29 -0800164 int16_t* const mAddr; // base address
165 const size_t mNumFrames; // total frames
166 const int mChannels;
167 size_t mNextFrame; // index of next frame to provide
168 size_t mUnrel; // number of frames not yet released
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700169 public:
Glenn Kastene00eefe2013-12-17 13:54:29 -0800170 Provider(const void* addr, size_t size, int channels)
171 : mAddr((int16_t*) addr),
172 mNumFrames(size / (channels*sizeof(int16_t))),
173 mChannels(channels),
174 mNextFrame(0), mUnrel(0) {
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700175 }
176 virtual status_t getNextBuffer(Buffer* buffer,
177 int64_t pts = kInvalidPTS) {
Glenn Kastene00eefe2013-12-17 13:54:29 -0800178 size_t requestedFrames = buffer->frameCount;
179 if (requestedFrames > mNumFrames - mNextFrame) {
180 buffer->frameCount = mNumFrames - mNextFrame;
181 }
182 if (gVerbose) {
183 printf("getNextBuffer() requested %u frames out of %u frames available,"
184 " and returned %u frames\n",
185 requestedFrames, mNumFrames - mNextFrame, buffer->frameCount);
186 }
187 mUnrel = buffer->frameCount;
188 if (buffer->frameCount > 0) {
189 buffer->i16 = &mAddr[mChannels * mNextFrame];
190 return NO_ERROR;
191 } else {
192 buffer->i16 = NULL;
193 return NOT_ENOUGH_DATA;
194 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700195 }
196 virtual void releaseBuffer(Buffer* buffer) {
Glenn Kastene00eefe2013-12-17 13:54:29 -0800197 if (buffer->frameCount > mUnrel) {
198 fprintf(stderr, "ERROR releaseBuffer() released %u frames but only %u available "
199 "to release\n", buffer->frameCount, mUnrel);
200 mNextFrame += mUnrel;
201 mUnrel = 0;
202 } else {
203 if (gVerbose) {
204 printf("releaseBuffer() released %u frames out of %u frames available "
205 "to release\n", buffer->frameCount, mUnrel);
206 }
207 mNextFrame += buffer->frameCount;
208 mUnrel -= buffer->frameCount;
209 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700210 }
Mathias Agopian3f717612012-11-04 18:49:14 -0800211 } provider(input_vaddr, input_size, channels);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700212
Mathias Agopian3f717612012-11-04 18:49:14 -0800213 size_t input_frames = input_size / (channels * sizeof(int16_t));
Glenn Kastene00eefe2013-12-17 13:54:29 -0800214 if (gVerbose) {
215 printf("%u input frames\n", input_frames);
216 }
Mathias Agopian3f717612012-11-04 18:49:14 -0800217 size_t output_size = 2 * 4 * ((int64_t) input_frames * output_freq) / input_freq;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700218 output_size &= ~7; // always stereo, 32-bits
219
220 void* output_vaddr = malloc(output_size);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700221
222 if (profiling) {
Mathias Agopian3f717612012-11-04 18:49:14 -0800223 AudioResampler* resampler = AudioResampler::create(16, channels,
224 output_freq, quality);
225
226 size_t out_frames = output_size/8;
227 resampler->setSampleRate(input_freq);
228 resampler->setVolume(0x1000, 0x1000);
229
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700230 memset(output_vaddr, 0, output_size);
231 timespec start, end;
Glenn Kastenda1a3252013-05-10 09:29:51 -0700232 clock_gettime(CLOCK_MONOTONIC, &start);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700233 resampler->resample((int*) output_vaddr, out_frames, &provider);
Mathias Agopian3f717612012-11-04 18:49:14 -0800234 resampler->resample((int*) output_vaddr, out_frames, &provider);
235 resampler->resample((int*) output_vaddr, out_frames, &provider);
236 resampler->resample((int*) output_vaddr, out_frames, &provider);
Glenn Kastenda1a3252013-05-10 09:29:51 -0700237 clock_gettime(CLOCK_MONOTONIC, &end);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700238 int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
239 int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
Mathias Agopian3f717612012-11-04 18:49:14 -0800240 int64_t time = (end_ns - start_ns)/4;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700241 printf("%f Mspl/s\n", out_frames/(time/1e9)/1e6);
Mathias Agopian3f717612012-11-04 18:49:14 -0800242
243 delete resampler;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700244 }
245
Mathias Agopian3f717612012-11-04 18:49:14 -0800246 AudioResampler* resampler = AudioResampler::create(16, channels,
247 output_freq, quality);
248 size_t out_frames = output_size/8;
249 resampler->setSampleRate(input_freq);
250 resampler->setVolume(0x1000, 0x1000);
251
252 memset(output_vaddr, 0, output_size);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800253 if (gVerbose) {
254 printf("resample() %u output frames\n", out_frames);
255 }
Mathias Agopian3f717612012-11-04 18:49:14 -0800256 resampler->resample((int*) output_vaddr, out_frames, &provider);
Glenn Kastene00eefe2013-12-17 13:54:29 -0800257 if (gVerbose) {
258 printf("resample() complete\n");
259 }
260 resampler->reset();
261 if (gVerbose) {
262 printf("reset() complete\n");
263 }
Mathias Agopian3f717612012-11-04 18:49:14 -0800264
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700265 // down-mix (we just truncate and keep the left channel)
266 int32_t* out = (int32_t*) output_vaddr;
Mathias Agopian3f717612012-11-04 18:49:14 -0800267 int16_t* convert = (int16_t*) malloc(out_frames * channels * sizeof(int16_t));
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700268 for (size_t i = 0; i < out_frames; i++) {
Mathias Agopian3f717612012-11-04 18:49:14 -0800269 for (int j=0 ; j<channels ; j++) {
270 int32_t s = out[i * 2 + j] >> 12;
271 if (s > 32767) s = 32767;
272 else if (s < -32768) s = -32768;
273 convert[i * channels + j] = int16_t(s);
274 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700275 }
276
277 // write output to disk
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700278 if (writeHeader) {
Glenn Kastenf5293642013-12-17 14:49:17 -0800279 SF_INFO info;
280 info.frames = 0;
281 info.samplerate = output_freq;
282 info.channels = channels;
283 info.format = SF_FORMAT_WAV | SF_FORMAT_PCM_16;
284 SNDFILE *sf = sf_open(file_out, SFM_WRITE, &info);
285 if (sf == NULL) {
286 perror(file_out);
287 return EXIT_FAILURE;
288 }
289 (void) sf_writef_short(sf, convert, out_frames);
290 sf_close(sf);
291 } else {
292 int output_fd = open(file_out, O_WRONLY | O_CREAT | O_TRUNC,
293 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
294 if (output_fd < 0) {
295 perror(file_out);
296 return EXIT_FAILURE;
297 }
298 write(output_fd, convert, out_frames * channels * sizeof(int16_t));
299 close(output_fd);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700300 }
301
Glenn Kastenf5293642013-12-17 14:49:17 -0800302 return EXIT_SUCCESS;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700303}