src/audio/alsa/SDL_alsa_audio.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1553 63fa37538842
child 1663 11775724e3fe
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 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@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 <sys/types.h>
slouken@1662
    27
#include <signal.h>             /* For kill() */
slouken@0
    28
slouken@1361
    29
#include "SDL_timer.h"
slouken@0
    30
#include "SDL_audio.h"
slouken@1361
    31
#include "../SDL_audiomem.h"
slouken@1361
    32
#include "../SDL_audio_c.h"
slouken@0
    33
#include "SDL_alsa_audio.h"
slouken@0
    34
slouken@1361
    35
#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
slouken@865
    36
#include <dlfcn.h>
slouken@865
    37
#include "SDL_name.h"
slouken@865
    38
#include "SDL_loadso.h"
slouken@865
    39
#else
slouken@865
    40
#define SDL_NAME(X)	X
slouken@865
    41
#endif
slouken@865
    42
slouken@865
    43
slouken@0
    44
/* The tag name used by ALSA audio */
slouken@0
    45
#define DRIVER_NAME         "alsa"
slouken@0
    46
slouken@354
    47
/* The default ALSA audio driver */
slouken@765
    48
#define DEFAULT_DEVICE	"default"
slouken@0
    49
slouken@0
    50
/* Audio driver functions */
slouken@1662
    51
static int ALSA_OpenAudio (_THIS, SDL_AudioSpec * spec);
slouken@1662
    52
static void ALSA_WaitAudio (_THIS);
slouken@1662
    53
static void ALSA_PlayAudio (_THIS);
slouken@1662
    54
static Uint8 *ALSA_GetAudioBuf (_THIS);
slouken@1662
    55
static void ALSA_CloseAudio (_THIS);
slouken@0
    56
slouken@1361
    57
#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
slouken@865
    58
slouken@1361
    59
static const char *alsa_library = SDL_AUDIO_DRIVER_ALSA_DYNAMIC;
slouken@865
    60
static void *alsa_handle = NULL;
slouken@865
    61
static int alsa_loaded = 0;
slouken@865
    62
slouken@1662
    63
static int (*SDL_snd_pcm_open) (snd_pcm_t ** pcm, const char *name,
slouken@1662
    64
                                snd_pcm_stream_t stream, int mode);
slouken@1662
    65
static int (*SDL_NAME (snd_pcm_open)) (snd_pcm_t ** pcm, const char *name,
slouken@1662
    66
                                       snd_pcm_stream_t stream, int mode);
slouken@1662
    67
static int (*SDL_NAME (snd_pcm_close)) (snd_pcm_t * pcm);
slouken@1662
    68
static snd_pcm_sframes_t (*SDL_NAME (snd_pcm_writei)) (snd_pcm_t * pcm,
slouken@1662
    69
                                                       const void *buffer,
slouken@1662
    70
                                                       snd_pcm_uframes_t
slouken@1662
    71
                                                       size);
slouken@1662
    72
static int (*SDL_NAME (snd_pcm_resume)) (snd_pcm_t * pcm);
slouken@1662
    73
static int (*SDL_NAME (snd_pcm_prepare)) (snd_pcm_t * pcm);
slouken@1662
    74
static int (*SDL_NAME (snd_pcm_drain)) (snd_pcm_t * pcm);
slouken@1662
    75
static const char *(*SDL_NAME (snd_strerror)) (int errnum);
slouken@1662
    76
static size_t (*SDL_NAME (snd_pcm_hw_params_sizeof)) (void);
slouken@1662
    77
static size_t (*SDL_NAME (snd_pcm_sw_params_sizeof)) (void);
slouken@1662
    78
static int (*SDL_NAME (snd_pcm_hw_params_any)) (snd_pcm_t * pcm,
slouken@1662
    79
                                                snd_pcm_hw_params_t * params);
slouken@1662
    80
static int (*SDL_NAME (snd_pcm_hw_params_set_access)) (snd_pcm_t * pcm,
slouken@1662
    81
                                                       snd_pcm_hw_params_t *
slouken@1662
    82
                                                       params,
slouken@1662
    83
                                                       snd_pcm_access_t
slouken@1662
    84
                                                       access);
slouken@1662
    85
static int (*SDL_NAME (snd_pcm_hw_params_set_format)) (snd_pcm_t * pcm,
slouken@1662
    86
                                                       snd_pcm_hw_params_t *
slouken@1662
    87
                                                       params,
slouken@1662
    88
                                                       snd_pcm_format_t val);
slouken@1662
    89
static int (*SDL_NAME (snd_pcm_hw_params_set_channels)) (snd_pcm_t * pcm,
slouken@1662
    90
                                                         snd_pcm_hw_params_t *
slouken@1662
    91
                                                         params,
slouken@1662
    92
                                                         unsigned int val);
slouken@1662
    93
static int (*SDL_NAME (snd_pcm_hw_params_get_channels)) (const
slouken@1662
    94
                                                         snd_pcm_hw_params_t *
slouken@1662
    95
                                                         params);
slouken@1662
    96
static unsigned int (*SDL_NAME (snd_pcm_hw_params_set_rate_near)) (snd_pcm_t *
slouken@1662
    97
                                                                   pcm,
slouken@1662
    98
                                                                   snd_pcm_hw_params_t
slouken@1662
    99
                                                                   * params,
slouken@1662
   100
                                                                   unsigned
slouken@1662
   101
                                                                   int val,
slouken@1662
   102
                                                                   int *dir);
slouken@1662
   103
static snd_pcm_uframes_t (*SDL_NAME (snd_pcm_hw_params_set_period_size_near))
slouken@1662
   104
slouken@1662
   105
slouken@1662
   106
slouken@1662
   107
slouken@1662
   108
slouken@1662
   109
slouken@1662
   110
 
slouken@1662
   111
    (snd_pcm_t * pcm, snd_pcm_hw_params_t * params, snd_pcm_uframes_t val,
slouken@1662
   112
   int *dir);
slouken@1662
   113
static
slouken@1662
   114
snd_pcm_sframes_t (*SDL_NAME (snd_pcm_hw_params_get_period_size)) (const
slouken@1662
   115
                                                                   snd_pcm_hw_params_t
slouken@1662
   116
                                                                   * params);
slouken@1662
   117
static unsigned
slouken@1662
   118
    int (*SDL_NAME (snd_pcm_hw_params_set_periods_near)) (snd_pcm_t * pcm,
slouken@1662
   119
                                                          snd_pcm_hw_params_t
slouken@1662
   120
                                                          * params,
slouken@1662
   121
                                                          unsigned int val,
slouken@1662
   122
                                                          int *dir);
slouken@1662
   123
static int (*SDL_NAME (snd_pcm_hw_params_get_periods)) (snd_pcm_hw_params_t *
slouken@1662
   124
                                                        params);
slouken@1662
   125
static int (*SDL_NAME (snd_pcm_hw_params)) (snd_pcm_t * pcm,
slouken@1662
   126
                                            snd_pcm_hw_params_t * params);
slouken@1552
   127
/*
slouken@1552
   128
*/
slouken@1662
   129
static int (*SDL_NAME (snd_pcm_sw_params_current)) (snd_pcm_t * pcm,
slouken@1662
   130
                                                    snd_pcm_sw_params_t *
slouken@1662
   131
                                                    swparams);
slouken@1662
   132
static int (*SDL_NAME (snd_pcm_sw_params_set_start_threshold)) (snd_pcm_t *
slouken@1662
   133
                                                                pcm,
slouken@1662
   134
                                                                snd_pcm_sw_params_t
slouken@1662
   135
                                                                * params,
slouken@1662
   136
                                                                snd_pcm_uframes_t
slouken@1662
   137
                                                                val);
slouken@1662
   138
static int (*SDL_NAME (snd_pcm_sw_params_set_avail_min)) (snd_pcm_t * pcm,
slouken@1662
   139
                                                          snd_pcm_sw_params_t
slouken@1662
   140
                                                          * params,
slouken@1662
   141
                                                          snd_pcm_uframes_t
slouken@1662
   142
                                                          val);
slouken@1662
   143
static int (*SDL_NAME (snd_pcm_sw_params)) (snd_pcm_t * pcm,
slouken@1662
   144
                                            snd_pcm_sw_params_t * params);
slouken@1662
   145
static int (*SDL_NAME (snd_pcm_nonblock)) (snd_pcm_t * pcm, int nonblock);
slouken@865
   146
#define snd_pcm_hw_params_sizeof SDL_NAME(snd_pcm_hw_params_sizeof)
slouken@1552
   147
#define snd_pcm_sw_params_sizeof SDL_NAME(snd_pcm_sw_params_sizeof)
slouken@865
   148
icculus@1161
   149
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
slouken@1662
   150
static struct
slouken@1662
   151
{
slouken@1662
   152
    const char *name;
slouken@1662
   153
    void **func;
slouken@865
   154
} alsa_functions[] = {
slouken@1662
   155
    {
slouken@1662
   156
    "snd_pcm_open", (void **) (char *) &SDL_NAME (snd_pcm_open)}, {
slouken@1662
   157
    "snd_pcm_close", (void **) (char *) &SDL_NAME (snd_pcm_close)}, {
slouken@1662
   158
    "snd_pcm_writei", (void **) (char *) &SDL_NAME (snd_pcm_writei)}, {
slouken@1662
   159
    "snd_pcm_resume", (void **) (char *) &SDL_NAME (snd_pcm_resume)}, {
slouken@1662
   160
    "snd_pcm_prepare", (void **) (char *) &SDL_NAME (snd_pcm_prepare)}, {
slouken@1662
   161
    "snd_pcm_drain", (void **) (char *) &SDL_NAME (snd_pcm_drain)}, {
slouken@1662
   162
    "snd_strerror", (void **) (char *) &SDL_NAME (snd_strerror)}, {
slouken@1662
   163
    "snd_pcm_hw_params_sizeof",
slouken@1662
   164
            (void **) (char *) &SDL_NAME (snd_pcm_hw_params_sizeof)}, {
slouken@1662
   165
    "snd_pcm_sw_params_sizeof",
slouken@1662
   166
            (void **) (char *) &SDL_NAME (snd_pcm_sw_params_sizeof)}, {
slouken@1662
   167
    "snd_pcm_hw_params_any",
slouken@1662
   168
            (void **) (char *) &SDL_NAME (snd_pcm_hw_params_any)}, {
slouken@1662
   169
    "snd_pcm_hw_params_set_access",
slouken@1662
   170
            (void **) (char *) &SDL_NAME (snd_pcm_hw_params_set_access)}, {
slouken@1662
   171
    "snd_pcm_hw_params_set_format",
slouken@1662
   172
            (void **) (char *) &SDL_NAME (snd_pcm_hw_params_set_format)}, {
slouken@1662
   173
    "snd_pcm_hw_params_set_channels",
slouken@1662
   174
            (void **) (char *) &SDL_NAME (snd_pcm_hw_params_set_channels)}, {
slouken@1662
   175
    "snd_pcm_hw_params_get_channels",
slouken@1662
   176
            (void **) (char *) &SDL_NAME (snd_pcm_hw_params_get_channels)}, {
slouken@1662
   177
    "snd_pcm_hw_params_set_rate_near",
slouken@1662
   178
            (void **) (char *) &SDL_NAME (snd_pcm_hw_params_set_rate_near)}, {
slouken@1662
   179
        "snd_pcm_hw_params_set_period_size_near", (void **) (char *)
slouken@1662
   180
    &SDL_NAME (snd_pcm_hw_params_set_period_size_near)}, {
slouken@1662
   181
    "snd_pcm_hw_params_get_period_size",
slouken@1662
   182
            (void **) (char *) &SDL_NAME (snd_pcm_hw_params_get_period_size)},
slouken@1662
   183
    {
slouken@1662
   184
        "snd_pcm_hw_params_set_periods_near", (void **) (char *)
slouken@1662
   185
    &SDL_NAME (snd_pcm_hw_params_set_periods_near)}, {
slouken@1662
   186
    "snd_pcm_hw_params_get_periods",
slouken@1662
   187
            (void **) (char *) &SDL_NAME (snd_pcm_hw_params_get_periods)}, {
slouken@1662
   188
    "snd_pcm_hw_params", (void **) (char *) &SDL_NAME (snd_pcm_hw_params)}, {
slouken@1662
   189
    "snd_pcm_sw_params_current",
slouken@1662
   190
            (void **) (char *) &SDL_NAME (snd_pcm_sw_params_current)}, {
slouken@1662
   191
        "snd_pcm_sw_params_set_start_threshold", (void **) (char *)
slouken@1662
   192
    &SDL_NAME (snd_pcm_sw_params_set_start_threshold)}, {
slouken@1662
   193
    "snd_pcm_sw_params_set_avail_min",
slouken@1662
   194
            (void **) (char *) &SDL_NAME (snd_pcm_sw_params_set_avail_min)}, {
slouken@1662
   195
    "snd_pcm_sw_params", (void **) (char *) &SDL_NAME (snd_pcm_sw_params)}, {
slouken@1662
   196
"snd_pcm_nonblock", (void **) (char *) &SDL_NAME (snd_pcm_nonblock)},};
slouken@865
   197
slouken@1662
   198
static void
slouken@1662
   199
UnloadALSALibrary (void)
slouken@1662
   200
{
slouken@1662
   201
    if (alsa_loaded) {
slouken@865
   202
/*		SDL_UnloadObject(alsa_handle);*/
slouken@1662
   203
        dlclose (alsa_handle);
slouken@1662
   204
        alsa_handle = NULL;
slouken@1662
   205
        alsa_loaded = 0;
slouken@1662
   206
    }
slouken@865
   207
}
slouken@865
   208
slouken@1662
   209
static int
slouken@1662
   210
LoadALSALibrary (void)
slouken@1662
   211
{
slouken@1662
   212
    int i, retval = -1;
slouken@865
   213
slouken@865
   214
/*	alsa_handle = SDL_LoadObject(alsa_library);*/
slouken@1662
   215
    alsa_handle = dlopen (alsa_library, RTLD_NOW);
slouken@1662
   216
    if (alsa_handle) {
slouken@1662
   217
        alsa_loaded = 1;
slouken@1662
   218
        retval = 0;
slouken@1662
   219
        for (i = 0; i < SDL_arraysize (alsa_functions); i++) {
slouken@865
   220
/*			*alsa_functions[i].func = SDL_LoadFunction(alsa_handle,alsa_functions[i].name);*/
slouken@1361
   221
#if HAVE_DLVSYM
slouken@1662
   222
            *alsa_functions[i].func =
slouken@1662
   223
                dlvsym (alsa_handle, alsa_functions[i].name, "ALSA_0.9");
slouken@1662
   224
            if (!*alsa_functions[i].func)
slouken@939
   225
#endif
slouken@1662
   226
                *alsa_functions[i].func =
slouken@1662
   227
                    dlsym (alsa_handle, alsa_functions[i].name);
slouken@1662
   228
            if (!*alsa_functions[i].func) {
slouken@1662
   229
                retval = -1;
slouken@1662
   230
                UnloadALSALibrary ();
slouken@1662
   231
                break;
slouken@1662
   232
            }
slouken@1662
   233
        }
slouken@1662
   234
    }
slouken@1662
   235
    return retval;
slouken@865
   236
}
slouken@865
   237
slouken@865
   238
#else
slouken@865
   239
slouken@1662
   240
static void
slouken@1662
   241
UnloadALSALibrary (void)
slouken@1662
   242
{
slouken@1662
   243
    return;
slouken@865
   244
}
slouken@865
   245
slouken@1662
   246
static int
slouken@1662
   247
LoadALSALibrary (void)
slouken@1662
   248
{
slouken@1662
   249
    return 0;
slouken@865
   250
}
slouken@865
   251
slouken@1361
   252
#endif /* SDL_AUDIO_DRIVER_ALSA_DYNAMIC */
slouken@865
   253
slouken@1662
   254
static const char *
slouken@1662
   255
get_audio_device (int channels)
slouken@0
   256
{
slouken@1662
   257
    const char *device;
slouken@1662
   258
slouken@1662
   259
    device = SDL_getenv ("AUDIODEV");   /* Is there a standard variable name? */
slouken@1662
   260
    if (device == NULL) {
slouken@1662
   261
        if (channels == 6)
slouken@1662
   262
            device = "surround51";
slouken@1662
   263
        else if (channels == 4)
slouken@1662
   264
            device = "surround40";
slouken@1662
   265
        else
slouken@1662
   266
            device = DEFAULT_DEVICE;
slouken@1662
   267
    }
slouken@1662
   268
    return device;
slouken@0
   269
}
slouken@0
   270
slouken@0
   271
/* Audio driver bootstrap functions */
slouken@0
   272
slouken@1662
   273
static int
slouken@1662
   274
Audio_Available (void)
slouken@0
   275
{
slouken@1662
   276
    int available;
slouken@1662
   277
    int status;
slouken@1662
   278
    snd_pcm_t *handle;
slouken@0
   279
slouken@1662
   280
    available = 0;
slouken@1662
   281
    if (LoadALSALibrary () < 0) {
slouken@1662
   282
        return available;
slouken@1662
   283
    }
slouken@1662
   284
    status =
slouken@1662
   285
        SDL_NAME (snd_pcm_open) (&handle, get_audio_device (2),
slouken@1662
   286
                                 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
slouken@1662
   287
    if (status >= 0) {
slouken@1662
   288
        available = 1;
slouken@1662
   289
        SDL_NAME (snd_pcm_close) (handle);
slouken@1662
   290
    }
slouken@1662
   291
    UnloadALSALibrary ();
slouken@1662
   292
    return (available);
slouken@0
   293
}
slouken@0
   294
slouken@1662
   295
static void
slouken@1662
   296
Audio_DeleteDevice (SDL_AudioDevice * device)
slouken@0
   297
{
slouken@1662
   298
    SDL_free (device->hidden);
slouken@1662
   299
    SDL_free (device);
slouken@1662
   300
    UnloadALSALibrary ();
slouken@0
   301
}
slouken@0
   302
slouken@1662
   303
static SDL_AudioDevice *
slouken@1662
   304
Audio_CreateDevice (int devindex)
slouken@0
   305
{
slouken@1662
   306
    SDL_AudioDevice *this;
slouken@0
   307
slouken@1662
   308
    /* Initialize all variables that we clean on shutdown */
slouken@1662
   309
    LoadALSALibrary ();
slouken@1662
   310
    this = (SDL_AudioDevice *) SDL_malloc (sizeof (SDL_AudioDevice));
slouken@1662
   311
    if (this) {
slouken@1662
   312
        SDL_memset (this, 0, (sizeof *this));
slouken@1662
   313
        this->hidden = (struct SDL_PrivateAudioData *)
slouken@1662
   314
            SDL_malloc ((sizeof *this->hidden));
slouken@1662
   315
    }
slouken@1662
   316
    if ((this == NULL) || (this->hidden == NULL)) {
slouken@1662
   317
        SDL_OutOfMemory ();
slouken@1662
   318
        if (this) {
slouken@1662
   319
            SDL_free (this);
slouken@1662
   320
        }
slouken@1662
   321
        return (0);
slouken@1662
   322
    }
slouken@1662
   323
    SDL_memset (this->hidden, 0, (sizeof *this->hidden));
slouken@0
   324
slouken@1662
   325
    /* Set the function pointers */
slouken@1662
   326
    this->OpenAudio = ALSA_OpenAudio;
slouken@1662
   327
    this->WaitAudio = ALSA_WaitAudio;
slouken@1662
   328
    this->PlayAudio = ALSA_PlayAudio;
slouken@1662
   329
    this->GetAudioBuf = ALSA_GetAudioBuf;
slouken@1662
   330
    this->CloseAudio = ALSA_CloseAudio;
slouken@0
   331
slouken@1662
   332
    this->free = Audio_DeleteDevice;
slouken@0
   333
slouken@1662
   334
    return this;
slouken@0
   335
}
slouken@0
   336
slouken@0
   337
AudioBootStrap ALSA_bootstrap = {
slouken@1662
   338
    DRIVER_NAME, "ALSA 0.9 PCM audio",
slouken@1662
   339
    Audio_Available, Audio_CreateDevice
slouken@0
   340
};
slouken@0
   341
slouken@0
   342
/* This function waits until it is possible to write a full sound buffer */
slouken@1662
   343
static void
slouken@1662
   344
ALSA_WaitAudio (_THIS)
slouken@0
   345
{
slouken@1662
   346
    /* Check to see if the thread-parent process is still alive */
slouken@1662
   347
    {
slouken@1662
   348
        static int cnt = 0;
slouken@1662
   349
        /* Note that this only works with thread implementations 
slouken@1662
   350
           that use a different process id for each thread.
slouken@1662
   351
         */
slouken@1662
   352
        if (parent && (((++cnt) % 10) == 0)) {  /* Check every 10 loops */
slouken@1662
   353
            if (kill (parent, 0) < 0) {
slouken@1662
   354
                this->enabled = 0;
slouken@1662
   355
            }
slouken@1662
   356
        }
slouken@1662
   357
    }
slouken@0
   358
}
slouken@0
   359
slouken@1662
   360
static void
slouken@1662
   361
ALSA_PlayAudio (_THIS)
slouken@0
   362
{
slouken@1662
   363
    int status;
slouken@1662
   364
    int sample_len;
slouken@1662
   365
    signed short *sample_buf;
slouken@765
   366
slouken@1662
   367
    sample_len = this->spec.samples;
slouken@1662
   368
    sample_buf = (signed short *) mixbuf;
slouken@1662
   369
    while (sample_len > 0) {
slouken@1662
   370
        status =
slouken@1662
   371
            SDL_NAME (snd_pcm_writei) (pcm_handle, sample_buf, sample_len);
slouken@1662
   372
        if (status < 0) {
slouken@1662
   373
            if (status == -EAGAIN) {
slouken@1662
   374
                SDL_Delay (1);
slouken@1662
   375
                continue;
slouken@1662
   376
            }
slouken@1662
   377
            if (status == -ESTRPIPE) {
slouken@1662
   378
                do {
slouken@1662
   379
                    SDL_Delay (1);
slouken@1662
   380
                    status = SDL_NAME (snd_pcm_resume) (pcm_handle);
slouken@1662
   381
                }
slouken@1662
   382
                while (status == -EAGAIN);
slouken@1662
   383
            }
slouken@1662
   384
            if (status < 0) {
slouken@1662
   385
                status = SDL_NAME (snd_pcm_prepare) (pcm_handle);
slouken@1662
   386
            }
slouken@1662
   387
            if (status < 0) {
slouken@1662
   388
                /* Hmm, not much we can do - abort */
slouken@1662
   389
                this->enabled = 0;
slouken@1662
   390
                return;
slouken@1662
   391
            }
slouken@1662
   392
            continue;
slouken@1662
   393
        }
slouken@1662
   394
        sample_buf += status * this->spec.channels;
slouken@1662
   395
        sample_len -= status;
slouken@1662
   396
    }
slouken@0
   397
}
slouken@0
   398
slouken@1662
   399
static Uint8 *
slouken@1662
   400
ALSA_GetAudioBuf (_THIS)
slouken@0
   401
{
slouken@1662
   402
    return (mixbuf);
slouken@354
   403
}
slouken@0
   404
slouken@1662
   405
static void
slouken@1662
   406
ALSA_CloseAudio (_THIS)
slouken@354
   407
{
slouken@1662
   408
    if (mixbuf != NULL) {
slouken@1662
   409
        SDL_FreeAudioMem (mixbuf);
slouken@1662
   410
        mixbuf = NULL;
slouken@1662
   411
    }
slouken@1662
   412
    if (pcm_handle) {
slouken@1662
   413
        SDL_NAME (snd_pcm_drain) (pcm_handle);
slouken@1662
   414
        SDL_NAME (snd_pcm_close) (pcm_handle);
slouken@1662
   415
        pcm_handle = NULL;
slouken@1662
   416
    }
slouken@354
   417
}
slouken@0
   418
slouken@1662
   419
static int
slouken@1662
   420
ALSA_OpenAudio (_THIS, SDL_AudioSpec * spec)
slouken@354
   421
{
slouken@1662
   422
    int status;
slouken@1662
   423
    snd_pcm_hw_params_t *hwparams;
slouken@1662
   424
    snd_pcm_sw_params_t *swparams;
slouken@1662
   425
    snd_pcm_format_t format;
slouken@1662
   426
    snd_pcm_uframes_t frames;
slouken@1662
   427
    Uint16 test_format;
slouken@0
   428
slouken@1662
   429
    /* Open the audio device */
slouken@1662
   430
    /* Name of device should depend on # channels in spec */
slouken@1662
   431
    status =
slouken@1662
   432
        SDL_NAME (snd_pcm_open) (&pcm_handle,
slouken@1662
   433
                                 get_audio_device (spec->channels),
slouken@1662
   434
                                 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
slouken@942
   435
slouken@1662
   436
    if (status < 0) {
slouken@1662
   437
        SDL_SetError ("Couldn't open audio device: %s",
slouken@1662
   438
                      SDL_NAME (snd_strerror) (status));
slouken@1662
   439
        return (-1);
slouken@1662
   440
    }
slouken@0
   441
slouken@1662
   442
    /* Figure out what the hardware is capable of */
slouken@1662
   443
    snd_pcm_hw_params_alloca (&hwparams);
slouken@1662
   444
    status = SDL_NAME (snd_pcm_hw_params_any) (pcm_handle, hwparams);
slouken@1662
   445
    if (status < 0) {
slouken@1662
   446
        SDL_SetError ("Couldn't get hardware config: %s",
slouken@1662
   447
                      SDL_NAME (snd_strerror) (status));
slouken@1662
   448
        ALSA_CloseAudio (this);
slouken@1662
   449
        return (-1);
slouken@1662
   450
    }
slouken@0
   451
slouken@1662
   452
    /* SDL only uses interleaved sample output */
slouken@1662
   453
    status =
slouken@1662
   454
        SDL_NAME (snd_pcm_hw_params_set_access) (pcm_handle, hwparams,
slouken@1662
   455
                                                 SND_PCM_ACCESS_RW_INTERLEAVED);
slouken@1662
   456
    if (status < 0) {
slouken@1662
   457
        SDL_SetError ("Couldn't set interleaved access: %s",
slouken@1662
   458
                      SDL_NAME (snd_strerror) (status));
slouken@1662
   459
        ALSA_CloseAudio (this);
slouken@1662
   460
        return (-1);
slouken@1662
   461
    }
slouken@0
   462
slouken@1662
   463
    /* Try for a closest match on audio format */
slouken@1662
   464
    status = -1;
slouken@1662
   465
    for (test_format = SDL_FirstAudioFormat (spec->format);
slouken@1662
   466
         test_format && (status < 0);) {
slouken@1662
   467
        switch (test_format) {
slouken@1662
   468
        case AUDIO_U8:
slouken@1662
   469
            format = SND_PCM_FORMAT_U8;
slouken@1662
   470
            break;
slouken@1662
   471
        case AUDIO_S8:
slouken@1662
   472
            format = SND_PCM_FORMAT_S8;
slouken@1662
   473
            break;
slouken@1662
   474
        case AUDIO_S16LSB:
slouken@1662
   475
            format = SND_PCM_FORMAT_S16_LE;
slouken@1662
   476
            break;
slouken@1662
   477
        case AUDIO_S16MSB:
slouken@1662
   478
            format = SND_PCM_FORMAT_S16_BE;
slouken@1662
   479
            break;
slouken@1662
   480
        case AUDIO_U16LSB:
slouken@1662
   481
            format = SND_PCM_FORMAT_U16_LE;
slouken@1662
   482
            break;
slouken@1662
   483
        case AUDIO_U16MSB:
slouken@1662
   484
            format = SND_PCM_FORMAT_U16_BE;
slouken@1662
   485
            break;
slouken@1662
   486
        default:
slouken@1662
   487
            format = 0;
slouken@1662
   488
            break;
slouken@1662
   489
        }
slouken@1662
   490
        if (format != 0) {
slouken@1662
   491
            status =
slouken@1662
   492
                SDL_NAME (snd_pcm_hw_params_set_format) (pcm_handle,
slouken@1662
   493
                                                         hwparams, format);
slouken@1662
   494
        }
slouken@1662
   495
        if (status < 0) {
slouken@1662
   496
            test_format = SDL_NextAudioFormat ();
slouken@1662
   497
        }
slouken@1662
   498
    }
slouken@1662
   499
    if (status < 0) {
slouken@1662
   500
        SDL_SetError ("Couldn't find any hardware audio formats");
slouken@1662
   501
        ALSA_CloseAudio (this);
slouken@1662
   502
        return (-1);
slouken@1662
   503
    }
slouken@1662
   504
    spec->format = test_format;
slouken@0
   505
slouken@1662
   506
    /* Set the number of channels */
slouken@1662
   507
    status =
slouken@1662
   508
        SDL_NAME (snd_pcm_hw_params_set_channels) (pcm_handle, hwparams,
slouken@1662
   509
                                                   spec->channels);
slouken@1662
   510
    if (status < 0) {
slouken@1662
   511
        status = SDL_NAME (snd_pcm_hw_params_get_channels) (hwparams);
slouken@1662
   512
        if ((status <= 0) || (status > 2)) {
slouken@1662
   513
            SDL_SetError ("Couldn't set audio channels");
slouken@1662
   514
            ALSA_CloseAudio (this);
slouken@1662
   515
            return (-1);
slouken@1662
   516
        }
slouken@1662
   517
        spec->channels = status;
slouken@1662
   518
    }
slouken@0
   519
slouken@1662
   520
    /* Set the audio rate */
slouken@1662
   521
    status =
slouken@1662
   522
        SDL_NAME (snd_pcm_hw_params_set_rate_near) (pcm_handle, hwparams,
slouken@1662
   523
                                                    spec->freq, NULL);
slouken@1662
   524
    if (status < 0) {
slouken@1662
   525
        SDL_SetError ("Couldn't set audio frequency: %s",
slouken@1662
   526
                      SDL_NAME (snd_strerror) (status));
slouken@1662
   527
        ALSA_CloseAudio (this);
slouken@1662
   528
        return (-1);
slouken@1662
   529
    }
slouken@1662
   530
    spec->freq = status;
slouken@0
   531
slouken@1662
   532
    /* Set the buffer size, in samples */
slouken@1662
   533
    frames = spec->samples;
slouken@1662
   534
    frames =
slouken@1662
   535
        SDL_NAME (snd_pcm_hw_params_set_period_size_near) (pcm_handle,
slouken@1662
   536
                                                           hwparams, frames,
slouken@1662
   537
                                                           NULL);
slouken@1662
   538
    spec->samples = frames;
slouken@1662
   539
    SDL_NAME (snd_pcm_hw_params_set_periods_near) (pcm_handle, hwparams, 2,
slouken@1662
   540
                                                   NULL);
slouken@354
   541
slouken@1662
   542
    /* "set" the hardware with the desired parameters */
slouken@1662
   543
    status = SDL_NAME (snd_pcm_hw_params) (pcm_handle, hwparams);
slouken@1662
   544
    if (status < 0) {
slouken@1662
   545
        SDL_SetError ("Couldn't set hardware audio parameters: %s",
slouken@1662
   546
                      SDL_NAME (snd_strerror) (status));
slouken@1662
   547
        ALSA_CloseAudio (this);
slouken@1662
   548
        return (-1);
slouken@1662
   549
    }
slouken@1552
   550
slouken@1553
   551
/* This is useful for debugging... */
slouken@1552
   552
/*
slouken@1553
   553
{ snd_pcm_sframes_t bufsize; int fragments;
slouken@1552
   554
   bufsize = SDL_NAME(snd_pcm_hw_params_get_period_size)(hwparams);
slouken@1552
   555
   fragments = SDL_NAME(snd_pcm_hw_params_get_periods)(hwparams);
slouken@1552
   556
slouken@1552
   557
   fprintf(stderr, "ALSA: bufsize = %ld, fragments = %d\n", bufsize, fragments);
slouken@1552
   558
}
slouken@1552
   559
*/
slouken@1552
   560
slouken@1662
   561
    /* Set the software parameters */
slouken@1662
   562
    snd_pcm_sw_params_alloca (&swparams);
slouken@1662
   563
    status = SDL_NAME (snd_pcm_sw_params_current) (pcm_handle, swparams);
slouken@1662
   564
    if (status < 0) {
slouken@1662
   565
        SDL_SetError ("Couldn't get software config: %s",
slouken@1662
   566
                      SDL_NAME (snd_strerror) (status));
slouken@1662
   567
        ALSA_CloseAudio (this);
slouken@1662
   568
        return (-1);
slouken@1662
   569
    }
slouken@1662
   570
    status =
slouken@1662
   571
        SDL_NAME (snd_pcm_sw_params_set_start_threshold) (pcm_handle,
slouken@1662
   572
                                                          swparams, 0);
slouken@1662
   573
    if (status < 0) {
slouken@1662
   574
        SDL_SetError ("Couldn't set start threshold: %s",
slouken@1662
   575
                      SDL_NAME (snd_strerror) (status));
slouken@1662
   576
        ALSA_CloseAudio (this);
slouken@1662
   577
        return (-1);
slouken@1662
   578
    }
slouken@1662
   579
    status =
slouken@1662
   580
        SDL_NAME (snd_pcm_sw_params_set_avail_min) (pcm_handle, swparams,
slouken@1662
   581
                                                    frames);
slouken@1662
   582
    if (status < 0) {
slouken@1662
   583
        SDL_SetError ("Couldn't set avail min: %s",
slouken@1662
   584
                      SDL_NAME (snd_strerror) (status));
slouken@1662
   585
        ALSA_CloseAudio (this);
slouken@1662
   586
        return (-1);
slouken@1662
   587
    }
slouken@1662
   588
    status = SDL_NAME (snd_pcm_sw_params) (pcm_handle, swparams);
slouken@1662
   589
    if (status < 0) {
slouken@1662
   590
        SDL_SetError ("Couldn't set software audio parameters: %s",
slouken@1662
   591
                      SDL_NAME (snd_strerror) (status));
slouken@1662
   592
        ALSA_CloseAudio (this);
slouken@1662
   593
        return (-1);
slouken@1662
   594
    }
slouken@0
   595
slouken@1662
   596
    /* Calculate the final parameters for this audio specification */
slouken@1662
   597
    SDL_CalculateAudioSpec (spec);
slouken@0
   598
slouken@1662
   599
    /* Allocate mixing buffer */
slouken@1662
   600
    mixlen = spec->size;
slouken@1662
   601
    mixbuf = (Uint8 *) SDL_AllocAudioMem (mixlen);
slouken@1662
   602
    if (mixbuf == NULL) {
slouken@1662
   603
        ALSA_CloseAudio (this);
slouken@1662
   604
        return (-1);
slouken@1662
   605
    }
slouken@1662
   606
    SDL_memset (mixbuf, spec->silence, spec->size);
slouken@0
   607
slouken@1662
   608
    /* Get the parent process id (we're the parent of the audio thread) */
slouken@1662
   609
    parent = getpid ();
slouken@0
   610
slouken@1662
   611
    /* Switch to blocking mode for playback */
slouken@1662
   612
    SDL_NAME (snd_pcm_nonblock) (pcm_handle, 0);
slouken@765
   613
slouken@1662
   614
    /* We're ready to rock and roll. :-) */
slouken@1662
   615
    return (0);
slouken@0
   616
}
slouken@1662
   617
slouken@1662
   618
/* vi: set ts=4 sw=4 expandtab: */