xref: /openbmc/qemu/audio/dsoundaudio.c (revision 174702986c04180e0c05cddbaf6b2547a07310eb)
11d14ffa9Sbellard /*
21d14ffa9Sbellard  * QEMU DirectSound audio driver
31d14ffa9Sbellard  *
41d14ffa9Sbellard  * Copyright (c) 2005 Vassili Karpov (malc)
51d14ffa9Sbellard  *
61d14ffa9Sbellard  * Permission is hereby granted, free of charge, to any person obtaining a copy
71d14ffa9Sbellard  * of this software and associated documentation files (the "Software"), to deal
81d14ffa9Sbellard  * in the Software without restriction, including without limitation the rights
91d14ffa9Sbellard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
101d14ffa9Sbellard  * copies of the Software, and to permit persons to whom the Software is
111d14ffa9Sbellard  * furnished to do so, subject to the following conditions:
121d14ffa9Sbellard  *
131d14ffa9Sbellard  * The above copyright notice and this permission notice shall be included in
141d14ffa9Sbellard  * all copies or substantial portions of the Software.
151d14ffa9Sbellard  *
161d14ffa9Sbellard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
171d14ffa9Sbellard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
181d14ffa9Sbellard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
191d14ffa9Sbellard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
201d14ffa9Sbellard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
211d14ffa9Sbellard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
221d14ffa9Sbellard  * THE SOFTWARE.
231d14ffa9Sbellard  */
241d14ffa9Sbellard 
251d14ffa9Sbellard /*
261d14ffa9Sbellard  * SEAL 1.07 by Carlos 'pel' Hasan was used as documentation
271d14ffa9Sbellard  */
281d14ffa9Sbellard 
296086a565SPeter Maydell #include "qemu/osdep.h"
30749bc4bfSpbrook #include "audio.h"
311d14ffa9Sbellard 
321d14ffa9Sbellard #define AUDIO_CAP "dsound"
331d14ffa9Sbellard #include "audio_int.h"
344a3b8b34SKővágó, Zoltán #include "qemu/host-utils.h"
350b8fa32fSMarkus Armbruster #include "qemu/module.h"
361d14ffa9Sbellard 
371d14ffa9Sbellard #include <windows.h>
384fddf62aSths #include <mmsystem.h>
391d14ffa9Sbellard #include <objbase.h>
401d14ffa9Sbellard #include <dsound.h>
411d14ffa9Sbellard 
42d5631638Smalc #include "audio_win_int.h"
43d5631638Smalc 
441d14ffa9Sbellard /* #define DEBUG_DSOUND */
451d14ffa9Sbellard 
46191e1f0aSKővágó, Zoltán typedef struct {
471d14ffa9Sbellard     LPDIRECTSOUND dsound;
481d14ffa9Sbellard     LPDIRECTSOUNDCAPTURE dsound_capture;
491ea879e5Smalc     struct audsettings settings;
504a3b8b34SKővágó, Zoltán     Audiodev *dev;
511d14ffa9Sbellard } dsound;
521d14ffa9Sbellard 
531d14ffa9Sbellard typedef struct {
541d14ffa9Sbellard     HWVoiceOut hw;
551d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsound_buffer;
56fb35c2ceSKővágó, Zoltán     bool first_time;
57191e1f0aSKővágó, Zoltán     dsound *s;
581d14ffa9Sbellard } DSoundVoiceOut;
591d14ffa9Sbellard 
601d14ffa9Sbellard typedef struct {
611d14ffa9Sbellard     HWVoiceIn hw;
621d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
63fb35c2ceSKővágó, Zoltán     bool first_time;
64191e1f0aSKővágó, Zoltán     dsound *s;
651d14ffa9Sbellard } DSoundVoiceIn;
661d14ffa9Sbellard 
671d14ffa9Sbellard static void dsound_log_hresult (HRESULT hr)
681d14ffa9Sbellard {
691d14ffa9Sbellard     const char *str = "BUG";
701d14ffa9Sbellard 
711d14ffa9Sbellard     switch (hr) {
721d14ffa9Sbellard     case DS_OK:
731d14ffa9Sbellard         str = "The method succeeded";
741d14ffa9Sbellard         break;
751d14ffa9Sbellard #ifdef DS_NO_VIRTUALIZATION
761d14ffa9Sbellard     case DS_NO_VIRTUALIZATION:
771d14ffa9Sbellard         str = "The buffer was created, but another 3D algorithm was substituted";
781d14ffa9Sbellard         break;
791d14ffa9Sbellard #endif
801d14ffa9Sbellard #ifdef DS_INCOMPLETE
811d14ffa9Sbellard     case DS_INCOMPLETE:
821d14ffa9Sbellard         str = "The method succeeded, but not all the optional effects were obtained";
831d14ffa9Sbellard         break;
841d14ffa9Sbellard #endif
851d14ffa9Sbellard #ifdef DSERR_ACCESSDENIED
861d14ffa9Sbellard     case DSERR_ACCESSDENIED:
871d14ffa9Sbellard         str = "The request failed because access was denied";
881d14ffa9Sbellard         break;
891d14ffa9Sbellard #endif
901d14ffa9Sbellard #ifdef DSERR_ALLOCATED
911d14ffa9Sbellard     case DSERR_ALLOCATED:
921d14ffa9Sbellard         str = "The request failed because resources, such as a priority level, were already in use by another caller";
931d14ffa9Sbellard         break;
941d14ffa9Sbellard #endif
951d14ffa9Sbellard #ifdef DSERR_ALREADYINITIALIZED
961d14ffa9Sbellard     case DSERR_ALREADYINITIALIZED:
971d14ffa9Sbellard         str = "The object is already initialized";
981d14ffa9Sbellard         break;
991d14ffa9Sbellard #endif
1001d14ffa9Sbellard #ifdef DSERR_BADFORMAT
1011d14ffa9Sbellard     case DSERR_BADFORMAT:
1021d14ffa9Sbellard         str = "The specified wave format is not supported";
1031d14ffa9Sbellard         break;
1041d14ffa9Sbellard #endif
1051d14ffa9Sbellard #ifdef DSERR_BADSENDBUFFERGUID
1061d14ffa9Sbellard     case DSERR_BADSENDBUFFERGUID:
1071d14ffa9Sbellard         str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
1081d14ffa9Sbellard         break;
1091d14ffa9Sbellard #endif
1101d14ffa9Sbellard #ifdef DSERR_BUFFERLOST
1111d14ffa9Sbellard     case DSERR_BUFFERLOST:
1121d14ffa9Sbellard         str = "The buffer memory has been lost and must be restored";
1131d14ffa9Sbellard         break;
1141d14ffa9Sbellard #endif
1151d14ffa9Sbellard #ifdef DSERR_BUFFERTOOSMALL
1161d14ffa9Sbellard     case DSERR_BUFFERTOOSMALL:
1171d14ffa9Sbellard         str = "The buffer size is not great enough to enable effects processing";
1181d14ffa9Sbellard         break;
1191d14ffa9Sbellard #endif
1201d14ffa9Sbellard #ifdef DSERR_CONTROLUNAVAIL
1211d14ffa9Sbellard     case DSERR_CONTROLUNAVAIL:
1221d14ffa9Sbellard         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";
1231d14ffa9Sbellard         break;
1241d14ffa9Sbellard #endif
1251d14ffa9Sbellard #ifdef DSERR_DS8_REQUIRED
1261d14ffa9Sbellard     case DSERR_DS8_REQUIRED:
1271d14ffa9Sbellard         str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
1281d14ffa9Sbellard         break;
1291d14ffa9Sbellard #endif
1301d14ffa9Sbellard #ifdef DSERR_FXUNAVAILABLE
1311d14ffa9Sbellard     case DSERR_FXUNAVAILABLE:
1321d14ffa9Sbellard         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";
1331d14ffa9Sbellard         break;
1341d14ffa9Sbellard #endif
1351d14ffa9Sbellard #ifdef DSERR_GENERIC
1361d14ffa9Sbellard     case DSERR_GENERIC :
1371d14ffa9Sbellard         str = "An undetermined error occurred inside the DirectSound subsystem";
1381d14ffa9Sbellard         break;
1391d14ffa9Sbellard #endif
1401d14ffa9Sbellard #ifdef DSERR_INVALIDCALL
1411d14ffa9Sbellard     case DSERR_INVALIDCALL:
1421d14ffa9Sbellard         str = "This function is not valid for the current state of this object";
1431d14ffa9Sbellard         break;
1441d14ffa9Sbellard #endif
1451d14ffa9Sbellard #ifdef DSERR_INVALIDPARAM
1461d14ffa9Sbellard     case DSERR_INVALIDPARAM:
1471d14ffa9Sbellard         str = "An invalid parameter was passed to the returning function";
1481d14ffa9Sbellard         break;
1491d14ffa9Sbellard #endif
1501d14ffa9Sbellard #ifdef DSERR_NOAGGREGATION
1511d14ffa9Sbellard     case DSERR_NOAGGREGATION:
1521d14ffa9Sbellard         str = "The object does not support aggregation";
1531d14ffa9Sbellard         break;
1541d14ffa9Sbellard #endif
1551d14ffa9Sbellard #ifdef DSERR_NODRIVER
1561d14ffa9Sbellard     case DSERR_NODRIVER:
1571d14ffa9Sbellard         str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
1581d14ffa9Sbellard         break;
1591d14ffa9Sbellard #endif
1601d14ffa9Sbellard #ifdef DSERR_NOINTERFACE
1611d14ffa9Sbellard     case DSERR_NOINTERFACE:
1621d14ffa9Sbellard         str = "The requested COM interface is not available";
1631d14ffa9Sbellard         break;
1641d14ffa9Sbellard #endif
1651d14ffa9Sbellard #ifdef DSERR_OBJECTNOTFOUND
1661d14ffa9Sbellard     case DSERR_OBJECTNOTFOUND:
1671d14ffa9Sbellard         str = "The requested object was not found";
1681d14ffa9Sbellard         break;
1691d14ffa9Sbellard #endif
1701d14ffa9Sbellard #ifdef DSERR_OTHERAPPHASPRIO
1711d14ffa9Sbellard     case DSERR_OTHERAPPHASPRIO:
1721d14ffa9Sbellard         str = "Another application has a higher priority level, preventing this call from succeeding";
1731d14ffa9Sbellard         break;
1741d14ffa9Sbellard #endif
1751d14ffa9Sbellard #ifdef DSERR_OUTOFMEMORY
1761d14ffa9Sbellard     case DSERR_OUTOFMEMORY:
1771d14ffa9Sbellard         str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
1781d14ffa9Sbellard         break;
1791d14ffa9Sbellard #endif
1801d14ffa9Sbellard #ifdef DSERR_PRIOLEVELNEEDED
1811d14ffa9Sbellard     case DSERR_PRIOLEVELNEEDED:
1821d14ffa9Sbellard         str = "A cooperative level of DSSCL_PRIORITY or higher is required";
1831d14ffa9Sbellard         break;
1841d14ffa9Sbellard #endif
1851d14ffa9Sbellard #ifdef DSERR_SENDLOOP
1861d14ffa9Sbellard     case DSERR_SENDLOOP:
1871d14ffa9Sbellard         str = "A circular loop of send effects was detected";
1881d14ffa9Sbellard         break;
1891d14ffa9Sbellard #endif
1901d14ffa9Sbellard #ifdef DSERR_UNINITIALIZED
1911d14ffa9Sbellard     case DSERR_UNINITIALIZED:
1921d14ffa9Sbellard         str = "The Initialize method has not been called or has not been called successfully before other methods were called";
1931d14ffa9Sbellard         break;
1941d14ffa9Sbellard #endif
1951d14ffa9Sbellard #ifdef DSERR_UNSUPPORTED
1961d14ffa9Sbellard     case DSERR_UNSUPPORTED:
1971d14ffa9Sbellard         str = "The function called is not supported at this time";
1981d14ffa9Sbellard         break;
1991d14ffa9Sbellard #endif
2001d14ffa9Sbellard     default:
2011d14ffa9Sbellard         AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr);
2021d14ffa9Sbellard         return;
2031d14ffa9Sbellard     }
2041d14ffa9Sbellard 
2051d14ffa9Sbellard     AUD_log (AUDIO_CAP, "Reason: %s\n", str);
2061d14ffa9Sbellard }
2071d14ffa9Sbellard 
2081d14ffa9Sbellard static void GCC_FMT_ATTR (2, 3) dsound_logerr (
2091d14ffa9Sbellard     HRESULT hr,
2101d14ffa9Sbellard     const char *fmt,
2111d14ffa9Sbellard     ...
2121d14ffa9Sbellard     )
2131d14ffa9Sbellard {
2141d14ffa9Sbellard     va_list ap;
2151d14ffa9Sbellard 
2161d14ffa9Sbellard     va_start (ap, fmt);
2171d14ffa9Sbellard     AUD_vlog (AUDIO_CAP, fmt, ap);
2181d14ffa9Sbellard     va_end (ap);
2191d14ffa9Sbellard 
2201d14ffa9Sbellard     dsound_log_hresult (hr);
2211d14ffa9Sbellard }
2221d14ffa9Sbellard 
2231d14ffa9Sbellard static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
2241d14ffa9Sbellard     HRESULT hr,
2251d14ffa9Sbellard     const char *typ,
2261d14ffa9Sbellard     const char *fmt,
2271d14ffa9Sbellard     ...
2281d14ffa9Sbellard     )
2291d14ffa9Sbellard {
2301d14ffa9Sbellard     va_list ap;
2311d14ffa9Sbellard 
232c0fe3827Sbellard     AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
2331d14ffa9Sbellard     va_start (ap, fmt);
2341d14ffa9Sbellard     AUD_vlog (AUDIO_CAP, fmt, ap);
2351d14ffa9Sbellard     va_end (ap);
2361d14ffa9Sbellard 
2371d14ffa9Sbellard     dsound_log_hresult (hr);
2381d14ffa9Sbellard }
2391d14ffa9Sbellard 
2401d14ffa9Sbellard #ifdef DEBUG_DSOUND
2411d14ffa9Sbellard static void print_wave_format (WAVEFORMATEX *wfx)
2421d14ffa9Sbellard {
2431d14ffa9Sbellard     dolog ("tag             = %d\n", wfx->wFormatTag);
2441d14ffa9Sbellard     dolog ("nChannels       = %d\n", wfx->nChannels);
2451d14ffa9Sbellard     dolog ("nSamplesPerSec  = %ld\n", wfx->nSamplesPerSec);
2461d14ffa9Sbellard     dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
2471d14ffa9Sbellard     dolog ("nBlockAlign     = %d\n", wfx->nBlockAlign);
2481d14ffa9Sbellard     dolog ("wBitsPerSample  = %d\n", wfx->wBitsPerSample);
2491d14ffa9Sbellard     dolog ("cbSize          = %d\n", wfx->cbSize);
2501d14ffa9Sbellard }
2511d14ffa9Sbellard #endif
2521d14ffa9Sbellard 
253191e1f0aSKővágó, Zoltán static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
2541d14ffa9Sbellard {
2551d14ffa9Sbellard     HRESULT hr;
2561d14ffa9Sbellard 
2571d14ffa9Sbellard     hr = IDirectSoundBuffer_Restore (dsb);
2581d14ffa9Sbellard 
2592762955fSKővágó, Zoltán     if (hr != DS_OK) {
260c0fe3827Sbellard         dsound_logerr (hr, "Could not restore playback buffer\n");
2611d14ffa9Sbellard         return -1;
2621d14ffa9Sbellard     }
2632762955fSKővágó, Zoltán     return 0;
2641d14ffa9Sbellard }
2651d14ffa9Sbellard 
2661d14ffa9Sbellard #include "dsound_template.h"
2671d14ffa9Sbellard #define DSBTYPE_IN
2681d14ffa9Sbellard #include "dsound_template.h"
2691d14ffa9Sbellard #undef DSBTYPE_IN
2701d14ffa9Sbellard 
271191e1f0aSKővágó, Zoltán static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp,
272191e1f0aSKővágó, Zoltán                                   dsound *s)
2731d14ffa9Sbellard {
2741d14ffa9Sbellard     HRESULT hr;
2751d14ffa9Sbellard 
2761d14ffa9Sbellard     hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
2771d14ffa9Sbellard     if (FAILED (hr)) {
278c0fe3827Sbellard         dsound_logerr (hr, "Could not get playback buffer status\n");
2791d14ffa9Sbellard         return -1;
2801d14ffa9Sbellard     }
2811d14ffa9Sbellard 
2824ba664cbSVolker Rümelin     if (*statusp & DSBSTATUS_BUFFERLOST) {
2832762955fSKővágó, Zoltán         dsound_restore_out(dsb, s);
2841d14ffa9Sbellard         return -1;
2851d14ffa9Sbellard     }
2861d14ffa9Sbellard 
2871d14ffa9Sbellard     return 0;
2881d14ffa9Sbellard }
2891d14ffa9Sbellard 
2901d14ffa9Sbellard static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
2911d14ffa9Sbellard                                  DWORD *statusp)
2921d14ffa9Sbellard {
2931d14ffa9Sbellard     HRESULT hr;
2941d14ffa9Sbellard 
2951d14ffa9Sbellard     hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
2961d14ffa9Sbellard     if (FAILED (hr)) {
297c0fe3827Sbellard         dsound_logerr (hr, "Could not get capture buffer status\n");
2981d14ffa9Sbellard         return -1;
2991d14ffa9Sbellard     }
3001d14ffa9Sbellard 
3011d14ffa9Sbellard     return 0;
3021d14ffa9Sbellard }
3031d14ffa9Sbellard 
304191e1f0aSKővágó, Zoltán static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
305191e1f0aSKővágó, Zoltán                                  dsound *s)
3061d14ffa9Sbellard {
3071d14ffa9Sbellard     int err;
3081d14ffa9Sbellard     LPVOID p1, p2;
3091d14ffa9Sbellard     DWORD blen1, blen2, len1, len2;
3101d14ffa9Sbellard 
3111d14ffa9Sbellard     err = dsound_lock_out (
3121d14ffa9Sbellard         dsb,
3131d14ffa9Sbellard         &hw->info,
3141d14ffa9Sbellard         0,
3157fa9754aSKővágó, Zoltán         hw->size_emul,
3161d14ffa9Sbellard         &p1, &p2,
3171d14ffa9Sbellard         &blen1, &blen2,
318191e1f0aSKővágó, Zoltán         1,
319191e1f0aSKővágó, Zoltán         s
3201d14ffa9Sbellard         );
3211d14ffa9Sbellard     if (err) {
3221d14ffa9Sbellard         return;
3231d14ffa9Sbellard     }
3241d14ffa9Sbellard 
3252b9cce8cSKővágó, Zoltán     len1 = blen1 / hw->info.bytes_per_frame;
3262b9cce8cSKővágó, Zoltán     len2 = blen2 / hw->info.bytes_per_frame;
3271d14ffa9Sbellard 
3281d14ffa9Sbellard #ifdef DEBUG_DSOUND
3291d14ffa9Sbellard     dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
3301d14ffa9Sbellard            p1, blen1, len1,
3311d14ffa9Sbellard            p2, blen2, len2);
3321d14ffa9Sbellard #endif
3331d14ffa9Sbellard 
3341d14ffa9Sbellard     if (p1 && len1) {
3351d14ffa9Sbellard         audio_pcm_info_clear_buf (&hw->info, p1, len1);
3361d14ffa9Sbellard     }
3371d14ffa9Sbellard 
3381d14ffa9Sbellard     if (p2 && len2) {
3391d14ffa9Sbellard         audio_pcm_info_clear_buf (&hw->info, p2, len2);
3401d14ffa9Sbellard     }
3411d14ffa9Sbellard 
3421d14ffa9Sbellard     dsound_unlock_out (dsb, p1, p2, blen1, blen2);
3431d14ffa9Sbellard }
3441d14ffa9Sbellard 
3451d14ffa9Sbellard static int dsound_open (dsound *s)
3461d14ffa9Sbellard {
3471d14ffa9Sbellard     HRESULT hr;
3481d14ffa9Sbellard     HWND hwnd;
3491d14ffa9Sbellard 
3501d14ffa9Sbellard     hwnd = GetForegroundWindow ();
3511d14ffa9Sbellard     hr = IDirectSound_SetCooperativeLevel (
3521d14ffa9Sbellard         s->dsound,
3531d14ffa9Sbellard         hwnd,
3541d14ffa9Sbellard         DSSCL_PRIORITY
3551d14ffa9Sbellard         );
3561d14ffa9Sbellard 
3571d14ffa9Sbellard     if (FAILED (hr)) {
358c0fe3827Sbellard         dsound_logerr (hr, "Could not set cooperative level for window %p\n",
3591d14ffa9Sbellard                        hwnd);
3601d14ffa9Sbellard         return -1;
3611d14ffa9Sbellard     }
3621d14ffa9Sbellard 
3631d14ffa9Sbellard     return 0;
3641d14ffa9Sbellard }
3651d14ffa9Sbellard 
366571a8c52SKővágó, Zoltán static void dsound_enable_out(HWVoiceOut *hw, bool enable)
3671d14ffa9Sbellard {
3681d14ffa9Sbellard     HRESULT hr;
3691d14ffa9Sbellard     DWORD status;
3701d14ffa9Sbellard     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
3711d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
372191e1f0aSKővágó, Zoltán     dsound *s = ds->s;
3731d14ffa9Sbellard 
3741d14ffa9Sbellard     if (!dsb) {
3751d14ffa9Sbellard         dolog ("Attempt to control voice without a buffer\n");
376571a8c52SKővágó, Zoltán         return;
3771d14ffa9Sbellard     }
3781d14ffa9Sbellard 
379571a8c52SKővágó, Zoltán     if (enable) {
380191e1f0aSKővágó, Zoltán         if (dsound_get_status_out (dsb, &status, s)) {
381571a8c52SKővágó, Zoltán             return;
3821d14ffa9Sbellard         }
3831d14ffa9Sbellard 
3841d14ffa9Sbellard         if (status & DSBSTATUS_PLAYING) {
385c0fe3827Sbellard             dolog ("warning: Voice is already playing\n");
386571a8c52SKővágó, Zoltán             return;
3871d14ffa9Sbellard         }
3881d14ffa9Sbellard 
389191e1f0aSKővágó, Zoltán         dsound_clear_sample (hw, dsb, s);
3901d14ffa9Sbellard 
3911d14ffa9Sbellard         hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
3921d14ffa9Sbellard         if (FAILED (hr)) {
393c0fe3827Sbellard             dsound_logerr (hr, "Could not start playing buffer\n");
394571a8c52SKővágó, Zoltán             return;
3951d14ffa9Sbellard         }
396571a8c52SKővágó, Zoltán     } else {
397191e1f0aSKővágó, Zoltán         if (dsound_get_status_out (dsb, &status, s)) {
398571a8c52SKővágó, Zoltán             return;
3991d14ffa9Sbellard         }
4001d14ffa9Sbellard 
4011d14ffa9Sbellard         if (status & DSBSTATUS_PLAYING) {
4021d14ffa9Sbellard             hr = IDirectSoundBuffer_Stop (dsb);
4031d14ffa9Sbellard             if (FAILED (hr)) {
404c0fe3827Sbellard                 dsound_logerr (hr, "Could not stop playing buffer\n");
405571a8c52SKővágó, Zoltán                 return;
4061d14ffa9Sbellard             }
4071d14ffa9Sbellard         }
4081d14ffa9Sbellard         else {
409c0fe3827Sbellard             dolog ("warning: Voice is not playing\n");
4101d14ffa9Sbellard         }
4111d14ffa9Sbellard     }
4121d14ffa9Sbellard }
4131d14ffa9Sbellard 
4147fa9754aSKővágó, Zoltán static void *dsound_get_buffer_out(HWVoiceOut *hw, size_t *size)
4151d14ffa9Sbellard {
4161d14ffa9Sbellard     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
4171d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
4187fa9754aSKővágó, Zoltán     HRESULT hr;
419fb35c2ceSKővágó, Zoltán     DWORD ppos, wpos, act_size;
4207fa9754aSKővágó, Zoltán     size_t req_size;
4217fa9754aSKővágó, Zoltán     int err;
4227fa9754aSKővágó, Zoltán     void *ret;
4231d14ffa9Sbellard 
424fb35c2ceSKővágó, Zoltán     hr = IDirectSoundBuffer_GetCurrentPosition(
425fb35c2ceSKővágó, Zoltán         dsb, &ppos, ds->first_time ? &wpos : NULL);
4261d14ffa9Sbellard     if (FAILED(hr)) {
427c0fe3827Sbellard         dsound_logerr(hr, "Could not get playback buffer position\n");
4287fa9754aSKővágó, Zoltán         *size = 0;
4297fa9754aSKővágó, Zoltán         return NULL;
4301d14ffa9Sbellard     }
4311d14ffa9Sbellard 
432fb35c2ceSKővágó, Zoltán     if (ds->first_time) {
433fb35c2ceSKővágó, Zoltán         hw->pos_emul = wpos;
434fb35c2ceSKővágó, Zoltán         ds->first_time = false;
435fb35c2ceSKővágó, Zoltán     }
436fb35c2ceSKővágó, Zoltán 
4377fa9754aSKővágó, Zoltán     req_size = audio_ring_dist(ppos, hw->pos_emul, hw->size_emul);
4387fa9754aSKővágó, Zoltán     req_size = MIN(req_size, hw->size_emul - hw->pos_emul);
4391d14ffa9Sbellard 
440fb35c2ceSKővágó, Zoltán     if (req_size == 0) {
441fb35c2ceSKővágó, Zoltán         *size = 0;
442fb35c2ceSKővágó, Zoltán         return NULL;
443fb35c2ceSKővágó, Zoltán     }
444fb35c2ceSKővágó, Zoltán 
4457fa9754aSKővágó, Zoltán     err = dsound_lock_out(dsb, &hw->info, hw->pos_emul, req_size, &ret, NULL,
4467fa9754aSKővágó, Zoltán                           &act_size, NULL, false, ds->s);
4471d14ffa9Sbellard     if (err) {
4487fa9754aSKővágó, Zoltán         dolog("Failed to lock buffer\n");
4497fa9754aSKővágó, Zoltán         *size = 0;
4507fa9754aSKővágó, Zoltán         return NULL;
4517fa9754aSKővágó, Zoltán     }
4527fa9754aSKővágó, Zoltán 
4537fa9754aSKővágó, Zoltán     *size = act_size;
4547fa9754aSKővágó, Zoltán     return ret;
4557fa9754aSKővágó, Zoltán }
4567fa9754aSKővágó, Zoltán 
4577fa9754aSKővágó, Zoltán static size_t dsound_put_buffer_out(HWVoiceOut *hw, void *buf, size_t len)
4587fa9754aSKővágó, Zoltán {
4597fa9754aSKővágó, Zoltán     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
4607fa9754aSKővágó, Zoltán     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
4617fa9754aSKővágó, Zoltán     int err = dsound_unlock_out(dsb, buf, NULL, len, 0);
4627fa9754aSKővágó, Zoltán 
4637fa9754aSKővágó, Zoltán     if (err) {
4647fa9754aSKővágó, Zoltán         dolog("Failed to unlock buffer!!\n");
4651d14ffa9Sbellard         return 0;
4661d14ffa9Sbellard     }
4677fa9754aSKővágó, Zoltán     hw->pos_emul = (hw->pos_emul + len) % hw->size_emul;
4681d14ffa9Sbellard 
4697fa9754aSKővágó, Zoltán     return len;
4701d14ffa9Sbellard }
4711d14ffa9Sbellard 
472571a8c52SKővágó, Zoltán static void dsound_enable_in(HWVoiceIn *hw, bool enable)
4731d14ffa9Sbellard {
4741d14ffa9Sbellard     HRESULT hr;
4751d14ffa9Sbellard     DWORD status;
4761d14ffa9Sbellard     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
4771d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
4781d14ffa9Sbellard 
4791d14ffa9Sbellard     if (!dscb) {
4801d14ffa9Sbellard         dolog ("Attempt to control capture voice without a buffer\n");
481571a8c52SKővágó, Zoltán         return;
4821d14ffa9Sbellard     }
4831d14ffa9Sbellard 
484571a8c52SKővágó, Zoltán     if (enable) {
4851d14ffa9Sbellard         if (dsound_get_status_in (dscb, &status)) {
486571a8c52SKővágó, Zoltán             return;
4871d14ffa9Sbellard         }
4881d14ffa9Sbellard 
4891d14ffa9Sbellard         if (status & DSCBSTATUS_CAPTURING) {
490c0fe3827Sbellard             dolog ("warning: Voice is already capturing\n");
491571a8c52SKővágó, Zoltán             return;
4921d14ffa9Sbellard         }
4931d14ffa9Sbellard 
4941d14ffa9Sbellard         /* clear ?? */
4951d14ffa9Sbellard 
4961d14ffa9Sbellard         hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
4971d14ffa9Sbellard         if (FAILED (hr)) {
498c0fe3827Sbellard             dsound_logerr (hr, "Could not start capturing\n");
499571a8c52SKővágó, Zoltán             return;
5001d14ffa9Sbellard         }
501571a8c52SKővágó, Zoltán     } else {
5021d14ffa9Sbellard         if (dsound_get_status_in (dscb, &status)) {
503571a8c52SKővágó, Zoltán             return;
5041d14ffa9Sbellard         }
5051d14ffa9Sbellard 
5061d14ffa9Sbellard         if (status & DSCBSTATUS_CAPTURING) {
5071d14ffa9Sbellard             hr = IDirectSoundCaptureBuffer_Stop (dscb);
5081d14ffa9Sbellard             if (FAILED (hr)) {
509c0fe3827Sbellard                 dsound_logerr (hr, "Could not stop capturing\n");
510571a8c52SKővágó, Zoltán                 return;
5111d14ffa9Sbellard             }
5121d14ffa9Sbellard         }
5131d14ffa9Sbellard         else {
514c0fe3827Sbellard             dolog ("warning: Voice is not capturing\n");
5151d14ffa9Sbellard         }
5161d14ffa9Sbellard     }
5171d14ffa9Sbellard }
5181d14ffa9Sbellard 
5197fa9754aSKővágó, Zoltán static void *dsound_get_buffer_in(HWVoiceIn *hw, size_t *size)
5201d14ffa9Sbellard {
5211d14ffa9Sbellard     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
5221d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
5237fa9754aSKővágó, Zoltán     HRESULT hr;
524fb35c2ceSKővágó, Zoltán     DWORD cpos, rpos, act_size;
5257fa9754aSKővágó, Zoltán     size_t req_size;
5267fa9754aSKővágó, Zoltán     int err;
5277fa9754aSKővágó, Zoltán     void *ret;
5281d14ffa9Sbellard 
529fb35c2ceSKővágó, Zoltán     hr = IDirectSoundCaptureBuffer_GetCurrentPosition(
530fb35c2ceSKővágó, Zoltán         dscb, &cpos, ds->first_time ? &rpos : NULL);
5311d14ffa9Sbellard     if (FAILED(hr)) {
532c0fe3827Sbellard         dsound_logerr(hr, "Could not get capture buffer position\n");
5337fa9754aSKővágó, Zoltán         *size = 0;
5347fa9754aSKővágó, Zoltán         return NULL;
5351d14ffa9Sbellard     }
5361d14ffa9Sbellard 
537fb35c2ceSKővágó, Zoltán     if (ds->first_time) {
538fb35c2ceSKővágó, Zoltán         hw->pos_emul = rpos;
539fb35c2ceSKővágó, Zoltán         ds->first_time = false;
540fb35c2ceSKővágó, Zoltán     }
541fb35c2ceSKővágó, Zoltán 
5427fa9754aSKővágó, Zoltán     req_size = audio_ring_dist(cpos, hw->pos_emul, hw->size_emul);
5437fa9754aSKővágó, Zoltán     req_size = MIN(req_size, hw->size_emul - hw->pos_emul);
5441d14ffa9Sbellard 
545*17470298SVolker Rümelin     if (req_size == 0) {
546*17470298SVolker Rümelin         *size = 0;
547*17470298SVolker Rümelin         return NULL;
548*17470298SVolker Rümelin     }
549*17470298SVolker Rümelin 
5507fa9754aSKővágó, Zoltán     err = dsound_lock_in(dscb, &hw->info, hw->pos_emul, req_size, &ret, NULL,
5517fa9754aSKővágó, Zoltán                          &act_size, NULL, false, ds->s);
5521d14ffa9Sbellard     if (err) {
5537fa9754aSKővágó, Zoltán         dolog("Failed to lock buffer\n");
5547fa9754aSKővágó, Zoltán         *size = 0;
5557fa9754aSKővágó, Zoltán         return NULL;
5561d14ffa9Sbellard     }
5571d14ffa9Sbellard 
5587fa9754aSKővágó, Zoltán     *size = act_size;
5597fa9754aSKővágó, Zoltán     return ret;
5601d14ffa9Sbellard }
5611d14ffa9Sbellard 
5627fa9754aSKővágó, Zoltán static void dsound_put_buffer_in(HWVoiceIn *hw, void *buf, size_t len)
5637fa9754aSKővágó, Zoltán {
5647fa9754aSKővágó, Zoltán     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
5657fa9754aSKővágó, Zoltán     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
5667fa9754aSKővágó, Zoltán     int err = dsound_unlock_in(dscb, buf, NULL, len, 0);
5671d14ffa9Sbellard 
5687fa9754aSKővágó, Zoltán     if (err) {
5697fa9754aSKővágó, Zoltán         dolog("Failed to unlock buffer!!\n");
5707fa9754aSKővágó, Zoltán         return;
5717fa9754aSKővágó, Zoltán     }
5727fa9754aSKővágó, Zoltán     hw->pos_emul = (hw->pos_emul + len) % hw->size_emul;
5731d14ffa9Sbellard }
5741d14ffa9Sbellard 
5751d14ffa9Sbellard static void dsound_audio_fini (void *opaque)
5761d14ffa9Sbellard {
5771d14ffa9Sbellard     HRESULT hr;
5781d14ffa9Sbellard     dsound *s = opaque;
5791d14ffa9Sbellard 
5801d14ffa9Sbellard     if (!s->dsound) {
581191e1f0aSKővágó, Zoltán         g_free(s);
5821d14ffa9Sbellard         return;
5831d14ffa9Sbellard     }
5841d14ffa9Sbellard 
5851d14ffa9Sbellard     hr = IDirectSound_Release (s->dsound);
5861d14ffa9Sbellard     if (FAILED (hr)) {
587c0fe3827Sbellard         dsound_logerr (hr, "Could not release DirectSound\n");
5881d14ffa9Sbellard     }
5891d14ffa9Sbellard     s->dsound = NULL;
5901d14ffa9Sbellard 
5911d14ffa9Sbellard     if (!s->dsound_capture) {
592191e1f0aSKővágó, Zoltán         g_free(s);
5931d14ffa9Sbellard         return;
5941d14ffa9Sbellard     }
5951d14ffa9Sbellard 
5961d14ffa9Sbellard     hr = IDirectSoundCapture_Release (s->dsound_capture);
5971d14ffa9Sbellard     if (FAILED (hr)) {
598c0fe3827Sbellard         dsound_logerr (hr, "Could not release DirectSoundCapture\n");
5991d14ffa9Sbellard     }
6001d14ffa9Sbellard     s->dsound_capture = NULL;
601191e1f0aSKővágó, Zoltán 
602191e1f0aSKővágó, Zoltán     g_free(s);
6031d14ffa9Sbellard }
6041d14ffa9Sbellard 
60571830221SKővágó, Zoltán static void *dsound_audio_init(Audiodev *dev)
6061d14ffa9Sbellard {
6071d14ffa9Sbellard     int err;
6081d14ffa9Sbellard     HRESULT hr;
609191e1f0aSKővágó, Zoltán     dsound *s = g_malloc0(sizeof(dsound));
6104a3b8b34SKővágó, Zoltán     AudiodevDsoundOptions *dso;
6111d14ffa9Sbellard 
6124a3b8b34SKővágó, Zoltán     assert(dev->driver == AUDIODEV_DRIVER_DSOUND);
6134a3b8b34SKővágó, Zoltán     s->dev = dev;
6144a3b8b34SKővágó, Zoltán     dso = &dev->u.dsound;
6154a3b8b34SKővágó, Zoltán 
6164a3b8b34SKővágó, Zoltán     if (!dso->has_latency) {
6174a3b8b34SKővágó, Zoltán         dso->has_latency = true;
6184a3b8b34SKővágó, Zoltán         dso->latency = 10000; /* 10 ms */
6194a3b8b34SKővágó, Zoltán     }
6204a3b8b34SKővágó, Zoltán 
6211d14ffa9Sbellard     hr = CoInitialize (NULL);
6221d14ffa9Sbellard     if (FAILED (hr)) {
623c0fe3827Sbellard         dsound_logerr (hr, "Could not initialize COM\n");
624191e1f0aSKővágó, Zoltán         g_free(s);
6251d14ffa9Sbellard         return NULL;
6261d14ffa9Sbellard     }
6271d14ffa9Sbellard 
6281d14ffa9Sbellard     hr = CoCreateInstance (
6291d14ffa9Sbellard         &CLSID_DirectSound,
6301d14ffa9Sbellard         NULL,
6311d14ffa9Sbellard         CLSCTX_ALL,
6321d14ffa9Sbellard         &IID_IDirectSound,
6331d14ffa9Sbellard         (void **) &s->dsound
6341d14ffa9Sbellard         );
6351d14ffa9Sbellard     if (FAILED (hr)) {
636c0fe3827Sbellard         dsound_logerr (hr, "Could not create DirectSound instance\n");
637191e1f0aSKővágó, Zoltán         g_free(s);
6381d14ffa9Sbellard         return NULL;
6391d14ffa9Sbellard     }
6401d14ffa9Sbellard 
6411d14ffa9Sbellard     hr = IDirectSound_Initialize (s->dsound, NULL);
6421d14ffa9Sbellard     if (FAILED (hr)) {
643c0fe3827Sbellard         dsound_logerr (hr, "Could not initialize DirectSound\n");
6448ead62cfSbellard 
6458ead62cfSbellard         hr = IDirectSound_Release (s->dsound);
6468ead62cfSbellard         if (FAILED (hr)) {
6478ead62cfSbellard             dsound_logerr (hr, "Could not release DirectSound\n");
6488ead62cfSbellard         }
649191e1f0aSKővágó, Zoltán         g_free(s);
6501d14ffa9Sbellard         return NULL;
6511d14ffa9Sbellard     }
6521d14ffa9Sbellard 
6531d14ffa9Sbellard     hr = CoCreateInstance (
6541d14ffa9Sbellard         &CLSID_DirectSoundCapture,
6551d14ffa9Sbellard         NULL,
6561d14ffa9Sbellard         CLSCTX_ALL,
6571d14ffa9Sbellard         &IID_IDirectSoundCapture,
6581d14ffa9Sbellard         (void **) &s->dsound_capture
6591d14ffa9Sbellard         );
6601d14ffa9Sbellard     if (FAILED (hr)) {
661c0fe3827Sbellard         dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
6621d14ffa9Sbellard     }
6631d14ffa9Sbellard     else {
6641d14ffa9Sbellard         hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
6651d14ffa9Sbellard         if (FAILED (hr)) {
666c0fe3827Sbellard             dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
6671d14ffa9Sbellard 
6681d14ffa9Sbellard             hr = IDirectSoundCapture_Release (s->dsound_capture);
6691d14ffa9Sbellard             if (FAILED (hr)) {
670c0fe3827Sbellard                 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
6711d14ffa9Sbellard             }
6721d14ffa9Sbellard             s->dsound_capture = NULL;
6731d14ffa9Sbellard         }
6741d14ffa9Sbellard     }
6751d14ffa9Sbellard 
6761d14ffa9Sbellard     err = dsound_open (s);
6771d14ffa9Sbellard     if (err) {
6781d14ffa9Sbellard         dsound_audio_fini (s);
6791d14ffa9Sbellard         return NULL;
6801d14ffa9Sbellard     }
6811d14ffa9Sbellard 
6821d14ffa9Sbellard     return s;
6831d14ffa9Sbellard }
6841d14ffa9Sbellard 
68535f4b58cSblueswir1 static struct audio_pcm_ops dsound_pcm_ops = {
6861dd3e4d1SJuan Quintela     .init_out = dsound_init_out,
6871dd3e4d1SJuan Quintela     .fini_out = dsound_fini_out,
6887fa9754aSKővágó, Zoltán     .write    = audio_generic_write,
6897fa9754aSKővágó, Zoltán     .get_buffer_out = dsound_get_buffer_out,
6907fa9754aSKővágó, Zoltán     .put_buffer_out = dsound_put_buffer_out,
691571a8c52SKővágó, Zoltán     .enable_out = dsound_enable_out,
6921d14ffa9Sbellard 
6931dd3e4d1SJuan Quintela     .init_in  = dsound_init_in,
6941dd3e4d1SJuan Quintela     .fini_in  = dsound_fini_in,
6957fa9754aSKővágó, Zoltán     .read     = audio_generic_read,
6967fa9754aSKővágó, Zoltán     .get_buffer_in = dsound_get_buffer_in,
6977fa9754aSKővágó, Zoltán     .put_buffer_in = dsound_put_buffer_in,
698571a8c52SKővágó, Zoltán     .enable_in = dsound_enable_in,
6991d14ffa9Sbellard };
7001d14ffa9Sbellard 
701d3893a39SGerd Hoffmann static struct audio_driver dsound_audio_driver = {
702bee37f32SJuan Quintela     .name           = "dsound",
703bee37f32SJuan Quintela     .descr          = "DirectSound http://wikipedia.org/wiki/DirectSound",
704bee37f32SJuan Quintela     .init           = dsound_audio_init,
705bee37f32SJuan Quintela     .fini           = dsound_audio_fini,
706bee37f32SJuan Quintela     .pcm_ops        = &dsound_pcm_ops,
707bee37f32SJuan Quintela     .can_be_default = 1,
708bee37f32SJuan Quintela     .max_voices_out = INT_MAX,
709bee37f32SJuan Quintela     .max_voices_in  = 1,
710bee37f32SJuan Quintela     .voice_size_out = sizeof (DSoundVoiceOut),
71115c875a3SConsul     .voice_size_in  = sizeof (DSoundVoiceIn)
7121d14ffa9Sbellard };
713d3893a39SGerd Hoffmann 
714d3893a39SGerd Hoffmann static void register_audio_dsound(void)
715d3893a39SGerd Hoffmann {
716d3893a39SGerd Hoffmann     audio_driver_register(&dsound_audio_driver);
717d3893a39SGerd Hoffmann }
718d3893a39SGerd Hoffmann type_init(register_audio_dsound);
719