src/audio/sdlgenaudiocvt.pl
changeset 1982 3b4ce57c6215
child 1985 8055185ae4ed
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/sdlgenaudiocvt.pl	Thu Aug 24 12:10:46 2006 +0000
     1.3 @@ -0,0 +1,313 @@
     1.4 +#!/usr/bin/perl -w
     1.5 +
     1.6 +use warnings;
     1.7 +use strict;
     1.8 +
     1.9 +my @audiotypes = qw(
    1.10 +    U8
    1.11 +    S8
    1.12 +    U16LSB
    1.13 +    S16LSB
    1.14 +    U16MSB
    1.15 +    S16MSB
    1.16 +    S32LSB
    1.17 +    S32MSB
    1.18 +    F32LSB
    1.19 +    F32MSB
    1.20 +);
    1.21 +
    1.22 +my %funcs;
    1.23 +
    1.24 +my $custom_converters = 0;
    1.25 +
    1.26 +
    1.27 +sub outputHeader {
    1.28 +    print <<EOF;
    1.29 +/* DO NOT EDIT THIS FILE! It is generated code. */
    1.30 +/* Please modify SDL/src/audio/sdlgenaudiocvt.pl instead. */
    1.31 +
    1.32 +/*
    1.33 +    SDL - Simple DirectMedia Layer
    1.34 +    Copyright (C) 1997-2006 Sam Lantinga
    1.35 +
    1.36 +    This library is free software; you can redistribute it and/or
    1.37 +    modify it under the terms of the GNU Lesser General Public
    1.38 +    License as published by the Free Software Foundation; either
    1.39 +    version 2.1 of the License, or (at your option) any later version.
    1.40 +
    1.41 +    This library is distributed in the hope that it will be useful,
    1.42 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.43 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.44 +    Lesser General Public License for more details.
    1.45 +
    1.46 +    You should have received a copy of the GNU Lesser General Public
    1.47 +    License along with this library; if not, write to the Free Software
    1.48 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.49 +
    1.50 +    Sam Lantinga
    1.51 +    slouken\@libsdl.org
    1.52 +*/
    1.53 +
    1.54 +#include "SDL_config.h"
    1.55 +#include "SDL_audio.h"
    1.56 +#include "SDL_audio_c.h"
    1.57 +
    1.58 +/* Now the generated code... */
    1.59 +
    1.60 +EOF
    1.61 +
    1.62 +    my @vals = ( 127, 255, 32767, 65535, 2147483647 );
    1.63 +    foreach (@vals) {
    1.64 +        my $val = $_;
    1.65 +        my $fval = 1.0 / $val;
    1.66 +        print("#define DIVBY${val} ${fval}f\n");
    1.67 +    }
    1.68 +
    1.69 +    print("\n");
    1.70 +}
    1.71 +
    1.72 +
    1.73 +sub splittype {
    1.74 +    my $t = shift;
    1.75 +    my ($signed, $size, $endian) = $t =~ /([USF])(\d+)([LM]SB|)/;
    1.76 +    my $float = ($signed eq 'F') ? 1 : 0;
    1.77 +    $signed = (($float) or ($signed eq 'S')) ? 1 : 0;
    1.78 +    $endian = 'NONE' if ($endian eq '');
    1.79 +
    1.80 +    my $ctype = '';
    1.81 +    if ($float) {
    1.82 +        $ctype = (($size == 32) ? 'float' : 'double');
    1.83 +    } else {
    1.84 +        $ctype = (($signed) ? 'S' : 'U') . "int${size}";
    1.85 +    }
    1.86 +
    1.87 +    return ($signed, $float, $size, $endian, $ctype);
    1.88 +}
    1.89 +
    1.90 +sub getSwapFunc {
    1.91 +    my ($size, $signed, $float, $endian, $val) = @_;
    1.92 +    my $BEorLE = (($endian eq 'MSB') ? 'BE' : 'LE');
    1.93 +    my $code = '';
    1.94 +
    1.95 +    if ($float) {
    1.96 +        $code = "SDL_SwapFloat${BEorLE}($val)";
    1.97 +    } else {
    1.98 +        if ($size > 8) {
    1.99 +            $code = "SDL_Swap${BEorLE}${size}($val)";
   1.100 +        } else {
   1.101 +            $code = $val;
   1.102 +        }
   1.103 +
   1.104 +        if (($signed) and (!$float)) {
   1.105 +            $code = "((Sint${size}) $code)";
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    return "${code}";
   1.110 +}
   1.111 +
   1.112 +
   1.113 +sub maxIntVal {
   1.114 +    my ($signed, $size) = @_;
   1.115 +    if ($signed) {
   1.116 +        if ($size == 8) {
   1.117 +            return 0x7F;
   1.118 +        } elsif ($size == 16) {
   1.119 +            return 0x7FFF;
   1.120 +        } elsif ($size == 32) {
   1.121 +            return 0x7FFFFFFF;
   1.122 +        }
   1.123 +    } else {
   1.124 +        if ($size == 8) {
   1.125 +            return 0xFF;
   1.126 +        } elsif ($size == 16) {
   1.127 +            return 0xFFFF;
   1.128 +        } elsif ($size == 32) {
   1.129 +            return 0xFFFFFFFF;
   1.130 +        }
   1.131 +    }
   1.132 +
   1.133 +    die("bug in script.\n");
   1.134 +}
   1.135 +
   1.136 +sub getFloatToIntMult {
   1.137 +    my ($signed, $size) = @_;
   1.138 +    my $val = maxIntVal($signed, $size) . '.0';
   1.139 +    $val .= 'f' if ($size < 32);
   1.140 +    return $val;
   1.141 +}
   1.142 +
   1.143 +sub getIntToFloatDivBy {
   1.144 +    my ($signed, $size) = @_;
   1.145 +    return 'DIVBY' . maxIntVal($signed, $size);
   1.146 +}
   1.147 +
   1.148 +sub getSignFlipVal {
   1.149 +    my $size = shift;
   1.150 +    if ($size == 8) {
   1.151 +        return '0x80';
   1.152 +    } elsif ($size == 16) {
   1.153 +        return '0x8000';
   1.154 +    } elsif ($size == 32) {
   1.155 +        return '0x80000000';
   1.156 +    }
   1.157 +
   1.158 +    die("bug in script.\n");
   1.159 +}
   1.160 +
   1.161 +sub buildCvtFunc {
   1.162 +    my ($from, $to) = @_;
   1.163 +    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
   1.164 +    my ($tsigned, $tfloat, $tsize, $tendian, $tctype) = splittype($to);
   1.165 +    my $diffs = 0;
   1.166 +    $diffs++ if ($fsize != $tsize);
   1.167 +    $diffs++ if ($fsigned != $tsigned);
   1.168 +    $diffs++ if ($ffloat != $tfloat);
   1.169 +    $diffs++ if ($fendian ne $tendian);
   1.170 +
   1.171 +    return if ($diffs == 0);
   1.172 +
   1.173 +    my $hashid = "$from/$to";
   1.174 +    if (1) { # !!! FIXME: if ($diffs > 1) {
   1.175 +        my $sym = "SDL_Convert_${from}_to_${to}";
   1.176 +        $funcs{$hashid} = $sym;
   1.177 +        $custom_converters++;
   1.178 +
   1.179 +        # Always unsigned for ints, for possible byteswaps.
   1.180 +        my $srctype = (($ffloat) ? 'float' : "Uint${fsize}");
   1.181 +
   1.182 +        print <<EOF;
   1.183 +static void SDLCALL
   1.184 +${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
   1.185 +{
   1.186 +    int i;
   1.187 +    const $srctype *src;
   1.188 +    $tctype *dst;
   1.189 +
   1.190 +#ifdef DEBUG_CONVERT
   1.191 +    fprintf(stderr, "Converting AUDIO_${from} to AUDIO_${to}.\\n");
   1.192 +#endif
   1.193 +
   1.194 +EOF
   1.195 +
   1.196 +        if ($fsize < $tsize) {
   1.197 +            my $mult = $tsize / $fsize;
   1.198 +            print <<EOF;
   1.199 +    src = (const $srctype *) (cvt->buf + cvt->len_cvt);
   1.200 +    dst = ($tctype *) (cvt->buf + cvt->len_cvt * $mult);
   1.201 +    for (i = cvt->len_cvt / sizeof ($srctype); i; --i, --src, --dst) {
   1.202 +EOF
   1.203 +        } else {
   1.204 +            print <<EOF;
   1.205 +    src = (const $srctype *) cvt->buf;
   1.206 +    dst = ($tctype *) cvt->buf;
   1.207 +    for (i = cvt->len_cvt / sizeof ($srctype); i; --i, ++src, ++dst) {
   1.208 +EOF
   1.209 +        }
   1.210 +
   1.211 +        # Have to convert to/from float/int.
   1.212 +        # !!! FIXME: cast through double for int32<->float?
   1.213 +        my $code = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, '*src');
   1.214 +        if ($ffloat != $tfloat) {
   1.215 +            if ($ffloat) {
   1.216 +                my $mult = getFloatToIntMult($tsigned, $tsize);
   1.217 +                $code = "(($tctype) ($code * $mult))";
   1.218 +            } else {
   1.219 +                # $divby will be the reciprocal, to avoid pipeline stalls
   1.220 +                #  from floating point division...so multiply it.
   1.221 +                my $divby = getIntToFloatDivBy($fsigned, $fsize);
   1.222 +                $code = "(((float) $code) * $divby)";
   1.223 +            }
   1.224 +        } else {
   1.225 +            # All integer conversions here.
   1.226 +            if ($fsigned != $tsigned) {
   1.227 +                my $signflipval = getSignFlipVal($fsize);
   1.228 +                $code = "(($code) ^ $signflipval)";
   1.229 +            }
   1.230 +
   1.231 +            my $shiftval = abs($fsize - $tsize);
   1.232 +            if ($fsize < $tsize) {
   1.233 +                $code = "((($tctype) $code) << $shiftval)";
   1.234 +            } elsif ($fsize > $tsize) {
   1.235 +                $code = "(($tctype) ($code >> $shiftval))";
   1.236 +            }
   1.237 +        }
   1.238 +
   1.239 +        my $swap = getSwapFunc($tsize, $tsigned, $tfloat, $tendian, 'val');
   1.240 +
   1.241 +        print <<EOF;
   1.242 +        const $tctype val = $code;
   1.243 +        *dst = ${swap};
   1.244 +    }
   1.245 +
   1.246 +EOF
   1.247 +
   1.248 +        if ($fsize > $tsize) {
   1.249 +            my $divby = $fsize / $tsize;
   1.250 +            print("    cvt->len_cvt /= $divby;\n");
   1.251 +        } elsif ($fsize < $tsize) {
   1.252 +            my $mult = $tsize / $fsize;
   1.253 +            print("    cvt->len_cvt *= $mult;\n");
   1.254 +        }
   1.255 +
   1.256 +        print <<EOF;
   1.257 +    format = AUDIO_$to;
   1.258 +    if (cvt->filters[++cvt->filter_index]) {
   1.259 +        cvt->filters[cvt->filter_index] (cvt, format);
   1.260 +    }
   1.261 +}
   1.262 +
   1.263 +EOF
   1.264 +
   1.265 +    } else {
   1.266 +        if ($fsigned != $tsigned) {
   1.267 +            $funcs{$hashid} = 'SDL_ConvertSigned';
   1.268 +        } elsif ($ffloat != $tfloat) {
   1.269 +            $funcs{$hashid} = 'SDL_ConvertFloat';
   1.270 +        } elsif ($fsize != $tsize) {
   1.271 +            $funcs{$hashid} = 'SDL_ConvertSize';
   1.272 +        } elsif ($fendian ne $tendian) {
   1.273 +            $funcs{$hashid} = 'SDL_ConvertEndian';
   1.274 +        } else {
   1.275 +            die("error in script.\n");
   1.276 +        }
   1.277 +    }
   1.278 +}
   1.279 +
   1.280 +outputHeader();
   1.281 +
   1.282 +foreach (@audiotypes) {
   1.283 +    my $from = $_;
   1.284 +    foreach (@audiotypes) {
   1.285 +        my $to = $_;
   1.286 +        buildCvtFunc($from, $to);
   1.287 +    }
   1.288 +}
   1.289 +
   1.290 +print <<EOF;
   1.291 +const SDL_AudioTypeFilters sdl_audio_type_filters[] =
   1.292 +{
   1.293 +EOF
   1.294 +
   1.295 +foreach (@audiotypes) {
   1.296 +    my $from = $_;
   1.297 +    foreach (@audiotypes) {
   1.298 +        my $to = $_;
   1.299 +        if ($from ne $to) {
   1.300 +            my $hashid = "$from/$to";
   1.301 +            my $sym = $funcs{$hashid};
   1.302 +            print("    { AUDIO_$from, AUDIO_$to, $sym },\n");
   1.303 +        }
   1.304 +    }
   1.305 +}
   1.306 +
   1.307 +print <<EOF;
   1.308 +};
   1.309 +
   1.310 +
   1.311 +EOF
   1.312 +
   1.313 +exit 0;
   1.314 +
   1.315 +# end of sdlaudiocvt.pl ...
   1.316 +