blob: 7a314cfabbfb6f4dc8b794e9a6215b2b7d8ea2a1 [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>
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070029
30using namespace android;
31
32struct HeaderWav {
33 HeaderWav(size_t size, int nc, int sr, int bits) {
34 strncpy(RIFF, "RIFF", 4);
35 chunkSize = size + sizeof(HeaderWav);
36 strncpy(WAVE, "WAVE", 4);
37 strncpy(fmt, "fmt ", 4);
38 fmtSize = 16;
39 audioFormat = 1;
40 numChannels = nc;
41 samplesRate = sr;
42 byteRate = sr * numChannels * (bits/8);
43 align = nc*(bits/8);
44 bitsPerSample = bits;
45 strncpy(data, "data", 4);
46 dataSize = size;
47 }
48
49 char RIFF[4]; // RIFF
50 uint32_t chunkSize; // File size
51 char WAVE[4]; // WAVE
52 char fmt[4]; // fmt\0
53 uint32_t fmtSize; // fmt size
54 uint16_t audioFormat; // 1=PCM
55 uint16_t numChannels; // num channels
56 uint32_t samplesRate; // sample rate in hz
57 uint32_t byteRate; // Bps
58 uint16_t align; // 2=16-bit mono, 4=16-bit stereo
59 uint16_t bitsPerSample; // bits per sample
60 char data[4]; // "data"
61 uint32_t dataSize; // size
62};
63
64static int usage(const char* name) {
Mathias Agopian3f717612012-11-04 18:49:14 -080065 fprintf(stderr,"Usage: %s [-p] [-h] [-s] [-q {dq|lq|mq|hq|vhq}] [-i input-sample-rate] "
66 "[-o output-sample-rate] [<input-file>] <output-file>\n", name);
67 fprintf(stderr," -p enable profiling\n");
68 fprintf(stderr," -h create wav file\n");
69 fprintf(stderr," -s stereo\n");
70 fprintf(stderr," -q resampler quality\n");
71 fprintf(stderr," dq : default quality\n");
72 fprintf(stderr," lq : low quality\n");
73 fprintf(stderr," mq : medium quality\n");
74 fprintf(stderr," hq : high quality\n");
75 fprintf(stderr," vhq : very high quality\n");
76 fprintf(stderr," -i input file sample rate\n");
77 fprintf(stderr," -o output file sample rate\n");
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070078 return -1;
79}
80
81int main(int argc, char* argv[]) {
82
Mathias Agopian3f717612012-11-04 18:49:14 -080083 const char* const progname = argv[0];
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070084 bool profiling = false;
85 bool writeHeader = false;
Mathias Agopian3f717612012-11-04 18:49:14 -080086 int channels = 1;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070087 int input_freq = 0;
88 int output_freq = 0;
89 AudioResampler::src_quality quality = AudioResampler::DEFAULT_QUALITY;
90
91 int ch;
Mathias Agopian3f717612012-11-04 18:49:14 -080092 while ((ch = getopt(argc, argv, "phsq:i:o:")) != -1) {
Mathias Agopian0fc2cb52012-10-21 01:01:38 -070093 switch (ch) {
94 case 'p':
95 profiling = true;
96 break;
97 case 'h':
98 writeHeader = true;
99 break;
Mathias Agopian3f717612012-11-04 18:49:14 -0800100 case 's':
101 channels = 2;
102 break;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700103 case 'q':
104 if (!strcmp(optarg, "dq"))
105 quality = AudioResampler::DEFAULT_QUALITY;
106 else if (!strcmp(optarg, "lq"))
107 quality = AudioResampler::LOW_QUALITY;
108 else if (!strcmp(optarg, "mq"))
109 quality = AudioResampler::MED_QUALITY;
110 else if (!strcmp(optarg, "hq"))
111 quality = AudioResampler::HIGH_QUALITY;
112 else if (!strcmp(optarg, "vhq"))
113 quality = AudioResampler::VERY_HIGH_QUALITY;
114 else {
Mathias Agopian3f717612012-11-04 18:49:14 -0800115 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700116 return -1;
117 }
118 break;
119 case 'i':
120 input_freq = atoi(optarg);
121 break;
122 case 'o':
123 output_freq = atoi(optarg);
124 break;
125 case '?':
126 default:
Mathias Agopian3f717612012-11-04 18:49:14 -0800127 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700128 return -1;
129 }
130 }
131 argc -= optind;
Mathias Agopian3f717612012-11-04 18:49:14 -0800132 argv += optind;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700133
Mathias Agopian3f717612012-11-04 18:49:14 -0800134 const char* file_in = NULL;
135 const char* file_out = NULL;
136 if (argc == 1) {
137 file_out = argv[0];
138 } else if (argc == 2) {
139 file_in = argv[0];
140 file_out = argv[1];
141 } else {
142 usage(progname);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700143 return -1;
144 }
145
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700146 // ----------------------------------------------------------
147
Mathias Agopian3f717612012-11-04 18:49:14 -0800148 size_t input_size;
149 void* input_vaddr;
150 if (argc == 2) {
151 struct stat st;
152 if (stat(file_in, &st) < 0) {
153 fprintf(stderr, "stat: %s\n", strerror(errno));
154 return -1;
155 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700156
Mathias Agopian3f717612012-11-04 18:49:14 -0800157 int input_fd = open(file_in, O_RDONLY);
158 if (input_fd < 0) {
159 fprintf(stderr, "open: %s\n", strerror(errno));
160 return -1;
161 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700162
Mathias Agopian3f717612012-11-04 18:49:14 -0800163 input_size = st.st_size;
164 input_vaddr = mmap(0, input_size, PROT_READ, MAP_PRIVATE, input_fd, 0);
165 if (input_vaddr == MAP_FAILED ) {
166 fprintf(stderr, "mmap: %s\n", strerror(errno));
167 return -1;
168 }
169 } else {
170 double k = 1000; // Hz / s
171 double time = (input_freq / 2) / k;
172 size_t input_frames = size_t(input_freq * time);
173 input_size = channels * sizeof(int16_t) * input_frames;
174 input_vaddr = malloc(input_size);
175 int16_t* in = (int16_t*)input_vaddr;
176 for (size_t i=0 ; i<input_frames ; i++) {
177 double t = double(i) / input_freq;
178 double y = sin(M_PI * k * t * t);
179 int16_t yi = floor(y * 32767.0 + 0.5);
Glenn Kastenb26e3e92012-11-14 08:32:08 -0800180 for (size_t j=0 ; j<(size_t)channels ; j++) {
Mathias Agopianad9af032012-11-04 15:16:13 -0800181 in[i*channels + j] = yi / (1+j);
Mathias Agopian3f717612012-11-04 18:49:14 -0800182 }
183 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700184 }
185
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700186 // ----------------------------------------------------------
187
188 class Provider: public AudioBufferProvider {
189 int16_t* mAddr;
190 size_t mNumFrames;
191 public:
Mathias Agopian3f717612012-11-04 18:49:14 -0800192 Provider(const void* addr, size_t size, int channels) {
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700193 mAddr = (int16_t*) addr;
Mathias Agopian3f717612012-11-04 18:49:14 -0800194 mNumFrames = size / (channels*sizeof(int16_t));
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700195 }
196 virtual status_t getNextBuffer(Buffer* buffer,
197 int64_t pts = kInvalidPTS) {
198 buffer->frameCount = mNumFrames;
199 buffer->i16 = mAddr;
200 return NO_ERROR;
201 }
202 virtual void releaseBuffer(Buffer* buffer) {
203 }
Mathias Agopian3f717612012-11-04 18:49:14 -0800204 } provider(input_vaddr, input_size, channels);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700205
Mathias Agopian3f717612012-11-04 18:49:14 -0800206 size_t input_frames = input_size / (channels * sizeof(int16_t));
207 size_t output_size = 2 * 4 * ((int64_t) input_frames * output_freq) / input_freq;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700208 output_size &= ~7; // always stereo, 32-bits
209
210 void* output_vaddr = malloc(output_size);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700211
212 if (profiling) {
Mathias Agopian3f717612012-11-04 18:49:14 -0800213 AudioResampler* resampler = AudioResampler::create(16, channels,
214 output_freq, quality);
215
216 size_t out_frames = output_size/8;
217 resampler->setSampleRate(input_freq);
218 resampler->setVolume(0x1000, 0x1000);
219
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700220 memset(output_vaddr, 0, output_size);
221 timespec start, end;
Glenn Kastenda1a3252013-05-10 09:29:51 -0700222 clock_gettime(CLOCK_MONOTONIC, &start);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700223 resampler->resample((int*) output_vaddr, out_frames, &provider);
Mathias Agopian3f717612012-11-04 18:49:14 -0800224 resampler->resample((int*) output_vaddr, out_frames, &provider);
225 resampler->resample((int*) output_vaddr, out_frames, &provider);
226 resampler->resample((int*) output_vaddr, out_frames, &provider);
Glenn Kastenda1a3252013-05-10 09:29:51 -0700227 clock_gettime(CLOCK_MONOTONIC, &end);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700228 int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
229 int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
Mathias Agopian3f717612012-11-04 18:49:14 -0800230 int64_t time = (end_ns - start_ns)/4;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700231 printf("%f Mspl/s\n", out_frames/(time/1e9)/1e6);
Mathias Agopian3f717612012-11-04 18:49:14 -0800232
233 delete resampler;
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700234 }
235
Mathias Agopian3f717612012-11-04 18:49:14 -0800236 AudioResampler* resampler = AudioResampler::create(16, channels,
237 output_freq, quality);
238 size_t out_frames = output_size/8;
239 resampler->setSampleRate(input_freq);
240 resampler->setVolume(0x1000, 0x1000);
241
242 memset(output_vaddr, 0, output_size);
243 resampler->resample((int*) output_vaddr, out_frames, &provider);
244
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700245 // down-mix (we just truncate and keep the left channel)
246 int32_t* out = (int32_t*) output_vaddr;
Mathias Agopian3f717612012-11-04 18:49:14 -0800247 int16_t* convert = (int16_t*) malloc(out_frames * channels * sizeof(int16_t));
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700248 for (size_t i = 0; i < out_frames; i++) {
Mathias Agopian3f717612012-11-04 18:49:14 -0800249 for (int j=0 ; j<channels ; j++) {
250 int32_t s = out[i * 2 + j] >> 12;
251 if (s > 32767) s = 32767;
252 else if (s < -32768) s = -32768;
253 convert[i * channels + j] = int16_t(s);
254 }
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700255 }
256
257 // write output to disk
Mathias Agopian3f717612012-11-04 18:49:14 -0800258 int output_fd = open(file_out, O_WRONLY | O_CREAT | O_TRUNC,
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700259 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
260 if (output_fd < 0) {
261 fprintf(stderr, "open: %s\n", strerror(errno));
262 return -1;
263 }
264
265 if (writeHeader) {
Mathias Agopian3f717612012-11-04 18:49:14 -0800266 HeaderWav wav(out_frames * channels * sizeof(int16_t), channels, output_freq, 16);
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700267 write(output_fd, &wav, sizeof(wav));
268 }
269
Mathias Agopian3f717612012-11-04 18:49:14 -0800270 write(output_fd, convert, out_frames * channels * sizeof(int16_t));
Mathias Agopian0fc2cb52012-10-21 01:01:38 -0700271 close(output_fd);
272
273 return 0;
274}