comparison src/audio/paudio/SDL_paudio.c @ 0:74212992fb08

Initial revision
author Sam Lantinga <slouken@lokigames.com>
date Thu, 26 Apr 2001 16:45:43 +0000
parents
children c9b51268668f
comparison
equal deleted inserted replaced
-1:000000000000 0:74212992fb08
1 /*
2 AIX support for the SDL - Simple DirectMedia Layer
3 Copyright (C) 2000 Carsten Griwodz
4
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.
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
13 Library General Public License for more details.
14
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
18
19 Carsten Griwodz
20 griff@kom.tu-darmstadt.de
21
22 based on linux/SDL_dspaudio.c by Sam Lantinga
23 */
24
25 #ifdef SAVE_RCSID
26 static char rcsid =
27 "@(#) $Id$";
28 #endif
29
30 /* Allow access to a raw mixing buffer */
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <sys/time.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41
42 #include "SDL_audio.h"
43 #include "SDL_error.h"
44 #include "SDL_audiomem.h"
45 #include "SDL_audio_c.h"
46 #include "SDL_timer.h"
47 #include "SDL_audiodev_c.h"
48 #include "SDL_paudio.h"
49
50 #define DEBUG_AUDIO 1
51
52 /* A conflict within AIX 4.3.3 <sys/> headers and probably others as well.
53 * I guess nobody ever uses audio... Shame over AIX header files. */
54 #include <sys/machine.h>
55 #undef BIG_ENDIAN
56 #include <sys/audio.h>
57
58 /* The tag name used by paud audio */
59 #define Paud_DRIVER_NAME "paud"
60
61 /* Open the audio device for playback, and don't block if busy */
62 /* #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK) */
63 #define OPEN_FLAGS O_WRONLY
64
65 /* Audio driver functions */
66 static int Paud_OpenAudio(_THIS, SDL_AudioSpec *spec);
67 static void Paud_WaitAudio(_THIS);
68 static void Paud_PlayAudio(_THIS);
69 static Uint8 *Paud_GetAudioBuf(_THIS);
70 static void Paud_CloseAudio(_THIS);
71
72 /* Audio driver bootstrap functions */
73
74 static int Audio_Available(void)
75 {
76 int fd;
77 int available;
78
79 available = 0;
80 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
81 if ( fd >= 0 ) {
82 available = 1;
83 close(fd);
84 }
85 return(available);
86 }
87
88 static void Audio_DeleteDevice(SDL_AudioDevice *device)
89 {
90 free(device->hidden);
91 free(device);
92 }
93
94 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
95 {
96 SDL_AudioDevice *this;
97
98 /* Initialize all variables that we clean on shutdown */
99 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
100 if ( this ) {
101 memset(this, 0, (sizeof *this));
102 this->hidden = (struct SDL_PrivateAudioData *)
103 malloc((sizeof *this->hidden));
104 }
105 if ( (this == NULL) || (this->hidden == NULL) ) {
106 SDL_OutOfMemory();
107 if ( this ) {
108 free(this);
109 }
110 return(0);
111 }
112 memset(this->hidden, 0, (sizeof *this->hidden));
113 audio_fd = -1;
114
115 /* Set the function pointers */
116 this->OpenAudio = Paud_OpenAudio;
117 this->WaitAudio = Paud_WaitAudio;
118 this->PlayAudio = Paud_PlayAudio;
119 this->GetAudioBuf = Paud_GetAudioBuf;
120 this->CloseAudio = Paud_CloseAudio;
121
122 this->free = Audio_DeleteDevice;
123
124 return this;
125 }
126
127 AudioBootStrap Paud_bootstrap = {
128 Paud_DRIVER_NAME, "AIX Paudio",
129 Audio_Available, Audio_CreateDevice
130 };
131
132 /* This function waits until it is possible to write a full sound buffer */
133 static void Paud_WaitAudio(_THIS)
134 {
135 fd_set fdset;
136
137 /* See if we need to use timed audio synchronization */
138 if ( frame_ticks ) {
139 /* Use timer for general audio synchronization */
140 Sint32 ticks;
141
142 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
143 if ( ticks > 0 ) {
144 SDL_Delay(ticks);
145 }
146 } else {
147 audio_buffer paud_bufinfo;
148
149 /* Use select() for audio synchronization */
150 struct timeval timeout;
151 FD_ZERO(&fdset);
152 FD_SET(audio_fd, &fdset);
153
154 if ( ioctl(audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0 ) {
155 #ifdef DEBUG_AUDIO
156 fprintf(stderr, "Couldn't get audio buffer information\n");
157 #endif
158 timeout.tv_sec = 10;
159 timeout.tv_usec = 0;
160 } else {
161 long ms_in_buf = paud_bufinfo.write_buf_time;
162 timeout.tv_sec = ms_in_buf/1000;
163 ms_in_buf = ms_in_buf - timeout.tv_sec*1000;
164 timeout.tv_usec = ms_in_buf*1000;
165 #ifdef DEBUG_AUDIO
166 fprintf( stderr,
167 "Waiting for write_buf_time=%ld,%ld\n",
168 timeout.tv_sec,
169 timeout.tv_usec );
170 #endif
171 }
172
173 #ifdef DEBUG_AUDIO
174 fprintf(stderr, "Waiting for audio to get ready\n");
175 #endif
176 if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
177 const char *message = "Audio timeout - buggy audio driver? (disabled)";
178 /*
179 * In general we should never print to the screen,
180 * but in this case we have no other way of letting
181 * the user know what happened.
182 */
183 fprintf(stderr, "SDL: %s - %s\n", strerror(errno), message);
184 this->enabled = 0;
185 /* Don't try to close - may hang */
186 audio_fd = -1;
187 #ifdef DEBUG_AUDIO
188 fprintf(stderr, "Done disabling audio\n");
189 #endif
190 }
191 #ifdef DEBUG_AUDIO
192 fprintf(stderr, "Ready!\n");
193 #endif
194 }
195 }
196
197 static void Paud_PlayAudio(_THIS)
198 {
199 int written;
200
201 /* Write the audio data, checking for EAGAIN on broken audio drivers */
202 do {
203 written = write(audio_fd, mixbuf, mixlen);
204 if ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) ) {
205 SDL_Delay(1); /* Let a little CPU time go by */
206 }
207 } while ( (written < 0) &&
208 ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)) );
209
210 /* If timer synchronization is enabled, set the next write frame */
211 if ( frame_ticks ) {
212 next_frame += frame_ticks;
213 }
214
215 /* If we couldn't write, assume fatal error for now */
216 if ( written < 0 ) {
217 this->enabled = 0;
218 }
219 #ifdef DEBUG_AUDIO
220 fprintf(stderr, "Wrote %d bytes of audio data\n", written);
221 #endif
222 }
223
224 static Uint8 *Paud_GetAudioBuf(_THIS)
225 {
226 return mixbuf;
227 }
228
229 static void Paud_CloseAudio(_THIS)
230 {
231 if ( mixbuf != NULL ) {
232 SDL_FreeAudioMem(mixbuf);
233 mixbuf = NULL;
234 }
235 if ( audio_fd >= 0 ) {
236 close(audio_fd);
237 audio_fd = -1;
238 }
239 }
240
241 static int Paud_OpenAudio(_THIS, SDL_AudioSpec *spec)
242 {
243 char audiodev[1024];
244 int format;
245 int bytes_per_sample;
246 Uint16 test_format;
247 audio_init paud_init;
248 audio_buffer paud_bufinfo;
249 audio_status paud_status;
250 audio_control paud_control;
251 audio_change paud_change;
252
253 /* Reset the timer synchronization flag */
254 frame_ticks = 0.0;
255
256 /* Open the audio device */
257 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
258 if ( audio_fd < 0 ) {
259 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
260 return -1;
261 }
262
263 /*
264 * We can't set the buffer size - just ask the device for the maximum
265 * that we can have.
266 */
267 if ( ioctl(audio_fd, AUDIO_BUFFER, &paud_bufinfo) < 0 ) {
268 SDL_SetError("Couldn't get audio buffer information");
269 return -1;
270 }
271
272 mixbuf = NULL;
273
274 if ( spec->channels > 1 )
275 spec->channels = 2;
276 else
277 spec->channels = 1;
278
279 /*
280 * Fields in the audio_init structure:
281 *
282 * Ignored by us:
283 *
284 * paud.loadpath[LOAD_PATH]; * DSP code to load, MWave chip only?
285 * paud.slot_number; * slot number of the adapter
286 * paud.device_id; * adapter identification number
287 *
288 * Input:
289 *
290 * paud.srate; * the sampling rate in Hz
291 * paud.bits_per_sample; * 8, 16, 32, ...
292 * paud.bsize; * block size for this rate
293 * paud.mode; * ADPCM, PCM, MU_LAW, A_LAW, SOURCE_MIX
294 * paud.channels; * 1=mono, 2=stereo
295 * paud.flags; * FIXED - fixed length data
296 * * LEFT_ALIGNED, RIGHT_ALIGNED (var len only)
297 * * TWOS_COMPLEMENT - 2's complement data
298 * * SIGNED - signed? comment seems wrong in sys/audio.h
299 * * BIG_ENDIAN
300 * paud.operation; * PLAY, RECORD
301 *
302 * Output:
303 *
304 * paud.flags; * PITCH - pitch is supported
305 * * INPUT - input is supported
306 * * OUTPUT - output is supported
307 * * MONITOR - monitor is supported
308 * * VOLUME - volume is supported
309 * * VOLUME_DELAY - volume delay is supported
310 * * BALANCE - balance is supported
311 * * BALANCE_DELAY - balance delay is supported
312 * * TREBLE - treble control is supported
313 * * BASS - bass control is supported
314 * * BESTFIT_PROVIDED - best fit returned
315 * * LOAD_CODE - DSP load needed
316 * paud.rc; * NO_PLAY - DSP code can't do play requests
317 * * NO_RECORD - DSP code can't do record requests
318 * * INVALID_REQUEST - request was invalid
319 * * CONFLICT - conflict with open's flags
320 * * OVERLOADED - out of DSP MIPS or memory
321 * paud.position_resolution; * smallest increment for position
322 */
323
324 paud_init.srate = spec->freq;
325 paud_init.mode = PCM;
326 paud_init.operation = PLAY;
327 paud_init.channels = spec->channels;
328
329 /* Try for a closest match on audio format */
330 format = 0;
331 for ( test_format = SDL_FirstAudioFormat(spec->format);
332 ! format && test_format; ) {
333 #ifdef DEBUG_AUDIO
334 fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
335 #endif
336 switch ( test_format ) {
337 case AUDIO_U8:
338 bytes_per_sample = 1;
339 paud_init.bits_per_sample = 8;
340 paud_init.flags = TWOS_COMPLEMENT | FIXED;
341 format = 1;
342 break;
343 case AUDIO_S8:
344 bytes_per_sample = 1;
345 paud_init.bits_per_sample = 8;
346 paud_init.flags = SIGNED |
347 TWOS_COMPLEMENT | FIXED;
348 format = 1;
349 break;
350 case AUDIO_S16LSB:
351 bytes_per_sample = 2;
352 paud_init.bits_per_sample = 16;
353 paud_init.flags = SIGNED |
354 TWOS_COMPLEMENT | FIXED;
355 format = 1;
356 break;
357 case AUDIO_S16MSB:
358 bytes_per_sample = 2;
359 paud_init.bits_per_sample = 16;
360 paud_init.flags = BIG_ENDIAN |
361 SIGNED |
362 TWOS_COMPLEMENT | FIXED;
363 format = 1;
364 break;
365 case AUDIO_U16LSB:
366 bytes_per_sample = 2;
367 paud_init.bits_per_sample = 16;
368 paud_init.flags = TWOS_COMPLEMENT | FIXED;
369 format = 1;
370 break;
371 case AUDIO_U16MSB:
372 bytes_per_sample = 2;
373 paud_init.bits_per_sample = 16;
374 paud_init.flags = BIG_ENDIAN |
375 TWOS_COMPLEMENT | FIXED;
376 format = 1;
377 break;
378 default:
379 break;
380 }
381 if ( ! format ) {
382 test_format = SDL_NextAudioFormat();
383 }
384 }
385 if ( format == 0 ) {
386 #ifdef DEBUG_AUDIO
387 fprintf(stderr, "Couldn't find any hardware audio formats\n");
388 #endif
389 SDL_SetError("Couldn't find any hardware audio formats");
390 return -1;
391 }
392 spec->format = test_format;
393
394 /*
395 * We know the buffer size and the max number of subsequent writes
396 * that can be pending. If more than one can pend, allow the application
397 * to do something like double buffering between our write buffer and
398 * the device's own buffer that we are filling with write() anyway.
399 *
400 * We calculate spec->samples like this because SDL_CalculateAudioSpec()
401 * will give put paud_bufinfo.write_buf_cap (or paud_bufinfo.write_buf_cap/2)
402 * into spec->size in return.
403 */
404 if ( paud_bufinfo.request_buf_cap == 1 )
405 {
406 spec->samples = paud_bufinfo.write_buf_cap
407 / bytes_per_sample
408 / spec->channels;
409 }
410 else
411 {
412 spec->samples = paud_bufinfo.write_buf_cap
413 / bytes_per_sample
414 / spec->channels
415 / 2;
416 }
417 paud_init.bsize = bytes_per_sample * spec->channels;
418
419 SDL_CalculateAudioSpec(spec);
420
421 /*
422 * The AIX paud device init can't modify the values of the audio_init
423 * structure that we pass to it. So we don't need any recalculation
424 * of this stuff and no reinit call as in linux dsp and dma code.
425 *
426 * /dev/paud supports all of the encoding formats, so we don't need
427 * to do anything like reopening the device, either.
428 */
429 if ( ioctl(audio_fd, AUDIO_INIT, &paud_init) < 0 ) {
430 switch ( paud_init.rc )
431 {
432 case 1 :
433 SDL_SetError("Couldn't set audio format: DSP can't do play requests");
434 return -1;
435 break;
436 case 2 :
437 SDL_SetError("Couldn't set audio format: DSP can't do record requests");
438 return -1;
439 break;
440 case 4 :
441 SDL_SetError("Couldn't set audio format: request was invalid");
442 return -1;
443 break;
444 case 5 :
445 SDL_SetError("Couldn't set audio format: conflict with open's flags");
446 return -1;
447 break;
448 case 6 :
449 SDL_SetError("Couldn't set audio format: out of DSP MIPS or memory");
450 return -1;
451 break;
452 default :
453 SDL_SetError("Couldn't set audio format: not documented in sys/audio.h");
454 return -1;
455 break;
456 }
457 }
458
459 /* Allocate mixing buffer */
460 mixlen = spec->size;
461 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
462 if ( mixbuf == NULL ) {
463 return -1;
464 }
465 memset(mixbuf, spec->silence, spec->size);
466
467 /*
468 * Set some paramters: full volume, first speaker that we can find.
469 * Ignore the other settings for now.
470 */
471 paud_change.input = AUDIO_IGNORE; /* the new input source */
472 paud_change.output = OUTPUT_1; /* EXTERNAL_SPEAKER,INTERNAL_SPEAKER,OUTPUT_1 */
473 paud_change.monitor = AUDIO_IGNORE; /* the new monitor state */
474 paud_change.volume = 0x7fffffff; /* volume level [0-0x7fffffff] */
475 paud_change.volume_delay = AUDIO_IGNORE; /* the new volume delay */
476 paud_change.balance = 0x3fffffff; /* the new balance */
477 paud_change.balance_delay = AUDIO_IGNORE; /* the new balance delay */
478 paud_change.treble = AUDIO_IGNORE; /* the new treble state */
479 paud_change.bass = AUDIO_IGNORE; /* the new bass state */
480 paud_change.pitch = AUDIO_IGNORE; /* the new pitch state */
481
482 paud_control.ioctl_request = AUDIO_CHANGE;
483 paud_control.request_info = (char*)&paud_change;
484 if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 ) {
485 #ifdef DEBUG_AUDIO
486 fprintf(stderr, "Can't change audio display settings\n" );
487 #endif
488 }
489
490 /*
491 * Tell the device to expect data. Actual start will wait for
492 * the first write() call.
493 */
494 paud_control.ioctl_request = AUDIO_START;
495 paud_control.position = 0;
496 if ( ioctl(audio_fd, AUDIO_CONTROL, &paud_control) < 0 ) {
497 #ifdef DEBUG_AUDIO
498 fprintf(stderr, "Can't start audio play\n" );
499 #endif
500 SDL_SetError("Can't start audio play");
501 return -1;
502 }
503
504 /* Check to see if we need to use select() workaround */
505 { char *workaround;
506 workaround = getenv("SDL_DSP_NOSELECT");
507 if ( workaround ) {
508 frame_ticks = (float)(spec->samples*1000)/spec->freq;
509 next_frame = SDL_GetTicks()+frame_ticks;
510 }
511 }
512
513 /* Get the parent process id (we're the parent of the audio thread) */
514 parent = getpid();
515
516 /* We're ready to rock and roll. :-) */
517 return 0;
518 }
519