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
·
316 lines (257 loc) · 7.49 KB

sdlgenaudiocvt.pl

File metadata and controls

executable file
·
316 lines (257 loc) · 7.49 KB
 
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
#!/usr/bin/perl -w
use warnings;
use strict;
my @audiotypes = qw(
U8
S8
U16LSB
S16LSB
U16MSB
S16MSB
S32LSB
S32MSB
F32LSB
F32MSB
);
my %funcs;
my $custom_converters = 0;
sub outputHeader {
print <<EOF;
Aug 28, 2006
Aug 28, 2006
26
/* DO NOT EDIT! This file is generated by sdlgenaudiocvt.pl */
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
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken\@libsdl.org
*/
#include "SDL_config.h"
#include "SDL_audio.h"
#include "SDL_audio_c.h"
Aug 28, 2006
Aug 28, 2006
53
/* *INDENT-OFF* */
Sep 1, 2006
Sep 1, 2006
57
my @vals = ( 127, 32767, 2147483647 );
58
59
60
61
62
63
64
65
66
foreach (@vals) {
my $val = $_;
my $fval = 1.0 / $val;
print("#define DIVBY${val} ${fval}f\n");
}
print("\n");
}
Aug 28, 2006
Aug 28, 2006
67
68
69
70
71
72
73
sub outputFooter {
print <<EOF;
/* *INDENT-ON* */
/* vi: set ts=4 sw=4 expandtab: */
EOF
}
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
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
116
117
118
119
120
121
122
my $size = shift;
if ($size == 8) {
return 0x7F;
} elsif ($size == 16) {
return 0x7FFF;
} elsif ($size == 32) {
return 0x7FFFFFFF;
123
124
125
126
127
128
}
die("bug in script.\n");
}
sub getFloatToIntMult {
Sep 1, 2006
Sep 1, 2006
129
130
my $size = shift;
my $val = maxIntVal($size) . '.0';
131
132
133
134
135
$val .= 'f' if ($size < 32);
return $val;
}
sub getIntToFloatDivBy {
Sep 1, 2006
Sep 1, 2006
136
137
my $size = shift;
return 'DIVBY' . maxIntVal($size);
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
}
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);
my $hashid = "$from/$to";
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;
#ifdef DEBUG_CONVERT
fprintf(stderr, "Converting AUDIO_${from} to AUDIO_${to}.\\n");
#endif
EOF
if ($fsize < $tsize) {
my $mult = $tsize / $fsize;
print <<EOF;
src = (const $srctype *) (cvt->buf + cvt->len_cvt);
dst = ($tctype *) (cvt->buf + cvt->len_cvt * $mult);
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
208
209
210
211
my $mult = getFloatToIntMult($tsize);
if (!$tsigned) { # bump from -1.0f/1.0f to 0.0f/2.0f
$code = "($code + 1.0f)";
}
212
213
214
215
$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
216
my $divby = getIntToFloatDivBy($fsize);
217
$code = "(((float) $code) * $divby)";
Sep 1, 2006
Sep 1, 2006
218
219
220
if (!$fsigned) { # bump from 0.0f/2.0f to -1.0f/1.0f.
$code = "($code - 1.0f)";
}
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
}
} 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;
format = AUDIO_$to;
if (cvt->filters[++cvt->filter_index]) {
cvt->filters[cvt->filter_index] (cvt, format);
}
}
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");
}
}
}
outputHeader();
foreach (@audiotypes) {
my $from = $_;
foreach (@audiotypes) {
my $to = $_;
buildCvtFunc($from, $to);
}
}
print <<EOF;
const SDL_AudioTypeFilters sdl_audio_type_filters[] =
{
EOF
foreach (@audiotypes) {
my $from = $_;
foreach (@audiotypes) {
my $to = $_;
if ($from ne $to) {
my $hashid = "$from/$to";
my $sym = $funcs{$hashid};
print(" { AUDIO_$from, AUDIO_$to, $sym },\n");
}
}
}
print <<EOF;
};
EOF
Aug 28, 2006
Aug 28, 2006
311
312
outputFooter();
313
314
exit 0;
Aug 28, 2006
Aug 28, 2006
315
# end of sdlgenaudiocvt.pl ...