1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/* OS/2 DART support for EsounD
17-12-99: Andrew Zabolotny <bit@eltech.ru>
*/
#define INCL_DOS
#define INCL_OS2MM
#include <os2.h>
/* Prevent a warning: PPFN redefined */
#define PPFN _PPFN
#include <os2me.h>
#undef PPFN
/* Define the macro below to grab audio device into exclusive use */
#undef DART_EXCLUSIVE
/* Define the macro below to rise our priority to timecritical */
#undef DART_TIMECRITICAL
/* Allocate 4 audio buffers for smooth playback (should be power of two) */
#define BUFFER_COUNT 8
#define BUFFER_COUNT_MASK (BUFFER_COUNT - 1)
static MCI_MIX_BUFFER MixBuffers[BUFFER_COUNT];
static MCI_MIXSETUP_PARMS MixSetupParms;
static MCI_BUFFER_PARMS BufferParms;
static ULONG DeviceIndex = 0; /* use default waveaudio device */
static ULONG DeviceHandle = 0;
static int BufferSize; /* Size of one audio buffer */
static volatile int MixBufferIndex; /* Next free buffer index */
static volatile int MixBufferCount; /* Number of buffers to play */
static HMTX MixBufferSem; /* Mix buffer semaphore */
/*******************************************************************/
/* display available devices */
#define ARCH_esd_audio_devices
const char * esd_audio_devices()
{
return "(card index: 0:default, 1, 2, ...)";
}
static int __audio_start ()
{
#ifdef DART_EXCLUSIVE
MCI_GENERIC_PARMS GenericParms;
/* grab exclusive rights to device instance (not entire device) */
GenericParms.hwndCallback = 0; /* Not needed, so set to 0 */
if (mciSendCommand(DeviceHandle, MCI_ACQUIREDEVICE, MCI_EXCLUSIVE_INSTANCE,
(PVOID) &GenericParms, 0) != MCIERR_SUCCESS) {
fprintf (stderr, "Cannot get exclusive access to the audio device!\n");
return -1;
}
#endif
return 0;
}
static int __audio_stop ()
{
MCI_GENERIC_PARMS GenericParms;
GenericParms.hwndCallback = 0;
mciSendCommand (DeviceHandle, MCI_STOP, MCI_WAIT, (PVOID) &GenericParms, 0);
#ifdef DART_EXCLUSIVE
GenericParms.hwndCallback = 0; /* Not needed, so set to 0 */
if (mciSendCommand(DeviceHandle, MCI_RELEASEDEVICE, MCI_RETURN_RESOURCE,
(PVOID) &GenericParms, 0) != MCIERR_SUCCESS) {
fprintf (stderr, "Cannot release exclusive access to the audio device!\n");
return -1;
}
#endif
MixBufferCount = 0;
}
/* Buffer update thread (created and called by DART)
This is a high-priority thread used to compute and update the audio stream,
automatically created by the DART subsystem. We compute the next audio
buffer and feed it to the waveaudio device. */
static LONG APIENTRY Dart_UpdateBuffers(ULONG ulStatus, PMCI_MIX_BUFFER pBuffer, ULONG ulFlags)
{
int flags = (ulFlags & ~MIX_STREAM_ERROR);
/* sanity check */
if (!pBuffer)
return TRUE;
/* if we have finished a buffer, we're ready to play a new one */
if ((flags == MIX_WRITE_COMPLETE)
|| (flags == MIX_READ_COMPLETE)) {
if (MixBufferCount) {
DosRequestMutexSem (MixBufferSem, SEM_INDEFINITE_WAIT);
MixBufferCount--;
DosReleaseMutexSem (MixBufferSem);
if (!MixBufferCount)
/* If there are no buffers left, stop playing/recording */
__audio_stop ();
}
}
return TRUE;
}
/*******************************************************************/
/* open the audio device */
#define ARCH_esd_audio_open
int esd_audio_open()
{
int bit, recording = ((esd_audio_format & ESD_MASK_FUNC) == ESD_RECORD);
MCI_AMP_OPEN_PARMS AmpOpenParms;
MCI_GENERIC_PARMS GenericParms;
/* Recording does not work yet */
if (recording)
return -1;
/* Create the mutex semaphore */
DosCreateMutexSem (NULL, &MixBufferSem, 0, FALSE);
DeviceIndex = esd_audio_device ? atoi (esd_audio_device) : 0;
MixBufferIndex = 0;
MixBufferCount = 0;
MixBuffers[0].pBuffer = NULL; /* marker */
DeviceHandle = 0;
memset(&GenericParms, 0, sizeof(MCI_GENERIC_PARMS));
/* open AMP device */
memset(&AmpOpenParms, 0, sizeof(MCI_AMP_OPEN_PARMS));
AmpOpenParms.usDeviceID = 0;
AmpOpenParms.pszDeviceType = (PSZ) MAKEULONG(MCI_DEVTYPE_AUDIO_AMPMIX,
(USHORT) DeviceIndex);
if (mciSendCommand(0, MCI_OPEN, MCI_WAIT | MCI_OPEN_SHAREABLE | MCI_OPEN_TYPE_ID,
(PVOID) & AmpOpenParms, 0) != MCIERR_SUCCESS) {
fprintf (stderr, "Error opening audio device %d\n", DeviceIndex);
return -1;
}
DeviceHandle = AmpOpenParms.usDeviceID;
/* setup playback parameters */
memset(&MixSetupParms, 0, sizeof(MCI_MIXSETUP_PARMS));
MixSetupParms.ulBitsPerSample = (esd_audio_format & ESD_BITS16) ? 16 : 8;
MixSetupParms.ulFormatTag = MCI_WAVE_FORMAT_PCM;
MixSetupParms.ulSamplesPerSec = esd_audio_rate;
MixSetupParms.ulChannels = (esd_audio_format & ESD_STEREO) ? 2 : 1;
MixSetupParms.ulFormatMode = recording ? MCI_RECORD : MCI_PLAY;
MixSetupParms.ulDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
MixSetupParms.pmixEvent = Dart_UpdateBuffers;
if (mciSendCommand(DeviceHandle, MCI_MIXSETUP,
MCI_WAIT | MCI_MIXSETUP_INIT,
(PVOID) &MixSetupParms, 0) != MCIERR_SUCCESS) {
fprintf (stderr, "Audio device %d does not support playing %d bits %dKHz %s\n",
DeviceIndex, MixSetupParms.ulBitsPerSample, esd_audio_rate / 1000,
esd_audio_format & ESD_STEREO ? "stereo" : "mono");
return -1;
}
/* We want audio buffers of a reasonable size, for about 1/16" */
BufferSize = esd_audio_rate >> 4;
if (esd_audio_format & ESD_STEREO)
BufferSize <<= 1;
if (esd_audio_format & ESD_BITS16)
BufferSize <<= 1;
for (bit = 15; bit >= 12; bit--)
if (BufferSize & (1 << bit))
break;
BufferSize = (1 << bit);
/* make sure buffer is not greater than 64 Kb, as DART can't handle this
situation. */
if (BufferSize > 65536)
BufferSize = 65536;
BufferParms.ulStructLength = sizeof(BufferParms);
BufferParms.ulNumBuffers = BUFFER_COUNT;
BufferParms.ulBufferSize = BufferSize;
BufferParms.pBufList = MixBuffers;
if (mciSendCommand(DeviceHandle, MCI_BUFFER,
MCI_WAIT | MCI_ALLOCATE_MEMORY,
(PVOID) &BufferParms, 0) != MCIERR_SUCCESS) {
mciSendCommand(DeviceHandle, MCI_CLOSE, MCI_WAIT, (PVOID) &GenericParms, 0);
fprintf (stderr, "Error while trying to allocate %d playback buffers of %dK\n",
BufferParms.ulNumBuffers, BufferParms.ulBufferSize / 1024);
return -1;
}
if (recording) {
MCI_CONNECTOR_PARMS ConnectorParms;
MCI_AMP_SET_PARMS AmpSetParms ;
/* Set the connector to 'line in' */
memset (&ConnectorParms, 0, sizeof (ConnectorParms));
ConnectorParms.ulConnectorType = MCI_LINE_IN_CONNECTOR;
mciSendCommand (DeviceHandle, MCI_CONNECTOR,
MCI_WAIT | MCI_ENABLE_CONNECTOR | MCI_CONNECTOR_TYPE,
&ConnectorParms, 0);
/* Allow the user to hear what is being recorded
by turning the monitor on */
memset (&AmpSetParms, 0, sizeof (AmpSetParms));
AmpSetParms.ulItem = MCI_AMP_SET_MONITOR;
mciSendCommand (DeviceHandle, MCI_SET,
MCI_WAIT | MCI_SET_ON | MCI_SET_ITEM,
&AmpSetParms, 0);
}
#ifdef DART_TIMECRITICAL
/* Rise the priority of the program since we're using small buffers */
DosSetPriority (PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
#endif
/* We don't really have a handle, so return a number >0 */
return 1;
}
/*******************************************************************/
/* close the audio device */
#define ARCH_esd_audio_close
void esd_audio_close()
{
MCI_GENERIC_PARMS GenericParms;
__audio_stop ();
if (MixBuffers[0].pBuffer) {
mciSendCommand(DeviceHandle, MCI_BUFFER, MCI_WAIT | MCI_DEALLOCATE_MEMORY,
&BufferParms, 0);
MixBuffers[0].pBuffer = NULL;
}
if (DeviceHandle) {
mciSendCommand(DeviceHandle, MCI_CLOSE, MCI_WAIT, (PVOID) &GenericParms, 0);
DeviceHandle = 0;
}
DosCloseMutexSem (MixBufferSem);
#ifdef DART_TIMECRITICAL
/* Drop the priority back */
DosSetPriority (PRTYS_THREAD, PRTYC_REGULAR, 0, 0);
#endif
}
/*******************************************************************/
/* make the sound device quiet for a while */
#define ARCH_esd_audio_pause
void esd_audio_pause()
{
/* Wait until all buffers are free */
while (MixBufferCount)
DosSleep (0);
return;
}
#define ARCH_esd_audio_write
/*******************************************************************/
/* dump a buffer to the sound device */
int esd_audio_write( void *buffer, int buf_size )
{
int i, bytes_played = 0;
MCI_GENERIC_PARMS GenericParms;
PMCI_MIX_BUFFER pbuf;
while (buf_size) {
/* Wait for buffers to free */
while (MixBufferCount >= BUFFER_COUNT)
DosSleep (0);
/* fill next available buffer */
pbuf = &MixBuffers[MixBufferIndex];
MixBufferIndex = (MixBufferIndex + 1) & BUFFER_COUNT_MASK;
i = buf_size;
if (i > BufferSize)
i = BufferSize;
pbuf->ulBufferLength = i;
memcpy (pbuf->pBuffer, buffer, i);
buffer = ((char *)buffer) + i;
bytes_played += i;
buf_size -= i;
/* Grab the mix buffers into exclusive use */
if (DosRequestMutexSem (MixBufferSem, SEM_INDEFINITE_WAIT))
break;
MixBufferCount++;
/* Release the semaphore */
DosReleaseMutexSem (MixBufferSem);
/* If the audio is stopped, grab it and start playing */
if (MixBufferCount == 1)
if (__audio_start () < 0)
break;
/* Output the buffer */
MixSetupParms.pmixWrite(MixSetupParms.ulMixHandle, pbuf, 1);
} /* endwhile */
return bytes_played;
}
#define ARCH_esd_audio_read
/*******************************************************************/
/* read a chunk from the sound device */
int esd_audio_read( void *buffer, int buf_size )
{
/* The following does not work yet - for unknown reason */
int i, bytes_sampled = 0;
MCI_GENERIC_PARMS GenericParms;
PMCI_MIX_BUFFER pbuf;
while (buf_size) {
/* Wait for buffers to free */
while (MixBufferCount >= BUFFER_COUNT)
DosSleep (0);
/* read into next available buffer */
pbuf = &MixBuffers[MixBufferIndex];
MixBufferIndex = (MixBufferIndex + 1) & BUFFER_COUNT_MASK;
i = buf_size;
if (i > BufferSize)
i = BufferSize;
pbuf->ulBufferLength = i;
/* Grab the mix buffers into exclusive use */
if (DosRequestMutexSem (MixBufferSem, SEM_INDEFINITE_WAIT))
break;
MixBufferCount++;
/* Release the semaphore */
DosReleaseMutexSem (MixBufferSem);
/* If the audio is stopped, grab it and start playing */
if (MixBufferCount == 1)
if (__audio_start () < 0)
break;
/* Read into the buffer */
MixSetupParms.pmixRead(MixSetupParms.ulMixHandle, pbuf, 1);
buffer = ((char *)buffer) + i;
bytes_sampled += i;
buf_size -= i;
} /* endwhile */
while (MixBufferCount)
DosSleep (0);
return bytes_sampled;
}
#define ARCH_esd_audio_flush
/*******************************************************************/
/* flush the audio buffer */
void esd_audio_flush()
{
/* Wait until all buffers are free */
while (MixBufferCount)
DosSleep (0);
}
|