VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/dsoundaudio.c@ 980

Last change on this file since 980 was 366, checked in by vboxsync, 18 years ago

made mixing of pcm_in work

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 26.8 KB
Line 
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
29#define _WIN32_DCOM
30#include <windows.h>
31#include <objbase.h>
32#include <dsound.h>
33
34#include "Builtins.h"
35#include "../../vl_vbox.h"
36#include "audio.h"
37#include <iprt/alloc.h>
38
39#define AUDIO_CAP "dsound"
40#include "audio_int.h"
41
42/* #define DEBUG_DSOUND */
43
44static struct {
45 int lock_retries;
46 int restore_retries;
47 int getstatus_retries;
48 int set_primary;
49 int bufsize_in;
50 int bufsize_out;
51 audsettings_t settings;
52 int latency_millis;
53} conf = {
54 1,
55 1,
56 1,
57 0,
58 16384,
59 16384,
60 {
61 44100,
62 2,
63 AUD_FMT_S16
64 },
65 10
66};
67
68typedef struct {
69 LPDIRECTSOUND dsound;
70 LPDIRECTSOUNDCAPTURE dsound_capture;
71 LPDIRECTSOUNDBUFFER dsound_primary_buffer;
72 audsettings_t settings;
73} dsound;
74
75static dsound glob_dsound;
76
77typedef struct {
78 HWVoiceOut hw;
79 LPDIRECTSOUNDBUFFER dsound_buffer;
80 DWORD old_pos;
81 int first_time;
82#ifdef DEBUG_DSOUND
83 DWORD old_ppos;
84 DWORD played;
85 DWORD mixed;
86#endif
87} DSoundVoiceOut;
88
89typedef struct {
90 HWVoiceIn hw;
91 int first_time;
92 LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
93} DSoundVoiceIn;
94
95static void dsound_log_hresult (HRESULT hr)
96{
97 const char *str = "BUG";
98
99 switch (hr) {
100 case DS_OK:
101 str = "The method succeeded";
102 break;
103#ifdef DS_NO_VIRTUALIZATION
104 case DS_NO_VIRTUALIZATION:
105 str = "The buffer was created, but another 3D algorithm was substituted";
106 break;
107#endif
108#ifdef DS_INCOMPLETE
109 case DS_INCOMPLETE:
110 str = "The method succeeded, but not all the optional effects were obtained";
111 break;
112#endif
113#ifdef DSERR_ACCESSDENIED
114 case DSERR_ACCESSDENIED:
115 str = "The request failed because access was denied";
116 break;
117#endif
118#ifdef DSERR_ALLOCATED
119 case DSERR_ALLOCATED:
120 str = "The request failed because resources, such as a priority level, were already in use by another caller";
121 break;
122#endif
123#ifdef DSERR_ALREADYINITIALIZED
124 case DSERR_ALREADYINITIALIZED:
125 str = "The object is already initialized";
126 break;
127#endif
128#ifdef DSERR_BADFORMAT
129 case DSERR_BADFORMAT:
130 str = "The specified wave format is not supported";
131 break;
132#endif
133#ifdef DSERR_BADSENDBUFFERGUID
134 case DSERR_BADSENDBUFFERGUID:
135 str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
136 break;
137#endif
138#ifdef DSERR_BUFFERLOST
139 case DSERR_BUFFERLOST:
140 str = "The buffer memory has been lost and must be restored";
141 break;
142#endif
143#ifdef DSERR_BUFFERTOOSMALL
144 case DSERR_BUFFERTOOSMALL:
145 str = "The buffer size is not great enough to enable effects processing";
146 break;
147#endif
148#ifdef DSERR_CONTROLUNAVAIL
149 case DSERR_CONTROLUNAVAIL:
150 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";
151 break;
152#endif
153#ifdef DSERR_DS8_REQUIRED
154 case DSERR_DS8_REQUIRED:
155 str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
156 break;
157#endif
158#ifdef DSERR_FXUNAVAILABLE
159 case DSERR_FXUNAVAILABLE:
160 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";
161 break;
162#endif
163#ifdef DSERR_GENERIC
164 case DSERR_GENERIC :
165 str = "An undetermined error occurred inside the DirectSound subsystem";
166 break;
167#endif
168#ifdef DSERR_INVALIDCALL
169 case DSERR_INVALIDCALL:
170 str = "This function is not valid for the current state of this object";
171 break;
172#endif
173#ifdef DSERR_INVALIDPARAM
174 case DSERR_INVALIDPARAM:
175 str = "An invalid parameter was passed to the returning function";
176 break;
177#endif
178#ifdef DSERR_NOAGGREGATION
179 case DSERR_NOAGGREGATION:
180 str = "The object does not support aggregation";
181 break;
182#endif
183#ifdef DSERR_NODRIVER
184 case DSERR_NODRIVER:
185 str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
186 break;
187#endif
188#ifdef DSERR_NOINTERFACE
189 case DSERR_NOINTERFACE:
190 str = "The requested COM interface is not available";
191 break;
192#endif
193#ifdef DSERR_OBJECTNOTFOUND
194 case DSERR_OBJECTNOTFOUND:
195 str = "The requested object was not found";
196 break;
197#endif
198#ifdef DSERR_OTHERAPPHASPRIO
199 case DSERR_OTHERAPPHASPRIO:
200 str = "Another application has a higher priority level, preventing this call from succeeding";
201 break;
202#endif
203#ifdef DSERR_OUTOFMEMORY
204 case DSERR_OUTOFMEMORY:
205 str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
206 break;
207#endif
208#ifdef DSERR_PRIOLEVELNEEDED
209 case DSERR_PRIOLEVELNEEDED:
210 str = "A cooperative level of DSSCL_PRIORITY or higher is required";
211 break;
212#endif
213#ifdef DSERR_SENDLOOP
214 case DSERR_SENDLOOP:
215 str = "A circular loop of send effects was detected";
216 break;
217#endif
218#ifdef DSERR_UNINITIALIZED
219 case DSERR_UNINITIALIZED:
220 str = "The Initialize method has not been called or has not been called successfully before other methods were called";
221 break;
222#endif
223#ifdef DSERR_UNSUPPORTED
224 case DSERR_UNSUPPORTED:
225 str = "The function called is not supported at this time";
226 break;
227#endif
228 default:
229 AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr);
230 return;
231 }
232
233 AUD_log (AUDIO_CAP, "Reason: %s\n", str);
234}
235
236static void GCC_FMT_ATTR (2, 3) dsound_logerr (
237 HRESULT hr,
238 const char *fmt,
239 ...
240 )
241{
242 va_list ap;
243
244 va_start (ap, fmt);
245 AUD_vlog (AUDIO_CAP, fmt, ap);
246 va_end (ap);
247
248 dsound_log_hresult (hr);
249}
250
251static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
252 HRESULT hr,
253 const char *typ,
254 const char *fmt,
255 ...
256 )
257{
258 va_list ap;
259
260 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
261 va_start (ap, fmt);
262 AUD_vlog (AUDIO_CAP, fmt, ap);
263 va_end (ap);
264
265 dsound_log_hresult (hr);
266}
267
268static DWORD millis_to_bytes (struct audio_pcm_info *info, DWORD millis)
269{
270 return (millis * info->bytes_per_second) / 1000;
271}
272
273#ifdef DEBUG_DSOUND
274static void print_wave_format (WAVEFORMATEX *wfx)
275{
276 dolog ("tag = %d\n", wfx->wFormatTag);
277 dolog ("nChannels = %d\n", wfx->nChannels);
278 dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec);
279 dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
280 dolog ("nBlockAlign = %d\n", wfx->nBlockAlign);
281 dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample);
282 dolog ("cbSize = %d\n", wfx->cbSize);
283}
284#endif
285
286static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb)
287{
288 HRESULT hr;
289 int i;
290
291 for (i = 0; i < conf.restore_retries; ++i) {
292 hr = IDirectSoundBuffer_Restore (dsb);
293
294 switch (hr) {
295 case DS_OK:
296 return 0;
297
298 case DSERR_BUFFERLOST:
299 continue;
300
301 default:
302 dsound_logerr (hr, "Could not restore playback buffer\n");
303 return -1;
304 }
305 }
306
307 dolog ("%d attempts to restore playback buffer failed\n", i);
308 return -1;
309}
310
311static int waveformat_from_audio_settings (WAVEFORMATEX *wfx, audsettings_t *as)
312{
313 memset (wfx, 0, sizeof (*wfx));
314
315 wfx->wFormatTag = WAVE_FORMAT_PCM;
316 wfx->nChannels = as->nchannels;
317 wfx->nSamplesPerSec = as->freq;
318 wfx->nAvgBytesPerSec = as->freq << (as->nchannels == 2);
319 wfx->nBlockAlign = 1 << (as->nchannels == 2);
320 wfx->cbSize = 0;
321
322 switch (as->fmt) {
323 case AUD_FMT_S8:
324 wfx->wBitsPerSample = 8;
325 break;
326
327 case AUD_FMT_U8:
328 wfx->wBitsPerSample = 8;
329 break;
330
331 case AUD_FMT_S16:
332 wfx->wBitsPerSample = 16;
333 wfx->nAvgBytesPerSec <<= 1;
334 wfx->nBlockAlign <<= 1;
335 break;
336
337 case AUD_FMT_U16:
338 wfx->wBitsPerSample = 16;
339 wfx->nAvgBytesPerSec <<= 1;
340 wfx->nBlockAlign <<= 1;
341 break;
342
343 default:
344 dolog ("Internal logic error: Bad audio format %d\n", as->freq);
345 return -1;
346 }
347
348 return 0;
349}
350
351static int waveformat_to_audio_settings (WAVEFORMATEX *wfx, audsettings_t *as)
352{
353 if (wfx->wFormatTag != WAVE_FORMAT_PCM) {
354 dolog ("Invalid wave format, tag is not PCM, but %d\n",
355 wfx->wFormatTag);
356 return -1;
357 }
358
359 if (!wfx->nSamplesPerSec) {
360 dolog ("Invalid wave format, frequency is zero\n");
361 return -1;
362 }
363 as->freq = wfx->nSamplesPerSec;
364
365 switch (wfx->nChannels) {
366 case 1:
367 as->nchannels = 1;
368 break;
369
370 case 2:
371 as->nchannels = 2;
372 break;
373
374 default:
375 dolog (
376 "Invalid wave format, number of channels is not 1 or 2, but %d\n",
377 wfx->nChannels
378 );
379 return -1;
380 }
381
382 switch (wfx->wBitsPerSample) {
383 case 8:
384 as->fmt = AUD_FMT_U8;
385 break;
386
387 case 16:
388 as->fmt = AUD_FMT_S16;
389 break;
390
391 default:
392 dolog ("Invalid wave format, bits per sample is not 8 or 16, but %d\n",
393 wfx->wBitsPerSample);
394 return -1;
395 }
396
397 return 0;
398}
399
400#include "dsound_template.h"
401#define DSBTYPE_IN
402#include "dsound_template.h"
403#undef DSBTYPE_IN
404
405static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp)
406{
407 HRESULT hr;
408 int i;
409
410 for (i = 0; i < conf.getstatus_retries; ++i) {
411 hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
412 if (FAILED (hr)) {
413 dsound_logerr (hr, "Could not get playback buffer status\n");
414 return -1;
415 }
416
417 if (*statusp & DSERR_BUFFERLOST) {
418 if (dsound_restore_out (dsb)) {
419 return -1;
420 }
421 continue;
422 }
423 break;
424 }
425
426 return 0;
427}
428
429static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
430 DWORD *statusp)
431{
432 HRESULT hr;
433
434 hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
435 if (FAILED (hr)) {
436 dsound_logerr (hr, "Could not get capture buffer status\n");
437 return -1;
438 }
439
440 return 0;
441}
442
443static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
444{
445 int src_len1 = dst_len;
446 int src_len2 = 0;
447 int pos = hw->rpos + dst_len;
448 st_sample_t *src1 = hw->mix_buf + hw->rpos;
449 st_sample_t *src2 = NULL;
450
451 if (pos > hw->samples) {
452 src_len1 = hw->samples - hw->rpos;
453 src2 = hw->mix_buf;
454 src_len2 = dst_len - src_len1;
455 pos = src_len2;
456 }
457
458 if (src_len1) {
459 hw->clip (dst, src1, src_len1);
460// mixeng_sniff_and_clear (hw, src1, dst, src_len1);
461 }
462
463 if (src_len2) {
464 dst = advance (dst, src_len1 << hw->info.shift);
465 hw->clip (dst, src2, src_len2);
466// mixeng_sniff_and_clear (hw, src2, dst, src_len2);
467 }
468
469 hw->rpos = pos % hw->samples;
470}
471
472static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb)
473{
474 int err;
475 LPVOID p1, p2;
476 DWORD blen1, blen2, len1, len2;
477
478 err = dsound_lock_out (
479 dsb,
480 &hw->info,
481 0,
482 hw->samples << hw->info.shift,
483 &p1, &p2,
484 &blen1, &blen2,
485 1
486 );
487 if (err) {
488 return;
489 }
490
491 len1 = blen1 >> hw->info.shift;
492 len2 = blen2 >> hw->info.shift;
493
494#ifdef DEBUG_DSOUND
495 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
496 p1, blen1, len1,
497 p2, blen2, len2);
498#endif
499
500 if (p1 && len1) {
501 audio_pcm_info_clear_buf (&hw->info, p1, len1);
502 }
503
504 if (p2 && len2) {
505 audio_pcm_info_clear_buf (&hw->info, p2, len2);
506 }
507
508 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
509}
510
511static void dsound_close (dsound *s)
512{
513 HRESULT hr;
514
515 if (s->dsound_primary_buffer) {
516 hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer);
517 if (FAILED (hr)) {
518 dsound_logerr (hr, "Could not release primary buffer\n");
519 }
520 s->dsound_primary_buffer = NULL;
521 }
522}
523
524static int dsound_open (dsound *s)
525{
526 int err;
527 HRESULT hr;
528 WAVEFORMATEX wfx;
529 DSBUFFERDESC dsbd;
530 HWND hwnd;
531
532 hwnd = GetForegroundWindow ();
533 hr = IDirectSound_SetCooperativeLevel (
534 s->dsound,
535 hwnd,
536 DSSCL_PRIORITY
537 );
538
539 if (FAILED (hr)) {
540 dsound_logerr (hr, "Could not set cooperative level for window %p\n",
541 hwnd);
542 return -1;
543 }
544
545 if (!conf.set_primary) {
546 return 0;
547 }
548
549 err = waveformat_from_audio_settings (&wfx, &conf.settings);
550 if (err) {
551 return -1;
552 }
553
554 memset (&dsbd, 0, sizeof (dsbd));
555 dsbd.dwSize = sizeof (dsbd);
556 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
557 dsbd.dwBufferBytes = 0;
558 dsbd.lpwfxFormat = NULL;
559
560 hr = IDirectSound_CreateSoundBuffer (
561 s->dsound,
562 &dsbd,
563 &s->dsound_primary_buffer,
564 NULL
565 );
566 if (FAILED (hr)) {
567 dsound_logerr (hr, "Could not create primary playback buffer\n");
568 return -1;
569 }
570
571 hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx);
572 if (FAILED (hr)) {
573 dsound_logerr (hr, "Could not set primary playback buffer format\n");
574 }
575
576 hr = IDirectSoundBuffer_GetFormat (
577 s->dsound_primary_buffer,
578 &wfx,
579 sizeof (wfx),
580 NULL
581 );
582 if (FAILED (hr)) {
583 dsound_logerr (hr, "Could not get primary playback buffer format\n");
584 goto fail0;
585 }
586
587#ifdef DEBUG_DSOUND
588 dolog ("Primary\n");
589 print_wave_format (&wfx);
590#endif
591
592 err = waveformat_to_audio_settings (&wfx, &s->settings);
593 if (err) {
594 goto fail0;
595 }
596
597 return 0;
598
599 fail0:
600 dsound_close (s);
601 return -1;
602}
603
604static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
605{
606 HRESULT hr;
607 DWORD status;
608 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
609 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
610
611 if (!dsb) {
612 dolog ("Attempt to control voice without a buffer\n");
613 return 0;
614 }
615
616 switch (cmd) {
617 case VOICE_ENABLE:
618 if (dsound_get_status_out (dsb, &status)) {
619 return -1;
620 }
621
622 if (status & DSBSTATUS_PLAYING) {
623 dolog ("warning: Voice is already playing\n");
624 return 0;
625 }
626
627 dsound_clear_sample (hw, dsb);
628
629 hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
630 if (FAILED (hr)) {
631 dsound_logerr (hr, "Could not start playing buffer\n");
632 return -1;
633 }
634 break;
635
636 case VOICE_DISABLE:
637 if (dsound_get_status_out (dsb, &status)) {
638 return -1;
639 }
640
641 if (status & DSBSTATUS_PLAYING) {
642 hr = IDirectSoundBuffer_Stop (dsb);
643 if (FAILED (hr)) {
644 dsound_logerr (hr, "Could not stop playing buffer\n");
645 return -1;
646 }
647 }
648 else {
649 dolog ("warning: Voice is not playing\n");
650 }
651 break;
652 }
653 return 0;
654}
655
656static int dsound_write (SWVoiceOut *sw, void *buf, int len)
657{
658 return audio_pcm_sw_write (sw, buf, len);
659}
660
661static int dsound_run_out (HWVoiceOut *hw)
662{
663 int err;
664 HRESULT hr;
665 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
666 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
667 int live, len, hwshift;
668 DWORD blen1, blen2;
669 DWORD len1, len2;
670 DWORD decr;
671 DWORD wpos, ppos, old_pos;
672 LPVOID p1, p2;
673 int bufsize;
674
675 if (!dsb) {
676 dolog ("Attempt to run empty with playback buffer\n");
677 return 0;
678 }
679
680 hwshift = hw->info.shift;
681 bufsize = hw->samples << hwshift;
682
683 live = audio_pcm_hw_get_live_out (hw);
684
685 hr = IDirectSoundBuffer_GetCurrentPosition (
686 dsb,
687 &ppos,
688 ds->first_time ? &wpos : NULL
689 );
690 if (FAILED (hr)) {
691 dsound_logerr (hr, "Could not get playback buffer position\n");
692 return 0;
693 }
694
695 len = live << hwshift;
696
697 if (ds->first_time) {
698 if (conf.latency_millis) {
699 DWORD cur_blat;
700
701 cur_blat = audio_ring_dist (wpos, ppos, bufsize);
702 ds->first_time = 0;
703 old_pos = wpos;
704 old_pos +=
705 millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat;
706 old_pos %= bufsize;
707 old_pos &= ~hw->info.align;
708 }
709 else {
710 old_pos = wpos;
711 }
712#ifdef DEBUG_DSOUND
713 ds->played = 0;
714 ds->mixed = 0;
715#endif
716 }
717 else {
718 if (ds->old_pos == ppos) {
719#ifdef DEBUG_DSOUND
720 dolog ("old_pos == ppos\n");
721#endif
722 return 0;
723 }
724
725#ifdef DEBUG_DSOUND
726 ds->played += audio_ring_dist (ds->old_pos, ppos, bufsize);
727#endif
728 old_pos = ds->old_pos;
729 }
730
731 if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
732 len = ppos - old_pos;
733 }
734 else {
735 if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) {
736 len = bufsize - old_pos + ppos;
737 }
738 }
739
740 if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) {
741 dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n",
742 len, bufsize, old_pos, ppos);
743 return 0;
744 }
745
746 len &= ~hw->info.align;
747 if (!len) {
748 return 0;
749 }
750
751#ifdef DEBUG_DSOUND
752 ds->old_ppos = ppos;
753#endif
754 err = dsound_lock_out (
755 dsb,
756 &hw->info,
757 old_pos,
758 len,
759 &p1, &p2,
760 &blen1, &blen2,
761 0
762 );
763 if (err) {
764 return 0;
765 }
766
767 len1 = blen1 >> hwshift;
768 len2 = blen2 >> hwshift;
769 decr = len1 + len2;
770
771 if (p1 && len1) {
772 dsound_write_sample (hw, p1, len1);
773 }
774
775 if (p2 && len2) {
776 dsound_write_sample (hw, p2, len2);
777 }
778
779 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
780 ds->old_pos = (old_pos + (decr << hwshift)) % bufsize;
781
782#ifdef DEBUG_DSOUND
783 ds->mixed += decr << hwshift;
784
785 dolog ("played %lu mixed %lu diff %ld sec %f\n",
786 ds->played,
787 ds->mixed,
788 ds->mixed - ds->played,
789 abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second);
790#endif
791 return decr;
792}
793
794static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
795{
796 HRESULT hr;
797 DWORD status;
798 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
799 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
800
801 if (!dscb) {
802 dolog ("Attempt to control capture voice without a buffer\n");
803 return -1;
804 }
805
806 switch (cmd) {
807 case VOICE_ENABLE:
808 if (dsound_get_status_in (dscb, &status)) {
809 return -1;
810 }
811
812 if (status & DSCBSTATUS_CAPTURING) {
813 dolog ("warning: Voice is already capturing\n");
814 return 0;
815 }
816
817 /* clear ?? */
818
819 hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
820 if (FAILED (hr)) {
821 dsound_logerr (hr, "Could not start capturing\n");
822 return -1;
823 }
824 break;
825
826 case VOICE_DISABLE:
827 if (dsound_get_status_in (dscb, &status)) {
828 return -1;
829 }
830
831 if (status & DSCBSTATUS_CAPTURING) {
832 hr = IDirectSoundCaptureBuffer_Stop (dscb);
833 if (FAILED (hr)) {
834 dsound_logerr (hr, "Could not stop capturing\n");
835 return -1;
836 }
837 }
838 else {
839 dolog ("warning: Voice is not capturing\n");
840 }
841 break;
842 }
843 return 0;
844}
845
846static int dsound_read (SWVoiceIn *sw, void *buf, int len)
847{
848 return audio_pcm_sw_read (sw, buf, len);
849}
850
851static int dsound_run_in (HWVoiceIn *hw)
852{
853 int err;
854 HRESULT hr;
855 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
856 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
857 int live, len, dead;
858 DWORD blen1, blen2;
859 DWORD len1, len2;
860 DWORD decr;
861 DWORD cpos, rpos;
862 LPVOID p1, p2;
863 int hwshift;
864
865 if (!dscb) {
866 dolog ("Attempt to run without capture buffer\n");
867 return 0;
868 }
869
870 hwshift = hw->info.shift;
871
872 live = audio_pcm_hw_get_live_in (hw);
873 dead = hw->samples - live;
874 if (!dead) {
875 return 0;
876 }
877
878 hr = IDirectSoundCaptureBuffer_GetCurrentPosition (
879 dscb,
880 &cpos,
881 ds->first_time ? &rpos : NULL
882 );
883 if (FAILED (hr)) {
884 dsound_logerr (hr, "Could not get capture buffer position\n");
885 return 0;
886 }
887
888 if (ds->first_time) {
889 ds->first_time = 0;
890 if (rpos & hw->info.align) {
891 ldebug ("warning: Misaligned capture read position %ld(%d)\n",
892 rpos, hw->info.align);
893 }
894 hw->wpos = rpos >> hwshift;
895 }
896
897 if (cpos & hw->info.align) {
898 ldebug ("warning: Misaligned capture position %ld(%d)\n",
899 cpos, hw->info.align);
900 }
901 cpos >>= hwshift;
902
903 len = audio_ring_dist (cpos, hw->wpos, hw->samples);
904 if (!len) {
905 return 0;
906 }
907 len = audio_MIN (len, dead);
908
909 err = dsound_lock_in (
910 dscb,
911 &hw->info,
912 hw->wpos << hwshift,
913 len << hwshift,
914 &p1,
915 &p2,
916 &blen1,
917 &blen2,
918 0
919 );
920 if (err) {
921 return 0;
922 }
923
924 len1 = blen1 >> hwshift;
925 len2 = blen2 >> hwshift;
926 decr = len1 + len2;
927
928#ifndef VBOX
929 if (p1 && len1) {
930 hw->conv (hw->conv_buf + hw->wpos, p1, len1, &nominal_volume);
931 }
932
933 if (p2 && len2) {
934 hw->conv (hw->conv_buf, p2, len2, &nominal_volume);
935 }
936#else
937 if (p1 && len1) {
938 hw->conv (hw->conv_buf + hw->wpos, p1, len1, &pcm_in_volume);
939 }
940
941 if (p2 && len2) {
942 hw->conv (hw->conv_buf, p2, len2, &pcm_in_volume);
943 }
944#endif
945
946 dsound_unlock_in (dscb, p1, p2, blen1, blen2);
947 hw->wpos = (hw->wpos + decr) % hw->samples;
948 return decr;
949}
950
951static void dsound_audio_fini (void *opaque)
952{
953 HRESULT hr;
954 dsound *s = opaque;
955
956 if (!s->dsound) {
957 return;
958 }
959
960 hr = IDirectSound_Release (s->dsound);
961 if (FAILED (hr)) {
962 dsound_logerr (hr, "Could not release DirectSound\n");
963 }
964 s->dsound = NULL;
965
966 if (!s->dsound_capture) {
967 return;
968 }
969
970 hr = IDirectSoundCapture_Release (s->dsound_capture);
971 if (FAILED (hr)) {
972 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
973 }
974 s->dsound_capture = NULL;
975}
976
977static void *dsound_audio_init (void)
978{
979 int err;
980 HRESULT hr;
981 dsound *s = &glob_dsound;
982
983 hr = CoInitializeEx (NULL, COINIT_MULTITHREADED);
984 if (FAILED (hr)) {
985 dsound_logerr (hr, "Could not initialize COM\n");
986 return NULL;
987 }
988
989 hr = CoCreateInstance (
990 &CLSID_DirectSound,
991 NULL,
992 CLSCTX_ALL,
993 &IID_IDirectSound,
994 (void **) &s->dsound
995 );
996 if (FAILED (hr)) {
997 dsound_logerr (hr, "Could not create DirectSound instance\n");
998 return NULL;
999 }
1000
1001 hr = IDirectSound_Initialize (s->dsound, NULL);
1002 if (FAILED (hr)) {
1003 dsound_logerr (hr, "Could not initialize DirectSound\n");
1004
1005 hr = IDirectSound_Release (s->dsound);
1006 if (FAILED (hr)) {
1007 dsound_logerr (hr, "Could not release DirectSound\n");
1008 }
1009 s->dsound = NULL;
1010 return NULL;
1011 }
1012
1013 hr = CoCreateInstance (
1014 &CLSID_DirectSoundCapture,
1015 NULL,
1016 CLSCTX_ALL,
1017 &IID_IDirectSoundCapture,
1018 (void **) &s->dsound_capture
1019 );
1020 if (FAILED (hr)) {
1021 dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
1022 }
1023 else {
1024 hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
1025 if (FAILED (hr)) {
1026 dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
1027
1028 hr = IDirectSoundCapture_Release (s->dsound_capture);
1029 if (FAILED (hr)) {
1030 dsound_logerr (hr,
1031 "Could not release DirectSoundCapture\n");
1032 }
1033 s->dsound_capture = NULL;
1034 }
1035 }
1036
1037 err = dsound_open (s);
1038 if (err) {
1039 dsound_audio_fini (s);
1040 return NULL;
1041 }
1042
1043 return s;
1044}
1045
1046static struct audio_option dsound_options[] = {
1047 {"LOCK_RETRIES", AUD_OPT_INT, &conf.lock_retries,
1048 "Number of times to attempt locking the buffer", NULL, 0},
1049 {"RESTOURE_RETRIES", AUD_OPT_INT, &conf.restore_retries,
1050 "Number of times to attempt restoring the buffer", NULL, 0},
1051 {"GETSTATUS_RETRIES", AUD_OPT_INT, &conf.getstatus_retries,
1052 "Number of times to attempt getting status of the buffer", NULL, 0},
1053 {"SET_PRIMARY", AUD_OPT_BOOL, &conf.set_primary,
1054 "Set the parameters of primary buffer", NULL, 0},
1055 {"LATENCY_MILLIS", AUD_OPT_INT, &conf.latency_millis,
1056 "(undocumented)", NULL, 0},
1057 {"PRIMARY_FREQ", AUD_OPT_INT, &conf.settings.freq,
1058 "Primary buffer frequency", NULL, 0},
1059 {"PRIMARY_CHANNELS", AUD_OPT_INT, &conf.settings.nchannels,
1060 "Primary buffer number of channels (1 - mono, 2 - stereo)", NULL, 0},
1061 {"PRIMARY_FMT", AUD_OPT_FMT, &conf.settings.fmt,
1062 "Primary buffer format", NULL, 0},
1063 {"BUFSIZE_OUT", AUD_OPT_INT, &conf.bufsize_out,
1064 "(undocumented)", NULL, 0},
1065 {"BUFSIZE_IN", AUD_OPT_INT, &conf.bufsize_in,
1066 "(undocumented)", NULL, 0},
1067 {NULL, 0, NULL, NULL, NULL, 0}
1068};
1069
1070static struct audio_pcm_ops dsound_pcm_ops = {
1071 dsound_init_out,
1072 dsound_fini_out,
1073 dsound_run_out,
1074 dsound_write,
1075 dsound_ctl_out,
1076
1077 dsound_init_in,
1078 dsound_fini_in,
1079 dsound_run_in,
1080 dsound_read,
1081 dsound_ctl_in
1082};
1083
1084struct audio_driver dsound_audio_driver = {
1085 INIT_FIELD (name = ) "dsound",
1086 INIT_FIELD (descr = )
1087 "DirectSound http://wikipedia.org/wiki/DirectSound",
1088 INIT_FIELD (options = ) dsound_options,
1089 INIT_FIELD (init = ) dsound_audio_init,
1090 INIT_FIELD (fini = ) dsound_audio_fini,
1091 INIT_FIELD (pcm_ops = ) &dsound_pcm_ops,
1092 INIT_FIELD (can_be_default = ) 1,
1093 INIT_FIELD (max_voices_out = ) INT_MAX,
1094 INIT_FIELD (max_voices_in = ) 1,
1095 INIT_FIELD (voice_size_out = ) sizeof (DSoundVoiceOut),
1096 INIT_FIELD (voice_size_in = ) sizeof (DSoundVoiceIn)
1097};
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette