-
Notifications
You must be signed in to change notification settings - Fork 9
/
dsnd.c
351 lines (283 loc) · 9.63 KB
/
dsnd.c
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
/*
Audio Overload SDK
Copyright (c) 2007, R. Belmont and Richard Bannister.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the names of R. Belmont and Richard Bannister nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "cpuintrf.h"
#include "m1sdr.h"
#include "ao.h"
#include "win32_utf8/win32_utf8.h"
static stereo_sample_t samples[44100*2]; // make sure we reserve enough for worst-case scenario
GUID DeviceGUID;
char *DevicePlayback;
LPDIRECTSOUND lpDS; // DirectSound COM object
LPDIRECTSOUNDBUFFER lpPDSB; // Primary DirectSound buffer
LPDIRECTSOUNDBUFFER lpSecB; // Secondary DirectSound buffer
int nDSoundSamRate=44100; // sample rate
int nDSoundSegCount=16; // 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'
// Yes, turning these into Unicode callbacks would have been preferable, but
// [lpContext] comes from the command line.
BOOL CALLBACK DeviceCompareProc(
LPGUID lpGUID, LPCSTR lpszDesc, LPCSTR lpszDrvName, LPVOID lpContext
)
{
char *device_requested = (char*)lpContext;
if(!strcasecmp(device_requested, lpszDesc)) {
memcpy(&DeviceGUID, lpGUID, sizeof(GUID));
DevicePlayback = device_requested;
return FALSE;
}
return TRUE;
}
BOOL CALLBACK DevicePrintProc(
LPGUID lpGUID, LPCSTR lpszDesc, LPCSTR lpszDrvName, LPVOID lpContext
)
{
printf("* %s\n", lpszDesc);
return TRUE;
}
void m1sdr_SetSamplesPerTick(UINT32 spf)
{
if (spf != (nDSoundFps/10))
{
m1sdr_Exit();
nDSoundFps = spf * 10;
m1sdr_Init(DevicePlayback, nDSoundSamRate);
}
}
m1sdr_ret_t m1sdr_TimeCheck(void)
{
int nPlaySeg=0, nFollowingSeg=0;
DWORD nPlay=0, nWrite=0;
int nRet=0;
if (!lpDS) return M1SDR_ERROR;
// 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)
{
// Don't need to do anything for a bit
return M1SDR_WAIT;
}
// 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)
{
m1sdr_Callback(nDSoundSegLen, samples);
}
else
{
memset(samples, 0, nDSoundSegLen*4);
}
nDSoundNextSeg = nFollowingSeg;
WRAP_INC(nFollowingSeg)
}
return M1SDR_OK;
}
void m1sdr_PrintDevices(void)
{
CoInitializeEx(NULL, COINIT_MULTITHREADED);
DirectSoundEnumerateU(DevicePrintProc, NULL);
CoUninitialize();
}
INT16 m1sdr_Init(char *device_requested, int sample_rate)
{
DSBUFFERDESC dsbuf;
WAVEFORMATEX format;
nDSoundSamRate = sample_rate;
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;
CoInitializeEx(NULL, COINIT_MULTITHREADED);
if(device_requested)
{
DirectSoundEnumerateU(DeviceCompareProc, device_requested);
if(DevicePlayback == NULL && device_requested[0] != '\0')
{
printf(
"Could not find audio output device \"%s\", outputting on the primary device.\n",
device_requested
);
}
}
// create an IDirectSound COM object
if (DS_OK != DirectSoundCreate(DevicePlayback ? &DeviceGUID : 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);
}
ZeroMemory(samples, nDSoundSegLen<<2);
bDSoundOkay=1; // This module was initted okay
return(1);
}
void m1sdr_Exit(void)
{
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;
}
CoUninitialize();
}
void m1sdr_PlayStart(void)
{
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;
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);
}
}
void m1sdr_FlushAudio(void)
{
memset(samples, 0, nDSoundSegLen * 4);
m1sdr_TimeCheck();
m1sdr_TimeCheck();
m1sdr_TimeCheck();
m1sdr_TimeCheck();
m1sdr_TimeCheck();
}