0
|
1 /*
|
|
2 SDL - Simple DirectMedia Layer
|
|
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
|
|
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 Sam Lantinga
|
|
20 slouken@devolution.com
|
|
21 */
|
|
22
|
|
23 #ifdef SAVE_RCSID
|
|
24 static char rcsid =
|
|
25 "@(#) $Id$";
|
|
26 #endif
|
|
27
|
|
28 /* Allow access to a raw mixing buffer */
|
|
29
|
|
30 #include <stdlib.h>
|
|
31 #include <stdio.h>
|
|
32 #include <string.h>
|
|
33 #include <errno.h>
|
|
34 #include <unistd.h>
|
|
35 #include <fcntl.h>
|
|
36 #include <signal.h>
|
|
37 #include <sys/time.h>
|
|
38 #include <sys/ioctl.h>
|
|
39 #include <sys/stat.h>
|
|
40 #include <sys/soundcard.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_dspaudio.h"
|
|
49
|
|
50 /* The tag name used by DSP audio */
|
|
51 #define DSP_DRIVER_NAME "dsp"
|
|
52
|
|
53 /* Open the audio device for playback, and don't block if busy */
|
|
54 /*#define USE_BLOCKING_WRITES*/
|
|
55 #ifdef USE_BLOCKING_WRITES
|
|
56 #define OPEN_FLAGS O_WRONLY
|
|
57 #else
|
|
58 #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK)
|
|
59 #endif
|
|
60
|
|
61 /* Audio driver functions */
|
|
62 static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec);
|
|
63 static void DSP_WaitAudio(_THIS);
|
|
64 static void DSP_PlayAudio(_THIS);
|
|
65 static Uint8 *DSP_GetAudioBuf(_THIS);
|
|
66 static void DSP_CloseAudio(_THIS);
|
|
67
|
|
68 /* Audio driver bootstrap functions */
|
|
69
|
|
70 static int Audio_Available(void)
|
|
71 {
|
|
72 int fd;
|
|
73 int available;
|
|
74
|
|
75 available = 0;
|
|
76 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
|
|
77 if ( fd >= 0 ) {
|
|
78 available = 1;
|
|
79 close(fd);
|
|
80 }
|
|
81 return(available);
|
|
82 }
|
|
83
|
|
84 static void Audio_DeleteDevice(SDL_AudioDevice *device)
|
|
85 {
|
|
86 free(device->hidden);
|
|
87 free(device);
|
|
88 }
|
|
89
|
|
90 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
|
|
91 {
|
|
92 SDL_AudioDevice *this;
|
|
93
|
|
94 /* Initialize all variables that we clean on shutdown */
|
|
95 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
|
|
96 if ( this ) {
|
|
97 memset(this, 0, (sizeof *this));
|
|
98 this->hidden = (struct SDL_PrivateAudioData *)
|
|
99 malloc((sizeof *this->hidden));
|
|
100 }
|
|
101 if ( (this == NULL) || (this->hidden == NULL) ) {
|
|
102 SDL_OutOfMemory();
|
|
103 if ( this ) {
|
|
104 free(this);
|
|
105 }
|
|
106 return(0);
|
|
107 }
|
|
108 memset(this->hidden, 0, (sizeof *this->hidden));
|
|
109 audio_fd = -1;
|
|
110
|
|
111 /* Set the function pointers */
|
|
112 this->OpenAudio = DSP_OpenAudio;
|
|
113 this->WaitAudio = DSP_WaitAudio;
|
|
114 this->PlayAudio = DSP_PlayAudio;
|
|
115 this->GetAudioBuf = DSP_GetAudioBuf;
|
|
116 this->CloseAudio = DSP_CloseAudio;
|
|
117
|
|
118 this->free = Audio_DeleteDevice;
|
|
119
|
|
120 return this;
|
|
121 }
|
|
122
|
|
123 AudioBootStrap DSP_bootstrap = {
|
|
124 DSP_DRIVER_NAME, "OSS /dev/dsp standard audio",
|
|
125 Audio_Available, Audio_CreateDevice
|
|
126 };
|
|
127
|
|
128 /* This function waits until it is possible to write a full sound buffer */
|
|
129 static void DSP_WaitAudio(_THIS)
|
|
130 {
|
|
131 #ifndef USE_BLOCKING_WRITES /* Not necessary because of blocking writes */
|
|
132 fd_set fdset;
|
|
133
|
|
134 /* Check to see if the thread-parent process is still alive */
|
|
135 { static int cnt = 0;
|
|
136 /* Note that this only works with thread implementations
|
|
137 that use a different process id for each thread.
|
|
138 */
|
|
139 if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
|
|
140 if ( kill(parent, 0) < 0 ) {
|
|
141 this->enabled = 0;
|
|
142 }
|
|
143 }
|
|
144 }
|
|
145
|
|
146 /* See if we need to use timed audio synchronization */
|
|
147 if ( frame_ticks ) {
|
|
148 /* Use timer for general audio synchronization */
|
|
149 Sint32 ticks;
|
|
150
|
|
151 ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
|
|
152 if ( ticks > 0 ) {
|
|
153 SDL_Delay(ticks);
|
|
154 }
|
|
155 } else {
|
|
156 /* Use select() for audio synchronization */
|
|
157 struct timeval timeout;
|
|
158 FD_ZERO(&fdset);
|
|
159 FD_SET(audio_fd, &fdset);
|
|
160 timeout.tv_sec = 10;
|
|
161 timeout.tv_usec = 0;
|
|
162 #ifdef DEBUG_AUDIO
|
|
163 fprintf(stderr, "Waiting for audio to get ready\n");
|
|
164 #endif
|
|
165 if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
|
|
166 const char *message =
|
|
167 "Audio timeout - buggy audio driver? (disabled)";
|
|
168 /* In general we should never print to the screen,
|
|
169 but in this case we have no other way of letting
|
|
170 the user know what happened.
|
|
171 */
|
|
172 fprintf(stderr, "SDL: %s\n", message);
|
|
173 this->enabled = 0;
|
|
174 /* Don't try to close - may hang */
|
|
175 audio_fd = -1;
|
|
176 #ifdef DEBUG_AUDIO
|
|
177 fprintf(stderr, "Done disabling audio\n");
|
|
178 #endif
|
|
179 }
|
|
180 #ifdef DEBUG_AUDIO
|
|
181 fprintf(stderr, "Ready!\n");
|
|
182 #endif
|
|
183 }
|
|
184 #endif /* !USE_BLOCKING_WRITES */
|
|
185 }
|
|
186
|
|
187 static void DSP_PlayAudio(_THIS)
|
|
188 {
|
|
189 int written;
|
|
190
|
|
191 /* Write the audio data, checking for EAGAIN on broken audio drivers */
|
|
192 do {
|
|
193 written = write(audio_fd, mixbuf, mixlen);
|
|
194 if ( (written < 0) && ((errno == 0) || (errno == EAGAIN)) ) {
|
|
195 SDL_Delay(1); /* Let a little CPU time go by */
|
|
196 }
|
|
197 } while ( (written < 0) &&
|
|
198 ((errno == 0) || (errno == EAGAIN) || (errno == EINTR)) );
|
|
199
|
|
200 /* If timer synchronization is enabled, set the next write frame */
|
|
201 if ( frame_ticks ) {
|
|
202 next_frame += frame_ticks;
|
|
203 }
|
|
204
|
|
205 /* If we couldn't write, assume fatal error for now */
|
|
206 if ( written < 0 ) {
|
|
207 this->enabled = 0;
|
|
208 }
|
|
209 #ifdef DEBUG_AUDIO
|
|
210 fprintf(stderr, "Wrote %d bytes of audio data\n", written);
|
|
211 #endif
|
|
212 }
|
|
213
|
|
214 static Uint8 *DSP_GetAudioBuf(_THIS)
|
|
215 {
|
|
216 return(mixbuf);
|
|
217 }
|
|
218
|
|
219 static void DSP_CloseAudio(_THIS)
|
|
220 {
|
|
221 if ( mixbuf != NULL ) {
|
|
222 SDL_FreeAudioMem(mixbuf);
|
|
223 mixbuf = NULL;
|
|
224 }
|
|
225 if ( audio_fd >= 0 ) {
|
|
226 close(audio_fd);
|
|
227 audio_fd = -1;
|
|
228 }
|
|
229 }
|
|
230
|
|
231 static int DSP_ReopenAudio(_THIS, const char *audiodev, int format,
|
|
232 SDL_AudioSpec *spec)
|
|
233 {
|
|
234 int frag_spec;
|
|
235 int value;
|
|
236
|
|
237 /* Close and then reopen the audio device */
|
|
238 close(audio_fd);
|
|
239 audio_fd = open(audiodev, O_WRONLY, 0);
|
|
240 if ( audio_fd < 0 ) {
|
|
241 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
|
|
242 return(-1);
|
|
243 }
|
|
244
|
|
245 /* Calculate the final parameters for this audio specification */
|
|
246 SDL_CalculateAudioSpec(spec);
|
|
247
|
|
248 /* Determine the power of two of the fragment size */
|
|
249 for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
|
|
250 if ( (0x01<<frag_spec) != spec->size ) {
|
|
251 SDL_SetError("Fragment size must be a power of two");
|
|
252 return(-1);
|
|
253 }
|
|
254 frag_spec |= 0x00020000; /* two fragments, for low latency */
|
|
255
|
|
256 /* Set the audio buffering parameters */
|
|
257 #ifdef DEBUG_AUDIO
|
|
258 fprintf(stderr, "Requesting %d fragments of size %d\n",
|
|
259 (frag_spec >> 16), 1<<(frag_spec&0xFFFF));
|
|
260 #endif
|
|
261 if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) {
|
|
262 fprintf(stderr, "Warning: Couldn't set audio fragment size\n");
|
|
263 }
|
|
264 #ifdef DEBUG_AUDIO
|
|
265 { audio_buf_info info;
|
|
266 ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info);
|
|
267 fprintf(stderr, "fragments = %d\n", info.fragments);
|
|
268 fprintf(stderr, "fragstotal = %d\n", info.fragstotal);
|
|
269 fprintf(stderr, "fragsize = %d\n", info.fragsize);
|
|
270 fprintf(stderr, "bytes = %d\n", info.bytes);
|
|
271 }
|
|
272 #endif
|
|
273
|
|
274 /* Set the audio format */
|
|
275 value = format;
|
|
276 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
|
|
277 (value != format) ) {
|
|
278 SDL_SetError("Couldn't set audio format");
|
|
279 return(-1);
|
|
280 }
|
|
281
|
|
282 /* Set the number of channels of output */
|
|
283 value = spec->channels;
|
|
284 #ifdef SNDCTL_DSP_CHANNELS
|
|
285 if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) {
|
|
286 #endif
|
|
287 value = (spec->channels > 1);
|
|
288 ioctl(audio_fd, SNDCTL_DSP_STEREO, &value);
|
|
289 value = (value ? 2 : 1);
|
|
290 #ifdef SNDCTL_DSP_CHANNELS
|
|
291 }
|
|
292 #endif
|
|
293 if ( value != spec->channels ) {
|
|
294 SDL_SetError("Couldn't set audio channels");
|
|
295 return(-1);
|
|
296 }
|
|
297
|
|
298 /* Set the DSP frequency */
|
|
299 value = spec->freq;
|
|
300 if ( ioctl(audio_fd, SOUND_PCM_WRITE_RATE, &value) < 0 ) {
|
|
301 SDL_SetError("Couldn't set audio frequency");
|
|
302 return(-1);
|
|
303 }
|
|
304 spec->freq = value;
|
|
305
|
|
306 /* We successfully re-opened the audio */
|
|
307 return(0);
|
|
308 }
|
|
309
|
|
310 static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec)
|
|
311 {
|
|
312 char audiodev[1024];
|
|
313 int format;
|
|
314 int value;
|
|
315 Uint16 test_format;
|
|
316
|
|
317 /* Reset the timer synchronization flag */
|
|
318 frame_ticks = 0.0;
|
|
319
|
|
320 /* Open the audio device */
|
|
321 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
|
|
322 if ( audio_fd < 0 ) {
|
|
323 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
|
|
324 return(-1);
|
|
325 }
|
|
326 mixbuf = NULL;
|
|
327
|
|
328 /* Get a list of supported hardware formats */
|
|
329 if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) {
|
|
330 SDL_SetError("Couldn't get audio format list");
|
|
331 return(-1);
|
|
332 }
|
|
333
|
|
334 /* Try for a closest match on audio format */
|
|
335 format = 0;
|
|
336 for ( test_format = SDL_FirstAudioFormat(spec->format);
|
|
337 ! format && test_format; ) {
|
|
338 #ifdef DEBUG_AUDIO
|
|
339 fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
|
|
340 #endif
|
|
341 switch ( test_format ) {
|
|
342 case AUDIO_U8:
|
|
343 if ( value & AFMT_U8 ) {
|
|
344 format = AFMT_U8;
|
|
345 }
|
|
346 break;
|
|
347 case AUDIO_S8:
|
|
348 if ( value & AFMT_S8 ) {
|
|
349 format = AFMT_S8;
|
|
350 }
|
|
351 break;
|
|
352 case AUDIO_S16LSB:
|
|
353 if ( value & AFMT_S16_LE ) {
|
|
354 format = AFMT_S16_LE;
|
|
355 }
|
|
356 break;
|
|
357 case AUDIO_S16MSB:
|
|
358 if ( value & AFMT_S16_BE ) {
|
|
359 format = AFMT_S16_BE;
|
|
360 }
|
|
361 break;
|
|
362 case AUDIO_U16LSB:
|
|
363 if ( value & AFMT_U16_LE ) {
|
|
364 format = AFMT_U16_LE;
|
|
365 }
|
|
366 break;
|
|
367 case AUDIO_U16MSB:
|
|
368 if ( value & AFMT_U16_BE ) {
|
|
369 format = AFMT_U16_BE;
|
|
370 }
|
|
371 break;
|
|
372 default:
|
|
373 break;
|
|
374 }
|
|
375 if ( ! format ) {
|
|
376 test_format = SDL_NextAudioFormat();
|
|
377 }
|
|
378 }
|
|
379 if ( format == 0 ) {
|
|
380 SDL_SetError("Couldn't find any hardware audio formats");
|
|
381 return(-1);
|
|
382 }
|
|
383 spec->format = test_format;
|
|
384
|
|
385 /* Set the audio format */
|
|
386 value = format;
|
|
387 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
|
|
388 (value != format) ) {
|
|
389 SDL_SetError("Couldn't set audio format");
|
|
390 return(-1);
|
|
391 }
|
|
392
|
|
393 /* Set the number of channels of output */
|
|
394 value = spec->channels;
|
|
395 #ifdef SNDCTL_DSP_CHANNELS
|
|
396 if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) {
|
|
397 #endif
|
|
398 value = (spec->channels > 1);
|
|
399 ioctl(audio_fd, SNDCTL_DSP_STEREO, &value);
|
|
400 value = (value ? 2 : 1);
|
|
401 #ifdef SNDCTL_DSP_CHANNELS
|
|
402 }
|
|
403 #endif
|
|
404 spec->channels = value;
|
|
405
|
|
406 /* Because some drivers don't allow setting the buffer size
|
|
407 after setting the format, we must re-open the audio device
|
|
408 once we know what format and channels are supported
|
|
409 */
|
|
410 if ( DSP_ReopenAudio(this, audiodev, format, spec) < 0 ) {
|
|
411 /* Error is set by DSP_ReopenAudio() */
|
|
412 return(-1);
|
|
413 }
|
|
414
|
|
415 /* Allocate mixing buffer */
|
|
416 mixlen = spec->size;
|
|
417 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
|
|
418 if ( mixbuf == NULL ) {
|
|
419 return(-1);
|
|
420 }
|
|
421 memset(mixbuf, spec->silence, spec->size);
|
|
422
|
|
423 #ifndef USE_BLOCKING_WRITES
|
|
424 /* Check to see if we need to use select() workaround */
|
|
425 { char *workaround;
|
|
426 workaround = getenv("SDL_DSP_NOSELECT");
|
|
427 if ( workaround ) {
|
|
428 frame_ticks = (float)(spec->samples*1000)/spec->freq;
|
|
429 next_frame = SDL_GetTicks()+frame_ticks;
|
|
430 }
|
|
431 }
|
|
432 #endif /* !USE_BLOCKING_WRITES */
|
|
433
|
|
434 /* Get the parent process id (we're the parent of the audio thread) */
|
|
435 parent = getpid();
|
|
436
|
|
437 /* We're ready to rock and roll. :-) */
|
|
438 return(0);
|
|
439 }
|