Mercurial > sdl-ios-xcode
annotate src/audio/dart/SDL_dart.c @ 1318:f95502c6fc72
Eliminate duplicate modes with different refresh rates
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Wed, 01 Feb 2006 09:28:42 +0000 |
parents | c9b51268668f |
children | 3692456e7b0f |
rev | line source |
---|---|
1190 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
1190 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
1190 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
1190 | 9 |
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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
13 Lesser General Public License for more details. |
1190 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
1190 | 18 |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
22 | |
23 /* Allow access to a raw mixing buffer */ | |
24 | |
25 #include <stdio.h> | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 | |
29 #include "SDL_types.h" | |
30 #include "SDL_error.h" | |
31 #include "SDL_timer.h" | |
32 #include "SDL_audio.h" | |
33 #include "SDL_audio_c.h" | |
34 #include "SDL_dart.h" | |
35 | |
36 // Buffer states: | |
37 #define BUFFER_EMPTY 0 | |
38 #define BUFFER_USED 1 | |
39 | |
40 typedef struct _tMixBufferDesc { | |
41 int iBufferUsage; // BUFFER_EMPTY or BUFFER_USED | |
42 SDL_AudioDevice *pSDLAudioDevice; | |
43 } tMixBufferDesc, *pMixBufferDesc; | |
44 | |
45 | |
46 //--------------------------------------------------------------------- | |
47 // DARTEventFunc | |
48 // | |
49 // This function is called by DART, when an event occures, like end of | |
50 // playback of a buffer, etc... | |
51 //--------------------------------------------------------------------- | |
52 LONG APIENTRY DARTEventFunc(ULONG ulStatus, | |
53 PMCI_MIX_BUFFER pBuffer, | |
54 ULONG ulFlags) | |
55 { | |
56 if (ulFlags && MIX_WRITE_COMPLETE) | |
57 { // Playback of buffer completed! | |
58 | |
59 // Get pointer to buffer description | |
60 pMixBufferDesc pBufDesc; | |
61 | |
62 if (pBuffer) | |
63 { | |
64 pBufDesc = (pMixBufferDesc) (*pBuffer).ulUserParm; | |
65 | |
66 if (pBufDesc) | |
67 { | |
68 SDL_AudioDevice *pSDLAudioDevice = pBufDesc->pSDLAudioDevice; | |
69 // Set the buffer to be empty | |
70 pBufDesc->iBufferUsage = BUFFER_EMPTY; | |
71 // And notify DART feeder thread that it will have to work a bit. | |
72 if (pSDLAudioDevice) | |
73 DosPostEventSem(pSDLAudioDevice->hidden->hevAudioBufferPlayed); | |
74 } | |
75 } | |
76 } | |
77 return TRUE; | |
78 } | |
79 | |
80 | |
81 int DART_OpenAudio(_THIS, SDL_AudioSpec *spec) | |
82 { | |
83 MCI_AMP_OPEN_PARMS AmpOpenParms; | |
84 MCI_GENERIC_PARMS GenericParms; | |
85 int iDeviceOrd = 0; // Default device to be used | |
86 int bOpenShared = 1; // Try opening it shared | |
87 int iBits = 16; // Default is 16 bits signed | |
88 int iFreq = 44100; // Default is 44KHz | |
89 int iChannels = 2; // Default is 2 channels (Stereo) | |
90 int iNumBufs = 2; // Number of audio buffers: 2 | |
91 int iBufSize; | |
92 int iOpenMode; | |
93 int iSilence; | |
94 int rc; | |
95 | |
96 // First thing is to try to open a given DART device! | |
97 memset(&AmpOpenParms, 0, sizeof(MCI_AMP_OPEN_PARMS)); | |
98 // pszDeviceType should contain the device type in low word, and device ordinal in high word! | |
99 AmpOpenParms.pszDeviceType = (PSZ) (MCI_DEVTYPE_AUDIO_AMPMIX | (iDeviceOrd << 16)); | |
100 | |
101 iOpenMode = MCI_WAIT | MCI_OPEN_TYPE_ID; | |
102 if (bOpenShared) iOpenMode |= MCI_OPEN_SHAREABLE; | |
103 | |
104 rc = mciSendCommand( 0, MCI_OPEN, | |
105 iOpenMode, | |
106 (PVOID) &AmpOpenParms, 0); | |
107 if (rc!=MCIERR_SUCCESS) // No audio available?? | |
108 return (-1); | |
109 // Save the device ID we got from DART! | |
110 // We will use this in the next calls! | |
111 iDeviceOrd = AmpOpenParms.usDeviceID; | |
112 | |
113 // Determine the audio parameters from the AudioSpec | |
114 switch ( spec->format & 0xFF ) | |
115 { | |
116 case 8: | |
117 /* Unsigned 8 bit audio data */ | |
118 spec->format = AUDIO_U8; | |
119 iSilence = 0x80; | |
120 iBits = 8; | |
121 break; | |
122 case 16: | |
123 /* Signed 16 bit audio data */ | |
124 spec->format = AUDIO_S16; | |
125 iSilence = 0x00; | |
126 iBits = 16; | |
127 break; | |
128 default: | |
129 // Close DART, and exit with error code! | |
130 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0); | |
131 SDL_SetError("Unsupported audio format"); | |
132 return(-1); | |
133 } | |
134 iFreq = spec->freq; | |
135 iChannels = spec->channels; | |
136 /* Update the fragment size as size in bytes */ | |
137 SDL_CalculateAudioSpec(spec); | |
138 iBufSize = spec->size; | |
139 | |
140 // Now query this device if it supports the given freq/bits/channels! | |
141 memset(&(_this->hidden->MixSetupParms), 0, sizeof(MCI_MIXSETUP_PARMS)); | |
142 _this->hidden->MixSetupParms.ulBitsPerSample = iBits; | |
143 _this->hidden->MixSetupParms.ulFormatTag = MCI_WAVE_FORMAT_PCM; | |
144 _this->hidden->MixSetupParms.ulSamplesPerSec = iFreq; | |
145 _this->hidden->MixSetupParms.ulChannels = iChannels; | |
146 _this->hidden->MixSetupParms.ulFormatMode = MCI_PLAY; | |
147 _this->hidden->MixSetupParms.ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; | |
148 _this->hidden->MixSetupParms.pmixEvent = DARTEventFunc; | |
149 rc = mciSendCommand (iDeviceOrd, MCI_MIXSETUP, | |
150 MCI_WAIT | MCI_MIXSETUP_QUERYMODE, | |
151 &(_this->hidden->MixSetupParms), 0); | |
152 if (rc!=MCIERR_SUCCESS) | |
153 { // The device cannot handle this format! | |
154 // Close DART, and exit with error code! | |
155 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0); | |
156 SDL_SetError("Audio device doesn't support requested audio format"); | |
157 return(-1); | |
158 } | |
159 // The device can handle this format, so initialize! | |
160 rc = mciSendCommand(iDeviceOrd, MCI_MIXSETUP, | |
161 MCI_WAIT | MCI_MIXSETUP_INIT, | |
162 &(_this->hidden->MixSetupParms), 0); | |
163 if (rc!=MCIERR_SUCCESS) | |
164 { // The device could not be opened! | |
165 // Close DART, and exit with error code! | |
166 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0); | |
167 SDL_SetError("Audio device could not be set up"); | |
168 return(-1); | |
169 } | |
170 // Ok, the device is initialized. | |
171 // Now we should allocate buffers. For this, we need a place where | |
172 // the buffer descriptors will be: | |
173 _this->hidden->pMixBuffers = (MCI_MIX_BUFFER *) malloc(sizeof(MCI_MIX_BUFFER)*iNumBufs); | |
174 if (!(_this->hidden->pMixBuffers)) | |
175 { // Not enough memory! | |
176 // Close DART, and exit with error code! | |
177 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0); | |
178 SDL_SetError("Not enough memory for audio buffer descriptors"); | |
179 return(-1); | |
180 } | |
181 // Now that we have the place for buffer list, we can ask DART for the | |
182 // buffers! | |
183 _this->hidden->BufferParms.ulNumBuffers = iNumBufs; // Number of buffers | |
184 _this->hidden->BufferParms.ulBufferSize = iBufSize; // each with this size | |
185 _this->hidden->BufferParms.pBufList = _this->hidden->pMixBuffers; // getting descriptorts into this list | |
186 // Allocate buffers! | |
187 rc = mciSendCommand(iDeviceOrd, MCI_BUFFER, | |
188 MCI_WAIT | MCI_ALLOCATE_MEMORY, | |
189 &(_this->hidden->BufferParms), 0); | |
190 if ((rc!=MCIERR_SUCCESS) || (iNumBufs != _this->hidden->BufferParms.ulNumBuffers) || (_this->hidden->BufferParms.ulBufferSize==0)) | |
191 { // Could not allocate memory! | |
192 // Close DART, and exit with error code! | |
193 free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL; | |
194 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0); | |
195 SDL_SetError("DART could not allocate buffers"); | |
196 return(-1); | |
197 } | |
198 // Ok, we have all the buffers allocated, let's mark them! | |
199 { | |
200 int i; | |
201 for (i=0; i<iNumBufs; i++) | |
202 { | |
203 pMixBufferDesc pBufferDesc = (pMixBufferDesc) malloc(sizeof(tMixBufferDesc));; | |
204 // Check if this buffer was really allocated by DART | |
205 if ((!(_this->hidden->pMixBuffers[i].pBuffer)) || (!pBufferDesc)) | |
206 { // Wrong buffer! | |
207 // Close DART, and exit with error code! | |
208 // Free buffer descriptions | |
209 { int j; | |
210 for (j=0; j<i; j++) free((void *)(_this->hidden->pMixBuffers[j].ulUserParm)); | |
211 } | |
212 // and cleanup | |
213 mciSendCommand(iDeviceOrd, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY, &(_this->hidden->BufferParms), 0); | |
214 free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL; | |
215 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0); | |
216 SDL_SetError("Error at internal buffer check"); | |
217 return(-1); | |
218 } | |
219 pBufferDesc->iBufferUsage = BUFFER_EMPTY; | |
220 pBufferDesc->pSDLAudioDevice = _this; | |
221 | |
222 _this->hidden->pMixBuffers[i].ulBufferLength = _this->hidden->BufferParms.ulBufferSize; | |
223 _this->hidden->pMixBuffers[i].ulUserParm = (ULONG) pBufferDesc; // User parameter: Description of buffer | |
224 _this->hidden->pMixBuffers[i].ulFlags = 0; // Some stuff should be flagged here for DART, like end of | |
225 // audio data, but as we will continously send | |
226 // audio data, there will be no end.:) | |
227 memset(_this->hidden->pMixBuffers[i].pBuffer, iSilence, iBufSize); | |
228 } | |
229 } | |
230 _this->hidden->iNextFreeBuffer = 0; | |
231 _this->hidden->iLastPlayedBuf = -1; | |
232 // Create event semaphore | |
233 if (DosCreateEventSem(NULL, &(_this->hidden->hevAudioBufferPlayed), 0, FALSE)!=NO_ERROR) | |
234 { | |
235 // Could not create event semaphore! | |
236 { | |
237 int i; | |
238 for (i=0; i<iNumBufs; i++) free((void *)(_this->hidden->pMixBuffers[i].ulUserParm)); | |
239 } | |
240 mciSendCommand(iDeviceOrd, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY, &(_this->hidden->BufferParms), 0); | |
241 free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL; | |
242 mciSendCommand(iDeviceOrd, MCI_CLOSE, MCI_WAIT, &GenericParms, 0); | |
243 SDL_SetError("Could not create event semaphore"); | |
244 return(-1); | |
245 } | |
246 | |
247 // Store the new settings in global variables | |
248 _this->hidden->iCurrDeviceOrd = iDeviceOrd; | |
249 _this->hidden->iCurrFreq = iFreq; | |
250 _this->hidden->iCurrBits = iBits; | |
251 _this->hidden->iCurrChannels = iChannels; | |
252 _this->hidden->iCurrNumBufs = iNumBufs; | |
253 _this->hidden->iCurrBufSize = iBufSize; | |
254 | |
255 return (0); | |
256 } | |
257 | |
258 | |
259 | |
260 void DART_ThreadInit(_THIS) | |
261 { | |
262 return; | |
263 } | |
264 | |
265 /* This function waits until it is possible to write a full sound buffer */ | |
266 void DART_WaitAudio(_THIS) | |
267 { | |
268 int i; | |
269 pMixBufferDesc pBufDesc; | |
270 ULONG ulPostCount; | |
271 | |
272 DosResetEventSem(_this->hidden->hevAudioBufferPlayed, &ulPostCount); | |
273 // If there is already an empty buffer, then return now! | |
274 for (i=0; i<_this->hidden->iCurrNumBufs; i++) | |
275 { | |
276 pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[i].ulUserParm; | |
277 if (pBufDesc->iBufferUsage == BUFFER_EMPTY) | |
278 return; | |
279 } | |
280 // If there is no empty buffer, wait for one to be empty! | |
281 DosWaitEventSem(_this->hidden->hevAudioBufferPlayed, 1000); // Wait max 1 sec!!! Important! | |
282 return; | |
283 } | |
284 | |
285 void DART_PlayAudio(_THIS) | |
286 { | |
287 int iFreeBuf = _this->hidden->iNextFreeBuffer; | |
288 pMixBufferDesc pBufDesc; | |
289 | |
290 pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[iFreeBuf].ulUserParm; | |
291 pBufDesc->iBufferUsage = BUFFER_USED; | |
292 // Send it to DART to be queued | |
293 _this->hidden->MixSetupParms.pmixWrite(_this->hidden->MixSetupParms.ulMixHandle, | |
294 &(_this->hidden->pMixBuffers[iFreeBuf]), 1); | |
295 | |
296 _this->hidden->iLastPlayedBuf = iFreeBuf; | |
297 iFreeBuf = (iFreeBuf+1) % _this->hidden->iCurrNumBufs; | |
298 _this->hidden->iNextFreeBuffer = iFreeBuf; | |
299 } | |
300 | |
301 Uint8 *DART_GetAudioBuf(_THIS) | |
302 { | |
303 int iFreeBuf; | |
304 Uint8 *pResult; | |
305 pMixBufferDesc pBufDesc; | |
306 | |
307 if (_this) | |
308 { | |
309 if (_this->hidden) | |
310 { | |
311 iFreeBuf = _this->hidden->iNextFreeBuffer; | |
312 pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[iFreeBuf].ulUserParm; | |
313 | |
314 if (pBufDesc) | |
315 { | |
316 if (pBufDesc->iBufferUsage == BUFFER_EMPTY) | |
317 { | |
318 pResult = _this->hidden->pMixBuffers[iFreeBuf].pBuffer; | |
319 return pResult; | |
320 } | |
321 } else | |
322 printf("[DART_GetAudioBuf] : ERROR! pBufDesc = %p\n", pBufDesc); | |
323 } else | |
324 printf("[DART_GetAudioBuf] : ERROR! _this->hidden = %p\n", _this->hidden); | |
325 } else | |
326 printf("[DART_GetAudioBuf] : ERROR! _this = %p\n", _this); | |
327 return NULL; | |
328 } | |
329 | |
330 void DART_WaitDone(_THIS) | |
331 { | |
332 pMixBufferDesc pBufDesc; | |
333 ULONG ulPostCount; | |
334 APIRET rc; | |
335 | |
336 pBufDesc = (pMixBufferDesc) _this->hidden->pMixBuffers[_this->hidden->iLastPlayedBuf].ulUserParm; | |
337 rc = NO_ERROR; | |
338 while ((pBufDesc->iBufferUsage != BUFFER_EMPTY) && (rc==NO_ERROR)) | |
339 { | |
340 DosResetEventSem(_this->hidden->hevAudioBufferPlayed, &ulPostCount); | |
341 rc = DosWaitEventSem(_this->hidden->hevAudioBufferPlayed, 1000); // 1 sec timeout! Important! | |
342 } | |
343 } | |
344 | |
345 void DART_CloseAudio(_THIS) | |
346 { | |
347 MCI_GENERIC_PARMS GenericParms; | |
348 int rc; | |
349 | |
350 // Stop DART playback | |
351 rc = mciSendCommand(_this->hidden->iCurrDeviceOrd, MCI_STOP, MCI_WAIT, &GenericParms, 0); | |
352 if (rc!=MCIERR_SUCCESS) | |
353 { | |
354 #ifdef SFX_DEBUG_BUILD | |
355 printf("Could not stop DART playback!\n"); | |
356 fflush(stdout); | |
357 #endif | |
358 } | |
359 | |
360 // Close event semaphore | |
361 DosCloseEventSem(_this->hidden->hevAudioBufferPlayed); | |
362 | |
363 // Free memory of buffer descriptions | |
364 { | |
365 int i; | |
366 for (i=0; i<_this->hidden->iCurrNumBufs; i++) free((void *)(_this->hidden->pMixBuffers[i].ulUserParm)); | |
367 } | |
368 | |
369 // Deallocate buffers | |
370 rc = mciSendCommand(_this->hidden->iCurrDeviceOrd, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY, &(_this->hidden->BufferParms), 0); | |
371 | |
372 // Free bufferlist | |
373 free(_this->hidden->pMixBuffers); _this->hidden->pMixBuffers = NULL; | |
374 | |
375 // Close dart | |
376 rc = mciSendCommand(_this->hidden->iCurrDeviceOrd, MCI_CLOSE, MCI_WAIT, &(GenericParms), 0); | |
377 } | |
378 | |
379 /* Audio driver bootstrap functions */ | |
380 | |
381 int Audio_Available(void) | |
382 { | |
383 return(1); | |
384 } | |
385 | |
386 void Audio_DeleteDevice(SDL_AudioDevice *device) | |
387 { | |
388 free(device->hidden); | |
389 free(device); | |
390 } | |
391 | |
392 SDL_AudioDevice *Audio_CreateDevice(int devindex) | |
393 { | |
394 SDL_AudioDevice *this; | |
395 | |
396 /* Initialize all variables that we clean on shutdown */ | |
397 this = (SDL_AudioDevice *)malloc(sizeof(SDL_AudioDevice)); | |
398 if ( this ) | |
399 { | |
400 memset(this, 0, (sizeof *this)); | |
401 this->hidden = (struct SDL_PrivateAudioData *) | |
402 malloc((sizeof *this->hidden)); | |
403 } | |
404 if ( (this == NULL) || (this->hidden == NULL) ) | |
405 { | |
406 SDL_OutOfMemory(); | |
407 if ( this ) | |
408 free(this); | |
409 return(0); | |
410 } | |
411 memset(this->hidden, 0, (sizeof *this->hidden)); | |
412 | |
413 /* Set the function pointers */ | |
414 this->OpenAudio = DART_OpenAudio; | |
415 this->ThreadInit = DART_ThreadInit; | |
416 this->WaitAudio = DART_WaitAudio; | |
417 this->PlayAudio = DART_PlayAudio; | |
418 this->GetAudioBuf = DART_GetAudioBuf; | |
419 this->WaitDone = DART_WaitDone; | |
420 this->CloseAudio = DART_CloseAudio; | |
421 | |
422 this->free = Audio_DeleteDevice; | |
423 | |
424 return this; | |
425 } | |
426 | |
427 AudioBootStrap DART_bootstrap = { | |
428 "dart", "OS/2 Direct Audio RouTines (DART)", | |
429 Audio_Available, Audio_CreateDevice | |
430 }; | |
431 |