Mercurial > sdl-ios-xcode
comparison 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 |
comparison
equal
deleted
inserted
replaced
353:d47637068e52 | 354:30935e76acb5 |
---|---|
42 #include "SDL_alsa_audio.h" | 42 #include "SDL_alsa_audio.h" |
43 | 43 |
44 /* The tag name used by ALSA audio */ | 44 /* The tag name used by ALSA audio */ |
45 #define DRIVER_NAME "alsa" | 45 #define DRIVER_NAME "alsa" |
46 | 46 |
47 /* default card and device numbers as listed in dev/snd */ | 47 /* The default ALSA audio driver */ |
48 static int card_no = 0; | 48 #define DEFAULT_DEVICE "plughw:0,0" |
49 static int device_no = 0; | |
50 | |
51 /* default channel communication parameters */ | |
52 #define DEFAULT_CPARAMS_RATE 22050 | |
53 #define DEFAULT_CPARAMS_VOICES 1 | |
54 #define DEFAULT_CPARAMS_FRAG_SIZE 512 | |
55 #define DEFAULT_CPARAMS_FRAGS_MIN 1 | |
56 #define DEFAULT_CPARAMS_FRAGS_MAX -1 | |
57 | |
58 /* Open the audio device for playback, and don't block if busy */ | |
59 #define OPEN_FLAGS (SND_PCM_OPEN_PLAYBACK|SND_PCM_OPEN_NONBLOCK) | |
60 | 49 |
61 /* Audio driver functions */ | 50 /* Audio driver functions */ |
62 static int PCM_OpenAudio(_THIS, SDL_AudioSpec *spec); | 51 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec); |
63 static void PCM_WaitAudio(_THIS); | 52 static void ALSA_WaitAudio(_THIS); |
64 static void PCM_PlayAudio(_THIS); | 53 static void ALSA_PlayAudio(_THIS); |
65 static Uint8 *PCM_GetAudioBuf(_THIS); | 54 static Uint8 *ALSA_GetAudioBuf(_THIS); |
66 static void PCM_CloseAudio(_THIS); | 55 static void ALSA_CloseAudio(_THIS); |
67 | 56 |
68 /* PCM transfer channel parameters initialize function */ | 57 static const char *get_audio_device() |
69 static void init_pcm_cparams(snd_pcm_channel_params_t* cparams) | 58 { |
70 { | 59 const char *device; |
71 memset(cparams,0,sizeof(snd_pcm_channel_params_t)); | 60 |
72 | 61 device = getenv("AUDIODEV"); /* Is there a standard variable name? */ |
73 cparams->channel = SND_PCM_CHANNEL_PLAYBACK; | 62 if ( device == NULL ) { |
74 cparams->mode = SND_PCM_MODE_BLOCK; | 63 device = DEFAULT_DEVICE; |
75 cparams->start_mode = SND_PCM_START_DATA; //_FULL | 64 } |
76 cparams->stop_mode = SND_PCM_STOP_STOP; | 65 return device; |
77 cparams->format.format = SND_PCM_SFMT_S16_LE; | |
78 cparams->format.interleave = 1; | |
79 cparams->format.rate = DEFAULT_CPARAMS_RATE; | |
80 cparams->format.voices = DEFAULT_CPARAMS_VOICES; | |
81 cparams->buf.block.frag_size = DEFAULT_CPARAMS_FRAG_SIZE; | |
82 cparams->buf.block.frags_min = DEFAULT_CPARAMS_FRAGS_MIN; | |
83 cparams->buf.block.frags_max = DEFAULT_CPARAMS_FRAGS_MAX; | |
84 } | 66 } |
85 | 67 |
86 /* Audio driver bootstrap functions */ | 68 /* Audio driver bootstrap functions */ |
87 | 69 |
88 static int Audio_Available(void) | 70 static int Audio_Available(void) |
89 /* | |
90 See if we can open a nonblocking channel. | |
91 Return value '1' means we can. | |
92 Return value '0' means we cannot. | |
93 */ | |
94 { | 71 { |
95 int available; | 72 int available; |
96 int rval; | 73 int status; |
97 snd_pcm_t *handle; | 74 snd_pcm_t *handle; |
98 snd_pcm_channel_params_t cparams; | |
99 #ifdef DEBUG_AUDIO | |
100 snd_pcm_channel_status_t cstatus; | |
101 #endif | |
102 | 75 |
103 available = 0; | 76 available = 0; |
104 handle = NULL; | 77 status = snd_pcm_open(&handle, get_audio_device(), SND_PCM_STREAM_PLAYBACK, 0); |
105 | 78 if ( status >= 0 ) { |
106 init_pcm_cparams(&cparams); | 79 available = 1; |
107 | 80 snd_pcm_close(handle); |
108 rval = snd_pcm_open(&handle, card_no, device_no, OPEN_FLAGS); | 81 } |
109 if (rval >= 0) | |
110 { | |
111 rval = snd_pcm_plugin_params(handle, &cparams); | |
112 | |
113 #ifdef DEBUG_AUDIO | |
114 snd_pcm_plugin_status(handle, &cstatus); | |
115 printf("status after snd_pcm_plugin_params call = %d\n",cstatus.status); | |
116 #endif | |
117 if (rval >= 0) | |
118 { | |
119 available = 1; | |
120 } | |
121 else | |
122 { | |
123 SDL_SetError("snd_pcm_channel_params failed: %s\n", snd_strerror (rval)); | |
124 } | |
125 | |
126 if ((rval = snd_pcm_close(handle)) < 0) | |
127 { | |
128 SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval)); | |
129 available = 0; | |
130 } | |
131 } | |
132 else | |
133 { | |
134 SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval)); | |
135 } | |
136 | |
137 return(available); | 82 return(available); |
138 } | 83 } |
139 | 84 |
140 static void Audio_DeleteDevice(SDL_AudioDevice *device) | 85 static void Audio_DeleteDevice(SDL_AudioDevice *device) |
141 { | 86 { |
160 free(this); | 105 free(this); |
161 } | 106 } |
162 return(0); | 107 return(0); |
163 } | 108 } |
164 memset(this->hidden, 0, (sizeof *this->hidden)); | 109 memset(this->hidden, 0, (sizeof *this->hidden)); |
165 audio_handle = NULL; | |
166 | 110 |
167 /* Set the function pointers */ | 111 /* Set the function pointers */ |
168 this->OpenAudio = PCM_OpenAudio; | 112 this->OpenAudio = ALSA_OpenAudio; |
169 this->WaitAudio = PCM_WaitAudio; | 113 this->WaitAudio = ALSA_WaitAudio; |
170 this->PlayAudio = PCM_PlayAudio; | 114 this->PlayAudio = ALSA_PlayAudio; |
171 this->GetAudioBuf = PCM_GetAudioBuf; | 115 this->GetAudioBuf = ALSA_GetAudioBuf; |
172 this->CloseAudio = PCM_CloseAudio; | 116 this->CloseAudio = ALSA_CloseAudio; |
173 | 117 |
174 this->free = Audio_DeleteDevice; | 118 this->free = Audio_DeleteDevice; |
175 | 119 |
176 return this; | 120 return this; |
177 } | 121 } |
178 | 122 |
179 AudioBootStrap ALSA_bootstrap = { | 123 AudioBootStrap ALSA_bootstrap = { |
180 DRIVER_NAME, "ALSA PCM audio", | 124 DRIVER_NAME, "ALSA 0.9 PCM audio", |
181 Audio_Available, Audio_CreateDevice | 125 Audio_Available, Audio_CreateDevice |
182 }; | 126 }; |
183 | 127 |
184 /* This function waits until it is possible to write a full sound buffer */ | 128 /* This function waits until it is possible to write a full sound buffer */ |
185 static void PCM_WaitAudio(_THIS) | 129 static void ALSA_WaitAudio(_THIS) |
186 { | 130 { |
187 | |
188 /* Check to see if the thread-parent process is still alive */ | 131 /* Check to see if the thread-parent process is still alive */ |
189 { static int cnt = 0; | 132 { static int cnt = 0; |
190 /* Note that this only works with thread implementations | 133 /* Note that this only works with thread implementations |
191 that use a different process id for each thread. | 134 that use a different process id for each thread. |
192 */ | 135 */ |
194 if ( kill(parent, 0) < 0 ) { | 137 if ( kill(parent, 0) < 0 ) { |
195 this->enabled = 0; | 138 this->enabled = 0; |
196 } | 139 } |
197 } | 140 } |
198 } | 141 } |
199 | 142 } |
200 /* See if we need to use timed audio synchronization */ | 143 |
201 if ( frame_ticks ) | 144 static void ALSA_PlayAudio(_THIS) |
202 { | 145 { |
203 /* Use timer for general audio synchronization */ | 146 int status; |
204 Sint32 ticks; | 147 int sample_len; |
205 | 148 signed short *sample_buf; |
206 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS; | 149 |
207 if ( ticks > 0 ) | 150 sample_len = this->spec.samples; |
208 { | 151 sample_buf = (signed short *)mixbuf; |
209 SDL_Delay(ticks); | 152 while ( sample_len > 0 ) { |
210 } | 153 status = snd_pcm_writei(pcm_handle, sample_buf, sample_len); |
211 } | 154 if ( status < 0 ) { |
212 else | 155 if ( status == -EAGAIN ) { |
213 { | 156 continue; |
214 /* Use select() for audio synchronization */ | 157 } |
215 fd_set fdset; | 158 if ( status == -ESTRPIPE ) { |
216 struct timeval timeout; | 159 do { |
217 FD_ZERO(&fdset); | 160 status = snd_pcm_resume(pcm_handle); |
218 FD_SET(audio_fd, &fdset); | 161 } while ( status == -EAGAIN ); |
219 timeout.tv_sec = 10; | 162 } |
220 timeout.tv_usec = 0; | 163 if ( status < 0 ) { |
221 #ifdef DEBUG_AUDIO | 164 status = snd_pcm_prepare(pcm_handle); |
222 fprintf(stderr, "Waiting for audio to get ready\n"); | 165 } |
223 #endif | 166 if ( status < 0 ) { |
224 if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) | 167 /* Hmm, not much we can do - abort */ |
225 { | 168 this->enabled = 0; |
226 const char *message = | 169 return; |
227 "Audio timeout - buggy audio driver? (disabled)"; | 170 } |
228 /* In general we should never print to the screen, | 171 } |
229 but in this case we have no other way of letting | 172 sample_buf += status * this->spec.channels; |
230 the user know what happened. | 173 sample_len -= status; |
231 */ | 174 } |
232 fprintf(stderr, "SDL: %s\n", message); | 175 } |
233 this->enabled = 0; | 176 |
234 /* Don't try to close - may hang */ | 177 static Uint8 *ALSA_GetAudioBuf(_THIS) |
235 audio_fd = -1; | 178 { |
236 #ifdef DEBUG_AUDIO | 179 return(mixbuf); |
237 fprintf(stderr, "Done disabling audio\n"); | 180 } |
238 #endif | 181 |
239 } | 182 static void ALSA_CloseAudio(_THIS) |
240 #ifdef DEBUG_AUDIO | 183 { |
241 fprintf(stderr, "Ready!\n"); | 184 if ( mixbuf != NULL ) { |
242 #endif | 185 SDL_FreeAudioMem(mixbuf); |
243 } | 186 mixbuf = NULL; |
244 } | 187 } |
245 | 188 if ( pcm_handle ) { |
246 static snd_pcm_channel_status_t cstatus; | 189 snd_pcm_close(pcm_handle); |
247 | 190 pcm_handle = NULL; |
248 static void PCM_PlayAudio(_THIS) | 191 } |
249 { | 192 } |
250 int written, rval; | 193 |
251 | 194 static int ALSA_OpenAudio(_THIS, SDL_AudioSpec *spec) |
252 /* Write the audio data, checking for EAGAIN (buffer full) and underrun */ | 195 { |
253 do { | 196 int status; |
254 written = snd_pcm_plugin_write(audio_handle, pcm_buf, pcm_len); | 197 snd_pcm_hw_params_t *params; |
255 #ifdef DEBUG_AUDIO | 198 snd_pcm_format_t format; |
256 fprintf(stderr, "written = %d pcm_len = %d\n",written,pcm_len); | 199 snd_pcm_uframes_t frames; |
257 #endif | 200 Uint16 test_format; |
258 if (written != pcm_len) | |
259 { | |
260 if (errno == EAGAIN) | |
261 { | |
262 SDL_Delay(1); /* Let a little CPU time go by and try to write again */ | |
263 #ifdef DEBUG_AUDIO | |
264 fprintf(stderr, "errno == EAGAIN\n"); | |
265 #endif | |
266 } | |
267 else | |
268 { | |
269 if( (rval = snd_pcm_plugin_status(audio_handle, &cstatus)) < 0 ) | |
270 { | |
271 SDL_SetError("snd_pcm_plugin_status failed: %s\n", snd_strerror(rval)); | |
272 return; | |
273 } | |
274 if ( (cstatus.status == SND_PCM_STATUS_UNDERRUN) | |
275 ||(cstatus.status == SND_PCM_STATUS_READY) ) | |
276 { | |
277 #ifdef DEBUG_AUDIO | |
278 fprintf(stderr, "buffer underrun\n"); | |
279 #endif | |
280 if ( (rval = snd_pcm_plugin_prepare (audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0 ) | |
281 { | |
282 SDL_SetError("snd_pcm_plugin_prepare failed: %s\n",snd_strerror(rval) ); | |
283 return; | |
284 } | |
285 /* if we reach here, try to write again */ | |
286 } | |
287 } | |
288 } | |
289 } while ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) ); | |
290 | |
291 /* Set the next write frame */ | |
292 if ( frame_ticks ) { | |
293 next_frame += frame_ticks; | |
294 } | |
295 | |
296 /* If we couldn't write, assume fatal error for now */ | |
297 if ( written < 0 ) { | |
298 this->enabled = 0; | |
299 } | |
300 return; | |
301 } | |
302 | |
303 static Uint8 *PCM_GetAudioBuf(_THIS) | |
304 { | |
305 return(pcm_buf); | |
306 } | |
307 | |
308 static void PCM_CloseAudio(_THIS) | |
309 { | |
310 int rval; | |
311 | |
312 if ( pcm_buf != NULL ) { | |
313 free(pcm_buf); | |
314 pcm_buf = NULL; | |
315 } | |
316 if ( audio_handle != NULL ) { | |
317 if ((rval = snd_pcm_plugin_flush(audio_handle,SND_PCM_CHANNEL_PLAYBACK)) < 0) | |
318 { | |
319 SDL_SetError("snd_pcm_plugin_flush failed: %s\n",snd_strerror(rval)); | |
320 return; | |
321 } | |
322 if ((rval = snd_pcm_close(audio_handle)) < 0) | |
323 { | |
324 SDL_SetError("snd_pcm_close failed: %s\n",snd_strerror(rval)); | |
325 return; | |
326 } | |
327 audio_handle = NULL; | |
328 } | |
329 } | |
330 | |
331 static int PCM_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
332 { | |
333 int rval; | |
334 snd_pcm_channel_params_t cparams; | |
335 snd_pcm_channel_setup_t csetup; | |
336 int format; | |
337 Uint16 test_format; | |
338 int twidth; | |
339 | |
340 /* initialize channel transfer parameters to default */ | |
341 init_pcm_cparams(&cparams); | |
342 | |
343 /* Reset the timer synchronization flag */ | |
344 frame_ticks = 0.0; | |
345 | 201 |
346 /* Open the audio device */ | 202 /* Open the audio device */ |
347 | 203 status = snd_pcm_open(&pcm_handle, get_audio_device(), SND_PCM_STREAM_PLAYBACK, 0); |
348 rval = snd_pcm_open(&audio_handle, card_no, device_no, OPEN_FLAGS); | 204 if ( status < 0 ) { |
349 if ( rval < 0 ) { | 205 SDL_SetError("Couldn't open audio device: %s", snd_strerror(status)); |
350 SDL_SetError("snd_pcm_open failed: %s\n", snd_strerror(rval)); | 206 return(-1); |
351 return(-1); | 207 } |
352 } | 208 |
353 | 209 /* Figure out what the hardware is capable of */ |
354 #ifdef PLUGIN_DISABLE_MMAP /* This is gone in newer versions of ALSA? */ | 210 snd_pcm_hw_params_alloca(¶ms); |
355 /* disable count status parameter */ | 211 status = snd_pcm_hw_params_any(pcm_handle, params); |
356 if ((rval = snd_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP))<0) | 212 if ( status < 0 ) { |
357 { | 213 SDL_SetError("Couldn't get hardware config: %s", snd_strerror(status)); |
358 SDL_SetError("snd_plugin_set_disable failed: %s\n", snd_strerror(rval)); | 214 ALSA_CloseAudio(this); |
359 return(-1); | 215 return(-1); |
360 } | 216 } |
361 #endif | 217 |
362 | 218 /* SDL only uses interleaved sample output */ |
363 pcm_buf = NULL; | 219 status = snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED); |
220 if ( status < 0 ) { | |
221 SDL_SetError("Couldn't set interleaved access: %s", snd_strerror(status)); | |
222 ALSA_CloseAudio(this); | |
223 return(-1); | |
224 } | |
364 | 225 |
365 /* Try for a closest match on audio format */ | 226 /* Try for a closest match on audio format */ |
366 format = 0; | 227 status = -1; |
367 for ( test_format = SDL_FirstAudioFormat(spec->format); | 228 for ( test_format = SDL_FirstAudioFormat(spec->format); |
368 ! format && test_format; ) | 229 test_format && (status < 0); ) { |
369 { | 230 switch ( test_format ) { |
370 #ifdef DEBUG_AUDIO | |
371 fprintf(stderr, "Trying format 0x%4.4x spec->samples %d\n", test_format,spec->samples); | |
372 #endif | |
373 /* if match found set format to equivalent ALSA format */ | |
374 switch ( test_format ) { | |
375 case AUDIO_U8: | 231 case AUDIO_U8: |
376 format = SND_PCM_SFMT_U8; | 232 format = SND_PCM_FORMAT_U8; |
377 cparams.buf.block.frag_size = spec->samples * spec->channels; | |
378 break; | 233 break; |
379 case AUDIO_S8: | 234 case AUDIO_S8: |
380 format = SND_PCM_SFMT_S8; | 235 format = SND_PCM_FORMAT_S8; |
381 cparams.buf.block.frag_size = spec->samples * spec->channels; | |
382 break; | 236 break; |
383 case AUDIO_S16LSB: | 237 case AUDIO_S16LSB: |
384 format = SND_PCM_SFMT_S16_LE; | 238 format = SND_PCM_FORMAT_S16_LE; |
385 cparams.buf.block.frag_size = spec->samples*2 * spec->channels; | |
386 break; | 239 break; |
387 case AUDIO_S16MSB: | 240 case AUDIO_S16MSB: |
388 format = SND_PCM_SFMT_S16_BE; | 241 format = SND_PCM_FORMAT_S16_BE; |
389 cparams.buf.block.frag_size = spec->samples*2 * spec->channels; | |
390 break; | 242 break; |
391 case AUDIO_U16LSB: | 243 case AUDIO_U16LSB: |
392 format = SND_PCM_SFMT_U16_LE; | 244 format = SND_PCM_FORMAT_U16_LE; |
393 cparams.buf.block.frag_size = spec->samples*2 * spec->channels; | |
394 break; | 245 break; |
395 case AUDIO_U16MSB: | 246 case AUDIO_U16MSB: |
396 format = SND_PCM_SFMT_U16_BE; | 247 format = SND_PCM_FORMAT_U16_BE; |
397 cparams.buf.block.frag_size = spec->samples*2 * spec->channels; | |
398 break; | 248 break; |
399 default: | 249 default: |
400 break; | 250 format = 0; |
401 } | 251 break; |
402 if ( ! format ) { | 252 } |
253 if ( format != 0 ) { | |
254 status = snd_pcm_hw_params_set_format(pcm_handle, params, format); | |
255 } | |
256 if ( status < 0 ) { | |
403 test_format = SDL_NextAudioFormat(); | 257 test_format = SDL_NextAudioFormat(); |
404 } | 258 } |
405 } | 259 } |
406 if ( format == 0 ) { | 260 if ( status < 0 ) { |
407 SDL_SetError("Couldn't find any hardware audio formats"); | 261 SDL_SetError("Couldn't find any hardware audio formats"); |
262 ALSA_CloseAudio(this); | |
408 return(-1); | 263 return(-1); |
409 } | 264 } |
410 spec->format = test_format; | 265 spec->format = test_format; |
411 | 266 |
412 /* Set the audio format */ | 267 /* Set the number of channels */ |
413 cparams.format.format = format; | 268 status = snd_pcm_hw_params_set_channels(pcm_handle, params, spec->channels); |
414 | 269 if ( status < 0 ) { |
415 /* Set mono or stereo audio (currently only two channels supported) */ | 270 status = snd_pcm_hw_params_get_channels(params); |
416 cparams.format.voices = spec->channels; | 271 if ( (status <= 0) || (status > 2) ) { |
417 | 272 SDL_SetError("Couldn't set audio channels"); |
418 #ifdef DEBUG_AUDIO | 273 ALSA_CloseAudio(this); |
419 printf("intializing channels %d\n", cparams.format.voices); | 274 return(-1); |
420 #endif | 275 } |
421 | 276 spec->channels = status; |
422 /* Set rate */ | 277 } |
423 cparams.format.rate = spec->freq ; | 278 |
424 | 279 /* Set the audio rate */ |
425 /* Setup the transfer parameters according to cparams */ | 280 status = snd_pcm_hw_params_set_rate_near(pcm_handle, params, spec->freq, NULL); |
426 rval = snd_pcm_plugin_params(audio_handle, &cparams); | 281 if ( status < 0 ) { |
427 if (rval < 0) { | 282 SDL_SetError("Couldn't set audio frequency: %s", snd_strerror(status)); |
428 SDL_SetError("snd_pcm_channel_params failed: %s\n", snd_strerror (rval)); | 283 ALSA_CloseAudio(this); |
429 return(-1); | 284 return(-1); |
430 } | 285 } |
431 | 286 spec->freq = status; |
432 /* Make sure channel is setup right one last time */ | 287 |
433 memset( &csetup, 0, sizeof( csetup ) ); | 288 /* Set the buffer size, in samples */ |
434 csetup.channel = SND_PCM_CHANNEL_PLAYBACK; | 289 frames = spec->samples; |
435 if ( snd_pcm_plugin_setup( audio_handle, &csetup ) < 0 ) | 290 frames = snd_pcm_hw_params_set_period_size_near(pcm_handle, params, frames, NULL); |
436 { | 291 spec->samples = frames; |
437 SDL_SetError("Unable to setup playback channel\n" ); | 292 snd_pcm_hw_params_set_periods_near(pcm_handle, params, 2, NULL); |
438 return(-1); | 293 |
439 } | 294 /* "set" the hardware with the desired parameters */ |
440 | 295 status = snd_pcm_hw_params(pcm_handle, params); |
441 #ifdef DEBUG_AUDIO | 296 if ( status < 0 ) { |
442 else | 297 SDL_SetError("Couldn't set audio parameters: %s", snd_strerror(status)); |
443 { | 298 ALSA_CloseAudio(this); |
444 fprintf(stderr,"requested format: %d\n",cparams.format.format); | 299 return(-1); |
445 fprintf(stderr,"requested frag size: %d\n",cparams.buf.block.frag_size); | 300 } |
446 fprintf(stderr,"requested max frags: %d\n\n",cparams.buf.block.frags_max); | 301 |
447 | 302 /* Calculate the final parameters for this audio specification */ |
448 fprintf(stderr,"real format: %d\n", csetup.format.format ); | 303 SDL_CalculateAudioSpec(spec); |
449 fprintf(stderr,"real frag size : %d\n", csetup.buf.block.frag_size ); | 304 |
450 fprintf(stderr,"real max frags : %d\n", csetup.buf.block.frags_max ); | 305 /* Allocate mixing buffer */ |
451 } | 306 mixlen = spec->size; |
452 #endif // DEBUG_AUDIO | 307 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen); |
453 | 308 if ( mixbuf == NULL ) { |
454 /* Allocate memory to the audio buffer and initialize with silence | 309 ALSA_CloseAudio(this); |
455 (Note that buffer size must be a multiple of fragment size, so find closest multiple) | 310 return(-1); |
456 */ | 311 } |
457 | 312 memset(mixbuf, spec->silence, spec->size); |
458 twidth = snd_pcm_format_width(format); | |
459 if (twidth < 0) { | |
460 printf("snd_pcm_format_width failed\n"); | |
461 twidth = 0; | |
462 } | |
463 #ifdef DEBUG_AUDIO | |
464 printf("format is %d bits wide\n",twidth); | |
465 #endif | |
466 | |
467 pcm_len = csetup.buf.block.frag_size * (twidth/8) * csetup.format.voices ; | |
468 | |
469 #ifdef DEBUG_AUDIO | |
470 printf("pcm_len set to %d\n", pcm_len); | |
471 #endif | |
472 | |
473 if (pcm_len == 0) | |
474 { | |
475 pcm_len = csetup.buf.block.frag_size; | |
476 } | |
477 | |
478 pcm_buf = (Uint8*)malloc(pcm_len); | |
479 if (pcm_buf == NULL) { | |
480 SDL_SetError("pcm_buf malloc failed\n"); | |
481 return(-1); | |
482 } | |
483 memset(pcm_buf,spec->silence,pcm_len); | |
484 | |
485 #ifdef DEBUG_AUDIO | |
486 fprintf(stderr,"pcm_buf malloced and silenced.\n"); | |
487 #endif | |
488 | |
489 /* get the file descriptor */ | |
490 if( (audio_fd = snd_pcm_file_descriptor(audio_handle, device_no)) < 0) | |
491 { | |
492 fprintf(stderr, "snd_pcm_file_descriptor failed with error code: %d\n", audio_fd); | |
493 } | |
494 | |
495 /* Trigger audio playback */ | |
496 rval = snd_pcm_plugin_prepare( audio_handle, SND_PCM_CHANNEL_PLAYBACK); | |
497 if (rval < 0) { | |
498 SDL_SetError("snd_pcm_plugin_prepare failed: %s\n", snd_strerror (rval)); | |
499 return(-1); | |
500 } | |
501 rval = snd_pcm_playback_go(audio_handle); | |
502 if (rval < 0) { | |
503 SDL_SetError("snd_pcm_playback_go failed: %s\n", snd_strerror (rval)); | |
504 return(-1); | |
505 } | |
506 | |
507 /* Check to see if we need to use select() workaround */ | |
508 { char *workaround; | |
509 workaround = getenv("SDL_DSP_NOSELECT"); | |
510 if ( workaround ) { | |
511 frame_ticks = (float)(spec->samples*1000)/spec->freq; | |
512 next_frame = SDL_GetTicks()+frame_ticks; | |
513 } | |
514 } | |
515 | 313 |
516 /* Get the parent process id (we're the parent of the audio thread) */ | 314 /* Get the parent process id (we're the parent of the audio thread) */ |
517 parent = getpid(); | 315 parent = getpid(); |
518 | 316 |
519 /* We're ready to rock and roll. :-) */ | 317 /* We're ready to rock and roll. :-) */ |