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 #include "SDL_config.h"
24 /* Allow access to a raw mixing buffer */
26 #include "SDL_timer.h"
27 #include "SDL_audio.h"
28 #include "../SDL_audio_c.h"
32 #define BUFFER_EMPTY 0
35 typedef struct _tMixBufferDesc {
36 int iBufferUsage; // BUFFER_EMPTY or BUFFER_USED
37 SDL_AudioDevice *pSDLAudioDevice;
38 } tMixBufferDesc, *pMixBufferDesc;
41 //---------------------------------------------------------------------
44 // This function is called by DART, when an event occures, like end of
45 // playback of a buffer, etc...
46 //---------------------------------------------------------------------
47 LONG APIENTRY DARTEventFunc(ULONG ulStatus,
48 PMCI_MIX_BUFFER pBuffer,
51 if (ulFlags && MIX_WRITE_COMPLETE)
52 { // Playback of buffer completed!
54 // Get pointer to buffer description
55 pMixBufferDesc pBufDesc;
59 pBufDesc = (pMixBufferDesc) (*pBuffer).ulUserParm;
63 SDL_AudioDevice *pSDLAudioDevice = pBufDesc->pSDLAudioDevice;
64 // Set the buffer to be empty
65 pBufDesc->iBufferUsage = BUFFER_EMPTY;
66 // And notify DART feeder thread that it will have to work a bit.
68 DosPostEventSem(pSDLAudioDevice->hidden->hevAudioBufferPlayed);
76 int DART_OpenAudio(_THIS, SDL_AudioSpec *spec)
78 Uint16 test_format = SDL_FirstAudioFormat(spec->format);
79 int valid_datatype = 0;
80 MCI_AMP_OPEN_PARMS AmpOpenParms;
81 MCI_GENERIC_PARMS GenericParms;
82 int iDeviceOrd = 0; // Default device to be used
83 int bOpenShared = 1; // Try opening it shared
84 int iBits = 16; // Default is 16 bits signed
85 int iFreq = 44100; // Default is 44KHz
86 int iChannels = 2; // Default is 2 channels (Stereo)
87 int iNumBufs = 2; // Number of audio buffers: 2
93 // First thing is to try to open a given DART device!
94 SDL_memset(&AmpOpenParms, 0, sizeof(MCI_AMP_OPEN_PARMS));
95 // pszDeviceType should contain the device type in low word, and device ordinal in high word!
96 AmpOpenParms.pszDeviceType = (PSZ) (MCI_DEVTYPE_AUDIO_AMPMIX | (iDeviceOrd << 16));
98 iOpenMode = MCI_WAIT | MCI_OPEN_TYPE_ID;
99 if (bOpenShared) iOpenMode |= MCI_OPEN_SHAREABLE;
101 rc = mciSendCommand( 0, MCI_OPEN,
103 (PVOID) &AmpOpenParms, 0);
104 if (rc!=MCIERR_SUCCESS) // No audio available??
106 // Save the device ID we got from DART!
107 // We will use this in the next calls!
108 iDeviceOrd = AmpOpenParms.usDeviceID;
110 // Determine the audio parameters from the AudioSpec
111 if (spec->channels > 2)
112 spec->channels = 2; // !!! FIXME: more than stereo support in OS/2?
114 while ((!valid_datatype) && (test_format)) {
115 spec->format = test_format;
117 switch (test_format) {
119 // Unsigned 8 bit audio data
125 // Signed 16 bit audio data
132 test_format = SDL_NextAudioFormat();
137 if (!valid_datatype) { // shouldn't happen, but just in case...
138 // Close DART, and exit with error code!
139 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
140 SDL_SetError("Unsupported audio format");
145 iChannels = spec->channels;
146 /* Update the fragment size as size in bytes */
147 SDL_CalculateAudioSpec(spec);
148 iBufSize = spec->size;
150 // Now query this device if it supports the given freq/bits/channels!
151 SDL_memset(&(_this->hidden->MixSetupParms), 0, sizeof(MCI_MIXSETUP_PARMS));
152 _this->hidden->MixSetupParms.ulBitsPerSample = iBits;
153 _this->hidden->MixSetupParms.ulFormatTag = MCI_WAVE_FORMAT_PCM;
154 _this->hidden->MixSetupParms.ulSamplesPerSec = iFreq;
155 _this->hidden->MixSetupParms.ulChannels = iChannels;
156 _this->hidden->MixSetupParms.ulFormatMode = MCI_PLAY;
157 _this->hidden->MixSetupParms.ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
158 _this->hidden->MixSetupParms.pmixEvent = DARTEventFunc;
159 rc = mciSendCommand (iDeviceOrd, MCI_MIXSETUP,
160 MCI_WAIT | MCI_MIXSETUP_QUERYMODE,
161 &(_this->hidden->MixSetupParms), 0);
162 if (rc!=MCIERR_SUCCESS)
163 { // The device cannot handle this format!
164 // Close DART, and exit with error code!
165 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
166 SDL_SetError("Audio device doesn't support requested audio format");
169 // The device can handle this format, so initialize!
170 rc = mciSendCommand(iDeviceOrd, MCI_MIXSETUP,
171 MCI_WAIT | MCI_MIXSETUP_INIT,
172 &(_this->hidden->MixSetupParms), 0);
173 if (rc!=MCIERR_SUCCESS)
174 { // The device could not be opened!
175 // Close DART, and exit with error code!
176 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
177 SDL_SetError("Audio device could not be set up");
180 // Ok, the device is initialized.
181 // Now we should allocate buffers. For this, we need a place where
182 // the buffer descriptors will be:
183 _this->hidden->pMixBuffers = (MCI_MIX_BUFFER *) SDL_malloc(sizeof(MCI_MIX_BUFFER)*iNumBufs);
184 if (!(_this->hidden->pMixBuffers))
185 { // Not enough memory!
186 // Close DART, and exit with error code!
187 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
188 SDL_SetError("Not enough memory for audio buffer descriptors");
191 // Now that we have the place for buffer list, we can ask DART for the
193 _this->hidden->BufferParms.ulNumBuffers = iNumBufs; // Number of buffers
194 _this->hidden->BufferParms.ulBufferSize = iBufSize; // each with this size
195 _this->hidden->BufferParms.pBufList = _this->hidden->pMixBuffers; // getting descriptorts into this list
197 rc = mciSendCommand(iDeviceOrd, MCI_BUFFER,
198 MCI_WAIT | MCI_ALLOCATE_MEMORY,
199 &(_this->hidden->BufferParms), 0);
200 if ((rc!=MCIERR_SUCCESS) || (iNumBufs != _this->hidden->BufferParms.ulNumBuffers) || (_this->hidden->BufferParms.ulBufferSize==0))
201 { // Could not allocate memory!
202 // Close DART, and exit with error code!
203 SDL_free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL;
204 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
205 SDL_SetError("DART could not allocate buffers");
208 // Ok, we have all the buffers allocated, let's mark them!
211 for (i=0; i<iNumBufs; i++)
213 pMixBufferDesc pBufferDesc = (pMixBufferDesc) SDL_malloc(sizeof(tMixBufferDesc));;
214 // Check if this buffer was really allocated by DART
215 if ((!(_this->hidden->pMixBuffers[i].pBuffer)) || (!pBufferDesc))
217 // Close DART, and exit with error code!
218 // Free buffer descriptions
220 for (j=0; j<i; j++) SDL_free((void *)(_this->hidden->pMixBuffers[j].ulUserParm));
223 mciSendCommand(iDeviceOrd, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY, &(_this->hidden->BufferParms), 0);
224 SDL_free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL;
225 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
226 SDL_SetError("Error at internal buffer check");
229 pBufferDesc->iBufferUsage = BUFFER_EMPTY;
230 pBufferDesc->pSDLAudioDevice = _this;
232 _this->hidden->pMixBuffers[i].ulBufferLength = _this->hidden->BufferParms.ulBufferSize;
233 _this->hidden->pMixBuffers[i].ulUserParm = (ULONG) pBufferDesc; // User parameter: Description of buffer
234 _this->hidden->pMixBuffers[i].ulFlags = 0; // Some stuff should be flagged here for DART, like end of
235 // audio data, but as we will continously send
236 // audio data, there will be no end.:)
237 SDL_memset(_this->hidden->pMixBuffers[i].pBuffer, iSilence, iBufSize);
240 _this->hidden->iNextFreeBuffer = 0;
241 _this->hidden->iLastPlayedBuf = -1;
242 // Create event semaphore
243 if (DosCreateEventSem(NULL, &(_this->hidden->hevAudioBufferPlayed), 0, FALSE)!=NO_ERROR)
245 // Could not create event semaphore!
248 for (i=0; i<iNumBufs; i++) SDL_free((void *)(_this->hidden->pMixBuffers[i].ulUserParm));
250 mciSendCommand(iDeviceOrd, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY, &(_this->hidden->BufferParms), 0);
251 SDL_free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL;
252 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0);
253 SDL_SetError("Could not create event semaphore");
257 // Store the new settings in global variables
258 _this->hidden->iCurrDeviceOrd = iDeviceOrd;
259 _this->hidden->iCurrFreq = iFreq;
260 _this->hidden->iCurrBits = iBits;
261 _this->hidden->iCurrChannels = iChannels;
262 _this->hidden->iCurrNumBufs = iNumBufs;
263 _this->hidden->iCurrBufSize = iBufSize;
270 void DART_ThreadInit(_THIS)
275 /* This function waits until it is possible to write a full sound buffer */
276 void DART_WaitAudio(_THIS)
279 pMixBufferDesc pBufDesc;
282 DosResetEventSem(_this->hidden->hevAudioBufferPlayed, &ulPostCount);
283 // If there is already an empty buffer, then return now!
284 for (i=0; i<_this->hidden->iCurrNumBufs; i++)
286 pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[i].ulUserParm;
287 if (pBufDesc->iBufferUsage == BUFFER_EMPTY)
290 // If there is no empty buffer, wait for one to be empty!
291 DosWaitEventSem(_this->hidden->hevAudioBufferPlayed, 1000); // Wait max 1 sec!!! Important!
295 void DART_PlayAudio(_THIS)
297 int iFreeBuf = _this->hidden->iNextFreeBuffer;
298 pMixBufferDesc pBufDesc;
300 pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[iFreeBuf].ulUserParm;
301 pBufDesc->iBufferUsage = BUFFER_USED;
302 // Send it to DART to be queued
303 _this->hidden->MixSetupParms.pmixWrite(_this->hidden->MixSetupParms.ulMixHandle,
304 &(_this->hidden->pMixBuffers[iFreeBuf]), 1);
306 _this->hidden->iLastPlayedBuf = iFreeBuf;
307 iFreeBuf = (iFreeBuf+1) % _this->hidden->iCurrNumBufs;
308 _this->hidden->iNextFreeBuffer = iFreeBuf;
311 Uint8 *DART_GetAudioBuf(_THIS)
315 pMixBufferDesc pBufDesc;
321 iFreeBuf = _this->hidden->iNextFreeBuffer;
322 pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[iFreeBuf].ulUserParm;
326 if (pBufDesc->iBufferUsage == BUFFER_EMPTY)
328 pResult = _this->hidden->pMixBuffers[iFreeBuf].pBuffer;
332 printf("[DART_GetAudioBuf] : ERROR! pBufDesc = %p\n", pBufDesc);
334 printf("[DART_GetAudioBuf] : ERROR! _this->hidden = %p\n", _this->hidden);
336 printf("[DART_GetAudioBuf] : ERROR! _this = %p\n", _this);
340 void DART_WaitDone(_THIS)
342 pMixBufferDesc pBufDesc;
346 pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[_this->hidden->iLastPlayedBuf].ulUserParm;
348 while ((pBufDesc->iBufferUsage != BUFFER_EMPTY) && (rc==NO_ERROR))
350 DosResetEventSem(_this->hidden->hevAudioBufferPlayed, &ulPostCount);
351 rc = DosWaitEventSem(_this->hidden->hevAudioBufferPlayed, 1000); // 1 sec timeout! Important!
355 void DART_CloseAudio(_THIS)
357 MCI_GENERIC_PARMS GenericParms;
360 // Stop DART playback
361 rc = mciSendCommand(_this->hidden->iCurrDeviceOrd, MCI_STOP, MCI_WAIT, &GenericParms, 0);
362 if (rc!=MCIERR_SUCCESS)
364 #ifdef SFX_DEBUG_BUILD
365 printf("Could not stop DART playback!\n");
370 // Close event semaphore
371 DosCloseEventSem(_this->hidden->hevAudioBufferPlayed);
373 // Free memory of buffer descriptions
376 for (i=0; i<_this->hidden->iCurrNumBufs; i++) SDL_free((void *)(_this->hidden->pMixBuffers[i].ulUserParm));
379 // Deallocate buffers
380 rc = mciSendCommand(_this->hidden->iCurrDeviceOrd, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY, &(_this->hidden->BufferParms), 0);
383 SDL_free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL;
386 rc = mciSendCommand(_this->hidden->iCurrDeviceOrd, MCI_CLOSE, MCI_WAIT, &(GenericParms), 0);
389 /* Audio driver bootstrap functions */
391 int Audio_Available(void)
396 void Audio_DeleteDevice(SDL_AudioDevice *device)
398 SDL_free(device->hidden);
402 SDL_AudioDevice *Audio_CreateDevice(int devindex)
404 SDL_AudioDevice *this;
406 /* Initialize all variables that we clean on shutdown */
407 this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
410 SDL_memset(this, 0, (sizeof *this));
411 this->hidden = (struct SDL_PrivateAudioData *)
412 SDL_malloc((sizeof *this->hidden));
414 if ( (this == NULL) || (this->hidden == NULL) )
421 SDL_memset(this->hidden, 0, (sizeof *this->hidden));
423 /* Set the function pointers */
424 this->OpenAudio = DART_OpenAudio;
425 this->ThreadInit = DART_ThreadInit;
426 this->WaitAudio = DART_WaitAudio;
427 this->PlayAudio = DART_PlayAudio;
428 this->GetAudioBuf = DART_GetAudioBuf;
429 this->WaitDone = DART_WaitDone;
430 this->CloseAudio = DART_CloseAudio;
432 this->free = Audio_DeleteDevice;
437 AudioBootStrap DART_bootstrap = {
438 "dart", "OS/2 Direct Audio RouTines (DART)",
439 Audio_Available, Audio_CreateDevice