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