19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <signal.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/types.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/time.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sched.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/select.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/neutrino.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/asoundlib.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audiomem.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_nto_audio.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The tag name used by NTO audio */
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DRIVER_NAME "qsa-nto"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* default channel communication parameters */
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_CPARAMS_RATE 22050
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_CPARAMS_VOICES 1
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* FIXME: need to add in the near future flexible logic with frag_size and frags count */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_CPARAMS_FRAG_SIZE 4096
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_CPARAMS_FRAGS_MIN 1
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFAULT_CPARAMS_FRAGS_MAX 1
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Open the audio device for playback, and don't block if busy */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define OPEN_FLAGS SND_PCM_OPEN_PLAYBACK
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define QSA_NO_WORKAROUNDS  0x00000000
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define QSA_MMAP_WORKAROUND 0x00000001
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct BuggyCards
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   char* cardname;
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   unsigned long bugtype;
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define QSA_WA_CARDS 3
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct BuggyCards buggycards[QSA_WA_CARDS]=
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   {"Sound Blaster Live!", QSA_MMAP_WORKAROUND},
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   {"Vortex 8820",         QSA_MMAP_WORKAROUND},
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   {"Vortex 8830",         QSA_MMAP_WORKAROUND},
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver functions */
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NTO_ThreadInit(_THIS);
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NTO_OpenAudio(_THIS, SDL_AudioSpec* spec);
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NTO_WaitAudio(_THIS);
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NTO_PlayAudio(_THIS);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8* NTO_GetAudioBuf(_THIS);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NTO_CloseAudio(_THIS);
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* card names check to apply the workarounds */
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NTO_CheckBuggyCards(_THIS, unsigned long checkfor)
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    char scardname[33];
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int it;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (snd_card_get_name(cardno, scardname, 32)<0)
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return 0;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (it=0; it<QSA_WA_CARDS; it++)
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       if (SDL_strcmp(buggycards[it].cardname, scardname)==0)
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       {
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (buggycards[it].bugtype==checkfor)
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          {
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              return 1;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          }
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       }
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NTO_ThreadInit(_THIS)
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   int status;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   struct sched_param param;
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   /* increasing default 10 priority to 25 to avoid jerky sound */
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   status=SchedGet(0, 0, &param);
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   param.sched_priority=param.sched_curpriority+15;
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   status=SchedSet(0, 0, SCHED_NOCHANGE, &param);
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* PCM transfer channel parameters initialize function */
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NTO_InitAudioParams(snd_pcm_channel_params_t* cpars)
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(cpars, 0, sizeof(snd_pcm_channel_params_t));
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->channel = SND_PCM_CHANNEL_PLAYBACK;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->mode = SND_PCM_MODE_BLOCK;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->start_mode = SND_PCM_START_DATA;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->stop_mode  = SND_PCM_STOP_STOP;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->format.format = SND_PCM_SFMT_S16_LE;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->format.interleave = 1;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->format.rate = DEFAULT_CPARAMS_RATE;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->format.voices = DEFAULT_CPARAMS_VOICES;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->buf.block.frag_size = DEFAULT_CPARAMS_FRAG_SIZE;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->buf.block.frags_min = DEFAULT_CPARAMS_FRAGS_MIN;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cpars->buf.block.frags_max = DEFAULT_CPARAMS_FRAGS_MAX;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NTO_AudioAvailable(void)
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /*  See if we can open a nonblocking channel.
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Return value '1' means we can.
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        Return value '0' means we cannot. */
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int available;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int rval;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    snd_pcm_t* handle;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    available = 0;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    handle = NULL;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    rval = snd_pcm_open_preferred(&handle, NULL, NULL, OPEN_FLAGS);
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (rval >= 0)
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        available = 1;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ((rval = snd_pcm_close(handle)) < 0)
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_SetError("NTO_AudioAvailable(): snd_pcm_close failed: %s\n", snd_strerror(rval));
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            available = 0;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("NTO_AudioAvailable(): there are no available audio devices.\n");
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (available);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NTO_DeleteAudioDevice(SDL_AudioDevice *device)
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((device)&&(device->hidden))
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_free(device->hidden);
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (device)
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_free(device);
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_AudioDevice* NTO_CreateAudioDevice(int devindex)
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_AudioDevice *this;
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Initialize all variables that we clean on shutdown */
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (this)
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_memset(this, 0, sizeof(SDL_AudioDevice));
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->hidden = (struct SDL_PrivateAudioData *)SDL_malloc(sizeof(struct SDL_PrivateAudioData));
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((this == NULL) || (this->hidden == NULL))
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_OutOfMemory();
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (this)
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_free(this);
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return (0);
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(this->hidden, 0, sizeof(struct SDL_PrivateAudioData));
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    audio_handle = NULL;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set the function pointers */
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->ThreadInit = NTO_ThreadInit;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->OpenAudio = NTO_OpenAudio;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->WaitAudio = NTO_WaitAudio;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->PlayAudio = NTO_PlayAudio;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->GetAudioBuf = NTO_GetAudioBuf;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->CloseAudio = NTO_CloseAudio;
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->free = NTO_DeleteAudioDevice;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return this;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap QNXNTOAUDIO_bootstrap =
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DRIVER_NAME, "QNX6 QSA-NTO Audio",
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NTO_AudioAvailable,
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NTO_CreateAudioDevice
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function waits until it is possible to write a full sound buffer */
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NTO_WaitAudio(_THIS)
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fd_set wfds;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int selectret;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FD_ZERO(&wfds);
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    FD_SET(audio_fd, &wfds);
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    do {
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        selectret=select(audio_fd + 1, NULL, &wfds, NULL, NULL);
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        switch (selectret)
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case -1:
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case  0: SDL_SetError("NTO_WaitAudio(): select() failed: %s\n", strerror(errno));
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     return;
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            default: if (FD_ISSET(audio_fd, &wfds))
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     {
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                         return;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     }
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     break;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } while(1);
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NTO_PlayAudio(_THIS)
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int written, rval;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int towrite;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    void* pcmbuffer;
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!this->enabled)
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    towrite = this->spec.size;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pcmbuffer = pcm_buf;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Write the audio data, checking for EAGAIN (buffer full) and underrun */
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    do {
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        written = snd_pcm_plugin_write(audio_handle, pcm_buf, towrite);
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (written != towrite)
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                /* Let a little CPU time go by and try to write again */
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                SDL_Delay(1);
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                /* if we wrote some data */
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                towrite -= written;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                pcmbuffer += written * this->spec.channels;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                continue;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            else
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                if ((errno == EINVAL) || (errno == EIO))
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    SDL_memset(&cstatus, 0, sizeof(cstatus));
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    cstatus.channel = SND_PCM_CHANNEL_PLAYBACK;
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    if ((rval = snd_pcm_plugin_status(audio_handle, &cstatus)) < 0)
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    {
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        SDL_SetError("NTO_PlayAudio(): snd_pcm_plugin_status failed: %s\n", snd_strerror(rval));
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        return;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    }
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    if ((cstatus.status == SND_PCM_STATUS_UNDERRUN) || (cstatus.status == SND_PCM_STATUS_READY))
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    {
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        if ((rval = snd_pcm_plugin_prepare(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0)
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        {
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            SDL_SetError("NTO_PlayAudio(): snd_pcm_plugin_prepare failed: %s\n", snd_strerror(rval));
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                            return;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        }
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    }
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    continue;
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                else
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                    return;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        else
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            /* we wrote all remaining data */
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            towrite -= written;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            pcmbuffer += written * this->spec.channels;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } while ((towrite > 0)  && (this->enabled));
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* If we couldn't write, assume fatal error for now */
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (towrite != 0)
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        this->enabled = 0;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return;
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic Uint8* NTO_GetAudioBuf(_THIS)
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return pcm_buf;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void NTO_CloseAudio(_THIS)
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int rval;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->enabled = 0;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (audio_handle != NULL)
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ((rval = snd_pcm_plugin_flush(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0)
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_SetError("NTO_CloseAudio(): snd_pcm_plugin_flush failed: %s\n", snd_strerror(rval));
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return;
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ((rval = snd_pcm_close(audio_handle)) < 0)
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_SetError("NTO_CloseAudio(): snd_pcm_close failed: %s\n",snd_strerror(rval));
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return;
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        audio_handle = NULL;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int NTO_OpenAudio(_THIS, SDL_AudioSpec* spec)
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int rval;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int format;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint16 test_format;
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int found;
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    audio_handle = NULL;
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->enabled = 0;
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (pcm_buf != NULL)
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_FreeAudioMem(pcm_buf);
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pcm_buf = NULL;
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* initialize channel transfer parameters to default */
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    NTO_InitAudioParams(&cparams);
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Open the audio device */
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    rval = snd_pcm_open_preferred(&audio_handle, &cardno, &deviceno, OPEN_FLAGS);
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (rval < 0)
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("NTO_OpenAudio(): snd_pcm_open failed: %s\n", snd_strerror(rval));
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return (-1);
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (!NTO_CheckBuggyCards(this, QSA_MMAP_WORKAROUND))
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* enable count status parameter */
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if ((rval = snd_pcm_plugin_set_disable(audio_handle, PLUGIN_DISABLE_MMAP)) < 0)
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            SDL_SetError("snd_pcm_plugin_set_disable failed: %s\n", snd_strerror(rval));
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return (-1);
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Try for a closest match on audio format */
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    format = 0;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* can't use format as SND_PCM_SFMT_U8 = 0 in nto */
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    found = 0;
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (test_format=SDL_FirstAudioFormat(spec->format); !found ;)
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        /* if match found set format to equivalent ALSA format */
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        switch (test_format)
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case AUDIO_U8:
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           format = SND_PCM_SFMT_U8;
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           found = 1;
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           break;
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case AUDIO_S8:
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           format = SND_PCM_SFMT_S8;
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           found = 1;
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           break;
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case AUDIO_S16LSB:
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           format = SND_PCM_SFMT_S16_LE;
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           found = 1;
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           break;
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case AUDIO_S16MSB:
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           format = SND_PCM_SFMT_S16_BE;
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           found = 1;
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           break;
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case AUDIO_U16LSB:
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           format = SND_PCM_SFMT_U16_LE;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           found = 1;
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           break;
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            case AUDIO_U16MSB:
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           format = SND_PCM_SFMT_U16_BE;
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           found = 1;
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           break;
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            default:
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                           break;
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (!found)
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            test_format = SDL_NextAudioFormat();
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* assumes test_format not 0 on success */
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (test_format == 0)
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("NTO_OpenAudio(): Couldn't find any hardware audio formats");
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return (-1);
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    spec->format = test_format;
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set the audio format */
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cparams.format.format = format;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set mono or stereo audio (currently only two channels supported) */
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cparams.format.voices = spec->channels;
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Set rate */
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    cparams.format.rate = spec->freq;
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Setup the transfer parameters according to cparams */
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    rval = snd_pcm_plugin_params(audio_handle, &cparams);
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (rval < 0)
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("NTO_OpenAudio(): snd_pcm_channel_params failed: %s\n", snd_strerror(rval));
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return (-1);
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Make sure channel is setup right one last time */
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(&csetup, 0x00, sizeof(csetup));
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    csetup.channel = SND_PCM_CHANNEL_PLAYBACK;
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (snd_pcm_plugin_setup(audio_handle, &csetup) < 0)
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("NTO_OpenAudio(): Unable to setup playback channel\n");
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return -1;
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Calculate the final parameters for this audio specification */
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_CalculateAudioSpec(spec);
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pcm_len = spec->size;
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (pcm_len==0)
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pcm_len = csetup.buf.block.frag_size * spec->channels * (snd_pcm_format_width(format)/8);
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Allocate memory to the audio buffer and initialize with silence (Note that
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       buffer size must be a multiple of fragment size, so find closest multiple)
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    */
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pcm_buf = (Uint8*)SDL_AllocAudioMem(pcm_len);
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (pcm_buf == NULL)
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("NTO_OpenAudio(): pcm buffer allocation failed\n");
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return (-1);
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(pcm_buf, spec->silence, pcm_len);
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* get the file descriptor */
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ((audio_fd = snd_pcm_file_descriptor(audio_handle, SND_PCM_CHANNEL_PLAYBACK)) < 0)
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("NTO_OpenAudio(): snd_pcm_file_descriptor failed with error code: %s\n", snd_strerror(rval));
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return (-1);
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Trigger audio playback */
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    rval = snd_pcm_plugin_prepare(audio_handle, SND_PCM_CHANNEL_PLAYBACK);
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (rval < 0)
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("snd_pcm_plugin_prepare failed: %s\n", snd_strerror(rval));
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return (-1);
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->enabled = 1;
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* Get the parent process id (we're the parent of the audio thread) */
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    parent = getpid();
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* We're really ready to rock and roll. :-) */
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (0);
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
508