src/audio/SDL_audio.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 25 Aug 2008 15:08:59 +0000
changeset 2716 f8f68f47285a
parent 2274 aedfcdeb69b6
child 2728 2768bd7281e0
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

Audio Ideas - Resampling and Pitch Shifting
by Aaron Wishnick, mentored by Ryan C. Gordon
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Allow access to a raw mixing buffer */
slouken@0
    25
slouken@0
    26
#include "SDL.h"
slouken@0
    27
#include "SDL_audio_c.h"
slouken@0
    28
#include "SDL_audiomem.h"
slouken@0
    29
#include "SDL_sysaudio.h"
slouken@0
    30
icculus@2049
    31
#define _THIS	SDL_AudioDevice *this
icculus@2049
    32
icculus@2049
    33
static SDL_AudioDriver current_audio;
icculus@2049
    34
static SDL_AudioDevice *open_devices[16];
icculus@2049
    35
icculus@2049
    36
/* !!! FIXME: These are wordy and unlocalized... */
icculus@2049
    37
#define DEFAULT_OUTPUT_DEVNAME "System audio output device"
icculus@2049
    38
#define DEFAULT_INPUT_DEVNAME "System audio capture device"
icculus@2049
    39
icculus@2049
    40
icculus@2049
    41
/*
icculus@2049
    42
 * Not all of these will be compiled and linked in, but it's convenient
icculus@2049
    43
 *  to have a complete list here and saves yet-another block of #ifdefs...
icculus@2049
    44
 *  Please see bootstrap[], below, for the actual #ifdef mess.
icculus@2049
    45
 */
icculus@2049
    46
extern AudioBootStrap BSD_AUDIO_bootstrap;
icculus@2049
    47
extern AudioBootStrap DSP_bootstrap;
icculus@2049
    48
extern AudioBootStrap DMA_bootstrap;
icculus@2049
    49
extern AudioBootStrap ALSA_bootstrap;
icculus@2271
    50
extern AudioBootStrap PULSEAUDIO_bootstrap;
icculus@2049
    51
extern AudioBootStrap QNXNTOAUDIO_bootstrap;
icculus@2049
    52
extern AudioBootStrap SUNAUDIO_bootstrap;
icculus@2049
    53
extern AudioBootStrap DMEDIA_bootstrap;
icculus@2049
    54
extern AudioBootStrap ARTS_bootstrap;
icculus@2049
    55
extern AudioBootStrap ESD_bootstrap;
icculus@2049
    56
extern AudioBootStrap NAS_bootstrap;
icculus@2049
    57
extern AudioBootStrap DSOUND_bootstrap;
icculus@2070
    58
extern AudioBootStrap WINWAVEOUT_bootstrap;
icculus@2049
    59
extern AudioBootStrap PAUDIO_bootstrap;
icculus@2049
    60
extern AudioBootStrap BEOSAUDIO_bootstrap;
icculus@2049
    61
extern AudioBootStrap COREAUDIO_bootstrap;
icculus@2049
    62
extern AudioBootStrap SNDMGR_bootstrap;
icculus@2049
    63
extern AudioBootStrap MINTAUDIO_GSXB_bootstrap;
icculus@2049
    64
extern AudioBootStrap MINTAUDIO_MCSN_bootstrap;
icculus@2049
    65
extern AudioBootStrap MINTAUDIO_STFA_bootstrap;
icculus@2049
    66
extern AudioBootStrap MINTAUDIO_XBIOS_bootstrap;
icculus@2049
    67
extern AudioBootStrap MINTAUDIO_DMA8_bootstrap;
icculus@2049
    68
extern AudioBootStrap DISKAUD_bootstrap;
icculus@2049
    69
extern AudioBootStrap DUMMYAUD_bootstrap;
icculus@2049
    70
extern AudioBootStrap DCAUD_bootstrap;
icculus@2049
    71
extern AudioBootStrap MMEAUDIO_bootstrap;
icculus@2049
    72
extern AudioBootStrap DART_bootstrap;
icculus@2049
    73
icculus@1190
    74
slouken@0
    75
/* Available audio drivers */
slouken@0
    76
static AudioBootStrap *bootstrap[] = {
slouken@1567
    77
#if SDL_AUDIO_DRIVER_BSD
slouken@1895
    78
    &BSD_AUDIO_bootstrap,
slouken@94
    79
#endif
slouken@1361
    80
#if SDL_AUDIO_DRIVER_OSS
slouken@1895
    81
    &DSP_bootstrap,
slouken@1895
    82
    &DMA_bootstrap,
slouken@0
    83
#endif
slouken@1361
    84
#if SDL_AUDIO_DRIVER_ALSA
slouken@1895
    85
    &ALSA_bootstrap,
slouken@0
    86
#endif
icculus@2271
    87
#if SDL_AUDIO_DRIVER_PULSEAUDIO
slouken@2274
    88
    &PULSEAUDIO_bootstrap,
icculus@2271
    89
#endif
slouken@1361
    90
#if SDL_AUDIO_DRIVER_QNXNTO
slouken@1895
    91
    &QNXNTOAUDIO_bootstrap,
slouken@663
    92
#endif
slouken@1361
    93
#if SDL_AUDIO_DRIVER_SUNAUDIO
slouken@1895
    94
    &SUNAUDIO_bootstrap,
slouken@148
    95
#endif
slouken@1361
    96
#if SDL_AUDIO_DRIVER_DMEDIA
slouken@1895
    97
    &DMEDIA_bootstrap,
slouken@35
    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
slouken@1361
   105
#if SDL_AUDIO_DRIVER_NAS
slouken@1895
   106
    &NAS_bootstrap,
slouken@0
   107
#endif
slouken@1361
   108
#if SDL_AUDIO_DRIVER_DSOUND
slouken@1895
   109
    &DSOUND_bootstrap,
slouken@0
   110
#endif
icculus@2070
   111
#if SDL_AUDIO_DRIVER_WINWAVEOUT
icculus@2070
   112
    &WINWAVEOUT_bootstrap,
slouken@0
   113
#endif
icculus@2049
   114
#if SDL_AUDIO_DRIVER_PAUDIO
icculus@2049
   115
    &PAUDIO_bootstrap,
slouken@1361
   116
#endif
icculus@2049
   117
#if SDL_AUDIO_DRIVER_BEOSAUDIO
icculus@2049
   118
    &BEOSAUDIO_bootstrap,
slouken@0
   119
#endif
slouken@1361
   120
#if SDL_AUDIO_DRIVER_COREAUDIO
slouken@1895
   121
    &COREAUDIO_bootstrap,
slouken@935
   122
#endif
slouken@1361
   123
#if SDL_AUDIO_DRIVER_SNDMGR
slouken@1895
   124
    &SNDMGR_bootstrap,
slouken@0
   125
#endif
slouken@1361
   126
#if SDL_AUDIO_DRIVER_MINT
slouken@1895
   127
    &MINTAUDIO_GSXB_bootstrap,
slouken@1895
   128
    &MINTAUDIO_MCSN_bootstrap,
slouken@1895
   129
    &MINTAUDIO_STFA_bootstrap,
slouken@1895
   130
    &MINTAUDIO_XBIOS_bootstrap,
slouken@1895
   131
    &MINTAUDIO_DMA8_bootstrap,
slouken@398
   132
#endif
slouken@1361
   133
#if SDL_AUDIO_DRIVER_DISK
slouken@1895
   134
    &DISKAUD_bootstrap,
slouken@68
   135
#endif
icculus@1532
   136
#if SDL_AUDIO_DRIVER_DUMMY
slouken@1895
   137
    &DUMMYAUD_bootstrap,
icculus@1532
   138
#endif
slouken@1361
   139
#if SDL_AUDIO_DRIVER_DC
slouken@1895
   140
    &DCAUD_bootstrap,
slouken@509
   141
#endif
slouken@1361
   142
#if SDL_AUDIO_DRIVER_MMEAUDIO
slouken@1895
   143
    &MMEAUDIO_bootstrap,
slouken@1361
   144
#endif
slouken@1361
   145
#if SDL_AUDIO_DRIVER_DART
slouken@1895
   146
    &DART_bootstrap,
icculus@1190
   147
#endif
slouken@1895
   148
    NULL
slouken@0
   149
};
icculus@2049
   150
slouken@2060
   151
static SDL_AudioDevice *
slouken@2060
   152
get_audio_device(SDL_AudioDeviceID id)
icculus@2049
   153
{
icculus@2049
   154
    id--;
slouken@2060
   155
    if ((id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL)) {
icculus@2049
   156
        SDL_SetError("Invalid audio device ID");
icculus@2049
   157
        return NULL;
icculus@2049
   158
    }
icculus@2049
   159
icculus@2049
   160
    return open_devices[id];
icculus@2049
   161
}
icculus@2049
   162
icculus@2049
   163
icculus@2049
   164
/* stubs for audio drivers that don't need a specific entry point... */
slouken@2060
   165
static int
slouken@2060
   166
SDL_AudioDetectDevices_Default(int iscapture)
slouken@2060
   167
{
slouken@2060
   168
    return -1;
slouken@2060
   169
}
slouken@2060
   170
static void
slouken@2060
   171
SDL_AudioThreadInit_Default(_THIS)
slouken@2060
   172
{                               /* no-op. */
slouken@2060
   173
}
slouken@2060
   174
static void
slouken@2060
   175
SDL_AudioWaitDevice_Default(_THIS)
slouken@2060
   176
{                               /* no-op. */
slouken@2060
   177
}
slouken@2060
   178
static void
slouken@2060
   179
SDL_AudioPlayDevice_Default(_THIS)
slouken@2060
   180
{                               /* no-op. */
slouken@2060
   181
}
slouken@2060
   182
static Uint8 *
slouken@2060
   183
SDL_AudioGetDeviceBuf_Default(_THIS)
slouken@2060
   184
{
slouken@2060
   185
    return NULL;
slouken@2060
   186
}
slouken@2060
   187
static void
slouken@2060
   188
SDL_AudioWaitDone_Default(_THIS)
slouken@2060
   189
{                               /* no-op. */
slouken@2060
   190
}
slouken@2060
   191
static void
slouken@2060
   192
SDL_AudioCloseDevice_Default(_THIS)
slouken@2060
   193
{                               /* no-op. */
slouken@2060
   194
}
slouken@2060
   195
static void
slouken@2060
   196
SDL_AudioDeinitialize_Default(void)
slouken@2060
   197
{                               /* no-op. */
slouken@2060
   198
}
icculus@2049
   199
icculus@2049
   200
static int
icculus@2049
   201
SDL_AudioOpenDevice_Default(_THIS, const char *devname, int iscapture)
icculus@2049
   202
{
icculus@2049
   203
    return 0;
icculus@2049
   204
}
icculus@2049
   205
slouken@2060
   206
static const char *
slouken@2060
   207
SDL_AudioGetDeviceName_Default(int index, int iscapture)
icculus@2049
   208
{
icculus@2049
   209
    SDL_SetError("No such device");
icculus@2049
   210
    return NULL;
icculus@2049
   211
}
slouken@0
   212
icculus@2049
   213
static void
icculus@2049
   214
SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   215
{
icculus@2049
   216
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
icculus@2049
   217
        return;
icculus@2049
   218
    }
icculus@2049
   219
    SDL_mutexP(device->mixer_lock);
icculus@2049
   220
}
icculus@2049
   221
icculus@2049
   222
static void
icculus@2049
   223
SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
icculus@2049
   224
{
icculus@2049
   225
    if (device->thread && (SDL_ThreadID() == device->threadid)) {
icculus@2049
   226
        return;
icculus@2049
   227
    }
icculus@2049
   228
    SDL_mutexV(device->mixer_lock);
icculus@2049
   229
}
icculus@2049
   230
slouken@0
   231
slouken@2060
   232
static void
slouken@2060
   233
finalize_audio_entry_points(void)
icculus@2049
   234
{
icculus@2049
   235
    /*
icculus@2049
   236
     * Fill in stub functions for unused driver entry points. This lets us
icculus@2049
   237
     *  blindly call them without having to check for validity first.
icculus@2049
   238
     */
icculus@2049
   239
slouken@2060
   240
#define FILL_STUB(x) \
icculus@2049
   241
        if (current_audio.impl.x == NULL) { \
icculus@2049
   242
            current_audio.impl.x = SDL_Audio##x##_Default; \
icculus@2049
   243
        }
icculus@2049
   244
    FILL_STUB(DetectDevices);
icculus@2049
   245
    FILL_STUB(GetDeviceName);
icculus@2049
   246
    FILL_STUB(OpenDevice);
icculus@2049
   247
    FILL_STUB(ThreadInit);
icculus@2049
   248
    FILL_STUB(WaitDevice);
icculus@2049
   249
    FILL_STUB(PlayDevice);
icculus@2049
   250
    FILL_STUB(GetDeviceBuf);
icculus@2049
   251
    FILL_STUB(WaitDone);
icculus@2049
   252
    FILL_STUB(CloseDevice);
icculus@2049
   253
    FILL_STUB(LockDevice);
icculus@2049
   254
    FILL_STUB(UnlockDevice);
icculus@2049
   255
    FILL_STUB(Deinitialize);
slouken@2060
   256
#undef FILL_STUB
icculus@2049
   257
}
icculus@2049
   258
slouken@2716
   259
/* Streaming functions (for when the input and output buffer sizes are different) */
slouken@2716
   260
/* Write [length] bytes from buf into the streamer */
slouken@2716
   261
void
slouken@2716
   262
SDL_StreamWrite(SDL_AudioStreamer * stream, Uint8 * buf, int length)
slouken@2716
   263
{
slouken@2716
   264
    int i;
slouken@2716
   265
slouken@2716
   266
    for (i = 0; i < length; ++i) {
slouken@2716
   267
        stream->buffer[stream->write_pos] = buf[i];
slouken@2716
   268
        ++stream->write_pos;
slouken@2716
   269
    }
slouken@2716
   270
}
slouken@2716
   271
slouken@2716
   272
/* Read [length] bytes out of the streamer into buf */
slouken@2716
   273
void
slouken@2716
   274
SDL_StreamRead(SDL_AudioStreamer * stream, Uint8 * buf, int length)
slouken@2716
   275
{
slouken@2716
   276
    int i;
slouken@2716
   277
slouken@2716
   278
    for (i = 0; i < length; ++i) {
slouken@2716
   279
        buf[i] = stream->buffer[stream->read_pos];
slouken@2716
   280
        ++stream->read_pos;
slouken@2716
   281
    }
slouken@2716
   282
}
slouken@2716
   283
slouken@2716
   284
int
slouken@2716
   285
SDL_StreamLength(SDL_AudioStreamer * stream)
slouken@2716
   286
{
slouken@2716
   287
    return (stream->write_pos - stream->read_pos) % stream->max_len;
slouken@2716
   288
}
slouken@2716
   289
slouken@2716
   290
/* Initialize the stream by allocating the buffer and setting the read/write heads to the beginning */
slouken@2716
   291
int
slouken@2716
   292
SDL_StreamInit(SDL_AudioStreamer * stream, int max_len, Uint8 silence)
slouken@2716
   293
{
slouken@2716
   294
    int i;
slouken@2716
   295
slouken@2716
   296
    /* First try to allocate the buffer */
slouken@2716
   297
    stream->buffer = (Uint8 *) malloc(max_len);
slouken@2716
   298
    if (stream->buffer == NULL) {
slouken@2716
   299
        return -1;
slouken@2716
   300
    }
slouken@2716
   301
slouken@2716
   302
    stream->max_len = max_len;
slouken@2716
   303
    stream->read_pos = 0;
slouken@2716
   304
    stream->write_pos = 0;
slouken@2716
   305
slouken@2716
   306
    /* Zero out the buffer */
slouken@2716
   307
    for (i = 0; i < max_len; ++i) {
slouken@2716
   308
        stream->buffer[i] = silence;
slouken@2716
   309
    }
slouken@2716
   310
}
slouken@2716
   311
slouken@2716
   312
/* Deinitialize the stream simply by freeing the buffer */
slouken@2716
   313
void
slouken@2716
   314
SDL_StreamDeinit(SDL_AudioStreamer * stream)
slouken@2716
   315
{
slouken@2716
   316
    if (stream->buffer != NULL) {
slouken@2716
   317
        free(stream->buffer);
slouken@2716
   318
    }
slouken@2716
   319
}
slouken@2716
   320
slouken@0
   321
slouken@0
   322
/* The general mixing thread function */
slouken@1895
   323
int SDLCALL
icculus@2049
   324
SDL_RunAudio(void *devicep)
slouken@0
   325
{
icculus@2049
   326
    SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
slouken@1895
   327
    Uint8 *stream;
slouken@1895
   328
    int stream_len;
slouken@1895
   329
    void *udata;
slouken@1895
   330
    void (SDLCALL * fill) (void *userdata, Uint8 * stream, int len);
slouken@1895
   331
    int silence;
slouken@2716
   332
    int stream_max_len;
slouken@2716
   333
slouken@2716
   334
    /* For streaming when the buffer sizes don't match up */
slouken@2716
   335
    Uint8 *istream;
slouken@2716
   336
    int istream_len;
slouken@0
   337
slouken@1895
   338
    /* Perform any thread setup */
icculus@2049
   339
    device->threadid = SDL_ThreadID();
icculus@2049
   340
    current_audio.impl.ThreadInit(device);
slouken@0
   341
slouken@1895
   342
    /* Set up the mixing function */
icculus@2049
   343
    fill = device->spec.callback;
icculus@2049
   344
    udata = device->spec.userdata;
slouken@21
   345
slouken@2716
   346
    /* By default do not stream */
slouken@2716
   347
    device->use_streamer = 0;
slouken@2716
   348
icculus@2049
   349
    if (device->convert.needed) {
icculus@2049
   350
        if (device->convert.src_format == AUDIO_U8) {
slouken@1895
   351
            silence = 0x80;
slouken@1895
   352
        } else {
slouken@1895
   353
            silence = 0;
slouken@1895
   354
        }
slouken@2716
   355
slouken@2716
   356
        /* If the result of the conversion alters the length, i.e. resampling is being used, use the streamer */
slouken@2716
   357
        if (device->convert.len_mult != 1 || device->convert.len_div != 1) {
slouken@2716
   358
            /* The streamer's maximum length should be twice whichever is larger: spec.size or len_cvt */
slouken@2716
   359
            stream_max_len = 2 * device->spec.size;
slouken@2716
   360
            if (device->convert.len_mult > device->convert.len_div) {
slouken@2716
   361
                stream_max_len *= device->convert.len_mult;
slouken@2716
   362
                stream_max_len /= device->convert.len_div;
slouken@2716
   363
            }
slouken@2716
   364
            if (SDL_StreamInit(&device->streamer, stream_max_len, silence) <
slouken@2716
   365
                0)
slouken@2716
   366
                return -1;
slouken@2716
   367
            device->use_streamer = 1;
slouken@2716
   368
slouken@2716
   369
            /* istream_len should be the length of what we grab from the callback and feed to conversion,
slouken@2716
   370
               so that we get close to spec_size. I.e. we want device.spec_size = istream_len * u / d
slouken@2716
   371
             */
slouken@2716
   372
            istream_len =
slouken@2716
   373
                device->spec.size * device->convert.len_div /
slouken@2716
   374
                device->convert.len_mult;
slouken@2716
   375
        }
slouken@2716
   376
slouken@2716
   377
        /* stream_len = device->convert.len; */
slouken@2716
   378
        stream_len = device->spec.size;
slouken@1895
   379
    } else {
icculus@2049
   380
        silence = device->spec.silence;
icculus@2049
   381
        stream_len = device->spec.size;
slouken@1895
   382
    }
icculus@1561
   383
slouken@2716
   384
    /* Determine if the streamer is necessary here */
slouken@2716
   385
    if (device->use_streamer == 1) {
slouken@2716
   386
        /* This code is almost the same as the old code. The difference is, instead of reding
slouken@2716
   387
           directly from the callback into "stream", then converting and sending the audio off,
slouken@2716
   388
           we go: callback -> "istream" -> (conversion) -> streamer -> stream -> device.
slouken@2716
   389
           However, reading and writing with streamer are done separately:
slouken@2716
   390
           - We only call the callback and write to the streamer when the streamer does not
slouken@2716
   391
           contain enough samples to output to the device.
slouken@2716
   392
           - We only read from the streamer and tell the device to play when the streamer
slouken@2716
   393
           does have enough samples to output.
slouken@2716
   394
           This allows us to perform resampling in the conversion step, where the output of the
slouken@2716
   395
           resampling process can be any number. We will have to see what a good size for the
slouken@2716
   396
           stream's maximum length is, but I suspect 2*max(len_cvt, stream_len) is a good figure.
slouken@2716
   397
         */
slouken@2716
   398
        while (device->enabled) {
slouken@2716
   399
            /* Only read in audio if the streamer doesn't have enough already (if it does not have enough samples to output) */
slouken@2716
   400
            if (SDL_StreamLength(&device->streamer) < stream_len) {
slouken@2716
   401
                /* Set up istream */
slouken@2716
   402
                if (device->convert.needed) {
slouken@2716
   403
                    if (device->convert.buf) {
slouken@2716
   404
                        istream = device->convert.buf;
slouken@2716
   405
                    } else {
slouken@2716
   406
                        continue;
slouken@2716
   407
                    }
slouken@2716
   408
                } else {
slouken@2716
   409
                    istream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   410
                    if (istream == NULL) {
slouken@2716
   411
                        istream = device->fake_stream;
slouken@2716
   412
                    }
slouken@2716
   413
                }
slouken@0
   414
slouken@2716
   415
                /* Read from the callback into the _input_ stream */
slouken@2716
   416
                if (!device->paused) {
slouken@2716
   417
                    SDL_mutexP(device->mixer_lock);
slouken@2716
   418
                    (*fill) (udata, istream, istream_len);
slouken@2716
   419
                    SDL_mutexV(device->mixer_lock);
slouken@2716
   420
                }
slouken@2716
   421
slouken@2716
   422
                /* Convert the audio if necessary and write to the streamer */
slouken@2716
   423
                if (device->convert.needed) {
slouken@2716
   424
                    SDL_ConvertAudio(&device->convert);
slouken@2716
   425
                    if (istream == NULL) {
slouken@2716
   426
                        istream = device->fake_stream;
slouken@2716
   427
                    }
slouken@2716
   428
                    /*SDL_memcpy(istream, device->convert.buf, device->convert.len_cvt); */
slouken@2716
   429
                    SDL_StreamWrite(&device->streamer, device->convert.buf,
slouken@2716
   430
                                    device->convert.len_cvt);
slouken@2716
   431
                } else {
slouken@2716
   432
                    SDL_StreamWrite(&device->streamer, istream, istream_len);
slouken@2716
   433
                }
slouken@1895
   434
            }
slouken@2716
   435
slouken@2716
   436
            /* Only output audio if the streamer has enough to output */
slouken@2716
   437
            if (SDL_StreamLength(&device->streamer) >= stream_len) {
slouken@2716
   438
                /* Set up the output stream */
slouken@2716
   439
                if (device->convert.needed) {
slouken@2716
   440
                    if (device->convert.buf) {
slouken@2716
   441
                        stream = device->convert.buf;
slouken@2716
   442
                    } else {
slouken@2716
   443
                        continue;
slouken@2716
   444
                    }
slouken@2716
   445
                } else {
slouken@2716
   446
                    stream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   447
                    if (stream == NULL) {
slouken@2716
   448
                        stream = device->fake_stream;
slouken@2716
   449
                    }
slouken@2716
   450
                }
slouken@2716
   451
slouken@2716
   452
                /* Now read from the streamer */
slouken@2716
   453
                SDL_StreamRead(&device->streamer, stream, stream_len);
slouken@2716
   454
slouken@2716
   455
                /* Ready current buffer for play and change current buffer */
slouken@2716
   456
                if (stream != device->fake_stream) {
slouken@2716
   457
                    current_audio.impl.PlayDevice(device);
slouken@2716
   458
                }
slouken@2716
   459
slouken@2716
   460
                /* Wait for an audio buffer to become available */
slouken@2716
   461
                if (stream == device->fake_stream) {
slouken@2716
   462
                    SDL_Delay((device->spec.samples * 1000) /
slouken@2716
   463
                              device->spec.freq);
slouken@2716
   464
                } else {
slouken@2716
   465
                    current_audio.impl.WaitDevice(device);
slouken@2716
   466
                }
slouken@1895
   467
            }
slouken@0
   468
slouken@1895
   469
        }
slouken@2716
   470
    } else {
slouken@2716
   471
        /* Otherwise, do not use the streamer. This is the old code. */
slouken@0
   472
slouken@2716
   473
        /* Loop, filling the audio buffers */
slouken@2716
   474
        while (device->enabled) {
slouken@2716
   475
slouken@2716
   476
            /* Fill the current buffer with sound */
slouken@2716
   477
            if (device->convert.needed) {
slouken@2716
   478
                if (device->convert.buf) {
slouken@2716
   479
                    stream = device->convert.buf;
slouken@2716
   480
                } else {
slouken@2716
   481
                    continue;
slouken@2716
   482
                }
slouken@2716
   483
            } else {
slouken@2716
   484
                stream = current_audio.impl.GetDeviceBuf(device);
slouken@2716
   485
                if (stream == NULL) {
slouken@2716
   486
                    stream = device->fake_stream;
slouken@2716
   487
                }
slouken@1895
   488
            }
slouken@0
   489
slouken@2716
   490
            if (!device->paused) {
slouken@2716
   491
                SDL_mutexP(device->mixer_lock);
slouken@2716
   492
                (*fill) (udata, stream, stream_len);
slouken@2716
   493
                SDL_mutexV(device->mixer_lock);
slouken@2716
   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@2716
   508
            if (stream != device->fake_stream) {
slouken@2716
   509
                current_audio.impl.PlayDevice(device);
slouken@2716
   510
            }
slouken@2716
   511
slouken@2716
   512
            /* Wait for an audio buffer to become available */
slouken@2716
   513
            if (stream == device->fake_stream) {
slouken@2716
   514
                SDL_Delay((device->spec.samples * 1000) / device->spec.freq);
slouken@2716
   515
            } else {
slouken@2716
   516
                current_audio.impl.WaitDevice(device);
slouken@2716
   517
            }
slouken@1895
   518
        }
slouken@1895
   519
    }
slouken@1562
   520
slouken@1895
   521
    /* Wait for the audio to drain.. */
icculus@2049
   522
    current_audio.impl.WaitDone(device);
slouken@21
   523
slouken@2716
   524
    /* If necessary, deinit the streamer */
slouken@2716
   525
    if (device->use_streamer == 1)
slouken@2716
   526
        SDL_StreamDeinit(&device->streamer);
slouken@2716
   527
slouken@1895
   528
    return (0);
slouken@0
   529
}
slouken@0
   530
slouken@322
   531
icculus@1982
   532
static SDL_AudioFormat
slouken@1895
   533
SDL_ParseAudioFormat(const char *string)
slouken@1794
   534
{
icculus@2076
   535
#define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
icculus@2049
   536
    CHECK_FMT_STRING(U8);
icculus@2049
   537
    CHECK_FMT_STRING(S8);
icculus@2049
   538
    CHECK_FMT_STRING(U16LSB);
icculus@2049
   539
    CHECK_FMT_STRING(S16LSB);
icculus@2049
   540
    CHECK_FMT_STRING(U16MSB);
icculus@2049
   541
    CHECK_FMT_STRING(S16MSB);
icculus@2049
   542
    CHECK_FMT_STRING(U16SYS);
icculus@2049
   543
    CHECK_FMT_STRING(S16SYS);
icculus@2049
   544
    CHECK_FMT_STRING(U16);
icculus@2049
   545
    CHECK_FMT_STRING(S16);
icculus@2049
   546
    CHECK_FMT_STRING(S32LSB);
icculus@2049
   547
    CHECK_FMT_STRING(S32MSB);
icculus@2049
   548
    CHECK_FMT_STRING(S32SYS);
icculus@2049
   549
    CHECK_FMT_STRING(S32);
icculus@2049
   550
    CHECK_FMT_STRING(F32LSB);
icculus@2049
   551
    CHECK_FMT_STRING(F32MSB);
icculus@2049
   552
    CHECK_FMT_STRING(F32SYS);
icculus@2049
   553
    CHECK_FMT_STRING(F32);
slouken@2060
   554
#undef CHECK_FMT_STRING
icculus@2049
   555
    return 0;
slouken@1895
   556
}
slouken@1895
   557
slouken@1895
   558
int
slouken@1895
   559
SDL_GetNumAudioDrivers(void)
slouken@1895
   560
{
slouken@1895
   561
    return (SDL_arraysize(bootstrap) - 1);
slouken@1895
   562
}
slouken@1895
   563
slouken@1895
   564
const char *
slouken@1895
   565
SDL_GetAudioDriver(int index)
slouken@1895
   566
{
slouken@1895
   567
    if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
slouken@1895
   568
        return (bootstrap[index]->name);
slouken@1895
   569
    }
slouken@1895
   570
    return (NULL);
slouken@1794
   571
}
slouken@1794
   572
slouken@1895
   573
int
slouken@1895
   574
SDL_AudioInit(const char *driver_name)
slouken@0
   575
{
icculus@2049
   576
    int i = 0;
icculus@2049
   577
    int initialized = 0;
icculus@2049
   578
    int tried_to_init = 0;
slouken@0
   579
icculus@2049
   580
    if (SDL_WasInit(SDL_INIT_AUDIO)) {
slouken@2060
   581
        SDL_AudioQuit();        /* shutdown driver if already running. */
slouken@1895
   582
    }
slouken@0
   583
slouken@2060
   584
    SDL_memset(&current_audio, '\0', sizeof(current_audio));
slouken@2060
   585
    SDL_memset(open_devices, '\0', sizeof(open_devices));
icculus@2049
   586
slouken@1895
   587
    /* Select the proper audio driver */
slouken@1909
   588
    if (driver_name == NULL) {
slouken@1909
   589
        driver_name = SDL_getenv("SDL_AUDIODRIVER");
slouken@1909
   590
    }
icculus@2049
   591
icculus@2049
   592
    for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
icculus@2049
   593
        /* make sure we should even try this driver before doing so... */
icculus@2049
   594
        const AudioBootStrap *backend = bootstrap[i];
slouken@2060
   595
        if (((driver_name) && (SDL_strcasecmp(backend->name, driver_name))) ||
slouken@2060
   596
            ((!driver_name) && (backend->demand_only))) {
icculus@2049
   597
            continue;
icculus@2049
   598
        }
slouken@0
   599
icculus@2049
   600
        tried_to_init = 1;
slouken@2060
   601
        SDL_memset(&current_audio, 0, sizeof(current_audio));
icculus@2049
   602
        current_audio.name = backend->name;
icculus@2049
   603
        current_audio.desc = backend->desc;
icculus@2049
   604
        initialized = backend->init(&current_audio.impl);
slouken@1895
   605
    }
icculus@2049
   606
icculus@2049
   607
    if (!initialized) {
icculus@2049
   608
        /* specific drivers will set the error message if they fail... */
icculus@2049
   609
        if (!tried_to_init) {
slouken@1895
   610
            if (driver_name) {
slouken@1895
   611
                SDL_SetError("%s not available", driver_name);
slouken@1895
   612
            } else {
slouken@1895
   613
                SDL_SetError("No available audio device");
slouken@1895
   614
            }
slouken@1895
   615
        }
icculus@2049
   616
slouken@2060
   617
        SDL_memset(&current_audio, 0, sizeof(current_audio));
slouken@2060
   618
        return (-1);            /* No driver was available, so fail. */
slouken@1895
   619
    }
icculus@2049
   620
icculus@2049
   621
    finalize_audio_entry_points();
icculus@2049
   622
slouken@1895
   623
    return (0);
slouken@0
   624
}
slouken@0
   625
slouken@1895
   626
/*
slouken@1895
   627
 * Get the current audio driver name
slouken@1895
   628
 */
slouken@1895
   629
const char *
slouken@1895
   630
SDL_GetCurrentAudioDriver()
slouken@0
   631
{
icculus@2049
   632
    return current_audio.name;
slouken@0
   633
}
slouken@0
   634
icculus@2049
   635
slouken@1895
   636
int
icculus@2049
   637
SDL_GetNumAudioDevices(int iscapture)
slouken@0
   638
{
icculus@2049
   639
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   640
        return -1;
icculus@2049
   641
    }
icculus@2049
   642
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   643
        return 0;
icculus@2049
   644
    }
icculus@2049
   645
icculus@2049
   646
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
   647
        return 1;
icculus@2049
   648
    }
icculus@2049
   649
icculus@2049
   650
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
   651
        return 1;
icculus@2049
   652
    }
icculus@2049
   653
icculus@2049
   654
    return current_audio.impl.DetectDevices(iscapture);
icculus@2049
   655
}
icculus@2049
   656
slouken@0
   657
icculus@2049
   658
const char *
icculus@2049
   659
SDL_GetAudioDeviceName(int index, int iscapture)
icculus@2049
   660
{
icculus@2049
   661
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   662
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   663
        return NULL;
icculus@2049
   664
    }
icculus@2049
   665
icculus@2049
   666
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   667
        SDL_SetError("No capture support");
icculus@2049
   668
        return NULL;
slouken@1895
   669
    }
icculus@2049
   670
icculus@2049
   671
    if (index < 0) {
icculus@2049
   672
        SDL_SetError("No such device");
icculus@2049
   673
        return NULL;
icculus@2049
   674
    }
slouken@0
   675
icculus@2049
   676
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
   677
        return DEFAULT_INPUT_DEVNAME;
icculus@2049
   678
    }
icculus@2049
   679
icculus@2049
   680
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
   681
        return DEFAULT_OUTPUT_DEVNAME;
slouken@1895
   682
    }
slouken@262
   683
icculus@2049
   684
    return current_audio.impl.GetDeviceName(index, iscapture);
icculus@2049
   685
}
icculus@2049
   686
icculus@2049
   687
icculus@2049
   688
static void
slouken@2060
   689
close_audio_device(SDL_AudioDevice * device)
icculus@2049
   690
{
icculus@2049
   691
    device->enabled = 0;
icculus@2049
   692
    if (device->thread != NULL) {
icculus@2049
   693
        SDL_WaitThread(device->thread, NULL);
icculus@2049
   694
    }
icculus@2049
   695
    if (device->mixer_lock != NULL) {
icculus@2049
   696
        SDL_DestroyMutex(device->mixer_lock);
icculus@2049
   697
    }
icculus@2049
   698
    if (device->fake_stream != NULL) {
icculus@2049
   699
        SDL_FreeAudioMem(device->fake_stream);
icculus@2049
   700
    }
icculus@2049
   701
    if (device->convert.needed) {
icculus@2049
   702
        SDL_FreeAudioMem(device->convert.buf);
icculus@2049
   703
    }
icculus@2049
   704
    if (device->opened) {
icculus@2049
   705
        current_audio.impl.CloseDevice(device);
icculus@2049
   706
        device->opened = 0;
icculus@2049
   707
    }
icculus@2049
   708
    SDL_FreeAudioMem(device);
icculus@2049
   709
}
icculus@2049
   710
icculus@2049
   711
icculus@2049
   712
/*
icculus@2049
   713
 * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
icculus@2049
   714
 *  Fills in a sanitized copy in (prepared).
icculus@2049
   715
 *  Returns non-zero if okay, zero on fatal parameters in (orig).
icculus@2049
   716
 */
icculus@2049
   717
static int
slouken@2060
   718
prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared)
icculus@2049
   719
{
slouken@2060
   720
    SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec));
icculus@2049
   721
icculus@2049
   722
    if (orig->callback == NULL) {
icculus@2049
   723
        SDL_SetError("SDL_OpenAudio() passed a NULL callback");
icculus@2049
   724
        return 0;
icculus@2049
   725
    }
icculus@2049
   726
icculus@2049
   727
    if (orig->freq == 0) {
icculus@2049
   728
        const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
slouken@2060
   729
        if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
slouken@2060
   730
            prepared->freq = 22050;     /* a reasonable default */
slouken@1895
   731
        }
slouken@1895
   732
    }
icculus@2049
   733
icculus@2049
   734
    if (orig->format == 0) {
icculus@2049
   735
        const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
icculus@2049
   736
        if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
slouken@2060
   737
            prepared->format = AUDIO_S16;       /* a reasonable default */
slouken@1895
   738
        }
slouken@1895
   739
    }
icculus@2049
   740
icculus@2049
   741
    switch (orig->channels) {
slouken@2060
   742
    case 0:{
slouken@2060
   743
            const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
slouken@2141
   744
            if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) {
slouken@2060
   745
                prepared->channels = 2; /* a reasonable default */
slouken@2060
   746
            }
slouken@2060
   747
            break;
icculus@2049
   748
        }
slouken@1895
   749
    case 1:                    /* Mono */
slouken@1895
   750
    case 2:                    /* Stereo */
slouken@1895
   751
    case 4:                    /* surround */
slouken@1895
   752
    case 6:                    /* surround with center and lfe */
slouken@1895
   753
        break;
slouken@1895
   754
    default:
icculus@2049
   755
        SDL_SetError("Unsupported number of audio channels.");
icculus@2049
   756
        return 0;
slouken@1895
   757
    }
icculus@2049
   758
icculus@2049
   759
    if (orig->samples == 0) {
icculus@2049
   760
        const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
slouken@2060
   761
        if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) {
icculus@2049
   762
            /* Pick a default of ~46 ms at desired frequency */
icculus@2049
   763
            /* !!! FIXME: remove this when the non-Po2 resampling is in. */
icculus@2049
   764
            const int samples = (prepared->freq / 1000) * 46;
icculus@2049
   765
            int power2 = 1;
icculus@2049
   766
            while (power2 < samples) {
icculus@2049
   767
                power2 *= 2;
icculus@2049
   768
            }
icculus@2049
   769
            prepared->samples = power2;
slouken@1895
   770
        }
slouken@1895
   771
    }
slouken@0
   772
slouken@1895
   773
    /* Calculate the silence and size of the audio specification */
icculus@2049
   774
    SDL_CalculateAudioSpec(prepared);
slouken@21
   775
icculus@2049
   776
    return 1;
icculus@2049
   777
}
slouken@1408
   778
slouken@21
   779
icculus@2049
   780
static SDL_AudioDeviceID
icculus@2049
   781
open_audio_device(const char *devname, int iscapture,
slouken@2060
   782
                  const SDL_AudioSpec * _desired, SDL_AudioSpec * obtained,
slouken@2060
   783
                  int min_id)
icculus@2049
   784
{
icculus@2049
   785
    SDL_AudioDeviceID id = 0;
icculus@2049
   786
    SDL_AudioSpec desired;
icculus@2049
   787
    SDL_AudioDevice *device;
icculus@2049
   788
    int i = 0;
slouken@21
   789
icculus@2049
   790
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   791
        SDL_SetError("Audio subsystem is not initialized");
icculus@2049
   792
        return 0;
icculus@2049
   793
    }
icculus@2049
   794
icculus@2049
   795
    if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
icculus@2049
   796
        SDL_SetError("No capture support");
icculus@2049
   797
        return 0;
icculus@2049
   798
    }
icculus@2049
   799
icculus@2049
   800
    if (!prepare_audiospec(_desired, &desired)) {
icculus@2049
   801
        return 0;
icculus@2049
   802
    }
icculus@2049
   803
icculus@2049
   804
    /* If app doesn't care about a specific device, let the user override. */
icculus@2049
   805
    if (devname == NULL) {
icculus@2049
   806
        devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
slouken@1895
   807
    }
slouken@21
   808
icculus@2049
   809
    /*
icculus@2049
   810
     * Catch device names at the high level for the simple case...
icculus@2049
   811
     * This lets us have a basic "device enumeration" for systems that
icculus@2049
   812
     *  don't have multiple devices, but makes sure the device name is
icculus@2049
   813
     *  always NULL when it hits the low level.
icculus@2049
   814
     *
icculus@2049
   815
     * Also make sure that the simple case prevents multiple simultaneous
icculus@2049
   816
     *  opens of the default system device.
icculus@2049
   817
     */
icculus@2049
   818
icculus@2049
   819
    if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
icculus@2049
   820
        if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
icculus@2049
   821
            SDL_SetError("No such device");
icculus@2049
   822
            return 0;
icculus@2049
   823
        }
icculus@2049
   824
        devname = NULL;
icculus@2049
   825
icculus@2049
   826
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
   827
            if ((open_devices[i]) && (open_devices[i]->iscapture)) {
icculus@2049
   828
                SDL_SetError("Audio device already open");
icculus@2049
   829
                return 0;
icculus@2049
   830
            }
icculus@2049
   831
        }
icculus@2049
   832
    }
icculus@2049
   833
icculus@2049
   834
    if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
icculus@2049
   835
        if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
icculus@2049
   836
            SDL_SetError("No such device");
icculus@2049
   837
            return 0;
icculus@2049
   838
        }
icculus@2049
   839
        devname = NULL;
icculus@2049
   840
icculus@2049
   841
        for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
   842
            if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
icculus@2049
   843
                SDL_SetError("Audio device already open");
icculus@2049
   844
                return 0;
icculus@2049
   845
            }
icculus@2049
   846
        }
icculus@2049
   847
    }
icculus@2049
   848
slouken@2060
   849
    device = (SDL_AudioDevice *) SDL_AllocAudioMem(sizeof(SDL_AudioDevice));
icculus@2049
   850
    if (device == NULL) {
icculus@2049
   851
        SDL_OutOfMemory();
icculus@2049
   852
        return 0;
icculus@2049
   853
    }
slouken@2060
   854
    SDL_memset(device, '\0', sizeof(SDL_AudioDevice));
slouken@2060
   855
    SDL_memcpy(&device->spec, &desired, sizeof(SDL_AudioSpec));
icculus@2049
   856
    device->enabled = 1;
icculus@2049
   857
    device->paused = 1;
icculus@2049
   858
    device->iscapture = iscapture;
icculus@2049
   859
icculus@2049
   860
    /* Create a semaphore for locking the sound buffers */
icculus@2049
   861
    if (!current_audio.impl.SkipMixerLock) {
icculus@2049
   862
        device->mixer_lock = SDL_CreateMutex();
icculus@2049
   863
        if (device->mixer_lock == NULL) {
icculus@2049
   864
            close_audio_device(device);
icculus@2049
   865
            SDL_SetError("Couldn't create mixer lock");
icculus@2049
   866
            return 0;
icculus@2049
   867
        }
icculus@2049
   868
    }
icculus@2049
   869
icculus@2049
   870
    if (!current_audio.impl.OpenDevice(device, devname, iscapture)) {
icculus@2049
   871
        close_audio_device(device);
icculus@2049
   872
        return 0;
icculus@2049
   873
    }
icculus@2049
   874
    device->opened = 1;
slouken@0
   875
slouken@1895
   876
    /* If the audio driver changes the buffer size, accept it */
icculus@2049
   877
    if (device->spec.samples != desired.samples) {
icculus@2049
   878
        desired.samples = device->spec.samples;
icculus@2049
   879
        SDL_CalculateAudioSpec(&device->spec);
slouken@1895
   880
    }
slouken@0
   881
slouken@1895
   882
    /* Allocate a fake audio memory buffer */
icculus@2049
   883
    device->fake_stream = SDL_AllocAudioMem(device->spec.size);
icculus@2049
   884
    if (device->fake_stream == NULL) {
icculus@2049
   885
        close_audio_device(device);
slouken@1895
   886
        SDL_OutOfMemory();
icculus@2049
   887
        return 0;
slouken@1895
   888
    }
slouken@0
   889
slouken@1895
   890
    /* See if we need to do any conversion */
slouken@1895
   891
    if (obtained != NULL) {
icculus@2049
   892
        SDL_memcpy(obtained, &device->spec, sizeof(SDL_AudioSpec));
icculus@2049
   893
    } else if (desired.freq != device->spec.freq ||
icculus@2049
   894
               desired.format != device->spec.format ||
icculus@2049
   895
               desired.channels != device->spec.channels) {
slouken@1895
   896
        /* Build an audio conversion block */
icculus@2049
   897
        if (SDL_BuildAudioCVT(&device->convert,
icculus@2049
   898
                              desired.format, desired.channels,
icculus@2049
   899
                              desired.freq,
icculus@2049
   900
                              device->spec.format, device->spec.channels,
icculus@2049
   901
                              device->spec.freq) < 0) {
icculus@2049
   902
            close_audio_device(device);
icculus@2049
   903
            return 0;
slouken@1895
   904
        }
icculus@2049
   905
        if (device->convert.needed) {
slouken@2060
   906
            device->convert.len = (int) (((double) desired.size) /
slouken@2060
   907
                                         device->convert.len_ratio);
icculus@2053
   908
icculus@2049
   909
            device->convert.buf =
icculus@2049
   910
                (Uint8 *) SDL_AllocAudioMem(device->convert.len *
icculus@2049
   911
                                            device->convert.len_mult);
icculus@2049
   912
            if (device->convert.buf == NULL) {
icculus@2049
   913
                close_audio_device(device);
slouken@1895
   914
                SDL_OutOfMemory();
icculus@2049
   915
                return 0;
slouken@1895
   916
            }
slouken@1895
   917
        }
slouken@1895
   918
    }
icculus@2049
   919
icculus@2049
   920
    /* Find an available device ID and store the structure... */
slouken@2060
   921
    for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
icculus@2049
   922
        if (open_devices[id] == NULL) {
icculus@2049
   923
            open_devices[id] = device;
icculus@2049
   924
            break;
icculus@2049
   925
        }
icculus@2049
   926
    }
icculus@2049
   927
icculus@2049
   928
    if (id == SDL_arraysize(open_devices)) {
icculus@2049
   929
        SDL_SetError("Too many open audio devices");
icculus@2049
   930
        close_audio_device(device);
icculus@2049
   931
        return 0;
icculus@2049
   932
    }
icculus@2049
   933
slouken@1895
   934
    /* Start the audio thread if necessary */
icculus@2049
   935
    if (!current_audio.impl.ProvidesOwnCallbackThread) {
slouken@1895
   936
        /* Start the audio thread */
icculus@2049
   937
/* !!! FIXME: this is nasty. */
slouken@1402
   938
#if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
slouken@1330
   939
#undef SDL_CreateThread
icculus@2049
   940
        device->thread = SDL_CreateThread(SDL_RunAudio, device, NULL, NULL);
slouken@1330
   941
#else
icculus@2049
   942
        device->thread = SDL_CreateThread(SDL_RunAudio, device);
slouken@1330
   943
#endif
icculus@2049
   944
        if (device->thread == NULL) {
slouken@2060
   945
            SDL_CloseAudioDevice(id + 1);
slouken@1895
   946
            SDL_SetError("Couldn't create audio thread");
icculus@2049
   947
            return 0;
icculus@2049
   948
        }
icculus@2049
   949
    }
icculus@2049
   950
slouken@2060
   951
    return id + 1;
icculus@2049
   952
}
icculus@2049
   953
icculus@2049
   954
icculus@2049
   955
int
icculus@2049
   956
SDL_OpenAudio(const SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
icculus@2049
   957
{
icculus@2049
   958
    SDL_AudioDeviceID id = 0;
icculus@2049
   959
icculus@2049
   960
    /* Start up the audio driver, if necessary. This is legacy behaviour! */
icculus@2049
   961
    if (!SDL_WasInit(SDL_INIT_AUDIO)) {
icculus@2049
   962
        if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
slouken@1895
   963
            return (-1);
slouken@1895
   964
        }
icculus@2049
   965
    }
slouken@0
   966
icculus@2049
   967
    /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
icculus@2049
   968
    if (open_devices[0] != NULL) {
icculus@2049
   969
        SDL_SetError("Audio device is already opened");
icculus@2049
   970
        return (-1);
slouken@1895
   971
    }
icculus@2049
   972
icculus@2049
   973
    id = open_audio_device(NULL, 0, desired, obtained, 1);
slouken@2060
   974
    if (id > 1) {               /* this should never happen in theory... */
icculus@2049
   975
        SDL_CloseAudioDevice(id);
slouken@2060
   976
        SDL_SetError("Internal error"); /* MUST be Device ID #1! */
icculus@2049
   977
        return (-1);
icculus@2049
   978
    }
slouken@21
   979
icculus@2049
   980
    return ((id == 0) ? -1 : 0);
icculus@2049
   981
}
slouken@21
   982
icculus@2049
   983
SDL_AudioDeviceID
icculus@2049
   984
SDL_OpenAudioDevice(const char *device, int iscapture,
slouken@2060
   985
                    const SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
icculus@2049
   986
{
icculus@2049
   987
    return open_audio_device(device, iscapture, desired, obtained, 2);
slouken@1895
   988
}
slouken@1895
   989
slouken@1895
   990
SDL_audiostatus
icculus@2049
   991
SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
slouken@1895
   992
{
icculus@2049
   993
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
   994
    SDL_audiostatus status = SDL_AUDIO_STOPPED;
icculus@2049
   995
    if (device && device->enabled) {
icculus@2049
   996
        if (device->paused) {
slouken@1895
   997
            status = SDL_AUDIO_PAUSED;
slouken@1895
   998
        } else {
slouken@1895
   999
            status = SDL_AUDIO_PLAYING;
slouken@1895
  1000
        }
slouken@1895
  1001
    }
slouken@1895
  1002
    return (status);
slouken@0
  1003
}
slouken@0
  1004
icculus@2049
  1005
icculus@2049
  1006
SDL_audiostatus
icculus@2049
  1007
SDL_GetAudioStatus(void)
icculus@2049
  1008
{
icculus@2049
  1009
    return SDL_GetAudioDeviceStatus(1);
icculus@2049
  1010
}
icculus@2049
  1011
icculus@2049
  1012
void
icculus@2049
  1013
SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
icculus@2049
  1014
{
icculus@2049
  1015
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1016
    if (device) {
icculus@2049
  1017
        device->paused = pause_on;
icculus@2049
  1018
    }
icculus@2049
  1019
}
icculus@2049
  1020
slouken@1895
  1021
void
slouken@1895
  1022
SDL_PauseAudio(int pause_on)
slouken@0
  1023
{
icculus@2049
  1024
    SDL_PauseAudioDevice(1, pause_on);
icculus@2049
  1025
}
icculus@2049
  1026
slouken@0
  1027
icculus@2049
  1028
void
icculus@2049
  1029
SDL_LockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1030
{
icculus@2049
  1031
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1032
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1033
    if (device) {
icculus@2049
  1034
        current_audio.impl.LockDevice(device);
slouken@1895
  1035
    }
slouken@0
  1036
}
slouken@0
  1037
slouken@1895
  1038
void
slouken@1895
  1039
SDL_LockAudio(void)
slouken@0
  1040
{
icculus@2049
  1041
    SDL_LockAudioDevice(1);
icculus@2049
  1042
}
slouken@0
  1043
icculus@2049
  1044
void
icculus@2049
  1045
SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1046
{
slouken@1895
  1047
    /* Obtain a lock on the mixing buffers */
icculus@2049
  1048
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1049
    if (device) {
icculus@2049
  1050
        current_audio.impl.UnlockDevice(device);
slouken@1895
  1051
    }
slouken@0
  1052
}
slouken@0
  1053
slouken@1895
  1054
void
slouken@1895
  1055
SDL_UnlockAudio(void)
slouken@0
  1056
{
icculus@2049
  1057
    SDL_UnlockAudioDevice(1);
icculus@2049
  1058
}
slouken@0
  1059
icculus@2049
  1060
void
icculus@2049
  1061
SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
icculus@2049
  1062
{
icculus@2049
  1063
    SDL_AudioDevice *device = get_audio_device(devid);
icculus@2049
  1064
    if (device) {
icculus@2049
  1065
        close_audio_device(device);
slouken@2060
  1066
        open_devices[devid - 1] = NULL;
slouken@1895
  1067
    }
slouken@0
  1068
}
slouken@0
  1069
slouken@1895
  1070
void
slouken@1895
  1071
SDL_CloseAudio(void)
slouken@0
  1072
{
icculus@2049
  1073
    SDL_CloseAudioDevice(1);
slouken@0
  1074
}
slouken@0
  1075
slouken@1895
  1076
void
slouken@1895
  1077
SDL_AudioQuit(void)
slouken@0
  1078
{
icculus@2049
  1079
    SDL_AudioDeviceID i;
icculus@2049
  1080
    for (i = 0; i < SDL_arraysize(open_devices); i++) {
icculus@2049
  1081
        SDL_CloseAudioDevice(i);
icculus@2049
  1082
    }
slouken@0
  1083
icculus@2049
  1084
    /* Free the driver data */
icculus@2049
  1085
    current_audio.impl.Deinitialize();
slouken@2060
  1086
    SDL_memset(&current_audio, '\0', sizeof(current_audio));
slouken@2060
  1087
    SDL_memset(open_devices, '\0', sizeof(open_devices));
slouken@0
  1088
}
slouken@0
  1089
icculus@1982
  1090
#define NUM_FORMATS 10
slouken@0
  1091
static int format_idx;
slouken@0
  1092
static int format_idx_sub;
icculus@1982
  1093
static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
slouken@1895
  1094
    {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1095
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
slouken@1895
  1096
    {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
icculus@1982
  1097
     AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
icculus@1982
  1098
    {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB,
icculus@1982
  1099
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1100
    {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB,
icculus@1982
  1101
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1102
    {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB,
icculus@1982
  1103
     AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
icculus@1982
  1104
    {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB,
icculus@1982
  1105
     AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1106
    {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB,
icculus@1993
  1107
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1108
    {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB,
icculus@1993
  1109
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1110
    {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB,
icculus@1993
  1111
     AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
icculus@1993
  1112
    {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB,
icculus@1993
  1113
     AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
slouken@0
  1114
};
slouken@0
  1115
icculus@1982
  1116
SDL_AudioFormat
icculus@1982
  1117
SDL_FirstAudioFormat(SDL_AudioFormat format)
slouken@0
  1118
{
slouken@1895
  1119
    for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
slouken@1895
  1120
        if (format_list[format_idx][0] == format) {
slouken@1895
  1121
            break;
slouken@1895
  1122
        }
slouken@1895
  1123
    }
slouken@1895
  1124
    format_idx_sub = 0;
slouken@1895
  1125
    return (SDL_NextAudioFormat());
slouken@0
  1126
}
slouken@0
  1127
icculus@1982
  1128
SDL_AudioFormat
slouken@1895
  1129
SDL_NextAudioFormat(void)
slouken@0
  1130
{
slouken@1895
  1131
    if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
slouken@1895
  1132
        return (0);
slouken@1895
  1133
    }
slouken@1895
  1134
    return (format_list[format_idx][format_idx_sub++]);
slouken@0
  1135
}
slouken@0
  1136
slouken@1895
  1137
void
slouken@1895
  1138
SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
slouken@0
  1139
{
slouken@1895
  1140
    switch (spec->format) {
slouken@1895
  1141
    case AUDIO_U8:
slouken@1895
  1142
        spec->silence = 0x80;
slouken@1895
  1143
        break;
slouken@1895
  1144
    default:
slouken@1895
  1145
        spec->silence = 0x00;
slouken@1895
  1146
        break;
slouken@1895
  1147
    }
icculus@2049
  1148
    spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
slouken@1895
  1149
    spec->size *= spec->channels;
slouken@1895
  1150
    spec->size *= spec->samples;
slouken@0
  1151
}
slouken@1895
  1152
icculus@2049
  1153
icculus@2049
  1154
/*
icculus@2049
  1155
 * Moved here from SDL_mixer.c, since it relies on internals of an opened
icculus@2049
  1156
 *  audio device (and is deprecated, by the way!).
icculus@2049
  1157
 */
icculus@2049
  1158
void
icculus@2049
  1159
SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
icculus@2049
  1160
{
icculus@2049
  1161
    /* Mix the user-level audio format */
icculus@2049
  1162
    SDL_AudioDevice *device = get_audio_device(1);
icculus@2049
  1163
    if (device != NULL) {
icculus@2049
  1164
        SDL_AudioFormat format;
icculus@2049
  1165
        if (device->convert.needed) {
icculus@2049
  1166
            format = device->convert.src_format;
icculus@2049
  1167
        } else {
icculus@2049
  1168
            format = device->spec.format;
icculus@2049
  1169
        }
icculus@2049
  1170
        SDL_MixAudioFormat(dst, src, format, len, volume);
icculus@2049
  1171
    }
icculus@2049
  1172
}
icculus@2049
  1173
slouken@1895
  1174
/* vi: set ts=4 sw=4 expandtab: */