xref: /openbmc/qemu/audio/dsoundaudio.c (revision d56316388d09c976ba2ac2ab67a400f98a11116f)
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 
29749bc4bfSpbrook #include "qemu-common.h"
30749bc4bfSpbrook #include "audio.h"
311d14ffa9Sbellard 
321d14ffa9Sbellard #define AUDIO_CAP "dsound"
331d14ffa9Sbellard #include "audio_int.h"
341d14ffa9Sbellard 
351d14ffa9Sbellard #include <windows.h>
364fddf62aSths #include <mmsystem.h>
371d14ffa9Sbellard #include <objbase.h>
381d14ffa9Sbellard #include <dsound.h>
391d14ffa9Sbellard 
40*d5631638Smalc #include "audio_win_int.h"
41*d5631638Smalc 
421d14ffa9Sbellard /* #define DEBUG_DSOUND */
431d14ffa9Sbellard 
441d14ffa9Sbellard static struct {
451d14ffa9Sbellard     int lock_retries;
461d14ffa9Sbellard     int restore_retries;
471d14ffa9Sbellard     int getstatus_retries;
481d14ffa9Sbellard     int set_primary;
491d14ffa9Sbellard     int bufsize_in;
501d14ffa9Sbellard     int bufsize_out;
511ea879e5Smalc     struct audsettings settings;
521d14ffa9Sbellard     int latency_millis;
531d14ffa9Sbellard } conf = {
541a40d5e2SJuan Quintela     .lock_retries       = 1,
551a40d5e2SJuan Quintela     .restore_retries    = 1,
561a40d5e2SJuan Quintela     .getstatus_retries  = 1,
571a40d5e2SJuan Quintela     .set_primary        = 0,
581a40d5e2SJuan Quintela     .bufsize_in         = 16384,
591a40d5e2SJuan Quintela     .bufsize_out        = 16384,
601a40d5e2SJuan Quintela     .settings.freq      = 44100,
611a40d5e2SJuan Quintela     .settings.nchannels = 2,
6215c875a3SConsul     .settings.fmt       = AUD_FMT_S16,
631a40d5e2SJuan Quintela     .latency_millis     = 10
641d14ffa9Sbellard };
651d14ffa9Sbellard 
661d14ffa9Sbellard typedef struct {
671d14ffa9Sbellard     LPDIRECTSOUND dsound;
681d14ffa9Sbellard     LPDIRECTSOUNDCAPTURE dsound_capture;
691d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsound_primary_buffer;
701ea879e5Smalc     struct audsettings settings;
711d14ffa9Sbellard } dsound;
721d14ffa9Sbellard 
731d14ffa9Sbellard static dsound glob_dsound;
741d14ffa9Sbellard 
751d14ffa9Sbellard typedef struct {
761d14ffa9Sbellard     HWVoiceOut hw;
771d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsound_buffer;
781d14ffa9Sbellard     DWORD old_pos;
791d14ffa9Sbellard     int first_time;
801d14ffa9Sbellard #ifdef DEBUG_DSOUND
811d14ffa9Sbellard     DWORD old_ppos;
821d14ffa9Sbellard     DWORD played;
831d14ffa9Sbellard     DWORD mixed;
841d14ffa9Sbellard #endif
851d14ffa9Sbellard } DSoundVoiceOut;
861d14ffa9Sbellard 
871d14ffa9Sbellard typedef struct {
881d14ffa9Sbellard     HWVoiceIn hw;
891d14ffa9Sbellard     int first_time;
901d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
911d14ffa9Sbellard } DSoundVoiceIn;
921d14ffa9Sbellard 
931d14ffa9Sbellard static void dsound_log_hresult (HRESULT hr)
941d14ffa9Sbellard {
951d14ffa9Sbellard     const char *str = "BUG";
961d14ffa9Sbellard 
971d14ffa9Sbellard     switch (hr) {
981d14ffa9Sbellard     case DS_OK:
991d14ffa9Sbellard         str = "The method succeeded";
1001d14ffa9Sbellard         break;
1011d14ffa9Sbellard #ifdef DS_NO_VIRTUALIZATION
1021d14ffa9Sbellard     case DS_NO_VIRTUALIZATION:
1031d14ffa9Sbellard         str = "The buffer was created, but another 3D algorithm was substituted";
1041d14ffa9Sbellard         break;
1051d14ffa9Sbellard #endif
1061d14ffa9Sbellard #ifdef DS_INCOMPLETE
1071d14ffa9Sbellard     case DS_INCOMPLETE:
1081d14ffa9Sbellard         str = "The method succeeded, but not all the optional effects were obtained";
1091d14ffa9Sbellard         break;
1101d14ffa9Sbellard #endif
1111d14ffa9Sbellard #ifdef DSERR_ACCESSDENIED
1121d14ffa9Sbellard     case DSERR_ACCESSDENIED:
1131d14ffa9Sbellard         str = "The request failed because access was denied";
1141d14ffa9Sbellard         break;
1151d14ffa9Sbellard #endif
1161d14ffa9Sbellard #ifdef DSERR_ALLOCATED
1171d14ffa9Sbellard     case DSERR_ALLOCATED:
1181d14ffa9Sbellard         str = "The request failed because resources, such as a priority level, were already in use by another caller";
1191d14ffa9Sbellard         break;
1201d14ffa9Sbellard #endif
1211d14ffa9Sbellard #ifdef DSERR_ALREADYINITIALIZED
1221d14ffa9Sbellard     case DSERR_ALREADYINITIALIZED:
1231d14ffa9Sbellard         str = "The object is already initialized";
1241d14ffa9Sbellard         break;
1251d14ffa9Sbellard #endif
1261d14ffa9Sbellard #ifdef DSERR_BADFORMAT
1271d14ffa9Sbellard     case DSERR_BADFORMAT:
1281d14ffa9Sbellard         str = "The specified wave format is not supported";
1291d14ffa9Sbellard         break;
1301d14ffa9Sbellard #endif
1311d14ffa9Sbellard #ifdef DSERR_BADSENDBUFFERGUID
1321d14ffa9Sbellard     case DSERR_BADSENDBUFFERGUID:
1331d14ffa9Sbellard         str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
1341d14ffa9Sbellard         break;
1351d14ffa9Sbellard #endif
1361d14ffa9Sbellard #ifdef DSERR_BUFFERLOST
1371d14ffa9Sbellard     case DSERR_BUFFERLOST:
1381d14ffa9Sbellard         str = "The buffer memory has been lost and must be restored";
1391d14ffa9Sbellard         break;
1401d14ffa9Sbellard #endif
1411d14ffa9Sbellard #ifdef DSERR_BUFFERTOOSMALL
1421d14ffa9Sbellard     case DSERR_BUFFERTOOSMALL:
1431d14ffa9Sbellard         str = "The buffer size is not great enough to enable effects processing";
1441d14ffa9Sbellard         break;
1451d14ffa9Sbellard #endif
1461d14ffa9Sbellard #ifdef DSERR_CONTROLUNAVAIL
1471d14ffa9Sbellard     case DSERR_CONTROLUNAVAIL:
1481d14ffa9Sbellard         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";
1491d14ffa9Sbellard         break;
1501d14ffa9Sbellard #endif
1511d14ffa9Sbellard #ifdef DSERR_DS8_REQUIRED
1521d14ffa9Sbellard     case DSERR_DS8_REQUIRED:
1531d14ffa9Sbellard         str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
1541d14ffa9Sbellard         break;
1551d14ffa9Sbellard #endif
1561d14ffa9Sbellard #ifdef DSERR_FXUNAVAILABLE
1571d14ffa9Sbellard     case DSERR_FXUNAVAILABLE:
1581d14ffa9Sbellard         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";
1591d14ffa9Sbellard         break;
1601d14ffa9Sbellard #endif
1611d14ffa9Sbellard #ifdef DSERR_GENERIC
1621d14ffa9Sbellard     case DSERR_GENERIC :
1631d14ffa9Sbellard         str = "An undetermined error occurred inside the DirectSound subsystem";
1641d14ffa9Sbellard         break;
1651d14ffa9Sbellard #endif
1661d14ffa9Sbellard #ifdef DSERR_INVALIDCALL
1671d14ffa9Sbellard     case DSERR_INVALIDCALL:
1681d14ffa9Sbellard         str = "This function is not valid for the current state of this object";
1691d14ffa9Sbellard         break;
1701d14ffa9Sbellard #endif
1711d14ffa9Sbellard #ifdef DSERR_INVALIDPARAM
1721d14ffa9Sbellard     case DSERR_INVALIDPARAM:
1731d14ffa9Sbellard         str = "An invalid parameter was passed to the returning function";
1741d14ffa9Sbellard         break;
1751d14ffa9Sbellard #endif
1761d14ffa9Sbellard #ifdef DSERR_NOAGGREGATION
1771d14ffa9Sbellard     case DSERR_NOAGGREGATION:
1781d14ffa9Sbellard         str = "The object does not support aggregation";
1791d14ffa9Sbellard         break;
1801d14ffa9Sbellard #endif
1811d14ffa9Sbellard #ifdef DSERR_NODRIVER
1821d14ffa9Sbellard     case DSERR_NODRIVER:
1831d14ffa9Sbellard         str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
1841d14ffa9Sbellard         break;
1851d14ffa9Sbellard #endif
1861d14ffa9Sbellard #ifdef DSERR_NOINTERFACE
1871d14ffa9Sbellard     case DSERR_NOINTERFACE:
1881d14ffa9Sbellard         str = "The requested COM interface is not available";
1891d14ffa9Sbellard         break;
1901d14ffa9Sbellard #endif
1911d14ffa9Sbellard #ifdef DSERR_OBJECTNOTFOUND
1921d14ffa9Sbellard     case DSERR_OBJECTNOTFOUND:
1931d14ffa9Sbellard         str = "The requested object was not found";
1941d14ffa9Sbellard         break;
1951d14ffa9Sbellard #endif
1961d14ffa9Sbellard #ifdef DSERR_OTHERAPPHASPRIO
1971d14ffa9Sbellard     case DSERR_OTHERAPPHASPRIO:
1981d14ffa9Sbellard         str = "Another application has a higher priority level, preventing this call from succeeding";
1991d14ffa9Sbellard         break;
2001d14ffa9Sbellard #endif
2011d14ffa9Sbellard #ifdef DSERR_OUTOFMEMORY
2021d14ffa9Sbellard     case DSERR_OUTOFMEMORY:
2031d14ffa9Sbellard         str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
2041d14ffa9Sbellard         break;
2051d14ffa9Sbellard #endif
2061d14ffa9Sbellard #ifdef DSERR_PRIOLEVELNEEDED
2071d14ffa9Sbellard     case DSERR_PRIOLEVELNEEDED:
2081d14ffa9Sbellard         str = "A cooperative level of DSSCL_PRIORITY or higher is required";
2091d14ffa9Sbellard         break;
2101d14ffa9Sbellard #endif
2111d14ffa9Sbellard #ifdef DSERR_SENDLOOP
2121d14ffa9Sbellard     case DSERR_SENDLOOP:
2131d14ffa9Sbellard         str = "A circular loop of send effects was detected";
2141d14ffa9Sbellard         break;
2151d14ffa9Sbellard #endif
2161d14ffa9Sbellard #ifdef DSERR_UNINITIALIZED
2171d14ffa9Sbellard     case DSERR_UNINITIALIZED:
2181d14ffa9Sbellard         str = "The Initialize method has not been called or has not been called successfully before other methods were called";
2191d14ffa9Sbellard         break;
2201d14ffa9Sbellard #endif
2211d14ffa9Sbellard #ifdef DSERR_UNSUPPORTED
2221d14ffa9Sbellard     case DSERR_UNSUPPORTED:
2231d14ffa9Sbellard         str = "The function called is not supported at this time";
2241d14ffa9Sbellard         break;
2251d14ffa9Sbellard #endif
2261d14ffa9Sbellard     default:
2271d14ffa9Sbellard         AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr);
2281d14ffa9Sbellard         return;
2291d14ffa9Sbellard     }
2301d14ffa9Sbellard 
2311d14ffa9Sbellard     AUD_log (AUDIO_CAP, "Reason: %s\n", str);
2321d14ffa9Sbellard }
2331d14ffa9Sbellard 
2341d14ffa9Sbellard static void GCC_FMT_ATTR (2, 3) dsound_logerr (
2351d14ffa9Sbellard     HRESULT hr,
2361d14ffa9Sbellard     const char *fmt,
2371d14ffa9Sbellard     ...
2381d14ffa9Sbellard     )
2391d14ffa9Sbellard {
2401d14ffa9Sbellard     va_list ap;
2411d14ffa9Sbellard 
2421d14ffa9Sbellard     va_start (ap, fmt);
2431d14ffa9Sbellard     AUD_vlog (AUDIO_CAP, fmt, ap);
2441d14ffa9Sbellard     va_end (ap);
2451d14ffa9Sbellard 
2461d14ffa9Sbellard     dsound_log_hresult (hr);
2471d14ffa9Sbellard }
2481d14ffa9Sbellard 
2491d14ffa9Sbellard static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
2501d14ffa9Sbellard     HRESULT hr,
2511d14ffa9Sbellard     const char *typ,
2521d14ffa9Sbellard     const char *fmt,
2531d14ffa9Sbellard     ...
2541d14ffa9Sbellard     )
2551d14ffa9Sbellard {
2561d14ffa9Sbellard     va_list ap;
2571d14ffa9Sbellard 
258c0fe3827Sbellard     AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
2591d14ffa9Sbellard     va_start (ap, fmt);
2601d14ffa9Sbellard     AUD_vlog (AUDIO_CAP, fmt, ap);
2611d14ffa9Sbellard     va_end (ap);
2621d14ffa9Sbellard 
2631d14ffa9Sbellard     dsound_log_hresult (hr);
2641d14ffa9Sbellard }
2651d14ffa9Sbellard 
2661d14ffa9Sbellard static DWORD millis_to_bytes (struct audio_pcm_info *info, DWORD millis)
2671d14ffa9Sbellard {
2681d14ffa9Sbellard     return (millis * info->bytes_per_second) / 1000;
2691d14ffa9Sbellard }
2701d14ffa9Sbellard 
2711d14ffa9Sbellard #ifdef DEBUG_DSOUND
2721d14ffa9Sbellard static void print_wave_format (WAVEFORMATEX *wfx)
2731d14ffa9Sbellard {
2741d14ffa9Sbellard     dolog ("tag             = %d\n", wfx->wFormatTag);
2751d14ffa9Sbellard     dolog ("nChannels       = %d\n", wfx->nChannels);
2761d14ffa9Sbellard     dolog ("nSamplesPerSec  = %ld\n", wfx->nSamplesPerSec);
2771d14ffa9Sbellard     dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
2781d14ffa9Sbellard     dolog ("nBlockAlign     = %d\n", wfx->nBlockAlign);
2791d14ffa9Sbellard     dolog ("wBitsPerSample  = %d\n", wfx->wBitsPerSample);
2801d14ffa9Sbellard     dolog ("cbSize          = %d\n", wfx->cbSize);
2811d14ffa9Sbellard }
2821d14ffa9Sbellard #endif
2831d14ffa9Sbellard 
2841d14ffa9Sbellard static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb)
2851d14ffa9Sbellard {
2861d14ffa9Sbellard     HRESULT hr;
2871d14ffa9Sbellard     int i;
2881d14ffa9Sbellard 
2891d14ffa9Sbellard     for (i = 0; i < conf.restore_retries; ++i) {
2901d14ffa9Sbellard         hr = IDirectSoundBuffer_Restore (dsb);
2911d14ffa9Sbellard 
2921d14ffa9Sbellard         switch (hr) {
2931d14ffa9Sbellard         case DS_OK:
2941d14ffa9Sbellard             return 0;
2951d14ffa9Sbellard 
2961d14ffa9Sbellard         case DSERR_BUFFERLOST:
2971d14ffa9Sbellard             continue;
2981d14ffa9Sbellard 
2991d14ffa9Sbellard         default:
300c0fe3827Sbellard             dsound_logerr (hr, "Could not restore playback buffer\n");
3011d14ffa9Sbellard             return -1;
3021d14ffa9Sbellard         }
3031d14ffa9Sbellard     }
3041d14ffa9Sbellard 
3051d14ffa9Sbellard     dolog ("%d attempts to restore playback buffer failed\n", i);
3061d14ffa9Sbellard     return -1;
3071d14ffa9Sbellard }
3081d14ffa9Sbellard 
3091d14ffa9Sbellard #include "dsound_template.h"
3101d14ffa9Sbellard #define DSBTYPE_IN
3111d14ffa9Sbellard #include "dsound_template.h"
3121d14ffa9Sbellard #undef DSBTYPE_IN
3131d14ffa9Sbellard 
3141d14ffa9Sbellard static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp)
3151d14ffa9Sbellard {
3161d14ffa9Sbellard     HRESULT hr;
3171d14ffa9Sbellard     int i;
3181d14ffa9Sbellard 
3191d14ffa9Sbellard     for (i = 0; i < conf.getstatus_retries; ++i) {
3201d14ffa9Sbellard         hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
3211d14ffa9Sbellard         if (FAILED (hr)) {
322c0fe3827Sbellard             dsound_logerr (hr, "Could not get playback buffer status\n");
3231d14ffa9Sbellard             return -1;
3241d14ffa9Sbellard         }
3251d14ffa9Sbellard 
3261d14ffa9Sbellard         if (*statusp & DSERR_BUFFERLOST) {
3271d14ffa9Sbellard             if (dsound_restore_out (dsb)) {
3281d14ffa9Sbellard                 return -1;
3291d14ffa9Sbellard             }
3301d14ffa9Sbellard             continue;
3311d14ffa9Sbellard         }
3321d14ffa9Sbellard         break;
3331d14ffa9Sbellard     }
3341d14ffa9Sbellard 
3351d14ffa9Sbellard     return 0;
3361d14ffa9Sbellard }
3371d14ffa9Sbellard 
3381d14ffa9Sbellard static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
3391d14ffa9Sbellard                                  DWORD *statusp)
3401d14ffa9Sbellard {
3411d14ffa9Sbellard     HRESULT hr;
3421d14ffa9Sbellard 
3431d14ffa9Sbellard     hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
3441d14ffa9Sbellard     if (FAILED (hr)) {
345c0fe3827Sbellard         dsound_logerr (hr, "Could not get capture buffer status\n");
3461d14ffa9Sbellard         return -1;
3471d14ffa9Sbellard     }
3481d14ffa9Sbellard 
3491d14ffa9Sbellard     return 0;
3501d14ffa9Sbellard }
3511d14ffa9Sbellard 
3521d14ffa9Sbellard static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
3531d14ffa9Sbellard {
3541d14ffa9Sbellard     int src_len1 = dst_len;
3551d14ffa9Sbellard     int src_len2 = 0;
3561d14ffa9Sbellard     int pos = hw->rpos + dst_len;
3571ea879e5Smalc     struct st_sample *src1 = hw->mix_buf + hw->rpos;
3581ea879e5Smalc     struct st_sample *src2 = NULL;
3591d14ffa9Sbellard 
3601d14ffa9Sbellard     if (pos > hw->samples) {
3611d14ffa9Sbellard         src_len1 = hw->samples - hw->rpos;
3621d14ffa9Sbellard         src2 = hw->mix_buf;
3631d14ffa9Sbellard         src_len2 = dst_len - src_len1;
3641d14ffa9Sbellard         pos = src_len2;
3651d14ffa9Sbellard     }
3661d14ffa9Sbellard 
3671d14ffa9Sbellard     if (src_len1) {
3681d14ffa9Sbellard         hw->clip (dst, src1, src_len1);
3691d14ffa9Sbellard     }
3701d14ffa9Sbellard 
3711d14ffa9Sbellard     if (src_len2) {
3721d14ffa9Sbellard         dst = advance (dst, src_len1 << hw->info.shift);
3731d14ffa9Sbellard         hw->clip (dst, src2, src_len2);
3741d14ffa9Sbellard     }
3751d14ffa9Sbellard 
3761d14ffa9Sbellard     hw->rpos = pos % hw->samples;
3771d14ffa9Sbellard }
3781d14ffa9Sbellard 
3791d14ffa9Sbellard static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb)
3801d14ffa9Sbellard {
3811d14ffa9Sbellard     int err;
3821d14ffa9Sbellard     LPVOID p1, p2;
3831d14ffa9Sbellard     DWORD blen1, blen2, len1, len2;
3841d14ffa9Sbellard 
3851d14ffa9Sbellard     err = dsound_lock_out (
3861d14ffa9Sbellard         dsb,
3871d14ffa9Sbellard         &hw->info,
3881d14ffa9Sbellard         0,
3891d14ffa9Sbellard         hw->samples << hw->info.shift,
3901d14ffa9Sbellard         &p1, &p2,
3911d14ffa9Sbellard         &blen1, &blen2,
3921d14ffa9Sbellard         1
3931d14ffa9Sbellard         );
3941d14ffa9Sbellard     if (err) {
3951d14ffa9Sbellard         return;
3961d14ffa9Sbellard     }
3971d14ffa9Sbellard 
3981d14ffa9Sbellard     len1 = blen1 >> hw->info.shift;
3991d14ffa9Sbellard     len2 = blen2 >> hw->info.shift;
4001d14ffa9Sbellard 
4011d14ffa9Sbellard #ifdef DEBUG_DSOUND
4021d14ffa9Sbellard     dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
4031d14ffa9Sbellard            p1, blen1, len1,
4041d14ffa9Sbellard            p2, blen2, len2);
4051d14ffa9Sbellard #endif
4061d14ffa9Sbellard 
4071d14ffa9Sbellard     if (p1 && len1) {
4081d14ffa9Sbellard         audio_pcm_info_clear_buf (&hw->info, p1, len1);
4091d14ffa9Sbellard     }
4101d14ffa9Sbellard 
4111d14ffa9Sbellard     if (p2 && len2) {
4121d14ffa9Sbellard         audio_pcm_info_clear_buf (&hw->info, p2, len2);
4131d14ffa9Sbellard     }
4141d14ffa9Sbellard 
4151d14ffa9Sbellard     dsound_unlock_out (dsb, p1, p2, blen1, blen2);
4161d14ffa9Sbellard }
4171d14ffa9Sbellard 
4181d14ffa9Sbellard static void dsound_close (dsound *s)
4191d14ffa9Sbellard {
4201d14ffa9Sbellard     HRESULT hr;
4211d14ffa9Sbellard 
4221d14ffa9Sbellard     if (s->dsound_primary_buffer) {
4231d14ffa9Sbellard         hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer);
4241d14ffa9Sbellard         if (FAILED (hr)) {
425c0fe3827Sbellard             dsound_logerr (hr, "Could not release primary buffer\n");
4261d14ffa9Sbellard         }
4271d14ffa9Sbellard         s->dsound_primary_buffer = NULL;
4281d14ffa9Sbellard     }
4291d14ffa9Sbellard }
4301d14ffa9Sbellard 
4311d14ffa9Sbellard static int dsound_open (dsound *s)
4321d14ffa9Sbellard {
4331d14ffa9Sbellard     int err;
4341d14ffa9Sbellard     HRESULT hr;
4351d14ffa9Sbellard     WAVEFORMATEX wfx;
4361d14ffa9Sbellard     DSBUFFERDESC dsbd;
4371d14ffa9Sbellard     HWND hwnd;
4381d14ffa9Sbellard 
4391d14ffa9Sbellard     hwnd = GetForegroundWindow ();
4401d14ffa9Sbellard     hr = IDirectSound_SetCooperativeLevel (
4411d14ffa9Sbellard         s->dsound,
4421d14ffa9Sbellard         hwnd,
4431d14ffa9Sbellard         DSSCL_PRIORITY
4441d14ffa9Sbellard         );
4451d14ffa9Sbellard 
4461d14ffa9Sbellard     if (FAILED (hr)) {
447c0fe3827Sbellard         dsound_logerr (hr, "Could not set cooperative level for window %p\n",
4481d14ffa9Sbellard                        hwnd);
4491d14ffa9Sbellard         return -1;
4501d14ffa9Sbellard     }
4511d14ffa9Sbellard 
4521d14ffa9Sbellard     if (!conf.set_primary) {
4531d14ffa9Sbellard         return 0;
4541d14ffa9Sbellard     }
4551d14ffa9Sbellard 
456c0fe3827Sbellard     err = waveformat_from_audio_settings (&wfx, &conf.settings);
4571d14ffa9Sbellard     if (err) {
4581d14ffa9Sbellard         return -1;
4591d14ffa9Sbellard     }
4601d14ffa9Sbellard 
4611d14ffa9Sbellard     memset (&dsbd, 0, sizeof (dsbd));
4621d14ffa9Sbellard     dsbd.dwSize = sizeof (dsbd);
4631d14ffa9Sbellard     dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
4641d14ffa9Sbellard     dsbd.dwBufferBytes = 0;
4651d14ffa9Sbellard     dsbd.lpwfxFormat = NULL;
4661d14ffa9Sbellard 
4671d14ffa9Sbellard     hr = IDirectSound_CreateSoundBuffer (
4681d14ffa9Sbellard         s->dsound,
4691d14ffa9Sbellard         &dsbd,
4701d14ffa9Sbellard         &s->dsound_primary_buffer,
4711d14ffa9Sbellard         NULL
4721d14ffa9Sbellard         );
4731d14ffa9Sbellard     if (FAILED (hr)) {
474c0fe3827Sbellard         dsound_logerr (hr, "Could not create primary playback buffer\n");
4751d14ffa9Sbellard         return -1;
4761d14ffa9Sbellard     }
4771d14ffa9Sbellard 
4781d14ffa9Sbellard     hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx);
4791d14ffa9Sbellard     if (FAILED (hr)) {
480c0fe3827Sbellard         dsound_logerr (hr, "Could not set primary playback buffer format\n");
4811d14ffa9Sbellard     }
4821d14ffa9Sbellard 
4831d14ffa9Sbellard     hr = IDirectSoundBuffer_GetFormat (
4841d14ffa9Sbellard         s->dsound_primary_buffer,
4851d14ffa9Sbellard         &wfx,
4861d14ffa9Sbellard         sizeof (wfx),
4871d14ffa9Sbellard         NULL
4881d14ffa9Sbellard         );
4891d14ffa9Sbellard     if (FAILED (hr)) {
490c0fe3827Sbellard         dsound_logerr (hr, "Could not get primary playback buffer format\n");
4911d14ffa9Sbellard         goto fail0;
4921d14ffa9Sbellard     }
4931d14ffa9Sbellard 
4941d14ffa9Sbellard #ifdef DEBUG_DSOUND
4951d14ffa9Sbellard     dolog ("Primary\n");
4961d14ffa9Sbellard     print_wave_format (&wfx);
4971d14ffa9Sbellard #endif
4981d14ffa9Sbellard 
499c0fe3827Sbellard     err = waveformat_to_audio_settings (&wfx, &s->settings);
5001d14ffa9Sbellard     if (err) {
5011d14ffa9Sbellard         goto fail0;
5021d14ffa9Sbellard     }
5031d14ffa9Sbellard 
5041d14ffa9Sbellard     return 0;
5051d14ffa9Sbellard 
5061d14ffa9Sbellard  fail0:
5071d14ffa9Sbellard     dsound_close (s);
5081d14ffa9Sbellard     return -1;
5091d14ffa9Sbellard }
5101d14ffa9Sbellard 
5111d14ffa9Sbellard static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
5121d14ffa9Sbellard {
5131d14ffa9Sbellard     HRESULT hr;
5141d14ffa9Sbellard     DWORD status;
5151d14ffa9Sbellard     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
5161d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
5171d14ffa9Sbellard 
5181d14ffa9Sbellard     if (!dsb) {
5191d14ffa9Sbellard         dolog ("Attempt to control voice without a buffer\n");
5201d14ffa9Sbellard         return 0;
5211d14ffa9Sbellard     }
5221d14ffa9Sbellard 
5231d14ffa9Sbellard     switch (cmd) {
5241d14ffa9Sbellard     case VOICE_ENABLE:
5251d14ffa9Sbellard         if (dsound_get_status_out (dsb, &status)) {
5261d14ffa9Sbellard             return -1;
5271d14ffa9Sbellard         }
5281d14ffa9Sbellard 
5291d14ffa9Sbellard         if (status & DSBSTATUS_PLAYING) {
530c0fe3827Sbellard             dolog ("warning: Voice is already playing\n");
5311d14ffa9Sbellard             return 0;
5321d14ffa9Sbellard         }
5331d14ffa9Sbellard 
5341d14ffa9Sbellard         dsound_clear_sample (hw, dsb);
5351d14ffa9Sbellard 
5361d14ffa9Sbellard         hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
5371d14ffa9Sbellard         if (FAILED (hr)) {
538c0fe3827Sbellard             dsound_logerr (hr, "Could not start playing buffer\n");
5391d14ffa9Sbellard             return -1;
5401d14ffa9Sbellard         }
5411d14ffa9Sbellard         break;
5421d14ffa9Sbellard 
5431d14ffa9Sbellard     case VOICE_DISABLE:
5441d14ffa9Sbellard         if (dsound_get_status_out (dsb, &status)) {
5451d14ffa9Sbellard             return -1;
5461d14ffa9Sbellard         }
5471d14ffa9Sbellard 
5481d14ffa9Sbellard         if (status & DSBSTATUS_PLAYING) {
5491d14ffa9Sbellard             hr = IDirectSoundBuffer_Stop (dsb);
5501d14ffa9Sbellard             if (FAILED (hr)) {
551c0fe3827Sbellard                 dsound_logerr (hr, "Could not stop playing buffer\n");
5521d14ffa9Sbellard                 return -1;
5531d14ffa9Sbellard             }
5541d14ffa9Sbellard         }
5551d14ffa9Sbellard         else {
556c0fe3827Sbellard             dolog ("warning: Voice is not playing\n");
5571d14ffa9Sbellard         }
5581d14ffa9Sbellard         break;
5591d14ffa9Sbellard     }
5601d14ffa9Sbellard     return 0;
5611d14ffa9Sbellard }
5621d14ffa9Sbellard 
5631d14ffa9Sbellard static int dsound_write (SWVoiceOut *sw, void *buf, int len)
5641d14ffa9Sbellard {
5651d14ffa9Sbellard     return audio_pcm_sw_write (sw, buf, len);
5661d14ffa9Sbellard }
5671d14ffa9Sbellard 
568bdff253cSmalc static int dsound_run_out (HWVoiceOut *hw, int live)
5691d14ffa9Sbellard {
5701d14ffa9Sbellard     int err;
5711d14ffa9Sbellard     HRESULT hr;
5721d14ffa9Sbellard     DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
5731d14ffa9Sbellard     LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
574bdff253cSmalc     int len, hwshift;
5751d14ffa9Sbellard     DWORD blen1, blen2;
5761d14ffa9Sbellard     DWORD len1, len2;
5771d14ffa9Sbellard     DWORD decr;
5781d14ffa9Sbellard     DWORD wpos, ppos, old_pos;
5791d14ffa9Sbellard     LPVOID p1, p2;
580c0fe3827Sbellard     int bufsize;
5811d14ffa9Sbellard 
5821d14ffa9Sbellard     if (!dsb) {
5831d14ffa9Sbellard         dolog ("Attempt to run empty with playback buffer\n");
5841d14ffa9Sbellard         return 0;
5851d14ffa9Sbellard     }
5861d14ffa9Sbellard 
5871d14ffa9Sbellard     hwshift = hw->info.shift;
588c0fe3827Sbellard     bufsize = hw->samples << hwshift;
5891d14ffa9Sbellard 
5901d14ffa9Sbellard     hr = IDirectSoundBuffer_GetCurrentPosition (
5911d14ffa9Sbellard         dsb,
5921d14ffa9Sbellard         &ppos,
5931d14ffa9Sbellard         ds->first_time ? &wpos : NULL
5941d14ffa9Sbellard         );
5951d14ffa9Sbellard     if (FAILED (hr)) {
596c0fe3827Sbellard         dsound_logerr (hr, "Could not get playback buffer position\n");
5971d14ffa9Sbellard         return 0;
5981d14ffa9Sbellard     }
5991d14ffa9Sbellard 
6001d14ffa9Sbellard     len = live << hwshift;
6011d14ffa9Sbellard 
6021d14ffa9Sbellard     if (ds->first_time) {
6031d14ffa9Sbellard         if (conf.latency_millis) {
604c0fe3827Sbellard             DWORD cur_blat;
6051d14ffa9Sbellard 
606c0fe3827Sbellard             cur_blat = audio_ring_dist (wpos, ppos, bufsize);
6071d14ffa9Sbellard             ds->first_time = 0;
6081d14ffa9Sbellard             old_pos = wpos;
6091d14ffa9Sbellard             old_pos +=
6101d14ffa9Sbellard                 millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat;
611c0fe3827Sbellard             old_pos %= bufsize;
6121d14ffa9Sbellard             old_pos &= ~hw->info.align;
6131d14ffa9Sbellard         }
6141d14ffa9Sbellard         else {
6151d14ffa9Sbellard             old_pos = wpos;
6161d14ffa9Sbellard         }
6171d14ffa9Sbellard #ifdef DEBUG_DSOUND
6181d14ffa9Sbellard         ds->played = 0;
6191d14ffa9Sbellard         ds->mixed = 0;
6201d14ffa9Sbellard #endif
6211d14ffa9Sbellard     }
6221d14ffa9Sbellard     else {
6231d14ffa9Sbellard         if (ds->old_pos == ppos) {
6241d14ffa9Sbellard #ifdef DEBUG_DSOUND
6251d14ffa9Sbellard             dolog ("old_pos == ppos\n");
6261d14ffa9Sbellard #endif
6271d14ffa9Sbellard             return 0;
6281d14ffa9Sbellard         }
6291d14ffa9Sbellard 
6301d14ffa9Sbellard #ifdef DEBUG_DSOUND
6311d14ffa9Sbellard         ds->played += audio_ring_dist (ds->old_pos, ppos, hw->bufsize);
6321d14ffa9Sbellard #endif
6331d14ffa9Sbellard         old_pos = ds->old_pos;
6341d14ffa9Sbellard     }
6351d14ffa9Sbellard 
6361d14ffa9Sbellard     if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
6371d14ffa9Sbellard         len = ppos - old_pos;
6381d14ffa9Sbellard     }
6391d14ffa9Sbellard     else {
640c0fe3827Sbellard         if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) {
641c0fe3827Sbellard             len = bufsize - old_pos + ppos;
6421d14ffa9Sbellard         }
6431d14ffa9Sbellard     }
6441d14ffa9Sbellard 
645c0fe3827Sbellard     if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) {
646c0fe3827Sbellard         dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n",
647c0fe3827Sbellard                len, bufsize, old_pos, ppos);
6481d14ffa9Sbellard         return 0;
6491d14ffa9Sbellard     }
6501d14ffa9Sbellard 
6511d14ffa9Sbellard     len &= ~hw->info.align;
6521d14ffa9Sbellard     if (!len) {
6531d14ffa9Sbellard         return 0;
6541d14ffa9Sbellard     }
6551d14ffa9Sbellard 
6561d14ffa9Sbellard #ifdef DEBUG_DSOUND
6571d14ffa9Sbellard     ds->old_ppos = ppos;
6581d14ffa9Sbellard #endif
6591d14ffa9Sbellard     err = dsound_lock_out (
6601d14ffa9Sbellard         dsb,
6611d14ffa9Sbellard         &hw->info,
6621d14ffa9Sbellard         old_pos,
6631d14ffa9Sbellard         len,
6641d14ffa9Sbellard         &p1, &p2,
6651d14ffa9Sbellard         &blen1, &blen2,
6661d14ffa9Sbellard         0
6671d14ffa9Sbellard         );
6681d14ffa9Sbellard     if (err) {
6691d14ffa9Sbellard         return 0;
6701d14ffa9Sbellard     }
6711d14ffa9Sbellard 
6721d14ffa9Sbellard     len1 = blen1 >> hwshift;
6731d14ffa9Sbellard     len2 = blen2 >> hwshift;
6741d14ffa9Sbellard     decr = len1 + len2;
6751d14ffa9Sbellard 
6761d14ffa9Sbellard     if (p1 && len1) {
6771d14ffa9Sbellard         dsound_write_sample (hw, p1, len1);
6781d14ffa9Sbellard     }
6791d14ffa9Sbellard 
6801d14ffa9Sbellard     if (p2 && len2) {
6811d14ffa9Sbellard         dsound_write_sample (hw, p2, len2);
6821d14ffa9Sbellard     }
6831d14ffa9Sbellard 
6841d14ffa9Sbellard     dsound_unlock_out (dsb, p1, p2, blen1, blen2);
685c0fe3827Sbellard     ds->old_pos = (old_pos + (decr << hwshift)) % bufsize;
6861d14ffa9Sbellard 
6871d14ffa9Sbellard #ifdef DEBUG_DSOUND
6881d14ffa9Sbellard     ds->mixed += decr << hwshift;
6891d14ffa9Sbellard 
6901d14ffa9Sbellard     dolog ("played %lu mixed %lu diff %ld sec %f\n",
6911d14ffa9Sbellard            ds->played,
6921d14ffa9Sbellard            ds->mixed,
6931d14ffa9Sbellard            ds->mixed - ds->played,
6941d14ffa9Sbellard            abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second);
6951d14ffa9Sbellard #endif
6961d14ffa9Sbellard     return decr;
6971d14ffa9Sbellard }
6981d14ffa9Sbellard 
6991d14ffa9Sbellard static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
7001d14ffa9Sbellard {
7011d14ffa9Sbellard     HRESULT hr;
7021d14ffa9Sbellard     DWORD status;
7031d14ffa9Sbellard     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
7041d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
7051d14ffa9Sbellard 
7061d14ffa9Sbellard     if (!dscb) {
7071d14ffa9Sbellard         dolog ("Attempt to control capture voice without a buffer\n");
7081d14ffa9Sbellard         return -1;
7091d14ffa9Sbellard     }
7101d14ffa9Sbellard 
7111d14ffa9Sbellard     switch (cmd) {
7121d14ffa9Sbellard     case VOICE_ENABLE:
7131d14ffa9Sbellard         if (dsound_get_status_in (dscb, &status)) {
7141d14ffa9Sbellard             return -1;
7151d14ffa9Sbellard         }
7161d14ffa9Sbellard 
7171d14ffa9Sbellard         if (status & DSCBSTATUS_CAPTURING) {
718c0fe3827Sbellard             dolog ("warning: Voice is already capturing\n");
7191d14ffa9Sbellard             return 0;
7201d14ffa9Sbellard         }
7211d14ffa9Sbellard 
7221d14ffa9Sbellard         /* clear ?? */
7231d14ffa9Sbellard 
7241d14ffa9Sbellard         hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
7251d14ffa9Sbellard         if (FAILED (hr)) {
726c0fe3827Sbellard             dsound_logerr (hr, "Could not start capturing\n");
7271d14ffa9Sbellard             return -1;
7281d14ffa9Sbellard         }
7291d14ffa9Sbellard         break;
7301d14ffa9Sbellard 
7311d14ffa9Sbellard     case VOICE_DISABLE:
7321d14ffa9Sbellard         if (dsound_get_status_in (dscb, &status)) {
7331d14ffa9Sbellard             return -1;
7341d14ffa9Sbellard         }
7351d14ffa9Sbellard 
7361d14ffa9Sbellard         if (status & DSCBSTATUS_CAPTURING) {
7371d14ffa9Sbellard             hr = IDirectSoundCaptureBuffer_Stop (dscb);
7381d14ffa9Sbellard             if (FAILED (hr)) {
739c0fe3827Sbellard                 dsound_logerr (hr, "Could not stop capturing\n");
7401d14ffa9Sbellard                 return -1;
7411d14ffa9Sbellard             }
7421d14ffa9Sbellard         }
7431d14ffa9Sbellard         else {
744c0fe3827Sbellard             dolog ("warning: Voice is not capturing\n");
7451d14ffa9Sbellard         }
7461d14ffa9Sbellard         break;
7471d14ffa9Sbellard     }
7481d14ffa9Sbellard     return 0;
7491d14ffa9Sbellard }
7501d14ffa9Sbellard 
7511d14ffa9Sbellard static int dsound_read (SWVoiceIn *sw, void *buf, int len)
7521d14ffa9Sbellard {
7531d14ffa9Sbellard     return audio_pcm_sw_read (sw, buf, len);
7541d14ffa9Sbellard }
7551d14ffa9Sbellard 
7561d14ffa9Sbellard static int dsound_run_in (HWVoiceIn *hw)
7571d14ffa9Sbellard {
7581d14ffa9Sbellard     int err;
7591d14ffa9Sbellard     HRESULT hr;
7601d14ffa9Sbellard     DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
7611d14ffa9Sbellard     LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
7621d14ffa9Sbellard     int live, len, dead;
7631d14ffa9Sbellard     DWORD blen1, blen2;
7641d14ffa9Sbellard     DWORD len1, len2;
7651d14ffa9Sbellard     DWORD decr;
7661d14ffa9Sbellard     DWORD cpos, rpos;
7671d14ffa9Sbellard     LPVOID p1, p2;
7681d14ffa9Sbellard     int hwshift;
7691d14ffa9Sbellard 
7701d14ffa9Sbellard     if (!dscb) {
7711d14ffa9Sbellard         dolog ("Attempt to run without capture buffer\n");
7721d14ffa9Sbellard         return 0;
7731d14ffa9Sbellard     }
7741d14ffa9Sbellard 
7751d14ffa9Sbellard     hwshift = hw->info.shift;
7761d14ffa9Sbellard 
7771d14ffa9Sbellard     live = audio_pcm_hw_get_live_in (hw);
7781d14ffa9Sbellard     dead = hw->samples - live;
7791d14ffa9Sbellard     if (!dead) {
7801d14ffa9Sbellard         return 0;
7811d14ffa9Sbellard     }
7821d14ffa9Sbellard 
7831d14ffa9Sbellard     hr = IDirectSoundCaptureBuffer_GetCurrentPosition (
7841d14ffa9Sbellard         dscb,
7851d14ffa9Sbellard         &cpos,
7861d14ffa9Sbellard         ds->first_time ? &rpos : NULL
7871d14ffa9Sbellard         );
7881d14ffa9Sbellard     if (FAILED (hr)) {
789c0fe3827Sbellard         dsound_logerr (hr, "Could not get capture buffer position\n");
7901d14ffa9Sbellard         return 0;
7911d14ffa9Sbellard     }
7921d14ffa9Sbellard 
7931d14ffa9Sbellard     if (ds->first_time) {
7941d14ffa9Sbellard         ds->first_time = 0;
7951d14ffa9Sbellard         if (rpos & hw->info.align) {
796c0fe3827Sbellard             ldebug ("warning: Misaligned capture read position %ld(%d)\n",
7971d14ffa9Sbellard                     rpos, hw->info.align);
7981d14ffa9Sbellard         }
7991d14ffa9Sbellard         hw->wpos = rpos >> hwshift;
8001d14ffa9Sbellard     }
8011d14ffa9Sbellard 
8021d14ffa9Sbellard     if (cpos & hw->info.align) {
803c0fe3827Sbellard         ldebug ("warning: Misaligned capture position %ld(%d)\n",
8041d14ffa9Sbellard                 cpos, hw->info.align);
8051d14ffa9Sbellard     }
8061d14ffa9Sbellard     cpos >>= hwshift;
8071d14ffa9Sbellard 
8081d14ffa9Sbellard     len = audio_ring_dist (cpos, hw->wpos, hw->samples);
8091d14ffa9Sbellard     if (!len) {
8101d14ffa9Sbellard         return 0;
8111d14ffa9Sbellard     }
8121d14ffa9Sbellard     len = audio_MIN (len, dead);
8131d14ffa9Sbellard 
8141d14ffa9Sbellard     err = dsound_lock_in (
8151d14ffa9Sbellard         dscb,
8161d14ffa9Sbellard         &hw->info,
8171d14ffa9Sbellard         hw->wpos << hwshift,
8181d14ffa9Sbellard         len << hwshift,
8191d14ffa9Sbellard         &p1,
8201d14ffa9Sbellard         &p2,
8211d14ffa9Sbellard         &blen1,
8221d14ffa9Sbellard         &blen2,
8231d14ffa9Sbellard         0
8241d14ffa9Sbellard         );
8251d14ffa9Sbellard     if (err) {
8261d14ffa9Sbellard         return 0;
8271d14ffa9Sbellard     }
8281d14ffa9Sbellard 
8291d14ffa9Sbellard     len1 = blen1 >> hwshift;
8301d14ffa9Sbellard     len2 = blen2 >> hwshift;
8311d14ffa9Sbellard     decr = len1 + len2;
8321d14ffa9Sbellard 
8331d14ffa9Sbellard     if (p1 && len1) {
8341d14ffa9Sbellard         hw->conv (hw->conv_buf + hw->wpos, p1, len1, &nominal_volume);
8351d14ffa9Sbellard     }
8361d14ffa9Sbellard 
8371d14ffa9Sbellard     if (p2 && len2) {
8381d14ffa9Sbellard         hw->conv (hw->conv_buf, p2, len2, &nominal_volume);
8391d14ffa9Sbellard     }
8401d14ffa9Sbellard 
8411d14ffa9Sbellard     dsound_unlock_in (dscb, p1, p2, blen1, blen2);
8421d14ffa9Sbellard     hw->wpos = (hw->wpos + decr) % hw->samples;
8431d14ffa9Sbellard     return decr;
8441d14ffa9Sbellard }
8451d14ffa9Sbellard 
8461d14ffa9Sbellard static void dsound_audio_fini (void *opaque)
8471d14ffa9Sbellard {
8481d14ffa9Sbellard     HRESULT hr;
8491d14ffa9Sbellard     dsound *s = opaque;
8501d14ffa9Sbellard 
8511d14ffa9Sbellard     if (!s->dsound) {
8521d14ffa9Sbellard         return;
8531d14ffa9Sbellard     }
8541d14ffa9Sbellard 
8551d14ffa9Sbellard     hr = IDirectSound_Release (s->dsound);
8561d14ffa9Sbellard     if (FAILED (hr)) {
857c0fe3827Sbellard         dsound_logerr (hr, "Could not release DirectSound\n");
8581d14ffa9Sbellard     }
8591d14ffa9Sbellard     s->dsound = NULL;
8601d14ffa9Sbellard 
8611d14ffa9Sbellard     if (!s->dsound_capture) {
8621d14ffa9Sbellard         return;
8631d14ffa9Sbellard     }
8641d14ffa9Sbellard 
8651d14ffa9Sbellard     hr = IDirectSoundCapture_Release (s->dsound_capture);
8661d14ffa9Sbellard     if (FAILED (hr)) {
867c0fe3827Sbellard         dsound_logerr (hr, "Could not release DirectSoundCapture\n");
8681d14ffa9Sbellard     }
8691d14ffa9Sbellard     s->dsound_capture = NULL;
8701d14ffa9Sbellard }
8711d14ffa9Sbellard 
8721d14ffa9Sbellard static void *dsound_audio_init (void)
8731d14ffa9Sbellard {
8741d14ffa9Sbellard     int err;
8751d14ffa9Sbellard     HRESULT hr;
8761d14ffa9Sbellard     dsound *s = &glob_dsound;
8771d14ffa9Sbellard 
8781d14ffa9Sbellard     hr = CoInitialize (NULL);
8791d14ffa9Sbellard     if (FAILED (hr)) {
880c0fe3827Sbellard         dsound_logerr (hr, "Could not initialize COM\n");
8811d14ffa9Sbellard         return NULL;
8821d14ffa9Sbellard     }
8831d14ffa9Sbellard 
8841d14ffa9Sbellard     hr = CoCreateInstance (
8851d14ffa9Sbellard         &CLSID_DirectSound,
8861d14ffa9Sbellard         NULL,
8871d14ffa9Sbellard         CLSCTX_ALL,
8881d14ffa9Sbellard         &IID_IDirectSound,
8891d14ffa9Sbellard         (void **) &s->dsound
8901d14ffa9Sbellard         );
8911d14ffa9Sbellard     if (FAILED (hr)) {
892c0fe3827Sbellard         dsound_logerr (hr, "Could not create DirectSound instance\n");
8931d14ffa9Sbellard         return NULL;
8941d14ffa9Sbellard     }
8951d14ffa9Sbellard 
8961d14ffa9Sbellard     hr = IDirectSound_Initialize (s->dsound, NULL);
8971d14ffa9Sbellard     if (FAILED (hr)) {
898c0fe3827Sbellard         dsound_logerr (hr, "Could not initialize DirectSound\n");
8998ead62cfSbellard 
9008ead62cfSbellard         hr = IDirectSound_Release (s->dsound);
9018ead62cfSbellard         if (FAILED (hr)) {
9028ead62cfSbellard             dsound_logerr (hr, "Could not release DirectSound\n");
9038ead62cfSbellard         }
9048ead62cfSbellard         s->dsound = NULL;
9051d14ffa9Sbellard         return NULL;
9061d14ffa9Sbellard     }
9071d14ffa9Sbellard 
9081d14ffa9Sbellard     hr = CoCreateInstance (
9091d14ffa9Sbellard         &CLSID_DirectSoundCapture,
9101d14ffa9Sbellard         NULL,
9111d14ffa9Sbellard         CLSCTX_ALL,
9121d14ffa9Sbellard         &IID_IDirectSoundCapture,
9131d14ffa9Sbellard         (void **) &s->dsound_capture
9141d14ffa9Sbellard         );
9151d14ffa9Sbellard     if (FAILED (hr)) {
916c0fe3827Sbellard         dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
9171d14ffa9Sbellard     }
9181d14ffa9Sbellard     else {
9191d14ffa9Sbellard         hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
9201d14ffa9Sbellard         if (FAILED (hr)) {
921c0fe3827Sbellard             dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
9221d14ffa9Sbellard 
9231d14ffa9Sbellard             hr = IDirectSoundCapture_Release (s->dsound_capture);
9241d14ffa9Sbellard             if (FAILED (hr)) {
925c0fe3827Sbellard                 dsound_logerr (hr, "Could not release DirectSoundCapture\n");
9261d14ffa9Sbellard             }
9271d14ffa9Sbellard             s->dsound_capture = NULL;
9281d14ffa9Sbellard         }
9291d14ffa9Sbellard     }
9301d14ffa9Sbellard 
9311d14ffa9Sbellard     err = dsound_open (s);
9321d14ffa9Sbellard     if (err) {
9331d14ffa9Sbellard         dsound_audio_fini (s);
9341d14ffa9Sbellard         return NULL;
9351d14ffa9Sbellard     }
9361d14ffa9Sbellard 
9371d14ffa9Sbellard     return s;
9381d14ffa9Sbellard }
9391d14ffa9Sbellard 
9401d14ffa9Sbellard static struct audio_option dsound_options[] = {
94198f9f48cSmalc     {
94298f9f48cSmalc         .name  = "LOCK_RETRIES",
9432700efa3SJuan Quintela         .tag   = AUD_OPT_INT,
9442700efa3SJuan Quintela         .valp  = &conf.lock_retries,
94598f9f48cSmalc         .descr = "Number of times to attempt locking the buffer"
94698f9f48cSmalc     },
94798f9f48cSmalc     {
94898f9f48cSmalc         .name  = "RESTOURE_RETRIES",
9492700efa3SJuan Quintela         .tag   = AUD_OPT_INT,
9502700efa3SJuan Quintela         .valp  = &conf.restore_retries,
95198f9f48cSmalc         .descr = "Number of times to attempt restoring the buffer"
95298f9f48cSmalc     },
95398f9f48cSmalc     {
95498f9f48cSmalc         .name  = "GETSTATUS_RETRIES",
9552700efa3SJuan Quintela         .tag   = AUD_OPT_INT,
9562700efa3SJuan Quintela         .valp  = &conf.getstatus_retries,
95798f9f48cSmalc         .descr = "Number of times to attempt getting status of the buffer"
95898f9f48cSmalc     },
95998f9f48cSmalc     {
96098f9f48cSmalc         .name  = "SET_PRIMARY",
9612700efa3SJuan Quintela         .tag   = AUD_OPT_BOOL,
96215c875a3SConsul         .valp  = &conf.set_primary,
96398f9f48cSmalc         .descr = "Set the parameters of primary buffer"
96498f9f48cSmalc     },
96598f9f48cSmalc     {
96698f9f48cSmalc         .name  = "LATENCY_MILLIS",
9672700efa3SJuan Quintela         .tag   = AUD_OPT_INT,
9682700efa3SJuan Quintela         .valp  = &conf.latency_millis,
96998f9f48cSmalc         .descr = "(undocumented)"
97098f9f48cSmalc     },
97198f9f48cSmalc     {
97298f9f48cSmalc         .name  = "PRIMARY_FREQ",
9732700efa3SJuan Quintela         .tag   = AUD_OPT_INT,
9742700efa3SJuan Quintela         .valp  = &conf.settings.freq,
97598f9f48cSmalc         .descr = "Primary buffer frequency"
97698f9f48cSmalc     },
97798f9f48cSmalc     {
97898f9f48cSmalc         .name  = "PRIMARY_CHANNELS",
9792700efa3SJuan Quintela         .tag   = AUD_OPT_INT,
9802700efa3SJuan Quintela         .valp  = &conf.settings.nchannels,
98198f9f48cSmalc         .descr = "Primary buffer number of channels (1 - mono, 2 - stereo)"
98298f9f48cSmalc     },
98398f9f48cSmalc     {
98498f9f48cSmalc         .name  = "PRIMARY_FMT",
9852700efa3SJuan Quintela         .tag   = AUD_OPT_FMT,
9862700efa3SJuan Quintela         .valp  = &conf.settings.fmt,
98798f9f48cSmalc         .descr = "Primary buffer format"
98898f9f48cSmalc     },
98998f9f48cSmalc     {
99098f9f48cSmalc         .name  = "BUFSIZE_OUT",
9912700efa3SJuan Quintela         .tag   = AUD_OPT_INT,
9922700efa3SJuan Quintela         .valp  = &conf.bufsize_out,
99398f9f48cSmalc         .descr = "(undocumented)"
99498f9f48cSmalc     },
99598f9f48cSmalc     {
99698f9f48cSmalc         .name  = "BUFSIZE_IN",
9972700efa3SJuan Quintela         .tag   = AUD_OPT_INT,
9982700efa3SJuan Quintela         .valp  = &conf.bufsize_in,
99998f9f48cSmalc         .descr = "(undocumented)"
100098f9f48cSmalc     },
10012700efa3SJuan Quintela     { /* End of list */ }
10021d14ffa9Sbellard };
10031d14ffa9Sbellard 
100435f4b58cSblueswir1 static struct audio_pcm_ops dsound_pcm_ops = {
10051dd3e4d1SJuan Quintela     .init_out = dsound_init_out,
10061dd3e4d1SJuan Quintela     .fini_out = dsound_fini_out,
10071dd3e4d1SJuan Quintela     .run_out  = dsound_run_out,
10081dd3e4d1SJuan Quintela     .write    = dsound_write,
10091dd3e4d1SJuan Quintela     .ctl_out  = dsound_ctl_out,
10101d14ffa9Sbellard 
10111dd3e4d1SJuan Quintela     .init_in  = dsound_init_in,
10121dd3e4d1SJuan Quintela     .fini_in  = dsound_fini_in,
10131dd3e4d1SJuan Quintela     .run_in   = dsound_run_in,
10141dd3e4d1SJuan Quintela     .read     = dsound_read,
10151dd3e4d1SJuan Quintela     .ctl_in   = dsound_ctl_in
10161d14ffa9Sbellard };
10171d14ffa9Sbellard 
10181d14ffa9Sbellard struct audio_driver dsound_audio_driver = {
1019bee37f32SJuan Quintela     .name           = "dsound",
1020bee37f32SJuan Quintela     .descr          = "DirectSound http://wikipedia.org/wiki/DirectSound",
1021bee37f32SJuan Quintela     .options        = dsound_options,
1022bee37f32SJuan Quintela     .init           = dsound_audio_init,
1023bee37f32SJuan Quintela     .fini           = dsound_audio_fini,
1024bee37f32SJuan Quintela     .pcm_ops        = &dsound_pcm_ops,
1025bee37f32SJuan Quintela     .can_be_default = 1,
1026bee37f32SJuan Quintela     .max_voices_out = INT_MAX,
1027bee37f32SJuan Quintela     .max_voices_in  = 1,
1028bee37f32SJuan Quintela     .voice_size_out = sizeof (DSoundVoiceOut),
102915c875a3SConsul     .voice_size_in  = sizeof (DSoundVoiceIn)
10301d14ffa9Sbellard };
1031