Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
executable file
·
761 lines (637 loc) · 20.6 KB

sdlgenaudiocvt.pl

File metadata and controls

executable file
·
761 lines (637 loc) · 20.6 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/perl -w
use warnings;
use strict;
my @audiotypes = qw(
U8
S8
U16LSB
S16LSB
U16MSB
S16MSB
S32LSB
S32MSB
F32LSB
F32MSB
);
Jan 9, 2009
Jan 9, 2009
19
my @channels = ( 1, 2, 4, 6, 8 );
20
21
22
23
my %funcs;
my $custom_converters = 0;
Jan 9, 2009
Jan 9, 2009
24
25
26
27
28
29
30
31
32
33
34
35
sub getTypeConvertHashId {
my ($from, $to) = @_;
return "TYPECONVERTER $from/$to";
}
sub getResamplerHashId {
my ($from, $channels, $upsample, $multiple) = @_;
return "RESAMPLER $from/$channels/$upsample/$multiple";
}
36
37
sub outputHeader {
print <<EOF;
Aug 28, 2006
Aug 28, 2006
38
/* DO NOT EDIT! This file is generated by sdlgenaudiocvt.pl */
Apr 8, 2011
Apr 8, 2011
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Simple DirectMedia Layer
Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
58
59
60
61
62
63
*/
#include "SDL_config.h"
#include "SDL_audio.h"
#include "SDL_audio_c.h"
Jan 11, 2009
Jan 11, 2009
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#ifndef DEBUG_CONVERT
#define DEBUG_CONVERT 0
#endif
/* If you can guarantee your data and need space, you can eliminate code... */
/* Just build the arbitrary resamplers if you're saving code space. */
#ifndef LESS_RESAMPLERS
#define LESS_RESAMPLERS 0
#endif
/* Don't build any resamplers if you're REALLY saving code space. */
#ifndef NO_RESAMPLERS
#define NO_RESAMPLERS 0
#endif
/* Don't build any type converters if you're saving code space. */
#ifndef NO_CONVERTERS
#define NO_CONVERTERS 0
#endif
Aug 28, 2006
Aug 28, 2006
87
/* *INDENT-OFF* */
Sep 1, 2006
Sep 1, 2006
91
my @vals = ( 127, 32767, 2147483647 );
92
93
94
95
96
97
98
99
100
foreach (@vals) {
my $val = $_;
my $fval = 1.0 / $val;
print("#define DIVBY${val} ${fval}f\n");
}
print("\n");
}
Aug 28, 2006
Aug 28, 2006
101
102
sub outputFooter {
print <<EOF;
Jan 9, 2009
Jan 9, 2009
103
104
/* $custom_converters converters generated. */
Aug 28, 2006
Aug 28, 2006
105
106
107
108
109
/* *INDENT-ON* */
/* vi: set ts=4 sw=4 expandtab: */
EOF
}
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
sub splittype {
my $t = shift;
my ($signed, $size, $endian) = $t =~ /([USF])(\d+)([LM]SB|)/;
my $float = ($signed eq 'F') ? 1 : 0;
$signed = (($float) or ($signed eq 'S')) ? 1 : 0;
$endian = 'NONE' if ($endian eq '');
my $ctype = '';
if ($float) {
$ctype = (($size == 32) ? 'float' : 'double');
} else {
$ctype = (($signed) ? 'S' : 'U') . "int${size}";
}
return ($signed, $float, $size, $endian, $ctype);
}
sub getSwapFunc {
my ($size, $signed, $float, $endian, $val) = @_;
my $BEorLE = (($endian eq 'MSB') ? 'BE' : 'LE');
my $code = '';
if ($float) {
$code = "SDL_SwapFloat${BEorLE}($val)";
} else {
if ($size > 8) {
$code = "SDL_Swap${BEorLE}${size}($val)";
} else {
$code = $val;
}
if (($signed) and (!$float)) {
$code = "((Sint${size}) $code)";
}
}
return "${code}";
}
sub maxIntVal {
Sep 1, 2006
Sep 1, 2006
152
153
154
155
156
157
158
my $size = shift;
if ($size == 8) {
return 0x7F;
} elsif ($size == 16) {
return 0x7FFF;
} elsif ($size == 32) {
return 0x7FFFFFFF;
159
160
161
162
163
164
}
die("bug in script.\n");
}
sub getFloatToIntMult {
Sep 1, 2006
Sep 1, 2006
165
166
my $size = shift;
my $val = maxIntVal($size) . '.0';
167
168
169
170
171
$val .= 'f' if ($size < 32);
return $val;
}
sub getIntToFloatDivBy {
Sep 1, 2006
Sep 1, 2006
172
173
my $size = shift;
return 'DIVBY' . maxIntVal($size);
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
}
sub getSignFlipVal {
my $size = shift;
if ($size == 8) {
return '0x80';
} elsif ($size == 16) {
return '0x8000';
} elsif ($size == 32) {
return '0x80000000';
}
die("bug in script.\n");
}
sub buildCvtFunc {
my ($from, $to) = @_;
my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
my ($tsigned, $tfloat, $tsize, $tendian, $tctype) = splittype($to);
my $diffs = 0;
$diffs++ if ($fsize != $tsize);
$diffs++ if ($fsigned != $tsigned);
$diffs++ if ($ffloat != $tfloat);
$diffs++ if ($fendian ne $tendian);
return if ($diffs == 0);
Jan 9, 2009
Jan 9, 2009
201
my $hashid = getTypeConvertHashId($from, $to);
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
if (1) { # !!! FIXME: if ($diffs > 1) {
my $sym = "SDL_Convert_${from}_to_${to}";
$funcs{$hashid} = $sym;
$custom_converters++;
# Always unsigned for ints, for possible byteswaps.
my $srctype = (($ffloat) ? 'float' : "Uint${fsize}");
print <<EOF;
static void SDLCALL
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
{
int i;
const $srctype *src;
$tctype *dst;
Jan 12, 2009
Jan 12, 2009
218
#if DEBUG_CONVERT
219
220
221
222
223
224
225
226
fprintf(stderr, "Converting AUDIO_${from} to AUDIO_${to}.\\n");
#endif
EOF
if ($fsize < $tsize) {
my $mult = $tsize / $fsize;
print <<EOF;
Jan 2, 2009
Jan 2, 2009
227
228
src = ((const $srctype *) (cvt->buf + cvt->len_cvt)) - 1;
dst = (($tctype *) (cvt->buf + cvt->len_cvt * $mult)) - 1;
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
for (i = cvt->len_cvt / sizeof ($srctype); i; --i, --src, --dst) {
EOF
} else {
print <<EOF;
src = (const $srctype *) cvt->buf;
dst = ($tctype *) cvt->buf;
for (i = cvt->len_cvt / sizeof ($srctype); i; --i, ++src, ++dst) {
EOF
}
# Have to convert to/from float/int.
# !!! FIXME: cast through double for int32<->float?
my $code = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, '*src');
if ($ffloat != $tfloat) {
if ($ffloat) {
Sep 1, 2006
Sep 1, 2006
244
245
246
247
my $mult = getFloatToIntMult($tsize);
if (!$tsigned) { # bump from -1.0f/1.0f to 0.0f/2.0f
$code = "($code + 1.0f)";
}
248
249
250
251
$code = "(($tctype) ($code * $mult))";
} else {
# $divby will be the reciprocal, to avoid pipeline stalls
# from floating point division...so multiply it.
Sep 1, 2006
Sep 1, 2006
252
my $divby = getIntToFloatDivBy($fsize);
253
$code = "(((float) $code) * $divby)";
Sep 1, 2006
Sep 1, 2006
254
255
256
if (!$fsigned) { # bump from 0.0f/2.0f to -1.0f/1.0f.
$code = "($code - 1.0f)";
}
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
}
} else {
# All integer conversions here.
if ($fsigned != $tsigned) {
my $signflipval = getSignFlipVal($fsize);
$code = "(($code) ^ $signflipval)";
}
my $shiftval = abs($fsize - $tsize);
if ($fsize < $tsize) {
$code = "((($tctype) $code) << $shiftval)";
} elsif ($fsize > $tsize) {
$code = "(($tctype) ($code >> $shiftval))";
}
}
my $swap = getSwapFunc($tsize, $tsigned, $tfloat, $tendian, 'val');
print <<EOF;
const $tctype val = $code;
*dst = ${swap};
}
EOF
if ($fsize > $tsize) {
my $divby = $fsize / $tsize;
print(" cvt->len_cvt /= $divby;\n");
} elsif ($fsize < $tsize) {
my $mult = $tsize / $fsize;
print(" cvt->len_cvt *= $mult;\n");
}
print <<EOF;
if (cvt->filters[++cvt->filter_index]) {
Jan 2, 2009
Jan 2, 2009
292
cvt->filters[cvt->filter_index] (cvt, AUDIO_$to);
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
}
}
EOF
} else {
if ($fsigned != $tsigned) {
$funcs{$hashid} = 'SDL_ConvertSigned';
} elsif ($ffloat != $tfloat) {
$funcs{$hashid} = 'SDL_ConvertFloat';
} elsif ($fsize != $tsize) {
$funcs{$hashid} = 'SDL_ConvertSize';
} elsif ($fendian ne $tendian) {
$funcs{$hashid} = 'SDL_ConvertEndian';
} else {
die("error in script.\n");
}
}
}
Jan 9, 2009
Jan 9, 2009
314
sub buildTypeConverters {
Jan 11, 2009
Jan 11, 2009
315
print "#if !NO_CONVERTERS\n\n";
Jan 9, 2009
Jan 9, 2009
316
317
318
319
320
321
322
foreach (@audiotypes) {
my $from = $_;
foreach (@audiotypes) {
my $to = $_;
buildCvtFunc($from, $to);
}
}
Jan 11, 2009
Jan 11, 2009
323
print "#endif /* !NO_CONVERTERS */\n\n\n";
Jan 9, 2009
Jan 9, 2009
324
325
print "const SDL_AudioTypeFilters sdl_audio_type_filters[] =\n{\n";
Jan 11, 2009
Jan 11, 2009
326
print "#if !NO_CONVERTERS\n";
327
foreach (@audiotypes) {
Jan 9, 2009
Jan 9, 2009
328
329
330
331
332
333
334
335
336
337
my $from = $_;
foreach (@audiotypes) {
my $to = $_;
if ($from ne $to) {
my $hashid = getTypeConvertHashId($from, $to);
my $sym = $funcs{$hashid};
print(" { AUDIO_$from, AUDIO_$to, $sym },\n");
}
}
}
Jan 11, 2009
Jan 11, 2009
338
print "#endif /* !NO_CONVERTERS */\n";
Jan 9, 2009
Jan 9, 2009
339
Jan 11, 2009
Jan 11, 2009
340
print(" { 0, 0, NULL }\n");
Jan 9, 2009
Jan 9, 2009
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
print "};\n\n\n";
}
sub getBiggerCtype {
my ($isfloat, $size) = @_;
if ($isfloat) {
if ($size == 32) {
return 'double';
}
die("bug in script.\n");
}
if ($size == 8) {
return 'Sint16';
} elsif ($size == 16) {
return 'Sint32'
} elsif ($size == 32) {
return 'Sint64'
Jan 9, 2009
Jan 9, 2009
361
362
die("bug in script.\n");
Jan 9, 2009
Jan 9, 2009
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
# These handle arbitrary resamples...44100Hz to 48000Hz, for example.
# Man, this code is skanky.
sub buildArbitraryResampleFunc {
# !!! FIXME: we do a lot of unnecessary and ugly casting in here, due to getSwapFunc().
my ($from, $channels, $upsample) = @_;
my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
my $bigger = getBiggerCtype($ffloat, $fsize);
my $interp = ($ffloat) ? '* 0.5' : '>> 1';
my $resample = ($upsample) ? 'Upsample' : 'Downsample';
my $hashid = getResamplerHashId($from, $channels, $upsample, 0);
my $sym = "SDL_${resample}_${from}_${channels}c";
$funcs{$hashid} = $sym;
$custom_converters++;
my $fudge = $fsize * $channels * 2; # !!! FIXME
my $eps_adjust = ($upsample) ? 'dstsize' : 'srcsize';
my $incr = '';
my $incr2 = '';
# !!! FIXME: DEBUG_CONVERT should report frequencies.
print <<EOF;
static void SDLCALL
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
Jan 12, 2009
Jan 12, 2009
393
#if DEBUG_CONVERT
Jan 9, 2009
Jan 9, 2009
394
395
396
397
398
399
fprintf(stderr, "$resample arbitrary (x%f) AUDIO_${from}, ${channels} channels.\\n", cvt->rate_incr);
#endif
const int srcsize = cvt->len_cvt - $fudge;
const int dstsize = (int) (((double)cvt->len_cvt) * cvt->rate_incr);
register int eps = 0;
Sep 19, 2010
Sep 19, 2010
402
403
my $endcomparison = '!=';
Jan 9, 2009
Jan 9, 2009
404
405
406
# Upsampling (growing the buffer) needs to work backwards, since we
# overwrite the buffer as we go.
if ($upsample) {
Sep 19, 2010
Sep 19, 2010
407
$endcomparison = '>'; # dst > target
Jan 9, 2009
Jan 9, 2009
408
409
410
411
412
413
print <<EOF;
$fctype *dst = (($fctype *) (cvt->buf + dstsize)) - $channels;
const $fctype *src = (($fctype *) (cvt->buf + cvt->len_cvt)) - $channels;
const $fctype *target = ((const $fctype *) cvt->buf) - $channels;
EOF
} else {
Sep 19, 2010
Sep 19, 2010
414
$endcomparison = '<'; # dst < target
Jan 9, 2009
Jan 9, 2009
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
print <<EOF;
$fctype *dst = ($fctype *) cvt->buf;
const $fctype *src = ($fctype *) cvt->buf;
const $fctype *target = (const $fctype *) (cvt->buf + dstsize);
EOF
}
for (my $i = 0; $i < $channels; $i++) {
my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
print <<EOF;
$fctype sample${idx} = $val;
EOF
}
for (my $i = 0; $i < $channels; $i++) {
my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
print <<EOF;
$fctype last_sample${idx} = sample${idx};
EOF
}
print <<EOF;
Sep 19, 2010
Sep 19, 2010
438
while (dst $endcomparison target) {
Jan 9, 2009
Jan 9, 2009
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
466
467
468
469
470
471
EOF
if ($upsample) {
for (my $i = 0; $i < $channels; $i++) {
# !!! FIXME: don't do this swap every write, just when the samples change.
my $idx = (($channels - $i) - 1);
my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "sample${idx}");
print <<EOF;
dst[$idx] = $val;
EOF
}
$incr = ($channels == 1) ? 'dst--' : "dst -= $channels";
$incr2 = ($channels == 1) ? 'src--' : "src -= $channels";
print <<EOF;
$incr;
eps += srcsize;
if ((eps << 1) >= dstsize) {
$incr2;
EOF
} else { # downsample.
$incr = ($channels == 1) ? 'src++' : "src += $channels";
print <<EOF;
$incr;
eps += dstsize;
if ((eps << 1) >= srcsize) {
EOF
for (my $i = 0; $i < $channels; $i++) {
my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "sample${i}");
print <<EOF;
dst[$i] = $val;
EOF
Jan 9, 2009
Jan 9, 2009
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
$incr = ($channels == 1) ? 'dst++' : "dst += $channels";
print <<EOF;
$incr;
EOF
}
for (my $i = 0; $i < $channels; $i++) {
my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
my $swapped = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
print <<EOF;
sample${idx} = ($fctype) (((($bigger) $swapped) + (($bigger) last_sample${idx})) $interp);
EOF
}
for (my $i = 0; $i < $channels; $i++) {
my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
print <<EOF;
last_sample${idx} = sample${idx};
EOF
}
print <<EOF;
eps -= $eps_adjust;
}
}
EOF
print <<EOF;
cvt->len_cvt = dstsize;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index] (cvt, format);
505
506
507
}
}
Jan 9, 2009
Jan 9, 2009
508
EOF
Jan 9, 2009
Jan 9, 2009
510
}
Jan 9, 2009
Jan 9, 2009
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# These handle clean resamples...doubling and quadrupling the sample rate, etc.
sub buildMultipleResampleFunc {
# !!! FIXME: we do a lot of unnecessary and ugly casting in here, due to getSwapFunc().
my ($from, $channels, $upsample, $multiple) = @_;
my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
my $bigger = getBiggerCtype($ffloat, $fsize);
my $interp = ($ffloat) ? '* 0.5' : '>> 1';
my $interp2 = ($ffloat) ? '* 0.25' : '>> 2';
my $mult3 = ($ffloat) ? '3.0' : '3';
my $lencvtop = ($upsample) ? '*' : '/';
my $resample = ($upsample) ? 'Upsample' : 'Downsample';
my $hashid = getResamplerHashId($from, $channels, $upsample, $multiple);
my $sym = "SDL_${resample}_${from}_${channels}c_x${multiple}";
$funcs{$hashid} = $sym;
$custom_converters++;
# !!! FIXME: DEBUG_CONVERT should report frequencies.
print <<EOF;
static void SDLCALL
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
{
Jan 12, 2009
Jan 12, 2009
535
#if DEBUG_CONVERT
Jan 9, 2009
Jan 9, 2009
536
537
538
539
540
541
542
fprintf(stderr, "$resample (x${multiple}) AUDIO_${from}, ${channels} channels.\\n");
#endif
const int srcsize = cvt->len_cvt;
const int dstsize = cvt->len_cvt $lencvtop $multiple;
EOF
Dec 28, 2009
Dec 28, 2009
543
544
my $endcomparison = '!=';
Jan 9, 2009
Jan 9, 2009
545
546
547
# Upsampling (growing the buffer) needs to work backwards, since we
# overwrite the buffer as we go.
if ($upsample) {
Dec 28, 2009
Dec 28, 2009
548
$endcomparison = '>'; # dst > target
Jan 9, 2009
Jan 9, 2009
549
550
551
552
553
554
print <<EOF;
$fctype *dst = (($fctype *) (cvt->buf + dstsize)) - $channels;
const $fctype *src = (($fctype *) (cvt->buf + cvt->len_cvt)) - $channels;
const $fctype *target = ((const $fctype *) cvt->buf) - $channels;
EOF
} else {
Dec 28, 2009
Dec 28, 2009
555
$endcomparison = '<'; # dst < target
Jan 9, 2009
Jan 9, 2009
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
print <<EOF;
$fctype *dst = ($fctype *) cvt->buf;
const $fctype *src = ($fctype *) cvt->buf;
const $fctype *target = (const $fctype *) (cvt->buf + dstsize);
EOF
}
for (my $i = 0; $i < $channels; $i++) {
my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
print <<EOF;
$bigger last_sample${idx} = ($bigger) $val;
EOF
}
print <<EOF;
Dec 28, 2009
Dec 28, 2009
572
while (dst $endcomparison target) {
Jan 9, 2009
Jan 9, 2009
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
EOF
for (my $i = 0; $i < $channels; $i++) {
my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
print <<EOF;
const $bigger sample${idx} = ($bigger) $val;
EOF
}
my $incr = '';
if ($upsample) {
$incr = ($channels == 1) ? 'src--' : "src -= $channels";
} else {
my $amount = $channels * $multiple;
$incr = "src += $amount"; # can't ever be 1, so no "++" version.
}
print <<EOF;
$incr;
EOF
# !!! FIXME: This really begs for some Altivec or SSE, etc.
if ($upsample) {
if ($multiple == 2) {
for (my $i = $channels-1; $i >= 0; $i--) {
my $dsti = $i + $channels;
print <<EOF;
dst[$dsti] = ($fctype) ((sample${i} + last_sample${i}) $interp);
EOF
}
for (my $i = $channels-1; $i >= 0; $i--) {
my $dsti = $i;
print <<EOF;
dst[$dsti] = ($fctype) sample${i};
EOF
}
} elsif ($multiple == 4) {
for (my $i = $channels-1; $i >= 0; $i--) {
my $dsti = $i + ($channels * 3);
print <<EOF;
dst[$dsti] = ($fctype) sample${i};
EOF
}
for (my $i = $channels-1; $i >= 0; $i--) {
my $dsti = $i + ($channels * 2);
print <<EOF;
dst[$dsti] = ($fctype) ((($mult3 * sample${i}) + last_sample${i}) $interp2);
EOF
}
for (my $i = $channels-1; $i >= 0; $i--) {
my $dsti = $i + ($channels * 1);
print <<EOF;
dst[$dsti] = ($fctype) ((sample${i} + last_sample${i}) $interp);
Jan 9, 2009
Jan 9, 2009
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
}
for (my $i = $channels-1; $i >= 0; $i--) {
my $dsti = $i + ($channels * 0);
print <<EOF;
dst[$dsti] = ($fctype) ((sample${i} + ($mult3 * last_sample${i})) $interp2);
EOF
}
} else {
die('bug in program.'); # we only handle x2 and x4.
}
} else { # downsample.
if ($multiple == 2) {
for (my $i = 0; $i < $channels; $i++) {
print <<EOF;
dst[$i] = ($fctype) ((sample${i} + last_sample${i}) $interp);
EOF
}
} elsif ($multiple == 4) {
# !!! FIXME: interpolate all 4 samples?
for (my $i = 0; $i < $channels; $i++) {
print <<EOF;
dst[$i] = ($fctype) ((sample${i} + last_sample${i}) $interp);
EOF
}
} else {
die('bug in program.'); # we only handle x2 and x4.
}
}
for (my $i = 0; $i < $channels; $i++) {
my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
print <<EOF;
last_sample${idx} = sample${idx};
EOF
}
if ($upsample) {
my $amount = $channels * $multiple;
$incr = "dst -= $amount"; # can't ever be 1, so no "--" version.
} else {
$incr = ($channels == 1) ? 'dst++' : "dst += $channels";
}
print <<EOF;
$incr;
}
Jan 9, 2009
Jan 9, 2009
679
680
681
682
683
684
685
686
687
688
689
cvt->len_cvt = dstsize;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index] (cvt, format);
}
}
EOF
}
sub buildResamplers {
Jan 11, 2009
Jan 11, 2009
690
691
692
693
694
695
696
697
698
699
700
print "#if !NO_RESAMPLERS\n\n";
foreach (@audiotypes) {
my $from = $_;
foreach (@channels) {
my $channel = $_;
buildArbitraryResampleFunc($from, $channel, 1);
buildArbitraryResampleFunc($from, $channel, 0);
}
}
print "\n#if !LESS_RESAMPLERS\n\n";
Jan 9, 2009
Jan 9, 2009
701
702
703
704
705
706
707
708
709
710
711
foreach (@audiotypes) {
my $from = $_;
foreach (@channels) {
my $channel = $_;
for (my $multiple = 2; $multiple <= 4; $multiple += 2) {
buildMultipleResampleFunc($from, $channel, 1, $multiple);
buildMultipleResampleFunc($from, $channel, 0, $multiple);
}
}
}
Jan 11, 2009
Jan 11, 2009
712
713
714
print "#endif /* !LESS_RESAMPLERS */\n";
print "#endif /* !NO_RESAMPLERS */\n\n\n";
Jan 9, 2009
Jan 9, 2009
715
print "const SDL_AudioRateFilters sdl_audio_rate_filters[] =\n{\n";
Jan 11, 2009
Jan 11, 2009
716
717
718
719
720
721
722
723
724
725
726
727
728
729
print "#if !NO_RESAMPLERS\n";
foreach (@audiotypes) {
my $from = $_;
foreach (@channels) {
my $channel = $_;
for (my $upsample = 0; $upsample <= 1; $upsample++) {
my $hashid = getResamplerHashId($from, $channel, $upsample, 0);
my $sym = $funcs{$hashid};
print(" { AUDIO_$from, $channel, $upsample, 0, $sym },\n");
}
}
}
print "#if !LESS_RESAMPLERS\n";
Jan 9, 2009
Jan 9, 2009
730
731
732
733
foreach (@audiotypes) {
my $from = $_;
foreach (@channels) {
my $channel = $_;
Jan 11, 2009
Jan 11, 2009
734
for (my $multiple = 2; $multiple <= 4; $multiple += 2) {
Jan 9, 2009
Jan 9, 2009
735
736
737
738
739
740
741
742
743
for (my $upsample = 0; $upsample <= 1; $upsample++) {
my $hashid = getResamplerHashId($from, $channel, $upsample, $multiple);
my $sym = $funcs{$hashid};
print(" { AUDIO_$from, $channel, $upsample, $multiple, $sym },\n");
}
}
}
}
Jan 11, 2009
Jan 11, 2009
744
745
print "#endif /* !LESS_RESAMPLERS */\n";
print "#endif /* !NO_RESAMPLERS */\n";
Jan 11, 2009
Jan 11, 2009
746
print(" { 0, 0, 0, 0, NULL }\n");
Jan 9, 2009
Jan 9, 2009
747
748
749
750
751
752
753
754
755
print "};\n\n";
}
# mainline ...
outputHeader();
buildTypeConverters();
buildResamplers();
Aug 28, 2006
Aug 28, 2006
756
757
outputFooter();
758
759
exit 0;
Aug 28, 2006
Aug 28, 2006
760
# end of sdlgenaudiocvt.pl ...