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 |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 32 | #else |
| 33 | #define NAME "playback buffer" |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 34 | #define NAME2 "DirectSound" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 35 | #define TYPE out |
| 36 | #define IFACE IDirectSoundBuffer |
| 37 | #define BUFPTR LPDIRECTSOUNDBUFFER |
| 38 | #define FIELD dsound_buffer |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 39 | #define FIELD2 dsound |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
| 42 | static int glue (dsound_unlock_, TYPE) ( |
| 43 | BUFPTR buf, |
| 44 | LPVOID p1, |
| 45 | LPVOID p2, |
| 46 | DWORD blen1, |
| 47 | DWORD blen2 |
| 48 | ) |
| 49 | { |
| 50 | HRESULT hr; |
| 51 | |
| 52 | hr = glue (IFACE, _Unlock) (buf, p1, blen1, p2, blen2); |
| 53 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 54 | dsound_logerr (hr, "Could not unlock " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int glue (dsound_lock_, TYPE) ( |
| 62 | BUFPTR buf, |
| 63 | struct audio_pcm_info *info, |
| 64 | DWORD pos, |
| 65 | DWORD len, |
| 66 | LPVOID *p1p, |
| 67 | LPVOID *p2p, |
| 68 | DWORD *blen1p, |
| 69 | DWORD *blen2p, |
Kővágó, Zoltán | 191e1f0 | 2015-06-03 23:03:52 +0200 | [diff] [blame] | 70 | int entire, |
| 71 | dsound *s |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 72 | ) |
| 73 | { |
| 74 | HRESULT hr; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 75 | LPVOID p1 = NULL, p2 = NULL; |
| 76 | DWORD blen1 = 0, blen2 = 0; |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 77 | DWORD flag; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 78 | |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 79 | #ifdef DSBTYPE_IN |
| 80 | flag = entire ? DSCBLOCK_ENTIREBUFFER : 0; |
| 81 | #else |
| 82 | flag = entire ? DSBLOCK_ENTIREBUFFER : 0; |
| 83 | #endif |
Kővágó, Zoltán | 2762955 | 2015-06-12 14:33:04 +0200 | [diff] [blame] | 84 | hr = glue(IFACE, _Lock)(buf, pos, len, &p1, &blen1, &p2, &blen2, flag); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 85 | |
Kővágó, Zoltán | 2762955 | 2015-06-12 14:33:04 +0200 | [diff] [blame] | 86 | if (FAILED (hr)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 87 | #ifndef DSBTYPE_IN |
Kővágó, Zoltán | 2762955 | 2015-06-12 14:33:04 +0200 | [diff] [blame] | 88 | if (hr == DSERR_BUFFERLOST) { |
| 89 | if (glue (dsound_restore_, TYPE) (buf, s)) { |
| 90 | dsound_logerr (hr, "Could not lock " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 91 | } |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 92 | goto fail; |
| 93 | } |
Kővágó, Zoltán | 2762955 | 2015-06-12 14:33:04 +0200 | [diff] [blame] | 94 | #endif |
| 95 | dsound_logerr (hr, "Could not lock " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 96 | goto fail; |
| 97 | } |
| 98 | |
| 99 | if ((p1 && (blen1 & info->align)) || (p2 && (blen2 & info->align))) { |
| 100 | dolog ("DirectSound returned misaligned buffer %ld %ld\n", |
| 101 | blen1, blen2); |
| 102 | glue (dsound_unlock_, TYPE) (buf, p1, p2, blen1, blen2); |
| 103 | goto fail; |
| 104 | } |
| 105 | |
| 106 | if (!p1 && blen1) { |
| 107 | dolog ("warning: !p1 && blen1=%ld\n", blen1); |
| 108 | blen1 = 0; |
| 109 | } |
| 110 | |
| 111 | if (!p2 && blen2) { |
| 112 | dolog ("warning: !p2 && blen2=%ld\n", blen2); |
| 113 | blen2 = 0; |
| 114 | } |
| 115 | |
| 116 | *p1p = p1; |
| 117 | *p2p = p2; |
| 118 | *blen1p = blen1; |
| 119 | *blen2p = blen2; |
| 120 | return 0; |
| 121 | |
| 122 | fail: |
| 123 | *p1p = NULL - 1; |
| 124 | *p2p = NULL - 1; |
| 125 | *blen1p = -1; |
| 126 | *blen2p = -1; |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | #ifdef DSBTYPE_IN |
| 131 | static void dsound_fini_in (HWVoiceIn *hw) |
| 132 | #else |
| 133 | static void dsound_fini_out (HWVoiceOut *hw) |
| 134 | #endif |
| 135 | { |
| 136 | HRESULT hr; |
| 137 | #ifdef DSBTYPE_IN |
| 138 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; |
| 139 | #else |
| 140 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; |
| 141 | #endif |
| 142 | |
| 143 | if (ds->FIELD) { |
| 144 | hr = glue (IFACE, _Stop) (ds->FIELD); |
| 145 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 146 | dsound_logerr (hr, "Could not stop " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | hr = glue (IFACE, _Release) (ds->FIELD); |
| 150 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 151 | dsound_logerr (hr, "Could not release " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 152 | } |
| 153 | ds->FIELD = NULL; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #ifdef DSBTYPE_IN |
Kővágó, Zoltán | 5706db1 | 2015-06-03 23:03:47 +0200 | [diff] [blame] | 158 | static int dsound_init_in(HWVoiceIn *hw, struct audsettings *as, |
| 159 | void *drv_opaque) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 160 | #else |
Kővágó, Zoltán | 5706db1 | 2015-06-03 23:03:47 +0200 | [diff] [blame] | 161 | static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as, |
| 162 | void *drv_opaque) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 163 | #endif |
| 164 | { |
| 165 | int err; |
| 166 | HRESULT hr; |
Kővágó, Zoltán | 191e1f0 | 2015-06-03 23:03:52 +0200 | [diff] [blame] | 167 | dsound *s = drv_opaque; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 168 | WAVEFORMATEX wfx; |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 169 | struct audsettings obt_as; |
Kővágó, Zoltán | 191e1f0 | 2015-06-03 23:03:52 +0200 | [diff] [blame] | 170 | DSoundConf *conf = &s->conf; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 171 | #ifdef DSBTYPE_IN |
| 172 | const char *typ = "ADC"; |
| 173 | DSoundVoiceIn *ds = (DSoundVoiceIn *) hw; |
| 174 | DSCBUFFERDESC bd; |
| 175 | DSCBCAPS bc; |
| 176 | #else |
| 177 | const char *typ = "DAC"; |
| 178 | DSoundVoiceOut *ds = (DSoundVoiceOut *) hw; |
| 179 | DSBUFFERDESC bd; |
| 180 | DSBCAPS bc; |
| 181 | #endif |
| 182 | |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 183 | if (!s->FIELD2) { |
balrog | 26463db | 2008-01-17 21:47:25 +0000 | [diff] [blame] | 184 | dolog ("Attempt to initialize voice without " NAME2 " object\n"); |
balrog | ca9cc28 | 2008-01-14 04:24:29 +0000 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 188 | err = waveformat_from_audio_settings (&wfx, as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 189 | if (err) { |
| 190 | return -1; |
| 191 | } |
| 192 | |
| 193 | memset (&bd, 0, sizeof (bd)); |
| 194 | bd.dwSize = sizeof (bd); |
| 195 | bd.lpwfxFormat = &wfx; |
| 196 | #ifdef DSBTYPE_IN |
Kővágó, Zoltán | 191e1f0 | 2015-06-03 23:03:52 +0200 | [diff] [blame] | 197 | bd.dwBufferBytes = conf->bufsize_in; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 198 | hr = IDirectSoundCapture_CreateCaptureBuffer ( |
| 199 | s->dsound_capture, |
| 200 | &bd, |
| 201 | &ds->dsound_capture_buffer, |
| 202 | NULL |
| 203 | ); |
| 204 | #else |
| 205 | bd.dwFlags = DSBCAPS_STICKYFOCUS | DSBCAPS_GETCURRENTPOSITION2; |
Kővágó, Zoltán | 191e1f0 | 2015-06-03 23:03:52 +0200 | [diff] [blame] | 206 | bd.dwBufferBytes = conf->bufsize_out; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 207 | hr = IDirectSound_CreateSoundBuffer ( |
| 208 | s->dsound, |
| 209 | &bd, |
| 210 | &ds->dsound_buffer, |
| 211 | NULL |
| 212 | ); |
| 213 | #endif |
| 214 | |
| 215 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 216 | dsound_logerr2 (hr, typ, "Could not create " NAME "\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 217 | return -1; |
| 218 | } |
| 219 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 220 | hr = glue (IFACE, _GetFormat) (ds->FIELD, &wfx, sizeof (wfx), NULL); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 221 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 222 | dsound_logerr2 (hr, typ, "Could not get " NAME " format\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 223 | goto fail0; |
| 224 | } |
| 225 | |
| 226 | #ifdef DEBUG_DSOUND |
| 227 | dolog (NAME "\n"); |
| 228 | print_wave_format (&wfx); |
| 229 | #endif |
| 230 | |
| 231 | memset (&bc, 0, sizeof (bc)); |
| 232 | bc.dwSize = sizeof (bc); |
| 233 | |
| 234 | hr = glue (IFACE, _GetCaps) (ds->FIELD, &bc); |
| 235 | if (FAILED (hr)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 236 | dsound_logerr2 (hr, typ, "Could not get " NAME " format\n"); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 237 | goto fail0; |
| 238 | } |
| 239 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 240 | err = waveformat_to_audio_settings (&wfx, &obt_as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 241 | if (err) { |
| 242 | goto fail0; |
| 243 | } |
| 244 | |
| 245 | ds->first_time = 1; |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 246 | obt_as.endianness = 0; |
| 247 | audio_pcm_init_info (&hw->info, &obt_as); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 248 | |
| 249 | if (bc.dwBufferBytes & hw->info.align) { |
| 250 | dolog ( |
| 251 | "GetCaps returned misaligned buffer size %ld, alignment %d\n", |
| 252 | bc.dwBufferBytes, hw->info.align + 1 |
| 253 | ); |
| 254 | } |
| 255 | hw->samples = bc.dwBufferBytes >> hw->info.shift; |
Kővágó, Zoltán | 191e1f0 | 2015-06-03 23:03:52 +0200 | [diff] [blame] | 256 | ds->s = s; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 257 | |
| 258 | #ifdef DEBUG_DSOUND |
| 259 | dolog ("caps %ld, desc %ld\n", |
| 260 | bc.dwBufferBytes, bd.dwBufferBytes); |
| 261 | |
| 262 | dolog ("bufsize %d, freq %d, chan %d, fmt %d\n", |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 263 | hw->bufsize, settings.freq, settings.nchannels, settings.fmt); |
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 |