]>
Commit | Line | Data |
---|---|---|
1d14ffa9 FB |
1 | /* |
2 | * QEMU DirectSound audio driver | |
3 | * | |
4 | * Copyright (c) 2005 Vassili Karpov (malc) | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | /* | |
26 | * SEAL 1.07 by Carlos 'pel' Hasan was used as documentation | |
27 | */ | |
28 | ||
6086a565 | 29 | #include "qemu/osdep.h" |
749bc4bf | 30 | #include "audio.h" |
1d14ffa9 FB |
31 | |
32 | #define AUDIO_CAP "dsound" | |
33 | #include "audio_int.h" | |
4a3b8b34 | 34 | #include "qemu/host-utils.h" |
0b8fa32f | 35 | #include "qemu/module.h" |
1d14ffa9 FB |
36 | |
37 | #include <windows.h> | |
4fddf62a | 38 | #include <mmsystem.h> |
1d14ffa9 FB |
39 | #include <objbase.h> |
40 | #include <dsound.h> | |
41 | ||
d5631638 | 42 | #include "audio_win_int.h" |
43 | ||
1d14ffa9 FB |
44 | /* #define DEBUG_DSOUND */ |
45 | ||
1d14ffa9 FB |
46 | typedef struct { |
47 | LPDIRECTSOUND dsound; | |
48 | LPDIRECTSOUNDCAPTURE dsound_capture; | |
1ea879e5 | 49 | struct audsettings settings; |
4a3b8b34 | 50 | Audiodev *dev; |
1d14ffa9 FB |
51 | } dsound; |
52 | ||
1d14ffa9 FB |
53 | typedef struct { |
54 | HWVoiceOut hw; | |
55 | LPDIRECTSOUNDBUFFER dsound_buffer; | |
fb35c2ce | 56 | bool first_time; |
191e1f0a | 57 | dsound *s; |
1d14ffa9 FB |
58 | } DSoundVoiceOut; |
59 | ||
60 | typedef struct { | |
61 | HWVoiceIn hw; | |
1d14ffa9 | 62 | LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer; |
fb35c2ce | 63 | bool first_time; |
191e1f0a | 64 | dsound *s; |
1d14ffa9 FB |
65 | } DSoundVoiceIn; |
66 | ||
67 | static void dsound_log_hresult (HRESULT hr) | |
68 | { | |
69 | const char *str = "BUG"; | |
70 | ||
71 | switch (hr) { | |
72 | case DS_OK: | |
73 | str = "The method succeeded"; | |
74 | break; | |
75 | #ifdef DS_NO_VIRTUALIZATION | |
76 | case DS_NO_VIRTUALIZATION: | |
77 | str = "The buffer was created, but another 3D algorithm was substituted"; | |
78 | break; | |
79 | #endif | |
80 | #ifdef DS_INCOMPLETE | |
81 | case DS_INCOMPLETE: | |
82 | str = "The method succeeded, but not all the optional effects were obtained"; | |
83 | break; | |
84 | #endif | |
85 | #ifdef DSERR_ACCESSDENIED | |
86 | case DSERR_ACCESSDENIED: | |
87 | str = "The request failed because access was denied"; | |
88 | break; | |
89 | #endif | |
90 | #ifdef DSERR_ALLOCATED | |
91 | case DSERR_ALLOCATED: | |
92 | str = "The request failed because resources, such as a priority level, were already in use by another caller"; | |
93 | break; | |
94 | #endif | |
95 | #ifdef DSERR_ALREADYINITIALIZED | |
96 | case DSERR_ALREADYINITIALIZED: | |
97 | str = "The object is already initialized"; | |
98 | break; | |
99 | #endif | |
100 | #ifdef DSERR_BADFORMAT | |
101 | case DSERR_BADFORMAT: | |
102 | str = "The specified wave format is not supported"; | |
103 | break; | |
104 | #endif | |
105 | #ifdef DSERR_BADSENDBUFFERGUID | |
106 | case DSERR_BADSENDBUFFERGUID: | |
107 | str = "The GUID specified in an audiopath file does not match a valid mix-in buffer"; | |
108 | break; | |
109 | #endif | |
110 | #ifdef DSERR_BUFFERLOST | |
111 | case DSERR_BUFFERLOST: | |
112 | str = "The buffer memory has been lost and must be restored"; | |
113 | break; | |
114 | #endif | |
115 | #ifdef DSERR_BUFFERTOOSMALL | |
116 | case DSERR_BUFFERTOOSMALL: | |
117 | str = "The buffer size is not great enough to enable effects processing"; | |
118 | break; | |
119 | #endif | |
120 | #ifdef DSERR_CONTROLUNAVAIL | |
121 | case DSERR_CONTROLUNAVAIL: | |
122 | str = "The buffer control (volume, pan, and so on) requested by the caller is not available. Controls must be specified when the buffer is created, using the dwFlags member of DSBUFFERDESC"; | |
123 | break; | |
124 | #endif | |
125 | #ifdef DSERR_DS8_REQUIRED | |
126 | case DSERR_DS8_REQUIRED: | |
127 | str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface"; | |
128 | break; | |
129 | #endif | |
130 | #ifdef DSERR_FXUNAVAILABLE | |
131 | case DSERR_FXUNAVAILABLE: | |
132 | str = "The effects requested could not be found on the system, or they are in the wrong order or in the wrong location; for example, an effect expected in hardware was found in software"; | |
133 | break; | |
134 | #endif | |
135 | #ifdef DSERR_GENERIC | |
136 | case DSERR_GENERIC : | |
137 | str = "An undetermined error occurred inside the DirectSound subsystem"; | |
138 | break; | |
139 | #endif | |
140 | #ifdef DSERR_INVALIDCALL | |
141 | case DSERR_INVALIDCALL: | |
142 | str = "This function is not valid for the current state of this object"; | |
143 | break; | |
144 | #endif | |
145 | #ifdef DSERR_INVALIDPARAM | |
146 | case DSERR_INVALIDPARAM: | |
147 | str = "An invalid parameter was passed to the returning function"; | |
148 | break; | |
149 | #endif | |
150 | #ifdef DSERR_NOAGGREGATION | |
151 | case DSERR_NOAGGREGATION: | |
152 | str = "The object does not support aggregation"; | |
153 | break; | |
154 | #endif | |
155 | #ifdef DSERR_NODRIVER | |
156 | case DSERR_NODRIVER: | |
157 | str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID"; | |
158 | break; | |
159 | #endif | |
160 | #ifdef DSERR_NOINTERFACE | |
161 | case DSERR_NOINTERFACE: | |
162 | str = "The requested COM interface is not available"; | |
163 | break; | |
164 | #endif | |
165 | #ifdef DSERR_OBJECTNOTFOUND | |
166 | case DSERR_OBJECTNOTFOUND: | |
167 | str = "The requested object was not found"; | |
168 | break; | |
169 | #endif | |
170 | #ifdef DSERR_OTHERAPPHASPRIO | |
171 | case DSERR_OTHERAPPHASPRIO: | |
172 | str = "Another application has a higher priority level, preventing this call from succeeding"; | |
173 | break; | |
174 | #endif | |
175 | #ifdef DSERR_OUTOFMEMORY | |
176 | case DSERR_OUTOFMEMORY: | |
177 | str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request"; | |
178 | break; | |
179 | #endif | |
180 | #ifdef DSERR_PRIOLEVELNEEDED | |
181 | case DSERR_PRIOLEVELNEEDED: | |
182 | str = "A cooperative level of DSSCL_PRIORITY or higher is required"; | |
183 | break; | |
184 | #endif | |
185 | #ifdef DSERR_SENDLOOP | |
186 | case DSERR_SENDLOOP: | |
187 | str = "A circular loop of send effects was detected"; | |
188 | break; | |
189 | #endif | |
190 | #ifdef DSERR_UNINITIALIZED | |
191 | case DSERR_UNINITIALIZED: | |
192 | str = "The Initialize method has not been called or has not been called successfully before other methods were called"; | |
193 | break; | |
194 | #endif | |
195 | #ifdef DSERR_UNSUPPORTED | |
196 | case DSERR_UNSUPPORTED: | |
197 | str = "The function called is not supported at this time"; | |
198 | break; | |
199 | #endif | |
200 | default: | |
201 | AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr); | |
202 | return; | |
203 | } | |
204 | ||
205 | AUD_log (AUDIO_CAP, "Reason: %s\n", str); | |
206 | } | |
207 | ||
208 | static void GCC_FMT_ATTR (2, 3) dsound_logerr ( | |
209 | HRESULT hr, | |
210 | const char *fmt, | |
211 | ... | |
212 | ) | |
213 | { | |
214 | va_list ap; | |
215 | ||
216 | va_start (ap, fmt); | |
217 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
218 | va_end (ap); | |
219 | ||
220 | dsound_log_hresult (hr); | |
221 | } | |
222 | ||
223 | static void GCC_FMT_ATTR (3, 4) dsound_logerr2 ( | |
224 | HRESULT hr, | |
225 | const char *typ, | |
226 | const char *fmt, | |
227 | ... | |
228 | ) | |
229 | { | |
230 | va_list ap; | |
231 | ||
c0fe3827 | 232 | AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ); |
1d14ffa9 FB |
233 | va_start (ap, fmt); |
234 | AUD_vlog (AUDIO_CAP, fmt, ap); | |
235 | va_end (ap); | |
236 | ||
237 | dsound_log_hresult (hr); | |
238 | } | |
239 | ||
1d14ffa9 FB |
240 | #ifdef DEBUG_DSOUND |
241 | static void print_wave_format (WAVEFORMATEX *wfx) | |
242 | { | |
243 | dolog ("tag = %d\n", wfx->wFormatTag); | |
244 | dolog ("nChannels = %d\n", wfx->nChannels); | |
245 | dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec); | |
246 | dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec); | |
247 | dolog ("nBlockAlign = %d\n", wfx->nBlockAlign); | |
248 | dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample); | |
249 | dolog ("cbSize = %d\n", wfx->cbSize); | |
250 | } | |
251 | #endif | |
252 | ||
191e1f0a | 253 | static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s) |
1d14ffa9 FB |
254 | { |
255 | HRESULT hr; | |
1d14ffa9 | 256 | |
2762955f | 257 | hr = IDirectSoundBuffer_Restore (dsb); |
1d14ffa9 | 258 | |
2762955f KZ |
259 | if (hr != DS_OK) { |
260 | dsound_logerr (hr, "Could not restore playback buffer\n"); | |
261 | return -1; | |
1d14ffa9 | 262 | } |
2762955f | 263 | return 0; |
1d14ffa9 FB |
264 | } |
265 | ||
1d14ffa9 FB |
266 | #include "dsound_template.h" |
267 | #define DSBTYPE_IN | |
268 | #include "dsound_template.h" | |
269 | #undef DSBTYPE_IN | |
270 | ||
191e1f0a KZ |
271 | static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp, |
272 | dsound *s) | |
1d14ffa9 FB |
273 | { |
274 | HRESULT hr; | |
1d14ffa9 | 275 | |
2762955f KZ |
276 | hr = IDirectSoundBuffer_GetStatus (dsb, statusp); |
277 | if (FAILED (hr)) { | |
278 | dsound_logerr (hr, "Could not get playback buffer status\n"); | |
279 | return -1; | |
280 | } | |
1d14ffa9 | 281 | |
4ba664cb | 282 | if (*statusp & DSBSTATUS_BUFFERLOST) { |
2762955f KZ |
283 | dsound_restore_out(dsb, s); |
284 | return -1; | |
1d14ffa9 FB |
285 | } |
286 | ||
287 | return 0; | |
288 | } | |
289 | ||
290 | static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb, | |
291 | DWORD *statusp) | |
292 | { | |
293 | HRESULT hr; | |
294 | ||
295 | hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp); | |
296 | if (FAILED (hr)) { | |
c0fe3827 | 297 | dsound_logerr (hr, "Could not get capture buffer status\n"); |
1d14ffa9 FB |
298 | return -1; |
299 | } | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
191e1f0a KZ |
304 | static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb, |
305 | dsound *s) | |
1d14ffa9 FB |
306 | { |
307 | int err; | |
308 | LPVOID p1, p2; | |
309 | DWORD blen1, blen2, len1, len2; | |
310 | ||
311 | err = dsound_lock_out ( | |
312 | dsb, | |
313 | &hw->info, | |
314 | 0, | |
7fa9754a | 315 | hw->size_emul, |
1d14ffa9 FB |
316 | &p1, &p2, |
317 | &blen1, &blen2, | |
191e1f0a KZ |
318 | 1, |
319 | s | |
1d14ffa9 FB |
320 | ); |
321 | if (err) { | |
322 | return; | |
323 | } | |
324 | ||
2b9cce8c KZ |
325 | len1 = blen1 / hw->info.bytes_per_frame; |
326 | len2 = blen2 / hw->info.bytes_per_frame; | |
1d14ffa9 FB |
327 | |
328 | #ifdef DEBUG_DSOUND | |
329 | dolog ("clear %p,%ld,%ld %p,%ld,%ld\n", | |
330 | p1, blen1, len1, | |
331 | p2, blen2, len2); | |
332 | #endif | |
333 | ||
334 | if (p1 && len1) { | |
335 | audio_pcm_info_clear_buf (&hw->info, p1, len1); | |
336 | } | |
337 | ||
338 | if (p2 && len2) { | |
339 | audio_pcm_info_clear_buf (&hw->info, p2, len2); | |
340 | } | |
341 | ||
342 | dsound_unlock_out (dsb, p1, p2, blen1, blen2); | |
343 | } | |
344 | ||
1d14ffa9 FB |
345 | static int dsound_open (dsound *s) |
346 | { | |
1d14ffa9 | 347 | HRESULT hr; |
1d14ffa9 FB |
348 | HWND hwnd; |
349 | ||
350 | hwnd = GetForegroundWindow (); | |
351 | hr = IDirectSound_SetCooperativeLevel ( | |
352 | s->dsound, | |
353 | hwnd, | |
354 | DSSCL_PRIORITY | |
355 | ); | |
356 | ||
357 | if (FAILED (hr)) { | |
c0fe3827 | 358 | dsound_logerr (hr, "Could not set cooperative level for window %p\n", |
1d14ffa9 FB |
359 | hwnd); |
360 | return -1; | |
361 | } | |
362 | ||
1d14ffa9 | 363 | return 0; |
1d14ffa9 FB |
364 | } |
365 | ||
571a8c52 | 366 | static void dsound_enable_out(HWVoiceOut *hw, bool enable) |
1d14ffa9 FB |
367 | { |
368 | HRESULT hr; | |
369 | DWORD status; | |
370 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; | |
371 | LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; | |
191e1f0a | 372 | dsound *s = ds->s; |
1d14ffa9 FB |
373 | |
374 | if (!dsb) { | |
375 | dolog ("Attempt to control voice without a buffer\n"); | |
571a8c52 | 376 | return; |
1d14ffa9 FB |
377 | } |
378 | ||
571a8c52 | 379 | if (enable) { |
191e1f0a | 380 | if (dsound_get_status_out (dsb, &status, s)) { |
571a8c52 | 381 | return; |
1d14ffa9 FB |
382 | } |
383 | ||
384 | if (status & DSBSTATUS_PLAYING) { | |
c0fe3827 | 385 | dolog ("warning: Voice is already playing\n"); |
571a8c52 | 386 | return; |
1d14ffa9 FB |
387 | } |
388 | ||
191e1f0a | 389 | dsound_clear_sample (hw, dsb, s); |
1d14ffa9 FB |
390 | |
391 | hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING); | |
392 | if (FAILED (hr)) { | |
c0fe3827 | 393 | dsound_logerr (hr, "Could not start playing buffer\n"); |
571a8c52 | 394 | return; |
1d14ffa9 | 395 | } |
571a8c52 | 396 | } else { |
191e1f0a | 397 | if (dsound_get_status_out (dsb, &status, s)) { |
571a8c52 | 398 | return; |
1d14ffa9 FB |
399 | } |
400 | ||
401 | if (status & DSBSTATUS_PLAYING) { | |
402 | hr = IDirectSoundBuffer_Stop (dsb); | |
403 | if (FAILED (hr)) { | |
c0fe3827 | 404 | dsound_logerr (hr, "Could not stop playing buffer\n"); |
571a8c52 | 405 | return; |
1d14ffa9 FB |
406 | } |
407 | } | |
408 | else { | |
c0fe3827 | 409 | dolog ("warning: Voice is not playing\n"); |
1d14ffa9 | 410 | } |
1d14ffa9 | 411 | } |
1d14ffa9 FB |
412 | } |
413 | ||
7fa9754a | 414 | static void *dsound_get_buffer_out(HWVoiceOut *hw, size_t *size) |
1d14ffa9 | 415 | { |
1d14ffa9 FB |
416 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; |
417 | LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; | |
7fa9754a | 418 | HRESULT hr; |
fb35c2ce | 419 | DWORD ppos, wpos, act_size; |
7fa9754a KZ |
420 | size_t req_size; |
421 | int err; | |
422 | void *ret; | |
1d14ffa9 | 423 | |
fb35c2ce KZ |
424 | hr = IDirectSoundBuffer_GetCurrentPosition( |
425 | dsb, &ppos, ds->first_time ? &wpos : NULL); | |
7fa9754a KZ |
426 | if (FAILED(hr)) { |
427 | dsound_logerr(hr, "Could not get playback buffer position\n"); | |
428 | *size = 0; | |
429 | return NULL; | |
1d14ffa9 | 430 | } |
1d14ffa9 | 431 | |
fb35c2ce KZ |
432 | if (ds->first_time) { |
433 | hw->pos_emul = wpos; | |
434 | ds->first_time = false; | |
435 | } | |
436 | ||
7fa9754a KZ |
437 | req_size = audio_ring_dist(ppos, hw->pos_emul, hw->size_emul); |
438 | req_size = MIN(req_size, hw->size_emul - hw->pos_emul); | |
1d14ffa9 | 439 | |
fb35c2ce KZ |
440 | if (req_size == 0) { |
441 | *size = 0; | |
442 | return NULL; | |
443 | } | |
444 | ||
7fa9754a KZ |
445 | err = dsound_lock_out(dsb, &hw->info, hw->pos_emul, req_size, &ret, NULL, |
446 | &act_size, NULL, false, ds->s); | |
447 | if (err) { | |
448 | dolog("Failed to lock buffer\n"); | |
449 | *size = 0; | |
450 | return NULL; | |
1d14ffa9 FB |
451 | } |
452 | ||
7fa9754a KZ |
453 | *size = act_size; |
454 | return ret; | |
455 | } | |
1d14ffa9 | 456 | |
7fa9754a KZ |
457 | static size_t dsound_put_buffer_out(HWVoiceOut *hw, void *buf, size_t len) |
458 | { | |
459 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; | |
460 | LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer; | |
461 | int err = dsound_unlock_out(dsb, buf, NULL, len, 0); | |
1d14ffa9 | 462 | |
1d14ffa9 | 463 | if (err) { |
7fa9754a | 464 | dolog("Failed to unlock buffer!!\n"); |
1d14ffa9 FB |
465 | return 0; |
466 | } | |
7fa9754a | 467 | hw->pos_emul = (hw->pos_emul + len) % hw->size_emul; |
1d14ffa9 | 468 | |
7fa9754a | 469 | return len; |
1d14ffa9 FB |
470 | } |
471 | ||
571a8c52 | 472 | static void dsound_enable_in(HWVoiceIn *hw, bool enable) |
1d14ffa9 FB |
473 | { |
474 | HRESULT hr; | |
475 | DWORD status; | |
476 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; | |
477 | LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; | |
478 | ||
479 | if (!dscb) { | |
480 | dolog ("Attempt to control capture voice without a buffer\n"); | |
571a8c52 | 481 | return; |
1d14ffa9 FB |
482 | } |
483 | ||
571a8c52 | 484 | if (enable) { |
1d14ffa9 | 485 | if (dsound_get_status_in (dscb, &status)) { |
571a8c52 | 486 | return; |
1d14ffa9 FB |
487 | } |
488 | ||
489 | if (status & DSCBSTATUS_CAPTURING) { | |
c0fe3827 | 490 | dolog ("warning: Voice is already capturing\n"); |
571a8c52 | 491 | return; |
1d14ffa9 FB |
492 | } |
493 | ||
494 | /* clear ?? */ | |
495 | ||
496 | hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING); | |
497 | if (FAILED (hr)) { | |
c0fe3827 | 498 | dsound_logerr (hr, "Could not start capturing\n"); |
571a8c52 | 499 | return; |
1d14ffa9 | 500 | } |
571a8c52 | 501 | } else { |
1d14ffa9 | 502 | if (dsound_get_status_in (dscb, &status)) { |
571a8c52 | 503 | return; |
1d14ffa9 FB |
504 | } |
505 | ||
506 | if (status & DSCBSTATUS_CAPTURING) { | |
507 | hr = IDirectSoundCaptureBuffer_Stop (dscb); | |
508 | if (FAILED (hr)) { | |
c0fe3827 | 509 | dsound_logerr (hr, "Could not stop capturing\n"); |
571a8c52 | 510 | return; |
1d14ffa9 FB |
511 | } |
512 | } | |
513 | else { | |
c0fe3827 | 514 | dolog ("warning: Voice is not capturing\n"); |
1d14ffa9 | 515 | } |
1d14ffa9 | 516 | } |
1d14ffa9 FB |
517 | } |
518 | ||
7fa9754a | 519 | static void *dsound_get_buffer_in(HWVoiceIn *hw, size_t *size) |
1d14ffa9 | 520 | { |
1d14ffa9 FB |
521 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; |
522 | LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; | |
7fa9754a | 523 | HRESULT hr; |
fb35c2ce | 524 | DWORD cpos, rpos, act_size; |
7fa9754a KZ |
525 | size_t req_size; |
526 | int err; | |
527 | void *ret; | |
1d14ffa9 | 528 | |
fb35c2ce KZ |
529 | hr = IDirectSoundCaptureBuffer_GetCurrentPosition( |
530 | dscb, &cpos, ds->first_time ? &rpos : NULL); | |
7fa9754a KZ |
531 | if (FAILED(hr)) { |
532 | dsound_logerr(hr, "Could not get capture buffer position\n"); | |
533 | *size = 0; | |
534 | return NULL; | |
1d14ffa9 | 535 | } |
1d14ffa9 | 536 | |
fb35c2ce KZ |
537 | if (ds->first_time) { |
538 | hw->pos_emul = rpos; | |
539 | ds->first_time = false; | |
540 | } | |
541 | ||
7fa9754a | 542 | req_size = audio_ring_dist(cpos, hw->pos_emul, hw->size_emul); |
8d1439b6 | 543 | req_size = MIN(*size, MIN(req_size, hw->size_emul - hw->pos_emul)); |
1d14ffa9 | 544 | |
17470298 VR |
545 | if (req_size == 0) { |
546 | *size = 0; | |
547 | return NULL; | |
548 | } | |
549 | ||
7fa9754a KZ |
550 | err = dsound_lock_in(dscb, &hw->info, hw->pos_emul, req_size, &ret, NULL, |
551 | &act_size, NULL, false, ds->s); | |
1d14ffa9 | 552 | if (err) { |
7fa9754a KZ |
553 | dolog("Failed to lock buffer\n"); |
554 | *size = 0; | |
555 | return NULL; | |
1d14ffa9 FB |
556 | } |
557 | ||
7fa9754a KZ |
558 | *size = act_size; |
559 | return ret; | |
560 | } | |
1d14ffa9 | 561 | |
7fa9754a KZ |
562 | static void dsound_put_buffer_in(HWVoiceIn *hw, void *buf, size_t len) |
563 | { | |
564 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; | |
565 | LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer; | |
566 | int err = dsound_unlock_in(dscb, buf, NULL, len, 0); | |
1d14ffa9 | 567 | |
7fa9754a KZ |
568 | if (err) { |
569 | dolog("Failed to unlock buffer!!\n"); | |
570 | return; | |
1d14ffa9 | 571 | } |
7fa9754a | 572 | hw->pos_emul = (hw->pos_emul + len) % hw->size_emul; |
1d14ffa9 FB |
573 | } |
574 | ||
575 | static void dsound_audio_fini (void *opaque) | |
576 | { | |
577 | HRESULT hr; | |
578 | dsound *s = opaque; | |
579 | ||
580 | if (!s->dsound) { | |
191e1f0a | 581 | g_free(s); |
1d14ffa9 FB |
582 | return; |
583 | } | |
584 | ||
585 | hr = IDirectSound_Release (s->dsound); | |
586 | if (FAILED (hr)) { | |
c0fe3827 | 587 | dsound_logerr (hr, "Could not release DirectSound\n"); |
1d14ffa9 FB |
588 | } |
589 | s->dsound = NULL; | |
590 | ||
591 | if (!s->dsound_capture) { | |
191e1f0a | 592 | g_free(s); |
1d14ffa9 FB |
593 | return; |
594 | } | |
595 | ||
596 | hr = IDirectSoundCapture_Release (s->dsound_capture); | |
597 | if (FAILED (hr)) { | |
c0fe3827 | 598 | dsound_logerr (hr, "Could not release DirectSoundCapture\n"); |
1d14ffa9 FB |
599 | } |
600 | s->dsound_capture = NULL; | |
191e1f0a KZ |
601 | |
602 | g_free(s); | |
1d14ffa9 FB |
603 | } |
604 | ||
71830221 | 605 | static void *dsound_audio_init(Audiodev *dev) |
1d14ffa9 FB |
606 | { |
607 | int err; | |
608 | HRESULT hr; | |
191e1f0a | 609 | dsound *s = g_malloc0(sizeof(dsound)); |
4a3b8b34 KZ |
610 | AudiodevDsoundOptions *dso; |
611 | ||
612 | assert(dev->driver == AUDIODEV_DRIVER_DSOUND); | |
613 | s->dev = dev; | |
614 | dso = &dev->u.dsound; | |
615 | ||
616 | if (!dso->has_latency) { | |
617 | dso->has_latency = true; | |
618 | dso->latency = 10000; /* 10 ms */ | |
619 | } | |
1d14ffa9 FB |
620 | |
621 | hr = CoInitialize (NULL); | |
622 | if (FAILED (hr)) { | |
c0fe3827 | 623 | dsound_logerr (hr, "Could not initialize COM\n"); |
191e1f0a | 624 | g_free(s); |
1d14ffa9 FB |
625 | return NULL; |
626 | } | |
627 | ||
628 | hr = CoCreateInstance ( | |
629 | &CLSID_DirectSound, | |
630 | NULL, | |
631 | CLSCTX_ALL, | |
632 | &IID_IDirectSound, | |
633 | (void **) &s->dsound | |
634 | ); | |
635 | if (FAILED (hr)) { | |
c0fe3827 | 636 | dsound_logerr (hr, "Could not create DirectSound instance\n"); |
191e1f0a | 637 | g_free(s); |
1d14ffa9 FB |
638 | return NULL; |
639 | } | |
640 | ||
641 | hr = IDirectSound_Initialize (s->dsound, NULL); | |
642 | if (FAILED (hr)) { | |
c0fe3827 | 643 | dsound_logerr (hr, "Could not initialize DirectSound\n"); |
8ead62cf FB |
644 | |
645 | hr = IDirectSound_Release (s->dsound); | |
646 | if (FAILED (hr)) { | |
647 | dsound_logerr (hr, "Could not release DirectSound\n"); | |
648 | } | |
191e1f0a | 649 | g_free(s); |
1d14ffa9 FB |
650 | return NULL; |
651 | } | |
652 | ||
653 | hr = CoCreateInstance ( | |
654 | &CLSID_DirectSoundCapture, | |
655 | NULL, | |
656 | CLSCTX_ALL, | |
657 | &IID_IDirectSoundCapture, | |
658 | (void **) &s->dsound_capture | |
659 | ); | |
660 | if (FAILED (hr)) { | |
c0fe3827 | 661 | dsound_logerr (hr, "Could not create DirectSoundCapture instance\n"); |
1d14ffa9 FB |
662 | } |
663 | else { | |
664 | hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL); | |
665 | if (FAILED (hr)) { | |
c0fe3827 | 666 | dsound_logerr (hr, "Could not initialize DirectSoundCapture\n"); |
1d14ffa9 FB |
667 | |
668 | hr = IDirectSoundCapture_Release (s->dsound_capture); | |
669 | if (FAILED (hr)) { | |
c0fe3827 | 670 | dsound_logerr (hr, "Could not release DirectSoundCapture\n"); |
1d14ffa9 FB |
671 | } |
672 | s->dsound_capture = NULL; | |
673 | } | |
674 | } | |
675 | ||
676 | err = dsound_open (s); | |
677 | if (err) { | |
678 | dsound_audio_fini (s); | |
679 | return NULL; | |
680 | } | |
681 | ||
682 | return s; | |
683 | } | |
684 | ||
35f4b58c | 685 | static struct audio_pcm_ops dsound_pcm_ops = { |
1dd3e4d1 JQ |
686 | .init_out = dsound_init_out, |
687 | .fini_out = dsound_fini_out, | |
7fa9754a KZ |
688 | .write = audio_generic_write, |
689 | .get_buffer_out = dsound_get_buffer_out, | |
690 | .put_buffer_out = dsound_put_buffer_out, | |
571a8c52 | 691 | .enable_out = dsound_enable_out, |
1dd3e4d1 JQ |
692 | |
693 | .init_in = dsound_init_in, | |
694 | .fini_in = dsound_fini_in, | |
7fa9754a KZ |
695 | .read = audio_generic_read, |
696 | .get_buffer_in = dsound_get_buffer_in, | |
697 | .put_buffer_in = dsound_put_buffer_in, | |
571a8c52 | 698 | .enable_in = dsound_enable_in, |
1d14ffa9 FB |
699 | }; |
700 | ||
d3893a39 | 701 | static struct audio_driver dsound_audio_driver = { |
bee37f32 JQ |
702 | .name = "dsound", |
703 | .descr = "DirectSound http://wikipedia.org/wiki/DirectSound", | |
bee37f32 JQ |
704 | .init = dsound_audio_init, |
705 | .fini = dsound_audio_fini, | |
706 | .pcm_ops = &dsound_pcm_ops, | |
707 | .can_be_default = 1, | |
708 | .max_voices_out = INT_MAX, | |
709 | .max_voices_in = 1, | |
710 | .voice_size_out = sizeof (DSoundVoiceOut), | |
15c875a3 | 711 | .voice_size_in = sizeof (DSoundVoiceIn) |
1d14ffa9 | 712 | }; |
d3893a39 GH |
713 | |
714 | static void register_audio_dsound(void) | |
715 | { | |
716 | audio_driver_register(&dsound_audio_driver); | |
717 | } | |
718 | type_init(register_audio_dsound); |