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