This repository has been archived by the owner on Jan 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsys_dsound.c
275 lines (230 loc) · 7.94 KB
/
sys_dsound.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
/*
Faun DirectSound backend
*/
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#ifndef WINVER
#define WINVER 0x0501
#endif
#include <stdio.h>
//#include <string.h>
#include <windows.h>
#define DIRECTSOUND_VERSION 0x0800
#include <dsound.h>
// Divide the ds_buffer into three sections.
#define SEC_COUNT 3
static LPDIRECTSOUND8 dsDevice;
static char dsErrorMsg[80];
/* Voice information for DirectSound */
typedef struct {
DSBUFFERDESC desc;
WAVEFORMATEX waveFmt;
LPDIRECTSOUNDBUFFER ds_buffer;
LPDIRECTSOUNDBUFFER8 ds8_buffer;
int section;
} DSVoice;
static const char* dsoundError(const char* msg, HRESULT hr)
{
const char* estr = NULL;
switch (hr) {
case DSERR_ALLOCATED: estr = "DSERR_ALLOCATED"; break;
case DSERR_BADFORMAT: estr = "DSERR_BADFORMAT"; break;
case DSERR_BUFFERTOOSMALL: estr = "DSERR_BUFFERTOOSMALL"; break;
case DSERR_CONTROLUNAVAIL: estr = "DSERR_CONTROLUNAVAIL"; break;
case DSERR_DS8_REQUIRED: estr = "DSERR_DS8_REQUIRED"; break;
case DSERR_INVALIDCALL: estr = "DSERR_INVALIDCALL"; break;
case DSERR_INVALIDPARAM: estr = "DSERR_INVALIDPARAM"; break;
case DSERR_NOAGGREGATION: estr = "DSERR_NOAGGREGATION"; break;
case DSERR_OUTOFMEMORY: estr = "DSERR_OUTOFMEMORY"; break;
case DSERR_UNINITIALIZED: estr = "DSERR_UNINITIALIZED"; break;
case DSERR_UNSUPPORTED: estr = "DSERR_UNSUPPORTED"; break;
}
if (estr)
sprintf(dsErrorMsg, "%s (%s)", msg, estr);
else
sprintf(dsErrorMsg, "%s (%ld)", msg, hr);
return dsErrorMsg;
}
/* The open method starts up the driver and should lock the device, using the
previously set parameters, or defaults. It shouldn't need to start sending
audio data to the device yet, however. */
static const char* sysaudio_open(const char* appName)
{
HRESULT hr;
HWND win;
(void) appName;
// Use default device.
hr = DirectSoundCreate8(NULL, &dsDevice, NULL);
if (FAILED(hr))
return dsoundError("DirectSoundCreate8 failed", hr);
win = GetForegroundWindow();
if (win == NULL)
win = GetDesktopWindow();
// DSSCL_PRIORITY
hr = IDirectSound8_SetCooperativeLevel(dsDevice, win, DSSCL_NORMAL);
if (FAILED(hr))
return dsoundError("SetCooperativeLevel failed", hr);
return NULL;
}
/* The close method should close the device, freeing any resources, and allow
other processes to use the device */
static void sysaudio_close()
{
IDirectSound8_Release(dsDevice);
}
/* The allocate_voice method should grab a voice from the system, and allocate
any data common to streaming and non-streaming sources. */
static const char* sysaudio_allocVoice(FaunVoice* voice, int updateHz,
const char* appName)
{
DSVoice* ds;
HRESULT hr;
int bitsPerSample = 16;
int channels;
int bufferSize;
(void) appName;
ds = (DSVoice*) calloc(1, sizeof(DSVoice));
if (! ds)
return "DSVoice allocate failed";
voice->backend = ds;
channels = faun_channelCount(voice->mix.chanLayout);
bufferSize = (SEC_COUNT*44100/updateHz) * (bitsPerSample/8) * channels;
ds->waveFmt.wFormatTag = WAVE_FORMAT_PCM;
ds->waveFmt.nChannels = channels;
ds->waveFmt.nSamplesPerSec = voice->mix.rate;
ds->waveFmt.nBlockAlign = channels * (bitsPerSample/8);
ds->waveFmt.nAvgBytesPerSec = ds->waveFmt.nBlockAlign *
voice->mix.rate;
ds->waveFmt.wBitsPerSample = bitsPerSample;
ds->waveFmt.cbSize = 0;
ds->desc.dwSize = sizeof(DSBUFFERDESC);
ds->desc.dwFlags = /*DSBCAPS_LOCSOFTWARE |*/
DSBCAPS_GETCURRENTPOSITION2 |
DSBCAPS_GLOBALFOCUS;
ds->desc.dwBufferBytes = bufferSize;
ds->desc.dwReserved = 0;
ds->desc.lpwfxFormat = &ds->waveFmt;
ds->desc.guid3DAlgorithm = DS3DALG_DEFAULT;
hr = IDirectSound8_CreateSoundBuffer(dsDevice, &ds->desc, &ds->ds_buffer,
NULL);
if (FAILED(hr))
return dsoundError("CreateSoundBuffer failed", hr);
// NOTE: When compiled as C (rather than C++) IID_ arguments require '&'.
hr = IDirectSoundBuffer_QueryInterface(ds->ds_buffer,
&IID_IDirectSoundBuffer8,
(LPVOID*) &ds->ds8_buffer);
if (FAILED(hr))
return dsoundError("QueryInterface failed", hr);
// Fill buffer with silence.
{
LPVOID part1, part2;
DWORD len1, len2;
hr = IDirectSoundBuffer8_Lock(ds->ds8_buffer, 0, bufferSize,
&part1, &len1, &part2, &len2,
DSBLOCK_ENTIREBUFFER);
if (FAILED(hr))
return dsoundError("Buffer Lock failed", hr);
memset(part1, 0, len1);
if (len2)
memset(part2, 0, len2);
IDirectSoundBuffer8_Unlock(ds->ds8_buffer, part1, len1, part2, len2);
}
IDirectSoundBuffer8_SetVolume(ds->ds8_buffer, DSBVOLUME_MAX);
IDirectSoundBuffer8_Play(ds->ds8_buffer, 0, 0, DSBPLAY_LOOPING);
ds->section = 0;
return NULL;
}
/* The deallocate_voice method should free the resources for the given voice,
but still retain a hold on the device. The voice should be stopped and
unloaded by the time this is called */
static void sysaudio_freeVoice(FaunVoice *voice)
{
DSVoice* ds = (DSVoice*) voice->backend;
if (ds->ds8_buffer) {
IDirectSoundBuffer8_Release(ds->ds8_buffer);
ds->ds8_buffer = NULL;
}
free(ds);
voice->backend = NULL;
}
static inline void convF32_S16(int16_t* dst, int16_t* end, const float* src)
{
while (dst != end)
*dst++ = (int16_t) (*src++ * 32767.0f);
}
static const char* sysaudio_write(FaunVoice* voice, const void* data,
uint32_t len)
{
DSVoice* ds = (DSVoice*) voice->backend;
DWORD playPos = 0;
DWORD writePos;
HRESULT hr;
LPVOID part1, part2;
DWORD len1, len2;
uint32_t secBytes = ds->desc.dwBufferBytes / SEC_COUNT;
int playSection;
int wait = 0;
if (len != secBytes*2)
return "Unexpected sysaudio_write len";
do
{
if (wait)
Sleep(2);
else
++wait;
IDirectSoundBuffer8_GetCurrentPosition(ds->ds8_buffer,
&playPos, &writePos);
playSection = playPos * SEC_COUNT / ds->desc.dwBufferBytes;
}
while (playSection == ds->section);
hr = IDirectSoundBuffer8_Lock(ds->ds8_buffer, ds->section * secBytes,
secBytes, &part1, &len1, &part2, &len2, 0);
if (FAILED(hr))
return dsoundError("Buffer Lock failed", hr);
{
const float* fdat = (const float*) data;
int16_t* sp = (int16_t*) part1;
int samples1 = len1 / sizeof(int16_t);
convF32_S16(sp, sp + samples1, fdat);
if (len2) {
sp = (int16_t*) part2;
convF32_S16(sp, sp + len2 / sizeof(int16_t), fdat + samples1);
}
}
IDirectSoundBuffer8_Unlock(ds->ds8_buffer, part1, len1, part2, len2);
if (ds->section == SEC_COUNT-1)
ds->section = 0;
else
++ds->section;
return NULL;
}
static int sysaudio_startVoice(FaunVoice *voice)
{
DSVoice *ds = (DSVoice*) voice->backend;
if (ds->ds8_buffer) {
IDirectSoundBuffer8_Play(ds->ds8_buffer, 0, 0, DSBPLAY_LOOPING);
return 1;
}
return 0;
}
static int sysaudio_stopVoice(FaunVoice* voice)
{
DSVoice *ds = (DSVoice*) voice->backend;
if (ds->ds8_buffer) {
IDirectSoundBuffer8_Stop(ds->ds8_buffer);
return 1;
}
return 0;
}
/*
static int sysaudio_isPlaying(const FaunVoice *voice)
{
DSVoice *ds = (DSVoice*) voice->backend;
DWORD status;
if (! ds)
return 0;
IDirectSoundBuffer8_GetStatus(ds->ds8_buffer, &status);
return (status & DSBSTATUS_PLAYING);
}
*/