src/audio/SDL_audiocvt.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 12 Jan 2009 08:46:28 +0000
changeset 3032 77c3e67f0740
parent 3021 f3dcf04412cf
child 3040 62d4992e5a92
permissions -rw-r--r--
Fixed Visual C++ build
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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"
slouken@2728
    23
slouken@0
    24
/* Functions for audio drivers to perform runtime conversion of audio format */
slouken@0
    25
slouken@0
    26
#include "SDL_audio.h"
icculus@1982
    27
#include "SDL_audio_c.h"
slouken@0
    28
icculus@3021
    29
/* #define DEBUG_CONVERT */
slouken@2716
    30
icculus@3021
    31
/* !!! FIXME */
icculus@3021
    32
#ifndef assert
icculus@3021
    33
#define assert(x)
slouken@2716
    34
#endif
slouken@2716
    35
slouken@0
    36
/* Effectively mix right and left channels into a single channel */
icculus@1982
    37
static void SDLCALL
icculus@1982
    38
SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
    39
{
slouken@1895
    40
    int i;
slouken@1895
    41
    Sint32 sample;
slouken@0
    42
slouken@0
    43
#ifdef DEBUG_CONVERT
slouken@1895
    44
    fprintf(stderr, "Converting to mono\n");
slouken@0
    45
#endif
slouken@1985
    46
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
    47
    case AUDIO_U8:
slouken@1895
    48
        {
slouken@1895
    49
            Uint8 *src, *dst;
slouken@0
    50
slouken@1895
    51
            src = cvt->buf;
slouken@1895
    52
            dst = cvt->buf;
slouken@1895
    53
            for (i = cvt->len_cvt / 2; i; --i) {
slouken@1895
    54
                sample = src[0] + src[1];
slouken@2042
    55
                *dst = (Uint8) (sample / 2);
slouken@1895
    56
                src += 2;
slouken@1895
    57
                dst += 1;
slouken@1895
    58
            }
slouken@1895
    59
        }
slouken@1895
    60
        break;
slouken@0
    61
slouken@1895
    62
    case AUDIO_S8:
slouken@1895
    63
        {
slouken@1895
    64
            Sint8 *src, *dst;
slouken@0
    65
slouken@1895
    66
            src = (Sint8 *) cvt->buf;
slouken@1895
    67
            dst = (Sint8 *) cvt->buf;
slouken@1895
    68
            for (i = cvt->len_cvt / 2; i; --i) {
slouken@1895
    69
                sample = src[0] + src[1];
slouken@2042
    70
                *dst = (Sint8) (sample / 2);
slouken@1895
    71
                src += 2;
slouken@1895
    72
                dst += 1;
slouken@1895
    73
            }
slouken@1895
    74
        }
slouken@1895
    75
        break;
slouken@0
    76
slouken@1895
    77
    case AUDIO_U16:
slouken@1895
    78
        {
slouken@1895
    79
            Uint8 *src, *dst;
slouken@0
    80
slouken@1895
    81
            src = cvt->buf;
slouken@1895
    82
            dst = cvt->buf;
icculus@1982
    83
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
    84
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
    85
                    sample = (Uint16) ((src[0] << 8) | src[1]) +
slouken@1895
    86
                        (Uint16) ((src[2] << 8) | src[3]);
slouken@2042
    87
                    sample /= 2;
slouken@2042
    88
                    dst[1] = (sample & 0xFF);
slouken@2042
    89
                    sample >>= 8;
slouken@2042
    90
                    dst[0] = (sample & 0xFF);
slouken@1895
    91
                    src += 4;
slouken@1895
    92
                    dst += 2;
slouken@1895
    93
                }
slouken@1895
    94
            } else {
slouken@1895
    95
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
    96
                    sample = (Uint16) ((src[1] << 8) | src[0]) +
slouken@1895
    97
                        (Uint16) ((src[3] << 8) | src[2]);
slouken@2042
    98
                    sample /= 2;
slouken@2042
    99
                    dst[0] = (sample & 0xFF);
slouken@2042
   100
                    sample >>= 8;
slouken@2042
   101
                    dst[1] = (sample & 0xFF);
slouken@1895
   102
                    src += 4;
slouken@1895
   103
                    dst += 2;
slouken@1895
   104
                }
slouken@1895
   105
            }
slouken@1895
   106
        }
slouken@1895
   107
        break;
slouken@0
   108
slouken@1895
   109
    case AUDIO_S16:
slouken@1895
   110
        {
slouken@1895
   111
            Uint8 *src, *dst;
slouken@0
   112
slouken@1895
   113
            src = cvt->buf;
slouken@1895
   114
            dst = cvt->buf;
icculus@1982
   115
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   116
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   117
                    sample = (Sint16) ((src[0] << 8) | src[1]) +
slouken@1895
   118
                        (Sint16) ((src[2] << 8) | src[3]);
slouken@2042
   119
                    sample /= 2;
slouken@2042
   120
                    dst[1] = (sample & 0xFF);
slouken@2042
   121
                    sample >>= 8;
slouken@2042
   122
                    dst[0] = (sample & 0xFF);
slouken@1895
   123
                    src += 4;
slouken@1895
   124
                    dst += 2;
slouken@1895
   125
                }
slouken@1895
   126
            } else {
slouken@1895
   127
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   128
                    sample = (Sint16) ((src[1] << 8) | src[0]) +
slouken@1895
   129
                        (Sint16) ((src[3] << 8) | src[2]);
slouken@2042
   130
                    sample /= 2;
slouken@2042
   131
                    dst[0] = (sample & 0xFF);
slouken@2042
   132
                    sample >>= 8;
slouken@2042
   133
                    dst[1] = (sample & 0xFF);
slouken@1895
   134
                    src += 4;
slouken@1895
   135
                    dst += 2;
slouken@1895
   136
                }
slouken@1895
   137
            }
slouken@1895
   138
        }
slouken@1895
   139
        break;
icculus@1982
   140
icculus@1982
   141
    case AUDIO_S32:
icculus@1982
   142
        {
icculus@1982
   143
            const Uint32 *src = (const Uint32 *) cvt->buf;
icculus@1982
   144
            Uint32 *dst = (Uint32 *) cvt->buf;
icculus@1982
   145
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   146
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@1982
   147
                    const Sint64 added =
slouken@1985
   148
                        (((Sint64) (Sint32) SDL_SwapBE32(src[0])) +
slouken@1985
   149
                         ((Sint64) (Sint32) SDL_SwapBE32(src[1])));
icculus@2078
   150
                    *(dst++) = SDL_SwapBE32((Uint32) ((Sint32) (added / 2)));
icculus@1982
   151
                }
icculus@1982
   152
            } else {
icculus@1982
   153
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@1982
   154
                    const Sint64 added =
slouken@1985
   155
                        (((Sint64) (Sint32) SDL_SwapLE32(src[0])) +
slouken@1985
   156
                         ((Sint64) (Sint32) SDL_SwapLE32(src[1])));
icculus@2078
   157
                    *(dst++) = SDL_SwapLE32((Uint32) ((Sint32) (added / 2)));
icculus@1982
   158
                }
icculus@1982
   159
            }
icculus@1982
   160
        }
icculus@1982
   161
        break;
icculus@1982
   162
icculus@1982
   163
    case AUDIO_F32:
icculus@1982
   164
        {
icculus@2014
   165
            const float *src = (const float *) cvt->buf;
icculus@2014
   166
            float *dst = (float *) cvt->buf;
icculus@1982
   167
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   168
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@2049
   169
                    const float src1 = SDL_SwapFloatBE(src[0]);
icculus@2049
   170
                    const float src2 = SDL_SwapFloatBE(src[1]);
icculus@1982
   171
                    const double added = ((double) src1) + ((double) src2);
icculus@2049
   172
                    const float halved = (float) (added * 0.5);
icculus@2049
   173
                    *(dst++) = SDL_SwapFloatBE(halved);
icculus@1982
   174
                }
icculus@1982
   175
            } else {
icculus@1982
   176
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@2049
   177
                    const float src1 = SDL_SwapFloatLE(src[0]);
icculus@2049
   178
                    const float src2 = SDL_SwapFloatLE(src[1]);
icculus@1982
   179
                    const double added = ((double) src1) + ((double) src2);
icculus@2049
   180
                    const float halved = (float) (added * 0.5);
icculus@2049
   181
                    *(dst++) = SDL_SwapFloatLE(halved);
icculus@1982
   182
                }
icculus@1982
   183
            }
icculus@1982
   184
        }
icculus@1982
   185
        break;
slouken@1895
   186
    }
icculus@1982
   187
slouken@1895
   188
    cvt->len_cvt /= 2;
slouken@1895
   189
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   190
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   191
    }
slouken@0
   192
}
slouken@0
   193
icculus@1982
   194
slouken@942
   195
/* Discard top 4 channels */
icculus@1982
   196
static void SDLCALL
icculus@1982
   197
SDL_ConvertStrip(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   198
{
slouken@1895
   199
    int i;
slouken@942
   200
slouken@942
   201
#ifdef DEBUG_CONVERT
icculus@1982
   202
    fprintf(stderr, "Converting down from 6 channels to stereo\n");
slouken@942
   203
#endif
slouken@942
   204
slouken@1985
   205
#define strip_chans_6_to_2(type) \
icculus@1982
   206
    { \
icculus@1982
   207
        const type *src = (const type *) cvt->buf; \
icculus@1982
   208
        type *dst = (type *) cvt->buf; \
icculus@1982
   209
        for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
icculus@1982
   210
            dst[0] = src[0]; \
icculus@1982
   211
            dst[1] = src[1]; \
icculus@1982
   212
            src += 6; \
icculus@1982
   213
            dst += 2; \
icculus@1982
   214
        } \
icculus@1982
   215
    }
slouken@942
   216
icculus@1982
   217
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   218
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   219
    case 8:
slouken@1985
   220
        strip_chans_6_to_2(Uint8);
slouken@1985
   221
        break;
slouken@1985
   222
    case 16:
slouken@1985
   223
        strip_chans_6_to_2(Uint16);
slouken@1985
   224
        break;
slouken@1985
   225
    case 32:
slouken@1985
   226
        strip_chans_6_to_2(Uint32);
slouken@1985
   227
        break;
icculus@1982
   228
    }
slouken@942
   229
slouken@1985
   230
#undef strip_chans_6_to_2
slouken@942
   231
slouken@1895
   232
    cvt->len_cvt /= 3;
slouken@1895
   233
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   234
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   235
    }
slouken@942
   236
}
slouken@942
   237
slouken@942
   238
slouken@942
   239
/* Discard top 2 channels of 6 */
icculus@1982
   240
static void SDLCALL
icculus@1982
   241
SDL_ConvertStrip_2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   242
{
slouken@1895
   243
    int i;
slouken@942
   244
slouken@942
   245
#ifdef DEBUG_CONVERT
slouken@1895
   246
    fprintf(stderr, "Converting 6 down to quad\n");
slouken@942
   247
#endif
slouken@942
   248
slouken@1985
   249
#define strip_chans_6_to_4(type) \
icculus@1982
   250
    { \
icculus@1982
   251
        const type *src = (const type *) cvt->buf; \
icculus@1982
   252
        type *dst = (type *) cvt->buf; \
icculus@1982
   253
        for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
icculus@1982
   254
            dst[0] = src[0]; \
icculus@1982
   255
            dst[1] = src[1]; \
icculus@1982
   256
            dst[2] = src[2]; \
icculus@1982
   257
            dst[3] = src[3]; \
icculus@1982
   258
            src += 6; \
icculus@1982
   259
            dst += 4; \
icculus@1982
   260
        } \
icculus@1982
   261
    }
slouken@942
   262
icculus@1982
   263
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   264
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   265
    case 8:
slouken@1985
   266
        strip_chans_6_to_4(Uint8);
slouken@1985
   267
        break;
slouken@1985
   268
    case 16:
slouken@1985
   269
        strip_chans_6_to_4(Uint16);
slouken@1985
   270
        break;
slouken@1985
   271
    case 32:
slouken@1985
   272
        strip_chans_6_to_4(Uint32);
slouken@1985
   273
        break;
icculus@1982
   274
    }
slouken@942
   275
slouken@1985
   276
#undef strip_chans_6_to_4
slouken@942
   277
icculus@1982
   278
    cvt->len_cvt /= 6;
icculus@1982
   279
    cvt->len_cvt *= 4;
slouken@1895
   280
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   281
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   282
    }
slouken@942
   283
}
slouken@0
   284
slouken@0
   285
/* Duplicate a mono channel to both stereo channels */
icculus@1982
   286
static void SDLCALL
icculus@1982
   287
SDL_ConvertStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
   288
{
slouken@1895
   289
    int i;
slouken@0
   290
slouken@0
   291
#ifdef DEBUG_CONVERT
slouken@1895
   292
    fprintf(stderr, "Converting to stereo\n");
slouken@0
   293
#endif
slouken@0
   294
slouken@1985
   295
#define dup_chans_1_to_2(type) \
icculus@1982
   296
    { \
icculus@1982
   297
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   298
        type *dst = (type *) (cvt->buf + cvt->len_cvt * 2); \
icculus@1982
   299
        for (i = cvt->len_cvt / 2; i; --i, --src) { \
icculus@1982
   300
            const type val = *src; \
icculus@1982
   301
            dst -= 2; \
icculus@1982
   302
            dst[0] = dst[1] = val; \
icculus@1982
   303
        } \
icculus@1982
   304
    }
slouken@0
   305
icculus@1982
   306
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   307
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   308
    case 8:
slouken@1985
   309
        dup_chans_1_to_2(Uint8);
slouken@1985
   310
        break;
slouken@1985
   311
    case 16:
slouken@1985
   312
        dup_chans_1_to_2(Uint16);
slouken@1985
   313
        break;
slouken@1985
   314
    case 32:
slouken@1985
   315
        dup_chans_1_to_2(Uint32);
slouken@1985
   316
        break;
slouken@1895
   317
    }
icculus@1982
   318
slouken@1985
   319
#undef dup_chans_1_to_2
icculus@1982
   320
slouken@1895
   321
    cvt->len_cvt *= 2;
slouken@1895
   322
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   323
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   324
    }
slouken@0
   325
}
slouken@0
   326
slouken@942
   327
slouken@942
   328
/* Duplicate a stereo channel to a pseudo-5.1 stream */
icculus@1982
   329
static void SDLCALL
icculus@1982
   330
SDL_ConvertSurround(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   331
{
slouken@1895
   332
    int i;
slouken@942
   333
slouken@942
   334
#ifdef DEBUG_CONVERT
slouken@1895
   335
    fprintf(stderr, "Converting stereo to surround\n");
slouken@942
   336
#endif
slouken@942
   337
slouken@1985
   338
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
   339
    case AUDIO_U8:
slouken@1895
   340
        {
slouken@1895
   341
            Uint8 *src, *dst, lf, rf, ce;
slouken@942
   342
slouken@1895
   343
            src = (Uint8 *) (cvt->buf + cvt->len_cvt);
slouken@1895
   344
            dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 3);
slouken@1895
   345
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   346
                dst -= 6;
slouken@1895
   347
                src -= 2;
slouken@1895
   348
                lf = src[0];
slouken@1895
   349
                rf = src[1];
slouken@1895
   350
                ce = (lf / 2) + (rf / 2);
slouken@1895
   351
                dst[0] = lf;
slouken@1895
   352
                dst[1] = rf;
slouken@1895
   353
                dst[2] = lf - ce;
slouken@1895
   354
                dst[3] = rf - ce;
slouken@1895
   355
                dst[4] = ce;
slouken@1895
   356
                dst[5] = ce;
slouken@1895
   357
            }
slouken@1895
   358
        }
slouken@1895
   359
        break;
slouken@942
   360
slouken@1895
   361
    case AUDIO_S8:
slouken@1895
   362
        {
slouken@1895
   363
            Sint8 *src, *dst, lf, rf, ce;
slouken@942
   364
slouken@1895
   365
            src = (Sint8 *) cvt->buf + cvt->len_cvt;
slouken@1895
   366
            dst = (Sint8 *) cvt->buf + cvt->len_cvt * 3;
slouken@1895
   367
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   368
                dst -= 6;
slouken@1895
   369
                src -= 2;
slouken@1895
   370
                lf = src[0];
slouken@1895
   371
                rf = src[1];
slouken@1895
   372
                ce = (lf / 2) + (rf / 2);
slouken@1895
   373
                dst[0] = lf;
slouken@1895
   374
                dst[1] = rf;
slouken@1895
   375
                dst[2] = lf - ce;
slouken@1895
   376
                dst[3] = rf - ce;
slouken@1895
   377
                dst[4] = ce;
slouken@1895
   378
                dst[5] = ce;
slouken@1895
   379
            }
slouken@1895
   380
        }
slouken@1895
   381
        break;
slouken@942
   382
slouken@1895
   383
    case AUDIO_U16:
slouken@1895
   384
        {
slouken@1895
   385
            Uint8 *src, *dst;
slouken@1895
   386
            Uint16 lf, rf, ce, lr, rr;
slouken@942
   387
slouken@1895
   388
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   389
            dst = cvt->buf + cvt->len_cvt * 3;
slouken@942
   390
icculus@1982
   391
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   392
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   393
                    dst -= 12;
slouken@1895
   394
                    src -= 4;
slouken@1895
   395
                    lf = (Uint16) ((src[0] << 8) | src[1]);
slouken@1895
   396
                    rf = (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
   397
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   398
                    rr = lf - ce;
slouken@1895
   399
                    lr = rf - ce;
slouken@1895
   400
                    dst[1] = (lf & 0xFF);
slouken@1895
   401
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   402
                    dst[3] = (rf & 0xFF);
slouken@1895
   403
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   404
slouken@1895
   405
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   406
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   407
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   408
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   409
slouken@1895
   410
                    dst[1 + 8] = (ce & 0xFF);
slouken@1895
   411
                    dst[0 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   412
                    dst[3 + 8] = (ce & 0xFF);
slouken@1895
   413
                    dst[2 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   414
                }
slouken@1895
   415
            } else {
slouken@1895
   416
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   417
                    dst -= 12;
slouken@1895
   418
                    src -= 4;
slouken@1895
   419
                    lf = (Uint16) ((src[1] << 8) | src[0]);
slouken@1895
   420
                    rf = (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   421
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   422
                    rr = lf - ce;
slouken@1895
   423
                    lr = rf - ce;
slouken@1895
   424
                    dst[0] = (lf & 0xFF);
slouken@1895
   425
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   426
                    dst[2] = (rf & 0xFF);
slouken@1895
   427
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   428
slouken@1895
   429
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   430
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   431
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   432
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   433
slouken@1895
   434
                    dst[0 + 8] = (ce & 0xFF);
slouken@1895
   435
                    dst[1 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   436
                    dst[2 + 8] = (ce & 0xFF);
slouken@1895
   437
                    dst[3 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   438
                }
slouken@1895
   439
            }
slouken@1895
   440
        }
slouken@1895
   441
        break;
slouken@942
   442
slouken@1895
   443
    case AUDIO_S16:
slouken@1895
   444
        {
slouken@1895
   445
            Uint8 *src, *dst;
slouken@1895
   446
            Sint16 lf, rf, ce, lr, rr;
slouken@942
   447
slouken@1895
   448
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   449
            dst = cvt->buf + cvt->len_cvt * 3;
slouken@942
   450
icculus@1982
   451
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   452
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   453
                    dst -= 12;
slouken@1895
   454
                    src -= 4;
slouken@1895
   455
                    lf = (Sint16) ((src[0] << 8) | src[1]);
slouken@1895
   456
                    rf = (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   457
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   458
                    rr = lf - ce;
slouken@1895
   459
                    lr = rf - ce;
slouken@1895
   460
                    dst[1] = (lf & 0xFF);
slouken@1895
   461
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   462
                    dst[3] = (rf & 0xFF);
slouken@1895
   463
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   464
slouken@1895
   465
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   466
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   467
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   468
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   469
slouken@1895
   470
                    dst[1 + 8] = (ce & 0xFF);
slouken@1895
   471
                    dst[0 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   472
                    dst[3 + 8] = (ce & 0xFF);
slouken@1895
   473
                    dst[2 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   474
                }
slouken@1895
   475
            } else {
slouken@1895
   476
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   477
                    dst -= 12;
slouken@1895
   478
                    src -= 4;
slouken@1895
   479
                    lf = (Sint16) ((src[1] << 8) | src[0]);
slouken@1895
   480
                    rf = (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   481
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   482
                    rr = lf - ce;
slouken@1895
   483
                    lr = rf - ce;
slouken@1895
   484
                    dst[0] = (lf & 0xFF);
slouken@1895
   485
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   486
                    dst[2] = (rf & 0xFF);
slouken@1895
   487
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   488
slouken@1895
   489
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   490
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   491
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   492
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   493
slouken@1895
   494
                    dst[0 + 8] = (ce & 0xFF);
slouken@1895
   495
                    dst[1 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   496
                    dst[2 + 8] = (ce & 0xFF);
slouken@1895
   497
                    dst[3 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   498
                }
slouken@1895
   499
            }
slouken@1895
   500
        }
slouken@1895
   501
        break;
icculus@1982
   502
icculus@1982
   503
    case AUDIO_S32:
icculus@1982
   504
        {
icculus@1982
   505
            Sint32 lf, rf, ce;
icculus@1982
   506
            const Uint32 *src = (const Uint32 *) cvt->buf + cvt->len_cvt;
icculus@1982
   507
            Uint32 *dst = (Uint32 *) cvt->buf + cvt->len_cvt * 3;
icculus@1982
   508
icculus@1982
   509
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   510
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   511
                    dst -= 6;
icculus@1982
   512
                    src -= 2;
icculus@1982
   513
                    lf = (Sint32) SDL_SwapBE32(src[0]);
icculus@1982
   514
                    rf = (Sint32) SDL_SwapBE32(src[1]);
icculus@1982
   515
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   516
                    dst[0] = SDL_SwapBE32((Uint32) lf);
icculus@1982
   517
                    dst[1] = SDL_SwapBE32((Uint32) rf);
icculus@1982
   518
                    dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
icculus@1982
   519
                    dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
icculus@1982
   520
                    dst[4] = SDL_SwapBE32((Uint32) ce);
icculus@1982
   521
                    dst[5] = SDL_SwapBE32((Uint32) ce);
icculus@1982
   522
                }
icculus@1982
   523
            } else {
icculus@1982
   524
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   525
                    dst -= 6;
icculus@1982
   526
                    src -= 2;
icculus@1982
   527
                    lf = (Sint32) SDL_SwapLE32(src[0]);
icculus@1982
   528
                    rf = (Sint32) SDL_SwapLE32(src[1]);
icculus@1982
   529
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   530
                    dst[0] = src[0];
icculus@1982
   531
                    dst[1] = src[1];
icculus@1982
   532
                    dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
icculus@1982
   533
                    dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
icculus@1982
   534
                    dst[4] = SDL_SwapLE32((Uint32) ce);
icculus@1982
   535
                    dst[5] = SDL_SwapLE32((Uint32) ce);
icculus@1982
   536
                }
icculus@1982
   537
            }
icculus@1982
   538
        }
icculus@1982
   539
        break;
icculus@1982
   540
icculus@1982
   541
    case AUDIO_F32:
icculus@1982
   542
        {
icculus@1982
   543
            float lf, rf, ce;
icculus@2014
   544
            const float *src = (const float *) cvt->buf + cvt->len_cvt;
icculus@2014
   545
            float *dst = (float *) cvt->buf + cvt->len_cvt * 3;
icculus@1982
   546
icculus@1982
   547
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   548
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   549
                    dst -= 6;
icculus@1982
   550
                    src -= 2;
icculus@2014
   551
                    lf = SDL_SwapFloatBE(src[0]);
icculus@2014
   552
                    rf = SDL_SwapFloatBE(src[1]);
icculus@1982
   553
                    ce = (lf * 0.5f) + (rf * 0.5f);
icculus@1982
   554
                    dst[0] = src[0];
icculus@1982
   555
                    dst[1] = src[1];
icculus@2014
   556
                    dst[2] = SDL_SwapFloatBE(lf - ce);
icculus@2014
   557
                    dst[3] = SDL_SwapFloatBE(rf - ce);
icculus@2014
   558
                    dst[4] = dst[5] = SDL_SwapFloatBE(ce);
icculus@1982
   559
                }
icculus@1982
   560
            } else {
icculus@1982
   561
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   562
                    dst -= 6;
icculus@1982
   563
                    src -= 2;
icculus@2014
   564
                    lf = SDL_SwapFloatLE(src[0]);
icculus@2014
   565
                    rf = SDL_SwapFloatLE(src[1]);
icculus@1982
   566
                    ce = (lf * 0.5f) + (rf * 0.5f);
icculus@1982
   567
                    dst[0] = src[0];
icculus@1982
   568
                    dst[1] = src[1];
icculus@2014
   569
                    dst[2] = SDL_SwapFloatLE(lf - ce);
icculus@2014
   570
                    dst[3] = SDL_SwapFloatLE(rf - ce);
icculus@2014
   571
                    dst[4] = dst[5] = SDL_SwapFloatLE(ce);
icculus@1982
   572
                }
icculus@1982
   573
            }
icculus@1982
   574
        }
icculus@1982
   575
        break;
icculus@1982
   576
slouken@1895
   577
    }
slouken@1895
   578
    cvt->len_cvt *= 3;
slouken@1895
   579
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   580
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   581
    }
slouken@942
   582
}
slouken@942
   583
slouken@942
   584
slouken@942
   585
/* Duplicate a stereo channel to a pseudo-4.0 stream */
icculus@1982
   586
static void SDLCALL
icculus@1982
   587
SDL_ConvertSurround_4(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   588
{
slouken@1895
   589
    int i;
slouken@942
   590
slouken@942
   591
#ifdef DEBUG_CONVERT
slouken@1895
   592
    fprintf(stderr, "Converting stereo to quad\n");
slouken@942
   593
#endif
slouken@942
   594
slouken@1985
   595
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
   596
    case AUDIO_U8:
slouken@1895
   597
        {
slouken@1895
   598
            Uint8 *src, *dst, lf, rf, ce;
slouken@942
   599
slouken@1895
   600
            src = (Uint8 *) (cvt->buf + cvt->len_cvt);
slouken@1895
   601
            dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 2);
slouken@1895
   602
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   603
                dst -= 4;
slouken@1895
   604
                src -= 2;
slouken@1895
   605
                lf = src[0];
slouken@1895
   606
                rf = src[1];
slouken@1895
   607
                ce = (lf / 2) + (rf / 2);
slouken@1895
   608
                dst[0] = lf;
slouken@1895
   609
                dst[1] = rf;
slouken@1895
   610
                dst[2] = lf - ce;
slouken@1895
   611
                dst[3] = rf - ce;
slouken@1895
   612
            }
slouken@1895
   613
        }
slouken@1895
   614
        break;
slouken@942
   615
slouken@1895
   616
    case AUDIO_S8:
slouken@1895
   617
        {
slouken@1895
   618
            Sint8 *src, *dst, lf, rf, ce;
slouken@942
   619
slouken@1895
   620
            src = (Sint8 *) cvt->buf + cvt->len_cvt;
slouken@1895
   621
            dst = (Sint8 *) cvt->buf + cvt->len_cvt * 2;
slouken@1895
   622
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   623
                dst -= 4;
slouken@1895
   624
                src -= 2;
slouken@1895
   625
                lf = src[0];
slouken@1895
   626
                rf = src[1];
slouken@1895
   627
                ce = (lf / 2) + (rf / 2);
slouken@1895
   628
                dst[0] = lf;
slouken@1895
   629
                dst[1] = rf;
slouken@1895
   630
                dst[2] = lf - ce;
slouken@1895
   631
                dst[3] = rf - ce;
slouken@1895
   632
            }
slouken@1895
   633
        }
slouken@1895
   634
        break;
slouken@942
   635
slouken@1895
   636
    case AUDIO_U16:
slouken@1895
   637
        {
slouken@1895
   638
            Uint8 *src, *dst;
slouken@1895
   639
            Uint16 lf, rf, ce, lr, rr;
slouken@942
   640
slouken@1895
   641
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   642
            dst = cvt->buf + cvt->len_cvt * 2;
slouken@942
   643
icculus@1982
   644
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   645
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   646
                    dst -= 8;
slouken@1895
   647
                    src -= 4;
slouken@1895
   648
                    lf = (Uint16) ((src[0] << 8) | src[1]);
slouken@1895
   649
                    rf = (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
   650
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   651
                    rr = lf - ce;
slouken@1895
   652
                    lr = rf - ce;
slouken@1895
   653
                    dst[1] = (lf & 0xFF);
slouken@1895
   654
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   655
                    dst[3] = (rf & 0xFF);
slouken@1895
   656
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   657
slouken@1895
   658
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   659
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   660
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   661
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   662
                }
slouken@1895
   663
            } else {
slouken@1895
   664
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   665
                    dst -= 8;
slouken@1895
   666
                    src -= 4;
slouken@1895
   667
                    lf = (Uint16) ((src[1] << 8) | src[0]);
slouken@1895
   668
                    rf = (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   669
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   670
                    rr = lf - ce;
slouken@1895
   671
                    lr = rf - ce;
slouken@1895
   672
                    dst[0] = (lf & 0xFF);
slouken@1895
   673
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   674
                    dst[2] = (rf & 0xFF);
slouken@1895
   675
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   676
slouken@1895
   677
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   678
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   679
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   680
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   681
                }
slouken@1895
   682
            }
slouken@1895
   683
        }
slouken@1895
   684
        break;
slouken@942
   685
slouken@1895
   686
    case AUDIO_S16:
slouken@1895
   687
        {
slouken@1895
   688
            Uint8 *src, *dst;
slouken@1895
   689
            Sint16 lf, rf, ce, lr, rr;
slouken@942
   690
slouken@1895
   691
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   692
            dst = cvt->buf + cvt->len_cvt * 2;
slouken@942
   693
icculus@1982
   694
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   695
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   696
                    dst -= 8;
slouken@1895
   697
                    src -= 4;
slouken@1895
   698
                    lf = (Sint16) ((src[0] << 8) | src[1]);
slouken@1895
   699
                    rf = (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   700
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   701
                    rr = lf - ce;
slouken@1895
   702
                    lr = rf - ce;
slouken@1895
   703
                    dst[1] = (lf & 0xFF);
slouken@1895
   704
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   705
                    dst[3] = (rf & 0xFF);
slouken@1895
   706
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   707
slouken@1895
   708
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   709
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   710
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   711
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   712
                }
slouken@1895
   713
            } else {
slouken@1895
   714
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   715
                    dst -= 8;
slouken@1895
   716
                    src -= 4;
slouken@1895
   717
                    lf = (Sint16) ((src[1] << 8) | src[0]);
slouken@1895
   718
                    rf = (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   719
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   720
                    rr = lf - ce;
slouken@1895
   721
                    lr = rf - ce;
slouken@1895
   722
                    dst[0] = (lf & 0xFF);
slouken@1895
   723
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   724
                    dst[2] = (rf & 0xFF);
slouken@1895
   725
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   726
slouken@1895
   727
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   728
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   729
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   730
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   731
                }
slouken@1895
   732
            }
slouken@1895
   733
        }
slouken@1895
   734
        break;
slouken@942
   735
icculus@1982
   736
    case AUDIO_S32:
icculus@1982
   737
        {
icculus@1982
   738
            const Uint32 *src = (const Uint32 *) (cvt->buf + cvt->len_cvt);
icculus@1982
   739
            Uint32 *dst = (Uint32 *) (cvt->buf + cvt->len_cvt * 2);
icculus@1982
   740
            Sint32 lf, rf, ce;
slouken@942
   741
icculus@1982
   742
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   743
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   744
                    dst -= 4;
icculus@1982
   745
                    src -= 2;
icculus@1982
   746
                    lf = (Sint32) SDL_SwapBE32(src[0]);
icculus@1982
   747
                    rf = (Sint32) SDL_SwapBE32(src[1]);
icculus@1982
   748
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   749
                    dst[0] = src[0];
icculus@1982
   750
                    dst[1] = src[1];
icculus@1982
   751
                    dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
icculus@1982
   752
                    dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
icculus@1982
   753
                }
icculus@1982
   754
            } else {
icculus@1982
   755
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   756
                    dst -= 4;
icculus@1982
   757
                    src -= 2;
icculus@1982
   758
                    lf = (Sint32) SDL_SwapLE32(src[0]);
icculus@1982
   759
                    rf = (Sint32) SDL_SwapLE32(src[1]);
icculus@1982
   760
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   761
                    dst[0] = src[0];
icculus@1982
   762
                    dst[1] = src[1];
icculus@1982
   763
                    dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
icculus@1982
   764
                    dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
icculus@1982
   765
                }
icculus@1982
   766
            }
slouken@1895
   767
        }
slouken@1895
   768
        break;
slouken@1895
   769
    }
slouken@1895
   770
    cvt->len_cvt *= 2;
slouken@1895
   771
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   772
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   773
    }
slouken@0
   774
}
slouken@0
   775
slouken@0
   776
slouken@1895
   777
int
slouken@1895
   778
SDL_ConvertAudio(SDL_AudioCVT * cvt)
slouken@0
   779
{
icculus@3021
   780
    /* !!! FIXME: (cvt) should be const; stack-copy it here. */
icculus@3021
   781
    /* !!! FIXME: (actually, we can't...len_cvt needs to be updated. Grr.) */
icculus@3021
   782
slouken@1895
   783
    /* Make sure there's data to convert */
slouken@1895
   784
    if (cvt->buf == NULL) {
slouken@1895
   785
        SDL_SetError("No buffer allocated for conversion");
slouken@1895
   786
        return (-1);
slouken@1895
   787
    }
slouken@1895
   788
    /* Return okay if no conversion is necessary */
slouken@1895
   789
    cvt->len_cvt = cvt->len;
slouken@1895
   790
    if (cvt->filters[0] == NULL) {
slouken@1895
   791
        return (0);
slouken@1895
   792
    }
slouken@0
   793
slouken@1895
   794
    /* Set up the conversion and go! */
slouken@1895
   795
    cvt->filter_index = 0;
slouken@1895
   796
    cvt->filters[0] (cvt, cvt->src_format);
slouken@1895
   797
    return (0);
slouken@0
   798
}
slouken@0
   799
icculus@1982
   800
icculus@1982
   801
static SDL_AudioFilter
icculus@1982
   802
SDL_HandTunedTypeCVT(SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
icculus@1982
   803
{
icculus@1982
   804
    /*
icculus@1982
   805
     * Fill in any future conversions that are specialized to a
icculus@1982
   806
     *  processor, platform, compiler, or library here.
icculus@1982
   807
     */
icculus@1982
   808
slouken@1985
   809
    return NULL;                /* no specialized converter code available. */
icculus@1982
   810
}
icculus@1982
   811
icculus@1982
   812
icculus@1982
   813
/*
icculus@1982
   814
 * Find a converter between two data types. We try to select a hand-tuned
icculus@1982
   815
 *  asm/vectorized/optimized function first, and then fallback to an
icculus@1982
   816
 *  autogenerated function that is customized to convert between two
icculus@1982
   817
 *  specific data types.
icculus@1982
   818
 */
icculus@1982
   819
static int
icculus@1982
   820
SDL_BuildAudioTypeCVT(SDL_AudioCVT * cvt,
icculus@1982
   821
                      SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
icculus@1982
   822
{
icculus@1982
   823
    if (src_fmt != dst_fmt) {
icculus@1982
   824
        const Uint16 src_bitsize = SDL_AUDIO_BITSIZE(src_fmt);
icculus@1982
   825
        const Uint16 dst_bitsize = SDL_AUDIO_BITSIZE(dst_fmt);
icculus@1982
   826
        SDL_AudioFilter filter = SDL_HandTunedTypeCVT(src_fmt, dst_fmt);
icculus@1982
   827
icculus@1982
   828
        /* No hand-tuned converter? Try the autogenerated ones. */
icculus@1982
   829
        if (filter == NULL) {
icculus@1982
   830
            int i;
icculus@1982
   831
            for (i = 0; sdl_audio_type_filters[i].filter != NULL; i++) {
icculus@1982
   832
                const SDL_AudioTypeFilters *filt = &sdl_audio_type_filters[i];
icculus@1982
   833
                if ((filt->src_fmt == src_fmt) && (filt->dst_fmt == dst_fmt)) {
icculus@1982
   834
                    filter = filt->filter;
icculus@1982
   835
                    break;
icculus@1982
   836
                }
icculus@1982
   837
            }
icculus@1982
   838
icculus@1982
   839
            if (filter == NULL) {
slouken@1985
   840
                return -1;      /* Still no matching converter?! */
icculus@1982
   841
            }
icculus@1982
   842
        }
icculus@1982
   843
icculus@1982
   844
        /* Update (cvt) with filter details... */
icculus@1982
   845
        cvt->filters[cvt->filter_index++] = filter;
icculus@1982
   846
        if (src_bitsize < dst_bitsize) {
icculus@1982
   847
            const int mult = (dst_bitsize / src_bitsize);
icculus@1982
   848
            cvt->len_mult *= mult;
icculus@1982
   849
            cvt->len_ratio *= mult;
icculus@1982
   850
        } else if (src_bitsize > dst_bitsize) {
icculus@1982
   851
            cvt->len_ratio /= (src_bitsize / dst_bitsize);
icculus@1982
   852
        }
icculus@1982
   853
slouken@1985
   854
        return 1;               /* added a converter. */
icculus@1982
   855
    }
icculus@1982
   856
slouken@1985
   857
    return 0;                   /* no conversion necessary. */
icculus@1982
   858
}
icculus@1982
   859
icculus@3021
   860
icculus@3021
   861
static SDL_AudioFilter
icculus@3021
   862
SDL_HandTunedResampleCVT(SDL_AudioCVT * cvt, int dst_channels,
icculus@3021
   863
                         int src_rate, int dst_rate)
slouken@2716
   864
{
icculus@3021
   865
    /*
icculus@3021
   866
     * Fill in any future conversions that are specialized to a
icculus@3021
   867
     *  processor, platform, compiler, or library here.
icculus@3021
   868
     */
slouken@2716
   869
icculus@3021
   870
    return NULL;                /* no specialized converter code available. */
icculus@3021
   871
}
slouken@2716
   872
icculus@3021
   873
static int
icculus@3021
   874
SDL_FindFrequencyMultiple(const int src_rate, const int dst_rate)
icculus@3021
   875
{
icculus@3021
   876
    int retval = 0;
slouken@2716
   877
icculus@3021
   878
    /* If we only built with the arbitrary resamplers, ignore multiples. */
icculus@3021
   879
#if !LESS_RESAMPLERS
icculus@3021
   880
    int lo, hi;
icculus@3021
   881
    int div;
slouken@2716
   882
icculus@3021
   883
    assert(src_rate != 0);
icculus@3021
   884
    assert(dst_rate != 0);
icculus@3021
   885
    assert(src_rate != dst_rate);
slouken@2716
   886
icculus@3021
   887
    if (src_rate < dst_rate) {
icculus@3021
   888
        lo = src_rate;
icculus@3021
   889
        hi = dst_rate;
icculus@3021
   890
    } else {
icculus@3021
   891
        lo = dst_rate;
icculus@3021
   892
        hi = src_rate;
icculus@3021
   893
    }
slouken@2716
   894
icculus@3021
   895
    /* zero means "not a supported multiple" ... we only do 2x and 4x. */
icculus@3021
   896
    if ((hi % lo) != 0)
icculus@3021
   897
        return 0;   /* not a multiple. */
slouken@2716
   898
icculus@3021
   899
    div = hi / lo;
icculus@3021
   900
    retval = ((div == 2) || (div == 4)) ? div : 0;
icculus@3021
   901
#endif
icculus@3021
   902
icculus@3021
   903
    return retval;
icculus@3021
   904
}
icculus@3021
   905
icculus@3021
   906
static int
icculus@3021
   907
SDL_BuildAudioResampleCVT(SDL_AudioCVT * cvt, int dst_channels,
icculus@3021
   908
                          int src_rate, int dst_rate)
icculus@3021
   909
{
icculus@3021
   910
    if (src_rate != dst_rate) {
icculus@3021
   911
        SDL_AudioFilter filter = SDL_HandTunedResampleCVT(cvt, dst_channels,
icculus@3021
   912
                                                          src_rate, dst_rate);
icculus@3021
   913
icculus@3021
   914
        /* No hand-tuned converter? Try the autogenerated ones. */
icculus@3021
   915
        if (filter == NULL) {
icculus@3021
   916
            int i;
icculus@3021
   917
            const int upsample = (src_rate < dst_rate) ? 1 : 0;
icculus@3021
   918
            const int multiple = SDL_FindFrequencyMultiple(src_rate, dst_rate);
icculus@3021
   919
icculus@3021
   920
            for (i = 0; sdl_audio_rate_filters[i].filter != NULL; i++) {
icculus@3021
   921
                const SDL_AudioRateFilters *filt = &sdl_audio_rate_filters[i];
icculus@3021
   922
                if ((filt->fmt == cvt->dst_format) &&
icculus@3021
   923
                    (filt->channels == dst_channels) &&
icculus@3021
   924
                    (filt->upsample == upsample) &&
icculus@3021
   925
                    (filt->multiple == multiple)) {
icculus@3021
   926
                    filter = filt->filter;
icculus@3021
   927
                    break;
icculus@3021
   928
                }
icculus@3021
   929
            }
icculus@3021
   930
icculus@3021
   931
            if (filter == NULL) {
icculus@3021
   932
                return -1;      /* Still no matching converter?! */
icculus@3021
   933
            }
slouken@2716
   934
        }
slouken@2716
   935
icculus@3021
   936
        /* Update (cvt) with filter details... */
icculus@3021
   937
        cvt->filters[cvt->filter_index++] = filter;
icculus@3021
   938
        if (src_rate < dst_rate) {
icculus@3021
   939
            const double mult = ((double) dst_rate) / ((double) src_rate);
slouken@3032
   940
            cvt->len_mult *= (int) SDL_ceil(mult);
icculus@3021
   941
            cvt->len_ratio *= mult;
icculus@3021
   942
        } else {
icculus@3021
   943
            cvt->len_ratio /= ((double) src_rate) / ((double) dst_rate);
slouken@2716
   944
        }
icculus@3021
   945
icculus@3021
   946
        return 1;               /* added a converter. */
slouken@2716
   947
    }
slouken@2716
   948
icculus@3021
   949
    return 0;                   /* no conversion necessary. */
slouken@2716
   950
}
icculus@1982
   951
icculus@1982
   952
icculus@1982
   953
/* Creates a set of audio filters to convert from one format to another.
icculus@1982
   954
   Returns -1 if the format conversion is not supported, 0 if there's
icculus@1982
   955
   no conversion needed, or 1 if the audio filter is set up.
slouken@0
   956
*/
slouken@1895
   957
slouken@1895
   958
int
slouken@1895
   959
SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
icculus@1982
   960
                  SDL_AudioFormat src_fmt, Uint8 src_channels, int src_rate,
icculus@1982
   961
                  SDL_AudioFormat dst_fmt, Uint8 dst_channels, int dst_rate)
slouken@0
   962
{
icculus@3021
   963
    /*
icculus@3021
   964
     * !!! FIXME: reorder filters based on which grow/shrink the buffer.
icculus@3021
   965
     * !!! FIXME: ideally, we should do everything that shrinks the buffer
icculus@3021
   966
     * !!! FIXME: first, so we don't have to process as many bytes in a given
icculus@3021
   967
     * !!! FIXME: filter and abuse the CPU cache less. This might not be as
icculus@3021
   968
     * !!! FIXME: good in practice as it sounds in theory, though.
icculus@3021
   969
     */
icculus@3021
   970
icculus@1982
   971
    /* there are no unsigned types over 16 bits, so catch this upfront. */
icculus@1982
   972
    if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
icculus@1982
   973
        return -1;
icculus@1982
   974
    }
icculus@1982
   975
    if ((SDL_AUDIO_BITSIZE(dst_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(dst_fmt))) {
icculus@1982
   976
        return -1;
icculus@1982
   977
    }
icculus@3021
   978
icculus@3021
   979
    /* prevent possible divisions by zero, etc. */
icculus@3021
   980
    if ((src_rate == 0) || (dst_rate == 0)) {
icculus@3021
   981
        return -1;
icculus@3021
   982
    }
icculus@3021
   983
slouken@1985
   984
#ifdef DEBUG_CONVERT
icculus@1982
   985
    printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
slouken@1985
   986
           src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);
slouken@1985
   987
#endif
icculus@1982
   988
slouken@1895
   989
    /* Start off with no conversion necessary */
icculus@2879
   990
    SDL_zerop(cvt);
icculus@1982
   991
    cvt->src_format = src_fmt;
icculus@1982
   992
    cvt->dst_format = dst_fmt;
slouken@1895
   993
    cvt->needed = 0;
slouken@1895
   994
    cvt->filter_index = 0;
slouken@1895
   995
    cvt->filters[0] = NULL;
slouken@1895
   996
    cvt->len_mult = 1;
slouken@1895
   997
    cvt->len_ratio = 1.0;
icculus@3021
   998
    cvt->rate_incr = ((double) dst_rate) / ((double) src_rate);
slouken@0
   999
icculus@1982
  1000
    /* Convert data types, if necessary. Updates (cvt). */
icculus@3021
  1001
    if (SDL_BuildAudioTypeCVT(cvt, src_fmt, dst_fmt) == -1) {
slouken@1985
  1002
        return -1;              /* shouldn't happen, but just in case... */
icculus@3021
  1003
    }
slouken@0
  1004
icculus@1982
  1005
    /* Channel conversion */
slouken@1895
  1006
    if (src_channels != dst_channels) {
slouken@1895
  1007
        if ((src_channels == 1) && (dst_channels > 1)) {
slouken@1895
  1008
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
slouken@1895
  1009
            cvt->len_mult *= 2;
slouken@1895
  1010
            src_channels = 2;
slouken@1895
  1011
            cvt->len_ratio *= 2;
slouken@1895
  1012
        }
slouken@1895
  1013
        if ((src_channels == 2) && (dst_channels == 6)) {
slouken@1895
  1014
            cvt->filters[cvt->filter_index++] = SDL_ConvertSurround;
slouken@1895
  1015
            src_channels = 6;
slouken@1895
  1016
            cvt->len_mult *= 3;
slouken@1895
  1017
            cvt->len_ratio *= 3;
slouken@1895
  1018
        }
slouken@1895
  1019
        if ((src_channels == 2) && (dst_channels == 4)) {
slouken@1895
  1020
            cvt->filters[cvt->filter_index++] = SDL_ConvertSurround_4;
slouken@1895
  1021
            src_channels = 4;
slouken@1895
  1022
            cvt->len_mult *= 2;
slouken@1895
  1023
            cvt->len_ratio *= 2;
slouken@1895
  1024
        }
slouken@1895
  1025
        while ((src_channels * 2) <= dst_channels) {
slouken@1895
  1026
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
slouken@1895
  1027
            cvt->len_mult *= 2;
slouken@1895
  1028
            src_channels *= 2;
slouken@1895
  1029
            cvt->len_ratio *= 2;
slouken@1895
  1030
        }
slouken@1895
  1031
        if ((src_channels == 6) && (dst_channels <= 2)) {
slouken@1895
  1032
            cvt->filters[cvt->filter_index++] = SDL_ConvertStrip;
slouken@1895
  1033
            src_channels = 2;
slouken@1895
  1034
            cvt->len_ratio /= 3;
slouken@1895
  1035
        }
slouken@1895
  1036
        if ((src_channels == 6) && (dst_channels == 4)) {
slouken@1895
  1037
            cvt->filters[cvt->filter_index++] = SDL_ConvertStrip_2;
slouken@1895
  1038
            src_channels = 4;
slouken@1895
  1039
            cvt->len_ratio /= 2;
slouken@1895
  1040
        }
slouken@1895
  1041
        /* This assumes that 4 channel audio is in the format:
slouken@1895
  1042
           Left {front/back} + Right {front/back}
slouken@1895
  1043
           so converting to L/R stereo works properly.
slouken@1895
  1044
         */
slouken@1895
  1045
        while (((src_channels % 2) == 0) &&
slouken@1895
  1046
               ((src_channels / 2) >= dst_channels)) {
slouken@1895
  1047
            cvt->filters[cvt->filter_index++] = SDL_ConvertMono;
slouken@1895
  1048
            src_channels /= 2;
slouken@1895
  1049
            cvt->len_ratio /= 2;
slouken@1895
  1050
        }
slouken@1895
  1051
        if (src_channels != dst_channels) {
slouken@1895
  1052
            /* Uh oh.. */ ;
slouken@1895
  1053
        }
slouken@1895
  1054
    }
slouken@0
  1055
icculus@3021
  1056
    /* Do rate conversion, if necessary. Updates (cvt). */
icculus@3021
  1057
    if (SDL_BuildAudioResampleCVT(cvt, dst_channels, src_rate, dst_rate) == -1) {
icculus@3021
  1058
        return -1;              /* shouldn't happen, but just in case... */
slouken@2716
  1059
    }
slouken@2716
  1060
slouken@1895
  1061
    /* Set up the filter information */
slouken@1895
  1062
    if (cvt->filter_index != 0) {
slouken@1895
  1063
        cvt->needed = 1;
icculus@1982
  1064
        cvt->src_format = src_fmt;
icculus@1982
  1065
        cvt->dst_format = dst_fmt;
slouken@1895
  1066
        cvt->len = 0;
slouken@1895
  1067
        cvt->buf = NULL;
slouken@1895
  1068
        cvt->filters[cvt->filter_index] = NULL;
slouken@1895
  1069
    }
slouken@1895
  1070
    return (cvt->needed);
slouken@0
  1071
}
slouken@1895
  1072
slouken@2716
  1073
slouken@1895
  1074
/* vi: set ts=4 sw=4 expandtab: */