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