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-common.h"
32 #define AUDIO_CAP "dsound"
33 #include "audio_int.h"
40 #include "audio_win_int.h"
42 /* #define DEBUG_DSOUND */
52 LPDIRECTSOUNDCAPTURE dsound_capture;
53 struct audsettings settings;
59 LPDIRECTSOUNDBUFFER dsound_buffer;
73 LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
77 static void dsound_log_hresult (HRESULT hr)
79 const char *str = "BUG";
83 str = "The method succeeded";
85 #ifdef DS_NO_VIRTUALIZATION
86 case DS_NO_VIRTUALIZATION:
87 str = "The buffer was created, but another 3D algorithm was substituted";
92 str = "The method succeeded, but not all the optional effects were obtained";
95 #ifdef DSERR_ACCESSDENIED
96 case DSERR_ACCESSDENIED:
97 str = "The request failed because access was denied";
100 #ifdef DSERR_ALLOCATED
101 case DSERR_ALLOCATED:
102 str = "The request failed because resources, such as a priority level, were already in use by another caller";
105 #ifdef DSERR_ALREADYINITIALIZED
106 case DSERR_ALREADYINITIALIZED:
107 str = "The object is already initialized";
110 #ifdef DSERR_BADFORMAT
111 case DSERR_BADFORMAT:
112 str = "The specified wave format is not supported";
115 #ifdef DSERR_BADSENDBUFFERGUID
116 case DSERR_BADSENDBUFFERGUID:
117 str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
120 #ifdef DSERR_BUFFERLOST
121 case DSERR_BUFFERLOST:
122 str = "The buffer memory has been lost and must be restored";
125 #ifdef DSERR_BUFFERTOOSMALL
126 case DSERR_BUFFERTOOSMALL:
127 str = "The buffer size is not great enough to enable effects processing";
130 #ifdef DSERR_CONTROLUNAVAIL
131 case DSERR_CONTROLUNAVAIL:
132 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";
135 #ifdef DSERR_DS8_REQUIRED
136 case DSERR_DS8_REQUIRED:
137 str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
140 #ifdef DSERR_FXUNAVAILABLE
141 case DSERR_FXUNAVAILABLE:
142 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";
147 str = "An undetermined error occurred inside the DirectSound subsystem";
150 #ifdef DSERR_INVALIDCALL
151 case DSERR_INVALIDCALL:
152 str = "This function is not valid for the current state of this object";
155 #ifdef DSERR_INVALIDPARAM
156 case DSERR_INVALIDPARAM:
157 str = "An invalid parameter was passed to the returning function";
160 #ifdef DSERR_NOAGGREGATION
161 case DSERR_NOAGGREGATION:
162 str = "The object does not support aggregation";
165 #ifdef DSERR_NODRIVER
167 str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
170 #ifdef DSERR_NOINTERFACE
171 case DSERR_NOINTERFACE:
172 str = "The requested COM interface is not available";
175 #ifdef DSERR_OBJECTNOTFOUND
176 case DSERR_OBJECTNOTFOUND:
177 str = "The requested object was not found";
180 #ifdef DSERR_OTHERAPPHASPRIO
181 case DSERR_OTHERAPPHASPRIO:
182 str = "Another application has a higher priority level, preventing this call from succeeding";
185 #ifdef DSERR_OUTOFMEMORY
186 case DSERR_OUTOFMEMORY:
187 str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
190 #ifdef DSERR_PRIOLEVELNEEDED
191 case DSERR_PRIOLEVELNEEDED:
192 str = "A cooperative level of DSSCL_PRIORITY or higher is required";
195 #ifdef DSERR_SENDLOOP
197 str = "A circular loop of send effects was detected";
200 #ifdef DSERR_UNINITIALIZED
201 case DSERR_UNINITIALIZED:
202 str = "The Initialize method has not been called or has not been called successfully before other methods were called";
205 #ifdef DSERR_UNSUPPORTED
206 case DSERR_UNSUPPORTED:
207 str = "The function called is not supported at this time";
211 AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr);
215 AUD_log (AUDIO_CAP, "Reason: %s\n", str);
218 static void GCC_FMT_ATTR (2, 3) dsound_logerr (
227 AUD_vlog (AUDIO_CAP, fmt, ap);
230 dsound_log_hresult (hr);
233 static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
242 AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
244 AUD_vlog (AUDIO_CAP, fmt, ap);
247 dsound_log_hresult (hr);
250 static DWORD millis_to_bytes (struct audio_pcm_info *info, DWORD millis)
252 return (millis * info->bytes_per_second) / 1000;
256 static void print_wave_format (WAVEFORMATEX *wfx)
258 dolog ("tag = %d\n", wfx->wFormatTag);
259 dolog ("nChannels = %d\n", wfx->nChannels);
260 dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec);
261 dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
262 dolog ("nBlockAlign = %d\n", wfx->nBlockAlign);
263 dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample);
264 dolog ("cbSize = %d\n", wfx->cbSize);
268 static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
272 hr = IDirectSoundBuffer_Restore (dsb);
275 dsound_logerr (hr, "Could not restore playback buffer\n");
281 #include "dsound_template.h"
283 #include "dsound_template.h"
286 static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp,
291 hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
293 dsound_logerr (hr, "Could not get playback buffer status\n");
297 if (*statusp & DSERR_BUFFERLOST) {
298 dsound_restore_out(dsb, s);
305 static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
310 hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
312 dsound_logerr (hr, "Could not get capture buffer status\n");
319 static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
321 int src_len1 = dst_len;
323 int pos = hw->rpos + dst_len;
324 struct st_sample *src1 = hw->mix_buf + hw->rpos;
325 struct st_sample *src2 = NULL;
327 if (pos > hw->samples) {
328 src_len1 = hw->samples - hw->rpos;
330 src_len2 = dst_len - src_len1;
335 hw->clip (dst, src1, src_len1);
339 dst = advance (dst, src_len1 << hw->info.shift);
340 hw->clip (dst, src2, src_len2);
343 hw->rpos = pos % hw->samples;
346 static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
351 DWORD blen1, blen2, len1, len2;
353 err = dsound_lock_out (
357 hw->samples << hw->info.shift,
367 len1 = blen1 >> hw->info.shift;
368 len2 = blen2 >> hw->info.shift;
371 dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
377 audio_pcm_info_clear_buf (&hw->info, p1, len1);
381 audio_pcm_info_clear_buf (&hw->info, p2, len2);
384 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
387 static int dsound_open (dsound *s)
392 hwnd = GetForegroundWindow ();
393 hr = IDirectSound_SetCooperativeLevel (
400 dsound_logerr (hr, "Could not set cooperative level for window %p\n",
408 static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
412 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
413 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
417 dolog ("Attempt to control voice without a buffer\n");
423 if (dsound_get_status_out (dsb, &status, s)) {
427 if (status & DSBSTATUS_PLAYING) {
428 dolog ("warning: Voice is already playing\n");
432 dsound_clear_sample (hw, dsb, s);
434 hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
436 dsound_logerr (hr, "Could not start playing buffer\n");
442 if (dsound_get_status_out (dsb, &status, s)) {
446 if (status & DSBSTATUS_PLAYING) {
447 hr = IDirectSoundBuffer_Stop (dsb);
449 dsound_logerr (hr, "Could not stop playing buffer\n");
454 dolog ("warning: Voice is not playing\n");
461 static int dsound_write (SWVoiceOut *sw, void *buf, int len)
463 return audio_pcm_sw_write (sw, buf, len);
466 static int dsound_run_out (HWVoiceOut *hw, int live)
470 DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
471 LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
476 DWORD wpos, ppos, old_pos;
480 DSoundConf *conf = &s->conf;
483 dolog ("Attempt to run empty with playback buffer\n");
487 hwshift = hw->info.shift;
488 bufsize = hw->samples << hwshift;
490 hr = IDirectSoundBuffer_GetCurrentPosition (
493 ds->first_time ? &wpos : NULL
496 dsound_logerr (hr, "Could not get playback buffer position\n");
500 len = live << hwshift;
502 if (ds->first_time) {
503 if (conf->latency_millis) {
506 cur_blat = audio_ring_dist (wpos, ppos, bufsize);
510 millis_to_bytes (&hw->info, conf->latency_millis) - cur_blat;
512 old_pos &= ~hw->info.align;
523 if (ds->old_pos == ppos) {
525 dolog ("old_pos == ppos\n");
531 ds->played += audio_ring_dist (ds->old_pos, ppos, hw->bufsize);
533 old_pos = ds->old_pos;
536 if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
537 len = ppos - old_pos;
540 if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) {
541 len = bufsize - old_pos + ppos;
545 if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) {
546 dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n",
547 len, bufsize, old_pos, ppos);
551 len &= ~hw->info.align;
559 err = dsound_lock_out (
573 len1 = blen1 >> hwshift;
574 len2 = blen2 >> hwshift;
578 dsound_write_sample (hw, p1, len1);
582 dsound_write_sample (hw, p2, len2);
585 dsound_unlock_out (dsb, p1, p2, blen1, blen2);
586 ds->old_pos = (old_pos + (decr << hwshift)) % bufsize;
589 ds->mixed += decr << hwshift;
591 dolog ("played %lu mixed %lu diff %ld sec %f\n",
594 ds->mixed - ds->played,
595 abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second);
600 static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
604 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
605 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
608 dolog ("Attempt to control capture voice without a buffer\n");
614 if (dsound_get_status_in (dscb, &status)) {
618 if (status & DSCBSTATUS_CAPTURING) {
619 dolog ("warning: Voice is already capturing\n");
625 hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
627 dsound_logerr (hr, "Could not start capturing\n");
633 if (dsound_get_status_in (dscb, &status)) {
637 if (status & DSCBSTATUS_CAPTURING) {
638 hr = IDirectSoundCaptureBuffer_Stop (dscb);
640 dsound_logerr (hr, "Could not stop capturing\n");
645 dolog ("warning: Voice is not capturing\n");
652 static int dsound_read (SWVoiceIn *sw, void *buf, int len)
654 return audio_pcm_sw_read (sw, buf, len);
657 static int dsound_run_in (HWVoiceIn *hw)
661 DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
662 LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
673 dolog ("Attempt to run without capture buffer\n");
677 hwshift = hw->info.shift;
679 live = audio_pcm_hw_get_live_in (hw);
680 dead = hw->samples - live;
685 hr = IDirectSoundCaptureBuffer_GetCurrentPosition (
688 ds->first_time ? &rpos : NULL
691 dsound_logerr (hr, "Could not get capture buffer position\n");
695 if (ds->first_time) {
697 if (rpos & hw->info.align) {
698 ldebug ("warning: Misaligned capture read position %ld(%d)\n",
699 rpos, hw->info.align);
701 hw->wpos = rpos >> hwshift;
704 if (cpos & hw->info.align) {
705 ldebug ("warning: Misaligned capture position %ld(%d)\n",
706 cpos, hw->info.align);
710 len = audio_ring_dist (cpos, hw->wpos, hw->samples);
714 len = audio_MIN (len, dead);
716 err = dsound_lock_in (
732 len1 = blen1 >> hwshift;
733 len2 = blen2 >> hwshift;
737 hw->conv (hw->conv_buf + hw->wpos, p1, len1);
741 hw->conv (hw->conv_buf, p2, len2);
744 dsound_unlock_in (dscb, p1, p2, blen1, blen2);
745 hw->wpos = (hw->wpos + decr) % hw->samples;
749 static DSoundConf glob_conf = {
751 .bufsize_out = 16384,
755 static void dsound_audio_fini (void *opaque)
765 hr = IDirectSound_Release (s->dsound);
767 dsound_logerr (hr, "Could not release DirectSound\n");
771 if (!s->dsound_capture) {
776 hr = IDirectSoundCapture_Release (s->dsound_capture);
778 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
780 s->dsound_capture = NULL;
785 static void *dsound_audio_init (void)
789 dsound *s = g_malloc0(sizeof(dsound));
792 hr = CoInitialize (NULL);
794 dsound_logerr (hr, "Could not initialize COM\n");
799 hr = CoCreateInstance (
807 dsound_logerr (hr, "Could not create DirectSound instance\n");
812 hr = IDirectSound_Initialize (s->dsound, NULL);
814 dsound_logerr (hr, "Could not initialize DirectSound\n");
816 hr = IDirectSound_Release (s->dsound);
818 dsound_logerr (hr, "Could not release DirectSound\n");
824 hr = CoCreateInstance (
825 &CLSID_DirectSoundCapture,
828 &IID_IDirectSoundCapture,
829 (void **) &s->dsound_capture
832 dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
835 hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
837 dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
839 hr = IDirectSoundCapture_Release (s->dsound_capture);
841 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
843 s->dsound_capture = NULL;
847 err = dsound_open (s);
849 dsound_audio_fini (s);
856 static struct audio_option dsound_options[] = {
858 .name = "LATENCY_MILLIS",
860 .valp = &glob_conf.latency_millis,
861 .descr = "(undocumented)"
864 .name = "BUFSIZE_OUT",
866 .valp = &glob_conf.bufsize_out,
867 .descr = "(undocumented)"
870 .name = "BUFSIZE_IN",
872 .valp = &glob_conf.bufsize_in,
873 .descr = "(undocumented)"
875 { /* End of list */ }
878 static struct audio_pcm_ops dsound_pcm_ops = {
879 .init_out = dsound_init_out,
880 .fini_out = dsound_fini_out,
881 .run_out = dsound_run_out,
882 .write = dsound_write,
883 .ctl_out = dsound_ctl_out,
885 .init_in = dsound_init_in,
886 .fini_in = dsound_fini_in,
887 .run_in = dsound_run_in,
889 .ctl_in = dsound_ctl_in
892 struct audio_driver dsound_audio_driver = {
894 .descr = "DirectSound http://wikipedia.org/wiki/DirectSound",
895 .options = dsound_options,
896 .init = dsound_audio_init,
897 .fini = dsound_audio_fini,
898 .pcm_ops = &dsound_pcm_ops,
900 .max_voices_out = INT_MAX,
902 .voice_size_out = sizeof (DSoundVoiceOut),
903 .voice_size_in = sizeof (DSoundVoiceIn)