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