annotate src/audio/alsa/SDL_alsa_audio.c @ 4349:471dac4b41dd SDL-1.2

Don't need the define anymore...
author Sam Lantinga <slouken@libsdl.org>
date Sat, 17 Oct 2009 07:05:33 +0000
parents b312352d8c8d
children a10dac5858fe
rev   line source
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
1 /*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
4159
a1b03ba2fcd0 Updated copyright date
Sam Lantinga <slouken@libsdl.org>
parents: 1878
diff changeset
3 Copyright (C) 1997-2009 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
6 modify it under the terms of the GNU Library General Public
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
8 version 2 of the License, or (at your option) any later version.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
13 Library General Public License for more details.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
14
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
15 You should have received a copy of the GNU Library General Public
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
16 License along with this library; if not, write to the Free
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
19 Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 0
diff changeset
20 slouken@libsdl.org
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
21 */
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1379
diff changeset
22 #include "SDL_config.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
23
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
24 /* Allow access to a raw mixing buffer */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
25
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
26 #include <sys/types.h>
1338
604d73db6802 Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents: 1336
diff changeset
27 #include <signal.h> /* For kill() */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
28
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
29 #include "SDL_timer.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
30 #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
31 #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
32 #include "../SDL_audio_c.h"
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
33 #include "SDL_alsa_audio.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
34
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
35 #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
36 #include "SDL_name.h"
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
37 #include "SDL_loadso.h"
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
38 #else
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
39 #define SDL_NAME(X) X
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
40 #endif
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
41
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
42
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
43 /* The tag name used by ALSA audio */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
44 #define DRIVER_NAME "alsa"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
45
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
46 /* Audio driver functions */
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
47 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
48 static void ALSA_WaitAudio(_THIS);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
49 static void ALSA_PlayAudio(_THIS);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
50 static Uint8 *ALSA_GetAudioBuf(_THIS);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
51 static void ALSA_CloseAudio(_THIS);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
52
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
53 #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
54
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
55 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
56 static void *alsa_handle = NULL;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
57 static int alsa_loaded = 0;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
58
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
59 static int (*SDL_NAME(snd_pcm_open))(snd_pcm_t **pcm, const char *name, snd_pcm_stream_t stream, int mode);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
60 static int (*SDL_NAME(snd_pcm_close))(snd_pcm_t *pcm);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
61 static snd_pcm_sframes_t (*SDL_NAME(snd_pcm_writei))(snd_pcm_t *pcm, const void *buffer, snd_pcm_uframes_t size);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
62 static int (*SDL_NAME(snd_pcm_resume))(snd_pcm_t *pcm);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
63 static int (*SDL_NAME(snd_pcm_prepare))(snd_pcm_t *pcm);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
64 static int (*SDL_NAME(snd_pcm_drain))(snd_pcm_t *pcm);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
65 static const char *(*SDL_NAME(snd_strerror))(int errnum);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
66 static size_t (*SDL_NAME(snd_pcm_hw_params_sizeof))(void);
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
67 static size_t (*SDL_NAME(snd_pcm_sw_params_sizeof))(void);
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
68 static int (*SDL_NAME(snd_pcm_hw_params_any))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
69 static int (*SDL_NAME(snd_pcm_hw_params_set_access))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_access_t access);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
70 static int (*SDL_NAME(snd_pcm_hw_params_set_format))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_format_t val);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
71 static int (*SDL_NAME(snd_pcm_hw_params_set_channels))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val);
4331
a24454ed4ac4 Fixed debug code to print correct values
Sam Lantinga <slouken@libsdl.org>
parents: 4329
diff changeset
72 static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const snd_pcm_hw_params_t *params, unsigned int *val);
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
73 static int (*SDL_NAME(snd_pcm_hw_params_set_rate_near))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
74 static int (*SDL_NAME(snd_pcm_hw_params_set_period_size_near))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir);
4331
a24454ed4ac4 Fixed debug code to print correct values
Sam Lantinga <slouken@libsdl.org>
parents: 4329
diff changeset
75 static int (*SDL_NAME(snd_pcm_hw_params_get_period_size))(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir);
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
76 static int (*SDL_NAME(snd_pcm_hw_params_set_periods_near))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
4331
a24454ed4ac4 Fixed debug code to print correct values
Sam Lantinga <slouken@libsdl.org>
parents: 4329
diff changeset
77 static int (*SDL_NAME(snd_pcm_hw_params_get_periods))(const snd_pcm_hw_params_t *params, unsigned int *val, int *dir);
4333
55717a755897 Switched from setting the period size and count to setting the buffer size directly, based on feedback from the ALSA development list:
Sam Lantinga <slouken@libsdl.org>
parents: 4332
diff changeset
78 static int (*SDL_NAME(snd_pcm_hw_params_set_buffer_size_near))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
4331
a24454ed4ac4 Fixed debug code to print correct values
Sam Lantinga <slouken@libsdl.org>
parents: 4329
diff changeset
79 static int (*SDL_NAME(snd_pcm_hw_params_get_buffer_size))(const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val);
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
80 static int (*SDL_NAME(snd_pcm_hw_params))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params);
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
81 /*
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
82 */
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
83 static int (*SDL_NAME(snd_pcm_sw_params_current))(snd_pcm_t *pcm, snd_pcm_sw_params_t *swparams);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
84 static int (*SDL_NAME(snd_pcm_sw_params_set_start_threshold))(snd_pcm_t *pcm, snd_pcm_sw_params_t *params, snd_pcm_uframes_t val);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
85 static int (*SDL_NAME(snd_pcm_sw_params))(snd_pcm_t *pcm, snd_pcm_sw_params_t *params);
1553
63fa37538842 Left the debug code more explicit
Sam Lantinga <slouken@libsdl.org>
parents: 1552
diff changeset
86 static int (*SDL_NAME(snd_pcm_nonblock))(snd_pcm_t *pcm, int nonblock);
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
87 #define snd_pcm_hw_params_sizeof SDL_NAME(snd_pcm_hw_params_sizeof)
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
88 #define snd_pcm_sw_params_sizeof SDL_NAME(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
89
1161
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
90 /* cast funcs to char* first, to please GCC's strict aliasing rules. */
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
91 static struct {
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
92 const char *name;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
93 void **func;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
94 } alsa_functions[] = {
1161
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
95 { "snd_pcm_open", (void**)(char*)&SDL_NAME(snd_pcm_open) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
96 { "snd_pcm_close", (void**)(char*)&SDL_NAME(snd_pcm_close) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
97 { "snd_pcm_writei", (void**)(char*)&SDL_NAME(snd_pcm_writei) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
98 { "snd_pcm_resume", (void**)(char*)&SDL_NAME(snd_pcm_resume) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
99 { "snd_pcm_prepare", (void**)(char*)&SDL_NAME(snd_pcm_prepare) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
100 { "snd_pcm_drain", (void**)(char*)&SDL_NAME(snd_pcm_drain) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
101 { "snd_strerror", (void**)(char*)&SDL_NAME(snd_strerror) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
102 { "snd_pcm_hw_params_sizeof", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_sizeof) },
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
103 { "snd_pcm_sw_params_sizeof", (void**)(char*)&SDL_NAME(snd_pcm_sw_params_sizeof) },
1161
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
104 { "snd_pcm_hw_params_any", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_any) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
105 { "snd_pcm_hw_params_set_access", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_access) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
106 { "snd_pcm_hw_params_set_format", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_format) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
107 { "snd_pcm_hw_params_set_channels", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_channels) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
108 { "snd_pcm_hw_params_get_channels", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_channels) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
109 { "snd_pcm_hw_params_set_rate_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_rate_near) },
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
110 { "snd_pcm_hw_params_set_period_size_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_period_size_near) },
1553
63fa37538842 Left the debug code more explicit
Sam Lantinga <slouken@libsdl.org>
parents: 1552
diff changeset
111 { "snd_pcm_hw_params_get_period_size", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_period_size) },
1161
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
112 { "snd_pcm_hw_params_set_periods_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_periods_near) },
1553
63fa37538842 Left the debug code more explicit
Sam Lantinga <slouken@libsdl.org>
parents: 1552
diff changeset
113 { "snd_pcm_hw_params_get_periods", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_periods) },
4333
55717a755897 Switched from setting the period size and count to setting the buffer size directly, based on feedback from the ALSA development list:
Sam Lantinga <slouken@libsdl.org>
parents: 4332
diff changeset
114 { "snd_pcm_hw_params_set_buffer_size_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_buffer_size_near) },
4331
a24454ed4ac4 Fixed debug code to print correct values
Sam Lantinga <slouken@libsdl.org>
parents: 4329
diff changeset
115 { "snd_pcm_hw_params_get_buffer_size", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_get_buffer_size) },
1161
05d4b93b911e Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents: 942
diff changeset
116 { "snd_pcm_hw_params", (void**)(char*)&SDL_NAME(snd_pcm_hw_params) },
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
117 { "snd_pcm_sw_params_current", (void**)(char*)&SDL_NAME(snd_pcm_sw_params_current) },
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
118 { "snd_pcm_sw_params_set_start_threshold", (void**)(char*)&SDL_NAME(snd_pcm_sw_params_set_start_threshold) },
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
119 { "snd_pcm_sw_params", (void**)(char*)&SDL_NAME(snd_pcm_sw_params) },
1553
63fa37538842 Left the debug code more explicit
Sam Lantinga <slouken@libsdl.org>
parents: 1552
diff changeset
120 { "snd_pcm_nonblock", (void**)(char*)&SDL_NAME(snd_pcm_nonblock) },
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
121 };
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
122
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
123 static void UnloadALSALibrary(void) {
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
124 if (alsa_loaded) {
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
125 SDL_UnloadObject(alsa_handle);
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
126 alsa_handle = NULL;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
127 alsa_loaded = 0;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
128 }
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
129 }
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
130
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
131 static int LoadALSALibrary(void) {
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
132 int i, retval = -1;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
133
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
134 alsa_handle = SDL_LoadObject(alsa_library);
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
135 if (alsa_handle) {
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
136 alsa_loaded = 1;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
137 retval = 0;
1379
c0a74f199ecf Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents: 1361
diff changeset
138 for (i = 0; i < SDL_arraysize(alsa_functions); i++) {
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
139 *alsa_functions[i].func = SDL_LoadFunction(alsa_handle,alsa_functions[i].name);
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
140 if (!*alsa_functions[i].func) {
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
141 retval = -1;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
142 UnloadALSALibrary();
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
143 break;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
144 }
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
145 }
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
146 }
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
147 return retval;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
148 }
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
149
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
150 #else
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
151
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
152 static void UnloadALSALibrary(void) {
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
153 return;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
154 }
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
155
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
156 static int LoadALSALibrary(void) {
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
157 return 0;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
158 }
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
159
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
160 #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
161
942
41a59de7f2ed Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents: 939
diff changeset
162 static const char *get_audio_device(int channels)
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
163 {
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
164 const char *device;
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
165
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1331
diff changeset
166 device = SDL_getenv("AUDIODEV"); /* Is there a standard variable name? */
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
167 if ( device == NULL ) {
4334
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
168 switch (channels) {
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
169 case 6:
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
170 device = "plug:surround51";
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
171 break;
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
172 case 4:
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
173 device = "plug:surround40";
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
174 break;
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
175 default:
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
176 device = "default";
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
177 break;
afadcd7d2319 Use the plug: ALSA devices for surround sound formats, which have automatic sample format/rate conversion.
Sam Lantinga <slouken@libsdl.org>
parents: 4333
diff changeset
178 }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
179 }
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
180 return device;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
181 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
182
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
183 /* Audio driver bootstrap functions */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
184
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
185 static int Audio_Available(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
186 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
187 int available;
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
188 int status;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
189 snd_pcm_t *handle;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
190
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
191 available = 0;
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
192 if (LoadALSALibrary() < 0) {
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
193 return available;
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
194 }
942
41a59de7f2ed Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents: 939
diff changeset
195 status = SDL_NAME(snd_pcm_open)(&handle, get_audio_device(2), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
196 if ( status >= 0 ) {
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
197 available = 1;
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
198 SDL_NAME(snd_pcm_close)(handle);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
199 }
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
200 UnloadALSALibrary();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
201 return(available);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
202 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
203
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
204 static void Audio_DeleteDevice(SDL_AudioDevice *device)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
205 {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1331
diff changeset
206 SDL_free(device->hidden);
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1331
diff changeset
207 SDL_free(device);
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
208 UnloadALSALibrary();
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
209 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
210
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
211 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
212 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
213 SDL_AudioDevice *this;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
214
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
215 /* Initialize all variables that we clean on shutdown */
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
216 LoadALSALibrary();
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1331
diff changeset
217 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
218 if ( this ) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1331
diff changeset
219 SDL_memset(this, 0, (sizeof *this));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
220 this->hidden = (struct SDL_PrivateAudioData *)
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1331
diff changeset
221 SDL_malloc((sizeof *this->hidden));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
222 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
223 if ( (this == NULL) || (this->hidden == NULL) ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
224 SDL_OutOfMemory();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
225 if ( this ) {
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1331
diff changeset
226 SDL_free(this);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
227 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
228 return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
229 }
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1331
diff changeset
230 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
231
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
232 /* Set the function pointers */
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
233 this->OpenAudio = ALSA_OpenAudio;
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
234 this->WaitAudio = ALSA_WaitAudio;
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
235 this->PlayAudio = ALSA_PlayAudio;
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
236 this->GetAudioBuf = ALSA_GetAudioBuf;
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
237 this->CloseAudio = ALSA_CloseAudio;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
238
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
239 this->free = Audio_DeleteDevice;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
240
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
241 return this;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
242 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
243
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
244 AudioBootStrap ALSA_bootstrap = {
4339
819270e2f893 Don't call it "ALSA 0.9 PCM audio" anymore, since we moved to the stable API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4337
diff changeset
245 DRIVER_NAME, "ALSA PCM audio",
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
246 Audio_Available, Audio_CreateDevice
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
247 };
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
248
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
249 /* This function waits until it is possible to write a full sound buffer */
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
250 static void ALSA_WaitAudio(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
251 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
252 /* Check to see if the thread-parent process is still alive */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
253 { static int cnt = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
254 /* Note that this only works with thread implementations
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
255 that use a different process id for each thread.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
256 */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
257 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
258 if ( kill(parent, 0) < 0 ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
259 this->enabled = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
260 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
261 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
262 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
263 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
264
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
265
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
266 /*
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
267 * 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
268 * "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
269 * 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
270 */
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
271 #define SWIZ6(T) \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
272 T *ptr = (T *) mixbuf; \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
273 const Uint32 count = (this->spec.samples / 6); \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
274 Uint32 i; \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
275 for (i = 0; i < count; i++, ptr += 6) { \
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
276 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
277 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
278 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
279 }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
280
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
281 static __inline__ void swizzle_alsa_channels_6_64bit(_THIS) { SWIZ6(Uint64); }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
282 static __inline__ void swizzle_alsa_channels_6_32bit(_THIS) { SWIZ6(Uint32); }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
283 static __inline__ void swizzle_alsa_channels_6_16bit(_THIS) { SWIZ6(Uint16); }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
284 static __inline__ void swizzle_alsa_channels_6_8bit(_THIS) { SWIZ6(Uint8); }
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 #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
287
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
288
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
289 /*
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
290 * Called right before feeding this->mixbuf to the hardware. Swizzle channels
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
291 * from Windows/Mac order to the format alsalib will want.
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 static __inline__ void swizzle_alsa_channels(_THIS)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
294 {
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
295 if (this->spec.channels == 6) {
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
296 const Uint16 fmtsize = (this->spec.format & 0xFF); /* bits/channel. */
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
297 if (fmtsize == 16)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
298 swizzle_alsa_channels_6_16bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
299 else if (fmtsize == 8)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
300 swizzle_alsa_channels_6_8bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
301 else if (fmtsize == 32)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
302 swizzle_alsa_channels_6_32bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
303 else if (fmtsize == 64)
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
304 swizzle_alsa_channels_6_64bit(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
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
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
307 /* !!! 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
308 }
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
309
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
310
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
311 static void ALSA_PlayAudio(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
312 {
4320
33d306630296 Corrected misuse of snd_pcm_writei() in ALSA driver.
Ryan C. Gordon <icculus@icculus.org>
parents: 4292
diff changeset
313 int status;
4332
67e799ffcadf ALSA's idea of a "frame" is the same as SDL's idea of a "sample". The frame is a single sample on a single channel, and we've defined the frames for each channel as being interleaved.
Sam Lantinga <slouken@libsdl.org>
parents: 4331
diff changeset
314 snd_pcm_uframes_t frames_left;
4320
33d306630296 Corrected misuse of snd_pcm_writei() in ALSA driver.
Ryan C. Gordon <icculus@icculus.org>
parents: 4292
diff changeset
315 const Uint8 *sample_buf = (const Uint8 *) mixbuf;
4336
77b20871fd44 Thilo helped me track down the audio issue with 22050 hz stereo sound. Yay!
Sam Lantinga <slouken@libsdl.org>
parents: 4334
diff changeset
316 const int frame_size = (((int) (this->spec.format & 0xFF)) / 8) * this->spec.channels;
765
4c2ba6161939 Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents: 547
diff changeset
317
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
318 swizzle_alsa_channels(this);
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
319
4332
67e799ffcadf ALSA's idea of a "frame" is the same as SDL's idea of a "sample". The frame is a single sample on a single channel, and we've defined the frames for each channel as being interleaved.
Sam Lantinga <slouken@libsdl.org>
parents: 4331
diff changeset
320 frames_left = ((snd_pcm_uframes_t) this->spec.samples);
1878
d7c9d7f42881 Swizzle ALSA channels for 5.1 output to match DirectSound and CoreAudio.
Ryan C. Gordon <icculus@icculus.org>
parents: 1553
diff changeset
321
4337
4eb6b4be1dbc Fixed bug #728
Sam Lantinga <slouken@libsdl.org>
parents: 4336
diff changeset
322 while ( frames_left > 0 && this->enabled ) {
4332
67e799ffcadf ALSA's idea of a "frame" is the same as SDL's idea of a "sample". The frame is a single sample on a single channel, and we've defined the frames for each channel as being interleaved.
Sam Lantinga <slouken@libsdl.org>
parents: 4331
diff changeset
323 status = SDL_NAME(snd_pcm_writei)(pcm_handle, sample_buf, frames_left);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
324 if ( status < 0 ) {
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
325 if ( status == -EAGAIN ) {
765
4c2ba6161939 Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents: 547
diff changeset
326 SDL_Delay(1);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
327 continue;
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
328 }
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
329 if ( status == -ESTRPIPE ) {
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
330 do {
765
4c2ba6161939 Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents: 547
diff changeset
331 SDL_Delay(1);
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
332 status = SDL_NAME(snd_pcm_resume)(pcm_handle);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
333 } while ( status == -EAGAIN );
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
334 }
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
335 if ( status < 0 ) {
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
336 status = SDL_NAME(snd_pcm_prepare)(pcm_handle);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
337 }
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
338 if ( status < 0 ) {
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
339 /* Hmm, not much we can do - abort */
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
340 this->enabled = 0;
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
341 return;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
342 }
356
a1e54d1ba16f *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 354
diff changeset
343 continue;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
344 }
4336
77b20871fd44 Thilo helped me track down the audio issue with 22050 hz stereo sound. Yay!
Sam Lantinga <slouken@libsdl.org>
parents: 4334
diff changeset
345 sample_buf += status * frame_size;
4332
67e799ffcadf ALSA's idea of a "frame" is the same as SDL's idea of a "sample". The frame is a single sample on a single channel, and we've defined the frames for each channel as being interleaved.
Sam Lantinga <slouken@libsdl.org>
parents: 4331
diff changeset
346 frames_left -= status;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
347 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
348 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
349
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
350 static Uint8 *ALSA_GetAudioBuf(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
351 {
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
352 return(mixbuf);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
353 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
354
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
355 static void ALSA_CloseAudio(_THIS)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
356 {
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
357 if ( mixbuf != NULL ) {
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
358 SDL_FreeAudioMem(mixbuf);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
359 mixbuf = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
360 }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
361 if ( pcm_handle ) {
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
362 SDL_NAME(snd_pcm_drain)(pcm_handle);
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
363 SDL_NAME(snd_pcm_close)(pcm_handle);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
364 pcm_handle = NULL;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
365 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
366 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
367
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
368 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
369 {
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
370 int status;
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
371 snd_pcm_hw_params_t *hwparams;
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
372 snd_pcm_sw_params_t *swparams;
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
373 snd_pcm_format_t format;
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
374 snd_pcm_uframes_t frames;
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
375 unsigned int rate;
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
376 unsigned int periods;
4333
55717a755897 Switched from setting the period size and count to setting the buffer size directly, based on feedback from the ALSA development list:
Sam Lantinga <slouken@libsdl.org>
parents: 4332
diff changeset
377 unsigned int channels;
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
378 Uint16 test_format;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
379
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
380 /* Open the audio device */
942
41a59de7f2ed Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents: 939
diff changeset
381 /* Name of device should depend on # channels in spec */
41a59de7f2ed Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents: 939
diff changeset
382 status = SDL_NAME(snd_pcm_open)(&pcm_handle, get_audio_device(spec->channels), SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
41a59de7f2ed Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents: 939
diff changeset
383
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
384 if ( status < 0 ) {
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
385 SDL_SetError("Couldn't open audio device: %s", SDL_NAME(snd_strerror)(status));
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
386 return(-1);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
387 }
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
388
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
389 /* Figure out what the hardware is capable of */
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
390 snd_pcm_hw_params_alloca(&hwparams);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
391 status = SDL_NAME(snd_pcm_hw_params_any)(pcm_handle, hwparams);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
392 if ( status < 0 ) {
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
393 SDL_SetError("Couldn't get hardware config: %s", SDL_NAME(snd_strerror)(status));
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
394 ALSA_CloseAudio(this);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
395 return(-1);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
396 }
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
397
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
398 /* SDL only uses interleaved sample output */
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
399 status = SDL_NAME(snd_pcm_hw_params_set_access)(pcm_handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
400 if ( status < 0 ) {
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
401 SDL_SetError("Couldn't set interleaved access: %s", SDL_NAME(snd_strerror)(status));
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
402 ALSA_CloseAudio(this);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
403 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
404 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
405
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
406 /* Try for a closest match on audio format */
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
407 status = -1;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
408 for ( test_format = SDL_FirstAudioFormat(spec->format);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
409 test_format && (status < 0); ) {
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
410 switch ( test_format ) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
411 case AUDIO_U8:
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
412 format = SND_PCM_FORMAT_U8;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
413 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
414 case AUDIO_S8:
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
415 format = SND_PCM_FORMAT_S8;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
416 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
417 case AUDIO_S16LSB:
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
418 format = SND_PCM_FORMAT_S16_LE;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
419 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
420 case AUDIO_S16MSB:
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
421 format = SND_PCM_FORMAT_S16_BE;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
422 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
423 case AUDIO_U16LSB:
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
424 format = SND_PCM_FORMAT_U16_LE;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
425 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
426 case AUDIO_U16MSB:
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
427 format = SND_PCM_FORMAT_U16_BE;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
428 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
429 default:
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
430 format = 0;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
431 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
432 }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
433 if ( format != 0 ) {
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
434 status = SDL_NAME(snd_pcm_hw_params_set_format)(pcm_handle, hwparams, format);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
435 }
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
436 if ( status < 0 ) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
437 test_format = SDL_NextAudioFormat();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
438 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
439 }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
440 if ( status < 0 ) {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
441 SDL_SetError("Couldn't find any hardware audio formats");
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
442 ALSA_CloseAudio(this);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
443 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
444 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
445 spec->format = test_format;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
446
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
447 /* Set the number of channels */
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
448 status = SDL_NAME(snd_pcm_hw_params_set_channels)(pcm_handle, hwparams, spec->channels);
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
449 channels = spec->channels;
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
450 if ( status < 0 ) {
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
451 status = SDL_NAME(snd_pcm_hw_params_get_channels)(hwparams, &channels);
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
452 if ( status < 0 ) {
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
453 SDL_SetError("Couldn't set audio channels");
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
454 ALSA_CloseAudio(this);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
455 return(-1);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
456 }
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
457 spec->channels = channels;
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
458 }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
459
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
460 /* Set the audio rate */
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
461 rate = spec->freq;
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
462
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
463 status = SDL_NAME(snd_pcm_hw_params_set_rate_near)(pcm_handle, hwparams, &rate, NULL);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
464 if ( status < 0 ) {
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
465 SDL_SetError("Couldn't set audio frequency: %s", SDL_NAME(snd_strerror)(status));
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
466 ALSA_CloseAudio(this);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
467 return(-1);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
468 }
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
469 spec->freq = rate;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
470
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
471 /* Set the buffer size, in samples */
4347
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
472 if (getenv("SDL_AUDIO_ALSA_SET_PERIOD_SIZE")) {
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
473 frames = spec->samples;
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
474 status = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, hwparams, &frames, NULL);
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
475 if ( status < 0 ) {
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
476 SDL_SetError("Couldn't set period size: %s", SDL_NAME(snd_strerror)(status));
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
477 ALSA_CloseAudio(this);
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
478 return(-1);
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
479 }
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
480
4347
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
481 spec->samples = frames;
4292
464126f4c7db Moved to stable ALSA API.
Ryan C. Gordon <icculus@icculus.org>
parents: 4159
diff changeset
482
4347
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
483 periods = 2;
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
484 status = SDL_NAME(snd_pcm_hw_params_set_periods_near)(pcm_handle, hwparams, &periods, NULL);
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
485 if ( status < 0 ) {
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
486 SDL_SetError("Couldn't set period count: %s", SDL_NAME(snd_strerror)(status));
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
487 ALSA_CloseAudio(this);
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
488 return(-1);
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
489 }
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
490 } else {
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
491 frames = spec->samples * 2;
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
492 status = SDL_NAME(snd_pcm_hw_params_set_buffer_size_near)(pcm_handle, hwparams, &frames);
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
493 if ( status < 0 ) {
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
494 SDL_SetError("Couldn't set buffer size: %s", SDL_NAME(snd_strerror)(status));
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
495 ALSA_CloseAudio(this);
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
496 return(-1);
38f22ed3a433 Option to fix bug #851
Sam Lantinga <slouken@libsdl.org>
parents: 4339
diff changeset
497 }
4333
55717a755897 Switched from setting the period size and count to setting the buffer size directly, based on feedback from the ALSA development list:
Sam Lantinga <slouken@libsdl.org>
parents: 4332
diff changeset
498 }
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
499
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
500 /* "set" the hardware with the desired parameters */
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
501 status = SDL_NAME(snd_pcm_hw_params)(pcm_handle, hwparams);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
502 if ( status < 0 ) {
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
503 SDL_SetError("Couldn't set hardware audio parameters: %s", SDL_NAME(snd_strerror)(status));
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
504 ALSA_CloseAudio(this);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
505 return(-1);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
506 }
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
507
4348
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
508 /* This is useful for debugging */
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
509 if (getenv("SDL_AUDIO_ALSA_DEBUG_PERIOD_SIZE")) {
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
510 snd_pcm_uframes_t bufsize;
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
511 snd_pcm_sframes_t persize;
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
512 unsigned int periods; int dir;
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
513
4348
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
514 SDL_NAME(snd_pcm_hw_params_get_buffer_size)(hwparams, &bufsize);
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
515 SDL_NAME(snd_pcm_hw_params_get_period_size)(hwparams, &persize, &dir);
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
516 SDL_NAME(snd_pcm_hw_params_get_periods)(hwparams, &periods, &dir);
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
517
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
518 fprintf(stderr, "ALSA: period size = %ld, periods = %u, buffer size = %lu\n", persize, periods, bufsize);
b312352d8c8d Enable the ALSA debug code via environment variable
Sam Lantinga <slouken@libsdl.org>
parents: 4347
diff changeset
519 }
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
520
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
521 /* Set the software parameters */
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
522 snd_pcm_sw_params_alloca(&swparams);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
523 status = SDL_NAME(snd_pcm_sw_params_current)(pcm_handle, swparams);
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
524 if ( status < 0 ) {
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
525 SDL_SetError("Couldn't get software config: %s", SDL_NAME(snd_strerror)(status));
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
526 ALSA_CloseAudio(this);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
527 return(-1);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
528 }
4331
a24454ed4ac4 Fixed debug code to print correct values
Sam Lantinga <slouken@libsdl.org>
parents: 4329
diff changeset
529 status = SDL_NAME(snd_pcm_sw_params_set_start_threshold)(pcm_handle, swparams, 1);
1552
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
530 if ( status < 0 ) {
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
531 SDL_SetError("Couldn't set start threshold: %s", SDL_NAME(snd_strerror)(status));
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
532 ALSA_CloseAudio(this);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
533 return(-1);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
534 }
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
535 status = SDL_NAME(snd_pcm_sw_params)(pcm_handle, swparams);
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
536 if ( status < 0 ) {
b2462b772cce Fixed bug #79
Sam Lantinga <slouken@libsdl.org>
parents: 1402
diff changeset
537 SDL_SetError("Couldn't set software audio parameters: %s", SDL_NAME(snd_strerror)(status));
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
538 ALSA_CloseAudio(this);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
539 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
540 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
541
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
542 /* Calculate the final parameters for this audio specification */
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
543 SDL_CalculateAudioSpec(spec);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
544
354
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
545 /* Allocate mixing buffer */
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
546 mixlen = spec->size;
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
547 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
548 if ( mixbuf == NULL ) {
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
549 ALSA_CloseAudio(this);
30935e76acb5 Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
550 return(-1);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
551 }
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1331
diff changeset
552 SDL_memset(mixbuf, spec->silence, spec->size);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
553
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
554 /* Get the parent process id (we're the parent of the audio thread) */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
555 parent = getpid();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
556
765
4c2ba6161939 Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents: 547
diff changeset
557 /* Switch to blocking mode for playback */
865
92615154bb68 Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
558 SDL_NAME(snd_pcm_nonblock)(pcm_handle, 0);
765
4c2ba6161939 Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents: 547
diff changeset
559
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
560 /* We're ready to rock and roll. :-) */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
561 return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
562 }