Mercurial > sdl-ios-xcode
annotate src/audio/alsa/SDL_alsa_audio.c @ 1280:f61f045343d3
Re-query the SDL_WINDOWID each time we initialize the video
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 29 Jan 2006 06:11:38 +0000 |
parents | 05d4b93b911e |
children | 1cbaeee565b1 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
765
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
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 | 21 */ |
22 | |
23 | |
24 | |
25 /* Allow access to a raw mixing buffer */ | |
26 | |
27 #include <stdlib.h> | |
28 #include <stdio.h> | |
29 #include <string.h> | |
30 #include <errno.h> | |
31 #include <unistd.h> | |
32 #include <fcntl.h> | |
33 #include <signal.h> | |
34 #include <sys/types.h> | |
35 #include <sys/time.h> | |
36 | |
37 #include "SDL_audio.h" | |
38 #include "SDL_error.h" | |
39 #include "SDL_audiomem.h" | |
40 #include "SDL_audio_c.h" | |
41 #include "SDL_timer.h" | |
42 #include "SDL_alsa_audio.h" | |
43 | |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
44 #ifdef ALSA_DYNAMIC |
939
c7c04f811994
Date: Tue, 01 Jun 2004 15:27:44 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
865
diff
changeset
|
45 #ifdef USE_DLVSYM |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
46 #define __USE_GNU |
939
c7c04f811994
Date: Tue, 01 Jun 2004 15:27:44 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
865
diff
changeset
|
47 #endif |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
48 #include <dlfcn.h> |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
49 #include "SDL_name.h" |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
50 #include "SDL_loadso.h" |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
51 #else |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
52 #define SDL_NAME(X) X |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
53 #endif |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
54 |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
55 |
0 | 56 /* The tag name used by ALSA audio */ |
57 #define DRIVER_NAME "alsa" | |
58 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
59 /* The default ALSA audio driver */ |
765
4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents:
547
diff
changeset
|
60 #define DEFAULT_DEVICE "default" |
0 | 61 |
62 /* Audio driver functions */ | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
63 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
|
64 static void ALSA_WaitAudio(_THIS); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
65 static void ALSA_PlayAudio(_THIS); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
66 static Uint8 *ALSA_GetAudioBuf(_THIS); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
67 static void ALSA_CloseAudio(_THIS); |
0 | 68 |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
69 #ifdef ALSA_DYNAMIC |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
70 |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
71 static const char *alsa_library = ALSA_DYNAMIC; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
72 static void *alsa_handle = NULL; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
73 static int alsa_loaded = 0; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
74 |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
75 static int (*SDL_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
|
76 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
|
77 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
|
78 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
|
79 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
|
80 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
|
81 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
|
82 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
|
83 static size_t (*SDL_NAME(snd_pcm_hw_params_sizeof))(void); |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
84 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
|
85 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
|
86 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
|
87 static int (*SDL_NAME(snd_pcm_hw_params_set_channels))(snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int val); |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
88 static int (*SDL_NAME(snd_pcm_hw_params_get_channels))(const 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
|
89 static unsigned 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); |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
90 static snd_pcm_uframes_t (*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); |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
91 static unsigned 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); |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
92 static int (*SDL_NAME(snd_pcm_hw_params))(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
|
93 static int (*SDL_NAME(snd_pcm_nonblock))(snd_pcm_t *pcm, int nonblock); |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
94 #define snd_pcm_hw_params_sizeof SDL_NAME(snd_pcm_hw_params_sizeof) |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
95 |
1161
05d4b93b911e
Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents:
942
diff
changeset
|
96 /* 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
|
97 static struct { |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
98 const char *name; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
99 void **func; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
100 } alsa_functions[] = { |
1161
05d4b93b911e
Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents:
942
diff
changeset
|
101 { "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
|
102 { "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
|
103 { "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
|
104 { "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
|
105 { "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
|
106 { "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
|
107 { "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
|
108 { "snd_pcm_hw_params_sizeof", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_sizeof) }, |
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_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
|
110 { "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
|
111 { "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
|
112 { "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
|
113 { "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
|
114 { "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
|
115 { "snd_pcm_hw_params_set_period_size_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_period_size_near) }, |
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_set_periods_near", (void**)(char*)&SDL_NAME(snd_pcm_hw_params_set_periods_near) }, |
05d4b93b911e
Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents:
942
diff
changeset
|
117 { "snd_pcm_hw_params", (void**)(char*)&SDL_NAME(snd_pcm_hw_params) }, |
05d4b93b911e
Placate gcc's strict aliasing rules with an extra cast.
Ryan C. Gordon <icculus@icculus.org>
parents:
942
diff
changeset
|
118 { "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
|
119 }; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
120 |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
121 static void UnloadALSALibrary(void) { |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
122 if (alsa_loaded) { |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
123 /* SDL_UnloadObject(alsa_handle);*/ |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
124 dlclose(alsa_handle); |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
125 alsa_handle = NULL; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
126 alsa_loaded = 0; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
127 } |
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 static int LoadALSALibrary(void) { |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
131 int i, retval = -1; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
132 |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
133 /* alsa_handle = SDL_LoadObject(alsa_library);*/ |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
134 alsa_handle = dlopen(alsa_library,RTLD_NOW); |
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; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
138 for (i = 0; i < SDL_TABLESIZE(alsa_functions); i++) { |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
139 /* *alsa_functions[i].func = SDL_LoadFunction(alsa_handle,alsa_functions[i].name);*/ |
939
c7c04f811994
Date: Tue, 01 Jun 2004 15:27:44 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
865
diff
changeset
|
140 #ifdef USE_DLVSYM |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
141 *alsa_functions[i].func = dlvsym(alsa_handle,alsa_functions[i].name,"ALSA_0.9"); |
939
c7c04f811994
Date: Tue, 01 Jun 2004 15:27:44 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
865
diff
changeset
|
142 if (!*alsa_functions[i].func) |
c7c04f811994
Date: Tue, 01 Jun 2004 15:27:44 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
865
diff
changeset
|
143 #endif |
c7c04f811994
Date: Tue, 01 Jun 2004 15:27:44 +0300
Sam Lantinga <slouken@libsdl.org>
parents:
865
diff
changeset
|
144 *alsa_functions[i].func = dlsym(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
|
145 if (!*alsa_functions[i].func) { |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
146 retval = -1; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
147 UnloadALSALibrary(); |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
148 break; |
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 } |
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 return retval; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
153 } |
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 #else |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
156 |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
157 static void UnloadALSALibrary(void) { |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
158 return; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
159 } |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
160 |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
161 static int LoadALSALibrary(void) { |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
162 return 0; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
163 } |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
164 |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
165 #endif /* ALSA_DYNAMIC */ |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
166 |
942
41a59de7f2ed
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents:
939
diff
changeset
|
167 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
|
168 { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
169 const char *device; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
170 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
171 device = getenv("AUDIODEV"); /* Is there a standard variable name? */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
172 if ( device == NULL ) { |
942
41a59de7f2ed
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents:
939
diff
changeset
|
173 if (channels == 6) device = "surround51"; |
41a59de7f2ed
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents:
939
diff
changeset
|
174 else if (channels == 4) device = "surround40"; |
41a59de7f2ed
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents:
939
diff
changeset
|
175 else device = DEFAULT_DEVICE; |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
176 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
177 return device; |
0 | 178 } |
179 | |
180 /* Audio driver bootstrap functions */ | |
181 | |
182 static int Audio_Available(void) | |
183 { | |
184 int available; | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
185 int status; |
0 | 186 snd_pcm_t *handle; |
187 | |
188 available = 0; | |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
189 if (LoadALSALibrary() < 0) { |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
190 return available; |
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
191 } |
942
41a59de7f2ed
Here are patches for SDL12 and SDL_mixer for 4 or 6 channel
Sam Lantinga <slouken@libsdl.org>
parents:
939
diff
changeset
|
192 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
|
193 if ( status >= 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
194 available = 1; |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
195 SDL_NAME(snd_pcm_close)(handle); |
0 | 196 } |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
197 UnloadALSALibrary(); |
0 | 198 return(available); |
199 } | |
200 | |
201 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
202 { | |
203 free(device->hidden); | |
204 free(device); | |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
205 UnloadALSALibrary(); |
0 | 206 } |
207 | |
208 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
209 { | |
210 SDL_AudioDevice *this; | |
211 | |
212 /* 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
|
213 LoadALSALibrary(); |
0 | 214 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); |
215 if ( this ) { | |
216 memset(this, 0, (sizeof *this)); | |
217 this->hidden = (struct SDL_PrivateAudioData *) | |
218 malloc((sizeof *this->hidden)); | |
219 } | |
220 if ( (this == NULL) || (this->hidden == NULL) ) { | |
221 SDL_OutOfMemory(); | |
222 if ( this ) { | |
223 free(this); | |
224 } | |
225 return(0); | |
226 } | |
227 memset(this->hidden, 0, (sizeof *this->hidden)); | |
228 | |
229 /* Set the function pointers */ | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
230 this->OpenAudio = ALSA_OpenAudio; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
231 this->WaitAudio = ALSA_WaitAudio; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
232 this->PlayAudio = ALSA_PlayAudio; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
233 this->GetAudioBuf = ALSA_GetAudioBuf; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
234 this->CloseAudio = ALSA_CloseAudio; |
0 | 235 |
236 this->free = Audio_DeleteDevice; | |
237 | |
238 return this; | |
239 } | |
240 | |
241 AudioBootStrap ALSA_bootstrap = { | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
242 DRIVER_NAME, "ALSA 0.9 PCM audio", |
0 | 243 Audio_Available, Audio_CreateDevice |
244 }; | |
245 | |
246 /* 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
|
247 static void ALSA_WaitAudio(_THIS) |
0 | 248 { |
249 /* Check to see if the thread-parent process is still alive */ | |
250 { static int cnt = 0; | |
251 /* Note that this only works with thread implementations | |
252 that use a different process id for each thread. | |
253 */ | |
254 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */ | |
255 if ( kill(parent, 0) < 0 ) { | |
256 this->enabled = 0; | |
257 } | |
258 } | |
259 } | |
260 } | |
261 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
262 static void ALSA_PlayAudio(_THIS) |
0 | 263 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
264 int status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
265 int sample_len; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
266 signed short *sample_buf; |
765
4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents:
547
diff
changeset
|
267 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
268 sample_len = this->spec.samples; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
269 sample_buf = (signed short *)mixbuf; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
270 while ( sample_len > 0 ) { |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
271 status = SDL_NAME(snd_pcm_writei)(pcm_handle, sample_buf, sample_len); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
272 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
273 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
|
274 SDL_Delay(1); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
275 continue; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
276 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
277 if ( status == -ESTRPIPE ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
278 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
|
279 SDL_Delay(1); |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
280 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
|
281 } while ( status == -EAGAIN ); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
282 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
283 if ( status < 0 ) { |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
284 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
|
285 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
286 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
287 /* 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
|
288 this->enabled = 0; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
289 return; |
0 | 290 } |
356
a1e54d1ba16f
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
354
diff
changeset
|
291 continue; |
0 | 292 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
293 sample_buf += status * this->spec.channels; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
294 sample_len -= status; |
0 | 295 } |
296 } | |
297 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
298 static Uint8 *ALSA_GetAudioBuf(_THIS) |
0 | 299 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
300 return(mixbuf); |
0 | 301 } |
302 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
303 static void ALSA_CloseAudio(_THIS) |
0 | 304 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
305 if ( mixbuf != NULL ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
306 SDL_FreeAudioMem(mixbuf); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
307 mixbuf = NULL; |
0 | 308 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
309 if ( pcm_handle ) { |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
310 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
|
311 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
|
312 pcm_handle = NULL; |
0 | 313 } |
314 } | |
315 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
316 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec) |
0 | 317 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
318 int status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
319 snd_pcm_hw_params_t *params; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
320 snd_pcm_format_t format; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
321 snd_pcm_uframes_t frames; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
322 Uint16 test_format; |
0 | 323 |
324 /* 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
|
325 /* 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
|
326 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
|
327 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
328 if ( status < 0 ) { |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
329 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
|
330 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
331 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
332 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
333 /* Figure out what the hardware is capable of */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
334 snd_pcm_hw_params_alloca(¶ms); |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
335 status = SDL_NAME(snd_pcm_hw_params_any)(pcm_handle, params); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
336 if ( status < 0 ) { |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
337 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
|
338 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
339 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
340 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
341 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
342 /* SDL only uses interleaved sample output */ |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
343 status = SDL_NAME(snd_pcm_hw_params_set_access)(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
344 if ( status < 0 ) { |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
345 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
|
346 ALSA_CloseAudio(this); |
0 | 347 return(-1); |
348 } | |
349 | |
350 /* 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
|
351 status = -1; |
0 | 352 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
|
353 test_format && (status < 0); ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
354 switch ( test_format ) { |
0 | 355 case AUDIO_U8: |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
356 format = SND_PCM_FORMAT_U8; |
0 | 357 break; |
358 case AUDIO_S8: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
359 format = SND_PCM_FORMAT_S8; |
0 | 360 break; |
361 case AUDIO_S16LSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
362 format = SND_PCM_FORMAT_S16_LE; |
0 | 363 break; |
364 case AUDIO_S16MSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
365 format = SND_PCM_FORMAT_S16_BE; |
0 | 366 break; |
367 case AUDIO_U16LSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
368 format = SND_PCM_FORMAT_U16_LE; |
0 | 369 break; |
370 case AUDIO_U16MSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
371 format = SND_PCM_FORMAT_U16_BE; |
0 | 372 break; |
373 default: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
374 format = 0; |
0 | 375 break; |
376 } | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
377 if ( format != 0 ) { |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
378 status = SDL_NAME(snd_pcm_hw_params_set_format)(pcm_handle, params, format); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
379 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
380 if ( status < 0 ) { |
0 | 381 test_format = SDL_NextAudioFormat(); |
382 } | |
383 } | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
384 if ( status < 0 ) { |
0 | 385 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
|
386 ALSA_CloseAudio(this); |
0 | 387 return(-1); |
388 } | |
389 spec->format = test_format; | |
390 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
391 /* Set the number of channels */ |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
392 status = SDL_NAME(snd_pcm_hw_params_set_channels)(pcm_handle, params, spec->channels); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
393 if ( status < 0 ) { |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
394 status = SDL_NAME(snd_pcm_hw_params_get_channels)(params); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
395 if ( (status <= 0) || (status > 2) ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
396 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
|
397 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
398 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
399 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
400 spec->channels = status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
401 } |
0 | 402 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
403 /* Set the audio rate */ |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
404 status = SDL_NAME(snd_pcm_hw_params_set_rate_near)(pcm_handle, params, spec->freq, NULL); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
405 if ( status < 0 ) { |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
406 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
|
407 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
408 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
409 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
410 spec->freq = status; |
0 | 411 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
412 /* Set the buffer size, in samples */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
413 frames = spec->samples; |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
414 frames = SDL_NAME(snd_pcm_hw_params_set_period_size_near)(pcm_handle, params, frames, NULL); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
415 spec->samples = frames; |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
416 SDL_NAME(snd_pcm_hw_params_set_periods_near)(pcm_handle, params, 2, NULL); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
417 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
418 /* "set" the hardware with the desired parameters */ |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
419 status = SDL_NAME(snd_pcm_hw_params)(pcm_handle, params); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
420 if ( status < 0 ) { |
865
92615154bb68
Date: Sun, 29 Feb 2004 15:14:22 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
421 SDL_SetError("Couldn't set 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
|
422 ALSA_CloseAudio(this); |
0 | 423 return(-1); |
424 } | |
425 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
426 /* 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
|
427 SDL_CalculateAudioSpec(spec); |
0 | 428 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
429 /* Allocate mixing buffer */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
430 mixlen = spec->size; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
431 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
432 if ( mixbuf == NULL ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
433 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
434 return(-1); |
0 | 435 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
436 memset(mixbuf, spec->silence, spec->size); |
0 | 437 |
438 /* Get the parent process id (we're the parent of the audio thread) */ | |
439 parent = getpid(); | |
440 | |
765
4c2ba6161939
Editors Note: The original patch was modified to use SDL_Delay() instead of
Sam Lantinga <slouken@libsdl.org>
parents:
547
diff
changeset
|
441 /* 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
|
442 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
|
443 |
0 | 444 /* We're ready to rock and roll. :-) */ |
445 return(0); | |
446 } |