18b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project/*
28b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * QEMU DirectSound audio driver header
38b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *
48b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * Copyright (c) 2005 Vassili Karpov (malc)
58b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *
68b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * Permission is hereby granted, free of charge, to any person obtaining a copy
78b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * of this software and associated documentation files (the "Software"), to deal
88b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * in the Software without restriction, including without limitation the rights
98b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
108b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * copies of the Software, and to permit persons to whom the Software is
118b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * furnished to do so, subject to the following conditions:
128b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *
138b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * The above copyright notice and this permission notice shall be included in
148b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * all copies or substantial portions of the Software.
158b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project *
168b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
178b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
188b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
198b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
208b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
218b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
228b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project * THE SOFTWARE.
238b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project */
248b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifdef DSBTYPE_IN
258b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define NAME "capture buffer"
268b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define NAME2 "DirectSoundCapture"
278b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TYPE in
288b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define IFACE IDirectSoundCaptureBuffer
298b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define BUFPTR LPDIRECTSOUNDCAPTUREBUFFER
308b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define FIELD dsound_capture_buffer
318b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define FIELD2 dsound_capture
328b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#else
338b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define NAME "playback buffer"
348b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define NAME2 "DirectSound"
358b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define TYPE out
368b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define IFACE IDirectSoundBuffer
378b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define BUFPTR LPDIRECTSOUNDBUFFER
388b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define FIELD dsound_buffer
398b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#define FIELD2 dsound
408b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
418b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
428b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectstatic int glue (dsound_unlock_, TYPE) (
438b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    BUFPTR buf,
448b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    LPVOID p1,
458b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    LPVOID p2,
468b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DWORD blen1,
478b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DWORD blen2
488b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    )
498b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project{
508b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    HRESULT hr;
518b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
528b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    hr = glue (IFACE, _Unlock) (buf, p1, blen1, p2, blen2);
538b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (FAILED (hr)) {
548b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        dsound_logerr (hr, "Could not unlock " NAME "\n");
558b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        return -1;
568b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
578b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
588b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    return 0;
598b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project}
608b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
618b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectstatic int glue (dsound_lock_, TYPE) (
628b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    BUFPTR buf,
638b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    struct audio_pcm_info *info,
648b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DWORD pos,
658b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DWORD len,
668b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    LPVOID *p1p,
678b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    LPVOID *p2p,
688b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DWORD *blen1p,
698b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DWORD *blen2p,
708b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    int entire
718b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    )
728b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project{
738b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    HRESULT hr;
748b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    int i;
758b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    LPVOID p1 = NULL, p2 = NULL;
768b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DWORD blen1 = 0, blen2 = 0;
778b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DWORD flag;
788b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
798b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifdef DSBTYPE_IN
808b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    flag = entire ? DSCBLOCK_ENTIREBUFFER : 0;
818b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#else
828b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    flag = entire ? DSBLOCK_ENTIREBUFFER : 0;
838b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
848b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    for (i = 0; i < conf.lock_retries; ++i) {
858b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        hr = glue (IFACE, _Lock) (
868b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            buf,
878b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            pos,
888b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            len,
898b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            &p1,
908b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            &blen1,
918b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            &p2,
928b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            &blen2,
938b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            flag
948b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            );
958b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
968b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        if (FAILED (hr)) {
978b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifndef DSBTYPE_IN
988b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            if (hr == DSERR_BUFFERLOST) {
998b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project                if (glue (dsound_restore_, TYPE) (buf)) {
1008b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project                    dsound_logerr (hr, "Could not lock " NAME "\n");
1018b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project                    goto fail;
1028b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project                }
1038b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project                continue;
1048b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            }
1058b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
1068b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            dsound_logerr (hr, "Could not lock " NAME "\n");
1078b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            goto fail;
1088b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        }
1098b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1108b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        break;
1118b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
1128b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1138b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (i == conf.lock_retries) {
1148b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        dolog ("%d attempts to lock " NAME " failed\n", i);
1158b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        goto fail;
1168b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
1178b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1188b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if ((p1 && (blen1 & info->align)) || (p2 && (blen2 & info->align))) {
1198b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        dolog ("DirectSound returned misaligned buffer %ld %ld\n",
1208b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project               blen1, blen2);
1218b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        glue (dsound_unlock_, TYPE) (buf, p1, p2, blen1, blen2);
1228b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        goto fail;
1238b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
1248b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1258b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (!p1 && blen1) {
1268b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        dolog ("warning: !p1 && blen1=%ld\n", blen1);
1278b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        blen1 = 0;
1288b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
1298b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1308b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (!p2 && blen2) {
1318b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        dolog ("warning: !p2 && blen2=%ld\n", blen2);
1328b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        blen2 = 0;
1338b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
1348b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1358b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    *p1p = p1;
1368b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    *p2p = p2;
1378b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    *blen1p = blen1;
1388b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    *blen2p = blen2;
1398b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    return 0;
1408b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1418b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project fail:
1428b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    *p1p = NULL - 1;
1438b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    *p2p = NULL - 1;
1448b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    *blen1p = -1;
1458b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    *blen2p = -1;
1468b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    return -1;
1478b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project}
1488b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1498b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifdef DSBTYPE_IN
1508b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectstatic void dsound_fini_in (HWVoiceIn *hw)
1518b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#else
1528b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Projectstatic void dsound_fini_out (HWVoiceOut *hw)
1538b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
1548b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project{
1558b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    HRESULT hr;
1568b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifdef DSBTYPE_IN
1578b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
1588b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#else
1598b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
1608b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
1618b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1628b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (ds->FIELD) {
1638b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        hr = glue (IFACE, _Stop) (ds->FIELD);
1648b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        if (FAILED (hr)) {
1658b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            dsound_logerr (hr, "Could not stop " NAME "\n");
1668b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        }
1678b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1688b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        hr = glue (IFACE, _Release) (ds->FIELD);
1698b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        if (FAILED (hr)) {
1708b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            dsound_logerr (hr, "Could not release " NAME "\n");
1718b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        }
1728b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        ds->FIELD = NULL;
1738b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
1748b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project}
1758b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1768b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifdef DSBTYPE_IN
1775d0e37bc290d1743cb5acf76eb6608f1303f27ddDavid 'Digit' Turnerstatic int dsound_init_in (HWVoiceIn *hw, struct audsettings *as)
1788b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#else
1795d0e37bc290d1743cb5acf76eb6608f1303f27ddDavid 'Digit' Turnerstatic int dsound_init_out (HWVoiceOut *hw, struct audsettings *as)
1808b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
1818b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project{
1828b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    int err;
1838b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    HRESULT hr;
1848b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    dsound *s = &glob_dsound;
1858b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    WAVEFORMATEX wfx;
1865d0e37bc290d1743cb5acf76eb6608f1303f27ddDavid 'Digit' Turner    struct audsettings obt_as;
1878b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifdef DSBTYPE_IN
1888b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    const char *typ = "ADC";
1898b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
1908b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DSCBUFFERDESC bd;
1918b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DSCBCAPS bc;
1928b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#else
1938b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    const char *typ = "DAC";
1948b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
1958b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DSBUFFERDESC bd;
1968b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    DSBCAPS bc;
1978b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
1988b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
1998b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (!s->FIELD2) {
2008b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        dolog ("Attempt to initialize voice without " NAME2 " object\n");
2018b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        return -1;
2028b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
2038b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2048b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    err = waveformat_from_audio_settings (&wfx, as);
2058b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (err) {
2068b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        return -1;
2078b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
2088b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2098b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    memset (&bd, 0, sizeof (bd));
2108b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    bd.dwSize = sizeof (bd);
2118b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    bd.lpwfxFormat = &wfx;
2128b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifdef DSBTYPE_IN
2138b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    bd.dwBufferBytes = conf.bufsize_in;
2148b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    hr = IDirectSoundCapture_CreateCaptureBuffer (
2158b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        s->dsound_capture,
2168b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        &bd,
2178b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        &ds->dsound_capture_buffer,
2188b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        NULL
2198b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        );
2208b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#else
2218b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    bd.dwFlags = DSBCAPS_STICKYFOCUS | DSBCAPS_GETCURRENTPOSITION2;
2228b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    bd.dwBufferBytes = conf.bufsize_out;
2238b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    hr = IDirectSound_CreateSoundBuffer (
2248b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        s->dsound,
2258b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        &bd,
2268b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        &ds->dsound_buffer,
2278b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        NULL
2288b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        );
2298b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
2308b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2318b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (FAILED (hr)) {
2328b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        dsound_logerr2 (hr, typ, "Could not create " NAME "\n");
2338b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        return -1;
2348b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
2358b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2368b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    hr = glue (IFACE, _GetFormat) (ds->FIELD, &wfx, sizeof (wfx), NULL);
2378b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (FAILED (hr)) {
2388b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        dsound_logerr2 (hr, typ, "Could not get " NAME " format\n");
2398b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        goto fail0;
2408b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
2418b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2428b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifdef DEBUG_DSOUND
2438b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    dolog (NAME "\n");
2448b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    print_wave_format (&wfx);
2458b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
2468b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2478b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    memset (&bc, 0, sizeof (bc));
2488b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    bc.dwSize = sizeof (bc);
2498b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2508b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    hr = glue (IFACE, _GetCaps) (ds->FIELD, &bc);
2518b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (FAILED (hr)) {
2528b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        dsound_logerr2 (hr, typ, "Could not get " NAME " format\n");
2538b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        goto fail0;
2548b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
2558b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2568b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    err = waveformat_to_audio_settings (&wfx, &obt_as);
2578b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (err) {
2588b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        goto fail0;
2598b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
2608b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2618b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    ds->first_time = 1;
2628b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    obt_as.endianness = 0;
2638b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    audio_pcm_init_info (&hw->info, &obt_as);
2648b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2658b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    if (bc.dwBufferBytes & hw->info.align) {
2668b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project        dolog (
2678b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            "GetCaps returned misaligned buffer size %ld, alignment %d\n",
2688b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            bc.dwBufferBytes, hw->info.align + 1
2698b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project            );
2708b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    }
2718b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    hw->samples = bc.dwBufferBytes >> hw->info.shift;
2728b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2738b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#ifdef DEBUG_DSOUND
2748b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    dolog ("caps %ld, desc %ld\n",
2758b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project           bc.dwBufferBytes, bd.dwBufferBytes);
2768b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2778b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    dolog ("bufsize %d, freq %d, chan %d, fmt %d\n",
2788b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project           hw->bufsize, settings.freq, settings.nchannels, settings.fmt);
2798b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#endif
2808b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    return 0;
2818b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2828b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project fail0:
2838b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    glue (dsound_fini_, TYPE) (hw);
2848b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project    return -1;
2858b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project}
2868b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project
2878b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#undef NAME
2885d0e37bc290d1743cb5acf76eb6608f1303f27ddDavid 'Digit' Turner#undef NAME2
2898b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#undef TYPE
2908b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#undef IFACE
2918b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#undef BUFPTR
2928b23a6c7e1aee255004dd19098d4c2462b61b849The Android Open Source Project#undef FIELD
2935d0e37bc290d1743cb5acf76eb6608f1303f27ddDavid 'Digit' Turner#undef FIELD2
294