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 */
47
int getstatus_retries;
51
struct audsettings settings;
56
.getstatus_retries = 1,
60
.settings.freq = 44100,
61
.settings.nchannels = 2,
62
.settings.fmt = AUD_FMT_S16,
68
LPDIRECTSOUNDCAPTURE dsound_capture;
69
LPDIRECTSOUNDBUFFER dsound_primary_buffer;
70
struct audsettings settings;
73
static dsound glob_dsound;
77
LPDIRECTSOUNDBUFFER dsound_buffer;
90
LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
93
static void dsound_log_hresult (HRESULT hr)
95
const char *str = "BUG";
99
str = "The method succeeded";
101
#ifdef DS_NO_VIRTUALIZATION
102
case DS_NO_VIRTUALIZATION:
103
str = "The buffer was created, but another 3D algorithm was substituted";
108
str = "The method succeeded, but not all the optional effects were obtained";
111
#ifdef DSERR_ACCESSDENIED
112
case DSERR_ACCESSDENIED:
113
str = "The request failed because access was denied";
116
#ifdef DSERR_ALLOCATED
117
case DSERR_ALLOCATED:
118
str = "The request failed because resources, such as a priority level, were already in use by another caller";
121
#ifdef DSERR_ALREADYINITIALIZED
122
case DSERR_ALREADYINITIALIZED:
123
str = "The object is already initialized";
126
#ifdef DSERR_BADFORMAT
127
case DSERR_BADFORMAT:
128
str = "The specified wave format is not supported";
131
#ifdef DSERR_BADSENDBUFFERGUID
132
case DSERR_BADSENDBUFFERGUID:
133
str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
136
#ifdef DSERR_BUFFERLOST
137
case DSERR_BUFFERLOST:
138
str = "The buffer memory has been lost and must be restored";
141
#ifdef DSERR_BUFFERTOOSMALL
142
case DSERR_BUFFERTOOSMALL:
143
str = "The buffer size is not great enough to enable effects processing";
146
#ifdef DSERR_CONTROLUNAVAIL
147
case DSERR_CONTROLUNAVAIL:
148
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";
151
#ifdef DSERR_DS8_REQUIRED
152
case DSERR_DS8_REQUIRED:
153
str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
156
#ifdef DSERR_FXUNAVAILABLE
157
case DSERR_FXUNAVAILABLE:
158
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";
163
str = "An undetermined error occurred inside the DirectSound subsystem";
166
#ifdef DSERR_INVALIDCALL
167
case DSERR_INVALIDCALL:
168
str = "This function is not valid for the current state of this object";
171
#ifdef DSERR_INVALIDPARAM
172
case DSERR_INVALIDPARAM:
173
str = "An invalid parameter was passed to the returning function";
176
#ifdef DSERR_NOAGGREGATION
177
case DSERR_NOAGGREGATION:
178
str = "The object does not support aggregation";
181
#ifdef DSERR_NODRIVER
183
str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
186
#ifdef DSERR_NOINTERFACE
187
case DSERR_NOINTERFACE:
188
str = "The requested COM interface is not available";
191
#ifdef DSERR_OBJECTNOTFOUND
192
case DSERR_OBJECTNOTFOUND:
193
str = "The requested object was not found";
196
#ifdef DSERR_OTHERAPPHASPRIO
197
case DSERR_OTHERAPPHASPRIO:
198
str = "Another application has a higher priority level, preventing this call from succeeding";
201
#ifdef DSERR_OUTOFMEMORY
202
case DSERR_OUTOFMEMORY:
203
str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
206
#ifdef DSERR_PRIOLEVELNEEDED
207
case DSERR_PRIOLEVELNEEDED:
208
str = "A cooperative level of DSSCL_PRIORITY or higher is required";
211
#ifdef DSERR_SENDLOOP
213
str = "A circular loop of send effects was detected";
216
#ifdef DSERR_UNINITIALIZED
217
case DSERR_UNINITIALIZED:
218
str = "The Initialize method has not been called or has not been called successfully before other methods were called";
221
#ifdef DSERR_UNSUPPORTED
222
case DSERR_UNSUPPORTED:
223
str = "The function called is not supported at this time";
227
AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr);
231
AUD_log (AUDIO_CAP, "Reason: %s\n", str);
234
static void GCC_FMT_ATTR (2, 3) dsound_logerr (
243
AUD_vlog (AUDIO_CAP, fmt, ap);
246
dsound_log_hresult (hr);
249
static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
258
AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
260
AUD_vlog (AUDIO_CAP, fmt, ap);
263
dsound_log_hresult (hr);
266
static DWORD millis_to_bytes (struct audio_pcm_info *info, DWORD millis)
268
return (millis * info->bytes_per_second) / 1000;
272
static void print_wave_format (WAVEFORMATEX *wfx)
274
dolog ("tag = %d\n", wfx->wFormatTag);
275
dolog ("nChannels = %d\n", wfx->nChannels);
276
dolog ("nSamplesPerSec = %ld\n", wfx->nSamplesPerSec);
277
dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
278
dolog ("nBlockAlign = %d\n", wfx->nBlockAlign);
279
dolog ("wBitsPerSample = %d\n", wfx->wBitsPerSample);
280
dolog ("cbSize = %d\n", wfx->cbSize);
284
static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb)
289
for (i = 0; i < conf.restore_retries; ++i) {
290
hr = IDirectSoundBuffer_Restore (dsb);
296
case DSERR_BUFFERLOST:
300
dsound_logerr (hr, "Could not restore playback buffer\n");
305
dolog ("%d attempts to restore playback buffer failed\n", i);
309
#include "dsound_template.h"
311
#include "dsound_template.h"
314
static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp)
319
for (i = 0; i < conf.getstatus_retries; ++i) {
320
hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
322
dsound_logerr (hr, "Could not get playback buffer status\n");
326
if (*statusp & DSERR_BUFFERLOST) {
327
if (dsound_restore_out (dsb)) {
338
static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
343
hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
345
dsound_logerr (hr, "Could not get capture buffer status\n");
352
static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
354
int src_len1 = dst_len;
356
int pos = hw->rpos + dst_len;
357
struct st_sample *src1 = hw->mix_buf + hw->rpos;
358
struct st_sample *src2 = NULL;
360
if (pos > hw->samples) {
361
src_len1 = hw->samples - hw->rpos;
363
src_len2 = dst_len - src_len1;
368
hw->clip (dst, src1, src_len1);
372
dst = advance (dst, src_len1 << hw->info.shift);
373
hw->clip (dst, src2, src_len2);
376
hw->rpos = pos % hw->samples;
379
static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb)
383
DWORD blen1, blen2, len1, len2;
385
err = dsound_lock_out (
389
hw->samples << hw->info.shift,
398
len1 = blen1 >> hw->info.shift;
399
len2 = blen2 >> hw->info.shift;
402
dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
408
audio_pcm_info_clear_buf (&hw->info, p1, len1);
412
audio_pcm_info_clear_buf (&hw->info, p2, len2);
415
dsound_unlock_out (dsb, p1, p2, blen1, blen2);
418
static void dsound_close (dsound *s)
422
if (s->dsound_primary_buffer) {
423
hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer);
425
dsound_logerr (hr, "Could not release primary buffer\n");
427
s->dsound_primary_buffer = NULL;
431
static int dsound_open (dsound *s)
439
hwnd = GetForegroundWindow ();
440
hr = IDirectSound_SetCooperativeLevel (
447
dsound_logerr (hr, "Could not set cooperative level for window %p\n",
452
if (!conf.set_primary) {
456
err = waveformat_from_audio_settings (&wfx, &conf.settings);
461
memset (&dsbd, 0, sizeof (dsbd));
462
dsbd.dwSize = sizeof (dsbd);
463
dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
464
dsbd.dwBufferBytes = 0;
465
dsbd.lpwfxFormat = NULL;
467
hr = IDirectSound_CreateSoundBuffer (
470
&s->dsound_primary_buffer,
474
dsound_logerr (hr, "Could not create primary playback buffer\n");
478
hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx);
480
dsound_logerr (hr, "Could not set primary playback buffer format\n");
483
hr = IDirectSoundBuffer_GetFormat (
484
s->dsound_primary_buffer,
490
dsound_logerr (hr, "Could not get primary playback buffer format\n");
496
print_wave_format (&wfx);
499
err = waveformat_to_audio_settings (&wfx, &s->settings);
511
static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
515
DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
516
LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
519
dolog ("Attempt to control voice without a buffer\n");
525
if (dsound_get_status_out (dsb, &status)) {
529
if (status & DSBSTATUS_PLAYING) {
530
dolog ("warning: Voice is already playing\n");
534
dsound_clear_sample (hw, dsb);
536
hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
538
dsound_logerr (hr, "Could not start playing buffer\n");
544
if (dsound_get_status_out (dsb, &status)) {
548
if (status & DSBSTATUS_PLAYING) {
549
hr = IDirectSoundBuffer_Stop (dsb);
551
dsound_logerr (hr, "Could not stop playing buffer\n");
556
dolog ("warning: Voice is not playing\n");
563
static int dsound_write (SWVoiceOut *sw, void *buf, int len)
565
return audio_pcm_sw_write (sw, buf, len);
568
static int dsound_run_out (HWVoiceOut *hw, int live)
572
DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
573
LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
578
DWORD wpos, ppos, old_pos;
583
dolog ("Attempt to run empty with playback buffer\n");
587
hwshift = hw->info.shift;
588
bufsize = hw->samples << hwshift;
590
hr = IDirectSoundBuffer_GetCurrentPosition (
593
ds->first_time ? &wpos : NULL
596
dsound_logerr (hr, "Could not get playback buffer position\n");
600
len = live << hwshift;
602
if (ds->first_time) {
603
if (conf.latency_millis) {
606
cur_blat = audio_ring_dist (wpos, ppos, bufsize);
610
millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat;
612
old_pos &= ~hw->info.align;
623
if (ds->old_pos == ppos) {
625
dolog ("old_pos == ppos\n");
631
ds->played += audio_ring_dist (ds->old_pos, ppos, hw->bufsize);
633
old_pos = ds->old_pos;
636
if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
637
len = ppos - old_pos;
640
if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) {
641
len = bufsize - old_pos + ppos;
645
if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) {
646
dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n",
647
len, bufsize, old_pos, ppos);
651
len &= ~hw->info.align;
659
err = dsound_lock_out (
672
len1 = blen1 >> hwshift;
673
len2 = blen2 >> hwshift;
677
dsound_write_sample (hw, p1, len1);
681
dsound_write_sample (hw, p2, len2);
684
dsound_unlock_out (dsb, p1, p2, blen1, blen2);
685
ds->old_pos = (old_pos + (decr << hwshift)) % bufsize;
688
ds->mixed += decr << hwshift;
690
dolog ("played %lu mixed %lu diff %ld sec %f\n",
693
ds->mixed - ds->played,
694
abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second);
699
static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
703
DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
704
LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
707
dolog ("Attempt to control capture voice without a buffer\n");
713
if (dsound_get_status_in (dscb, &status)) {
717
if (status & DSCBSTATUS_CAPTURING) {
718
dolog ("warning: Voice is already capturing\n");
724
hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
726
dsound_logerr (hr, "Could not start capturing\n");
732
if (dsound_get_status_in (dscb, &status)) {
736
if (status & DSCBSTATUS_CAPTURING) {
737
hr = IDirectSoundCaptureBuffer_Stop (dscb);
739
dsound_logerr (hr, "Could not stop capturing\n");
744
dolog ("warning: Voice is not capturing\n");
751
static int dsound_read (SWVoiceIn *sw, void *buf, int len)
753
return audio_pcm_sw_read (sw, buf, len);
756
static int dsound_run_in (HWVoiceIn *hw)
760
DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
761
LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
771
dolog ("Attempt to run without capture buffer\n");
775
hwshift = hw->info.shift;
777
live = audio_pcm_hw_get_live_in (hw);
778
dead = hw->samples - live;
783
hr = IDirectSoundCaptureBuffer_GetCurrentPosition (
786
ds->first_time ? &rpos : NULL
789
dsound_logerr (hr, "Could not get capture buffer position\n");
793
if (ds->first_time) {
795
if (rpos & hw->info.align) {
796
ldebug ("warning: Misaligned capture read position %ld(%d)\n",
797
rpos, hw->info.align);
799
hw->wpos = rpos >> hwshift;
802
if (cpos & hw->info.align) {
803
ldebug ("warning: Misaligned capture position %ld(%d)\n",
804
cpos, hw->info.align);
808
len = audio_ring_dist (cpos, hw->wpos, hw->samples);
812
len = audio_MIN (len, dead);
814
err = dsound_lock_in (
829
len1 = blen1 >> hwshift;
830
len2 = blen2 >> hwshift;
834
hw->conv (hw->conv_buf + hw->wpos, p1, len1);
838
hw->conv (hw->conv_buf, p2, len2);
841
dsound_unlock_in (dscb, p1, p2, blen1, blen2);
842
hw->wpos = (hw->wpos + decr) % hw->samples;
846
static void dsound_audio_fini (void *opaque)
855
hr = IDirectSound_Release (s->dsound);
857
dsound_logerr (hr, "Could not release DirectSound\n");
861
if (!s->dsound_capture) {
865
hr = IDirectSoundCapture_Release (s->dsound_capture);
867
dsound_logerr (hr, "Could not release DirectSoundCapture\n");
869
s->dsound_capture = NULL;
872
static void *dsound_audio_init (void)
876
dsound *s = &glob_dsound;
878
hr = CoInitialize (NULL);
880
dsound_logerr (hr, "Could not initialize COM\n");
884
hr = CoCreateInstance (
892
dsound_logerr (hr, "Could not create DirectSound instance\n");
896
hr = IDirectSound_Initialize (s->dsound, NULL);
898
dsound_logerr (hr, "Could not initialize DirectSound\n");
900
hr = IDirectSound_Release (s->dsound);
902
dsound_logerr (hr, "Could not release DirectSound\n");
908
hr = CoCreateInstance (
909
&CLSID_DirectSoundCapture,
912
&IID_IDirectSoundCapture,
913
(void **) &s->dsound_capture
916
dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
919
hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
921
dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
923
hr = IDirectSoundCapture_Release (s->dsound_capture);
925
dsound_logerr (hr, "Could not release DirectSoundCapture\n");
927
s->dsound_capture = NULL;
931
err = dsound_open (s);
933
dsound_audio_fini (s);
940
static struct audio_option dsound_options[] = {
942
.name = "LOCK_RETRIES",
944
.valp = &conf.lock_retries,
945
.descr = "Number of times to attempt locking the buffer"
948
.name = "RESTOURE_RETRIES",
950
.valp = &conf.restore_retries,
951
.descr = "Number of times to attempt restoring the buffer"
954
.name = "GETSTATUS_RETRIES",
956
.valp = &conf.getstatus_retries,
957
.descr = "Number of times to attempt getting status of the buffer"
960
.name = "SET_PRIMARY",
962
.valp = &conf.set_primary,
963
.descr = "Set the parameters of primary buffer"
966
.name = "LATENCY_MILLIS",
968
.valp = &conf.latency_millis,
969
.descr = "(undocumented)"
972
.name = "PRIMARY_FREQ",
974
.valp = &conf.settings.freq,
975
.descr = "Primary buffer frequency"
978
.name = "PRIMARY_CHANNELS",
980
.valp = &conf.settings.nchannels,
981
.descr = "Primary buffer number of channels (1 - mono, 2 - stereo)"
984
.name = "PRIMARY_FMT",
986
.valp = &conf.settings.fmt,
987
.descr = "Primary buffer format"
990
.name = "BUFSIZE_OUT",
992
.valp = &conf.bufsize_out,
993
.descr = "(undocumented)"
996
.name = "BUFSIZE_IN",
998
.valp = &conf.bufsize_in,
999
.descr = "(undocumented)"
1001
{ /* End of list */ }
1004
static struct audio_pcm_ops dsound_pcm_ops = {
1005
.init_out = dsound_init_out,
1006
.fini_out = dsound_fini_out,
1007
.run_out = dsound_run_out,
1008
.write = dsound_write,
1009
.ctl_out = dsound_ctl_out,
1011
.init_in = dsound_init_in,
1012
.fini_in = dsound_fini_in,
1013
.run_in = dsound_run_in,
1014
.read = dsound_read,
1015
.ctl_in = dsound_ctl_in
1018
struct audio_driver dsound_audio_driver = {
1020
.descr = "DirectSound http://wikipedia.org/wiki/DirectSound",
1021
.options = dsound_options,
1022
.init = dsound_audio_init,
1023
.fini = dsound_audio_fini,
1024
.pcm_ops = &dsound_pcm_ops,
1025
.can_be_default = 1,
1026
.max_voices_out = INT_MAX,
1028
.voice_size_out = sizeof (DSoundVoiceOut),
1029
.voice_size_in = sizeof (DSoundVoiceIn)