src/audio/SDL_audiocvt.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 23 Jan 2017 00:57:19 -0500
changeset 10832 189266031c6f
parent 10831 fcbb4d7f2344
child 10833 86f6353f1aae
permissions -rw-r--r--
audio: Added SSE3 implementation of SDL_ConvertStereoToMono().
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@2728
    22
slouken@0
    23
/* Functions for audio drivers to perform runtime conversion of audio format */
slouken@0
    24
slouken@0
    25
#include "SDL_audio.h"
icculus@1982
    26
#include "SDL_audio_c.h"
slouken@0
    27
slouken@10773
    28
#include "SDL_loadso.h"
icculus@6281
    29
#include "SDL_assert.h"
icculus@10757
    30
#include "../SDL_dataqueue.h"
icculus@6281
    31
icculus@10832
    32
/* !!! FIXME: wire this up to the configure script, etc. */
icculus@10832
    33
#include "SDL_cpuinfo.h"
icculus@10832
    34
#define HAVE_SSE3_INTRINSICS 0
icculus@10832
    35
icculus@10832
    36
#if HAVE_SSE3_INTRINSICS
icculus@10832
    37
#include <pmmintrin.h>
icculus@10832
    38
#endif
icculus@10832
    39
icculus@10832
    40
#if HAVE_SSE3_INTRINSICS
icculus@10832
    41
/* Effectively mix right and left channels into a single channel */
icculus@10832
    42
static void SDLCALL
icculus@10832
    43
SDL_ConvertStereoToMono_SSE3(SDL_AudioCVT * cvt, SDL_AudioFormat format)
icculus@10832
    44
{
icculus@10832
    45
    float *dst = (float *) cvt->buf;
icculus@10832
    46
    const float *src = dst;
icculus@10832
    47
    int i = cvt->len_cvt / 8;
icculus@10832
    48
icculus@10832
    49
    LOG_DEBUG_CONVERT("stereo", "mono (using SSE3)");
icculus@10832
    50
    SDL_assert(format == AUDIO_F32SYS);
icculus@10832
    51
icculus@10832
    52
    /* We can only do this if dst is aligned to 16 bytes; since src is the
icculus@10832
    53
       same pointer and it moves by 2, it can't be forcibly aligned. */
icculus@10832
    54
    if ((((size_t) dst) & 15) == 0) {
icculus@10832
    55
        /* Aligned! Do SSE blocks as long as we have 16 bytes available. */
icculus@10832
    56
        const __m128 divby2 = _mm_set1_ps(0.5f);
icculus@10832
    57
        while (i >= 4) {   /* 4 * float32 */
icculus@10832
    58
            _mm_store_ps(dst, _mm_mul_ps(_mm_hadd_ps(_mm_load_ps(src), _mm_load_ps(src+4)), divby2));
icculus@10832
    59
            i -= 4; src += 8; dst += 4;
icculus@10832
    60
        }
icculus@10832
    61
    }
icculus@10832
    62
icculus@10832
    63
    /* Finish off any leftovers with scalar operations. */
icculus@10832
    64
    while (i) {
icculus@10832
    65
        *dst = (src[0] + src[1]) * 0.5f;
icculus@10832
    66
        dst++; i--; src += 2;
icculus@10832
    67
    }
icculus@10832
    68
icculus@10832
    69
    cvt->len_cvt /= 2;
icculus@10832
    70
    if (cvt->filters[++cvt->filter_index]) {
icculus@10832
    71
        cvt->filters[cvt->filter_index] (cvt, format);
icculus@10832
    72
    }
icculus@10832
    73
}
icculus@10832
    74
#endif
icculus@10832
    75
slouken@0
    76
/* Effectively mix right and left channels into a single channel */
icculus@1982
    77
static void SDLCALL
icculus@10793
    78
SDL_ConvertStereoToMono(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
    79
{
icculus@10575
    80
    float *dst = (float *) cvt->buf;
icculus@10575
    81
    const float *src = dst;
slouken@1895
    82
    int i;
slouken@0
    83
icculus@10575
    84
    LOG_DEBUG_CONVERT("stereo", "mono");
icculus@10575
    85
    SDL_assert(format == AUDIO_F32SYS);
slouken@0
    86
icculus@10575
    87
    for (i = cvt->len_cvt / 8; i; --i, src += 2) {
icculus@10831
    88
        *(dst++) = (src[0] + src[1]) * 0.5f;
slouken@1895
    89
    }
icculus@1982
    90
slouken@1895
    91
    cvt->len_cvt /= 2;
slouken@1895
    92
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
    93
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
    94
    }
slouken@0
    95
}
slouken@0
    96
icculus@1982
    97
icculus@10793
    98
/* Convert from 5.1 to stereo. Average left and right, discard subwoofer. */
icculus@1982
    99
static void SDLCALL
icculus@10793
   100
SDL_Convert51ToStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   101
{
icculus@10575
   102
    float *dst = (float *) cvt->buf;
icculus@10575
   103
    const float *src = dst;
slouken@1895
   104
    int i;
slouken@942
   105
icculus@10793
   106
    LOG_DEBUG_CONVERT("5.1", "stereo");
icculus@10575
   107
    SDL_assert(format == AUDIO_F32SYS);
slouken@942
   108
icculus@10793
   109
    /* this assumes FL+FR+FC+subwoof+BL+BR layout. */
icculus@10575
   110
    for (i = cvt->len_cvt / (sizeof (float) * 6); i; --i, src += 6, dst += 2) {
icculus@10793
   111
        const double front_center = (double) src[2];
icculus@10793
   112
        dst[0] = (float) ((src[0] + front_center + src[4]) / 3.0);  /* left */
icculus@10793
   113
        dst[1] = (float) ((src[1] + front_center + src[5]) / 3.0);  /* right */
icculus@1982
   114
    }
slouken@942
   115
slouken@1895
   116
    cvt->len_cvt /= 3;
slouken@1895
   117
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   118
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   119
    }
slouken@942
   120
}
slouken@942
   121
slouken@942
   122
icculus@10793
   123
/* Convert from 5.1 to quad */
icculus@1982
   124
static void SDLCALL
icculus@10793
   125
SDL_Convert51ToQuad(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   126
{
icculus@10575
   127
    float *dst = (float *) cvt->buf;
icculus@10575
   128
    const float *src = dst;
slouken@1895
   129
    int i;
slouken@942
   130
icculus@10793
   131
    LOG_DEBUG_CONVERT("5.1", "quad");
icculus@10575
   132
    SDL_assert(format == AUDIO_F32SYS);
slouken@942
   133
icculus@10793
   134
    /* assumes quad is FL+FR+BL+BR layout and 5.1 is FL+FR+FC+subwoof+BL+BR */
icculus@10575
   135
    for (i = cvt->len_cvt / (sizeof (float) * 6); i; --i, src += 6, dst += 4) {
icculus@10793
   136
        /* FIXME: this is a good candidate for SIMD. */
icculus@10793
   137
        const double front_center = (double) src[2];
icculus@10793
   138
        dst[0] = (float) ((src[0] + front_center) * 0.5);  /* FL */
icculus@10793
   139
        dst[1] = (float) ((src[1] + front_center) * 0.5);  /* FR */
icculus@10793
   140
        dst[2] = (float) ((src[4] + front_center) * 0.5);  /* BL */
icculus@10793
   141
        dst[3] = (float) ((src[5] + front_center) * 0.5);  /* BR */
icculus@1982
   142
    }
slouken@942
   143
icculus@1982
   144
    cvt->len_cvt /= 6;
icculus@1982
   145
    cvt->len_cvt *= 4;
slouken@1895
   146
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   147
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   148
    }
slouken@942
   149
}
slouken@0
   150
icculus@10793
   151
slouken@0
   152
/* Duplicate a mono channel to both stereo channels */
icculus@1982
   153
static void SDLCALL
icculus@10793
   154
SDL_ConvertMonoToStereo(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@0
   155
{
icculus@10575
   156
    const float *src = (const float *) (cvt->buf + cvt->len_cvt);
icculus@10575
   157
    float *dst = (float *) (cvt->buf + cvt->len_cvt * 2);
slouken@1895
   158
    int i;
slouken@0
   159
icculus@10575
   160
    LOG_DEBUG_CONVERT("mono", "stereo");
icculus@10575
   161
    SDL_assert(format == AUDIO_F32SYS);
slouken@0
   162
icculus@10575
   163
    for (i = cvt->len_cvt / sizeof (float); i; --i) {
icculus@10575
   164
        src--;
icculus@10575
   165
        dst -= 2;
icculus@10575
   166
        dst[0] = dst[1] = *src;
icculus@1982
   167
    }
slouken@0
   168
slouken@1895
   169
    cvt->len_cvt *= 2;
slouken@1895
   170
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   171
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   172
    }
slouken@0
   173
}
slouken@0
   174
slouken@942
   175
slouken@942
   176
/* Duplicate a stereo channel to a pseudo-5.1 stream */
icculus@1982
   177
static void SDLCALL
icculus@10793
   178
SDL_ConvertStereoTo51(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   179
{
slouken@1895
   180
    int i;
icculus@10575
   181
    float lf, rf, ce;
icculus@10575
   182
    const float *src = (const float *) (cvt->buf + cvt->len_cvt);
icculus@10575
   183
    float *dst = (float *) (cvt->buf + cvt->len_cvt * 3);
slouken@942
   184
icculus@10575
   185
    LOG_DEBUG_CONVERT("stereo", "5.1");
icculus@10575
   186
    SDL_assert(format == AUDIO_F32SYS);
slouken@942
   187
icculus@10575
   188
    for (i = cvt->len_cvt / 8; i; --i) {
icculus@10575
   189
        dst -= 6;
icculus@10575
   190
        src -= 2;
icculus@10575
   191
        lf = src[0];
icculus@10575
   192
        rf = src[1];
icculus@10793
   193
        ce = (lf + rf) * 0.5f;
icculus@10793
   194
        dst[0] = lf + (lf - ce);  /* FL */
icculus@10793
   195
        dst[1] = rf + (rf - ce);  /* FR */
icculus@10793
   196
        dst[2] = ce;  /* FC */
icculus@10793
   197
        dst[3] = ce;  /* !!! FIXME: wrong! This is the subwoofer. */
icculus@10793
   198
        dst[4] = lf;  /* BL */
icculus@10793
   199
        dst[5] = rf;  /* BR */
icculus@10575
   200
    }
slouken@942
   201
slouken@1895
   202
    cvt->len_cvt *= 3;
slouken@1895
   203
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   204
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   205
    }
slouken@942
   206
}
slouken@942
   207
slouken@942
   208
slouken@942
   209
/* Duplicate a stereo channel to a pseudo-4.0 stream */
icculus@1982
   210
static void SDLCALL
icculus@10793
   211
SDL_ConvertStereoToQuad(SDL_AudioCVT * cvt, SDL_AudioFormat format)
slouken@942
   212
{
icculus@10575
   213
    const float *src = (const float *) (cvt->buf + cvt->len_cvt);
icculus@10575
   214
    float *dst = (float *) (cvt->buf + cvt->len_cvt * 2);
icculus@10793
   215
    float lf, rf;
slouken@1895
   216
    int i;
slouken@942
   217
icculus@10575
   218
    LOG_DEBUG_CONVERT("stereo", "quad");
icculus@10575
   219
    SDL_assert(format == AUDIO_F32SYS);
slouken@942
   220
icculus@10575
   221
    for (i = cvt->len_cvt / 8; i; --i) {
icculus@10575
   222
        dst -= 4;
icculus@10575
   223
        src -= 2;
icculus@10575
   224
        lf = src[0];
icculus@10575
   225
        rf = src[1];
icculus@10793
   226
        dst[0] = lf;  /* FL */
icculus@10793
   227
        dst[1] = rf;  /* FR */
icculus@10793
   228
        dst[2] = lf;  /* BL */
icculus@10793
   229
        dst[3] = rf;  /* BR */
icculus@10575
   230
    }
slouken@942
   231
slouken@1895
   232
    cvt->len_cvt *= 2;
slouken@1895
   233
    if (cvt->filters[++cvt->filter_index]) {
slouken@1895
   234
        cvt->filters[cvt->filter_index] (cvt, format);
slouken@1895
   235
    }
slouken@0
   236
}
slouken@0
   237
icculus@10799
   238
static int
icculus@10799
   239
SDL_ResampleAudioSimple(const int chans, const double rate_incr,
icculus@10799
   240
                        float *last_sample, const float *inbuf,
icculus@10799
   241
                        const int inbuflen, float *outbuf, const int outbuflen)
icculus@10799
   242
{
icculus@10817
   243
    const int framelen = chans * (int)sizeof (float);
icculus@10799
   244
    const int total = (inbuflen / framelen);
icculus@10817
   245
    const int finalpos = (total * chans) - chans;
icculus@10817
   246
    const int dest_samples = (int)(((double)total) * rate_incr);
icculus@10799
   247
    const double src_incr = 1.0 / rate_incr;
icculus@10817
   248
    float *dst = outbuf;
icculus@10817
   249
    float *target = (dst + (dest_samples * chans));
icculus@10799
   250
    double idx = 0.0;
icculus@10799
   251
    int i;
icculus@10799
   252
icculus@10817
   253
    SDL_assert((dest_samples * framelen) <= outbuflen);
icculus@10799
   254
    SDL_assert((inbuflen % framelen) == 0);
icculus@10799
   255
icculus@10830
   256
    while (dst < target) {
icculus@10799
   257
        const int pos = ((int)idx) * chans;
icculus@10830
   258
        const float *src = &inbuf[pos];
icculus@10830
   259
        SDL_assert(pos <= finalpos);
icculus@10799
   260
        for (i = 0; i < chans; i++) {
icculus@10799
   261
            const float val = *(src++);
icculus@10799
   262
            *(dst++) = (val + last_sample[i]) * 0.5f;
icculus@10799
   263
            last_sample[i] = val;
icculus@10799
   264
        }
icculus@10799
   265
        idx += src_incr;
icculus@10799
   266
    }
icculus@10799
   267
icculus@10817
   268
    return (int) ((dst - outbuf) * (int)sizeof(float));
icculus@10799
   269
}
icculus@10799
   270
slouken@0
   271
slouken@1895
   272
int
slouken@1895
   273
SDL_ConvertAudio(SDL_AudioCVT * cvt)
slouken@0
   274
{
icculus@3021
   275
    /* !!! FIXME: (cvt) should be const; stack-copy it here. */
icculus@3021
   276
    /* !!! FIXME: (actually, we can't...len_cvt needs to be updated. Grr.) */
icculus@3021
   277
slouken@1895
   278
    /* Make sure there's data to convert */
slouken@1895
   279
    if (cvt->buf == NULL) {
icculus@10575
   280
        return SDL_SetError("No buffer allocated for conversion");
slouken@1895
   281
    }
icculus@10575
   282
slouken@1895
   283
    /* Return okay if no conversion is necessary */
slouken@1895
   284
    cvt->len_cvt = cvt->len;
slouken@1895
   285
    if (cvt->filters[0] == NULL) {
icculus@10575
   286
        return 0;
slouken@1895
   287
    }
slouken@0
   288
slouken@1895
   289
    /* Set up the conversion and go! */
slouken@1895
   290
    cvt->filter_index = 0;
slouken@1895
   291
    cvt->filters[0] (cvt, cvt->src_format);
icculus@10575
   292
    return 0;
slouken@0
   293
}
slouken@0
   294
icculus@10575
   295
static void SDLCALL
icculus@10575
   296
SDL_Convert_Byteswap(SDL_AudioCVT *cvt, SDL_AudioFormat format)
icculus@10575
   297
{
slouken@10579
   298
#if DEBUG_CONVERT
slouken@10579
   299
    printf("Converting byte order\n");
slouken@10579
   300
#endif
icculus@1982
   301
icculus@10575
   302
    switch (SDL_AUDIO_BITSIZE(format)) {
icculus@10575
   303
        #define CASESWAP(b) \
icculus@10575
   304
            case b: { \
icculus@10575
   305
                Uint##b *ptr = (Uint##b *) cvt->buf; \
icculus@10575
   306
                int i; \
icculus@10575
   307
                for (i = cvt->len_cvt / sizeof (*ptr); i; --i, ++ptr) { \
icculus@10575
   308
                    *ptr = SDL_Swap##b(*ptr); \
icculus@10575
   309
                } \
icculus@10575
   310
                break; \
icculus@10575
   311
            }
icculus@1982
   312
icculus@10575
   313
        CASESWAP(16);
icculus@10575
   314
        CASESWAP(32);
icculus@10575
   315
        CASESWAP(64);
icculus@10575
   316
icculus@10575
   317
        #undef CASESWAP
icculus@10575
   318
icculus@10575
   319
        default: SDL_assert(!"unhandled byteswap datatype!"); break;
icculus@10575
   320
    }
icculus@10575
   321
icculus@10575
   322
    if (cvt->filters[++cvt->filter_index]) {
icculus@10575
   323
        /* flip endian flag for data. */
icculus@10575
   324
        if (format & SDL_AUDIO_MASK_ENDIAN) {
icculus@10575
   325
            format &= ~SDL_AUDIO_MASK_ENDIAN;
icculus@10575
   326
        } else {
icculus@10575
   327
            format |= SDL_AUDIO_MASK_ENDIAN;
icculus@10575
   328
        }
icculus@10575
   329
        cvt->filters[cvt->filter_index](cvt, format);
icculus@10575
   330
    }
icculus@1982
   331
}
icculus@1982
   332
icculus@1982
   333
icculus@1982
   334
static int
icculus@10575
   335
SDL_BuildAudioTypeCVTToFloat(SDL_AudioCVT *cvt, const SDL_AudioFormat src_fmt)
icculus@1982
   336
{
icculus@10575
   337
    int retval = 0;  /* 0 == no conversion necessary. */
icculus@1982
   338
icculus@10575
   339
    if ((SDL_AUDIO_ISBIGENDIAN(src_fmt) != 0) == (SDL_BYTEORDER == SDL_LIL_ENDIAN)) {
icculus@10575
   340
        cvt->filters[cvt->filter_index++] = SDL_Convert_Byteswap;
icculus@10575
   341
        retval = 1;  /* added a converter. */
icculus@10575
   342
    }
icculus@1982
   343
icculus@10575
   344
    if (!SDL_AUDIO_ISFLOAT(src_fmt)) {
icculus@10576
   345
        const Uint16 src_bitsize = SDL_AUDIO_BITSIZE(src_fmt);
icculus@10576
   346
        const Uint16 dst_bitsize = 32;
icculus@10575
   347
        SDL_AudioFilter filter = NULL;
icculus@10576
   348
icculus@10575
   349
        switch (src_fmt & ~SDL_AUDIO_MASK_ENDIAN) {
icculus@10575
   350
            case AUDIO_S8: filter = SDL_Convert_S8_to_F32; break;
icculus@10575
   351
            case AUDIO_U8: filter = SDL_Convert_U8_to_F32; break;
icculus@10575
   352
            case AUDIO_S16: filter = SDL_Convert_S16_to_F32; break;
philipp@10591
   353
            case AUDIO_U16: filter = SDL_Convert_U16_to_F32; break;
icculus@10575
   354
            case AUDIO_S32: filter = SDL_Convert_S32_to_F32; break;
icculus@10575
   355
            default: SDL_assert(!"Unexpected audio format!"); break;
icculus@1982
   356
        }
icculus@1982
   357
icculus@10575
   358
        if (!filter) {
icculus@10575
   359
            return SDL_SetError("No conversion available for these formats");
icculus@10575
   360
        }
icculus@10575
   361
icculus@1982
   362
        cvt->filters[cvt->filter_index++] = filter;
icculus@1982
   363
        if (src_bitsize < dst_bitsize) {
icculus@1982
   364
            const int mult = (dst_bitsize / src_bitsize);
icculus@1982
   365
            cvt->len_mult *= mult;
icculus@1982
   366
            cvt->len_ratio *= mult;
icculus@1982
   367
        } else if (src_bitsize > dst_bitsize) {
icculus@1982
   368
            cvt->len_ratio /= (src_bitsize / dst_bitsize);
icculus@1982
   369
        }
icculus@10576
   370
icculus@10575
   371
        retval = 1;  /* added a converter. */
icculus@1982
   372
    }
icculus@1982
   373
icculus@10575
   374
    return retval;
icculus@1982
   375
}
icculus@1982
   376
icculus@10575
   377
static int
icculus@10575
   378
SDL_BuildAudioTypeCVTFromFloat(SDL_AudioCVT *cvt, const SDL_AudioFormat dst_fmt)
icculus@10575
   379
{
icculus@10575
   380
    int retval = 0;  /* 0 == no conversion necessary. */
icculus@3021
   381
icculus@10575
   382
    if (!SDL_AUDIO_ISFLOAT(dst_fmt)) {
icculus@10577
   383
        const Uint16 dst_bitsize = SDL_AUDIO_BITSIZE(dst_fmt);
icculus@10577
   384
        const Uint16 src_bitsize = 32;
icculus@10575
   385
        SDL_AudioFilter filter = NULL;
icculus@10575
   386
        switch (dst_fmt & ~SDL_AUDIO_MASK_ENDIAN) {
icculus@10575
   387
            case AUDIO_S8: filter = SDL_Convert_F32_to_S8; break;
icculus@10575
   388
            case AUDIO_U8: filter = SDL_Convert_F32_to_U8; break;
icculus@10575
   389
            case AUDIO_S16: filter = SDL_Convert_F32_to_S16; break;
philipp@10591
   390
            case AUDIO_U16: filter = SDL_Convert_F32_to_U16; break;
icculus@10575
   391
            case AUDIO_S32: filter = SDL_Convert_F32_to_S32; break;
icculus@10575
   392
            default: SDL_assert(!"Unexpected audio format!"); break;
icculus@10575
   393
        }
slouken@2716
   394
icculus@10575
   395
        if (!filter) {
icculus@10575
   396
            return SDL_SetError("No conversion available for these formats");
icculus@10575
   397
        }
icculus@10575
   398
icculus@10575
   399
        cvt->filters[cvt->filter_index++] = filter;
icculus@10575
   400
        if (src_bitsize < dst_bitsize) {
icculus@10575
   401
            const int mult = (dst_bitsize / src_bitsize);
icculus@10575
   402
            cvt->len_mult *= mult;
icculus@10575
   403
            cvt->len_ratio *= mult;
icculus@10575
   404
        } else if (src_bitsize > dst_bitsize) {
icculus@10575
   405
            cvt->len_ratio /= (src_bitsize / dst_bitsize);
icculus@10575
   406
        }
icculus@10575
   407
        retval = 1;  /* added a converter. */
icculus@10575
   408
    }
icculus@10575
   409
icculus@10575
   410
    if ((SDL_AUDIO_ISBIGENDIAN(dst_fmt) != 0) == (SDL_BYTEORDER == SDL_LIL_ENDIAN)) {
icculus@10575
   411
        cvt->filters[cvt->filter_index++] = SDL_Convert_Byteswap;
icculus@10575
   412
        retval = 1;  /* added a converter. */
icculus@10575
   413
    }
icculus@10575
   414
icculus@10575
   415
    return retval;
icculus@3021
   416
}
slouken@2716
   417
icculus@10799
   418
static void
icculus@10799
   419
SDL_ResampleCVT(SDL_AudioCVT *cvt, const int chans, const SDL_AudioFormat format)
icculus@10799
   420
{
icculus@10799
   421
    const float *src = (const float *) cvt->buf;
icculus@10799
   422
    const int srclen = cvt->len_cvt;
icculus@10799
   423
    float *dst = (float *) (cvt->buf + srclen);
icculus@10799
   424
    const int dstlen = (cvt->len * cvt->len_mult) - srclen;
icculus@10804
   425
    float state[8];
icculus@10756
   426
icculus@10799
   427
    SDL_assert(format == AUDIO_F32SYS);
icculus@10799
   428
slouken@10805
   429
    SDL_memcpy(state, src, chans*sizeof(*src));
icculus@10799
   430
icculus@10804
   431
    cvt->len_cvt = SDL_ResampleAudioSimple(chans, cvt->rate_incr, state, src, srclen, dst, dstlen);
icculus@10799
   432
icculus@10799
   433
    SDL_memcpy(cvt->buf, dst, cvt->len_cvt);
icculus@10799
   434
    if (cvt->filters[++cvt->filter_index]) {
icculus@10799
   435
        cvt->filters[cvt->filter_index](cvt, format);
icculus@10799
   436
    }
icculus@10799
   437
}
icculus@10799
   438
icculus@10799
   439
/* !!! FIXME: We only have this macro salsa because SDL_AudioCVT doesn't
icculus@10799
   440
   !!! FIXME:  store channel info, so we have to have function entry
icculus@10799
   441
   !!! FIXME:  points for each supported channel count and multiple
icculus@10799
   442
   !!! FIXME:  vs arbitrary. When we rev the ABI, clean this up. */
icculus@10756
   443
#define RESAMPLER_FUNCS(chans) \
icculus@10756
   444
    static void SDLCALL \
icculus@10799
   445
    SDL_ResampleCVT_c##chans(SDL_AudioCVT *cvt, SDL_AudioFormat format) { \
icculus@10799
   446
        SDL_ResampleCVT(cvt, chans, format); \
icculus@10756
   447
    }
icculus@10756
   448
RESAMPLER_FUNCS(1)
icculus@10756
   449
RESAMPLER_FUNCS(2)
icculus@10756
   450
RESAMPLER_FUNCS(4)
icculus@10756
   451
RESAMPLER_FUNCS(6)
icculus@10756
   452
RESAMPLER_FUNCS(8)
icculus@10756
   453
#undef RESAMPLER_FUNCS
icculus@10756
   454
icculus@10799
   455
static SDL_AudioFilter
icculus@10799
   456
ChooseCVTResampler(const int dst_channels)
icculus@3021
   457
{
icculus@10799
   458
    switch (dst_channels) {
icculus@10799
   459
        case 1: return SDL_ResampleCVT_c1;
icculus@10799
   460
        case 2: return SDL_ResampleCVT_c2;
icculus@10799
   461
        case 4: return SDL_ResampleCVT_c4;
icculus@10799
   462
        case 6: return SDL_ResampleCVT_c6;
icculus@10799
   463
        case 8: return SDL_ResampleCVT_c8;
icculus@10799
   464
        default: break;
icculus@3021
   465
    }
slouken@2716
   466
icculus@10799
   467
    return NULL;
icculus@10756
   468
}
icculus@10575
   469
icculus@3021
   470
static int
icculus@10756
   471
SDL_BuildAudioResampleCVT(SDL_AudioCVT * cvt, const int dst_channels,
icculus@10756
   472
                          const int src_rate, const int dst_rate)
icculus@3021
   473
{
icculus@10756
   474
    SDL_AudioFilter filter;
icculus@3021
   475
icculus@10756
   476
    if (src_rate == dst_rate) {
icculus@10756
   477
        return 0;  /* no conversion necessary. */
slouken@2716
   478
    }
slouken@2716
   479
icculus@10799
   480
    filter = ChooseCVTResampler(dst_channels);
icculus@10756
   481
    if (filter == NULL) {
icculus@10756
   482
        return SDL_SetError("No conversion available for these rates");
icculus@10756
   483
    }
icculus@10756
   484
icculus@10756
   485
    /* Update (cvt) with filter details... */
icculus@10756
   486
    cvt->filters[cvt->filter_index++] = filter;
icculus@10756
   487
    if (src_rate < dst_rate) {
icculus@10756
   488
        const double mult = ((double) dst_rate) / ((double) src_rate);
icculus@10756
   489
        cvt->len_mult *= (int) SDL_ceil(mult);
icculus@10756
   490
        cvt->len_ratio *= mult;
icculus@10756
   491
    } else {
icculus@10756
   492
        cvt->len_ratio /= ((double) src_rate) / ((double) dst_rate);
icculus@10756
   493
    }
icculus@10756
   494
icculus@10799
   495
    /* the buffer is big enough to hold the destination now, but
icculus@10799
   496
       we need it large enough to hold a separate scratch buffer. */
icculus@10799
   497
    cvt->len_mult *= 2;
icculus@10799
   498
icculus@10756
   499
    return 1;               /* added a converter. */
slouken@2716
   500
}
icculus@1982
   501
icculus@1982
   502
icculus@1982
   503
/* Creates a set of audio filters to convert from one format to another.
icculus@1982
   504
   Returns -1 if the format conversion is not supported, 0 if there's
icculus@1982
   505
   no conversion needed, or 1 if the audio filter is set up.
slouken@0
   506
*/
slouken@1895
   507
slouken@1895
   508
int
slouken@1895
   509
SDL_BuildAudioCVT(SDL_AudioCVT * cvt,
icculus@1982
   510
                  SDL_AudioFormat src_fmt, Uint8 src_channels, int src_rate,
icculus@1982
   511
                  SDL_AudioFormat dst_fmt, Uint8 dst_channels, int dst_rate)
slouken@0
   512
{
aschiffler@6819
   513
    /* Sanity check target pointer */
aschiffler@6819
   514
    if (cvt == NULL) {
icculus@7037
   515
        return SDL_InvalidParamError("cvt");
aschiffler@6819
   516
    }
slouken@7191
   517
slouken@10767
   518
    /* Make sure we zero out the audio conversion before error checking */
slouken@10767
   519
    SDL_zerop(cvt);
slouken@10767
   520
slouken@3491
   521
    /* there are no unsigned types over 16 bits, so catch this up front. */
icculus@1982
   522
    if ((SDL_AUDIO_BITSIZE(src_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(src_fmt))) {
icculus@7037
   523
        return SDL_SetError("Invalid source format");
icculus@1982
   524
    }
icculus@1982
   525
    if ((SDL_AUDIO_BITSIZE(dst_fmt) > 16) && (!SDL_AUDIO_ISSIGNED(dst_fmt))) {
icculus@7037
   526
        return SDL_SetError("Invalid destination format");
icculus@1982
   527
    }
icculus@3021
   528
icculus@3021
   529
    /* prevent possible divisions by zero, etc. */
aschiffler@6819
   530
    if ((src_channels == 0) || (dst_channels == 0)) {
icculus@7037
   531
        return SDL_SetError("Source or destination channels is zero");
aschiffler@6819
   532
    }
icculus@3021
   533
    if ((src_rate == 0) || (dst_rate == 0)) {
icculus@7037
   534
        return SDL_SetError("Source or destination rate is zero");
icculus@3021
   535
    }
slouken@10579
   536
#if DEBUG_CONVERT
icculus@1982
   537
    printf("Build format %04x->%04x, channels %u->%u, rate %d->%d\n",
slouken@1985
   538
           src_fmt, dst_fmt, src_channels, dst_channels, src_rate, dst_rate);
slouken@1985
   539
#endif
icculus@1982
   540
slouken@1895
   541
    /* Start off with no conversion necessary */
icculus@1982
   542
    cvt->src_format = src_fmt;
icculus@1982
   543
    cvt->dst_format = dst_fmt;
slouken@1895
   544
    cvt->needed = 0;
slouken@1895
   545
    cvt->filter_index = 0;
slouken@1895
   546
    cvt->filters[0] = NULL;
slouken@1895
   547
    cvt->len_mult = 1;
slouken@1895
   548
    cvt->len_ratio = 1.0;
icculus@3021
   549
    cvt->rate_incr = ((double) dst_rate) / ((double) src_rate);
slouken@0
   550
icculus@10575
   551
    /* Type conversion goes like this now:
icculus@10575
   552
        - byteswap to CPU native format first if necessary.
icculus@10575
   553
        - convert to native Float32 if necessary.
icculus@10575
   554
        - resample and change channel count if necessary.
icculus@10575
   555
        - convert back to native format.
icculus@10575
   556
        - byteswap back to foreign format if necessary.
icculus@10575
   557
icculus@10575
   558
       The expectation is we can process data faster in float32
icculus@10575
   559
       (possibly with SIMD), and making several passes over the same
icculus@10756
   560
       buffer is likely to be CPU cache-friendly, avoiding the
icculus@10575
   561
       biggest performance hit in modern times. Previously we had
icculus@10575
   562
       (script-generated) custom converters for every data type and
icculus@10575
   563
       it was a bloat on SDL compile times and final library size. */
icculus@10575
   564
slouken@10767
   565
    /* see if we can skip float conversion entirely. */
slouken@10767
   566
    if (src_rate == dst_rate && src_channels == dst_channels) {
slouken@10767
   567
        if (src_fmt == dst_fmt) {
slouken@10767
   568
            return 0;
slouken@10767
   569
        }
slouken@10767
   570
slouken@10767
   571
        /* just a byteswap needed? */
slouken@10767
   572
        if ((src_fmt & ~SDL_AUDIO_MASK_ENDIAN) == (dst_fmt & ~SDL_AUDIO_MASK_ENDIAN)) {
slouken@10767
   573
            cvt->filters[cvt->filter_index++] = SDL_Convert_Byteswap;
slouken@10767
   574
            cvt->needed = 1;
slouken@10767
   575
            return 1;
slouken@10767
   576
        }
icculus@10575
   577
    }
icculus@10575
   578
icculus@1982
   579
    /* Convert data types, if necessary. Updates (cvt). */
slouken@10767
   580
    if (SDL_BuildAudioTypeCVTToFloat(cvt, src_fmt) < 0) {
slouken@1985
   581
        return -1;              /* shouldn't happen, but just in case... */
icculus@3021
   582
    }
slouken@0
   583
icculus@1982
   584
    /* Channel conversion */
slouken@1895
   585
    if (src_channels != dst_channels) {
slouken@1895
   586
        if ((src_channels == 1) && (dst_channels > 1)) {
icculus@10793
   587
            cvt->filters[cvt->filter_index++] = SDL_ConvertMonoToStereo;
slouken@1895
   588
            cvt->len_mult *= 2;
slouken@1895
   589
            src_channels = 2;
slouken@1895
   590
            cvt->len_ratio *= 2;
slouken@1895
   591
        }
slouken@1895
   592
        if ((src_channels == 2) && (dst_channels == 6)) {
icculus@10793
   593
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereoTo51;
slouken@1895
   594
            src_channels = 6;
slouken@1895
   595
            cvt->len_mult *= 3;
slouken@1895
   596
            cvt->len_ratio *= 3;
slouken@1895
   597
        }
slouken@1895
   598
        if ((src_channels == 2) && (dst_channels == 4)) {
icculus@10793
   599
            cvt->filters[cvt->filter_index++] = SDL_ConvertStereoToQuad;
slouken@1895
   600
            src_channels = 4;
slouken@1895
   601
            cvt->len_mult *= 2;
slouken@1895
   602
            cvt->len_ratio *= 2;
slouken@1895
   603
        }
slouken@1895
   604
        while ((src_channels * 2) <= dst_channels) {
icculus@10793
   605
            cvt->filters[cvt->filter_index++] = SDL_ConvertMonoToStereo;
slouken@1895
   606
            cvt->len_mult *= 2;
slouken@1895
   607
            src_channels *= 2;
slouken@1895
   608
            cvt->len_ratio *= 2;
slouken@1895
   609
        }
slouken@1895
   610
        if ((src_channels == 6) && (dst_channels <= 2)) {
icculus@10793
   611
            cvt->filters[cvt->filter_index++] = SDL_Convert51ToStereo;
slouken@1895
   612
            src_channels = 2;
slouken@1895
   613
            cvt->len_ratio /= 3;
slouken@1895
   614
        }
slouken@1895
   615
        if ((src_channels == 6) && (dst_channels == 4)) {
icculus@10793
   616
            cvt->filters[cvt->filter_index++] = SDL_Convert51ToQuad;
slouken@1895
   617
            src_channels = 4;
slouken@1895
   618
            cvt->len_ratio /= 2;
slouken@1895
   619
        }
slouken@1895
   620
        /* This assumes that 4 channel audio is in the format:
slouken@1895
   621
           Left {front/back} + Right {front/back}
slouken@1895
   622
           so converting to L/R stereo works properly.
slouken@1895
   623
         */
slouken@1895
   624
        while (((src_channels % 2) == 0) &&
slouken@1895
   625
               ((src_channels / 2) >= dst_channels)) {
icculus@10832
   626
            SDL_AudioFilter filter = NULL;
icculus@10832
   627
icculus@10832
   628
            #if HAVE_SSE3_INTRINSICS
icculus@10832
   629
            if (SDL_HasSSE3()) {
icculus@10832
   630
                filter = SDL_ConvertStereoToMono_SSE3;
icculus@10832
   631
            }
icculus@10832
   632
            #endif
icculus@10832
   633
icculus@10832
   634
            if (!filter) {
icculus@10832
   635
                filter = SDL_ConvertStereoToMono;
icculus@10832
   636
            }
icculus@10832
   637
icculus@10832
   638
            cvt->filters[cvt->filter_index++] = filter;
icculus@10832
   639
slouken@1895
   640
            src_channels /= 2;
slouken@1895
   641
            cvt->len_ratio /= 2;
slouken@1895
   642
        }
slouken@1895
   643
        if (src_channels != dst_channels) {
slouken@1895
   644
            /* Uh oh.. */ ;
slouken@1895
   645
        }
slouken@1895
   646
    }
slouken@0
   647
icculus@3021
   648
    /* Do rate conversion, if necessary. Updates (cvt). */
slouken@10767
   649
    if (SDL_BuildAudioResampleCVT(cvt, dst_channels, src_rate, dst_rate) < 0) {
icculus@3021
   650
        return -1;              /* shouldn't happen, but just in case... */
slouken@2716
   651
    }
slouken@2716
   652
icculus@10756
   653
    /* Move to final data type. */
slouken@10767
   654
    if (SDL_BuildAudioTypeCVTFromFloat(cvt, dst_fmt) < 0) {
icculus@10575
   655
        return -1;              /* shouldn't happen, but just in case... */
slouken@1895
   656
    }
icculus@10575
   657
icculus@10575
   658
    cvt->needed = (cvt->filter_index != 0);
slouken@1895
   659
    return (cvt->needed);
slouken@0
   660
}
slouken@1895
   661
slouken@10773
   662
typedef int (*SDL_ResampleAudioStreamFunc)(SDL_AudioStream *stream, const float *inbuf, const int inbuflen, float *outbuf, const int outbuflen);
slouken@10773
   663
typedef void (*SDL_ResetAudioStreamResamplerFunc)(SDL_AudioStream *stream);
slouken@10773
   664
typedef void (*SDL_CleanupAudioStreamResamplerFunc)(SDL_AudioStream *stream);
icculus@10757
   665
icculus@10757
   666
struct SDL_AudioStream
icculus@10757
   667
{
icculus@10757
   668
    SDL_AudioCVT cvt_before_resampling;
icculus@10757
   669
    SDL_AudioCVT cvt_after_resampling;
icculus@10757
   670
    SDL_DataQueue *queue;
icculus@10757
   671
    Uint8 *work_buffer;
icculus@10757
   672
    int work_buffer_len;
icculus@10757
   673
    Uint8 *resample_buffer;
icculus@10757
   674
    int resample_buffer_len;
icculus@10757
   675
    int src_sample_frame_size;
icculus@10757
   676
    SDL_AudioFormat src_format;
icculus@10757
   677
    Uint8 src_channels;
icculus@10757
   678
    int src_rate;
icculus@10757
   679
    int dst_sample_frame_size;
icculus@10757
   680
    SDL_AudioFormat dst_format;
icculus@10757
   681
    Uint8 dst_channels;
icculus@10757
   682
    int dst_rate;
icculus@10757
   683
    double rate_incr;
icculus@10757
   684
    Uint8 pre_resample_channels;
slouken@10773
   685
    int packetlen;
slouken@10773
   686
    void *resampler_state;
slouken@10773
   687
    SDL_ResampleAudioStreamFunc resampler_func;
slouken@10773
   688
    SDL_ResetAudioStreamResamplerFunc reset_resampler_func;
slouken@10773
   689
    SDL_CleanupAudioStreamResamplerFunc cleanup_resampler_func;
slouken@10773
   690
};
slouken@10773
   691
slouken@10777
   692
#ifdef HAVE_LIBSAMPLERATE_H
slouken@10773
   693
static int
slouken@10773
   694
SDL_ResampleAudioStream_SRC(SDL_AudioStream *stream, const float *inbuf, const int inbuflen, float *outbuf, const int outbuflen)
slouken@10773
   695
{
icculus@10799
   696
    const int framelen = sizeof(float) * stream->pre_resample_channels;
icculus@10790
   697
    SRC_STATE *state = (SRC_STATE *)stream->resampler_state;
slouken@10773
   698
    SRC_DATA data;
slouken@10773
   699
    int result;
slouken@10773
   700
slouken@10777
   701
    data.data_in = (float *)inbuf; /* Older versions of libsamplerate had a non-const pointer, but didn't write to it */
icculus@10799
   702
    data.input_frames = inbuflen / framelen;
slouken@10773
   703
    data.input_frames_used = 0;
slouken@10773
   704
slouken@10773
   705
    data.data_out = outbuf;
icculus@10799
   706
    data.output_frames = outbuflen / framelen;
slouken@10773
   707
slouken@10773
   708
    data.end_of_input = 0;
slouken@10773
   709
    data.src_ratio = stream->rate_incr;
slouken@10773
   710
icculus@10790
   711
    result = SRC_src_process(state, &data);
slouken@10773
   712
    if (result != 0) {
icculus@10790
   713
        SDL_SetError("src_process() failed: %s", SRC_src_strerror(result));
slouken@10773
   714
        return 0;
slouken@10773
   715
    }
slouken@10773
   716
slouken@10773
   717
    /* If this fails, we need to store them off somewhere */
slouken@10773
   718
    SDL_assert(data.input_frames_used == data.input_frames);
slouken@10773
   719
slouken@10773
   720
    return data.output_frames_gen * (sizeof(float) * stream->pre_resample_channels);
slouken@10773
   721
}
slouken@10773
   722
slouken@10773
   723
static void
slouken@10773
   724
SDL_ResetAudioStreamResampler_SRC(SDL_AudioStream *stream)
slouken@10773
   725
{
icculus@10790
   726
    SRC_src_reset((SRC_STATE *)stream->resampler_state);
slouken@10773
   727
}
slouken@10773
   728
slouken@10773
   729
static void
slouken@10773
   730
SDL_CleanupAudioStreamResampler_SRC(SDL_AudioStream *stream)
slouken@10773
   731
{
icculus@10790
   732
    SRC_STATE *state = (SRC_STATE *)stream->resampler_state;
slouken@10773
   733
    if (state) {
icculus@10790
   734
        SRC_src_delete(state);
slouken@10773
   735
    }
slouken@10773
   736
slouken@10773
   737
    stream->resampler_state = NULL;
slouken@10773
   738
    stream->resampler_func = NULL;
slouken@10773
   739
    stream->reset_resampler_func = NULL;
slouken@10773
   740
    stream->cleanup_resampler_func = NULL;
slouken@10773
   741
}
slouken@10773
   742
slouken@10773
   743
static SDL_bool
slouken@10773
   744
SetupLibSampleRateResampling(SDL_AudioStream *stream)
slouken@10773
   745
{
icculus@10790
   746
    int result = 0;
icculus@10790
   747
    SRC_STATE *state = NULL;
slouken@10773
   748
icculus@10790
   749
    if (SRC_available) {
icculus@10790
   750
        state = SRC_src_new(SRC_SINC_FASTEST, stream->pre_resample_channels, &result);
icculus@10790
   751
        if (!state) {
icculus@10790
   752
            SDL_SetError("src_new() failed: %s", SRC_src_strerror(result));
icculus@10790
   753
        }
slouken@10773
   754
    }
slouken@10773
   755
icculus@10790
   756
    if (!state) {
icculus@10790
   757
        SDL_CleanupAudioStreamResampler_SRC(stream);
slouken@10773
   758
        return SDL_FALSE;
slouken@10773
   759
    }
slouken@10773
   760
slouken@10773
   761
    stream->resampler_state = state;
slouken@10773
   762
    stream->resampler_func = SDL_ResampleAudioStream_SRC;
slouken@10773
   763
    stream->reset_resampler_func = SDL_ResetAudioStreamResampler_SRC;
slouken@10773
   764
    stream->cleanup_resampler_func = SDL_CleanupAudioStreamResampler_SRC;
slouken@10773
   765
slouken@10773
   766
    return SDL_TRUE;
slouken@10773
   767
}
icculus@10790
   768
#endif /* HAVE_LIBSAMPLERATE_H */
slouken@10773
   769
slouken@10773
   770
slouken@10773
   771
typedef struct
slouken@10773
   772
{
icculus@10757
   773
    SDL_bool resampler_seeded;
icculus@10757
   774
    float resampler_state[8];
slouken@10773
   775
} SDL_AudioStreamResamplerState;
slouken@10773
   776
slouken@10773
   777
static int
slouken@10773
   778
SDL_ResampleAudioStream(SDL_AudioStream *stream, const float *inbuf, const int inbuflen, float *outbuf, const int outbuflen)
slouken@10773
   779
{
slouken@10773
   780
    SDL_AudioStreamResamplerState *state = (SDL_AudioStreamResamplerState*)stream->resampler_state;
slouken@10773
   781
    const int chans = (int)stream->pre_resample_channels;
slouken@10773
   782
icculus@10799
   783
    SDL_assert(chans <= SDL_arraysize(state->resampler_state));
slouken@10773
   784
slouken@10773
   785
    if (!state->resampler_seeded) {
icculus@10799
   786
        int i;
slouken@10773
   787
        for (i = 0; i < chans; i++) {
slouken@10773
   788
            state->resampler_state[i] = inbuf[i];
slouken@10773
   789
        }
slouken@10773
   790
        state->resampler_seeded = SDL_TRUE;
slouken@10773
   791
    }
slouken@10773
   792
icculus@10799
   793
    return SDL_ResampleAudioSimple(chans, stream->rate_incr, state->resampler_state, inbuf, inbuflen, outbuf, outbuflen);
slouken@10773
   794
}
slouken@10773
   795
slouken@10773
   796
static void
slouken@10773
   797
SDL_ResetAudioStreamResampler(SDL_AudioStream *stream)
slouken@10773
   798
{
slouken@10773
   799
    SDL_AudioStreamResamplerState *state = (SDL_AudioStreamResamplerState*)stream->resampler_state;
slouken@10773
   800
    state->resampler_seeded = SDL_FALSE;
slouken@10773
   801
}
slouken@10773
   802
slouken@10773
   803
static void
slouken@10773
   804
SDL_CleanupAudioStreamResampler(SDL_AudioStream *stream)
slouken@10773
   805
{
slouken@10773
   806
    SDL_free(stream->resampler_state);
slouken@10773
   807
}
icculus@10757
   808
icculus@10789
   809
SDL_AudioStream *
icculus@10789
   810
SDL_NewAudioStream(const SDL_AudioFormat src_format,
icculus@10789
   811
                   const Uint8 src_channels,
icculus@10789
   812
                   const int src_rate,
icculus@10789
   813
                   const SDL_AudioFormat dst_format,
icculus@10789
   814
                   const Uint8 dst_channels,
icculus@10789
   815
                   const int dst_rate)
icculus@10757
   816
{
icculus@10757
   817
    const int packetlen = 4096;  /* !!! FIXME: good enough for now. */
icculus@10757
   818
    Uint8 pre_resample_channels;
icculus@10757
   819
    SDL_AudioStream *retval;
icculus@10757
   820
icculus@10757
   821
    retval = (SDL_AudioStream *) SDL_calloc(1, sizeof (SDL_AudioStream));
icculus@10757
   822
    if (!retval) {
icculus@10757
   823
        return NULL;
icculus@10757
   824
    }
icculus@10757
   825
icculus@10757
   826
    /* If increasing channels, do it after resampling, since we'd just
icculus@10757
   827
       do more work to resample duplicate channels. If we're decreasing, do
icculus@10757
   828
       it first so we resample the interpolated data instead of interpolating
icculus@10757
   829
       the resampled data (!!! FIXME: decide if that works in practice, though!). */
icculus@10757
   830
    pre_resample_channels = SDL_min(src_channels, dst_channels);
icculus@10757
   831
icculus@10757
   832
    retval->src_sample_frame_size = SDL_AUDIO_BITSIZE(src_format) * src_channels;
icculus@10757
   833
    retval->src_format = src_format;
icculus@10757
   834
    retval->src_channels = src_channels;
icculus@10757
   835
    retval->src_rate = src_rate;
icculus@10757
   836
    retval->dst_sample_frame_size = SDL_AUDIO_BITSIZE(dst_format) * dst_channels;
icculus@10757
   837
    retval->dst_format = dst_format;
icculus@10757
   838
    retval->dst_channels = dst_channels;
icculus@10757
   839
    retval->dst_rate = dst_rate;
icculus@10757
   840
    retval->pre_resample_channels = pre_resample_channels;
icculus@10757
   841
    retval->packetlen = packetlen;
icculus@10757
   842
    retval->rate_incr = ((double) dst_rate) / ((double) src_rate);
icculus@10757
   843
icculus@10757
   844
    /* Not resampling? It's an easy conversion (and maybe not even that!). */
icculus@10757
   845
    if (src_rate == dst_rate) {
icculus@10757
   846
        retval->cvt_before_resampling.needed = SDL_FALSE;
icculus@10757
   847
        retval->cvt_before_resampling.len_mult = 1;
slouken@10773
   848
        if (SDL_BuildAudioCVT(&retval->cvt_after_resampling, src_format, src_channels, dst_rate, dst_format, dst_channels, dst_rate) < 0) {
slouken@10773
   849
            SDL_FreeAudioStream(retval);
icculus@10757
   850
            return NULL;  /* SDL_BuildAudioCVT should have called SDL_SetError. */
icculus@10757
   851
        }
icculus@10757
   852
    } else {
icculus@10757
   853
        /* Don't resample at first. Just get us to Float32 format. */
icculus@10757
   854
        /* !!! FIXME: convert to int32 on devices without hardware float. */
slouken@10773
   855
        if (SDL_BuildAudioCVT(&retval->cvt_before_resampling, src_format, src_channels, src_rate, AUDIO_F32SYS, pre_resample_channels, src_rate) < 0) {
slouken@10773
   856
            SDL_FreeAudioStream(retval);
icculus@10757
   857
            return NULL;  /* SDL_BuildAudioCVT should have called SDL_SetError. */
icculus@10757
   858
        }
icculus@10757
   859
slouken@10777
   860
#ifdef HAVE_LIBSAMPLERATE_H
slouken@10773
   861
        SetupLibSampleRateResampling(retval);
slouken@10773
   862
#endif
slouken@10773
   863
slouken@10773
   864
        if (!retval->resampler_func) {
slouken@10773
   865
            retval->resampler_state = SDL_calloc(1, sizeof(SDL_AudioStreamResamplerState));
slouken@10773
   866
            if (!retval->resampler_state) {
slouken@10773
   867
                SDL_FreeAudioStream(retval);
slouken@10773
   868
                SDL_OutOfMemory();
slouken@10773
   869
                return NULL;
slouken@10773
   870
            }
slouken@10773
   871
            retval->resampler_func = SDL_ResampleAudioStream;
slouken@10773
   872
            retval->reset_resampler_func = SDL_ResetAudioStreamResampler;
slouken@10773
   873
            retval->cleanup_resampler_func = SDL_CleanupAudioStreamResampler;
slouken@10773
   874
        }
slouken@10773
   875
icculus@10757
   876
        /* Convert us to the final format after resampling. */
slouken@10773
   877
        if (SDL_BuildAudioCVT(&retval->cvt_after_resampling, AUDIO_F32SYS, pre_resample_channels, dst_rate, dst_format, dst_channels, dst_rate) < 0) {
slouken@10773
   878
            SDL_FreeAudioStream(retval);
icculus@10757
   879
            return NULL;  /* SDL_BuildAudioCVT should have called SDL_SetError. */
icculus@10757
   880
        }
icculus@10757
   881
    }
icculus@10757
   882
icculus@10757
   883
    retval->queue = SDL_NewDataQueue(packetlen, packetlen * 2);
icculus@10757
   884
    if (!retval->queue) {
slouken@10773
   885
        SDL_FreeAudioStream(retval);
icculus@10757
   886
        return NULL;  /* SDL_NewDataQueue should have called SDL_SetError. */
icculus@10757
   887
    }
icculus@10757
   888
icculus@10757
   889
    return retval;
icculus@10757
   890
}
icculus@10757
   891
icculus@10757
   892
static Uint8 *
icculus@10757
   893
EnsureBufferSize(Uint8 **buf, int *len, const int newlen)
icculus@10757
   894
{
icculus@10757
   895
    if (*len < newlen) {
icculus@10757
   896
        void *ptr = SDL_realloc(*buf, newlen);
icculus@10757
   897
        if (!ptr) {
icculus@10757
   898
            SDL_OutOfMemory();
icculus@10757
   899
            return NULL;
icculus@10757
   900
        }
icculus@10757
   901
        *buf = (Uint8 *) ptr;
icculus@10757
   902
        *len = newlen;
icculus@10757
   903
    }
icculus@10757
   904
    return *buf;
icculus@10757
   905
}
icculus@10757
   906
icculus@10757
   907
int
icculus@10757
   908
SDL_AudioStreamPut(SDL_AudioStream *stream, const void *buf, const Uint32 _buflen)
icculus@10757
   909
{
icculus@10757
   910
    int buflen = (int) _buflen;
icculus@10757
   911
icculus@10757
   912
    if (!stream) {
icculus@10757
   913
        return SDL_InvalidParamError("stream");
icculus@10757
   914
    } else if (!buf) {
icculus@10757
   915
        return SDL_InvalidParamError("buf");
icculus@10757
   916
    } else if (buflen == 0) {
icculus@10757
   917
        return 0;  /* nothing to do. */
icculus@10757
   918
    } else if ((buflen % stream->src_sample_frame_size) != 0) {
icculus@10757
   919
        return SDL_SetError("Can't add partial sample frames");
icculus@10757
   920
    }
icculus@10757
   921
icculus@10757
   922
    if (stream->cvt_before_resampling.needed) {
icculus@10757
   923
        const int workbuflen = buflen * stream->cvt_before_resampling.len_mult;  /* will be "* 1" if not needed */
icculus@10757
   924
        Uint8 *workbuf = EnsureBufferSize(&stream->work_buffer, &stream->work_buffer_len, workbuflen);
icculus@10757
   925
        if (workbuf == NULL) {
icculus@10757
   926
            return -1;  /* probably out of memory. */
icculus@10757
   927
        }
icculus@10757
   928
        SDL_memcpy(workbuf, buf, buflen);
icculus@10757
   929
        stream->cvt_before_resampling.buf = workbuf;
icculus@10757
   930
        stream->cvt_before_resampling.len = buflen;
icculus@10757
   931
        if (SDL_ConvertAudio(&stream->cvt_before_resampling) == -1) {
icculus@10757
   932
            return -1;   /* uhoh! */
icculus@10757
   933
        }
icculus@10757
   934
        buf = workbuf;
icculus@10757
   935
        buflen = stream->cvt_before_resampling.len_cvt;
icculus@10757
   936
    }
icculus@10757
   937
icculus@10757
   938
    if (stream->dst_rate != stream->src_rate) {
icculus@10757
   939
        const int workbuflen = buflen * ((int) SDL_ceil(stream->rate_incr));
icculus@10757
   940
        float *workbuf = (float *) EnsureBufferSize(&stream->resample_buffer, &stream->resample_buffer_len, workbuflen);
icculus@10757
   941
        if (workbuf == NULL) {
icculus@10757
   942
            return -1;  /* probably out of memory. */
icculus@10757
   943
        }
slouken@10773
   944
        buflen = stream->resampler_func(stream, (float *) buf, buflen, workbuf, workbuflen);
icculus@10757
   945
        buf = workbuf;
icculus@10757
   946
    }
icculus@10757
   947
icculus@10757
   948
    if (stream->cvt_after_resampling.needed) {
icculus@10757
   949
        const int workbuflen = buflen * stream->cvt_before_resampling.len_mult;  /* will be "* 1" if not needed */
icculus@10757
   950
        Uint8 *workbuf;
icculus@10757
   951
icculus@10757
   952
        if (buf == stream->resample_buffer) {
icculus@10757
   953
            workbuf = EnsureBufferSize(&stream->resample_buffer, &stream->resample_buffer_len, workbuflen);
icculus@10757
   954
        } else {
icculus@10757
   955
            const int inplace = (buf == stream->work_buffer);
icculus@10757
   956
            workbuf = EnsureBufferSize(&stream->work_buffer, &stream->work_buffer_len, workbuflen);
icculus@10757
   957
            if (workbuf && !inplace) {
icculus@10757
   958
                SDL_memcpy(workbuf, buf, buflen);
icculus@10757
   959
            }
icculus@10757
   960
        }
icculus@10757
   961
icculus@10757
   962
        if (workbuf == NULL) {
icculus@10757
   963
            return -1;  /* probably out of memory. */
icculus@10757
   964
        }
icculus@10757
   965
icculus@10757
   966
        stream->cvt_after_resampling.buf = workbuf;
icculus@10757
   967
        stream->cvt_after_resampling.len = buflen;
icculus@10757
   968
        if (SDL_ConvertAudio(&stream->cvt_after_resampling) == -1) {
icculus@10757
   969
            return -1;   /* uhoh! */
icculus@10757
   970
        }
icculus@10757
   971
        buf = workbuf;
icculus@10757
   972
        buflen = stream->cvt_after_resampling.len_cvt;
icculus@10757
   973
    }
icculus@10757
   974
icculus@10757
   975
    return SDL_WriteToDataQueue(stream->queue, buf, buflen);
icculus@10757
   976
}
icculus@10757
   977
icculus@10757
   978
void
icculus@10757
   979
SDL_AudioStreamClear(SDL_AudioStream *stream)
icculus@10757
   980
{
icculus@10757
   981
    if (!stream) {
icculus@10757
   982
        SDL_InvalidParamError("stream");
icculus@10757
   983
    } else {
icculus@10757
   984
        SDL_ClearDataQueue(stream->queue, stream->packetlen * 2);
icculus@10776
   985
        if (stream->reset_resampler_func) {
icculus@10776
   986
            stream->reset_resampler_func(stream);
icculus@10776
   987
        }
icculus@10757
   988
    }
icculus@10757
   989
}
icculus@10757
   990
icculus@10757
   991
icculus@10757
   992
/* get converted/resampled data from the stream */
icculus@10757
   993
int
icculus@10764
   994
SDL_AudioStreamGet(SDL_AudioStream *stream, void *buf, const Uint32 len)
icculus@10757
   995
{
icculus@10757
   996
    if (!stream) {
icculus@10757
   997
        return SDL_InvalidParamError("stream");
icculus@10757
   998
    } else if (!buf) {
icculus@10757
   999
        return SDL_InvalidParamError("buf");
icculus@10757
  1000
    } else if (len == 0) {
icculus@10757
  1001
        return 0;  /* nothing to do. */
icculus@10757
  1002
    } else if ((len % stream->dst_sample_frame_size) != 0) {
icculus@10757
  1003
        return SDL_SetError("Can't request partial sample frames");
icculus@10757
  1004
    }
icculus@10757
  1005
icculus@10764
  1006
    return (int) SDL_ReadFromDataQueue(stream->queue, buf, len);
icculus@10757
  1007
}
icculus@10757
  1008
icculus@10757
  1009
/* number of converted/resampled bytes available */
icculus@10757
  1010
int
icculus@10757
  1011
SDL_AudioStreamAvailable(SDL_AudioStream *stream)
icculus@10757
  1012
{
icculus@10757
  1013
    return stream ? (int) SDL_CountDataQueue(stream->queue) : 0;
icculus@10757
  1014
}
icculus@10757
  1015
icculus@10757
  1016
/* dispose of a stream */
icculus@10757
  1017
void
icculus@10757
  1018
SDL_FreeAudioStream(SDL_AudioStream *stream)
icculus@10757
  1019
{
icculus@10757
  1020
    if (stream) {
slouken@10773
  1021
        if (stream->cleanup_resampler_func) {
slouken@10773
  1022
            stream->cleanup_resampler_func(stream);
slouken@10773
  1023
        }
icculus@10757
  1024
        SDL_FreeDataQueue(stream->queue);
icculus@10757
  1025
        SDL_free(stream->work_buffer);
icculus@10757
  1026
        SDL_free(stream->resample_buffer);
icculus@10757
  1027
        SDL_free(stream);
icculus@10757
  1028
    }
icculus@10757
  1029
}
icculus@10757
  1030
icculus@10575
  1031
/* vi: set ts=4 sw=4 expandtab: */
slouken@2716
  1032