source: trunk/src/dsound/new/dart.cpp@ 3054

Last change on this file since 3054 was 3044, checked in by mike, 26 years ago

Killed no longer needed files

File size: 13.2 KB
Line 
1/*
2 * Kevin Langman
3 *
4 * The Dart functions are here to avoid the conficts with the
5 * the OS/2 mcios2.h, meerror.h, os2medef.h and mmsystem.h
6 *
7 */
8
9#define INCL_DOS
10#define INCL_OS2MM
11#include <os2wrap.h>
12#include <os2mewrap.h>
13
14#include <stdlib.h>
15#include <string.h>
16
17#define DART_DSOUND
18#include "dart.h"
19#include "dsmixer.h"
20
21#include <misc.h>
22
23static MCI_MIXSETUP_PARMS *MixSetup_Global;
24static MCI_MIX_BUFFER *pMixBuffs;
25static long lLastBuff;
26static char *pDSoundBuff;
27static BOOL fIsPlaying = FALSE;
28
29USHORT usDeviceID; /* Amp Mixer device id */
30MCI_MIX_BUFFER MixBuffers[NUM_DART_BUFFS]; /* Device buffers */
31MCI_MIXSETUP_PARMS MixSetupParms; /* Mixer parameters */
32MCI_BUFFER_PARMS BufferParms; /* Device buffer parms */
33
34
35#define ULONG_LOWD(ul) (*(USHORT *)((ULONG *)(&ul))) /* Low Word */
36
37//******************************************************************************
38//******************************************************************************
39LONG APIENTRY OS2_Dart_Update(ULONG ulStatus,PMCI_MIX_BUFFER pBuffer,ULONG ulFlags)
40{
41 //MCI_STATUS_PARMS Status;
42 ULONG rc;
43
44 if ((ulFlags == MIX_WRITE_COMPLETE) ||
45 ((ulFlags == (MIX_WRITE_COMPLETE | MIX_STREAM_ERROR))&&
46 (ulStatus == ERROR_DEVICE_UNDERRUN)))
47 {
48 lLastBuff++;
49 if (lLastBuff == NUM_DART_BUFFS)
50 lLastBuff = 0;
51
52 /* Now mix sound from all playing secondary SoundBuffers into the primary buffer */
53 MixCallback(BUFFER_SIZE/NUM_DART_BUFFS);
54
55 /* Fill The next buff from the DSound Buffer */
56 memcpy( pMixBuffs[lLastBuff].pBuffer, &pDSoundBuff[lLastBuff*(BUFFER_SIZE/NUM_DART_BUFFS)], BUFFER_SIZE/NUM_DART_BUFFS );
57
58 /* Send the NEXT Buffer to Dart for playing! */
59 rc = MixSetup_Global->pmixWrite(MixSetup_Global->ulMixHandle, &pMixBuffs[lLastBuff], 1 );
60 //dprintf(("DSOUND-DART: Playing Next Buffer %d", rc));
61 }
62
63 return TRUE;
64}
65
66
67long Dart_Open_Device(USHORT *pusDeviceID, void **vpMixBuffer, void **vpMixSetup,
68 void **vpBuffParms, void **ppvBuffer)
69{
70
71 MCI_AMP_OPEN_PARMS AmpOpenParms;
72 //MCI_WAVE_SET_PARMS WaveSetParms;
73 ULONG rc, ulNew;
74 LONG lAdd = 5;
75 short device = 0;
76
77 dprintf(("DSOUND-DART: Dart_Open_Device"));
78
79 DosSetRelMaxFH(&lAdd, &ulNew);
80
81 *vpMixBuffer = &MixBuffers;
82 *vpMixSetup = &MixSetupParms;
83 *vpBuffParms = &BufferParms;
84
85 pMixBuffs = &MixBuffers[0];
86 lLastBuff = 0;
87
88 /* Is there a way to avoid the use of the MixSetup_Global ????? */
89 MixSetup_Global = &MixSetupParms;
90 /****************************************************************/
91
92 // If the DSOUND_DEVICE is set then use that number as the device for DART.
93 // this will allow people with many sound cards to use the card of their choice
94 // for an instance of a DSOUND enabled app!
95 if (getenv("DSOUND_DEVICE") != NULL) {
96 device = atoi(getenv("DSOUND_DEVICE"));
97 }
98
99 /* Open the AmpMix Device and start processing the buffer! */
100 memset(&AmpOpenParms, 0, sizeof(MCI_AMP_OPEN_PARMS));
101 AmpOpenParms.usDeviceID = 0;
102 AmpOpenParms.pszDeviceType = (PSZ)MCI_DEVTYPE_AUDIO_AMPMIX;
103
104 rc = mciSendCommand(0, MCI_OPEN, MCI_WAIT | MCI_OPEN_TYPE_ID, (PVOID)&AmpOpenParms, 0);
105 if (rc != MCIERR_SUCCESS) {
106 dprintf(("DSOUND-DART: MCI_OPEN %d", rc));
107 return DSERR_GENERIC;
108 }
109 *pusDeviceID = AmpOpenParms.usDeviceID;
110 usDeviceID = AmpOpenParms.usDeviceID;
111
112 /* setup playback parameters */
113 memset(&MixSetupParms, 0, sizeof(MCI_MIXSETUP_PARMS));
114 MixSetupParms.ulBitsPerSample = 16;
115 MixSetupParms.ulFormatTag = MCI_WAVE_FORMAT_PCM;
116 MixSetupParms.ulSamplesPerSec = 22500;
117 MixSetupParms.ulChannels = 2;
118 MixSetupParms.ulFormatMode = MCI_PLAY;
119 MixSetupParms.ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
120 MixSetupParms.pmixEvent = OS2_Dart_Update;
121
122 rc = mciSendCommand(usDeviceID, MCI_MIXSETUP, MCI_WAIT | MCI_MIXSETUP_INIT,
123 (PVOID)&MixSetupParms, 0);
124 if (rc != MCIERR_SUCCESS) {
125 dprintf(("DSOUND-DART: MCI_MIXSETUP (Constructor) %d", rc));
126 return DSERR_GENERIC;
127 }
128
129 /* Create the Audio Buffer */
130 // OK... Since DSound only uses 1 buffer and uses the GetPosition API to
131 // figure out where it can and can't write, I have emulating this by
132 // using 16 1kb buffers and do locking by tracking what buffer is currently
133 // playing. This maybe less CPU friendly then other methods but it's the best
134 // my little brain could come up with!!
135
136 MixSetupParms.ulBufferSize = BUFFER_SIZE/NUM_DART_BUFFS;
137
138// BufferParms->ulStructLength = sizeof(MCI_BUFFER_PARMS);
139 BufferParms.ulNumBuffers = NUM_DART_BUFFS;
140 BufferParms.ulBufferSize = MixSetupParms.ulBufferSize;
141 BufferParms.pBufList = MixBuffers;
142
143 rc = mciSendCommand(usDeviceID, MCI_BUFFER, MCI_WAIT | MCI_ALLOCATE_MEMORY,
144 (PVOID)&BufferParms, 0);
145 if ( ULONG_LOWD(rc) != MCIERR_SUCCESS ) {
146 dprintf(("DSOUND-DART: MCI_BUFFER (Alloc) %d", rc));
147 mciSendCommand(*pusDeviceID, MCI_CLOSE, MCI_WAIT, NULL, 0);
148 return DSERR_OUTOFMEMORY;
149 }
150
151 /* Clear the Buffer */
152 // Todo: I don't know if Dart expects Signed or Unsigned data... This will
153 // ok until I look in to this... Also it may be that Dart uses signed for
154 // 8bit data and unsigned for 16it.. Anyhow the worst that can happen by setting
155 // the buffer to 0 is a click on Dart_Play and a Clink when real sound is written
156 // to the buffer!
157 for (device = 0; device < NUM_DART_BUFFS; device++) {
158 memset(MixBuffers[device].pBuffer, 0, BUFFER_SIZE/NUM_DART_BUFFS);
159 }
160
161 // Allocate memory for the DSound "Holder" buffer.
162 // TODO: Move this to the Constructor for OS2PrimBuff
163 // so that the Deconstructor can be used to free the memory!
164 *(char**)ppvBuffer = (char*)malloc(BUFFER_SIZE);
165 if (*ppvBuffer == NULL) {
166 return DSERR_OUTOFMEMORY;
167 }
168 pDSoundBuff = (char*)(*ppvBuffer);
169
170 dprintf(("DSOUND-DART: Dart_Open_Device Exiting"));
171
172 return DS_OK;
173}
174
175long Dart_Close_Device(USHORT usDeviceID, void *vpMixBuffer, void *vpMixSetup,
176 void *vpBuffParms )
177{
178 MCI_MIX_BUFFER *MixBuffer;
179 MCI_MIXSETUP_PARMS *MixSetup;
180 MCI_BUFFER_PARMS *BufferParms;
181 ULONG rc;
182
183 dprintf(("DSOUND-DART: Dart_Close_Device"));
184
185 MixBuffer = (MCI_MIX_BUFFER*)vpMixBuffer;
186 MixSetup = (MCI_MIXSETUP_PARMS*)vpMixSetup;
187 BufferParms = (MCI_BUFFER_PARMS*)vpBuffParms;
188
189 rc = mciSendCommand(usDeviceID, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY, BufferParms, 0);
190 if (rc != MCIERR_SUCCESS) {
191 dprintf(("DSOUND-DART: MCI_BUFFER (Close) %d", rc));
192 }
193 rc = mciSendCommand(usDeviceID, MCI_CLOSE, MCI_WAIT, NULL, 0);
194 if (rc != MCIERR_SUCCESS) {
195 dprintf(("DSOUND-DART: MCI_CLOSE (Close) %d", rc));
196 }
197
198 dprintf(("DSOUND-DART: Dart_Close_Device returning DS_OK"));
199 return DS_OK;
200}
201
202
203long Dart_GetPosition(USHORT usDeviceID, LONG *pulPosition)
204{
205 dprintf(("DSOUND-DART: Dart_GetPosition"));
206
207 *pulPosition = (lLastBuff * (BUFFER_SIZE/NUM_DART_BUFFS)) + (BUFFER_SIZE/NUM_DART_BUFFS);
208 if (*pulPosition > BUFFER_SIZE)
209 *pulPosition = 0;
210
211 dprintf(("DSOUND-DART: Returning %d", *pulPosition));
212
213 return DS_OK;
214}
215
216long Dart_SetFormat(USHORT *pusDeviceID, void *vpMixSetup, void *vpBufferParms, void *vpMixBuffer, LONG lBPS, LONG lSPS, LONG lChannels )
217{
218 ULONG rc;
219 MCI_MIXSETUP_PARMS *MixSetup;
220 MCI_BUFFER_PARMS *BufferParms;
221 MCI_MIX_BUFFER *MixBuffer;
222 MCI_AMP_OPEN_PARMS AmpOpenParms;
223 short device = 0;
224
225 MixSetup = (MCI_MIXSETUP_PARMS*)vpMixSetup;
226 BufferParms = (MCI_BUFFER_PARMS*)vpBufferParms;
227 MixBuffer = (MCI_MIX_BUFFER*)vpMixBuffer;
228
229 dprintf(("DSOUND-DART: Dart_SetFormat"));
230
231 // If the DSOUND_DEVICE is set then use that number as the device for DART.
232 // this will allow people with many sound cards to use the card of there choice
233 // for an instance of a DSOUND enabled app!
234 if (getenv("DSOUND_DEVICE") != NULL) {
235 device = atoi(getenv( "DSOUND_DEVICE" ));
236 }
237
238 /* Dealloc to avoid the 5511 error */
239 rc = mciSendCommand(*pusDeviceID, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY,
240 BufferParms, 0);
241 if (rc != MCIERR_SUCCESS) {
242 dprintf(("DSOUND-DART: MCI_DEALLOCATE_MEMORY (SetFormat) %d", rc));
243 return DSERR_GENERIC;
244 }
245
246 rc = mciSendCommand(*pusDeviceID, MCI_CLOSE, MCI_WAIT, NULL, 0);
247 if (rc != MCIERR_SUCCESS) {
248 dprintf(("DSOUND-DART: MCI_CLOSE (SetFormat) %d", rc));
249 return(DSERR_GENERIC);
250 }
251
252 /* Reopen the MixAmp Device and start processing the buffer! */
253 memset(&AmpOpenParms,0,sizeof(MCI_AMP_OPEN_PARMS));
254 AmpOpenParms.usDeviceID = 0;
255 AmpOpenParms.pszDeviceType = (PSZ)MAKEULONG(MCI_DEVTYPE_AUDIO_AMPMIX, (USHORT)device);
256
257 rc = mciSendCommand(0, MCI_OPEN, MCI_WAIT | MCI_OPEN_TYPE_ID, (PVOID)&AmpOpenParms, 0);
258 if (rc != MCIERR_SUCCESS) {
259 dprintf(("DSOUND-DART: MCI_OPEN %d", rc));
260 return DSERR_GENERIC;
261 }
262 *pusDeviceID = AmpOpenParms.usDeviceID;
263
264 /* setup playback parameters */
265 memset(MixSetup, 0, sizeof(MCI_MIXSETUP_PARMS));
266 MixSetup->ulBitsPerSample = lBPS;
267 MixSetup->ulFormatTag = MCI_WAVE_FORMAT_PCM;
268 MixSetup->ulSamplesPerSec = lSPS;
269 MixSetup->ulChannels = lChannels;
270 MixSetup->ulFormatMode = MCI_PLAY;
271 MixSetup->ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
272 MixSetup->pmixEvent = OS2_Dart_Update;
273
274 rc = mciSendCommand(*pusDeviceID, MCI_MIXSETUP, MCI_WAIT | MCI_MIXSETUP_INIT,
275 (PVOID)MixSetup, 0);
276 if (rc != MCIERR_SUCCESS) {
277 dprintf(("DSOUND-DART: MCI_MIXSETUP (SetFormat) %d", rc));
278 return DSERR_GENERIC;
279 }
280
281 memset(BufferParms, 0, sizeof(MCI_BUFFER_PARMS));
282 memset(MixBuffer, 0, sizeof(MCI_MIX_BUFFER) * NUM_DART_BUFFS);
283 BufferParms->ulStructLength = sizeof(MCI_BUFFER_PARMS);
284 BufferParms->ulNumBuffers = NUM_DART_BUFFS;
285 BufferParms->ulBufferSize = BUFFER_SIZE/NUM_DART_BUFFS;
286 BufferParms->pBufList = MixBuffer;
287 MixBuffer->pBuffer = NULL;
288
289 rc = mciSendCommand(*pusDeviceID, MCI_BUFFER, MCI_WAIT | MCI_ALLOCATE_MEMORY,
290 (PVOID)BufferParms, 0);
291 if (rc != MCIERR_SUCCESS) {
292 dprintf(("DSOUND-DART: MCI_BUFFER_ALLOCATE_MEMORY (SetFormat) %d", rc));
293 mciSendCommand(*pusDeviceID, MCI_CLOSE, MCI_WAIT, NULL, 0);
294 memset(MixBuffer, 0, sizeof(MCI_MIX_BUFFER) * NUM_DART_BUFFS);
295 return DSERR_OUTOFMEMORY;
296 }
297
298 /* Clear the Buffer */
299 // Todo: I don't know if Dart expects Signed or Unsigned data... This will
300 // ok until I look in to this... Also it may be that Dart uses signed for
301 // 8bit data and unsigned for 16it.. Anyhow the worst that can happen by setting
302 // the buffer to 0 is a click on Dart_Play and a Clink when real sound is written
303 // to the buffer!
304 for (int i=0; i<NUM_DART_BUFFS; i++) {
305 memset(MixBuffer[i].pBuffer, 0, BUFFER_SIZE/NUM_DART_BUFFS);
306 }
307
308 /* If the primary buffer was playing, we have to restart it!! */
309 if (fIsPlaying) {
310 dprintf(("DSOUND-DART: Restarting playback!!!!"));
311
312 /* Mix the first buffer before playing */
313 MixCallback(BUFFER_SIZE/NUM_DART_BUFFS);
314 memcpy(pMixBuffs[lLastBuff].pBuffer, &pDSoundBuff[lLastBuff*(BUFFER_SIZE/NUM_DART_BUFFS)], BUFFER_SIZE/NUM_DART_BUFFS);
315
316 USHORT sel = RestoreOS2FS();
317 /* Note: the call to pmixWrite trashes the FS selector, we have to save */
318 /* and then restore FS!!! Otherwise exception handling will be broken. */
319 MixSetupParms.pmixWrite(MixSetupParms.ulMixHandle, MixBuffers, 2);
320 SetFS(sel);
321 fIsPlaying = TRUE;
322 }
323
324 return DS_OK;
325}
326
327
328long Dart_Stop(USHORT usDeviceID)
329{
330 ULONG rc;
331
332 dprintf(("DSOUND-DART: Dart_Stop"));
333
334 if (!fIsPlaying)
335 return DS_OK;
336
337 fIsPlaying = FALSE;
338// rc = mciSendCommand(usDeviceID, MCI_PAUSE, MCI_WAIT, NULL, 0);
339 rc = mciSendCommand(usDeviceID, MCI_STOP, MCI_WAIT, NULL, 0);
340 if (rc != MCIERR_SUCCESS) {
341 dprintf(("DSOUND-DART: MCI_PAUSE %d", rc));
342 return DSERR_GENERIC;
343 }
344 return DS_OK;
345}
346
347long Dart_Play(USHORT usDeviceID, void *vpMixSetup, void *vpMixBuffer, long playing)
348{
349 ULONG rc;
350 MCI_MIXSETUP_PARMS *MixSetup;
351 MCI_MIX_BUFFER *MixBuffer;
352
353 MixSetup = (MCI_MIXSETUP_PARMS*)vpMixSetup;
354 MixBuffer = (MCI_MIX_BUFFER*)vpMixBuffer;
355
356 dprintf(("DSOUND-DART: Dart_Play"));
357
358 if (playing == TRUE) {
359 rc = mciSendCommand(usDeviceID, MCI_RESUME, MCI_WAIT, NULL, 0);
360 if (rc != MCIERR_SUCCESS) {
361 dprintf(("DSOUND-DART: MCI_RESUME %d", rc));
362 return DSERR_GENERIC;
363 }
364 } else { //if (playing==FALSE)
365 dprintf(("DSOUND-DART: Playback started!!!!"));
366
367 /* Mix the first buffer before playing */
368 MixCallback(BUFFER_SIZE/NUM_DART_BUFFS);
369 memcpy(pMixBuffs[lLastBuff].pBuffer, &pDSoundBuff[lLastBuff*(BUFFER_SIZE/NUM_DART_BUFFS)], BUFFER_SIZE/NUM_DART_BUFFS);
370
371 USHORT sel = RestoreOS2FS();
372 /* Note: the call to pmixWrite trashes the FS selector, we have to save */
373 /* and then restore FS!!! Otherwise exception handling will be broken. */
374 MixSetupParms.pmixWrite(MixSetupParms.ulMixHandle, MixBuffers, 2);
375 SetFS(sel);
376 fIsPlaying = TRUE;
377 }
378
379 return DS_OK;
380}
Note: See TracBrowser for help on using the repository browser.