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
20 griff@kom.tu-darmstadt.de
22 based on linux/SDL_dspaudio.c by Sam Lantinga
24 #include "SDL_config.h"
26 /* Allow access to a raw mixing buffer */
32 #include <sys/ioctl.h>
35 #include "SDL_timer.h"
36 #include "SDL_audio.h"
37 #include "../SDL_audiomem.h"
38 #include "../SDL_audio_c.h"
39 #include "../SDL_audiodev_c.h"
40 #include "SDL_paudio.h"
44 /* A conflict within AIX 4.3.3 <sys/> headers and probably others as well.
45 * I guess nobody ever uses audio... Shame over AIX header files. */
46 #include <sys/machine.h>
48 #include <sys/audio.h>
50 /* The tag name used by paud audio */
51 #define Paud_DRIVER_NAME "paud"
53 /* Open the audio device for playback, and don't block if busy */
54 /* #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK) */
55 #define OPEN_FLAGS O_WRONLY
57 /* Audio driver functions */
58 static int Paud_OpenAudio(_THIS, SDL_AudioSpec *spec);
59 static void Paud_WaitAudio(_THIS);
60 static void Paud_PlayAudio(_THIS);
61 static Uint8 *Paud_GetAudioBuf(_THIS);
62 static void Paud_CloseAudio(_THIS);
64 /* Audio driver bootstrap functions */
66 static int Audio_Available(void)
72 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
80 static void Audio_DeleteDevice(SDL_AudioDevice *device)
82 SDL_free(device->hidden);
86 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
88 SDL_AudioDevice *this;
90 /* Initialize all variables that we clean on shutdown */
91 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
93 SDL_memset(this, 0, (sizeof *this));
94 this->hidden = (struct SDL_PrivateAudioData *)
95 SDL_malloc((sizeof *this->hidden));
97 if ( (this == NULL) || (this->hidden == NULL) ) {
104 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
107 /* Set the function pointers */
108 this->OpenAudio = Paud_OpenAudio;
109 this->WaitAudio = Paud_WaitAudio;
110 this->PlayAudio = Paud_PlayAudio;
111 this->GetAudioBuf = Paud_GetAudioBuf;
112 this->CloseAudio = Paud_CloseAudio;
114 this->free = Audio_DeleteDevice;
119 AudioBootStrap Paud_bootstrap = {
120 Paud_DRIVER_NAME, "AIX Paudio",
121 Audio_Available, Audio_CreateDevice
124 /* This function waits until it is possible to write a full sound buffer */
125 static void Paud_WaitAudio(_THIS)
129 /* See if we need to use timed audio synchronization */
131 /* Use timer for general audio synchronization */
134 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
139 audio_buffer paud_bufinfo;
141 /* Use select() for audio synchronization */
142 struct timeval timeout;
144 FD_SET(audio_fd, &fdset);
146 if ( ioctl(audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0 ) {
148 fprintf(stderr, "Couldn't get audio buffer information\n");
153 long ms_in_buf = paud_bufinfo.write_buf_time;
154 timeout.tv_sec = ms_in_buf/1000;
155 ms_in_buf = ms_in_buf - timeout.tv_sec*1000;
156 timeout.tv_usec = ms_in_buf*1000;
159 "Waiting for write_buf_time=%ld,%ld\n",
166 fprintf(stderr, "Waiting for audio to get ready\n");
168 if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
169 const char *message = "Audio timeout - buggy audio driver? (disabled)";
171 * In general we should never print to the screen,
172 * but in this case we have no other way of letting
173 * the user know what happened.
175 fprintf(stderr, "SDL: %s - %s\n", strerror(errno), message);
177 /* Don't try to close - may hang */
180 fprintf(stderr, "Done disabling audio\n");
184 fprintf(stderr, "Ready!\n");
189 static void Paud_PlayAudio(_THIS)
193 /* Write the audio data, checking for EAGAIN on broken audio drivers */
195 written = write(audio_fd, mixbuf, mixlen);
196 if ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) ) {
197 SDL_Delay(1); /* Let a little CPU time go by */
199 } while ( (written < 0) &&
200 ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)) );
202 /* If timer synchronization is enabled, set the next write frame */
204 next_frame += frame_ticks;
207 /* If we couldn't write, assume fatal error for now */
212 fprintf(stderr, "Wrote %d bytes of audio data\n", written);
216 static Uint8 *Paud_GetAudioBuf(_THIS)
221 static void Paud_CloseAudio(_THIS)
223 if ( mixbuf != NULL ) {
224 SDL_FreeAudioMem(mixbuf);
227 if ( audio_fd >= 0 ) {
233 static int Paud_OpenAudio(_THIS, SDL_AudioSpec *spec)
237 int bytes_per_sample;
239 audio_init paud_init;
240 audio_buffer paud_bufinfo;
241 audio_status paud_status;
242 audio_control paud_control;
243 audio_change paud_change;
245 /* Reset the timer synchronization flag */
248 /* Open the audio device */
249 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
250 if ( audio_fd < 0 ) {
251 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
256 * We can't set the buffer size - just ask the device for the maximum
259 if ( ioctl(audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0 ) {
260 SDL_SetError("Couldn't get audio buffer information");
266 if ( spec->channels > 1 )
272 * Fields in the audio_init structure:
276 * paud.loadpath[LOAD_PATH]; * DSP code to load, MWave chip only?
277 * paud.slot_number; * slot number of the adapter
278 * paud.device_id; * adapter identification number
282 * paud.srate; * the sampling rate in Hz
283 * paud.bits_per_sample; * 8, 16, 32, ...
284 * paud.bsize; * block size for this rate
285 * paud.mode; * ADPCM, PCM, MU_LAW, A_LAW, SOURCE_MIX
286 * paud.channels; * 1=mono, 2=stereo
287 * paud.flags; * FIXED - fixed length data
288 * * LEFT_ALIGNED, RIGHT_ALIGNED (var len only)
289 * * TWOS_COMPLEMENT - 2's complement data
290 * * SIGNED - signed? comment seems wrong in sys/audio.h
292 * paud.operation; * PLAY, RECORD
296 * paud.flags; * PITCH - pitch is supported
297 * * INPUT - input is supported
298 * * OUTPUT - output is supported
299 * * MONITOR - monitor is supported
300 * * VOLUME - volume is supported
301 * * VOLUME_DELAY - volume delay is supported
302 * * BALANCE - balance is supported
303 * * BALANCE_DELAY - balance delay is supported
304 * * TREBLE - treble control is supported
305 * * BASS - bass control is supported
306 * * BESTFIT_PROVIDED - best fit returned
307 * * LOAD_CODE - DSP load needed
308 * paud.rc; * NO_PLAY - DSP code can't do play requests
309 * * NO_RECORD - DSP code can't do record requests
310 * * INVALID_REQUEST - request was invalid
311 * * CONFLICT - conflict with open's flags
312 * * OVERLOADED - out of DSP MIPS or memory
313 * paud.position_resolution; * smallest increment for position
316 paud_init.srate = spec->freq;
317 paud_init.mode = PCM;
318 paud_init.operation = PLAY;
319 paud_init.channels = spec->channels;
321 /* Try for a closest match on audio format */
323 for ( test_format = SDL_FirstAudioFormat(spec->format);
324 ! format && test_format; ) {
326 fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
328 switch ( test_format ) {
330 bytes_per_sample = 1;
331 paud_init.bits_per_sample = 8;
332 paud_init.flags = TWOS_COMPLEMENT | FIXED;
336 bytes_per_sample = 1;
337 paud_init.bits_per_sample = 8;
338 paud_init.flags = SIGNED |
339 TWOS_COMPLEMENT | FIXED;
343 bytes_per_sample = 2;
344 paud_init.bits_per_sample = 16;
345 paud_init.flags = SIGNED |
346 TWOS_COMPLEMENT | FIXED;
350 bytes_per_sample = 2;
351 paud_init.bits_per_sample = 16;
352 paud_init.flags = BIG_ENDIAN |
354 TWOS_COMPLEMENT | FIXED;
358 bytes_per_sample = 2;
359 paud_init.bits_per_sample = 16;
360 paud_init.flags = TWOS_COMPLEMENT | FIXED;
364 bytes_per_sample = 2;
365 paud_init.bits_per_sample = 16;
366 paud_init.flags = BIG_ENDIAN |
367 TWOS_COMPLEMENT | FIXED;
374 test_format = SDL_NextAudioFormat();
379 fprintf(stderr, "Couldn't find any hardware audio formats\n");
381 SDL_SetError("Couldn't find any hardware audio formats");
384 spec->format = test_format;
387 * We know the buffer size and the max number of subsequent writes
388 * that can be pending. If more than one can pend, allow the application
389 * to do something like double buffering between our write buffer and
390 * the device's own buffer that we are filling with write() anyway.
392 * We calculate spec->samples like this because SDL_CalculateAudioSpec()
393 * will give put paud_bufinfo.write_buf_cap (or paud_bufinfo.write_buf_cap/2)
394 * into spec->size in return.
396 if ( paud_bufinfo.request_buf_cap == 1 )
398 spec->samples = paud_bufinfo.write_buf_cap
404 spec->samples = paud_bufinfo.write_buf_cap
409 paud_init.bsize = bytes_per_sample * spec->channels;
411 SDL_CalculateAudioSpec(spec);
414 * The AIX paud device init can't modify the values of the audio_init
415 * structure that we pass to it. So we don't need any recalculation
416 * of this stuff and no reinit call as in linux dsp and dma code.
418 * /dev/paud supports all of the encoding formats, so we don't need
419 * to do anything like reopening the device, either.
421 if ( ioctl(audio_fd, AUDIO_INIT, &paud_init) < 0 ) {
422 switch ( paud_init.rc )
425 SDL_SetError("Couldn't set audio format: DSP can't do play requests");
429 SDL_SetError("Couldn't set audio format: DSP can't do record requests");
433 SDL_SetError("Couldn't set audio format: request was invalid");
437 SDL_SetError("Couldn't set audio format: conflict with open's flags");
441 SDL_SetError("Couldn't set audio format: out of DSP MIPS or memory");
445 SDL_SetError("Couldn't set audio format: not documented in sys/audio.h");
451 /* Allocate mixing buffer */
453 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
454 if ( mixbuf == NULL ) {
457 SDL_memset(mixbuf, spec->silence, spec->size);
460 * Set some paramters: full volume, first speaker that we can find.
461 * Ignore the other settings for now.
463 paud_change.input = AUDIO_IGNORE; /* the new input source */
464 paud_change.output = OUTPUT_1; /* EXTERNAL_SPEAKER,INTERNAL_SPEAKER,OUTPUT_1 */
465 paud_change.monitor = AUDIO_IGNORE; /* the new monitor state */
466 paud_change.volume = 0x7fffffff; /* volume level [0-0x7fffffff] */
467 paud_change.volume_delay = AUDIO_IGNORE; /* the new volume delay */
468 paud_change.balance = 0x3fffffff; /* the new balance */
469 paud_change.balance_delay = AUDIO_IGNORE; /* the new balance delay */
470 paud_change.treble = AUDIO_IGNORE; /* the new treble state */
471 paud_change.bass = AUDIO_IGNORE; /* the new bass state */
472 paud_change.pitch = AUDIO_IGNORE; /* the new pitch state */
474 paud_control.ioctl_request = AUDIO_CHANGE;
475 paud_control.request_info = (char*)&paud_change;
476 if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 ) {
478 fprintf(stderr, "Can't change audio display settings\n" );
483 * Tell the device to expect data. Actual start will wait for
484 * the first write() call.
486 paud_control.ioctl_request = AUDIO_START;
487 paud_control.position = 0;
488 if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 ) {
490 fprintf(stderr, "Can't start audio play\n" );
492 SDL_SetError("Can't start audio play");
496 /* Check to see if we need to use select() workaround */
498 workaround = SDL_getenv("SDL_DSP_NOSELECT");
500 frame_ticks = (float)(spec->samples*1000)/spec->freq;
501 next_frame = SDL_GetTicks()+frame_ticks;
505 /* Get the parent process id (we're the parent of the audio thread) */
508 /* We're ready to rock and roll. :-) */