src/audio/SDL_audio.c
author Ryan C. Gordon
Thu, 11 Jul 2013 23:53:00 -0400
changeset 7406 fb02213c11d3
parent 7367 d6eabafa8776
child 7407 db8a19d767d7
permissions -rw-r--r--
Explicitly write silence to the audio device while it is paused.

This is what SDL 1.2 did; we'll do this properly (add a method for the target
driver to pause) when I rewrite all this code after the official 2.0 release.

Fixes Bugzilla #1857.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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
*/
slouken@1402
    21
#include "SDL_config.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;
icculus@2049
    54
extern AudioBootStrap NAS_bootstrap;
icculus@5592
    55
extern AudioBootStrap XAUDIO2_bootstrap;
icculus@2049
    56
extern AudioBootStrap DSOUND_bootstrap;
icculus@5588
    57
extern AudioBootStrap WINMM_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 DISKAUD_bootstrap;
icculus@2049
    63
extern AudioBootStrap DUMMYAUD_bootstrap;
icculus@2049
    64
extern AudioBootStrap DCAUD_bootstrap;
icculus@2049
    65
extern AudioBootStrap DART_bootstrap;
slouken@2735
    66
extern AudioBootStrap NDSAUD_bootstrap;
slouken@2947
    67
extern AudioBootStrap FUSIONSOUND_bootstrap;
paul@4718
    68
extern AudioBootStrap ANDROIDAUD_bootstrap;
kimonline@7009
    69
extern AudioBootStrap PSPAUD_bootstrap;
icculus@7367
    70
extern AudioBootStrap SNDIO_bootstrap;
icculus@1190
    71
slouken@0
    72
/* Available audio drivers */
slouken@3162
    73
static const AudioBootStrap *const bootstrap[] = {
icculus@2939
    74
#if SDL_AUDIO_DRIVER_PULSEAUDIO
icculus@2939
    75
    &PULSEAUDIO_bootstrap,
slouken@0
    76
#endif
slouken@1361
    77
#if SDL_AUDIO_DRIVER_ALSA
slouken@1895
    78
    &ALSA_bootstrap,
slouken@0
    79
#endif
icculus@7367
    80
#if SDL_AUDIO_DRIVER_SNDIO
icculus@7367
    81
    &SNDIO_bootstrap,
icculus@7367
    82
#endif
slouken@4548
    83
#if SDL_AUDIO_DRIVER_BSD
slouken@4548
    84
    &BSD_AUDIO_bootstrap,
slouken@4548
    85
#endif
icculus@2939
    86
#if SDL_AUDIO_DRIVER_OSS
icculus@2939
    87
    &DSP_bootstrap,
icculus@2271
    88
#endif
slouken@3099
    89
#if SDL_AUDIO_DRIVER_QSA
slouken@3099
    90
    &QSAAUDIO_bootstrap,
slouken@663
    91
#endif
slouken@1361
    92
#if SDL_AUDIO_DRIVER_SUNAUDIO
slouken@1895
    93
    &SUNAUDIO_bootstrap,
slouken@148
    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
icculus@5592
   104
#if SDL_AUDIO_DRIVER_XAUDIO2
icculus@5592
   105
    &XAUDIO2_bootstrap,
icculus@5592
   106
#endif
slouken@1361
   107
#if SDL_AUDIO_DRIVER_DSOUND
slouken@1895
   108
    &DSOUND_bootstrap,
slouken@0
   109
#endif
icculus@5588
   110
#if SDL_AUDIO_DRIVER_WINMM
icculus@5588
   111
    &WINMM_bootstrap,
slouken@0
   112
#endif
icculus@2049
   113
#if SDL_AUDIO_DRIVER_PAUDIO
icculus@2049
   114
    &PAUDIO_bootstrap,
slouken@1361
   115
#endif
icculus@2049
   116
#if SDL_AUDIO_DRIVER_BEOSAUDIO
icculus@2049
   117
    &BEOSAUDIO_bootstrap,
slouken@0
   118
#endif
slouken@1361
   119
#if SDL_AUDIO_DRIVER_COREAUDIO
slouken@1895
   120
    &COREAUDIO_bootstrap,
slouken@935
   121
#endif
slouken@1361
   122
#if SDL_AUDIO_DRIVER_DISK
slouken@1895
   123
    &DISKAUD_bootstrap,
slouken@68
   124
#endif
icculus@1532
   125
#if SDL_AUDIO_DRIVER_DUMMY
slouken@1895
   126
    &DUMMYAUD_bootstrap,
icculus@1532
   127
#endif
slouken@2947
   128
#if SDL_AUDIO_DRIVER_FUSIONSOUND
slouken@2947
   129
    &FUSIONSOUND_bootstrap,
slouken@2947
   130
#endif
paul@4718
   131
#if SDL_AUDIO_DRIVER_ANDROID
paul@4718
   132
    &ANDROIDAUD_bootstrap,
paul@4718
   133
#endif
kimonline@7009
   134
#if SDL_AUDIO_DRIVER_PSP
kimonline@7009
   135
    &PSPAUD_bootstrap,
kimonline@7009
   136
#endif
slouken@1895
   137
    NULL
slouken@0
   138
};
icculus@2049
   139
slouken@2060
   140
static SDL_AudioDevice *
slouken@2060
   141
get_audio_device(SDL_AudioDeviceID id)
icculus@2049
   142
{
icculus@2049
   143
    id--;
slouken@2060
   144
    if ((id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL)) {
icculus@2049
   145
        SDL_SetError("Invalid audio device ID");
icculus@2049
   146
        return NULL;
icculus@2049
   147
    }
icculus@2049
   148
icculus@2049
   149
    return open_devices[id];
icculus@2049
   150
}
icculus@2049
   151
icculus@2049
   152
icculus@2049
   153
/* stubs for audio drivers that don't need a specific entry point... */
icculus@5593
   154
static void
icculus@5593
   155
SDL_AudioDetectDevices_Default(int iscapture, SDL_AddAudioDevice addfn)
icculus@5593
   156
{                               /* no-op. */
slouken@2060
   157
}
slouken@2735
   158
slouken@2060
   159
static void
slouken@2060
   160
SDL_AudioThreadInit_Default(_THIS)
slouken@2060
   161
{                               /* no-op. */
slouken@2060
   162
}
slouken@2735
   163
slouken@2060
   164
static void
slouken@2060
   165
SDL_AudioWaitDevice_Default(_THIS)
slouken@2060
   166
{                               /* no-op. */
slouken@2060
   167
}
slouken@2735
   168
slouken@2060
   169
static void
slouken@2060
   170
SDL_AudioPlayDevice_Default(_THIS)
slouken@2060
   171
{                               /* no-op. */
slouken@2060
   172
}
slouken@2735
   173
slouken@2060
   174
static Uint8 *
slouken@2060
   175
SDL_AudioGetDeviceBuf_Default(_THIS)
slouken@2060
   176
{
slouken@2060
   177
    return NULL;
slouken@2060
   178
}
slouken@2735
   179
slouken@2060
   180
static void
slouken@2060
   181
SDL_AudioWaitDone_Default(_THIS)
slouken@2060
   182
{                               /* no-op. */
slouken@2060
   183
}
slouken@2735
   184
slouken@2060
   185
static void
slouken@2060
   186
SDL_AudioCloseDevice_Default(_THIS)
slouken@2060
   187
{                               /* no-op. */
slouken@2060
   188
}
slouken@2735
   189
slouken@2060
   190
static void
slouken@2060
   191
SDL_AudioDeinitialize_Default(void)
slouken@2060
   192
{                               /* no-op. */
slouken@2060
   193
}
icculus@2049
   194
icculus@2049
   195
static int
icculus@2049
   196
SDL_AudioOpenDevice_Default(_THIS, const char *devname, int iscapture)
icculus@2049
   197
{
icculus@7038
   198
    return -1;
icculus@2049
   199
}
icculus@2049
   200
icculus@2049
   201
static void
icculus@2049
   202
SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   203
{
icculus@2049
   204
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
icculus@2049
   205
        return;
icculus@2049
   206
    }
slouken@6977
   207
    SDL_LockMutex(device->mixer_lock);
icculus@2049
   208
}
icculus@2049
   209
icculus@2049
   210
static void
icculus@2049
   211
SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   212
{
icculus@2049
   213
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
icculus@2049
   214
        return;
icculus@2049
   215
    }
slouken@6977
   216
    SDL_UnlockMutex(device->mixer_lock);
icculus@2049
   217
}
icculus@2049
   218
slouken@0
   219
slouken@2060
   220
static void
slouken@2060
   221
finalize_audio_entry_points(void)
icculus@2049
   222
{
icculus@2049
   223
    /*
icculus@2049
   224
     * Fill in stub functions for unused driver entry points. This lets us
icculus@2049
   225
     *  blindly call them without having to check for validity first.
icculus@2049
   226
     */
icculus@2049
   227
slouken@2060
   228
#define FILL_STUB(x) \
icculus@2049
   229
        if (current_audio.impl.x == NULL) { \
icculus@2049
   230
            current_audio.impl.x = SDL_Audio##x##_Default; \
icculus@2049
   231
        }
icculus@2049
   232
    FILL_STUB(DetectDevices);
icculus@2049
   233
    FILL_STUB(OpenDevice);
icculus@2049
   234
    FILL_STUB(ThreadInit);
icculus@2049
   235
    FILL_STUB(WaitDevice);
icculus@2049
   236
    FILL_STUB(PlayDevice);
icculus@2049
   237
    FILL_STUB(GetDeviceBuf);
icculus@2049
   238
    FILL_STUB(WaitDone);
icculus@2049
   239
    FILL_STUB(CloseDevice);
icculus@2049
   240
    FILL_STUB(LockDevice);
icculus@2049
   241
    FILL_STUB(UnlockDevice);
icculus@2049
   242
    FILL_STUB(Deinitialize);
slouken@2060
   243
#undef FILL_STUB
icculus@2049
   244
}
icculus@2049
   245
slouken@2716
   246
/* Streaming functions (for when the input and output buffer sizes are different) */
slouken@2716
   247
/* Write [length] bytes from buf into the streamer */
slouken@4472
   248
static void
slouken@2716
   249
SDL_StreamWrite(SDL_AudioStreamer * stream, Uint8 * buf, int length)
slouken@2716
   250
{
slouken@2716
   251
    int i;
slouken@2716
   252
slouken@2716
   253
    for (i = 0; i < length; ++i) {
slouken@2716
   254
        stream->buffer[stream->write_pos] = buf[i];
slouken@2716
   255
        ++stream->write_pos;
slouken@2716
   256
    }
slouken@2716
   257
}
slouken@2716
   258
slouken@2716
   259
/* Read [length] bytes out of the streamer into buf */
slouken@4472
   260
static void
slouken@2716
   261
SDL_StreamRead(SDL_AudioStreamer * stream, Uint8 * buf, int length)
slouken@2716
   262
{
slouken@2716
   263
    int i;
slouken@2716
   264
slouken@2716
   265
    for (i = 0; i < length; ++i) {
slouken@2716
   266
        buf[i] = stream->buffer[stream->read_pos];
slouken@2716
   267
        ++stream->read_pos;
slouken@2716
   268
    }
slouken@2716
   269
}
slouken@2716
   270
slouken@4472
   271
static int
slouken@2716
   272
SDL_StreamLength(SDL_AudioStreamer * stream)
slouken@2716
   273
{
slouken@2716
   274
    return (stream->write_pos - stream->read_pos) % stream->max_len;
slouken@2716
   275
}
slouken@2716
   276
slouken@2716
   277
/* Initialize the stream by allocating the buffer and setting the read/write heads to the beginning */
slouken@4507
   278
#if 0
slouken@4472
   279
static int
slouken@2716
   280
SDL_StreamInit(SDL_AudioStreamer * stream, int max_len, Uint8 silence)
slouken@2716
   281
{
slouken@2716
   282
    /* First try to allocate the buffer */
slouken@2728
   283
    stream->buffer = (Uint8 *) SDL_malloc(max_len);
slouken@2716
   284
    if (stream->buffer == NULL) {
slouken@2716
   285
        return -1;
slouken@2716
   286
    }
slouken@2716
   287
slouken@2716
   288
    stream->max_len = max_len;
slouken@2716
   289
    stream->read_pos = 0;
slouken@2716
   290
    stream->write_pos = 0;
slouken@2716
   291
slouken@2716
   292
    /* Zero out the buffer */
slouken@2728
   293
    SDL_memset(stream->buffer, silence, max_len);
slouken@2728
   294
slouken@2728
   295
    return 0;
slouken@2716
   296
}
slouken@4507
   297
#endif
slouken@2716
   298
slouken@2716
   299
/* Deinitialize the stream simply by freeing the buffer */
slouken@4472
   300
static void
slouken@2716
   301
SDL_StreamDeinit(SDL_AudioStreamer * stream)
slouken@2716
   302
{
slouken@2716
   303
    if (stream->buffer != NULL) {
slouken@2728
   304
        SDL_free(stream->buffer);
slouken@2716
   305
    }
slouken@2716
   306
}
slouken@2716
   307
aschiffler@4865
   308
#if defined(ANDROID)
paul@4722
   309
#include <android/log.h>
aschiffler@4865
   310
#endif
paul@4722
   311
slouken@0
   312
/* The general mixing thread function */
slouken@1895
   313
int SDLCALL
icculus@2049
   314
SDL_RunAudio(void *devicep)
slouken@0
   315
{
icculus@2049
   316
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
slouken@1895
   317
    Uint8 *stream;
slouken@1895
   318
    int stream_len;
slouken@1895
   319
    void *udata;
slouken@1895
   320
    void (SDLCALL * fill) (void *userdata, Uint8 * stream, int len);
slouken@3336
   321
    Uint32 delay;
airlangga@5526
   322
    /* For streaming when the buffer sizes don't match up */
airlangga@5526
   323
    Uint8 *istream;
airlangga@5526
   324
    int istream_len = 0;
slouken@2716
   325
slouken@5509
   326
    /* The audio mixing is always a high priority thread */
slouken@5509
   327
    SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
slouken@5509
   328
slouken@1895
   329
    /* Perform any thread setup */
icculus@2049
   330
    device->threadid = SDL_ThreadID();
icculus@2049
   331
    current_audio.impl.ThreadInit(device);
slouken@0
   332
slouken@1895
   333
    /* Set up the mixing function */
icculus@2049
   334
    fill = device->spec.callback;
icculus@2049
   335
    udata = device->spec.userdata;
slouken@21
   336
slouken@2716
   337
    /* By default do not stream */
slouken@2716
   338
    device->use_streamer = 0;
slouken@2716
   339
icculus@2049
   340
    if (device->convert.needed) {
slouken@3040
   341
#if 0                           /* !!! FIXME: I took len_div out of the structure. Use rate_incr instead? */
slouken@2716
   342
        /* If the result of the conversion alters the length, i.e. resampling is being used, use the streamer */
slouken@2716
   343
        if (device->convert.len_mult != 1 || device->convert.len_div != 1) {
slouken@2716
   344
            /* The streamer's maximum length should be twice whichever is larger: spec.size or len_cvt */
slouken@2716
   345
            stream_max_len = 2 * device->spec.size;
slouken@2716
   346
            if (device->convert.len_mult > device->convert.len_div) {
slouken@2716
   347
                stream_max_len *= device->convert.len_mult;
slouken@2716
   348
                stream_max_len /= device->convert.len_div;
slouken@2716
   349
            }
slouken@2716
   350
            if (SDL_StreamInit(&device->streamer, stream_max_len, silence) <
slouken@2716
   351
                0)
slouken@2716
   352
                return -1;
slouken@2716
   353
            device->use_streamer = 1;
slouken@2716
   354
slouken@2716
   355
            /* istream_len should be the length of what we grab from the callback and feed to conversion,
slouken@2716
   356
               so that we get close to spec_size. I.e. we want device.spec_size = istream_len * u / d
slouken@2716
   357
             */
slouken@2716
   358
            istream_len =
slouken@2716
   359
                device->spec.size * device->convert.len_div /
slouken@2716
   360
                device->convert.len_mult;
slouken@2716
   361
        }
icculus@3021
   362
#endif
slouken@2716
   363
slouken@2716
   364
        /* stream_len = device->convert.len; */
slouken@2716
   365
        stream_len = device->spec.size;
slouken@1895
   366
    } else {
icculus@2049
   367
        stream_len = device->spec.size;
slouken@1895
   368
    }
icculus@1561
   369
slouken@3336
   370
    /* Calculate the delay while paused */
slouken@3336
   371
    delay = ((device->spec.samples * 1000) / device->spec.freq);
slouken@3336
   372
slouken@2716
   373
    /* Determine if the streamer is necessary here */
slouken@2716
   374
    if (device->use_streamer == 1) {
slouken@3336
   375
        /* This code is almost the same as the old code. The difference is, instead of reading
slouken@2716
   376
           directly from the callback into "stream", then converting and sending the audio off,
slouken@2716
   377
           we go: callback -> "istream" -> (conversion) -> streamer -> stream -> device.
slouken@2716
   378
           However, reading and writing with streamer are done separately:
slouken@2716
   379
           - We only call the callback and write to the streamer when the streamer does not
slouken@2716
   380
           contain enough samples to output to the device.
slouken@2716
   381
           - We only read from the streamer and tell the device to play when the streamer
slouken@2716
   382
           does have enough samples to output.
slouken@2716
   383
           This allows us to perform resampling in the conversion step, where the output of the
slouken@2716
   384
           resampling process can be any number. We will have to see what a good size for the
slouken@2716
   385
           stream's maximum length is, but I suspect 2*max(len_cvt, stream_len) is a good figure.
slouken@2716
   386
         */
slouken@2716
   387
        while (device->enabled) {
slouken@3336
   388
slouken@3336
   389
            if (device->paused) {
slouken@3336
   390
                SDL_Delay(delay);
slouken@3336
   391
                continue;
slouken@3336
   392
            }
slouken@3336
   393
slouken@2716
   394
            /* Only read in audio if the streamer doesn't have enough already (if it does not have enough samples to output) */
slouken@2716
   395
            if (SDL_StreamLength(&device->streamer) < stream_len) {
slouken@2716
   396
                /* Set up istream */
slouken@2716
   397
                if (device->convert.needed) {
slouken@2716
   398
                    if (device->convert.buf) {
slouken@2716
   399
                        istream = device->convert.buf;
slouken@2716
   400
                    } else {
slouken@2716
   401
                        continue;
slouken@2716
   402
                    }
slouken@2716
   403
                } else {
slouken@3336
   404
/* FIXME: Ryan, this is probably wrong.  I imagine we don't want to get
slouken@3336
   405
 * a device buffer both here and below in the stream output.
slouken@3336
   406
 */
slouken@2716
   407
                    istream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   408
                    if (istream == NULL) {
slouken@2716
   409
                        istream = device->fake_stream;
slouken@2716
   410
                    }
slouken@2716
   411
                }
slouken@0
   412
slouken@2716
   413
                /* Read from the callback into the _input_ stream */
slouken@6977
   414
                SDL_LockMutex(device->mixer_lock);
slouken@3336
   415
                (*fill) (udata, istream, istream_len);
slouken@6977
   416
                SDL_UnlockMutex(device->mixer_lock);
slouken@2716
   417
slouken@2716
   418
                /* Convert the audio if necessary and write to the streamer */
slouken@2716
   419
                if (device->convert.needed) {
slouken@2716
   420
                    SDL_ConvertAudio(&device->convert);
slouken@2716
   421
                    if (istream == NULL) {
slouken@2716
   422
                        istream = device->fake_stream;
slouken@2716
   423
                    }
slouken@2716
   424
                    /*SDL_memcpy(istream, device->convert.buf, device->convert.len_cvt); */
slouken@2716
   425
                    SDL_StreamWrite(&device->streamer, device->convert.buf,
slouken@2716
   426
                                    device->convert.len_cvt);
slouken@2716
   427
                } else {
slouken@2716
   428
                    SDL_StreamWrite(&device->streamer, istream, istream_len);
slouken@2716
   429
                }
slouken@1895
   430
            }
slouken@2716
   431
slouken@2716
   432
            /* Only output audio if the streamer has enough to output */
slouken@2716
   433
            if (SDL_StreamLength(&device->streamer) >= stream_len) {
slouken@2716
   434
                /* Set up the output stream */
slouken@2716
   435
                if (device->convert.needed) {
slouken@2716
   436
                    if (device->convert.buf) {
slouken@2716
   437
                        stream = device->convert.buf;
slouken@2716
   438
                    } else {
slouken@2716
   439
                        continue;
slouken@2716
   440
                    }
slouken@2716
   441
                } else {
slouken@2716
   442
                    stream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   443
                    if (stream == NULL) {
slouken@2716
   444
                        stream = device->fake_stream;
slouken@2716
   445
                    }
slouken@2716
   446
                }
slouken@2716
   447
slouken@2716
   448
                /* Now read from the streamer */
slouken@2716
   449
                SDL_StreamRead(&device->streamer, stream, stream_len);
slouken@2716
   450
slouken@2716
   451
                /* Ready current buffer for play and change current buffer */
slouken@3336
   452
                if (stream != device->fake_stream) {
slouken@2716
   453
                    current_audio.impl.PlayDevice(device);
slouken@3096
   454
                    /* Wait for an audio buffer to become available */
slouken@3096
   455
                    current_audio.impl.WaitDevice(device);
slouken@2716
   456
                } else {
slouken@3336
   457
                    SDL_Delay(delay);
slouken@2716
   458
                }
slouken@1895
   459
            }
slouken@0
   460
slouken@1895
   461
        }
slouken@2716
   462
    } else {
slouken@2716
   463
        /* Otherwise, do not use the streamer. This is the old code. */
icculus@7406
   464
        const int silence = (int) device->spec.silence;
slouken@0
   465
slouken@2716
   466
        /* Loop, filling the audio buffers */
slouken@2716
   467
        while (device->enabled) {
slouken@2716
   468
slouken@3336
   469
            if (device->paused) {
slouken@3336
   470
                SDL_Delay(delay);
slouken@3336
   471
                continue;
slouken@3336
   472
            }
slouken@3336
   473
slouken@2716
   474
            /* Fill the current buffer with sound */
slouken@2716
   475
            if (device->convert.needed) {
slouken@2716
   476
                if (device->convert.buf) {
slouken@2716
   477
                    stream = device->convert.buf;
slouken@2716
   478
                } else {
slouken@2716
   479
                    continue;
slouken@2716
   480
                }
slouken@2716
   481
            } else {
slouken@2716
   482
                stream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   483
                if (stream == NULL) {
slouken@2716
   484
                    stream = device->fake_stream;
slouken@2716
   485
                }
slouken@1895
   486
            }
slouken@0
   487
icculus@7406
   488
            if (device->paused) {
icculus@7406
   489
                SDL_memset(stream, silence, stream_len);
icculus@7406
   490
            } else {
icculus@7406
   491
                SDL_LockMutex(device->mixer_lock);
icculus@7406
   492
                (*fill) (udata, stream, stream_len);
icculus@7406
   493
                SDL_UnlockMutex(device->mixer_lock);
icculus@7406
   494
            }
slouken@1562
   495
slouken@2716
   496
            /* Convert the audio if necessary */
slouken@2716
   497
            if (device->convert.needed) {
slouken@2716
   498
                SDL_ConvertAudio(&device->convert);
slouken@2716
   499
                stream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   500
                if (stream == NULL) {
slouken@2716
   501
                    stream = device->fake_stream;
slouken@2716
   502
                }
slouken@2716
   503
                SDL_memcpy(stream, device->convert.buf,
slouken@2716
   504
                           device->convert.len_cvt);
slouken@2716
   505
            }
slouken@2716
   506
slouken@2716
   507
            /* Ready current buffer for play and change current buffer */
slouken@3336
   508
            if (stream != device->fake_stream) {
slouken@2716
   509
                current_audio.impl.PlayDevice(device);
slouken@3096
   510
                /* Wait for an audio buffer to become available */
slouken@3096
   511
                current_audio.impl.WaitDevice(device);
slouken@2716
   512
            } else {
slouken@3336
   513
                SDL_Delay(delay);
slouken@2716
   514
            }
slouken@1895
   515
        }
slouken@1895
   516
    }
slouken@1562
   517
slouken@1895
   518
    /* Wait for the audio to drain.. */
icculus@2049
   519
    current_audio.impl.WaitDone(device);
slouken@21
   520
slouken@2716
   521
    /* If necessary, deinit the streamer */
slouken@2716
   522
    if (device->use_streamer == 1)
slouken@2716
   523
        SDL_StreamDeinit(&device->streamer);
slouken@2716
   524
slouken@1895
   525
    return (0);
slouken@0
   526
}
slouken@0
   527
slouken@322
   528
icculus@1982
   529
static SDL_AudioFormat
slouken@1895
   530
SDL_ParseAudioFormat(const char *string)
slouken@1794
   531
{
icculus@2076
   532
#define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
icculus@2049
   533
    CHECK_FMT_STRING(U8);
icculus@2049
   534
    CHECK_FMT_STRING(S8);
icculus@2049
   535
    CHECK_FMT_STRING(U16LSB);
icculus@2049
   536
    CHECK_FMT_STRING(S16LSB);
icculus@2049
   537
    CHECK_FMT_STRING(U16MSB);
icculus@2049
   538
    CHECK_FMT_STRING(S16MSB);
icculus@2049
   539
    CHECK_FMT_STRING(U16SYS);
icculus@2049
   540
    CHECK_FMT_STRING(S16SYS);
icculus@2049
   541
    CHECK_FMT_STRING(U16);
icculus@2049
   542
    CHECK_FMT_STRING(S16);
icculus@2049
   543
    CHECK_FMT_STRING(S32LSB);
icculus@2049
   544
    CHECK_FMT_STRING(S32MSB);
icculus@2049
   545
    CHECK_FMT_STRING(S32SYS);
icculus@2049
   546
    CHECK_FMT_STRING(S32);
icculus@2049
   547
    CHECK_FMT_STRING(F32LSB);
icculus@2049
   548
    CHECK_FMT_STRING(F32MSB);
icculus@2049
   549
    CHECK_FMT_STRING(F32SYS);
icculus@2049
   550
    CHECK_FMT_STRING(F32);
slouken@2060
   551
#undef CHECK_FMT_STRING
icculus@2049
   552
    return 0;
slouken@1895
   553
}
slouken@1895
   554
slouken@1895
   555
int
slouken@1895
   556
SDL_GetNumAudioDrivers(void)
slouken@1895
   557
{
slouken@1895
   558
    return (SDL_arraysize(bootstrap) - 1);
slouken@1895
   559
}
slouken@1895
   560
slouken@1895
   561
const char *
slouken@1895
   562
SDL_GetAudioDriver(int index)
slouken@1895
   563
{
slouken@1895
   564
    if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
slouken@1895
   565
        return (bootstrap[index]->name);
slouken@1895
   566
    }
slouken@1895
   567
    return (NULL);
slouken@1794
   568
}
slouken@1794
   569
slouken@1895
   570
int
slouken@1895
   571
SDL_AudioInit(const char *driver_name)
slouken@0
   572
{
icculus@2049
   573
    int i = 0;
icculus@2049
   574
    int initialized = 0;
icculus@2049
   575
    int tried_to_init = 0;
slouken@0
   576
icculus@2049
   577
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
slouken@2060
   578
        SDL_AudioQuit();        /* shutdown driver if already running. */
slouken@1895
   579
    }
slouken@0
   580
slouken@2060
   581
    SDL_memset(&current_audio, '\0', sizeof(current_audio));
slouken@2060
   582
    SDL_memset(open_devices, '\0', sizeof(open_devices));
icculus@2049
   583
slouken@1895
   584
    /* Select the proper audio driver */
slouken@1909
   585
    if (driver_name == NULL) {
slouken@1909
   586
        driver_name = SDL_getenv("SDL_AUDIODRIVER");
slouken@1909
   587
    }
icculus@2049
   588
icculus@2049
   589
    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
icculus@2049
   590
        /* make sure we should even try this driver before doing so... */
icculus@2049
   591
        const AudioBootStrap *backend = bootstrap[i];
slouken@6900
   592
        if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
slouken@6900
   593
            (!driver_name && backend->demand_only)) {
icculus@2049
   594
            continue;
icculus@2049
   595
        }
slouken@0
   596
icculus@2049
   597
        tried_to_init = 1;
slouken@2060
   598
        SDL_memset(&current_audio, 0, sizeof(current_audio));
icculus@2049
   599
        current_audio.name = backend->name;
icculus@2049
   600
        current_audio.desc = backend->desc;
icculus@3699
   601
        initialized = backend->init(&current_audio.impl);
slouken@1895
   602
    }
icculus@2049
   603
icculus@2049
   604
    if (!initialized) {
icculus@2049
   605
        /* specific drivers will set the error message if they fail... */
icculus@2049
   606
        if (!tried_to_init) {
slouken@1895
   607
            if (driver_name) {
icculus@3699
   608
                SDL_SetError("Audio target '%s' not available", driver_name);
slouken@1895
   609
            } else {
slouken@1895
   610
                SDL_SetError("No available audio device");
slouken@1895
   611
            }
slouken@1895
   612
        }
icculus@2049
   613
slouken@2060
   614
        SDL_memset(&current_audio, 0, sizeof(current_audio));
slouken@2060
   615
        return (-1);            /* No driver was available, so fail. */
slouken@1895
   616
    }
icculus@2049
   617
icculus@2049
   618
    finalize_audio_entry_points();
icculus@2049
   619
slouken@1895
   620
    return (0);
slouken@0
   621
}
slouken@0
   622
slouken@1895
   623
/*
slouken@1895
   624
 * Get the current audio driver name
slouken@1895
   625
 */
slouken@1895
   626
const char *
slouken@1895
   627
SDL_GetCurrentAudioDriver()
slouken@0
   628
{
icculus@2049
   629
    return current_audio.name;
slouken@0
   630
}
slouken@0
   631
icculus@5593
   632
static void
icculus@5593
   633
free_device_list(char ***devices, int *devCount)
icculus@5593
   634
{
icculus@5593
   635
    int i = *devCount;
icculus@5593
   636
    if ((i > 0) && (*devices != NULL)) {
icculus@5593
   637
        while (i--) {
icculus@5593
   638
            SDL_free((*devices)[i]);
icculus@5593
   639
        }
icculus@5593
   640
    }
icculus@5593
   641
icculus@5593
   642
    if (*devices != NULL) {
icculus@5593
   643
        SDL_free(*devices);
icculus@5593
   644
    }
icculus@5593
   645
icculus@5593
   646
    *devices = NULL;
icculus@5593
   647
    *devCount = 0;
icculus@5593
   648
}
icculus@5593
   649
icculus@5593
   650
static
icculus@5593
   651
void SDL_AddCaptureAudioDevice(const char *_name)
icculus@5593
   652
{
icculus@5593
   653
    char *name = NULL;
icculus@5593
   654
    void *ptr = SDL_realloc(current_audio.inputDevices,
icculus@5593
   655
                          (current_audio.inputDeviceCount+1) * sizeof(char*));
icculus@5593
   656
    if (ptr == NULL) {
icculus@5593
   657
        return;  /* oh well. */
icculus@5593
   658
    }
icculus@5593
   659
icculus@5593
   660
    current_audio.inputDevices = (char **) ptr;
icculus@5593
   661
    name = SDL_strdup(_name);  /* if this returns NULL, that's okay. */
icculus@5593
   662
    current_audio.inputDevices[current_audio.inputDeviceCount++] = name;
icculus@5593
   663
}
icculus@5593
   664
icculus@5593
   665
static
icculus@5593
   666
void SDL_AddOutputAudioDevice(const char *_name)
icculus@5593
   667
{
icculus@5593
   668
    char *name = NULL;
icculus@5593
   669
    void *ptr = SDL_realloc(current_audio.outputDevices,
icculus@5593
   670
                          (current_audio.outputDeviceCount+1) * sizeof(char*));
icculus@5593
   671
    if (ptr == NULL) {
icculus@5593
   672
        return;  /* oh well. */
icculus@5593
   673
    }
icculus@5593
   674
icculus@5593
   675
    current_audio.outputDevices = (char **) ptr;
icculus@5593
   676
    name = SDL_strdup(_name);  /* if this returns NULL, that's okay. */
icculus@5593
   677
    current_audio.outputDevices[current_audio.outputDeviceCount++] = name;
icculus@5593
   678
}
icculus@5593
   679
icculus@2049
   680
slouken@1895
   681
int
icculus@2049
   682
SDL_GetNumAudioDevices(int iscapture)
slouken@0
   683
{
icculus@5593
   684
    int retval = 0;
icculus@5593
   685
icculus@2049
   686
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   687
        return -1;
icculus@2049
   688
    }
icculus@5593
   689
icculus@2049
   690
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   691
        return 0;
icculus@2049
   692
    }
icculus@2049
   693
icculus@2049
   694
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
   695
        return 1;
icculus@2049
   696
    }
icculus@2049
   697
icculus@2049
   698
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
   699
        return 1;
icculus@2049
   700
    }
icculus@2049
   701
icculus@5593
   702
    if (iscapture) {
icculus@5593
   703
        free_device_list(&current_audio.inputDevices,
icculus@5593
   704
                         &current_audio.inputDeviceCount);
icculus@5593
   705
        current_audio.impl.DetectDevices(iscapture, SDL_AddCaptureAudioDevice);
icculus@5593
   706
        retval = current_audio.inputDeviceCount;
icculus@5593
   707
    } else {
icculus@5593
   708
        free_device_list(&current_audio.outputDevices,
icculus@5593
   709
                         &current_audio.outputDeviceCount);
icculus@5593
   710
        current_audio.impl.DetectDevices(iscapture, SDL_AddOutputAudioDevice);
icculus@5593
   711
        retval = current_audio.outputDeviceCount;
icculus@5593
   712
    }
icculus@5593
   713
icculus@5593
   714
    return retval;
icculus@2049
   715
}
icculus@2049
   716
slouken@0
   717
icculus@2049
   718
const char *
icculus@2049
   719
SDL_GetAudioDeviceName(int index, int iscapture)
icculus@2049
   720
{
icculus@2049
   721
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   722
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   723
        return NULL;
icculus@2049
   724
    }
icculus@2049
   725
icculus@2049
   726
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   727
        SDL_SetError("No capture support");
icculus@2049
   728
        return NULL;
slouken@1895
   729
    }
icculus@2049
   730
icculus@2049
   731
    if (index < 0) {
icculus@5593
   732
        goto no_such_device;
icculus@2049
   733
    }
slouken@0
   734
icculus@2049
   735
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
   736
        return DEFAULT_INPUT_DEVNAME;
icculus@2049
   737
    }
icculus@2049
   738
icculus@2049
   739
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
   740
        return DEFAULT_OUTPUT_DEVNAME;
slouken@1895
   741
    }
slouken@262
   742
icculus@5593
   743
    if (iscapture) {
icculus@5593
   744
        if (index >= current_audio.inputDeviceCount) {
icculus@5593
   745
            goto no_such_device;
icculus@5593
   746
        }
icculus@5593
   747
        return current_audio.inputDevices[index];
icculus@5593
   748
    } else {
icculus@5593
   749
        if (index >= current_audio.outputDeviceCount) {
icculus@5593
   750
            goto no_such_device;
icculus@5593
   751
        }
icculus@5593
   752
        return current_audio.outputDevices[index];
icculus@5593
   753
    }
icculus@5593
   754
icculus@5593
   755
no_such_device:
icculus@5593
   756
    SDL_SetError("No such device");
icculus@5593
   757
    return NULL;
icculus@2049
   758
}
icculus@2049
   759
icculus@2049
   760
icculus@2049
   761
static void
slouken@2060
   762
close_audio_device(SDL_AudioDevice * device)
icculus@2049
   763
{
icculus@2049
   764
    device->enabled = 0;
icculus@2049
   765
    if (device->thread != NULL) {
icculus@2049
   766
        SDL_WaitThread(device->thread, NULL);
icculus@2049
   767
    }
icculus@2049
   768
    if (device->mixer_lock != NULL) {
icculus@2049
   769
        SDL_DestroyMutex(device->mixer_lock);
icculus@2049
   770
    }
icculus@2049
   771
    if (device->fake_stream != NULL) {
icculus@2049
   772
        SDL_FreeAudioMem(device->fake_stream);
icculus@2049
   773
    }
icculus@2049
   774
    if (device->convert.needed) {
icculus@2049
   775
        SDL_FreeAudioMem(device->convert.buf);
icculus@2049
   776
    }
icculus@2049
   777
    if (device->opened) {
icculus@2049
   778
        current_audio.impl.CloseDevice(device);
icculus@2049
   779
        device->opened = 0;
icculus@2049
   780
    }
icculus@2049
   781
    SDL_FreeAudioMem(device);
icculus@2049
   782
}
icculus@2049
   783
icculus@2049
   784
icculus@2049
   785
/*
icculus@2049
   786
 * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
icculus@2049
   787
 *  Fills in a sanitized copy in (prepared).
icculus@2049
   788
 *  Returns non-zero if okay, zero on fatal parameters in (orig).
icculus@2049
   789
 */
icculus@2049
   790
static int
slouken@2060
   791
prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared)
icculus@2049
   792
{
slouken@2060
   793
    SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec));
icculus@2049
   794
icculus@2049
   795
    if (orig->callback == NULL) {
icculus@2049
   796
        SDL_SetError("SDL_OpenAudio() passed a NULL callback");
icculus@2049
   797
        return 0;
icculus@2049
   798
    }
icculus@2049
   799
icculus@2049
   800
    if (orig->freq == 0) {
icculus@2049
   801
        const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
slouken@2060
   802
        if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
slouken@2060
   803
            prepared->freq = 22050;     /* a reasonable default */
slouken@1895
   804
        }
slouken@1895
   805
    }
icculus@2049
   806
icculus@2049
   807
    if (orig->format == 0) {
icculus@2049
   808
        const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
icculus@2049
   809
        if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
slouken@2060
   810
            prepared->format = AUDIO_S16;       /* a reasonable default */
slouken@1895
   811
        }
slouken@1895
   812
    }
icculus@2049
   813
icculus@2049
   814
    switch (orig->channels) {
slouken@2060
   815
    case 0:{
slouken@2060
   816
            const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
slouken@2141
   817
            if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) {
slouken@2060
   818
                prepared->channels = 2; /* a reasonable default */
slouken@2060
   819
            }
slouken@2060
   820
            break;
icculus@2049
   821
        }
slouken@1895
   822
    case 1:                    /* Mono */
slouken@1895
   823
    case 2:                    /* Stereo */
slouken@1895
   824
    case 4:                    /* surround */
slouken@1895
   825
    case 6:                    /* surround with center and lfe */
slouken@1895
   826
        break;
slouken@1895
   827
    default:
icculus@2049
   828
        SDL_SetError("Unsupported number of audio channels.");
icculus@2049
   829
        return 0;
slouken@1895
   830
    }
icculus@2049
   831
icculus@2049
   832
    if (orig->samples == 0) {
icculus@2049
   833
        const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
slouken@2060
   834
        if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) {
icculus@2049
   835
            /* Pick a default of ~46 ms at desired frequency */
icculus@2049
   836
            /* !!! FIXME: remove this when the non-Po2 resampling is in. */
icculus@2049
   837
            const int samples = (prepared->freq / 1000) * 46;
icculus@2049
   838
            int power2 = 1;
icculus@2049
   839
            while (power2 < samples) {
icculus@2049
   840
                power2 *= 2;
icculus@2049
   841
            }
icculus@2049
   842
            prepared->samples = power2;
slouken@1895
   843
        }
slouken@1895
   844
    }
slouken@0
   845
slouken@1895
   846
    /* Calculate the silence and size of the audio specification */
icculus@2049
   847
    SDL_CalculateAudioSpec(prepared);
slouken@21
   848
icculus@2049
   849
    return 1;
icculus@2049
   850
}
slouken@1408
   851
slouken@21
   852
icculus@2049
   853
static SDL_AudioDeviceID
icculus@2049
   854
open_audio_device(const char *devname, int iscapture,
slouken@2866
   855
                  const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
   856
                  int allowed_changes, int min_id)
icculus@2049
   857
{
icculus@2049
   858
    SDL_AudioDeviceID id = 0;
slouken@2866
   859
    SDL_AudioSpec _obtained;
icculus@2049
   860
    SDL_AudioDevice *device;
slouken@2866
   861
    SDL_bool build_cvt;
icculus@2049
   862
    int i = 0;
slouken@21
   863
icculus@2049
   864
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   865
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   866
        return 0;
icculus@2049
   867
    }
icculus@2049
   868
icculus@2049
   869
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   870
        SDL_SetError("No capture support");
icculus@2049
   871
        return 0;
icculus@2049
   872
    }
icculus@2049
   873
slouken@2866
   874
    if (!obtained) {
slouken@2866
   875
        obtained = &_obtained;
slouken@2866
   876
    }
slouken@2866
   877
    if (!prepare_audiospec(desired, obtained)) {
icculus@2049
   878
        return 0;
icculus@2049
   879
    }
icculus@2049
   880
icculus@2049
   881
    /* If app doesn't care about a specific device, let the user override. */
icculus@2049
   882
    if (devname == NULL) {
icculus@2049
   883
        devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
slouken@1895
   884
    }
slouken@21
   885
icculus@2049
   886
    /*
icculus@2049
   887
     * Catch device names at the high level for the simple case...
icculus@2049
   888
     * This lets us have a basic "device enumeration" for systems that
icculus@2049
   889
     *  don't have multiple devices, but makes sure the device name is
icculus@2049
   890
     *  always NULL when it hits the low level.
icculus@2049
   891
     *
icculus@2049
   892
     * Also make sure that the simple case prevents multiple simultaneous
icculus@2049
   893
     *  opens of the default system device.
icculus@2049
   894
     */
icculus@2049
   895
icculus@2049
   896
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
   897
        if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
icculus@2049
   898
            SDL_SetError("No such device");
icculus@2049
   899
            return 0;
icculus@2049
   900
        }
icculus@2049
   901
        devname = NULL;
icculus@2049
   902
icculus@2049
   903
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
   904
            if ((open_devices[i]) && (open_devices[i]->iscapture)) {
icculus@2049
   905
                SDL_SetError("Audio device already open");
icculus@2049
   906
                return 0;
icculus@2049
   907
            }
icculus@2049
   908
        }
icculus@2049
   909
    }
icculus@2049
   910
icculus@2049
   911
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
   912
        if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
icculus@2049
   913
            SDL_SetError("No such device");
icculus@2049
   914
            return 0;
icculus@2049
   915
        }
icculus@2049
   916
        devname = NULL;
icculus@2049
   917
icculus@2049
   918
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
   919
            if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
icculus@2049
   920
                SDL_SetError("Audio device already open");
icculus@2049
   921
                return 0;
icculus@2049
   922
            }
icculus@2049
   923
        }
icculus@2049
   924
    }
icculus@2049
   925
slouken@2060
   926
    device = (SDL_AudioDevice *) SDL_AllocAudioMem(sizeof(SDL_AudioDevice));
icculus@2049
   927
    if (device == NULL) {
icculus@2049
   928
        SDL_OutOfMemory();
icculus@2049
   929
        return 0;
icculus@2049
   930
    }
slouken@2060
   931
    SDL_memset(device, '\0', sizeof(SDL_AudioDevice));
slouken@2866
   932
    device->spec = *obtained;
icculus@2049
   933
    device->enabled = 1;
icculus@2049
   934
    device->paused = 1;
icculus@2049
   935
    device->iscapture = iscapture;
icculus@2049
   936
icculus@2049
   937
    /* Create a semaphore for locking the sound buffers */
icculus@2049
   938
    if (!current_audio.impl.SkipMixerLock) {
icculus@2049
   939
        device->mixer_lock = SDL_CreateMutex();
icculus@2049
   940
        if (device->mixer_lock == NULL) {
icculus@2049
   941
            close_audio_device(device);
icculus@2049
   942
            SDL_SetError("Couldn't create mixer lock");
icculus@2049
   943
            return 0;
icculus@2049
   944
        }
icculus@2049
   945
    }
icculus@2049
   946
icculus@5593
   947
    /* force a device detection if we haven't done one yet. */
icculus@5593
   948
    if ( ((iscapture) && (current_audio.inputDevices == NULL)) ||
icculus@5593
   949
         ((!iscapture) && (current_audio.outputDevices == NULL)) )
icculus@5593
   950
        SDL_GetNumAudioDevices(iscapture);
icculus@5593
   951
icculus@7038
   952
    if (current_audio.impl.OpenDevice(device, devname, iscapture) < 0) {
icculus@2049
   953
        close_audio_device(device);
icculus@2049
   954
        return 0;
icculus@2049
   955
    }
icculus@2049
   956
    device->opened = 1;
slouken@0
   957
slouken@1895
   958
    /* Allocate a fake audio memory buffer */
aschiffler@4865
   959
    device->fake_stream = (Uint8 *)SDL_AllocAudioMem(device->spec.size);
icculus@2049
   960
    if (device->fake_stream == NULL) {
icculus@2049
   961
        close_audio_device(device);
slouken@1895
   962
        SDL_OutOfMemory();
icculus@2049
   963
        return 0;
slouken@1895
   964
    }
slouken@0
   965
slouken@2866
   966
    /* If the audio driver changes the buffer size, accept it */
slouken@2866
   967
    if (device->spec.samples != obtained->samples) {
slouken@2866
   968
        obtained->samples = device->spec.samples;
slouken@2866
   969
        SDL_CalculateAudioSpec(obtained);
slouken@2866
   970
    }
slouken@2866
   971
slouken@1895
   972
    /* See if we need to do any conversion */
slouken@2866
   973
    build_cvt = SDL_FALSE;
slouken@2866
   974
    if (obtained->freq != device->spec.freq) {
slouken@2866
   975
        if (allowed_changes & SDL_AUDIO_ALLOW_FREQUENCY_CHANGE) {
slouken@2866
   976
            obtained->freq = device->spec.freq;
slouken@2866
   977
        } else {
slouken@2866
   978
            build_cvt = SDL_TRUE;
slouken@2866
   979
        }
slouken@2866
   980
    }
slouken@2866
   981
    if (obtained->format != device->spec.format) {
slouken@2866
   982
        if (allowed_changes & SDL_AUDIO_ALLOW_FORMAT_CHANGE) {
slouken@2866
   983
            obtained->format = device->spec.format;
slouken@2866
   984
        } else {
slouken@2866
   985
            build_cvt = SDL_TRUE;
slouken@2866
   986
        }
slouken@2866
   987
    }
slouken@2866
   988
    if (obtained->channels != device->spec.channels) {
slouken@2866
   989
        if (allowed_changes & SDL_AUDIO_ALLOW_CHANNELS_CHANGE) {
slouken@2866
   990
            obtained->channels = device->spec.channels;
slouken@2866
   991
        } else {
slouken@2866
   992
            build_cvt = SDL_TRUE;
slouken@2866
   993
        }
slouken@2866
   994
    }
slouken@2866
   995
    if (build_cvt) {
slouken@1895
   996
        /* Build an audio conversion block */
icculus@2049
   997
        if (SDL_BuildAudioCVT(&device->convert,
slouken@2866
   998
                              obtained->format, obtained->channels,
slouken@2866
   999
                              obtained->freq,
icculus@2049
  1000
                              device->spec.format, device->spec.channels,
icculus@2049
  1001
                              device->spec.freq) < 0) {
icculus@2049
  1002
            close_audio_device(device);
icculus@2049
  1003
            return 0;
slouken@1895
  1004
        }
icculus@2049
  1005
        if (device->convert.needed) {
slouken@2866
  1006
            device->convert.len = (int) (((double) obtained->size) /
slouken@2060
  1007
                                         device->convert.len_ratio);
icculus@2053
  1008
icculus@2049
  1009
            device->convert.buf =
icculus@2049
  1010
                (Uint8 *) SDL_AllocAudioMem(device->convert.len *
icculus@2049
  1011
                                            device->convert.len_mult);
icculus@2049
  1012
            if (device->convert.buf == NULL) {
icculus@2049
  1013
                close_audio_device(device);
slouken@1895
  1014
                SDL_OutOfMemory();
icculus@2049
  1015
                return 0;
slouken@1895
  1016
            }
slouken@1895
  1017
        }
slouken@1895
  1018
    }
icculus@2049
  1019
icculus@2049
  1020
    /* Find an available device ID and store the structure... */
slouken@2060
  1021
    for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
icculus@2049
  1022
        if (open_devices[id] == NULL) {
icculus@2049
  1023
            open_devices[id] = device;
icculus@2049
  1024
            break;
icculus@2049
  1025
        }
icculus@2049
  1026
    }
icculus@2049
  1027
icculus@2049
  1028
    if (id == SDL_arraysize(open_devices)) {
icculus@2049
  1029
        SDL_SetError("Too many open audio devices");
icculus@2049
  1030
        close_audio_device(device);
icculus@2049
  1031
        return 0;
icculus@2049
  1032
    }
icculus@2049
  1033
slouken@1895
  1034
    /* Start the audio thread if necessary */
icculus@2049
  1035
    if (!current_audio.impl.ProvidesOwnCallbackThread) {
slouken@1895
  1036
        /* Start the audio thread */
icculus@5969
  1037
        char name[64];
icculus@5969
  1038
        SDL_snprintf(name, sizeof (name), "SDLAudioDev%d", (int) (id + 1));
icculus@2049
  1039
/* !!! FIXME: this is nasty. */
icculus@6430
  1040
#if defined(__WIN32__) && !defined(HAVE_LIBC)
slouken@1330
  1041
#undef SDL_CreateThread
icculus@5969
  1042
        device->thread = SDL_CreateThread(SDL_RunAudio, name, device, NULL, NULL);
slouken@1330
  1043
#else
icculus@5969
  1044
        device->thread = SDL_CreateThread(SDL_RunAudio, name, device);
slouken@1330
  1045
#endif
icculus@2049
  1046
        if (device->thread == NULL) {
slouken@2060
  1047
            SDL_CloseAudioDevice(id + 1);
slouken@1895
  1048
            SDL_SetError("Couldn't create audio thread");
icculus@2049
  1049
            return 0;
icculus@2049
  1050
        }
icculus@2049
  1051
    }
icculus@2049
  1052
slouken@2060
  1053
    return id + 1;
icculus@2049
  1054
}
icculus@2049
  1055
icculus@2049
  1056
icculus@2049
  1057
int
slouken@2866
  1058
SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
icculus@2049
  1059
{
icculus@2049
  1060
    SDL_AudioDeviceID id = 0;
icculus@2049
  1061
icculus@2049
  1062
    /* Start up the audio driver, if necessary. This is legacy behaviour! */
icculus@2049
  1063
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
  1064
        if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
slouken@1895
  1065
            return (-1);
slouken@1895
  1066
        }
icculus@2049
  1067
    }
slouken@0
  1068
icculus@2049
  1069
    /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
icculus@2049
  1070
    if (open_devices[0] != NULL) {
icculus@2049
  1071
        SDL_SetError("Audio device is already opened");
icculus@2049
  1072
        return (-1);
slouken@1895
  1073
    }
icculus@2049
  1074
slouken@2866
  1075
    if (obtained) {
slouken@2866
  1076
        id = open_audio_device(NULL, 0, desired, obtained,
slouken@2866
  1077
                               SDL_AUDIO_ALLOW_ANY_CHANGE, 1);
slouken@2866
  1078
    } else {
slouken@2866
  1079
        id = open_audio_device(NULL, 0, desired, desired, 0, 1);
slouken@2866
  1080
    }
slouken@21
  1081
icculus@5964
  1082
    SDL_assert((id == 0) || (id == 1));
icculus@2049
  1083
    return ((id == 0) ? -1 : 0);
icculus@2049
  1084
}
slouken@21
  1085
icculus@2049
  1086
SDL_AudioDeviceID
icculus@2049
  1087
SDL_OpenAudioDevice(const char *device, int iscapture,
slouken@2866
  1088
                    const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
slouken@2866
  1089
                    int allowed_changes)
icculus@2049
  1090
{
slouken@2866
  1091
    return open_audio_device(device, iscapture, desired, obtained,
slouken@2866
  1092
                             allowed_changes, 2);
slouken@1895
  1093
}
slouken@1895
  1094
slouken@3537
  1095
SDL_AudioStatus
icculus@2049
  1096
SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
slouken@1895
  1097
{
icculus@2049
  1098
    SDL_AudioDevice *device = get_audio_device(devid);
slouken@3537
  1099
    SDL_AudioStatus status = SDL_AUDIO_STOPPED;
icculus@2049
  1100
    if (device && device->enabled) {
icculus@2049
  1101
        if (device->paused) {
slouken@1895
  1102
            status = SDL_AUDIO_PAUSED;
slouken@1895
  1103
        } else {
slouken@1895
  1104
            status = SDL_AUDIO_PLAYING;
slouken@1895
  1105
        }
slouken@1895
  1106
    }
slouken@1895
  1107
    return (status);
slouken@0
  1108
}
slouken@0
  1109
icculus@2049
  1110
slouken@3537
  1111
SDL_AudioStatus
icculus@2049
  1112
SDL_GetAudioStatus(void)
icculus@2049
  1113
{
icculus@2049
  1114
    return SDL_GetAudioDeviceStatus(1);
icculus@2049
  1115
}
icculus@2049
  1116
icculus@2049
  1117
void
icculus@2049
  1118
SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
icculus@2049
  1119
{
icculus@2049
  1120
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1121
    if (device) {
icculus@2049
  1122
        device->paused = pause_on;
icculus@2049
  1123
    }
icculus@2049
  1124
}
icculus@2049
  1125
slouken@1895
  1126
void
slouken@1895
  1127
SDL_PauseAudio(int pause_on)
slouken@0
  1128
{
icculus@2049
  1129
    SDL_PauseAudioDevice(1, pause_on);
icculus@2049
  1130
}
icculus@2049
  1131
slouken@0
  1132
icculus@2049
  1133
void
icculus@2049
  1134
SDL_LockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1135
{
icculus@2049
  1136
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1137
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1138
    if (device) {
icculus@2049
  1139
        current_audio.impl.LockDevice(device);
slouken@1895
  1140
    }
slouken@0
  1141
}
slouken@0
  1142
slouken@1895
  1143
void
slouken@1895
  1144
SDL_LockAudio(void)
slouken@0
  1145
{
icculus@2049
  1146
    SDL_LockAudioDevice(1);
icculus@2049
  1147
}
slouken@0
  1148
icculus@2049
  1149
void
icculus@2049
  1150
SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1151
{
slouken@1895
  1152
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1153
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1154
    if (device) {
icculus@2049
  1155
        current_audio.impl.UnlockDevice(device);
slouken@1895
  1156
    }
slouken@0
  1157
}
slouken@0
  1158
slouken@1895
  1159
void
slouken@1895
  1160
SDL_UnlockAudio(void)
slouken@0
  1161
{
icculus@2049
  1162
    SDL_UnlockAudioDevice(1);
icculus@2049
  1163
}
slouken@0
  1164
icculus@2049
  1165
void
icculus@2049
  1166
SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1167
{
icculus@2049
  1168
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1169
    if (device) {
icculus@2049
  1170
        close_audio_device(device);
slouken@2060
  1171
        open_devices[devid - 1] = NULL;
slouken@1895
  1172
    }
slouken@0
  1173
}
slouken@0
  1174
slouken@1895
  1175
void
slouken@1895
  1176
SDL_CloseAudio(void)
slouken@0
  1177
{
icculus@2049
  1178
    SDL_CloseAudioDevice(1);
slouken@0
  1179
}
slouken@0
  1180
slouken@1895
  1181
void
slouken@1895
  1182
SDL_AudioQuit(void)
slouken@0
  1183
{
icculus@7348
  1184
    SDL_AudioDeviceID i;
icculus@7348
  1185
icculus@7345
  1186
    if (!current_audio.name) {  /* not initialized?! */
icculus@7345
  1187
        return;
icculus@7345
  1188
    }
icculus@7345
  1189
icculus@2049
  1190
    for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@7342
  1191
        if (open_devices[i] != NULL) {
icculus@7342
  1192
            SDL_CloseAudioDevice(i);
icculus@7342
  1193
        }
icculus@2049
  1194
    }
slouken@0
  1195
icculus@2049
  1196
    /* Free the driver data */
icculus@2049
  1197
    current_audio.impl.Deinitialize();
icculus@5593
  1198
    free_device_list(&current_audio.outputDevices,
icculus@5593
  1199
                     &current_audio.outputDeviceCount);
icculus@5593
  1200
    free_device_list(&current_audio.inputDevices,
icculus@5593
  1201
                     &current_audio.inputDeviceCount);
slouken@2060
  1202
    SDL_memset(&current_audio, '\0', sizeof(current_audio));
slouken@2060
  1203
    SDL_memset(open_devices, '\0', sizeof(open_devices));
slouken@0
  1204
}
slouken@0
  1205
icculus@1982
  1206
#define NUM_FORMATS 10
slouken@0
  1207
static int format_idx;
slouken@0
  1208
static int format_idx_sub;
icculus@1982
  1209
static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
slouken@1895
  1210
    {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1211
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
slouken@1895
  1212
    {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1213
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
icculus@1982
  1214
    {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB,
icculus@1982
  1215
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1216
    {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB,
icculus@1982
  1217
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1218
    {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB,
icculus@1982
  1219
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1220
    {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB,
icculus@1982
  1221
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1222
    {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB,
icculus@1993
  1223
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1224
    {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB,
icculus@1993
  1225
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1226
    {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB,
icculus@1993
  1227
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1228
    {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB,
icculus@1993
  1229
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
slouken@0
  1230
};
slouken@0
  1231
icculus@1982
  1232
SDL_AudioFormat
icculus@1982
  1233
SDL_FirstAudioFormat(SDL_AudioFormat format)
slouken@0
  1234
{
slouken@1895
  1235
    for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
slouken@1895
  1236
        if (format_list[format_idx][0] == format) {
slouken@1895
  1237
            break;
slouken@1895
  1238
        }
slouken@1895
  1239
    }
slouken@1895
  1240
    format_idx_sub = 0;
slouken@1895
  1241
    return (SDL_NextAudioFormat());
slouken@0
  1242
}
slouken@0
  1243
icculus@1982
  1244
SDL_AudioFormat
slouken@1895
  1245
SDL_NextAudioFormat(void)
slouken@0
  1246
{
slouken@1895
  1247
    if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
slouken@1895
  1248
        return (0);
slouken@1895
  1249
    }
slouken@1895
  1250
    return (format_list[format_idx][format_idx_sub++]);
slouken@0
  1251
}
slouken@0
  1252
slouken@1895
  1253
void
slouken@1895
  1254
SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
slouken@0
  1255
{
slouken@1895
  1256
    switch (spec->format) {
slouken@1895
  1257
    case AUDIO_U8:
slouken@1895
  1258
        spec->silence = 0x80;
slouken@1895
  1259
        break;
slouken@1895
  1260
    default:
slouken@1895
  1261
        spec->silence = 0x00;
slouken@1895
  1262
        break;
slouken@1895
  1263
    }
icculus@2049
  1264
    spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
slouken@1895
  1265
    spec->size *= spec->channels;
slouken@1895
  1266
    spec->size *= spec->samples;
slouken@0
  1267
}
slouken@1895
  1268
icculus@2049
  1269
icculus@2049
  1270
/*
icculus@2049
  1271
 * Moved here from SDL_mixer.c, since it relies on internals of an opened
icculus@2049
  1272
 *  audio device (and is deprecated, by the way!).
icculus@2049
  1273
 */
icculus@2049
  1274
void
icculus@2049
  1275
SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
icculus@2049
  1276
{
icculus@2049
  1277
    /* Mix the user-level audio format */
icculus@2049
  1278
    SDL_AudioDevice *device = get_audio_device(1);
icculus@2049
  1279
    if (device != NULL) {
icculus@2049
  1280
        SDL_AudioFormat format;
icculus@2049
  1281
        if (device->convert.needed) {
icculus@2049
  1282
            format = device->convert.src_format;
icculus@2049
  1283
        } else {
icculus@2049
  1284
            format = device->spec.format;
icculus@2049
  1285
        }
icculus@2049
  1286
        SDL_MixAudioFormat(dst, src, format, len, volume);
icculus@2049
  1287
    }
icculus@2049
  1288
}
icculus@2049
  1289
slouken@1895
  1290
/* vi: set ts=4 sw=4 expandtab: */