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 Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 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    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Allow access to a raw mixing buffer */
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_timer.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_audio.h"
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_audio_c.h"
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_dart.h"
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// Buffer states:
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BUFFER_EMPTY       0
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BUFFER_USED        1
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct _tMixBufferDesc {
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int              iBufferUsage;      // BUFFER_EMPTY or BUFFER_USED
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_AudioDevice *pSDLAudioDevice;
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} tMixBufferDesc, *pMixBufferDesc;
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//---------------------------------------------------------------------
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// DARTEventFunc
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// This function is called by DART, when an event occures, like end of
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall// playback of a buffer, etc...
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall//---------------------------------------------------------------------
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallLONG APIENTRY DARTEventFunc(ULONG ulStatus,
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    PMCI_MIX_BUFFER pBuffer,
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			    ULONG ulFlags)
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ulFlags && MIX_WRITE_COMPLETE)
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  { // Playback of buffer completed!
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Get pointer to buffer description
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pMixBufferDesc pBufDesc;
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (pBuffer)
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pBufDesc = (pMixBufferDesc) (*pBuffer).ulUserParm;
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pBufDesc)
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_AudioDevice *pSDLAudioDevice = pBufDesc->pSDLAudioDevice;
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Set the buffer to be empty
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        pBufDesc->iBufferUsage = BUFFER_EMPTY;
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // And notify DART feeder thread that it will have to work a bit.
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (pSDLAudioDevice)
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        DosPostEventSem(pSDLAudioDevice->hidden->hevAudioBufferPlayed);
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return TRUE;
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DART_OpenAudio(_THIS, SDL_AudioSpec *spec)
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  Uint16 test_format = SDL_FirstAudioFormat(spec->format);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int valid_datatype = 0;
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  MCI_AMP_OPEN_PARMS AmpOpenParms;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  MCI_GENERIC_PARMS GenericParms;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int iDeviceOrd = 0; // Default device to be used
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int bOpenShared = 1; // Try opening it shared
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int iBits = 16; // Default is 16 bits signed
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int iFreq = 44100; // Default is 44KHz
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int iChannels = 2; // Default is 2 channels (Stereo)
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int iNumBufs = 2;  // Number of audio buffers: 2
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int iBufSize;
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int iOpenMode;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int iSilence;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int rc;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // First thing is to try to open a given DART device!
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(&AmpOpenParms, 0, sizeof(MCI_AMP_OPEN_PARMS));
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // pszDeviceType should contain the device type in low word, and device ordinal in high word!
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  AmpOpenParms.pszDeviceType = (PSZ) (MCI_DEVTYPE_AUDIO_AMPMIX | (iDeviceOrd << 16));
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iOpenMode = MCI_WAIT | MCI_OPEN_TYPE_ID;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (bOpenShared) iOpenMode |= MCI_OPEN_SHAREABLE;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rc = mciSendCommand( 0, MCI_OPEN,
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                       iOpenMode,
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       (PVOID) &AmpOpenParms, 0);
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (rc!=MCIERR_SUCCESS) // No audio available??
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (-1);
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Save the device ID we got from DART!
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // We will use this in the next calls!
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iDeviceOrd = AmpOpenParms.usDeviceID;
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Determine the audio parameters from the AudioSpec
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (spec->channels > 2)
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    spec->channels = 2;  // !!! FIXME: more than stereo support in OS/2?
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  while ((!valid_datatype) && (test_format)) {
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    spec->format = test_format;
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    valid_datatype = 1;
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch (test_format) {
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case AUDIO_U8:
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Unsigned 8 bit audio data
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        iSilence = 0x80;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        iBits = 8;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case AUDIO_S16LSB:
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Signed 16 bit audio data
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        iSilence = 0x00;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        iBits = 16;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      default:
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        valid_datatype = 0;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        test_format = SDL_NextAudioFormat();
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        break;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!valid_datatype) { // shouldn't happen, but just in case...
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Close DART, and exit with error code!
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Unsupported audio format");
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return (-1);
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iFreq = spec->freq;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iChannels = spec->channels;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Update the fragment size as size in bytes */
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_CalculateAudioSpec(spec);
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iBufSize = spec->size;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now query this device if it supports the given freq/bits/channels!
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(&(_this->hidden->MixSetupParms), 0, sizeof(MCI_MIXSETUP_PARMS));
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->MixSetupParms.ulBitsPerSample = iBits;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->MixSetupParms.ulFormatTag = MCI_WAVE_FORMAT_PCM;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->MixSetupParms.ulSamplesPerSec = iFreq;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->MixSetupParms.ulChannels = iChannels;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->MixSetupParms.ulFormatMode = MCI_PLAY;
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->MixSetupParms.ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->MixSetupParms.pmixEvent = DARTEventFunc;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rc = mciSendCommand (iDeviceOrd, MCI_MIXSETUP,
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                       MCI_WAIT | MCI_MIXSETUP_QUERYMODE,
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                       &(_this->hidden->MixSetupParms), 0);
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (rc!=MCIERR_SUCCESS)
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  { // The device cannot handle this format!
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Close DART, and exit with error code!
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Audio device doesn't support requested audio format");
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(-1);
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // The device can handle this format, so initialize!
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rc = mciSendCommand(iDeviceOrd, MCI_MIXSETUP,
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      MCI_WAIT | MCI_MIXSETUP_INIT,
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      &(_this->hidden->MixSetupParms), 0);
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (rc!=MCIERR_SUCCESS)
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  { // The device could not be opened!
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Close DART, and exit with error code!
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Audio device could not be set up");
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(-1);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Ok, the device is initialized.
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now we should allocate buffers. For this, we need a place where
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // the buffer descriptors will be:
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->pMixBuffers = (MCI_MIX_BUFFER *) SDL_malloc(sizeof(MCI_MIX_BUFFER)*iNumBufs);
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!(_this->hidden->pMixBuffers))
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  { // Not enough memory!
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Close DART, and exit with error code!
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Not enough memory for audio buffer descriptors");
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(-1);
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Now that we have the place for buffer list, we can ask DART for the
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // buffers!
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->BufferParms.ulNumBuffers = iNumBufs;               // Number of buffers
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->BufferParms.ulBufferSize = iBufSize;               // each with this size
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->BufferParms.pBufList = _this->hidden->pMixBuffers; // getting descriptorts into this list
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Allocate buffers!
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rc = mciSendCommand(iDeviceOrd, MCI_BUFFER,
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      MCI_WAIT | MCI_ALLOCATE_MEMORY,
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      &(_this->hidden->BufferParms), 0);
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ((rc!=MCIERR_SUCCESS) || (iNumBufs != _this->hidden->BufferParms.ulNumBuffers) || (_this->hidden->BufferParms.ulBufferSize==0))
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  { // Could not allocate memory!
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Close DART, and exit with error code!
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("DART could not allocate buffers");
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(-1);
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Ok, we have all the buffers allocated, let's mark them!
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i=0; i<iNumBufs; i++)
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pMixBufferDesc pBufferDesc = (pMixBufferDesc) SDL_malloc(sizeof(tMixBufferDesc));;
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      // Check if this buffer was really allocated by DART
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if ((!(_this->hidden->pMixBuffers[i].pBuffer)) || (!pBufferDesc))
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      { // Wrong buffer!
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Close DART, and exit with error code!
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // Free buffer descriptions
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        { int j;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          for (j=0; j<i; j++) SDL_free((void *)(_this->hidden->pMixBuffers[j].ulUserParm));
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        // and cleanup
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        mciSendCommand(iDeviceOrd, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY, &(_this->hidden->BufferParms), 0);
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL;
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        SDL_SetError("Error at internal buffer check");
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        return(-1);
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pBufferDesc->iBufferUsage = BUFFER_EMPTY;
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pBufferDesc->pSDLAudioDevice = _this;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      _this->hidden->pMixBuffers[i].ulBufferLength = _this->hidden->BufferParms.ulBufferSize;
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      _this->hidden->pMixBuffers[i].ulUserParm = (ULONG) pBufferDesc; // User parameter: Description of buffer
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      _this->hidden->pMixBuffers[i].ulFlags = 0; // Some stuff should be flagged here for DART, like end of
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                            // audio data, but as we will continously send
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                            // audio data, there will be no end.:)
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_memset(_this->hidden->pMixBuffers[i].pBuffer, iSilence, iBufSize);
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iNextFreeBuffer = 0;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iLastPlayedBuf = -1;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Create event semaphore
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (DosCreateEventSem(NULL, &(_this->hidden->hevAudioBufferPlayed), 0, FALSE)!=NO_ERROR)
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    // Could not create event semaphore!
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      int i;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      for (i=0; i<iNumBufs; i++) SDL_free((void *)(_this->hidden->pMixBuffers[i].ulUserParm));
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    mciSendCommand(iDeviceOrd, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY, &(_this->hidden->BufferParms), 0);
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL;
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Could not create event semaphore");
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(-1);
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Store the new settings in global variables
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iCurrDeviceOrd = iDeviceOrd;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iCurrFreq = iFreq;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iCurrBits = iBits;
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iCurrChannels = iChannels;
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iCurrNumBufs = iNumBufs;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iCurrBufSize = iBufSize;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return (0);
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DART_ThreadInit(_THIS)
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return;
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This function waits until it is possible to write a full sound buffer */
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DART_WaitAudio(_THIS)
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int i;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pMixBufferDesc pBufDesc;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ULONG ulPostCount;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DosResetEventSem(_this->hidden->hevAudioBufferPlayed, &ulPostCount);
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If there is already an empty buffer, then return now!
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (i=0; i<_this->hidden->iCurrNumBufs; i++)
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[i].ulUserParm;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (pBufDesc->iBufferUsage == BUFFER_EMPTY)
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // If there is no empty buffer, wait for one to be empty!
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DosWaitEventSem(_this->hidden->hevAudioBufferPlayed, 1000); // Wait max 1 sec!!! Important!
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DART_PlayAudio(_THIS)
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int iFreeBuf = _this->hidden->iNextFreeBuffer;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pMixBufferDesc pBufDesc;
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[iFreeBuf].ulUserParm;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pBufDesc->iBufferUsage = BUFFER_USED;
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Send it to DART to be queued
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->MixSetupParms.pmixWrite(_this->hidden->MixSetupParms.ulMixHandle,
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        &(_this->hidden->pMixBuffers[iFreeBuf]), 1);
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iLastPlayedBuf = iFreeBuf;
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  iFreeBuf = (iFreeBuf+1) % _this->hidden->iCurrNumBufs;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  _this->hidden->iNextFreeBuffer = iFreeBuf;
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallUint8 *DART_GetAudioBuf(_THIS)
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int iFreeBuf;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  Uint8 *pResult;
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pMixBufferDesc pBufDesc;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (_this)
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (_this->hidden)
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      iFreeBuf = _this->hidden->iNextFreeBuffer;
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[iFreeBuf].ulUserParm;
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (pBufDesc)
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      {
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (pBufDesc->iBufferUsage == BUFFER_EMPTY)
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          pResult = _this->hidden->pMixBuffers[iFreeBuf].pBuffer;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          return pResult;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      } else
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        printf("[DART_GetAudioBuf] : ERROR! pBufDesc = %p\n", pBufDesc);
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("[DART_GetAudioBuf] : ERROR! _this->hidden = %p\n", _this->hidden);
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("[DART_GetAudioBuf] : ERROR! _this = %p\n", _this);
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return NULL;
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DART_WaitDone(_THIS)
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pMixBufferDesc pBufDesc;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ULONG ulPostCount;
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  APIRET rc;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[_this->hidden->iLastPlayedBuf].ulUserParm;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rc = NO_ERROR;
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  while ((pBufDesc->iBufferUsage != BUFFER_EMPTY) && (rc==NO_ERROR))
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DosResetEventSem(_this->hidden->hevAudioBufferPlayed, &ulPostCount);
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    rc = DosWaitEventSem(_this->hidden->hevAudioBufferPlayed, 1000); // 1 sec timeout! Important!
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DART_CloseAudio(_THIS)
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  MCI_GENERIC_PARMS GenericParms;
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int rc;
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Stop DART playback
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rc = mciSendCommand(_this->hidden->iCurrDeviceOrd, MCI_STOP, MCI_WAIT, &GenericParms, 0);
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (rc!=MCIERR_SUCCESS)
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SFX_DEBUG_BUILD
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("Could not stop DART playback!\n");
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    fflush(stdout);
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Close event semaphore
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DosCloseEventSem(_this->hidden->hevAudioBufferPlayed);
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Free memory of buffer descriptions
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int i;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (i=0; i<_this->hidden->iCurrNumBufs; i++) SDL_free((void *)(_this->hidden->pMixBuffers[i].ulUserParm));
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Deallocate buffers
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rc = mciSendCommand(_this->hidden->iCurrDeviceOrd, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY, &(_this->hidden->BufferParms), 0);
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Free bufferlist
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL;
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  // Close dart
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rc = mciSendCommand(_this->hidden->iCurrDeviceOrd, MCI_CLOSE, MCI_WAIT, &(GenericParms), 0);
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Audio driver bootstrap functions */
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint Audio_Available(void)
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return(1);
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid Audio_DeleteDevice(SDL_AudioDevice *device)
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_free(device->hidden);
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_free(device);
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_AudioDevice *Audio_CreateDevice(int devindex)
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_AudioDevice *this;
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Initialize all variables that we clean on shutdown */
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ( this )
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_memset(this, 0, (sizeof *this));
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->hidden = (struct SDL_PrivateAudioData *)
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_malloc((sizeof *this->hidden));
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ( (this == NULL) || (this->hidden == NULL) )
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  {
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_OutOfMemory();
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if ( this )
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_free(this);
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(0);
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(this->hidden, 0, (sizeof *this->hidden));
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Set the function pointers */
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->OpenAudio = DART_OpenAudio;
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->ThreadInit = DART_ThreadInit;
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->WaitAudio = DART_WaitAudio;
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->PlayAudio = DART_PlayAudio;
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->GetAudioBuf = DART_GetAudioBuf;
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->WaitDone = DART_WaitDone;
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->CloseAudio = DART_CloseAudio;
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->free = Audio_DeleteDevice;
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return this;
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallAudioBootStrap DART_bootstrap = {
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	"dart", "OS/2 Direct Audio RouTines (DART)",
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Audio_Available, Audio_CreateDevice
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
442