Skip to content

Latest commit

 

History

History
1057 lines (911 loc) · 31.7 KB

alt_audio_convert.c

File metadata and controls

1057 lines (911 loc) · 31.7 KB
 
Jul 13, 2002
Jul 13, 2002
1
/*
Jun 18, 2002
Jun 18, 2002
2
3
4
* Extended Audio Converter for SDL (Simple DirectMedia Layer)
* Copyright (C) 2002 Frank Ranostaj
* Institute of Applied Physik
Apr 17, 2008
Apr 17, 2008
5
* Johann Wolfgang Goethe-Universität
Jun 18, 2002
Jun 18, 2002
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
* Frankfurt am Main, Germany
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Frank Ranostaj
* ranostaj@stud.uni-frankfurt.de
*
* (This code blatantly abducted for SDL_sound. Thanks, Frank! --ryan.)
*/
May 20, 2002
May 20, 2002
27
Oct 12, 2003
Oct 12, 2003
28
29
30
31
32
33
#if HAVE_CONFIG_H
# include <config.h>
#endif
#if SOUND_USE_ALTCVT
Jun 18, 2002
Jun 18, 2002
34
35
#include "alt_audio_convert.h"
#include <math.h>
May 20, 2002
May 20, 2002
36
Jun 18, 2002
Jun 18, 2002
37
/* just to make sure this is defined... */
May 20, 2002
May 20, 2002
38
Jun 18, 2002
Jun 18, 2002
39
40
41
#ifndef min
#define min(x, y) ( ((x) < (y)) ? (x) : (y) )
#endif
May 20, 2002
May 20, 2002
42
Jun 29, 2002
Jun 29, 2002
43
44
45
46
#ifndef max
#define max(x, y) ( ((x) > (y)) ? (x) : (y) )
#endif
Jun 21, 2002
Jun 21, 2002
47
48
#ifndef abs
#define abs(x) ( ((x) > (0)) ? (x) : -(x) )
Jun 18, 2002
Jun 18, 2002
49
#endif
May 20, 2002
May 20, 2002
50
51
Jul 13, 2002
Jul 13, 2002
52
/* some macros for "parsing" format */
Jun 12, 2002
Jun 12, 2002
53
54
55
#define IS_8BIT(x) ((x).format & 0x0008)
#define IS_16BIT(x) ((x).format & 0x0010)
Jun 18, 2002
Jun 18, 2002
56
#define IS_FLOAT(x) ((x).format & 0x0020)
Jun 12, 2002
Jun 12, 2002
57
#define IS_SIGNED(x) ((x).format & 0x8000)
Jun 18, 2002
Jun 18, 2002
58
#define IS_SYSENDIAN(x) ((~AUDIO_U16SYS ^ (x).format) & 0x1000)
Jul 13, 2002
Jul 13, 2002
59
#define SDL_MSB_POSITION_IN_SHORT ((0x1000 & AUDIO_U16SYS)>>12)
May 20, 2002
May 20, 2002
60
61
Jun 18, 2002
Jun 18, 2002
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
/*-------------------------------------------------------------------------*/
/* the purpose of the RateConverterBuffer is to provide a continous storage
for head and tail of the (sample)-buffer. This allows a simple and
perfomant implemantation of the sample rate converters. Depending of the
operation mode, two layouts for the RateConverterBuffer.inbuffer are
possible:
in the Loop Mode:
... T-4 T-3 T-2 T-1 H+0 H+1 H+2 H+3 H+4 ...
|
linp, finp
in the Single Mode (non Loop):
... T-4 T-3 T-2 T-1 0 0 0 ... 0 0 0 H+0 H+1 H+2 H+3 H+4 ...
| |
linp finp
The RateConverterBuffer allows an accurate attack and decay of the
filters in the rate Converters.
The pointer finp are actually shifted against the depicted position so
that on the first invocation of the rate converter the input of the
filter is nearly complete in the zero region, only one input value is
used. After the calculation of the first output value, the pointer are
incremented or decremented depending on down or up conversion and the
first two input value are taken into account. This procedure repeats
until the filter has processed all zeroes. The distance of the pointer
Jun 21, 2002
Jun 21, 2002
89
movement is stored in flength, always positive.
Jun 18, 2002
Jun 18, 2002
90
91
92
93
94
95
96
Further a pointer cinp to the sample buffer itself is stored. The pointer
to the sample buffer is shifted too, so that on the first use of this
pointer the filter is complete in the sample buffer. The pointer moves
over the sample buffer until it reaches the other end. The distance of
the movement is stored in clength.
Jun 21, 2002
Jun 21, 2002
97
Finally the decay of the filter is done by linp and llength like finp,
Jun 18, 2002
Jun 18, 2002
98
99
100
101
102
103
104
105
106
107
flength, but in reverse order.
buffer denotes the start or the end of the output buffer, depending
on direction of the rate conversion.
All pointer and length referring the buffer as Sint16. All length
are refering to the input buffer */
typedef struct
{
Jun 21, 2002
Jun 21, 2002
108
Sint16 inbuffer[24*_fsize];
Jun 18, 2002
Jun 18, 2002
109
110
Sint16 *finp, *cinp, *linp;
int flength, clength, llength;
Jul 13, 2002
Jul 13, 2002
111
112
Sint16 *buffer;
VarFilter *filter;
Jun 18, 2002
Jun 18, 2002
113
114
} RateConverterBuffer;
Jul 13, 2002
Jul 13, 2002
115
116
117
118
119
120
typedef struct
{
Sint16 carry;
Sint16 pos;
} RateAux;
May 20, 2002
May 20, 2002
121
122
123
124
125
126
127
128
129
130
131
/* Mono (1 channel ) */
#define Suffix(x) x##1
#include "filter_templates.h"
#undef Suffix
/* Stereo (2 channel ) */
#define Suffix(x) x##2
#include "filter_templates.h"
#undef Suffix
Jun 18, 2002
Jun 18, 2002
132
May 20, 2002
May 20, 2002
133
/*-------------------------------------------------------------------------*/
Sep 26, 2002
Sep 26, 2002
134
135
136
137
138
139
140
141
142
143
int Sound_estimateBufferSize( Sound_AudioCVT *Data, int size )
{
size *= Data->len_mult;
size += Data->len_add;
return ( size + 3 ) & -4; /* force Size in multipels of 4 Byte */
}
/*-------------------------------------------------------------------------*/
int Sound_AltConvertAudio( Sound_AudioCVT *Data,
Uint8* buffer, int length, int mode )
May 20, 2002
May 20, 2002
144
{
Jun 12, 2002
Jun 12, 2002
145
146
AdapterC Temp;
int i;
May 20, 2002
May 20, 2002
147
Jun 12, 2002
Jun 12, 2002
148
149
/* Make sure there's a converter */
if( Data == NULL ) {
Sep 26, 2002
Sep 26, 2002
150
SDL_SetError("No converter given");
Jun 12, 2002
Jun 12, 2002
151
152
return(-1);
}
May 20, 2002
May 20, 2002
153
Jun 12, 2002
Jun 12, 2002
154
155
/* Make sure there's data to convert */
if( buffer == NULL ) {
Sep 26, 2002
Sep 26, 2002
156
SDL_SetError("No buffer allocated for conversion");
Jul 13, 2002
Jul 13, 2002
157
158
159
160
return(-1);
}
if( length < 0 ) {
Sep 26, 2002
Sep 26, 2002
161
SDL_SetError("Lenght < 0");
Jun 12, 2002
Jun 12, 2002
162
163
return(-1);
}
May 20, 2002
May 20, 2002
164
Jun 12, 2002
Jun 12, 2002
165
166
167
168
/* Set up the conversion and go! */
Temp.buffer = buffer;
Temp.mode = mode;
Temp.filter = &Data->filter;
May 20, 2002
May 20, 2002
169
Jun 12, 2002
Jun 12, 2002
170
171
for( i = 0; Data->adapter[i] != NULL; i++ )
length = (*Data->adapter[i])( Temp, length);
May 20, 2002
May 20, 2002
172
Jun 12, 2002
Jun 12, 2002
173
174
return length;
}
May 20, 2002
May 20, 2002
175
Jun 12, 2002
Jun 12, 2002
176
177
int Sound_ConvertAudio( Sound_AudioCVT *Data )
{
Jun 25, 2002
Jun 25, 2002
178
int length;
Jun 12, 2002
Jun 12, 2002
179
/* !!! FIXME: Try the looping stuff under certain circumstances? --ryan. */
Sep 26, 2002
Sep 26, 2002
180
length = Sound_AltConvertAudio( Data, Data->buf, Data->len, 0 );
Jun 25, 2002
Jun 25, 2002
181
182
Data->len_cvt = length;
return length;
May 20, 2002
May 20, 2002
183
184
185
}
/*-------------------------------------------------------------------------*/
Jun 12, 2002
Jun 12, 2002
186
static int expand8BitTo16BitSys( AdapterC Data, int length )
May 20, 2002
May 20, 2002
187
{
Jun 12, 2002
Jun 12, 2002
188
int i;
Jul 13, 2002
Jul 13, 2002
189
190
191
Uint8* inp = Data.buffer - 1;
Uint16* buffer = (Uint16*)Data.buffer - 1;
for( i = length + 1; --i; )
Jun 12, 2002
Jun 12, 2002
192
193
194
195
196
197
198
buffer[i] = inp[i]<<8;
return 2*length;
}
static int expand8BitTo16BitWrong( AdapterC Data, int length )
{
int i;
Jul 13, 2002
Jul 13, 2002
199
200
201
Uint8* inp = Data.buffer - 1;
Uint16* buffer = (Uint16*)Data.buffer - 1;
for( i = length + 1; --i; )
Jun 12, 2002
Jun 12, 2002
202
203
204
205
206
207
208
209
buffer[i] = inp[i];
return 2*length;
}
/*-------------------------------------------------------------------------*/
static int expand16BitToFloat( AdapterC Data, int length )
{
int i;
Jul 13, 2002
Jul 13, 2002
210
211
212
Sint16* inp = (Sint16*)Data.buffer - 1;
float* buffer = (float*)Data.buffer - 1;
for( i = length>>1 + 1; --i; )
Jun 12, 2002
Jun 12, 2002
213
214
buffer[i] = inp[i]*(1./32767);
return 2*length;
May 20, 2002
May 20, 2002
215
216
217
}
/*-------------------------------------------------------------------------*/
May 22, 2002
May 22, 2002
218
static int swapBytes( AdapterC Data, int length )
May 20, 2002
May 20, 2002
219
{
Jun 12, 2002
Jun 12, 2002
220
221
222
223
224
225
226
227
228
229
/*
* !!! FIXME !!!
*
*
* Use the faster SDL-Macros to swap
* - Frank
*/
int i;
Uint16 a,b;
Jul 13, 2002
Jul 13, 2002
230
231
Uint16* buffer = (Uint16*) Data.buffer - 1;
for( i = length>>1 + 1; --i; )
Jun 12, 2002
Jun 12, 2002
232
233
234
235
236
{
a = b = buffer[i];
buffer[i] = ( a << 8 ) | ( b >> 8 );
}
return length;
May 20, 2002
May 20, 2002
237
238
239
}
/*-------------------------------------------------------------------------*/
Jun 12, 2002
Jun 12, 2002
240
static int cutFloatTo16Bit( AdapterC Data, int length )
May 20, 2002
May 20, 2002
241
{
Jun 12, 2002
Jun 12, 2002
242
243
244
245
246
247
248
249
250
251
252
253
254
255
int i;
float* inp = (float*) Data.buffer;
Sint16* buffer = (Sint16*) Data.buffer;
length>>=2;
for( i = 0; i < length; i++ )
{
if( inp[i] > 1. )
buffer[i] = 32767;
else if( inp[i] < -1. )
buffer[i] = -32768;
else
buffer[i] = 32767 * inp[i];
}
return 2*length;
May 20, 2002
May 20, 2002
256
257
258
}
/*-------------------------------------------------------------------------*/
Jul 13, 2002
Jul 13, 2002
259
static int cut16BitTo8Bit( AdapterC Data, int length, int off )
May 20, 2002
May 20, 2002
260
{
Jun 12, 2002
Jun 12, 2002
261
int i;
Jul 13, 2002
Jul 13, 2002
262
Uint8* inp = Data.buffer + off;
Jun 12, 2002
Jun 12, 2002
263
264
265
Uint8* buffer = Data.buffer;
length >>= 1;
for( i = 0; i < length; i++ )
Jul 13, 2002
Jul 13, 2002
266
buffer[i] = inp[2*i];
Jun 12, 2002
Jun 12, 2002
267
268
269
return length;
}
Jul 13, 2002
Jul 13, 2002
270
271
272
273
274
static int cut16BitSysTo8Bit( AdapterC Data, int length )
{
return cut16BitTo8Bit( Data, length, SDL_MSB_POSITION_IN_SHORT );
}
Jun 12, 2002
Jun 12, 2002
275
276
static int cut16BitWrongTo8Bit( AdapterC Data, int length )
{
Jul 13, 2002
Jul 13, 2002
277
return cut16BitTo8Bit( Data, length, 1-SDL_MSB_POSITION_IN_SHORT );
Jun 12, 2002
Jun 12, 2002
278
279
280
}
/*-------------------------------------------------------------------------*/
Jun 29, 2002
Jun 29, 2002
281
282
/* poor mans mmx :-) */
static int changeSigned( AdapterC Data, int length, Uint32 XOR )
Jun 12, 2002
Jun 12, 2002
283
284
{
int i;
Jul 13, 2002
Jul 13, 2002
285
286
Uint32* buffer = (Uint32*) Data.buffer - 1;
for( i = ( length + 7 ) >> 2; --i; )
Jun 12, 2002
Jun 12, 2002
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
buffer[i] ^= XOR;
return length;
}
static int changeSigned16BitSys( AdapterC Data, int length )
{
return changeSigned( Data, length, 0x80008000 );
}
static int changeSigned16BitWrong( AdapterC Data, int length )
{
return changeSigned( Data, length, 0x00800080 );
}
static int changeSigned8Bit( AdapterC Data, int length )
{
return changeSigned( Data, length, 0x80808080 );
May 20, 2002
May 20, 2002
304
305
306
}
/*-------------------------------------------------------------------------*/
Jun 12, 2002
Jun 12, 2002
307
308
309
310
311
312
static int convertStereoToMonoS16Bit( AdapterC Data, int length )
{
int i;
Sint16* buffer = (Sint16*) Data.buffer;
Sint16* src = (Sint16*) Data.buffer;
length >>= 2;
Jun 25, 2002
Jun 25, 2002
313
314
for( i = 0; i < length; i++, src+=2 )
buffer[i] = ((int) src[0] + src[1] ) >> 1;
Jun 12, 2002
Jun 12, 2002
315
316
317
318
319
320
321
322
323
return 2*length;
}
static int convertStereoToMonoU16Bit( AdapterC Data, int length )
{
int i;
Uint16* buffer = (Uint16*) Data.buffer;
Uint16* src = (Uint16*) Data.buffer;
length >>= 2;
Jun 25, 2002
Jun 25, 2002
324
325
for( i = 0; i < length; i++, src+=2 )
buffer[i] = ((int) src[0] + src[1] ) >> 1;
Jun 12, 2002
Jun 12, 2002
326
327
328
329
return 2*length;
}
static int convertStereoToMonoS8Bit( AdapterC Data, int length )
May 20, 2002
May 20, 2002
330
{
Jun 12, 2002
Jun 12, 2002
331
332
333
334
int i;
Sint8* buffer = (Sint8*) Data.buffer;
Sint8* src = (Sint8*) Data.buffer;
length >>= 1;
Jun 25, 2002
Jun 25, 2002
335
336
for( i = 0; i < length; i++, src+=2 )
buffer[i] = ((int) src[0] + src[1] ) >> 1;
Jun 12, 2002
Jun 12, 2002
337
338
return length;
}
May 20, 2002
May 20, 2002
339
Jun 12, 2002
Jun 12, 2002
340
341
342
343
344
345
static int convertStereoToMonoU8Bit( AdapterC Data, int length )
{
int i;
Uint8* buffer = (Uint8*) Data.buffer;
Uint8* src = (Uint8*) Data.buffer;
length >>= 1;
Jun 25, 2002
Jun 25, 2002
346
347
for( i = 0; i < length; i++, src+=2 )
buffer[i] = ((int) src[0] + src[1] ) >> 1;
Jun 12, 2002
Jun 12, 2002
348
return length;
May 20, 2002
May 20, 2002
349
350
351
}
/*-------------------------------------------------------------------------*/
Jun 12, 2002
Jun 12, 2002
352
static int convertMonoToStereo16Bit( AdapterC Data, int length )
May 20, 2002
May 20, 2002
353
{
Jun 12, 2002
Jun 12, 2002
354
int i;
Sep 26, 2002
Sep 26, 2002
355
356
357
Uint16* buffer;
Uint16* dst;
Sep 26, 2002
Sep 26, 2002
358
length >>=1;
Sep 26, 2002
Sep 26, 2002
359
360
buffer = (Uint16*)Data.buffer - 1;
dst = (Uint16*)Data.buffer + 2*length - 2;
Sep 26, 2002
Sep 26, 2002
361
for( i = length + 1; --i; dst-=2 )
Jun 29, 2002
Jun 29, 2002
362
dst[0] = dst[1] = buffer[i];
Sep 26, 2002
Sep 26, 2002
363
return 4*length;
Jun 12, 2002
Jun 12, 2002
364
}
May 20, 2002
May 20, 2002
365
Jun 12, 2002
Jun 12, 2002
366
367
368
static int convertMonoToStereo8Bit( AdapterC Data, int length )
{
int i;
Jul 13, 2002
Jul 13, 2002
369
Uint8* buffer = Data.buffer - 1;
Sep 26, 2002
Sep 26, 2002
370
Uint8* dst = Data.buffer + 2*length - 2;
Jul 13, 2002
Jul 13, 2002
371
for( i = length + 1; --i; dst-=2 )
Jul 3, 2002
Jul 3, 2002
372
dst[0] = dst[1] = buffer[i];
Jun 12, 2002
Jun 12, 2002
373
return 2*length;
May 20, 2002
May 20, 2002
374
375
376
}
/*-------------------------------------------------------------------------*/
May 22, 2002
May 22, 2002
377
static int minus5dB( AdapterC Data, int length )
May 20, 2002
May 20, 2002
378
{
Jun 12, 2002
Jun 12, 2002
379
380
int i;
Sint16* buffer = (Sint16*) Data.buffer;
Jul 13, 2002
Jul 13, 2002
381
382
for(i = length>>1 + 1; --i; )
buffer[i] = (38084 * (int)buffer[i]) >> 16;
Jun 12, 2002
Jun 12, 2002
383
return length;
May 20, 2002
May 20, 2002
384
385
386
}
/*-------------------------------------------------------------------------*/
Jul 3, 2002
Jul 3, 2002
387
388
const Fraction Half = {1, 2};
const Fraction Double = {2, 1};
Sep 26, 2002
Sep 26, 2002
389
390
const Fraction One = {1, 1};
Jun 21, 2002
Jun 21, 2002
391
Jul 3, 2002
Jul 3, 2002
392
static void initStraigthBuffer( RateConverterBuffer *rcb,
Sep 26, 2002
Sep 26, 2002
393
int length, Fraction r )
Jul 3, 2002
Jul 3, 2002
394
395
{
int i, size, minsize;
Jun 25, 2002
Jun 25, 2002
396
size = 8 * _fsize;
Jul 3, 2002
Jul 3, 2002
397
minsize = min( size, length );
Jun 18, 2002
Jun 18, 2002
398
Jul 3, 2002
Jul 3, 2002
399
400
401
402
403
404
405
406
407
408
409
410
for( i = 0; i < minsize; i++ )
{
rcb->inbuffer[i] = rcb->buffer[length-size+i];
rcb->inbuffer[i+size] = 0;
rcb->inbuffer[i+2*size] = rcb->buffer[i];
}
for( ; i < size; i++ )
{
rcb->inbuffer[i] = 0;
rcb->inbuffer[i+size] = 0;
rcb->inbuffer[i+2*size] = 0;
}
Jun 18, 2002
Jun 18, 2002
411
Jul 3, 2002
Jul 3, 2002
412
413
414
415
length = max( length, size );
rcb->flength = rcb->llength = size;
rcb->clength = length - size;
Sep 26, 2002
Sep 26, 2002
416
if( r.numerator < r.denominator )
Jun 18, 2002
Jun 18, 2002
417
{
Sep 26, 2002
Sep 26, 2002
418
rcb->finp = rcb->inbuffer + 5*size/2;
Jul 3, 2002
Jul 3, 2002
419
420
rcb->cinp = rcb->buffer + length - size/2;
rcb->linp = rcb->inbuffer + 3*size/2;
Sep 26, 2002
Sep 26, 2002
421
422
rcb->buffer += ( 1 + r.denominator * ( length + size )
/ r.numerator ) & -2;
Jun 18, 2002
Jun 18, 2002
423
424
425
}
else
{
Jul 3, 2002
Jul 3, 2002
426
427
428
429
430
rcb->finp = rcb->inbuffer + size/2;
rcb->cinp = rcb->buffer + size/2;
rcb->linp = rcb->inbuffer + 3*size/2;
}
}
Jun 18, 2002
Jun 18, 2002
431
Jul 3, 2002
Jul 3, 2002
432
static void initLoopBuffer( RateConverterBuffer *rcb,
Sep 26, 2002
Sep 26, 2002
433
int length, Fraction r )
Jul 3, 2002
Jul 3, 2002
434
435
436
437
438
439
440
441
442
{
/* !!!FIXME: modulo length, take scale into account,
check against the Straight part -frank */
int i, size;
size = 8 * _fsize;
for( i = 0; i < size; i++ )
{
rcb->inbuffer[i] = rcb->buffer[length-size+i];
rcb->inbuffer[i+size] = rcb->buffer[i];
Jun 18, 2002
Jun 18, 2002
443
}
Jul 3, 2002
Jul 3, 2002
444
445
446
447
448
449
450
rcb->finp = rcb->linp = rcb->inbuffer + size;
if( size < 0 )
rcb->buffer += r.numerator * ( length + 2 * size )
/ r.denominator;
}
static void initRateConverterBuffer( RateConverterBuffer *rcb,
Jul 13, 2002
Jul 13, 2002
451
AdapterC* Data, int length, Fraction ratio )
Jul 3, 2002
Jul 3, 2002
452
453
454
{
length >>= 1;
rcb->buffer = (Sint16*)( Data->buffer );
Jul 13, 2002
Jul 13, 2002
455
rcb->filter = Data->filter;
Jul 3, 2002
Jul 3, 2002
456
457
if( Data->mode & SDL_SOUND_Loop )
Sep 26, 2002
Sep 26, 2002
458
initLoopBuffer( rcb, length, ratio );
Jul 3, 2002
Jul 3, 2002
459
else
Sep 26, 2002
Sep 26, 2002
460
461
462
463
464
initStraigthBuffer( rcb, length, ratio );
fprintf( stderr, " finp: %8x length: %8x\n", rcb->finp, rcb->flength );
fprintf( stderr, " cinp: %8x length: %8x\n", rcb->cinp, rcb->clength );
fprintf( stderr, " linp: %8x length: %8x\n", rcb->linp, rcb->llength );
May 20, 2002
May 20, 2002
465
466
}
Jun 18, 2002
Jun 18, 2002
467
468
469
470
471
472
473
474
static void nextRateConverterBuffer( RateConverterBuffer *rcb )
{
rcb->buffer++;
rcb->finp++;
rcb->cinp++;
rcb->linp++;
}
Jul 13, 2002
Jul 13, 2002
475
476
477
typedef Sint16* (*RateConverter)( Sint16*, Sint16*, int,
VarFilter*, RateAux* );
Sep 26, 2002
Sep 26, 2002
478
static Sint16* doRateConversion( RateConverterBuffer* rcb, RateConverter rc )
May 20, 2002
May 20, 2002
479
{
Jul 13, 2002
Jul 13, 2002
480
481
482
RateAux aux = {0,0};
Sint16 *outp = rcb->buffer;
VarFilter* filter = rcb->filter;
Jun 18, 2002
Jun 18, 2002
483
Sep 26, 2002
Sep 26, 2002
484
485
486
487
488
489
490
outp = (*rc)( outp, rcb->finp, rcb->flength, filter, &aux );
fprintf( stderr, " outp: %8x aux.carry: %8x\n", outp, aux.carry );
outp = (*rc)( outp, rcb->cinp, rcb->clength, filter, &aux );
fprintf( stderr, " outp: %8x aux.carry: %8x\n", outp, aux.carry );
outp = (*rc)( outp, rcb->linp, rcb->llength, filter, &aux );
fprintf( stderr, " outp: %8x aux.carry: %8x\n", outp, aux.carry );
return outp;
May 20, 2002
May 20, 2002
491
492
}
Jun 18, 2002
Jun 18, 2002
493
Sep 26, 2002
Sep 26, 2002
494
495
496
497
498
499
/*-------------------------------------------------------------------------*/
static void clearSint16Buffer( Sint8* buffer, Sint16*r )
{
while( r >= (Sint16*)buffer ) *r-- = 0;
}
May 20, 2002
May 20, 2002
500
/*-------------------------------------------------------------------------*/
Jun 18, 2002
Jun 18, 2002
501
static int doubleRateMono( AdapterC Data, int length )
May 20, 2002
May 20, 2002
502
{
Sep 26, 2002
Sep 26, 2002
503
Sint16* r;
Jun 18, 2002
Jun 18, 2002
504
RateConverterBuffer rcb;
Sep 26, 2002
Sep 26, 2002
505
506
507
508
initRateConverterBuffer( &rcb, &Data, length, Half );
r = 1 + doRateConversion( &rcb, doubleRate1 );
clearSint16Buffer( Data.buffer, r );
return 2 * ( rcb.buffer - (Sint16*)Data.buffer + 2 );
May 20, 2002
May 20, 2002
509
510
}
Jun 18, 2002
Jun 18, 2002
511
512
static int doubleRateStereo( AdapterC Data, int length )
{
Sep 26, 2002
Sep 26, 2002
513
Sint16* r;
Jun 18, 2002
Jun 18, 2002
514
RateConverterBuffer rcb;
Sep 26, 2002
Sep 26, 2002
515
fprintf( stderr, "\n Buffer: %8x length: %8x\n", Data.buffer, length );
Sep 26, 2002
Sep 26, 2002
516
initRateConverterBuffer( &rcb, &Data, length, Half );
Jul 13, 2002
Jul 13, 2002
517
doRateConversion( &rcb, doubleRate2 );
Jun 18, 2002
Jun 18, 2002
518
nextRateConverterBuffer( &rcb );
Sep 26, 2002
Sep 26, 2002
519
520
521
r = 2 + doRateConversion( &rcb, doubleRate2 );
clearSint16Buffer( Data.buffer, r );
return 2 * ( rcb.buffer - (Sint16*)Data.buffer + 3 );
Jun 18, 2002
Jun 18, 2002
522
523
524
}
/*-------------------------------------------------------------------------*/
May 22, 2002
May 22, 2002
525
static int halfRateMono( AdapterC Data, int length )
May 20, 2002
May 20, 2002
526
{
Sep 26, 2002
Sep 26, 2002
527
Sint16* r;
Jun 18, 2002
Jun 18, 2002
528
RateConverterBuffer rcb;
Sep 26, 2002
Sep 26, 2002
529
530
531
initRateConverterBuffer( &rcb, &Data, length, Double );
r = doRateConversion( &rcb, halfRate1 );
return 2 * ( r - (Sint16*)Data.buffer );
Jun 18, 2002
Jun 18, 2002
532
533
534
535
}
static int halfRateStereo( AdapterC Data, int length )
{
Sep 26, 2002
Sep 26, 2002
536
Sint16* r;
Jun 18, 2002
Jun 18, 2002
537
RateConverterBuffer rcb;
Sep 26, 2002
Sep 26, 2002
538
initRateConverterBuffer( &rcb, &Data, length, Double );
Jul 13, 2002
Jul 13, 2002
539
doRateConversion( &rcb, halfRate2 );
Jun 18, 2002
Jun 18, 2002
540
nextRateConverterBuffer( &rcb );
Sep 26, 2002
Sep 26, 2002
541
542
r = doRateConversion( &rcb, halfRate2 );
return 2 * ( r - (Sint16*)Data.buffer );
May 20, 2002
May 20, 2002
543
544
545
}
/*-------------------------------------------------------------------------*/
Jun 18, 2002
Jun 18, 2002
546
static int increaseRateMono( AdapterC Data, int length )
May 20, 2002
May 20, 2002
547
{
Sep 26, 2002
Sep 26, 2002
548
Sint16* r;
Jun 18, 2002
Jun 18, 2002
549
RateConverterBuffer rcb;
Jul 13, 2002
Jul 13, 2002
550
initRateConverterBuffer( &rcb, &Data, length, Data.filter->ratio );
Sep 26, 2002
Sep 26, 2002
551
552
553
r = doRateConversion( &rcb, increaseRate1 );
clearSint16Buffer( Data.buffer, r );
return 2 * ( rcb.buffer - (Sint16*)Data.buffer + 1 );
May 20, 2002
May 20, 2002
554
555
}
Jun 18, 2002
Jun 18, 2002
556
static int increaseRateStereo( AdapterC Data, int length )
May 20, 2002
May 20, 2002
557
{
Sep 26, 2002
Sep 26, 2002
558
Sint16* r;
Jun 18, 2002
Jun 18, 2002
559
RateConverterBuffer rcb;
Sep 26, 2002
Sep 26, 2002
560
fprintf( stderr, "\n Buffer: %8x length: %8x\n", Data.buffer, length );
Jul 13, 2002
Jul 13, 2002
561
562
initRateConverterBuffer( &rcb, &Data, length, Data.filter->ratio );
doRateConversion( &rcb, increaseRate2 );
Jun 18, 2002
Jun 18, 2002
563
nextRateConverterBuffer( &rcb );
Sep 26, 2002
Sep 26, 2002
564
565
566
r = doRateConversion( &rcb, increaseRate2 );
clearSint16Buffer( Data.buffer, r );
return 2 * ( rcb.buffer - (Sint16*)Data.buffer + 1 );
Jun 12, 2002
Jun 12, 2002
567
568
}
Jun 18, 2002
Jun 18, 2002
569
570
/*-------------------------------------------------------------------------*/
static int decreaseRateMono( AdapterC Data, int length )
Jun 12, 2002
Jun 12, 2002
571
{
Sep 26, 2002
Sep 26, 2002
572
Sint16* r;
Jun 18, 2002
Jun 18, 2002
573
RateConverterBuffer rcb;
Jul 13, 2002
Jul 13, 2002
574
initRateConverterBuffer( &rcb, &Data, length, Data.filter->ratio );
Sep 26, 2002
Sep 26, 2002
575
576
r = doRateConversion( &rcb, decreaseRate1 );
return 2 * ( r - (Sint16*)Data.buffer );
Jun 12, 2002
Jun 12, 2002
577
578
}
Jun 18, 2002
Jun 18, 2002
579
static int decreaseRateStereo( AdapterC Data, int length )
Jun 12, 2002
Jun 12, 2002
580
{
Sep 26, 2002
Sep 26, 2002
581
Sint16* r;
Jun 18, 2002
Jun 18, 2002
582
RateConverterBuffer rcb;
Jul 13, 2002
Jul 13, 2002
583
584
initRateConverterBuffer( &rcb, &Data, length, Data.filter->ratio );
doRateConversion( &rcb, decreaseRate2 );
Jun 18, 2002
Jun 18, 2002
585
nextRateConverterBuffer( &rcb );
Sep 26, 2002
Sep 26, 2002
586
587
r = doRateConversion( &rcb, decreaseRate2 );
return 2 * ( r - (Sint16*)Data.buffer );
Jun 29, 2002
Jun 29, 2002
588
}
Jun 18, 2002
Jun 18, 2002
589
May 20, 2002
May 20, 2002
590
/*-------------------------------------------------------------------------*/
Jun 18, 2002
Jun 18, 2002
591
/* gives a maximal error of 3% and typical less than 0.2% */
May 22, 2002
May 22, 2002
592
static Fraction findFraction( float Value )
May 20, 2002
May 20, 2002
593
{
Jun 18, 2002
Jun 18, 2002
594
595
const Sint8 frac[95]={
2, -1, /* /1 */
Jun 12, 2002
Jun 12, 2002
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
1, 3, -1, /* /2 */
2, 4, 5, -1, /* /3 */
3, 5, 7, -1, /* /4 */
3, 4, 6, 7, 8, 9, -1, /* /5 */
5, 7, 11, -1, /* /6 */
4, 5, 6, 8, 9, 10, 11, 12, 13, -1, /* /7 */
5, 7, 9, 11, 13, 15, -1, /* /8 */
5, 7, 8, 10, 11, 13, 14, 16, -1, /* /9 */
7, 9, 11, 13, -1, /* /10 */
6, 7, 8, 9, 10, 12, 13, 14, 15, 16, -1, /* /11 */
7, 11, 13, -1, /* /12 */
7, 8, 9, 10, 11, 12, 14, 15, 16, -1, /* /13 */
9, 11, 13, 15, -1, /* /14 */
8, 11, 13, 14, 16, -1, /* /15 */
9, 11, 13, 15 }; /* /16 */
Jul 13, 2002
Jul 13, 2002
613
Fraction Result = {0,0};
Jun 25, 2002
Jun 25, 2002
614
int i,num,den=1;
Jun 12, 2002
Jun 12, 2002
615
616
617
618
float RelErr, BestErr = 0;
if( Value < 31/64. || Value > 64/31. ) return Result;
Jun 25, 2002
Jun 25, 2002
619
for( i = 0; i < SDL_TABLESIZE(frac); i++ )
Jun 12, 2002
Jun 12, 2002
620
{
Jun 25, 2002
Jun 25, 2002
621
num = frac[i];
Jun 12, 2002
Jun 12, 2002
622
623
624
625
626
627
628
629
630
631
632
if( num < 0 ) den++;
RelErr = Value * num / den;
RelErr = min( RelErr, 1/RelErr );
if( RelErr > BestErr )
{
BestErr = RelErr;
Result.denominator = den;
Result.numerator = num;
}
}
return Result;
May 20, 2002
May 20, 2002
633
634
}
Jun 12, 2002
Jun 12, 2002
635
/*-------------------------------------------------------------------------*/
May 22, 2002
May 22, 2002
636
static float sinc( float x )
May 20, 2002
May 20, 2002
637
{
Jun 12, 2002
Jun 12, 2002
638
639
640
641
if( x > -1e-24 && x < 1e-24 ) return 1.;
else return sin(x)/x;
}
Jun 29, 2002
Jun 29, 2002
642
643
static float calculateVarFilter( Sint16* dst,
float Ratio, float phase, float scale )
Jun 12, 2002
Jun 12, 2002
644
645
646
647
648
649
650
651
652
653
654
655
656
657
{
const Uint16 KaiserWindow7[]= {
22930, 16292, 14648, 14288, 14470, 14945, 15608, 16404,
17304, 18289, 19347, 20467, 21644, 22872, 24145, 25460,
26812, 28198, 29612, 31052, 32513, 33991, 35482, 36983,
38487, 39993, 41494, 42986, 44466, 45928, 47368, 48782,
50165, 51513, 52821, 54086, 55302, 56466, 57575, 58624,
59610, 60529, 61379, 62156, 62858, 63483, 64027, 64490,
64870, 65165, 65375, 65498, 65535, 65484, 65347, 65124,
64815, 64422, 63946, 63389, 62753, 62039, 61251, 60391 };
int i;
float w;
const float fg = -.018 + .5 * Ratio;
const float omega = 2 * M_PI * fg;
Jun 29, 2002
Jun 29, 2002
658
fprintf( stderr, " phase: %6g \n", phase );
Jul 3, 2002
Jul 3, 2002
659
phase += 63;
Jun 12, 2002
Jun 12, 2002
660
661
662
for( i = 0; i < 64; i++)
{
w = scale * ( KaiserWindow7[i] * ( i + 1 ));
Jul 3, 2002
Jul 3, 2002
663
664
dst[i] = w * sinc( omega * (i-phase) );
dst[127-i] = w * sinc( omega * (127-i-phase) );
Jun 12, 2002
Jun 12, 2002
665
}
Jul 3, 2002
Jul 3, 2002
666
fprintf( stderr, " center: %6d %6d \n", dst[63], dst[64] );
Jun 29, 2002
Jun 29, 2002
667
return fg;
May 20, 2002
May 20, 2002
668
669
}
Sep 26, 2002
Sep 26, 2002
670
static Fraction setupVarFilter( Sound_AudioCVT *Data, float Ratio )
May 20, 2002
May 20, 2002
671
{
Jun 29, 2002
Jun 29, 2002
672
673
int pos,n,d, incr, phase = 0;
float Scale, rd, fg;
Jun 12, 2002
Jun 12, 2002
674
Fraction IRatio;
Sep 26, 2002
Sep 26, 2002
675
VarFilter* filter = &Data->filter;
Jun 25, 2002
Jun 25, 2002
676
Jun 12, 2002
Jun 12, 2002
677
IRatio = findFraction( Ratio );
Jun 29, 2002
Jun 29, 2002
678
679
680
// Scale = Ratio < 1. ? 0.0364733 : 0.0211952;
Scale = 0.0084778;
Ratio = min( Ratio, 0.97 );
May 20, 2002
May 20, 2002
681
Jul 3, 2002
Jul 3, 2002
682
filter->ratio = IRatio;
Jun 12, 2002
Jun 12, 2002
683
684
n = IRatio.numerator;
d = IRatio.denominator;
Jun 25, 2002
Jun 25, 2002
685
rd = 1. / d;
Jun 12, 2002
Jun 12, 2002
686
Jun 29, 2002
Jun 29, 2002
687
688
689
fprintf( stderr, "Filter:\n" );
for( pos = 0; pos < d; pos++ )
Jun 12, 2002
Jun 12, 2002
690
{
Jun 29, 2002
Jun 29, 2002
691
fg = calculateVarFilter( filter->c[pos], Ratio, phase*rd, Scale );
Jun 25, 2002
Jun 25, 2002
692
phase += n;
Jun 29, 2002
Jun 29, 2002
693
filter->incr[pos] = phase / d;
Jun 25, 2002
Jun 25, 2002
694
phase %= d;
Jun 12, 2002
Jun 12, 2002
695
}
Jun 29, 2002
Jun 29, 2002
696
697
698
699
700
701
702
fprintf( stderr, " fg: %6g\n\n", fg );
/* !!!FIXME: get rid of the inversion -Frank*/
IRatio.numerator = d;
IRatio.denominator = n;
return IRatio;
}
/*-------------------------------------------------------------------------*/
Sep 26, 2002
Sep 26, 2002
703
static void initAudioCVT( Sound_AudioCVT *Data )
Jun 29, 2002
Jun 29, 2002
704
{
Sep 26, 2002
Sep 26, 2002
705
706
707
708
709
710
Data->len_ratio = 1.;
Data->len_mult = 1;
Data->add = 0;
Data->len_add = 0;
Data->filter_index = 0;
}
Jun 29, 2002
Jun 29, 2002
711
Sep 26, 2002
Sep 26, 2002
712
713
static void adjustSize( Sound_AudioCVT *Data, int add, Fraction f )
{
Jun 29, 2002
Jun 29, 2002
714
715
716
717
718
719
720
double ratio = f.numerator / (double) f.denominator;
Data->len_ratio *= ratio;
Data->len_mult = max( Data->len_mult, ceil(Data->len_ratio) );
Data->add = ratio * (Data->add + add);
Data->len_add = max( Data->len_add, ceil(Data->add) );
}
Sep 26, 2002
Sep 26, 2002
721
static Adapter* addAdapter( Sound_AudioCVT *Data, Adapter a )
Jun 29, 2002
Jun 29, 2002
722
{
Sep 26, 2002
Sep 26, 2002
723
724
725
726
727
728
729
730
731
732
733
734
735
736
Data->adapter[Data->filter_index] = a;
return &Data->adapter[Data->filter_index++];
}
static void addHAdapter( Sound_AudioCVT *Data, Adapter a )
{
adjustSize( Data, 0, Half );
addAdapter( Data, a );
}
static void addDAdapter( Sound_AudioCVT *Data, Adapter a )
{
adjustSize( Data, 0, Double );
addAdapter( Data, a );
May 20, 2002
May 20, 2002
737
}
May 22, 2002
May 22, 2002
738
Sep 26, 2002
Sep 26, 2002
739
Jun 12, 2002
Jun 12, 2002
740
/*-------------------------------------------------------------------------*/
Jul 3, 2002
Jul 3, 2002
741
742
743
744
745
const Adapter doubleRate[2] = { doubleRateMono, doubleRateStereo };
const Adapter halfRate[2] = { halfRateMono, halfRateStereo };
const Adapter increaseRate[2] = { increaseRateMono, increaseRateStereo };
const Adapter decreaseRate[2] = { decreaseRateMono, decreaseRateStereo };
Sep 26, 2002
Sep 26, 2002
746
747
static int createRateConverter( Sound_AudioCVT *Data,
int SrcRate, int DestRate, int channel )
Jun 12, 2002
Jun 12, 2002
748
{
Jul 13, 2002
Jul 13, 2002
749
750
const int c = channel - 1;
const int size = 16 * channel * _fsize;
Sep 26, 2002
Sep 26, 2002
751
Adapter* AdapterPos;
Jun 12, 2002
Jun 12, 2002
752
float Ratio = DestRate;
Jul 13, 2002
Jul 13, 2002
753
Fraction f;
Jun 12, 2002
Jun 12, 2002
754
755
if( SrcRate < 1 || SrcRate > 1<<18 ||
Sep 26, 2002
Sep 26, 2002
756
DestRate < 1 || DestRate > 1<<18 ) return -1;
Jun 12, 2002
Jun 12, 2002
757
758
Ratio /= SrcRate;
Sep 26, 2002
Sep 26, 2002
759
AdapterPos = addAdapter( Data, minus5dB );
Jun 12, 2002
Jun 12, 2002
760
761
762
763
while( Ratio > 64./31.)
{
Ratio /= 2.;
Sep 26, 2002
Sep 26, 2002
764
addAdapter( Data, doubleRate[c] );
Jul 13, 2002
Jul 13, 2002
765
adjustSize( Data, size, Double );
Jun 12, 2002
Jun 12, 2002
766
767
768
769
770
}
while( Ratio < 31./64. )
{
Ratio *= 2;
Sep 26, 2002
Sep 26, 2002
771
addAdapter( Data, halfRate[c] );
Jul 13, 2002
Jul 13, 2002
772
adjustSize( Data, size, Half );
Jun 12, 2002
Jun 12, 2002
773
774
775
776
}
if( Ratio > 1. )
{
Sep 26, 2002
Sep 26, 2002
777
778
*AdapterPos = increaseRate[c];
f = setupVarFilter( Data, Ratio );
Jul 13, 2002
Jul 13, 2002
779
adjustSize( Data, size, f );
Jun 12, 2002
Jun 12, 2002
780
781
782
}
else
{
Sep 26, 2002
Sep 26, 2002
783
784
f = setupVarFilter( Data, Ratio );
addAdapter( Data, decreaseRate[c]);
Jul 13, 2002
Jul 13, 2002
785
adjustSize( Data, size, f );
Jun 12, 2002
Jun 12, 2002
786
}
Sep 26, 2002
Sep 26, 2002
787
788
return 0;
Jun 12, 2002
Jun 12, 2002
789
}
May 22, 2002
May 22, 2002
790
Jun 12, 2002
Jun 12, 2002
791
/*-------------------------------------------------------------------------*/
Sep 26, 2002
Sep 26, 2002
792
static void createFormatConverter16Bit(Sound_AudioCVT *Data,
Jun 12, 2002
Jun 12, 2002
793
794
795
796
797
SDL_AudioSpec src, SDL_AudioSpec dst )
{
if( src.channels == 2 && dst.channels == 1 )
{
if( !IS_SYSENDIAN(src) )
Sep 26, 2002
Sep 26, 2002
798
addAdapter( Data, swapBytes );
Jun 12, 2002
Jun 12, 2002
799
800
if( IS_SIGNED(src) )
Sep 26, 2002
Sep 26, 2002
801
addHAdapter( Data, convertStereoToMonoS16Bit );
Jun 12, 2002
Jun 12, 2002
802
else
Sep 26, 2002
Sep 26, 2002
803
addHAdapter( Data, convertStereoToMonoU16Bit );
Jun 12, 2002
Jun 12, 2002
804
805
if( !IS_SYSENDIAN(dst) )
Sep 26, 2002
Sep 26, 2002
806
addAdapter( Data, swapBytes );
Jun 12, 2002
Jun 12, 2002
807
808
}
else if( IS_SYSENDIAN(src) != IS_SYSENDIAN(dst) )
Sep 26, 2002
Sep 26, 2002
809
addAdapter( Data, swapBytes );
Jun 12, 2002
Jun 12, 2002
810
811
812
813
if( IS_SIGNED(src) != IS_SIGNED(dst) )
{
if( IS_SYSENDIAN(dst) )
Sep 26, 2002
Sep 26, 2002
814
addAdapter( Data, changeSigned16BitSys );
Jun 12, 2002
Jun 12, 2002
815
else
Sep 26, 2002
Sep 26, 2002
816
addAdapter( Data, changeSigned16BitWrong );
Jun 12, 2002
Jun 12, 2002
817
818
819
}
if( src.channels == 1 && dst.channels == 2 )
Sep 26, 2002
Sep 26, 2002
820
addDAdapter( Data, convertMonoToStereo16Bit );
Jun 12, 2002
Jun 12, 2002
821
822
823
}
/*-------------------------------------------------------------------------*/
Sep 26, 2002
Sep 26, 2002
824
static void createFormatConverter8Bit(Sound_AudioCVT *Data,
Jun 12, 2002
Jun 12, 2002
825
826
827
828
829
SDL_AudioSpec src, SDL_AudioSpec dst )
{
if( IS_16BIT(src) )
{
if( IS_SYSENDIAN(src) )
Sep 26, 2002
Sep 26, 2002
830
addHAdapter( Data, cut16BitSysTo8Bit );
Jun 12, 2002
Jun 12, 2002
831
else
Sep 26, 2002
Sep 26, 2002
832
addHAdapter( Data, cut16BitWrongTo8Bit );
Jun 12, 2002
Jun 12, 2002
833
834
835
836
837
}
if( src.channels == 2 && dst.channels == 1 )
{
if( IS_SIGNED(src) )
Sep 26, 2002
Sep 26, 2002
838
addHAdapter( Data, convertStereoToMonoS8Bit );
Jun 12, 2002
Jun 12, 2002
839
else
Sep 26, 2002
Sep 26, 2002
840
addHAdapter( Data, convertStereoToMonoU8Bit );
Jun 12, 2002
Jun 12, 2002
841
842
843
}
if( IS_SIGNED(src) != IS_SIGNED(dst) )
Sep 26, 2002
Sep 26, 2002
844
addDAdapter( Data, changeSigned8Bit );
Jun 12, 2002
Jun 12, 2002
845
846
if( src.channels == 1 && dst.channels == 2 )
Sep 26, 2002
Sep 26, 2002
847
addDAdapter( Data, convertMonoToStereo8Bit );
Jun 12, 2002
Jun 12, 2002
848
849
850
851
if( !IS_8BIT(dst) )
{
if( IS_SYSENDIAN(dst) )
Sep 26, 2002
Sep 26, 2002
852
addDAdapter( Data, expand8BitTo16BitSys );
Jun 12, 2002
Jun 12, 2002
853
else
Sep 26, 2002
Sep 26, 2002
854
addDAdapter( Data, expand8BitTo16BitWrong );
Jun 12, 2002
Jun 12, 2002
855
856
857
858
}
}
/*-------------------------------------------------------------------------*/
Sep 26, 2002
Sep 26, 2002
859
static void createFormatConverter(Sound_AudioCVT *Data,
Jun 12, 2002
Jun 12, 2002
860
861
862
SDL_AudioSpec src, SDL_AudioSpec dst )
{
if( IS_FLOAT(src) )
Sep 26, 2002
Sep 26, 2002
863
addHAdapter( Data, cutFloatTo16Bit );
Jun 12, 2002
Jun 12, 2002
864
865
if( IS_8BIT(src) || IS_8BIT(dst) )
Sep 26, 2002
Sep 26, 2002
866
createFormatConverter8Bit( Data, src, dst);
Jun 12, 2002
Jun 12, 2002
867
else
Sep 26, 2002
Sep 26, 2002
868
createFormatConverter16Bit( Data, src, dst);
Jun 12, 2002
Jun 12, 2002
869
870
if( IS_FLOAT(dst) )
Sep 26, 2002
Sep 26, 2002
871
addDAdapter( Data, expand16BitToFloat );
Jun 29, 2002
Jun 29, 2002
872
}
Jun 12, 2002
Jun 12, 2002
873
874
/*-------------------------------------------------------------------------*/
Sep 26, 2002
Sep 26, 2002
875
int Sound_AltBuildAudioCVT( Sound_AudioCVT *Data,
Jun 18, 2002
Jun 18, 2002
876
SDL_AudioSpec src, SDL_AudioSpec dst )
Jun 12, 2002
Jun 12, 2002
877
{
Sep 26, 2002
Sep 26, 2002
878
SDL_AudioSpec im;
Jun 12, 2002
Jun 12, 2002
879
880
if( Data == NULL ) return -1;
Jul 3, 2002
Jul 3, 2002
881
Sep 26, 2002
Sep 26, 2002
882
initAudioCVT( Data );
Jul 3, 2002
Jul 3, 2002
883
Data->filter.ratio.denominator = 0;
Jun 29, 2002
Jun 29, 2002
884
Data->filter.mask = dst.size - 1;
Jun 12, 2002
Jun 12, 2002
885
886
887
888
889
/* Check channels */
if( src.channels < 1 || src.channels > 2 ||
dst.channels < 1 || dst.channels > 2 ) goto error_exit;
Sep 26, 2002
Sep 26, 2002
890
if( src.freq != dst.freq )
Jun 12, 2002
Jun 12, 2002
891
{
Sep 26, 2002
Sep 26, 2002
892
893
894
895
/* Convert to intermidiate format: signed 16Bit System-Endian */
im.format = AUDIO_S16SYS;
im.channels = min( src.channels, dst.channels );
createFormatConverter( Data, src, im );
Jun 12, 2002
Jun 12, 2002
896
897
/* Do rate conversion */
Sep 26, 2002
Sep 26, 2002
898
899
900
901
902
if( createRateConverter( Data, src.freq, dst.freq, im.channels ) )
goto error_exit;
src = im;
}
Jun 12, 2002
Jun 12, 2002
903
904
/* Convert to final format */
Sep 26, 2002
Sep 26, 2002
905
createFormatConverter( Data, src, dst );
Jun 12, 2002
Jun 12, 2002
906
Sep 26, 2002
Sep 26, 2002
907
908
/* Finalize adapter list */
addAdapter( Data, NULL );
Jun 12, 2002
Jun 12, 2002
909
910
911
912
913
914
915
916
917
918
919
920
/* !!! FIXME: Is it okay to assign NULL to a function pointer?
Borland says no. -frank */
return 0;
error_exit:
/* !!! FIXME: Is it okay to assign NULL to a function pointer?
Borland says no. -frank */
Data->adapter[0] = NULL;
return -1;
}
/*-------------------------------------------------------------------------*/
May 22, 2002
May 22, 2002
921
922
923
924
static char *fmt_to_str(Uint16 fmt)
{
switch (fmt)
{
Jun 12, 2002
Jun 12, 2002
925
926
927
928
929
930
case AUDIO_U8: return " U8";
case AUDIO_S8: return " S8";
case AUDIO_U16MSB: return "U16MSB";
case AUDIO_S16MSB: return "S16MSB";
case AUDIO_U16LSB: return "U16LSB";
case AUDIO_S16LSB: return "S16LSB";
May 22, 2002
May 22, 2002
931
932
933
934
}
return "??????";
}
Jun 12, 2002
Jun 12, 2002
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
#define AdapterDesc(x) { x, #x }
static void show_AudioCVT( Sound_AudioCVT *Data )
{
int i,j;
const struct{ int (*adapter) ( AdapterC, int); Sint8 *name; }
AdapterDescription[] = {
AdapterDesc(expand8BitTo16BitSys),
AdapterDesc(expand8BitTo16BitWrong),
AdapterDesc(expand16BitToFloat),
AdapterDesc(swapBytes),
AdapterDesc(cut16BitSysTo8Bit),
AdapterDesc(cut16BitWrongTo8Bit),
AdapterDesc(cutFloatTo16Bit),
AdapterDesc(changeSigned16BitSys),
AdapterDesc(changeSigned16BitWrong),
AdapterDesc(changeSigned8Bit),
AdapterDesc(convertStereoToMonoS16Bit),
AdapterDesc(convertStereoToMonoU16Bit),
AdapterDesc(convertStereoToMonoS8Bit),
AdapterDesc(convertStereoToMonoU8Bit),
AdapterDesc(convertMonoToStereo16Bit),
AdapterDesc(convertMonoToStereo8Bit),
AdapterDesc(minus5dB),
AdapterDesc(doubleRateMono),
Jun 18, 2002
Jun 18, 2002
960
AdapterDesc(doubleRateStereo),
Jun 12, 2002
Jun 12, 2002
961
AdapterDesc(halfRateMono),
Jun 18, 2002
Jun 18, 2002
962
963
964
965
966
AdapterDesc(halfRateStereo),
AdapterDesc(increaseRateMono),
AdapterDesc(increaseRateStereo),
AdapterDesc(decreaseRateMono),
AdapterDesc(decreaseRateStereo),
Jun 29, 2002
Jun 29, 2002
967
{ NULL, "----------NULL-----------\n" }
Jun 12, 2002
Jun 12, 2002
968
969
};
Jun 29, 2002
Jun 29, 2002
970
971
972
973
974
975
976
977
978
979
fprintf( stderr, "Sound_AudioCVT:\n" );
fprintf( stderr, " needed: %8d\n", Data->needed );
fprintf( stderr, " add: %8g\n", Data->add );
fprintf( stderr, " len_add: %8d\n", Data->len_add );
fprintf( stderr, " len_ratio: %8g\n", Data->len_ratio );
fprintf( stderr, " len_mult: %8d\n", Data->len_mult );
fprintf( stderr, " filter->mask: %#7x\n", Data->filter.mask );
fprintf( stderr, "\n" );
fprintf( stderr, "Adapter List: \n" );
Jun 12, 2002
Jun 12, 2002
980
981
for( i = 0; i < 32; i++ )
{
Jun 21, 2002
Jun 21, 2002
982
for( j = 0; j < SDL_TABLESIZE(AdapterDescription); j++ )
Jun 12, 2002
Jun 12, 2002
983
984
985
{
if( Data->adapter[i] == AdapterDescription[j].adapter )
{
Jun 29, 2002
Jun 29, 2002
986
fprintf( stderr, " %s \n", AdapterDescription[j].name );
Jun 25, 2002
Jun 25, 2002
987
if( Data->adapter[i] == NULL ) goto sucess_exit;
Jun 12, 2002
Jun 12, 2002
988
989
990
991
goto cont;
}
}
fprintf( stderr, " Error: unknown adapter\n" );
Jun 25, 2002
Jun 25, 2002
992
Jun 12, 2002
Jun 12, 2002
993
994
995
cont:
}
fprintf( stderr, " Error: NULL adapter missing\n" );
Jun 25, 2002
Jun 25, 2002
996
sucess_exit:
Jul 3, 2002
Jul 3, 2002
997
if( Data->filter.ratio.denominator )
Jun 25, 2002
Jun 25, 2002
998
999
{
fprintf( stderr, "Variable Rate Converter:\n"
Jun 29, 2002
Jun 29, 2002
1000
" numerator: %3d\n"