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