src/audio/alsa/SDL_alsa_audio.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 02 Aug 2016 15:06:40 -0400
changeset 10225 874c22f118c2
parent 10224 8769b18e16a1
child 10229 ac7e62852dab
permissions -rw-r--r--
alsa: Implemented audio capture support!
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
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     3
  Copyright (C) 1997-2016 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
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7860
diff changeset
    21
#include "../../SDL_internal.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
10225
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
    32
#include "SDL_assert.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    33
#include "SDL_timer.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
#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
    35
#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
    36
#include "../SDL_audio_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    37
#include "SDL_alsa_audio.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    38
6046
3b33b82cdbac Fixed some preprocessor mistakes introduced in iOS project cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
    39
#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
    40
#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
    41
#endif
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
    42
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
    43
static int (*ALSA_snd_pcm_open)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    44
  (snd_pcm_t **, const char *, snd_pcm_stream_t, int);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    45
static int (*ALSA_snd_pcm_close) (snd_pcm_t * pcm);
10225
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
    46
static snd_pcm_sframes_t (*ALSA_snd_pcm_writei)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    47
  (snd_pcm_t *, const void *, snd_pcm_uframes_t);
10225
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
    48
static snd_pcm_sframes_t (*ALSA_snd_pcm_readi)
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
    49
  (snd_pcm_t *, 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
    50
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
    51
static int (*ALSA_snd_pcm_prepare) (snd_pcm_t *);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    52
static int (*ALSA_snd_pcm_drain) (snd_pcm_t *);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    53
static const char *(*ALSA_snd_strerror) (int);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    54
static size_t(*ALSA_snd_pcm_hw_params_sizeof) (void);
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    55
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
    56
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
    57
  (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
    58
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
    59
static int (*ALSA_snd_pcm_hw_params_set_access)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    60
  (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
    61
static int (*ALSA_snd_pcm_hw_params_set_format)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    62
  (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
    63
static int (*ALSA_snd_pcm_hw_params_set_channels)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    64
  (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
    65
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
    66
  (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
    67
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
    68
  (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
    69
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
    70
  (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
    71
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
    72
  (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
    73
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
    74
  (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
    75
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
    76
  (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
    77
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
    78
  (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
    79
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
    80
  (const snd_pcm_hw_params_t *, snd_pcm_uframes_t *);
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    81
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
    82
static int (*ALSA_snd_pcm_sw_params_current) (snd_pcm_t *,
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    83
                                              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
    84
static int (*ALSA_snd_pcm_sw_params_set_start_threshold)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
    85
  (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
    86
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
    87
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
    88
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
    89
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
    90
  (snd_pcm_t *, snd_pcm_sw_params_t *, snd_pcm_uframes_t);
10225
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
    91
static int (*ALSA_snd_pcm_reset)(snd_pcm_t *);
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    92
static int (*ALSA_snd_device_name_hint) (int, const char *, void ***);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    93
static char* (*ALSA_snd_device_name_get_hint) (const void *, const char *);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
    94
static int (*ALSA_snd_device_name_free_hint) (void **);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    95
6046
3b33b82cdbac Fixed some preprocessor mistakes introduced in iOS project cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
    96
#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
    97
#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
    98
#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
    99
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   100
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
   101
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
   102
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   103
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
   104
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
   105
{
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
   106
    *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
   107
    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
   108
        /* 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
   109
        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
   110
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   111
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   112
    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
   113
}
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   114
1161
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
   115
/* 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
   116
#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
   117
    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
   118
#else
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   119
#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
   120
#endif
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   121
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   122
static int
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   123
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
   124
{
2049
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_open);
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_close);
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   127
    SDL_ALSA_SYM(snd_pcm_writei);
10225
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   128
    SDL_ALSA_SYM(snd_pcm_readi);
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
   129
    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
   130
    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
   131
    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
   132
    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
   133
    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
   134
    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
   135
    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
   136
    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
   137
    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
   138
    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
   139
    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
   140
    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
   141
    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
   142
    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
   143
    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
   144
    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
   145
    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
   146
    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
   147
    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
   148
    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
   149
    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
   150
    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
   151
    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
   152
    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
   153
    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
   154
    SDL_ALSA_SYM(snd_pcm_sw_params_set_avail_min);
10225
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   155
    SDL_ALSA_SYM(snd_pcm_reset);
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   156
    SDL_ALSA_SYM(snd_device_name_hint);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   157
    SDL_ALSA_SYM(snd_device_name_get_hint);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   158
    SDL_ALSA_SYM(snd_device_name_free_hint);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   159
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   160
    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
   161
}
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   162
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   163
#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
   164
6046
3b33b82cdbac Fixed some preprocessor mistakes introduced in iOS project cleanup.
Ryan C. Gordon <icculus@icculus.org>
parents: 6044
diff changeset
   165
#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
   166
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
   167
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
   168
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
   169
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   170
    if (alsa_handle != NULL) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7038
diff changeset
   171
        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
   172
        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
   173
    }
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   174
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   175
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
   176
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
   177
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
   178
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   179
    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
   180
    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
   181
        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
   182
        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
   183
            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
   184
            /* 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
   185
        } else {
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   186
            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
   187
            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
   188
                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
   189
            }
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
        }
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
    }
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
   192
    return retval;
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   193
}
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
#else
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   196
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
   197
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
   198
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
   199
{
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   200
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   201
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
   202
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
   203
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
   204
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   205
    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
   206
    return 0;
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   207
}
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   208
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
   209
#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
   210
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
   211
static const char *
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   212
get_audio_device(void *handle, const int channels)
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   213
{
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
   214
    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
   215
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   216
    if (handle != NULL) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   217
        return (const char *) handle;
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   218
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   219
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   220
    /* !!! FIXME: we also check "SDL_AUDIO_DEVICE_NAME" at the higher level. */
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
   221
    device = SDL_getenv("AUDIODEV");    /* Is there a standard variable name? */
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   222
    if (device != NULL) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   223
        return device;
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
   224
    }
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   225
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   226
    if (channels == 6) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   227
        return "plug:surround51";
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   228
    } else if (channels == 4) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   229
        return "plug:surround40";
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   230
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   231
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   232
    return "default";
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   233
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   234
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   235
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   236
/* 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
   237
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
   238
ALSA_WaitDevice(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   239
{
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
   240
    /* We're in blocking mode, so there's nothing to do here */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   241
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   242
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   243
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   244
/* !!! 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
   245
/*
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   246
 * 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
   247
 * "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
   248
 *  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
   249
 */
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   250
#define SWIZ6(T, buf, numframes) \
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   251
    T *ptr = (T *) buf; \
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   252
    Uint32 i; \
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   253
    for (i = 0; i < numframes; 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
   254
        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
   255
        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
   256
        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
   257
    }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   258
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   259
static SDL_INLINE void
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   260
swizzle_alsa_channels_6_64bit(void *buffer, Uint32 bufferlen)
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
   261
{
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   262
    SWIZ6(Uint64, buffer, bufferlen);
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
   263
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2060
diff changeset
   264
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   265
static SDL_INLINE void
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   266
swizzle_alsa_channels_6_32bit(void *buffer, Uint32 bufferlen)
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
   267
{
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   268
    SWIZ6(Uint32, buffer, bufferlen);
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
   269
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2060
diff changeset
   270
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   271
static SDL_INLINE void
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   272
swizzle_alsa_channels_6_16bit(void *buffer, Uint32 bufferlen)
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
   273
{
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   274
    SWIZ6(Uint16, buffer, bufferlen);
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
}
2735
204be4fc2726 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2060
diff changeset
   276
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   277
static SDL_INLINE void
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   278
swizzle_alsa_channels_6_8bit(void *buffer, Uint32 bufferlen)
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
{
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   280
    SWIZ6(Uint8, buffer, bufferlen);
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
   281
}
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   282
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   283
#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
   284
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   285
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   286
/*
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   287
 * 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
   288
 *  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
   289
 */
7860
2b0bcdea3a79 Fixed bug 2129 - fix for bug 2121 breaks linking for mingw and throws multiple warnings
Sam Lantinga <slouken@libsdl.org>
parents: 7719
diff changeset
   290
static SDL_INLINE void
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   291
swizzle_alsa_channels(_THIS, void *buffer, Uint32 bufferlen)
1878
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
    if (this->spec.channels == 6) {
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   294
        switch (SDL_AUDIO_BITSIZE(this->spec.format)) {
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   295
            case 8: swizzle_alsa_channels_6_8bit(buffer, bufferlen); break;
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   296
            case 16: swizzle_alsa_channels_6_16bit(buffer, bufferlen); break;
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   297
            case 32: swizzle_alsa_channels_6_32bit(buffer, bufferlen); break;
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   298
            case 64: swizzle_alsa_channels_6_64bit(buffer, bufferlen); break;
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   299
            default: SDL_assert(!"unhandled bitsize"); break;
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   300
        }
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   301
    }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   302
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   303
    /* !!! 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
   304
}
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   305
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   306
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
   307
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
   308
ALSA_PlayDevice(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   309
{
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
   310
    const Uint8 *sample_buf = (const Uint8 *) this->hidden->mixbuf;
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   311
    const int frame_size = (((int) SDL_AUDIO_BITSIZE(this->spec.format)) / 8) *
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
   312
                                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
   313
    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
   314
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   315
    swizzle_alsa_channels(this, this->hidden->mixbuf, frames_left);
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
   316
10220
6fa358b97f4b audio: Made some SDL_AudioDevice fields atomic.
Ryan C. Gordon <icculus@icculus.org>
parents: 10206
diff changeset
   317
    while ( frames_left > 0 && SDL_AtomicGet(&this->enabled) ) {
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
        /* !!! FIXME: This works, but needs more testing before going live */
7678
286c42d7c5ed OCD fixes: Adds a space after /* (glory to regular expressions!)
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7677
diff changeset
   319
        /* ALSA_snd_pcm_wait(this->hidden->pcm_handle, -1); */
10224
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   320
        int status = ALSA_snd_pcm_writei(this->hidden->pcm_handle,
8769b18e16a1 alsa: Cleaned up the 5.1 swizzle mess a little.
Ryan C. Gordon <icculus@icculus.org>
parents: 10220
diff changeset
   321
                                         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
   322
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
        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
   324
            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
   325
                /* 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
   326
                   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
   327
                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
   328
                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
   329
            }
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
   330
            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
   331
            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
   332
                /* 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
   333
                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
   334
                        ALSA_snd_strerror(status));
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   335
                SDL_OpenedAudioDeviceDisconnected(this);
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
   336
                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
   337
            }
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
   338
            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
   339
        }
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
   340
        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
   341
        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
   342
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   343
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   344
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
   345
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
   346
ALSA_GetDeviceBuf(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   347
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   348
    return (this->hidden->mixbuf);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   349
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   350
10225
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   351
static int
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   352
ALSA_CaptureFromDevice(_THIS, void *buffer, int buflen)
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   353
{
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   354
    Uint8 *sample_buf = (Uint8 *) buffer;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   355
    const int frame_size = (((int) SDL_AUDIO_BITSIZE(this->spec.format)) / 8) *
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   356
                                this->spec.channels;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   357
    const int total_frames = buflen / frame_size;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   358
    snd_pcm_uframes_t frames_left = total_frames;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   359
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   360
    SDL_assert((buflen % frame_size) == 0);
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   361
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   362
    while ( frames_left > 0 && SDL_AtomicGet(&this->enabled) ) {
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   363
        /* !!! FIXME: This works, but needs more testing before going live */
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   364
        /* ALSA_snd_pcm_wait(this->hidden->pcm_handle, -1); */
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   365
        int status = ALSA_snd_pcm_readi(this->hidden->pcm_handle,
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   366
                                        sample_buf, frames_left);
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   367
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   368
        if (status < 0) {
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   369
            /*printf("ALSA: capture error %d\n", status);*/
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   370
            if (status == -EAGAIN) {
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   371
                /* Apparently snd_pcm_recover() doesn't handle this case -
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   372
                   does it assume snd_pcm_wait() above? */
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   373
                SDL_Delay(1);
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   374
                continue;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   375
            }
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   376
            status = ALSA_snd_pcm_recover(this->hidden->pcm_handle, status, 0);
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   377
            if (status < 0) {
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   378
                /* Hmm, not much we can do - abort */
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   379
                fprintf(stderr, "ALSA read failed (unrecoverable): %s\n",
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   380
                        ALSA_snd_strerror(status));
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   381
                return -1;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   382
            }
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   383
            continue;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   384
        }
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   385
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   386
        /*printf("ALSA: captured %d bytes\n", status * frame_size);*/
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   387
        sample_buf += status * frame_size;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   388
        frames_left -= status;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   389
    }
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   390
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   391
    swizzle_alsa_channels(this, buffer, total_frames - frames_left);
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   392
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   393
    return (total_frames - frames_left) * frame_size;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   394
}
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   395
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   396
static void
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   397
ALSA_FlushCapture(_THIS)
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   398
{
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   399
    ALSA_snd_pcm_reset(this->hidden->pcm_handle);
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   400
}
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   401
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
   402
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
   403
ALSA_CloseDevice(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   404
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   405
    if (this->hidden != NULL) {
7719
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7678
diff changeset
   406
        SDL_FreeAudioMem(this->hidden->mixbuf);
31b5f9ff36ca Christoph Mallon: Remove pointless if (x) before SDL_free(x)
Sam Lantinga <slouken@libsdl.org>
parents: 7678
diff changeset
   407
        this->hidden->mixbuf = 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
   408
        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
   409
            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
   410
            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
   411
            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
   412
        }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   413
        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
   414
        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
   415
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   416
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   417
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
   418
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
   419
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
   420
{
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
    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
   422
    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
   423
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
    /* "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
   425
    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
   426
    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
   427
        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
   428
    }
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
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
    /* 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
   431
    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
   432
    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
   433
        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
   434
    }
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
    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
   436
        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
   437
    }
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
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
    /* !!! 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
   440
    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
   441
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
    /* 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
   443
    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
   444
        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
   445
        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
   446
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_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
   448
        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
   449
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
        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
   451
            "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
   452
            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
   453
    }
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
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
    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
   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
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
   459
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
   460
{
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
    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
   462
    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
   463
    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
   464
    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
   465
    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
   466
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
    /* 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
   468
    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
   469
    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
   470
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
   471
    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
   472
        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
   473
        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
   474
            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
   475
            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
   476
                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
   477
            }
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
   478
        }
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
   479
    }
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
   480
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
   481
    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
   482
    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
   483
                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
   484
    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
   485
        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
   486
    }
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
   487
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
   488
    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
   489
    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
   490
                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
   491
    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
   492
        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
   493
    }
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
   494
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
   495
    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
   496
}
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
   497
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
   498
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
   499
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
   500
{
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
   501
    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
   502
    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
   503
    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
   504
    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
   505
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
   506
    /* 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
   507
    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
   508
    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
   509
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
   510
    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
   511
        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
   512
        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
   513
            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
   514
            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
   515
                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
   516
            }
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
   517
        }
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
   518
    }
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
   519
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
   520
    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
   521
    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
   522
                    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
   523
    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
   524
        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
   525
    }
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
   526
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
   527
    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
   528
}
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
   529
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
   530
static int
9394
bb28e5281770 Bunch of reworking to how we manage audio devices.
Ryan C. Gordon <icculus@icculus.org>
parents: 9393
diff changeset
   531
ALSA_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   532
{
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   533
    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
   534
    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
   535
    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
   536
    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
   537
    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
   538
    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
   539
    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
   540
    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
   541
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   542
    /* 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
   543
    this->hidden = (struct SDL_PrivateAudioData *)
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   544
        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
   545
    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
   546
        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
   547
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   548
    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   549
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
   550
    /* 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
   551
    /* 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
   552
    status = ALSA_snd_pcm_open(&pcm_handle,
10225
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   553
                get_audio_device(handle, this->spec.channels),
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   554
                iscapture ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK,
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   555
                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
   556
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
    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
   558
        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
   559
        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
   560
                            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
   561
    }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   562
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   563
    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
   564
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
   565
    /* 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
   566
    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
   567
    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
   568
    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
   569
        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
   570
        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
   571
                            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
   572
    }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   573
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
   574
    /* 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
   575
    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
   576
                                               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
   577
    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
   578
        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
   579
        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
   580
                     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
   581
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   582
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
   583
    /* 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
   584
    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
   585
    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
   586
         test_format && (status < 0);) {
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   587
        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
   588
        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
   589
        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
   590
            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
   591
            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
   592
        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
   593
            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
   594
            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
   595
        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
   596
            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
   597
            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
   598
        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
   599
            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
   600
            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
   601
        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
   602
            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
   603
            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
   604
        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
   605
            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
   606
            break;
1995
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   607
        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
   608
            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
   609
            break;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   610
        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
   611
            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
   612
            break;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   613
        case AUDIO_F32LSB:
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   614
            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
   615
            break;
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   616
        case AUDIO_F32MSB:
0ca6ba107642 ALSA backend can handle int32 and float32 data directly.
Ryan C. Gordon <icculus@icculus.org>
parents: 1982
diff changeset
   617
            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
   618
            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
   619
        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
   620
            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
   621
            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
   622
        }
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
   623
        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
   624
            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
   625
                                                       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
   626
        }
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
   627
        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
   628
            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
   629
        }
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
    }
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
   631
    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
   632
        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
   633
        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
   634
    }
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   635
    this->spec.format = test_format;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   636
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
   637
    /* 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
   638
    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
   639
                                                 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
   640
    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
   641
    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
   642
        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
   643
        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
   644
            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
   645
            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
   646
        }
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
   647
        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
   648
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   649
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
   650
    /* 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
   651
    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
   652
    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
   653
                                                  &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
   654
    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
   655
        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
   656
        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
   657
                            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
   658
    }
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
   659
    this->spec.freq = rate;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   660
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
   661
    /* 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
   662
    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
   663
         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
   664
        /* Failed to set desired buffer size, do the best you can... */
10206
30ca8e64db6a ALSA: Fixed adding wrong status to error message (thanks, romain145!).
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 10142
diff changeset
   665
        status = ALSA_set_period_size(this, hwparams, 1);
30ca8e64db6a ALSA: Fixed adding wrong status to error message (thanks, romain145!).
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 10142
diff changeset
   666
        if (status < 0) {
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
   667
            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
   668
            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
   669
        }
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
   670
    }
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
   671
    /* 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
   672
    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
   673
    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
   674
    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
   675
        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
   676
        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
   677
                            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
   678
    }
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
   679
    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
   680
    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
   681
        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
   682
        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
   683
                            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
   684
    }
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   685
    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
   686
        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
   687
    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
   688
        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
   689
        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
   690
                            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
   691
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   692
    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
   693
    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
   694
        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
   695
        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
   696
                            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
   697
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   698
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
   699
    /* 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
   700
    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
   701
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
   702
    /* 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
   703
    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
   704
    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
   705
    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
   706
        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
   707
        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
   708
    }
7365
a3fc9175306d Minor ALSA tweaks (include-once macro name, len for memset() more clear).
Ryan C. Gordon <icculus@icculus.org>
parents: 7191
diff changeset
   709
    SDL_memset(this->hidden->mixbuf, this->spec.silence, this->hidden->mixlen);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   710
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
   711
    /* 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
   712
    ALSA_snd_pcm_nonblock(pcm_handle, 0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   713
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
   714
    /* 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
   715
    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
   716
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   717
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   718
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
   719
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
   720
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   721
    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
   722
}
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   723
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   724
static void
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   725
add_device(const int iscapture, const char *name, const char *_desc)
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   726
{
10142
bc8ac6c0cd98 alsa: Make device enumeration robust against weird results with NULL strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 10104
diff changeset
   727
    char *desc = NULL;
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   728
    char *handle = NULL;
10142
bc8ac6c0cd98 alsa: Make device enumeration robust against weird results with NULL strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 10104
diff changeset
   729
    char *ptr = NULL;
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   730
10142
bc8ac6c0cd98 alsa: Make device enumeration robust against weird results with NULL strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 10104
diff changeset
   731
    if (!name || !_desc) {
bc8ac6c0cd98 alsa: Make device enumeration robust against weird results with NULL strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 10104
diff changeset
   732
        return;  /* nothing we can do with this...? */
bc8ac6c0cd98 alsa: Make device enumeration robust against weird results with NULL strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 10104
diff changeset
   733
    }
bc8ac6c0cd98 alsa: Make device enumeration robust against weird results with NULL strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 10104
diff changeset
   734
bc8ac6c0cd98 alsa: Make device enumeration robust against weird results with NULL strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 10104
diff changeset
   735
    desc = SDL_strdup(_desc);
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   736
    if (!desc) {
10142
bc8ac6c0cd98 alsa: Make device enumeration robust against weird results with NULL strings.
Ryan C. Gordon <icculus@icculus.org>
parents: 10104
diff changeset
   737
        return;  /* oh well, out of memory. Skip it. */
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   738
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   739
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   740
    /* some strings have newlines, like "HDA NVidia, HDMI 0\nHDMI Audio Output" */
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   741
    for (ptr = strchr(desc, '\n'); ptr; ptr = strchr(ptr + 1, '\n')) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   742
        *ptr = ' ';
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   743
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   744
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   745
    handle = SDL_strdup(name);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   746
    if (handle != NULL) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   747
        SDL_AddAudioDevice(iscapture, desc, handle);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   748
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   749
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   750
    SDL_free(desc);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   751
}
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   752
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   753
static void
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   754
ALSA_DetectDevices(void)
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   755
{
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   756
    void **hints = NULL;
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   757
    int i;
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   758
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   759
    /* !!! FIXME: use udev instead. */
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   760
    /* We won't deal with disconnects and hotplugs without udev, but at least
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   761
       you'll get a reasonable device list at startup. */
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   762
#if 1 /*!SDL_USE_LIBUDEV */
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   763
    if (ALSA_snd_device_name_hint(-1, "pcm", &hints) == -1) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   764
        return;  /* oh well. */
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   765
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   766
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   767
    for (i = 0; hints[i]; i++) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   768
        char *name = ALSA_snd_device_name_get_hint(hints[i], "NAME");
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   769
        char *desc = ALSA_snd_device_name_get_hint(hints[i], "DESC");
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   770
        char *ioid = ALSA_snd_device_name_get_hint(hints[i], "IOID");
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   771
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   772
        if ((ioid == NULL) || (SDL_strcmp(ioid, "Output") == 0)) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   773
            add_device(SDL_FALSE, name, desc);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   774
        }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   775
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   776
        if ((ioid == NULL) || (SDL_strcmp(ioid, "Input") == 0)) {
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   777
            add_device(SDL_TRUE, name, desc);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   778
        }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   779
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   780
        free(name);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   781
        free(desc);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   782
        free(ioid);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   783
    }
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   784
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   785
    ALSA_snd_device_name_free_hint(hints);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   786
#else
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   787
#error Fill in udev support here.
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   788
#endif
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   789
}
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   790
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   791
static void
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   792
ALSA_FreeDeviceHandle(void *handle)
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   793
{
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   794
#if 1 /*!SDL_USE_LIBUDEV*/
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   795
    SDL_free(handle);
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   796
#else
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   797
#error Fill in udev support here.
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   798
#endif
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   799
}
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   800
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   801
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   802
static int
2060
866052b01ee5 indent is evil
Sam Lantinga <slouken@libsdl.org>
parents: 2049
diff changeset
   803
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
   804
{
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   805
    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
   806
        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
   807
    }
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   808
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   809
    /* Set the function pointers */
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   810
    impl->DetectDevices = ALSA_DetectDevices;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   811
    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
   812
    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
   813
    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
   814
    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
   815
    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
   816
    impl->Deinitialize = ALSA_Deinitialize;
10104
4cd67316b308 alsa: Implemented basic device detection.
Ryan C. Gordon <icculus@icculus.org>
parents: 9998
diff changeset
   817
    impl->FreeDeviceHandle = ALSA_FreeDeviceHandle;
10225
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   818
    impl->CaptureFromDevice = ALSA_CaptureFromDevice;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   819
    impl->FlushCapture = ALSA_FlushCapture;
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   820
874c22f118c2 alsa: Implemented audio capture support!
Ryan C. Gordon <icculus@icculus.org>
parents: 10224
diff changeset
   821
    impl->HasCaptureSupport = SDL_TRUE;
2049
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   822
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
   823
    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
   824
}
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   825
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   826
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   827
AudioBootStrap ALSA_bootstrap = {
5594
e741303e08a8 Removed needless macros in various audio targets.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
   828
    "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
   829
};
5f6550e5184f Merged SDL-ryan-multiple-audio-device branch r2803:2871 into the trunk.
Ryan C. Gordon <icculus@icculus.org>
parents: 2043
diff changeset
   830
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5622
diff changeset
   831
#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
   832
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
   833
/* vi: set ts=4 sw=4 expandtab: */