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 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.
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.
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
22 #include "SDL_config.h"
25 * Driver for native OpenBSD/NetBSD audio(4).
33 #include <sys/ioctl.h>
35 #include <sys/types.h>
36 #include <sys/audioio.h>
38 #include "SDL_timer.h"
39 #include "SDL_audio.h"
40 #include "../SDL_audiomem.h"
41 #include "../SDL_audio_c.h"
42 #include "../SDL_audiodev_c.h"
43 #include "SDL_bsdaudio.h"
45 /* The tag name used by NetBSD/OpenBSD audio */
47 #define BSD_AUDIO_DRIVER_NAME "netbsd"
48 #define BSD_AUDIO_DRIVER_DESC "Native NetBSD audio"
50 #define BSD_AUDIO_DRIVER_NAME "openbsd"
51 #define BSD_AUDIO_DRIVER_DESC "Native OpenBSD audio"
54 /* Open the audio device for playback, and don't block if busy */
55 /* #define USE_BLOCKING_WRITES */
57 /* Use timer for synchronization */
58 /* #define USE_TIMER_SYNC */
60 /* #define DEBUG_AUDIO */
61 /* #define DEBUG_AUDIO_STREAM */
63 #ifdef USE_BLOCKING_WRITES
64 #define OPEN_FLAGS O_WRONLY
66 #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK)
69 /* Audio driver functions */
70 static void OBSD_WaitAudio(_THIS);
71 static int OBSD_OpenAudio(_THIS, SDL_AudioSpec *spec);
72 static void OBSD_PlayAudio(_THIS);
73 static Uint8 *OBSD_GetAudioBuf(_THIS);
74 static void OBSD_CloseAudio(_THIS);
77 static void OBSD_Status(_THIS);
80 /* Audio driver bootstrap functions */
89 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
98 Audio_DeleteDevice(SDL_AudioDevice *device)
100 SDL_free(device->hidden);
104 static SDL_AudioDevice
105 *Audio_CreateDevice(int devindex)
107 SDL_AudioDevice *this;
109 /* Initialize all variables that we clean on shutdown */
110 this = (SDL_AudioDevice*)SDL_malloc(sizeof(SDL_AudioDevice));
112 SDL_memset(this, 0, (sizeof *this));
114 (struct SDL_PrivateAudioData*)SDL_malloc((sizeof *this->hidden));
116 if((this == NULL) || (this->hidden == NULL)) {
118 if(this) SDL_free(this);
121 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
124 /* Set the function pointers */
125 this->OpenAudio = OBSD_OpenAudio;
126 this->WaitAudio = OBSD_WaitAudio;
127 this->PlayAudio = OBSD_PlayAudio;
128 this->GetAudioBuf = OBSD_GetAudioBuf;
129 this->CloseAudio = OBSD_CloseAudio;
131 this->free = Audio_DeleteDevice;
136 AudioBootStrap BSD_AUDIO_bootstrap = {
137 BSD_AUDIO_DRIVER_NAME, BSD_AUDIO_DRIVER_DESC,
138 Audio_Available, Audio_CreateDevice
141 /* This function waits until it is possible to write a full sound buffer */
143 OBSD_WaitAudio(_THIS)
145 #ifndef USE_BLOCKING_WRITES /* Not necessary when using blocking writes */
146 /* See if we need to use timed audio synchronization */
148 /* Use timer for general audio synchronization */
151 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
156 /* Use select() for audio synchronization */
158 struct timeval timeout;
161 FD_SET(audio_fd, &fdset);
165 fprintf(stderr, "Waiting for audio to get ready\n");
167 if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
168 const char *message =
169 "Audio timeout - buggy audio driver? (disabled)";
170 /* In general we should never print to the screen,
171 but in this case we have no other way of letting
172 the user know what happened.
174 fprintf(stderr, "SDL: %s\n", message);
176 /* Don't try to close - may hang */
179 fprintf(stderr, "Done disabling audio\n");
183 fprintf(stderr, "Ready!\n");
186 #endif /* !USE_BLOCKING_WRITES */
190 OBSD_PlayAudio(_THIS)
194 /* Write the audio data, checking for EAGAIN on broken audio drivers */
196 written = write(audio_fd, &mixbuf[p], mixlen-p);
199 if (written == -1 && errno != 0 && errno != EAGAIN && errno != EINTR)
201 /* Non recoverable error has occurred. It should be reported!!! */
206 if ( p < written || ((written < 0) && ((errno == 0) || (errno == EAGAIN))) ) {
207 SDL_Delay(1); /* Let a little CPU time go by */
209 } while ( p < written );
211 /* If timer synchronization is enabled, set the next write frame */
213 next_frame += frame_ticks;
216 /* If we couldn't write, assume fatal error for now */
221 fprintf(stderr, "Wrote %d bytes of audio data\n", written);
226 *OBSD_GetAudioBuf(_THIS)
232 OBSD_CloseAudio(_THIS)
235 SDL_FreeAudioMem(mixbuf);
250 if(ioctl(audio_fd, AUDIO_GETINFO, &info) < 0) {
251 fprintf(stderr,"AUDIO_GETINFO failed.\n");
256 "[play/record info]\n"
257 "buffer size : %d bytes\n"
258 "sample rate : %i Hz\n"
260 "precision : %i-bit\n"
263 "sample count : %i\n"
266 "error occured : %s\n"
270 info.play.buffer_size,
271 info.play.sample_rate,
278 info.play.pause ? "yes" : "no",
279 info.play.error ? "yes" : "no",
280 info.play.waiting ? "yes" : "no",
281 info.play.active ? "yes": "no");
285 "monitor_gain : %i\n"
286 "hw block size : %d bytes\n"
287 "hi watermark : %i\n"
288 "lo watermark : %i\n"
293 info.hiwat, info.lowat,
294 (info.mode == AUMODE_PLAY) ? "PLAY"
295 : (info.mode = AUMODE_RECORD) ? "RECORD"
296 : (info.mode == AUMODE_PLAY_ALL ? "PLAY_ALL"
299 #endif /* DEBUG_AUDIO */
302 OBSD_OpenAudio(_THIS, SDL_AudioSpec *spec)
308 AUDIO_INITINFO(&info);
310 /* Calculate the final parameters for this audio specification */
311 SDL_CalculateAudioSpec(spec);
313 #ifdef USE_TIMER_SYNC
317 /* Open the audio device */
318 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
320 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
324 /* Set to play mode */
325 info.mode = AUMODE_PLAY;
326 if(ioctl(audio_fd, AUDIO_SETINFO, &info) < 0) {
327 SDL_SetError("Couldn't put device into play mode");
332 AUDIO_INITINFO(&info);
333 for (format = SDL_FirstAudioFormat(spec->format);
334 format; format = SDL_NextAudioFormat())
338 info.play.encoding = AUDIO_ENCODING_ULINEAR;
339 info.play.precision = 8;
342 info.play.encoding = AUDIO_ENCODING_SLINEAR;
343 info.play.precision = 8;
346 info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
347 info.play.precision = 16;
350 info.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
351 info.play.precision = 16;
354 info.play.encoding = AUDIO_ENCODING_ULINEAR_LE;
355 info.play.precision = 16;
358 info.play.encoding = AUDIO_ENCODING_ULINEAR_BE;
359 info.play.precision = 16;
364 if (ioctl(audio_fd, AUDIO_SETINFO, &info) == 0)
369 SDL_SetError("No supported encoding for 0x%x", spec->format);
373 spec->format = format;
375 AUDIO_INITINFO(&info);
376 info.play.channels = spec->channels;
377 if (ioctl(audio_fd, AUDIO_SETINFO, &info) == -1)
379 AUDIO_INITINFO(&info);
380 info.play.sample_rate = spec->freq;
381 info.blocksize = spec->size;
384 (void)ioctl(audio_fd, AUDIO_SETINFO, &info);
385 (void)ioctl(audio_fd, AUDIO_GETINFO, &info);
386 spec->freq = info.play.sample_rate;
387 /* Allocate mixing buffer */
389 mixbuf = (Uint8*)SDL_AllocAudioMem(mixlen);
393 SDL_memset(mixbuf, spec->silence, spec->size);
395 /* Get the parent process id (we're the parent of the audio thread) */
402 /* We're ready to rock and roll. :-) */