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, such as a priority level, were already in use by another caller";
95 #ifdef DSERR_ALREADYINITIALIZED
96 case DSERR_ALREADYINITIALIZED
:
97 str
= "The object is already initialized";
100 #ifdef DSERR_BADFORMAT
101 case DSERR_BADFORMAT
:
102 str
= "The specified wave format is not supported";
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";
110 #ifdef DSERR_BUFFERLOST
111 case DSERR_BUFFERLOST
:
112 str
= "The buffer memory has been lost and must be restored";
115 #ifdef DSERR_BUFFERTOOSMALL
116 case DSERR_BUFFERTOOSMALL
:
117 str
= "The buffer size is not great enough to enable effects processing";
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";
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";
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";
137 str
= "An undetermined error occurred inside the DirectSound subsystem";
140 #ifdef DSERR_INVALIDCALL
141 case DSERR_INVALIDCALL
:
142 str
= "This function is not valid for the current state of this object";
145 #ifdef DSERR_INVALIDPARAM
146 case DSERR_INVALIDPARAM
:
147 str
= "An invalid parameter was passed to the returning function";
150 #ifdef DSERR_NOAGGREGATION
151 case DSERR_NOAGGREGATION
:
152 str
= "The object does not support aggregation";
155 #ifdef DSERR_NODRIVER
157 str
= "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
160 #ifdef DSERR_NOINTERFACE
161 case DSERR_NOINTERFACE
:
162 str
= "The requested COM interface is not available";
165 #ifdef DSERR_OBJECTNOTFOUND
166 case DSERR_OBJECTNOTFOUND
:
167 str
= "The requested object was not found";
170 #ifdef DSERR_OTHERAPPHASPRIO
171 case DSERR_OTHERAPPHASPRIO
:
172 str
= "Another application has a higher priority level, preventing this call from succeeding";
175 #ifdef DSERR_OUTOFMEMORY
176 case DSERR_OUTOFMEMORY
:
177 str
= "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
180 #ifdef DSERR_PRIOLEVELNEEDED
181 case DSERR_PRIOLEVELNEEDED
:
182 str
= "A cooperative level of DSSCL_PRIORITY or higher is required";
185 #ifdef DSERR_SENDLOOP
187 str
= "A circular loop of send effects was detected";
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";
195 #ifdef DSERR_UNSUPPORTED
196 case DSERR_UNSUPPORTED
:
197 str
= "The function called is not supported at this time";
201 AUD_log (AUDIO_CAP
, "Reason: Unknown (HRESULT %#lx)\n", hr
);
205 AUD_log (AUDIO_CAP
, "Reason: %s\n", str
);
208 static void GCC_FMT_ATTR (2, 3) dsound_logerr (
217 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
220 dsound_log_hresult (hr
);
223 static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
232 AUD_log (AUDIO_CAP
, "Could not initialize %s\n", typ
);
234 AUD_vlog (AUDIO_CAP
, fmt
, ap
);
237 dsound_log_hresult (hr
);
241 static void print_wave_format (WAVEFORMATEX
*wfx
)
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
);
253 static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb
, dsound
*s
)
257 hr
= IDirectSoundBuffer_Restore (dsb
);
260 dsound_logerr (hr
, "Could not restore playback buffer\n");
266 #include "dsound_template.h"
268 #include "dsound_template.h"
271 static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb
, DWORD
*statusp
,
276 hr
= IDirectSoundBuffer_GetStatus (dsb
, statusp
);
278 dsound_logerr (hr
, "Could not get playback buffer status\n");
282 if (*statusp
& DSBSTATUS_BUFFERLOST
) {
283 dsound_restore_out(dsb
, s
);
290 static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb
,
295 hr
= IDirectSoundCaptureBuffer_GetStatus (dscb
, statusp
);
297 dsound_logerr (hr
, "Could not get capture buffer status\n");
304 static void dsound_clear_sample (HWVoiceOut
*hw
, LPDIRECTSOUNDBUFFER dsb
,
309 DWORD blen1
, blen2
, len1
, len2
;
311 err
= dsound_lock_out (
325 len1
= blen1
/ hw
->info
.bytes_per_frame
;
326 len2
= blen2
/ hw
->info
.bytes_per_frame
;
329 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
335 audio_pcm_info_clear_buf (&hw
->info
, p1
, len1
);
339 audio_pcm_info_clear_buf (&hw
->info
, p2
, len2
);
342 dsound_unlock_out (dsb
, p1
, p2
, blen1
, blen2
);
345 static int dsound_open (dsound
*s
)
350 hwnd
= GetForegroundWindow ();
351 hr
= IDirectSound_SetCooperativeLevel (
358 dsound_logerr (hr
, "Could not set cooperative level for window %p\n",
366 static void dsound_enable_out(HWVoiceOut
*hw
, bool enable
)
370 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
371 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
375 dolog ("Attempt to control voice without a buffer\n");
380 if (dsound_get_status_out (dsb
, &status
, s
)) {
384 if (status
& DSBSTATUS_PLAYING
) {
385 dolog ("warning: Voice is already playing\n");
389 dsound_clear_sample (hw
, dsb
, s
);
391 hr
= IDirectSoundBuffer_Play (dsb
, 0, 0, DSBPLAY_LOOPING
);
393 dsound_logerr (hr
, "Could not start playing buffer\n");
397 if (dsound_get_status_out (dsb
, &status
, s
)) {
401 if (status
& DSBSTATUS_PLAYING
) {
402 hr
= IDirectSoundBuffer_Stop (dsb
);
404 dsound_logerr (hr
, "Could not stop playing buffer\n");
409 dolog ("warning: Voice is not playing\n");
414 static void *dsound_get_buffer_out(HWVoiceOut
*hw
, size_t *size
)
416 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
417 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
419 DWORD ppos
, wpos
, act_size
;
424 hr
= IDirectSoundBuffer_GetCurrentPosition(
425 dsb
, &ppos
, ds
->first_time
? &wpos
: NULL
);
427 dsound_logerr(hr
, "Could not get playback buffer position\n");
432 if (ds
->first_time
) {
434 ds
->first_time
= false;
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
);
445 err
= dsound_lock_out(dsb
, &hw
->info
, hw
->pos_emul
, req_size
, &ret
, NULL
,
446 &act_size
, NULL
, false, ds
->s
);
448 dolog("Failed to lock buffer\n");
457 static size_t dsound_put_buffer_out(HWVoiceOut
*hw
, void *buf
, size_t len
)
459 DSoundVoiceOut
*ds
= (DSoundVoiceOut
*) hw
;
460 LPDIRECTSOUNDBUFFER dsb
= ds
->dsound_buffer
;
461 int err
= dsound_unlock_out(dsb
, buf
, NULL
, len
, 0);
464 dolog("Failed to unlock buffer!!\n");
467 hw
->pos_emul
= (hw
->pos_emul
+ len
) % hw
->size_emul
;
472 static void dsound_enable_in(HWVoiceIn
*hw
, bool enable
)
476 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
477 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
480 dolog ("Attempt to control capture voice without a buffer\n");
485 if (dsound_get_status_in (dscb
, &status
)) {
489 if (status
& DSCBSTATUS_CAPTURING
) {
490 dolog ("warning: Voice is already capturing\n");
496 hr
= IDirectSoundCaptureBuffer_Start (dscb
, DSCBSTART_LOOPING
);
498 dsound_logerr (hr
, "Could not start capturing\n");
502 if (dsound_get_status_in (dscb
, &status
)) {
506 if (status
& DSCBSTATUS_CAPTURING
) {
507 hr
= IDirectSoundCaptureBuffer_Stop (dscb
);
509 dsound_logerr (hr
, "Could not stop capturing\n");
514 dolog ("warning: Voice is not capturing\n");
519 static void *dsound_get_buffer_in(HWVoiceIn
*hw
, size_t *size
)
521 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
522 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
524 DWORD cpos
, rpos
, act_size
;
529 hr
= IDirectSoundCaptureBuffer_GetCurrentPosition(
530 dscb
, &cpos
, ds
->first_time
? &rpos
: NULL
);
532 dsound_logerr(hr
, "Could not get capture buffer position\n");
537 if (ds
->first_time
) {
539 ds
->first_time
= false;
542 req_size
= audio_ring_dist(cpos
, hw
->pos_emul
, hw
->size_emul
);
543 req_size
= MIN(*size
, MIN(req_size
, hw
->size_emul
- hw
->pos_emul
));
550 err
= dsound_lock_in(dscb
, &hw
->info
, hw
->pos_emul
, req_size
, &ret
, NULL
,
551 &act_size
, NULL
, false, ds
->s
);
553 dolog("Failed to lock buffer\n");
562 static void dsound_put_buffer_in(HWVoiceIn
*hw
, void *buf
, size_t len
)
564 DSoundVoiceIn
*ds
= (DSoundVoiceIn
*) hw
;
565 LPDIRECTSOUNDCAPTUREBUFFER dscb
= ds
->dsound_capture_buffer
;
566 int err
= dsound_unlock_in(dscb
, buf
, NULL
, len
, 0);
569 dolog("Failed to unlock buffer!!\n");
572 hw
->pos_emul
= (hw
->pos_emul
+ len
) % hw
->size_emul
;
575 static void dsound_audio_fini (void *opaque
)
585 hr
= IDirectSound_Release (s
->dsound
);
587 dsound_logerr (hr
, "Could not release DirectSound\n");
591 if (!s
->dsound_capture
) {
596 hr
= IDirectSoundCapture_Release (s
->dsound_capture
);
598 dsound_logerr (hr
, "Could not release DirectSoundCapture\n");
600 s
->dsound_capture
= NULL
;
605 static void *dsound_audio_init(Audiodev
*dev
)
609 dsound
*s
= g_malloc0(sizeof(dsound
));
610 AudiodevDsoundOptions
*dso
;
612 assert(dev
->driver
== AUDIODEV_DRIVER_DSOUND
);
614 dso
= &dev
->u
.dsound
;
616 if (!dso
->has_latency
) {
617 dso
->has_latency
= true;
618 dso
->latency
= 10000; /* 10 ms */
621 hr
= CoInitialize (NULL
);
623 dsound_logerr (hr
, "Could not initialize COM\n");
628 hr
= CoCreateInstance (
636 dsound_logerr (hr
, "Could not create DirectSound instance\n");
641 hr
= IDirectSound_Initialize (s
->dsound
, NULL
);
643 dsound_logerr (hr
, "Could not initialize DirectSound\n");
645 hr
= IDirectSound_Release (s
->dsound
);
647 dsound_logerr (hr
, "Could not release DirectSound\n");
653 hr
= CoCreateInstance (
654 &CLSID_DirectSoundCapture
,
657 &IID_IDirectSoundCapture
,
658 (void **) &s
->dsound_capture
661 dsound_logerr (hr
, "Could not create DirectSoundCapture instance\n");
664 hr
= IDirectSoundCapture_Initialize (s
->dsound_capture
, NULL
);
666 dsound_logerr (hr
, "Could not initialize DirectSoundCapture\n");
668 hr
= IDirectSoundCapture_Release (s
->dsound_capture
);
670 dsound_logerr (hr
, "Could not release DirectSoundCapture\n");
672 s
->dsound_capture
= NULL
;
676 err
= dsound_open (s
);
678 dsound_audio_fini (s
);
685 static struct audio_pcm_ops dsound_pcm_ops
= {
686 .init_out
= dsound_init_out
,
687 .fini_out
= dsound_fini_out
,
688 .write
= audio_generic_write
,
689 .get_buffer_out
= dsound_get_buffer_out
,
690 .put_buffer_out
= dsound_put_buffer_out
,
691 .enable_out
= dsound_enable_out
,
693 .init_in
= dsound_init_in
,
694 .fini_in
= dsound_fini_in
,
695 .read
= audio_generic_read
,
696 .get_buffer_in
= dsound_get_buffer_in
,
697 .put_buffer_in
= dsound_put_buffer_in
,
698 .enable_in
= dsound_enable_in
,
701 static struct audio_driver dsound_audio_driver
= {
703 .descr
= "DirectSound http://wikipedia.org/wiki/DirectSound",
704 .init
= dsound_audio_init
,
705 .fini
= dsound_audio_fini
,
706 .pcm_ops
= &dsound_pcm_ops
,
708 .max_voices_out
= INT_MAX
,
710 .voice_size_out
= sizeof (DSoundVoiceOut
),
711 .voice_size_in
= sizeof (DSoundVoiceIn
)
714 static void register_audio_dsound(void)
716 audio_driver_register(&dsound_audio_driver
);
718 type_init(register_audio_dsound
);