src/audio/sdlgenaudiocvt.pl
author Sam Lantinga
Mon, 08 Dec 2008 00:27:32 +0000
changeset 2859 99210400e8b9
parent 2011 b2b7154ce016
child 2955 2692999d5271
permissions -rwxr-xr-x
Updated copyright date
icculus@1982
     1
#!/usr/bin/perl -w
icculus@1982
     2
icculus@1982
     3
use warnings;
icculus@1982
     4
use strict;
icculus@1982
     5
icculus@1982
     6
my @audiotypes = qw(
icculus@1982
     7
    U8
icculus@1982
     8
    S8
icculus@1982
     9
    U16LSB
icculus@1982
    10
    S16LSB
icculus@1982
    11
    U16MSB
icculus@1982
    12
    S16MSB
icculus@1982
    13
    S32LSB
icculus@1982
    14
    S32MSB
icculus@1982
    15
    F32LSB
icculus@1982
    16
    F32MSB
icculus@1982
    17
);
icculus@1982
    18
icculus@1982
    19
my %funcs;
icculus@1982
    20
icculus@1982
    21
my $custom_converters = 0;
icculus@1982
    22
icculus@1982
    23
icculus@1982
    24
sub outputHeader {
icculus@1982
    25
    print <<EOF;
slouken@1985
    26
/* DO NOT EDIT!  This file is generated by sdlgenaudiocvt.pl */
icculus@1982
    27
/*
icculus@1982
    28
    SDL - Simple DirectMedia Layer
slouken@2859
    29
    Copyright (C) 1997-2009 Sam Lantinga
icculus@1982
    30
icculus@1982
    31
    This library is free software; you can redistribute it and/or
icculus@1982
    32
    modify it under the terms of the GNU Lesser General Public
icculus@1982
    33
    License as published by the Free Software Foundation; either
icculus@1982
    34
    version 2.1 of the License, or (at your option) any later version.
icculus@1982
    35
icculus@1982
    36
    This library is distributed in the hope that it will be useful,
icculus@1982
    37
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1982
    38
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@1982
    39
    Lesser General Public License for more details.
icculus@1982
    40
icculus@1982
    41
    You should have received a copy of the GNU Lesser General Public
icculus@1982
    42
    License along with this library; if not, write to the Free Software
icculus@1982
    43
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@1982
    44
icculus@1982
    45
    Sam Lantinga
icculus@1982
    46
    slouken\@libsdl.org
icculus@1982
    47
*/
icculus@1982
    48
icculus@1982
    49
#include "SDL_config.h"
icculus@1982
    50
#include "SDL_audio.h"
icculus@1982
    51
#include "SDL_audio_c.h"
icculus@1982
    52
slouken@1985
    53
/* *INDENT-OFF* */
icculus@1982
    54
icculus@1982
    55
EOF
icculus@1982
    56
icculus@2011
    57
    my @vals = ( 127, 32767, 2147483647 );
icculus@1982
    58
    foreach (@vals) {
icculus@1982
    59
        my $val = $_;
icculus@1982
    60
        my $fval = 1.0 / $val;
icculus@1982
    61
        print("#define DIVBY${val} ${fval}f\n");
icculus@1982
    62
    }
icculus@1982
    63
icculus@1982
    64
    print("\n");
icculus@1982
    65
}
icculus@1982
    66
slouken@1985
    67
sub outputFooter {
slouken@1985
    68
    print <<EOF;
slouken@1985
    69
/* *INDENT-ON* */
slouken@1985
    70
slouken@1985
    71
/* vi: set ts=4 sw=4 expandtab: */
slouken@1985
    72
EOF
slouken@1985
    73
}
icculus@1982
    74
icculus@1982
    75
sub splittype {
icculus@1982
    76
    my $t = shift;
icculus@1982
    77
    my ($signed, $size, $endian) = $t =~ /([USF])(\d+)([LM]SB|)/;
icculus@1982
    78
    my $float = ($signed eq 'F') ? 1 : 0;
icculus@1982
    79
    $signed = (($float) or ($signed eq 'S')) ? 1 : 0;
icculus@1982
    80
    $endian = 'NONE' if ($endian eq '');
icculus@1982
    81
icculus@1982
    82
    my $ctype = '';
icculus@1982
    83
    if ($float) {
icculus@1982
    84
        $ctype = (($size == 32) ? 'float' : 'double');
icculus@1982
    85
    } else {
icculus@1982
    86
        $ctype = (($signed) ? 'S' : 'U') . "int${size}";
icculus@1982
    87
    }
icculus@1982
    88
icculus@1982
    89
    return ($signed, $float, $size, $endian, $ctype);
icculus@1982
    90
}
icculus@1982
    91
icculus@1982
    92
sub getSwapFunc {
icculus@1982
    93
    my ($size, $signed, $float, $endian, $val) = @_;
icculus@1982
    94
    my $BEorLE = (($endian eq 'MSB') ? 'BE' : 'LE');
icculus@1982
    95
    my $code = '';
icculus@1982
    96
icculus@1982
    97
    if ($float) {
icculus@1982
    98
        $code = "SDL_SwapFloat${BEorLE}($val)";
icculus@1982
    99
    } else {
icculus@1982
   100
        if ($size > 8) {
icculus@1982
   101
            $code = "SDL_Swap${BEorLE}${size}($val)";
icculus@1982
   102
        } else {
icculus@1982
   103
            $code = $val;
icculus@1982
   104
        }
icculus@1982
   105
icculus@1982
   106
        if (($signed) and (!$float)) {
icculus@1982
   107
            $code = "((Sint${size}) $code)";
icculus@1982
   108
        }
icculus@1982
   109
    }
icculus@1982
   110
icculus@1982
   111
    return "${code}";
icculus@1982
   112
}
icculus@1982
   113
icculus@1982
   114
icculus@1982
   115
sub maxIntVal {
icculus@2011
   116
    my $size = shift;
icculus@2011
   117
    if ($size == 8) {
icculus@2011
   118
        return 0x7F;
icculus@2011
   119
    } elsif ($size == 16) {
icculus@2011
   120
        return 0x7FFF;
icculus@2011
   121
    } elsif ($size == 32) {
icculus@2011
   122
        return 0x7FFFFFFF;
icculus@1982
   123
    }
icculus@1982
   124
icculus@1982
   125
    die("bug in script.\n");
icculus@1982
   126
}
icculus@1982
   127
icculus@1982
   128
sub getFloatToIntMult {
icculus@2011
   129
    my $size = shift;
icculus@2011
   130
    my $val = maxIntVal($size) . '.0';
icculus@1982
   131
    $val .= 'f' if ($size < 32);
icculus@1982
   132
    return $val;
icculus@1982
   133
}
icculus@1982
   134
icculus@1982
   135
sub getIntToFloatDivBy {
icculus@2011
   136
    my $size = shift;
icculus@2011
   137
    return 'DIVBY' . maxIntVal($size);
icculus@1982
   138
}
icculus@1982
   139
icculus@1982
   140
sub getSignFlipVal {
icculus@1982
   141
    my $size = shift;
icculus@1982
   142
    if ($size == 8) {
icculus@1982
   143
        return '0x80';
icculus@1982
   144
    } elsif ($size == 16) {
icculus@1982
   145
        return '0x8000';
icculus@1982
   146
    } elsif ($size == 32) {
icculus@1982
   147
        return '0x80000000';
icculus@1982
   148
    }
icculus@1982
   149
icculus@1982
   150
    die("bug in script.\n");
icculus@1982
   151
}
icculus@1982
   152
icculus@1982
   153
sub buildCvtFunc {
icculus@1982
   154
    my ($from, $to) = @_;
icculus@1982
   155
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@1982
   156
    my ($tsigned, $tfloat, $tsize, $tendian, $tctype) = splittype($to);
icculus@1982
   157
    my $diffs = 0;
icculus@1982
   158
    $diffs++ if ($fsize != $tsize);
icculus@1982
   159
    $diffs++ if ($fsigned != $tsigned);
icculus@1982
   160
    $diffs++ if ($ffloat != $tfloat);
icculus@1982
   161
    $diffs++ if ($fendian ne $tendian);
icculus@1982
   162
icculus@1982
   163
    return if ($diffs == 0);
icculus@1982
   164
icculus@1982
   165
    my $hashid = "$from/$to";
icculus@1982
   166
    if (1) { # !!! FIXME: if ($diffs > 1) {
icculus@1982
   167
        my $sym = "SDL_Convert_${from}_to_${to}";
icculus@1982
   168
        $funcs{$hashid} = $sym;
icculus@1982
   169
        $custom_converters++;
icculus@1982
   170
icculus@1982
   171
        # Always unsigned for ints, for possible byteswaps.
icculus@1982
   172
        my $srctype = (($ffloat) ? 'float' : "Uint${fsize}");
icculus@1982
   173
icculus@1982
   174
        print <<EOF;
icculus@1982
   175
static void SDLCALL
icculus@1982
   176
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@1982
   177
{
icculus@1982
   178
    int i;
icculus@1982
   179
    const $srctype *src;
icculus@1982
   180
    $tctype *dst;
icculus@1982
   181
icculus@1982
   182
#ifdef DEBUG_CONVERT
icculus@1982
   183
    fprintf(stderr, "Converting AUDIO_${from} to AUDIO_${to}.\\n");
icculus@1982
   184
#endif
icculus@1982
   185
icculus@1982
   186
EOF
icculus@1982
   187
icculus@1982
   188
        if ($fsize < $tsize) {
icculus@1982
   189
            my $mult = $tsize / $fsize;
icculus@1982
   190
            print <<EOF;
icculus@1982
   191
    src = (const $srctype *) (cvt->buf + cvt->len_cvt);
icculus@1982
   192
    dst = ($tctype *) (cvt->buf + cvt->len_cvt * $mult);
icculus@1982
   193
    for (i = cvt->len_cvt / sizeof ($srctype); i; --i, --src, --dst) {
icculus@1982
   194
EOF
icculus@1982
   195
        } else {
icculus@1982
   196
            print <<EOF;
icculus@1982
   197
    src = (const $srctype *) cvt->buf;
icculus@1982
   198
    dst = ($tctype *) cvt->buf;
icculus@1982
   199
    for (i = cvt->len_cvt / sizeof ($srctype); i; --i, ++src, ++dst) {
icculus@1982
   200
EOF
icculus@1982
   201
        }
icculus@1982
   202
icculus@1982
   203
        # Have to convert to/from float/int.
icculus@1982
   204
        # !!! FIXME: cast through double for int32<->float?
icculus@1982
   205
        my $code = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, '*src');
icculus@1982
   206
        if ($ffloat != $tfloat) {
icculus@1982
   207
            if ($ffloat) {
icculus@2011
   208
                my $mult = getFloatToIntMult($tsize);
icculus@2011
   209
                if (!$tsigned) {   # bump from -1.0f/1.0f to 0.0f/2.0f
icculus@2011
   210
                    $code = "($code + 1.0f)";
icculus@2011
   211
                }
icculus@1982
   212
                $code = "(($tctype) ($code * $mult))";
icculus@1982
   213
            } else {
icculus@1982
   214
                # $divby will be the reciprocal, to avoid pipeline stalls
icculus@1982
   215
                #  from floating point division...so multiply it.
icculus@2011
   216
                my $divby = getIntToFloatDivBy($fsize);
icculus@1982
   217
                $code = "(((float) $code) * $divby)";
icculus@2011
   218
                if (!$fsigned) {   # bump from 0.0f/2.0f to -1.0f/1.0f.
icculus@2011
   219
                    $code = "($code - 1.0f)";
icculus@2011
   220
                }
icculus@1982
   221
            }
icculus@1982
   222
        } else {
icculus@1982
   223
            # All integer conversions here.
icculus@1982
   224
            if ($fsigned != $tsigned) {
icculus@1982
   225
                my $signflipval = getSignFlipVal($fsize);
icculus@1982
   226
                $code = "(($code) ^ $signflipval)";
icculus@1982
   227
            }
icculus@1982
   228
icculus@1982
   229
            my $shiftval = abs($fsize - $tsize);
icculus@1982
   230
            if ($fsize < $tsize) {
icculus@1982
   231
                $code = "((($tctype) $code) << $shiftval)";
icculus@1982
   232
            } elsif ($fsize > $tsize) {
icculus@1982
   233
                $code = "(($tctype) ($code >> $shiftval))";
icculus@1982
   234
            }
icculus@1982
   235
        }
icculus@1982
   236
icculus@1982
   237
        my $swap = getSwapFunc($tsize, $tsigned, $tfloat, $tendian, 'val');
icculus@1982
   238
icculus@1982
   239
        print <<EOF;
icculus@1982
   240
        const $tctype val = $code;
icculus@1982
   241
        *dst = ${swap};
icculus@1982
   242
    }
icculus@1982
   243
icculus@1982
   244
EOF
icculus@1982
   245
icculus@1982
   246
        if ($fsize > $tsize) {
icculus@1982
   247
            my $divby = $fsize / $tsize;
icculus@1982
   248
            print("    cvt->len_cvt /= $divby;\n");
icculus@1982
   249
        } elsif ($fsize < $tsize) {
icculus@1982
   250
            my $mult = $tsize / $fsize;
icculus@1982
   251
            print("    cvt->len_cvt *= $mult;\n");
icculus@1982
   252
        }
icculus@1982
   253
icculus@1982
   254
        print <<EOF;
icculus@1982
   255
    format = AUDIO_$to;
icculus@1982
   256
    if (cvt->filters[++cvt->filter_index]) {
icculus@1982
   257
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   258
    }
icculus@1982
   259
}
icculus@1982
   260
icculus@1982
   261
EOF
icculus@1982
   262
icculus@1982
   263
    } else {
icculus@1982
   264
        if ($fsigned != $tsigned) {
icculus@1982
   265
            $funcs{$hashid} = 'SDL_ConvertSigned';
icculus@1982
   266
        } elsif ($ffloat != $tfloat) {
icculus@1982
   267
            $funcs{$hashid} = 'SDL_ConvertFloat';
icculus@1982
   268
        } elsif ($fsize != $tsize) {
icculus@1982
   269
            $funcs{$hashid} = 'SDL_ConvertSize';
icculus@1982
   270
        } elsif ($fendian ne $tendian) {
icculus@1982
   271
            $funcs{$hashid} = 'SDL_ConvertEndian';
icculus@1982
   272
        } else {
icculus@1982
   273
            die("error in script.\n");
icculus@1982
   274
        }
icculus@1982
   275
    }
icculus@1982
   276
}
icculus@1982
   277
icculus@1982
   278
outputHeader();
icculus@1982
   279
icculus@1982
   280
foreach (@audiotypes) {
icculus@1982
   281
    my $from = $_;
icculus@1982
   282
    foreach (@audiotypes) {
icculus@1982
   283
        my $to = $_;
icculus@1982
   284
        buildCvtFunc($from, $to);
icculus@1982
   285
    }
icculus@1982
   286
}
icculus@1982
   287
icculus@1982
   288
print <<EOF;
icculus@1982
   289
const SDL_AudioTypeFilters sdl_audio_type_filters[] =
icculus@1982
   290
{
icculus@1982
   291
EOF
icculus@1982
   292
icculus@1982
   293
foreach (@audiotypes) {
icculus@1982
   294
    my $from = $_;
icculus@1982
   295
    foreach (@audiotypes) {
icculus@1982
   296
        my $to = $_;
icculus@1982
   297
        if ($from ne $to) {
icculus@1982
   298
            my $hashid = "$from/$to";
icculus@1982
   299
            my $sym = $funcs{$hashid};
icculus@1982
   300
            print("    { AUDIO_$from, AUDIO_$to, $sym },\n");
icculus@1982
   301
        }
icculus@1982
   302
    }
icculus@1982
   303
}
icculus@1982
   304
icculus@1982
   305
print <<EOF;
icculus@1982
   306
};
icculus@1982
   307
icculus@1982
   308
icculus@1982
   309
EOF
icculus@1982
   310
slouken@1985
   311
outputFooter();
slouken@1985
   312
icculus@1982
   313
exit 0;
icculus@1982
   314
slouken@1985
   315
# end of sdlgenaudiocvt.pl ...
icculus@1982
   316