src/audio/SDL_audiocvt.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 01 Sep 2006 19:29:49 +0000
changeset 2014 7abe37467fa5
parent 1985 8055185ae4ed
child 2042 3908e1f808e1
permissions -rw-r--r--
Replaced unions with calls to SDL_SwapFloat...
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    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
slouken@0
    29
/* Effectively mix right and left channels into a single channel */
icculus@1982
    30
static void SDLCALL
icculus@1982
    31
SDL_ConvertMono(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
    32
{
slouken@1895
    33
    int i;
slouken@1895
    34
    Sint32 sample;
slouken@0
    35
slouken@0
    36
#ifdef DEBUG_CONVERT
slouken@1895
    37
    fprintf(stderr, "Converting to mono\n");
slouken@0
    38
#endif
slouken@1985
    39
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
    40
    case AUDIO_U8:
slouken@1895
    41
        {
slouken@1895
    42
            Uint8 *src, *dst;
slouken@0
    43
slouken@1895
    44
            src = cvt->buf;
slouken@1895
    45
            dst = cvt->buf;
slouken@1895
    46
            for (i = cvt->len_cvt / 2; i; --i) {
slouken@1895
    47
                sample = src[0] + src[1];
slouken@1895
    48
                if (sample > 255) {
slouken@1895
    49
                    *dst = 255;
slouken@1895
    50
                } else {
slouken@1895
    51
                    *dst = (Uint8) sample;
slouken@1895
    52
                }
slouken@1895
    53
                src += 2;
slouken@1895
    54
                dst += 1;
slouken@1895
    55
            }
slouken@1895
    56
        }
slouken@1895
    57
        break;
slouken@0
    58
slouken@1895
    59
    case AUDIO_S8:
slouken@1895
    60
        {
slouken@1895
    61
            Sint8 *src, *dst;
slouken@0
    62
slouken@1895
    63
            src = (Sint8 *) cvt->buf;
slouken@1895
    64
            dst = (Sint8 *) cvt->buf;
slouken@1895
    65
            for (i = cvt->len_cvt / 2; i; --i) {
slouken@1895
    66
                sample = src[0] + src[1];
slouken@1895
    67
                if (sample > 127) {
slouken@1895
    68
                    *dst = 127;
slouken@1895
    69
                } else if (sample < -128) {
slouken@1895
    70
                    *dst = -128;
slouken@1895
    71
                } else {
slouken@1895
    72
                    *dst = (Sint8) sample;
slouken@1895
    73
                }
slouken@1895
    74
                src += 2;
slouken@1895
    75
                dst += 1;
slouken@1895
    76
            }
slouken@1895
    77
        }
slouken@1895
    78
        break;
slouken@0
    79
slouken@1895
    80
    case AUDIO_U16:
slouken@1895
    81
        {
slouken@1895
    82
            Uint8 *src, *dst;
slouken@0
    83
slouken@1895
    84
            src = cvt->buf;
slouken@1895
    85
            dst = cvt->buf;
icculus@1982
    86
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
    87
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
    88
                    sample = (Uint16) ((src[0] << 8) | src[1]) +
slouken@1895
    89
                        (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
    90
                    if (sample > 65535) {
slouken@1895
    91
                        dst[0] = 0xFF;
slouken@1895
    92
                        dst[1] = 0xFF;
slouken@1895
    93
                    } else {
slouken@1895
    94
                        dst[1] = (sample & 0xFF);
slouken@1895
    95
                        sample >>= 8;
slouken@1895
    96
                        dst[0] = (sample & 0xFF);
slouken@1895
    97
                    }
slouken@1895
    98
                    src += 4;
slouken@1895
    99
                    dst += 2;
slouken@1895
   100
                }
slouken@1895
   101
            } else {
slouken@1895
   102
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   103
                    sample = (Uint16) ((src[1] << 8) | src[0]) +
slouken@1895
   104
                        (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   105
                    if (sample > 65535) {
slouken@1895
   106
                        dst[0] = 0xFF;
slouken@1895
   107
                        dst[1] = 0xFF;
slouken@1895
   108
                    } else {
slouken@1895
   109
                        dst[0] = (sample & 0xFF);
slouken@1895
   110
                        sample >>= 8;
slouken@1895
   111
                        dst[1] = (sample & 0xFF);
slouken@1895
   112
                    }
slouken@1895
   113
                    src += 4;
slouken@1895
   114
                    dst += 2;
slouken@1895
   115
                }
slouken@1895
   116
            }
slouken@1895
   117
        }
slouken@1895
   118
        break;
slouken@0
   119
slouken@1895
   120
    case AUDIO_S16:
slouken@1895
   121
        {
slouken@1895
   122
            Uint8 *src, *dst;
slouken@0
   123
slouken@1895
   124
            src = cvt->buf;
slouken@1895
   125
            dst = cvt->buf;
icculus@1982
   126
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   127
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   128
                    sample = (Sint16) ((src[0] << 8) | src[1]) +
slouken@1895
   129
                        (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   130
                    if (sample > 32767) {
slouken@1895
   131
                        dst[0] = 0x7F;
slouken@1895
   132
                        dst[1] = 0xFF;
slouken@1895
   133
                    } else if (sample < -32768) {
slouken@1895
   134
                        dst[0] = 0x80;
slouken@1895
   135
                        dst[1] = 0x00;
slouken@1895
   136
                    } else {
slouken@1895
   137
                        dst[1] = (sample & 0xFF);
slouken@1895
   138
                        sample >>= 8;
slouken@1895
   139
                        dst[0] = (sample & 0xFF);
slouken@1895
   140
                    }
slouken@1895
   141
                    src += 4;
slouken@1895
   142
                    dst += 2;
slouken@1895
   143
                }
slouken@1895
   144
            } else {
slouken@1895
   145
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   146
                    sample = (Sint16) ((src[1] << 8) | src[0]) +
slouken@1895
   147
                        (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   148
                    if (sample > 32767) {
slouken@1895
   149
                        dst[1] = 0x7F;
slouken@1895
   150
                        dst[0] = 0xFF;
slouken@1895
   151
                    } else if (sample < -32768) {
slouken@1895
   152
                        dst[1] = 0x80;
slouken@1895
   153
                        dst[0] = 0x00;
slouken@1895
   154
                    } else {
slouken@1895
   155
                        dst[0] = (sample & 0xFF);
slouken@1895
   156
                        sample >>= 8;
slouken@1895
   157
                        dst[1] = (sample & 0xFF);
slouken@1895
   158
                    }
slouken@1895
   159
                    src += 4;
slouken@1895
   160
                    dst += 2;
slouken@1895
   161
                }
slouken@1895
   162
            }
slouken@1895
   163
        }
slouken@1895
   164
        break;
icculus@1982
   165
icculus@1982
   166
    case AUDIO_S32:
icculus@1982
   167
        {
icculus@1982
   168
            const Uint32 *src = (const Uint32 *) cvt->buf;
icculus@1982
   169
            Uint32 *dst = (Uint32 *) cvt->buf;
icculus@1982
   170
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   171
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@1982
   172
                    const Sint64 added =
slouken@1985
   173
                        (((Sint64) (Sint32) SDL_SwapBE32(src[0])) +
slouken@1985
   174
                         ((Sint64) (Sint32) SDL_SwapBE32(src[1])));
icculus@1982
   175
                    *(dst++) = SDL_SwapBE32((Uint32) ((Sint32) (added >> 1)));
icculus@1982
   176
                }
icculus@1982
   177
            } else {
icculus@1982
   178
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@1982
   179
                    const Sint64 added =
slouken@1985
   180
                        (((Sint64) (Sint32) SDL_SwapLE32(src[0])) +
slouken@1985
   181
                         ((Sint64) (Sint32) SDL_SwapLE32(src[1])));
icculus@1982
   182
                    *(dst++) = SDL_SwapLE32((Uint32) ((Sint32) (added >> 1)));
icculus@1982
   183
                }
icculus@1982
   184
            }
icculus@1982
   185
        }
icculus@1982
   186
        break;
icculus@1982
   187
icculus@1982
   188
    case AUDIO_F32:
icculus@1982
   189
        {
icculus@2014
   190
            const float *src = (const float *) cvt->buf;
icculus@2014
   191
            float *dst = (float *) cvt->buf;
icculus@1982
   192
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   193
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@1982
   194
                    float src1, src2;
icculus@2014
   195
                    src1 = SDL_SwapFloatBE(src[0]);
icculus@2014
   196
                    src2 = SDL_SwapFloatBE(src[1]);
icculus@1982
   197
                    const double added = ((double) src1) + ((double) src2);
icculus@2014
   198
                    src1 = (float) (added * 0.5);
icculus@2014
   199
                    *(dst++) = SDL_SwapFloatBE(src1);
icculus@1982
   200
                }
icculus@1982
   201
            } else {
icculus@1982
   202
                for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@1982
   203
                    float src1, src2;
icculus@2014
   204
                    src1 = SDL_SwapFloatLE(src[0]);
icculus@2014
   205
                    src2 = SDL_SwapFloatLE(src[1]);
icculus@1982
   206
                    const double added = ((double) src1) + ((double) src2);
icculus@2014
   207
                    src1 = (float) (added * 0.5);
icculus@2014
   208
                    *(dst++) = SDL_SwapFloatLE(src1);
icculus@1982
   209
                }
icculus@1982
   210
            }
icculus@1982
   211
        }
icculus@1982
   212
        break;
slouken@1895
   213
    }
icculus@1982
   214
slouken@1895
   215
    cvt->len_cvt /= 2;
slouken@1895
   216
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   217
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   218
    }
slouken@0
   219
}
slouken@0
   220
icculus@1982
   221
slouken@942
   222
/* Discard top 4 channels */
icculus@1982
   223
static void SDLCALL
icculus@1982
   224
SDL_ConvertStrip(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   225
{
slouken@1895
   226
    int i;
slouken@942
   227
slouken@942
   228
#ifdef DEBUG_CONVERT
icculus@1982
   229
    fprintf(stderr, "Converting down from 6 channels to stereo\n");
slouken@942
   230
#endif
slouken@942
   231
slouken@1985
   232
#define strip_chans_6_to_2(type) \
icculus@1982
   233
    { \
icculus@1982
   234
        const type *src = (const type *) cvt->buf; \
icculus@1982
   235
        type *dst = (type *) cvt->buf; \
icculus@1982
   236
        for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
icculus@1982
   237
            dst[0] = src[0]; \
icculus@1982
   238
            dst[1] = src[1]; \
icculus@1982
   239
            src += 6; \
icculus@1982
   240
            dst += 2; \
icculus@1982
   241
        } \
icculus@1982
   242
    }
slouken@942
   243
icculus@1982
   244
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   245
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   246
    case 8:
slouken@1985
   247
        strip_chans_6_to_2(Uint8);
slouken@1985
   248
        break;
slouken@1985
   249
    case 16:
slouken@1985
   250
        strip_chans_6_to_2(Uint16);
slouken@1985
   251
        break;
slouken@1985
   252
    case 32:
slouken@1985
   253
        strip_chans_6_to_2(Uint32);
slouken@1985
   254
        break;
icculus@1982
   255
    }
slouken@942
   256
slouken@1985
   257
#undef strip_chans_6_to_2
slouken@942
   258
slouken@1895
   259
    cvt->len_cvt /= 3;
slouken@1895
   260
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   261
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   262
    }
slouken@942
   263
}
slouken@942
   264
slouken@942
   265
slouken@942
   266
/* Discard top 2 channels of 6 */
icculus@1982
   267
static void SDLCALL
icculus@1982
   268
SDL_ConvertStrip_2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   269
{
slouken@1895
   270
    int i;
slouken@942
   271
slouken@942
   272
#ifdef DEBUG_CONVERT
slouken@1895
   273
    fprintf(stderr, "Converting 6 down to quad\n");
slouken@942
   274
#endif
slouken@942
   275
slouken@1985
   276
#define strip_chans_6_to_4(type) \
icculus@1982
   277
    { \
icculus@1982
   278
        const type *src = (const type *) cvt->buf; \
icculus@1982
   279
        type *dst = (type *) cvt->buf; \
icculus@1982
   280
        for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
icculus@1982
   281
            dst[0] = src[0]; \
icculus@1982
   282
            dst[1] = src[1]; \
icculus@1982
   283
            dst[2] = src[2]; \
icculus@1982
   284
            dst[3] = src[3]; \
icculus@1982
   285
            src += 6; \
icculus@1982
   286
            dst += 4; \
icculus@1982
   287
        } \
icculus@1982
   288
    }
slouken@942
   289
icculus@1982
   290
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   291
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   292
    case 8:
slouken@1985
   293
        strip_chans_6_to_4(Uint8);
slouken@1985
   294
        break;
slouken@1985
   295
    case 16:
slouken@1985
   296
        strip_chans_6_to_4(Uint16);
slouken@1985
   297
        break;
slouken@1985
   298
    case 32:
slouken@1985
   299
        strip_chans_6_to_4(Uint32);
slouken@1985
   300
        break;
icculus@1982
   301
    }
slouken@942
   302
slouken@1985
   303
#undef strip_chans_6_to_4
slouken@942
   304
icculus@1982
   305
    cvt->len_cvt /= 6;
icculus@1982
   306
    cvt->len_cvt *= 4;
slouken@1895
   307
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   308
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   309
    }
slouken@942
   310
}
slouken@0
   311
slouken@0
   312
/* Duplicate a mono channel to both stereo channels */
icculus@1982
   313
static void SDLCALL
icculus@1982
   314
SDL_ConvertStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
   315
{
slouken@1895
   316
    int i;
slouken@0
   317
slouken@0
   318
#ifdef DEBUG_CONVERT
slouken@1895
   319
    fprintf(stderr, "Converting to stereo\n");
slouken@0
   320
#endif
slouken@0
   321
slouken@1985
   322
#define dup_chans_1_to_2(type) \
icculus@1982
   323
    { \
icculus@1982
   324
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   325
        type *dst = (type *) (cvt->buf + cvt->len_cvt * 2); \
icculus@1982
   326
        for (i = cvt->len_cvt / 2; i; --i, --src) { \
icculus@1982
   327
            const type val = *src; \
icculus@1982
   328
            dst -= 2; \
icculus@1982
   329
            dst[0] = dst[1] = val; \
icculus@1982
   330
        } \
icculus@1982
   331
    }
slouken@0
   332
icculus@1982
   333
    /* this function only cares about typesize, and data as a block of bits. */
icculus@1982
   334
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1985
   335
    case 8:
slouken@1985
   336
        dup_chans_1_to_2(Uint8);
slouken@1985
   337
        break;
slouken@1985
   338
    case 16:
slouken@1985
   339
        dup_chans_1_to_2(Uint16);
slouken@1985
   340
        break;
slouken@1985
   341
    case 32:
slouken@1985
   342
        dup_chans_1_to_2(Uint32);
slouken@1985
   343
        break;
slouken@1895
   344
    }
icculus@1982
   345
slouken@1985
   346
#undef dup_chans_1_to_2
icculus@1982
   347
slouken@1895
   348
    cvt->len_cvt *= 2;
slouken@1895
   349
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   350
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   351
    }
slouken@0
   352
}
slouken@0
   353
slouken@942
   354
slouken@942
   355
/* Duplicate a stereo channel to a pseudo-5.1 stream */
icculus@1982
   356
static void SDLCALL
icculus@1982
   357
SDL_ConvertSurround(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   358
{
slouken@1895
   359
    int i;
slouken@942
   360
slouken@942
   361
#ifdef DEBUG_CONVERT
slouken@1895
   362
    fprintf(stderr, "Converting stereo to surround\n");
slouken@942
   363
#endif
slouken@942
   364
slouken@1985
   365
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
   366
    case AUDIO_U8:
slouken@1895
   367
        {
slouken@1895
   368
            Uint8 *src, *dst, lf, rf, ce;
slouken@942
   369
slouken@1895
   370
            src = (Uint8 *) (cvt->buf + cvt->len_cvt);
slouken@1895
   371
            dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 3);
slouken@1895
   372
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   373
                dst -= 6;
slouken@1895
   374
                src -= 2;
slouken@1895
   375
                lf = src[0];
slouken@1895
   376
                rf = src[1];
slouken@1895
   377
                ce = (lf / 2) + (rf / 2);
slouken@1895
   378
                dst[0] = lf;
slouken@1895
   379
                dst[1] = rf;
slouken@1895
   380
                dst[2] = lf - ce;
slouken@1895
   381
                dst[3] = rf - ce;
slouken@1895
   382
                dst[4] = ce;
slouken@1895
   383
                dst[5] = ce;
slouken@1895
   384
            }
slouken@1895
   385
        }
slouken@1895
   386
        break;
slouken@942
   387
slouken@1895
   388
    case AUDIO_S8:
slouken@1895
   389
        {
slouken@1895
   390
            Sint8 *src, *dst, lf, rf, ce;
slouken@942
   391
slouken@1895
   392
            src = (Sint8 *) cvt->buf + cvt->len_cvt;
slouken@1895
   393
            dst = (Sint8 *) cvt->buf + cvt->len_cvt * 3;
slouken@1895
   394
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   395
                dst -= 6;
slouken@1895
   396
                src -= 2;
slouken@1895
   397
                lf = src[0];
slouken@1895
   398
                rf = src[1];
slouken@1895
   399
                ce = (lf / 2) + (rf / 2);
slouken@1895
   400
                dst[0] = lf;
slouken@1895
   401
                dst[1] = rf;
slouken@1895
   402
                dst[2] = lf - ce;
slouken@1895
   403
                dst[3] = rf - ce;
slouken@1895
   404
                dst[4] = ce;
slouken@1895
   405
                dst[5] = ce;
slouken@1895
   406
            }
slouken@1895
   407
        }
slouken@1895
   408
        break;
slouken@942
   409
slouken@1895
   410
    case AUDIO_U16:
slouken@1895
   411
        {
slouken@1895
   412
            Uint8 *src, *dst;
slouken@1895
   413
            Uint16 lf, rf, ce, lr, rr;
slouken@942
   414
slouken@1895
   415
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   416
            dst = cvt->buf + cvt->len_cvt * 3;
slouken@942
   417
icculus@1982
   418
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   419
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   420
                    dst -= 12;
slouken@1895
   421
                    src -= 4;
slouken@1895
   422
                    lf = (Uint16) ((src[0] << 8) | src[1]);
slouken@1895
   423
                    rf = (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
   424
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   425
                    rr = lf - ce;
slouken@1895
   426
                    lr = rf - ce;
slouken@1895
   427
                    dst[1] = (lf & 0xFF);
slouken@1895
   428
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   429
                    dst[3] = (rf & 0xFF);
slouken@1895
   430
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   431
slouken@1895
   432
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   433
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   434
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   435
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   436
slouken@1895
   437
                    dst[1 + 8] = (ce & 0xFF);
slouken@1895
   438
                    dst[0 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   439
                    dst[3 + 8] = (ce & 0xFF);
slouken@1895
   440
                    dst[2 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   441
                }
slouken@1895
   442
            } else {
slouken@1895
   443
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   444
                    dst -= 12;
slouken@1895
   445
                    src -= 4;
slouken@1895
   446
                    lf = (Uint16) ((src[1] << 8) | src[0]);
slouken@1895
   447
                    rf = (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   448
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   449
                    rr = lf - ce;
slouken@1895
   450
                    lr = rf - ce;
slouken@1895
   451
                    dst[0] = (lf & 0xFF);
slouken@1895
   452
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   453
                    dst[2] = (rf & 0xFF);
slouken@1895
   454
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   455
slouken@1895
   456
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   457
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   458
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   459
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   460
slouken@1895
   461
                    dst[0 + 8] = (ce & 0xFF);
slouken@1895
   462
                    dst[1 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   463
                    dst[2 + 8] = (ce & 0xFF);
slouken@1895
   464
                    dst[3 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   465
                }
slouken@1895
   466
            }
slouken@1895
   467
        }
slouken@1895
   468
        break;
slouken@942
   469
slouken@1895
   470
    case AUDIO_S16:
slouken@1895
   471
        {
slouken@1895
   472
            Uint8 *src, *dst;
slouken@1895
   473
            Sint16 lf, rf, ce, lr, rr;
slouken@942
   474
slouken@1895
   475
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   476
            dst = cvt->buf + cvt->len_cvt * 3;
slouken@942
   477
icculus@1982
   478
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   479
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   480
                    dst -= 12;
slouken@1895
   481
                    src -= 4;
slouken@1895
   482
                    lf = (Sint16) ((src[0] << 8) | src[1]);
slouken@1895
   483
                    rf = (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   484
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   485
                    rr = lf - ce;
slouken@1895
   486
                    lr = rf - ce;
slouken@1895
   487
                    dst[1] = (lf & 0xFF);
slouken@1895
   488
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   489
                    dst[3] = (rf & 0xFF);
slouken@1895
   490
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   491
slouken@1895
   492
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   493
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   494
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   495
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   496
slouken@1895
   497
                    dst[1 + 8] = (ce & 0xFF);
slouken@1895
   498
                    dst[0 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   499
                    dst[3 + 8] = (ce & 0xFF);
slouken@1895
   500
                    dst[2 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   501
                }
slouken@1895
   502
            } else {
slouken@1895
   503
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   504
                    dst -= 12;
slouken@1895
   505
                    src -= 4;
slouken@1895
   506
                    lf = (Sint16) ((src[1] << 8) | src[0]);
slouken@1895
   507
                    rf = (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   508
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   509
                    rr = lf - ce;
slouken@1895
   510
                    lr = rf - ce;
slouken@1895
   511
                    dst[0] = (lf & 0xFF);
slouken@1895
   512
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   513
                    dst[2] = (rf & 0xFF);
slouken@1895
   514
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   515
slouken@1895
   516
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   517
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   518
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   519
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@942
   520
slouken@1895
   521
                    dst[0 + 8] = (ce & 0xFF);
slouken@1895
   522
                    dst[1 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   523
                    dst[2 + 8] = (ce & 0xFF);
slouken@1895
   524
                    dst[3 + 8] = ((ce >> 8) & 0xFF);
slouken@1895
   525
                }
slouken@1895
   526
            }
slouken@1895
   527
        }
slouken@1895
   528
        break;
icculus@1982
   529
icculus@1982
   530
    case AUDIO_S32:
icculus@1982
   531
        {
icculus@1982
   532
            Sint32 lf, rf, ce;
icculus@1982
   533
            const Uint32 *src = (const Uint32 *) cvt->buf + cvt->len_cvt;
icculus@1982
   534
            Uint32 *dst = (Uint32 *) cvt->buf + cvt->len_cvt * 3;
icculus@1982
   535
icculus@1982
   536
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   537
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   538
                    dst -= 6;
icculus@1982
   539
                    src -= 2;
icculus@1982
   540
                    lf = (Sint32) SDL_SwapBE32(src[0]);
icculus@1982
   541
                    rf = (Sint32) SDL_SwapBE32(src[1]);
icculus@1982
   542
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   543
                    dst[0] = SDL_SwapBE32((Uint32) lf);
icculus@1982
   544
                    dst[1] = SDL_SwapBE32((Uint32) rf);
icculus@1982
   545
                    dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
icculus@1982
   546
                    dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
icculus@1982
   547
                    dst[4] = SDL_SwapBE32((Uint32) ce);
icculus@1982
   548
                    dst[5] = SDL_SwapBE32((Uint32) ce);
icculus@1982
   549
                }
icculus@1982
   550
            } else {
icculus@1982
   551
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   552
                    dst -= 6;
icculus@1982
   553
                    src -= 2;
icculus@1982
   554
                    lf = (Sint32) SDL_SwapLE32(src[0]);
icculus@1982
   555
                    rf = (Sint32) SDL_SwapLE32(src[1]);
icculus@1982
   556
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   557
                    dst[0] = src[0];
icculus@1982
   558
                    dst[1] = src[1];
icculus@1982
   559
                    dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
icculus@1982
   560
                    dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
icculus@1982
   561
                    dst[4] = SDL_SwapLE32((Uint32) ce);
icculus@1982
   562
                    dst[5] = SDL_SwapLE32((Uint32) ce);
icculus@1982
   563
                }
icculus@1982
   564
            }
icculus@1982
   565
        }
icculus@1982
   566
        break;
icculus@1982
   567
icculus@1982
   568
    case AUDIO_F32:
icculus@1982
   569
        {
icculus@1982
   570
            float lf, rf, ce;
icculus@2014
   571
            const float *src = (const float *) cvt->buf + cvt->len_cvt;
icculus@2014
   572
            float *dst = (float *) cvt->buf + cvt->len_cvt * 3;
icculus@1982
   573
icculus@1982
   574
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   575
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   576
                    dst -= 6;
icculus@1982
   577
                    src -= 2;
icculus@2014
   578
                    lf = SDL_SwapFloatBE(src[0]);
icculus@2014
   579
                    rf = SDL_SwapFloatBE(src[1]);
icculus@1982
   580
                    ce = (lf * 0.5f) + (rf * 0.5f);
icculus@1982
   581
                    dst[0] = src[0];
icculus@1982
   582
                    dst[1] = src[1];
icculus@2014
   583
                    dst[2] = SDL_SwapFloatBE(lf - ce);
icculus@2014
   584
                    dst[3] = SDL_SwapFloatBE(rf - ce);
icculus@2014
   585
                    dst[4] = dst[5] = SDL_SwapFloatBE(ce);
icculus@1982
   586
                }
icculus@1982
   587
            } else {
icculus@1982
   588
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   589
                    dst -= 6;
icculus@1982
   590
                    src -= 2;
icculus@2014
   591
                    lf = SDL_SwapFloatLE(src[0]);
icculus@2014
   592
                    rf = SDL_SwapFloatLE(src[1]);
icculus@1982
   593
                    ce = (lf * 0.5f) + (rf * 0.5f);
icculus@1982
   594
                    dst[0] = src[0];
icculus@1982
   595
                    dst[1] = src[1];
icculus@2014
   596
                    dst[2] = SDL_SwapFloatLE(lf - ce);
icculus@2014
   597
                    dst[3] = SDL_SwapFloatLE(rf - ce);
icculus@2014
   598
                    dst[4] = dst[5] = SDL_SwapFloatLE(ce);
icculus@1982
   599
                }
icculus@1982
   600
            }
icculus@1982
   601
        }
icculus@1982
   602
        break;
icculus@1982
   603
slouken@1895
   604
    }
slouken@1895
   605
    cvt->len_cvt *= 3;
slouken@1895
   606
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   607
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   608
    }
slouken@942
   609
}
slouken@942
   610
slouken@942
   611
slouken@942
   612
/* Duplicate a stereo channel to a pseudo-4.0 stream */
icculus@1982
   613
static void SDLCALL
icculus@1982
   614
SDL_ConvertSurround_4(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   615
{
slouken@1895
   616
    int i;
slouken@942
   617
slouken@942
   618
#ifdef DEBUG_CONVERT
slouken@1895
   619
    fprintf(stderr, "Converting stereo to quad\n");
slouken@942
   620
#endif
slouken@942
   621
slouken@1985
   622
    switch (format & (SDL_AUDIO_MASK_SIGNED | SDL_AUDIO_MASK_BITSIZE)) {
slouken@1895
   623
    case AUDIO_U8:
slouken@1895
   624
        {
slouken@1895
   625
            Uint8 *src, *dst, lf, rf, ce;
slouken@942
   626
slouken@1895
   627
            src = (Uint8 *) (cvt->buf + cvt->len_cvt);
slouken@1895
   628
            dst = (Uint8 *) (cvt->buf + cvt->len_cvt * 2);
slouken@1895
   629
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   630
                dst -= 4;
slouken@1895
   631
                src -= 2;
slouken@1895
   632
                lf = src[0];
slouken@1895
   633
                rf = src[1];
slouken@1895
   634
                ce = (lf / 2) + (rf / 2);
slouken@1895
   635
                dst[0] = lf;
slouken@1895
   636
                dst[1] = rf;
slouken@1895
   637
                dst[2] = lf - ce;
slouken@1895
   638
                dst[3] = rf - ce;
slouken@1895
   639
            }
slouken@1895
   640
        }
slouken@1895
   641
        break;
slouken@942
   642
slouken@1895
   643
    case AUDIO_S8:
slouken@1895
   644
        {
slouken@1895
   645
            Sint8 *src, *dst, lf, rf, ce;
slouken@942
   646
slouken@1895
   647
            src = (Sint8 *) cvt->buf + cvt->len_cvt;
slouken@1895
   648
            dst = (Sint8 *) cvt->buf + cvt->len_cvt * 2;
slouken@1895
   649
            for (i = cvt->len_cvt; i; --i) {
slouken@1895
   650
                dst -= 4;
slouken@1895
   651
                src -= 2;
slouken@1895
   652
                lf = src[0];
slouken@1895
   653
                rf = src[1];
slouken@1895
   654
                ce = (lf / 2) + (rf / 2);
slouken@1895
   655
                dst[0] = lf;
slouken@1895
   656
                dst[1] = rf;
slouken@1895
   657
                dst[2] = lf - ce;
slouken@1895
   658
                dst[3] = rf - ce;
slouken@1895
   659
            }
slouken@1895
   660
        }
slouken@1895
   661
        break;
slouken@942
   662
slouken@1895
   663
    case AUDIO_U16:
slouken@1895
   664
        {
slouken@1895
   665
            Uint8 *src, *dst;
slouken@1895
   666
            Uint16 lf, rf, ce, lr, rr;
slouken@942
   667
slouken@1895
   668
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   669
            dst = cvt->buf + cvt->len_cvt * 2;
slouken@942
   670
icculus@1982
   671
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   672
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   673
                    dst -= 8;
slouken@1895
   674
                    src -= 4;
slouken@1895
   675
                    lf = (Uint16) ((src[0] << 8) | src[1]);
slouken@1895
   676
                    rf = (Uint16) ((src[2] << 8) | src[3]);
slouken@1895
   677
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   678
                    rr = lf - ce;
slouken@1895
   679
                    lr = rf - ce;
slouken@1895
   680
                    dst[1] = (lf & 0xFF);
slouken@1895
   681
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   682
                    dst[3] = (rf & 0xFF);
slouken@1895
   683
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   684
slouken@1895
   685
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   686
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   687
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   688
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   689
                }
slouken@1895
   690
            } else {
slouken@1895
   691
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   692
                    dst -= 8;
slouken@1895
   693
                    src -= 4;
slouken@1895
   694
                    lf = (Uint16) ((src[1] << 8) | src[0]);
slouken@1895
   695
                    rf = (Uint16) ((src[3] << 8) | src[2]);
slouken@1895
   696
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   697
                    rr = lf - ce;
slouken@1895
   698
                    lr = rf - ce;
slouken@1895
   699
                    dst[0] = (lf & 0xFF);
slouken@1895
   700
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   701
                    dst[2] = (rf & 0xFF);
slouken@1895
   702
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   703
slouken@1895
   704
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   705
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   706
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   707
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   708
                }
slouken@1895
   709
            }
slouken@1895
   710
        }
slouken@1895
   711
        break;
slouken@942
   712
slouken@1895
   713
    case AUDIO_S16:
slouken@1895
   714
        {
slouken@1895
   715
            Uint8 *src, *dst;
slouken@1895
   716
            Sint16 lf, rf, ce, lr, rr;
slouken@942
   717
slouken@1895
   718
            src = cvt->buf + cvt->len_cvt;
slouken@1895
   719
            dst = cvt->buf + cvt->len_cvt * 2;
slouken@942
   720
icculus@1982
   721
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
slouken@1895
   722
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   723
                    dst -= 8;
slouken@1895
   724
                    src -= 4;
slouken@1895
   725
                    lf = (Sint16) ((src[0] << 8) | src[1]);
slouken@1895
   726
                    rf = (Sint16) ((src[2] << 8) | src[3]);
slouken@1895
   727
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   728
                    rr = lf - ce;
slouken@1895
   729
                    lr = rf - ce;
slouken@1895
   730
                    dst[1] = (lf & 0xFF);
slouken@1895
   731
                    dst[0] = ((lf >> 8) & 0xFF);
slouken@1895
   732
                    dst[3] = (rf & 0xFF);
slouken@1895
   733
                    dst[2] = ((rf >> 8) & 0xFF);
slouken@942
   734
slouken@1895
   735
                    dst[1 + 4] = (lr & 0xFF);
slouken@1895
   736
                    dst[0 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   737
                    dst[3 + 4] = (rr & 0xFF);
slouken@1895
   738
                    dst[2 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   739
                }
slouken@1895
   740
            } else {
slouken@1895
   741
                for (i = cvt->len_cvt / 4; i; --i) {
slouken@1895
   742
                    dst -= 8;
slouken@1895
   743
                    src -= 4;
slouken@1895
   744
                    lf = (Sint16) ((src[1] << 8) | src[0]);
slouken@1895
   745
                    rf = (Sint16) ((src[3] << 8) | src[2]);
slouken@1895
   746
                    ce = (lf / 2) + (rf / 2);
slouken@1895
   747
                    rr = lf - ce;
slouken@1895
   748
                    lr = rf - ce;
slouken@1895
   749
                    dst[0] = (lf & 0xFF);
slouken@1895
   750
                    dst[1] = ((lf >> 8) & 0xFF);
slouken@1895
   751
                    dst[2] = (rf & 0xFF);
slouken@1895
   752
                    dst[3] = ((rf >> 8) & 0xFF);
slouken@942
   753
slouken@1895
   754
                    dst[0 + 4] = (lr & 0xFF);
slouken@1895
   755
                    dst[1 + 4] = ((lr >> 8) & 0xFF);
slouken@1895
   756
                    dst[2 + 4] = (rr & 0xFF);
slouken@1895
   757
                    dst[3 + 4] = ((rr >> 8) & 0xFF);
slouken@1895
   758
                }
slouken@1895
   759
            }
slouken@1895
   760
        }
slouken@1895
   761
        break;
slouken@942
   762
icculus@1982
   763
    case AUDIO_S32:
icculus@1982
   764
        {
icculus@1982
   765
            const Uint32 *src = (const Uint32 *) (cvt->buf + cvt->len_cvt);
icculus@1982
   766
            Uint32 *dst = (Uint32 *) (cvt->buf + cvt->len_cvt * 2);
icculus@1982
   767
            Sint32 lf, rf, ce;
slouken@942
   768
icculus@1982
   769
            if (SDL_AUDIO_ISBIGENDIAN(format)) {
icculus@1982
   770
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   771
                    dst -= 4;
icculus@1982
   772
                    src -= 2;
icculus@1982
   773
                    lf = (Sint32) SDL_SwapBE32(src[0]);
icculus@1982
   774
                    rf = (Sint32) SDL_SwapBE32(src[1]);
icculus@1982
   775
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   776
                    dst[0] = src[0];
icculus@1982
   777
                    dst[1] = src[1];
icculus@1982
   778
                    dst[2] = SDL_SwapBE32((Uint32) (lf - ce));
icculus@1982
   779
                    dst[3] = SDL_SwapBE32((Uint32) (rf - ce));
icculus@1982
   780
                }
icculus@1982
   781
            } else {
icculus@1982
   782
                for (i = cvt->len_cvt / 8; i; --i) {
icculus@1982
   783
                    dst -= 4;
icculus@1982
   784
                    src -= 2;
icculus@1982
   785
                    lf = (Sint32) SDL_SwapLE32(src[0]);
icculus@1982
   786
                    rf = (Sint32) SDL_SwapLE32(src[1]);
icculus@1982
   787
                    ce = (lf / 2) + (rf / 2);
icculus@1982
   788
                    dst[0] = src[0];
icculus@1982
   789
                    dst[1] = src[1];
icculus@1982
   790
                    dst[2] = SDL_SwapLE32((Uint32) (lf - ce));
icculus@1982
   791
                    dst[3] = SDL_SwapLE32((Uint32) (rf - ce));
icculus@1982
   792
                }
icculus@1982
   793
            }
slouken@1895
   794
        }
slouken@1895
   795
        break;
slouken@1895
   796
    }
slouken@1895
   797
    cvt->len_cvt *= 2;
slouken@1895
   798
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   799
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   800
    }
slouken@0
   801
}
slouken@0
   802
icculus@1982
   803
/* Convert rate up by multiple of 2 */
icculus@1982
   804
static void SDLCALL
icculus@1982
   805
SDL_RateMUL2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@1982
   806
{
icculus@1982
   807
    int i;
icculus@1982
   808
icculus@1982
   809
#ifdef DEBUG_CONVERT
icculus@1982
   810
    fprintf(stderr, "Converting audio rate * 2 (mono)\n");
icculus@1982
   811
#endif
icculus@1982
   812
slouken@1985
   813
#define mul2_mono(type) { \
icculus@1982
   814
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   815
        type *dst = (type *) (cvt->buf + (cvt->len_cvt * 2)); \
icculus@1982
   816
        for (i = cvt->len_cvt / sizeof (type); i; --i) { \
icculus@1982
   817
            src--; \
icculus@1982
   818
            dst[-1] = dst[-2] = src[0]; \
icculus@1982
   819
            dst -= 2; \
icculus@1982
   820
        } \
icculus@1982
   821
    }
icculus@1982
   822
icculus@1982
   823
    switch (SDL_AUDIO_BITSIZE(format)) {
icculus@1982
   824
    case 8:
icculus@1982
   825
        mul2_mono(Uint8);
icculus@1982
   826
        break;
icculus@1982
   827
    case 16:
icculus@1982
   828
        mul2_mono(Uint16);
icculus@1982
   829
        break;
icculus@1982
   830
    case 32:
icculus@1982
   831
        mul2_mono(Uint32);
icculus@1982
   832
        break;
icculus@1982
   833
    }
icculus@1982
   834
slouken@1985
   835
#undef mul2_mono
icculus@1982
   836
icculus@1982
   837
    cvt->len_cvt *= 2;
icculus@1982
   838
    if (cvt->filters[++cvt->filter_index]) {
icculus@1982
   839
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@1982
   840
    }
icculus@1982
   841
}
icculus@1982
   842
slouken@942
   843
slouken@942
   844
/* Convert rate up by multiple of 2, for stereo */
icculus@1982
   845
static void SDLCALL
icculus@1982
   846
SDL_RateMUL2_c2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   847
{
slouken@1895
   848
    int i;
slouken@942
   849
slouken@942
   850
#ifdef DEBUG_CONVERT
icculus@1982
   851
    fprintf(stderr, "Converting audio rate * 2 (stereo)\n");
slouken@942
   852
#endif
icculus@1982
   853
slouken@1985
   854
#define mul2_stereo(type) { \
icculus@1982
   855
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   856
        type *dst = (type *) (cvt->buf + (cvt->len_cvt * 2)); \
icculus@1982
   857
        for (i = cvt->len_cvt / (sizeof (type) * 2); i; --i) { \
icculus@1982
   858
            const type r = src[-1]; \
icculus@1982
   859
            const type l = src[-2]; \
icculus@1982
   860
            src -= 2; \
icculus@1982
   861
            dst[-1] = r; \
icculus@1982
   862
            dst[-2] = l; \
icculus@1982
   863
            dst[-3] = r; \
icculus@1982
   864
            dst[-4] = l; \
icculus@1982
   865
            dst -= 4; \
icculus@1982
   866
        } \
icculus@1982
   867
    }
icculus@1982
   868
icculus@1982
   869
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
   870
    case 8:
icculus@1982
   871
        mul2_stereo(Uint8);
slouken@1895
   872
        break;
slouken@1895
   873
    case 16:
icculus@1982
   874
        mul2_stereo(Uint16);
icculus@1982
   875
        break;
icculus@1982
   876
    case 32:
icculus@1982
   877
        mul2_stereo(Uint32);
slouken@1895
   878
        break;
slouken@1895
   879
    }
icculus@1982
   880
slouken@1985
   881
#undef mul2_stereo
icculus@1982
   882
slouken@1895
   883
    cvt->len_cvt *= 2;
slouken@1895
   884
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   885
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   886
    }
slouken@942
   887
}
slouken@942
   888
slouken@942
   889
/* Convert rate up by multiple of 2, for quad */
icculus@1982
   890
static void SDLCALL
icculus@1982
   891
SDL_RateMUL2_c4(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   892
{
slouken@1895
   893
    int i;
slouken@942
   894
slouken@942
   895
#ifdef DEBUG_CONVERT
icculus@1982
   896
    fprintf(stderr, "Converting audio rate * 2 (quad)\n");
slouken@942
   897
#endif
icculus@1982
   898
slouken@1985
   899
#define mul2_quad(type) { \
icculus@1982
   900
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   901
        type *dst = (type *) (cvt->buf + (cvt->len_cvt * 2)); \
icculus@1982
   902
        for (i = cvt->len_cvt / (sizeof (type) * 4); i; --i) { \
icculus@1982
   903
            const type c1 = src[-1]; \
icculus@1982
   904
            const type c2 = src[-2]; \
icculus@1982
   905
            const type c3 = src[-3]; \
icculus@1982
   906
            const type c4 = src[-4]; \
icculus@1982
   907
            src -= 4; \
icculus@1982
   908
            dst[-1] = c1; \
icculus@1982
   909
            dst[-2] = c2; \
icculus@1982
   910
            dst[-3] = c3; \
icculus@1982
   911
            dst[-4] = c4; \
icculus@1982
   912
            dst[-5] = c1; \
icculus@1982
   913
            dst[-6] = c2; \
icculus@1982
   914
            dst[-7] = c3; \
icculus@1982
   915
            dst[-8] = c4; \
icculus@1982
   916
            dst -= 8; \
icculus@1982
   917
        } \
icculus@1982
   918
    }
icculus@1982
   919
icculus@1982
   920
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
   921
    case 8:
icculus@1982
   922
        mul2_quad(Uint8);
slouken@1895
   923
        break;
slouken@1895
   924
    case 16:
icculus@1982
   925
        mul2_quad(Uint16);
icculus@1982
   926
        break;
icculus@1982
   927
    case 32:
icculus@1982
   928
        mul2_quad(Uint32);
slouken@1895
   929
        break;
slouken@1895
   930
    }
icculus@1982
   931
slouken@1985
   932
#undef mul2_quad
icculus@1982
   933
slouken@1895
   934
    cvt->len_cvt *= 2;
slouken@1895
   935
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   936
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   937
    }
slouken@942
   938
}
slouken@942
   939
slouken@942
   940
slouken@942
   941
/* Convert rate up by multiple of 2, for 5.1 */
icculus@1982
   942
static void SDLCALL
icculus@1982
   943
SDL_RateMUL2_c6(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   944
{
slouken@1895
   945
    int i;
slouken@942
   946
slouken@942
   947
#ifdef DEBUG_CONVERT
icculus@1982
   948
    fprintf(stderr, "Converting audio rate * 2 (six channels)\n");
slouken@942
   949
#endif
icculus@1982
   950
slouken@1985
   951
#define mul2_chansix(type) { \
icculus@1982
   952
        const type *src = (const type *) (cvt->buf + cvt->len_cvt); \
icculus@1982
   953
        type *dst = (type *) (cvt->buf + (cvt->len_cvt * 2)); \
icculus@1982
   954
        for (i = cvt->len_cvt / (sizeof (type) * 6); i; --i) { \
icculus@1982
   955
            const type c1 = src[-1]; \
icculus@1982
   956
            const type c2 = src[-2]; \
icculus@1982
   957
            const type c3 = src[-3]; \
icculus@1982
   958
            const type c4 = src[-4]; \
icculus@1982
   959
            const type c5 = src[-5]; \
icculus@1982
   960
            const type c6 = src[-6]; \
icculus@1982
   961
            src -= 6; \
icculus@1982
   962
            dst[-1] = c1; \
icculus@1982
   963
            dst[-2] = c2; \
icculus@1982
   964
            dst[-3] = c3; \
icculus@1982
   965
            dst[-4] = c4; \
icculus@1982
   966
            dst[-5] = c5; \
icculus@1982
   967
            dst[-6] = c6; \
icculus@1982
   968
            dst[-7] = c1; \
icculus@1982
   969
            dst[-8] = c2; \
icculus@1982
   970
            dst[-9] = c3; \
icculus@1982
   971
            dst[-10] = c4; \
icculus@1982
   972
            dst[-11] = c5; \
icculus@1982
   973
            dst[-12] = c6; \
icculus@1982
   974
            dst -= 12; \
icculus@1982
   975
        } \
icculus@1982
   976
    }
icculus@1982
   977
icculus@1982
   978
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
   979
    case 8:
icculus@1982
   980
        mul2_chansix(Uint8);
slouken@1895
   981
        break;
slouken@1895
   982
    case 16:
icculus@1982
   983
        mul2_chansix(Uint16);
icculus@1982
   984
        break;
icculus@1982
   985
    case 32:
icculus@1982
   986
        mul2_chansix(Uint32);
slouken@1895
   987
        break;
slouken@1895
   988
    }
icculus@1982
   989
slouken@1985
   990
#undef mul2_chansix
icculus@1982
   991
slouken@1895
   992
    cvt->len_cvt *= 2;
slouken@1895
   993
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   994
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   995
    }
slouken@942
   996
}
slouken@942
   997
slouken@0
   998
/* Convert rate down by multiple of 2 */
icculus@1982
   999
static void SDLCALL
icculus@1982
  1000
SDL_RateDIV2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
  1001
{
slouken@1895
  1002
    int i;
slouken@0
  1003
slouken@0
  1004
#ifdef DEBUG_CONVERT
icculus@1982
  1005
    fprintf(stderr, "Converting audio rate / 2 (mono)\n");
slouken@0
  1006
#endif
icculus@1982
  1007
slouken@1985
  1008
#define div2_mono(type) { \
icculus@1982
  1009
        const type *src = (const type *) cvt->buf; \
icculus@1982
  1010
        type *dst = (type *) cvt->buf; \
icculus@1982
  1011
        for (i = cvt->len_cvt / (sizeof (type) * 2); i; --i) { \
icculus@1982
  1012
            dst[0] = src[0]; \
icculus@1982
  1013
            src += 2; \
icculus@1982
  1014
            dst++; \
icculus@1982
  1015
        } \
icculus@1982
  1016
    }
icculus@1982
  1017
icculus@1982
  1018
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1019
    case 8:
icculus@1982
  1020
        div2_mono(Uint8);
slouken@1895
  1021
        break;
slouken@1895
  1022
    case 16:
icculus@1982
  1023
        div2_mono(Uint16);
icculus@1982
  1024
        break;
icculus@1982
  1025
    case 32:
icculus@1982
  1026
        div2_mono(Uint32);
slouken@1895
  1027
        break;
slouken@1895
  1028
    }
icculus@1982
  1029
slouken@1985
  1030
#undef div2_mono
icculus@1982
  1031
slouken@1895
  1032
    cvt->len_cvt /= 2;
slouken@1895
  1033
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
  1034
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
  1035
    }
slouken@0
  1036
}
slouken@0
  1037
slouken@942
  1038
slouken@942
  1039
/* Convert rate down by multiple of 2, for stereo */
icculus@1982
  1040
static void SDLCALL
icculus@1982
  1041
SDL_RateDIV2_c2(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
  1042
{
slouken@1895
  1043
    int i;
slouken@942
  1044
slouken@942
  1045
#ifdef DEBUG_CONVERT
icculus@1982
  1046
    fprintf(stderr, "Converting audio rate / 2 (stereo)\n");
slouken@942
  1047
#endif
icculus@1982
  1048
slouken@1985
  1049
#define div2_stereo(type) { \
icculus@1982
  1050
        const type *src = (const type *) cvt->buf; \
icculus@1982
  1051
        type *dst = (type *) cvt->buf; \
icculus@1982
  1052
        for (i = cvt->len_cvt / (sizeof (type) * 4); i; --i) { \
icculus@1982
  1053
            dst[0] = src[0]; \
icculus@1982
  1054
            dst[1] = src[1]; \
icculus@1982
  1055
            src += 4; \
icculus@1982
  1056
            dst += 2; \
icculus@1982
  1057
        } \
icculus@1982
  1058
    }
icculus@1982
  1059
icculus@1982
  1060
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1061
    case 8:
icculus@1982
  1062
        div2_stereo(Uint8);
slouken@1895
  1063
        break;
slouken@1895
  1064
    case 16:
icculus@1982
  1065
        div2_stereo(Uint16);
icculus@1982
  1066
        break;
icculus@1982
  1067
    case 32:
icculus@1982
  1068
        div2_stereo(Uint32);
slouken@1895
  1069
        break;
slouken@1895
  1070
    }
icculus@1982
  1071
slouken@1985
  1072
#undef div2_stereo
icculus@1982
  1073
slouken@1895
  1074
    cvt->len_cvt /= 2;
slouken@1895
  1075
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
  1076
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
  1077
    }
slouken@942
  1078
}
slouken@942
  1079
slouken@942
  1080
slouken@942
  1081
/* Convert rate down by multiple of 2, for quad */
icculus@1982
  1082
static void SDLCALL
icculus@1982
  1083
SDL_RateDIV2_c4(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
  1084
{
slouken@1895
  1085
    int i;
slouken@942
  1086
slouken@942
  1087
#ifdef DEBUG_CONVERT
icculus@1982
  1088
    fprintf(stderr, "Converting audio rate / 2 (quad)\n");
slouken@942
  1089
#endif
icculus@1982
  1090
slouken@1985
  1091
#define div2_quad(type) { \
icculus@1982
  1092
        const type *src = (const type *) cvt->buf; \
icculus@1982
  1093
        type *dst = (type *) cvt->buf; \
icculus@1982
  1094
        for (i = cvt->len_cvt / (sizeof (type) * 8); i; --i) { \
icculus@1982
  1095
            dst[0] = src[0]; \
icculus@1982
  1096
            dst[1] = src[1]; \
icculus@1982
  1097
            dst[2] = src[2]; \
icculus@1982
  1098
            dst[3] = src[3]; \
icculus@1982
  1099
            src += 8; \
icculus@1982
  1100
            dst += 4; \
icculus@1982
  1101
        } \
icculus@1982
  1102
    }
icculus@1982
  1103
icculus@1982
  1104
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1105
    case 8:
icculus@1982
  1106
        div2_quad(Uint8);
slouken@1895
  1107
        break;
slouken@1895
  1108
    case 16:
icculus@1982
  1109
        div2_quad(Uint16);
icculus@1982
  1110
        break;
icculus@1982
  1111
    case 32:
icculus@1982
  1112
        div2_quad(Uint32);
slouken@1895
  1113
        break;
slouken@1895
  1114
    }
icculus@1982
  1115
slouken@1985
  1116
#undef div2_quad
icculus@1982
  1117
slouken@1895
  1118
    cvt->len_cvt /= 2;
slouken@1895
  1119
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
  1120
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
  1121
    }
slouken@942
  1122
}
slouken@942
  1123
slouken@942
  1124
/* Convert rate down by multiple of 2, for 5.1 */
icculus@1982
  1125
static void SDLCALL
icculus@1982
  1126
SDL_RateDIV2_c6(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
  1127
{
slouken@1895
  1128
    int i;
slouken@942
  1129
slouken@942
  1130
#ifdef DEBUG_CONVERT
icculus@1982
  1131
    fprintf(stderr, "Converting audio rate / 2 (six channels)\n");
slouken@942
  1132
#endif
icculus@1982
  1133
slouken@1985
  1134
#define div2_chansix(type) { \
icculus@1982
  1135
        const type *src = (const type *) cvt->buf; \
icculus@1982
  1136
        type *dst = (type *) cvt->buf; \
icculus@1982
  1137
        for (i = cvt->len_cvt / (sizeof (type) * 12); i; --i) { \
icculus@1982
  1138
            dst[0] = src[0]; \
icculus@1982
  1139
            dst[1] = src[1]; \
icculus@1982
  1140
            dst[2] = src[2]; \
icculus@1982
  1141
            dst[3] = src[3]; \
icculus@1982
  1142
            dst[4] = src[4]; \
icculus@1982
  1143
            dst[5] = src[5]; \
icculus@1982
  1144
            src += 12; \
icculus@1982
  1145
            dst += 6; \
icculus@1982
  1146
        } \
icculus@1982
  1147
    }
icculus@1982
  1148
icculus@1982
  1149
    switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1150
    case 8:
icculus@1982
  1151
        div2_chansix(Uint8);
slouken@1895
  1152
        break;
slouken@1895
  1153
    case 16:
icculus@1982
  1154
        div2_chansix(Uint16);
icculus@1982
  1155
        break;
icculus@1982
  1156
    case 32:
icculus@1982
  1157
        div2_chansix(Uint32);
slouken@1895
  1158
        break;
slouken@1895
  1159
    }
icculus@1982
  1160
slouken@1985
  1161
#undef div_chansix
icculus@1982
  1162
slouken@1895
  1163
    cvt->len_cvt /= 2;
slouken@1895
  1164
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
  1165
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
  1166
    }
slouken@942
  1167
}
slouken@942
  1168
slouken@0
  1169
/* Very slow rate conversion routine */
icculus@1982
  1170
static void SDLCALL
icculus@1982
  1171
SDL_RateSLOW(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
  1172
{
slouken@1895
  1173
    double ipos;
slouken@1895
  1174
    int i, clen;
slouken@0
  1175
slouken@0
  1176
#ifdef DEBUG_CONVERT
slouken@1895
  1177
    fprintf(stderr, "Converting audio rate * %4.4f\n", 1.0 / cvt->rate_incr);
slouken@0
  1178
#endif
slouken@1895
  1179
    clen = (int) ((double) cvt->len_cvt / cvt->rate_incr);
slouken@1895
  1180
    if (cvt->rate_incr > 1.0) {
icculus@1982
  1181
        switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1182
        case 8:
slouken@1895
  1183
            {
slouken@1895
  1184
                Uint8 *output;
slouken@0
  1185
slouken@1895
  1186
                output = cvt->buf;
slouken@1895
  1187
                ipos = 0.0;
slouken@1895
  1188
                for (i = clen; i; --i) {
slouken@1895
  1189
                    *output = cvt->buf[(int) ipos];
slouken@1895
  1190
                    ipos += cvt->rate_incr;
slouken@1895
  1191
                    output += 1;
slouken@1895
  1192
                }
slouken@1895
  1193
            }
slouken@1895
  1194
            break;
slouken@0
  1195
slouken@1895
  1196
        case 16:
slouken@1895
  1197
            {
slouken@1895
  1198
                Uint16 *output;
slouken@0
  1199
slouken@1895
  1200
                clen &= ~1;
slouken@1895
  1201
                output = (Uint16 *) cvt->buf;
slouken@1895
  1202
                ipos = 0.0;
slouken@1895
  1203
                for (i = clen / 2; i; --i) {
slouken@1895
  1204
                    *output = ((Uint16 *) cvt->buf)[(int) ipos];
slouken@1895
  1205
                    ipos += cvt->rate_incr;
slouken@1895
  1206
                    output += 1;
slouken@1895
  1207
                }
slouken@1895
  1208
            }
slouken@1895
  1209
            break;
icculus@1982
  1210
icculus@1982
  1211
        case 32:
icculus@1982
  1212
            {
icculus@1982
  1213
                /* !!! FIXME: need 32-bit converter here! */
icculus@1982
  1214
                fprintf(stderr, "FIXME: need 32-bit converter here!\n");
icculus@1982
  1215
            }
slouken@1895
  1216
        }
slouken@1895
  1217
    } else {
icculus@1982
  1218
        switch (SDL_AUDIO_BITSIZE(format)) {
slouken@1895
  1219
        case 8:
slouken@1895
  1220
            {
slouken@1895
  1221
                Uint8 *output;
slouken@0
  1222
slouken@1895
  1223
                output = cvt->buf + clen;
slouken@1895
  1224
                ipos = (double) cvt->len_cvt;
slouken@1895
  1225
                for (i = clen; i; --i) {
slouken@1895
  1226
                    ipos -= cvt->rate_incr;
slouken@1895
  1227
                    output -= 1;
slouken@1895
  1228
                    *output = cvt->buf[(int) ipos];
slouken@1895
  1229
                }
slouken@1895
  1230
            }
slouken@1895
  1231
            break;
slouken@0
  1232
slouken@1895
  1233
        case 16:
slouken@1895
  1234
            {
slouken@1895
  1235
                Uint16 *output;
slouken@0
  1236
slouken@1895
  1237
                clen &= ~1;
slouken@1895
  1238
                output = (Uint16 *) (cvt->buf + clen);
slouken@1895
  1239
                ipos = (double) cvt->len_cvt / 2;
slouken@1895
  1240
                for (i = clen / 2; i; --i) {
slouken@1895
  1241
                    ipos -= cvt->rate_incr;
slouken@1895
  1242
                    output -= 1;
slouken@1895
  1243
                    *output = ((Uint16 *) cvt->buf)[(int) ipos];
slouken@1895
  1244
                }
slouken@1895
  1245
            }
slouken@1895
  1246
            break;
icculus@1982
  1247
icculus@1982
  1248
        case 32:
icculus@1982
  1249
            {
icculus@1982
  1250
                /* !!! FIXME: need 32-bit converter here! */
icculus@1982
  1251
                fprintf(stderr, "FIXME: need 32-bit converter here!\n");
icculus@1982
  1252
            }
slouken@1895
  1253
        }
slouken@1895
  1254
    }
icculus@1982
  1255
slouken@1895
  1256
    cvt->len_cvt = clen;
slouken@1895
  1257
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
  1258
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
  1259
    }
slouken@0
  1260
}
slouken@0
  1261
slouken@1895
  1262
int
slouken@1895
  1263
SDL_ConvertAudio(SDL_AudioCVT * cvt)
slouken@0
  1264
{
slouken@1895
  1265
    /* Make sure there's data to convert */
slouken@1895
  1266
    if (cvt->buf == NULL) {
slouken@1895
  1267
        SDL_SetError("No buffer allocated for conversion");
slouken@1895
  1268
        return (-1);
slouken@1895
  1269
    }
slouken@1895
  1270
    /* Return okay if no conversion is necessary */
slouken@1895
  1271
    cvt->len_cvt = cvt->len;
slouken@1895
  1272
    if (cvt->filters[0] == NULL) {
slouken@1895
  1273
        return (0);
slouken@1895
  1274
    }
slouken@0
  1275
slouken@1895
  1276
    /* Set up the conversion and go! */
slouken@1895
  1277
    cvt->filter_index = 0;
slouken@1895
  1278
    cvt->filters[0] (cvt, cvt->src_format);
slouken@1895
  1279
    return (0);
slouken@0
  1280
}
slouken@0
  1281
icculus@1982
  1282
icculus@1982
  1283
static SDL_AudioFilter
icculus@1982
  1284
SDL_HandTunedTypeCVT(SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
icculus@1982
  1285
{
icculus@1982
  1286
    /*
icculus@1982
  1287
     * Fill in any future conversions that are specialized to a
icculus@1982
  1288
     *  processor, platform, compiler, or library here.
icculus@1982
  1289
     */
icculus@1982
  1290
slouken@1985
  1291
    return NULL;                /* no specialized converter code available. */
icculus@1982
  1292
}
icculus@1982
  1293
icculus@1982
  1294
icculus@1982
  1295
/*
icculus@1982
  1296
 * Find a converter between two data types. We try to select a hand-tuned
icculus@1982
  1297
 *  asm/vectorized/optimized function first, and then fallback to an
icculus@1982
  1298
 *  autogenerated function that is customized to convert between two
icculus@1982
  1299
 *  specific data types.
icculus@1982
  1300
 */
icculus@1982
  1301
static int
icculus@1982
  1302
SDL_BuildAudioTypeCVT(SDL_AudioCVT * cvt,
icculus@1982
  1303
                      SDL_AudioFormat src_fmt, SDL_AudioFormat dst_fmt)
icculus@1982
  1304
{
icculus@1982
  1305
    if (src_fmt != dst_fmt) {
icculus@1982
  1306
        const Uint16 src_bitsize = SDL_AUDIO_BITSIZE(src_fmt);
icculus@1982
  1307
        const Uint16 dst_bitsize = SDL_AUDIO_BITSIZE(dst_fmt);
icculus@1982
  1308
        SDL_AudioFilter filter = SDL_HandTunedTypeCVT(src_fmt, dst_fmt);
icculus@1982
  1309
icculus@1982
  1310
        /* No hand-tuned converter? Try the autogenerated ones. */
icculus@1982
  1311
        if (filter == NULL) {
icculus@1982
  1312
            int i;
icculus@1982
  1313
            for (i = 0; sdl_audio_type_filters[i].filter != NULL; i++) {
icculus@1982
  1314
                const SDL_AudioTypeFilters *filt = &sdl_audio_type_filters[i];
icculus@1982
  1315
                if ((filt->src_fmt == src_fmt) && (filt->dst_fmt == dst_fmt)) {
icculus@1982
  1316
                    filter = filt->filter;
icculus@1982
  1317
                    break;
icculus@1982
  1318
                }
icculus@1982
  1319
            }
icculus@1982
  1320
icculus@1982
  1321
            if (filter == NULL) {
slouken@1985
  1322
                return -1;      /* Still no matching converter?! */
icculus@1982
  1323
            }
icculus@1982
  1324
        }
icculus@1982
  1325
icculus@1982
  1326
        /* Update (cvt) with filter details... */
icculus@1982
  1327
        cvt->filters[cvt->filter_index++] = filter;
icculus@1982
  1328
        if (src_bitsize < dst_bitsize) {
icculus@1982
  1329
            const int mult = (dst_bitsize / src_bitsize);
icculus@1982
  1330
            cvt->len_mult *= mult;
icculus@1982
  1331
            cvt->len_ratio *= mult;
icculus@1982
  1332
        } else if (src_bitsize > dst_bitsize) {
icculus@1982
  1333
            cvt->len_ratio /= (src_bitsize / dst_bitsize);
icculus@1982
  1334
        }
icculus@1982
  1335
slouken@1985
  1336
        return 1;               /* added a converter. */
icculus@1982
  1337
    }
icculus@1982
  1338
slouken@1985
  1339
    return 0;                   /* no conversion necessary. */
icculus@1982
  1340
}
icculus@1982
  1341
icculus@1982
  1342
icculus@1982
  1343
icculus@1982
  1344
/* Creates a set of audio filters to convert from one format to another.
icculus@1982
  1345
   Returns -1 if the format conversion is not supported, 0 if there's
icculus@1982
  1346
   no conversion needed, or 1 if the audio filter is set up.
slouken@0
  1347
*/
slouken@1895
  1348
slouken@1895
  1349
int
slouken@1895
  1350
SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
icculus@1982
  1351
                  SDL_AudioFormat src_fmt, Uint8 src_channels, int src_rate,
icculus@1982
  1352
                  SDL_AudioFormat dst_fmt, Uint8 dst_channels, int dst_rate)
slouken@0
  1353
{
icculus@1982
  1354
    /* there are no unsigned types over 16 bits, so catch this upfront. */
icculus@1982
  1355
    if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
icculus@1982
  1356
        return -1;
icculus@1982
  1357
    }
icculus@1982
  1358
    if ((SDL_AUDIO_BITSIZE(dst_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(dst_fmt))) {
icculus@1982
  1359
        return -1;
icculus@1982
  1360
    }
slouken@1985
  1361
#ifdef DEBUG_CONVERT
icculus@1982
  1362
    printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
slouken@1985
  1363
           src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);
slouken@1985
  1364
#endif
icculus@1982
  1365
slouken@1895
  1366
    /* Start off with no conversion necessary */
icculus@1982
  1367
icculus@1982
  1368
    cvt->src_format = src_fmt;
icculus@1982
  1369
    cvt->dst_format = dst_fmt;
slouken@1895
  1370
    cvt->needed = 0;
slouken@1895
  1371
    cvt->filter_index = 0;
slouken@1895
  1372
    cvt->filters[0] = NULL;
slouken@1895
  1373
    cvt->len_mult = 1;
slouken@1895
  1374
    cvt->len_ratio = 1.0;
slouken@0
  1375
icculus@1982
  1376
    /* Convert data types, if necessary. Updates (cvt). */
icculus@1982
  1377
    if (SDL_BuildAudioTypeCVT(cvt, src_fmt, dst_fmt) == -1)
slouken@1985
  1378
        return -1;              /* shouldn't happen, but just in case... */
slouken@0
  1379
icculus@1982
  1380
    /* Channel conversion */
slouken@1895
  1381
    if (src_channels != dst_channels) {
slouken@1895
  1382
        if ((src_channels == 1) && (dst_channels > 1)) {
slouken@1895
  1383
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
slouken@1895
  1384
            cvt->len_mult *= 2;
slouken@1895
  1385
            src_channels = 2;
slouken@1895
  1386
            cvt->len_ratio *= 2;
slouken@1895
  1387
        }
slouken@1895
  1388
        if ((src_channels == 2) && (dst_channels == 6)) {
slouken@1895
  1389
            cvt->filters[cvt->filter_index++] = SDL_ConvertSurround;
slouken@1895
  1390
            src_channels = 6;
slouken@1895
  1391
            cvt->len_mult *= 3;
slouken@1895
  1392
            cvt->len_ratio *= 3;
slouken@1895
  1393
        }
slouken@1895
  1394
        if ((src_channels == 2) && (dst_channels == 4)) {
slouken@1895
  1395
            cvt->filters[cvt->filter_index++] = SDL_ConvertSurround_4;
slouken@1895
  1396
            src_channels = 4;
slouken@1895
  1397
            cvt->len_mult *= 2;
slouken@1895
  1398
            cvt->len_ratio *= 2;
slouken@1895
  1399
        }
slouken@1895
  1400
        while ((src_channels * 2) <= dst_channels) {
slouken@1895
  1401
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereo;
slouken@1895
  1402
            cvt->len_mult *= 2;
slouken@1895
  1403
            src_channels *= 2;
slouken@1895
  1404
            cvt->len_ratio *= 2;
slouken@1895
  1405
        }
slouken@1895
  1406
        if ((src_channels == 6) && (dst_channels <= 2)) {
slouken@1895
  1407
            cvt->filters[cvt->filter_index++] = SDL_ConvertStrip;
slouken@1895
  1408
            src_channels = 2;
slouken@1895
  1409
            cvt->len_ratio /= 3;
slouken@1895
  1410
        }
slouken@1895
  1411
        if ((src_channels == 6) && (dst_channels == 4)) {
slouken@1895
  1412
            cvt->filters[cvt->filter_index++] = SDL_ConvertStrip_2;
slouken@1895
  1413
            src_channels = 4;
slouken@1895
  1414
            cvt->len_ratio /= 2;
slouken@1895
  1415
        }
slouken@1895
  1416
        /* This assumes that 4 channel audio is in the format:
slouken@1895
  1417
           Left {front/back} + Right {front/back}
slouken@1895
  1418
           so converting to L/R stereo works properly.
slouken@1895
  1419
         */
slouken@1895
  1420
        while (((src_channels % 2) == 0) &&
slouken@1895
  1421
               ((src_channels / 2) >= dst_channels)) {
slouken@1895
  1422
            cvt->filters[cvt->filter_index++] = SDL_ConvertMono;
slouken@1895
  1423
            src_channels /= 2;
slouken@1895
  1424
            cvt->len_ratio /= 2;
slouken@1895
  1425
        }
slouken@1895
  1426
        if (src_channels != dst_channels) {
slouken@1895
  1427
            /* Uh oh.. */ ;
slouken@1895
  1428
        }
slouken@1895
  1429
    }
slouken@0
  1430
slouken@1895
  1431
    /* Do rate conversion */
slouken@1895
  1432
    cvt->rate_incr = 0.0;
slouken@1895
  1433
    if ((src_rate / 100) != (dst_rate / 100)) {
slouken@1895
  1434
        Uint32 hi_rate, lo_rate;
slouken@1895
  1435
        int len_mult;
slouken@1895
  1436
        double len_ratio;
icculus@1982
  1437
        SDL_AudioFilter rate_cvt = NULL;
slouken@1895
  1438
slouken@1895
  1439
        if (src_rate > dst_rate) {
slouken@1895
  1440
            hi_rate = src_rate;
slouken@1895
  1441
            lo_rate = dst_rate;
slouken@1895
  1442
            switch (src_channels) {
slouken@1895
  1443
            case 1:
slouken@1895
  1444
                rate_cvt = SDL_RateDIV2;
slouken@1895
  1445
                break;
slouken@1895
  1446
            case 2:
slouken@1895
  1447
                rate_cvt = SDL_RateDIV2_c2;
slouken@1895
  1448
                break;
slouken@1895
  1449
            case 4:
slouken@1895
  1450
                rate_cvt = SDL_RateDIV2_c4;
slouken@1895
  1451
                break;
slouken@1895
  1452
            case 6:
slouken@1895
  1453
                rate_cvt = SDL_RateDIV2_c6;
slouken@1895
  1454
                break;
slouken@1895
  1455
            default:
slouken@1895
  1456
                return -1;
slouken@1895
  1457
            }
slouken@1895
  1458
            len_mult = 1;
slouken@1895
  1459
            len_ratio = 0.5;
slouken@1895
  1460
        } else {
slouken@1895
  1461
            hi_rate = dst_rate;
slouken@1895
  1462
            lo_rate = src_rate;
slouken@1895
  1463
            switch (src_channels) {
slouken@1895
  1464
            case 1:
slouken@1895
  1465
                rate_cvt = SDL_RateMUL2;
slouken@1895
  1466
                break;
slouken@1895
  1467
            case 2:
slouken@1895
  1468
                rate_cvt = SDL_RateMUL2_c2;
slouken@1895
  1469
                break;
slouken@1895
  1470
            case 4:
slouken@1895
  1471
                rate_cvt = SDL_RateMUL2_c4;
slouken@1895
  1472
                break;
slouken@1895
  1473
            case 6:
slouken@1895
  1474
                rate_cvt = SDL_RateMUL2_c6;
slouken@1895
  1475
                break;
slouken@1895
  1476
            default:
slouken@1895
  1477
                return -1;
slouken@1895
  1478
            }
slouken@1895
  1479
            len_mult = 2;
slouken@1895
  1480
            len_ratio = 2.0;
slouken@1895
  1481
        }
slouken@1895
  1482
        /* If hi_rate = lo_rate*2^x then conversion is easy */
slouken@1895
  1483
        while (((lo_rate * 2) / 100) <= (hi_rate / 100)) {
slouken@1895
  1484
            cvt->filters[cvt->filter_index++] = rate_cvt;
slouken@1895
  1485
            cvt->len_mult *= len_mult;
slouken@1895
  1486
            lo_rate *= 2;
slouken@1895
  1487
            cvt->len_ratio *= len_ratio;
slouken@1895
  1488
        }
slouken@1895
  1489
        /* We may need a slow conversion here to finish up */
slouken@1895
  1490
        if ((lo_rate / 100) != (hi_rate / 100)) {
slouken@0
  1491
#if 1
slouken@1895
  1492
            /* The problem with this is that if the input buffer is
slouken@1895
  1493
               say 1K, and the conversion rate is say 1.1, then the
slouken@1895
  1494
               output buffer is 1.1K, which may not be an acceptable
slouken@1895
  1495
               buffer size for the audio driver (not a power of 2)
slouken@1895
  1496
             */
slouken@1895
  1497
            /* For now, punt and hope the rate distortion isn't great.
slouken@1895
  1498
             */
slouken@0
  1499
#else
slouken@1895
  1500
            if (src_rate < dst_rate) {
slouken@1895
  1501
                cvt->rate_incr = (double) lo_rate / hi_rate;
slouken@1895
  1502
                cvt->len_mult *= 2;
slouken@1895
  1503
                cvt->len_ratio /= cvt->rate_incr;
slouken@1895
  1504
            } else {
slouken@1895
  1505
                cvt->rate_incr = (double) hi_rate / lo_rate;
slouken@1895
  1506
                cvt->len_ratio *= cvt->rate_incr;
slouken@1895
  1507
            }
slouken@1895
  1508
            cvt->filters[cvt->filter_index++] = SDL_RateSLOW;
slouken@0
  1509
#endif
slouken@1895
  1510
        }
slouken@1895
  1511
    }
slouken@0
  1512
slouken@1895
  1513
    /* Set up the filter information */
slouken@1895
  1514
    if (cvt->filter_index != 0) {
slouken@1895
  1515
        cvt->needed = 1;
icculus@1982
  1516
        cvt->src_format = src_fmt;
icculus@1982
  1517
        cvt->dst_format = dst_fmt;
slouken@1895
  1518
        cvt->len = 0;
slouken@1895
  1519
        cvt->buf = NULL;
slouken@1895
  1520
        cvt->filters[cvt->filter_index] = NULL;
slouken@1895
  1521
    }
slouken@1895
  1522
    return (cvt->needed);
slouken@0
  1523
}
slouken@1895
  1524
slouken@1895
  1525
/* vi: set ts=4 sw=4 expandtab: */