src/audio/sdlgenaudiocvt.pl
author Ryan C. Gordon
Fri, 09 Jan 2009 15:41:45 +0000
changeset 3008 786a48f8309c
parent 2956 1210d5a28e16
child 3019 dfd23eb79be9
permissions -rwxr-xr-x
First shot at autogenerated audio resamplers.

Don't check in a new SDL_audiotypecvt.c yet, though.
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
slouken@1985
    65
/* *INDENT-OFF* */
icculus@1982
    66
icculus@1982
    67
EOF
icculus@1982
    68
icculus@2011
    69
    my @vals = ( 127, 32767, 2147483647 );
icculus@1982
    70
    foreach (@vals) {
icculus@1982
    71
        my $val = $_;
icculus@1982
    72
        my $fval = 1.0 / $val;
icculus@1982
    73
        print("#define DIVBY${val} ${fval}f\n");
icculus@1982
    74
    }
icculus@1982
    75
icculus@1982
    76
    print("\n");
icculus@1982
    77
}
icculus@1982
    78
slouken@1985
    79
sub outputFooter {
slouken@1985
    80
    print <<EOF;
icculus@3008
    81
/* $custom_converters converters generated. */
icculus@3008
    82
slouken@1985
    83
/* *INDENT-ON* */
slouken@1985
    84
slouken@1985
    85
/* vi: set ts=4 sw=4 expandtab: */
slouken@1985
    86
EOF
slouken@1985
    87
}
icculus@1982
    88
icculus@1982
    89
sub splittype {
icculus@1982
    90
    my $t = shift;
icculus@1982
    91
    my ($signed, $size, $endian) = $t =~ /([USF])(\d+)([LM]SB|)/;
icculus@1982
    92
    my $float = ($signed eq 'F') ? 1 : 0;
icculus@1982
    93
    $signed = (($float) or ($signed eq 'S')) ? 1 : 0;
icculus@1982
    94
    $endian = 'NONE' if ($endian eq '');
icculus@1982
    95
icculus@1982
    96
    my $ctype = '';
icculus@1982
    97
    if ($float) {
icculus@1982
    98
        $ctype = (($size == 32) ? 'float' : 'double');
icculus@1982
    99
    } else {
icculus@1982
   100
        $ctype = (($signed) ? 'S' : 'U') . "int${size}";
icculus@1982
   101
    }
icculus@1982
   102
icculus@1982
   103
    return ($signed, $float, $size, $endian, $ctype);
icculus@1982
   104
}
icculus@1982
   105
icculus@1982
   106
sub getSwapFunc {
icculus@1982
   107
    my ($size, $signed, $float, $endian, $val) = @_;
icculus@1982
   108
    my $BEorLE = (($endian eq 'MSB') ? 'BE' : 'LE');
icculus@1982
   109
    my $code = '';
icculus@1982
   110
icculus@1982
   111
    if ($float) {
icculus@1982
   112
        $code = "SDL_SwapFloat${BEorLE}($val)";
icculus@1982
   113
    } else {
icculus@1982
   114
        if ($size > 8) {
icculus@1982
   115
            $code = "SDL_Swap${BEorLE}${size}($val)";
icculus@1982
   116
        } else {
icculus@1982
   117
            $code = $val;
icculus@1982
   118
        }
icculus@1982
   119
icculus@1982
   120
        if (($signed) and (!$float)) {
icculus@1982
   121
            $code = "((Sint${size}) $code)";
icculus@1982
   122
        }
icculus@1982
   123
    }
icculus@1982
   124
icculus@1982
   125
    return "${code}";
icculus@1982
   126
}
icculus@1982
   127
icculus@1982
   128
icculus@1982
   129
sub maxIntVal {
icculus@2011
   130
    my $size = shift;
icculus@2011
   131
    if ($size == 8) {
icculus@2011
   132
        return 0x7F;
icculus@2011
   133
    } elsif ($size == 16) {
icculus@2011
   134
        return 0x7FFF;
icculus@2011
   135
    } elsif ($size == 32) {
icculus@2011
   136
        return 0x7FFFFFFF;
icculus@1982
   137
    }
icculus@1982
   138
icculus@1982
   139
    die("bug in script.\n");
icculus@1982
   140
}
icculus@1982
   141
icculus@1982
   142
sub getFloatToIntMult {
icculus@2011
   143
    my $size = shift;
icculus@2011
   144
    my $val = maxIntVal($size) . '.0';
icculus@1982
   145
    $val .= 'f' if ($size < 32);
icculus@1982
   146
    return $val;
icculus@1982
   147
}
icculus@1982
   148
icculus@1982
   149
sub getIntToFloatDivBy {
icculus@2011
   150
    my $size = shift;
icculus@2011
   151
    return 'DIVBY' . maxIntVal($size);
icculus@1982
   152
}
icculus@1982
   153
icculus@1982
   154
sub getSignFlipVal {
icculus@1982
   155
    my $size = shift;
icculus@1982
   156
    if ($size == 8) {
icculus@1982
   157
        return '0x80';
icculus@1982
   158
    } elsif ($size == 16) {
icculus@1982
   159
        return '0x8000';
icculus@1982
   160
    } elsif ($size == 32) {
icculus@1982
   161
        return '0x80000000';
icculus@1982
   162
    }
icculus@1982
   163
icculus@1982
   164
    die("bug in script.\n");
icculus@1982
   165
}
icculus@1982
   166
icculus@1982
   167
sub buildCvtFunc {
icculus@1982
   168
    my ($from, $to) = @_;
icculus@1982
   169
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@1982
   170
    my ($tsigned, $tfloat, $tsize, $tendian, $tctype) = splittype($to);
icculus@1982
   171
    my $diffs = 0;
icculus@1982
   172
    $diffs++ if ($fsize != $tsize);
icculus@1982
   173
    $diffs++ if ($fsigned != $tsigned);
icculus@1982
   174
    $diffs++ if ($ffloat != $tfloat);
icculus@1982
   175
    $diffs++ if ($fendian ne $tendian);
icculus@1982
   176
icculus@1982
   177
    return if ($diffs == 0);
icculus@1982
   178
icculus@3008
   179
    my $hashid = getTypeConvertHashId($from, $to);
icculus@1982
   180
    if (1) { # !!! FIXME: if ($diffs > 1) {
icculus@1982
   181
        my $sym = "SDL_Convert_${from}_to_${to}";
icculus@1982
   182
        $funcs{$hashid} = $sym;
icculus@1982
   183
        $custom_converters++;
icculus@1982
   184
icculus@1982
   185
        # Always unsigned for ints, for possible byteswaps.
icculus@1982
   186
        my $srctype = (($ffloat) ? 'float' : "Uint${fsize}");
icculus@1982
   187
icculus@1982
   188
        print <<EOF;
icculus@1982
   189
static void SDLCALL
icculus@1982
   190
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@1982
   191
{
icculus@1982
   192
    int i;
icculus@1982
   193
    const $srctype *src;
icculus@1982
   194
    $tctype *dst;
icculus@1982
   195
icculus@1982
   196
#ifdef DEBUG_CONVERT
icculus@1982
   197
    fprintf(stderr, "Converting AUDIO_${from} to AUDIO_${to}.\\n");
icculus@1982
   198
#endif
icculus@1982
   199
icculus@1982
   200
EOF
icculus@1982
   201
icculus@1982
   202
        if ($fsize < $tsize) {
icculus@1982
   203
            my $mult = $tsize / $fsize;
icculus@1982
   204
            print <<EOF;
icculus@2956
   205
    src = ((const $srctype *) (cvt->buf + cvt->len_cvt)) - 1;
icculus@2956
   206
    dst = (($tctype *) (cvt->buf + cvt->len_cvt * $mult)) - 1;
icculus@1982
   207
    for (i = cvt->len_cvt / sizeof ($srctype); i; --i, --src, --dst) {
icculus@1982
   208
EOF
icculus@1982
   209
        } else {
icculus@1982
   210
            print <<EOF;
icculus@1982
   211
    src = (const $srctype *) cvt->buf;
icculus@1982
   212
    dst = ($tctype *) cvt->buf;
icculus@1982
   213
    for (i = cvt->len_cvt / sizeof ($srctype); i; --i, ++src, ++dst) {
icculus@1982
   214
EOF
icculus@1982
   215
        }
icculus@1982
   216
icculus@1982
   217
        # Have to convert to/from float/int.
icculus@1982
   218
        # !!! FIXME: cast through double for int32<->float?
icculus@1982
   219
        my $code = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, '*src');
icculus@1982
   220
        if ($ffloat != $tfloat) {
icculus@1982
   221
            if ($ffloat) {
icculus@2011
   222
                my $mult = getFloatToIntMult($tsize);
icculus@2011
   223
                if (!$tsigned) {   # bump from -1.0f/1.0f to 0.0f/2.0f
icculus@2011
   224
                    $code = "($code + 1.0f)";
icculus@2011
   225
                }
icculus@1982
   226
                $code = "(($tctype) ($code * $mult))";
icculus@1982
   227
            } else {
icculus@1982
   228
                # $divby will be the reciprocal, to avoid pipeline stalls
icculus@1982
   229
                #  from floating point division...so multiply it.
icculus@2011
   230
                my $divby = getIntToFloatDivBy($fsize);
icculus@1982
   231
                $code = "(((float) $code) * $divby)";
icculus@2011
   232
                if (!$fsigned) {   # bump from 0.0f/2.0f to -1.0f/1.0f.
icculus@2011
   233
                    $code = "($code - 1.0f)";
icculus@2011
   234
                }
icculus@1982
   235
            }
icculus@1982
   236
        } else {
icculus@1982
   237
            # All integer conversions here.
icculus@1982
   238
            if ($fsigned != $tsigned) {
icculus@1982
   239
                my $signflipval = getSignFlipVal($fsize);
icculus@1982
   240
                $code = "(($code) ^ $signflipval)";
icculus@1982
   241
            }
icculus@1982
   242
icculus@1982
   243
            my $shiftval = abs($fsize - $tsize);
icculus@1982
   244
            if ($fsize < $tsize) {
icculus@1982
   245
                $code = "((($tctype) $code) << $shiftval)";
icculus@1982
   246
            } elsif ($fsize > $tsize) {
icculus@1982
   247
                $code = "(($tctype) ($code >> $shiftval))";
icculus@1982
   248
            }
icculus@1982
   249
        }
icculus@1982
   250
icculus@1982
   251
        my $swap = getSwapFunc($tsize, $tsigned, $tfloat, $tendian, 'val');
icculus@1982
   252
icculus@1982
   253
        print <<EOF;
icculus@1982
   254
        const $tctype val = $code;
icculus@1982
   255
        *dst = ${swap};
icculus@1982
   256
    }
icculus@1982
   257
icculus@1982
   258
EOF
icculus@1982
   259
icculus@1982
   260
        if ($fsize > $tsize) {
icculus@1982
   261
            my $divby = $fsize / $tsize;
icculus@1982
   262
            print("    cvt->len_cvt /= $divby;\n");
icculus@1982
   263
        } elsif ($fsize < $tsize) {
icculus@1982
   264
            my $mult = $tsize / $fsize;
icculus@1982
   265
            print("    cvt->len_cvt *= $mult;\n");
icculus@1982
   266
        }
icculus@1982
   267
icculus@1982
   268
        print <<EOF;
icculus@1982
   269
    if (cvt->filters[++cvt->filter_index]) {
icculus@2955
   270
        cvt->filters[cvt->filter_index] (cvt, AUDIO_$to);
icculus@1982
   271
    }
icculus@1982
   272
}
icculus@1982
   273
icculus@1982
   274
EOF
icculus@1982
   275
icculus@1982
   276
    } else {
icculus@1982
   277
        if ($fsigned != $tsigned) {
icculus@1982
   278
            $funcs{$hashid} = 'SDL_ConvertSigned';
icculus@1982
   279
        } elsif ($ffloat != $tfloat) {
icculus@1982
   280
            $funcs{$hashid} = 'SDL_ConvertFloat';
icculus@1982
   281
        } elsif ($fsize != $tsize) {
icculus@1982
   282
            $funcs{$hashid} = 'SDL_ConvertSize';
icculus@1982
   283
        } elsif ($fendian ne $tendian) {
icculus@1982
   284
            $funcs{$hashid} = 'SDL_ConvertEndian';
icculus@1982
   285
        } else {
icculus@1982
   286
            die("error in script.\n");
icculus@1982
   287
        }
icculus@1982
   288
    }
icculus@1982
   289
}
icculus@1982
   290
icculus@3008
   291
icculus@3008
   292
sub buildTypeConverters {
icculus@3008
   293
    foreach (@audiotypes) {
icculus@3008
   294
        my $from = $_;
icculus@3008
   295
        foreach (@audiotypes) {
icculus@3008
   296
            my $to = $_;
icculus@3008
   297
            buildCvtFunc($from, $to);
icculus@3008
   298
        }
icculus@3008
   299
    }
icculus@3008
   300
icculus@3008
   301
    print "const SDL_AudioTypeFilters sdl_audio_type_filters[] =\n{\n";
icculus@3008
   302
    foreach (@audiotypes) {
icculus@3008
   303
        my $from = $_;
icculus@3008
   304
        foreach (@audiotypes) {
icculus@3008
   305
            my $to = $_;
icculus@3008
   306
            if ($from ne $to) {
icculus@3008
   307
                my $hashid = getTypeConvertHashId($from, $to);
icculus@3008
   308
                my $sym = $funcs{$hashid};
icculus@3008
   309
                print("    { AUDIO_$from, AUDIO_$to, $sym },\n");
icculus@3008
   310
            }
icculus@3008
   311
        }
icculus@3008
   312
    }
icculus@3008
   313
icculus@3008
   314
    print "};\n\n\n";
icculus@3008
   315
}
icculus@3008
   316
icculus@3008
   317
sub getBiggerCtype {
icculus@3008
   318
    my ($isfloat, $size) = @_;
icculus@3008
   319
icculus@3008
   320
    if ($isfloat) {
icculus@3008
   321
        if ($size == 32) {
icculus@3008
   322
            return 'double';
icculus@3008
   323
        }
icculus@3008
   324
        die("bug in script.\n");
icculus@3008
   325
    }
icculus@3008
   326
icculus@3008
   327
    if ($size == 8) {
icculus@3008
   328
        return 'Sint16';
icculus@3008
   329
    } elsif ($size == 16) {
icculus@3008
   330
        return 'Sint32'
icculus@3008
   331
    } elsif ($size == 32) {
icculus@3008
   332
        return 'Sint64'
icculus@3008
   333
    }
icculus@3008
   334
icculus@3008
   335
    die("bug in script.\n");
icculus@3008
   336
}
icculus@3008
   337
icculus@3008
   338
icculus@3008
   339
# These handle arbitrary resamples...44100Hz to 48000Hz, for example.
icculus@3008
   340
# Man, this code is skanky.
icculus@3008
   341
sub buildArbitraryResampleFunc {
icculus@3008
   342
    # !!! FIXME: we do a lot of unnecessary and ugly casting in here, due to getSwapFunc().
icculus@3008
   343
    my ($from, $channels, $upsample) = @_;
icculus@3008
   344
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@3008
   345
icculus@3008
   346
    my $bigger = getBiggerCtype($ffloat, $fsize);
icculus@3008
   347
    my $interp = ($ffloat) ? '* 0.5' : '>> 1';
icculus@3008
   348
icculus@3008
   349
    my $resample = ($upsample) ? 'Upsample' : 'Downsample';
icculus@3008
   350
    my $hashid = getResamplerHashId($from, $channels, $upsample, 0);
icculus@3008
   351
    my $sym = "SDL_${resample}_${from}_${channels}c";
icculus@3008
   352
    $funcs{$hashid} = $sym;
icculus@3008
   353
    $custom_converters++;
icculus@3008
   354
icculus@3008
   355
    my $fudge = $fsize * $channels * 2;  # !!! FIXME
icculus@3008
   356
    my $eps_adjust = ($upsample) ? 'dstsize' : 'srcsize';
icculus@3008
   357
    my $incr = '';
icculus@3008
   358
    my $incr2 = '';
icculus@3008
   359
icculus@3008
   360
icculus@3008
   361
    # !!! FIXME: DEBUG_CONVERT should report frequencies.
icculus@3008
   362
    print <<EOF;
icculus@3008
   363
static void SDLCALL
icculus@3008
   364
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@3008
   365
{
icculus@3008
   366
#ifdef DEBUG_CONVERT
icculus@3008
   367
    fprintf(stderr, "$resample arbitrary (x%f) AUDIO_${from}, ${channels} channels.\\n", cvt->rate_incr);
icculus@3008
   368
#endif
icculus@3008
   369
icculus@3008
   370
    const int srcsize = cvt->len_cvt - $fudge;
icculus@3008
   371
    const int dstsize = (int) (((double)cvt->len_cvt) * cvt->rate_incr);
icculus@3008
   372
    register int eps = 0;
icculus@3008
   373
EOF
icculus@1982
   374
icculus@3008
   375
    # Upsampling (growing the buffer) needs to work backwards, since we
icculus@3008
   376
    #  overwrite the buffer as we go.
icculus@3008
   377
    if ($upsample) {
icculus@3008
   378
        print <<EOF;
icculus@3008
   379
    $fctype *dst = (($fctype *) (cvt->buf + dstsize)) - $channels;
icculus@3008
   380
    const $fctype *src = (($fctype *) (cvt->buf + cvt->len_cvt)) - $channels;
icculus@3008
   381
    const $fctype *target = ((const $fctype *) cvt->buf) - $channels;
icculus@3008
   382
EOF
icculus@3008
   383
    } else {
icculus@3008
   384
        print <<EOF;
icculus@3008
   385
    $fctype *dst = ($fctype *) cvt->buf;
icculus@3008
   386
    const $fctype *src = ($fctype *) cvt->buf;
icculus@3008
   387
    const $fctype *target = (const $fctype *) (cvt->buf + dstsize);
icculus@3008
   388
EOF
icculus@3008
   389
    }
icculus@3008
   390
icculus@3008
   391
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   392
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   393
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   394
        print <<EOF;
icculus@3008
   395
    $fctype sample${idx} = $val;
icculus@3008
   396
EOF
icculus@3008
   397
    }
icculus@3008
   398
icculus@3008
   399
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   400
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   401
        print <<EOF;
icculus@3008
   402
    $fctype last_sample${idx} = sample${idx};
icculus@3008
   403
EOF
icculus@3008
   404
    }
icculus@3008
   405
icculus@3008
   406
    print <<EOF;
icculus@3008
   407
    while (dst != target) {
icculus@3008
   408
EOF
icculus@3008
   409
icculus@3008
   410
    if ($upsample) {
icculus@3008
   411
        for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   412
            # !!! FIXME: don't do this swap every write, just when the samples change.
icculus@3008
   413
            my $idx = (($channels - $i) - 1);
icculus@3008
   414
            my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "sample${idx}");
icculus@3008
   415
            print <<EOF;
icculus@3008
   416
        dst[$idx] = $val;
icculus@3008
   417
EOF
icculus@3008
   418
        }
icculus@3008
   419
icculus@3008
   420
        $incr = ($channels == 1) ? 'dst--' : "dst -= $channels";
icculus@3008
   421
        $incr2 = ($channels == 1) ? 'src--' : "src -= $channels";
icculus@3008
   422
icculus@3008
   423
        print <<EOF;
icculus@3008
   424
        $incr;
icculus@3008
   425
        eps += srcsize;
icculus@3008
   426
        if ((eps << 1) >= dstsize) {
icculus@3008
   427
            $incr2;
icculus@3008
   428
EOF
icculus@3008
   429
    } else {  # downsample.
icculus@3008
   430
        $incr = ($channels == 1) ? 'src++' : "src += $channels";
icculus@3008
   431
        print <<EOF;
icculus@3008
   432
        $incr;
icculus@3008
   433
        eps += dstsize;
icculus@3008
   434
        if ((eps << 1) >= srcsize) {
icculus@3008
   435
EOF
icculus@3008
   436
        for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   437
            my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "sample${i}");
icculus@3008
   438
            print <<EOF;
icculus@3008
   439
            dst[$i] = $val;
icculus@3008
   440
EOF
icculus@3008
   441
        }
icculus@3008
   442
icculus@3008
   443
        $incr = ($channels == 1) ? 'dst++' : "dst += $channels";
icculus@3008
   444
        print <<EOF;
icculus@3008
   445
            $incr;
icculus@3008
   446
EOF
icculus@3008
   447
    }
icculus@3008
   448
icculus@3008
   449
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   450
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   451
        my $swapped = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   452
        print <<EOF;
icculus@3008
   453
            sample${idx} = ($fctype) (((($bigger) $swapped) + (($bigger) last_sample${idx})) $interp);
icculus@3008
   454
EOF
icculus@3008
   455
    }
icculus@3008
   456
icculus@3008
   457
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   458
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   459
        print <<EOF;
icculus@3008
   460
            last_sample${idx} = sample${idx};
icculus@3008
   461
EOF
icculus@3008
   462
    }
icculus@3008
   463
icculus@3008
   464
    print <<EOF;
icculus@3008
   465
            eps -= $eps_adjust;
icculus@3008
   466
        }
icculus@3008
   467
    }
icculus@3008
   468
EOF
icculus@3008
   469
icculus@3008
   470
        print <<EOF;
icculus@3008
   471
    cvt->len_cvt = dstsize;
icculus@3008
   472
    if (cvt->filters[++cvt->filter_index]) {
icculus@3008
   473
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   474
    }
icculus@1982
   475
}
icculus@1982
   476
icculus@3008
   477
EOF
icculus@3008
   478
icculus@3008
   479
}
icculus@3008
   480
icculus@3008
   481
# These handle clean resamples...doubling and quadrupling the sample rate, etc.
icculus@3008
   482
sub buildMultipleResampleFunc {
icculus@3008
   483
    # !!! FIXME: we do a lot of unnecessary and ugly casting in here, due to getSwapFunc().
icculus@3008
   484
    my ($from, $channels, $upsample, $multiple) = @_;
icculus@3008
   485
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@3008
   486
icculus@3008
   487
    my $bigger = getBiggerCtype($ffloat, $fsize);
icculus@3008
   488
    my $interp = ($ffloat) ? '* 0.5' : '>> 1';
icculus@3008
   489
    my $interp2 = ($ffloat) ? '* 0.25' : '>> 2';
icculus@3008
   490
    my $mult3 = ($ffloat) ? '3.0' : '3';
icculus@3008
   491
    my $lencvtop = ($upsample) ? '*' : '/';
icculus@3008
   492
icculus@3008
   493
    my $resample = ($upsample) ? 'Upsample' : 'Downsample';
icculus@3008
   494
    my $hashid = getResamplerHashId($from, $channels, $upsample, $multiple);
icculus@3008
   495
    my $sym = "SDL_${resample}_${from}_${channels}c_x${multiple}";
icculus@3008
   496
    $funcs{$hashid} = $sym;
icculus@3008
   497
    $custom_converters++;
icculus@3008
   498
icculus@3008
   499
    # !!! FIXME: DEBUG_CONVERT should report frequencies.
icculus@3008
   500
    print <<EOF;
icculus@3008
   501
static void SDLCALL
icculus@3008
   502
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@1982
   503
{
icculus@3008
   504
#ifdef DEBUG_CONVERT
icculus@3008
   505
    fprintf(stderr, "$resample (x${multiple}) AUDIO_${from}, ${channels} channels.\\n");
icculus@3008
   506
#endif
icculus@3008
   507
icculus@3008
   508
    const int srcsize = cvt->len_cvt;
icculus@3008
   509
    const int dstsize = cvt->len_cvt $lencvtop $multiple;
icculus@3008
   510
EOF
icculus@3008
   511
icculus@3008
   512
    # Upsampling (growing the buffer) needs to work backwards, since we
icculus@3008
   513
    #  overwrite the buffer as we go.
icculus@3008
   514
    if ($upsample) {
icculus@3008
   515
        print <<EOF;
icculus@3008
   516
    $fctype *dst = (($fctype *) (cvt->buf + dstsize)) - $channels;
icculus@3008
   517
    const $fctype *src = (($fctype *) (cvt->buf + cvt->len_cvt)) - $channels;
icculus@3008
   518
    const $fctype *target = ((const $fctype *) cvt->buf) - $channels;
icculus@3008
   519
EOF
icculus@3008
   520
    } else {
icculus@3008
   521
        print <<EOF;
icculus@3008
   522
    $fctype *dst = ($fctype *) cvt->buf;
icculus@3008
   523
    const $fctype *src = ($fctype *) cvt->buf;
icculus@3008
   524
    const $fctype *target = (const $fctype *) (cvt->buf + dstsize);
icculus@3008
   525
EOF
icculus@3008
   526
    }
icculus@3008
   527
icculus@3008
   528
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   529
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   530
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   531
        print <<EOF;
icculus@3008
   532
    $bigger last_sample${idx} = ($bigger) $val;
icculus@3008
   533
EOF
icculus@3008
   534
    }
icculus@3008
   535
icculus@3008
   536
    print <<EOF;
icculus@3008
   537
    while (dst != target) {
icculus@3008
   538
EOF
icculus@3008
   539
icculus@3008
   540
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   541
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   542
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   543
        print <<EOF;
icculus@3008
   544
        const $bigger sample${idx} = ($bigger) $val;
icculus@3008
   545
EOF
icculus@3008
   546
    }
icculus@3008
   547
icculus@3008
   548
    my $incr = '';
icculus@3008
   549
    if ($upsample) {
icculus@3008
   550
        $incr = ($channels == 1) ? 'src--' : "src -= $channels";
icculus@3008
   551
    } else {
icculus@3008
   552
        my $amount = $channels * $multiple;
icculus@3008
   553
        $incr = "src += $amount";  # can't ever be 1, so no "++" version.
icculus@3008
   554
    }
icculus@3008
   555
icculus@3008
   556
icculus@3008
   557
    print <<EOF;
icculus@3008
   558
        $incr;
icculus@1982
   559
EOF
icculus@1982
   560
icculus@3008
   561
    # !!! FIXME: This really begs for some Altivec or SSE, etc.
icculus@3008
   562
    if ($upsample) {
icculus@3008
   563
        if ($multiple == 2) {
icculus@3008
   564
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   565
                my $dsti = $i + $channels;
icculus@3008
   566
                print <<EOF;
icculus@3008
   567
        dst[$dsti] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   568
EOF
icculus@3008
   569
            }
icculus@3008
   570
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   571
                my $dsti = $i;
icculus@3008
   572
                print <<EOF;
icculus@3008
   573
        dst[$dsti] = ($fctype) sample${i};
icculus@3008
   574
EOF
icculus@3008
   575
            }
icculus@3008
   576
        } elsif ($multiple == 4) {
icculus@3008
   577
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   578
                my $dsti = $i + ($channels * 3);
icculus@3008
   579
                print <<EOF;
icculus@3008
   580
        dst[$dsti] = ($fctype) sample${i};
icculus@3008
   581
EOF
icculus@3008
   582
            }
icculus@3008
   583
icculus@3008
   584
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   585
                my $dsti = $i + ($channels * 2);
icculus@3008
   586
                print <<EOF;
icculus@3008
   587
        dst[$dsti] = ($fctype) ((($mult3 * sample${i}) + last_sample${i}) $interp2);
icculus@3008
   588
EOF
icculus@3008
   589
            }
icculus@3008
   590
icculus@3008
   591
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   592
                my $dsti = $i + ($channels * 1);
icculus@3008
   593
                print <<EOF;
icculus@3008
   594
        dst[$dsti] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   595
EOF
icculus@3008
   596
            }
icculus@3008
   597
icculus@3008
   598
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   599
                my $dsti = $i + ($channels * 0);
icculus@3008
   600
                print <<EOF;
icculus@3008
   601
        dst[$dsti] = ($fctype) ((sample${i} + ($mult3 * last_sample${i})) $interp2);
icculus@3008
   602
EOF
icculus@3008
   603
            }
icculus@3008
   604
        } else {
icculus@3008
   605
            die('bug in program.');  # we only handle x2 and x4.
icculus@1982
   606
        }
icculus@3008
   607
    } else {  # downsample.
icculus@3008
   608
        if ($multiple == 2) {
icculus@3008
   609
            for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   610
                print <<EOF;
icculus@3008
   611
        dst[$i] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   612
EOF
icculus@3008
   613
            }
icculus@3008
   614
        } elsif ($multiple == 4) {
icculus@3008
   615
            # !!! FIXME: interpolate all 4 samples?
icculus@3008
   616
            for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   617
                print <<EOF;
icculus@3008
   618
        dst[$i] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   619
EOF
icculus@3008
   620
            }
icculus@3008
   621
        } else {
icculus@3008
   622
            die('bug in program.');  # we only handle x2 and x4.
icculus@3008
   623
        }
icculus@3008
   624
    }
icculus@3008
   625
icculus@3008
   626
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   627
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   628
        print <<EOF;
icculus@3008
   629
        last_sample${idx} = sample${idx};
icculus@3008
   630
EOF
icculus@3008
   631
    }
icculus@3008
   632
icculus@3008
   633
    if ($upsample) {
icculus@3008
   634
        my $amount = $channels * $multiple;
icculus@3008
   635
        $incr = "dst -= $amount";  # can't ever be 1, so no "--" version.
icculus@3008
   636
    } else {
icculus@3008
   637
        $incr = ($channels == 1) ? 'dst++' : "dst += $channels";
icculus@3008
   638
    }
icculus@3008
   639
icculus@3008
   640
    print <<EOF;
icculus@3008
   641
        $incr;
icculus@3008
   642
    }
icculus@3008
   643
icculus@3008
   644
    cvt->len_cvt = dstsize;
icculus@3008
   645
    if (cvt->filters[++cvt->filter_index]) {
icculus@3008
   646
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   647
    }
icculus@1982
   648
}
icculus@1982
   649
icculus@3008
   650
EOF
icculus@3008
   651
icculus@3008
   652
}
icculus@3008
   653
icculus@3008
   654
sub buildResamplers {
icculus@3008
   655
    foreach (@audiotypes) {
icculus@3008
   656
        my $from = $_;
icculus@3008
   657
        foreach (@channels) {
icculus@3008
   658
            my $channel = $_;
icculus@3008
   659
            for (my $multiple = 2; $multiple <= 4; $multiple += 2) {
icculus@3008
   660
                buildMultipleResampleFunc($from, $channel, 1, $multiple);
icculus@3008
   661
                buildMultipleResampleFunc($from, $channel, 0, $multiple);
icculus@3008
   662
            }
icculus@3008
   663
            buildArbitraryResampleFunc($from, $channel, 1);
icculus@3008
   664
            buildArbitraryResampleFunc($from, $channel, 0);
icculus@3008
   665
        }
icculus@3008
   666
    }
icculus@3008
   667
icculus@3008
   668
    print "const SDL_AudioRateFilters sdl_audio_rate_filters[] =\n{\n";
icculus@3008
   669
    foreach (@audiotypes) {
icculus@3008
   670
        my $from = $_;
icculus@3008
   671
        foreach (@channels) {
icculus@3008
   672
            my $channel = $_;
icculus@3008
   673
            for (my $multiple = 0; $multiple <= 4; $multiple += 2) {
icculus@3008
   674
                for (my $upsample = 0; $upsample <= 1; $upsample++) {
icculus@3008
   675
                    my $hashid = getResamplerHashId($from, $channel, $upsample, $multiple);
icculus@3008
   676
                    my $sym = $funcs{$hashid};
icculus@3008
   677
                    print("    { AUDIO_$from, $channel, $upsample, $multiple, $sym },\n");
icculus@3008
   678
                }
icculus@3008
   679
            }
icculus@3008
   680
        }
icculus@3008
   681
    }
icculus@3008
   682
icculus@3008
   683
    print "};\n\n";
icculus@3008
   684
}
icculus@1982
   685
icculus@1982
   686
icculus@3008
   687
# mainline ...
icculus@1982
   688
icculus@3008
   689
outputHeader();
icculus@3008
   690
buildTypeConverters();
icculus@3008
   691
buildResamplers();
slouken@1985
   692
outputFooter();
slouken@1985
   693
icculus@1982
   694
exit 0;
icculus@1982
   695
slouken@1985
   696
# end of sdlgenaudiocvt.pl ...
icculus@1982
   697