bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Audio subsystem 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 | |
| 25 | #ifdef DAC |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 26 | #define NAME "playback" |
| 27 | #define HWBUF hw->mix_buf |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 28 | #define TYPE out |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 29 | #define HW HWVoiceOut |
| 30 | #define SW SWVoiceOut |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 31 | #else |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 32 | #define NAME "capture" |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 33 | #define TYPE in |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 34 | #define HW HWVoiceIn |
| 35 | #define SW SWVoiceIn |
| 36 | #define HWBUF hw->conv_buf |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 39 | static void glue (audio_init_nb_voices_, TYPE) ( |
| 40 | AudioState *s, |
| 41 | struct audio_driver *drv |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 42 | ) |
| 43 | { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 44 | int max_voices = glue (drv->max_voices_, TYPE); |
| 45 | int voice_size = glue (drv->voice_size_, TYPE); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 46 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 47 | if (glue (s->nb_hw_voices_, TYPE) > max_voices) { |
| 48 | if (!max_voices) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 49 | #ifdef DAC |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 50 | dolog ("Driver `%s' does not support " NAME "\n", drv->name); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 51 | #endif |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 52 | } |
| 53 | else { |
| 54 | dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n", |
| 55 | drv->name, |
| 56 | glue (s->nb_hw_voices_, TYPE), |
| 57 | max_voices); |
| 58 | } |
| 59 | glue (s->nb_hw_voices_, TYPE) = max_voices; |
| 60 | } |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 61 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 62 | if (audio_bug (AUDIO_FUNC, !voice_size && max_voices)) { |
| 63 | dolog ("drv=`%s' voice_size=0 max_voices=%d\n", |
| 64 | drv->name, max_voices); |
| 65 | glue (s->nb_hw_voices_, TYPE) = 0; |
| 66 | } |
| 67 | |
| 68 | if (audio_bug (AUDIO_FUNC, voice_size && !max_voices)) { |
| 69 | dolog ("drv=`%s' voice_size=%d max_voices=0\n", |
| 70 | drv->name, voice_size); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw) |
| 75 | { |
| 76 | if (HWBUF) { |
| 77 | qemu_free (HWBUF); |
| 78 | } |
| 79 | |
| 80 | HWBUF = NULL; |
| 81 | } |
| 82 | |
| 83 | static int glue (audio_pcm_hw_alloc_resources_, TYPE) (HW *hw) |
| 84 | { |
| 85 | HWBUF = audio_calloc (AUDIO_FUNC, hw->samples, sizeof (st_sample_t)); |
| 86 | if (!HWBUF) { |
| 87 | dolog ("Could not allocate " NAME " buffer (%d samples)\n", |
| 88 | hw->samples); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 89 | return -1; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 95 | static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw) |
| 96 | { |
| 97 | if (sw->buf) { |
| 98 | qemu_free (sw->buf); |
| 99 | } |
| 100 | |
| 101 | if (sw->rate) { |
| 102 | st_rate_stop (sw->rate); |
| 103 | } |
| 104 | |
| 105 | sw->buf = NULL; |
| 106 | sw->rate = NULL; |
| 107 | } |
| 108 | |
| 109 | static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw) |
| 110 | { |
| 111 | int samples; |
| 112 | |
| 113 | #ifdef DAC |
| 114 | samples = sw->hw->samples; |
| 115 | #else |
| 116 | samples = ((int64_t) sw->hw->samples << 32) / sw->ratio; |
| 117 | #endif |
| 118 | |
| 119 | sw->buf = audio_calloc (AUDIO_FUNC, samples, sizeof (st_sample_t)); |
| 120 | if (!sw->buf) { |
| 121 | dolog ("Could not allocate buffer for `%s' (%d samples)\n", |
| 122 | SW_NAME (sw), samples); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | #ifdef DAC |
| 127 | sw->rate = st_rate_start (sw->info.freq, sw->hw->info.freq); |
| 128 | #else |
| 129 | sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq); |
| 130 | #endif |
| 131 | if (!sw->rate) { |
| 132 | qemu_free (sw->buf); |
| 133 | sw->buf = NULL; |
| 134 | return -1; |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int glue (audio_pcm_sw_init_, TYPE) ( |
| 140 | SW *sw, |
| 141 | HW *hw, |
| 142 | const char *name, |
| 143 | audsettings_t *as, |
| 144 | int endian |
| 145 | ) |
| 146 | { |
| 147 | int err; |
| 148 | |
| 149 | audio_pcm_init_info (&sw->info, as, audio_need_to_swap_endian (endian)); |
| 150 | sw->hw = hw; |
| 151 | sw->active = 0; |
| 152 | #ifdef DAC |
| 153 | sw->ratio = ((int64_t) sw->hw->info.freq << 32) / sw->info.freq; |
| 154 | sw->total_hw_samples_mixed = 0; |
| 155 | sw->empty = 1; |
| 156 | #else |
| 157 | sw->ratio = ((int64_t) sw->info.freq << 32) / sw->hw->info.freq; |
| 158 | #endif |
| 159 | |
| 160 | #ifdef DAC |
| 161 | sw->conv = mixeng_conv |
| 162 | #else |
| 163 | sw->clip = mixeng_clip |
| 164 | #endif |
| 165 | [sw->info.nchannels == 2] |
| 166 | [sw->info.sign] |
| 167 | [sw->info.swap_endian] |
| 168 | [sw->info.bits == 16]; |
| 169 | |
| 170 | sw->name = qemu_strdup (name); |
| 171 | err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw); |
| 172 | if (err) { |
| 173 | qemu_free (sw->name); |
| 174 | sw->name = NULL; |
| 175 | } |
| 176 | return err; |
| 177 | } |
| 178 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 179 | static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw) |
| 180 | { |
| 181 | glue (audio_pcm_sw_free_resources_, TYPE) (sw); |
| 182 | if (sw->name) { |
| 183 | qemu_free (sw->name); |
| 184 | sw->name = NULL; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw) |
| 189 | { |
| 190 | LIST_INSERT_HEAD (&hw->sw_head, sw, entries); |
| 191 | } |
| 192 | |
| 193 | static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw) |
| 194 | { |
| 195 | LIST_REMOVE (sw, entries); |
| 196 | } |
| 197 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 198 | static void glue (audio_pcm_hw_gc_, TYPE) (AudioState *s, HW **hwp) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 199 | { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 200 | HW *hw = *hwp; |
| 201 | |
| 202 | if (!hw->sw_head.lh_first) { |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame^] | 203 | #ifdef DAC |
| 204 | audio_detach_capture (hw); |
| 205 | #endif |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 206 | LIST_REMOVE (hw, entries); |
| 207 | glue (s->nb_hw_voices_, TYPE) += 1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 208 | glue (audio_pcm_hw_free_resources_ ,TYPE) (hw); |
| 209 | glue (hw->pcm_ops->fini_, TYPE) (hw); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 210 | qemu_free (hw); |
| 211 | *hwp = NULL; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 215 | static HW *glue (audio_pcm_hw_find_any_, TYPE) (AudioState *s, HW *hw) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 216 | { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 217 | return hw ? hw->entries.le_next : s->glue (hw_head_, TYPE).lh_first; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 218 | } |
| 219 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 220 | static HW *glue (audio_pcm_hw_find_any_enabled_, TYPE) (AudioState *s, HW *hw) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 221 | { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 222 | while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) { |
| 223 | if (hw->enabled) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 224 | return hw; |
| 225 | } |
| 226 | } |
| 227 | return NULL; |
| 228 | } |
| 229 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 230 | static HW *glue (audio_pcm_hw_find_specific_, TYPE) ( |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 231 | AudioState *s, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 232 | HW *hw, |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 233 | audsettings_t *as |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 234 | ) |
| 235 | { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 236 | while ((hw = glue (audio_pcm_hw_find_any_, TYPE) (s, hw))) { |
| 237 | if (audio_pcm_info_eq (&hw->info, as)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 238 | return hw; |
| 239 | } |
| 240 | } |
| 241 | return NULL; |
| 242 | } |
| 243 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 244 | static HW *glue (audio_pcm_hw_add_new_, TYPE) (AudioState *s, audsettings_t *as) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 245 | { |
| 246 | HW *hw; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 247 | struct audio_driver *drv = s->drv; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 248 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 249 | if (!glue (s->nb_hw_voices_, TYPE)) { |
| 250 | return NULL; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 251 | } |
| 252 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 253 | if (audio_bug (AUDIO_FUNC, !drv)) { |
| 254 | dolog ("No host audio driver\n"); |
| 255 | return NULL; |
| 256 | } |
| 257 | |
| 258 | if (audio_bug (AUDIO_FUNC, !drv->pcm_ops)) { |
| 259 | dolog ("Host audio driver without pcm_ops\n"); |
| 260 | return NULL; |
| 261 | } |
| 262 | |
| 263 | hw = audio_calloc (AUDIO_FUNC, 1, glue (drv->voice_size_, TYPE)); |
| 264 | if (!hw) { |
| 265 | dolog ("Can not allocate voice `%s' size %d\n", |
| 266 | drv->name, glue (drv->voice_size_, TYPE)); |
| 267 | return NULL; |
| 268 | } |
| 269 | |
| 270 | hw->pcm_ops = drv->pcm_ops; |
| 271 | LIST_INIT (&hw->sw_head); |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame^] | 272 | #ifdef DAC |
| 273 | LIST_INIT (&hw->sw_cap_head); |
| 274 | #endif |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 275 | if (glue (hw->pcm_ops->init_, TYPE) (hw, as)) { |
| 276 | goto err0; |
| 277 | } |
| 278 | |
| 279 | if (audio_bug (AUDIO_FUNC, hw->samples <= 0)) { |
| 280 | dolog ("hw->samples=%d\n", hw->samples); |
| 281 | goto err1; |
| 282 | } |
| 283 | |
| 284 | #ifdef DAC |
| 285 | hw->clip = mixeng_clip |
| 286 | #else |
| 287 | hw->conv = mixeng_conv |
| 288 | #endif |
| 289 | [hw->info.nchannels == 2] |
| 290 | [hw->info.sign] |
| 291 | [hw->info.swap_endian] |
| 292 | [hw->info.bits == 16]; |
| 293 | |
| 294 | if (glue (audio_pcm_hw_alloc_resources_, TYPE) (hw)) { |
| 295 | goto err1; |
| 296 | } |
| 297 | |
| 298 | LIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries); |
| 299 | glue (s->nb_hw_voices_, TYPE) -= 1; |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame^] | 300 | #ifdef DAC |
| 301 | audio_attach_capture (s, hw); |
| 302 | #endif |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 303 | return hw; |
| 304 | |
| 305 | err1: |
| 306 | glue (hw->pcm_ops->fini_, TYPE) (hw); |
| 307 | err0: |
| 308 | qemu_free (hw); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 309 | return NULL; |
| 310 | } |
| 311 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 312 | static HW *glue (audio_pcm_hw_add_, TYPE) (AudioState *s, audsettings_t *as) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 313 | { |
| 314 | HW *hw; |
| 315 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 316 | if (glue (conf.fixed_, TYPE).enabled && glue (conf.fixed_, TYPE).greedy) { |
| 317 | hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 318 | if (hw) { |
| 319 | return hw; |
| 320 | } |
| 321 | } |
| 322 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 323 | hw = glue (audio_pcm_hw_find_specific_, TYPE) (s, NULL, as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 324 | if (hw) { |
| 325 | return hw; |
| 326 | } |
| 327 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 328 | hw = glue (audio_pcm_hw_add_new_, TYPE) (s, as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 329 | if (hw) { |
| 330 | return hw; |
| 331 | } |
| 332 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 333 | return glue (audio_pcm_hw_find_any_, TYPE) (s, NULL); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static SW *glue (audio_pcm_create_voice_pair_, TYPE) ( |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 337 | AudioState *s, |
| 338 | const char *sw_name, |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 339 | audsettings_t *as, |
| 340 | int sw_endian |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 341 | ) |
| 342 | { |
| 343 | SW *sw; |
| 344 | HW *hw; |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 345 | audsettings_t hw_as; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 346 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 347 | if (glue (conf.fixed_, TYPE).enabled) { |
| 348 | hw_as = glue (conf.fixed_, TYPE).settings; |
| 349 | } |
| 350 | else { |
| 351 | hw_as = *as; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 352 | } |
| 353 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 354 | sw = audio_calloc (AUDIO_FUNC, 1, sizeof (*sw)); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 355 | if (!sw) { |
bellard | e7cad33 | 2005-11-11 00:03:36 +0000 | [diff] [blame] | 356 | dolog ("Could not allocate soft voice `%s' (%zu bytes)\n", |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 357 | sw_name ? sw_name : "unknown", sizeof (*sw)); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 358 | goto err1; |
| 359 | } |
| 360 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 361 | hw = glue (audio_pcm_hw_add_, TYPE) (s, &hw_as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 362 | if (!hw) { |
| 363 | goto err2; |
| 364 | } |
| 365 | |
| 366 | glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw); |
| 367 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 368 | if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as, sw_endian)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 369 | goto err3; |
| 370 | } |
| 371 | |
| 372 | return sw; |
| 373 | |
| 374 | err3: |
| 375 | glue (audio_pcm_hw_del_sw_, TYPE) (sw); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 376 | glue (audio_pcm_hw_gc_, TYPE) (s, &hw); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 377 | err2: |
| 378 | qemu_free (sw); |
| 379 | err1: |
| 380 | return NULL; |
| 381 | } |
| 382 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 383 | static void glue (audio_close_, TYPE) (AudioState *s, SW *sw) |
| 384 | { |
| 385 | glue (audio_pcm_sw_fini_, TYPE) (sw); |
| 386 | glue (audio_pcm_hw_del_sw_, TYPE) (sw); |
| 387 | glue (audio_pcm_hw_gc_, TYPE) (s, &sw->hw); |
| 388 | qemu_free (sw); |
| 389 | } |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 390 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 391 | void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 392 | { |
| 393 | if (sw) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 394 | if (audio_bug (AUDIO_FUNC, !card || !card->audio)) { |
| 395 | dolog ("card=%p card->audio=%p\n", |
| 396 | card, card ? card->audio : NULL); |
| 397 | return; |
| 398 | } |
| 399 | |
| 400 | glue (audio_close_, TYPE) (card->audio, sw); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
| 404 | SW *glue (AUD_open_, TYPE) ( |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 405 | QEMUSoundCard *card, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 406 | SW *sw, |
| 407 | const char *name, |
| 408 | void *callback_opaque , |
| 409 | audio_callback_fn_t callback_fn, |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 410 | audsettings_t *as, |
| 411 | int sw_endian |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 412 | ) |
| 413 | { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 414 | AudioState *s; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 415 | #ifdef DAC |
| 416 | int live = 0; |
| 417 | SW *old_sw = NULL; |
| 418 | #endif |
| 419 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 420 | ldebug ("open %s, freq %d, nchannels %d, fmt %d\n", |
| 421 | name, as->freq, as->nchannels, as->fmt); |
| 422 | |
| 423 | if (audio_bug (AUDIO_FUNC, |
| 424 | !card || !card->audio || !name || !callback_fn || !as)) { |
| 425 | dolog ("card=%p card->audio=%p name=%p callback_fn=%p as=%p\n", |
| 426 | card, card ? card->audio : NULL, name, callback_fn, as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 427 | goto fail; |
| 428 | } |
| 429 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 430 | s = card->audio; |
| 431 | |
| 432 | if (audio_bug (AUDIO_FUNC, audio_validate_settigs (as))) { |
| 433 | audio_print_settings (as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 434 | goto fail; |
| 435 | } |
| 436 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 437 | if (audio_bug (AUDIO_FUNC, !s->drv)) { |
| 438 | dolog ("Can not open `%s' (no host audio driver)\n", name); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 439 | goto fail; |
| 440 | } |
| 441 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 442 | if (sw && audio_pcm_info_eq (&sw->info, as)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 443 | return sw; |
| 444 | } |
| 445 | |
| 446 | #ifdef DAC |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 447 | if (conf.plive && sw && (!sw->active && !sw->empty)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 448 | live = sw->total_hw_samples_mixed; |
| 449 | |
| 450 | #ifdef DEBUG_PLIVE |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 451 | dolog ("Replacing voice %s with %d live samples\n", SW_NAME (sw), live); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 452 | dolog ("Old %s freq %d, bits %d, channels %d\n", |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 453 | SW_NAME (sw), sw->info.freq, sw->info.bits, sw->info.nchannels); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 454 | dolog ("New %s freq %d, bits %d, channels %d\n", |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 455 | name, |
| 456 | freq, |
| 457 | (fmt == AUD_FMT_S16 || fmt == AUD_FMT_U16) ? 16 : 8, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 458 | nchannels); |
| 459 | #endif |
| 460 | |
| 461 | if (live) { |
| 462 | old_sw = sw; |
| 463 | old_sw->callback.fn = NULL; |
| 464 | sw = NULL; |
| 465 | } |
| 466 | } |
| 467 | #endif |
| 468 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 469 | if (!glue (conf.fixed_, TYPE).enabled && sw) { |
| 470 | glue (AUD_close_, TYPE) (card, sw); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 471 | sw = NULL; |
| 472 | } |
| 473 | |
| 474 | if (sw) { |
| 475 | HW *hw = sw->hw; |
| 476 | |
| 477 | if (!hw) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 478 | dolog ("Internal logic error voice `%s' has no hardware store\n", |
| 479 | SW_NAME (sw)); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 480 | goto fail; |
| 481 | } |
| 482 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 483 | glue (audio_pcm_sw_fini_, TYPE) (sw); |
| 484 | if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as, sw_endian)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 485 | goto fail; |
| 486 | } |
| 487 | } |
| 488 | else { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 489 | sw = glue (audio_pcm_create_voice_pair_, TYPE) (s, name, as, sw_endian); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 490 | if (!sw) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 491 | dolog ("Failed to create voice `%s'\n", name); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 492 | return NULL; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
| 496 | if (sw) { |
| 497 | sw->vol = nominal_volume; |
| 498 | sw->callback.fn = callback_fn; |
| 499 | sw->callback.opaque = callback_opaque; |
| 500 | |
| 501 | #ifdef DAC |
| 502 | if (live) { |
| 503 | int mixed = |
| 504 | (live << old_sw->info.shift) |
| 505 | * old_sw->info.bytes_per_second |
| 506 | / sw->info.bytes_per_second; |
| 507 | |
| 508 | #ifdef DEBUG_PLIVE |
| 509 | dolog ("Silence will be mixed %d\n", mixed); |
| 510 | #endif |
| 511 | sw->total_hw_samples_mixed += mixed; |
| 512 | } |
| 513 | #endif |
| 514 | |
| 515 | #ifdef DEBUG_AUDIO |
| 516 | dolog ("%s\n", name); |
| 517 | audio_pcm_print_info ("hw", &sw->hw->info); |
| 518 | audio_pcm_print_info ("sw", &sw->info); |
| 519 | #endif |
| 520 | } |
| 521 | |
| 522 | return sw; |
| 523 | |
| 524 | fail: |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 525 | glue (AUD_close_, TYPE) (card, sw); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | int glue (AUD_is_active_, TYPE) (SW *sw) |
| 530 | { |
| 531 | return sw ? sw->active : 0; |
| 532 | } |
| 533 | |
| 534 | void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts) |
| 535 | { |
| 536 | if (!sw) { |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | ts->old_ts = sw->hw->ts_helper; |
| 541 | } |
| 542 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 543 | uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 544 | { |
| 545 | uint64_t delta, cur_ts, old_ts; |
| 546 | |
| 547 | if (!sw) { |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | cur_ts = sw->hw->ts_helper; |
| 552 | old_ts = ts->old_ts; |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame^] | 553 | /* dolog ("cur %lld old %lld\n", cur_ts, old_ts); */ |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 554 | |
| 555 | if (cur_ts >= old_ts) { |
| 556 | delta = cur_ts - old_ts; |
| 557 | } |
| 558 | else { |
| 559 | delta = UINT64_MAX - old_ts + cur_ts; |
| 560 | } |
| 561 | |
| 562 | if (!delta) { |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | return (delta * sw->hw->info.freq) / 1000000; |
| 567 | } |
| 568 | |
| 569 | #undef TYPE |
| 570 | #undef HW |
| 571 | #undef SW |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 572 | #undef HWBUF |
| 573 | #undef NAME |