bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU DirectSound audio driver header |
| 3 | * |
| 4 | * Copyright (c) 2005 Vassili Karpov (malc) |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | #ifdef DSBTYPE_IN |
| 25 | #define NAME "capture buffer" |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 26 | #define NAME2 "DirectSoundCapture" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 27 | #define TYPE in |
| 28 | #define IFACE IDirectSoundCaptureBuffer |
| 29 | #define BUFPTR LPDIRECTSOUNDCAPTUREBUFFER |
| 30 | #define FIELD dsound_capture_buffer |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 31 | #define FIELD2 dsound_capture |
Kővágó, Zoltán | 7fa9754 | 2019-09-19 23:24:12 +0200 | [diff] [blame] | 32 | #define HWVOICE HWVoiceIn |
| 33 | #define DSOUNDVOICE DSoundVoiceIn |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 34 | #else |
| 35 | #define NAME "playback buffer" |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 36 | #define NAME2 "DirectSound" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 37 | #define TYPE out |
| 38 | #define IFACE IDirectSoundBuffer |
| 39 | #define BUFPTR LPDIRECTSOUNDBUFFER |
| 40 | #define FIELD dsound_buffer |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 41 | #define FIELD2 dsound |
Kővágó, Zoltán | 7fa9754 | 2019-09-19 23:24:12 +0200 | [diff] [blame] | 42 | #define HWVOICE HWVoiceOut |
| 43 | #define DSOUNDVOICE DSoundVoiceOut |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | static int glue (dsound_unlock_, TYPE) ( |
| 47 | BUFPTR buf, |
| 48 | LPVOID p1, |
| 49 | LPVOID p2, |
| 50 | DWORD blen1, |
| 51 | DWORD blen2 |
| 52 | ) |
| 53 | { |
| 54 | HRESULT hr; |
| 55 | |
| 56 | hr = glue (IFACE, _Unlock) (buf, p1, blen1, p2, blen2); |
| 57 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 58 | dsound_logerr (hr, "Could not unlock " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 59 | return -1; |
| 60 | } |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int glue (dsound_lock_, TYPE) ( |
| 66 | BUFPTR buf, |
| 67 | struct audio_pcm_info *info, |
| 68 | DWORD pos, |
| 69 | DWORD len, |
| 70 | LPVOID *p1p, |
| 71 | LPVOID *p2p, |
| 72 | DWORD *blen1p, |
| 73 | DWORD *blen2p, |
Kővágó, Zoltán | 191e1f0 | 2015-06-03 23:03:52 +0200 | [diff] [blame] | 74 | int entire, |
| 75 | dsound *s |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 76 | ) |
| 77 | { |
| 78 | HRESULT hr; |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 79 | DWORD flag; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 80 | |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 81 | #ifdef DSBTYPE_IN |
| 82 | flag = entire ? DSCBLOCK_ENTIREBUFFER : 0; |
| 83 | #else |
| 84 | flag = entire ? DSBLOCK_ENTIREBUFFER : 0; |
| 85 | #endif |
Kővágó, Zoltán | 7fa9754 | 2019-09-19 23:24:12 +0200 | [diff] [blame] | 86 | hr = glue(IFACE, _Lock)(buf, pos, len, p1p, blen1p, p2p, blen2p, flag); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 87 | |
Kővágó, Zoltán | 2762955 | 2015-06-12 14:33:04 +0200 | [diff] [blame] | 88 | if (FAILED (hr)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 89 | #ifndef DSBTYPE_IN |
Kővágó, Zoltán | 2762955 | 2015-06-12 14:33:04 +0200 | [diff] [blame] | 90 | if (hr == DSERR_BUFFERLOST) { |
| 91 | if (glue (dsound_restore_, TYPE) (buf, s)) { |
| 92 | dsound_logerr (hr, "Could not lock " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 93 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 94 | goto fail; |
| 95 | } |
Kővágó, Zoltán | 2762955 | 2015-06-12 14:33:04 +0200 | [diff] [blame] | 96 | #endif |
| 97 | dsound_logerr (hr, "Could not lock " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 98 | goto fail; |
| 99 | } |
| 100 | |
Kővágó, Zoltán | 2b9cce8 | 2019-10-13 21:58:02 +0200 | [diff] [blame] | 101 | if ((p1p && *p1p && (*blen1p % info->bytes_per_frame)) || |
| 102 | (p2p && *p2p && (*blen2p % info->bytes_per_frame))) { |
Kővágó, Zoltán | 7fa9754 | 2019-09-19 23:24:12 +0200 | [diff] [blame] | 103 | dolog("DirectSound returned misaligned buffer %ld %ld\n", |
| 104 | *blen1p, *blen2p); |
| 105 | glue(dsound_unlock_, TYPE)(buf, *p1p, p2p ? *p2p : NULL, *blen1p, |
| 106 | blen2p ? *blen2p : 0); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 107 | goto fail; |
| 108 | } |
| 109 | |
Kővágó, Zoltán | 7fa9754 | 2019-09-19 23:24:12 +0200 | [diff] [blame] | 110 | if (p1p && !*p1p && *blen1p) { |
| 111 | dolog("warning: !p1 && blen1=%ld\n", *blen1p); |
| 112 | *blen1p = 0; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Kővágó, Zoltán | 7fa9754 | 2019-09-19 23:24:12 +0200 | [diff] [blame] | 115 | if (p2p && !*p2p && *blen2p) { |
| 116 | dolog("warning: !p2 && blen2=%ld\n", *blen2p); |
| 117 | *blen2p = 0; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 118 | } |
| 119 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | |
| 122 | fail: |
| 123 | *p1p = NULL - 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 124 | *blen1p = -1; |
Kővágó, Zoltán | 7fa9754 | 2019-09-19 23:24:12 +0200 | [diff] [blame] | 125 | if (p2p) { |
| 126 | *p2p = NULL - 1; |
| 127 | *blen2p = -1; |
| 128 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 129 | return -1; |
| 130 | } |
| 131 | |
| 132 | #ifdef DSBTYPE_IN |
| 133 | static void dsound_fini_in (HWVoiceIn *hw) |
| 134 | #else |
| 135 | static void dsound_fini_out (HWVoiceOut *hw) |
| 136 | #endif |
| 137 | { |
| 138 | HRESULT hr; |
| 139 | #ifdef DSBTYPE_IN |
| 140 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; |
| 141 | #else |
| 142 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; |
| 143 | #endif |
| 144 | |
| 145 | if (ds->FIELD) { |
| 146 | hr = glue (IFACE, _Stop) (ds->FIELD); |
| 147 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 148 | dsound_logerr (hr, "Could not stop " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | hr = glue (IFACE, _Release) (ds->FIELD); |
| 152 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 153 | dsound_logerr (hr, "Could not release " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 154 | } |
| 155 | ds->FIELD = NULL; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #ifdef DSBTYPE_IN |
Kővágó, Zoltán | 5706db1 | 2015-06-03 23:03:47 +0200 | [diff] [blame] | 160 | static int dsound_init_in(HWVoiceIn *hw, struct audsettings *as, |
| 161 | void *drv_opaque) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 162 | #else |
Kővágó, Zoltán | 5706db1 | 2015-06-03 23:03:47 +0200 | [diff] [blame] | 163 | static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as, |
| 164 | void *drv_opaque) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 165 | #endif |
| 166 | { |
| 167 | int err; |
| 168 | HRESULT hr; |
Kővágó, Zoltán | 191e1f0 | 2015-06-03 23:03:52 +0200 | [diff] [blame] | 169 | dsound *s = drv_opaque; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 170 | WAVEFORMATEX wfx; |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 171 | struct audsettings obt_as; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 172 | #ifdef DSBTYPE_IN |
| 173 | const char *typ = "ADC"; |
| 174 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; |
| 175 | DSCBUFFERDESC bd; |
| 176 | DSCBCAPS bc; |
Kővágó, Zoltán | 4a3b8b3 | 2019-03-08 23:34:18 +0100 | [diff] [blame] | 177 | AudiodevPerDirectionOptions *pdo = s->dev->u.dsound.in; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 178 | #else |
| 179 | const char *typ = "DAC"; |
| 180 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; |
| 181 | DSBUFFERDESC bd; |
| 182 | DSBCAPS bc; |
Kővágó, Zoltán | 4a3b8b3 | 2019-03-08 23:34:18 +0100 | [diff] [blame] | 183 | AudiodevPerDirectionOptions *pdo = s->dev->u.dsound.out; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 184 | #endif |
| 185 | |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 186 | if (!s->FIELD2) { |
balrog | 26463db | 2008-01-17 21:47:25 +0000 | [diff] [blame] | 187 | dolog ("Attempt to initialize voice without " NAME2 " object\n"); |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 188 | return -1; |
| 189 | } |
| 190 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 191 | err = waveformat_from_audio_settings (&wfx, as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 192 | if (err) { |
| 193 | return -1; |
| 194 | } |
| 195 | |
| 196 | memset (&bd, 0, sizeof (bd)); |
| 197 | bd.dwSize = sizeof (bd); |
| 198 | bd.lpwfxFormat = &wfx; |
Kővágó, Zoltán | 4a3b8b3 | 2019-03-08 23:34:18 +0100 | [diff] [blame] | 199 | bd.dwBufferBytes = audio_buffer_bytes(pdo, as, 92880); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 200 | #ifdef DSBTYPE_IN |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 201 | hr = IDirectSoundCapture_CreateCaptureBuffer ( |
| 202 | s->dsound_capture, |
| 203 | &bd, |
| 204 | &ds->dsound_capture_buffer, |
| 205 | NULL |
| 206 | ); |
| 207 | #else |
Volker Rümelin | 401dcf0 | 2021-01-10 11:02:36 +0100 | [diff] [blame] | 208 | bd.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_GETCURRENTPOSITION2; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 209 | hr = IDirectSound_CreateSoundBuffer ( |
| 210 | s->dsound, |
| 211 | &bd, |
| 212 | &ds->dsound_buffer, |
| 213 | NULL |
| 214 | ); |
| 215 | #endif |
| 216 | |
| 217 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 218 | dsound_logerr2 (hr, typ, "Could not create " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 219 | return -1; |
| 220 | } |
| 221 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 222 | hr = glue (IFACE, _GetFormat) (ds->FIELD, &wfx, sizeof (wfx), NULL); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 223 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 224 | dsound_logerr2 (hr, typ, "Could not get " NAME " format\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 225 | goto fail0; |
| 226 | } |
| 227 | |
| 228 | #ifdef DEBUG_DSOUND |
| 229 | dolog (NAME "\n"); |
| 230 | print_wave_format (&wfx); |
| 231 | #endif |
| 232 | |
| 233 | memset (&bc, 0, sizeof (bc)); |
| 234 | bc.dwSize = sizeof (bc); |
| 235 | |
| 236 | hr = glue (IFACE, _GetCaps) (ds->FIELD, &bc); |
| 237 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 238 | dsound_logerr2 (hr, typ, "Could not get " NAME " format\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 239 | goto fail0; |
| 240 | } |
| 241 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 242 | err = waveformat_to_audio_settings (&wfx, &obt_as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 243 | if (err) { |
| 244 | goto fail0; |
| 245 | } |
| 246 | |
Kővágó, Zoltán | fb35c2c | 2020-02-03 00:02:23 +0100 | [diff] [blame] | 247 | ds->first_time = true; |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 248 | obt_as.endianness = 0; |
| 249 | audio_pcm_init_info (&hw->info, &obt_as); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 250 | |
Kővágó, Zoltán | 2b9cce8 | 2019-10-13 21:58:02 +0200 | [diff] [blame] | 251 | if (bc.dwBufferBytes % hw->info.bytes_per_frame) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 252 | dolog ( |
| 253 | "GetCaps returned misaligned buffer size %ld, alignment %d\n", |
Kővágó, Zoltán | 2b9cce8 | 2019-10-13 21:58:02 +0200 | [diff] [blame] | 254 | bc.dwBufferBytes, hw->info.bytes_per_frame |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 255 | ); |
| 256 | } |
Kővágó, Zoltán | 7fa9754 | 2019-09-19 23:24:12 +0200 | [diff] [blame] | 257 | hw->size_emul = bc.dwBufferBytes; |
Kővágó, Zoltán | 2b9cce8 | 2019-10-13 21:58:02 +0200 | [diff] [blame] | 258 | hw->samples = bc.dwBufferBytes / hw->info.bytes_per_frame; |
Kővágó, Zoltán | 191e1f0 | 2015-06-03 23:03:52 +0200 | [diff] [blame] | 259 | ds->s = s; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 260 | |
| 261 | #ifdef DEBUG_DSOUND |
| 262 | dolog ("caps %ld, desc %ld\n", |
| 263 | bc.dwBufferBytes, bd.dwBufferBytes); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 264 | #endif |
| 265 | return 0; |
| 266 | |
| 267 | fail0: |
| 268 | glue (dsound_fini_, TYPE) (hw); |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | #undef NAME |
malc | 832e907 | 2009-01-22 22:09:55 +0000 | [diff] [blame] | 273 | #undef NAME2 |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 274 | #undef TYPE |
| 275 | #undef IFACE |
| 276 | #undef BUFPTR |
| 277 | #undef FIELD |
malc | 832e907 | 2009-01-22 22:09:55 +0000 | [diff] [blame] | 278 | #undef FIELD2 |
Kővágó, Zoltán | 7fa9754 | 2019-09-19 23:24:12 +0200 | [diff] [blame] | 279 | #undef HWVOICE |
| 280 | #undef DSOUNDVOICE |