src/audio/SDL_audio.c
author Ryan C. Gordon
Thu, 18 Dec 2014 00:19:52 -0500
changeset 9278 8900afb78a19
parent 9148 e9b6e9f4a10e
child 9381 812b2dabf8a4
permissions -rw-r--r--
Initial merge of Emscripten port!

With this commit, you can compile SDL2 with Emscripten
( http://emscripten.org/ ), and make your SDL-based C/C++ program
into a web app.

This port was due to the efforts of several people, including: Charlie Birks,
Sathyanarayanan Gunasekaran, Jukka Jylänki, Alon Zakai, Edward Rudd,
Bruce Mitchener, and Martin Gerhardy. (Thanks, everyone!)
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@0
    22
slouken@0
    23
/* Allow access to a raw mixing buffer */
slouken@0
    24
slouken@0
    25
#include "SDL.h"
slouken@2984
    26
#include "SDL_audio.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
aschiffler@4865
    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 ALSA_bootstrap;
icculus@2271
    49
extern AudioBootStrap PULSEAUDIO_bootstrap;
slouken@3099
    50
extern AudioBootStrap QSAAUDIO_bootstrap;
icculus@2049
    51
extern AudioBootStrap SUNAUDIO_bootstrap;
icculus@2049
    52
extern AudioBootStrap ARTS_bootstrap;
icculus@2049
    53
extern AudioBootStrap ESD_bootstrap;
gabomdq@8833
    54
#if SDL_AUDIO_DRIVER_NACL
gabomdq@8833
    55
extern AudioBootStrap NACLAUD_bootstrap;
gabomdq@8833
    56
#endif
icculus@2049
    57
extern AudioBootStrap NAS_bootstrap;
icculus@5592
    58
extern AudioBootStrap XAUDIO2_bootstrap;
icculus@2049
    59
extern AudioBootStrap DSOUND_bootstrap;
icculus@5588
    60
extern AudioBootStrap WINMM_bootstrap;
icculus@2049
    61
extern AudioBootStrap PAUDIO_bootstrap;
icculus@7981
    62
extern AudioBootStrap HAIKUAUDIO_bootstrap;
icculus@2049
    63
extern AudioBootStrap COREAUDIO_bootstrap;
icculus@2049
    64
extern AudioBootStrap SNDMGR_bootstrap;
icculus@2049
    65
extern AudioBootStrap DISKAUD_bootstrap;
icculus@2049
    66
extern AudioBootStrap DUMMYAUD_bootstrap;
icculus@2049
    67
extern AudioBootStrap DCAUD_bootstrap;
icculus@2049
    68
extern AudioBootStrap DART_bootstrap;
slouken@2735
    69
extern AudioBootStrap NDSAUD_bootstrap;
slouken@2947
    70
extern AudioBootStrap FUSIONSOUND_bootstrap;
paul@4718
    71
extern AudioBootStrap ANDROIDAUD_bootstrap;
kimonline@7009
    72
extern AudioBootStrap PSPAUD_bootstrap;
icculus@7367
    73
extern AudioBootStrap SNDIO_bootstrap;
icculus@9278
    74
extern AudioBootStrap EmscriptenAudio_bootstrap;
icculus@1190
    75
gabomdq@8833
    76
slouken@0
    77
/* Available audio drivers */
slouken@3162
    78
static const AudioBootStrap *const bootstrap[] = {
icculus@2939
    79
#if SDL_AUDIO_DRIVER_PULSEAUDIO
icculus@2939
    80
    &PULSEAUDIO_bootstrap,
slouken@0
    81
#endif
slouken@1361
    82
#if SDL_AUDIO_DRIVER_ALSA
slouken@1895
    83
    &ALSA_bootstrap,
slouken@0
    84
#endif
icculus@7367
    85
#if SDL_AUDIO_DRIVER_SNDIO
icculus@7367
    86
    &SNDIO_bootstrap,
icculus@7367
    87
#endif
slouken@4548
    88
#if SDL_AUDIO_DRIVER_BSD
slouken@4548
    89
    &BSD_AUDIO_bootstrap,
slouken@4548
    90
#endif
icculus@2939
    91
#if SDL_AUDIO_DRIVER_OSS
icculus@2939
    92
    &DSP_bootstrap,
icculus@2271
    93
#endif
slouken@3099
    94
#if SDL_AUDIO_DRIVER_QSA
slouken@3099
    95
    &QSAAUDIO_bootstrap,
slouken@663
    96
#endif
slouken@1361
    97
#if SDL_AUDIO_DRIVER_SUNAUDIO
slouken@1895
    98
    &SUNAUDIO_bootstrap,
slouken@148
    99
#endif
slouken@1361
   100
#if SDL_AUDIO_DRIVER_ARTS
slouken@1895
   101
    &ARTS_bootstrap,
slouken@0
   102
#endif
slouken@1361
   103
#if SDL_AUDIO_DRIVER_ESD
slouken@1895
   104
    &ESD_bootstrap,
slouken@0
   105
#endif
gabomdq@8833
   106
#if SDL_AUDIO_DRIVER_NACL
gabomdq@8833
   107
   &NACLAUD_bootstrap,
gabomdq@8833
   108
#endif
slouken@1361
   109
#if SDL_AUDIO_DRIVER_NAS
slouken@1895
   110
    &NAS_bootstrap,
slouken@0
   111
#endif
icculus@5592
   112
#if SDL_AUDIO_DRIVER_XAUDIO2
icculus@5592
   113
    &XAUDIO2_bootstrap,
icculus@5592
   114
#endif
slouken@1361
   115
#if SDL_AUDIO_DRIVER_DSOUND
slouken@1895
   116
    &DSOUND_bootstrap,
slouken@0
   117
#endif
icculus@5588
   118
#if SDL_AUDIO_DRIVER_WINMM
icculus@5588
   119
    &WINMM_bootstrap,
slouken@0
   120
#endif
icculus@2049
   121
#if SDL_AUDIO_DRIVER_PAUDIO
icculus@2049
   122
    &PAUDIO_bootstrap,
slouken@1361
   123
#endif
icculus@7981
   124
#if SDL_AUDIO_DRIVER_HAIKU
icculus@7981
   125
    &HAIKUAUDIO_bootstrap,
slouken@0
   126
#endif
slouken@1361
   127
#if SDL_AUDIO_DRIVER_COREAUDIO
slouken@1895
   128
    &COREAUDIO_bootstrap,
slouken@935
   129
#endif
slouken@1361
   130
#if SDL_AUDIO_DRIVER_DISK
slouken@1895
   131
    &DISKAUD_bootstrap,
slouken@68
   132
#endif
icculus@1532
   133
#if SDL_AUDIO_DRIVER_DUMMY
slouken@1895
   134
    &DUMMYAUD_bootstrap,
icculus@1532
   135
#endif
slouken@2947
   136
#if SDL_AUDIO_DRIVER_FUSIONSOUND
slouken@2947
   137
    &FUSIONSOUND_bootstrap,
slouken@2947
   138
#endif
paul@4718
   139
#if SDL_AUDIO_DRIVER_ANDROID
paul@4718
   140
    &ANDROIDAUD_bootstrap,
paul@4718
   141
#endif
kimonline@7009
   142
#if SDL_AUDIO_DRIVER_PSP
kimonline@7009
   143
    &PSPAUD_bootstrap,
kimonline@7009
   144
#endif
icculus@9278
   145
#if SDL_AUDIO_DRIVER_EMSCRIPTEN
icculus@9278
   146
    &EmscriptenAudio_bootstrap,
icculus@9278
   147
#endif
slouken@1895
   148
    NULL
slouken@0
   149
};
icculus@2049
   150
slouken@2060
   151
static SDL_AudioDevice *
slouken@2060
   152
get_audio_device(SDL_AudioDeviceID id)
icculus@2049
   153
{
icculus@2049
   154
    id--;
slouken@2060
   155
    if ((id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL)) {
icculus@2049
   156
        SDL_SetError("Invalid audio device ID");
icculus@2049
   157
        return NULL;
icculus@2049
   158
    }
icculus@2049
   159
icculus@2049
   160
    return open_devices[id];
icculus@2049
   161
}
icculus@2049
   162
icculus@2049
   163
icculus@2049
   164
/* stubs for audio drivers that don't need a specific entry point... */
icculus@5593
   165
static void
icculus@5593
   166
SDL_AudioDetectDevices_Default(int iscapture, SDL_AddAudioDevice addfn)
icculus@5593
   167
{                               /* no-op. */
slouken@2060
   168
}
slouken@2735
   169
slouken@2060
   170
static void
slouken@2060
   171
SDL_AudioThreadInit_Default(_THIS)
slouken@2060
   172
{                               /* no-op. */
slouken@2060
   173
}
slouken@2735
   174
slouken@2060
   175
static void
slouken@2060
   176
SDL_AudioWaitDevice_Default(_THIS)
slouken@2060
   177
{                               /* no-op. */
slouken@2060
   178
}
slouken@2735
   179
slouken@2060
   180
static void
slouken@2060
   181
SDL_AudioPlayDevice_Default(_THIS)
slouken@2060
   182
{                               /* no-op. */
slouken@2060
   183
}
slouken@2735
   184
icculus@9031
   185
static int
icculus@9031
   186
SDL_AudioGetPendingBytes_Default(_THIS)
icculus@9031
   187
{
icculus@9031
   188
    return 0;
icculus@9031
   189
}
icculus@9031
   190
slouken@2060
   191
static Uint8 *
slouken@2060
   192
SDL_AudioGetDeviceBuf_Default(_THIS)
slouken@2060
   193
{
slouken@2060
   194
    return NULL;
slouken@2060
   195
}
slouken@2735
   196
slouken@2060
   197
static void
slouken@2060
   198
SDL_AudioWaitDone_Default(_THIS)
slouken@2060
   199
{                               /* no-op. */
slouken@2060
   200
}
slouken@2735
   201
slouken@2060
   202
static void
slouken@2060
   203
SDL_AudioCloseDevice_Default(_THIS)
slouken@2060
   204
{                               /* no-op. */
slouken@2060
   205
}
slouken@2735
   206
slouken@2060
   207
static void
slouken@2060
   208
SDL_AudioDeinitialize_Default(void)
slouken@2060
   209
{                               /* no-op. */
slouken@2060
   210
}
icculus@2049
   211
icculus@2049
   212
static int
icculus@2049
   213
SDL_AudioOpenDevice_Default(_THIS, const char *devname, int iscapture)
icculus@2049
   214
{
icculus@7038
   215
    return -1;
icculus@2049
   216
}
icculus@2049
   217
icculus@9010
   218
static SDL_INLINE SDL_bool
icculus@9010
   219
is_in_audio_device_thread(SDL_AudioDevice * device)
icculus@9010
   220
{
icculus@9010
   221
    /* The device thread locks the same mutex, but not through the public API.
icculus@9010
   222
       This check is in case the application, in the audio callback,
icculus@9010
   223
       tries to lock the thread that we've already locked from the
icculus@9010
   224
       device thread...just in case we only have non-recursive mutexes. */
icculus@9010
   225
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
icculus@9010
   226
        return SDL_TRUE;
icculus@9010
   227
    }
icculus@9010
   228
icculus@9010
   229
    return SDL_FALSE;
icculus@9010
   230
}
icculus@9010
   231
icculus@2049
   232
static void
icculus@2049
   233
SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   234
{
icculus@9010
   235
    if (!is_in_audio_device_thread(device)) {
icculus@9010
   236
        SDL_LockMutex(device->mixer_lock);
icculus@2049
   237
    }
icculus@2049
   238
}
icculus@2049
   239
icculus@2049
   240
static void
icculus@2049
   241
SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   242
{
icculus@9010
   243
    if (!is_in_audio_device_thread(device)) {
icculus@9010
   244
        SDL_UnlockMutex(device->mixer_lock);
icculus@2049
   245
    }
icculus@2049
   246
}
icculus@2049
   247
slouken@0
   248
slouken@2060
   249
static void
slouken@2060
   250
finalize_audio_entry_points(void)
icculus@2049
   251
{
icculus@2049
   252
    /*
icculus@2049
   253
     * Fill in stub functions for unused driver entry points. This lets us
icculus@2049
   254
     *  blindly call them without having to check for validity first.
icculus@2049
   255
     */
icculus@2049
   256
slouken@2060
   257
#define FILL_STUB(x) \
icculus@2049
   258
        if (current_audio.impl.x == NULL) { \
icculus@2049
   259
            current_audio.impl.x = SDL_Audio##x##_Default; \
icculus@2049
   260
        }
icculus@2049
   261
    FILL_STUB(DetectDevices);
icculus@2049
   262
    FILL_STUB(OpenDevice);
icculus@2049
   263
    FILL_STUB(ThreadInit);
icculus@2049
   264
    FILL_STUB(WaitDevice);
icculus@2049
   265
    FILL_STUB(PlayDevice);
icculus@9031
   266
    FILL_STUB(GetPendingBytes);
icculus@2049
   267
    FILL_STUB(GetDeviceBuf);
icculus@2049
   268
    FILL_STUB(WaitDone);
icculus@2049
   269
    FILL_STUB(CloseDevice);
icculus@2049
   270
    FILL_STUB(LockDevice);
icculus@2049
   271
    FILL_STUB(UnlockDevice);
icculus@2049
   272
    FILL_STUB(Deinitialize);
slouken@2060
   273
#undef FILL_STUB
icculus@2049
   274
}
icculus@2049
   275
icculus@8660
   276
#if 0  /* !!! FIXME: rewrite/remove this streamer code. */
slouken@2716
   277
/* Streaming functions (for when the input and output buffer sizes are different) */
slouken@2716
   278
/* Write [length] bytes from buf into the streamer */
slouken@4472
   279
static void
slouken@2716
   280
SDL_StreamWrite(SDL_AudioStreamer * stream, Uint8 * buf, int length)
slouken@2716
   281
{
slouken@2716
   282
    int i;
slouken@2716
   283
slouken@2716
   284
    for (i = 0; i < length; ++i) {
slouken@2716
   285
        stream->buffer[stream->write_pos] = buf[i];
slouken@2716
   286
        ++stream->write_pos;
slouken@2716
   287
    }
slouken@2716
   288
}
slouken@2716
   289
slouken@2716
   290
/* Read [length] bytes out of the streamer into buf */
slouken@4472
   291
static void
slouken@2716
   292
SDL_StreamRead(SDL_AudioStreamer * stream, Uint8 * buf, int length)
slouken@2716
   293
{
slouken@2716
   294
    int i;
slouken@2716
   295
slouken@2716
   296
    for (i = 0; i < length; ++i) {
slouken@2716
   297
        buf[i] = stream->buffer[stream->read_pos];
slouken@2716
   298
        ++stream->read_pos;
slouken@2716
   299
    }
slouken@2716
   300
}
slouken@2716
   301
slouken@4472
   302
static int
slouken@2716
   303
SDL_StreamLength(SDL_AudioStreamer * stream)
slouken@2716
   304
{
slouken@2716
   305
    return (stream->write_pos - stream->read_pos) % stream->max_len;
slouken@2716
   306
}
slouken@2716
   307
slouken@2716
   308
/* Initialize the stream by allocating the buffer and setting the read/write heads to the beginning */
slouken@4507
   309
#if 0
slouken@4472
   310
static int
slouken@2716
   311
SDL_StreamInit(SDL_AudioStreamer * stream, int max_len, Uint8 silence)
slouken@2716
   312
{
slouken@2716
   313
    /* First try to allocate the buffer */
slouken@2728
   314
    stream->buffer = (Uint8 *) SDL_malloc(max_len);
slouken@2716
   315
    if (stream->buffer == NULL) {
slouken@2716
   316
        return -1;
slouken@2716
   317
    }
slouken@2716
   318
slouken@2716
   319
    stream->max_len = max_len;
slouken@2716
   320
    stream->read_pos = 0;
slouken@2716
   321
    stream->write_pos = 0;
slouken@2716
   322
slouken@2716
   323
    /* Zero out the buffer */
slouken@2728
   324
    SDL_memset(stream->buffer, silence, max_len);
slouken@2728
   325
slouken@2728
   326
    return 0;
slouken@2716
   327
}
slouken@4507
   328
#endif
slouken@2716
   329
slouken@2716
   330
/* Deinitialize the stream simply by freeing the buffer */
slouken@4472
   331
static void
slouken@2716
   332
SDL_StreamDeinit(SDL_AudioStreamer * stream)
slouken@2716
   333
{
slouken@7719
   334
    SDL_free(stream->buffer);
slouken@2716
   335
}
icculus@8660
   336
#endif
slouken@2716
   337
icculus@9012
   338
icculus@9012
   339
/* buffer queueing support... */
icculus@9012
   340
icculus@9012
   341
/* this expects that you managed thread safety elsewhere. */
icculus@9012
   342
static void
icculus@9012
   343
free_audio_queue(SDL_AudioBufferQueue *buffer)
icculus@9012
   344
{
icculus@9012
   345
    while (buffer) {
icculus@9012
   346
        SDL_AudioBufferQueue *next = buffer->next;
icculus@9012
   347
        SDL_free(buffer);
icculus@9012
   348
        buffer = next;
icculus@9012
   349
    }
icculus@9012
   350
}
icculus@9012
   351
icculus@9012
   352
static void SDLCALL
icculus@9012
   353
SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int _len)
icculus@9012
   354
{
icculus@9012
   355
    /* this function always holds the mixer lock before being called. */
icculus@9012
   356
    Uint32 len = (Uint32) _len;
icculus@9012
   357
    SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
icculus@9012
   358
    SDL_AudioBufferQueue *buffer;
icculus@9012
   359
icculus@9012
   360
    SDL_assert(device != NULL);  /* this shouldn't ever happen, right?! */
icculus@9012
   361
    SDL_assert(_len >= 0);  /* this shouldn't ever happen, right?! */
icculus@9012
   362
icculus@9012
   363
    while ((len > 0) && ((buffer = device->buffer_queue_head) != NULL)) {
icculus@9012
   364
        const Uint32 avail = buffer->datalen - buffer->startpos;
icculus@9012
   365
        const Uint32 cpy = SDL_min(len, avail);
icculus@9012
   366
        SDL_assert(device->queued_bytes >= avail);
icculus@9012
   367
icculus@9012
   368
        SDL_memcpy(stream, buffer->data + buffer->startpos, cpy);
icculus@9012
   369
        buffer->startpos += cpy;
icculus@9012
   370
        stream += cpy;
icculus@9012
   371
        device->queued_bytes -= cpy;
icculus@9012
   372
        len -= cpy;
icculus@9012
   373
icculus@9012
   374
        if (buffer->startpos == buffer->datalen) {  /* packet is done, put it in the pool. */
icculus@9012
   375
            device->buffer_queue_head = buffer->next;
icculus@9012
   376
            SDL_assert((buffer->next != NULL) || (buffer == device->buffer_queue_tail));
icculus@9012
   377
            buffer->next = device->buffer_queue_pool;
icculus@9012
   378
            device->buffer_queue_pool = buffer;
icculus@9012
   379
        }
icculus@9012
   380
    }
icculus@9012
   381
icculus@9012
   382
    SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
icculus@9012
   383
icculus@9012
   384
    if (len > 0) {  /* fill any remaining space in the stream with silence. */
icculus@9012
   385
        SDL_assert(device->buffer_queue_head == NULL);
icculus@9012
   386
        SDL_memset(stream, device->spec.silence, len);
icculus@9012
   387
    }
icculus@9012
   388
icculus@9012
   389
    if (device->buffer_queue_head == NULL) {
icculus@9012
   390
        device->buffer_queue_tail = NULL;  /* in case we drained the queue entirely. */
icculus@9012
   391
    }
icculus@9012
   392
}
icculus@9012
   393
icculus@9012
   394
int
icculus@9012
   395
SDL_QueueAudio(SDL_AudioDeviceID devid, const void *_data, Uint32 len)
icculus@9012
   396
{
icculus@9012
   397
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@9012
   398
    const Uint8 *data = (const Uint8 *) _data;
icculus@9012
   399
    SDL_AudioBufferQueue *orighead;
icculus@9012
   400
    SDL_AudioBufferQueue *origtail;
icculus@9012
   401
    Uint32 origlen;
icculus@9012
   402
    Uint32 datalen;
icculus@9012
   403
icculus@9012
   404
    if (!device) {
icculus@9012
   405
        return -1;  /* get_audio_device() will have set the error state */
icculus@9012
   406
    }
icculus@9012
   407
icculus@9012
   408
    if (device->spec.callback != SDL_BufferQueueDrainCallback) {
icculus@9012
   409
        return SDL_SetError("Audio device has a callback, queueing not allowed");
icculus@9012
   410
    }
icculus@9012
   411
icculus@9012
   412
    current_audio.impl.LockDevice(device);
icculus@9012
   413
icculus@9012
   414
    orighead = device->buffer_queue_head;
icculus@9012
   415
    origtail = device->buffer_queue_tail;
icculus@9012
   416
    origlen = origtail ? origtail->datalen : 0;
icculus@9012
   417
icculus@9012
   418
    while (len > 0) {
icculus@9012
   419
        SDL_AudioBufferQueue *packet = device->buffer_queue_tail;
icculus@9012
   420
        SDL_assert(!packet || (packet->datalen <= SDL_AUDIOBUFFERQUEUE_PACKETLEN));
icculus@9012
   421
        if (!packet || (packet->datalen >= SDL_AUDIOBUFFERQUEUE_PACKETLEN)) {
icculus@9012
   422
            /* tail packet missing or completely full; we need a new packet. */
icculus@9012
   423
            packet = device->buffer_queue_pool;
icculus@9012
   424
            if (packet != NULL) {
icculus@9012
   425
                /* we have one available in the pool. */
icculus@9012
   426
                device->buffer_queue_pool = packet->next;
icculus@9012
   427
            } else {
icculus@9012
   428
                /* Have to allocate a new one! */
icculus@9012
   429
                packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
icculus@9012
   430
                if (packet == NULL) {
icculus@9012
   431
                    /* uhoh, reset so we've queued nothing new, free what we can. */
icculus@9012
   432
                    if (!origtail) {
icculus@9012
   433
                        packet = device->buffer_queue_head;  /* whole queue. */
icculus@9012
   434
                    } else {
icculus@9012
   435
                        packet = origtail->next;  /* what we added to existing queue. */
icculus@9012
   436
                        origtail->next = NULL;
icculus@9012
   437
                        origtail->datalen = origlen;
icculus@9012
   438
                    }
icculus@9012
   439
                    device->buffer_queue_head = orighead;
icculus@9012
   440
                    device->buffer_queue_tail = origtail;
icculus@9012
   441
                    device->buffer_queue_pool = NULL;
icculus@9012
   442
icculus@9012
   443
                    current_audio.impl.UnlockDevice(device);
icculus@9012
   444
icculus@9012
   445
                    free_audio_queue(packet);  /* give back what we can. */
icculus@9012
   446
icculus@9012
   447
                    return SDL_OutOfMemory();
icculus@9012
   448
                }
icculus@9012
   449
            }
icculus@9012
   450
            packet->datalen = 0;
icculus@9012
   451
            packet->startpos = 0;
icculus@9012
   452
            packet->next = NULL;
icculus@9012
   453
icculus@9012
   454
            SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
icculus@9012
   455
            if (device->buffer_queue_tail == NULL) {
icculus@9012
   456
                device->buffer_queue_head = packet;
icculus@9012
   457
            } else {
icculus@9012
   458
                device->buffer_queue_tail->next = packet;
icculus@9012
   459
            }
icculus@9012
   460
            device->buffer_queue_tail = packet;
icculus@9012
   461
        }
icculus@9012
   462
icculus@9012
   463
        datalen = SDL_min(len, SDL_AUDIOBUFFERQUEUE_PACKETLEN - packet->datalen);
icculus@9012
   464
        SDL_memcpy(packet->data + packet->datalen, data, datalen);
icculus@9012
   465
        data += datalen;
icculus@9012
   466
        len -= datalen;
icculus@9012
   467
        packet->datalen += datalen;
icculus@9012
   468
        device->queued_bytes += datalen;
icculus@9012
   469
    }
icculus@9012
   470
icculus@9012
   471
    current_audio.impl.UnlockDevice(device);
icculus@9012
   472
icculus@9012
   473
    return 0;
icculus@9012
   474
}
icculus@9012
   475
icculus@9012
   476
Uint32
icculus@9012
   477
SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid)
icculus@9012
   478
{
icculus@9012
   479
    Uint32 retval = 0;
icculus@9012
   480
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@9032
   481
icculus@9032
   482
    /* Nothing to do unless we're set up for queueing. */
icculus@9032
   483
    if (device && (device->spec.callback == SDL_BufferQueueDrainCallback)) {
icculus@9012
   484
        current_audio.impl.LockDevice(device);
icculus@9031
   485
        retval = device->queued_bytes + current_audio.impl.GetPendingBytes(device);
icculus@9012
   486
        current_audio.impl.UnlockDevice(device);
icculus@9012
   487
    }
icculus@9012
   488
icculus@9012
   489
    return retval;
icculus@9012
   490
}
icculus@9012
   491
icculus@9012
   492
void
icculus@9012
   493
SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
icculus@9012
   494
{
icculus@9012
   495
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@9012
   496
    SDL_AudioBufferQueue *buffer = NULL;
icculus@9012
   497
    if (!device) {
icculus@9012
   498
        return;  /* nothing to do. */
icculus@9012
   499
    }
icculus@9012
   500
icculus@9012
   501
    /* Blank out the device and release the mutex. Free it afterwards. */
icculus@9012
   502
    current_audio.impl.LockDevice(device);
icculus@9012
   503
    buffer = device->buffer_queue_head;
icculus@9012
   504
    device->buffer_queue_tail = NULL;
icculus@9012
   505
    device->buffer_queue_head = NULL;
icculus@9012
   506
    device->queued_bytes = 0;
icculus@9012
   507
    current_audio.impl.UnlockDevice(device);
icculus@9012
   508
icculus@9012
   509
    free_audio_queue(buffer);
icculus@9012
   510
}
icculus@9012
   511
icculus@9012
   512
dimitris@8761
   513
#if defined(__ANDROID__)
paul@4722
   514
#include <android/log.h>
aschiffler@4865
   515
#endif
paul@4722
   516
slouken@0
   517
/* The general mixing thread function */
slouken@1895
   518
int SDLCALL
icculus@2049
   519
SDL_RunAudio(void *devicep)
slouken@0
   520
{
icculus@2049
   521
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
slouken@1895
   522
    Uint8 *stream;
slouken@1895
   523
    int stream_len;
slouken@1895
   524
    void *udata;
slouken@1895
   525
    void (SDLCALL * fill) (void *userdata, Uint8 * stream, int len);
slouken@3336
   526
    Uint32 delay;
icculus@8660
   527
icculus@8660
   528
#if 0  /* !!! FIXME: rewrite/remove this streamer code. */
airlangga@5526
   529
    /* For streaming when the buffer sizes don't match up */
airlangga@5526
   530
    Uint8 *istream;
airlangga@5526
   531
    int istream_len = 0;
icculus@8660
   532
#endif
slouken@2716
   533
slouken@5509
   534
    /* The audio mixing is always a high priority thread */
slouken@5509
   535
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
slouken@5509
   536
slouken@1895
   537
    /* Perform any thread setup */
icculus@2049
   538
    device->threadid = SDL_ThreadID();
icculus@2049
   539
    current_audio.impl.ThreadInit(device);
slouken@0
   540
slouken@1895
   541
    /* Set up the mixing function */
icculus@2049
   542
    fill = device->spec.callback;
icculus@2049
   543
    udata = device->spec.userdata;
slouken@21
   544
slouken@2716
   545
    /* By default do not stream */
slouken@2716
   546
    device->use_streamer = 0;
slouken@2716
   547
icculus@2049
   548
    if (device->convert.needed) {
slouken@3040
   549
#if 0                           /* !!! FIXME: I took len_div out of the structure. Use rate_incr instead? */
slouken@2716
   550
        /* If the result of the conversion alters the length, i.e. resampling is being used, use the streamer */
slouken@2716
   551
        if (device->convert.len_mult != 1 || device->convert.len_div != 1) {
slouken@2716
   552
            /* The streamer's maximum length should be twice whichever is larger: spec.size or len_cvt */
slouken@2716
   553
            stream_max_len = 2 * device->spec.size;
slouken@2716
   554
            if (device->convert.len_mult > device->convert.len_div) {
slouken@2716
   555
                stream_max_len *= device->convert.len_mult;
slouken@2716
   556
                stream_max_len /= device->convert.len_div;
slouken@2716
   557
            }
slouken@2716
   558
            if (SDL_StreamInit(&device->streamer, stream_max_len, silence) <
slouken@2716
   559
                0)
slouken@2716
   560
                return -1;
slouken@2716
   561
            device->use_streamer = 1;
slouken@2716
   562
slouken@2716
   563
            /* istream_len should be the length of what we grab from the callback and feed to conversion,
slouken@2716
   564
               so that we get close to spec_size. I.e. we want device.spec_size = istream_len * u / d
slouken@2716
   565
             */
slouken@2716
   566
            istream_len =
slouken@2716
   567
                device->spec.size * device->convert.len_div /
slouken@2716
   568
                device->convert.len_mult;
slouken@2716
   569
        }
icculus@3021
   570
#endif
slouken@7518
   571
        stream_len = device->convert.len;
slouken@1895
   572
    } else {
icculus@2049
   573
        stream_len = device->spec.size;
slouken@1895
   574
    }
icculus@1561
   575
slouken@3336
   576
    /* Calculate the delay while paused */
slouken@3336
   577
    delay = ((device->spec.samples * 1000) / device->spec.freq);
slouken@3336
   578
slouken@2716
   579
    /* Determine if the streamer is necessary here */
icculus@8658
   580
#if 0  /* !!! FIXME: rewrite/remove this streamer code. */
slouken@2716
   581
    if (device->use_streamer == 1) {
slouken@3336
   582
        /* This code is almost the same as the old code. The difference is, instead of reading
slouken@2716
   583
           directly from the callback into "stream", then converting and sending the audio off,
slouken@2716
   584
           we go: callback -> "istream" -> (conversion) -> streamer -> stream -> device.
slouken@2716
   585
           However, reading and writing with streamer are done separately:
slouken@2716
   586
           - We only call the callback and write to the streamer when the streamer does not
slouken@2716
   587
           contain enough samples to output to the device.
slouken@2716
   588
           - We only read from the streamer and tell the device to play when the streamer
slouken@2716
   589
           does have enough samples to output.
slouken@2716
   590
           This allows us to perform resampling in the conversion step, where the output of the
slouken@2716
   591
           resampling process can be any number. We will have to see what a good size for the
slouken@2716
   592
           stream's maximum length is, but I suspect 2*max(len_cvt, stream_len) is a good figure.
slouken@2716
   593
         */
slouken@2716
   594
        while (device->enabled) {
slouken@3336
   595
slouken@3336
   596
            if (device->paused) {
slouken@3336
   597
                SDL_Delay(delay);
slouken@3336
   598
                continue;
slouken@3336
   599
            }
slouken@3336
   600
slouken@2716
   601
            /* Only read in audio if the streamer doesn't have enough already (if it does not have enough samples to output) */
slouken@2716
   602
            if (SDL_StreamLength(&device->streamer) < stream_len) {
slouken@2716
   603
                /* Set up istream */
slouken@2716
   604
                if (device->convert.needed) {
slouken@2716
   605
                    if (device->convert.buf) {
slouken@2716
   606
                        istream = device->convert.buf;
slouken@2716
   607
                    } else {
slouken@2716
   608
                        continue;
slouken@2716
   609
                    }
slouken@2716
   610
                } else {
slouken@3336
   611
/* FIXME: Ryan, this is probably wrong.  I imagine we don't want to get
slouken@3336
   612
 * a device buffer both here and below in the stream output.
slouken@3336
   613
 */
slouken@2716
   614
                    istream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   615
                    if (istream == NULL) {
slouken@2716
   616
                        istream = device->fake_stream;
slouken@2716
   617
                    }
slouken@2716
   618
                }
slouken@0
   619
slouken@2716
   620
                /* Read from the callback into the _input_ stream */
slouken@6977
   621
                SDL_LockMutex(device->mixer_lock);
slouken@3336
   622
                (*fill) (udata, istream, istream_len);
slouken@6977
   623
                SDL_UnlockMutex(device->mixer_lock);
slouken@2716
   624
slouken@2716
   625
                /* Convert the audio if necessary and write to the streamer */
slouken@2716
   626
                if (device->convert.needed) {
slouken@2716
   627
                    SDL_ConvertAudio(&device->convert);
slouken@2716
   628
                    if (istream == NULL) {
slouken@2716
   629
                        istream = device->fake_stream;
slouken@2716
   630
                    }
gabomdq@7678
   631
                    /* SDL_memcpy(istream, device->convert.buf, device->convert.len_cvt); */
slouken@2716
   632
                    SDL_StreamWrite(&device->streamer, device->convert.buf,
slouken@2716
   633
                                    device->convert.len_cvt);
slouken@2716
   634
                } else {
slouken@2716
   635
                    SDL_StreamWrite(&device->streamer, istream, istream_len);
slouken@2716
   636
                }
slouken@1895
   637
            }
slouken@2716
   638
slouken@2716
   639
            /* Only output audio if the streamer has enough to output */
slouken@2716
   640
            if (SDL_StreamLength(&device->streamer) >= stream_len) {
slouken@2716
   641
                /* Set up the output stream */
slouken@2716
   642
                if (device->convert.needed) {
slouken@2716
   643
                    if (device->convert.buf) {
slouken@2716
   644
                        stream = device->convert.buf;
slouken@2716
   645
                    } else {
slouken@2716
   646
                        continue;
slouken@2716
   647
                    }
slouken@2716
   648
                } else {
slouken@2716
   649
                    stream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   650
                    if (stream == NULL) {
slouken@2716
   651
                        stream = device->fake_stream;
slouken@2716
   652
                    }
slouken@2716
   653
                }
slouken@2716
   654
slouken@2716
   655
                /* Now read from the streamer */
slouken@2716
   656
                SDL_StreamRead(&device->streamer, stream, stream_len);
slouken@2716
   657
slouken@2716
   658
                /* Ready current buffer for play and change current buffer */
slouken@3336
   659
                if (stream != device->fake_stream) {
slouken@2716
   660
                    current_audio.impl.PlayDevice(device);
slouken@3096
   661
                    /* Wait for an audio buffer to become available */
slouken@3096
   662
                    current_audio.impl.WaitDevice(device);
slouken@2716
   663
                } else {
slouken@3336
   664
                    SDL_Delay(delay);
slouken@2716
   665
                }
slouken@1895
   666
            }
slouken@0
   667
slouken@1895
   668
        }
icculus@8660
   669
    } else
icculus@8658
   670
#endif
icculus@8660
   671
    {
slouken@2716
   672
        /* Otherwise, do not use the streamer. This is the old code. */
icculus@7406
   673
        const int silence = (int) device->spec.silence;
slouken@0
   674
slouken@2716
   675
        /* Loop, filling the audio buffers */
slouken@2716
   676
        while (device->enabled) {
slouken@2716
   677
slouken@2716
   678
            /* Fill the current buffer with sound */
slouken@2716
   679
            if (device->convert.needed) {
slouken@2716
   680
                if (device->convert.buf) {
slouken@2716
   681
                    stream = device->convert.buf;
slouken@2716
   682
                } else {
slouken@2716
   683
                    continue;
slouken@2716
   684
                }
slouken@2716
   685
            } else {
slouken@2716
   686
                stream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   687
                if (stream == NULL) {
slouken@2716
   688
                    stream = device->fake_stream;
slouken@2716
   689
                }
slouken@1895
   690
            }
slouken@0
   691
icculus@7447
   692
            SDL_LockMutex(device->mixer_lock);
icculus@7406
   693
            if (device->paused) {
icculus@7406
   694
                SDL_memset(stream, silence, stream_len);
icculus@7406
   695
            } else {
icculus@7406
   696
                (*fill) (udata, stream, stream_len);
icculus@7406
   697
            }
icculus@7447
   698
            SDL_UnlockMutex(device->mixer_lock);
slouken@1562
   699
slouken@2716
   700
            /* Convert the audio if necessary */
slouken@2716
   701
            if (device->convert.needed) {
slouken@2716
   702
                SDL_ConvertAudio(&device->convert);
slouken@2716
   703
                stream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   704
                if (stream == NULL) {
slouken@2716
   705
                    stream = device->fake_stream;
slouken@2716
   706
                }
slouken@2716
   707
                SDL_memcpy(stream, device->convert.buf,
slouken@2716
   708
                           device->convert.len_cvt);
slouken@2716
   709
            }
slouken@2716
   710
slouken@2716
   711
            /* Ready current buffer for play and change current buffer */
slouken@3336
   712
            if (stream != device->fake_stream) {
slouken@2716
   713
                current_audio.impl.PlayDevice(device);
slouken@3096
   714
                /* Wait for an audio buffer to become available */
slouken@3096
   715
                current_audio.impl.WaitDevice(device);
slouken@2716
   716
            } else {
slouken@3336
   717
                SDL_Delay(delay);
slouken@2716
   718
            }
slouken@1895
   719
        }
slouken@1895
   720
    }
slouken@1562
   721
slouken@1895
   722
    /* Wait for the audio to drain.. */
icculus@2049
   723
    current_audio.impl.WaitDone(device);
slouken@21
   724
slouken@2716
   725
    /* If necessary, deinit the streamer */
icculus@8658
   726
#if 0  /* !!! FIXME: rewrite/remove this streamer code. */
slouken@2716
   727
    if (device->use_streamer == 1)
slouken@2716
   728
        SDL_StreamDeinit(&device->streamer);
icculus@8658
   729
#endif
slouken@2716
   730
slouken@1895
   731
    return (0);
slouken@0
   732
}
slouken@0
   733
slouken@322
   734
icculus@1982
   735
static SDL_AudioFormat
slouken@1895
   736
SDL_ParseAudioFormat(const char *string)
slouken@1794
   737
{
icculus@2076
   738
#define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
icculus@2049
   739
    CHECK_FMT_STRING(U8);
icculus@2049
   740
    CHECK_FMT_STRING(S8);
icculus@2049
   741
    CHECK_FMT_STRING(U16LSB);
icculus@2049
   742
    CHECK_FMT_STRING(S16LSB);
icculus@2049
   743
    CHECK_FMT_STRING(U16MSB);
icculus@2049
   744
    CHECK_FMT_STRING(S16MSB);
icculus@2049
   745
    CHECK_FMT_STRING(U16SYS);
icculus@2049
   746
    CHECK_FMT_STRING(S16SYS);
icculus@2049
   747
    CHECK_FMT_STRING(U16);
icculus@2049
   748
    CHECK_FMT_STRING(S16);
icculus@2049
   749
    CHECK_FMT_STRING(S32LSB);
icculus@2049
   750
    CHECK_FMT_STRING(S32MSB);
icculus@2049
   751
    CHECK_FMT_STRING(S32SYS);
icculus@2049
   752
    CHECK_FMT_STRING(S32);
icculus@2049
   753
    CHECK_FMT_STRING(F32LSB);
icculus@2049
   754
    CHECK_FMT_STRING(F32MSB);
icculus@2049
   755
    CHECK_FMT_STRING(F32SYS);
icculus@2049
   756
    CHECK_FMT_STRING(F32);
slouken@2060
   757
#undef CHECK_FMT_STRING
icculus@2049
   758
    return 0;
slouken@1895
   759
}
slouken@1895
   760
slouken@1895
   761
int
slouken@1895
   762
SDL_GetNumAudioDrivers(void)
slouken@1895
   763
{
slouken@1895
   764
    return (SDL_arraysize(bootstrap) - 1);
slouken@1895
   765
}
slouken@1895
   766
slouken@1895
   767
const char *
slouken@1895
   768
SDL_GetAudioDriver(int index)
slouken@1895
   769
{
slouken@1895
   770
    if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
slouken@1895
   771
        return (bootstrap[index]->name);
slouken@1895
   772
    }
slouken@1895
   773
    return (NULL);
slouken@1794
   774
}
slouken@1794
   775
slouken@1895
   776
int
slouken@1895
   777
SDL_AudioInit(const char *driver_name)
slouken@0
   778
{
icculus@2049
   779
    int i = 0;
icculus@2049
   780
    int initialized = 0;
icculus@2049
   781
    int tried_to_init = 0;
slouken@0
   782
icculus@2049
   783
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
slouken@2060
   784
        SDL_AudioQuit();        /* shutdown driver if already running. */
slouken@1895
   785
    }
slouken@0
   786
slouken@2060
   787
    SDL_memset(&current_audio, '\0', sizeof(current_audio));
slouken@2060
   788
    SDL_memset(open_devices, '\0', sizeof(open_devices));
icculus@2049
   789
slouken@1895
   790
    /* Select the proper audio driver */
slouken@1909
   791
    if (driver_name == NULL) {
slouken@1909
   792
        driver_name = SDL_getenv("SDL_AUDIODRIVER");
slouken@1909
   793
    }
icculus@2049
   794
icculus@2049
   795
    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
icculus@2049
   796
        /* make sure we should even try this driver before doing so... */
icculus@2049
   797
        const AudioBootStrap *backend = bootstrap[i];
slouken@6900
   798
        if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
slouken@6900
   799
            (!driver_name && backend->demand_only)) {
icculus@2049
   800
            continue;
icculus@2049
   801
        }
slouken@0
   802
icculus@2049
   803
        tried_to_init = 1;
slouken@2060
   804
        SDL_memset(&current_audio, 0, sizeof(current_audio));
icculus@2049
   805
        current_audio.name = backend->name;
icculus@2049
   806
        current_audio.desc = backend->desc;
icculus@3699
   807
        initialized = backend->init(&current_audio.impl);
slouken@1895
   808
    }
icculus@2049
   809
icculus@2049
   810
    if (!initialized) {
icculus@2049
   811
        /* specific drivers will set the error message if they fail... */
icculus@2049
   812
        if (!tried_to_init) {
slouken@1895
   813
            if (driver_name) {
icculus@3699
   814
                SDL_SetError("Audio target '%s' not available", driver_name);
slouken@1895
   815
            } else {
slouken@1895
   816
                SDL_SetError("No available audio device");
slouken@1895
   817
            }
slouken@1895
   818
        }
icculus@2049
   819
slouken@2060
   820
        SDL_memset(&current_audio, 0, sizeof(current_audio));
slouken@2060
   821
        return (-1);            /* No driver was available, so fail. */
slouken@1895
   822
    }
icculus@2049
   823
icculus@2049
   824
    finalize_audio_entry_points();
icculus@2049
   825
slouken@1895
   826
    return (0);
slouken@0
   827
}
slouken@0
   828
slouken@1895
   829
/*
slouken@1895
   830
 * Get the current audio driver name
slouken@1895
   831
 */
slouken@1895
   832
const char *
slouken@1895
   833
SDL_GetCurrentAudioDriver()
slouken@0
   834
{
icculus@2049
   835
    return current_audio.name;
slouken@0
   836
}
slouken@0
   837
icculus@5593
   838
static void
icculus@5593
   839
free_device_list(char ***devices, int *devCount)
icculus@5593
   840
{
icculus@5593
   841
    int i = *devCount;
icculus@5593
   842
    if ((i > 0) && (*devices != NULL)) {
icculus@5593
   843
        while (i--) {
icculus@5593
   844
            SDL_free((*devices)[i]);
icculus@5593
   845
        }
icculus@5593
   846
    }
icculus@5593
   847
slouken@7719
   848
    SDL_free(*devices);
icculus@5593
   849
icculus@5593
   850
    *devices = NULL;
icculus@5593
   851
    *devCount = 0;
icculus@5593
   852
}
icculus@5593
   853
icculus@5593
   854
static
icculus@5593
   855
void SDL_AddCaptureAudioDevice(const char *_name)
icculus@5593
   856
{
icculus@5593
   857
    char *name = NULL;
icculus@5593
   858
    void *ptr = SDL_realloc(current_audio.inputDevices,
icculus@5593
   859
                          (current_audio.inputDeviceCount+1) * sizeof(char*));
icculus@5593
   860
    if (ptr == NULL) {
icculus@5593
   861
        return;  /* oh well. */
icculus@5593
   862
    }
icculus@5593
   863
icculus@5593
   864
    current_audio.inputDevices = (char **) ptr;
icculus@5593
   865
    name = SDL_strdup(_name);  /* if this returns NULL, that's okay. */
icculus@5593
   866
    current_audio.inputDevices[current_audio.inputDeviceCount++] = name;
icculus@5593
   867
}
icculus@5593
   868
icculus@5593
   869
static
icculus@5593
   870
void SDL_AddOutputAudioDevice(const char *_name)
icculus@5593
   871
{
icculus@5593
   872
    char *name = NULL;
icculus@5593
   873
    void *ptr = SDL_realloc(current_audio.outputDevices,
icculus@5593
   874
                          (current_audio.outputDeviceCount+1) * sizeof(char*));
icculus@5593
   875
    if (ptr == NULL) {
icculus@5593
   876
        return;  /* oh well. */
icculus@5593
   877
    }
icculus@5593
   878
icculus@5593
   879
    current_audio.outputDevices = (char **) ptr;
icculus@5593
   880
    name = SDL_strdup(_name);  /* if this returns NULL, that's okay. */
icculus@5593
   881
    current_audio.outputDevices[current_audio.outputDeviceCount++] = name;
icculus@5593
   882
}
icculus@5593
   883
icculus@2049
   884
slouken@1895
   885
int
icculus@2049
   886
SDL_GetNumAudioDevices(int iscapture)
slouken@0
   887
{
icculus@5593
   888
    int retval = 0;
icculus@5593
   889
icculus@2049
   890
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   891
        return -1;
icculus@2049
   892
    }
icculus@5593
   893
icculus@2049
   894
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   895
        return 0;
icculus@2049
   896
    }
icculus@2049
   897
icculus@2049
   898
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
   899
        return 1;
icculus@2049
   900
    }
icculus@2049
   901
icculus@2049
   902
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
   903
        return 1;
icculus@2049
   904
    }
icculus@2049
   905
icculus@5593
   906
    if (iscapture) {
icculus@5593
   907
        free_device_list(&current_audio.inputDevices,
icculus@5593
   908
                         &current_audio.inputDeviceCount);
icculus@5593
   909
        current_audio.impl.DetectDevices(iscapture, SDL_AddCaptureAudioDevice);
icculus@5593
   910
        retval = current_audio.inputDeviceCount;
icculus@5593
   911
    } else {
icculus@5593
   912
        free_device_list(&current_audio.outputDevices,
icculus@5593
   913
                         &current_audio.outputDeviceCount);
icculus@5593
   914
        current_audio.impl.DetectDevices(iscapture, SDL_AddOutputAudioDevice);
icculus@5593
   915
        retval = current_audio.outputDeviceCount;
icculus@5593
   916
    }
icculus@5593
   917
icculus@5593
   918
    return retval;
icculus@2049
   919
}
icculus@2049
   920
slouken@0
   921
icculus@2049
   922
const char *
icculus@2049
   923
SDL_GetAudioDeviceName(int index, int iscapture)
icculus@2049
   924
{
icculus@2049
   925
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   926
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   927
        return NULL;
icculus@2049
   928
    }
icculus@2049
   929
icculus@2049
   930
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   931
        SDL_SetError("No capture support");
icculus@2049
   932
        return NULL;
slouken@1895
   933
    }
icculus@2049
   934
icculus@2049
   935
    if (index < 0) {
icculus@5593
   936
        goto no_such_device;
icculus@2049
   937
    }
slouken@0
   938
icculus@2049
   939
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
slouken@7904
   940
        if (index > 0) {
slouken@7904
   941
            goto no_such_device;
slouken@7904
   942
        }
icculus@2049
   943
        return DEFAULT_INPUT_DEVNAME;
icculus@2049
   944
    }
icculus@2049
   945
icculus@2049
   946
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
slouken@7904
   947
        if (index > 0) {
slouken@7904
   948
            goto no_such_device;
slouken@7904
   949
        }
icculus@2049
   950
        return DEFAULT_OUTPUT_DEVNAME;
slouken@1895
   951
    }
slouken@262
   952
icculus@5593
   953
    if (iscapture) {
icculus@5593
   954
        if (index >= current_audio.inputDeviceCount) {
icculus@5593
   955
            goto no_such_device;
icculus@5593
   956
        }
icculus@5593
   957
        return current_audio.inputDevices[index];
icculus@5593
   958
    } else {
icculus@5593
   959
        if (index >= current_audio.outputDeviceCount) {
icculus@5593
   960
            goto no_such_device;
icculus@5593
   961
        }
icculus@5593
   962
        return current_audio.outputDevices[index];
icculus@5593
   963
    }
icculus@5593
   964
icculus@5593
   965
no_such_device:
icculus@5593
   966
    SDL_SetError("No such device");
icculus@5593
   967
    return NULL;
icculus@2049
   968
}
icculus@2049
   969
icculus@2049
   970
icculus@2049
   971
static void
slouken@2060
   972
close_audio_device(SDL_AudioDevice * device)
icculus@2049
   973
{
icculus@2049
   974
    device->enabled = 0;
icculus@2049
   975
    if (device->thread != NULL) {
icculus@2049
   976
        SDL_WaitThread(device->thread, NULL);
icculus@2049
   977
    }
icculus@2049
   978
    if (device->mixer_lock != NULL) {
icculus@2049
   979
        SDL_DestroyMutex(device->mixer_lock);
icculus@2049
   980
    }
slouken@7719
   981
    SDL_FreeAudioMem(device->fake_stream);
icculus@2049
   982
    if (device->convert.needed) {
icculus@2049
   983
        SDL_FreeAudioMem(device->convert.buf);
icculus@2049
   984
    }
icculus@2049
   985
    if (device->opened) {
icculus@2049
   986
        current_audio.impl.CloseDevice(device);
icculus@2049
   987
        device->opened = 0;
icculus@2049
   988
    }
icculus@9012
   989
icculus@9012
   990
    free_audio_queue(device->buffer_queue_head);
icculus@9012
   991
    free_audio_queue(device->buffer_queue_pool);
icculus@9012
   992
icculus@2049
   993
    SDL_FreeAudioMem(device);
icculus@2049
   994
}
icculus@2049
   995
icculus@2049
   996
icculus@2049
   997
/*
icculus@2049
   998
 * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
icculus@2049
   999
 *  Fills in a sanitized copy in (prepared).
icculus@2049
  1000
 *  Returns non-zero if okay, zero on fatal parameters in (orig).
icculus@2049
  1001
 */
icculus@2049
  1002
static int
slouken@2060
  1003
prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared)
icculus@2049
  1004
{
slouken@2060
  1005
    SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec));
icculus@2049
  1006
icculus@2049
  1007
    if (orig->freq == 0) {
icculus@2049
  1008
        const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
slouken@2060
  1009
        if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
slouken@2060
  1010
            prepared->freq = 22050;     /* a reasonable default */
slouken@1895
  1011
        }
slouken@1895
  1012
    }
icculus@2049
  1013
icculus@2049
  1014
    if (orig->format == 0) {
icculus@2049
  1015
        const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
icculus@2049
  1016
        if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
slouken@2060
  1017
            prepared->format = AUDIO_S16;       /* a reasonable default */
slouken@1895
  1018
        }
slouken@1895
  1019
    }
icculus@2049
  1020
icculus@2049
  1021
    switch (orig->channels) {
slouken@2060
  1022
    case 0:{
slouken@2060
  1023
            const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
slouken@2141
  1024
            if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) {
slouken@2060
  1025
                prepared->channels = 2; /* a reasonable default */
slouken@2060
  1026
            }
slouken@2060
  1027
            break;
icculus@2049
  1028
        }
slouken@1895
  1029
    case 1:                    /* Mono */
slouken@1895
  1030
    case 2:                    /* Stereo */
slouken@1895
  1031
    case 4:                    /* surround */
slouken@1895
  1032
    case 6:                    /* surround with center and lfe */
slouken@1895
  1033
        break;
slouken@1895
  1034
    default:
icculus@2049
  1035
        SDL_SetError("Unsupported number of audio channels.");
icculus@2049
  1036
        return 0;
slouken@1895
  1037
    }
icculus@2049
  1038
icculus@2049
  1039
    if (orig->samples == 0) {
icculus@2049
  1040
        const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
slouken@2060
  1041
        if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) {
icculus@2049
  1042
            /* Pick a default of ~46 ms at desired frequency */
icculus@2049
  1043
            /* !!! FIXME: remove this when the non-Po2 resampling is in. */
icculus@2049
  1044
            const int samples = (prepared->freq / 1000) * 46;
icculus@2049
  1045
            int power2 = 1;
icculus@2049
  1046
            while (power2 < samples) {
icculus@2049
  1047
                power2 *= 2;
icculus@2049
  1048
            }
icculus@2049
  1049
            prepared->samples = power2;
slouken@1895
  1050
        }
slouken@1895
  1051
    }
slouken@0
  1052
slouken@1895
  1053
    /* Calculate the silence and size of the audio specification */
icculus@2049
  1054
    SDL_CalculateAudioSpec(prepared);
slouken@21
  1055
icculus@2049
  1056
    return 1;
icculus@2049
  1057
}
slouken@1408
  1058
icculus@2049
  1059
static SDL_AudioDeviceID
icculus@2049
  1060
open_audio_device(const char *devname, int iscapture,
slouken@2866
  1061
                  const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
  1062
                  int allowed_changes, int min_id)
icculus@2049
  1063
{
icculus@2049
  1064
    SDL_AudioDeviceID id = 0;
slouken@2866
  1065
    SDL_AudioSpec _obtained;
icculus@2049
  1066
    SDL_AudioDevice *device;
slouken@2866
  1067
    SDL_bool build_cvt;
icculus@2049
  1068
    int i = 0;
slouken@21
  1069
icculus@2049
  1070
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
  1071
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
  1072
        return 0;
icculus@2049
  1073
    }
icculus@2049
  1074
icculus@2049
  1075
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
  1076
        SDL_SetError("No capture support");
icculus@2049
  1077
        return 0;
icculus@2049
  1078
    }
icculus@2049
  1079
slouken@2866
  1080
    if (!obtained) {
slouken@2866
  1081
        obtained = &_obtained;
slouken@2866
  1082
    }
slouken@2866
  1083
    if (!prepare_audiospec(desired, obtained)) {
icculus@2049
  1084
        return 0;
icculus@2049
  1085
    }
icculus@2049
  1086
icculus@2049
  1087
    /* If app doesn't care about a specific device, let the user override. */
icculus@2049
  1088
    if (devname == NULL) {
icculus@2049
  1089
        devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
slouken@1895
  1090
    }
slouken@21
  1091
icculus@2049
  1092
    /*
icculus@2049
  1093
     * Catch device names at the high level for the simple case...
icculus@2049
  1094
     * This lets us have a basic "device enumeration" for systems that
icculus@2049
  1095
     *  don't have multiple devices, but makes sure the device name is
icculus@2049
  1096
     *  always NULL when it hits the low level.
icculus@2049
  1097
     *
icculus@2049
  1098
     * Also make sure that the simple case prevents multiple simultaneous
icculus@2049
  1099
     *  opens of the default system device.
icculus@2049
  1100
     */
icculus@2049
  1101
icculus@2049
  1102
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
  1103
        if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
icculus@2049
  1104
            SDL_SetError("No such device");
icculus@2049
  1105
            return 0;
icculus@2049
  1106
        }
icculus@2049
  1107
        devname = NULL;
icculus@2049
  1108
icculus@2049
  1109
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1110
            if ((open_devices[i]) && (open_devices[i]->iscapture)) {
icculus@2049
  1111
                SDL_SetError("Audio device already open");
icculus@2049
  1112
                return 0;
icculus@2049
  1113
            }
icculus@2049
  1114
        }
icculus@2049
  1115
    }
icculus@2049
  1116
icculus@2049
  1117
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
  1118
        if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
icculus@2049
  1119
            SDL_SetError("No such device");
icculus@2049
  1120
            return 0;
icculus@2049
  1121
        }
icculus@2049
  1122
        devname = NULL;
icculus@2049
  1123
icculus@2049
  1124
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1125
            if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
icculus@2049
  1126
                SDL_SetError("Audio device already open");
icculus@2049
  1127
                return 0;
icculus@2049
  1128
            }
icculus@2049
  1129
        }
icculus@2049
  1130
    }
icculus@2049
  1131
slouken@2060
  1132
    device = (SDL_AudioDevice *) SDL_AllocAudioMem(sizeof(SDL_AudioDevice));
icculus@2049
  1133
    if (device == NULL) {
icculus@2049
  1134
        SDL_OutOfMemory();
icculus@2049
  1135
        return 0;
icculus@2049
  1136
    }
icculus@9012
  1137
    SDL_zerop(device);
slouken@2866
  1138
    device->spec = *obtained;
icculus@2049
  1139
    device->enabled = 1;
icculus@2049
  1140
    device->paused = 1;
icculus@2049
  1141
    device->iscapture = iscapture;
icculus@2049
  1142
icculus@2049
  1143
    /* Create a semaphore for locking the sound buffers */
icculus@2049
  1144
    if (!current_audio.impl.SkipMixerLock) {
icculus@2049
  1145
        device->mixer_lock = SDL_CreateMutex();
icculus@2049
  1146
        if (device->mixer_lock == NULL) {
icculus@2049
  1147
            close_audio_device(device);
icculus@2049
  1148
            SDL_SetError("Couldn't create mixer lock");
icculus@2049
  1149
            return 0;
icculus@2049
  1150
        }
icculus@2049
  1151
    }
icculus@2049
  1152
icculus@5593
  1153
    /* force a device detection if we haven't done one yet. */
icculus@5593
  1154
    if ( ((iscapture) && (current_audio.inputDevices == NULL)) ||
icculus@9012
  1155
         ((!iscapture) && (current_audio.outputDevices == NULL)) ) {
icculus@5593
  1156
        SDL_GetNumAudioDevices(iscapture);
icculus@9012
  1157
    }
icculus@5593
  1158
icculus@7038
  1159
    if (current_audio.impl.OpenDevice(device, devname, iscapture) < 0) {
icculus@2049
  1160
        close_audio_device(device);
icculus@2049
  1161
        return 0;
icculus@2049
  1162
    }
icculus@2049
  1163
    device->opened = 1;
slouken@0
  1164
slouken@1895
  1165
    /* Allocate a fake audio memory buffer */
aschiffler@4865
  1166
    device->fake_stream = (Uint8 *)SDL_AllocAudioMem(device->spec.size);
icculus@2049
  1167
    if (device->fake_stream == NULL) {
icculus@2049
  1168
        close_audio_device(device);
slouken@1895
  1169
        SDL_OutOfMemory();
icculus@2049
  1170
        return 0;
slouken@1895
  1171
    }
slouken@0
  1172
slouken@1895
  1173
    /* See if we need to do any conversion */
slouken@2866
  1174
    build_cvt = SDL_FALSE;
slouken@2866
  1175
    if (obtained->freq != device->spec.freq) {
slouken@2866
  1176
        if (allowed_changes & SDL_AUDIO_ALLOW_FREQUENCY_CHANGE) {
slouken@2866
  1177
            obtained->freq = device->spec.freq;
slouken@2866
  1178
        } else {
slouken@2866
  1179
            build_cvt = SDL_TRUE;
slouken@2866
  1180
        }
slouken@2866
  1181
    }
slouken@2866
  1182
    if (obtained->format != device->spec.format) {
slouken@2866
  1183
        if (allowed_changes & SDL_AUDIO_ALLOW_FORMAT_CHANGE) {
slouken@2866
  1184
            obtained->format = device->spec.format;
slouken@2866
  1185
        } else {
slouken@2866
  1186
            build_cvt = SDL_TRUE;
slouken@2866
  1187
        }
slouken@2866
  1188
    }
slouken@2866
  1189
    if (obtained->channels != device->spec.channels) {
slouken@2866
  1190
        if (allowed_changes & SDL_AUDIO_ALLOW_CHANNELS_CHANGE) {
slouken@2866
  1191
            obtained->channels = device->spec.channels;
slouken@2866
  1192
        } else {
slouken@2866
  1193
            build_cvt = SDL_TRUE;
slouken@2866
  1194
        }
slouken@2866
  1195
    }
slouken@7518
  1196
slouken@7518
  1197
    /* If the audio driver changes the buffer size, accept it.
slouken@7518
  1198
       This needs to be done after the format is modified above,
slouken@7518
  1199
       otherwise it might not have the correct buffer size.
slouken@7518
  1200
     */
slouken@7518
  1201
    if (device->spec.samples != obtained->samples) {
slouken@7518
  1202
        obtained->samples = device->spec.samples;
slouken@7518
  1203
        SDL_CalculateAudioSpec(obtained);
slouken@7518
  1204
    }
slouken@7518
  1205
slouken@2866
  1206
    if (build_cvt) {
slouken@1895
  1207
        /* Build an audio conversion block */
icculus@2049
  1208
        if (SDL_BuildAudioCVT(&device->convert,
slouken@2866
  1209
                              obtained->format, obtained->channels,
slouken@2866
  1210
                              obtained->freq,
icculus@2049
  1211
                              device->spec.format, device->spec.channels,
icculus@2049
  1212
                              device->spec.freq) < 0) {
icculus@2049
  1213
            close_audio_device(device);
icculus@2049
  1214
            return 0;
slouken@1895
  1215
        }
icculus@2049
  1216
        if (device->convert.needed) {
slouken@7518
  1217
            device->convert.len = (int) (((double) device->spec.size) /
slouken@2060
  1218
                                         device->convert.len_ratio);
icculus@2053
  1219
icculus@2049
  1220
            device->convert.buf =
icculus@2049
  1221
                (Uint8 *) SDL_AllocAudioMem(device->convert.len *
icculus@2049
  1222
                                            device->convert.len_mult);
icculus@2049
  1223
            if (device->convert.buf == NULL) {
icculus@2049
  1224
                close_audio_device(device);
slouken@1895
  1225
                SDL_OutOfMemory();
icculus@2049
  1226
                return 0;
slouken@1895
  1227
            }
slouken@1895
  1228
        }
slouken@1895
  1229
    }
icculus@2049
  1230
icculus@9012
  1231
    if (device->spec.callback == NULL) {  /* use buffer queueing? */
icculus@9012
  1232
        /* pool a few packets to start. Enough for two callbacks. */
icculus@9012
  1233
        const int packetlen = SDL_AUDIOBUFFERQUEUE_PACKETLEN;
icculus@9012
  1234
        const int wantbytes = ((device->convert.needed) ? device->convert.len : device->spec.size) * 2;
icculus@9012
  1235
        const int wantpackets = (wantbytes / packetlen) + ((wantbytes % packetlen) ? packetlen : 0);
icculus@9012
  1236
        for (i = 0; i < wantpackets; i++) {
icculus@9012
  1237
            SDL_AudioBufferQueue *packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
icculus@9012
  1238
            if (packet) { /* don't care if this fails, we'll deal later. */
icculus@9012
  1239
                packet->datalen = 0;
icculus@9012
  1240
                packet->startpos = 0;
icculus@9012
  1241
                packet->next = device->buffer_queue_pool;
icculus@9012
  1242
                device->buffer_queue_pool = packet;
icculus@9012
  1243
            }
icculus@9012
  1244
        }
icculus@9012
  1245
icculus@9012
  1246
        device->spec.callback = SDL_BufferQueueDrainCallback;
icculus@9012
  1247
        device->spec.userdata = device;
icculus@9012
  1248
    }
icculus@9012
  1249
icculus@2049
  1250
    /* Find an available device ID and store the structure... */
slouken@2060
  1251
    for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
icculus@2049
  1252
        if (open_devices[id] == NULL) {
icculus@2049
  1253
            open_devices[id] = device;
icculus@2049
  1254
            break;
icculus@2049
  1255
        }
icculus@2049
  1256
    }
icculus@2049
  1257
icculus@2049
  1258
    if (id == SDL_arraysize(open_devices)) {
icculus@2049
  1259
        SDL_SetError("Too many open audio devices");
icculus@2049
  1260
        close_audio_device(device);
icculus@2049
  1261
        return 0;
icculus@2049
  1262
    }
icculus@2049
  1263
slouken@1895
  1264
    /* Start the audio thread if necessary */
icculus@2049
  1265
    if (!current_audio.impl.ProvidesOwnCallbackThread) {
slouken@1895
  1266
        /* Start the audio thread */
icculus@5969
  1267
        char name[64];
icculus@5969
  1268
        SDL_snprintf(name, sizeof (name), "SDLAudioDev%d", (int) (id + 1));
icculus@2049
  1269
/* !!! FIXME: this is nasty. */
icculus@6430
  1270
#if defined(__WIN32__) && !defined(HAVE_LIBC)
slouken@1330
  1271
#undef SDL_CreateThread
icculus@8094
  1272
#if SDL_DYNAMIC_API
icculus@8094
  1273
        device->thread = SDL_CreateThread_REAL(SDL_RunAudio, name, device, NULL, NULL);
icculus@8094
  1274
#else
icculus@5969
  1275
        device->thread = SDL_CreateThread(SDL_RunAudio, name, device, NULL, NULL);
icculus@8094
  1276
#endif
slouken@1330
  1277
#else
icculus@5969
  1278
        device->thread = SDL_CreateThread(SDL_RunAudio, name, device);
slouken@1330
  1279
#endif
icculus@2049
  1280
        if (device->thread == NULL) {
slouken@2060
  1281
            SDL_CloseAudioDevice(id + 1);
slouken@1895
  1282
            SDL_SetError("Couldn't create audio thread");
icculus@2049
  1283
            return 0;
icculus@2049
  1284
        }
icculus@2049
  1285
    }
icculus@2049
  1286
slouken@2060
  1287
    return id + 1;
icculus@2049
  1288
}
icculus@2049
  1289
icculus@2049
  1290
icculus@2049
  1291
int
slouken@2866
  1292
SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
icculus@2049
  1293
{
icculus@2049
  1294
    SDL_AudioDeviceID id = 0;
icculus@2049
  1295
icculus@2049
  1296
    /* Start up the audio driver, if necessary. This is legacy behaviour! */
icculus@2049
  1297
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
  1298
        if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
slouken@1895
  1299
            return (-1);
slouken@1895
  1300
        }
icculus@2049
  1301
    }
slouken@0
  1302
icculus@2049
  1303
    /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
icculus@2049
  1304
    if (open_devices[0] != NULL) {
icculus@2049
  1305
        SDL_SetError("Audio device is already opened");
icculus@2049
  1306
        return (-1);
slouken@1895
  1307
    }
icculus@2049
  1308
slouken@2866
  1309
    if (obtained) {
slouken@2866
  1310
        id = open_audio_device(NULL, 0, desired, obtained,
slouken@2866
  1311
                               SDL_AUDIO_ALLOW_ANY_CHANGE, 1);
slouken@2866
  1312
    } else {
slouken@8919
  1313
        id = open_audio_device(NULL, 0, desired, NULL, 0, 1);
slouken@2866
  1314
    }
slouken@21
  1315
icculus@5964
  1316
    SDL_assert((id == 0) || (id == 1));
icculus@2049
  1317
    return ((id == 0) ? -1 : 0);
icculus@2049
  1318
}
slouken@21
  1319
icculus@2049
  1320
SDL_AudioDeviceID
icculus@2049
  1321
SDL_OpenAudioDevice(const char *device, int iscapture,
slouken@2866
  1322
                    const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
  1323
                    int allowed_changes)
icculus@2049
  1324
{
slouken@2866
  1325
    return open_audio_device(device, iscapture, desired, obtained,
slouken@2866
  1326
                             allowed_changes, 2);
slouken@1895
  1327
}
slouken@1895
  1328
slouken@3537
  1329
SDL_AudioStatus
icculus@2049
  1330
SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
slouken@1895
  1331
{
icculus@2049
  1332
    SDL_AudioDevice *device = get_audio_device(devid);
slouken@3537
  1333
    SDL_AudioStatus status = SDL_AUDIO_STOPPED;
icculus@2049
  1334
    if (device && device->enabled) {
icculus@2049
  1335
        if (device->paused) {
slouken@1895
  1336
            status = SDL_AUDIO_PAUSED;
slouken@1895
  1337
        } else {
slouken@1895
  1338
            status = SDL_AUDIO_PLAYING;
slouken@1895
  1339
        }
slouken@1895
  1340
    }
slouken@1895
  1341
    return (status);
slouken@0
  1342
}
slouken@0
  1343
icculus@2049
  1344
slouken@3537
  1345
SDL_AudioStatus
icculus@2049
  1346
SDL_GetAudioStatus(void)
icculus@2049
  1347
{
icculus@2049
  1348
    return SDL_GetAudioDeviceStatus(1);
icculus@2049
  1349
}
icculus@2049
  1350
icculus@2049
  1351
void
icculus@2049
  1352
SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
icculus@2049
  1353
{
icculus@2049
  1354
    SDL_AudioDevice *device = get_audio_device(devid);
gabomdq@9148
  1355
    if (device) {
gabomdq@9148
  1356
        current_audio.impl.LockDevice(device);
icculus@2049
  1357
        device->paused = pause_on;
gabomdq@9148
  1358
        current_audio.impl.UnlockDevice(device);
icculus@2049
  1359
    }
icculus@2049
  1360
}
icculus@2049
  1361
slouken@1895
  1362
void
slouken@1895
  1363
SDL_PauseAudio(int pause_on)
slouken@0
  1364
{
gabomdq@9148
  1365
    SDL_PauseAudioDevice(1, pause_on);
icculus@2049
  1366
}
icculus@2049
  1367
slouken@0
  1368
icculus@2049
  1369
void
icculus@2049
  1370
SDL_LockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1371
{
icculus@2049
  1372
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1373
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1374
    if (device) {
icculus@2049
  1375
        current_audio.impl.LockDevice(device);
slouken@1895
  1376
    }
slouken@0
  1377
}
slouken@0
  1378
slouken@1895
  1379
void
slouken@1895
  1380
SDL_LockAudio(void)
slouken@0
  1381
{
icculus@2049
  1382
    SDL_LockAudioDevice(1);
icculus@2049
  1383
}
slouken@0
  1384
icculus@2049
  1385
void
icculus@2049
  1386
SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1387
{
slouken@1895
  1388
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1389
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1390
    if (device) {
icculus@2049
  1391
        current_audio.impl.UnlockDevice(device);
slouken@1895
  1392
    }
slouken@0
  1393
}
slouken@0
  1394
slouken@1895
  1395
void
slouken@1895
  1396
SDL_UnlockAudio(void)
slouken@0
  1397
{
icculus@2049
  1398
    SDL_UnlockAudioDevice(1);
icculus@2049
  1399
}
slouken@0
  1400
icculus@2049
  1401
void
icculus@2049
  1402
SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1403
{
icculus@2049
  1404
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1405
    if (device) {
icculus@2049
  1406
        close_audio_device(device);
slouken@2060
  1407
        open_devices[devid - 1] = NULL;
slouken@1895
  1408
    }
slouken@0
  1409
}
slouken@0
  1410
slouken@1895
  1411
void
slouken@1895
  1412
SDL_CloseAudio(void)
slouken@0
  1413
{
icculus@2049
  1414
    SDL_CloseAudioDevice(1);
slouken@0
  1415
}
slouken@0
  1416
slouken@1895
  1417
void
slouken@1895
  1418
SDL_AudioQuit(void)
slouken@0
  1419
{
icculus@7348
  1420
    SDL_AudioDeviceID i;
icculus@7348
  1421
icculus@7345
  1422
    if (!current_audio.name) {  /* not initialized?! */
icculus@7345
  1423
        return;
icculus@7345
  1424
    }
icculus@7345
  1425
icculus@2049
  1426
    for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@7342
  1427
        if (open_devices[i] != NULL) {
icculus@7446
  1428
            SDL_CloseAudioDevice(i+1);
icculus@7342
  1429
        }
icculus@2049
  1430
    }
slouken@0
  1431
icculus@2049
  1432
    /* Free the driver data */
icculus@2049
  1433
    current_audio.impl.Deinitialize();
icculus@5593
  1434
    free_device_list(&current_audio.outputDevices,
icculus@5593
  1435
                     &current_audio.outputDeviceCount);
icculus@5593
  1436
    free_device_list(&current_audio.inputDevices,
icculus@5593
  1437
                     &current_audio.inputDeviceCount);
slouken@2060
  1438
    SDL_memset(&current_audio, '\0', sizeof(current_audio));
slouken@2060
  1439
    SDL_memset(open_devices, '\0', sizeof(open_devices));
slouken@0
  1440
}
slouken@0
  1441
icculus@1982
  1442
#define NUM_FORMATS 10
slouken@0
  1443
static int format_idx;
slouken@0
  1444
static int format_idx_sub;
icculus@1982
  1445
static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
slouken@1895
  1446
    {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1447
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
slouken@1895
  1448
    {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1449
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
icculus@1982
  1450
    {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB,
icculus@1982
  1451
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1452
    {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB,
icculus@1982
  1453
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1454
    {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB,
icculus@1982
  1455
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1456
    {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB,
icculus@1982
  1457
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1458
    {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB,
icculus@1993
  1459
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1460
    {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB,
icculus@1993
  1461
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1462
    {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB,
icculus@1993
  1463
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1464
    {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB,
icculus@1993
  1465
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
slouken@0
  1466
};
slouken@0
  1467
icculus@1982
  1468
SDL_AudioFormat
icculus@1982
  1469
SDL_FirstAudioFormat(SDL_AudioFormat format)
slouken@0
  1470
{
slouken@1895
  1471
    for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
slouken@1895
  1472
        if (format_list[format_idx][0] == format) {
slouken@1895
  1473
            break;
slouken@1895
  1474
        }
slouken@1895
  1475
    }
slouken@1895
  1476
    format_idx_sub = 0;
slouken@1895
  1477
    return (SDL_NextAudioFormat());
slouken@0
  1478
}
slouken@0
  1479
icculus@1982
  1480
SDL_AudioFormat
slouken@1895
  1481
SDL_NextAudioFormat(void)
slouken@0
  1482
{
slouken@1895
  1483
    if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
slouken@1895
  1484
        return (0);
slouken@1895
  1485
    }
slouken@1895
  1486
    return (format_list[format_idx][format_idx_sub++]);
slouken@0
  1487
}
slouken@0
  1488
slouken@1895
  1489
void
slouken@1895
  1490
SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
slouken@0
  1491
{
slouken@1895
  1492
    switch (spec->format) {
slouken@1895
  1493
    case AUDIO_U8:
slouken@1895
  1494
        spec->silence = 0x80;
slouken@1895
  1495
        break;
slouken@1895
  1496
    default:
slouken@1895
  1497
        spec->silence = 0x00;
slouken@1895
  1498
        break;
slouken@1895
  1499
    }
icculus@2049
  1500
    spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
slouken@1895
  1501
    spec->size *= spec->channels;
slouken@1895
  1502
    spec->size *= spec->samples;
slouken@0
  1503
}
slouken@1895
  1504
icculus@2049
  1505
icculus@2049
  1506
/*
icculus@2049
  1507
 * Moved here from SDL_mixer.c, since it relies on internals of an opened
icculus@2049
  1508
 *  audio device (and is deprecated, by the way!).
icculus@2049
  1509
 */
icculus@2049
  1510
void
icculus@2049
  1511
SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
icculus@2049
  1512
{
icculus@2049
  1513
    /* Mix the user-level audio format */
icculus@2049
  1514
    SDL_AudioDevice *device = get_audio_device(1);
icculus@2049
  1515
    if (device != NULL) {
icculus@2049
  1516
        SDL_AudioFormat format;
icculus@2049
  1517
        if (device->convert.needed) {
icculus@2049
  1518
            format = device->convert.src_format;
icculus@2049
  1519
        } else {
icculus@2049
  1520
            format = device->spec.format;
icculus@2049
  1521
        }
icculus@2049
  1522
        SDL_MixAudioFormat(dst, src, format, len, volume);
icculus@2049
  1523
    }
icculus@2049
  1524
}
icculus@2049
  1525
slouken@1895
  1526
/* vi: set ts=4 sw=4 expandtab: */