1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "AudioResampler.h"
#include <media/AudioBufferProvider.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <errno.h>
#include <time.h>
#include <math.h>
using namespace android;
struct HeaderWav {
HeaderWav(size_t size, int nc, int sr, int bits) {
strncpy(RIFF, "RIFF", 4);
chunkSize = size + sizeof(HeaderWav);
strncpy(WAVE, "WAVE", 4);
strncpy(fmt, "fmt ", 4);
fmtSize = 16;
audioFormat = 1;
numChannels = nc;
samplesRate = sr;
byteRate = sr * numChannels * (bits/8);
align = nc*(bits/8);
bitsPerSample = bits;
strncpy(data, "data", 4);
dataSize = size;
}
char RIFF[4]; // RIFF
uint32_t chunkSize; // File size
char WAVE[4]; // WAVE
char fmt[4]; // fmt\0
uint32_t fmtSize; // fmt size
uint16_t audioFormat; // 1=PCM
uint16_t numChannels; // num channels
uint32_t samplesRate; // sample rate in hz
uint32_t byteRate; // Bps
uint16_t align; // 2=16-bit mono, 4=16-bit stereo
uint16_t bitsPerSample; // bits per sample
char data[4]; // "data"
uint32_t dataSize; // size
};
static int usage(const char* name) {
fprintf(stderr,"Usage: %s [-p] [-h] [-s] [-q {dq|lq|mq|hq|vhq}] [-i input-sample-rate] "
"[-o output-sample-rate] [<input-file>] <output-file>\n", name);
fprintf(stderr," -p enable profiling\n");
fprintf(stderr," -h create wav file\n");
fprintf(stderr," -s stereo\n");
fprintf(stderr," -q resampler quality\n");
fprintf(stderr," dq : default quality\n");
fprintf(stderr," lq : low quality\n");
fprintf(stderr," mq : medium quality\n");
fprintf(stderr," hq : high quality\n");
fprintf(stderr," vhq : very high quality\n");
fprintf(stderr," -i input file sample rate\n");
fprintf(stderr," -o output file sample rate\n");
return -1;
}
int main(int argc, char* argv[]) {
const char* const progname = argv[0];
bool profiling = false;
bool writeHeader = false;
int channels = 1;
int input_freq = 0;
int output_freq = 0;
AudioResampler::src_quality quality = AudioResampler::DEFAULT_QUALITY;
int ch;
while ((ch = getopt(argc, argv, "phsq:i:o:")) != -1) {
switch (ch) {
case 'p':
profiling = true;
break;
case 'h':
writeHeader = true;
break;
case 's':
channels = 2;
break;
case 'q':
if (!strcmp(optarg, "dq"))
quality = AudioResampler::DEFAULT_QUALITY;
else if (!strcmp(optarg, "lq"))
quality = AudioResampler::LOW_QUALITY;
else if (!strcmp(optarg, "mq"))
quality = AudioResampler::MED_QUALITY;
else if (!strcmp(optarg, "hq"))
quality = AudioResampler::HIGH_QUALITY;
else if (!strcmp(optarg, "vhq"))
quality = AudioResampler::VERY_HIGH_QUALITY;
else {
usage(progname);
return -1;
}
break;
case 'i':
input_freq = atoi(optarg);
break;
case 'o':
output_freq = atoi(optarg);
break;
case '?':
default:
usage(progname);
return -1;
}
}
argc -= optind;
argv += optind;
const char* file_in = NULL;
const char* file_out = NULL;
if (argc == 1) {
file_out = argv[0];
} else if (argc == 2) {
file_in = argv[0];
file_out = argv[1];
} else {
usage(progname);
return -1;
}
// ----------------------------------------------------------
size_t input_size;
void* input_vaddr;
if (argc == 2) {
struct stat st;
if (stat(file_in, &st) < 0) {
fprintf(stderr, "stat: %s\n", strerror(errno));
return -1;
}
int input_fd = open(file_in, O_RDONLY);
if (input_fd < 0) {
fprintf(stderr, "open: %s\n", strerror(errno));
return -1;
}
input_size = st.st_size;
input_vaddr = mmap(0, input_size, PROT_READ, MAP_PRIVATE, input_fd, 0);
if (input_vaddr == MAP_FAILED ) {
fprintf(stderr, "mmap: %s\n", strerror(errno));
return -1;
}
} else {
double k = 1000; // Hz / s
double time = (input_freq / 2) / k;
size_t input_frames = size_t(input_freq * time);
input_size = channels * sizeof(int16_t) * input_frames;
input_vaddr = malloc(input_size);
int16_t* in = (int16_t*)input_vaddr;
for (size_t i=0 ; i<input_frames ; i++) {
double t = double(i) / input_freq;
double y = sin(M_PI * k * t * t);
int16_t yi = floor(y * 32767.0 + 0.5);
for (size_t j=0 ; j<(size_t)channels ; j++) {
in[i*channels + j] = yi / (1+j);
}
}
}
// ----------------------------------------------------------
class Provider: public AudioBufferProvider {
int16_t* mAddr;
size_t mNumFrames;
public:
Provider(const void* addr, size_t size, int channels) {
mAddr = (int16_t*) addr;
mNumFrames = size / (channels*sizeof(int16_t));
}
virtual status_t getNextBuffer(Buffer* buffer,
int64_t pts = kInvalidPTS) {
buffer->frameCount = mNumFrames;
buffer->i16 = mAddr;
return NO_ERROR;
}
virtual void releaseBuffer(Buffer* buffer) {
}
} provider(input_vaddr, input_size, channels);
size_t input_frames = input_size / (channels * sizeof(int16_t));
size_t output_size = 2 * 4 * ((int64_t) input_frames * output_freq) / input_freq;
output_size &= ~7; // always stereo, 32-bits
void* output_vaddr = malloc(output_size);
if (profiling) {
AudioResampler* resampler = AudioResampler::create(16, channels,
output_freq, quality);
size_t out_frames = output_size/8;
resampler->setSampleRate(input_freq);
resampler->setVolume(0x1000, 0x1000);
memset(output_vaddr, 0, output_size);
timespec start, end;
clock_gettime(CLOCK_MONOTONIC_HR, &start);
resampler->resample((int*) output_vaddr, out_frames, &provider);
resampler->resample((int*) output_vaddr, out_frames, &provider);
resampler->resample((int*) output_vaddr, out_frames, &provider);
resampler->resample((int*) output_vaddr, out_frames, &provider);
clock_gettime(CLOCK_MONOTONIC_HR, &end);
int64_t start_ns = start.tv_sec * 1000000000LL + start.tv_nsec;
int64_t end_ns = end.tv_sec * 1000000000LL + end.tv_nsec;
int64_t time = (end_ns - start_ns)/4;
printf("%f Mspl/s\n", out_frames/(time/1e9)/1e6);
delete resampler;
}
AudioResampler* resampler = AudioResampler::create(16, channels,
output_freq, quality);
size_t out_frames = output_size/8;
resampler->setSampleRate(input_freq);
resampler->setVolume(0x1000, 0x1000);
memset(output_vaddr, 0, output_size);
resampler->resample((int*) output_vaddr, out_frames, &provider);
// down-mix (we just truncate and keep the left channel)
int32_t* out = (int32_t*) output_vaddr;
int16_t* convert = (int16_t*) malloc(out_frames * channels * sizeof(int16_t));
for (size_t i = 0; i < out_frames; i++) {
for (int j=0 ; j<channels ; j++) {
int32_t s = out[i * 2 + j] >> 12;
if (s > 32767) s = 32767;
else if (s < -32768) s = -32768;
convert[i * channels + j] = int16_t(s);
}
}
// write output to disk
int output_fd = open(file_out, O_WRONLY | O_CREAT | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (output_fd < 0) {
fprintf(stderr, "open: %s\n", strerror(errno));
return -1;
}
if (writeHeader) {
HeaderWav wav(out_frames * channels * sizeof(int16_t), channels, output_freq, 16);
write(output_fd, &wav, sizeof(wav));
}
write(output_fd, convert, out_frames * channels * sizeof(int16_t));
close(output_fd);
return 0;
}
|