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