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