Statistics
| Branch: | Revision:

root / audio / dsoundaudio.c @ 749bc4bf

History | View | Annotate | Download (26.4 kB)

1 1d14ffa9 bellard
/*
2 1d14ffa9 bellard
 * QEMU DirectSound audio driver
3 1d14ffa9 bellard
 *
4 1d14ffa9 bellard
 * Copyright (c) 2005 Vassili Karpov (malc)
5 1d14ffa9 bellard
 *
6 1d14ffa9 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 1d14ffa9 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 1d14ffa9 bellard
 * in the Software without restriction, including without limitation the rights
9 1d14ffa9 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 1d14ffa9 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 1d14ffa9 bellard
 * furnished to do so, subject to the following conditions:
12 1d14ffa9 bellard
 *
13 1d14ffa9 bellard
 * The above copyright notice and this permission notice shall be included in
14 1d14ffa9 bellard
 * all copies or substantial portions of the Software.
15 1d14ffa9 bellard
 *
16 1d14ffa9 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 1d14ffa9 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 1d14ffa9 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 1d14ffa9 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 1d14ffa9 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 1d14ffa9 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 1d14ffa9 bellard
 * THE SOFTWARE.
23 1d14ffa9 bellard
 */
24 1d14ffa9 bellard
25 1d14ffa9 bellard
/*
26 1d14ffa9 bellard
 * SEAL 1.07 by Carlos 'pel' Hasan was used as documentation
27 1d14ffa9 bellard
 */
28 1d14ffa9 bellard
29 749bc4bf pbrook
#include "qemu-common.h"
30 749bc4bf pbrook
#include "audio.h"
31 1d14ffa9 bellard
32 1d14ffa9 bellard
#define AUDIO_CAP "dsound"
33 1d14ffa9 bellard
#include "audio_int.h"
34 1d14ffa9 bellard
35 1d14ffa9 bellard
#include <windows.h>
36 1d14ffa9 bellard
#include <objbase.h>
37 1d14ffa9 bellard
#include <dsound.h>
38 1d14ffa9 bellard
39 1d14ffa9 bellard
/* #define DEBUG_DSOUND */
40 1d14ffa9 bellard
41 1d14ffa9 bellard
static struct {
42 1d14ffa9 bellard
    int lock_retries;
43 1d14ffa9 bellard
    int restore_retries;
44 1d14ffa9 bellard
    int getstatus_retries;
45 1d14ffa9 bellard
    int set_primary;
46 1d14ffa9 bellard
    int bufsize_in;
47 1d14ffa9 bellard
    int bufsize_out;
48 c0fe3827 bellard
    audsettings_t settings;
49 1d14ffa9 bellard
    int latency_millis;
50 1d14ffa9 bellard
} conf = {
51 1d14ffa9 bellard
    1,
52 1d14ffa9 bellard
    1,
53 1d14ffa9 bellard
    1,
54 1d14ffa9 bellard
    0,
55 1d14ffa9 bellard
    16384,
56 1d14ffa9 bellard
    16384,
57 1d14ffa9 bellard
    {
58 1d14ffa9 bellard
        44100,
59 1d14ffa9 bellard
        2,
60 1d14ffa9 bellard
        AUD_FMT_S16
61 1d14ffa9 bellard
    },
62 1d14ffa9 bellard
    10
63 1d14ffa9 bellard
};
64 1d14ffa9 bellard
65 1d14ffa9 bellard
typedef struct {
66 1d14ffa9 bellard
    LPDIRECTSOUND dsound;
67 1d14ffa9 bellard
    LPDIRECTSOUNDCAPTURE dsound_capture;
68 1d14ffa9 bellard
    LPDIRECTSOUNDBUFFER dsound_primary_buffer;
69 c0fe3827 bellard
    audsettings_t settings;
70 1d14ffa9 bellard
} dsound;
71 1d14ffa9 bellard
72 1d14ffa9 bellard
static dsound glob_dsound;
73 1d14ffa9 bellard
74 1d14ffa9 bellard
typedef struct {
75 1d14ffa9 bellard
    HWVoiceOut hw;
76 1d14ffa9 bellard
    LPDIRECTSOUNDBUFFER dsound_buffer;
77 1d14ffa9 bellard
    DWORD old_pos;
78 1d14ffa9 bellard
    int first_time;
79 1d14ffa9 bellard
#ifdef DEBUG_DSOUND
80 1d14ffa9 bellard
    DWORD old_ppos;
81 1d14ffa9 bellard
    DWORD played;
82 1d14ffa9 bellard
    DWORD mixed;
83 1d14ffa9 bellard
#endif
84 1d14ffa9 bellard
} DSoundVoiceOut;
85 1d14ffa9 bellard
86 1d14ffa9 bellard
typedef struct {
87 1d14ffa9 bellard
    HWVoiceIn hw;
88 1d14ffa9 bellard
    int first_time;
89 1d14ffa9 bellard
    LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
90 1d14ffa9 bellard
} DSoundVoiceIn;
91 1d14ffa9 bellard
92 1d14ffa9 bellard
static void dsound_log_hresult (HRESULT hr)
93 1d14ffa9 bellard
{
94 1d14ffa9 bellard
    const char *str = "BUG";
95 1d14ffa9 bellard
96 1d14ffa9 bellard
    switch (hr) {
97 1d14ffa9 bellard
    case DS_OK:
98 1d14ffa9 bellard
        str = "The method succeeded";
99 1d14ffa9 bellard
        break;
100 1d14ffa9 bellard
#ifdef DS_NO_VIRTUALIZATION
101 1d14ffa9 bellard
    case DS_NO_VIRTUALIZATION:
102 1d14ffa9 bellard
        str = "The buffer was created, but another 3D algorithm was substituted";
103 1d14ffa9 bellard
        break;
104 1d14ffa9 bellard
#endif
105 1d14ffa9 bellard
#ifdef DS_INCOMPLETE
106 1d14ffa9 bellard
    case DS_INCOMPLETE:
107 1d14ffa9 bellard
        str = "The method succeeded, but not all the optional effects were obtained";
108 1d14ffa9 bellard
        break;
109 1d14ffa9 bellard
#endif
110 1d14ffa9 bellard
#ifdef DSERR_ACCESSDENIED
111 1d14ffa9 bellard
    case DSERR_ACCESSDENIED:
112 1d14ffa9 bellard
        str = "The request failed because access was denied";
113 1d14ffa9 bellard
        break;
114 1d14ffa9 bellard
#endif
115 1d14ffa9 bellard
#ifdef DSERR_ALLOCATED
116 1d14ffa9 bellard
    case DSERR_ALLOCATED:
117 1d14ffa9 bellard
        str = "The request failed because resources, such as a priority level, were already in use by another caller";
118 1d14ffa9 bellard
        break;
119 1d14ffa9 bellard
#endif
120 1d14ffa9 bellard
#ifdef DSERR_ALREADYINITIALIZED
121 1d14ffa9 bellard
    case DSERR_ALREADYINITIALIZED:
122 1d14ffa9 bellard
        str = "The object is already initialized";
123 1d14ffa9 bellard
        break;
124 1d14ffa9 bellard
#endif
125 1d14ffa9 bellard
#ifdef DSERR_BADFORMAT
126 1d14ffa9 bellard
    case DSERR_BADFORMAT:
127 1d14ffa9 bellard
        str = "The specified wave format is not supported";
128 1d14ffa9 bellard
        break;
129 1d14ffa9 bellard
#endif
130 1d14ffa9 bellard
#ifdef DSERR_BADSENDBUFFERGUID
131 1d14ffa9 bellard
    case DSERR_BADSENDBUFFERGUID:
132 1d14ffa9 bellard
        str = "The GUID specified in an audiopath file does not match a valid mix-in buffer";
133 1d14ffa9 bellard
        break;
134 1d14ffa9 bellard
#endif
135 1d14ffa9 bellard
#ifdef DSERR_BUFFERLOST
136 1d14ffa9 bellard
    case DSERR_BUFFERLOST:
137 1d14ffa9 bellard
        str = "The buffer memory has been lost and must be restored";
138 1d14ffa9 bellard
        break;
139 1d14ffa9 bellard
#endif
140 1d14ffa9 bellard
#ifdef DSERR_BUFFERTOOSMALL
141 1d14ffa9 bellard
    case DSERR_BUFFERTOOSMALL:
142 1d14ffa9 bellard
        str = "The buffer size is not great enough to enable effects processing";
143 1d14ffa9 bellard
        break;
144 1d14ffa9 bellard
#endif
145 1d14ffa9 bellard
#ifdef DSERR_CONTROLUNAVAIL
146 1d14ffa9 bellard
    case DSERR_CONTROLUNAVAIL:
147 1d14ffa9 bellard
        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";
148 1d14ffa9 bellard
        break;
149 1d14ffa9 bellard
#endif
150 1d14ffa9 bellard
#ifdef DSERR_DS8_REQUIRED
151 1d14ffa9 bellard
    case DSERR_DS8_REQUIRED:
152 1d14ffa9 bellard
        str = "A DirectSound object of class CLSID_DirectSound8 or later is required for the requested functionality. For more information, see IDirectSound8 Interface";
153 1d14ffa9 bellard
        break;
154 1d14ffa9 bellard
#endif
155 1d14ffa9 bellard
#ifdef DSERR_FXUNAVAILABLE
156 1d14ffa9 bellard
    case DSERR_FXUNAVAILABLE:
157 1d14ffa9 bellard
        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";
158 1d14ffa9 bellard
        break;
159 1d14ffa9 bellard
#endif
160 1d14ffa9 bellard
#ifdef DSERR_GENERIC
161 1d14ffa9 bellard
    case DSERR_GENERIC :
162 1d14ffa9 bellard
        str = "An undetermined error occurred inside the DirectSound subsystem";
163 1d14ffa9 bellard
        break;
164 1d14ffa9 bellard
#endif
165 1d14ffa9 bellard
#ifdef DSERR_INVALIDCALL
166 1d14ffa9 bellard
    case DSERR_INVALIDCALL:
167 1d14ffa9 bellard
        str = "This function is not valid for the current state of this object";
168 1d14ffa9 bellard
        break;
169 1d14ffa9 bellard
#endif
170 1d14ffa9 bellard
#ifdef DSERR_INVALIDPARAM
171 1d14ffa9 bellard
    case DSERR_INVALIDPARAM:
172 1d14ffa9 bellard
        str = "An invalid parameter was passed to the returning function";
173 1d14ffa9 bellard
        break;
174 1d14ffa9 bellard
#endif
175 1d14ffa9 bellard
#ifdef DSERR_NOAGGREGATION
176 1d14ffa9 bellard
    case DSERR_NOAGGREGATION:
177 1d14ffa9 bellard
        str = "The object does not support aggregation";
178 1d14ffa9 bellard
        break;
179 1d14ffa9 bellard
#endif
180 1d14ffa9 bellard
#ifdef DSERR_NODRIVER
181 1d14ffa9 bellard
    case DSERR_NODRIVER:
182 1d14ffa9 bellard
        str = "No sound driver is available for use, or the given GUID is not a valid DirectSound device ID";
183 1d14ffa9 bellard
        break;
184 1d14ffa9 bellard
#endif
185 1d14ffa9 bellard
#ifdef DSERR_NOINTERFACE
186 1d14ffa9 bellard
    case DSERR_NOINTERFACE:
187 1d14ffa9 bellard
        str = "The requested COM interface is not available";
188 1d14ffa9 bellard
        break;
189 1d14ffa9 bellard
#endif
190 1d14ffa9 bellard
#ifdef DSERR_OBJECTNOTFOUND
191 1d14ffa9 bellard
    case DSERR_OBJECTNOTFOUND:
192 1d14ffa9 bellard
        str = "The requested object was not found";
193 1d14ffa9 bellard
        break;
194 1d14ffa9 bellard
#endif
195 1d14ffa9 bellard
#ifdef DSERR_OTHERAPPHASPRIO
196 1d14ffa9 bellard
    case DSERR_OTHERAPPHASPRIO:
197 1d14ffa9 bellard
        str = "Another application has a higher priority level, preventing this call from succeeding";
198 1d14ffa9 bellard
        break;
199 1d14ffa9 bellard
#endif
200 1d14ffa9 bellard
#ifdef DSERR_OUTOFMEMORY
201 1d14ffa9 bellard
    case DSERR_OUTOFMEMORY:
202 1d14ffa9 bellard
        str = "The DirectSound subsystem could not allocate sufficient memory to complete the caller's request";
203 1d14ffa9 bellard
        break;
204 1d14ffa9 bellard
#endif
205 1d14ffa9 bellard
#ifdef DSERR_PRIOLEVELNEEDED
206 1d14ffa9 bellard
    case DSERR_PRIOLEVELNEEDED:
207 1d14ffa9 bellard
        str = "A cooperative level of DSSCL_PRIORITY or higher is required";
208 1d14ffa9 bellard
        break;
209 1d14ffa9 bellard
#endif
210 1d14ffa9 bellard
#ifdef DSERR_SENDLOOP
211 1d14ffa9 bellard
    case DSERR_SENDLOOP:
212 1d14ffa9 bellard
        str = "A circular loop of send effects was detected";
213 1d14ffa9 bellard
        break;
214 1d14ffa9 bellard
#endif
215 1d14ffa9 bellard
#ifdef DSERR_UNINITIALIZED
216 1d14ffa9 bellard
    case DSERR_UNINITIALIZED:
217 1d14ffa9 bellard
        str = "The Initialize method has not been called or has not been called successfully before other methods were called";
218 1d14ffa9 bellard
        break;
219 1d14ffa9 bellard
#endif
220 1d14ffa9 bellard
#ifdef DSERR_UNSUPPORTED
221 1d14ffa9 bellard
    case DSERR_UNSUPPORTED:
222 1d14ffa9 bellard
        str = "The function called is not supported at this time";
223 1d14ffa9 bellard
        break;
224 1d14ffa9 bellard
#endif
225 1d14ffa9 bellard
    default:
226 1d14ffa9 bellard
        AUD_log (AUDIO_CAP, "Reason: Unknown (HRESULT %#lx)\n", hr);
227 1d14ffa9 bellard
        return;
228 1d14ffa9 bellard
    }
229 1d14ffa9 bellard
230 1d14ffa9 bellard
    AUD_log (AUDIO_CAP, "Reason: %s\n", str);
231 1d14ffa9 bellard
}
232 1d14ffa9 bellard
233 1d14ffa9 bellard
static void GCC_FMT_ATTR (2, 3) dsound_logerr (
234 1d14ffa9 bellard
    HRESULT hr,
235 1d14ffa9 bellard
    const char *fmt,
236 1d14ffa9 bellard
    ...
237 1d14ffa9 bellard
    )
238 1d14ffa9 bellard
{
239 1d14ffa9 bellard
    va_list ap;
240 1d14ffa9 bellard
241 1d14ffa9 bellard
    va_start (ap, fmt);
242 1d14ffa9 bellard
    AUD_vlog (AUDIO_CAP, fmt, ap);
243 1d14ffa9 bellard
    va_end (ap);
244 1d14ffa9 bellard
245 1d14ffa9 bellard
    dsound_log_hresult (hr);
246 1d14ffa9 bellard
}
247 1d14ffa9 bellard
248 1d14ffa9 bellard
static void GCC_FMT_ATTR (3, 4) dsound_logerr2 (
249 1d14ffa9 bellard
    HRESULT hr,
250 1d14ffa9 bellard
    const char *typ,
251 1d14ffa9 bellard
    const char *fmt,
252 1d14ffa9 bellard
    ...
253 1d14ffa9 bellard
    )
254 1d14ffa9 bellard
{
255 1d14ffa9 bellard
    va_list ap;
256 1d14ffa9 bellard
257 c0fe3827 bellard
    AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
258 1d14ffa9 bellard
    va_start (ap, fmt);
259 1d14ffa9 bellard
    AUD_vlog (AUDIO_CAP, fmt, ap);
260 1d14ffa9 bellard
    va_end (ap);
261 1d14ffa9 bellard
262 1d14ffa9 bellard
    dsound_log_hresult (hr);
263 1d14ffa9 bellard
}
264 1d14ffa9 bellard
265 1d14ffa9 bellard
static DWORD millis_to_bytes (struct audio_pcm_info *info, DWORD millis)
266 1d14ffa9 bellard
{
267 1d14ffa9 bellard
    return (millis * info->bytes_per_second) / 1000;
268 1d14ffa9 bellard
}
269 1d14ffa9 bellard
270 1d14ffa9 bellard
#ifdef DEBUG_DSOUND
271 1d14ffa9 bellard
static void print_wave_format (WAVEFORMATEX *wfx)
272 1d14ffa9 bellard
{
273 1d14ffa9 bellard
    dolog ("tag             = %d\n", wfx->wFormatTag);
274 1d14ffa9 bellard
    dolog ("nChannels       = %d\n", wfx->nChannels);
275 1d14ffa9 bellard
    dolog ("nSamplesPerSec  = %ld\n", wfx->nSamplesPerSec);
276 1d14ffa9 bellard
    dolog ("nAvgBytesPerSec = %ld\n", wfx->nAvgBytesPerSec);
277 1d14ffa9 bellard
    dolog ("nBlockAlign     = %d\n", wfx->nBlockAlign);
278 1d14ffa9 bellard
    dolog ("wBitsPerSample  = %d\n", wfx->wBitsPerSample);
279 1d14ffa9 bellard
    dolog ("cbSize          = %d\n", wfx->cbSize);
280 1d14ffa9 bellard
}
281 1d14ffa9 bellard
#endif
282 1d14ffa9 bellard
283 1d14ffa9 bellard
static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb)
284 1d14ffa9 bellard
{
285 1d14ffa9 bellard
    HRESULT hr;
286 1d14ffa9 bellard
    int i;
287 1d14ffa9 bellard
288 1d14ffa9 bellard
    for (i = 0; i < conf.restore_retries; ++i) {
289 1d14ffa9 bellard
        hr = IDirectSoundBuffer_Restore (dsb);
290 1d14ffa9 bellard
291 1d14ffa9 bellard
        switch (hr) {
292 1d14ffa9 bellard
        case DS_OK:
293 1d14ffa9 bellard
            return 0;
294 1d14ffa9 bellard
295 1d14ffa9 bellard
        case DSERR_BUFFERLOST:
296 1d14ffa9 bellard
            continue;
297 1d14ffa9 bellard
298 1d14ffa9 bellard
        default:
299 c0fe3827 bellard
            dsound_logerr (hr, "Could not restore playback buffer\n");
300 1d14ffa9 bellard
            return -1;
301 1d14ffa9 bellard
        }
302 1d14ffa9 bellard
    }
303 1d14ffa9 bellard
304 1d14ffa9 bellard
    dolog ("%d attempts to restore playback buffer failed\n", i);
305 1d14ffa9 bellard
    return -1;
306 1d14ffa9 bellard
}
307 1d14ffa9 bellard
308 c0fe3827 bellard
static int waveformat_from_audio_settings (WAVEFORMATEX *wfx, audsettings_t *as)
309 1d14ffa9 bellard
{
310 1d14ffa9 bellard
    memset (wfx, 0, sizeof (*wfx));
311 1d14ffa9 bellard
312 1d14ffa9 bellard
    wfx->wFormatTag = WAVE_FORMAT_PCM;
313 c0fe3827 bellard
    wfx->nChannels = as->nchannels;
314 c0fe3827 bellard
    wfx->nSamplesPerSec = as->freq;
315 c0fe3827 bellard
    wfx->nAvgBytesPerSec = as->freq << (as->nchannels == 2);
316 c0fe3827 bellard
    wfx->nBlockAlign = 1 << (as->nchannels == 2);
317 1d14ffa9 bellard
    wfx->cbSize = 0;
318 1d14ffa9 bellard
319 c0fe3827 bellard
    switch (as->fmt) {
320 1d14ffa9 bellard
    case AUD_FMT_S8:
321 1d14ffa9 bellard
        wfx->wBitsPerSample = 8;
322 1d14ffa9 bellard
        break;
323 1d14ffa9 bellard
324 1d14ffa9 bellard
    case AUD_FMT_U8:
325 1d14ffa9 bellard
        wfx->wBitsPerSample = 8;
326 1d14ffa9 bellard
        break;
327 1d14ffa9 bellard
328 1d14ffa9 bellard
    case AUD_FMT_S16:
329 1d14ffa9 bellard
        wfx->wBitsPerSample = 16;
330 1d14ffa9 bellard
        wfx->nAvgBytesPerSec <<= 1;
331 1d14ffa9 bellard
        wfx->nBlockAlign <<= 1;
332 1d14ffa9 bellard
        break;
333 1d14ffa9 bellard
334 1d14ffa9 bellard
    case AUD_FMT_U16:
335 1d14ffa9 bellard
        wfx->wBitsPerSample = 16;
336 1d14ffa9 bellard
        wfx->nAvgBytesPerSec <<= 1;
337 1d14ffa9 bellard
        wfx->nBlockAlign <<= 1;
338 1d14ffa9 bellard
        break;
339 1d14ffa9 bellard
340 1d14ffa9 bellard
    default:
341 c0fe3827 bellard
        dolog ("Internal logic error: Bad audio format %d\n", as->freq);
342 1d14ffa9 bellard
        return -1;
343 1d14ffa9 bellard
    }
344 1d14ffa9 bellard
345 1d14ffa9 bellard
    return 0;
346 1d14ffa9 bellard
}
347 1d14ffa9 bellard
348 c0fe3827 bellard
static int waveformat_to_audio_settings (WAVEFORMATEX *wfx, audsettings_t *as)
349 1d14ffa9 bellard
{
350 1d14ffa9 bellard
    if (wfx->wFormatTag != WAVE_FORMAT_PCM) {
351 1d14ffa9 bellard
        dolog ("Invalid wave format, tag is not PCM, but %d\n",
352 1d14ffa9 bellard
               wfx->wFormatTag);
353 1d14ffa9 bellard
        return -1;
354 1d14ffa9 bellard
    }
355 1d14ffa9 bellard
356 1d14ffa9 bellard
    if (!wfx->nSamplesPerSec) {
357 1d14ffa9 bellard
        dolog ("Invalid wave format, frequency is zero\n");
358 1d14ffa9 bellard
        return -1;
359 1d14ffa9 bellard
    }
360 c0fe3827 bellard
    as->freq = wfx->nSamplesPerSec;
361 1d14ffa9 bellard
362 1d14ffa9 bellard
    switch (wfx->nChannels) {
363 1d14ffa9 bellard
    case 1:
364 c0fe3827 bellard
        as->nchannels = 1;
365 1d14ffa9 bellard
        break;
366 1d14ffa9 bellard
367 1d14ffa9 bellard
    case 2:
368 c0fe3827 bellard
        as->nchannels = 2;
369 1d14ffa9 bellard
        break;
370 1d14ffa9 bellard
371 1d14ffa9 bellard
    default:
372 1d14ffa9 bellard
        dolog (
373 1d14ffa9 bellard
            "Invalid wave format, number of channels is not 1 or 2, but %d\n",
374 1d14ffa9 bellard
            wfx->nChannels
375 1d14ffa9 bellard
            );
376 1d14ffa9 bellard
        return -1;
377 1d14ffa9 bellard
    }
378 1d14ffa9 bellard
379 1d14ffa9 bellard
    switch (wfx->wBitsPerSample) {
380 1d14ffa9 bellard
    case 8:
381 c0fe3827 bellard
        as->fmt = AUD_FMT_U8;
382 1d14ffa9 bellard
        break;
383 1d14ffa9 bellard
384 1d14ffa9 bellard
    case 16:
385 c0fe3827 bellard
        as->fmt = AUD_FMT_S16;
386 1d14ffa9 bellard
        break;
387 1d14ffa9 bellard
388 1d14ffa9 bellard
    default:
389 1d14ffa9 bellard
        dolog ("Invalid wave format, bits per sample is not 8 or 16, but %d\n",
390 1d14ffa9 bellard
               wfx->wBitsPerSample);
391 1d14ffa9 bellard
        return -1;
392 1d14ffa9 bellard
    }
393 1d14ffa9 bellard
394 1d14ffa9 bellard
    return 0;
395 1d14ffa9 bellard
}
396 1d14ffa9 bellard
397 1d14ffa9 bellard
#include "dsound_template.h"
398 1d14ffa9 bellard
#define DSBTYPE_IN
399 1d14ffa9 bellard
#include "dsound_template.h"
400 1d14ffa9 bellard
#undef DSBTYPE_IN
401 1d14ffa9 bellard
402 1d14ffa9 bellard
static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp)
403 1d14ffa9 bellard
{
404 1d14ffa9 bellard
    HRESULT hr;
405 1d14ffa9 bellard
    int i;
406 1d14ffa9 bellard
407 1d14ffa9 bellard
    for (i = 0; i < conf.getstatus_retries; ++i) {
408 1d14ffa9 bellard
        hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
409 1d14ffa9 bellard
        if (FAILED (hr)) {
410 c0fe3827 bellard
            dsound_logerr (hr, "Could not get playback buffer status\n");
411 1d14ffa9 bellard
            return -1;
412 1d14ffa9 bellard
        }
413 1d14ffa9 bellard
414 1d14ffa9 bellard
        if (*statusp & DSERR_BUFFERLOST) {
415 1d14ffa9 bellard
            if (dsound_restore_out (dsb)) {
416 1d14ffa9 bellard
                return -1;
417 1d14ffa9 bellard
            }
418 1d14ffa9 bellard
            continue;
419 1d14ffa9 bellard
        }
420 1d14ffa9 bellard
        break;
421 1d14ffa9 bellard
    }
422 1d14ffa9 bellard
423 1d14ffa9 bellard
    return 0;
424 1d14ffa9 bellard
}
425 1d14ffa9 bellard
426 1d14ffa9 bellard
static int dsound_get_status_in (LPDIRECTSOUNDCAPTUREBUFFER dscb,
427 1d14ffa9 bellard
                                 DWORD *statusp)
428 1d14ffa9 bellard
{
429 1d14ffa9 bellard
    HRESULT hr;
430 1d14ffa9 bellard
431 1d14ffa9 bellard
    hr = IDirectSoundCaptureBuffer_GetStatus (dscb, statusp);
432 1d14ffa9 bellard
    if (FAILED (hr)) {
433 c0fe3827 bellard
        dsound_logerr (hr, "Could not get capture buffer status\n");
434 1d14ffa9 bellard
        return -1;
435 1d14ffa9 bellard
    }
436 1d14ffa9 bellard
437 1d14ffa9 bellard
    return 0;
438 1d14ffa9 bellard
}
439 1d14ffa9 bellard
440 1d14ffa9 bellard
static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
441 1d14ffa9 bellard
{
442 1d14ffa9 bellard
    int src_len1 = dst_len;
443 1d14ffa9 bellard
    int src_len2 = 0;
444 1d14ffa9 bellard
    int pos = hw->rpos + dst_len;
445 1d14ffa9 bellard
    st_sample_t *src1 = hw->mix_buf + hw->rpos;
446 1d14ffa9 bellard
    st_sample_t *src2 = NULL;
447 1d14ffa9 bellard
448 1d14ffa9 bellard
    if (pos > hw->samples) {
449 1d14ffa9 bellard
        src_len1 = hw->samples - hw->rpos;
450 1d14ffa9 bellard
        src2 = hw->mix_buf;
451 1d14ffa9 bellard
        src_len2 = dst_len - src_len1;
452 1d14ffa9 bellard
        pos = src_len2;
453 1d14ffa9 bellard
    }
454 1d14ffa9 bellard
455 1d14ffa9 bellard
    if (src_len1) {
456 1d14ffa9 bellard
        hw->clip (dst, src1, src_len1);
457 1d14ffa9 bellard
    }
458 1d14ffa9 bellard
459 1d14ffa9 bellard
    if (src_len2) {
460 1d14ffa9 bellard
        dst = advance (dst, src_len1 << hw->info.shift);
461 1d14ffa9 bellard
        hw->clip (dst, src2, src_len2);
462 1d14ffa9 bellard
    }
463 1d14ffa9 bellard
464 1d14ffa9 bellard
    hw->rpos = pos % hw->samples;
465 1d14ffa9 bellard
}
466 1d14ffa9 bellard
467 1d14ffa9 bellard
static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb)
468 1d14ffa9 bellard
{
469 1d14ffa9 bellard
    int err;
470 1d14ffa9 bellard
    LPVOID p1, p2;
471 1d14ffa9 bellard
    DWORD blen1, blen2, len1, len2;
472 1d14ffa9 bellard
473 1d14ffa9 bellard
    err = dsound_lock_out (
474 1d14ffa9 bellard
        dsb,
475 1d14ffa9 bellard
        &hw->info,
476 1d14ffa9 bellard
        0,
477 1d14ffa9 bellard
        hw->samples << hw->info.shift,
478 1d14ffa9 bellard
        &p1, &p2,
479 1d14ffa9 bellard
        &blen1, &blen2,
480 1d14ffa9 bellard
        1
481 1d14ffa9 bellard
        );
482 1d14ffa9 bellard
    if (err) {
483 1d14ffa9 bellard
        return;
484 1d14ffa9 bellard
    }
485 1d14ffa9 bellard
486 1d14ffa9 bellard
    len1 = blen1 >> hw->info.shift;
487 1d14ffa9 bellard
    len2 = blen2 >> hw->info.shift;
488 1d14ffa9 bellard
489 1d14ffa9 bellard
#ifdef DEBUG_DSOUND
490 1d14ffa9 bellard
    dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
491 1d14ffa9 bellard
           p1, blen1, len1,
492 1d14ffa9 bellard
           p2, blen2, len2);
493 1d14ffa9 bellard
#endif
494 1d14ffa9 bellard
495 1d14ffa9 bellard
    if (p1 && len1) {
496 1d14ffa9 bellard
        audio_pcm_info_clear_buf (&hw->info, p1, len1);
497 1d14ffa9 bellard
    }
498 1d14ffa9 bellard
499 1d14ffa9 bellard
    if (p2 && len2) {
500 1d14ffa9 bellard
        audio_pcm_info_clear_buf (&hw->info, p2, len2);
501 1d14ffa9 bellard
    }
502 1d14ffa9 bellard
503 1d14ffa9 bellard
    dsound_unlock_out (dsb, p1, p2, blen1, blen2);
504 1d14ffa9 bellard
}
505 1d14ffa9 bellard
506 1d14ffa9 bellard
static void dsound_close (dsound *s)
507 1d14ffa9 bellard
{
508 1d14ffa9 bellard
    HRESULT hr;
509 1d14ffa9 bellard
510 1d14ffa9 bellard
    if (s->dsound_primary_buffer) {
511 1d14ffa9 bellard
        hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer);
512 1d14ffa9 bellard
        if (FAILED (hr)) {
513 c0fe3827 bellard
            dsound_logerr (hr, "Could not release primary buffer\n");
514 1d14ffa9 bellard
        }
515 1d14ffa9 bellard
        s->dsound_primary_buffer = NULL;
516 1d14ffa9 bellard
    }
517 1d14ffa9 bellard
}
518 1d14ffa9 bellard
519 1d14ffa9 bellard
static int dsound_open (dsound *s)
520 1d14ffa9 bellard
{
521 1d14ffa9 bellard
    int err;
522 1d14ffa9 bellard
    HRESULT hr;
523 1d14ffa9 bellard
    WAVEFORMATEX wfx;
524 1d14ffa9 bellard
    DSBUFFERDESC dsbd;
525 1d14ffa9 bellard
    HWND hwnd;
526 1d14ffa9 bellard
527 1d14ffa9 bellard
    hwnd = GetForegroundWindow ();
528 1d14ffa9 bellard
    hr = IDirectSound_SetCooperativeLevel (
529 1d14ffa9 bellard
        s->dsound,
530 1d14ffa9 bellard
        hwnd,
531 1d14ffa9 bellard
        DSSCL_PRIORITY
532 1d14ffa9 bellard
        );
533 1d14ffa9 bellard
534 1d14ffa9 bellard
    if (FAILED (hr)) {
535 c0fe3827 bellard
        dsound_logerr (hr, "Could not set cooperative level for window %p\n",
536 1d14ffa9 bellard
                       hwnd);
537 1d14ffa9 bellard
        return -1;
538 1d14ffa9 bellard
    }
539 1d14ffa9 bellard
540 1d14ffa9 bellard
    if (!conf.set_primary) {
541 1d14ffa9 bellard
        return 0;
542 1d14ffa9 bellard
    }
543 1d14ffa9 bellard
544 c0fe3827 bellard
    err = waveformat_from_audio_settings (&wfx, &conf.settings);
545 1d14ffa9 bellard
    if (err) {
546 1d14ffa9 bellard
        return -1;
547 1d14ffa9 bellard
    }
548 1d14ffa9 bellard
549 1d14ffa9 bellard
    memset (&dsbd, 0, sizeof (dsbd));
550 1d14ffa9 bellard
    dsbd.dwSize = sizeof (dsbd);
551 1d14ffa9 bellard
    dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
552 1d14ffa9 bellard
    dsbd.dwBufferBytes = 0;
553 1d14ffa9 bellard
    dsbd.lpwfxFormat = NULL;
554 1d14ffa9 bellard
555 1d14ffa9 bellard
    hr = IDirectSound_CreateSoundBuffer (
556 1d14ffa9 bellard
        s->dsound,
557 1d14ffa9 bellard
        &dsbd,
558 1d14ffa9 bellard
        &s->dsound_primary_buffer,
559 1d14ffa9 bellard
        NULL
560 1d14ffa9 bellard
        );
561 1d14ffa9 bellard
    if (FAILED (hr)) {
562 c0fe3827 bellard
        dsound_logerr (hr, "Could not create primary playback buffer\n");
563 1d14ffa9 bellard
        return -1;
564 1d14ffa9 bellard
    }
565 1d14ffa9 bellard
566 1d14ffa9 bellard
    hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx);
567 1d14ffa9 bellard
    if (FAILED (hr)) {
568 c0fe3827 bellard
        dsound_logerr (hr, "Could not set primary playback buffer format\n");
569 1d14ffa9 bellard
    }
570 1d14ffa9 bellard
571 1d14ffa9 bellard
    hr = IDirectSoundBuffer_GetFormat (
572 1d14ffa9 bellard
        s->dsound_primary_buffer,
573 1d14ffa9 bellard
        &wfx,
574 1d14ffa9 bellard
        sizeof (wfx),
575 1d14ffa9 bellard
        NULL
576 1d14ffa9 bellard
        );
577 1d14ffa9 bellard
    if (FAILED (hr)) {
578 c0fe3827 bellard
        dsound_logerr (hr, "Could not get primary playback buffer format\n");
579 1d14ffa9 bellard
        goto fail0;
580 1d14ffa9 bellard
    }
581 1d14ffa9 bellard
582 1d14ffa9 bellard
#ifdef DEBUG_DSOUND
583 1d14ffa9 bellard
    dolog ("Primary\n");
584 1d14ffa9 bellard
    print_wave_format (&wfx);
585 1d14ffa9 bellard
#endif
586 1d14ffa9 bellard
587 c0fe3827 bellard
    err = waveformat_to_audio_settings (&wfx, &s->settings);
588 1d14ffa9 bellard
    if (err) {
589 1d14ffa9 bellard
        goto fail0;
590 1d14ffa9 bellard
    }
591 1d14ffa9 bellard
592 1d14ffa9 bellard
    return 0;
593 1d14ffa9 bellard
594 1d14ffa9 bellard
 fail0:
595 1d14ffa9 bellard
    dsound_close (s);
596 1d14ffa9 bellard
    return -1;
597 1d14ffa9 bellard
}
598 1d14ffa9 bellard
599 1d14ffa9 bellard
static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
600 1d14ffa9 bellard
{
601 1d14ffa9 bellard
    HRESULT hr;
602 1d14ffa9 bellard
    DWORD status;
603 1d14ffa9 bellard
    DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
604 1d14ffa9 bellard
    LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
605 1d14ffa9 bellard
606 1d14ffa9 bellard
    if (!dsb) {
607 1d14ffa9 bellard
        dolog ("Attempt to control voice without a buffer\n");
608 1d14ffa9 bellard
        return 0;
609 1d14ffa9 bellard
    }
610 1d14ffa9 bellard
611 1d14ffa9 bellard
    switch (cmd) {
612 1d14ffa9 bellard
    case VOICE_ENABLE:
613 1d14ffa9 bellard
        if (dsound_get_status_out (dsb, &status)) {
614 1d14ffa9 bellard
            return -1;
615 1d14ffa9 bellard
        }
616 1d14ffa9 bellard
617 1d14ffa9 bellard
        if (status & DSBSTATUS_PLAYING) {
618 c0fe3827 bellard
            dolog ("warning: Voice is already playing\n");
619 1d14ffa9 bellard
            return 0;
620 1d14ffa9 bellard
        }
621 1d14ffa9 bellard
622 1d14ffa9 bellard
        dsound_clear_sample (hw, dsb);
623 1d14ffa9 bellard
624 1d14ffa9 bellard
        hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
625 1d14ffa9 bellard
        if (FAILED (hr)) {
626 c0fe3827 bellard
            dsound_logerr (hr, "Could not start playing buffer\n");
627 1d14ffa9 bellard
            return -1;
628 1d14ffa9 bellard
        }
629 1d14ffa9 bellard
        break;
630 1d14ffa9 bellard
631 1d14ffa9 bellard
    case VOICE_DISABLE:
632 1d14ffa9 bellard
        if (dsound_get_status_out (dsb, &status)) {
633 1d14ffa9 bellard
            return -1;
634 1d14ffa9 bellard
        }
635 1d14ffa9 bellard
636 1d14ffa9 bellard
        if (status & DSBSTATUS_PLAYING) {
637 1d14ffa9 bellard
            hr = IDirectSoundBuffer_Stop (dsb);
638 1d14ffa9 bellard
            if (FAILED (hr)) {
639 c0fe3827 bellard
                dsound_logerr (hr, "Could not stop playing buffer\n");
640 1d14ffa9 bellard
                return -1;
641 1d14ffa9 bellard
            }
642 1d14ffa9 bellard
        }
643 1d14ffa9 bellard
        else {
644 c0fe3827 bellard
            dolog ("warning: Voice is not playing\n");
645 1d14ffa9 bellard
        }
646 1d14ffa9 bellard
        break;
647 1d14ffa9 bellard
    }
648 1d14ffa9 bellard
    return 0;
649 1d14ffa9 bellard
}
650 1d14ffa9 bellard
651 1d14ffa9 bellard
static int dsound_write (SWVoiceOut *sw, void *buf, int len)
652 1d14ffa9 bellard
{
653 1d14ffa9 bellard
    return audio_pcm_sw_write (sw, buf, len);
654 1d14ffa9 bellard
}
655 1d14ffa9 bellard
656 1d14ffa9 bellard
static int dsound_run_out (HWVoiceOut *hw)
657 1d14ffa9 bellard
{
658 1d14ffa9 bellard
    int err;
659 1d14ffa9 bellard
    HRESULT hr;
660 1d14ffa9 bellard
    DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
661 1d14ffa9 bellard
    LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
662 1d14ffa9 bellard
    int live, len, hwshift;
663 1d14ffa9 bellard
    DWORD blen1, blen2;
664 1d14ffa9 bellard
    DWORD len1, len2;
665 1d14ffa9 bellard
    DWORD decr;
666 1d14ffa9 bellard
    DWORD wpos, ppos, old_pos;
667 1d14ffa9 bellard
    LPVOID p1, p2;
668 c0fe3827 bellard
    int bufsize;
669 1d14ffa9 bellard
670 1d14ffa9 bellard
    if (!dsb) {
671 1d14ffa9 bellard
        dolog ("Attempt to run empty with playback buffer\n");
672 1d14ffa9 bellard
        return 0;
673 1d14ffa9 bellard
    }
674 1d14ffa9 bellard
675 1d14ffa9 bellard
    hwshift = hw->info.shift;
676 c0fe3827 bellard
    bufsize = hw->samples << hwshift;
677 1d14ffa9 bellard
678 1d14ffa9 bellard
    live = audio_pcm_hw_get_live_out (hw);
679 1d14ffa9 bellard
680 1d14ffa9 bellard
    hr = IDirectSoundBuffer_GetCurrentPosition (
681 1d14ffa9 bellard
        dsb,
682 1d14ffa9 bellard
        &ppos,
683 1d14ffa9 bellard
        ds->first_time ? &wpos : NULL
684 1d14ffa9 bellard
        );
685 1d14ffa9 bellard
    if (FAILED (hr)) {
686 c0fe3827 bellard
        dsound_logerr (hr, "Could not get playback buffer position\n");
687 1d14ffa9 bellard
        return 0;
688 1d14ffa9 bellard
    }
689 1d14ffa9 bellard
690 1d14ffa9 bellard
    len = live << hwshift;
691 1d14ffa9 bellard
692 1d14ffa9 bellard
    if (ds->first_time) {
693 1d14ffa9 bellard
        if (conf.latency_millis) {
694 c0fe3827 bellard
            DWORD cur_blat;
695 1d14ffa9 bellard
696 c0fe3827 bellard
            cur_blat = audio_ring_dist (wpos, ppos, bufsize);
697 1d14ffa9 bellard
            ds->first_time = 0;
698 1d14ffa9 bellard
            old_pos = wpos;
699 1d14ffa9 bellard
            old_pos +=
700 1d14ffa9 bellard
                millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat;
701 c0fe3827 bellard
            old_pos %= bufsize;
702 1d14ffa9 bellard
            old_pos &= ~hw->info.align;
703 1d14ffa9 bellard
        }
704 1d14ffa9 bellard
        else {
705 1d14ffa9 bellard
            old_pos = wpos;
706 1d14ffa9 bellard
        }
707 1d14ffa9 bellard
#ifdef DEBUG_DSOUND
708 1d14ffa9 bellard
        ds->played = 0;
709 1d14ffa9 bellard
        ds->mixed = 0;
710 1d14ffa9 bellard
#endif
711 1d14ffa9 bellard
    }
712 1d14ffa9 bellard
    else {
713 1d14ffa9 bellard
        if (ds->old_pos == ppos) {
714 1d14ffa9 bellard
#ifdef DEBUG_DSOUND
715 1d14ffa9 bellard
            dolog ("old_pos == ppos\n");
716 1d14ffa9 bellard
#endif
717 1d14ffa9 bellard
            return 0;
718 1d14ffa9 bellard
        }
719 1d14ffa9 bellard
720 1d14ffa9 bellard
#ifdef DEBUG_DSOUND
721 1d14ffa9 bellard
        ds->played += audio_ring_dist (ds->old_pos, ppos, hw->bufsize);
722 1d14ffa9 bellard
#endif
723 1d14ffa9 bellard
        old_pos = ds->old_pos;
724 1d14ffa9 bellard
    }
725 1d14ffa9 bellard
726 1d14ffa9 bellard
    if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
727 1d14ffa9 bellard
        len = ppos - old_pos;
728 1d14ffa9 bellard
    }
729 1d14ffa9 bellard
    else {
730 c0fe3827 bellard
        if ((old_pos > ppos) && ((old_pos + len) > (ppos + bufsize))) {
731 c0fe3827 bellard
            len = bufsize - old_pos + ppos;
732 1d14ffa9 bellard
        }
733 1d14ffa9 bellard
    }
734 1d14ffa9 bellard
735 c0fe3827 bellard
    if (audio_bug (AUDIO_FUNC, len < 0 || len > bufsize)) {
736 c0fe3827 bellard
        dolog ("len=%d bufsize=%d old_pos=%ld ppos=%ld\n",
737 c0fe3827 bellard
               len, bufsize, old_pos, ppos);
738 1d14ffa9 bellard
        return 0;
739 1d14ffa9 bellard
    }
740 1d14ffa9 bellard
741 1d14ffa9 bellard
    len &= ~hw->info.align;
742 1d14ffa9 bellard
    if (!len) {
743 1d14ffa9 bellard
        return 0;
744 1d14ffa9 bellard
    }
745 1d14ffa9 bellard
746 1d14ffa9 bellard
#ifdef DEBUG_DSOUND
747 1d14ffa9 bellard
    ds->old_ppos = ppos;
748 1d14ffa9 bellard
#endif
749 1d14ffa9 bellard
    err = dsound_lock_out (
750 1d14ffa9 bellard
        dsb,
751 1d14ffa9 bellard
        &hw->info,
752 1d14ffa9 bellard
        old_pos,
753 1d14ffa9 bellard
        len,
754 1d14ffa9 bellard
        &p1, &p2,
755 1d14ffa9 bellard
        &blen1, &blen2,
756 1d14ffa9 bellard
        0
757 1d14ffa9 bellard
        );
758 1d14ffa9 bellard
    if (err) {
759 1d14ffa9 bellard
        return 0;
760 1d14ffa9 bellard
    }
761 1d14ffa9 bellard
762 1d14ffa9 bellard
    len1 = blen1 >> hwshift;
763 1d14ffa9 bellard
    len2 = blen2 >> hwshift;
764 1d14ffa9 bellard
    decr = len1 + len2;
765 1d14ffa9 bellard
766 1d14ffa9 bellard
    if (p1 && len1) {
767 1d14ffa9 bellard
        dsound_write_sample (hw, p1, len1);
768 1d14ffa9 bellard
    }
769 1d14ffa9 bellard
770 1d14ffa9 bellard
    if (p2 && len2) {
771 1d14ffa9 bellard
        dsound_write_sample (hw, p2, len2);
772 1d14ffa9 bellard
    }
773 1d14ffa9 bellard
774 1d14ffa9 bellard
    dsound_unlock_out (dsb, p1, p2, blen1, blen2);
775 c0fe3827 bellard
    ds->old_pos = (old_pos + (decr << hwshift)) % bufsize;
776 1d14ffa9 bellard
777 1d14ffa9 bellard
#ifdef DEBUG_DSOUND
778 1d14ffa9 bellard
    ds->mixed += decr << hwshift;
779 1d14ffa9 bellard
780 1d14ffa9 bellard
    dolog ("played %lu mixed %lu diff %ld sec %f\n",
781 1d14ffa9 bellard
           ds->played,
782 1d14ffa9 bellard
           ds->mixed,
783 1d14ffa9 bellard
           ds->mixed - ds->played,
784 1d14ffa9 bellard
           abs (ds->mixed - ds->played) / (double) hw->info.bytes_per_second);
785 1d14ffa9 bellard
#endif
786 1d14ffa9 bellard
    return decr;
787 1d14ffa9 bellard
}
788 1d14ffa9 bellard
789 1d14ffa9 bellard
static int dsound_ctl_in (HWVoiceIn *hw, int cmd, ...)
790 1d14ffa9 bellard
{
791 1d14ffa9 bellard
    HRESULT hr;
792 1d14ffa9 bellard
    DWORD status;
793 1d14ffa9 bellard
    DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
794 1d14ffa9 bellard
    LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
795 1d14ffa9 bellard
796 1d14ffa9 bellard
    if (!dscb) {
797 1d14ffa9 bellard
        dolog ("Attempt to control capture voice without a buffer\n");
798 1d14ffa9 bellard
        return -1;
799 1d14ffa9 bellard
    }
800 1d14ffa9 bellard
801 1d14ffa9 bellard
    switch (cmd) {
802 1d14ffa9 bellard
    case VOICE_ENABLE:
803 1d14ffa9 bellard
        if (dsound_get_status_in (dscb, &status)) {
804 1d14ffa9 bellard
            return -1;
805 1d14ffa9 bellard
        }
806 1d14ffa9 bellard
807 1d14ffa9 bellard
        if (status & DSCBSTATUS_CAPTURING) {
808 c0fe3827 bellard
            dolog ("warning: Voice is already capturing\n");
809 1d14ffa9 bellard
            return 0;
810 1d14ffa9 bellard
        }
811 1d14ffa9 bellard
812 1d14ffa9 bellard
        /* clear ?? */
813 1d14ffa9 bellard
814 1d14ffa9 bellard
        hr = IDirectSoundCaptureBuffer_Start (dscb, DSCBSTART_LOOPING);
815 1d14ffa9 bellard
        if (FAILED (hr)) {
816 c0fe3827 bellard
            dsound_logerr (hr, "Could not start capturing\n");
817 1d14ffa9 bellard
            return -1;
818 1d14ffa9 bellard
        }
819 1d14ffa9 bellard
        break;
820 1d14ffa9 bellard
821 1d14ffa9 bellard
    case VOICE_DISABLE:
822 1d14ffa9 bellard
        if (dsound_get_status_in (dscb, &status)) {
823 1d14ffa9 bellard
            return -1;
824 1d14ffa9 bellard
        }
825 1d14ffa9 bellard
826 1d14ffa9 bellard
        if (status & DSCBSTATUS_CAPTURING) {
827 1d14ffa9 bellard
            hr = IDirectSoundCaptureBuffer_Stop (dscb);
828 1d14ffa9 bellard
            if (FAILED (hr)) {
829 c0fe3827 bellard
                dsound_logerr (hr, "Could not stop capturing\n");
830 1d14ffa9 bellard
                return -1;
831 1d14ffa9 bellard
            }
832 1d14ffa9 bellard
        }
833 1d14ffa9 bellard
        else {
834 c0fe3827 bellard
            dolog ("warning: Voice is not capturing\n");
835 1d14ffa9 bellard
        }
836 1d14ffa9 bellard
        break;
837 1d14ffa9 bellard
    }
838 1d14ffa9 bellard
    return 0;
839 1d14ffa9 bellard
}
840 1d14ffa9 bellard
841 1d14ffa9 bellard
static int dsound_read (SWVoiceIn *sw, void *buf, int len)
842 1d14ffa9 bellard
{
843 1d14ffa9 bellard
    return audio_pcm_sw_read (sw, buf, len);
844 1d14ffa9 bellard
}
845 1d14ffa9 bellard
846 1d14ffa9 bellard
static int dsound_run_in (HWVoiceIn *hw)
847 1d14ffa9 bellard
{
848 1d14ffa9 bellard
    int err;
849 1d14ffa9 bellard
    HRESULT hr;
850 1d14ffa9 bellard
    DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
851 1d14ffa9 bellard
    LPDIRECTSOUNDCAPTUREBUFFER dscb = ds->dsound_capture_buffer;
852 1d14ffa9 bellard
    int live, len, dead;
853 1d14ffa9 bellard
    DWORD blen1, blen2;
854 1d14ffa9 bellard
    DWORD len1, len2;
855 1d14ffa9 bellard
    DWORD decr;
856 1d14ffa9 bellard
    DWORD cpos, rpos;
857 1d14ffa9 bellard
    LPVOID p1, p2;
858 1d14ffa9 bellard
    int hwshift;
859 1d14ffa9 bellard
860 1d14ffa9 bellard
    if (!dscb) {
861 1d14ffa9 bellard
        dolog ("Attempt to run without capture buffer\n");
862 1d14ffa9 bellard
        return 0;
863 1d14ffa9 bellard
    }
864 1d14ffa9 bellard
865 1d14ffa9 bellard
    hwshift = hw->info.shift;
866 1d14ffa9 bellard
867 1d14ffa9 bellard
    live = audio_pcm_hw_get_live_in (hw);
868 1d14ffa9 bellard
    dead = hw->samples - live;
869 1d14ffa9 bellard
    if (!dead) {
870 1d14ffa9 bellard
        return 0;
871 1d14ffa9 bellard
    }
872 1d14ffa9 bellard
873 1d14ffa9 bellard
    hr = IDirectSoundCaptureBuffer_GetCurrentPosition (
874 1d14ffa9 bellard
        dscb,
875 1d14ffa9 bellard
        &cpos,
876 1d14ffa9 bellard
        ds->first_time ? &rpos : NULL
877 1d14ffa9 bellard
        );
878 1d14ffa9 bellard
    if (FAILED (hr)) {
879 c0fe3827 bellard
        dsound_logerr (hr, "Could not get capture buffer position\n");
880 1d14ffa9 bellard
        return 0;
881 1d14ffa9 bellard
    }
882 1d14ffa9 bellard
883 1d14ffa9 bellard
    if (ds->first_time) {
884 1d14ffa9 bellard
        ds->first_time = 0;
885 1d14ffa9 bellard
        if (rpos & hw->info.align) {
886 c0fe3827 bellard
            ldebug ("warning: Misaligned capture read position %ld(%d)\n",
887 1d14ffa9 bellard
                    rpos, hw->info.align);
888 1d14ffa9 bellard
        }
889 1d14ffa9 bellard
        hw->wpos = rpos >> hwshift;
890 1d14ffa9 bellard
    }
891 1d14ffa9 bellard
892 1d14ffa9 bellard
    if (cpos & hw->info.align) {
893 c0fe3827 bellard
        ldebug ("warning: Misaligned capture position %ld(%d)\n",
894 1d14ffa9 bellard
                cpos, hw->info.align);
895 1d14ffa9 bellard
    }
896 1d14ffa9 bellard
    cpos >>= hwshift;
897 1d14ffa9 bellard
898 1d14ffa9 bellard
    len = audio_ring_dist (cpos, hw->wpos, hw->samples);
899 1d14ffa9 bellard
    if (!len) {
900 1d14ffa9 bellard
        return 0;
901 1d14ffa9 bellard
    }
902 1d14ffa9 bellard
    len = audio_MIN (len, dead);
903 1d14ffa9 bellard
904 1d14ffa9 bellard
    err = dsound_lock_in (
905 1d14ffa9 bellard
        dscb,
906 1d14ffa9 bellard
        &hw->info,
907 1d14ffa9 bellard
        hw->wpos << hwshift,
908 1d14ffa9 bellard
        len << hwshift,
909 1d14ffa9 bellard
        &p1,
910 1d14ffa9 bellard
        &p2,
911 1d14ffa9 bellard
        &blen1,
912 1d14ffa9 bellard
        &blen2,
913 1d14ffa9 bellard
        0
914 1d14ffa9 bellard
        );
915 1d14ffa9 bellard
    if (err) {
916 1d14ffa9 bellard
        return 0;
917 1d14ffa9 bellard
    }
918 1d14ffa9 bellard
919 1d14ffa9 bellard
    len1 = blen1 >> hwshift;
920 1d14ffa9 bellard
    len2 = blen2 >> hwshift;
921 1d14ffa9 bellard
    decr = len1 + len2;
922 1d14ffa9 bellard
923 1d14ffa9 bellard
    if (p1 && len1) {
924 1d14ffa9 bellard
        hw->conv (hw->conv_buf + hw->wpos, p1, len1, &nominal_volume);
925 1d14ffa9 bellard
    }
926 1d14ffa9 bellard
927 1d14ffa9 bellard
    if (p2 && len2) {
928 1d14ffa9 bellard
        hw->conv (hw->conv_buf, p2, len2, &nominal_volume);
929 1d14ffa9 bellard
    }
930 1d14ffa9 bellard
931 1d14ffa9 bellard
    dsound_unlock_in (dscb, p1, p2, blen1, blen2);
932 1d14ffa9 bellard
    hw->wpos = (hw->wpos + decr) % hw->samples;
933 1d14ffa9 bellard
    return decr;
934 1d14ffa9 bellard
}
935 1d14ffa9 bellard
936 1d14ffa9 bellard
static void dsound_audio_fini (void *opaque)
937 1d14ffa9 bellard
{
938 1d14ffa9 bellard
    HRESULT hr;
939 1d14ffa9 bellard
    dsound *s = opaque;
940 1d14ffa9 bellard
941 1d14ffa9 bellard
    if (!s->dsound) {
942 1d14ffa9 bellard
        return;
943 1d14ffa9 bellard
    }
944 1d14ffa9 bellard
945 1d14ffa9 bellard
    hr = IDirectSound_Release (s->dsound);
946 1d14ffa9 bellard
    if (FAILED (hr)) {
947 c0fe3827 bellard
        dsound_logerr (hr, "Could not release DirectSound\n");
948 1d14ffa9 bellard
    }
949 1d14ffa9 bellard
    s->dsound = NULL;
950 1d14ffa9 bellard
951 1d14ffa9 bellard
    if (!s->dsound_capture) {
952 1d14ffa9 bellard
        return;
953 1d14ffa9 bellard
    }
954 1d14ffa9 bellard
955 1d14ffa9 bellard
    hr = IDirectSoundCapture_Release (s->dsound_capture);
956 1d14ffa9 bellard
    if (FAILED (hr)) {
957 c0fe3827 bellard
        dsound_logerr (hr, "Could not release DirectSoundCapture\n");
958 1d14ffa9 bellard
    }
959 1d14ffa9 bellard
    s->dsound_capture = NULL;
960 1d14ffa9 bellard
}
961 1d14ffa9 bellard
962 1d14ffa9 bellard
static void *dsound_audio_init (void)
963 1d14ffa9 bellard
{
964 1d14ffa9 bellard
    int err;
965 1d14ffa9 bellard
    HRESULT hr;
966 1d14ffa9 bellard
    dsound *s = &glob_dsound;
967 1d14ffa9 bellard
968 1d14ffa9 bellard
    hr = CoInitialize (NULL);
969 1d14ffa9 bellard
    if (FAILED (hr)) {
970 c0fe3827 bellard
        dsound_logerr (hr, "Could not initialize COM\n");
971 1d14ffa9 bellard
        return NULL;
972 1d14ffa9 bellard
    }
973 1d14ffa9 bellard
974 1d14ffa9 bellard
    hr = CoCreateInstance (
975 1d14ffa9 bellard
        &CLSID_DirectSound,
976 1d14ffa9 bellard
        NULL,
977 1d14ffa9 bellard
        CLSCTX_ALL,
978 1d14ffa9 bellard
        &IID_IDirectSound,
979 1d14ffa9 bellard
        (void **) &s->dsound
980 1d14ffa9 bellard
        );
981 1d14ffa9 bellard
    if (FAILED (hr)) {
982 c0fe3827 bellard
        dsound_logerr (hr, "Could not create DirectSound instance\n");
983 1d14ffa9 bellard
        return NULL;
984 1d14ffa9 bellard
    }
985 1d14ffa9 bellard
986 1d14ffa9 bellard
    hr = IDirectSound_Initialize (s->dsound, NULL);
987 1d14ffa9 bellard
    if (FAILED (hr)) {
988 c0fe3827 bellard
        dsound_logerr (hr, "Could not initialize DirectSound\n");
989 8ead62cf bellard
990 8ead62cf bellard
        hr = IDirectSound_Release (s->dsound);
991 8ead62cf bellard
        if (FAILED (hr)) {
992 8ead62cf bellard
            dsound_logerr (hr, "Could not release DirectSound\n");
993 8ead62cf bellard
        }
994 8ead62cf bellard
        s->dsound = NULL;
995 1d14ffa9 bellard
        return NULL;
996 1d14ffa9 bellard
    }
997 1d14ffa9 bellard
998 1d14ffa9 bellard
    hr = CoCreateInstance (
999 1d14ffa9 bellard
        &CLSID_DirectSoundCapture,
1000 1d14ffa9 bellard
        NULL,
1001 1d14ffa9 bellard
        CLSCTX_ALL,
1002 1d14ffa9 bellard
        &IID_IDirectSoundCapture,
1003 1d14ffa9 bellard
        (void **) &s->dsound_capture
1004 1d14ffa9 bellard
        );
1005 1d14ffa9 bellard
    if (FAILED (hr)) {
1006 c0fe3827 bellard
        dsound_logerr (hr, "Could not create DirectSoundCapture instance\n");
1007 1d14ffa9 bellard
    }
1008 1d14ffa9 bellard
    else {
1009 1d14ffa9 bellard
        hr = IDirectSoundCapture_Initialize (s->dsound_capture, NULL);
1010 1d14ffa9 bellard
        if (FAILED (hr)) {
1011 c0fe3827 bellard
            dsound_logerr (hr, "Could not initialize DirectSoundCapture\n");
1012 1d14ffa9 bellard
1013 1d14ffa9 bellard
            hr = IDirectSoundCapture_Release (s->dsound_capture);
1014 1d14ffa9 bellard
            if (FAILED (hr)) {
1015 c0fe3827 bellard
                dsound_logerr (hr, "Could not release DirectSoundCapture\n");
1016 1d14ffa9 bellard
            }
1017 1d14ffa9 bellard
            s->dsound_capture = NULL;
1018 1d14ffa9 bellard
        }
1019 1d14ffa9 bellard
    }
1020 1d14ffa9 bellard
1021 1d14ffa9 bellard
    err = dsound_open (s);
1022 1d14ffa9 bellard
    if (err) {
1023 1d14ffa9 bellard
        dsound_audio_fini (s);
1024 1d14ffa9 bellard
        return NULL;
1025 1d14ffa9 bellard
    }
1026 1d14ffa9 bellard
1027 1d14ffa9 bellard
    return s;
1028 1d14ffa9 bellard
}
1029 1d14ffa9 bellard
1030 1d14ffa9 bellard
static struct audio_option dsound_options[] = {
1031 1d14ffa9 bellard
    {"LOCK_RETRIES", AUD_OPT_INT, &conf.lock_retries,
1032 1d14ffa9 bellard
     "Number of times to attempt locking the buffer", NULL, 0},
1033 1d14ffa9 bellard
    {"RESTOURE_RETRIES", AUD_OPT_INT, &conf.restore_retries,
1034 1d14ffa9 bellard
     "Number of times to attempt restoring the buffer", NULL, 0},
1035 1d14ffa9 bellard
    {"GETSTATUS_RETRIES", AUD_OPT_INT, &conf.getstatus_retries,
1036 1d14ffa9 bellard
     "Number of times to attempt getting status of the buffer", NULL, 0},
1037 1d14ffa9 bellard
    {"SET_PRIMARY", AUD_OPT_BOOL, &conf.set_primary,
1038 1d14ffa9 bellard
     "Set the parameters of primary buffer", NULL, 0},
1039 1d14ffa9 bellard
    {"LATENCY_MILLIS", AUD_OPT_INT, &conf.latency_millis,
1040 1d14ffa9 bellard
     "(undocumented)", NULL, 0},
1041 c0fe3827 bellard
    {"PRIMARY_FREQ", AUD_OPT_INT, &conf.settings.freq,
1042 1d14ffa9 bellard
     "Primary buffer frequency", NULL, 0},
1043 c0fe3827 bellard
    {"PRIMARY_CHANNELS", AUD_OPT_INT, &conf.settings.nchannels,
1044 1d14ffa9 bellard
     "Primary buffer number of channels (1 - mono, 2 - stereo)", NULL, 0},
1045 c0fe3827 bellard
    {"PRIMARY_FMT", AUD_OPT_FMT, &conf.settings.fmt,
1046 1d14ffa9 bellard
     "Primary buffer format", NULL, 0},
1047 1d14ffa9 bellard
    {"BUFSIZE_OUT", AUD_OPT_INT, &conf.bufsize_out,
1048 1d14ffa9 bellard
     "(undocumented)", NULL, 0},
1049 1d14ffa9 bellard
    {"BUFSIZE_IN", AUD_OPT_INT, &conf.bufsize_in,
1050 1d14ffa9 bellard
     "(undocumented)", NULL, 0},
1051 1d14ffa9 bellard
    {NULL, 0, NULL, NULL, NULL, 0}
1052 1d14ffa9 bellard
};
1053 1d14ffa9 bellard
1054 1d14ffa9 bellard
static struct audio_pcm_ops dsound_pcm_ops = {
1055 1d14ffa9 bellard
    dsound_init_out,
1056 1d14ffa9 bellard
    dsound_fini_out,
1057 1d14ffa9 bellard
    dsound_run_out,
1058 1d14ffa9 bellard
    dsound_write,
1059 1d14ffa9 bellard
    dsound_ctl_out,
1060 1d14ffa9 bellard
1061 1d14ffa9 bellard
    dsound_init_in,
1062 1d14ffa9 bellard
    dsound_fini_in,
1063 1d14ffa9 bellard
    dsound_run_in,
1064 1d14ffa9 bellard
    dsound_read,
1065 1d14ffa9 bellard
    dsound_ctl_in
1066 1d14ffa9 bellard
};
1067 1d14ffa9 bellard
1068 1d14ffa9 bellard
struct audio_driver dsound_audio_driver = {
1069 1d14ffa9 bellard
    INIT_FIELD (name           = ) "dsound",
1070 1d14ffa9 bellard
    INIT_FIELD (descr          = )
1071 1d14ffa9 bellard
    "DirectSound http://wikipedia.org/wiki/DirectSound",
1072 1d14ffa9 bellard
    INIT_FIELD (options        = ) dsound_options,
1073 1d14ffa9 bellard
    INIT_FIELD (init           = ) dsound_audio_init,
1074 1d14ffa9 bellard
    INIT_FIELD (fini           = ) dsound_audio_fini,
1075 1d14ffa9 bellard
    INIT_FIELD (pcm_ops        = ) &dsound_pcm_ops,
1076 1d14ffa9 bellard
    INIT_FIELD (can_be_default = ) 1,
1077 1d14ffa9 bellard
    INIT_FIELD (max_voices_out = ) INT_MAX,
1078 1d14ffa9 bellard
    INIT_FIELD (max_voices_in  = ) 1,
1079 1d14ffa9 bellard
    INIT_FIELD (voice_size_out = ) sizeof (DSoundVoiceOut),
1080 1d14ffa9 bellard
    INIT_FIELD (voice_size_in  = ) sizeof (DSoundVoiceIn)
1081 1d14ffa9 bellard
};