2 * QEMU DirectSound audio driver
4 * Copyright (c) 2005 Vassili Karpov (malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 * SEAL 1.07 by Carlos 'pel' Hasan was used as documentation
29 #include "qemu/osdep.h"
32 #define AUDIO_CAP "dsound"
33 #include "audio_int.h"
34 #include "qemu/host-utils.h"
35 #include "qemu/module.h"
42 #include "audio_win_int.h"
44 /* #define DEBUG_DSOUND */
48 LPDIRECTSOUNDCAPTURE dsound_capture
;
49 struct audsettings settings
;
55 LPDIRECTSOUNDBUFFER dsound_buffer
;
69 LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer
;
73 static void dsound_log_hresult (HRESULT hr
)
75 const char *str
= "BUG";
79 str
= "The method succeeded";
81 #ifdef DS_NO_VIRTUALIZATION
82 case DS_NO_VIRTUALIZATION
:
83 str
= "The buffer was created, but another 3D algorithm was substituted";
88 str
= "The method succeeded, but not all the optional effects were obtained";
91 #ifdef DSERR_ACCESSDENIED
92 case DSERR_ACCESSDENIED
:
93 str
= "The request failed because access was denied";
96 #ifdef DSERR_ALLOCATED
98 str
= "The request failed because resources, such as a priority level, were already in use by another caller";
101 #ifdef DSERR_ALREADYINITIALIZED
102 case DSERR_ALREADYINITIALIZED
:
103 str
= "The object is already initialized";
106 #ifdef DSERR_BADFORMAT
107 case DSERR_BADFORMAT
:
108 str
= "The specified wave format is not supported";
111 #ifdef DSERR_BADSENDBUFFERGUID
112 case DSERR_BADSENDBUFFERGUID
:
113 str
= "The GUID specified in an audiopath file does not match a valid mix-in buffer";
116 #ifdef DSERR_BUFFERLOST
117 case DSERR_BUFFERLOST
:
118 str
= "The buffer memory has been lost and must be restored";
121 #ifdef DSERR_BUFFERTOOSMALL
122 case DSERR_BUFFERTOOSMALL
:
123 str
= "The buffer size is not great enough to enable effects processing";
126 #ifdef DSERR_CONTROLUNAVAIL
127 case DSERR_CONTROLUNAVAIL
:
128 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";
131 #ifdef DSERR_DS8_REQUIRED
132 case DSERR_DS8_REQUIRED
:
133 str
= "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
136 #ifdef DSERR_FXUNAVAILABLE
137 case DSERR_FXUNAVAILABLE
:
138 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";
143 str
= "An undetermined error occurred inside the DirectSound subsystem";
146 #ifdef DSERR_INVALIDCALL
147 case DSERR_INVALIDCALL
:
148 str
= "This function is not valid for the current state of this object";
151 #ifdef DSERR_INVALIDPARAM
152 case DSERR_INVALIDPARAM
:
153 str
= "An invalid parameter was passed to the returning function";
156 #ifdef DSERR_NOAGGREGATION
157 case DSERR_NOAGGREGATION
:
158 str
= "The object does not support aggregation";
161 #ifdef DSERR_NODRIVER
163 str
= "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
166 #ifdef DSERR_NOINTERFACE
167 case DSERR_NOINTERFACE
:
168 str
= "The requested COM interface is not available";
171 #ifdef DSERR_OBJECTNOTFOUND
172 case DSERR_OBJECTNOTFOUND
:
173 str
= "The requested object was not found";
176 #ifdef DSERR_OTHERAPPHASPRIO
177 case DSERR_OTHERAPPHASPRIO
:
178 str
= "Another application has a higher priority level, preventing this call from succeeding";
181 #ifdef DSERR_OUTOFMEMORY
182 case DSERR_OUTOFMEMORY
:
183 str
= "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
186 #ifdef DSERR_PRIOLEVELNEEDED
187 case DSERR_PRIOLEVELNEEDED
:
188 str
= "A cooperative level of DSSCL_PRIORITY or higher is required";
191 #ifdef DSERR_SENDLOOP
193 str
= "A circular loop of send effects was detected";
196 #ifdef DSERR_UNINITIALIZED
197 case DSERR_UNINITIALIZED
:
198 str
= "The Initialize method has not been called or has not been called successfully before other methods were called";
201 #ifdef DSERR_UNSUPPORTED
202 case DSERR_UNSUPPORTED
:
203 str
= "The function called is not supported at this time";
207 AUD_log (AUDIO_CAP
, "Reason: Unknown (HRESULT %#lx)\n", hr
);
211 AUD_log (AUDIO_CAP
, "Reason: %s\n", str
);
214 static void GCC_FMT_ATTR (2, 3) dsound_logerr (
223 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
226 dsound_log_hresult (hr
);
229 static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
238 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
240 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
243 dsound_log_hresult (hr
);
246 static uint64_t usecs_to_bytes(struct audio_pcm_info
*info
, uint32_t usecs
)
248 return muldiv64(usecs
, info
->bytes_per_second
, 1000000);
252 static void print_wave_format (WAVEFORMATEX
*wfx
)
254 dolog ("tag = %d\n", wfx
->wFormatTag
);
255 dolog ("nChannels = %d\n", wfx
->nChannels
);
256 dolog ("nSamplesPerSec = %ld\n", wfx
->nSamplesPerSec
);
257 dolog ("nAvgBytesPerSec = %ld\n", wfx
->nAvgBytesPerSec
);
258 dolog ("nBlockAlign = %d\n", wfx
->nBlockAlign
);
259 dolog ("wBitsPerSample = %d\n", wfx
->wBitsPerSample
);
260 dolog ("cbSize = %d\n", wfx
->cbSize
);
264 static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb
, dsound
*s
)
268 hr
= IDirectSoundBuffer_Restore (dsb
);
271 dsound_logerr (hr
, "Could not restore playback buffer\n");
277 #include "dsound_template.h"
279 #include "dsound_template.h"
282 static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb
, DWORD
*statusp
,
287 hr
= IDirectSoundBuffer_GetStatus (dsb
, statusp
);
289 dsound_logerr (hr
, "Could not get playback buffer status\n");
293 if (*statusp
& DSERR_BUFFERLOST
) {
294 dsound_restore_out(dsb
, s
);
301 static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb
,
306 hr
= IDirectSoundCaptureBuffer_GetStatus (dscb
, statusp
);
308 dsound_logerr (hr
, "Could not get capture buffer status\n");
315 static void dsound_write_sample (HWVoiceOut
*hw
, uint8_t *dst
, int dst_len
)
317 int src_len1
= dst_len
;
319 int pos
= hw
->rpos
+ dst_len
;
320 struct st_sample
*src1
= hw
->mix_buf
+ hw
->rpos
;
321 struct st_sample
*src2
= NULL
;
323 if (pos
> hw
->samples
) {
324 src_len1
= hw
->samples
- hw
->rpos
;
326 src_len2
= dst_len
- src_len1
;
331 hw
->clip (dst
, src1
, src_len1
);
335 dst
= advance (dst
, src_len1
<< hw
->info
.shift
);
336 hw
->clip (dst
, src2
, src_len2
);
339 hw
->rpos
= pos
% hw
->samples
;
342 static void dsound_clear_sample (HWVoiceOut
*hw
, LPDIRECTSOUNDBUFFER dsb
,
347 DWORD blen1
, blen2
, len1
, len2
;
349 err
= dsound_lock_out (
353 hw
->samples
<< hw
->info
.shift
,
363 len1
= blen1
>> hw
->info
.shift
;
364 len2
= blen2
>> hw
->info
.shift
;
367 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
373 audio_pcm_info_clear_buf (&hw
->info
, p1
, len1
);
377 audio_pcm_info_clear_buf (&hw
->info
, p2
, len2
);
380 dsound_unlock_out (dsb
, p1
, p2
, blen1
, blen2
);
383 static int dsound_open (dsound
*s
)
388 hwnd
= GetForegroundWindow ();
389 hr
= IDirectSound_SetCooperativeLevel (
396 dsound_logerr (hr
, "Could not set cooperative level for window %p\n",
404 static int dsound_ctl_out (HWVoiceOut
*hw
, int cmd
, ...)
408 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
409 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
413 dolog ("Attempt to control voice without a buffer\n");
419 if (dsound_get_status_out (dsb
, &status
, s
)) {
423 if (status
& DSBSTATUS_PLAYING
) {
424 dolog ("warning: Voice is already playing\n");
428 dsound_clear_sample (hw
, dsb
, s
);
430 hr
= IDirectSoundBuffer_Play (dsb
, 0, 0, DSBPLAY_LOOPING
);
432 dsound_logerr (hr
, "Could not start playing buffer\n");
438 if (dsound_get_status_out (dsb
, &status
, s
)) {
442 if (status
& DSBSTATUS_PLAYING
) {
443 hr
= IDirectSoundBuffer_Stop (dsb
);
445 dsound_logerr (hr
, "Could not stop playing buffer\n");
450 dolog ("warning: Voice is not playing\n");
457 static size_t dsound_run_out(HWVoiceOut
*hw
, size_t live
)
461 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
462 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
468 DWORD wpos
, ppos
, old_pos
;
472 AudiodevDsoundOptions
*dso
= &s
->dev
->u
.dsound
;
475 dolog ("Attempt to run empty with playback buffer\n");
479 hwshift
= hw
->info
.shift
;
480 bufsize
= hw
->samples
<< hwshift
;
482 hr
= IDirectSoundBuffer_GetCurrentPosition (
485 ds
->first_time
? &wpos
: NULL
488 dsound_logerr (hr
, "Could not get playback buffer position\n");
492 len
= live
<< hwshift
;
494 if (ds
->first_time
) {
498 cur_blat
= audio_ring_dist (wpos
, ppos
, bufsize
);
502 usecs_to_bytes(&hw
->info
, dso
->latency
) - cur_blat
;
504 old_pos
&= ~hw
->info
.align
;
515 if (ds
->old_pos
== ppos
) {
517 dolog ("old_pos == ppos\n");
523 ds
->played
+= audio_ring_dist (ds
->old_pos
, ppos
, hw
->bufsize
);
525 old_pos
= ds
->old_pos
;
528 if ((old_pos
< ppos
) && ((old_pos
+ len
) > ppos
)) {
529 len
= ppos
- old_pos
;
532 if ((old_pos
> ppos
) && ((old_pos
+ len
) > (ppos
+ bufsize
))) {
533 len
= bufsize
- old_pos
+ ppos
;
537 if (audio_bug(__func__
, len
> bufsize
)) {
538 dolog("len=%zu bufsize=%zu old_pos=%ld ppos=%ld\n",
539 len
, bufsize
, old_pos
, ppos
);
543 len
&= ~hw
->info
.align
;
551 err
= dsound_lock_out (
565 len1
= blen1
>> hwshift
;
566 len2
= blen2
>> hwshift
;
570 dsound_write_sample (hw
, p1
, len1
);
574 dsound_write_sample (hw
, p2
, len2
);
577 dsound_unlock_out (dsb
, p1
, p2
, blen1
, blen2
);
578 ds
->old_pos
= (old_pos
+ (decr
<< hwshift
)) % bufsize
;
581 ds
->mixed
+= decr
<< hwshift
;
583 dolog ("played %lu mixed %lu diff %ld sec %f\n",
586 ds
->mixed
- ds
->played
,
587 abs (ds
->mixed
- ds
->played
) / (double) hw
->info
.bytes_per_second
);
592 static int dsound_ctl_in (HWVoiceIn
*hw
, int cmd
, ...)
596 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
597 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
600 dolog ("Attempt to control capture voice without a buffer\n");
606 if (dsound_get_status_in (dscb
, &status
)) {
610 if (status
& DSCBSTATUS_CAPTURING
) {
611 dolog ("warning: Voice is already capturing\n");
617 hr
= IDirectSoundCaptureBuffer_Start (dscb
, DSCBSTART_LOOPING
);
619 dsound_logerr (hr
, "Could not start capturing\n");
625 if (dsound_get_status_in (dscb
, &status
)) {
629 if (status
& DSCBSTATUS_CAPTURING
) {
630 hr
= IDirectSoundCaptureBuffer_Stop (dscb
);
632 dsound_logerr (hr
, "Could not stop capturing\n");
637 dolog ("warning: Voice is not capturing\n");
644 static size_t dsound_run_in(HWVoiceIn
*hw
)
648 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
649 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
650 size_t live
, len
, dead
;
660 dolog ("Attempt to run without capture buffer\n");
664 hwshift
= hw
->info
.shift
;
666 live
= audio_pcm_hw_get_live_in (hw
);
667 dead
= hw
->samples
- live
;
672 hr
= IDirectSoundCaptureBuffer_GetCurrentPosition (
675 ds
->first_time
? &rpos
: NULL
678 dsound_logerr (hr
, "Could not get capture buffer position\n");
682 if (ds
->first_time
) {
684 if (rpos
& hw
->info
.align
) {
685 ldebug ("warning: Misaligned capture read position %ld(%d)\n",
686 rpos
, hw
->info
.align
);
688 hw
->wpos
= rpos
>> hwshift
;
691 if (cpos
& hw
->info
.align
) {
692 ldebug ("warning: Misaligned capture position %ld(%d)\n",
693 cpos
, hw
->info
.align
);
697 len
= audio_ring_dist (cpos
, hw
->wpos
, hw
->samples
);
701 len
= MIN (len
, dead
);
703 err
= dsound_lock_in (
719 len1
= blen1
>> hwshift
;
720 len2
= blen2
>> hwshift
;
724 hw
->conv (hw
->conv_buf
+ hw
->wpos
, p1
, len1
);
728 hw
->conv (hw
->conv_buf
, p2
, len2
);
731 dsound_unlock_in (dscb
, p1
, p2
, blen1
, blen2
);
732 hw
->wpos
= (hw
->wpos
+ decr
) % hw
->samples
;
736 static void dsound_audio_fini (void *opaque
)
746 hr
= IDirectSound_Release (s
->dsound
);
748 dsound_logerr (hr
, "Could not release DirectSound\n");
752 if (!s
->dsound_capture
) {
757 hr
= IDirectSoundCapture_Release (s
->dsound_capture
);
759 dsound_logerr (hr
, "Could not release DirectSoundCapture\n");
761 s
->dsound_capture
= NULL
;
766 static void *dsound_audio_init(Audiodev
*dev
)
770 dsound
*s
= g_malloc0(sizeof(dsound
));
771 AudiodevDsoundOptions
*dso
;
773 assert(dev
->driver
== AUDIODEV_DRIVER_DSOUND
);
775 dso
= &dev
->u
.dsound
;
777 if (!dso
->has_latency
) {
778 dso
->has_latency
= true;
779 dso
->latency
= 10000; /* 10 ms */
782 hr
= CoInitialize (NULL
);
784 dsound_logerr (hr
, "Could not initialize COM\n");
789 hr
= CoCreateInstance (
797 dsound_logerr (hr
, "Could not create DirectSound instance\n");
802 hr
= IDirectSound_Initialize (s
->dsound
, NULL
);
804 dsound_logerr (hr
, "Could not initialize DirectSound\n");
806 hr
= IDirectSound_Release (s
->dsound
);
808 dsound_logerr (hr
, "Could not release DirectSound\n");
814 hr
= CoCreateInstance (
815 &CLSID_DirectSoundCapture
,
818 &IID_IDirectSoundCapture
,
819 (void **) &s
->dsound_capture
822 dsound_logerr (hr
, "Could not create DirectSoundCapture instance\n");
825 hr
= IDirectSoundCapture_Initialize (s
->dsound_capture
, NULL
);
827 dsound_logerr (hr
, "Could not initialize DirectSoundCapture\n");
829 hr
= IDirectSoundCapture_Release (s
->dsound_capture
);
831 dsound_logerr (hr
, "Could not release DirectSoundCapture\n");
833 s
->dsound_capture
= NULL
;
837 err
= dsound_open (s
);
839 dsound_audio_fini (s
);
846 static struct audio_pcm_ops dsound_pcm_ops
= {
847 .init_out
= dsound_init_out
,
848 .fini_out
= dsound_fini_out
,
849 .run_out
= dsound_run_out
,
850 .ctl_out
= dsound_ctl_out
,
852 .init_in
= dsound_init_in
,
853 .fini_in
= dsound_fini_in
,
854 .run_in
= dsound_run_in
,
855 .ctl_in
= dsound_ctl_in
858 static struct audio_driver dsound_audio_driver
= {
860 .descr
= "DirectSound http://wikipedia.org/wiki/DirectSound",
861 .init
= dsound_audio_init
,
862 .fini
= dsound_audio_fini
,
863 .pcm_ops
= &dsound_pcm_ops
,
865 .max_voices_out
= INT_MAX
,
867 .voice_size_out
= sizeof (DSoundVoiceOut
),
868 .voice_size_in
= sizeof (DSoundVoiceIn
)
871 static void register_audio_dsound(void)
873 audio_driver_register(&dsound_audio_driver
);
875 type_init(register_audio_dsound
);