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 */
29 #include <sys/ioctl.h>
30 #include <sys/audioio.h>
33 #include <sys/audioio.h>
36 #include <sys/types.h>
40 #include "SDL_timer.h"
41 #include "SDL_audio.h"
42 #include "../SDL_audiomem.h"
43 #include "../SDL_audio_c.h"
44 #include "../SDL_audiodev_c.h"
45 #include "SDL_sunaudio.h"
47 /* Open the audio device for playback, and don't block if busy */
48 #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK)
50 /* Audio driver functions */
51 static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec);
52 static void DSP_WaitAudio(_THIS);
53 static void DSP_PlayAudio(_THIS);
54 static Uint8 *DSP_GetAudioBuf(_THIS);
55 static void DSP_CloseAudio(_THIS);
57 static Uint8 snd2au(int sample);
59 /* Audio driver bootstrap functions */
61 static int Audio_Available(void)
67 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 1);
75 static void Audio_DeleteDevice(SDL_AudioDevice *device)
77 SDL_free(device->hidden);
81 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
83 SDL_AudioDevice *this;
85 /* Initialize all variables that we clean on shutdown */
86 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
88 SDL_memset(this, 0, (sizeof *this));
89 this->hidden = (struct SDL_PrivateAudioData *)
90 SDL_malloc((sizeof *this->hidden));
92 if ( (this == NULL) || (this->hidden == NULL) ) {
99 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
102 /* Set the function pointers */
103 this->OpenAudio = DSP_OpenAudio;
104 this->WaitAudio = DSP_WaitAudio;
105 this->PlayAudio = DSP_PlayAudio;
106 this->GetAudioBuf = DSP_GetAudioBuf;
107 this->CloseAudio = DSP_CloseAudio;
109 this->free = Audio_DeleteDevice;
114 AudioBootStrap SUNAUDIO_bootstrap = {
115 "audio", "UNIX /dev/audio interface",
116 Audio_Available, Audio_CreateDevice
120 void CheckUnderflow(_THIS)
126 ioctl(audio_fd, AUDIO_GETINFO, &info);
127 left = (written - info.play.samples);
128 if ( written && (left == 0) ) {
129 fprintf(stderr, "audio underflow!\n");
135 void DSP_WaitAudio(_THIS)
138 #define SLEEP_FUDGE 10 /* 10 ms scheduling fudge factor */
142 ioctl(audio_fd, AUDIO_GETINFO, &info);
143 left = (written - info.play.samples);
144 if ( left > fragsize ) {
147 sleepy = ((left - fragsize)/frequency);
148 sleepy -= SLEEP_FUDGE;
157 FD_SET(audio_fd, &fdset);
158 select(audio_fd+1, NULL, &fdset, NULL, NULL);
162 void DSP_PlayAudio(_THIS)
164 /* Write the audio data */
166 /* Assuming that this->spec.freq >= 8000 Hz */
167 int accum, incr, pos;
171 incr = this->spec.freq/8;
173 switch (audio_fmt & 0xFF) {
178 for ( pos=0; pos < fragsize; ++pos ) {
179 *aubuf = snd2au((0x80-*sndbuf)*64);
181 while ( accum > 0 ) {
192 sndbuf = (Sint16 *)mixbuf;
193 for ( pos=0; pos < fragsize; ++pos ) {
194 *aubuf = snd2au(*sndbuf/4);
196 while ( accum > 0 ) {
206 CheckUnderflow(this);
208 if ( write(audio_fd, ulaw_buf, fragsize) < 0 ) {
209 /* Assume fatal error, for now */
215 CheckUnderflow(this);
217 if ( write(audio_fd, mixbuf, this->spec.size) < 0 ) {
218 /* Assume fatal error, for now */
225 Uint8 *DSP_GetAudioBuf(_THIS)
230 void DSP_CloseAudio(_THIS)
232 if ( mixbuf != NULL ) {
233 SDL_FreeAudioMem(mixbuf);
236 if ( ulaw_buf != NULL ) {
243 int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec)
249 int desired_freq = spec->freq;
251 /* Initialize our freeable variables, in case we fail*/
256 /* Determine the audio parameters from the AudioSpec */
257 switch ( spec->format & 0xFF ) {
259 case 8: { /* Unsigned 8 bit audio data */
260 spec->format = AUDIO_U8;
262 enc = AUDIO_ENCODING_LINEAR8;
267 case 16: { /* Signed 16 bit audio data */
268 spec->format = AUDIO_S16SYS;
270 enc = AUDIO_ENCODING_LINEAR;
276 SDL_SetError("Unsupported audio format");
280 audio_fmt = spec->format;
282 /* Open the audio device */
283 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 1);
284 if ( audio_fd < 0 ) {
285 SDL_SetError("Couldn't open %s: %s", audiodev,
290 ulaw_only = 0; /* modern Suns do support linear audio */
294 AUDIO_INITINFO(&info); /* init all fields to "no change" */
296 /* Try to set the requested settings */
297 info.play.sample_rate = spec->freq;
298 info.play.channels = spec->channels;
299 info.play.precision = (enc == AUDIO_ENCODING_ULAW)
300 ? 8 : spec->format & 0xff;
301 info.play.encoding = enc;
302 if( ioctl(audio_fd, AUDIO_SETINFO, &info) == 0 ) {
304 /* Check to be sure we got what we wanted */
305 if(ioctl(audio_fd, AUDIO_GETINFO, &info) < 0) {
306 SDL_SetError("Error getting audio parameters: %s",
310 if(info.play.encoding == enc
311 && info.play.precision == (spec->format & 0xff)
312 && info.play.channels == spec->channels) {
313 /* Yow! All seems to be well! */
314 spec->freq = info.play.sample_rate;
320 case AUDIO_ENCODING_LINEAR8:
321 /* unsigned 8bit apparently not supported here */
322 enc = AUDIO_ENCODING_LINEAR;
323 spec->format = AUDIO_S16SYS;
324 break; /* try again */
326 case AUDIO_ENCODING_LINEAR:
327 /* linear 16bit didn't work either, resort to ยต-law */
328 enc = AUDIO_ENCODING_ULAW;
331 spec->format = AUDIO_U8;
337 SDL_SetError("Error setting audio parameters: %s",
342 #endif /* AUDIO_SETINFO */
345 /* We can actually convert on-the-fly to U-Law */
347 spec->freq = desired_freq;
348 fragsize = (spec->samples*1000)/(spec->freq/8);
350 ulaw_buf = (Uint8 *)SDL_malloc(fragsize);
351 if ( ulaw_buf == NULL ) {
357 fragsize = spec->samples;
358 frequency = spec->freq/1000;
361 fprintf(stderr, "Audio device %s U-Law only\n",
362 ulaw_only ? "is" : "is not");
363 fprintf(stderr, "format=0x%x chan=%d freq=%d\n",
364 spec->format, spec->channels, spec->freq);
367 /* Update the fragment size as size in bytes */
368 SDL_CalculateAudioSpec(spec);
370 /* Allocate mixing buffer */
371 mixbuf = (Uint8 *)SDL_AllocAudioMem(spec->size);
372 if ( mixbuf == NULL ) {
376 SDL_memset(mixbuf, spec->silence, spec->size);
378 /* We're ready to rock and roll. :-) */
382 /************************************************************************/
383 /* This function (snd2au()) copyrighted: */
384 /************************************************************************/
385 /* Copyright 1989 by Rich Gopstein and Harris Corporation */
387 /* Permission to use, copy, modify, and distribute this software */
388 /* and its documentation for any purpose and without fee is */
389 /* hereby granted, provided that the above copyright notice */
390 /* appears in all copies and that both that copyright notice and */
391 /* this permission notice appear in supporting documentation, and */
392 /* that the name of Rich Gopstein and Harris Corporation not be */
393 /* used in advertising or publicity pertaining to distribution */
394 /* of the software without specific, written prior permission. */
395 /* Rich Gopstein and Harris Corporation make no representations */
396 /* about the suitability of this software for any purpose. It */
397 /* provided "as is" without express or implied warranty. */
398 /************************************************************************/
400 static Uint8 snd2au(int sample)
413 sample = 0xF0 | (15 - sample / 2);
414 } else if (sample < 96) {
415 sample = 0xE0 | (15 - (sample - 32) / 4);
416 } else if (sample < 224) {
417 sample = 0xD0 | (15 - (sample - 96) / 8);
418 } else if (sample < 480) {
419 sample = 0xC0 | (15 - (sample - 224) / 16);
420 } else if (sample < 992) {
421 sample = 0xB0 | (15 - (sample - 480) / 32);
422 } else if (sample < 2016) {
423 sample = 0xA0 | (15 - (sample - 992) / 64);
424 } else if (sample < 4064) {
425 sample = 0x90 | (15 - (sample - 2016) / 128);
426 } else if (sample < 8160) {
427 sample = 0x80 | (15 - (sample - 4064) / 256);
431 return (mask & sample);