Mercurial > sdl-ios-xcode
annotate src/audio/alsa/SDL_alsa_audio.c @ 354:30935e76acb5
Updated ALSA audio support for ALSA 0.9
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 15 Apr 2002 07:38:54 +0000 |
parents | f6ffac90895c |
children | a1e54d1ba16f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
297
f6ffac90895c
Updated copyright information for 2002
Sam Lantinga <slouken@libsdl.org>
parents:
252
diff
changeset
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 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 | |
44 /* The tag name used by ALSA audio */ | |
45 #define DRIVER_NAME "alsa" | |
46 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
47 /* The default ALSA audio driver */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
48 #define DEFAULT_DEVICE "plughw:0,0" |
0 | 49 |
50 /* Audio driver functions */ | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
51 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
|
52 static void ALSA_WaitAudio(_THIS); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
53 static void ALSA_PlayAudio(_THIS); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
54 static Uint8 *ALSA_GetAudioBuf(_THIS); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
55 static void ALSA_CloseAudio(_THIS); |
0 | 56 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
57 static const char *get_audio_device() |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
58 { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
59 const char *device; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
60 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
61 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
|
62 if ( device == NULL ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
63 device = DEFAULT_DEVICE; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
64 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
65 return device; |
0 | 66 } |
67 | |
68 /* Audio driver bootstrap functions */ | |
69 | |
70 static int Audio_Available(void) | |
71 { | |
72 int available; | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
73 int status; |
0 | 74 snd_pcm_t *handle; |
75 | |
76 available = 0; | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
77 status = snd_pcm_open(&handle, get_audio_device(), SND_PCM_STREAM_PLAYBACK, 0); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
78 if ( status >= 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
79 available = 1; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
80 snd_pcm_close(handle); |
0 | 81 } |
82 return(available); | |
83 } | |
84 | |
85 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
86 { | |
87 free(device->hidden); | |
88 free(device); | |
89 } | |
90 | |
91 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
92 { | |
93 SDL_AudioDevice *this; | |
94 | |
95 /* Initialize all variables that we clean on shutdown */ | |
96 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); | |
97 if ( this ) { | |
98 memset(this, 0, (sizeof *this)); | |
99 this->hidden = (struct SDL_PrivateAudioData *) | |
100 malloc((sizeof *this->hidden)); | |
101 } | |
102 if ( (this == NULL) || (this->hidden == NULL) ) { | |
103 SDL_OutOfMemory(); | |
104 if ( this ) { | |
105 free(this); | |
106 } | |
107 return(0); | |
108 } | |
109 memset(this->hidden, 0, (sizeof *this->hidden)); | |
110 | |
111 /* Set the function pointers */ | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
112 this->OpenAudio = ALSA_OpenAudio; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
113 this->WaitAudio = ALSA_WaitAudio; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
114 this->PlayAudio = ALSA_PlayAudio; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
115 this->GetAudioBuf = ALSA_GetAudioBuf; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
116 this->CloseAudio = ALSA_CloseAudio; |
0 | 117 |
118 this->free = Audio_DeleteDevice; | |
119 | |
120 return this; | |
121 } | |
122 | |
123 AudioBootStrap ALSA_bootstrap = { | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
124 DRIVER_NAME, "ALSA 0.9 PCM audio", |
0 | 125 Audio_Available, Audio_CreateDevice |
126 }; | |
127 | |
128 /* 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
|
129 static void ALSA_WaitAudio(_THIS) |
0 | 130 { |
131 /* Check to see if the thread-parent process is still alive */ | |
132 { static int cnt = 0; | |
133 /* Note that this only works with thread implementations | |
134 that use a different process id for each thread. | |
135 */ | |
136 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */ | |
137 if ( kill(parent, 0) < 0 ) { | |
138 this->enabled = 0; | |
139 } | |
140 } | |
141 } | |
142 } | |
143 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
144 static void ALSA_PlayAudio(_THIS) |
0 | 145 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
146 int status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
147 int sample_len; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
148 signed short *sample_buf; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
149 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
150 sample_len = this->spec.samples; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
151 sample_buf = (signed short *)mixbuf; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
152 while ( sample_len > 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
153 status = snd_pcm_writei(pcm_handle, sample_buf, sample_len); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
154 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
155 if ( status == -EAGAIN ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
156 continue; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
157 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
158 if ( status == -ESTRPIPE ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
159 do { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
160 status = snd_pcm_resume(pcm_handle); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
161 } while ( status == -EAGAIN ); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
162 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
163 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
164 status = snd_pcm_prepare(pcm_handle); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
165 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
166 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
167 /* 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
|
168 this->enabled = 0; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
169 return; |
0 | 170 } |
171 } | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
172 sample_buf += status * this->spec.channels; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
173 sample_len -= status; |
0 | 174 } |
175 } | |
176 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
177 static Uint8 *ALSA_GetAudioBuf(_THIS) |
0 | 178 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
179 return(mixbuf); |
0 | 180 } |
181 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
182 static void ALSA_CloseAudio(_THIS) |
0 | 183 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
184 if ( mixbuf != NULL ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
185 SDL_FreeAudioMem(mixbuf); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
186 mixbuf = NULL; |
0 | 187 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
188 if ( pcm_handle ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
189 snd_pcm_close(pcm_handle); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
190 pcm_handle = NULL; |
0 | 191 } |
192 } | |
193 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
194 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec) |
0 | 195 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
196 int status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
197 snd_pcm_hw_params_t *params; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
198 snd_pcm_format_t format; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
199 snd_pcm_uframes_t frames; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
200 Uint16 test_format; |
0 | 201 |
202 /* Open the audio device */ | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
203 status = snd_pcm_open(&pcm_handle, get_audio_device(), SND_PCM_STREAM_PLAYBACK, 0); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
204 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
205 SDL_SetError("Couldn't open audio device: %s", snd_strerror(status)); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
206 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
207 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
208 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
209 /* 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
|
210 snd_pcm_hw_params_alloca(¶ms); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
211 status = snd_pcm_hw_params_any(pcm_handle, params); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
212 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
213 SDL_SetError("Couldn't get hardware config: %s", snd_strerror(status)); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
214 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
215 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
216 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
217 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
218 /* SDL only uses interleaved sample output */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
219 status = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
220 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
221 SDL_SetError("Couldn't set interleaved access: %s", snd_strerror(status)); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
222 ALSA_CloseAudio(this); |
0 | 223 return(-1); |
224 } | |
225 | |
226 /* 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
|
227 status = -1; |
0 | 228 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
|
229 test_format && (status < 0); ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
230 switch ( test_format ) { |
0 | 231 case AUDIO_U8: |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
232 format = SND_PCM_FORMAT_U8; |
0 | 233 break; |
234 case AUDIO_S8: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
235 format = SND_PCM_FORMAT_S8; |
0 | 236 break; |
237 case AUDIO_S16LSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
238 format = SND_PCM_FORMAT_S16_LE; |
0 | 239 break; |
240 case AUDIO_S16MSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
241 format = SND_PCM_FORMAT_S16_BE; |
0 | 242 break; |
243 case AUDIO_U16LSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
244 format = SND_PCM_FORMAT_U16_LE; |
0 | 245 break; |
246 case AUDIO_U16MSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
247 format = SND_PCM_FORMAT_U16_BE; |
0 | 248 break; |
249 default: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
250 format = 0; |
0 | 251 break; |
252 } | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
253 if ( format != 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
254 status = snd_pcm_hw_params_set_format(pcm_handle, params, format); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
255 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
256 if ( status < 0 ) { |
0 | 257 test_format = SDL_NextAudioFormat(); |
258 } | |
259 } | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
260 if ( status < 0 ) { |
0 | 261 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
|
262 ALSA_CloseAudio(this); |
0 | 263 return(-1); |
264 } | |
265 spec->format = test_format; | |
266 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
267 /* Set the number of channels */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
268 status = snd_pcm_hw_params_set_channels(pcm_handle, params, spec->channels); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
269 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
270 status = snd_pcm_hw_params_get_channels(params); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
271 if ( (status <= 0) || (status > 2) ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
272 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
|
273 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
274 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
275 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
276 spec->channels = status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
277 } |
0 | 278 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
279 /* Set the audio rate */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
280 status = snd_pcm_hw_params_set_rate_near(pcm_handle, params, spec->freq, NULL); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
281 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
282 SDL_SetError("Couldn't set audio frequency: %s", snd_strerror(status)); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
283 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
284 return(-1); |
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 spec->freq = status; |
0 | 287 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
288 /* Set the buffer size, in samples */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
289 frames = spec->samples; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
290 frames = snd_pcm_hw_params_set_period_size_near(pcm_handle, params, frames, NULL); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
291 spec->samples = frames; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
292 snd_pcm_hw_params_set_periods_near(pcm_handle, params, 2, NULL); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
293 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
294 /* "set" the hardware with the desired parameters */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
295 status = snd_pcm_hw_params(pcm_handle, params); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
296 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
297 SDL_SetError("Couldn't set audio parameters: %s", snd_strerror(status)); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
298 ALSA_CloseAudio(this); |
0 | 299 return(-1); |
300 } | |
301 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
302 /* 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
|
303 SDL_CalculateAudioSpec(spec); |
0 | 304 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
305 /* Allocate mixing buffer */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
306 mixlen = spec->size; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
307 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
308 if ( mixbuf == NULL ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
309 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
310 return(-1); |
0 | 311 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
312 memset(mixbuf, spec->silence, spec->size); |
0 | 313 |
314 /* Get the parent process id (we're the parent of the audio thread) */ | |
315 parent = getpid(); | |
316 | |
317 /* We're ready to rock and roll. :-) */ | |
318 return(0); | |
319 } |