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