Mercurial > sdl-ios-xcode
annotate src/audio/dma/SDL_dmaaudio.c @ 1:cf2af46e9e2a
Changes since SDL 1.2.0 release
author | Sam Lantinga <slouken@lokigames.com> |
---|---|
date | Thu, 26 Apr 2001 16:50:19 +0000 |
parents | 74212992fb08 |
children | d3bc792e136d |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga | |
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 | |
20 slouken@devolution.com | |
21 */ | |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 /* Allow access to a raw mixing buffer */ | |
29 | |
30 #include <stdlib.h> | |
31 #include <stdio.h> | |
32 #include <string.h> | |
33 #include <errno.h> | |
34 #include <unistd.h> | |
35 #include <fcntl.h> | |
36 #include <signal.h> | |
37 #include <sys/types.h> | |
38 #include <sys/time.h> | |
39 #include <sys/ioctl.h> | |
40 #include <sys/stat.h> | |
41 #include <sys/mman.h> | |
42 #ifdef linux | |
43 #include <linux/soundcard.h> | |
44 #endif | |
45 #ifdef __bsdi__ | |
46 #include <sys/soundcard.h> | |
47 #endif | |
48 #ifdef __FreeBSD__ | |
49 #include <machine/soundcard.h> | |
50 #endif | |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
51 #ifdef __OpenBSD__ |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
52 #include <soundcard.h> |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
53 #endif |
0 | 54 #ifdef __USLC__ |
55 #include <sys/soundcard.h> | |
56 #endif | |
57 | |
58 #ifndef MAP_FAILED | |
59 #define MAP_FAILED ((Uint8 *)-1) | |
60 #endif | |
61 | |
62 #include "SDL_audio.h" | |
63 #include "SDL_error.h" | |
64 #include "SDL_audiomem.h" | |
65 #include "SDL_audio_c.h" | |
66 #include "SDL_timer.h" | |
67 #include "SDL_audiodev_c.h" | |
68 #include "SDL_dmaaudio.h" | |
69 | |
70 /* The tag name used by DMA audio */ | |
71 #define DMA_DRIVER_NAME "dma" | |
72 | |
73 /* Open the audio device for playback, and don't block if busy */ | |
74 #define OPEN_FLAGS (O_RDWR|O_NONBLOCK) | |
75 | |
76 /* Audio driver functions */ | |
77 static int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec); | |
78 static void DMA_WaitAudio(_THIS); | |
79 static void DMA_PlayAudio(_THIS); | |
80 static Uint8 *DMA_GetAudioBuf(_THIS); | |
81 static void DMA_CloseAudio(_THIS); | |
82 | |
83 /* Audio driver bootstrap functions */ | |
84 | |
85 static int Audio_Available(void) | |
86 { | |
87 int available; | |
88 int fd; | |
89 | |
90 available = 0; | |
91 | |
92 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0); | |
93 if ( fd >= 0 ) { | |
94 int caps; | |
95 struct audio_buf_info info; | |
96 | |
97 if ( (ioctl(fd, SNDCTL_DSP_GETCAPS, &caps) == 0) && | |
98 (caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) && | |
99 (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) == 0) ) { | |
100 available = 1; | |
101 } | |
102 close(fd); | |
103 } | |
104 return(available); | |
105 } | |
106 | |
107 static void Audio_DeleteDevice(SDL_AudioDevice *device) | |
108 { | |
109 free(device->hidden); | |
110 free(device); | |
111 } | |
112 | |
113 static SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
114 { | |
115 SDL_AudioDevice *this; | |
116 | |
117 /* Initialize all variables that we clean on shutdown */ | |
118 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); | |
119 if ( this ) { | |
120 memset(this, 0, (sizeof *this)); | |
121 this->hidden = (struct SDL_PrivateAudioData *) | |
122 malloc((sizeof *this->hidden)); | |
123 } | |
124 if ( (this == NULL) || (this->hidden == NULL) ) { | |
125 SDL_OutOfMemory(); | |
126 if ( this ) { | |
127 free(this); | |
128 } | |
129 return(0); | |
130 } | |
131 memset(this->hidden, 0, (sizeof *this->hidden)); | |
132 audio_fd = -1; | |
133 | |
134 /* Set the function pointers */ | |
135 this->OpenAudio = DMA_OpenAudio; | |
136 this->WaitAudio = DMA_WaitAudio; | |
137 this->PlayAudio = DMA_PlayAudio; | |
138 this->GetAudioBuf = DMA_GetAudioBuf; | |
139 this->CloseAudio = DMA_CloseAudio; | |
140 | |
141 this->free = Audio_DeleteDevice; | |
142 | |
143 return this; | |
144 } | |
145 | |
146 AudioBootStrap DMA_bootstrap = { | |
147 DMA_DRIVER_NAME, "OSS /dev/dsp DMA audio", | |
148 Audio_Available, Audio_CreateDevice | |
149 }; | |
150 | |
151 /* This function waits until it is possible to write a full sound buffer */ | |
152 static void DMA_WaitAudio(_THIS) | |
153 { | |
154 fd_set fdset; | |
155 | |
156 /* Check to see if the thread-parent process is still alive */ | |
157 { static int cnt = 0; | |
158 /* Note that this only works with thread implementations | |
159 that use a different process id for each thread. | |
160 */ | |
161 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */ | |
162 if ( kill(parent, 0) < 0 ) { | |
163 this->enabled = 0; | |
164 } | |
165 } | |
166 } | |
167 | |
168 /* See if we need to use timed audio synchronization */ | |
169 if ( frame_ticks ) { | |
170 /* Use timer for general audio synchronization */ | |
171 Sint32 ticks; | |
172 | |
173 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS; | |
174 if ( ticks > 0 ) { | |
175 SDL_Delay(ticks); | |
176 } | |
177 } else { | |
178 /* Use select() for audio synchronization */ | |
179 struct timeval timeout; | |
180 FD_ZERO(&fdset); | |
181 FD_SET(audio_fd, &fdset); | |
182 timeout.tv_sec = 10; | |
183 timeout.tv_usec = 0; | |
184 #ifdef DEBUG_AUDIO | |
185 fprintf(stderr, "Waiting for audio to get ready\n"); | |
186 #endif | |
187 if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) { | |
188 const char *message = | |
189 #ifdef AUDIO_OSPACE_HACK | |
190 "Audio timeout - buggy audio driver? (trying ospace)"; | |
191 #else | |
192 "Audio timeout - buggy audio driver? (disabled)"; | |
193 #endif | |
194 /* In general we should never print to the screen, | |
195 but in this case we have no other way of letting | |
196 the user know what happened. | |
197 */ | |
198 fprintf(stderr, "SDL: %s\n", message); | |
199 #ifdef AUDIO_OSPACE_HACK | |
200 /* We may be able to use GET_OSPACE trick */ | |
201 frame_ticks = (float)(this->spec->samples*1000) / | |
202 this->spec->freq; | |
203 next_frame = SDL_GetTicks()+frame_ticks; | |
204 #else | |
205 this->enabled = 0; | |
206 /* Don't try to close - may hang */ | |
207 audio_fd = -1; | |
208 #ifdef DEBUG_AUDIO | |
209 fprintf(stderr, "Done disabling audio\n"); | |
210 #endif | |
211 #endif /* AUDIO_OSPACE_HACK */ | |
212 } | |
213 #ifdef DEBUG_AUDIO | |
214 fprintf(stderr, "Ready!\n"); | |
215 #endif | |
216 } | |
217 } | |
218 | |
219 static void DMA_PlayAudio(_THIS) | |
220 { | |
221 /* If timer synchronization is enabled, set the next write frame */ | |
222 if ( frame_ticks ) { | |
223 next_frame += frame_ticks; | |
224 } | |
225 return; | |
226 } | |
227 | |
228 static Uint8 *DMA_GetAudioBuf(_THIS) | |
229 { | |
230 count_info info; | |
231 int playing; | |
232 int filling; | |
233 | |
234 /* Get number of blocks, looping if we're not using select() */ | |
235 do { | |
236 if ( ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &info) < 0 ) { | |
237 /* Uh oh... */ | |
238 this->enabled = 0; | |
239 return(NULL); | |
240 } | |
241 } while ( frame_ticks && (info.blocks < 1) ); | |
242 #ifdef DEBUG_AUDIO | |
243 if ( info.blocks > 1 ) { | |
244 printf("Warning: audio underflow (%d frags)\n", info.blocks-1); | |
245 } | |
246 #endif | |
247 playing = info.ptr / this->spec.size; | |
248 filling = (playing + 1)%num_buffers; | |
249 return (dma_buf + (filling * this->spec.size)); | |
250 } | |
251 | |
252 static void DMA_CloseAudio(_THIS) | |
253 { | |
254 if ( dma_buf != NULL ) { | |
255 munmap(dma_buf, dma_len); | |
256 dma_buf = NULL; | |
257 } | |
258 if ( audio_fd >= 0 ) { | |
259 close(audio_fd); | |
260 audio_fd = -1; | |
261 } | |
262 } | |
263 | |
264 static int DMA_ReopenAudio(_THIS, const char *audiodev, int format, int stereo, | |
265 SDL_AudioSpec *spec) | |
266 { | |
267 int frag_spec; | |
268 int value; | |
269 | |
270 /* Close and then reopen the audio device */ | |
271 close(audio_fd); | |
272 audio_fd = open(audiodev, O_RDWR, 0); | |
273 if ( audio_fd < 0 ) { | |
274 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); | |
275 return(-1); | |
276 } | |
277 ioctl(audio_fd, SNDCTL_DSP_RESET, 0); | |
278 | |
279 /* Calculate the final parameters for this audio specification */ | |
280 SDL_CalculateAudioSpec(spec); | |
281 | |
282 /* Determine the power of two of the fragment size */ | |
283 for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec ); | |
284 if ( (0x01<<frag_spec) != spec->size ) { | |
285 SDL_SetError("Fragment size must be a power of two"); | |
286 return(-1); | |
287 } | |
288 | |
289 /* Set the audio buffering parameters */ | |
290 if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) { | |
291 SDL_SetError("Couldn't set audio fragment spec"); | |
292 return(-1); | |
293 } | |
294 | |
295 /* Set the audio format */ | |
296 value = format; | |
297 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || | |
298 (value != format) ) { | |
299 SDL_SetError("Couldn't set audio format"); | |
300 return(-1); | |
301 } | |
302 | |
303 /* Set mono or stereo audio */ | |
304 value = (spec->channels > 1); | |
305 if ( (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) < 0) || | |
306 (value != stereo) ) { | |
307 SDL_SetError("Couldn't set audio channels"); | |
308 return(-1); | |
309 } | |
310 | |
311 /* Set the DSP frequency */ | |
312 value = spec->freq; | |
313 if ( ioctl(audio_fd, SOUND_PCM_WRITE_RATE, &value) < 0 ) { | |
314 SDL_SetError("Couldn't set audio frequency"); | |
315 return(-1); | |
316 } | |
317 spec->freq = value; | |
318 | |
319 /* We successfully re-opened the audio */ | |
320 return(0); | |
321 } | |
322 | |
323 static int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
324 { | |
325 char audiodev[1024]; | |
326 int format; | |
327 int stereo; | |
328 int value; | |
329 Uint16 test_format; | |
330 struct audio_buf_info info; | |
331 | |
332 /* Reset the timer synchronization flag */ | |
333 frame_ticks = 0.0; | |
334 | |
335 /* Open the audio device */ | |
336 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0); | |
337 if ( audio_fd < 0 ) { | |
338 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno)); | |
339 return(-1); | |
340 } | |
341 dma_buf = NULL; | |
342 ioctl(audio_fd, SNDCTL_DSP_RESET, 0); | |
343 | |
344 /* Get a list of supported hardware formats */ | |
345 if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) { | |
346 SDL_SetError("Couldn't get audio format list"); | |
347 return(-1); | |
348 } | |
349 | |
350 /* Try for a closest match on audio format */ | |
351 format = 0; | |
352 for ( test_format = SDL_FirstAudioFormat(spec->format); | |
353 ! format && test_format; ) { | |
354 #ifdef DEBUG_AUDIO | |
355 fprintf(stderr, "Trying format 0x%4.4x\n", test_format); | |
356 #endif | |
357 switch ( test_format ) { | |
358 case AUDIO_U8: | |
359 if ( value & AFMT_U8 ) { | |
360 format = AFMT_U8; | |
361 } | |
362 break; | |
363 case AUDIO_S8: | |
364 if ( value & AFMT_S8 ) { | |
365 format = AFMT_S8; | |
366 } | |
367 break; | |
368 case AUDIO_S16LSB: | |
369 if ( value & AFMT_S16_LE ) { | |
370 format = AFMT_S16_LE; | |
371 } | |
372 break; | |
373 case AUDIO_S16MSB: | |
374 if ( value & AFMT_S16_BE ) { | |
375 format = AFMT_S16_BE; | |
376 } | |
377 break; | |
378 case AUDIO_U16LSB: | |
379 if ( value & AFMT_U16_LE ) { | |
380 format = AFMT_U16_LE; | |
381 } | |
382 break; | |
383 case AUDIO_U16MSB: | |
384 if ( value & AFMT_U16_BE ) { | |
385 format = AFMT_U16_BE; | |
386 } | |
387 break; | |
388 default: | |
389 break; | |
390 } | |
391 if ( ! format ) { | |
392 test_format = SDL_NextAudioFormat(); | |
393 } | |
394 } | |
395 if ( format == 0 ) { | |
396 SDL_SetError("Couldn't find any hardware audio formats"); | |
397 return(-1); | |
398 } | |
399 spec->format = test_format; | |
400 | |
401 /* Set the audio format */ | |
402 value = format; | |
403 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) || | |
404 (value != format) ) { | |
405 SDL_SetError("Couldn't set audio format"); | |
406 return(-1); | |
407 } | |
408 | |
409 /* Set mono or stereo audio (currently only two channels supported) */ | |
410 stereo = (spec->channels > 1); | |
411 ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo); | |
412 if ( stereo ) { | |
413 spec->channels = 2; | |
414 } else { | |
415 spec->channels = 1; | |
416 } | |
417 | |
418 /* Because some drivers don't allow setting the buffer size | |
419 after setting the format, we must re-open the audio device | |
420 once we know what format and channels are supported | |
421 */ | |
422 if ( DMA_ReopenAudio(this, audiodev, format, stereo, spec) < 0 ) { | |
423 /* Error is set by DMA_ReopenAudio() */ | |
424 return(-1); | |
425 } | |
426 | |
427 /* Memory map the audio buffer */ | |
428 if ( ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info) < 0 ) { | |
429 SDL_SetError("Couldn't get OSPACE parameters"); | |
430 return(-1); | |
431 } | |
432 spec->size = info.fragsize; | |
433 spec->samples = spec->size / ((spec->format & 0xFF) / 8); | |
434 spec->samples /= spec->channels; | |
435 num_buffers = info.fragstotal; | |
436 dma_len = num_buffers*spec->size; | |
437 dma_buf = (Uint8 *)mmap(NULL, dma_len, PROT_WRITE, MAP_SHARED, | |
438 audio_fd, 0); | |
439 if ( dma_buf == MAP_FAILED ) { | |
440 SDL_SetError("DMA memory map failed"); | |
441 dma_buf = NULL; | |
442 return(-1); | |
443 } | |
444 memset(dma_buf, spec->silence, dma_len); | |
445 | |
446 /* Check to see if we need to use select() workaround */ | |
447 { char *workaround; | |
448 workaround = getenv("SDL_DSP_NOSELECT"); | |
449 if ( workaround ) { | |
450 frame_ticks = (float)(spec->samples*1000)/spec->freq; | |
451 next_frame = SDL_GetTicks()+frame_ticks; | |
452 } | |
453 } | |
454 | |
455 /* Trigger audio playback */ | |
456 value = 0; | |
457 ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value); | |
458 value = PCM_ENABLE_OUTPUT; | |
459 if ( ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value) < 0 ) { | |
460 SDL_SetError("Couldn't trigger audio output"); | |
461 return(-1); | |
462 } | |
463 | |
464 /* Get the parent process id (we're the parent of the audio thread) */ | |
465 parent = getpid(); | |
466 | |
467 /* We're ready to rock and roll. :-) */ | |
468 return(0); | |
469 } |