src/audio/sdlgenaudiocvt.pl
author Sam Lantinga
Sat, 19 Sep 2009 13:29:40 +0000
changeset 3280 00cace2d9080
parent 3032 77c3e67f0740
child 3602 bfa8d34ce03a
permissions -rwxr-xr-x
Merged a cleaned up version of Jiang's code changes from Google Summer of Code 2009
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@3008
    19
my @channels = ( 1, 2, 4, 6, 8 );
icculus@1982
    20
my %funcs;
icculus@3008
    21
my $custom_converters = 0;
icculus@3008
    22
icculus@1982
    23
icculus@3008
    24
sub getTypeConvertHashId {
icculus@3008
    25
    my ($from, $to) = @_;
icculus@3008
    26
    return "TYPECONVERTER $from/$to";
icculus@3008
    27
}
icculus@3008
    28
icculus@3008
    29
icculus@3008
    30
sub getResamplerHashId {
icculus@3008
    31
    my ($from, $channels, $upsample, $multiple) = @_;
icculus@3008
    32
    return "RESAMPLER $from/$channels/$upsample/$multiple";
icculus@3008
    33
}
icculus@1982
    34
icculus@1982
    35
icculus@1982
    36
sub outputHeader {
icculus@1982
    37
    print <<EOF;
slouken@1985
    38
/* DO NOT EDIT!  This file is generated by sdlgenaudiocvt.pl */
icculus@1982
    39
/*
icculus@1982
    40
    SDL - Simple DirectMedia Layer
slouken@2859
    41
    Copyright (C) 1997-2009 Sam Lantinga
icculus@1982
    42
icculus@1982
    43
    This library is free software; you can redistribute it and/or
icculus@1982
    44
    modify it under the terms of the GNU Lesser General Public
icculus@1982
    45
    License as published by the Free Software Foundation; either
icculus@1982
    46
    version 2.1 of the License, or (at your option) any later version.
icculus@1982
    47
icculus@1982
    48
    This library is distributed in the hope that it will be useful,
icculus@1982
    49
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1982
    50
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
icculus@1982
    51
    Lesser General Public License for more details.
icculus@1982
    52
icculus@1982
    53
    You should have received a copy of the GNU Lesser General Public
icculus@1982
    54
    License along with this library; if not, write to the Free Software
icculus@1982
    55
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@1982
    56
icculus@1982
    57
    Sam Lantinga
icculus@1982
    58
    slouken\@libsdl.org
icculus@1982
    59
*/
icculus@1982
    60
icculus@1982
    61
#include "SDL_config.h"
icculus@1982
    62
#include "SDL_audio.h"
icculus@1982
    63
#include "SDL_audio_c.h"
icculus@1982
    64
icculus@3019
    65
#ifndef DEBUG_CONVERT
icculus@3019
    66
#define DEBUG_CONVERT 0
icculus@3019
    67
#endif
icculus@3019
    68
icculus@3019
    69
icculus@3019
    70
/* If you can guarantee your data and need space, you can eliminate code... */
icculus@3019
    71
icculus@3019
    72
/* Just build the arbitrary resamplers if you're saving code space. */
icculus@3019
    73
#ifndef LESS_RESAMPLERS
icculus@3019
    74
#define LESS_RESAMPLERS 0
icculus@3019
    75
#endif
icculus@3019
    76
icculus@3019
    77
/* Don't build any resamplers if you're REALLY saving code space. */
icculus@3019
    78
#ifndef NO_RESAMPLERS
icculus@3019
    79
#define NO_RESAMPLERS 0
icculus@3019
    80
#endif
icculus@3019
    81
icculus@3019
    82
/* Don't build any type converters if you're saving code space. */
icculus@3019
    83
#ifndef NO_CONVERTERS
icculus@3019
    84
#define NO_CONVERTERS 0
icculus@3019
    85
#endif
icculus@3019
    86
icculus@3019
    87
slouken@1985
    88
/* *INDENT-OFF* */
icculus@1982
    89
icculus@1982
    90
EOF
icculus@1982
    91
icculus@2011
    92
    my @vals = ( 127, 32767, 2147483647 );
icculus@1982
    93
    foreach (@vals) {
icculus@1982
    94
        my $val = $_;
icculus@1982
    95
        my $fval = 1.0 / $val;
icculus@1982
    96
        print("#define DIVBY${val} ${fval}f\n");
icculus@1982
    97
    }
icculus@1982
    98
icculus@1982
    99
    print("\n");
icculus@1982
   100
}
icculus@1982
   101
slouken@1985
   102
sub outputFooter {
slouken@1985
   103
    print <<EOF;
icculus@3008
   104
/* $custom_converters converters generated. */
icculus@3008
   105
slouken@1985
   106
/* *INDENT-ON* */
slouken@1985
   107
slouken@1985
   108
/* vi: set ts=4 sw=4 expandtab: */
slouken@1985
   109
EOF
slouken@1985
   110
}
icculus@1982
   111
icculus@1982
   112
sub splittype {
icculus@1982
   113
    my $t = shift;
icculus@1982
   114
    my ($signed, $size, $endian) = $t =~ /([USF])(\d+)([LM]SB|)/;
icculus@1982
   115
    my $float = ($signed eq 'F') ? 1 : 0;
icculus@1982
   116
    $signed = (($float) or ($signed eq 'S')) ? 1 : 0;
icculus@1982
   117
    $endian = 'NONE' if ($endian eq '');
icculus@1982
   118
icculus@1982
   119
    my $ctype = '';
icculus@1982
   120
    if ($float) {
icculus@1982
   121
        $ctype = (($size == 32) ? 'float' : 'double');
icculus@1982
   122
    } else {
icculus@1982
   123
        $ctype = (($signed) ? 'S' : 'U') . "int${size}";
icculus@1982
   124
    }
icculus@1982
   125
icculus@1982
   126
    return ($signed, $float, $size, $endian, $ctype);
icculus@1982
   127
}
icculus@1982
   128
icculus@1982
   129
sub getSwapFunc {
icculus@1982
   130
    my ($size, $signed, $float, $endian, $val) = @_;
icculus@1982
   131
    my $BEorLE = (($endian eq 'MSB') ? 'BE' : 'LE');
icculus@1982
   132
    my $code = '';
icculus@1982
   133
icculus@1982
   134
    if ($float) {
icculus@1982
   135
        $code = "SDL_SwapFloat${BEorLE}($val)";
icculus@1982
   136
    } else {
icculus@1982
   137
        if ($size > 8) {
icculus@1982
   138
            $code = "SDL_Swap${BEorLE}${size}($val)";
icculus@1982
   139
        } else {
icculus@1982
   140
            $code = $val;
icculus@1982
   141
        }
icculus@1982
   142
icculus@1982
   143
        if (($signed) and (!$float)) {
icculus@1982
   144
            $code = "((Sint${size}) $code)";
icculus@1982
   145
        }
icculus@1982
   146
    }
icculus@1982
   147
icculus@1982
   148
    return "${code}";
icculus@1982
   149
}
icculus@1982
   150
icculus@1982
   151
icculus@1982
   152
sub maxIntVal {
icculus@2011
   153
    my $size = shift;
icculus@2011
   154
    if ($size == 8) {
icculus@2011
   155
        return 0x7F;
icculus@2011
   156
    } elsif ($size == 16) {
icculus@2011
   157
        return 0x7FFF;
icculus@2011
   158
    } elsif ($size == 32) {
icculus@2011
   159
        return 0x7FFFFFFF;
icculus@1982
   160
    }
icculus@1982
   161
icculus@1982
   162
    die("bug in script.\n");
icculus@1982
   163
}
icculus@1982
   164
icculus@1982
   165
sub getFloatToIntMult {
icculus@2011
   166
    my $size = shift;
icculus@2011
   167
    my $val = maxIntVal($size) . '.0';
icculus@1982
   168
    $val .= 'f' if ($size < 32);
icculus@1982
   169
    return $val;
icculus@1982
   170
}
icculus@1982
   171
icculus@1982
   172
sub getIntToFloatDivBy {
icculus@2011
   173
    my $size = shift;
icculus@2011
   174
    return 'DIVBY' . maxIntVal($size);
icculus@1982
   175
}
icculus@1982
   176
icculus@1982
   177
sub getSignFlipVal {
icculus@1982
   178
    my $size = shift;
icculus@1982
   179
    if ($size == 8) {
icculus@1982
   180
        return '0x80';
icculus@1982
   181
    } elsif ($size == 16) {
icculus@1982
   182
        return '0x8000';
icculus@1982
   183
    } elsif ($size == 32) {
icculus@1982
   184
        return '0x80000000';
icculus@1982
   185
    }
icculus@1982
   186
icculus@1982
   187
    die("bug in script.\n");
icculus@1982
   188
}
icculus@1982
   189
icculus@1982
   190
sub buildCvtFunc {
icculus@1982
   191
    my ($from, $to) = @_;
icculus@1982
   192
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@1982
   193
    my ($tsigned, $tfloat, $tsize, $tendian, $tctype) = splittype($to);
icculus@1982
   194
    my $diffs = 0;
icculus@1982
   195
    $diffs++ if ($fsize != $tsize);
icculus@1982
   196
    $diffs++ if ($fsigned != $tsigned);
icculus@1982
   197
    $diffs++ if ($ffloat != $tfloat);
icculus@1982
   198
    $diffs++ if ($fendian ne $tendian);
icculus@1982
   199
icculus@1982
   200
    return if ($diffs == 0);
icculus@1982
   201
icculus@3008
   202
    my $hashid = getTypeConvertHashId($from, $to);
icculus@1982
   203
    if (1) { # !!! FIXME: if ($diffs > 1) {
icculus@1982
   204
        my $sym = "SDL_Convert_${from}_to_${to}";
icculus@1982
   205
        $funcs{$hashid} = $sym;
icculus@1982
   206
        $custom_converters++;
icculus@1982
   207
icculus@1982
   208
        # Always unsigned for ints, for possible byteswaps.
icculus@1982
   209
        my $srctype = (($ffloat) ? 'float' : "Uint${fsize}");
icculus@1982
   210
icculus@1982
   211
        print <<EOF;
icculus@1982
   212
static void SDLCALL
icculus@1982
   213
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@1982
   214
{
icculus@1982
   215
    int i;
icculus@1982
   216
    const $srctype *src;
icculus@1982
   217
    $tctype *dst;
icculus@1982
   218
slouken@3032
   219
#if DEBUG_CONVERT
icculus@1982
   220
    fprintf(stderr, "Converting AUDIO_${from} to AUDIO_${to}.\\n");
icculus@1982
   221
#endif
icculus@1982
   222
icculus@1982
   223
EOF
icculus@1982
   224
icculus@1982
   225
        if ($fsize < $tsize) {
icculus@1982
   226
            my $mult = $tsize / $fsize;
icculus@1982
   227
            print <<EOF;
icculus@2956
   228
    src = ((const $srctype *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@2956
   229
    dst = (($tctype *) (cvt->buf + cvt->len_cvt * $mult)) - 1;
icculus@1982
   230
    for (i = cvt->len_cvt / sizeof ($srctype); i; --i, --src, --dst) {
icculus@1982
   231
EOF
icculus@1982
   232
        } else {
icculus@1982
   233
            print <<EOF;
icculus@1982
   234
    src = (const $srctype *) cvt->buf;
icculus@1982
   235
    dst = ($tctype *) cvt->buf;
icculus@1982
   236
    for (i = cvt->len_cvt / sizeof ($srctype); i; --i, ++src, ++dst) {
icculus@1982
   237
EOF
icculus@1982
   238
        }
icculus@1982
   239
icculus@1982
   240
        # Have to convert to/from float/int.
icculus@1982
   241
        # !!! FIXME: cast through double for int32<->float?
icculus@1982
   242
        my $code = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, '*src');
icculus@1982
   243
        if ($ffloat != $tfloat) {
icculus@1982
   244
            if ($ffloat) {
icculus@2011
   245
                my $mult = getFloatToIntMult($tsize);
icculus@2011
   246
                if (!$tsigned) {   # bump from -1.0f/1.0f to 0.0f/2.0f
icculus@2011
   247
                    $code = "($code + 1.0f)";
icculus@2011
   248
                }
icculus@1982
   249
                $code = "(($tctype) ($code * $mult))";
icculus@1982
   250
            } else {
icculus@1982
   251
                # $divby will be the reciprocal, to avoid pipeline stalls
icculus@1982
   252
                #  from floating point division...so multiply it.
icculus@2011
   253
                my $divby = getIntToFloatDivBy($fsize);
icculus@1982
   254
                $code = "(((float) $code) * $divby)";
icculus@2011
   255
                if (!$fsigned) {   # bump from 0.0f/2.0f to -1.0f/1.0f.
icculus@2011
   256
                    $code = "($code - 1.0f)";
icculus@2011
   257
                }
icculus@1982
   258
            }
icculus@1982
   259
        } else {
icculus@1982
   260
            # All integer conversions here.
icculus@1982
   261
            if ($fsigned != $tsigned) {
icculus@1982
   262
                my $signflipval = getSignFlipVal($fsize);
icculus@1982
   263
                $code = "(($code) ^ $signflipval)";
icculus@1982
   264
            }
icculus@1982
   265
icculus@1982
   266
            my $shiftval = abs($fsize - $tsize);
icculus@1982
   267
            if ($fsize < $tsize) {
icculus@1982
   268
                $code = "((($tctype) $code) << $shiftval)";
icculus@1982
   269
            } elsif ($fsize > $tsize) {
icculus@1982
   270
                $code = "(($tctype) ($code >> $shiftval))";
icculus@1982
   271
            }
icculus@1982
   272
        }
icculus@1982
   273
icculus@1982
   274
        my $swap = getSwapFunc($tsize, $tsigned, $tfloat, $tendian, 'val');
icculus@1982
   275
icculus@1982
   276
        print <<EOF;
icculus@1982
   277
        const $tctype val = $code;
icculus@1982
   278
        *dst = ${swap};
icculus@1982
   279
    }
icculus@1982
   280
icculus@1982
   281
EOF
icculus@1982
   282
icculus@1982
   283
        if ($fsize > $tsize) {
icculus@1982
   284
            my $divby = $fsize / $tsize;
icculus@1982
   285
            print("    cvt->len_cvt /= $divby;\n");
icculus@1982
   286
        } elsif ($fsize < $tsize) {
icculus@1982
   287
            my $mult = $tsize / $fsize;
icculus@1982
   288
            print("    cvt->len_cvt *= $mult;\n");
icculus@1982
   289
        }
icculus@1982
   290
icculus@1982
   291
        print <<EOF;
icculus@1982
   292
    if (cvt->filters[++cvt->filter_index]) {
icculus@2955
   293
        cvt->filters[cvt->filter_index] (cvt, AUDIO_$to);
icculus@1982
   294
    }
icculus@1982
   295
}
icculus@1982
   296
icculus@1982
   297
EOF
icculus@1982
   298
icculus@1982
   299
    } else {
icculus@1982
   300
        if ($fsigned != $tsigned) {
icculus@1982
   301
            $funcs{$hashid} = 'SDL_ConvertSigned';
icculus@1982
   302
        } elsif ($ffloat != $tfloat) {
icculus@1982
   303
            $funcs{$hashid} = 'SDL_ConvertFloat';
icculus@1982
   304
        } elsif ($fsize != $tsize) {
icculus@1982
   305
            $funcs{$hashid} = 'SDL_ConvertSize';
icculus@1982
   306
        } elsif ($fendian ne $tendian) {
icculus@1982
   307
            $funcs{$hashid} = 'SDL_ConvertEndian';
icculus@1982
   308
        } else {
icculus@1982
   309
            die("error in script.\n");
icculus@1982
   310
        }
icculus@1982
   311
    }
icculus@1982
   312
}
icculus@1982
   313
icculus@3008
   314
icculus@3008
   315
sub buildTypeConverters {
icculus@3019
   316
    print "#if !NO_CONVERTERS\n\n";
icculus@3008
   317
    foreach (@audiotypes) {
icculus@3008
   318
        my $from = $_;
icculus@3008
   319
        foreach (@audiotypes) {
icculus@3008
   320
            my $to = $_;
icculus@3008
   321
            buildCvtFunc($from, $to);
icculus@3008
   322
        }
icculus@3008
   323
    }
icculus@3019
   324
    print "#endif  /* !NO_CONVERTERS */\n\n\n";
icculus@3008
   325
icculus@3008
   326
    print "const SDL_AudioTypeFilters sdl_audio_type_filters[] =\n{\n";
icculus@3019
   327
    print "#if !NO_CONVERTERS\n";
icculus@3008
   328
    foreach (@audiotypes) {
icculus@3008
   329
        my $from = $_;
icculus@3008
   330
        foreach (@audiotypes) {
icculus@3008
   331
            my $to = $_;
icculus@3008
   332
            if ($from ne $to) {
icculus@3008
   333
                my $hashid = getTypeConvertHashId($from, $to);
icculus@3008
   334
                my $sym = $funcs{$hashid};
icculus@3008
   335
                print("    { AUDIO_$from, AUDIO_$to, $sym },\n");
icculus@3008
   336
            }
icculus@3008
   337
        }
icculus@3008
   338
    }
icculus@3019
   339
    print "#endif  /* !NO_CONVERTERS */\n";
icculus@3008
   340
icculus@3020
   341
    print("    { 0, 0, NULL }\n");
icculus@3008
   342
    print "};\n\n\n";
icculus@3008
   343
}
icculus@3008
   344
icculus@3008
   345
sub getBiggerCtype {
icculus@3008
   346
    my ($isfloat, $size) = @_;
icculus@3008
   347
icculus@3008
   348
    if ($isfloat) {
icculus@3008
   349
        if ($size == 32) {
icculus@3008
   350
            return 'double';
icculus@3008
   351
        }
icculus@3008
   352
        die("bug in script.\n");
icculus@3008
   353
    }
icculus@3008
   354
icculus@3008
   355
    if ($size == 8) {
icculus@3008
   356
        return 'Sint16';
icculus@3008
   357
    } elsif ($size == 16) {
icculus@3008
   358
        return 'Sint32'
icculus@3008
   359
    } elsif ($size == 32) {
icculus@3008
   360
        return 'Sint64'
icculus@3008
   361
    }
icculus@3008
   362
icculus@3008
   363
    die("bug in script.\n");
icculus@3008
   364
}
icculus@3008
   365
icculus@3008
   366
icculus@3008
   367
# These handle arbitrary resamples...44100Hz to 48000Hz, for example.
icculus@3008
   368
# Man, this code is skanky.
icculus@3008
   369
sub buildArbitraryResampleFunc {
icculus@3008
   370
    # !!! FIXME: we do a lot of unnecessary and ugly casting in here, due to getSwapFunc().
icculus@3008
   371
    my ($from, $channels, $upsample) = @_;
icculus@3008
   372
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@3008
   373
icculus@3008
   374
    my $bigger = getBiggerCtype($ffloat, $fsize);
icculus@3008
   375
    my $interp = ($ffloat) ? '* 0.5' : '>> 1';
icculus@3008
   376
icculus@3008
   377
    my $resample = ($upsample) ? 'Upsample' : 'Downsample';
icculus@3008
   378
    my $hashid = getResamplerHashId($from, $channels, $upsample, 0);
icculus@3008
   379
    my $sym = "SDL_${resample}_${from}_${channels}c";
icculus@3008
   380
    $funcs{$hashid} = $sym;
icculus@3008
   381
    $custom_converters++;
icculus@3008
   382
icculus@3008
   383
    my $fudge = $fsize * $channels * 2;  # !!! FIXME
icculus@3008
   384
    my $eps_adjust = ($upsample) ? 'dstsize' : 'srcsize';
icculus@3008
   385
    my $incr = '';
icculus@3008
   386
    my $incr2 = '';
icculus@3008
   387
icculus@3008
   388
icculus@3008
   389
    # !!! FIXME: DEBUG_CONVERT should report frequencies.
icculus@3008
   390
    print <<EOF;
icculus@3008
   391
static void SDLCALL
icculus@3008
   392
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@3008
   393
{
slouken@3032
   394
#if DEBUG_CONVERT
icculus@3008
   395
    fprintf(stderr, "$resample arbitrary (x%f) AUDIO_${from}, ${channels} channels.\\n", cvt->rate_incr);
icculus@3008
   396
#endif
icculus@3008
   397
icculus@3008
   398
    const int srcsize = cvt->len_cvt - $fudge;
icculus@3008
   399
    const int dstsize = (int) (((double)cvt->len_cvt) * cvt->rate_incr);
icculus@3008
   400
    register int eps = 0;
icculus@3008
   401
EOF
icculus@1982
   402
icculus@3008
   403
    # Upsampling (growing the buffer) needs to work backwards, since we
icculus@3008
   404
    #  overwrite the buffer as we go.
icculus@3008
   405
    if ($upsample) {
icculus@3008
   406
        print <<EOF;
icculus@3008
   407
    $fctype *dst = (($fctype *) (cvt->buf + dstsize)) - $channels;
icculus@3008
   408
    const $fctype *src = (($fctype *) (cvt->buf + cvt->len_cvt)) - $channels;
icculus@3008
   409
    const $fctype *target = ((const $fctype *) cvt->buf) - $channels;
icculus@3008
   410
EOF
icculus@3008
   411
    } else {
icculus@3008
   412
        print <<EOF;
icculus@3008
   413
    $fctype *dst = ($fctype *) cvt->buf;
icculus@3008
   414
    const $fctype *src = ($fctype *) cvt->buf;
icculus@3008
   415
    const $fctype *target = (const $fctype *) (cvt->buf + dstsize);
icculus@3008
   416
EOF
icculus@3008
   417
    }
icculus@3008
   418
icculus@3008
   419
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   420
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   421
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   422
        print <<EOF;
icculus@3008
   423
    $fctype sample${idx} = $val;
icculus@3008
   424
EOF
icculus@3008
   425
    }
icculus@3008
   426
icculus@3008
   427
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   428
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   429
        print <<EOF;
icculus@3008
   430
    $fctype last_sample${idx} = sample${idx};
icculus@3008
   431
EOF
icculus@3008
   432
    }
icculus@3008
   433
icculus@3008
   434
    print <<EOF;
icculus@3008
   435
    while (dst != target) {
icculus@3008
   436
EOF
icculus@3008
   437
icculus@3008
   438
    if ($upsample) {
icculus@3008
   439
        for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   440
            # !!! FIXME: don't do this swap every write, just when the samples change.
icculus@3008
   441
            my $idx = (($channels - $i) - 1);
icculus@3008
   442
            my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "sample${idx}");
icculus@3008
   443
            print <<EOF;
icculus@3008
   444
        dst[$idx] = $val;
icculus@3008
   445
EOF
icculus@3008
   446
        }
icculus@3008
   447
icculus@3008
   448
        $incr = ($channels == 1) ? 'dst--' : "dst -= $channels";
icculus@3008
   449
        $incr2 = ($channels == 1) ? 'src--' : "src -= $channels";
icculus@3008
   450
icculus@3008
   451
        print <<EOF;
icculus@3008
   452
        $incr;
icculus@3008
   453
        eps += srcsize;
icculus@3008
   454
        if ((eps << 1) >= dstsize) {
icculus@3008
   455
            $incr2;
icculus@3008
   456
EOF
icculus@3008
   457
    } else {  # downsample.
icculus@3008
   458
        $incr = ($channels == 1) ? 'src++' : "src += $channels";
icculus@3008
   459
        print <<EOF;
icculus@3008
   460
        $incr;
icculus@3008
   461
        eps += dstsize;
icculus@3008
   462
        if ((eps << 1) >= srcsize) {
icculus@3008
   463
EOF
icculus@3008
   464
        for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   465
            my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "sample${i}");
icculus@3008
   466
            print <<EOF;
icculus@3008
   467
            dst[$i] = $val;
icculus@3008
   468
EOF
icculus@3008
   469
        }
icculus@3008
   470
icculus@3008
   471
        $incr = ($channels == 1) ? 'dst++' : "dst += $channels";
icculus@3008
   472
        print <<EOF;
icculus@3008
   473
            $incr;
icculus@3008
   474
EOF
icculus@3008
   475
    }
icculus@3008
   476
icculus@3008
   477
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   478
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   479
        my $swapped = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   480
        print <<EOF;
icculus@3008
   481
            sample${idx} = ($fctype) (((($bigger) $swapped) + (($bigger) last_sample${idx})) $interp);
icculus@3008
   482
EOF
icculus@3008
   483
    }
icculus@3008
   484
icculus@3008
   485
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   486
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   487
        print <<EOF;
icculus@3008
   488
            last_sample${idx} = sample${idx};
icculus@3008
   489
EOF
icculus@3008
   490
    }
icculus@3008
   491
icculus@3008
   492
    print <<EOF;
icculus@3008
   493
            eps -= $eps_adjust;
icculus@3008
   494
        }
icculus@3008
   495
    }
icculus@3008
   496
EOF
icculus@3008
   497
icculus@3008
   498
        print <<EOF;
icculus@3008
   499
    cvt->len_cvt = dstsize;
icculus@3008
   500
    if (cvt->filters[++cvt->filter_index]) {
icculus@3008
   501
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   502
    }
icculus@1982
   503
}
icculus@1982
   504
icculus@3008
   505
EOF
icculus@3008
   506
icculus@3008
   507
}
icculus@3008
   508
icculus@3008
   509
# These handle clean resamples...doubling and quadrupling the sample rate, etc.
icculus@3008
   510
sub buildMultipleResampleFunc {
icculus@3008
   511
    # !!! FIXME: we do a lot of unnecessary and ugly casting in here, due to getSwapFunc().
icculus@3008
   512
    my ($from, $channels, $upsample, $multiple) = @_;
icculus@3008
   513
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@3008
   514
icculus@3008
   515
    my $bigger = getBiggerCtype($ffloat, $fsize);
icculus@3008
   516
    my $interp = ($ffloat) ? '* 0.5' : '>> 1';
icculus@3008
   517
    my $interp2 = ($ffloat) ? '* 0.25' : '>> 2';
icculus@3008
   518
    my $mult3 = ($ffloat) ? '3.0' : '3';
icculus@3008
   519
    my $lencvtop = ($upsample) ? '*' : '/';
icculus@3008
   520
icculus@3008
   521
    my $resample = ($upsample) ? 'Upsample' : 'Downsample';
icculus@3008
   522
    my $hashid = getResamplerHashId($from, $channels, $upsample, $multiple);
icculus@3008
   523
    my $sym = "SDL_${resample}_${from}_${channels}c_x${multiple}";
icculus@3008
   524
    $funcs{$hashid} = $sym;
icculus@3008
   525
    $custom_converters++;
icculus@3008
   526
icculus@3008
   527
    # !!! FIXME: DEBUG_CONVERT should report frequencies.
icculus@3008
   528
    print <<EOF;
icculus@3008
   529
static void SDLCALL
icculus@3008
   530
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@1982
   531
{
slouken@3032
   532
#if DEBUG_CONVERT
icculus@3008
   533
    fprintf(stderr, "$resample (x${multiple}) AUDIO_${from}, ${channels} channels.\\n");
icculus@3008
   534
#endif
icculus@3008
   535
icculus@3008
   536
    const int srcsize = cvt->len_cvt;
icculus@3008
   537
    const int dstsize = cvt->len_cvt $lencvtop $multiple;
icculus@3008
   538
EOF
icculus@3008
   539
icculus@3008
   540
    # Upsampling (growing the buffer) needs to work backwards, since we
icculus@3008
   541
    #  overwrite the buffer as we go.
icculus@3008
   542
    if ($upsample) {
icculus@3008
   543
        print <<EOF;
icculus@3008
   544
    $fctype *dst = (($fctype *) (cvt->buf + dstsize)) - $channels;
icculus@3008
   545
    const $fctype *src = (($fctype *) (cvt->buf + cvt->len_cvt)) - $channels;
icculus@3008
   546
    const $fctype *target = ((const $fctype *) cvt->buf) - $channels;
icculus@3008
   547
EOF
icculus@3008
   548
    } else {
icculus@3008
   549
        print <<EOF;
icculus@3008
   550
    $fctype *dst = ($fctype *) cvt->buf;
icculus@3008
   551
    const $fctype *src = ($fctype *) cvt->buf;
icculus@3008
   552
    const $fctype *target = (const $fctype *) (cvt->buf + dstsize);
icculus@3008
   553
EOF
icculus@3008
   554
    }
icculus@3008
   555
icculus@3008
   556
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   557
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   558
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   559
        print <<EOF;
icculus@3008
   560
    $bigger last_sample${idx} = ($bigger) $val;
icculus@3008
   561
EOF
icculus@3008
   562
    }
icculus@3008
   563
icculus@3008
   564
    print <<EOF;
icculus@3008
   565
    while (dst != target) {
icculus@3008
   566
EOF
icculus@3008
   567
icculus@3008
   568
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   569
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   570
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   571
        print <<EOF;
icculus@3008
   572
        const $bigger sample${idx} = ($bigger) $val;
icculus@3008
   573
EOF
icculus@3008
   574
    }
icculus@3008
   575
icculus@3008
   576
    my $incr = '';
icculus@3008
   577
    if ($upsample) {
icculus@3008
   578
        $incr = ($channels == 1) ? 'src--' : "src -= $channels";
icculus@3008
   579
    } else {
icculus@3008
   580
        my $amount = $channels * $multiple;
icculus@3008
   581
        $incr = "src += $amount";  # can't ever be 1, so no "++" version.
icculus@3008
   582
    }
icculus@3008
   583
icculus@3008
   584
icculus@3008
   585
    print <<EOF;
icculus@3008
   586
        $incr;
icculus@1982
   587
EOF
icculus@1982
   588
icculus@3008
   589
    # !!! FIXME: This really begs for some Altivec or SSE, etc.
icculus@3008
   590
    if ($upsample) {
icculus@3008
   591
        if ($multiple == 2) {
icculus@3008
   592
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   593
                my $dsti = $i + $channels;
icculus@3008
   594
                print <<EOF;
icculus@3008
   595
        dst[$dsti] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   596
EOF
icculus@3008
   597
            }
icculus@3008
   598
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   599
                my $dsti = $i;
icculus@3008
   600
                print <<EOF;
icculus@3008
   601
        dst[$dsti] = ($fctype) sample${i};
icculus@3008
   602
EOF
icculus@3008
   603
            }
icculus@3008
   604
        } elsif ($multiple == 4) {
icculus@3008
   605
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   606
                my $dsti = $i + ($channels * 3);
icculus@3008
   607
                print <<EOF;
icculus@3008
   608
        dst[$dsti] = ($fctype) sample${i};
icculus@3008
   609
EOF
icculus@3008
   610
            }
icculus@3008
   611
icculus@3008
   612
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   613
                my $dsti = $i + ($channels * 2);
icculus@3008
   614
                print <<EOF;
icculus@3008
   615
        dst[$dsti] = ($fctype) ((($mult3 * sample${i}) + last_sample${i}) $interp2);
icculus@3008
   616
EOF
icculus@3008
   617
            }
icculus@3008
   618
icculus@3008
   619
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   620
                my $dsti = $i + ($channels * 1);
icculus@3008
   621
                print <<EOF;
icculus@3008
   622
        dst[$dsti] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   623
EOF
icculus@3008
   624
            }
icculus@3008
   625
icculus@3008
   626
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   627
                my $dsti = $i + ($channels * 0);
icculus@3008
   628
                print <<EOF;
icculus@3008
   629
        dst[$dsti] = ($fctype) ((sample${i} + ($mult3 * last_sample${i})) $interp2);
icculus@3008
   630
EOF
icculus@3008
   631
            }
icculus@3008
   632
        } else {
icculus@3008
   633
            die('bug in program.');  # we only handle x2 and x4.
icculus@1982
   634
        }
icculus@3008
   635
    } else {  # downsample.
icculus@3008
   636
        if ($multiple == 2) {
icculus@3008
   637
            for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   638
                print <<EOF;
icculus@3008
   639
        dst[$i] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   640
EOF
icculus@3008
   641
            }
icculus@3008
   642
        } elsif ($multiple == 4) {
icculus@3008
   643
            # !!! FIXME: interpolate all 4 samples?
icculus@3008
   644
            for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   645
                print <<EOF;
icculus@3008
   646
        dst[$i] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   647
EOF
icculus@3008
   648
            }
icculus@3008
   649
        } else {
icculus@3008
   650
            die('bug in program.');  # we only handle x2 and x4.
icculus@3008
   651
        }
icculus@3008
   652
    }
icculus@3008
   653
icculus@3008
   654
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   655
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   656
        print <<EOF;
icculus@3008
   657
        last_sample${idx} = sample${idx};
icculus@3008
   658
EOF
icculus@3008
   659
    }
icculus@3008
   660
icculus@3008
   661
    if ($upsample) {
icculus@3008
   662
        my $amount = $channels * $multiple;
icculus@3008
   663
        $incr = "dst -= $amount";  # can't ever be 1, so no "--" version.
icculus@3008
   664
    } else {
icculus@3008
   665
        $incr = ($channels == 1) ? 'dst++' : "dst += $channels";
icculus@3008
   666
    }
icculus@3008
   667
icculus@3008
   668
    print <<EOF;
icculus@3008
   669
        $incr;
icculus@3008
   670
    }
icculus@3008
   671
icculus@3008
   672
    cvt->len_cvt = dstsize;
icculus@3008
   673
    if (cvt->filters[++cvt->filter_index]) {
icculus@3008
   674
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   675
    }
icculus@1982
   676
}
icculus@1982
   677
icculus@3008
   678
EOF
icculus@3008
   679
icculus@3008
   680
}
icculus@3008
   681
icculus@3008
   682
sub buildResamplers {
icculus@3019
   683
    print "#if !NO_RESAMPLERS\n\n";
icculus@3019
   684
    foreach (@audiotypes) {
icculus@3019
   685
        my $from = $_;
icculus@3019
   686
        foreach (@channels) {
icculus@3019
   687
            my $channel = $_;
icculus@3019
   688
            buildArbitraryResampleFunc($from, $channel, 1);
icculus@3019
   689
            buildArbitraryResampleFunc($from, $channel, 0);
icculus@3019
   690
        }
icculus@3019
   691
    }
icculus@3019
   692
icculus@3019
   693
    print "\n#if !LESS_RESAMPLERS\n\n";
icculus@3008
   694
    foreach (@audiotypes) {
icculus@3008
   695
        my $from = $_;
icculus@3008
   696
        foreach (@channels) {
icculus@3008
   697
            my $channel = $_;
icculus@3008
   698
            for (my $multiple = 2; $multiple <= 4; $multiple += 2) {
icculus@3008
   699
                buildMultipleResampleFunc($from, $channel, 1, $multiple);
icculus@3008
   700
                buildMultipleResampleFunc($from, $channel, 0, $multiple);
icculus@3008
   701
            }
icculus@3008
   702
        }
icculus@3008
   703
    }
icculus@3008
   704
icculus@3019
   705
    print "#endif  /* !LESS_RESAMPLERS */\n";
icculus@3019
   706
    print "#endif  /* !NO_RESAMPLERS */\n\n\n";
icculus@3019
   707
icculus@3008
   708
    print "const SDL_AudioRateFilters sdl_audio_rate_filters[] =\n{\n";
icculus@3019
   709
    print "#if !NO_RESAMPLERS\n";
icculus@3008
   710
    foreach (@audiotypes) {
icculus@3008
   711
        my $from = $_;
icculus@3008
   712
        foreach (@channels) {
icculus@3008
   713
            my $channel = $_;
icculus@3019
   714
            for (my $upsample = 0; $upsample <= 1; $upsample++) {
icculus@3019
   715
                my $hashid = getResamplerHashId($from, $channel, $upsample, 0);
icculus@3019
   716
                my $sym = $funcs{$hashid};
icculus@3019
   717
                print("    { AUDIO_$from, $channel, $upsample, 0, $sym },\n");
icculus@3019
   718
            }
icculus@3019
   719
        }
icculus@3019
   720
    }
icculus@3019
   721
icculus@3019
   722
    print "#if !LESS_RESAMPLERS\n";
icculus@3019
   723
    foreach (@audiotypes) {
icculus@3019
   724
        my $from = $_;
icculus@3019
   725
        foreach (@channels) {
icculus@3019
   726
            my $channel = $_;
icculus@3019
   727
            for (my $multiple = 2; $multiple <= 4; $multiple += 2) {
icculus@3008
   728
                for (my $upsample = 0; $upsample <= 1; $upsample++) {
icculus@3008
   729
                    my $hashid = getResamplerHashId($from, $channel, $upsample, $multiple);
icculus@3008
   730
                    my $sym = $funcs{$hashid};
icculus@3008
   731
                    print("    { AUDIO_$from, $channel, $upsample, $multiple, $sym },\n");
icculus@3008
   732
                }
icculus@3008
   733
            }
icculus@3008
   734
        }
icculus@3008
   735
    }
icculus@3008
   736
icculus@3019
   737
    print "#endif  /* !LESS_RESAMPLERS */\n";
icculus@3019
   738
    print "#endif  /* !NO_RESAMPLERS */\n";
icculus@3020
   739
    print("    { 0, 0, 0, 0, NULL }\n");
icculus@3008
   740
    print "};\n\n";
icculus@3008
   741
}
icculus@1982
   742
icculus@1982
   743
icculus@3008
   744
# mainline ...
icculus@1982
   745
icculus@3008
   746
outputHeader();
icculus@3008
   747
buildTypeConverters();
icculus@3008
   748
buildResamplers();
slouken@1985
   749
outputFooter();
slouken@1985
   750
icculus@1982
   751
exit 0;
icculus@1982
   752
slouken@1985
   753
# end of sdlgenaudiocvt.pl ...
icculus@1982
   754