src/audio/sdlgenaudiocvt.pl
author Ryan C. Gordon
Sun, 11 Jan 2009 04:39:09 +0000
changeset 3019 dfd23eb79be9
parent 3008 786a48f8309c
child 3020 70d876a0b90e
permissions -rwxr-xr-x
Allow builds that reduce or eliminate the converters/resamplers.

We should probably give options to drop resamplers by channels, too, for
developers that know they'll never need more than stereo, etc.
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
icculus@1982
   219
#ifdef 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@3008
   341
    print "};\n\n\n";
icculus@3008
   342
}
icculus@3008
   343
icculus@3008
   344
sub getBiggerCtype {
icculus@3008
   345
    my ($isfloat, $size) = @_;
icculus@3008
   346
icculus@3008
   347
    if ($isfloat) {
icculus@3008
   348
        if ($size == 32) {
icculus@3008
   349
            return 'double';
icculus@3008
   350
        }
icculus@3008
   351
        die("bug in script.\n");
icculus@3008
   352
    }
icculus@3008
   353
icculus@3008
   354
    if ($size == 8) {
icculus@3008
   355
        return 'Sint16';
icculus@3008
   356
    } elsif ($size == 16) {
icculus@3008
   357
        return 'Sint32'
icculus@3008
   358
    } elsif ($size == 32) {
icculus@3008
   359
        return 'Sint64'
icculus@3008
   360
    }
icculus@3008
   361
icculus@3008
   362
    die("bug in script.\n");
icculus@3008
   363
}
icculus@3008
   364
icculus@3008
   365
icculus@3008
   366
# These handle arbitrary resamples...44100Hz to 48000Hz, for example.
icculus@3008
   367
# Man, this code is skanky.
icculus@3008
   368
sub buildArbitraryResampleFunc {
icculus@3008
   369
    # !!! FIXME: we do a lot of unnecessary and ugly casting in here, due to getSwapFunc().
icculus@3008
   370
    my ($from, $channels, $upsample) = @_;
icculus@3008
   371
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@3008
   372
icculus@3008
   373
    my $bigger = getBiggerCtype($ffloat, $fsize);
icculus@3008
   374
    my $interp = ($ffloat) ? '* 0.5' : '>> 1';
icculus@3008
   375
icculus@3008
   376
    my $resample = ($upsample) ? 'Upsample' : 'Downsample';
icculus@3008
   377
    my $hashid = getResamplerHashId($from, $channels, $upsample, 0);
icculus@3008
   378
    my $sym = "SDL_${resample}_${from}_${channels}c";
icculus@3008
   379
    $funcs{$hashid} = $sym;
icculus@3008
   380
    $custom_converters++;
icculus@3008
   381
icculus@3008
   382
    my $fudge = $fsize * $channels * 2;  # !!! FIXME
icculus@3008
   383
    my $eps_adjust = ($upsample) ? 'dstsize' : 'srcsize';
icculus@3008
   384
    my $incr = '';
icculus@3008
   385
    my $incr2 = '';
icculus@3008
   386
icculus@3008
   387
icculus@3008
   388
    # !!! FIXME: DEBUG_CONVERT should report frequencies.
icculus@3008
   389
    print <<EOF;
icculus@3008
   390
static void SDLCALL
icculus@3008
   391
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@3008
   392
{
icculus@3008
   393
#ifdef DEBUG_CONVERT
icculus@3008
   394
    fprintf(stderr, "$resample arbitrary (x%f) AUDIO_${from}, ${channels} channels.\\n", cvt->rate_incr);
icculus@3008
   395
#endif
icculus@3008
   396
icculus@3008
   397
    const int srcsize = cvt->len_cvt - $fudge;
icculus@3008
   398
    const int dstsize = (int) (((double)cvt->len_cvt) * cvt->rate_incr);
icculus@3008
   399
    register int eps = 0;
icculus@3008
   400
EOF
icculus@1982
   401
icculus@3008
   402
    # Upsampling (growing the buffer) needs to work backwards, since we
icculus@3008
   403
    #  overwrite the buffer as we go.
icculus@3008
   404
    if ($upsample) {
icculus@3008
   405
        print <<EOF;
icculus@3008
   406
    $fctype *dst = (($fctype *) (cvt->buf + dstsize)) - $channels;
icculus@3008
   407
    const $fctype *src = (($fctype *) (cvt->buf + cvt->len_cvt)) - $channels;
icculus@3008
   408
    const $fctype *target = ((const $fctype *) cvt->buf) - $channels;
icculus@3008
   409
EOF
icculus@3008
   410
    } else {
icculus@3008
   411
        print <<EOF;
icculus@3008
   412
    $fctype *dst = ($fctype *) cvt->buf;
icculus@3008
   413
    const $fctype *src = ($fctype *) cvt->buf;
icculus@3008
   414
    const $fctype *target = (const $fctype *) (cvt->buf + dstsize);
icculus@3008
   415
EOF
icculus@3008
   416
    }
icculus@3008
   417
icculus@3008
   418
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   419
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   420
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   421
        print <<EOF;
icculus@3008
   422
    $fctype sample${idx} = $val;
icculus@3008
   423
EOF
icculus@3008
   424
    }
icculus@3008
   425
icculus@3008
   426
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   427
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   428
        print <<EOF;
icculus@3008
   429
    $fctype last_sample${idx} = sample${idx};
icculus@3008
   430
EOF
icculus@3008
   431
    }
icculus@3008
   432
icculus@3008
   433
    print <<EOF;
icculus@3008
   434
    while (dst != target) {
icculus@3008
   435
EOF
icculus@3008
   436
icculus@3008
   437
    if ($upsample) {
icculus@3008
   438
        for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   439
            # !!! FIXME: don't do this swap every write, just when the samples change.
icculus@3008
   440
            my $idx = (($channels - $i) - 1);
icculus@3008
   441
            my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "sample${idx}");
icculus@3008
   442
            print <<EOF;
icculus@3008
   443
        dst[$idx] = $val;
icculus@3008
   444
EOF
icculus@3008
   445
        }
icculus@3008
   446
icculus@3008
   447
        $incr = ($channels == 1) ? 'dst--' : "dst -= $channels";
icculus@3008
   448
        $incr2 = ($channels == 1) ? 'src--' : "src -= $channels";
icculus@3008
   449
icculus@3008
   450
        print <<EOF;
icculus@3008
   451
        $incr;
icculus@3008
   452
        eps += srcsize;
icculus@3008
   453
        if ((eps << 1) >= dstsize) {
icculus@3008
   454
            $incr2;
icculus@3008
   455
EOF
icculus@3008
   456
    } else {  # downsample.
icculus@3008
   457
        $incr = ($channels == 1) ? 'src++' : "src += $channels";
icculus@3008
   458
        print <<EOF;
icculus@3008
   459
        $incr;
icculus@3008
   460
        eps += dstsize;
icculus@3008
   461
        if ((eps << 1) >= srcsize) {
icculus@3008
   462
EOF
icculus@3008
   463
        for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   464
            my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "sample${i}");
icculus@3008
   465
            print <<EOF;
icculus@3008
   466
            dst[$i] = $val;
icculus@3008
   467
EOF
icculus@3008
   468
        }
icculus@3008
   469
icculus@3008
   470
        $incr = ($channels == 1) ? 'dst++' : "dst += $channels";
icculus@3008
   471
        print <<EOF;
icculus@3008
   472
            $incr;
icculus@3008
   473
EOF
icculus@3008
   474
    }
icculus@3008
   475
icculus@3008
   476
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   477
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   478
        my $swapped = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   479
        print <<EOF;
icculus@3008
   480
            sample${idx} = ($fctype) (((($bigger) $swapped) + (($bigger) last_sample${idx})) $interp);
icculus@3008
   481
EOF
icculus@3008
   482
    }
icculus@3008
   483
icculus@3008
   484
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   485
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   486
        print <<EOF;
icculus@3008
   487
            last_sample${idx} = sample${idx};
icculus@3008
   488
EOF
icculus@3008
   489
    }
icculus@3008
   490
icculus@3008
   491
    print <<EOF;
icculus@3008
   492
            eps -= $eps_adjust;
icculus@3008
   493
        }
icculus@3008
   494
    }
icculus@3008
   495
EOF
icculus@3008
   496
icculus@3008
   497
        print <<EOF;
icculus@3008
   498
    cvt->len_cvt = dstsize;
icculus@3008
   499
    if (cvt->filters[++cvt->filter_index]) {
icculus@3008
   500
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   501
    }
icculus@1982
   502
}
icculus@1982
   503
icculus@3008
   504
EOF
icculus@3008
   505
icculus@3008
   506
}
icculus@3008
   507
icculus@3008
   508
# These handle clean resamples...doubling and quadrupling the sample rate, etc.
icculus@3008
   509
sub buildMultipleResampleFunc {
icculus@3008
   510
    # !!! FIXME: we do a lot of unnecessary and ugly casting in here, due to getSwapFunc().
icculus@3008
   511
    my ($from, $channels, $upsample, $multiple) = @_;
icculus@3008
   512
    my ($fsigned, $ffloat, $fsize, $fendian, $fctype) = splittype($from);
icculus@3008
   513
icculus@3008
   514
    my $bigger = getBiggerCtype($ffloat, $fsize);
icculus@3008
   515
    my $interp = ($ffloat) ? '* 0.5' : '>> 1';
icculus@3008
   516
    my $interp2 = ($ffloat) ? '* 0.25' : '>> 2';
icculus@3008
   517
    my $mult3 = ($ffloat) ? '3.0' : '3';
icculus@3008
   518
    my $lencvtop = ($upsample) ? '*' : '/';
icculus@3008
   519
icculus@3008
   520
    my $resample = ($upsample) ? 'Upsample' : 'Downsample';
icculus@3008
   521
    my $hashid = getResamplerHashId($from, $channels, $upsample, $multiple);
icculus@3008
   522
    my $sym = "SDL_${resample}_${from}_${channels}c_x${multiple}";
icculus@3008
   523
    $funcs{$hashid} = $sym;
icculus@3008
   524
    $custom_converters++;
icculus@3008
   525
icculus@3008
   526
    # !!! FIXME: DEBUG_CONVERT should report frequencies.
icculus@3008
   527
    print <<EOF;
icculus@3008
   528
static void SDLCALL
icculus@3008
   529
${sym}(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@1982
   530
{
icculus@3008
   531
#ifdef DEBUG_CONVERT
icculus@3008
   532
    fprintf(stderr, "$resample (x${multiple}) AUDIO_${from}, ${channels} channels.\\n");
icculus@3008
   533
#endif
icculus@3008
   534
icculus@3008
   535
    const int srcsize = cvt->len_cvt;
icculus@3008
   536
    const int dstsize = cvt->len_cvt $lencvtop $multiple;
icculus@3008
   537
EOF
icculus@3008
   538
icculus@3008
   539
    # Upsampling (growing the buffer) needs to work backwards, since we
icculus@3008
   540
    #  overwrite the buffer as we go.
icculus@3008
   541
    if ($upsample) {
icculus@3008
   542
        print <<EOF;
icculus@3008
   543
    $fctype *dst = (($fctype *) (cvt->buf + dstsize)) - $channels;
icculus@3008
   544
    const $fctype *src = (($fctype *) (cvt->buf + cvt->len_cvt)) - $channels;
icculus@3008
   545
    const $fctype *target = ((const $fctype *) cvt->buf) - $channels;
icculus@3008
   546
EOF
icculus@3008
   547
    } else {
icculus@3008
   548
        print <<EOF;
icculus@3008
   549
    $fctype *dst = ($fctype *) cvt->buf;
icculus@3008
   550
    const $fctype *src = ($fctype *) cvt->buf;
icculus@3008
   551
    const $fctype *target = (const $fctype *) (cvt->buf + dstsize);
icculus@3008
   552
EOF
icculus@3008
   553
    }
icculus@3008
   554
icculus@3008
   555
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   556
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   557
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   558
        print <<EOF;
icculus@3008
   559
    $bigger last_sample${idx} = ($bigger) $val;
icculus@3008
   560
EOF
icculus@3008
   561
    }
icculus@3008
   562
icculus@3008
   563
    print <<EOF;
icculus@3008
   564
    while (dst != target) {
icculus@3008
   565
EOF
icculus@3008
   566
icculus@3008
   567
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   568
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   569
        my $val = getSwapFunc($fsize, $fsigned, $ffloat, $fendian, "src[$idx]");
icculus@3008
   570
        print <<EOF;
icculus@3008
   571
        const $bigger sample${idx} = ($bigger) $val;
icculus@3008
   572
EOF
icculus@3008
   573
    }
icculus@3008
   574
icculus@3008
   575
    my $incr = '';
icculus@3008
   576
    if ($upsample) {
icculus@3008
   577
        $incr = ($channels == 1) ? 'src--' : "src -= $channels";
icculus@3008
   578
    } else {
icculus@3008
   579
        my $amount = $channels * $multiple;
icculus@3008
   580
        $incr = "src += $amount";  # can't ever be 1, so no "++" version.
icculus@3008
   581
    }
icculus@3008
   582
icculus@3008
   583
icculus@3008
   584
    print <<EOF;
icculus@3008
   585
        $incr;
icculus@1982
   586
EOF
icculus@1982
   587
icculus@3008
   588
    # !!! FIXME: This really begs for some Altivec or SSE, etc.
icculus@3008
   589
    if ($upsample) {
icculus@3008
   590
        if ($multiple == 2) {
icculus@3008
   591
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   592
                my $dsti = $i + $channels;
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
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   598
                my $dsti = $i;
icculus@3008
   599
                print <<EOF;
icculus@3008
   600
        dst[$dsti] = ($fctype) sample${i};
icculus@3008
   601
EOF
icculus@3008
   602
            }
icculus@3008
   603
        } elsif ($multiple == 4) {
icculus@3008
   604
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   605
                my $dsti = $i + ($channels * 3);
icculus@3008
   606
                print <<EOF;
icculus@3008
   607
        dst[$dsti] = ($fctype) sample${i};
icculus@3008
   608
EOF
icculus@3008
   609
            }
icculus@3008
   610
icculus@3008
   611
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   612
                my $dsti = $i + ($channels * 2);
icculus@3008
   613
                print <<EOF;
icculus@3008
   614
        dst[$dsti] = ($fctype) ((($mult3 * sample${i}) + last_sample${i}) $interp2);
icculus@3008
   615
EOF
icculus@3008
   616
            }
icculus@3008
   617
icculus@3008
   618
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   619
                my $dsti = $i + ($channels * 1);
icculus@3008
   620
                print <<EOF;
icculus@3008
   621
        dst[$dsti] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   622
EOF
icculus@3008
   623
            }
icculus@3008
   624
icculus@3008
   625
            for (my $i = $channels-1; $i >= 0; $i--) {
icculus@3008
   626
                my $dsti = $i + ($channels * 0);
icculus@3008
   627
                print <<EOF;
icculus@3008
   628
        dst[$dsti] = ($fctype) ((sample${i} + ($mult3 * last_sample${i})) $interp2);
icculus@3008
   629
EOF
icculus@3008
   630
            }
icculus@3008
   631
        } else {
icculus@3008
   632
            die('bug in program.');  # we only handle x2 and x4.
icculus@1982
   633
        }
icculus@3008
   634
    } else {  # downsample.
icculus@3008
   635
        if ($multiple == 2) {
icculus@3008
   636
            for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   637
                print <<EOF;
icculus@3008
   638
        dst[$i] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   639
EOF
icculus@3008
   640
            }
icculus@3008
   641
        } elsif ($multiple == 4) {
icculus@3008
   642
            # !!! FIXME: interpolate all 4 samples?
icculus@3008
   643
            for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   644
                print <<EOF;
icculus@3008
   645
        dst[$i] = ($fctype) ((sample${i} + last_sample${i}) $interp);
icculus@3008
   646
EOF
icculus@3008
   647
            }
icculus@3008
   648
        } else {
icculus@3008
   649
            die('bug in program.');  # we only handle x2 and x4.
icculus@3008
   650
        }
icculus@3008
   651
    }
icculus@3008
   652
icculus@3008
   653
    for (my $i = 0; $i < $channels; $i++) {
icculus@3008
   654
        my $idx = ($upsample) ? (($channels - $i) - 1) : $i;
icculus@3008
   655
        print <<EOF;
icculus@3008
   656
        last_sample${idx} = sample${idx};
icculus@3008
   657
EOF
icculus@3008
   658
    }
icculus@3008
   659
icculus@3008
   660
    if ($upsample) {
icculus@3008
   661
        my $amount = $channels * $multiple;
icculus@3008
   662
        $incr = "dst -= $amount";  # can't ever be 1, so no "--" version.
icculus@3008
   663
    } else {
icculus@3008
   664
        $incr = ($channels == 1) ? 'dst++' : "dst += $channels";
icculus@3008
   665
    }
icculus@3008
   666
icculus@3008
   667
    print <<EOF;
icculus@3008
   668
        $incr;
icculus@3008
   669
    }
icculus@3008
   670
icculus@3008
   671
    cvt->len_cvt = dstsize;
icculus@3008
   672
    if (cvt->filters[++cvt->filter_index]) {
icculus@3008
   673
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   674
    }
icculus@1982
   675
}
icculus@1982
   676
icculus@3008
   677
EOF
icculus@3008
   678
icculus@3008
   679
}
icculus@3008
   680
icculus@3008
   681
sub buildResamplers {
icculus@3019
   682
    print "#if !NO_RESAMPLERS\n\n";
icculus@3019
   683
    foreach (@audiotypes) {
icculus@3019
   684
        my $from = $_;
icculus@3019
   685
        foreach (@channels) {
icculus@3019
   686
            my $channel = $_;
icculus@3019
   687
            buildArbitraryResampleFunc($from, $channel, 1);
icculus@3019
   688
            buildArbitraryResampleFunc($from, $channel, 0);
icculus@3019
   689
        }
icculus@3019
   690
    }
icculus@3019
   691
icculus@3019
   692
    print "\n#if !LESS_RESAMPLERS\n\n";
icculus@3008
   693
    foreach (@audiotypes) {
icculus@3008
   694
        my $from = $_;
icculus@3008
   695
        foreach (@channels) {
icculus@3008
   696
            my $channel = $_;
icculus@3008
   697
            for (my $multiple = 2; $multiple <= 4; $multiple += 2) {
icculus@3008
   698
                buildMultipleResampleFunc($from, $channel, 1, $multiple);
icculus@3008
   699
                buildMultipleResampleFunc($from, $channel, 0, $multiple);
icculus@3008
   700
            }
icculus@3008
   701
        }
icculus@3008
   702
    }
icculus@3008
   703
icculus@3019
   704
    print "#endif  /* !LESS_RESAMPLERS */\n";
icculus@3019
   705
    print "#endif  /* !NO_RESAMPLERS */\n\n\n";
icculus@3019
   706
icculus@3008
   707
    print "const SDL_AudioRateFilters sdl_audio_rate_filters[] =\n{\n";
icculus@3019
   708
    print "#if !NO_RESAMPLERS\n";
icculus@3008
   709
    foreach (@audiotypes) {
icculus@3008
   710
        my $from = $_;
icculus@3008
   711
        foreach (@channels) {
icculus@3008
   712
            my $channel = $_;
icculus@3019
   713
            for (my $upsample = 0; $upsample <= 1; $upsample++) {
icculus@3019
   714
                my $hashid = getResamplerHashId($from, $channel, $upsample, 0);
icculus@3019
   715
                my $sym = $funcs{$hashid};
icculus@3019
   716
                print("    { AUDIO_$from, $channel, $upsample, 0, $sym },\n");
icculus@3019
   717
            }
icculus@3019
   718
        }
icculus@3019
   719
    }
icculus@3019
   720
icculus@3019
   721
    print "#if !LESS_RESAMPLERS\n";
icculus@3019
   722
    foreach (@audiotypes) {
icculus@3019
   723
        my $from = $_;
icculus@3019
   724
        foreach (@channels) {
icculus@3019
   725
            my $channel = $_;
icculus@3019
   726
            for (my $multiple = 2; $multiple <= 4; $multiple += 2) {
icculus@3008
   727
                for (my $upsample = 0; $upsample <= 1; $upsample++) {
icculus@3008
   728
                    my $hashid = getResamplerHashId($from, $channel, $upsample, $multiple);
icculus@3008
   729
                    my $sym = $funcs{$hashid};
icculus@3008
   730
                    print("    { AUDIO_$from, $channel, $upsample, $multiple, $sym },\n");
icculus@3008
   731
                }
icculus@3008
   732
            }
icculus@3008
   733
        }
icculus@3008
   734
    }
icculus@3008
   735
icculus@3019
   736
    print "#endif  /* !LESS_RESAMPLERS */\n";
icculus@3019
   737
    print "#endif  /* !NO_RESAMPLERS */\n";
icculus@3008
   738
    print "};\n\n";
icculus@3008
   739
}
icculus@1982
   740
icculus@1982
   741
icculus@3008
   742
# mainline ...
icculus@1982
   743
icculus@3008
   744
outputHeader();
icculus@3008
   745
buildTypeConverters();
icculus@3008
   746
buildResamplers();
slouken@1985
   747
outputFooter();
slouken@1985
   748
icculus@1982
   749
exit 0;
icculus@1982
   750
slouken@1985
   751
# end of sdlgenaudiocvt.pl ...
icculus@1982
   752