src/audio/alsa/SDL_alsa_audio.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 31 Mar 2013 13:05:40 -0400
changeset 7038 7f22b9ba218f
parent 6885 700f1b25f77f
child 7191 75360622e65f
permissions -rw-r--r--
Changed audio subsystem's OpenDevice interface to return -1 on error. This lets us 'return SDL_SetError("whatever");' on one line. Fixes Bugzilla #1778.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
     7
  arising from the use of this software.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    11
  freely, subject to the following restrictions:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5315
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
*/
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1379
diff changeset
    21
#include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5622
diff changeset
    23
#if SDL_AUDIO_DRIVER_ALSA
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5622
diff changeset
    24
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
/* Allow access to a raw mixing buffer */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
#include <sys/types.h>
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
    28
#include <signal.h>             /* For kill() */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    29
#include <errno.h>
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    30
#include <string.h>
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    32
#include "SDL_timer.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
#include "SDL_audio.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    34
#include "../SDL_audiomem.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    35
#include "../SDL_audio_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    36
#include "SDL_alsa_audio.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    37
6046
3b33b82cdbac Fixed some preprocessor mistakes introduced in iOS project cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
    38
#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    39
#include "SDL_loadso.h"
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    40
#endif
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    41
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    42
static int (*ALSA_snd_pcm_open)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    43
  (snd_pcm_t **, const char *, snd_pcm_stream_t, int);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    44
static int (*ALSA_snd_pcm_close) (snd_pcm_t * pcm);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    45
static snd_pcm_sframes_t(*ALSA_snd_pcm_writei)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    46
  (snd_pcm_t *, const void *, snd_pcm_uframes_t);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    47
static int (*ALSA_snd_pcm_recover) (snd_pcm_t *, int, int);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    48
static int (*ALSA_snd_pcm_prepare) (snd_pcm_t *);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    49
static int (*ALSA_snd_pcm_drain) (snd_pcm_t *);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    50
static const char *(*ALSA_snd_strerror) (int);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    51
static size_t(*ALSA_snd_pcm_hw_params_sizeof) (void);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    52
static size_t(*ALSA_snd_pcm_sw_params_sizeof) (void);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    53
static void (*ALSA_snd_pcm_hw_params_copy)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    54
  (snd_pcm_hw_params_t *, const snd_pcm_hw_params_t *);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    55
static int (*ALSA_snd_pcm_hw_params_any) (snd_pcm_t *, snd_pcm_hw_params_t *);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    56
static int (*ALSA_snd_pcm_hw_params_set_access)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    57
  (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_access_t);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    58
static int (*ALSA_snd_pcm_hw_params_set_format)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    59
  (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_format_t);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    60
static int (*ALSA_snd_pcm_hw_params_set_channels)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    61
  (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    62
static int (*ALSA_snd_pcm_hw_params_get_channels)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    63
  (const snd_pcm_hw_params_t *, unsigned int *);
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    64
static int (*ALSA_snd_pcm_hw_params_set_rate_near)
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    65
  (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    66
static int (*ALSA_snd_pcm_hw_params_set_period_size_near)
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    67
  (snd_pcm_t *, snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    68
static int (*ALSA_snd_pcm_hw_params_get_period_size)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    69
  (const snd_pcm_hw_params_t *, snd_pcm_uframes_t *, int *);
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    70
static int (*ALSA_snd_pcm_hw_params_set_periods_near)
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    71
  (snd_pcm_t *, snd_pcm_hw_params_t *, unsigned int *, int *);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    72
static int (*ALSA_snd_pcm_hw_params_get_periods)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    73
  (const snd_pcm_hw_params_t *, unsigned int *, int *);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    74
static int (*ALSA_snd_pcm_hw_params_set_buffer_size_near)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    75
  (snd_pcm_t *pcm, snd_pcm_hw_params_t *, snd_pcm_uframes_t *);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    76
static int (*ALSA_snd_pcm_hw_params_get_buffer_size)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    77
  (const snd_pcm_hw_params_t *, snd_pcm_uframes_t *);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    78
static int (*ALSA_snd_pcm_hw_params) (snd_pcm_t *, snd_pcm_hw_params_t *);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    79
static int (*ALSA_snd_pcm_sw_params_current) (snd_pcm_t *,
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    80
                                              snd_pcm_sw_params_t *);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    81
static int (*ALSA_snd_pcm_sw_params_set_start_threshold)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    82
  (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    83
static int (*ALSA_snd_pcm_sw_params) (snd_pcm_t *, snd_pcm_sw_params_t *);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    84
static int (*ALSA_snd_pcm_nonblock) (snd_pcm_t *, int);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
    85
static int (*ALSA_snd_pcm_wait)(snd_pcm_t *, int);
5622
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
    86
static int (*ALSA_snd_pcm_sw_params_set_avail_min)
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
    87
  (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    88
6046
3b33b82cdbac Fixed some preprocessor mistakes introduced in iOS project cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
    89
#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
5315
192f2d605339 Fixed bug #1085 (Jump to NULL function pointer on ALSA_OpenDevice)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    90
#define snd_pcm_hw_params_sizeof ALSA_snd_pcm_hw_params_sizeof
192f2d605339 Fixed bug #1085 (Jump to NULL function pointer on ALSA_OpenDevice)
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    91
#define snd_pcm_sw_params_sizeof ALSA_snd_pcm_sw_params_sizeof
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    92
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    93
static const char *alsa_library = SDL_AUDIO_DRIVER_ALSA_DYNAMIC;
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    94
static void *alsa_handle = NULL;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    95
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    96
static int
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    97
load_alsa_sym(const char *fn, void **addr)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    98
{
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
    99
    *addr = SDL_LoadFunction(alsa_handle, fn);
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   100
    if (*addr == NULL) {
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   101
        /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   102
        return 0;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   103
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   104
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   105
    return 1;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   106
}
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   107
1161
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
   108
/* cast funcs to char* first, to please GCC's strict aliasing rules. */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   109
#define SDL_ALSA_SYM(x) \
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   110
    if (!load_alsa_sym(#x, (void **) (char *) &ALSA_##x)) return -1
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   111
#else
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   112
#define SDL_ALSA_SYM(x) ALSA_##x = x
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   113
#endif
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   114
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   115
static int
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   116
load_alsa_syms(void)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   117
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   118
    SDL_ALSA_SYM(snd_pcm_open);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   119
    SDL_ALSA_SYM(snd_pcm_close);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   120
    SDL_ALSA_SYM(snd_pcm_writei);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   121
    SDL_ALSA_SYM(snd_pcm_recover);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   122
    SDL_ALSA_SYM(snd_pcm_prepare);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   123
    SDL_ALSA_SYM(snd_pcm_drain);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   124
    SDL_ALSA_SYM(snd_strerror);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   125
    SDL_ALSA_SYM(snd_pcm_hw_params_sizeof);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   126
    SDL_ALSA_SYM(snd_pcm_sw_params_sizeof);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   127
    SDL_ALSA_SYM(snd_pcm_hw_params_copy);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   128
    SDL_ALSA_SYM(snd_pcm_hw_params_any);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   129
    SDL_ALSA_SYM(snd_pcm_hw_params_set_access);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   130
    SDL_ALSA_SYM(snd_pcm_hw_params_set_format);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   131
    SDL_ALSA_SYM(snd_pcm_hw_params_set_channels);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   132
    SDL_ALSA_SYM(snd_pcm_hw_params_get_channels);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   133
    SDL_ALSA_SYM(snd_pcm_hw_params_set_rate_near);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   134
    SDL_ALSA_SYM(snd_pcm_hw_params_set_period_size_near);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   135
    SDL_ALSA_SYM(snd_pcm_hw_params_get_period_size);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   136
    SDL_ALSA_SYM(snd_pcm_hw_params_set_periods_near);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   137
    SDL_ALSA_SYM(snd_pcm_hw_params_get_periods);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   138
    SDL_ALSA_SYM(snd_pcm_hw_params_set_buffer_size_near);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   139
    SDL_ALSA_SYM(snd_pcm_hw_params_get_buffer_size);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   140
    SDL_ALSA_SYM(snd_pcm_hw_params);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   141
    SDL_ALSA_SYM(snd_pcm_sw_params_current);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   142
    SDL_ALSA_SYM(snd_pcm_sw_params_set_start_threshold);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   143
    SDL_ALSA_SYM(snd_pcm_sw_params);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   144
    SDL_ALSA_SYM(snd_pcm_nonblock);
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   145
    SDL_ALSA_SYM(snd_pcm_wait);
5622
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
   146
    SDL_ALSA_SYM(snd_pcm_sw_params_set_avail_min);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   147
    return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   148
}
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   149
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   150
#undef SDL_ALSA_SYM
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   151
6046
3b33b82cdbac Fixed some preprocessor mistakes introduced in iOS project cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
   152
#ifdef SDL_AUDIO_DRIVER_ALSA_DYNAMIC
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   153
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   154
static void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   155
UnloadALSALibrary(void)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   156
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   157
    if (alsa_handle != NULL) {
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   158
		SDL_UnloadObject(alsa_handle);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   159
        alsa_handle = NULL;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   160
    }
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   161
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   162
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   163
static int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   164
LoadALSALibrary(void)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   165
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   166
    int retval = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   167
    if (alsa_handle == NULL) {
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   168
        alsa_handle = SDL_LoadObject(alsa_library);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   169
        if (alsa_handle == NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   170
            retval = -1;
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   171
            /* Don't call SDL_SetError(): SDL_LoadObject already did. */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   172
        } else {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   173
            retval = load_alsa_syms();
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   174
            if (retval < 0) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   175
                UnloadALSALibrary();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   176
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   177
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   178
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   179
    return retval;
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   180
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   181
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   182
#else
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   183
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   184
static void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   185
UnloadALSALibrary(void)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   186
{
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   187
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   188
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   189
static int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   190
LoadALSALibrary(void)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   191
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   192
    load_alsa_syms();
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   193
    return 0;
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   194
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   195
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   196
#endif /* SDL_AUDIO_DRIVER_ALSA_DYNAMIC */
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   197
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   198
static const char *
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   199
get_audio_device(int channels)
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   200
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   201
    const char *device;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   202
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   203
    device = SDL_getenv("AUDIODEV");    /* Is there a standard variable name? */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   204
    if (device == NULL) {
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   205
        switch (channels) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   206
        case 6:
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   207
            device = "plug:surround51";
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   208
            break;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   209
        case 4:
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   210
            device = "plug:surround40";
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   211
            break;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   212
        default:
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   213
            device = "default";
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   214
            break;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   215
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   216
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   217
    return device;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   218
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   219
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   220
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   221
/* This function waits until it is possible to write a full sound buffer */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   222
static void
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   223
ALSA_WaitDevice(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   224
{
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   225
    /* We're in blocking mode, so there's nothing to do here */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   226
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   227
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   228
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   229
/* !!! FIXME: is there a channel swizzler in alsalib instead? */
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   230
/*
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   231
 * http://bugzilla.libsdl.org/show_bug.cgi?id=110
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   232
 * "For Linux ALSA, this is FL-FR-RL-RR-C-LFE
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   233
 *  and for Windows DirectX [and CoreAudio], this is FL-FR-C-LFE-RL-RR"
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   234
 */
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   235
#define SWIZ6(T) \
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   236
    T *ptr = (T *) this->hidden->mixbuf; \
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   237
    Uint32 i; \
3704
9bc9ff36eb8f Merged r5549:5550 from branches/SDL-1.2: ALSA 6-channel swizzle fix.
Ryan C. Gordon <icculus@icculus.org>
parents: 3699
diff changeset
   238
    for (i = 0; i < this->spec.samples; i++, ptr += 6) { \
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   239
        T tmp; \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   240
        tmp = ptr[2]; ptr[2] = ptr[4]; ptr[4] = tmp; \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   241
        tmp = ptr[3]; ptr[3] = ptr[5]; ptr[5] = tmp; \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   242
    }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   243
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   244
static __inline__ void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   245
swizzle_alsa_channels_6_64bit(_THIS)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   246
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   247
    SWIZ6(Uint64);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   248
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2060
diff changeset
   249
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   250
static __inline__ void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   251
swizzle_alsa_channels_6_32bit(_THIS)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   252
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   253
    SWIZ6(Uint32);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   254
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2060
diff changeset
   255
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   256
static __inline__ void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   257
swizzle_alsa_channels_6_16bit(_THIS)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   258
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   259
    SWIZ6(Uint16);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   260
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2060
diff changeset
   261
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   262
static __inline__ void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   263
swizzle_alsa_channels_6_8bit(_THIS)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   264
{
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   265
    SWIZ6(Uint8);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   266
}
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   267
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   268
#undef SWIZ6
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   269
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   270
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   271
/*
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   272
 * Called right before feeding this->hidden->mixbuf to the hardware. Swizzle
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   273
 *  channels from Windows/Mac order to the format alsalib will want.
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   274
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   275
static __inline__ void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   276
swizzle_alsa_channels(_THIS)
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   277
{
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   278
    if (this->spec.channels == 6) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   279
        const Uint16 fmtsize = (this->spec.format & 0xFF);      /* bits/channel. */
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   280
        if (fmtsize == 16)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   281
            swizzle_alsa_channels_6_16bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   282
        else if (fmtsize == 8)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   283
            swizzle_alsa_channels_6_8bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   284
        else if (fmtsize == 32)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   285
            swizzle_alsa_channels_6_32bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   286
        else if (fmtsize == 64)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   287
            swizzle_alsa_channels_6_64bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   288
    }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   289
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   290
    /* !!! FIXME: update this for 7.1 if needed, later. */
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   291
}
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   292
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   293
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   294
static void
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   295
ALSA_PlayDevice(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   296
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   297
    int status;
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   298
    const Uint8 *sample_buf = (const Uint8 *) this->hidden->mixbuf;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   299
    const int frame_size = (((int) (this->spec.format & 0xFF)) / 8) *
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   300
                                this->spec.channels;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   301
    snd_pcm_uframes_t frames_left = ((snd_pcm_uframes_t) this->spec.samples);
765
4c2ba6161939 Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents: 547
diff changeset
   302
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   303
    swizzle_alsa_channels(this);
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   304
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   305
    while ( frames_left > 0 && this->enabled ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   306
        /* !!! FIXME: This works, but needs more testing before going live */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   307
        /*ALSA_snd_pcm_wait(this->hidden->pcm_handle, -1);*/
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   308
        status = ALSA_snd_pcm_writei(this->hidden->pcm_handle,
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   309
                                     sample_buf, frames_left);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   310
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   311
        if (status < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   312
            if (status == -EAGAIN) {
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   313
                /* Apparently snd_pcm_recover() doesn't handle this case -
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   314
                   does it assume snd_pcm_wait() above? */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   315
                SDL_Delay(1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   316
                continue;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   317
            }
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   318
            status = ALSA_snd_pcm_recover(this->hidden->pcm_handle, status, 0);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   319
            if (status < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   320
                /* Hmm, not much we can do - abort */
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   321
                fprintf(stderr, "ALSA write failed (unrecoverable): %s\n",
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   322
                        ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   323
                this->enabled = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   324
                return;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   325
            }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   326
            continue;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   327
        }
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   328
        sample_buf += status * frame_size;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   329
        frames_left -= status;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   330
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   331
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   332
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   333
static Uint8 *
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   334
ALSA_GetDeviceBuf(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   335
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   336
    return (this->hidden->mixbuf);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   337
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   338
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   339
static void
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   340
ALSA_CloseDevice(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   341
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   342
    if (this->hidden != NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   343
        if (this->hidden->mixbuf != NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   344
            SDL_FreeAudioMem(this->hidden->mixbuf);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   345
            this->hidden->mixbuf = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   346
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   347
        if (this->hidden->pcm_handle) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   348
            ALSA_snd_pcm_drain(this->hidden->pcm_handle);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   349
            ALSA_snd_pcm_close(this->hidden->pcm_handle);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   350
            this->hidden->pcm_handle = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   351
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   352
        SDL_free(this->hidden);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   353
        this->hidden = NULL;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   354
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   355
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   356
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   357
static int
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   358
ALSA_finalize_hardware(_THIS, snd_pcm_hw_params_t *hwparams, int override)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   359
{
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   360
    int status;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   361
    snd_pcm_uframes_t bufsize;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   362
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   363
    /* "set" the hardware with the desired parameters */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   364
    status = ALSA_snd_pcm_hw_params(this->hidden->pcm_handle, hwparams);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   365
    if ( status < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   366
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   367
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   368
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   369
    /* Get samples for the actual buffer size */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   370
    status = ALSA_snd_pcm_hw_params_get_buffer_size(hwparams, &bufsize);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   371
    if ( status < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   372
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   373
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   374
    if ( !override && bufsize != this->spec.samples * 2 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   375
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   376
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   377
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   378
    /* !!! FIXME: Is this safe to do? */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   379
    this->spec.samples = bufsize / 2;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   380
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   381
    /* This is useful for debugging */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   382
    if ( SDL_getenv("SDL_AUDIO_ALSA_DEBUG") ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   383
        snd_pcm_uframes_t persize = 0;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   384
        unsigned int periods = 0;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   385
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   386
        ALSA_snd_pcm_hw_params_get_period_size(hwparams, &persize, NULL);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   387
        ALSA_snd_pcm_hw_params_get_periods(hwparams, &periods, NULL);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   388
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   389
        fprintf(stderr,
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   390
            "ALSA: period size = %ld, periods = %u, buffer size = %lu\n",
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   391
            persize, periods, bufsize);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   392
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   393
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   394
    return(0);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   395
}
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   396
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   397
static int
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   398
ALSA_set_period_size(_THIS, snd_pcm_hw_params_t *params, int override)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   399
{
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   400
    const char *env;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   401
    int status;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   402
    snd_pcm_hw_params_t *hwparams;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   403
    snd_pcm_uframes_t frames;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   404
    unsigned int periods;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   405
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   406
    /* Copy the hardware parameters for this setup */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   407
    snd_pcm_hw_params_alloca(&hwparams);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   408
    ALSA_snd_pcm_hw_params_copy(hwparams, params);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   409
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   410
    if ( !override ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   411
        env = SDL_getenv("SDL_AUDIO_ALSA_SET_PERIOD_SIZE");
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   412
        if ( env ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   413
            override = SDL_atoi(env);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   414
            if ( override == 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   415
                return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   416
            }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   417
        }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   418
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   419
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   420
    frames = this->spec.samples;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   421
    status = ALSA_snd_pcm_hw_params_set_period_size_near(
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   422
                this->hidden->pcm_handle, hwparams, &frames, NULL);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   423
    if ( status < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   424
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   425
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   426
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   427
    periods = 2;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   428
    status = ALSA_snd_pcm_hw_params_set_periods_near(
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   429
                this->hidden->pcm_handle, hwparams, &periods, NULL);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   430
    if ( status < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   431
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   432
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   433
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   434
    return ALSA_finalize_hardware(this, hwparams, override);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   435
}
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   436
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   437
static int
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   438
ALSA_set_buffer_size(_THIS, snd_pcm_hw_params_t *params, int override)
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   439
{
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   440
    const char *env;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   441
    int status;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   442
    snd_pcm_hw_params_t *hwparams;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   443
    snd_pcm_uframes_t frames;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   444
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   445
    /* Copy the hardware parameters for this setup */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   446
    snd_pcm_hw_params_alloca(&hwparams);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   447
    ALSA_snd_pcm_hw_params_copy(hwparams, params);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   448
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   449
    if ( !override ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   450
        env = SDL_getenv("SDL_AUDIO_ALSA_SET_BUFFER_SIZE");
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   451
        if ( env ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   452
            override = SDL_atoi(env);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   453
            if ( override == 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   454
                return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   455
            }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   456
        }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   457
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   458
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   459
    frames = this->spec.samples * 2;
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   460
    status = ALSA_snd_pcm_hw_params_set_buffer_size_near(
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   461
                    this->hidden->pcm_handle, hwparams, &frames);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   462
    if ( status < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   463
        return(-1);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   464
    }
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   465
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   466
    return ALSA_finalize_hardware(this, hwparams, override);
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   467
}
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   468
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   469
static int
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   470
ALSA_OpenDevice(_THIS, const char *devname, int iscapture)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   471
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   472
    int status = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   473
    snd_pcm_t *pcm_handle = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   474
    snd_pcm_hw_params_t *hwparams = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   475
    snd_pcm_sw_params_t *swparams = NULL;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   476
    snd_pcm_format_t format = 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   477
    SDL_AudioFormat test_format = 0;
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   478
    unsigned int rate = 0;
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   479
    unsigned int channels = 0;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   480
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   481
    /* Initialize all variables that we clean on shutdown */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   482
    this->hidden = (struct SDL_PrivateAudioData *)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   483
        SDL_malloc((sizeof *this->hidden));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   484
    if (this->hidden == NULL) {
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   485
        return SDL_OutOfMemory();
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   486
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   487
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   488
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   489
    /* Open the audio device */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   490
    /* Name of device should depend on # channels in spec */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   491
    status = ALSA_snd_pcm_open(&pcm_handle,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   492
                               get_audio_device(this->spec.channels),
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   493
                               SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
942
41a59de7f2ed Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents: 939
diff changeset
   494
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   495
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   496
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   497
        return SDL_SetError("ALSA: Couldn't open audio device: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   498
                            ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   499
    }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   500
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   501
    this->hidden->pcm_handle = pcm_handle;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   502
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   503
    /* Figure out what the hardware is capable of */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   504
    snd_pcm_hw_params_alloca(&hwparams);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   505
    status = ALSA_snd_pcm_hw_params_any(pcm_handle, hwparams);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   506
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   507
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   508
        return SDL_SetError("ALSA: Couldn't get hardware config: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   509
                            ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   510
    }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   511
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   512
    /* SDL only uses interleaved sample output */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   513
    status = ALSA_snd_pcm_hw_params_set_access(pcm_handle, hwparams,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   514
                                               SND_PCM_ACCESS_RW_INTERLEAVED);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   515
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   516
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   517
        return SDL_SetError("ALSA: Couldn't set interleaved access: %s",
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   518
                     ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   519
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   520
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   521
    /* Try for a closest match on audio format */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   522
    status = -1;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   523
    for (test_format = SDL_FirstAudioFormat(this->spec.format);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   524
         test_format && (status < 0);) {
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   525
        status = 0;             /* if we can't support a format, it'll become -1. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   526
        switch (test_format) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   527
        case AUDIO_U8:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   528
            format = SND_PCM_FORMAT_U8;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   529
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   530
        case AUDIO_S8:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   531
            format = SND_PCM_FORMAT_S8;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   532
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   533
        case AUDIO_S16LSB:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   534
            format = SND_PCM_FORMAT_S16_LE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   535
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   536
        case AUDIO_S16MSB:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   537
            format = SND_PCM_FORMAT_S16_BE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   538
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   539
        case AUDIO_U16LSB:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   540
            format = SND_PCM_FORMAT_U16_LE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   541
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   542
        case AUDIO_U16MSB:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   543
            format = SND_PCM_FORMAT_U16_BE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   544
            break;
1995
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   545
        case AUDIO_S32LSB:
2010
39897da56f63 Whoops, wrong tokens for int32 support in ALSA driver (specified unsigned
Ryan C. Gordon <icculus@icculus.org>
parents: 2009
diff changeset
   546
            format = SND_PCM_FORMAT_S32_LE;
1995
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   547
            break;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   548
        case AUDIO_S32MSB:
2010
39897da56f63 Whoops, wrong tokens for int32 support in ALSA driver (specified unsigned
Ryan C. Gordon <icculus@icculus.org>
parents: 2009
diff changeset
   549
            format = SND_PCM_FORMAT_S32_BE;
1995
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   550
            break;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   551
        case AUDIO_F32LSB:
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   552
            format = SND_PCM_FORMAT_FLOAT_LE;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   553
            break;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   554
        case AUDIO_F32MSB:
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   555
            format = SND_PCM_FORMAT_FLOAT_BE;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   556
            break;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   557
        default:
2009
f2058fb367e4 ALSA was testing if (format) was set to zero as an error condition, but
Ryan C. Gordon <icculus@icculus.org>
parents: 1995
diff changeset
   558
            status = -1;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   559
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   560
        }
2009
f2058fb367e4 ALSA was testing if (format) was set to zero as an error condition, but
Ryan C. Gordon <icculus@icculus.org>
parents: 1995
diff changeset
   561
        if (status >= 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   562
            status = ALSA_snd_pcm_hw_params_set_format(pcm_handle,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   563
                                                       hwparams, format);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   564
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   565
        if (status < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   566
            test_format = SDL_NextAudioFormat();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   567
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   568
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   569
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   570
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   571
        return SDL_SetError("ALSA: Couldn't find any hardware audio formats");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   572
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   573
    this->spec.format = test_format;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   574
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   575
    /* Set the number of channels */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   576
    status = ALSA_snd_pcm_hw_params_set_channels(pcm_handle, hwparams,
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   577
                                                 this->spec.channels);
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   578
    channels = this->spec.channels;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   579
    if (status < 0) {
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   580
        status = ALSA_snd_pcm_hw_params_get_channels(hwparams, &channels);
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   581
        if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   582
            ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   583
            return SDL_SetError("ALSA: Couldn't set audio channels");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   584
        }
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   585
        this->spec.channels = channels;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   586
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   587
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   588
    /* Set the audio rate */
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   589
    rate = this->spec.freq;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   590
    status = ALSA_snd_pcm_hw_params_set_rate_near(pcm_handle, hwparams,
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   591
                                                  &rate, NULL);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   592
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   593
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   594
        return SDL_SetError("ALSA: Couldn't set audio frequency: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   595
                            ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   596
    }
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   597
    this->spec.freq = rate;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   598
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   599
    /* Set the buffer size, in samples */
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   600
    if ( ALSA_set_period_size(this, hwparams, 0) < 0 &&
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   601
         ALSA_set_buffer_size(this, hwparams, 0) < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   602
        /* Failed to set desired buffer size, do the best you can... */
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   603
        if ( ALSA_set_period_size(this, hwparams, 1) < 0 ) {
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   604
            ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   605
            return SDL_SetError("Couldn't set hardware audio parameters: %s", ALSA_snd_strerror(status));
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   606
        }
3362
4e83cdb58134 Merged r4990:4991 from branches/SDL-1.2: ALSA 1.0 API and dlvsym() removal.
Ryan C. Gordon <icculus@icculus.org>
parents: 3068
diff changeset
   607
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   608
    /* Set the software parameters */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   609
    snd_pcm_sw_params_alloca(&swparams);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   610
    status = ALSA_snd_pcm_sw_params_current(pcm_handle, swparams);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   611
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   612
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   613
        return SDL_SetError("ALSA: Couldn't get software config: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   614
                            ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   615
    }
5622
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
   616
    status = ALSA_snd_pcm_sw_params_set_avail_min(pcm_handle, swparams, this->spec.samples);
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
   617
    if (status < 0) {
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
   618
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   619
        return SDL_SetError("Couldn't set minimum available samples: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   620
                            ALSA_snd_strerror(status));
5622
08e941dad8e9 Ported ALSA minimum-sample-count fix from 1.2 branch to 1.3.
Ryan C. Gordon <icculus@icculus.org>
parents: 5594
diff changeset
   621
    }
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   622
    status =
3627
631173ffd68f Merged r4991:5154 from branches/SDL-1.2/src/audio/alsa: many 1.2.14 ALSA fixes.
Ryan C. Gordon <icculus@icculus.org>
parents: 3362
diff changeset
   623
        ALSA_snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, 1);
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   624
    if (status < 0) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   625
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   626
        return SDL_SetError("ALSA: Couldn't set start threshold: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   627
                            ALSA_snd_strerror(status));
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   628
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   629
    status = ALSA_snd_pcm_sw_params(pcm_handle, swparams);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   630
    if (status < 0) {
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   631
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   632
        return SDL_SetError("Couldn't set software audio parameters: %s",
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   633
                            ALSA_snd_strerror(status));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   634
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   635
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   636
    /* Calculate the final parameters for this audio specification */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   637
    SDL_CalculateAudioSpec(&this->spec);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   638
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   639
    /* Allocate mixing buffer */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   640
    this->hidden->mixlen = this->spec.size;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   641
    this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   642
    if (this->hidden->mixbuf == NULL) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   643
        ALSA_CloseDevice(this);
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   644
        return SDL_OutOfMemory();
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   645
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   646
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   647
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   648
    /* Switch to blocking mode for playback */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   649
    ALSA_snd_pcm_nonblock(pcm_handle, 0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   650
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   651
    /* We're ready to rock and roll. :-) */
7038
7f22b9ba218f Changed audio subsystem's OpenDevice interface to return -1 on error.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   652
    return 0;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   653
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   654
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   655
static void
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   656
ALSA_Deinitialize(void)
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   657
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   658
    UnloadALSALibrary();
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   659
}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   660
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   661
static int
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   662
ALSA_Init(SDL_AudioDriverImpl * impl)
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   663
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   664
    if (LoadALSALibrary() < 0) {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   665
        return 0;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   666
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   667
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   668
    /* Set the function pointers */
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   669
    impl->OpenDevice = ALSA_OpenDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   670
    impl->WaitDevice = ALSA_WaitDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   671
    impl->GetDeviceBuf = ALSA_GetDeviceBuf;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   672
    impl->PlayDevice = ALSA_PlayDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   673
    impl->CloseDevice = ALSA_CloseDevice;
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   674
    impl->Deinitialize = ALSA_Deinitialize;
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   675
    impl->OnlyHasDefaultOutputDevice = 1;       /* !!! FIXME: Add device enum! */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   676
3699
4160ba33b597 Removed test for "driver is valid, but doesn't see any audio devices."
Ryan C. Gordon <icculus@icculus.org>
parents: 3697
diff changeset
   677
    return 1;   /* this audio target is available. */
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   678
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   679
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   680
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   681
AudioBootStrap ALSA_bootstrap = {
5594
e741303e08a8 Removed needless macros in various audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   682
    "alsa", "ALSA PCM audio", ALSA_Init, 0
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   683
};
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   684
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5622
diff changeset
   685
#endif /* SDL_AUDIO_DRIVER_ALSA */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5622
diff changeset
   686
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
   687
/* vi: set ts=4 sw=4 expandtab: */