src/audio/SDL_audio.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 17 Oct 2006 09:15:21 +0000
changeset 2049 5f6550e5184f
parent 2045 510892a215a2
child 2053 716cf90f24a0
permissions -rw-r--r--
Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
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
/* Allow access to a raw mixing buffer */
slouken@0
    25
slouken@0
    26
#include "SDL.h"
slouken@0
    27
#include "SDL_audio_c.h"
slouken@0
    28
#include "SDL_audiomem.h"
slouken@0
    29
#include "SDL_sysaudio.h"
slouken@0
    30
icculus@2049
    31
#define _THIS	SDL_AudioDevice *this
icculus@2049
    32
icculus@2049
    33
static SDL_AudioDriver current_audio;
icculus@2049
    34
static SDL_AudioDevice *open_devices[16];
icculus@2049
    35
icculus@2049
    36
/* !!! FIXME: These are wordy and unlocalized... */
icculus@2049
    37
#define DEFAULT_OUTPUT_DEVNAME "System audio output device"
icculus@2049
    38
#define DEFAULT_INPUT_DEVNAME "System audio capture device"
icculus@2049
    39
icculus@2049
    40
icculus@2049
    41
/*
icculus@2049
    42
 * Not all of these will be compiled and linked in, but it's convenient
icculus@2049
    43
 *  to have a complete list here and saves yet-another block of #ifdefs...
icculus@2049
    44
 *  Please see bootstrap[], below, for the actual #ifdef mess.
icculus@2049
    45
 */
icculus@2049
    46
extern AudioBootStrap BSD_AUDIO_bootstrap;
icculus@2049
    47
extern AudioBootStrap DSP_bootstrap;
icculus@2049
    48
extern AudioBootStrap DMA_bootstrap;
icculus@2049
    49
extern AudioBootStrap ALSA_bootstrap;
icculus@2049
    50
extern AudioBootStrap QNXNTOAUDIO_bootstrap;
icculus@2049
    51
extern AudioBootStrap SUNAUDIO_bootstrap;
icculus@2049
    52
extern AudioBootStrap DMEDIA_bootstrap;
icculus@2049
    53
extern AudioBootStrap ARTS_bootstrap;
icculus@2049
    54
extern AudioBootStrap ESD_bootstrap;
icculus@2049
    55
extern AudioBootStrap NAS_bootstrap;
icculus@2049
    56
extern AudioBootStrap DSOUND_bootstrap;
icculus@2049
    57
extern AudioBootStrap WAVEOUT_bootstrap;
icculus@2049
    58
extern AudioBootStrap PAUDIO_bootstrap;
icculus@2049
    59
extern AudioBootStrap BEOSAUDIO_bootstrap;
icculus@2049
    60
extern AudioBootStrap COREAUDIO_bootstrap;
icculus@2049
    61
extern AudioBootStrap SNDMGR_bootstrap;
icculus@2049
    62
extern AudioBootStrap MINTAUDIO_GSXB_bootstrap;
icculus@2049
    63
extern AudioBootStrap MINTAUDIO_MCSN_bootstrap;
icculus@2049
    64
extern AudioBootStrap MINTAUDIO_STFA_bootstrap;
icculus@2049
    65
extern AudioBootStrap MINTAUDIO_XBIOS_bootstrap;
icculus@2049
    66
extern AudioBootStrap MINTAUDIO_DMA8_bootstrap;
icculus@2049
    67
extern AudioBootStrap DISKAUD_bootstrap;
icculus@2049
    68
extern AudioBootStrap DUMMYAUD_bootstrap;
icculus@2049
    69
extern AudioBootStrap DCAUD_bootstrap;
icculus@2049
    70
extern AudioBootStrap MMEAUDIO_bootstrap;
icculus@2049
    71
extern AudioBootStrap DART_bootstrap;
icculus@2049
    72
icculus@1190
    73
slouken@0
    74
/* Available audio drivers */
slouken@0
    75
static AudioBootStrap *bootstrap[] = {
slouken@1567
    76
#if SDL_AUDIO_DRIVER_BSD
slouken@1895
    77
    &BSD_AUDIO_bootstrap,
slouken@94
    78
#endif
slouken@1361
    79
#if SDL_AUDIO_DRIVER_OSS
slouken@1895
    80
    &DSP_bootstrap,
slouken@1895
    81
    &DMA_bootstrap,
slouken@0
    82
#endif
slouken@1361
    83
#if SDL_AUDIO_DRIVER_ALSA
slouken@1895
    84
    &ALSA_bootstrap,
slouken@0
    85
#endif
slouken@1361
    86
#if SDL_AUDIO_DRIVER_QNXNTO
slouken@1895
    87
    &QNXNTOAUDIO_bootstrap,
slouken@663
    88
#endif
slouken@1361
    89
#if SDL_AUDIO_DRIVER_SUNAUDIO
slouken@1895
    90
    &SUNAUDIO_bootstrap,
slouken@148
    91
#endif
slouken@1361
    92
#if SDL_AUDIO_DRIVER_DMEDIA
slouken@1895
    93
    &DMEDIA_bootstrap,
slouken@35
    94
#endif
slouken@1361
    95
#if SDL_AUDIO_DRIVER_ARTS
slouken@1895
    96
    &ARTS_bootstrap,
slouken@0
    97
#endif
slouken@1361
    98
#if SDL_AUDIO_DRIVER_ESD
slouken@1895
    99
    &ESD_bootstrap,
slouken@0
   100
#endif
slouken@1361
   101
#if SDL_AUDIO_DRIVER_NAS
slouken@1895
   102
    &NAS_bootstrap,
slouken@0
   103
#endif
slouken@1361
   104
#if SDL_AUDIO_DRIVER_DSOUND
slouken@1895
   105
    &DSOUND_bootstrap,
slouken@0
   106
#endif
slouken@1361
   107
#if SDL_AUDIO_DRIVER_WAVEOUT
slouken@1895
   108
    &WAVEOUT_bootstrap,
slouken@0
   109
#endif
icculus@2049
   110
#if SDL_AUDIO_DRIVER_PAUDIO
icculus@2049
   111
    &PAUDIO_bootstrap,
slouken@1361
   112
#endif
icculus@2049
   113
#if SDL_AUDIO_DRIVER_BEOSAUDIO
icculus@2049
   114
    &BEOSAUDIO_bootstrap,
slouken@0
   115
#endif
slouken@1361
   116
#if SDL_AUDIO_DRIVER_COREAUDIO
slouken@1895
   117
    &COREAUDIO_bootstrap,
slouken@935
   118
#endif
slouken@1361
   119
#if SDL_AUDIO_DRIVER_SNDMGR
slouken@1895
   120
    &SNDMGR_bootstrap,
slouken@0
   121
#endif
slouken@1361
   122
#if SDL_AUDIO_DRIVER_MINT
slouken@1895
   123
    &MINTAUDIO_GSXB_bootstrap,
slouken@1895
   124
    &MINTAUDIO_MCSN_bootstrap,
slouken@1895
   125
    &MINTAUDIO_STFA_bootstrap,
slouken@1895
   126
    &MINTAUDIO_XBIOS_bootstrap,
slouken@1895
   127
    &MINTAUDIO_DMA8_bootstrap,
slouken@398
   128
#endif
slouken@1361
   129
#if SDL_AUDIO_DRIVER_DISK
slouken@1895
   130
    &DISKAUD_bootstrap,
slouken@68
   131
#endif
icculus@1532
   132
#if SDL_AUDIO_DRIVER_DUMMY
slouken@1895
   133
    &DUMMYAUD_bootstrap,
icculus@1532
   134
#endif
slouken@1361
   135
#if SDL_AUDIO_DRIVER_DC
slouken@1895
   136
    &DCAUD_bootstrap,
slouken@509
   137
#endif
slouken@1361
   138
#if SDL_AUDIO_DRIVER_MMEAUDIO
slouken@1895
   139
    &MMEAUDIO_bootstrap,
slouken@1361
   140
#endif
slouken@1361
   141
#if SDL_AUDIO_DRIVER_DART
slouken@1895
   142
    &DART_bootstrap,
icculus@1190
   143
#endif
slouken@1895
   144
    NULL
slouken@0
   145
};
icculus@2049
   146
icculus@2049
   147
static SDL_AudioDevice *get_audio_device(SDL_AudioDeviceID id)
icculus@2049
   148
{
icculus@2049
   149
    id--;
icculus@2049
   150
    if ( (id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL) ) {
icculus@2049
   151
        SDL_SetError("Invalid audio device ID");
icculus@2049
   152
        return NULL;
icculus@2049
   153
    }
icculus@2049
   154
icculus@2049
   155
    return open_devices[id];
icculus@2049
   156
}
icculus@2049
   157
icculus@2049
   158
icculus@2049
   159
/* stubs for audio drivers that don't need a specific entry point... */
icculus@2049
   160
static int SDL_AudioDetectDevices_Default(int iscapture) { return -1; }
icculus@2049
   161
static void SDL_AudioThreadInit_Default(_THIS) { /* no-op. */ }
icculus@2049
   162
static void SDL_AudioWaitDevice_Default(_THIS) { /* no-op. */ }
icculus@2049
   163
static void SDL_AudioPlayDevice_Default(_THIS) { /* no-op. */ }
icculus@2049
   164
static Uint8 *SDL_AudioGetDeviceBuf_Default(_THIS) { return NULL; }
icculus@2049
   165
static void SDL_AudioWaitDone_Default(_THIS) { /* no-op. */ }
icculus@2049
   166
static void SDL_AudioCloseDevice_Default(_THIS) { /* no-op. */ }
icculus@2049
   167
static void SDL_AudioDeinitialize_Default(void) { /* no-op. */ }
icculus@2049
   168
icculus@2049
   169
static int
icculus@2049
   170
SDL_AudioOpenDevice_Default(_THIS, const char *devname, int iscapture)
icculus@2049
   171
{
icculus@2049
   172
    return 0;
icculus@2049
   173
}
icculus@2049
   174
icculus@2049
   175
static const char *SDL_AudioGetDeviceName_Default(int index, int iscapture)
icculus@2049
   176
{
icculus@2049
   177
    SDL_SetError("No such device");
icculus@2049
   178
    return NULL;
icculus@2049
   179
}
slouken@0
   180
icculus@2049
   181
static void
icculus@2049
   182
SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   183
{
icculus@2049
   184
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
icculus@2049
   185
        return;
icculus@2049
   186
    }
icculus@2049
   187
    SDL_mutexP(device->mixer_lock);
icculus@2049
   188
}
icculus@2049
   189
icculus@2049
   190
static void
icculus@2049
   191
SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   192
{
icculus@2049
   193
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
icculus@2049
   194
        return;
icculus@2049
   195
    }
icculus@2049
   196
    SDL_mutexV(device->mixer_lock);
icculus@2049
   197
}
icculus@2049
   198
slouken@0
   199
icculus@2049
   200
static void finalize_audio_entry_points(void)
icculus@2049
   201
{
icculus@2049
   202
    /*
icculus@2049
   203
     * Fill in stub functions for unused driver entry points. This lets us
icculus@2049
   204
     *  blindly call them without having to check for validity first.
icculus@2049
   205
     */
icculus@2049
   206
icculus@2049
   207
    #define FILL_STUB(x) \
icculus@2049
   208
        if (current_audio.impl.x == NULL) { \
icculus@2049
   209
            current_audio.impl.x = SDL_Audio##x##_Default; \
icculus@2049
   210
        }
icculus@2049
   211
    FILL_STUB(DetectDevices);
icculus@2049
   212
    FILL_STUB(GetDeviceName);
icculus@2049
   213
    FILL_STUB(OpenDevice);
icculus@2049
   214
    FILL_STUB(ThreadInit);
icculus@2049
   215
    FILL_STUB(WaitDevice);
icculus@2049
   216
    FILL_STUB(PlayDevice);
icculus@2049
   217
    FILL_STUB(GetDeviceBuf);
icculus@2049
   218
    FILL_STUB(WaitDone);
icculus@2049
   219
    FILL_STUB(CloseDevice);
icculus@2049
   220
    FILL_STUB(LockDevice);
icculus@2049
   221
    FILL_STUB(UnlockDevice);
icculus@2049
   222
    FILL_STUB(Deinitialize);
icculus@2049
   223
    #undef FILL_STUB
icculus@2049
   224
}
icculus@2049
   225
slouken@0
   226
slouken@0
   227
/* The general mixing thread function */
slouken@1895
   228
int SDLCALL
icculus@2049
   229
SDL_RunAudio(void *devicep)
slouken@0
   230
{
icculus@2049
   231
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
icculus@2049
   232
    const int legacy_device = (device == open_devices[0]);
slouken@1895
   233
    Uint8 *stream;
slouken@1895
   234
    int stream_len;
slouken@1895
   235
    void *udata;
slouken@1895
   236
    void (SDLCALL * fill) (void *userdata, Uint8 * stream, int len);
slouken@1895
   237
    int silence;
slouken@0
   238
slouken@1895
   239
    /* Perform any thread setup */
icculus@2049
   240
    device->threadid = SDL_ThreadID();
icculus@2049
   241
    current_audio.impl.ThreadInit(device);
slouken@0
   242
slouken@1895
   243
    /* Set up the mixing function */
icculus@2049
   244
    fill = device->spec.callback;
icculus@2049
   245
    udata = device->spec.userdata;
slouken@21
   246
icculus@2049
   247
    if (device->convert.needed) {
icculus@2049
   248
        if (device->convert.src_format == AUDIO_U8) {
slouken@1895
   249
            silence = 0x80;
slouken@1895
   250
        } else {
slouken@1895
   251
            silence = 0;
slouken@1895
   252
        }
icculus@2049
   253
        stream_len = device->convert.len;
slouken@1895
   254
    } else {
icculus@2049
   255
        silence = device->spec.silence;
icculus@2049
   256
        stream_len = device->spec.size;
slouken@1895
   257
    }
icculus@1561
   258
slouken@1895
   259
    /* Loop, filling the audio buffers */
icculus@2049
   260
    while (device->enabled) {
slouken@0
   261
slouken@1895
   262
        /* Fill the current buffer with sound */
icculus@2049
   263
        if (device->convert.needed) {
icculus@2049
   264
            if (device->convert.buf) {
icculus@2049
   265
                stream = device->convert.buf;
slouken@1895
   266
            } else {
slouken@1895
   267
                continue;
slouken@1895
   268
            }
slouken@1895
   269
        } else {
icculus@2049
   270
            stream = current_audio.impl.GetDeviceBuf(device);
slouken@1895
   271
            if (stream == NULL) {
icculus@2049
   272
                stream = device->fake_stream;
slouken@1895
   273
            }
slouken@1895
   274
        }
slouken@0
   275
icculus@2049
   276
        /* New code should fill buffer or set it to silence themselves. */
icculus@2049
   277
        if (legacy_device) {
icculus@2049
   278
            SDL_memset(stream, silence, stream_len);
icculus@2049
   279
        }
icculus@2049
   280
icculus@2049
   281
        if (!device->paused) {
icculus@2049
   282
            SDL_mutexP(device->mixer_lock);
slouken@1895
   283
            (*fill) (udata, stream, stream_len);
icculus@2049
   284
            SDL_mutexV(device->mixer_lock);
slouken@1895
   285
        }
slouken@0
   286
slouken@1895
   287
        /* Convert the audio if necessary */
icculus@2049
   288
        if (device->convert.needed) {
icculus@2049
   289
            SDL_ConvertAudio(&device->convert);
icculus@2049
   290
            stream = current_audio.impl.GetDeviceBuf(device);
slouken@1895
   291
            if (stream == NULL) {
icculus@2049
   292
                stream = device->fake_stream;
slouken@1895
   293
            }
icculus@2049
   294
            SDL_memcpy(stream, device->convert.buf, device->convert.len_cvt);
slouken@1895
   295
        }
slouken@0
   296
slouken@1895
   297
        /* Ready current buffer for play and change current buffer */
icculus@2049
   298
        if (stream != device->fake_stream) {
icculus@2049
   299
            current_audio.impl.PlayDevice(device);
slouken@1895
   300
        }
slouken@1562
   301
slouken@1895
   302
        /* Wait for an audio buffer to become available */
icculus@2049
   303
        if (stream == device->fake_stream) {
icculus@2049
   304
            SDL_Delay((device->spec.samples * 1000) / device->spec.freq);
slouken@1895
   305
        } else {
icculus@2049
   306
            current_audio.impl.WaitDevice(device);
slouken@1895
   307
        }
slouken@1895
   308
    }
slouken@1562
   309
slouken@1895
   310
    /* Wait for the audio to drain.. */
icculus@2049
   311
    current_audio.impl.WaitDone(device);
slouken@21
   312
slouken@1895
   313
    return (0);
slouken@0
   314
}
slouken@0
   315
slouken@322
   316
icculus@1982
   317
static SDL_AudioFormat
slouken@1895
   318
SDL_ParseAudioFormat(const char *string)
slouken@1794
   319
{
icculus@2049
   320
    #define CHECK_FMT_STRING(x) if (strcmp(string, #x) == 0) return AUDIO_##x
icculus@2049
   321
    CHECK_FMT_STRING(U8);
icculus@2049
   322
    CHECK_FMT_STRING(S8);
icculus@2049
   323
    CHECK_FMT_STRING(U16LSB);
icculus@2049
   324
    CHECK_FMT_STRING(S16LSB);
icculus@2049
   325
    CHECK_FMT_STRING(U16MSB);
icculus@2049
   326
    CHECK_FMT_STRING(S16MSB);
icculus@2049
   327
    CHECK_FMT_STRING(U16SYS);
icculus@2049
   328
    CHECK_FMT_STRING(S16SYS);
icculus@2049
   329
    CHECK_FMT_STRING(U16);
icculus@2049
   330
    CHECK_FMT_STRING(S16);
icculus@2049
   331
    CHECK_FMT_STRING(S32LSB);
icculus@2049
   332
    CHECK_FMT_STRING(S32MSB);
icculus@2049
   333
    CHECK_FMT_STRING(S32SYS);
icculus@2049
   334
    CHECK_FMT_STRING(S32);
icculus@2049
   335
    CHECK_FMT_STRING(F32LSB);
icculus@2049
   336
    CHECK_FMT_STRING(F32MSB);
icculus@2049
   337
    CHECK_FMT_STRING(F32SYS);
icculus@2049
   338
    CHECK_FMT_STRING(F32);
icculus@2049
   339
    #undef CHECK_FMT_STRING
icculus@2049
   340
    return 0;
slouken@1895
   341
}
slouken@1895
   342
slouken@1895
   343
int
slouken@1895
   344
SDL_GetNumAudioDrivers(void)
slouken@1895
   345
{
slouken@1895
   346
    return (SDL_arraysize(bootstrap) - 1);
slouken@1895
   347
}
slouken@1895
   348
slouken@1895
   349
const char *
slouken@1895
   350
SDL_GetAudioDriver(int index)
slouken@1895
   351
{
slouken@1895
   352
    if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
slouken@1895
   353
        return (bootstrap[index]->name);
slouken@1895
   354
    }
slouken@1895
   355
    return (NULL);
slouken@1794
   356
}
slouken@1794
   357
slouken@1895
   358
int
slouken@1895
   359
SDL_AudioInit(const char *driver_name)
slouken@0
   360
{
icculus@2049
   361
    int i = 0;
icculus@2049
   362
    int initialized = 0;
icculus@2049
   363
    int tried_to_init = 0;
slouken@0
   364
icculus@2049
   365
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   366
        SDL_AudioQuit();  /* shutdown driver if already running. */
slouken@1895
   367
    }
slouken@0
   368
icculus@2049
   369
    SDL_memset(&current_audio, '\0', sizeof (current_audio));
icculus@2049
   370
    SDL_memset(open_devices, '\0', sizeof (open_devices));
icculus@2049
   371
slouken@1895
   372
    /* Select the proper audio driver */
slouken@1909
   373
    if (driver_name == NULL) {
slouken@1909
   374
        driver_name = SDL_getenv("SDL_AUDIODRIVER");
slouken@1909
   375
    }
icculus@2049
   376
icculus@2049
   377
    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
icculus@2049
   378
        /* make sure we should even try this driver before doing so... */
icculus@2049
   379
        const AudioBootStrap *backend = bootstrap[i];
icculus@2049
   380
        if ( ((driver_name) && (SDL_strcasecmp(backend->name, driver_name))) ||
icculus@2049
   381
             ((!driver_name) && (backend->demand_only)) ) {
icculus@2049
   382
            continue;
icculus@2049
   383
        }
slouken@0
   384
icculus@2049
   385
        tried_to_init = 1;
icculus@2049
   386
        SDL_memset(&current_audio, 0, sizeof (current_audio));
icculus@2049
   387
        current_audio.name = backend->name;
icculus@2049
   388
        current_audio.desc = backend->desc;
icculus@2049
   389
        initialized = backend->init(&current_audio.impl);
slouken@1895
   390
    }
icculus@2049
   391
icculus@2049
   392
    if (!initialized) {
icculus@2049
   393
        /* specific drivers will set the error message if they fail... */
icculus@2049
   394
        if (!tried_to_init) {
slouken@1895
   395
            if (driver_name) {
slouken@1895
   396
                SDL_SetError("%s not available", driver_name);
slouken@1895
   397
            } else {
slouken@1895
   398
                SDL_SetError("No available audio device");
slouken@1895
   399
            }
slouken@1895
   400
        }
icculus@2049
   401
icculus@2049
   402
        SDL_memset(&current_audio, 0, sizeof (current_audio));
icculus@2049
   403
        return (-1);  /* No driver was available, so fail. */
slouken@1895
   404
    }
icculus@2049
   405
icculus@2049
   406
    finalize_audio_entry_points();
icculus@2049
   407
slouken@1895
   408
    return (0);
slouken@0
   409
}
slouken@0
   410
slouken@1895
   411
/*
slouken@1895
   412
 * Get the current audio driver name
slouken@1895
   413
 */
slouken@1895
   414
const char *
slouken@1895
   415
SDL_GetCurrentAudioDriver()
slouken@0
   416
{
icculus@2049
   417
    return current_audio.name;
slouken@0
   418
}
slouken@0
   419
icculus@2049
   420
slouken@1895
   421
int
icculus@2049
   422
SDL_GetNumAudioDevices(int iscapture)
slouken@0
   423
{
icculus@2049
   424
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   425
        return -1;
icculus@2049
   426
    }
icculus@2049
   427
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   428
        return 0;
icculus@2049
   429
    }
icculus@2049
   430
icculus@2049
   431
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
   432
        return 1;
icculus@2049
   433
    }
icculus@2049
   434
icculus@2049
   435
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
   436
        return 1;
icculus@2049
   437
    }
icculus@2049
   438
icculus@2049
   439
    return current_audio.impl.DetectDevices(iscapture);
icculus@2049
   440
}
icculus@2049
   441
slouken@0
   442
icculus@2049
   443
const char *
icculus@2049
   444
SDL_GetAudioDeviceName(int index, int iscapture)
icculus@2049
   445
{
icculus@2049
   446
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   447
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   448
        return NULL;
icculus@2049
   449
    }
icculus@2049
   450
icculus@2049
   451
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   452
        SDL_SetError("No capture support");
icculus@2049
   453
        return NULL;
slouken@1895
   454
    }
icculus@2049
   455
icculus@2049
   456
    if (index < 0) {
icculus@2049
   457
        SDL_SetError("No such device");
icculus@2049
   458
        return NULL;
icculus@2049
   459
    }
slouken@0
   460
icculus@2049
   461
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
   462
        return DEFAULT_INPUT_DEVNAME;
icculus@2049
   463
    }
icculus@2049
   464
icculus@2049
   465
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
   466
        return DEFAULT_OUTPUT_DEVNAME;
slouken@1895
   467
    }
slouken@262
   468
icculus@2049
   469
    return current_audio.impl.GetDeviceName(index, iscapture);
icculus@2049
   470
}
icculus@2049
   471
icculus@2049
   472
icculus@2049
   473
static void
icculus@2049
   474
close_audio_device(SDL_AudioDevice *device)
icculus@2049
   475
{
icculus@2049
   476
    device->enabled = 0;
icculus@2049
   477
    if (device->thread != NULL) {
icculus@2049
   478
        SDL_WaitThread(device->thread, NULL);
icculus@2049
   479
    }
icculus@2049
   480
    if (device->mixer_lock != NULL) {
icculus@2049
   481
        SDL_DestroyMutex(device->mixer_lock);
icculus@2049
   482
    }
icculus@2049
   483
    if (device->fake_stream != NULL) {
icculus@2049
   484
        SDL_FreeAudioMem(device->fake_stream);
icculus@2049
   485
    }
icculus@2049
   486
    if (device->convert.needed) {
icculus@2049
   487
        SDL_FreeAudioMem(device->convert.buf);
icculus@2049
   488
    }
icculus@2049
   489
    if (device->opened) {
icculus@2049
   490
        current_audio.impl.CloseDevice(device);
icculus@2049
   491
        device->opened = 0;
icculus@2049
   492
    }
icculus@2049
   493
    SDL_FreeAudioMem(device);
icculus@2049
   494
}
icculus@2049
   495
icculus@2049
   496
icculus@2049
   497
/*
icculus@2049
   498
 * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
icculus@2049
   499
 *  Fills in a sanitized copy in (prepared).
icculus@2049
   500
 *  Returns non-zero if okay, zero on fatal parameters in (orig).
icculus@2049
   501
 */
icculus@2049
   502
static int
icculus@2049
   503
prepare_audiospec(const SDL_AudioSpec *orig, SDL_AudioSpec *prepared)
icculus@2049
   504
{
icculus@2049
   505
    SDL_memcpy(prepared, orig, sizeof (SDL_AudioSpec));
icculus@2049
   506
icculus@2049
   507
    if (orig->callback == NULL) {
icculus@2049
   508
        SDL_SetError("SDL_OpenAudio() passed a NULL callback");
icculus@2049
   509
        return 0;
icculus@2049
   510
    }
icculus@2049
   511
icculus@2049
   512
    if (orig->freq == 0) {
icculus@2049
   513
        const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
icculus@2049
   514
        if ( (!env) || ((prepared->freq = SDL_atoi(env)) == 0) ) {
icculus@2049
   515
            prepared->freq = 22050;  /* a reasonable default */
slouken@1895
   516
        }
slouken@1895
   517
    }
icculus@2049
   518
icculus@2049
   519
    if (orig->format == 0) {
icculus@2049
   520
        const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
icculus@2049
   521
        if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
icculus@2049
   522
            prepared->format = AUDIO_S16;  /* a reasonable default */
slouken@1895
   523
        }
slouken@1895
   524
    }
icculus@2049
   525
icculus@2049
   526
    switch (orig->channels) {
icculus@2049
   527
    case 0: {
icculus@2049
   528
        const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
icculus@2049
   529
        if ( (!env) || ((prepared->channels = SDL_atoi(env)) == 0) ) {
icculus@2049
   530
            prepared->channels = 2;  /* a reasonable default */
icculus@2049
   531
        }
icculus@2049
   532
        break;
slouken@1895
   533
    }
slouken@1895
   534
    case 1:                    /* Mono */
slouken@1895
   535
    case 2:                    /* Stereo */
slouken@1895
   536
    case 4:                    /* surround */
slouken@1895
   537
    case 6:                    /* surround with center and lfe */
slouken@1895
   538
        break;
slouken@1895
   539
    default:
icculus@2049
   540
        SDL_SetError("Unsupported number of audio channels.");
icculus@2049
   541
        return 0;
slouken@1895
   542
    }
icculus@2049
   543
icculus@2049
   544
    if (orig->samples == 0) {
icculus@2049
   545
        const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
icculus@2049
   546
        if ( (!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0) ) {
icculus@2049
   547
            /* Pick a default of ~46 ms at desired frequency */
icculus@2049
   548
            /* !!! FIXME: remove this when the non-Po2 resampling is in. */
icculus@2049
   549
            const int samples = (prepared->freq / 1000) * 46;
icculus@2049
   550
            int power2 = 1;
icculus@2049
   551
            while (power2 < samples) {
icculus@2049
   552
                power2 *= 2;
icculus@2049
   553
            }
icculus@2049
   554
            prepared->samples = power2;
slouken@1895
   555
        }
slouken@1895
   556
    }
slouken@0
   557
slouken@1895
   558
    /* Calculate the silence and size of the audio specification */
icculus@2049
   559
    SDL_CalculateAudioSpec(prepared);
slouken@21
   560
icculus@2049
   561
    return 1;
icculus@2049
   562
}
slouken@1408
   563
slouken@21
   564
icculus@2049
   565
static SDL_AudioDeviceID
icculus@2049
   566
open_audio_device(const char *devname, int iscapture,
icculus@2049
   567
                    const SDL_AudioSpec *_desired, SDL_AudioSpec *obtained,
icculus@2049
   568
                    int min_id)
icculus@2049
   569
{
icculus@2049
   570
    SDL_AudioDeviceID id = 0;
icculus@2049
   571
    SDL_AudioSpec desired;
icculus@2049
   572
    SDL_AudioDevice *device;
icculus@2049
   573
    int i = 0;
slouken@21
   574
icculus@2049
   575
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   576
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   577
        return 0;
icculus@2049
   578
    }
icculus@2049
   579
icculus@2049
   580
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   581
        SDL_SetError("No capture support");
icculus@2049
   582
        return 0;
icculus@2049
   583
    }
icculus@2049
   584
icculus@2049
   585
    if (!prepare_audiospec(_desired, &desired)) {
icculus@2049
   586
        return 0;
icculus@2049
   587
    }
icculus@2049
   588
icculus@2049
   589
    /* If app doesn't care about a specific device, let the user override. */
icculus@2049
   590
    if (devname == NULL) {
icculus@2049
   591
        devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
slouken@1895
   592
    }
slouken@21
   593
icculus@2049
   594
    /*
icculus@2049
   595
     * Catch device names at the high level for the simple case...
icculus@2049
   596
     * This lets us have a basic "device enumeration" for systems that
icculus@2049
   597
     *  don't have multiple devices, but makes sure the device name is
icculus@2049
   598
     *  always NULL when it hits the low level.
icculus@2049
   599
     *
icculus@2049
   600
     * Also make sure that the simple case prevents multiple simultaneous
icculus@2049
   601
     *  opens of the default system device.
icculus@2049
   602
     */
icculus@2049
   603
icculus@2049
   604
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
   605
        if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
icculus@2049
   606
            SDL_SetError("No such device");
icculus@2049
   607
            return 0;
icculus@2049
   608
        }
icculus@2049
   609
        devname = NULL;
icculus@2049
   610
icculus@2049
   611
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
   612
            if ((open_devices[i]) && (open_devices[i]->iscapture)) {
icculus@2049
   613
                SDL_SetError("Audio device already open");
icculus@2049
   614
                return 0;
icculus@2049
   615
            }
icculus@2049
   616
        }
icculus@2049
   617
    }
icculus@2049
   618
icculus@2049
   619
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
   620
        if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
icculus@2049
   621
            SDL_SetError("No such device");
icculus@2049
   622
            return 0;
icculus@2049
   623
        }
icculus@2049
   624
        devname = NULL;
icculus@2049
   625
icculus@2049
   626
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
   627
            if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
icculus@2049
   628
                SDL_SetError("Audio device already open");
icculus@2049
   629
                return 0;
icculus@2049
   630
            }
icculus@2049
   631
        }
icculus@2049
   632
    }
icculus@2049
   633
icculus@2049
   634
    device = (SDL_AudioDevice *) SDL_AllocAudioMem(sizeof (SDL_AudioDevice));
icculus@2049
   635
    if (device == NULL) {
icculus@2049
   636
        SDL_OutOfMemory();
icculus@2049
   637
        return 0;
icculus@2049
   638
    }
icculus@2049
   639
    SDL_memset(device, '\0', sizeof (SDL_AudioDevice));
icculus@2049
   640
    SDL_memcpy(&device->spec, &desired, sizeof (SDL_AudioSpec));
icculus@2049
   641
    device->enabled = 1;
icculus@2049
   642
    device->paused = 1;
icculus@2049
   643
    device->iscapture = iscapture;
icculus@2049
   644
icculus@2049
   645
    /* Create a semaphore for locking the sound buffers */
icculus@2049
   646
    if (!current_audio.impl.SkipMixerLock) {
icculus@2049
   647
        device->mixer_lock = SDL_CreateMutex();
icculus@2049
   648
        if (device->mixer_lock == NULL) {
icculus@2049
   649
            close_audio_device(device);
icculus@2049
   650
            SDL_SetError("Couldn't create mixer lock");
icculus@2049
   651
            return 0;
icculus@2049
   652
        }
icculus@2049
   653
    }
icculus@2049
   654
icculus@2049
   655
    if (!current_audio.impl.OpenDevice(device, devname, iscapture)) {
icculus@2049
   656
        close_audio_device(device);
icculus@2049
   657
        return 0;
icculus@2049
   658
    }
icculus@2049
   659
    device->opened = 1;
slouken@0
   660
slouken@1895
   661
    /* If the audio driver changes the buffer size, accept it */
icculus@2049
   662
    if (device->spec.samples != desired.samples) {
icculus@2049
   663
        desired.samples = device->spec.samples;
icculus@2049
   664
        SDL_CalculateAudioSpec(&device->spec);
slouken@1895
   665
    }
slouken@0
   666
slouken@1895
   667
    /* Allocate a fake audio memory buffer */
icculus@2049
   668
    device->fake_stream = SDL_AllocAudioMem(device->spec.size);
icculus@2049
   669
    if (device->fake_stream == NULL) {
icculus@2049
   670
        close_audio_device(device);
slouken@1895
   671
        SDL_OutOfMemory();
icculus@2049
   672
        return 0;
slouken@1895
   673
    }
slouken@0
   674
slouken@1895
   675
    /* See if we need to do any conversion */
slouken@1895
   676
    if (obtained != NULL) {
icculus@2049
   677
        SDL_memcpy(obtained, &device->spec, sizeof(SDL_AudioSpec));
icculus@2049
   678
    } else if (desired.freq != device->spec.freq ||
icculus@2049
   679
               desired.format != device->spec.format ||
icculus@2049
   680
               desired.channels != device->spec.channels) {
slouken@1895
   681
        /* Build an audio conversion block */
icculus@2049
   682
        if (SDL_BuildAudioCVT(&device->convert,
icculus@2049
   683
                              desired.format, desired.channels,
icculus@2049
   684
                              desired.freq,
icculus@2049
   685
                              device->spec.format, device->spec.channels,
icculus@2049
   686
                              device->spec.freq) < 0) {
icculus@2049
   687
            close_audio_device(device);
icculus@2049
   688
            return 0;
slouken@1895
   689
        }
icculus@2049
   690
        if (device->convert.needed) {
icculus@2049
   691
            device->convert.len = desired.size;
icculus@2049
   692
            device->convert.buf =
icculus@2049
   693
                (Uint8 *) SDL_AllocAudioMem(device->convert.len *
icculus@2049
   694
                                            device->convert.len_mult);
icculus@2049
   695
            if (device->convert.buf == NULL) {
icculus@2049
   696
                close_audio_device(device);
slouken@1895
   697
                SDL_OutOfMemory();
icculus@2049
   698
                return 0;
slouken@1895
   699
            }
slouken@1895
   700
        }
slouken@1895
   701
    }
icculus@2049
   702
icculus@2049
   703
    /* Find an available device ID and store the structure... */
icculus@2049
   704
    for (id = min_id-1; id < SDL_arraysize(open_devices); id++) {
icculus@2049
   705
        if (open_devices[id] == NULL) {
icculus@2049
   706
            open_devices[id] = device;
icculus@2049
   707
            break;
icculus@2049
   708
        }
icculus@2049
   709
    }
icculus@2049
   710
icculus@2049
   711
    if (id == SDL_arraysize(open_devices)) {
icculus@2049
   712
        SDL_SetError("Too many open audio devices");
icculus@2049
   713
        close_audio_device(device);
icculus@2049
   714
        return 0;
icculus@2049
   715
    }
icculus@2049
   716
slouken@1895
   717
    /* Start the audio thread if necessary */
icculus@2049
   718
    if (!current_audio.impl.ProvidesOwnCallbackThread) {
slouken@1895
   719
        /* Start the audio thread */
icculus@2049
   720
/* !!! FIXME: this is nasty. */
slouken@1402
   721
#if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
slouken@1330
   722
#undef SDL_CreateThread
icculus@2049
   723
        device->thread = SDL_CreateThread(SDL_RunAudio, device, NULL, NULL);
slouken@1330
   724
#else
icculus@2049
   725
        device->thread = SDL_CreateThread(SDL_RunAudio, device);
slouken@1330
   726
#endif
icculus@2049
   727
        if (device->thread == NULL) {
icculus@2049
   728
            SDL_CloseAudioDevice(id+1);
slouken@1895
   729
            SDL_SetError("Couldn't create audio thread");
icculus@2049
   730
            return 0;
icculus@2049
   731
        }
icculus@2049
   732
    }
icculus@2049
   733
icculus@2049
   734
    return id+1;
icculus@2049
   735
}
icculus@2049
   736
icculus@2049
   737
icculus@2049
   738
int
icculus@2049
   739
SDL_OpenAudio(const SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
icculus@2049
   740
{
icculus@2049
   741
    SDL_AudioDeviceID id = 0;
icculus@2049
   742
icculus@2049
   743
    /* Start up the audio driver, if necessary. This is legacy behaviour! */
icculus@2049
   744
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   745
        if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
slouken@1895
   746
            return (-1);
slouken@1895
   747
        }
icculus@2049
   748
    }
slouken@0
   749
icculus@2049
   750
    /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
icculus@2049
   751
    if (open_devices[0] != NULL) {
icculus@2049
   752
        SDL_SetError("Audio device is already opened");
icculus@2049
   753
        return (-1);
slouken@1895
   754
    }
icculus@2049
   755
icculus@2049
   756
    id = open_audio_device(NULL, 0, desired, obtained, 1);
icculus@2049
   757
    if (id > 1) {  /* this should never happen in theory... */
icculus@2049
   758
        SDL_CloseAudioDevice(id);
icculus@2049
   759
        SDL_SetError("Internal error");  /* MUST be Device ID #1! */
icculus@2049
   760
        return (-1);
icculus@2049
   761
    }
slouken@21
   762
icculus@2049
   763
    return ((id == 0) ? -1 : 0);
icculus@2049
   764
}
slouken@21
   765
icculus@2049
   766
SDL_AudioDeviceID
icculus@2049
   767
SDL_OpenAudioDevice(const char *device, int iscapture,
icculus@2049
   768
                    const SDL_AudioSpec *desired, SDL_AudioSpec *obtained)
icculus@2049
   769
{
icculus@2049
   770
    return open_audio_device(device, iscapture, desired, obtained, 2);
slouken@1895
   771
}
slouken@1895
   772
slouken@1895
   773
SDL_audiostatus
icculus@2049
   774
SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
slouken@1895
   775
{
icculus@2049
   776
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
   777
    SDL_audiostatus status = SDL_AUDIO_STOPPED;
icculus@2049
   778
    if (device && device->enabled) {
icculus@2049
   779
        if (device->paused) {
slouken@1895
   780
            status = SDL_AUDIO_PAUSED;
slouken@1895
   781
        } else {
slouken@1895
   782
            status = SDL_AUDIO_PLAYING;
slouken@1895
   783
        }
slouken@1895
   784
    }
slouken@1895
   785
    return (status);
slouken@0
   786
}
slouken@0
   787
icculus@2049
   788
icculus@2049
   789
SDL_audiostatus
icculus@2049
   790
SDL_GetAudioStatus(void)
icculus@2049
   791
{
icculus@2049
   792
    return SDL_GetAudioDeviceStatus(1);
icculus@2049
   793
}
icculus@2049
   794
icculus@2049
   795
void
icculus@2049
   796
SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
icculus@2049
   797
{
icculus@2049
   798
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
   799
    if (device) {
icculus@2049
   800
        device->paused = pause_on;
icculus@2049
   801
    }
icculus@2049
   802
}
icculus@2049
   803
slouken@1895
   804
void
slouken@1895
   805
SDL_PauseAudio(int pause_on)
slouken@0
   806
{
icculus@2049
   807
    SDL_PauseAudioDevice(1, pause_on);
icculus@2049
   808
}
icculus@2049
   809
slouken@0
   810
icculus@2049
   811
void
icculus@2049
   812
SDL_LockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
   813
{
icculus@2049
   814
    /* Obtain a lock on the mixing buffers */
icculus@2049
   815
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
   816
    if (device) {
icculus@2049
   817
        current_audio.impl.LockDevice(device);
slouken@1895
   818
    }
slouken@0
   819
}
slouken@0
   820
slouken@1895
   821
void
slouken@1895
   822
SDL_LockAudio(void)
slouken@0
   823
{
icculus@2049
   824
    SDL_LockAudioDevice(1);
icculus@2049
   825
}
slouken@0
   826
icculus@2049
   827
void
icculus@2049
   828
SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
   829
{
slouken@1895
   830
    /* Obtain a lock on the mixing buffers */
icculus@2049
   831
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
   832
    if (device) {
icculus@2049
   833
        current_audio.impl.UnlockDevice(device);
slouken@1895
   834
    }
slouken@0
   835
}
slouken@0
   836
slouken@1895
   837
void
slouken@1895
   838
SDL_UnlockAudio(void)
slouken@0
   839
{
icculus@2049
   840
    SDL_UnlockAudioDevice(1);
icculus@2049
   841
}
slouken@0
   842
icculus@2049
   843
void
icculus@2049
   844
SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
   845
{
icculus@2049
   846
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
   847
    if (device) {
icculus@2049
   848
        close_audio_device(device);
icculus@2049
   849
        open_devices[devid-1] = NULL;
slouken@1895
   850
    }
slouken@0
   851
}
slouken@0
   852
slouken@1895
   853
void
slouken@1895
   854
SDL_CloseAudio(void)
slouken@0
   855
{
icculus@2049
   856
    SDL_CloseAudioDevice(1);
slouken@0
   857
}
slouken@0
   858
slouken@1895
   859
void
slouken@1895
   860
SDL_AudioQuit(void)
slouken@0
   861
{
icculus@2049
   862
    SDL_AudioDeviceID i;
icculus@2049
   863
    for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
   864
        SDL_CloseAudioDevice(i);
icculus@2049
   865
    }
slouken@0
   866
icculus@2049
   867
    /* Free the driver data */
icculus@2049
   868
    current_audio.impl.Deinitialize();
icculus@2049
   869
    SDL_memset(&current_audio, '\0', sizeof (current_audio));
icculus@2049
   870
    SDL_memset(open_devices, '\0', sizeof (open_devices));
slouken@0
   871
}
slouken@0
   872
icculus@1982
   873
#define NUM_FORMATS 10
slouken@0
   874
static int format_idx;
slouken@0
   875
static int format_idx_sub;
icculus@1982
   876
static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
slouken@1895
   877
    {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
   878
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
slouken@1895
   879
    {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
   880
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
icculus@1982
   881
    {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB,
icculus@1982
   882
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
   883
    {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB,
icculus@1982
   884
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1982
   885
    {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB,
icculus@1982
   886
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
   887
    {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB,
icculus@1982
   888
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
   889
    {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB,
icculus@1993
   890
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
   891
    {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB,
icculus@1993
   892
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
   893
    {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB,
icculus@1993
   894
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
   895
    {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB,
icculus@1993
   896
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
slouken@0
   897
};
slouken@0
   898
icculus@1982
   899
SDL_AudioFormat
icculus@1982
   900
SDL_FirstAudioFormat(SDL_AudioFormat format)
slouken@0
   901
{
slouken@1895
   902
    for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
slouken@1895
   903
        if (format_list[format_idx][0] == format) {
slouken@1895
   904
            break;
slouken@1895
   905
        }
slouken@1895
   906
    }
slouken@1895
   907
    format_idx_sub = 0;
slouken@1895
   908
    return (SDL_NextAudioFormat());
slouken@0
   909
}
slouken@0
   910
icculus@1982
   911
SDL_AudioFormat
slouken@1895
   912
SDL_NextAudioFormat(void)
slouken@0
   913
{
slouken@1895
   914
    if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
slouken@1895
   915
        return (0);
slouken@1895
   916
    }
slouken@1895
   917
    return (format_list[format_idx][format_idx_sub++]);
slouken@0
   918
}
slouken@0
   919
slouken@1895
   920
void
slouken@1895
   921
SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
slouken@0
   922
{
slouken@1895
   923
    switch (spec->format) {
slouken@1895
   924
    case AUDIO_U8:
slouken@1895
   925
        spec->silence = 0x80;
slouken@1895
   926
        break;
slouken@1895
   927
    default:
slouken@1895
   928
        spec->silence = 0x00;
slouken@1895
   929
        break;
slouken@1895
   930
    }
icculus@2049
   931
    spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
slouken@1895
   932
    spec->size *= spec->channels;
slouken@1895
   933
    spec->size *= spec->samples;
slouken@0
   934
}
slouken@1895
   935
icculus@2049
   936
icculus@2049
   937
/*
icculus@2049
   938
 * Moved here from SDL_mixer.c, since it relies on internals of an opened
icculus@2049
   939
 *  audio device (and is deprecated, by the way!).
icculus@2049
   940
 */
icculus@2049
   941
void
icculus@2049
   942
SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
icculus@2049
   943
{
icculus@2049
   944
    /* Mix the user-level audio format */
icculus@2049
   945
    SDL_AudioDevice *device = get_audio_device(1);
icculus@2049
   946
    if (device != NULL) {
icculus@2049
   947
        SDL_AudioFormat format;
icculus@2049
   948
        if (device->convert.needed) {
icculus@2049
   949
            format = device->convert.src_format;
icculus@2049
   950
        } else {
icculus@2049
   951
            format = device->spec.format;
icculus@2049
   952
        }
icculus@2049
   953
        SDL_MixAudioFormat(dst, src, format, len, volume);
icculus@2049
   954
    }
icculus@2049
   955
}
icculus@2049
   956
slouken@1895
   957
/* vi: set ts=4 sw=4 expandtab: */