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 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.
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.
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
22 #include "SDL_config.h"
28 #include <sys/types.h>
31 #include <sys/select.h>
32 #include <sys/neutrino.h>
33 #include <sys/asoundlib.h>
35 #include "SDL_timer.h"
36 #include "SDL_audio.h"
37 #include "../SDL_audiomem.h"
38 #include "../SDL_audio_c.h"
39 #include "SDL_nto_audio.h"
41 /* The tag name used by NTO audio */
42 #define DRIVER_NAME "qsa-nto"
44 /* default channel communication parameters */
45 #define DEFAULT_CPARAMS_RATE 22050
46 #define DEFAULT_CPARAMS_VOICES 1
47 /* FIXME: need to add in the near future flexible logic with frag_size and frags count */
48 #define DEFAULT_CPARAMS_FRAG_SIZE 4096
49 #define DEFAULT_CPARAMS_FRAGS_MIN 1
50 #define DEFAULT_CPARAMS_FRAGS_MAX 1
52 /* Open the audio device for playback, and don't block if busy */
53 #define OPEN_FLAGS SND_PCM_OPEN_PLAYBACK
55 #define QSA_NO_WORKAROUNDS 0x00000000
56 #define QSA_MMAP_WORKAROUND 0x00000001
61 unsigned long bugtype;
64 #define QSA_WA_CARDS 3
66 struct BuggyCards buggycards[QSA_WA_CARDS]=
68 {"Sound Blaster Live!", QSA_MMAP_WORKAROUND},
69 {"Vortex 8820", QSA_MMAP_WORKAROUND},
70 {"Vortex 8830", QSA_MMAP_WORKAROUND},
73 /* Audio driver functions */
74 static void NTO_ThreadInit(_THIS);
75 static int NTO_OpenAudio(_THIS, SDL_AudioSpec* spec);
76 static void NTO_WaitAudio(_THIS);
77 static void NTO_PlayAudio(_THIS);
78 static Uint8* NTO_GetAudioBuf(_THIS);
79 static void NTO_CloseAudio(_THIS);
81 /* card names check to apply the workarounds */
82 static int NTO_CheckBuggyCards(_THIS, unsigned long checkfor)
87 if (snd_card_get_name(cardno, scardname, 32)<0)
92 for (it=0; it<QSA_WA_CARDS; it++)
94 if (SDL_strcmp(buggycards[it].cardname, scardname)==0)
96 if (buggycards[it].bugtype==checkfor)
106 static void NTO_ThreadInit(_THIS)
109 struct sched_param param;
111 /* increasing default 10 priority to 25 to avoid jerky sound */
112 status=SchedGet(0, 0, ¶m);
113 param.sched_priority=param.sched_curpriority+15;
114 status=SchedSet(0, 0, SCHED_NOCHANGE, ¶m);
117 /* PCM transfer channel parameters initialize function */
118 static void NTO_InitAudioParams(snd_pcm_channel_params_t* cpars)
120 SDL_memset(cpars, 0, sizeof(snd_pcm_channel_params_t));
122 cpars->channel = SND_PCM_CHANNEL_PLAYBACK;
123 cpars->mode = SND_PCM_MODE_BLOCK;
124 cpars->start_mode = SND_PCM_START_DATA;
125 cpars->stop_mode = SND_PCM_STOP_STOP;
126 cpars->format.format = SND_PCM_SFMT_S16_LE;
127 cpars->format.interleave = 1;
128 cpars->format.rate = DEFAULT_CPARAMS_RATE;
129 cpars->format.voices = DEFAULT_CPARAMS_VOICES;
130 cpars->buf.block.frag_size = DEFAULT_CPARAMS_FRAG_SIZE;
131 cpars->buf.block.frags_min = DEFAULT_CPARAMS_FRAGS_MIN;
132 cpars->buf.block.frags_max = DEFAULT_CPARAMS_FRAGS_MAX;
135 static int NTO_AudioAvailable(void)
137 /* See if we can open a nonblocking channel.
138 Return value '1' means we can.
139 Return value '0' means we cannot. */
148 rval = snd_pcm_open_preferred(&handle, NULL, NULL, OPEN_FLAGS);
154 if ((rval = snd_pcm_close(handle)) < 0)
156 SDL_SetError("NTO_AudioAvailable(): snd_pcm_close failed: %s\n", snd_strerror(rval));
162 SDL_SetError("NTO_AudioAvailable(): there are no available audio devices.\n");
168 static void NTO_DeleteAudioDevice(SDL_AudioDevice *device)
170 if ((device)&&(device->hidden))
172 SDL_free(device->hidden);
180 static SDL_AudioDevice* NTO_CreateAudioDevice(int devindex)
182 SDL_AudioDevice *this;
184 /* Initialize all variables that we clean on shutdown */
185 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
188 SDL_memset(this, 0, sizeof(SDL_AudioDevice));
189 this->hidden = (struct SDL_PrivateAudioData *)SDL_malloc(sizeof(struct SDL_PrivateAudioData));
191 if ((this == NULL) || (this->hidden == NULL))
200 SDL_memset(this->hidden, 0, sizeof(struct SDL_PrivateAudioData));
203 /* Set the function pointers */
204 this->ThreadInit = NTO_ThreadInit;
205 this->OpenAudio = NTO_OpenAudio;
206 this->WaitAudio = NTO_WaitAudio;
207 this->PlayAudio = NTO_PlayAudio;
208 this->GetAudioBuf = NTO_GetAudioBuf;
209 this->CloseAudio = NTO_CloseAudio;
211 this->free = NTO_DeleteAudioDevice;
216 AudioBootStrap QNXNTOAUDIO_bootstrap =
218 DRIVER_NAME, "QNX6 QSA-NTO Audio",
220 NTO_CreateAudioDevice
223 /* This function waits until it is possible to write a full sound buffer */
224 static void NTO_WaitAudio(_THIS)
230 FD_SET(audio_fd, &wfds);
233 selectret=select(audio_fd + 1, NULL, &wfds, NULL, NULL);
237 case 0: SDL_SetError("NTO_WaitAudio(): select() failed: %s\n", strerror(errno));
239 default: if (FD_ISSET(audio_fd, &wfds))
248 static void NTO_PlayAudio(_THIS)
259 towrite = this->spec.size;
262 /* Write the audio data, checking for EAGAIN (buffer full) and underrun */
264 written = snd_pcm_plugin_write(audio_handle, pcm_buf, towrite);
265 if (written != towrite)
267 if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
269 /* Let a little CPU time go by and try to write again */
271 /* if we wrote some data */
273 pcmbuffer += written * this->spec.channels;
278 if ((errno == EINVAL) || (errno == EIO))
280 SDL_memset(&cstatus, 0, sizeof(cstatus));
281 cstatus.channel = SND_PCM_CHANNEL_PLAYBACK;
282 if ((rval = snd_pcm_plugin_status(audio_handle, &cstatus)) < 0)
284 SDL_SetError("NTO_PlayAudio(): snd_pcm_plugin_status failed: %s\n", snd_strerror(rval));
287 if ((cstatus.status == SND_PCM_STATUS_UNDERRUN) || (cstatus.status == SND_PCM_STATUS_READY))
289 if ((rval = snd_pcm_plugin_prepare(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0)
291 SDL_SetError("NTO_PlayAudio(): snd_pcm_plugin_prepare failed: %s\n", snd_strerror(rval));
305 /* we wrote all remaining data */
307 pcmbuffer += written * this->spec.channels;
309 } while ((towrite > 0) && (this->enabled));
311 /* If we couldn't write, assume fatal error for now */
320 static Uint8* NTO_GetAudioBuf(_THIS)
325 static void NTO_CloseAudio(_THIS)
331 if (audio_handle != NULL)
333 if ((rval = snd_pcm_plugin_flush(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0)
335 SDL_SetError("NTO_CloseAudio(): snd_pcm_plugin_flush failed: %s\n", snd_strerror(rval));
338 if ((rval = snd_pcm_close(audio_handle)) < 0)
340 SDL_SetError("NTO_CloseAudio(): snd_pcm_close failed: %s\n",snd_strerror(rval));
347 static int NTO_OpenAudio(_THIS, SDL_AudioSpec* spec)
359 SDL_FreeAudioMem(pcm_buf);
363 /* initialize channel transfer parameters to default */
364 NTO_InitAudioParams(&cparams);
366 /* Open the audio device */
367 rval = snd_pcm_open_preferred(&audio_handle, &cardno, &deviceno, OPEN_FLAGS);
370 SDL_SetError("NTO_OpenAudio(): snd_pcm_open failed: %s\n", snd_strerror(rval));
374 if (!NTO_CheckBuggyCards(this, QSA_MMAP_WORKAROUND))
376 /* enable count status parameter */
377 if ((rval = snd_pcm_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP)) < 0)
379 SDL_SetError("snd_pcm_plugin_set_disable failed: %s\n", snd_strerror(rval));
384 /* Try for a closest match on audio format */
386 /* can't use format as SND_PCM_SFMT_U8 = 0 in nto */
389 for (test_format=SDL_FirstAudioFormat(spec->format); !found ;)
391 /* if match found set format to equivalent ALSA format */
395 format = SND_PCM_SFMT_U8;
399 format = SND_PCM_SFMT_S8;
403 format = SND_PCM_SFMT_S16_LE;
407 format = SND_PCM_SFMT_S16_BE;
411 format = SND_PCM_SFMT_U16_LE;
415 format = SND_PCM_SFMT_U16_BE;
424 test_format = SDL_NextAudioFormat();
428 /* assumes test_format not 0 on success */
429 if (test_format == 0)
431 SDL_SetError("NTO_OpenAudio(): Couldn't find any hardware audio formats");
435 spec->format = test_format;
437 /* Set the audio format */
438 cparams.format.format = format;
440 /* Set mono or stereo audio (currently only two channels supported) */
441 cparams.format.voices = spec->channels;
444 cparams.format.rate = spec->freq;
446 /* Setup the transfer parameters according to cparams */
447 rval = snd_pcm_plugin_params(audio_handle, &cparams);
450 SDL_SetError("NTO_OpenAudio(): snd_pcm_channel_params failed: %s\n", snd_strerror(rval));
454 /* Make sure channel is setup right one last time */
455 SDL_memset(&csetup, 0x00, sizeof(csetup));
456 csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
457 if (snd_pcm_plugin_setup(audio_handle, &csetup) < 0)
459 SDL_SetError("NTO_OpenAudio(): Unable to setup playback channel\n");
464 /* Calculate the final parameters for this audio specification */
465 SDL_CalculateAudioSpec(spec);
467 pcm_len = spec->size;
471 pcm_len = csetup.buf.block.frag_size * spec->channels * (snd_pcm_format_width(format)/8);
474 /* Allocate memory to the audio buffer and initialize with silence (Note that
475 buffer size must be a multiple of fragment size, so find closest multiple)
477 pcm_buf = (Uint8*)SDL_AllocAudioMem(pcm_len);
480 SDL_SetError("NTO_OpenAudio(): pcm buffer allocation failed\n");
483 SDL_memset(pcm_buf, spec->silence, pcm_len);
485 /* get the file descriptor */
486 if ((audio_fd = snd_pcm_file_descriptor(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0)
488 SDL_SetError("NTO_OpenAudio(): snd_pcm_file_descriptor failed with error code: %s\n", snd_strerror(rval));
492 /* Trigger audio playback */
493 rval = snd_pcm_plugin_prepare(audio_handle, SND_PCM_CHANNEL_PLAYBACK);
496 SDL_SetError("snd_pcm_plugin_prepare failed: %s\n", snd_strerror(rval));
502 /* Get the parent process id (we're the parent of the audio thread) */
505 /* We're really ready to rock and roll. :-) */