2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* Allow access to a raw mixing buffer */
27 #include <string.h> /* For strerror() */
32 #include <sys/types.h>
34 #include <sys/ioctl.h>
38 #if SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H
39 /* This is installed on some systems */
40 #include <soundcard.h>
42 /* This is recommended by OSS */
43 #include <sys/soundcard.h>
47 #define MAP_FAILED ((Uint8 *)-1)
50 #include "SDL_timer.h"
51 #include "SDL_audio.h"
52 #include "../SDL_audio_c.h"
53 #include "../SDL_audiodev_c.h"
54 #include "SDL_dmaaudio.h"
56 /* The tag name used by DMA audio */
57 #define DMA_DRIVER_NAME "dma"
59 /* Open the audio device for playback, and don't block if busy */
60 #define OPEN_FLAGS (O_RDWR|O_NONBLOCK)
62 /* Audio driver functions */
63 static int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec);
64 static void DMA_WaitAudio(_THIS);
65 static void DMA_PlayAudio(_THIS);
66 static Uint8 *DMA_GetAudioBuf(_THIS);
67 static void DMA_CloseAudio(_THIS);
69 /* Audio driver bootstrap functions */
71 static int Audio_Available(void)
78 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
81 struct audio_buf_info info;
83 if ( (ioctl(fd, SNDCTL_DSP_GETCAPS, &caps) == 0) &&
84 (caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) &&
85 (ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) == 0) ) {
93 static void Audio_DeleteDevice(SDL_AudioDevice *device)
95 SDL_free(device->hidden);
99 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
101 SDL_AudioDevice *this;
103 /* Initialize all variables that we clean on shutdown */
104 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
106 SDL_memset(this, 0, (sizeof *this));
107 this->hidden = (struct SDL_PrivateAudioData *)
108 SDL_malloc((sizeof *this->hidden));
110 if ( (this == NULL) || (this->hidden == NULL) ) {
117 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
120 /* Set the function pointers */
121 this->OpenAudio = DMA_OpenAudio;
122 this->WaitAudio = DMA_WaitAudio;
123 this->PlayAudio = DMA_PlayAudio;
124 this->GetAudioBuf = DMA_GetAudioBuf;
125 this->CloseAudio = DMA_CloseAudio;
127 this->free = Audio_DeleteDevice;
132 AudioBootStrap DMA_bootstrap = {
133 DMA_DRIVER_NAME, "OSS /dev/dsp DMA audio",
134 Audio_Available, Audio_CreateDevice
137 /* This function waits until it is possible to write a full sound buffer */
138 static void DMA_WaitAudio(_THIS)
142 /* Check to see if the thread-parent process is still alive */
143 { static int cnt = 0;
144 /* Note that this only works with thread implementations
145 that use a different process id for each thread.
147 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
148 if ( kill(parent, 0) < 0 ) {
154 /* See if we need to use timed audio synchronization */
156 /* Use timer for general audio synchronization */
159 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
164 /* Use select() for audio synchronization */
165 struct timeval timeout;
167 FD_SET(audio_fd, &fdset);
171 fprintf(stderr, "Waiting for audio to get ready\n");
173 if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
174 const char *message =
175 #ifdef AUDIO_OSPACE_HACK
176 "Audio timeout - buggy audio driver? (trying ospace)";
178 "Audio timeout - buggy audio driver? (disabled)";
180 /* In general we should never print to the screen,
181 but in this case we have no other way of letting
182 the user know what happened.
184 fprintf(stderr, "SDL: %s\n", message);
185 #ifdef AUDIO_OSPACE_HACK
186 /* We may be able to use GET_OSPACE trick */
187 frame_ticks = (float)(this->spec->samples*1000) /
189 next_frame = SDL_GetTicks()+frame_ticks;
192 /* Don't try to close - may hang */
195 fprintf(stderr, "Done disabling audio\n");
197 #endif /* AUDIO_OSPACE_HACK */
200 fprintf(stderr, "Ready!\n");
205 static void DMA_PlayAudio(_THIS)
207 /* If timer synchronization is enabled, set the next write frame */
209 next_frame += frame_ticks;
214 static Uint8 *DMA_GetAudioBuf(_THIS)
220 /* Get number of blocks, looping if we're not using select() */
222 if ( ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &info) < 0 ) {
227 } while ( frame_ticks && (info.blocks < 1) );
229 if ( info.blocks > 1 ) {
230 printf("Warning: audio underflow (%d frags)\n", info.blocks-1);
233 playing = info.ptr / this->spec.size;
234 filling = (playing + 1)%num_buffers;
235 return (dma_buf + (filling * this->spec.size));
238 static void DMA_CloseAudio(_THIS)
240 if ( dma_buf != NULL ) {
241 munmap(dma_buf, dma_len);
244 if ( audio_fd >= 0 ) {
250 static int DMA_ReopenAudio(_THIS, const char *audiodev, int format, int stereo,
256 /* Close and then reopen the audio device */
258 audio_fd = open(audiodev, O_RDWR, 0);
259 if ( audio_fd < 0 ) {
260 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
264 /* Calculate the final parameters for this audio specification */
265 SDL_CalculateAudioSpec(spec);
267 /* Determine the power of two of the fragment size */
268 for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
269 if ( (0x01<<frag_spec) != spec->size ) {
270 SDL_SetError("Fragment size must be a power of two");
274 /* Set the audio buffering parameters */
275 if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) {
276 SDL_SetError("Couldn't set audio fragment spec");
280 /* Set the audio format */
282 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
283 (value != format) ) {
284 SDL_SetError("Couldn't set audio format");
288 /* Set mono or stereo audio */
289 value = (spec->channels > 1);
290 if ( (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) < 0) ||
291 (value != stereo) ) {
292 SDL_SetError("Couldn't set audio channels");
296 /* Set the DSP frequency */
298 if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) {
299 SDL_SetError("Couldn't set audio frequency");
304 /* We successfully re-opened the audio */
308 static int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec)
315 struct audio_buf_info info;
317 /* Reset the timer synchronization flag */
320 /* Open the audio device */
321 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
322 if ( audio_fd < 0 ) {
323 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
327 ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
329 /* Get a list of supported hardware formats */
330 if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) {
331 SDL_SetError("Couldn't get audio format list");
335 /* Try for a closest match on audio format */
337 for ( test_format = SDL_FirstAudioFormat(spec->format);
338 ! format && test_format; ) {
340 fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
342 switch ( test_format ) {
344 if ( value & AFMT_U8 ) {
349 if ( value & AFMT_S8 ) {
354 if ( value & AFMT_S16_LE ) {
355 format = AFMT_S16_LE;
359 if ( value & AFMT_S16_BE ) {
360 format = AFMT_S16_BE;
364 if ( value & AFMT_U16_LE ) {
365 format = AFMT_U16_LE;
369 if ( value & AFMT_U16_BE ) {
370 format = AFMT_U16_BE;
378 test_format = SDL_NextAudioFormat();
382 SDL_SetError("Couldn't find any hardware audio formats");
385 spec->format = test_format;
387 /* Set the audio format */
389 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
390 (value != format) ) {
391 SDL_SetError("Couldn't set audio format");
395 /* Set mono or stereo audio (currently only two channels supported) */
396 stereo = (spec->channels > 1);
397 ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo);
404 /* Because some drivers don't allow setting the buffer size
405 after setting the format, we must re-open the audio device
406 once we know what format and channels are supported
408 if ( DMA_ReopenAudio(this, audiodev, format, stereo, spec) < 0 ) {
409 /* Error is set by DMA_ReopenAudio() */
413 /* Memory map the audio buffer */
414 if ( ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info) < 0 ) {
415 SDL_SetError("Couldn't get OSPACE parameters");
418 spec->size = info.fragsize;
419 spec->samples = spec->size / ((spec->format & 0xFF) / 8);
420 spec->samples /= spec->channels;
421 num_buffers = info.fragstotal;
422 dma_len = num_buffers*spec->size;
423 dma_buf = (Uint8 *)mmap(NULL, dma_len, PROT_WRITE, MAP_SHARED,
425 if ( dma_buf == MAP_FAILED ) {
426 SDL_SetError("DMA memory map failed");
430 SDL_memset(dma_buf, spec->silence, dma_len);
432 /* Check to see if we need to use select() workaround */
434 workaround = SDL_getenv("SDL_DSP_NOSELECT");
436 frame_ticks = (float)(spec->samples*1000)/spec->freq;
437 next_frame = SDL_GetTicks()+frame_ticks;
441 /* Trigger audio playback */
443 ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value);
444 value = PCM_ENABLE_OUTPUT;
445 if ( ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value) < 0 ) {
446 SDL_SetError("Couldn't trigger audio output");
450 /* Get the parent process id (we're the parent of the audio thread) */
453 /* We're ready to rock and roll. :-) */