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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
/*
* The code in this file, together with the rate_arm_asm.s file offers
* an ARM optimised version of the code in rate.cpp. The operation of this
* code should be identical to that of rate.cpp, but faster. The heavy
* lifting is done in the assembler file.
*
* To be as portable as possible we implement the core routines with C
* linkage in assembly, and implement the C++ routines that call into
* the C here. The C++ symbol mangling varies wildly between compilers,
* so this is the simplest way to ensure that the C/C++ combination should
* work on as many ARM based platforms as possible.
*
* Essentially the algorithm herein is the same as that in rate.cpp, so
* anyone seeking to understand this should attempt to understand that
* first. That code was based in turn on code with Copyright 1998 Fabrice
* Bellard - part of SoX (http://sox.sourceforge.net).
* Max Horn adapted that code to the needs of ScummVM and partially rewrote
* it, in the process removing any use of floating point arithmetic. Various
* other improvments over the original code were made.
*/
#include "sound/audiostream.h"
#include "sound/rate.h"
#include "sound/mixer.h"
#include "common/util.h"
//#define DEBUG_RATECONV
namespace Audio {
/**
* The precision of the fractional computations used by the rate converter.
* Normally you should never have to modify this value.
* This stuff is defined in common/frac.h, but we redefine it here as the
* ARM routine we call doesn't respect those definitions.
*/
#define FRAC_BITS 16
#define FRAC_ONE (1<<FRAC_BITS)
/**
* The size of the intermediate input cache. Bigger values may increase
* performance, but only until some point (depends largely on cache size,
* target processor and various other factors), at which it will decrease
* again.
*/
#define INTERMEDIATE_BUFFER_SIZE 512
/**
* Audio rate converter based on simple resampling. Used when no
* interpolation is required.
*
* Limited to sampling frequency <= 65535 Hz.
*/
typedef struct {
const st_sample_t *inPtr;
int inLen;
/** position of how far output is ahead of input */
/** Holds what would have been opos-ipos */
long opos;
/** fractional position increment in the output stream */
long opos_inc;
st_sample_t inBuf[INTERMEDIATE_BUFFER_SIZE];
} SimpleRateDetails;
template<bool stereo, bool reverseStereo>
class SimpleRateConverter : public RateConverter {
protected:
SimpleRateDetails sr;
public:
SimpleRateConverter(st_rate_t inrate, st_rate_t outrate);
int flow(AudioStream &input, st_sample_t *obuf, st_size_t osamp, st_volume_t vol_l, st_volume_t vol_r);
int drain(st_sample_t *obuf, st_size_t osamp, st_volume_t vol) {
return (ST_SUCCESS);
}
};
/*
* Prepare processing.
*/
template<bool stereo, bool reverseStereo>
SimpleRateConverter<stereo, reverseStereo>::SimpleRateConverter(st_rate_t inrate, st_rate_t outrate) {
if (inrate == outrate) {
error("Input and Output rates must be different to use rate effect");
}
if ((inrate % outrate) != 0) {
error("Input rate must be a multiple of Output rate to use rate effect");
}
if (inrate >= 65536 || outrate >= 65536) {
error("rate effect can only handle rates < 65536");
}
sr.opos = 1;
/* increment */
sr.opos_inc = inrate / outrate;
sr.inLen = 0;
}
extern "C" {
#ifndef IPHONE
#define ARM_SimpleRate_M _ARM_SimpleRate_M
#define ARM_SimpleRate_S _ARM_SimpleRate_S
#define ARM_SimpleRate_R _ARM_SimpleRate_R
#endif
}
extern "C" st_sample_t *ARM_SimpleRate_M(
AudioStream &input,
int (*fn)(Audio::AudioStream&,int16*,int),
SimpleRateDetails *sr,
st_sample_t *obuf,
st_size_t osamp,
st_volume_t vol_l,
st_volume_t vol_r);
extern "C" st_sample_t *ARM_SimpleRate_S(
AudioStream &input,
int (*fn)(Audio::AudioStream&,int16*,int),
SimpleRateDetails *sr,
st_sample_t *obuf,
st_size_t osamp,
st_volume_t vol_l,
st_volume_t vol_r);
extern "C" st_sample_t *ARM_SimpleRate_R(
AudioStream &input,
int (*fn)(Audio::AudioStream&,int16*,int),
SimpleRateDetails *sr,
st_sample_t *obuf,
st_size_t osamp,
st_volume_t vol_l,
st_volume_t vol_r);
extern "C" int SimpleRate_readFudge(Audio::AudioStream &input,
int16 *a, int b)
{
#ifdef DEBUG_RATECONV
debug("Reading ptr=%x n%d", a, b);
#endif
return input.readBuffer(a, b);
}
template<bool stereo, bool reverseStereo>
int SimpleRateConverter<stereo, reverseStereo>::flow(AudioStream &input, st_sample_t *obuf, st_size_t osamp, st_volume_t vol_l, st_volume_t vol_r) {
#ifdef DEBUG_RATECONV
debug("Simple st=%d rev=%d", stereo, reverseStereo);
#endif
st_sample_t *ostart = obuf;
if (!stereo) {
obuf = ARM_SimpleRate_M(input,
&SimpleRate_readFudge,
&sr,
obuf, osamp, vol_l, vol_r);
} else if (reverseStereo) {
obuf = ARM_SimpleRate_R(input,
&SimpleRate_readFudge,
&sr,
obuf, osamp, vol_l, vol_r);
} else {
obuf = ARM_SimpleRate_S(input,
&SimpleRate_readFudge,
&sr,
obuf, osamp, vol_l, vol_r);
}
return (obuf-ostart)/2;
}
/**
* Audio rate converter based on simple linear Interpolation.
*
* The use of fractional increment allows us to use no buffer. It
* avoid the problems at the end of the buffer we had with the old
* method which stored a possibly big buffer of size
* lcm(in_rate,out_rate).
*
* Limited to sampling frequency <= 65535 Hz.
*/
typedef struct {
const st_sample_t *inPtr;
int inLen;
/** position of how far output is ahead of input */
/** Holds what would have been opos-ipos<<16 + opos_frac */
long opos;
/** integer position increment in the output stream */
long opos_inc;
/** current sample(s) in the input stream (left/right channel) */
st_sample_t icur[2];
/** last sample(s) in the input stream (left/right channel) */
/** Note, these are deliberately ints, not st_sample_t's */
int32 ilast[2];
st_sample_t inBuf[INTERMEDIATE_BUFFER_SIZE];
} LinearRateDetails;
extern "C" {
#ifndef IPHONE
#define ARM_LinearRate_M _ARM_LinearRate_M
#define ARM_LinearRate_S _ARM_LinearRate_S
#define ARM_LinearRate_R _ARM_LinearRate_R
#endif
}
extern "C" st_sample_t *ARM_LinearRate_M(
AudioStream &input,
int (*fn)(Audio::AudioStream&,int16*,int),
LinearRateDetails *lr,
st_sample_t *obuf,
st_size_t osamp,
st_volume_t vol_l,
st_volume_t vol_r);
extern "C" st_sample_t *ARM_LinearRate_S(
AudioStream &input,
int (*fn)(Audio::AudioStream&,int16*,int),
LinearRateDetails *lr,
st_sample_t *obuf,
st_size_t osamp,
st_volume_t vol_l,
st_volume_t vol_r);
extern "C" st_sample_t *ARM_LinearRate_R(
AudioStream &input,
int (*fn)(Audio::AudioStream&,int16*,int),
LinearRateDetails *lr,
st_sample_t *obuf,
st_size_t osamp,
st_volume_t vol_l,
st_volume_t vol_r);
template<bool stereo, bool reverseStereo>
class LinearRateConverter : public RateConverter {
protected:
LinearRateDetails lr;
public:
LinearRateConverter(st_rate_t inrate, st_rate_t outrate);
int flow(AudioStream &input, st_sample_t *obuf, st_size_t osamp, st_volume_t vol_l, st_volume_t vol_r);
int drain(st_sample_t *obuf, st_size_t osamp, st_volume_t vol) {
return (ST_SUCCESS);
}
};
/*
* Prepare processing.
*/
template<bool stereo, bool reverseStereo>
LinearRateConverter<stereo, reverseStereo>::LinearRateConverter(st_rate_t inrate, st_rate_t outrate) {
unsigned long incr;
if (inrate == outrate) {
error("Input and Output rates must be different to use rate effect");
}
if (inrate >= 65536 || outrate >= 65536) {
error("rate effect can only handle rates < 65536");
}
lr.opos = FRAC_ONE;
/* increment */
incr = (inrate << FRAC_BITS) / outrate;
lr.opos_inc = incr;
lr.ilast[0] = lr.ilast[1] = 32768;
lr.icur[0] = lr.icur[1] = 0;
lr.inLen = 0;
}
/*
* Processed signed long samples from ibuf to obuf.
* Return number of sample pairs processed.
*/
template<bool stereo, bool reverseStereo>
int LinearRateConverter<stereo, reverseStereo>::flow(AudioStream &input, st_sample_t *obuf, st_size_t osamp, st_volume_t vol_l, st_volume_t vol_r) {
#ifdef DEBUG_RATECONV
debug("Linear st=%d rev=%d", stereo, reverseStereo);
#endif
st_sample_t *ostart = obuf;
if (!stereo) {
obuf = ARM_LinearRate_M(input,
&SimpleRate_readFudge,
&lr,
obuf, osamp, vol_l, vol_r);
} else if (reverseStereo) {
obuf = ARM_LinearRate_R(input,
&SimpleRate_readFudge,
&lr,
obuf, osamp, vol_l, vol_r);
} else {
obuf = ARM_LinearRate_S(input,
&SimpleRate_readFudge,
&lr,
obuf, osamp, vol_l, vol_r);
}
return (obuf-ostart)/2;
}
#pragma mark -
/**
* Simple audio rate converter for the case that the inrate equals the outrate.
*/
extern "C" {
#ifndef IPHONE
#define ARM_CopyRate_M _ARM_CopyRate_M
#define ARM_CopyRate_S _ARM_CopyRate_S
#define ARM_CopyRate_R _ARM_CopyRate_R
#endif
}
extern "C" st_sample_t *ARM_CopyRate_M(
st_size_t len,
st_sample_t *obuf,
st_volume_t vol_l,
st_volume_t vol_r,
st_sample_t *_buffer);
extern "C" st_sample_t *ARM_CopyRate_S(
st_size_t len,
st_sample_t *obuf,
st_volume_t vol_l,
st_volume_t vol_r,
st_sample_t *_buffer);
extern "C" st_sample_t *ARM_CopyRate_R(
st_size_t len,
st_sample_t *obuf,
st_volume_t vol_l,
st_volume_t vol_r,
st_sample_t *_buffer);
template<bool stereo, bool reverseStereo>
class CopyRateConverter : public RateConverter {
st_sample_t *_buffer;
st_size_t _bufferSize;
public:
CopyRateConverter() : _buffer(0), _bufferSize(0) {}
~CopyRateConverter() {
free(_buffer);
}
virtual int flow(AudioStream &input, st_sample_t *obuf, st_size_t osamp, st_volume_t vol_l, st_volume_t vol_r) {
assert(input.isStereo() == stereo);
#ifdef DEBUG_RATECONV
debug("Copy st=%d rev=%d", stereo, reverseStereo);
#endif
st_size_t len;
st_sample_t *ostart = obuf;
if (stereo)
osamp *= 2;
// Reallocate temp buffer, if necessary
if (osamp > _bufferSize) {
free(_buffer);
_buffer = (st_sample_t *)malloc(osamp * 2);
_bufferSize = osamp;
}
// Read up to 'osamp' samples into our temporary buffer
len = input.readBuffer(_buffer, osamp);
if (len <= 0)
return 0;
// Mix the data into the output buffer
if (stereo && reverseStereo)
obuf = ARM_CopyRate_R(len, obuf, vol_l, vol_r, _buffer);
else if (stereo)
obuf = ARM_CopyRate_S(len, obuf, vol_l, vol_r, _buffer);
else
obuf = ARM_CopyRate_M(len, obuf, vol_l, vol_r, _buffer);
return (obuf-ostart)/2;
}
virtual int drain(st_sample_t *obuf, st_size_t osamp, st_volume_t vol) {
return (ST_SUCCESS);
}
};
#pragma mark -
/**
* Create and return a RateConverter object for the specified input and output rates.
*/
RateConverter *makeRateConverter(st_rate_t inrate, st_rate_t outrate, bool stereo, bool reverseStereo) {
if (inrate != outrate) {
if ((inrate % outrate) == 0) {
if (stereo) {
if (reverseStereo)
return new SimpleRateConverter<true, true>(inrate, outrate);
else
return new SimpleRateConverter<true, false>(inrate, outrate);
} else
return new SimpleRateConverter<false, false>(inrate, outrate);
} else {
if (stereo) {
if (reverseStereo)
return new LinearRateConverter<true, true>(inrate, outrate);
else
return new LinearRateConverter<true, false>(inrate, outrate);
} else
return new LinearRateConverter<false, false>(inrate, outrate);
}
} else {
if (stereo) {
if (reverseStereo)
return new CopyRateConverter<true, true>();
else
return new CopyRateConverter<true, false>();
} else
return new CopyRateConverter<false, false>();
}
}
} // End of namespace Audio
|