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