src/audio/SDL_audiocvt.c
author Aaron Wishnick <schnarf@gmail.com>
Tue, 12 Aug 2008 00:24:42 +0000
branchgsoc2008_audio_resampling
changeset 2663 0caed045d01b
parent 2662 5470680ca587
permissions -rw-r--r--
General cleanup and fixed a buffer overrun bug. It may be necessary to normalize filter gain differently or something.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
schnarf@2655
    23
#include <math.h>
slouken@0
    24
slouken@0
    25
/* Functions for audio drivers to perform runtime conversion of audio format */
slouken@0
    26
slouken@0
    27
#include "SDL_audio.h"
icculus@1982
    28
#include "SDL_audio_c.h"
slouken@0
    29
schnarf@2656
    30
#define DEBUG_CONVERT
schnarf@2656
    31
schnarf@2663
    32
/* These are fractional multiplication routines. That is, their inputs
schnarf@2663
    33
   are two numbers in the range [-1, 1) and the result falls in that
schnarf@2663
    34
   same range. The output is the same size as the inputs, i.e.
schnarf@2663
    35
   32-bit x 32-bit = 32-bit.
schnarf@2663
    36
 */
schnarf@2658
    37
schnarf@2658
    38
/* We hope here that the right shift includes sign extension */
schnarf@2658
    39
#ifdef SDL_HAS_64BIT_Type		
schnarf@2658
    40
#define SDL_FixMpy32(a, b) ((((Sint64)a * (Sint64)b) >> 31) & 0xffffffff)
schnarf@2657
    41
#else
schnarf@2663
    42
/* If we don't have the 64-bit type, do something more complicated. See http://www.8052.com/mul16.phtml or http://www.cs.uaf.edu/~cs301/notes/Chapter5/node5.html */
schnarf@2658
    43
#define SDL_FixMpy32(a, b) ((((Sint64)a * (Sint64)b) >> 31) & 0xffffffff)
schnarf@2657
    44
#endif
schnarf@2658
    45
#define SDL_FixMpy16(a, b) ((((Sint32)a * (Sint32)b) >> 14) & 0xffff)
schnarf@2658
    46
#define SDL_FixMpy8(a, b) ((((Sint16)a * (Sint16)b) >> 7) & 0xff)
schnarf@2663
    47
/* This macro just makes the floating point filtering code not have to be a special case. */
schnarf@2663
    48
#define SDL_FloatMpy(a, b) (a * b)
schnarf@2663
    49
schnarf@2663
    50
/* These macros take floating point numbers in the range [-1.0f, 1.0f) and
schnarf@2663
    51
   represent them as fixed-point numbers in that same range. There's no
schnarf@2663
    52
   checking that the floating point argument is inside the appropriate range.
schnarf@2663
    53
 */
schnarf@2663
    54
schnarf@2663
    55
#define SDL_Make_1_7(a) (Sint8)(a * 128.0f)
schnarf@2663
    56
#define SDL_Make_1_15(a) (Sint16)(a * 32768.0f)
schnarf@2663
    57
#define SDL_Make_1_31(a) (Sint32)(a * 2147483648.0f)
schnarf@2663
    58
#define SDL_Make_2_6(a) (Sint8)(a * 64.0f)
schnarf@2663
    59
#define SDL_Make_2_14(a) (Sint16)(a * 16384.0f)
schnarf@2663
    60
#define SDL_Make_2_30(a) (Sint32)(a * 1073741824.0f)
schnarf@2657
    61
slouken@0
    62
/* Effectively mix right and left channels into a single channel */
icculus@1982
    63
static void SDLCALL
icculus@1982
    64
SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
    65
{
slouken@1895
    66
    int i;
slouken@1895
    67
    Sint32 sample;
slouken@0
    68
slouken@0
    69
#ifdef DEBUG_CONVERT
slouken@1895
    70
    fprintf(stderr, "Converting to mono\n");
slouken@0
    71
#endif
slouken@1985
    72
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
    73
    case AUDIO_U8:
slouken@1895
    74
        {
slouken@1895
    75
            Uint8 *src, *dst;
slouken@0
    76
slouken@1895
    77
            src = cvt->buf;
slouken@1895
    78
            dst = cvt->buf;
slouken@1895
    79
            for (i = cvt->len_cvt / 2; i; --i) {
slouken@1895
    80
                sample = src[0] + src[1];
slouken@2042
    81
                *dst = (Uint8) (sample / 2);
slouken@1895
    82
                src += 2;
slouken@1895
    83
                dst += 1;
slouken@1895
    84
            }
slouken@1895
    85
        }
slouken@1895
    86
        break;
slouken@0
    87
slouken@1895
    88
    case AUDIO_S8:
slouken@1895
    89
        {
slouken@1895
    90
            Sint8 *src, *dst;
slouken@0
    91
slouken@1895
    92
            src = (Sint8 *) cvt->buf;
slouken@1895
    93
            dst = (Sint8 *) cvt->buf;
slouken@1895
    94
            for (i = cvt->len_cvt / 2; i; --i) {
slouken@1895
    95
                sample = src[0] + src[1];
slouken@2042
    96
                *dst = (Sint8) (sample / 2);
slouken@1895
    97
                src += 2;
slouken@1895
    98
                dst += 1;
slouken@1895
    99
            }
slouken@1895
   100
        }
slouken@1895
   101
        break;
slouken@0
   102
slouken@1895
   103
    case AUDIO_U16:
slouken@1895
   104
        {
slouken@1895
   105
            Uint8 *src, *dst;
slouken@0
   106
slouken@1895
   107
            src = cvt->buf;
slouken@1895
   108
            dst = cvt->buf;
icculus@1982
   109
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   110
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   111
                    sample = (Uint16) ((src[0] << 8) | src[1]) +
slouken@1895
   112
                        (Uint16) ((src[2] << 8) | src[3]);
slouken@2042
   113
                    sample /= 2;
slouken@2042
   114
                    dst[1] = (sample & 0xFF);
slouken@2042
   115
                    sample >>= 8;
slouken@2042
   116
                    dst[0] = (sample & 0xFF);
slouken@1895
   117
                    src += 4;
slouken@1895
   118
                    dst += 2;
slouken@1895
   119
                }
slouken@1895
   120
            } else {
slouken@1895
   121
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   122
                    sample = (Uint16) ((src[1] << 8) | src[0]) +
slouken@1895
   123
                        (Uint16) ((src[3] << 8) | src[2]);
slouken@2042
   124
                    sample /= 2;
slouken@2042
   125
                    dst[0] = (sample & 0xFF);
slouken@2042
   126
                    sample >>= 8;
slouken@2042
   127
                    dst[1] = (sample & 0xFF);
slouken@1895
   128
                    src += 4;
slouken@1895
   129
                    dst += 2;
slouken@1895
   130
                }
slouken@1895
   131
            }
slouken@1895
   132
        }
slouken@1895
   133
        break;
slouken@0
   134
slouken@1895
   135
    case AUDIO_S16:
slouken@1895
   136
        {
slouken@1895
   137
            Uint8 *src, *dst;
slouken@0
   138
slouken@1895
   139
            src = cvt->buf;
slouken@1895
   140
            dst = cvt->buf;
icculus@1982
   141
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   142
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   143
                    sample = (Sint16) ((src[0] << 8) | src[1]) +
slouken@1895
   144
                        (Sint16) ((src[2] << 8) | src[3]);
slouken@2042
   145
                    sample /= 2;
slouken@2042
   146
                    dst[1] = (sample & 0xFF);
slouken@2042
   147
                    sample >>= 8;
slouken@2042
   148
                    dst[0] = (sample & 0xFF);
slouken@1895
   149
                    src += 4;
slouken@1895
   150
                    dst += 2;
slouken@1895
   151
                }
slouken@1895
   152
            } else {
slouken@1895
   153
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   154
                    sample = (Sint16) ((src[1] << 8) | src[0]) +
slouken@1895
   155
                        (Sint16) ((src[3] << 8) | src[2]);
slouken@2042
   156
                    sample /= 2;
slouken@2042
   157
                    dst[0] = (sample & 0xFF);
slouken@2042
   158
                    sample >>= 8;
slouken@2042
   159
                    dst[1] = (sample & 0xFF);
slouken@1895
   160
                    src += 4;
slouken@1895
   161
                    dst += 2;
slouken@1895
   162
                }
slouken@1895
   163
            }
slouken@1895
   164
        }
slouken@1895
   165
        break;
icculus@1982
   166
icculus@1982
   167
    case AUDIO_S32:
icculus@1982
   168
        {
icculus@1982
   169
            const Uint32 *src = (const Uint32 *) cvt->buf;
icculus@1982
   170
            Uint32 *dst = (Uint32 *) cvt->buf;
icculus@1982
   171
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   172
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@1982
   173
                    const Sint64 added =
slouken@1985
   174
                        (((Sint64) (Sint32) SDL_SwapBE32(src[0])) +
slouken@1985
   175
                         ((Sint64) (Sint32) SDL_SwapBE32(src[1])));
icculus@2078
   176
                    *(dst++) = SDL_SwapBE32((Uint32) ((Sint32) (added / 2)));
icculus@1982
   177
                }
icculus@1982
   178
            } else {
icculus@1982
   179
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@1982
   180
                    const Sint64 added =
slouken@1985
   181
                        (((Sint64) (Sint32) SDL_SwapLE32(src[0])) +
slouken@1985
   182
                         ((Sint64) (Sint32) SDL_SwapLE32(src[1])));
icculus@2078
   183
                    *(dst++) = SDL_SwapLE32((Uint32) ((Sint32) (added / 2)));
icculus@1982
   184
                }
icculus@1982
   185
            }
icculus@1982
   186
        }
icculus@1982
   187
        break;
icculus@1982
   188
icculus@1982
   189
    case AUDIO_F32:
icculus@1982
   190
        {
icculus@2014
   191
            const float *src = (const float *) cvt->buf;
icculus@2014
   192
            float *dst = (float *) cvt->buf;
icculus@1982
   193
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   194
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@2049
   195
                    const float src1 = SDL_SwapFloatBE(src[0]);
icculus@2049
   196
                    const float src2 = SDL_SwapFloatBE(src[1]);
icculus@1982
   197
                    const double added = ((double) src1) + ((double) src2);
icculus@2049
   198
                    const float halved = (float) (added * 0.5);
icculus@2049
   199
                    *(dst++) = SDL_SwapFloatBE(halved);
icculus@1982
   200
                }
icculus@1982
   201
            } else {
icculus@1982
   202
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@2049
   203
                    const float src1 = SDL_SwapFloatLE(src[0]);
icculus@2049
   204
                    const float src2 = SDL_SwapFloatLE(src[1]);
icculus@1982
   205
                    const double added = ((double) src1) + ((double) src2);
icculus@2049
   206
                    const float halved = (float) (added * 0.5);
icculus@2049
   207
                    *(dst++) = SDL_SwapFloatLE(halved);
icculus@1982
   208
                }
icculus@1982
   209
            }
icculus@1982
   210
        }
icculus@1982
   211
        break;
slouken@1895
   212
    }
icculus@1982
   213
slouken@1895
   214
    cvt->len_cvt /= 2;
slouken@1895
   215
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   216
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   217
    }
slouken@0
   218
}
slouken@0
   219
icculus@1982
   220
slouken@942
   221
/* Discard top 4 channels */
icculus@1982
   222
static void SDLCALL
icculus@1982
   223
SDL_ConvertStrip(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   224
{
slouken@1895
   225
    int i;
slouken@942
   226
slouken@942
   227
#ifdef DEBUG_CONVERT
icculus@1982
   228
    fprintf(stderr, "Converting down from 6 channels to stereo\n");
slouken@942
   229
#endif
slouken@942
   230
slouken@1985
   231
#define strip_chans_6_to_2(type) \
icculus@1982
   232
    { \
icculus@1982
   233
        const type *src = (const type *) cvt->buf; \
icculus@1982
   234
        type *dst = (type *) cvt->buf; \
icculus@1982
   235
        for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
icculus@1982
   236
            dst[0] = src[0]; \
icculus@1982
   237
            dst[1] = src[1]; \
icculus@1982
   238
            src += 6; \
icculus@1982
   239
            dst += 2; \
icculus@1982
   240
        } \
icculus@1982
   241
    }
slouken@942
   242
icculus@1982
   243
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   244
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   245
    case 8:
slouken@1985
   246
        strip_chans_6_to_2(Uint8);
slouken@1985
   247
        break;
slouken@1985
   248
    case 16:
slouken@1985
   249
        strip_chans_6_to_2(Uint16);
slouken@1985
   250
        break;
slouken@1985
   251
    case 32:
slouken@1985
   252
        strip_chans_6_to_2(Uint32);
slouken@1985
   253
        break;
icculus@1982
   254
    }
slouken@942
   255
slouken@1985
   256
#undef strip_chans_6_to_2
slouken@942
   257
slouken@1895
   258
    cvt->len_cvt /= 3;
slouken@1895
   259
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   260
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   261
    }
slouken@942
   262
}
slouken@942
   263
slouken@942
   264
slouken@942
   265
/* Discard top 2 channels of 6 */
icculus@1982
   266
static void SDLCALL
icculus@1982
   267
SDL_ConvertStrip_2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   268
{
slouken@1895
   269
    int i;
slouken@942
   270
slouken@942
   271
#ifdef DEBUG_CONVERT
slouken@1895
   272
    fprintf(stderr, "Converting 6 down to quad\n");
slouken@942
   273
#endif
slouken@942
   274
slouken@1985
   275
#define strip_chans_6_to_4(type) \
icculus@1982
   276
    { \
icculus@1982
   277
        const type *src = (const type *) cvt->buf; \
icculus@1982
   278
        type *dst = (type *) cvt->buf; \
icculus@1982
   279
        for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
icculus@1982
   280
            dst[0] = src[0]; \
icculus@1982
   281
            dst[1] = src[1]; \
icculus@1982
   282
            dst[2] = src[2]; \
icculus@1982
   283
            dst[3] = src[3]; \
icculus@1982
   284
            src += 6; \
icculus@1982
   285
            dst += 4; \
icculus@1982
   286
        } \
icculus@1982
   287
    }
slouken@942
   288
icculus@1982
   289
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   290
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   291
    case 8:
slouken@1985
   292
        strip_chans_6_to_4(Uint8);
slouken@1985
   293
        break;
slouken@1985
   294
    case 16:
slouken@1985
   295
        strip_chans_6_to_4(Uint16);
slouken@1985
   296
        break;
slouken@1985
   297
    case 32:
slouken@1985
   298
        strip_chans_6_to_4(Uint32);
slouken@1985
   299
        break;
icculus@1982
   300
    }
slouken@942
   301
slouken@1985
   302
#undef strip_chans_6_to_4
slouken@942
   303
icculus@1982
   304
    cvt->len_cvt /= 6;
icculus@1982
   305
    cvt->len_cvt *= 4;
slouken@1895
   306
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   307
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   308
    }
slouken@942
   309
}
slouken@0
   310
slouken@0
   311
/* Duplicate a mono channel to both stereo channels */
icculus@1982
   312
static void SDLCALL
icculus@1982
   313
SDL_ConvertStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
   314
{
slouken@1895
   315
    int i;
slouken@0
   316
slouken@0
   317
#ifdef DEBUG_CONVERT
slouken@1895
   318
    fprintf(stderr, "Converting to stereo\n");
slouken@0
   319
#endif
slouken@0
   320
slouken@1985
   321
#define dup_chans_1_to_2(type) \
icculus@1982
   322
    { \
icculus@1982
   323
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   324
        type *dst = (type *) (cvt->buf + cvt->len_cvt * 2); \
icculus@1982
   325
        for (i = cvt->len_cvt / 2; i; --i, --src) { \
icculus@1982
   326
            const type val = *src; \
icculus@1982
   327
            dst -= 2; \
icculus@1982
   328
            dst[0] = dst[1] = val; \
icculus@1982
   329
        } \
icculus@1982
   330
    }
slouken@0
   331
icculus@1982
   332
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   333
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   334
    case 8:
slouken@1985
   335
        dup_chans_1_to_2(Uint8);
slouken@1985
   336
        break;
slouken@1985
   337
    case 16:
slouken@1985
   338
        dup_chans_1_to_2(Uint16);
slouken@1985
   339
        break;
slouken@1985
   340
    case 32:
slouken@1985
   341
        dup_chans_1_to_2(Uint32);
slouken@1985
   342
        break;
slouken@1895
   343
    }
icculus@1982
   344
slouken@1985
   345
#undef dup_chans_1_to_2
icculus@1982
   346
slouken@1895
   347
    cvt->len_cvt *= 2;
slouken@1895
   348
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   349
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   350
    }
slouken@0
   351
}
slouken@0
   352
slouken@942
   353
slouken@942
   354
/* Duplicate a stereo channel to a pseudo-5.1 stream */
icculus@1982
   355
static void SDLCALL
icculus@1982
   356
SDL_ConvertSurround(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   357
{
slouken@1895
   358
    int i;
slouken@942
   359
slouken@942
   360
#ifdef DEBUG_CONVERT
slouken@1895
   361
    fprintf(stderr, "Converting stereo to surround\n");
slouken@942
   362
#endif
slouken@942
   363
slouken@1985
   364
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
   365
    case AUDIO_U8:
slouken@1895
   366
        {
slouken@1895
   367
            Uint8 *src, *dst, lf, rf, ce;
slouken@942
   368
slouken@1895
   369
            src = (Uint8 *) (cvt->buf + cvt->len_cvt);
slouken@1895
   370
            dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 3);
slouken@1895
   371
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   372
                dst -= 6;
slouken@1895
   373
                src -= 2;
slouken@1895
   374
                lf = src[0];
slouken@1895
   375
                rf = src[1];
slouken@1895
   376
                ce = (lf / 2) + (rf / 2);
slouken@1895
   377
                dst[0] = lf;
slouken@1895
   378
                dst[1] = rf;
slouken@1895
   379
                dst[2] = lf - ce;
slouken@1895
   380
                dst[3] = rf - ce;
slouken@1895
   381
                dst[4] = ce;
slouken@1895
   382
                dst[5] = ce;
slouken@1895
   383
            }
slouken@1895
   384
        }
slouken@1895
   385
        break;
slouken@942
   386
slouken@1895
   387
    case AUDIO_S8:
slouken@1895
   388
        {
slouken@1895
   389
            Sint8 *src, *dst, lf, rf, ce;
slouken@942
   390
slouken@1895
   391
            src = (Sint8 *) cvt->buf + cvt->len_cvt;
slouken@1895
   392
            dst = (Sint8 *) cvt->buf + cvt->len_cvt * 3;
slouken@1895
   393
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   394
                dst -= 6;
slouken@1895
   395
                src -= 2;
slouken@1895
   396
                lf = src[0];
slouken@1895
   397
                rf = src[1];
slouken@1895
   398
                ce = (lf / 2) + (rf / 2);
slouken@1895
   399
                dst[0] = lf;
slouken@1895
   400
                dst[1] = rf;
slouken@1895
   401
                dst[2] = lf - ce;
slouken@1895
   402
                dst[3] = rf - ce;
slouken@1895
   403
                dst[4] = ce;
slouken@1895
   404
                dst[5] = ce;
slouken@1895
   405
            }
slouken@1895
   406
        }
slouken@1895
   407
        break;
slouken@942
   408
slouken@1895
   409
    case AUDIO_U16:
slouken@1895
   410
        {
slouken@1895
   411
            Uint8 *src, *dst;
slouken@1895
   412
            Uint16 lf, rf, ce, lr, rr;
slouken@942
   413
slouken@1895
   414
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   415
            dst = cvt->buf + cvt->len_cvt * 3;
slouken@942
   416
icculus@1982
   417
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   418
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   419
                    dst -= 12;
slouken@1895
   420
                    src -= 4;
slouken@1895
   421
                    lf = (Uint16) ((src[0] << 8) | src[1]);
slouken@1895
   422
                    rf = (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
   423
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   424
                    rr = lf - ce;
slouken@1895
   425
                    lr = rf - ce;
slouken@1895
   426
                    dst[1] = (lf & 0xFF);
slouken@1895
   427
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   428
                    dst[3] = (rf & 0xFF);
slouken@1895
   429
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   430
slouken@1895
   431
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   432
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   433
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   434
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   435
slouken@1895
   436
                    dst[1 + 8] = (ce & 0xFF);
slouken@1895
   437
                    dst[0 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   438
                    dst[3 + 8] = (ce & 0xFF);
slouken@1895
   439
                    dst[2 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   440
                }
slouken@1895
   441
            } else {
slouken@1895
   442
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   443
                    dst -= 12;
slouken@1895
   444
                    src -= 4;
slouken@1895
   445
                    lf = (Uint16) ((src[1] << 8) | src[0]);
slouken@1895
   446
                    rf = (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   447
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   448
                    rr = lf - ce;
slouken@1895
   449
                    lr = rf - ce;
slouken@1895
   450
                    dst[0] = (lf & 0xFF);
slouken@1895
   451
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   452
                    dst[2] = (rf & 0xFF);
slouken@1895
   453
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   454
slouken@1895
   455
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   456
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   457
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   458
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   459
slouken@1895
   460
                    dst[0 + 8] = (ce & 0xFF);
slouken@1895
   461
                    dst[1 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   462
                    dst[2 + 8] = (ce & 0xFF);
slouken@1895
   463
                    dst[3 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   464
                }
slouken@1895
   465
            }
slouken@1895
   466
        }
slouken@1895
   467
        break;
slouken@942
   468
slouken@1895
   469
    case AUDIO_S16:
slouken@1895
   470
        {
slouken@1895
   471
            Uint8 *src, *dst;
slouken@1895
   472
            Sint16 lf, rf, ce, lr, rr;
slouken@942
   473
slouken@1895
   474
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   475
            dst = cvt->buf + cvt->len_cvt * 3;
slouken@942
   476
icculus@1982
   477
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   478
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   479
                    dst -= 12;
slouken@1895
   480
                    src -= 4;
slouken@1895
   481
                    lf = (Sint16) ((src[0] << 8) | src[1]);
slouken@1895
   482
                    rf = (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   483
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   484
                    rr = lf - ce;
slouken@1895
   485
                    lr = rf - ce;
slouken@1895
   486
                    dst[1] = (lf & 0xFF);
slouken@1895
   487
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   488
                    dst[3] = (rf & 0xFF);
slouken@1895
   489
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   490
slouken@1895
   491
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   492
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   493
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   494
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   495
slouken@1895
   496
                    dst[1 + 8] = (ce & 0xFF);
slouken@1895
   497
                    dst[0 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   498
                    dst[3 + 8] = (ce & 0xFF);
slouken@1895
   499
                    dst[2 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   500
                }
slouken@1895
   501
            } else {
slouken@1895
   502
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   503
                    dst -= 12;
slouken@1895
   504
                    src -= 4;
slouken@1895
   505
                    lf = (Sint16) ((src[1] << 8) | src[0]);
slouken@1895
   506
                    rf = (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   507
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   508
                    rr = lf - ce;
slouken@1895
   509
                    lr = rf - ce;
slouken@1895
   510
                    dst[0] = (lf & 0xFF);
slouken@1895
   511
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   512
                    dst[2] = (rf & 0xFF);
slouken@1895
   513
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   514
slouken@1895
   515
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   516
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   517
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   518
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   519
slouken@1895
   520
                    dst[0 + 8] = (ce & 0xFF);
slouken@1895
   521
                    dst[1 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   522
                    dst[2 + 8] = (ce & 0xFF);
slouken@1895
   523
                    dst[3 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   524
                }
slouken@1895
   525
            }
slouken@1895
   526
        }
slouken@1895
   527
        break;
icculus@1982
   528
icculus@1982
   529
    case AUDIO_S32:
icculus@1982
   530
        {
icculus@1982
   531
            Sint32 lf, rf, ce;
icculus@1982
   532
            const Uint32 *src = (const Uint32 *) cvt->buf + cvt->len_cvt;
icculus@1982
   533
            Uint32 *dst = (Uint32 *) cvt->buf + cvt->len_cvt * 3;
icculus@1982
   534
icculus@1982
   535
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   536
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   537
                    dst -= 6;
icculus@1982
   538
                    src -= 2;
icculus@1982
   539
                    lf = (Sint32) SDL_SwapBE32(src[0]);
icculus@1982
   540
                    rf = (Sint32) SDL_SwapBE32(src[1]);
icculus@1982
   541
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   542
                    dst[0] = SDL_SwapBE32((Uint32) lf);
icculus@1982
   543
                    dst[1] = SDL_SwapBE32((Uint32) rf);
icculus@1982
   544
                    dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
icculus@1982
   545
                    dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
icculus@1982
   546
                    dst[4] = SDL_SwapBE32((Uint32) ce);
icculus@1982
   547
                    dst[5] = SDL_SwapBE32((Uint32) ce);
icculus@1982
   548
                }
icculus@1982
   549
            } else {
icculus@1982
   550
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   551
                    dst -= 6;
icculus@1982
   552
                    src -= 2;
icculus@1982
   553
                    lf = (Sint32) SDL_SwapLE32(src[0]);
icculus@1982
   554
                    rf = (Sint32) SDL_SwapLE32(src[1]);
icculus@1982
   555
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   556
                    dst[0] = src[0];
icculus@1982
   557
                    dst[1] = src[1];
icculus@1982
   558
                    dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
icculus@1982
   559
                    dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
icculus@1982
   560
                    dst[4] = SDL_SwapLE32((Uint32) ce);
icculus@1982
   561
                    dst[5] = SDL_SwapLE32((Uint32) ce);
icculus@1982
   562
                }
icculus@1982
   563
            }
icculus@1982
   564
        }
icculus@1982
   565
        break;
icculus@1982
   566
icculus@1982
   567
    case AUDIO_F32:
icculus@1982
   568
        {
icculus@1982
   569
            float lf, rf, ce;
icculus@2014
   570
            const float *src = (const float *) cvt->buf + cvt->len_cvt;
icculus@2014
   571
            float *dst = (float *) cvt->buf + cvt->len_cvt * 3;
icculus@1982
   572
icculus@1982
   573
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   574
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   575
                    dst -= 6;
icculus@1982
   576
                    src -= 2;
icculus@2014
   577
                    lf = SDL_SwapFloatBE(src[0]);
icculus@2014
   578
                    rf = SDL_SwapFloatBE(src[1]);
icculus@1982
   579
                    ce = (lf * 0.5f) + (rf * 0.5f);
icculus@1982
   580
                    dst[0] = src[0];
icculus@1982
   581
                    dst[1] = src[1];
icculus@2014
   582
                    dst[2] = SDL_SwapFloatBE(lf - ce);
icculus@2014
   583
                    dst[3] = SDL_SwapFloatBE(rf - ce);
icculus@2014
   584
                    dst[4] = dst[5] = SDL_SwapFloatBE(ce);
icculus@1982
   585
                }
icculus@1982
   586
            } else {
icculus@1982
   587
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   588
                    dst -= 6;
icculus@1982
   589
                    src -= 2;
icculus@2014
   590
                    lf = SDL_SwapFloatLE(src[0]);
icculus@2014
   591
                    rf = SDL_SwapFloatLE(src[1]);
icculus@1982
   592
                    ce = (lf * 0.5f) + (rf * 0.5f);
icculus@1982
   593
                    dst[0] = src[0];
icculus@1982
   594
                    dst[1] = src[1];
icculus@2014
   595
                    dst[2] = SDL_SwapFloatLE(lf - ce);
icculus@2014
   596
                    dst[3] = SDL_SwapFloatLE(rf - ce);
icculus@2014
   597
                    dst[4] = dst[5] = SDL_SwapFloatLE(ce);
icculus@1982
   598
                }
icculus@1982
   599
            }
icculus@1982
   600
        }
icculus@1982
   601
        break;
icculus@1982
   602
slouken@1895
   603
    }
slouken@1895
   604
    cvt->len_cvt *= 3;
slouken@1895
   605
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   606
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   607
    }
slouken@942
   608
}
slouken@942
   609
slouken@942
   610
slouken@942
   611
/* Duplicate a stereo channel to a pseudo-4.0 stream */
icculus@1982
   612
static void SDLCALL
icculus@1982
   613
SDL_ConvertSurround_4(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   614
{
slouken@1895
   615
    int i;
slouken@942
   616
slouken@942
   617
#ifdef DEBUG_CONVERT
slouken@1895
   618
    fprintf(stderr, "Converting stereo to quad\n");
slouken@942
   619
#endif
slouken@942
   620
slouken@1985
   621
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
   622
    case AUDIO_U8:
slouken@1895
   623
        {
slouken@1895
   624
            Uint8 *src, *dst, lf, rf, ce;
slouken@942
   625
slouken@1895
   626
            src = (Uint8 *) (cvt->buf + cvt->len_cvt);
slouken@1895
   627
            dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 2);
slouken@1895
   628
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   629
                dst -= 4;
slouken@1895
   630
                src -= 2;
slouken@1895
   631
                lf = src[0];
slouken@1895
   632
                rf = src[1];
slouken@1895
   633
                ce = (lf / 2) + (rf / 2);
slouken@1895
   634
                dst[0] = lf;
slouken@1895
   635
                dst[1] = rf;
slouken@1895
   636
                dst[2] = lf - ce;
slouken@1895
   637
                dst[3] = rf - ce;
slouken@1895
   638
            }
slouken@1895
   639
        }
slouken@1895
   640
        break;
slouken@942
   641
slouken@1895
   642
    case AUDIO_S8:
slouken@1895
   643
        {
slouken@1895
   644
            Sint8 *src, *dst, lf, rf, ce;
slouken@942
   645
slouken@1895
   646
            src = (Sint8 *) cvt->buf + cvt->len_cvt;
slouken@1895
   647
            dst = (Sint8 *) cvt->buf + cvt->len_cvt * 2;
slouken@1895
   648
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   649
                dst -= 4;
slouken@1895
   650
                src -= 2;
slouken@1895
   651
                lf = src[0];
slouken@1895
   652
                rf = src[1];
slouken@1895
   653
                ce = (lf / 2) + (rf / 2);
slouken@1895
   654
                dst[0] = lf;
slouken@1895
   655
                dst[1] = rf;
slouken@1895
   656
                dst[2] = lf - ce;
slouken@1895
   657
                dst[3] = rf - ce;
slouken@1895
   658
            }
slouken@1895
   659
        }
slouken@1895
   660
        break;
slouken@942
   661
slouken@1895
   662
    case AUDIO_U16:
slouken@1895
   663
        {
slouken@1895
   664
            Uint8 *src, *dst;
slouken@1895
   665
            Uint16 lf, rf, ce, lr, rr;
slouken@942
   666
slouken@1895
   667
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   668
            dst = cvt->buf + cvt->len_cvt * 2;
slouken@942
   669
icculus@1982
   670
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   671
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   672
                    dst -= 8;
slouken@1895
   673
                    src -= 4;
slouken@1895
   674
                    lf = (Uint16) ((src[0] << 8) | src[1]);
slouken@1895
   675
                    rf = (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
   676
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   677
                    rr = lf - ce;
slouken@1895
   678
                    lr = rf - ce;
slouken@1895
   679
                    dst[1] = (lf & 0xFF);
slouken@1895
   680
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   681
                    dst[3] = (rf & 0xFF);
slouken@1895
   682
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   683
slouken@1895
   684
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   685
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   686
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   687
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   688
                }
slouken@1895
   689
            } else {
slouken@1895
   690
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   691
                    dst -= 8;
slouken@1895
   692
                    src -= 4;
slouken@1895
   693
                    lf = (Uint16) ((src[1] << 8) | src[0]);
slouken@1895
   694
                    rf = (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   695
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   696
                    rr = lf - ce;
slouken@1895
   697
                    lr = rf - ce;
slouken@1895
   698
                    dst[0] = (lf & 0xFF);
slouken@1895
   699
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   700
                    dst[2] = (rf & 0xFF);
slouken@1895
   701
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   702
slouken@1895
   703
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   704
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   705
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   706
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   707
                }
slouken@1895
   708
            }
slouken@1895
   709
        }
slouken@1895
   710
        break;
slouken@942
   711
slouken@1895
   712
    case AUDIO_S16:
slouken@1895
   713
        {
slouken@1895
   714
            Uint8 *src, *dst;
slouken@1895
   715
            Sint16 lf, rf, ce, lr, rr;
slouken@942
   716
slouken@1895
   717
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   718
            dst = cvt->buf + cvt->len_cvt * 2;
slouken@942
   719
icculus@1982
   720
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   721
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   722
                    dst -= 8;
slouken@1895
   723
                    src -= 4;
slouken@1895
   724
                    lf = (Sint16) ((src[0] << 8) | src[1]);
slouken@1895
   725
                    rf = (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   726
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   727
                    rr = lf - ce;
slouken@1895
   728
                    lr = rf - ce;
slouken@1895
   729
                    dst[1] = (lf & 0xFF);
slouken@1895
   730
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   731
                    dst[3] = (rf & 0xFF);
slouken@1895
   732
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   733
slouken@1895
   734
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   735
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   736
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   737
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   738
                }
slouken@1895
   739
            } else {
slouken@1895
   740
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   741
                    dst -= 8;
slouken@1895
   742
                    src -= 4;
slouken@1895
   743
                    lf = (Sint16) ((src[1] << 8) | src[0]);
slouken@1895
   744
                    rf = (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   745
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   746
                    rr = lf - ce;
slouken@1895
   747
                    lr = rf - ce;
slouken@1895
   748
                    dst[0] = (lf & 0xFF);
slouken@1895
   749
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   750
                    dst[2] = (rf & 0xFF);
slouken@1895
   751
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   752
slouken@1895
   753
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   754
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   755
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   756
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   757
                }
slouken@1895
   758
            }
slouken@1895
   759
        }
slouken@1895
   760
        break;
slouken@942
   761
icculus@1982
   762
    case AUDIO_S32:
icculus@1982
   763
        {
icculus@1982
   764
            const Uint32 *src = (const Uint32 *) (cvt->buf + cvt->len_cvt);
icculus@1982
   765
            Uint32 *dst = (Uint32 *) (cvt->buf + cvt->len_cvt * 2);
icculus@1982
   766
            Sint32 lf, rf, ce;
slouken@942
   767
icculus@1982
   768
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   769
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   770
                    dst -= 4;
icculus@1982
   771
                    src -= 2;
icculus@1982
   772
                    lf = (Sint32) SDL_SwapBE32(src[0]);
icculus@1982
   773
                    rf = (Sint32) SDL_SwapBE32(src[1]);
icculus@1982
   774
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   775
                    dst[0] = src[0];
icculus@1982
   776
                    dst[1] = src[1];
icculus@1982
   777
                    dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
icculus@1982
   778
                    dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
icculus@1982
   779
                }
icculus@1982
   780
            } else {
icculus@1982
   781
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   782
                    dst -= 4;
icculus@1982
   783
                    src -= 2;
icculus@1982
   784
                    lf = (Sint32) SDL_SwapLE32(src[0]);
icculus@1982
   785
                    rf = (Sint32) SDL_SwapLE32(src[1]);
icculus@1982
   786
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   787
                    dst[0] = src[0];
icculus@1982
   788
                    dst[1] = src[1];
icculus@1982
   789
                    dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
icculus@1982
   790
                    dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
icculus@1982
   791
                }
icculus@1982
   792
            }
slouken@1895
   793
        }
slouken@1895
   794
        break;
slouken@1895
   795
    }
slouken@1895
   796
    cvt->len_cvt *= 2;
slouken@1895
   797
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   798
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   799
    }
slouken@0
   800
}
slouken@0
   801
icculus@1982
   802
/* Convert rate up by multiple of 2 */
icculus@1982
   803
static void SDLCALL
icculus@1982
   804
SDL_RateMUL2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@1982
   805
{
icculus@1982
   806
    int i;
icculus@1982
   807
icculus@1982
   808
#ifdef DEBUG_CONVERT
icculus@1982
   809
    fprintf(stderr, "Converting audio rate * 2 (mono)\n");
icculus@1982
   810
#endif
icculus@1982
   811
slouken@1985
   812
#define mul2_mono(type) { \
icculus@1982
   813
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   814
        type *dst = (type *) (cvt->buf + (cvt->len_cvt * 2)); \
icculus@1982
   815
        for (i = cvt->len_cvt / sizeof (type); i; --i) { \
icculus@1982
   816
            src--; \
icculus@1982
   817
            dst[-1] = dst[-2] = src[0]; \
icculus@1982
   818
            dst -= 2; \
icculus@1982
   819
        } \
icculus@1982
   820
    }
icculus@1982
   821
icculus@1982
   822
    switch (SDL_AUDIO_BITSIZE(format)) {
icculus@1982
   823
    case 8:
icculus@1982
   824
        mul2_mono(Uint8);
icculus@1982
   825
        break;
icculus@1982
   826
    case 16:
icculus@1982
   827
        mul2_mono(Uint16);
icculus@1982
   828
        break;
icculus@1982
   829
    case 32:
icculus@1982
   830
        mul2_mono(Uint32);
icculus@1982
   831
        break;
icculus@1982
   832
    }
icculus@1982
   833
slouken@1985
   834
#undef mul2_mono
icculus@1982
   835
icculus@1982
   836
    cvt->len_cvt *= 2;
icculus@1982
   837
    if (cvt->filters[++cvt->filter_index]) {
icculus@1982
   838
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   839
    }
icculus@1982
   840
}
icculus@1982
   841
slouken@942
   842
slouken@942
   843
/* Convert rate up by multiple of 2, for stereo */
icculus@1982
   844
static void SDLCALL
icculus@1982
   845
SDL_RateMUL2_c2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   846
{
slouken@1895
   847
    int i;
slouken@942
   848
slouken@942
   849
#ifdef DEBUG_CONVERT
icculus@1982
   850
    fprintf(stderr, "Converting audio rate * 2 (stereo)\n");
slouken@942
   851
#endif
icculus@1982
   852
slouken@1985
   853
#define mul2_stereo(type) { \
icculus@1982
   854
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   855
        type *dst = (type *) (cvt->buf + (cvt->len_cvt * 2)); \
icculus@1982
   856
        for (i = cvt->len_cvt / (sizeof (type) * 2); i; --i) { \
icculus@1982
   857
            const type r = src[-1]; \
icculus@1982
   858
            const type l = src[-2]; \
icculus@1982
   859
            src -= 2; \
icculus@1982
   860
            dst[-1] = r; \
icculus@1982
   861
            dst[-2] = l; \
icculus@1982
   862
            dst[-3] = r; \
icculus@1982
   863
            dst[-4] = l; \
icculus@1982
   864
            dst -= 4; \
icculus@1982
   865
        } \
icculus@1982
   866
    }
icculus@1982
   867
icculus@1982
   868
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
   869
    case 8:
icculus@1982
   870
        mul2_stereo(Uint8);
slouken@1895
   871
        break;
slouken@1895
   872
    case 16:
icculus@1982
   873
        mul2_stereo(Uint16);
icculus@1982
   874
        break;
icculus@1982
   875
    case 32:
icculus@1982
   876
        mul2_stereo(Uint32);
slouken@1895
   877
        break;
slouken@1895
   878
    }
icculus@1982
   879
slouken@1985
   880
#undef mul2_stereo
icculus@1982
   881
slouken@1895
   882
    cvt->len_cvt *= 2;
slouken@1895
   883
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   884
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   885
    }
slouken@942
   886
}
slouken@942
   887
slouken@942
   888
/* Convert rate up by multiple of 2, for quad */
icculus@1982
   889
static void SDLCALL
icculus@1982
   890
SDL_RateMUL2_c4(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   891
{
slouken@1895
   892
    int i;
slouken@942
   893
slouken@942
   894
#ifdef DEBUG_CONVERT
icculus@1982
   895
    fprintf(stderr, "Converting audio rate * 2 (quad)\n");
slouken@942
   896
#endif
icculus@1982
   897
slouken@1985
   898
#define mul2_quad(type) { \
icculus@1982
   899
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   900
        type *dst = (type *) (cvt->buf + (cvt->len_cvt * 2)); \
icculus@1982
   901
        for (i = cvt->len_cvt / (sizeof (type) * 4); i; --i) { \
icculus@1982
   902
            const type c1 = src[-1]; \
icculus@1982
   903
            const type c2 = src[-2]; \
icculus@1982
   904
            const type c3 = src[-3]; \
icculus@1982
   905
            const type c4 = src[-4]; \
icculus@1982
   906
            src -= 4; \
icculus@1982
   907
            dst[-1] = c1; \
icculus@1982
   908
            dst[-2] = c2; \
icculus@1982
   909
            dst[-3] = c3; \
icculus@1982
   910
            dst[-4] = c4; \
icculus@1982
   911
            dst[-5] = c1; \
icculus@1982
   912
            dst[-6] = c2; \
icculus@1982
   913
            dst[-7] = c3; \
icculus@1982
   914
            dst[-8] = c4; \
icculus@1982
   915
            dst -= 8; \
icculus@1982
   916
        } \
icculus@1982
   917
    }
icculus@1982
   918
icculus@1982
   919
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
   920
    case 8:
icculus@1982
   921
        mul2_quad(Uint8);
slouken@1895
   922
        break;
slouken@1895
   923
    case 16:
icculus@1982
   924
        mul2_quad(Uint16);
icculus@1982
   925
        break;
icculus@1982
   926
    case 32:
icculus@1982
   927
        mul2_quad(Uint32);
slouken@1895
   928
        break;
slouken@1895
   929
    }
icculus@1982
   930
slouken@1985
   931
#undef mul2_quad
icculus@1982
   932
slouken@1895
   933
    cvt->len_cvt *= 2;
slouken@1895
   934
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   935
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   936
    }
slouken@942
   937
}
slouken@942
   938
slouken@942
   939
slouken@942
   940
/* Convert rate up by multiple of 2, for 5.1 */
icculus@1982
   941
static void SDLCALL
icculus@1982
   942
SDL_RateMUL2_c6(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   943
{
slouken@1895
   944
    int i;
slouken@942
   945
slouken@942
   946
#ifdef DEBUG_CONVERT
icculus@1982
   947
    fprintf(stderr, "Converting audio rate * 2 (six channels)\n");
slouken@942
   948
#endif
icculus@1982
   949
slouken@1985
   950
#define mul2_chansix(type) { \
icculus@1982
   951
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   952
        type *dst = (type *) (cvt->buf + (cvt->len_cvt * 2)); \
icculus@1982
   953
        for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
icculus@1982
   954
            const type c1 = src[-1]; \
icculus@1982
   955
            const type c2 = src[-2]; \
icculus@1982
   956
            const type c3 = src[-3]; \
icculus@1982
   957
            const type c4 = src[-4]; \
icculus@1982
   958
            const type c5 = src[-5]; \
icculus@1982
   959
            const type c6 = src[-6]; \
icculus@1982
   960
            src -= 6; \
icculus@1982
   961
            dst[-1] = c1; \
icculus@1982
   962
            dst[-2] = c2; \
icculus@1982
   963
            dst[-3] = c3; \
icculus@1982
   964
            dst[-4] = c4; \
icculus@1982
   965
            dst[-5] = c5; \
icculus@1982
   966
            dst[-6] = c6; \
icculus@1982
   967
            dst[-7] = c1; \
icculus@1982
   968
            dst[-8] = c2; \
icculus@1982
   969
            dst[-9] = c3; \
icculus@1982
   970
            dst[-10] = c4; \
icculus@1982
   971
            dst[-11] = c5; \
icculus@1982
   972
            dst[-12] = c6; \
icculus@1982
   973
            dst -= 12; \
icculus@1982
   974
        } \
icculus@1982
   975
    }
icculus@1982
   976
icculus@1982
   977
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
   978
    case 8:
icculus@1982
   979
        mul2_chansix(Uint8);
slouken@1895
   980
        break;
slouken@1895
   981
    case 16:
icculus@1982
   982
        mul2_chansix(Uint16);
icculus@1982
   983
        break;
icculus@1982
   984
    case 32:
icculus@1982
   985
        mul2_chansix(Uint32);
slouken@1895
   986
        break;
slouken@1895
   987
    }
icculus@1982
   988
slouken@1985
   989
#undef mul2_chansix
icculus@1982
   990
slouken@1895
   991
    cvt->len_cvt *= 2;
slouken@1895
   992
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   993
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   994
    }
slouken@942
   995
}
slouken@942
   996
slouken@0
   997
/* Convert rate down by multiple of 2 */
icculus@1982
   998
static void SDLCALL
icculus@1982
   999
SDL_RateDIV2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
  1000
{
slouken@1895
  1001
    int i;
slouken@0
  1002
slouken@0
  1003
#ifdef DEBUG_CONVERT
icculus@1982
  1004
    fprintf(stderr, "Converting audio rate / 2 (mono)\n");
slouken@0
  1005
#endif
icculus@1982
  1006
slouken@1985
  1007
#define div2_mono(type) { \
icculus@1982
  1008
        const type *src = (const type *) cvt->buf; \
icculus@1982
  1009
        type *dst = (type *) cvt->buf; \
icculus@1982
  1010
        for (i = cvt->len_cvt / (sizeof (type) * 2); i; --i) { \
icculus@1982
  1011
            dst[0] = src[0]; \
icculus@1982
  1012
            src += 2; \
icculus@1982
  1013
            dst++; \
icculus@1982
  1014
        } \
icculus@1982
  1015
    }
icculus@1982
  1016
icculus@1982
  1017
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1018
    case 8:
icculus@1982
  1019
        div2_mono(Uint8);
slouken@1895
  1020
        break;
slouken@1895
  1021
    case 16:
icculus@1982
  1022
        div2_mono(Uint16);
icculus@1982
  1023
        break;
icculus@1982
  1024
    case 32:
icculus@1982
  1025
        div2_mono(Uint32);
slouken@1895
  1026
        break;
slouken@1895
  1027
    }
icculus@1982
  1028
slouken@1985
  1029
#undef div2_mono
icculus@1982
  1030
slouken@1895
  1031
    cvt->len_cvt /= 2;
slouken@1895
  1032
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
  1033
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
  1034
    }
slouken@0
  1035
}
slouken@0
  1036
slouken@942
  1037
slouken@942
  1038
/* Convert rate down by multiple of 2, for stereo */
icculus@1982
  1039
static void SDLCALL
icculus@1982
  1040
SDL_RateDIV2_c2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
  1041
{
slouken@1895
  1042
    int i;
slouken@942
  1043
slouken@942
  1044
#ifdef DEBUG_CONVERT
icculus@1982
  1045
    fprintf(stderr, "Converting audio rate / 2 (stereo)\n");
slouken@942
  1046
#endif
icculus@1982
  1047
slouken@1985
  1048
#define div2_stereo(type) { \
icculus@1982
  1049
        const type *src = (const type *) cvt->buf; \
icculus@1982
  1050
        type *dst = (type *) cvt->buf; \
icculus@1982
  1051
        for (i = cvt->len_cvt / (sizeof (type) * 4); i; --i) { \
icculus@1982
  1052
            dst[0] = src[0]; \
icculus@1982
  1053
            dst[1] = src[1]; \
icculus@1982
  1054
            src += 4; \
icculus@1982
  1055
            dst += 2; \
icculus@1982
  1056
        } \
icculus@1982
  1057
    }
icculus@1982
  1058
icculus@1982
  1059
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1060
    case 8:
icculus@1982
  1061
        div2_stereo(Uint8);
slouken@1895
  1062
        break;
slouken@1895
  1063
    case 16:
icculus@1982
  1064
        div2_stereo(Uint16);
icculus@1982
  1065
        break;
icculus@1982
  1066
    case 32:
icculus@1982
  1067
        div2_stereo(Uint32);
slouken@1895
  1068
        break;
slouken@1895
  1069
    }
icculus@1982
  1070
slouken@1985
  1071
#undef div2_stereo
icculus@1982
  1072
slouken@1895
  1073
    cvt->len_cvt /= 2;
slouken@1895
  1074
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
  1075
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
  1076
    }
slouken@942
  1077
}
slouken@942
  1078
slouken@942
  1079
slouken@942
  1080
/* Convert rate down by multiple of 2, for quad */
icculus@1982
  1081
static void SDLCALL
icculus@1982
  1082
SDL_RateDIV2_c4(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
  1083
{
slouken@1895
  1084
    int i;
slouken@942
  1085
slouken@942
  1086
#ifdef DEBUG_CONVERT
icculus@1982
  1087
    fprintf(stderr, "Converting audio rate / 2 (quad)\n");
slouken@942
  1088
#endif
icculus@1982
  1089
slouken@1985
  1090
#define div2_quad(type) { \
icculus@1982
  1091
        const type *src = (const type *) cvt->buf; \
icculus@1982
  1092
        type *dst = (type *) cvt->buf; \
icculus@1982
  1093
        for (i = cvt->len_cvt / (sizeof (type) * 8); i; --i) { \
icculus@1982
  1094
            dst[0] = src[0]; \
icculus@1982
  1095
            dst[1] = src[1]; \
icculus@1982
  1096
            dst[2] = src[2]; \
icculus@1982
  1097
            dst[3] = src[3]; \
icculus@1982
  1098
            src += 8; \
icculus@1982
  1099
            dst += 4; \
icculus@1982
  1100
        } \
icculus@1982
  1101
    }
icculus@1982
  1102
icculus@1982
  1103
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1104
    case 8:
icculus@1982
  1105
        div2_quad(Uint8);
slouken@1895
  1106
        break;
slouken@1895
  1107
    case 16:
icculus@1982
  1108
        div2_quad(Uint16);
icculus@1982
  1109
        break;
icculus@1982
  1110
    case 32:
icculus@1982
  1111
        div2_quad(Uint32);
slouken@1895
  1112
        break;
slouken@1895
  1113
    }
icculus@1982
  1114
slouken@1985
  1115
#undef div2_quad
icculus@1982
  1116
slouken@1895
  1117
    cvt->len_cvt /= 2;
slouken@1895
  1118
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
  1119
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
  1120
    }
slouken@942
  1121
}
slouken@942
  1122
slouken@942
  1123
/* Convert rate down by multiple of 2, for 5.1 */
icculus@1982
  1124
static void SDLCALL
icculus@1982
  1125
SDL_RateDIV2_c6(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
  1126
{
slouken@1895
  1127
    int i;
slouken@942
  1128
slouken@942
  1129
#ifdef DEBUG_CONVERT
icculus@1982
  1130
    fprintf(stderr, "Converting audio rate / 2 (six channels)\n");
slouken@942
  1131
#endif
icculus@1982
  1132
slouken@1985
  1133
#define div2_chansix(type) { \
icculus@1982
  1134
        const type *src = (const type *) cvt->buf; \
icculus@1982
  1135
        type *dst = (type *) cvt->buf; \
icculus@1982
  1136
        for (i = cvt->len_cvt / (sizeof (type) * 12); i; --i) { \
icculus@1982
  1137
            dst[0] = src[0]; \
icculus@1982
  1138
            dst[1] = src[1]; \
icculus@1982
  1139
            dst[2] = src[2]; \
icculus@1982
  1140
            dst[3] = src[3]; \
icculus@1982
  1141
            dst[4] = src[4]; \
icculus@1982
  1142
            dst[5] = src[5]; \
icculus@1982
  1143
            src += 12; \
icculus@1982
  1144
            dst += 6; \
icculus@1982
  1145
        } \
icculus@1982
  1146
    }
icculus@1982
  1147
icculus@1982
  1148
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1149
    case 8:
icculus@1982
  1150
        div2_chansix(Uint8);
slouken@1895
  1151
        break;
slouken@1895
  1152
    case 16:
icculus@1982
  1153
        div2_chansix(Uint16);
icculus@1982
  1154
        break;
icculus@1982
  1155
    case 32:
icculus@1982
  1156
        div2_chansix(Uint32);
slouken@1895
  1157
        break;
slouken@1895
  1158
    }
icculus@1982
  1159
slouken@1985
  1160
#undef div_chansix
icculus@1982
  1161
slouken@1895
  1162
    cvt->len_cvt /= 2;
slouken@1895
  1163
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
  1164
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
  1165
    }
slouken@942
  1166
}
slouken@942
  1167
slouken@0
  1168
/* Very slow rate conversion routine */
icculus@1982
  1169
static void SDLCALL
icculus@1982
  1170
SDL_RateSLOW(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
  1171
{
slouken@1895
  1172
    double ipos;
slouken@1895
  1173
    int i, clen;
slouken@0
  1174
slouken@0
  1175
#ifdef DEBUG_CONVERT
slouken@1895
  1176
    fprintf(stderr, "Converting audio rate * %4.4f\n", 1.0 / cvt->rate_incr);
slouken@0
  1177
#endif
slouken@1895
  1178
    clen = (int) ((double) cvt->len_cvt / cvt->rate_incr);
slouken@1895
  1179
    if (cvt->rate_incr > 1.0) {
icculus@1982
  1180
        switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1181
        case 8:
slouken@1895
  1182
            {
slouken@1895
  1183
                Uint8 *output;
slouken@0
  1184
slouken@1895
  1185
                output = cvt->buf;
slouken@1895
  1186
                ipos = 0.0;
slouken@1895
  1187
                for (i = clen; i; --i) {
slouken@1895
  1188
                    *output = cvt->buf[(int) ipos];
slouken@1895
  1189
                    ipos += cvt->rate_incr;
slouken@1895
  1190
                    output += 1;
slouken@1895
  1191
                }
slouken@1895
  1192
            }
slouken@1895
  1193
            break;
slouken@0
  1194
slouken@1895
  1195
        case 16:
slouken@1895
  1196
            {
slouken@1895
  1197
                Uint16 *output;
slouken@0
  1198
slouken@1895
  1199
                clen &= ~1;
slouken@1895
  1200
                output = (Uint16 *) cvt->buf;
slouken@1895
  1201
                ipos = 0.0;
slouken@1895
  1202
                for (i = clen / 2; i; --i) {
slouken@1895
  1203
                    *output = ((Uint16 *) cvt->buf)[(int) ipos];
slouken@1895
  1204
                    ipos += cvt->rate_incr;
slouken@1895
  1205
                    output += 1;
slouken@1895
  1206
                }
slouken@1895
  1207
            }
slouken@1895
  1208
            break;
icculus@1982
  1209
icculus@1982
  1210
        case 32:
icculus@1982
  1211
            {
icculus@1982
  1212
                /* !!! FIXME: need 32-bit converter here! */
slouken@2130
  1213
#ifdef DEBUG_CONVERT
icculus@1982
  1214
                fprintf(stderr, "FIXME: need 32-bit converter here!\n");
slouken@2130
  1215
#endif
icculus@1982
  1216
            }
slouken@1895
  1217
        }
slouken@1895
  1218
    } else {
icculus@1982
  1219
        switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1220
        case 8:
slouken@1895
  1221
            {
slouken@1895
  1222
                Uint8 *output;
slouken@0
  1223
slouken@1895
  1224
                output = cvt->buf + clen;
slouken@1895
  1225
                ipos = (double) cvt->len_cvt;
slouken@1895
  1226
                for (i = clen; i; --i) {
slouken@1895
  1227
                    ipos -= cvt->rate_incr;
slouken@1895
  1228
                    output -= 1;
slouken@1895
  1229
                    *output = cvt->buf[(int) ipos];
slouken@1895
  1230
                }
slouken@1895
  1231
            }
slouken@1895
  1232
            break;
slouken@0
  1233
slouken@1895
  1234
        case 16:
slouken@1895
  1235
            {
slouken@1895
  1236
                Uint16 *output;
slouken@0
  1237
slouken@1895
  1238
                clen &= ~1;
slouken@1895
  1239
                output = (Uint16 *) (cvt->buf + clen);
slouken@1895
  1240
                ipos = (double) cvt->len_cvt / 2;
slouken@1895
  1241
                for (i = clen / 2; i; --i) {
slouken@1895
  1242
                    ipos -= cvt->rate_incr;
slouken@1895
  1243
                    output -= 1;
slouken@1895
  1244
                    *output = ((Uint16 *) cvt->buf)[(int) ipos];
slouken@1895
  1245
                }
slouken@1895
  1246
            }
slouken@1895
  1247
            break;
icculus@1982
  1248
icculus@1982
  1249
        case 32:
icculus@1982
  1250
            {
icculus@1982
  1251
                /* !!! FIXME: need 32-bit converter here! */
slouken@2130
  1252
#ifdef DEBUG_CONVERT
icculus@1982
  1253
                fprintf(stderr, "FIXME: need 32-bit converter here!\n");
slouken@2130
  1254
#endif
icculus@1982
  1255
            }
slouken@1895
  1256
        }
slouken@1895
  1257
    }
icculus@1982
  1258
slouken@1895
  1259
    cvt->len_cvt = clen;
slouken@1895
  1260
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
  1261
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
  1262
    }
slouken@0
  1263
}
slouken@0
  1264
slouken@1895
  1265
int
slouken@1895
  1266
SDL_ConvertAudio(SDL_AudioCVT * cvt)
slouken@0
  1267
{
slouken@1895
  1268
    /* Make sure there's data to convert */
slouken@1895
  1269
    if (cvt->buf == NULL) {
slouken@1895
  1270
        SDL_SetError("No buffer allocated for conversion");
slouken@1895
  1271
        return (-1);
slouken@1895
  1272
    }
slouken@1895
  1273
    /* Return okay if no conversion is necessary */
slouken@1895
  1274
    cvt->len_cvt = cvt->len;
slouken@1895
  1275
    if (cvt->filters[0] == NULL) {
slouken@1895
  1276
        return (0);
slouken@1895
  1277
    }
slouken@0
  1278
slouken@1895
  1279
    /* Set up the conversion and go! */
slouken@1895
  1280
    cvt->filter_index = 0;
slouken@1895
  1281
    cvt->filters[0] (cvt, cvt->src_format);
slouken@1895
  1282
    return (0);
slouken@0
  1283
}
slouken@0
  1284
icculus@1982
  1285
icculus@1982
  1286
static SDL_AudioFilter
icculus@1982
  1287
SDL_HandTunedTypeCVT(SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
icculus@1982
  1288
{
icculus@1982
  1289
    /*
icculus@1982
  1290
     * Fill in any future conversions that are specialized to a
icculus@1982
  1291
     *  processor, platform, compiler, or library here.
icculus@1982
  1292
     */
icculus@1982
  1293
slouken@1985
  1294
    return NULL;                /* no specialized converter code available. */
icculus@1982
  1295
}
icculus@1982
  1296
icculus@1982
  1297
icculus@1982
  1298
/*
icculus@1982
  1299
 * Find a converter between two data types. We try to select a hand-tuned
icculus@1982
  1300
 *  asm/vectorized/optimized function first, and then fallback to an
icculus@1982
  1301
 *  autogenerated function that is customized to convert between two
icculus@1982
  1302
 *  specific data types.
icculus@1982
  1303
 */
icculus@1982
  1304
static int
icculus@1982
  1305
SDL_BuildAudioTypeCVT(SDL_AudioCVT * cvt,
icculus@1982
  1306
                      SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
icculus@1982
  1307
{
icculus@1982
  1308
    if (src_fmt != dst_fmt) {
icculus@1982
  1309
        const Uint16 src_bitsize = SDL_AUDIO_BITSIZE(src_fmt);
icculus@1982
  1310
        const Uint16 dst_bitsize = SDL_AUDIO_BITSIZE(dst_fmt);
icculus@1982
  1311
        SDL_AudioFilter filter = SDL_HandTunedTypeCVT(src_fmt, dst_fmt);
icculus@1982
  1312
icculus@1982
  1313
        /* No hand-tuned converter? Try the autogenerated ones. */
icculus@1982
  1314
        if (filter == NULL) {
icculus@1982
  1315
            int i;
icculus@1982
  1316
            for (i = 0; sdl_audio_type_filters[i].filter != NULL; i++) {
icculus@1982
  1317
                const SDL_AudioTypeFilters *filt = &sdl_audio_type_filters[i];
icculus@1982
  1318
                if ((filt->src_fmt == src_fmt) && (filt->dst_fmt == dst_fmt)) {
icculus@1982
  1319
                    filter = filt->filter;
icculus@1982
  1320
                    break;
icculus@1982
  1321
                }
icculus@1982
  1322
            }
icculus@1982
  1323
icculus@1982
  1324
            if (filter == NULL) {
slouken@1985
  1325
                return -1;      /* Still no matching converter?! */
icculus@1982
  1326
            }
icculus@1982
  1327
        }
icculus@1982
  1328
icculus@1982
  1329
        /* Update (cvt) with filter details... */
icculus@1982
  1330
        cvt->filters[cvt->filter_index++] = filter;
icculus@1982
  1331
        if (src_bitsize < dst_bitsize) {
icculus@1982
  1332
            const int mult = (dst_bitsize / src_bitsize);
icculus@1982
  1333
            cvt->len_mult *= mult;
icculus@1982
  1334
            cvt->len_ratio *= mult;
icculus@1982
  1335
        } else if (src_bitsize > dst_bitsize) {
icculus@1982
  1336
            cvt->len_ratio /= (src_bitsize / dst_bitsize);
icculus@1982
  1337
        }
icculus@1982
  1338
slouken@1985
  1339
        return 1;               /* added a converter. */
icculus@1982
  1340
    }
icculus@1982
  1341
slouken@1985
  1342
    return 0;                   /* no conversion necessary. */
icculus@1982
  1343
}
icculus@1982
  1344
schnarf@2655
  1345
/* Generate the necessary IIR lowpass coefficients for resampling.
schnarf@2655
  1346
   Assume that the SDL_AudioCVT struct is already set up with
schnarf@2655
  1347
   the correct values for len_mult and len_div, and use the
schnarf@2655
  1348
   type of dst_format. Also assume the buffer is allocated.
schnarf@2655
  1349
   Note the buffer needs to be 6 units long.
schnarf@2655
  1350
   For now, use RBJ's cookbook coefficients. It might be more
schnarf@2655
  1351
   optimal to create a Butterworth filter, but this is more difficult.
schnarf@2655
  1352
*/
schnarf@2655
  1353
int SDL_BuildIIRLowpass(SDL_AudioCVT * cvt, SDL_AudioFormat format) {
schnarf@2655
  1354
	float fc;			/* cutoff frequency */
schnarf@2655
  1355
	float coeff[6];		/* floating point iir coefficients b0, b1, b2, a0, a1, a2 */
schnarf@2655
  1356
	float scale;
schnarf@2655
  1357
	float w0, alpha, cosw0;
schnarf@2657
  1358
	int i;
schnarf@2655
  1359
	
schnarf@2655
  1360
	/* The higher Q is, the higher CUTOFF can be. Need to find a good balance to avoid aliasing */
schnarf@2655
  1361
	static const float Q = 5.0f;
schnarf@2655
  1362
	static const float CUTOFF = 0.4f;
schnarf@2655
  1363
	
schnarf@2655
  1364
	fc = (cvt->len_mult > cvt->len_div) ? CUTOFF / (float)cvt->len_mult : CUTOFF / (float)cvt->len_div;
schnarf@2655
  1365
	
schnarf@2655
  1366
	w0 = 2.0f * M_PI * fc;
schnarf@2655
  1367
	cosw0 = cosf(w0);
schnarf@2655
  1368
	alpha = sin(w0) / (2.0f * Q);
schnarf@2655
  1369
	
schnarf@2655
  1370
	/* Compute coefficients, normalizing by a0 */
schnarf@2655
  1371
	scale = 1.0f / (1.0f + alpha);
schnarf@2655
  1372
	
schnarf@2655
  1373
	coeff[0] = (1.0f - cosw0) / 2.0f * scale;
schnarf@2655
  1374
	coeff[1] = (1.0f - cosw0) * scale;
schnarf@2655
  1375
	coeff[2] = coeff[0];
schnarf@2655
  1376
	
schnarf@2655
  1377
	coeff[3] = 1.0f;	/* a0 is normalized to 1 */
schnarf@2655
  1378
	coeff[4] = -2.0f * cosw0 * scale;
schnarf@2655
  1379
	coeff[5] = (1.0f - alpha) * scale;
schnarf@2655
  1380
	
schnarf@2656
  1381
	/* Copy the coefficients to the struct. If necessary, convert coefficients to fixed point, using the range (-2.0, 2.0) */
schnarf@2657
  1382
#define convert_fixed(type, fix) { \
schnarf@2657
  1383
			type *cvt_coeff = (type *)cvt->coeff; \
schnarf@2657
  1384
			for(i = 0; i < 6; ++i) { \
schnarf@2657
  1385
				cvt_coeff[i] = fix(coeff[i]); \
schnarf@2657
  1386
			} \
schnarf@2657
  1387
		}
schnarf@2657
  1388
		
schnarf@2656
  1389
	if(SDL_AUDIO_ISFLOAT(format) && SDL_AUDIO_BITSIZE(format) == 32) {
schnarf@2656
  1390
		float *cvt_coeff = (float *)cvt->coeff;
schnarf@2656
  1391
		for(i = 0; i < 6; ++i) {
schnarf@2656
  1392
			cvt_coeff[i] = coeff[i];
schnarf@2656
  1393
		}
schnarf@2656
  1394
	} else {
schnarf@2657
  1395
		switch(SDL_AUDIO_BITSIZE(format)) {
schnarf@2657
  1396
			case 8:
schnarf@2657
  1397
				convert_fixed(Uint8, SDL_Make_2_6);
schnarf@2657
  1398
				break;
schnarf@2657
  1399
			case 16:
schnarf@2657
  1400
				convert_fixed(Uint16, SDL_Make_2_14);
schnarf@2657
  1401
				break;
schnarf@2657
  1402
			case 32:
schnarf@2657
  1403
				convert_fixed(Uint32, SDL_Make_2_30);
schnarf@2657
  1404
				break;
schnarf@2657
  1405
		}
schnarf@2656
  1406
	}
schnarf@2655
  1407
	
schnarf@2657
  1408
#ifdef DEBUG_CONVERT
schnarf@2657
  1409
#define debug_iir(type) { \
schnarf@2657
  1410
			type *cvt_coeff = (type *)cvt->coeff; \
schnarf@2657
  1411
			for(i = 0; i < 6; ++i) { \
schnarf@2657
  1412
				printf("coeff[%u] = %f = 0x%x\n", i, coeff[i], cvt_coeff[i]); \
schnarf@2657
  1413
			} \
schnarf@2657
  1414
		}
schnarf@2657
  1415
		if(SDL_AUDIO_ISFLOAT(format) && SDL_AUDIO_BITSIZE(format) == 32) {
schnarf@2657
  1416
			float *cvt_coeff = (float *)cvt->coeff;
schnarf@2657
  1417
			for(i = 0; i < 6; ++i) { \
schnarf@2657
  1418
				printf("coeff[%u] = %f = %f\n", i, coeff[i], cvt_coeff[i]); \
schnarf@2657
  1419
			}
schnarf@2657
  1420
		} else {
schnarf@2657
  1421
			switch(SDL_AUDIO_BITSIZE(format)) {
schnarf@2657
  1422
				case 8:
schnarf@2657
  1423
					debug_iir(Uint8);
schnarf@2657
  1424
					break;
schnarf@2657
  1425
				case 16:
schnarf@2657
  1426
					debug_iir(Uint16);
schnarf@2657
  1427
					break;
schnarf@2657
  1428
				case 32:
schnarf@2657
  1429
					debug_iir(Uint32);
schnarf@2657
  1430
					break;
schnarf@2657
  1431
			}
schnarf@2657
  1432
		}
schnarf@2657
  1433
#undef debug_iir
schnarf@2657
  1434
#endif
schnarf@2657
  1435
	
schnarf@2655
  1436
	/* Initialize the state buffer to all zeroes, and set initial position */
schnarf@2655
  1437
	memset(cvt->state_buf, 0, 4 * SDL_AUDIO_BITSIZE(format) / 4);
schnarf@2655
  1438
	cvt->state_pos = 0;
schnarf@2657
  1439
#undef convert_fixed
schnarf@2655
  1440
}
schnarf@2655
  1441
schnarf@2656
  1442
/* Apply the lowpass IIR filter to the given SDL_AudioCVT struct */
schnarf@2663
  1443
/* This was implemented because it would be much faster than the fir filter, 
schnarf@2663
  1444
   but it doesn't seem to have a steep enough cutoff so we'd need several
schnarf@2663
  1445
   cascaded biquads, which probably isn't a great idea. Therefore, this
schnarf@2663
  1446
   function can probably be discarded.
schnarf@2663
  1447
*/
schnarf@2657
  1448
static void SDL_FilterIIR(SDL_AudioCVT * cvt, SDL_AudioFormat format) {
schnarf@2658
  1449
	Uint32 i, n;
schnarf@2656
  1450
	
schnarf@2658
  1451
	/* TODO: Check that n is calculated right */
schnarf@2658
  1452
	n = 8 * cvt->len_cvt / SDL_AUDIO_BITSIZE(format);
schnarf@2657
  1453
schnarf@2658
  1454
	/* Note that the coefficients are 2_x and the input is 1_x. Do we need to shift left at the end here? The right shift temp = buf[n] >> 1 needs to depend on whether the type is signed or not for sign extension.*/
schnarf@2658
  1455
	/* cvt->state_pos = 1: state[0] = x_n-1, state[1] = x_n-2, state[2] = y_n-1, state[3] - y_n-2 */
schnarf@2657
  1456
#define iir_fix(type, mult) {\
schnarf@2657
  1457
			type *coeff = (type *)cvt->coeff; \
schnarf@2657
  1458
			type *state = (type *)cvt->state_buf; \
schnarf@2657
  1459
			type *buf = (type *)cvt->buf; \
schnarf@2657
  1460
			type temp; \
schnarf@2657
  1461
			for(i = 0; i < n; ++i) { \
schnarf@2658
  1462
					temp = buf[i] >> 1; \
schnarf@2657
  1463
					if(cvt->state_pos) { \
schnarf@2658
  1464
						buf[i] = mult(coeff[0], temp) + mult(coeff[1], state[0]) + mult(coeff[2], state[1]) - mult(coeff[4], state[2]) - mult(coeff[5], state[3]); \
schnarf@2657
  1465
						state[1] = temp; \
schnarf@2658
  1466
						state[3] = buf[i]; \
schnarf@2657
  1467
						cvt->state_pos = 0; \
schnarf@2657
  1468
					} else { \
schnarf@2658
  1469
						buf[i] = mult(coeff[0], temp) + mult(coeff[1], state[1]) + mult(coeff[2], state[0]) - mult(coeff[4], state[3]) - mult(coeff[5], state[2]); \
schnarf@2657
  1470
						state[0] = temp; \
schnarf@2658
  1471
						state[2] = buf[i]; \
schnarf@2658
  1472
						cvt->state_pos = 1; \
schnarf@2657
  1473
					} \
schnarf@2657
  1474
				} \
schnarf@2657
  1475
		}
schnarf@2658
  1476
/* Need to test to see if the previous method or this one is faster */
schnarf@2658
  1477
/*#define iir_fix(type, mult) {\
schnarf@2658
  1478
			type *coeff = (type *)cvt->coeff; \
schnarf@2658
  1479
			type *state = (type *)cvt->state_buf; \
schnarf@2658
  1480
			type *buf = (type *)cvt->buf; \
schnarf@2658
  1481
			type temp; \
schnarf@2658
  1482
			for(i = 0; i < n; ++i) { \
schnarf@2658
  1483
					temp = buf[i] >> 1; \
schnarf@2658
  1484
					buf[i] = mult(coeff[0], temp) + mult(coeff[1], state[0]) + mult(coeff[2], state[1]) - mult(coeff[4], state[2]) - mult(coeff[5], state[3]); \
schnarf@2658
  1485
					state[1] = state[0]; \
schnarf@2658
  1486
					state[0] = temp; \
schnarf@2658
  1487
					state[3] = state[2]; \
schnarf@2658
  1488
					state[2] = buf[i]; \
schnarf@2658
  1489
				} \
schnarf@2658
  1490
		}*/
schnarf@2657
  1491
schnarf@2656
  1492
	if(SDL_AUDIO_ISFLOAT(format) && SDL_AUDIO_BITSIZE(format) == 32) {
schnarf@2656
  1493
		float *coeff = (float *)cvt->coeff;
schnarf@2656
  1494
		float *state = (float *)cvt->state_buf;
schnarf@2656
  1495
		float *buf = (float *)cvt->buf;
schnarf@2656
  1496
		float temp;
schnarf@2656
  1497
schnarf@2656
  1498
		for(i = 0; i < n; ++i) {
schnarf@2656
  1499
			/* y[n] = b0 * x[n] + b1 * x[n-1] + b2 * x[n-2] - a1 * y[n-1] - a[2] * y[n-2] */
schnarf@2658
  1500
			temp = buf[i];
schnarf@2657
  1501
			if(cvt->state_pos) {
schnarf@2658
  1502
				buf[i] = coeff[0] * buf[n] + coeff[1] * state[0] + coeff[2] * state[1] - coeff[4] * state[2] - coeff[5] * state[3];
schnarf@2656
  1503
				state[1] = temp;
schnarf@2658
  1504
				state[3] = buf[i];
schnarf@2656
  1505
				cvt->state_pos = 0;
schnarf@2656
  1506
			} else {
schnarf@2658
  1507
				buf[i] = coeff[0] * buf[n] + coeff[1] * state[1] + coeff[2] * state[0] - coeff[4] * state[3] - coeff[5] * state[2];
schnarf@2656
  1508
				state[0] = temp;
schnarf@2658
  1509
				state[2] = buf[i];
schnarf@2656
  1510
				cvt->state_pos = 1;
schnarf@2656
  1511
			}
schnarf@2656
  1512
		}
schnarf@2656
  1513
	} else {
schnarf@2658
  1514
		/* Treat everything as signed! */
schnarf@2657
  1515
		switch(SDL_AUDIO_BITSIZE(format)) {
schnarf@2657
  1516
			case 8:
schnarf@2658
  1517
				iir_fix(Sint8, SDL_FixMpy8);
schnarf@2657
  1518
				break;
schnarf@2657
  1519
			case 16:
schnarf@2658
  1520
				iir_fix(Sint16, SDL_FixMpy16);
schnarf@2657
  1521
				break;
schnarf@2657
  1522
			case 32:
schnarf@2658
  1523
				iir_fix(Sint32, SDL_FixMpy32);
schnarf@2657
  1524
				break;
schnarf@2657
  1525
		}
schnarf@2656
  1526
	}
schnarf@2657
  1527
#undef iir_fix
schnarf@2656
  1528
}
schnarf@2656
  1529
schnarf@2663
  1530
/* Apply the windowed sinc FIR filter to the given SDL_AudioCVT struct.
schnarf@2663
  1531
*/
schnarf@2657
  1532
static void SDL_FilterFIR(SDL_AudioCVT * cvt, SDL_AudioFormat format) {
schnarf@2659
  1533
	int n = 8 * cvt->len_cvt / SDL_AUDIO_BITSIZE(format);
schnarf@2655
  1534
	int m = cvt->len_sinc;
schnarf@2655
  1535
	int i, j;
schnarf@2659
  1536
				
schnarf@2661
  1537
	/* 
schnarf@2661
  1538
	   Note: We can make a big optimization here by taking advantage
schnarf@2655
  1539
	   of the fact that the signal is zero stuffed, so we can do
schnarf@2661
  1540
	   significantly fewer multiplications and additions. However, this
schnarf@2663
  1541
	   depends on the zero stuffing ratio, so it may not pay off. This would
schnarf@2663
  1542
	   basically be a polyphase filter.
schnarf@2655
  1543
	*/
schnarf@2663
  1544
	/* One other way to do this fast is to look at the fir filter from a different angle:
schnarf@2663
  1545
	   After we zero stuff, we have input of all zeroes, except for every len_mult
schnarf@2663
  1546
	   sample. If we choose a sinc length equal to len_mult, then the fir filter becomes
schnarf@2663
  1547
	   much more simple: we're just taking a windowed sinc, shifting it to start at each
schnarf@2663
  1548
	   len_mult sample, and scaling it by the value of that sample. If we do this, then
schnarf@2663
  1549
	   we don't even need to worry about the sample histories, and the inner loop here is
schnarf@2663
  1550
	   unnecessary. This probably sacrifices some quality but could really speed things up as well.
schnarf@2663
  1551
	*/
schnarf@2663
  1552
	/* We only calculate the values of samples which are 0 (mod len_div) because
schnarf@2663
  1553
	   those are the only ones used. All the other ones are discarded in the
schnarf@2663
  1554
	   third step of resampling. This is a huge speedup. As a warning, though,
schnarf@2663
  1555
	   if for some reason this is used elsewhere where there are no samples discarded,
schnarf@2663
  1556
	   the output will not be corrrect if len_div is not 1. To make this filter a
schnarf@2663
  1557
	   generic FIR filter, simply remove the if statement "if(i % cvt->len_div == 0)"
schnarf@2663
  1558
	   around the inner loop so that every sample is processed.
schnarf@2663
  1559
	*/
schnarf@2663
  1560
	/* This is basically just a FIR filter. i.e. for input x_n and m coefficients,
schnarf@2663
  1561
	   y_n = x_n*sinc_0 + x_(n-1)*sinc_1 +  x_(n-2)*sinc_2 + ... + x_(n-m+1)*sinc_(m-1)
schnarf@2663
  1562
	*/
schnarf@2657
  1563
#define filter_sinc(type, mult) { \
schnarf@2656
  1564
			type *sinc = (type *)cvt->coeff; \
schnarf@2655
  1565
			type *state = (type *)cvt->state_buf; \
schnarf@2655
  1566
			type *buf = (type *)cvt->buf; \
schnarf@2655
  1567
			for(i = 0; i < n; ++i) { \
schnarf@2659
  1568
				state[cvt->state_pos] = buf[i]; \
schnarf@2655
  1569
				buf[i] = 0; \
schnarf@2662
  1570
				if( i % cvt->len_div == 0 ) { \
schnarf@2662
  1571
					for(j = 0; j < m;  ++j) { \
schnarf@2662
  1572
						buf[i] += mult(sinc[j], state[(cvt->state_pos + j) % m]); \
schnarf@2662
  1573
					} \
schnarf@2662
  1574
				}\
schnarf@2661
  1575
				cvt->state_pos = (cvt->state_pos + 1) % m; \
schnarf@2655
  1576
			} \
schnarf@2655
  1577
		}
schnarf@2656
  1578
	
schnarf@2656
  1579
	if(SDL_AUDIO_ISFLOAT(format) && SDL_AUDIO_BITSIZE(format) == 32) {
schnarf@2663
  1580
		filter_sinc(float, SDL_FloatMpy);
schnarf@2656
  1581
	} else {
schnarf@2656
  1582
		switch (SDL_AUDIO_BITSIZE(format)) {
schnarf@2656
  1583
			case 8:
schnarf@2658
  1584
				filter_sinc(Sint8, SDL_FixMpy8);
schnarf@2656
  1585
				break;
schnarf@2656
  1586
			case 16:
schnarf@2658
  1587
				filter_sinc(Sint16, SDL_FixMpy16);
schnarf@2656
  1588
				break;
schnarf@2656
  1589
			case 32:
schnarf@2658
  1590
				filter_sinc(Sint32, SDL_FixMpy32);
schnarf@2656
  1591
				break;
schnarf@2656
  1592
		}
schnarf@2655
  1593
	}
schnarf@2655
  1594
	
schnarf@2655
  1595
#undef filter_sinc
schnarf@2655
  1596
			
schnarf@2655
  1597
}
schnarf@2655
  1598
schnarf@2655
  1599
/* Generate the necessary windowed sinc filter for resampling.
schnarf@2655
  1600
   Assume that the SDL_AudioCVT struct is already set up with
schnarf@2655
  1601
   the correct values for len_mult and len_div, and use the
schnarf@2655
  1602
   type of dst_format. Also assume the buffer is allocated.
schnarf@2655
  1603
   Note the buffer needs to be m+1 units long.
schnarf@2655
  1604
*/
schnarf@2655
  1605
int
schnarf@2655
  1606
SDL_BuildWindowedSinc(SDL_AudioCVT * cvt, SDL_AudioFormat format, unsigned int m) {
schnarf@2655
  1607
	float fScale;		/* scale factor for fixed point */
schnarf@2655
  1608
	float *fSinc;		/* floating point sinc buffer, to be converted to fixed point */
schnarf@2655
  1609
	float fc;			/* cutoff frequency */
schnarf@2655
  1610
	float two_pi_fc, two_pi_over_m, four_pi_over_m, m_over_two;
schnarf@2655
  1611
	float norm_sum, norm_fact;
schnarf@2655
  1612
	unsigned int i;
schnarf@2655
  1613
schnarf@2655
  1614
	/* Check that the buffer is allocated */
schnarf@2656
  1615
	if( cvt->coeff == NULL ) {
schnarf@2655
  1616
		return -1;
schnarf@2655
  1617
	}
schnarf@2655
  1618
schnarf@2655
  1619
	/* Set the length */
schnarf@2659
  1620
	cvt->len_sinc = m + 1;
schnarf@2655
  1621
	
schnarf@2655
  1622
	/* Allocate the floating point windowed sinc. */
schnarf@2663
  1623
	fSinc = (float *)malloc((m + 1) * sizeof(float));
schnarf@2655
  1624
	if( fSinc == NULL ) {
schnarf@2655
  1625
		return -1;
schnarf@2655
  1626
	}
schnarf@2655
  1627
	
schnarf@2655
  1628
	/* Set up the filter parameters */
schnarf@2655
  1629
	fc = (cvt->len_mult > cvt->len_div) ? 0.5f / (float)cvt->len_mult : 0.5f / (float)cvt->len_div;
schnarf@2661
  1630
#ifdef DEBUG_CONVERT
schnarf@2661
  1631
	printf("Lowpass cutoff frequency = %f\n", fc);
schnarf@2661
  1632
#endif
schnarf@2655
  1633
	two_pi_fc = 2.0f * M_PI * fc;
schnarf@2655
  1634
	two_pi_over_m = 2.0f * M_PI / (float)m;
schnarf@2655
  1635
	four_pi_over_m = 2.0f * two_pi_over_m;
schnarf@2655
  1636
	m_over_two = (float)m / 2.0f;
schnarf@2655
  1637
	norm_sum = 0.0f;
schnarf@2655
  1638
	
schnarf@2655
  1639
	for(i = 0; i <= m; ++i ) {
schnarf@2655
  1640
		if( i == m/2 ) {
schnarf@2655
  1641
			fSinc[i] = two_pi_fc;
schnarf@2655
  1642
		} else {
schnarf@2655
  1643
			fSinc[i] = sinf(two_pi_fc * ((float)i - m_over_two)) / ((float)i - m_over_two);
schnarf@2655
  1644
			/* Apply blackman window */
schnarf@2655
  1645
			fSinc[i] *= 0.42f - 0.5f * cosf(two_pi_over_m * (float)i) + 0.08f * cosf(four_pi_over_m * (float)i);
schnarf@2655
  1646
		}
schnarf@2660
  1647
		norm_sum += fabs(fSinc[i]);
schnarf@2655
  1648
	}
schnarf@2663
  1649
	
schnarf@2663
  1650
	norm_fact = 1.0f / norm_sum;
schnarf@2663
  1651
	
schnarf@2657
  1652
#define convert_fixed(type, fix) { \
schnarf@2656
  1653
		type *dst = (type *)cvt->coeff; \
schnarf@2655
  1654
		for( i = 0; i <= m; ++i ) { \
schnarf@2657
  1655
			dst[i] = fix(fSinc[i] * norm_fact); \
schnarf@2655
  1656
		} \
schnarf@2655
  1657
	}
schnarf@2655
  1658
	
schnarf@2655
  1659
	/* If we're using floating point, we only need to normalize */
schnarf@2655
  1660
	if(SDL_AUDIO_ISFLOAT(format) && SDL_AUDIO_BITSIZE(format) == 32) {
schnarf@2656
  1661
		float *fDest = (float *)cvt->coeff;
schnarf@2655
  1662
		for(i = 0; i <= m; ++i) {
schnarf@2655
  1663
			fDest[i] = fSinc[i] * norm_fact;
schnarf@2655
  1664
		}
schnarf@2655
  1665
	} else {
schnarf@2655
  1666
		switch (SDL_AUDIO_BITSIZE(format)) {
schnarf@2655
  1667
			case 8:
schnarf@2657
  1668
				convert_fixed(Uint8, SDL_Make_1_7);
schnarf@2655
  1669
				break;
schnarf@2655
  1670
			case 16:
schnarf@2657
  1671
				convert_fixed(Uint16, SDL_Make_1_15);
schnarf@2655
  1672
				break;
schnarf@2655
  1673
			case 32:
schnarf@2657
  1674
				convert_fixed(Uint32, SDL_Make_1_31);
schnarf@2655
  1675
				break;
schnarf@2655
  1676
		}
schnarf@2655
  1677
	}
schnarf@2655
  1678
	
schnarf@2655
  1679
	/* Initialize the state buffer to all zeroes, and set initial position */
schnarf@2661
  1680
	memset(cvt->state_buf, 0, cvt->len_sinc * SDL_AUDIO_BITSIZE(format) / 4);
schnarf@2655
  1681
	cvt->state_pos = 0;
schnarf@2655
  1682
	
schnarf@2655
  1683
	/* Clean up */
schnarf@2655
  1684
#undef convert_fixed
schnarf@2655
  1685
	free(fSinc);
schnarf@2655
  1686
}
icculus@1982
  1687
schnarf@2656
  1688
/* This is used to reduce the resampling ratio */
schnarf@2656
  1689
inline int SDL_GCD(int a, int b) {
schnarf@2656
  1690
	int temp;
schnarf@2656
  1691
	while(b != 0) {
schnarf@2656
  1692
		temp = a % b;
schnarf@2656
  1693
		a = b;
schnarf@2656
  1694
		b = temp;
schnarf@2656
  1695
	}
schnarf@2656
  1696
	return a;
schnarf@2656
  1697
}
schnarf@2656
  1698
schnarf@2663
  1699
/* Perform proper resampling. This is pretty slow but it's the best-sounding method. */
schnarf@2657
  1700
static void SDLCALL
schnarf@2657
  1701
SDL_Resample(SDL_AudioCVT * cvt, SDL_AudioFormat format)
schnarf@2657
  1702
{
schnarf@2657
  1703
    int i, j;
schnarf@2657
  1704
schnarf@2657
  1705
#ifdef DEBUG_CONVERT
schnarf@2657
  1706
    printf("Converting audio rate via proper resampling (mono)\n");
schnarf@2657
  1707
#endif
schnarf@2657
  1708
schnarf@2657
  1709
#define zerostuff_mono(type) { \
schnarf@2657
  1710
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
schnarf@2657
  1711
        type *dst = (type *) (cvt->buf + (cvt->len_cvt * cvt->len_mult)); \
schnarf@2657
  1712
        for (i = cvt->len_cvt / sizeof (type); i; --i) { \
schnarf@2657
  1713
            src--; \
schnarf@2657
  1714
            dst[-1] = src[0]; \
schnarf@2657
  1715
			for( j = -cvt->len_mult; j < -1; ++j ) { \
schnarf@2657
  1716
				dst[j] = 0; \
schnarf@2657
  1717
			} \
schnarf@2657
  1718
            dst -= cvt->len_mult; \
schnarf@2657
  1719
        } \
schnarf@2657
  1720
    }
schnarf@2657
  1721
	
schnarf@2657
  1722
#define discard_mono(type) { \
schnarf@2657
  1723
        const type *src = (const type *) (cvt->buf); \
schnarf@2657
  1724
        type *dst = (type *) (cvt->buf); \
schnarf@2662
  1725
        for (i = 0; i < (cvt->len_cvt / sizeof(type)) / cvt->len_div; ++i) { \
schnarf@2657
  1726
            dst[0] = src[0]; \
schnarf@2657
  1727
            src += cvt->len_div; \
schnarf@2657
  1728
            ++dst; \
schnarf@2657
  1729
        } \
schnarf@2657
  1730
    }
schnarf@2657
  1731
schnarf@2663
  1732
	/* Step 1: Zero stuff the conversion buffer. This upsamples by a factor of len_mult,
schnarf@2663
  1733
	   creating aliasing at frequencies above the original nyquist frequency.
schnarf@2663
  1734
	 */
schnarf@2661
  1735
#ifdef DEBUG_CONVERT
schnarf@2657
  1736
	printf("Zero-stuffing by a factor of %u\n", cvt->len_mult);
schnarf@2657
  1737
#endif
schnarf@2657
  1738
    switch (SDL_AUDIO_BITSIZE(format)) {
schnarf@2657
  1739
    case 8:
schnarf@2657
  1740
        zerostuff_mono(Uint8);
schnarf@2657
  1741
        break;
schnarf@2657
  1742
    case 16:
schnarf@2657
  1743
        zerostuff_mono(Uint16);
schnarf@2657
  1744
        break;
schnarf@2657
  1745
    case 32:
schnarf@2657
  1746
        zerostuff_mono(Uint32);
schnarf@2657
  1747
        break;
schnarf@2657
  1748
    }
schnarf@2657
  1749
	
schnarf@2661
  1750
	cvt->len_cvt *= cvt->len_mult;
schnarf@2657
  1751
schnarf@2663
  1752
	/* Step 2: Use a windowed sinc FIR filter (lowpass filter) to remove the alias
schnarf@2663
  1753
	   frequencies. This is the slow part.
schnarf@2663
  1754
	 */
schnarf@2663
  1755
	SDL_FilterFIR( cvt, format );
schnarf@2662
  1756
	
schnarf@2663
  1757
	/* Step 3: Now downsample by discarding samples. */
schnarf@2662
  1758
schnarf@2661
  1759
#ifdef DEBUG_CONVERT
schnarf@2657
  1760
	printf("Discarding samples by a factor of %u\n", cvt->len_div);
schnarf@2657
  1761
#endif
schnarf@2657
  1762
    switch (SDL_AUDIO_BITSIZE(format)) {
schnarf@2657
  1763
    case 8:
schnarf@2657
  1764
        discard_mono(Uint8);
schnarf@2657
  1765
        break;
schnarf@2657
  1766
    case 16:
schnarf@2657
  1767
        discard_mono(Uint16);
schnarf@2657
  1768
        break;
schnarf@2657
  1769
    case 32:
schnarf@2657
  1770
        discard_mono(Uint32);
schnarf@2657
  1771
        break;
schnarf@2657
  1772
    }
schnarf@2657
  1773
	
schnarf@2657
  1774
#undef zerostuff_mono
schnarf@2657
  1775
#undef discard_mono
schnarf@2657
  1776
schnarf@2661
  1777
    cvt->len_cvt /= cvt->len_div;
schnarf@2657
  1778
	
schnarf@2657
  1779
    if (cvt->filters[++cvt->filter_index]) {
schnarf@2657
  1780
        cvt->filters[cvt->filter_index] (cvt, format);
schnarf@2657
  1781
    }
schnarf@2657
  1782
}
schnarf@2657
  1783
icculus@1982
  1784
icculus@1982
  1785
/* Creates a set of audio filters to convert from one format to another.
icculus@1982
  1786
   Returns -1 if the format conversion is not supported, 0 if there's
icculus@1982
  1787
   no conversion needed, or 1 if the audio filter is set up.
slouken@0
  1788
*/
slouken@1895
  1789
slouken@1895
  1790
int
slouken@1895
  1791
SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
icculus@1982
  1792
                  SDL_AudioFormat src_fmt, Uint8 src_channels, int src_rate,
icculus@1982
  1793
                  SDL_AudioFormat dst_fmt, Uint8 dst_channels, int dst_rate)
slouken@0
  1794
{
icculus@1982
  1795
    /* there are no unsigned types over 16 bits, so catch this upfront. */
icculus@1982
  1796
    if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
icculus@1982
  1797
        return -1;
icculus@1982
  1798
    }
icculus@1982
  1799
    if ((SDL_AUDIO_BITSIZE(dst_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(dst_fmt))) {
icculus@1982
  1800
        return -1;
icculus@1982
  1801
    }
slouken@1985
  1802
#ifdef DEBUG_CONVERT
icculus@1982
  1803
    printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
slouken@1985
  1804
           src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);
slouken@1985
  1805
#endif
icculus@1982
  1806
slouken@1895
  1807
    /* Start off with no conversion necessary */
icculus@1982
  1808
icculus@1982
  1809
    cvt->src_format = src_fmt;
icculus@1982
  1810
    cvt->dst_format = dst_fmt;
slouken@1895
  1811
    cvt->needed = 0;
slouken@1895
  1812
    cvt->filter_index = 0;
slouken@1895
  1813
    cvt->filters[0] = NULL;
slouken@1895
  1814
    cvt->len_mult = 1;
slouken@1895
  1815
    cvt->len_ratio = 1.0;
slouken@0
  1816
icculus@1982
  1817
    /* Convert data types, if necessary. Updates (cvt). */
icculus@1982
  1818
    if (SDL_BuildAudioTypeCVT(cvt, src_fmt, dst_fmt) == -1)
slouken@1985
  1819
        return -1;              /* shouldn't happen, but just in case... */
slouken@0
  1820
icculus@1982
  1821
    /* Channel conversion */
slouken@1895
  1822
    if (src_channels != dst_channels) {
slouken@1895
  1823
        if ((src_channels == 1) && (dst_channels > 1)) {
slouken@1895
  1824
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
slouken@1895
  1825
            cvt->len_mult *= 2;
slouken@1895
  1826
            src_channels = 2;
slouken@1895
  1827
            cvt->len_ratio *= 2;
slouken@1895
  1828
        }
slouken@1895
  1829
        if ((src_channels == 2) && (dst_channels == 6)) {
slouken@1895
  1830
            cvt->filters[cvt->filter_index++] = SDL_ConvertSurround;
slouken@1895
  1831
            src_channels = 6;
slouken@1895
  1832
            cvt->len_mult *= 3;
slouken@1895
  1833
            cvt->len_ratio *= 3;
slouken@1895
  1834
        }
slouken@1895
  1835
        if ((src_channels == 2) && (dst_channels == 4)) {
slouken@1895
  1836
            cvt->filters[cvt->filter_index++] = SDL_ConvertSurround_4;
slouken@1895
  1837
            src_channels = 4;
slouken@1895
  1838
            cvt->len_mult *= 2;
slouken@1895
  1839
            cvt->len_ratio *= 2;
slouken@1895
  1840
        }
slouken@1895
  1841
        while ((src_channels * 2) <= dst_channels) {
slouken@1895
  1842
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
slouken@1895
  1843
            cvt->len_mult *= 2;
slouken@1895
  1844
            src_channels *= 2;
slouken@1895
  1845
            cvt->len_ratio *= 2;
slouken@1895
  1846
        }
slouken@1895
  1847
        if ((src_channels == 6) && (dst_channels <= 2)) {
slouken@1895
  1848
            cvt->filters[cvt->filter_index++] = SDL_ConvertStrip;
slouken@1895
  1849
            src_channels = 2;
slouken@1895
  1850
            cvt->len_ratio /= 3;
slouken@1895
  1851
        }
slouken@1895
  1852
        if ((src_channels == 6) && (dst_channels == 4)) {
slouken@1895
  1853
            cvt->filters[cvt->filter_index++] = SDL_ConvertStrip_2;
slouken@1895
  1854
            src_channels = 4;
slouken@1895
  1855
            cvt->len_ratio /= 2;
slouken@1895
  1856
        }
slouken@1895
  1857
        /* This assumes that 4 channel audio is in the format:
slouken@1895
  1858
           Left {front/back} + Right {front/back}
slouken@1895
  1859
           so converting to L/R stereo works properly.
slouken@1895
  1860
         */
slouken@1895
  1861
        while (((src_channels % 2) == 0) &&
slouken@1895
  1862
               ((src_channels / 2) >= dst_channels)) {
slouken@1895
  1863
            cvt->filters[cvt->filter_index++] = SDL_ConvertMono;
slouken@1895
  1864
            src_channels /= 2;
slouken@1895
  1865
            cvt->len_ratio /= 2;
slouken@1895
  1866
        }
slouken@1895
  1867
        if (src_channels != dst_channels) {
slouken@1895
  1868
            /* Uh oh.. */ ;
slouken@1895
  1869
        }
slouken@1895
  1870
    }
schnarf@2663
  1871
	
slouken@1895
  1872
    /* Do rate conversion */
schnarf@2663
  1873
	if( src_rate != dst_rate ) {
schnarf@2663
  1874
		int rate_gcd;
schnarf@2663
  1875
		rate_gcd = SDL_GCD(src_rate, dst_rate);
schnarf@2663
  1876
		cvt->len_mult = dst_rate / rate_gcd;
schnarf@2663
  1877
		cvt->len_div = src_rate / rate_gcd;
schnarf@2663
  1878
		cvt->len_ratio = (double)cvt->len_mult / (double)cvt->len_div;
schnarf@2663
  1879
		cvt->filters[cvt->filter_index++] = SDL_Resample;
schnarf@2663
  1880
		SDL_BuildWindowedSinc(cvt, dst_fmt, 768);
schnarf@2663
  1881
	}
schnarf@2656
  1882
	
schnarf@2663
  1883
/*
schnarf@2663
  1884
    cvt->rate_incr = 0.0;
slouken@1895
  1885
    if ((src_rate / 100) != (dst_rate / 100)) {
slouken@1895
  1886
        Uint32 hi_rate, lo_rate;
slouken@1895
  1887
        int len_mult;
slouken@1895
  1888
        double len_ratio;
icculus@1982
  1889
        SDL_AudioFilter rate_cvt = NULL;
slouken@1895
  1890
slouken@1895
  1891
        if (src_rate > dst_rate) {
slouken@1895
  1892
            hi_rate = src_rate;
slouken@1895
  1893
            lo_rate = dst_rate;
slouken@1895
  1894
            switch (src_channels) {
slouken@1895
  1895
            case 1:
slouken@1895
  1896
                rate_cvt = SDL_RateDIV2;
slouken@1895
  1897
                break;
slouken@1895
  1898
            case 2:
slouken@1895
  1899
                rate_cvt = SDL_RateDIV2_c2;
slouken@1895
  1900
                break;
slouken@1895
  1901
            case 4:
slouken@1895
  1902
                rate_cvt = SDL_RateDIV2_c4;
slouken@1895
  1903
                break;
slouken@1895
  1904
            case 6:
slouken@1895
  1905
                rate_cvt = SDL_RateDIV2_c6;
slouken@1895
  1906
                break;
slouken@1895
  1907
            default:
slouken@1895
  1908
                return -1;
slouken@1895
  1909
            }
slouken@1895
  1910
            len_mult = 1;
slouken@1895
  1911
            len_ratio = 0.5;
slouken@1895
  1912
        } else {
slouken@1895
  1913
            hi_rate = dst_rate;
slouken@1895
  1914
            lo_rate = src_rate;
slouken@1895
  1915
            switch (src_channels) {
slouken@1895
  1916
            case 1:
slouken@1895
  1917
                rate_cvt = SDL_RateMUL2;
slouken@1895
  1918
                break;
slouken@1895
  1919
            case 2:
slouken@1895
  1920
                rate_cvt = SDL_RateMUL2_c2;
slouken@1895
  1921
                break;
slouken@1895
  1922
            case 4:
slouken@1895
  1923
                rate_cvt = SDL_RateMUL2_c4;
slouken@1895
  1924
                break;
slouken@1895
  1925
            case 6:
slouken@1895
  1926
                rate_cvt = SDL_RateMUL2_c6;
slouken@1895
  1927
                break;
slouken@1895
  1928
            default:
slouken@1895
  1929
                return -1;
slouken@1895
  1930
            }
slouken@1895
  1931
            len_mult = 2;
slouken@1895
  1932
            len_ratio = 2.0;
schnarf@2656
  1933
        }*/
slouken@1895
  1934
        /* If hi_rate = lo_rate*2^x then conversion is easy */
schnarf@2663
  1935
     /*   while (((lo_rate * 2) / 100) <= (hi_rate / 100)) {
slouken@1895
  1936
            cvt->filters[cvt->filter_index++] = rate_cvt;
slouken@1895
  1937
            cvt->len_mult *= len_mult;
slouken@1895
  1938
            lo_rate *= 2;
slouken@1895
  1939
            cvt->len_ratio *= len_ratio;
schnarf@2656
  1940
        }*/
slouken@1895
  1941
        /* We may need a slow conversion here to finish up */
schnarf@2663
  1942
    /*    if ((lo_rate / 100) != (hi_rate / 100)) {
schnarf@2663
  1943
#if 1*/
slouken@1895
  1944
            /* The problem with this is that if the input buffer is
slouken@1895
  1945
               say 1K, and the conversion rate is say 1.1, then the
slouken@1895
  1946
               output buffer is 1.1K, which may not be an acceptable
slouken@1895
  1947
               buffer size for the audio driver (not a power of 2)
slouken@1895
  1948
             */
slouken@1895
  1949
            /* For now, punt and hope the rate distortion isn't great.
slouken@1895
  1950
             */
schnarf@2663
  1951
/*#else
slouken@1895
  1952
            if (src_rate < dst_rate) {
slouken@1895
  1953
                cvt->rate_incr = (double) lo_rate / hi_rate;
slouken@1895
  1954
                cvt->len_mult *= 2;
slouken@1895
  1955
                cvt->len_ratio /= cvt->rate_incr;
slouken@1895
  1956
            } else {
slouken@1895
  1957
                cvt->rate_incr = (double) hi_rate / lo_rate;
slouken@1895
  1958
                cvt->len_ratio *= cvt->rate_incr;
slouken@1895
  1959
            }
slouken@1895
  1960
            cvt->filters[cvt->filter_index++] = SDL_RateSLOW;
slouken@0
  1961
#endif
schnarf@2663
  1962
        }
schnarf@2656
  1963
    }*/
slouken@0
  1964
slouken@1895
  1965
    /* Set up the filter information */
slouken@1895
  1966
    if (cvt->filter_index != 0) {
slouken@1895
  1967
        cvt->needed = 1;
icculus@1982
  1968
        cvt->src_format = src_fmt;
icculus@1982
  1969
        cvt->dst_format = dst_fmt;
slouken@1895
  1970
        cvt->len = 0;
slouken@1895
  1971
        cvt->buf = NULL;
slouken@1895
  1972
        cvt->filters[cvt->filter_index] = NULL;
slouken@1895
  1973
    }
slouken@1895
  1974
    return (cvt->needed);
slouken@0
  1975
}
slouken@1895
  1976
schnarf@2657
  1977
#undef SDL_FixMpy8
schnarf@2657
  1978
#undef SDL_FixMpy16
schnarf@2657
  1979
#undef SDL_FixMpy32
schnarf@2663
  1980
#undef SDL_FloatMpy
schnarf@2657
  1981
#undef SDL_Make_1_7
schnarf@2657
  1982
#undef SDL_Make_1_15
schnarf@2657
  1983
#undef SDL_Make_1_31
schnarf@2657
  1984
#undef SDL_Make_2_6
schnarf@2657
  1985
#undef SDL_Make_2_14
schnarf@2657
  1986
#undef SDL_Make_2_30
schnarf@2657
  1987
slouken@1895
  1988
/* vi: set ts=4 sw=4 expandtab: */