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
;
61 LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer
;
65 static void dsound_log_hresult (HRESULT hr
)
67 const char *str
= "BUG";
71 str
= "The method succeeded";
73 #ifdef DS_NO_VIRTUALIZATION
74 case DS_NO_VIRTUALIZATION
:
75 str
= "The buffer was created, but another 3D algorithm was substituted";
80 str
= "The method succeeded, but not all the optional effects were obtained";
83 #ifdef DSERR_ACCESSDENIED
84 case DSERR_ACCESSDENIED
:
85 str
= "The request failed because access was denied";
88 #ifdef DSERR_ALLOCATED
90 str
= "The request failed because resources, such as a priority level, were already in use by another caller";
93 #ifdef DSERR_ALREADYINITIALIZED
94 case DSERR_ALREADYINITIALIZED
:
95 str
= "The object is already initialized";
98 #ifdef DSERR_BADFORMAT
100 str
= "The specified wave format is not supported";
103 #ifdef DSERR_BADSENDBUFFERGUID
104 case DSERR_BADSENDBUFFERGUID
:
105 str
= "The GUID specified in an audiopath file does not match a valid mix-in buffer";
108 #ifdef DSERR_BUFFERLOST
109 case DSERR_BUFFERLOST
:
110 str
= "The buffer memory has been lost and must be restored";
113 #ifdef DSERR_BUFFERTOOSMALL
114 case DSERR_BUFFERTOOSMALL
:
115 str
= "The buffer size is not great enough to enable effects processing";
118 #ifdef DSERR_CONTROLUNAVAIL
119 case DSERR_CONTROLUNAVAIL
:
120 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 #ifdef DSERR_DS8_REQUIRED
124 case DSERR_DS8_REQUIRED
:
125 str
= "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
128 #ifdef DSERR_FXUNAVAILABLE
129 case DSERR_FXUNAVAILABLE
:
130 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";
135 str
= "An undetermined error occurred inside the DirectSound subsystem";
138 #ifdef DSERR_INVALIDCALL
139 case DSERR_INVALIDCALL
:
140 str
= "This function is not valid for the current state of this object";
143 #ifdef DSERR_INVALIDPARAM
144 case DSERR_INVALIDPARAM
:
145 str
= "An invalid parameter was passed to the returning function";
148 #ifdef DSERR_NOAGGREGATION
149 case DSERR_NOAGGREGATION
:
150 str
= "The object does not support aggregation";
153 #ifdef DSERR_NODRIVER
155 str
= "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
158 #ifdef DSERR_NOINTERFACE
159 case DSERR_NOINTERFACE
:
160 str
= "The requested COM interface is not available";
163 #ifdef DSERR_OBJECTNOTFOUND
164 case DSERR_OBJECTNOTFOUND
:
165 str
= "The requested object was not found";
168 #ifdef DSERR_OTHERAPPHASPRIO
169 case DSERR_OTHERAPPHASPRIO
:
170 str
= "Another application has a higher priority level, preventing this call from succeeding";
173 #ifdef DSERR_OUTOFMEMORY
174 case DSERR_OUTOFMEMORY
:
175 str
= "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
178 #ifdef DSERR_PRIOLEVELNEEDED
179 case DSERR_PRIOLEVELNEEDED
:
180 str
= "A cooperative level of DSSCL_PRIORITY or higher is required";
183 #ifdef DSERR_SENDLOOP
185 str
= "A circular loop of send effects was detected";
188 #ifdef DSERR_UNINITIALIZED
189 case DSERR_UNINITIALIZED
:
190 str
= "The Initialize method has not been called or has not been called successfully before other methods were called";
193 #ifdef DSERR_UNSUPPORTED
194 case DSERR_UNSUPPORTED
:
195 str
= "The function called is not supported at this time";
199 AUD_log (AUDIO_CAP
, "Reason: Unknown (HRESULT %#lx)\n", hr
);
203 AUD_log (AUDIO_CAP
, "Reason: %s\n", str
);
206 static void GCC_FMT_ATTR (2, 3) dsound_logerr (
215 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
218 dsound_log_hresult (hr
);
221 static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
230 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
232 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
235 dsound_log_hresult (hr
);
239 static void print_wave_format (WAVEFORMATEX
*wfx
)
241 dolog ("tag = %d\n", wfx
->wFormatTag
);
242 dolog ("nChannels = %d\n", wfx
->nChannels
);
243 dolog ("nSamplesPerSec = %ld\n", wfx
->nSamplesPerSec
);
244 dolog ("nAvgBytesPerSec = %ld\n", wfx
->nAvgBytesPerSec
);
245 dolog ("nBlockAlign = %d\n", wfx
->nBlockAlign
);
246 dolog ("wBitsPerSample = %d\n", wfx
->wBitsPerSample
);
247 dolog ("cbSize = %d\n", wfx
->cbSize
);
251 static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb
, dsound
*s
)
255 hr
= IDirectSoundBuffer_Restore (dsb
);
258 dsound_logerr (hr
, "Could not restore playback buffer\n");
264 #include "dsound_template.h"
266 #include "dsound_template.h"
269 static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb
, DWORD
*statusp
,
274 hr
= IDirectSoundBuffer_GetStatus (dsb
, statusp
);
276 dsound_logerr (hr
, "Could not get playback buffer status\n");
280 if (*statusp
& DSERR_BUFFERLOST
) {
281 dsound_restore_out(dsb
, s
);
288 static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb
,
293 hr
= IDirectSoundCaptureBuffer_GetStatus (dscb
, statusp
);
295 dsound_logerr (hr
, "Could not get capture buffer status\n");
302 static void dsound_clear_sample (HWVoiceOut
*hw
, LPDIRECTSOUNDBUFFER dsb
,
307 DWORD blen1
, blen2
, len1
, len2
;
309 err
= dsound_lock_out (
323 len1
= blen1
>> hw
->info
.shift
;
324 len2
= blen2
>> hw
->info
.shift
;
327 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
333 audio_pcm_info_clear_buf (&hw
->info
, p1
, len1
);
337 audio_pcm_info_clear_buf (&hw
->info
, p2
, len2
);
340 dsound_unlock_out (dsb
, p1
, p2
, blen1
, blen2
);
343 static int dsound_open (dsound
*s
)
348 hwnd
= GetForegroundWindow ();
349 hr
= IDirectSound_SetCooperativeLevel (
356 dsound_logerr (hr
, "Could not set cooperative level for window %p\n",
364 static void dsound_enable_out(HWVoiceOut
*hw
, bool enable
)
368 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
369 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
373 dolog ("Attempt to control voice without a buffer\n");
378 if (dsound_get_status_out (dsb
, &status
, s
)) {
382 if (status
& DSBSTATUS_PLAYING
) {
383 dolog ("warning: Voice is already playing\n");
387 dsound_clear_sample (hw
, dsb
, s
);
389 hr
= IDirectSoundBuffer_Play (dsb
, 0, 0, DSBPLAY_LOOPING
);
391 dsound_logerr (hr
, "Could not start playing buffer\n");
395 if (dsound_get_status_out (dsb
, &status
, s
)) {
399 if (status
& DSBSTATUS_PLAYING
) {
400 hr
= IDirectSoundBuffer_Stop (dsb
);
402 dsound_logerr (hr
, "Could not stop playing buffer\n");
407 dolog ("warning: Voice is not playing\n");
412 static void *dsound_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
414 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
415 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
417 DWORD ppos
, act_size
;
422 hr
= IDirectSoundBuffer_GetCurrentPosition(dsb
, &ppos
, NULL
);
424 dsound_logerr(hr
, "Could not get playback buffer position\n");
429 req_size
= audio_ring_dist(ppos
, hw
->pos_emul
, hw
->size_emul
);
430 req_size
= MIN(req_size
, hw
->size_emul
- hw
->pos_emul
);
432 err
= dsound_lock_out(dsb
, &hw
->info
, hw
->pos_emul
, req_size
, &ret
, NULL
,
433 &act_size
, NULL
, false, ds
->s
);
435 dolog("Failed to lock buffer\n");
444 static size_t dsound_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t len
)
446 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
447 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
448 int err
= dsound_unlock_out(dsb
, buf
, NULL
, len
, 0);
451 dolog("Failed to unlock buffer!!\n");
454 hw
->pos_emul
= (hw
->pos_emul
+ len
) % hw
->size_emul
;
459 static void dsound_enable_in(HWVoiceIn
*hw
, bool enable
)
463 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
464 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
467 dolog ("Attempt to control capture voice without a buffer\n");
472 if (dsound_get_status_in (dscb
, &status
)) {
476 if (status
& DSCBSTATUS_CAPTURING
) {
477 dolog ("warning: Voice is already capturing\n");
483 hr
= IDirectSoundCaptureBuffer_Start (dscb
, DSCBSTART_LOOPING
);
485 dsound_logerr (hr
, "Could not start capturing\n");
489 if (dsound_get_status_in (dscb
, &status
)) {
493 if (status
& DSCBSTATUS_CAPTURING
) {
494 hr
= IDirectSoundCaptureBuffer_Stop (dscb
);
496 dsound_logerr (hr
, "Could not stop capturing\n");
501 dolog ("warning: Voice is not capturing\n");
506 static void *dsound_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
508 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
509 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
511 DWORD cpos
, act_size
;
516 hr
= IDirectSoundCaptureBuffer_GetCurrentPosition(dscb
, &cpos
, NULL
);
518 dsound_logerr(hr
, "Could not get capture buffer position\n");
523 req_size
= audio_ring_dist(cpos
, hw
->pos_emul
, hw
->size_emul
);
524 req_size
= MIN(req_size
, hw
->size_emul
- hw
->pos_emul
);
526 err
= dsound_lock_in(dscb
, &hw
->info
, hw
->pos_emul
, req_size
, &ret
, NULL
,
527 &act_size
, NULL
, false, ds
->s
);
529 dolog("Failed to lock buffer\n");
538 static void dsound_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t len
)
540 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
541 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
542 int err
= dsound_unlock_in(dscb
, buf
, NULL
, len
, 0);
545 dolog("Failed to unlock buffer!!\n");
548 hw
->pos_emul
= (hw
->pos_emul
+ len
) % hw
->size_emul
;
551 static void dsound_audio_fini (void *opaque
)
561 hr
= IDirectSound_Release (s
->dsound
);
563 dsound_logerr (hr
, "Could not release DirectSound\n");
567 if (!s
->dsound_capture
) {
572 hr
= IDirectSoundCapture_Release (s
->dsound_capture
);
574 dsound_logerr (hr
, "Could not release DirectSoundCapture\n");
576 s
->dsound_capture
= NULL
;
581 static void *dsound_audio_init(Audiodev
*dev
)
585 dsound
*s
= g_malloc0(sizeof(dsound
));
586 AudiodevDsoundOptions
*dso
;
588 assert(dev
->driver
== AUDIODEV_DRIVER_DSOUND
);
590 dso
= &dev
->u
.dsound
;
592 if (!dso
->has_latency
) {
593 dso
->has_latency
= true;
594 dso
->latency
= 10000; /* 10 ms */
597 hr
= CoInitialize (NULL
);
599 dsound_logerr (hr
, "Could not initialize COM\n");
604 hr
= CoCreateInstance (
612 dsound_logerr (hr
, "Could not create DirectSound instance\n");
617 hr
= IDirectSound_Initialize (s
->dsound
, NULL
);
619 dsound_logerr (hr
, "Could not initialize DirectSound\n");
621 hr
= IDirectSound_Release (s
->dsound
);
623 dsound_logerr (hr
, "Could not release DirectSound\n");
629 hr
= CoCreateInstance (
630 &CLSID_DirectSoundCapture
,
633 &IID_IDirectSoundCapture
,
634 (void **) &s
->dsound_capture
637 dsound_logerr (hr
, "Could not create DirectSoundCapture instance\n");
640 hr
= IDirectSoundCapture_Initialize (s
->dsound_capture
, NULL
);
642 dsound_logerr (hr
, "Could not initialize DirectSoundCapture\n");
644 hr
= IDirectSoundCapture_Release (s
->dsound_capture
);
646 dsound_logerr (hr
, "Could not release DirectSoundCapture\n");
648 s
->dsound_capture
= NULL
;
652 err
= dsound_open (s
);
654 dsound_audio_fini (s
);
661 static struct audio_pcm_ops dsound_pcm_ops
= {
662 .init_out
= dsound_init_out
,
663 .fini_out
= dsound_fini_out
,
664 .write
= audio_generic_write
,
665 .get_buffer_out
= dsound_get_buffer_out
,
666 .put_buffer_out
= dsound_put_buffer_out
,
667 .enable_out
= dsound_enable_out
,
669 .init_in
= dsound_init_in
,
670 .fini_in
= dsound_fini_in
,
671 .read
= audio_generic_read
,
672 .get_buffer_in
= dsound_get_buffer_in
,
673 .put_buffer_in
= dsound_put_buffer_in
,
674 .enable_in
= dsound_enable_in
,
677 static struct audio_driver dsound_audio_driver
= {
679 .descr
= "DirectSound http://wikipedia.org/wiki/DirectSound",
680 .init
= dsound_audio_init
,
681 .fini
= dsound_audio_fini
,
682 .pcm_ops
= &dsound_pcm_ops
,
684 .max_voices_out
= INT_MAX
,
686 .voice_size_out
= sizeof (DSoundVoiceOut
),
687 .voice_size_in
= sizeof (DSoundVoiceIn
)
690 static void register_audio_dsound(void)
692 audio_driver_register(&dsound_audio_driver
);
694 type_init(register_audio_dsound
);