Statistics
| Branch: | Revision:

root / audio / dsoundaudio.c @ 6c270db7

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