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
;
62 LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer
;
67 static void dsound_log_hresult (HRESULT hr
)
69 const char *str
= "BUG";
73 str
= "The method succeeded";
75 #ifdef DS_NO_VIRTUALIZATION
76 case DS_NO_VIRTUALIZATION
:
77 str
= "The buffer was created, but another 3D algorithm was substituted";
82 str
= "The method succeeded, but not all the optional effects were obtained";
85 #ifdef DSERR_ACCESSDENIED
86 case DSERR_ACCESSDENIED
:
87 str
= "The request failed because access was denied";
90 #ifdef DSERR_ALLOCATED
92 str
= "The request failed because resources, "
93 "such as a priority level, were already in use "
97 #ifdef DSERR_ALREADYINITIALIZED
98 case DSERR_ALREADYINITIALIZED
:
99 str
= "The object is already initialized";
102 #ifdef DSERR_BADFORMAT
103 case DSERR_BADFORMAT
:
104 str
= "The specified wave format is not supported";
107 #ifdef DSERR_BADSENDBUFFERGUID
108 case DSERR_BADSENDBUFFERGUID
:
109 str
= "The GUID specified in an audiopath file "
110 "does not match a valid mix-in buffer";
113 #ifdef DSERR_BUFFERLOST
114 case DSERR_BUFFERLOST
:
115 str
= "The buffer memory has been lost and must be restored";
118 #ifdef DSERR_BUFFERTOOSMALL
119 case DSERR_BUFFERTOOSMALL
:
120 str
= "The buffer size is not great enough to "
121 "enable effects processing";
124 #ifdef DSERR_CONTROLUNAVAIL
125 case DSERR_CONTROLUNAVAIL
:
126 str
= "The buffer control (volume, pan, and so on) "
127 "requested by the caller is not available. "
128 "Controls must be specified when the buffer is created, "
129 "using the dwFlags member of DSBUFFERDESC";
132 #ifdef DSERR_DS8_REQUIRED
133 case DSERR_DS8_REQUIRED
:
134 str
= "A DirectSound object of class CLSID_DirectSound8 or later "
135 "is required for the requested functionality. "
136 "For more information, see IDirectSound8 Interface";
139 #ifdef DSERR_FXUNAVAILABLE
140 case DSERR_FXUNAVAILABLE
:
141 str
= "The effects requested could not be found on the system, "
142 "or they are in the wrong order or in the wrong location; "
143 "for example, an effect expected in hardware "
144 "was found in software";
149 str
= "An undetermined error occurred inside the DirectSound subsystem";
152 #ifdef DSERR_INVALIDCALL
153 case DSERR_INVALIDCALL
:
154 str
= "This function is not valid for the current state of this object";
157 #ifdef DSERR_INVALIDPARAM
158 case DSERR_INVALIDPARAM
:
159 str
= "An invalid parameter was passed to the returning function";
162 #ifdef DSERR_NOAGGREGATION
163 case DSERR_NOAGGREGATION
:
164 str
= "The object does not support aggregation";
167 #ifdef DSERR_NODRIVER
169 str
= "No sound driver is available for use, "
170 "or the given GUID is not a valid DirectSound device ID";
173 #ifdef DSERR_NOINTERFACE
174 case DSERR_NOINTERFACE
:
175 str
= "The requested COM interface is not available";
178 #ifdef DSERR_OBJECTNOTFOUND
179 case DSERR_OBJECTNOTFOUND
:
180 str
= "The requested object was not found";
183 #ifdef DSERR_OTHERAPPHASPRIO
184 case DSERR_OTHERAPPHASPRIO
:
185 str
= "Another application has a higher priority level, "
186 "preventing this call from succeeding";
189 #ifdef DSERR_OUTOFMEMORY
190 case DSERR_OUTOFMEMORY
:
191 str
= "The DirectSound subsystem could not allocate "
192 "sufficient memory to complete the caller's request";
195 #ifdef DSERR_PRIOLEVELNEEDED
196 case DSERR_PRIOLEVELNEEDED
:
197 str
= "A cooperative level of DSSCL_PRIORITY or higher is required";
200 #ifdef DSERR_SENDLOOP
202 str
= "A circular loop of send effects was detected";
205 #ifdef DSERR_UNINITIALIZED
206 case DSERR_UNINITIALIZED
:
207 str
= "The Initialize method has not been called "
208 "or has not been called successfully "
209 "before other methods were called";
212 #ifdef DSERR_UNSUPPORTED
213 case DSERR_UNSUPPORTED
:
214 str
= "The function called is not supported at this time";
218 AUD_log (AUDIO_CAP
, "Reason: Unknown (HRESULT 0x%lx)\n", hr
);
222 AUD_log (AUDIO_CAP
, "Reason: %s\n", str
);
225 static void G_GNUC_PRINTF (2, 3) dsound_logerr (
234 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
237 dsound_log_hresult (hr
);
240 static void G_GNUC_PRINTF (3, 4) dsound_logerr2 (
249 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
251 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
254 dsound_log_hresult (hr
);
258 static void print_wave_format (WAVEFORMATEX
*wfx
)
260 dolog ("tag = %d\n", wfx
->wFormatTag
);
261 dolog ("nChannels = %d\n", wfx
->nChannels
);
262 dolog ("nSamplesPerSec = %ld\n", wfx
->nSamplesPerSec
);
263 dolog ("nAvgBytesPerSec = %ld\n", wfx
->nAvgBytesPerSec
);
264 dolog ("nBlockAlign = %d\n", wfx
->nBlockAlign
);
265 dolog ("wBitsPerSample = %d\n", wfx
->wBitsPerSample
);
266 dolog ("cbSize = %d\n", wfx
->cbSize
);
270 static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb
, dsound
*s
)
274 hr
= IDirectSoundBuffer_Restore (dsb
);
277 dsound_logerr (hr
, "Could not restore playback buffer\n");
283 #include "dsound_template.h"
285 #include "dsound_template.h"
288 static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb
, DWORD
*statusp
,
293 hr
= IDirectSoundBuffer_GetStatus (dsb
, statusp
);
295 dsound_logerr (hr
, "Could not get playback buffer status\n");
299 if (*statusp
& DSBSTATUS_BUFFERLOST
) {
300 dsound_restore_out(dsb
, s
);
307 static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb
,
312 hr
= IDirectSoundCaptureBuffer_GetStatus (dscb
, statusp
);
314 dsound_logerr (hr
, "Could not get capture buffer status\n");
321 static void dsound_clear_sample (HWVoiceOut
*hw
, LPDIRECTSOUNDBUFFER dsb
,
326 DWORD blen1
, blen2
, len1
, len2
;
328 err
= dsound_lock_out (
342 len1
= blen1
/ hw
->info
.bytes_per_frame
;
343 len2
= blen2
/ hw
->info
.bytes_per_frame
;
346 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
352 audio_pcm_info_clear_buf (&hw
->info
, p1
, len1
);
356 audio_pcm_info_clear_buf (&hw
->info
, p2
, len2
);
359 dsound_unlock_out (dsb
, p1
, p2
, blen1
, blen2
);
362 static int dsound_set_cooperative_level(dsound
*s
)
367 hwnd
= GetDesktopWindow();
368 hr
= IDirectSound_SetCooperativeLevel (
375 dsound_logerr (hr
, "Could not set cooperative level for window %p\n",
383 static void dsound_enable_out(HWVoiceOut
*hw
, bool enable
)
387 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
388 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
392 dolog ("Attempt to control voice without a buffer\n");
397 if (dsound_get_status_out (dsb
, &status
, s
)) {
401 if (status
& DSBSTATUS_PLAYING
) {
402 dolog ("warning: Voice is already playing\n");
406 dsound_clear_sample (hw
, dsb
, s
);
408 hr
= IDirectSoundBuffer_Play (dsb
, 0, 0, DSBPLAY_LOOPING
);
410 dsound_logerr (hr
, "Could not start playing buffer\n");
414 if (dsound_get_status_out (dsb
, &status
, s
)) {
418 if (status
& DSBSTATUS_PLAYING
) {
419 hr
= IDirectSoundBuffer_Stop (dsb
);
421 dsound_logerr (hr
, "Could not stop playing buffer\n");
425 dolog ("warning: Voice is not playing\n");
430 static size_t dsound_buffer_get_free(HWVoiceOut
*hw
)
432 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
433 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
437 hr
= IDirectSoundBuffer_GetCurrentPosition(
438 dsb
, &ppos
, ds
->first_time
? &wpos
: NULL
);
440 dsound_logerr(hr
, "Could not get playback buffer position\n");
444 if (ds
->first_time
) {
446 ds
->first_time
= false;
449 return audio_ring_dist(ppos
, hw
->pos_emul
, hw
->size_emul
);
452 static void *dsound_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
454 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*)hw
;
455 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
461 req_size
= MIN(*size
, hw
->size_emul
- hw
->pos_emul
);
462 assert(req_size
> 0);
464 err
= dsound_lock_out(dsb
, &hw
->info
, hw
->pos_emul
, req_size
, &ret
, NULL
,
465 &act_size
, NULL
, false, ds
->s
);
467 dolog("Failed to lock buffer\n");
476 static size_t dsound_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t len
)
478 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
479 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
480 int err
= dsound_unlock_out(dsb
, buf
, NULL
, len
, 0);
483 dolog("Failed to unlock buffer!!\n");
486 hw
->pos_emul
= (hw
->pos_emul
+ len
) % hw
->size_emul
;
491 static void dsound_enable_in(HWVoiceIn
*hw
, bool enable
)
495 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
496 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
499 dolog ("Attempt to control capture voice without a buffer\n");
504 if (dsound_get_status_in (dscb
, &status
)) {
508 if (status
& DSCBSTATUS_CAPTURING
) {
509 dolog ("warning: Voice is already capturing\n");
515 hr
= IDirectSoundCaptureBuffer_Start (dscb
, DSCBSTART_LOOPING
);
517 dsound_logerr (hr
, "Could not start capturing\n");
521 if (dsound_get_status_in (dscb
, &status
)) {
525 if (status
& DSCBSTATUS_CAPTURING
) {
526 hr
= IDirectSoundCaptureBuffer_Stop (dscb
);
528 dsound_logerr (hr
, "Could not stop capturing\n");
532 dolog ("warning: Voice is not capturing\n");
537 static void *dsound_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
539 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
540 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
542 DWORD rpos
, act_size
;
547 hr
= IDirectSoundCaptureBuffer_GetCurrentPosition(dscb
, NULL
, &rpos
);
549 dsound_logerr(hr
, "Could not get capture buffer position\n");
554 if (ds
->first_time
) {
556 ds
->first_time
= false;
559 req_size
= audio_ring_dist(rpos
, hw
->pos_emul
, hw
->size_emul
);
560 req_size
= MIN(*size
, MIN(req_size
, hw
->size_emul
- hw
->pos_emul
));
567 err
= dsound_lock_in(dscb
, &hw
->info
, hw
->pos_emul
, req_size
, &ret
, NULL
,
568 &act_size
, NULL
, false, ds
->s
);
570 dolog("Failed to lock buffer\n");
579 static void dsound_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t len
)
581 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
582 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
583 int err
= dsound_unlock_in(dscb
, buf
, NULL
, len
, 0);
586 dolog("Failed to unlock buffer!!\n");
589 hw
->pos_emul
= (hw
->pos_emul
+ len
) % hw
->size_emul
;
592 static void dsound_audio_fini (void *opaque
)
602 hr
= IDirectSound_Release (s
->dsound
);
604 dsound_logerr (hr
, "Could not release DirectSound\n");
608 if (!s
->dsound_capture
) {
613 hr
= IDirectSoundCapture_Release (s
->dsound_capture
);
615 dsound_logerr (hr
, "Could not release DirectSoundCapture\n");
617 s
->dsound_capture
= NULL
;
622 static void *dsound_audio_init(Audiodev
*dev
)
626 dsound
*s
= g_new0(dsound
, 1);
627 AudiodevDsoundOptions
*dso
;
629 assert(dev
->driver
== AUDIODEV_DRIVER_DSOUND
);
631 dso
= &dev
->u
.dsound
;
633 if (!dso
->has_latency
) {
634 dso
->has_latency
= true;
635 dso
->latency
= 10000; /* 10 ms */
638 hr
= CoInitialize (NULL
);
640 dsound_logerr (hr
, "Could not initialize COM\n");
645 hr
= CoCreateInstance (
653 dsound_logerr (hr
, "Could not create DirectSound instance\n");
658 hr
= IDirectSound_Initialize (s
->dsound
, NULL
);
660 dsound_logerr (hr
, "Could not initialize DirectSound\n");
662 hr
= IDirectSound_Release (s
->dsound
);
664 dsound_logerr (hr
, "Could not release DirectSound\n");
670 hr
= CoCreateInstance (
671 &CLSID_DirectSoundCapture
,
674 &IID_IDirectSoundCapture
,
675 (void **) &s
->dsound_capture
678 dsound_logerr (hr
, "Could not create DirectSoundCapture instance\n");
680 hr
= IDirectSoundCapture_Initialize (s
->dsound_capture
, NULL
);
682 dsound_logerr (hr
, "Could not initialize DirectSoundCapture\n");
684 hr
= IDirectSoundCapture_Release (s
->dsound_capture
);
686 dsound_logerr (hr
, "Could not release DirectSoundCapture\n");
688 s
->dsound_capture
= NULL
;
692 err
= dsound_set_cooperative_level(s
);
694 dsound_audio_fini (s
);
701 static struct audio_pcm_ops dsound_pcm_ops
= {
702 .init_out
= dsound_init_out
,
703 .fini_out
= dsound_fini_out
,
704 .write
= audio_generic_write
,
705 .buffer_get_free
= dsound_buffer_get_free
,
706 .get_buffer_out
= dsound_get_buffer_out
,
707 .put_buffer_out
= dsound_put_buffer_out
,
708 .enable_out
= dsound_enable_out
,
710 .init_in
= dsound_init_in
,
711 .fini_in
= dsound_fini_in
,
712 .read
= audio_generic_read
,
713 .get_buffer_in
= dsound_get_buffer_in
,
714 .put_buffer_in
= dsound_put_buffer_in
,
715 .enable_in
= dsound_enable_in
,
718 static struct audio_driver dsound_audio_driver
= {
720 .descr
= "DirectSound http://wikipedia.org/wiki/DirectSound",
721 .init
= dsound_audio_init
,
722 .fini
= dsound_audio_fini
,
723 .pcm_ops
= &dsound_pcm_ops
,
725 .max_voices_out
= INT_MAX
,
727 .voice_size_out
= sizeof (DSoundVoiceOut
),
728 .voice_size_in
= sizeof (DSoundVoiceIn
)
731 static void register_audio_dsound(void)
733 audio_driver_register(&dsound_audio_driver
);
735 type_init(register_audio_dsound
);