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/types.h>
|
|
38 |
#include <sys/time.h>
|
|
39 |
#include <sys/ioctl.h>
|
|
40 |
#include <sys/stat.h>
|
|
41 |
#include <sys/mman.h>
|
|
42 |
#include <sys/soundcard.h>
|
|
43 |
|
|
44 |
#ifndef MAP_FAILED
|
|
45 |
#define MAP_FAILED ((Uint8 *)-1)
|
|
46 |
#endif
|
|
47 |
|
|
48 |
#include "SDL_audio.h"
|
|
49 |
#include "SDL_error.h"
|
|
50 |
#include "SDL_audiomem.h"
|
|
51 |
#include "SDL_audio_c.h"
|
|
52 |
#include "SDL_timer.h"
|
|
53 |
#include "SDL_audiodev_c.h"
|
|
54 |
#include "SDL_dmaaudio.h"
|
|
55 |
|
|
56 |
/* The tag name used by DMA audio */
|
|
57 |
#define DMA_DRIVER_NAME "dma"
|
|
58 |
|
|
59 |
/* Open the audio device for playback, and don't block if busy */
|
|
60 |
#define OPEN_FLAGS (O_RDWR|O_NONBLOCK)
|
|
61 |
|
|
62 |
/* Audio driver functions */
|
|
63 |
static int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec);
|
|
64 |
static void DMA_WaitAudio(_THIS);
|
|
65 |
static void DMA_PlayAudio(_THIS);
|
|
66 |
static Uint8 *DMA_GetAudioBuf(_THIS);
|
|
67 |
static void DMA_CloseAudio(_THIS);
|
|
68 |
|
|
69 |
/* Audio driver bootstrap functions */
|
|
70 |
|
|
71 |
static int Audio_Available(void)
|
|
72 |
{
|
|
73 |
int available;
|
|
74 |
int fd;
|
|
75 |
|
|
76 |
available = 0;
|
|
77 |
|
|
78 |
fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
|
|
79 |
if ( fd >= 0 ) {
|
|
80 |
int caps;
|
|
81 |
struct audio_buf_info info;
|
|
82 |
|
|
83 |
if ( (ioctl(fd, SNDCTL_DSP_GETCAPS, &caps) == 0) &&
|
|
84 |
(caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) &&
|
|
85 |
(ioctl(fd, SNDCTL_DSP_GETOSPACE, &info) == 0) ) {
|
|
86 |
available = 1;
|
|
87 |
}
|
|
88 |
close(fd);
|
|
89 |
}
|
|
90 |
return(available);
|
|
91 |
}
|
|
92 |
|
|
93 |
static void Audio_DeleteDevice(SDL_AudioDevice *device)
|
|
94 |
{
|
|
95 |
free(device->hidden);
|
|
96 |
free(device);
|
|
97 |
}
|
|
98 |
|
|
99 |
static SDL_AudioDevice *Audio_CreateDevice(int devindex)
|
|
100 |
{
|
|
101 |
SDL_AudioDevice *this;
|
|
102 |
|
|
103 |
/* Initialize all variables that we clean on shutdown */
|
|
104 |
this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice));
|
|
105 |
if ( this ) {
|
|
106 |
memset(this, 0, (sizeof *this));
|
|
107 |
this->hidden = (struct SDL_PrivateAudioData *)
|
|
108 |
malloc((sizeof *this->hidden));
|
|
109 |
}
|
|
110 |
if ( (this == NULL) || (this->hidden == NULL) ) {
|
|
111 |
SDL_OutOfMemory();
|
|
112 |
if ( this ) {
|
|
113 |
free(this);
|
|
114 |
}
|
|
115 |
return(0);
|
|
116 |
}
|
|
117 |
memset(this->hidden, 0, (sizeof *this->hidden));
|
|
118 |
audio_fd = -1;
|
|
119 |
|
|
120 |
/* Set the function pointers */
|
|
121 |
this->OpenAudio = DMA_OpenAudio;
|
|
122 |
this->WaitAudio = DMA_WaitAudio;
|
|
123 |
this->PlayAudio = DMA_PlayAudio;
|
|
124 |
this->GetAudioBuf = DMA_GetAudioBuf;
|
|
125 |
this->CloseAudio = DMA_CloseAudio;
|
|
126 |
|
|
127 |
this->free = Audio_DeleteDevice;
|
|
128 |
|
|
129 |
return this;
|
|
130 |
}
|
|
131 |
|
|
132 |
AudioBootStrap DMA_bootstrap = {
|
|
133 |
DMA_DRIVER_NAME, "OSS /dev/dsp DMA audio",
|
|
134 |
Audio_Available, Audio_CreateDevice
|
|
135 |
};
|
|
136 |
|
|
137 |
/* This function waits until it is possible to write a full sound buffer */
|
|
138 |
static void DMA_WaitAudio(_THIS)
|
|
139 |
{
|
|
140 |
fd_set fdset;
|
|
141 |
|
|
142 |
/* Check to see if the thread-parent process is still alive */
|
|
143 |
{ static int cnt = 0;
|
|
144 |
/* Note that this only works with thread implementations
|
|
145 |
that use a different process id for each thread.
|
|
146 |
*/
|
|
147 |
if (parent && (((++cnt)%10) == 0)) { /* Check every 10 loops */
|
|
148 |
if ( kill(parent, 0) < 0 ) {
|
|
149 |
this->enabled = 0;
|
|
150 |
}
|
|
151 |
}
|
|
152 |
}
|
|
153 |
|
|
154 |
/* See if we need to use timed audio synchronization */
|
|
155 |
if ( frame_ticks ) {
|
|
156 |
/* Use timer for general audio synchronization */
|
|
157 |
Sint32 ticks;
|
|
158 |
|
|
159 |
ticks = ((Sint32)(next_frame - SDL_GetTicks()))-FUDGE_TICKS;
|
|
160 |
if ( ticks > 0 ) {
|
|
161 |
SDL_Delay(ticks);
|
|
162 |
}
|
|
163 |
} else {
|
|
164 |
/* Use select() for audio synchronization */
|
|
165 |
struct timeval timeout;
|
|
166 |
FD_ZERO(&fdset);
|
|
167 |
FD_SET(audio_fd, &fdset);
|
|
168 |
timeout.tv_sec = 10;
|
|
169 |
timeout.tv_usec = 0;
|
|
170 |
#ifdef DEBUG_AUDIO
|
|
171 |
fprintf(stderr, "Waiting for audio to get ready\n");
|
|
172 |
#endif
|
|
173 |
if ( select(audio_fd+1, NULL, &fdset, NULL, &timeout) <= 0 ) {
|
|
174 |
const char *message =
|
|
175 |
#ifdef AUDIO_OSPACE_HACK
|
|
176 |
"Audio timeout - buggy audio driver? (trying ospace)";
|
|
177 |
#else
|
|
178 |
"Audio timeout - buggy audio driver? (disabled)";
|
|
179 |
#endif
|
|
180 |
/* In general we should never print to the screen,
|
|
181 |
but in this case we have no other way of letting
|
|
182 |
the user know what happened.
|
|
183 |
*/
|
|
184 |
fprintf(stderr, "SDL: %s\n", message);
|
|
185 |
#ifdef AUDIO_OSPACE_HACK
|
|
186 |
/* We may be able to use GET_OSPACE trick */
|
|
187 |
frame_ticks = (float)(this->spec->samples*1000) /
|
|
188 |
this->spec->freq;
|
|
189 |
next_frame = SDL_GetTicks()+frame_ticks;
|
|
190 |
#else
|
|
191 |
this->enabled = 0;
|
|
192 |
/* Don't try to close - may hang */
|
|
193 |
audio_fd = -1;
|
|
194 |
#ifdef DEBUG_AUDIO
|
|
195 |
fprintf(stderr, "Done disabling audio\n");
|
|
196 |
#endif
|
|
197 |
#endif /* AUDIO_OSPACE_HACK */
|
|
198 |
}
|
|
199 |
#ifdef DEBUG_AUDIO
|
|
200 |
fprintf(stderr, "Ready!\n");
|
|
201 |
#endif
|
|
202 |
}
|
|
203 |
}
|
|
204 |
|
|
205 |
static void DMA_PlayAudio(_THIS)
|
|
206 |
{
|
|
207 |
/* If timer synchronization is enabled, set the next write frame */
|
|
208 |
if ( frame_ticks ) {
|
|
209 |
next_frame += frame_ticks;
|
|
210 |
}
|
|
211 |
return;
|
|
212 |
}
|
|
213 |
|
|
214 |
static Uint8 *DMA_GetAudioBuf(_THIS)
|
|
215 |
{
|
|
216 |
count_info info;
|
|
217 |
int playing;
|
|
218 |
int filling;
|
|
219 |
|
|
220 |
/* Get number of blocks, looping if we're not using select() */
|
|
221 |
do {
|
|
222 |
if ( ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &info) < 0 ) {
|
|
223 |
/* Uh oh... */
|
|
224 |
this->enabled = 0;
|
|
225 |
return(NULL);
|
|
226 |
}
|
|
227 |
} while ( frame_ticks && (info.blocks < 1) );
|
|
228 |
#ifdef DEBUG_AUDIO
|
|
229 |
if ( info.blocks > 1 ) {
|
|
230 |
printf("Warning: audio underflow (%d frags)\n", info.blocks-1);
|
|
231 |
}
|
|
232 |
#endif
|
|
233 |
playing = info.ptr / this->spec.size;
|
|
234 |
filling = (playing + 1)%num_buffers;
|
|
235 |
return (dma_buf + (filling * this->spec.size));
|
|
236 |
}
|
|
237 |
|
|
238 |
static void DMA_CloseAudio(_THIS)
|
|
239 |
{
|
|
240 |
if ( dma_buf != NULL ) {
|
|
241 |
munmap(dma_buf, dma_len);
|
|
242 |
dma_buf = NULL;
|
|
243 |
}
|
|
244 |
if ( audio_fd >= 0 ) {
|
|
245 |
close(audio_fd);
|
|
246 |
audio_fd = -1;
|
|
247 |
}
|
|
248 |
}
|
|
249 |
|
|
250 |
static int DMA_ReopenAudio(_THIS, const char *audiodev, int format, int stereo,
|
|
251 |
SDL_AudioSpec *spec)
|
|
252 |
{
|
|
253 |
int frag_spec;
|
|
254 |
int value;
|
|
255 |
|
|
256 |
/* Close and then reopen the audio device */
|
|
257 |
close(audio_fd);
|
|
258 |
audio_fd = open(audiodev, O_RDWR, 0);
|
|
259 |
if ( audio_fd < 0 ) {
|
|
260 |
SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
|
|
261 |
return(-1);
|
|
262 |
}
|
|
263 |
ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
|
|
264 |
|
|
265 |
/* Calculate the final parameters for this audio specification */
|
|
266 |
SDL_CalculateAudioSpec(spec);
|
|
267 |
|
|
268 |
/* Determine the power of two of the fragment size */
|
|
269 |
for ( frag_spec = 0; (0x01<<frag_spec) < spec->size; ++frag_spec );
|
|
270 |
if ( (0x01<<frag_spec) != spec->size ) {
|
|
271 |
SDL_SetError("Fragment size must be a power of two");
|
|
272 |
return(-1);
|
|
273 |
}
|
|
274 |
|
|
275 |
/* Set the audio buffering parameters */
|
|
276 |
if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) {
|
|
277 |
SDL_SetError("Couldn't set audio fragment spec");
|
|
278 |
return(-1);
|
|
279 |
}
|
|
280 |
|
|
281 |
/* Set the audio format */
|
|
282 |
value = format;
|
|
283 |
if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
|
|
284 |
(value != format) ) {
|
|
285 |
SDL_SetError("Couldn't set audio format");
|
|
286 |
return(-1);
|
|
287 |
}
|
|
288 |
|
|
289 |
/* Set mono or stereo audio */
|
|
290 |
value = (spec->channels > 1);
|
|
291 |
if ( (ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo) < 0) ||
|
|
292 |
(value != stereo) ) {
|
|
293 |
SDL_SetError("Couldn't set audio channels");
|
|
294 |
return(-1);
|
|
295 |
}
|
|
296 |
|
|
297 |
/* Set the DSP frequency */
|
|
298 |
value = spec->freq;
|
|
299 |
if ( ioctl(audio_fd, SOUND_PCM_WRITE_RATE, &value) < 0 ) {
|
|
300 |
SDL_SetError("Couldn't set audio frequency");
|
|
301 |
return(-1);
|
|
302 |
}
|
|
303 |
spec->freq = value;
|
|
304 |
|
|
305 |
/* We successfully re-opened the audio */
|
|
306 |
return(0);
|
|
307 |
}
|
|
308 |
|
|
309 |
static int DMA_OpenAudio(_THIS, SDL_AudioSpec *spec)
|
|
310 |
{
|
|
311 |
char audiodev[1024];
|
|
312 |
int format;
|
|
313 |
int stereo;
|
|
314 |
int value;
|
|
315 |
Uint16 test_format;
|
|
316 |
struct audio_buf_info info;
|
|
317 |
|
|
318 |
/* Reset the timer synchronization flag */
|
|
319 |
frame_ticks = 0.0;
|
|
320 |
|
|
321 |
/* Open the audio device */
|
|
322 |
audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
|
|
323 |
if ( audio_fd < 0 ) {
|
|
324 |
SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
|
|
325 |
return(-1);
|
|
326 |
}
|
|
327 |
dma_buf = NULL;
|
|
328 |
ioctl(audio_fd, SNDCTL_DSP_RESET, 0);
|
|
329 |
|
|
330 |
/* Get a list of supported hardware formats */
|
|
331 |
if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) {
|
|
332 |
SDL_SetError("Couldn't get audio format list");
|
|
333 |
return(-1);
|
|
334 |
}
|
|
335 |
|
|
336 |
/* Try for a closest match on audio format */
|
|
337 |
format = 0;
|
|
338 |
for ( test_format = SDL_FirstAudioFormat(spec->format);
|
|
339 |
! format && test_format; ) {
|
|
340 |
#ifdef DEBUG_AUDIO
|
|
341 |
fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
|
|
342 |
#endif
|
|
343 |
switch ( test_format ) {
|
|
344 |
case AUDIO_U8:
|
|
345 |
if ( value & AFMT_U8 ) {
|
|
346 |
format = AFMT_U8;
|
|
347 |
}
|
|
348 |
break;
|
|
349 |
case AUDIO_S8:
|
|
350 |
if ( value & AFMT_S8 ) {
|
|
351 |
format = AFMT_S8;
|
|
352 |
}
|
|
353 |
break;
|
|
354 |
case AUDIO_S16LSB:
|
|
355 |
if ( value & AFMT_S16_LE ) {
|
|
356 |
format = AFMT_S16_LE;
|
|
357 |
}
|
|
358 |
break;
|
|
359 |
case AUDIO_S16MSB:
|
|
360 |
if ( value & AFMT_S16_BE ) {
|
|
361 |
format = AFMT_S16_BE;
|
|
362 |
}
|
|
363 |
break;
|
|
364 |
case AUDIO_U16LSB:
|
|
365 |
if ( value & AFMT_U16_LE ) {
|
|
366 |
format = AFMT_U16_LE;
|
|
367 |
}
|
|
368 |
break;
|
|
369 |
case AUDIO_U16MSB:
|
|
370 |
if ( value & AFMT_U16_BE ) {
|
|
371 |
format = AFMT_U16_BE;
|
|
372 |
}
|
|
373 |
break;
|
|
374 |
default:
|
|
375 |
break;
|
|
376 |
}
|
|
377 |
if ( ! format ) {
|
|
378 |
test_format = SDL_NextAudioFormat();
|
|
379 |
}
|
|
380 |
}
|
|
381 |
if ( format == 0 ) {
|
|
382 |
SDL_SetError("Couldn't find any hardware audio formats");
|
|
383 |
return(-1);
|
|
384 |
}
|
|
385 |
spec->format = test_format;
|
|
386 |
|
|
387 |
/* Set the audio format */
|
|
388 |
value = format;
|
|
389 |
if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
|
|
390 |
(value != format) ) {
|
|
391 |
SDL_SetError("Couldn't set audio format");
|
|
392 |
return(-1);
|
|
393 |
}
|
|
394 |
|
|
395 |
/* Set mono or stereo audio (currently only two channels supported) */
|
|
396 |
stereo = (spec->channels > 1);
|
|
397 |
ioctl(audio_fd, SNDCTL_DSP_STEREO, &stereo);
|
|
398 |
if ( stereo ) {
|
|
399 |
spec->channels = 2;
|
|
400 |
} else {
|
|
401 |
spec->channels = 1;
|
|
402 |
}
|
|
403 |
|
|
404 |
/* Because some drivers don't allow setting the buffer size
|
|
405 |
after setting the format, we must re-open the audio device
|
|
406 |
once we know what format and channels are supported
|
|
407 |
*/
|
|
408 |
if ( DMA_ReopenAudio(this, audiodev, format, stereo, spec) < 0 ) {
|
|
409 |
/* Error is set by DMA_ReopenAudio() */
|
|
410 |
return(-1);
|
|
411 |
}
|
|
412 |
|
|
413 |
/* Memory map the audio buffer */
|
|
414 |
if ( ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info) < 0 ) {
|
|
415 |
SDL_SetError("Couldn't get OSPACE parameters");
|
|
416 |
return(-1);
|
|
417 |
}
|
|
418 |
spec->size = info.fragsize;
|
|
419 |
spec->samples = spec->size / ((spec->format & 0xFF) / 8);
|
|
420 |
spec->samples /= spec->channels;
|
|
421 |
num_buffers = info.fragstotal;
|
|
422 |
dma_len = num_buffers*spec->size;
|
|
423 |
dma_buf = (Uint8 *)mmap(NULL, dma_len, PROT_WRITE, MAP_SHARED,
|
|
424 |
audio_fd, 0);
|
|
425 |
if ( dma_buf == MAP_FAILED ) {
|
|
426 |
SDL_SetError("DMA memory map failed");
|
|
427 |
dma_buf = NULL;
|
|
428 |
return(-1);
|
|
429 |
}
|
|
430 |
memset(dma_buf, spec->silence, dma_len);
|
|
431 |
|
|
432 |
/* Check to see if we need to use select() workaround */
|
|
433 |
{ char *workaround;
|
|
434 |
workaround = getenv("SDL_DSP_NOSELECT");
|
|
435 |
if ( workaround ) {
|
|
436 |
frame_ticks = (float)(spec->samples*1000)/spec->freq;
|
|
437 |
next_frame = SDL_GetTicks()+frame_ticks;
|
|
438 |
}
|
|
439 |
}
|
|
440 |
|
|
441 |
/* Trigger audio playback */
|
|
442 |
value = 0;
|
|
443 |
ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value);
|
|
444 |
value = PCM_ENABLE_OUTPUT;
|
|
445 |
if ( ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &value) < 0 ) {
|
|
446 |
SDL_SetError("Couldn't trigger audio output");
|
|
447 |
return(-1);
|
|
448 |
}
|
|
449 |
|
|
450 |
/* Get the parent process id (we're the parent of the audio thread) */
|
|
451 |
parent = getpid();
|
|
452 |
|
|
453 |
/* We're ready to rock and roll. :-) */
|
|
454 |
return(0);
|
|
455 |
}
|