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
22 Modified in Oct 2004 by Hannu Savolainen
25 #include "SDL_config.h"
27 /* Allow access to a raw mixing buffer */
29 #include <stdio.h> /* For perror() */
30 #include <string.h> /* For strerror() */
36 #include <sys/ioctl.h>
39 #if SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H
40 /* This is installed on some systems */
41 #include <soundcard.h>
43 /* This is recommended by OSS */
44 #include <sys/soundcard.h>
47 #include "SDL_timer.h"
48 #include "SDL_audio.h"
49 #include "../SDL_audiomem.h"
50 #include "../SDL_audio_c.h"
51 #include "../SDL_audiodev_c.h"
52 #include "SDL_dspaudio.h"
54 /* The tag name used by DSP audio */
55 #define DSP_DRIVER_NAME "dsp"
57 /* Open the audio device for playback, and don't block if busy */
58 #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK)
60 /* Audio driver functions */
61 static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec);
62 static void DSP_WaitAudio(_THIS);
63 static void DSP_PlayAudio(_THIS);
64 static Uint8 *DSP_GetAudioBuf(_THIS);
65 static void DSP_CloseAudio(_THIS);
67 /* Audio driver bootstrap functions */
69 static int Audio_Available(void)
75 fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
83 static void Audio_DeleteDevice(SDL_AudioDevice *device)
85 SDL_free(device->hidden);
89 static SDL_AudioDevice *Audio_CreateDevice(int devindex)
91 SDL_AudioDevice *this;
93 /* Initialize all variables that we clean on shutdown */
94 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
96 SDL_memset(this, 0, (sizeof *this));
97 this->hidden = (struct SDL_PrivateAudioData *)
98 SDL_malloc((sizeof *this->hidden));
100 if ( (this == NULL) || (this->hidden == NULL) ) {
107 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
110 /* Set the function pointers */
111 this->OpenAudio = DSP_OpenAudio;
112 this->WaitAudio = DSP_WaitAudio;
113 this->PlayAudio = DSP_PlayAudio;
114 this->GetAudioBuf = DSP_GetAudioBuf;
115 this->CloseAudio = DSP_CloseAudio;
117 this->free = Audio_DeleteDevice;
122 AudioBootStrap DSP_bootstrap = {
123 DSP_DRIVER_NAME, "OSS /dev/dsp standard audio",
124 Audio_Available, Audio_CreateDevice
127 /* This function waits until it is possible to write a full sound buffer */
128 static void DSP_WaitAudio(_THIS)
130 /* Not needed at all since OSS handles waiting automagically */
133 static void DSP_PlayAudio(_THIS)
135 if (write(audio_fd, mixbuf, mixlen)==-1)
137 perror("Audio write");
142 fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen);
146 static Uint8 *DSP_GetAudioBuf(_THIS)
151 static void DSP_CloseAudio(_THIS)
153 if ( mixbuf != NULL ) {
154 SDL_FreeAudioMem(mixbuf);
157 if ( audio_fd >= 0 ) {
163 static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec)
171 /* Make sure fragment size stays a power of 2, or OSS fails. */
172 /* I don't know which of these are actually legal values, though... */
173 if (spec->channels > 8)
175 else if (spec->channels > 4)
177 else if (spec->channels > 2)
180 /* Open the audio device */
181 audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
182 if ( audio_fd < 0 ) {
183 SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
188 /* Make the file descriptor use blocking writes with fcntl() */
190 flags = fcntl(audio_fd, F_GETFL);
191 flags &= ~O_NONBLOCK;
192 if ( fcntl(audio_fd, F_SETFL, flags) < 0 ) {
193 SDL_SetError("Couldn't set audio blocking mode");
194 DSP_CloseAudio(this);
199 /* Get a list of supported hardware formats */
200 if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) {
201 perror("SNDCTL_DSP_GETFMTS");
202 SDL_SetError("Couldn't get audio format list");
203 DSP_CloseAudio(this);
207 /* Try for a closest match on audio format */
209 for ( test_format = SDL_FirstAudioFormat(spec->format);
210 ! format && test_format; ) {
212 fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
214 switch ( test_format ) {
216 if ( value & AFMT_U8 ) {
221 if ( value & AFMT_S16_LE ) {
222 format = AFMT_S16_LE;
226 if ( value & AFMT_S16_BE ) {
227 format = AFMT_S16_BE;
232 * These formats are not used by any real life systems so they are not
236 if ( value & AFMT_S8 ) {
241 if ( value & AFMT_U16_LE ) {
242 format = AFMT_U16_LE;
246 if ( value & AFMT_U16_BE ) {
247 format = AFMT_U16_BE;
256 test_format = SDL_NextAudioFormat();
260 SDL_SetError("Couldn't find any hardware audio formats");
261 DSP_CloseAudio(this);
264 spec->format = test_format;
266 /* Set the audio format */
268 if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
269 (value != format) ) {
270 perror("SNDCTL_DSP_SETFMT");
271 SDL_SetError("Couldn't set audio format");
272 DSP_CloseAudio(this);
276 /* Set the number of channels of output */
277 value = spec->channels;
278 if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) {
279 perror("SNDCTL_DSP_CHANNELS");
280 SDL_SetError("Cannot set the number of channels");
281 DSP_CloseAudio(this);
284 spec->channels = value;
286 /* Set the DSP frequency */
288 if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) {
289 perror("SNDCTL_DSP_SPEED");
290 SDL_SetError("Couldn't set audio frequency");
291 DSP_CloseAudio(this);
296 /* Calculate the final parameters for this audio specification */
297 SDL_CalculateAudioSpec(spec);
299 /* Determine the power of two of the fragment size */
300 for ( frag_spec = 0; (0x01U<<frag_spec) < spec->size; ++frag_spec );
301 if ( (0x01U<<frag_spec) != spec->size ) {
302 SDL_SetError("Fragment size must be a power of two");
303 DSP_CloseAudio(this);
306 frag_spec |= 0x00020000; /* two fragments, for low latency */
308 /* Set the audio buffering parameters */
310 fprintf(stderr, "Requesting %d fragments of size %d\n",
311 (frag_spec >> 16), 1<<(frag_spec&0xFFFF));
313 if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) {
314 perror("SNDCTL_DSP_SETFRAGMENT");
317 { audio_buf_info info;
318 ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info);
319 fprintf(stderr, "fragments = %d\n", info.fragments);
320 fprintf(stderr, "fragstotal = %d\n", info.fragstotal);
321 fprintf(stderr, "fragsize = %d\n", info.fragsize);
322 fprintf(stderr, "bytes = %d\n", info.bytes);
326 /* Allocate mixing buffer */
328 mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
329 if ( mixbuf == NULL ) {
330 DSP_CloseAudio(this);
333 SDL_memset(mixbuf, spec->silence, spec->size);
335 /* Get the parent process id (we're the parent of the audio thread) */
338 /* We're ready to rock and roll. :-) */