Mercurial > sdl-ios-xcode
annotate src/audio/alsa/SDL_alsa_audio.c @ 547:5d07f9a47f17
Open the ALSA devices in non-blocking mode (thanks Aleksey!)
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 17 Nov 2002 20:01:54 +0000 |
parents | 5868b0f832f2 |
children | 4c2ba6161939 |
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; | |
547
5d07f9a47f17
Open the ALSA devices in non-blocking mode (thanks Aleksey!)
Sam Lantinga <slouken@libsdl.org>
parents:
357
diff
changeset
|
77 status = snd_pcm_open(&handle, get_audio_device(), 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
|
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 } |
356
a1e54d1ba16f
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
354
diff
changeset
|
171 continue; |
0 | 172 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
173 sample_buf += status * this->spec.channels; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
174 sample_len -= status; |
0 | 175 } |
176 } | |
177 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
178 static Uint8 *ALSA_GetAudioBuf(_THIS) |
0 | 179 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
180 return(mixbuf); |
0 | 181 } |
182 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
183 static void ALSA_CloseAudio(_THIS) |
0 | 184 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
185 if ( mixbuf != NULL ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
186 SDL_FreeAudioMem(mixbuf); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
187 mixbuf = NULL; |
0 | 188 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
189 if ( pcm_handle ) { |
357
5868b0f832f2
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
356
diff
changeset
|
190 snd_pcm_drain(pcm_handle); |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
191 snd_pcm_close(pcm_handle); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
192 pcm_handle = NULL; |
0 | 193 } |
194 } | |
195 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
196 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec) |
0 | 197 { |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
198 int status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
199 snd_pcm_hw_params_t *params; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
200 snd_pcm_format_t format; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
201 snd_pcm_uframes_t frames; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
202 Uint16 test_format; |
0 | 203 |
204 /* Open the audio device */ | |
547
5d07f9a47f17
Open the ALSA devices in non-blocking mode (thanks Aleksey!)
Sam Lantinga <slouken@libsdl.org>
parents:
357
diff
changeset
|
205 status = snd_pcm_open(&pcm_handle, get_audio_device(), 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
|
206 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
207 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
|
208 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
209 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
210 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
211 /* 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
|
212 snd_pcm_hw_params_alloca(¶ms); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
213 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
|
214 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
215 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
|
216 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
217 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
218 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
219 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
220 /* SDL only uses interleaved sample output */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
221 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
|
222 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
223 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
|
224 ALSA_CloseAudio(this); |
0 | 225 return(-1); |
226 } | |
227 | |
228 /* 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
|
229 status = -1; |
0 | 230 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
|
231 test_format && (status < 0); ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
232 switch ( test_format ) { |
0 | 233 case AUDIO_U8: |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
234 format = SND_PCM_FORMAT_U8; |
0 | 235 break; |
236 case AUDIO_S8: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
237 format = SND_PCM_FORMAT_S8; |
0 | 238 break; |
239 case AUDIO_S16LSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
240 format = SND_PCM_FORMAT_S16_LE; |
0 | 241 break; |
242 case AUDIO_S16MSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
243 format = SND_PCM_FORMAT_S16_BE; |
0 | 244 break; |
245 case AUDIO_U16LSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
246 format = SND_PCM_FORMAT_U16_LE; |
0 | 247 break; |
248 case AUDIO_U16MSB: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
249 format = SND_PCM_FORMAT_U16_BE; |
0 | 250 break; |
251 default: | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
252 format = 0; |
0 | 253 break; |
254 } | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
255 if ( format != 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
256 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
|
257 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
258 if ( status < 0 ) { |
0 | 259 test_format = SDL_NextAudioFormat(); |
260 } | |
261 } | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
262 if ( status < 0 ) { |
0 | 263 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
|
264 ALSA_CloseAudio(this); |
0 | 265 return(-1); |
266 } | |
267 spec->format = test_format; | |
268 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
269 /* Set the number of channels */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
270 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
|
271 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
272 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
|
273 if ( (status <= 0) || (status > 2) ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
274 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
|
275 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
276 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
277 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
278 spec->channels = status; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
279 } |
0 | 280 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
281 /* Set the audio rate */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
282 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
|
283 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
284 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
|
285 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
286 return(-1); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
287 } |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
288 spec->freq = status; |
0 | 289 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
290 /* Set the buffer size, in samples */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
291 frames = spec->samples; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
292 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
|
293 spec->samples = frames; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
294 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
|
295 |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
296 /* "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
|
297 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
|
298 if ( status < 0 ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
299 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
|
300 ALSA_CloseAudio(this); |
0 | 301 return(-1); |
302 } | |
303 | |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
304 /* 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
|
305 SDL_CalculateAudioSpec(spec); |
0 | 306 |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
307 /* Allocate mixing buffer */ |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
308 mixlen = spec->size; |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
309 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
310 if ( mixbuf == NULL ) { |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
311 ALSA_CloseAudio(this); |
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
312 return(-1); |
0 | 313 } |
354
30935e76acb5
Updated ALSA audio support for ALSA 0.9
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
314 memset(mixbuf, spec->silence, spec->size); |
0 | 315 |
316 /* Get the parent process id (we're the parent of the audio thread) */ | |
317 parent = getpid(); | |
318 | |
319 /* We're ready to rock and roll. :-) */ | |
320 return(0); | |
321 } |