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