-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdsnd.cpp
More file actions
366 lines (284 loc) · 8.35 KB
/
Copy pathdsnd.cpp
File metadata and controls
366 lines (284 loc) · 8.35 KB
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
358
359
360
361
362
363
364
365
/*
dsnd.c - Win32 DirectSound output driver.
Copyright (c) 1997-2001 R.B.
All Rights Reserved.
*/
#define DIRECTSOUND_VERSION 0x500
#include <windows.h>
#include <windowsx.h>
#include <cguid.h>
#include <math.h>
#include <stdio.h>
#include <dsound.h>
#include "cpuintrf.h"
#include "oss.h"
#include "wavelog.h"
static INT16 *samples; // make sure we reserve enough for worst-case scenario
static int s32SoundEnable=1;
void (*m1sdr_Callback)(unsigned long dwSamples, short *samples);
unsigned long cbUserData;
static int hw_present, oss_pause;
LPDIRECTSOUND lpDS; // DirectSound COM object
LPDIRECTSOUNDBUFFER lpPDSB; // Primary DirectSound buffer
LPDIRECTSOUNDBUFFER lpSecB; // Secondary DirectSound buffer
int nDSoundSamRate=44100; // sample rate
int nDSoundSegCount=48; // Segs in the pdsbLoop buffer
static int cbLoopLen=0; // Loop length (in bytes) calculated
int nDSoundFps=600; // Application fps * 10
int nDSoundSegLen=0; // Seg length in samples (calculated from Rate/Fps)
short *DSoundNextSound=NULL; // The next sound seg we will add to the sample loop
unsigned char bDSoundOkay=0; // True if DSound was initted okay
unsigned char bDSoundPlaying=0; // True if the Loop buffer is playing
#define WRAP_INC(x) { x++; if (x>=nDSoundSegCount) x=0; }
static int nDSoundNextSeg=0; // We have filled the sound in the loop up to the beginning of 'nNextSeg'
void m1sdr_SetSamplesPerTick(UINT32 spf)
{
}
void m1sdr_TimeCheck(void)
{
int nPlaySeg=0, nFollowingSeg=0;
DWORD nPlay=0, nWrite=0;
int nRet=0;
if (!lpDS) return;
// We should do nothing until nPlay has left nDSoundNextSeg
IDirectSoundBuffer_GetCurrentPosition(lpSecB, &nPlay, &nWrite);
nPlaySeg=nPlay/(nDSoundSegLen<<2);
if (nPlaySeg>nDSoundSegCount-1) nPlaySeg=nDSoundSegCount-1;
if (nPlaySeg<0) nPlaySeg=0; // important to ensure nPlaySeg clipped for below
if (nDSoundNextSeg == nPlaySeg)
{
Sleep(200); // Don't need to do anything for a bit
goto End;
}
// work out which seg we will fill next
nFollowingSeg = nDSoundNextSeg;
WRAP_INC(nFollowingSeg)
while (nDSoundNextSeg != nPlaySeg)
{
void *pData=NULL,*pData2=NULL; DWORD cbLen=0,cbLen2=0;
// fill nNextSeg
// Lock the relevant seg of the loop buffer
nRet = IDirectSoundBuffer_Lock(lpSecB, nDSoundNextSeg*(nDSoundSegLen<<2), nDSoundSegLen<<2, &pData, &cbLen, &pData2, &cbLen2, 0);
if (nRet>=0 && pData!=NULL)
{
// Locked the seg okay - write the sound we calculated last time
memcpy(pData, samples, nDSoundSegLen<<2);
}
// Unlock (2nd 0 is because we wrote nothing to second part)
if (nRet>=0) IDirectSoundBuffer_Unlock(lpSecB, pData, cbLen, pData2, 0);
// generate more samples
if ((m1sdr_Callback) && (!oss_pause))
{
//printf("callback: %ld samples\n", samples);
m1sdr_Callback(nDSoundSegLen, samples);
}
else
{
memset(samples, 0, nDSoundSegLen*4);
}
waveLogFrame((unsigned char *)samples, nDSoundSegLen<<2);
nDSoundNextSeg = nFollowingSeg;
WRAP_INC(nFollowingSeg)
}
End:
return;
}
// m1sdr_Init - sets up directsound how we want
// thanks to Michael Abrash in DDJ
// for ideas on this.
INT16 m1sdr_Init(int sample_rate)
{
DSBUFFERDESC dsbuf;
WAVEFORMATEX format;
if (!s32SoundEnable) return(0);
nDSoundSamRate = sample_rate;
samples = NULL;
lpDS = NULL;
lpPDSB = NULL;
lpSecB = NULL;
// Calculate the Seg Length and Loop length
// (round to nearest sample)
nDSoundSegLen=(nDSoundSamRate*10+(nDSoundFps>>1))/nDSoundFps;
cbLoopLen=(nDSoundSegLen*nDSoundSegCount)<<2;
// create an IDirectSound COM object
if (DS_OK != DirectSoundCreate(NULL, &lpDS, NULL))
{
printf("Unable to create DirectSound object!\n");
return(0);
}
// set cooperative level where we need it
if (DS_OK != IDirectSound_SetCooperativeLevel(lpDS, GetForegroundWindow(), DSSCL_PRIORITY))
{
printf("Unable to set cooperative level!\n");
return(0);
}
// now create a primary sound buffer
memset(&format, 0, sizeof(format));
format.wFormatTag = WAVE_FORMAT_PCM;
format.nChannels = 2;
format.wBitsPerSample = 16;
format.nSamplesPerSec = nDSoundSamRate;
format.nBlockAlign = 4; // stereo 16-bit
format.cbSize = 0;
format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
memset(&dsbuf, 0, sizeof(dsbuf));
dsbuf.dwSize = sizeof(DSBUFFERDESC);
dsbuf.dwFlags = DSBCAPS_PRIMARYBUFFER;
dsbuf.dwBufferBytes = 0;
dsbuf.lpwfxFormat = NULL;
if (DS_OK != IDirectSound_CreateSoundBuffer(lpDS, &dsbuf, &lpPDSB, NULL))
{
printf("Unable to create primary buffer!");
return(0);
}
// and set it's format how we want
if (DS_OK != IDirectSoundBuffer_SetFormat(lpPDSB, &format))
{
printf("Unable to set primary buffer format!\n");
return(0);
}
// start the primary buffer playing now so we get
// minimal lag when we trigger our secondary buffer
IDirectSoundBuffer_Play(lpPDSB, 0, 0, DSBPLAY_LOOPING);
// that's done, now let's create our secondary buffer
memset(&dsbuf, 0, sizeof(DSBUFFERDESC));
dsbuf.dwSize = sizeof(DSBUFFERDESC);
// we'll take default controls for this one
dsbuf.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLPOSITIONNOTIFY;
dsbuf.dwBufferBytes = cbLoopLen;
dsbuf.lpwfxFormat = (LPWAVEFORMATEX)&format;
if (DS_OK != IDirectSound_CreateSoundBuffer(lpDS, &dsbuf, &lpSecB, NULL))
{
printf("Unable to create secondary buffer\n");
return(0);
}
// ok, cool, we're ready to go!
// blank out the entire sound buffer
{
LPVOID ptr; DWORD len;
IDirectSoundBuffer_Lock(lpSecB, 0, 0, &ptr, &len, NULL, NULL, DSBLOCK_ENTIREBUFFER);
ZeroMemory(ptr, len);
IDirectSoundBuffer_Unlock(lpSecB, ptr, len, 0, 0);
}
// allocate and zero our local buffer
samples = (INT16 *)malloc(nDSoundSegLen<<2);
ZeroMemory(samples, nDSoundSegLen<<2);
bDSoundOkay=1; // This module was initted okay
return(1);
}
void m1sdr_Exit(void)
{
if (!s32SoundEnable)
{
return;
}
if (lpSecB)
{
IDirectSoundBuffer_Stop(lpSecB);
IDirectSoundBuffer_Release(lpSecB);
lpSecB = NULL;
}
if (lpPDSB)
{
IDirectSoundBuffer_Stop(lpPDSB);
IDirectSoundBuffer_Release(lpPDSB);
lpPDSB = NULL;
}
if (lpDS)
{
IDirectSound_Release(lpDS);
lpDS = NULL;
}
if (samples)
{
free(samples);
}
}
void m1sdr_PlayStart(void)
{
waveLogStart();
IDirectSound_SetCooperativeLevel(lpDS, GetForegroundWindow(), DSSCL_PRIORITY);
IDirectSoundBuffer_SetCurrentPosition(lpSecB, 0);
IDirectSoundBuffer_Play(lpSecB, 0, 0, DSBPLAY_LOOPING);
}
void m1sdr_PlayStop(void)
{
DSBUFFERDESC dsbuf;
WAVEFORMATEX format;
waveLogStop();
IDirectSoundBuffer_Stop(lpSecB);
// this is a bit cheezity-hacky
IDirectSoundBuffer_Release(lpSecB);
memset(&format, 0, sizeof(format));
format.wFormatTag = WAVE_FORMAT_PCM;
format.nChannels = 2;
format.wBitsPerSample = 16;
format.nSamplesPerSec = nDSoundSamRate;
format.nBlockAlign = 4; // stereo 16-bit
format.cbSize = 0;
format.nAvgBytesPerSec=format.nSamplesPerSec*format.nBlockAlign;
memset(&dsbuf, 0, sizeof(DSBUFFERDESC));
dsbuf.dwSize = sizeof(DSBUFFERDESC);
// we'll take default controls for this one
dsbuf.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLPOSITIONNOTIFY;
dsbuf.dwBufferBytes = cbLoopLen;
dsbuf.lpwfxFormat = (LPWAVEFORMATEX)&format;
if (DS_OK != IDirectSound_CreateSoundBuffer(lpDS, &dsbuf, &lpSecB, NULL))
{
printf("Unable to create secondary buffer\n");
return;
}
// zero out the buffer
{
LPVOID ptr; DWORD len;
IDirectSoundBuffer_Lock(lpSecB, 0, 0, &ptr, &len, NULL, NULL, DSBLOCK_ENTIREBUFFER);
ZeroMemory(ptr, len);
IDirectSoundBuffer_Unlock(lpSecB, ptr, len, 0, 0);
}
}
INT32 m1sdr_HwPresent(void)
{
return hw_present;
}
INT16 m1sdr_IsThere(void)
{
if(DS_OK == DirectSoundCreate(NULL, &lpDS, NULL))
{
IDirectSound_Release(lpDS);
hw_present = 1;
return(1);
}
else
{
hw_present = 0;
return(0);
}
}
void m1sdr_SetCallback(void *fn)
{
if (fn == (void *)NULL)
{
printf("ERROR: NULL CALLBACK!\n");
}
// printf("m1sdr_SetCallback: aok!\n");
m1sdr_Callback = (void (*)(unsigned long, signed short *))fn;
}
void m1sdr_FlushAudio(void)
{
int oldpause = oss_pause;
oss_pause = 1;
memset(samples, 0, nDSoundSegLen * 4);
m1sdr_TimeCheck();
m1sdr_TimeCheck();
m1sdr_TimeCheck();
m1sdr_TimeCheck();
m1sdr_TimeCheck();
oss_pause = oldpause;
}
void m1sdr_Pause(int set)
{
oss_pause = set;
}
void m1sdr_SetNoWait(int nw)
{
}