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