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 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 39 | static void glue(audio_init_nb_voices_, TYPE)(AudioState *s, |
Paolo Bonzini | 5c63d14 | 2023-09-22 18:36:28 +0200 | [diff] [blame] | 40 | struct audio_driver *drv, int min_voices) |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 41 | { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 42 | int max_voices = glue (drv->max_voices_, TYPE); |
Volker Rümelin | 3724ab3 | 2023-01-21 10:47:31 +0100 | [diff] [blame] | 43 | size_t voice_size = glue(drv->voice_size_, TYPE); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 44 | |
Paolo Bonzini | 5c63d14 | 2023-09-22 18:36:28 +0200 | [diff] [blame] | 45 | glue (s->nb_hw_voices_, TYPE) = glue(audio_get_pdo_, TYPE)(s->dev)->voices; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 46 | if (glue (s->nb_hw_voices_, TYPE) > max_voices) { |
| 47 | if (!max_voices) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 48 | #ifdef DAC |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 49 | dolog ("Driver `%s' does not support " NAME "\n", drv->name); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 50 | #endif |
Zhang Han | 6c6886b | 2021-01-15 09:24:25 +0800 | [diff] [blame] | 51 | } else { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 52 | dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n", |
| 53 | drv->name, |
| 54 | glue (s->nb_hw_voices_, TYPE), |
| 55 | max_voices); |
| 56 | } |
| 57 | glue (s->nb_hw_voices_, TYPE) = max_voices; |
| 58 | } |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 59 | |
Paolo Bonzini | 5c63d14 | 2023-09-22 18:36:28 +0200 | [diff] [blame] | 60 | if (glue (s->nb_hw_voices_, TYPE) < min_voices) { |
| 61 | dolog ("Bogus number of " NAME " voices %d, setting to %d\n", |
| 62 | glue (s->nb_hw_voices_, TYPE), |
| 63 | min_voices); |
| 64 | } |
| 65 | |
Alistair Francis | 470bcab | 2018-02-03 09:43:02 +0100 | [diff] [blame] | 66 | if (audio_bug(__func__, !voice_size && max_voices)) { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 67 | dolog ("drv=`%s' voice_size=0 max_voices=%d\n", |
| 68 | drv->name, max_voices); |
Volker Rümelin | 12f4abf | 2022-09-17 15:16:25 +0200 | [diff] [blame] | 69 | glue (s->nb_hw_voices_, TYPE) = 0; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Alistair Francis | 470bcab | 2018-02-03 09:43:02 +0100 | [diff] [blame] | 72 | if (audio_bug(__func__, voice_size && !max_voices)) { |
Volker Rümelin | 3724ab3 | 2023-01-21 10:47:31 +0100 | [diff] [blame] | 73 | dolog("drv=`%s' voice_size=%zu max_voices=0\n", |
| 74 | drv->name, voice_size); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw) |
| 79 | { |
Kővágó, Zoltán | ff095e5 | 2019-09-19 23:24:09 +0200 | [diff] [blame] | 80 | g_free(hw->buf_emul); |
Volker Rümelin | 8dbd3d1 | 2023-02-24 20:05:41 +0100 | [diff] [blame] | 81 | g_free(HWBUF.buffer); |
| 82 | HWBUF.buffer = NULL; |
| 83 | HWBUF.size = 0; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Kővágó, Zoltán | dc88e38 | 2019-09-19 23:24:20 +0200 | [diff] [blame] | 86 | static void glue(audio_pcm_hw_alloc_resources_, TYPE)(HW *hw) |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 87 | { |
Kővágó, Zoltán | 1930616 | 2019-10-13 21:57:59 +0200 | [diff] [blame] | 88 | if (glue(audio_get_pdo_, TYPE)(hw->s->dev)->mixing_engine) { |
| 89 | size_t samples = hw->samples; |
| 90 | if (audio_bug(__func__, samples == 0)) { |
| 91 | dolog("Attempted to allocate empty buffer\n"); |
| 92 | } |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 93 | |
Volker Rümelin | 8dbd3d1 | 2023-02-24 20:05:41 +0100 | [diff] [blame] | 94 | HWBUF.buffer = g_new0(st_sample, samples); |
| 95 | HWBUF.size = samples; |
| 96 | HWBUF.pos = 0; |
Kővágó, Zoltán | 1930616 | 2019-10-13 21:57:59 +0200 | [diff] [blame] | 97 | } else { |
Volker Rümelin | 8dbd3d1 | 2023-02-24 20:05:41 +0100 | [diff] [blame] | 98 | HWBUF.buffer = NULL; |
| 99 | HWBUF.size = 0; |
Kővágó, Zoltán | 1930616 | 2019-10-13 21:57:59 +0200 | [diff] [blame] | 100 | } |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 101 | } |
| 102 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 103 | static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw) |
| 104 | { |
Volker Rümelin | 2c3f9a0 | 2023-02-24 20:05:42 +0100 | [diff] [blame] | 105 | g_free(sw->resample_buf.buffer); |
| 106 | sw->resample_buf.buffer = NULL; |
| 107 | sw->resample_buf.size = 0; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 108 | |
| 109 | if (sw->rate) { |
| 110 | st_rate_stop (sw->rate); |
| 111 | } |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 112 | sw->rate = NULL; |
| 113 | } |
| 114 | |
| 115 | static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw) |
| 116 | { |
Volker Rümelin | 148392a | 2023-02-24 20:05:54 +0100 | [diff] [blame] | 117 | HW *hw = sw->hw; |
Volker Rümelin | 2f886a3 | 2023-02-24 20:05:55 +0100 | [diff] [blame] | 118 | uint64_t samples; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 119 | |
Kővágó, Zoltán | 1930616 | 2019-10-13 21:57:59 +0200 | [diff] [blame] | 120 | if (!glue(audio_get_pdo_, TYPE)(sw->s->dev)->mixing_engine) { |
| 121 | return 0; |
| 122 | } |
| 123 | |
Volker Rümelin | 2f886a3 | 2023-02-24 20:05:55 +0100 | [diff] [blame] | 124 | samples = muldiv64(HWBUF.size, sw->info.freq, hw->info.freq); |
Volker Rümelin | b9ae74e | 2023-01-21 10:47:25 +0100 | [diff] [blame] | 125 | if (samples == 0) { |
Volker Rümelin | 2f886a3 | 2023-02-24 20:05:55 +0100 | [diff] [blame] | 126 | uint64_t f_fe_min; |
| 127 | uint64_t f_be = (uint32_t)hw->info.freq; |
Volker Rümelin | b9ae74e | 2023-01-21 10:47:25 +0100 | [diff] [blame] | 128 | |
| 129 | /* f_fe_min = ceil(1 [frames] * f_be [Hz] / size_be [frames]) */ |
Volker Rümelin | 2f886a3 | 2023-02-24 20:05:55 +0100 | [diff] [blame] | 130 | f_fe_min = (f_be + HWBUF.size - 1) / HWBUF.size; |
Volker Rümelin | b9ae74e | 2023-01-21 10:47:25 +0100 | [diff] [blame] | 131 | qemu_log_mask(LOG_UNIMP, |
| 132 | AUDIO_CAP ": The guest selected a " NAME " sample rate" |
Volker Rümelin | 2f886a3 | 2023-02-24 20:05:55 +0100 | [diff] [blame] | 133 | " of %d Hz for %s. Only sample rates >= %" PRIu64 " Hz" |
| 134 | " are supported.\n", |
Volker Rümelin | b9ae74e | 2023-01-21 10:47:25 +0100 | [diff] [blame] | 135 | sw->info.freq, sw->name, f_fe_min); |
| 136 | return -1; |
| 137 | } |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 138 | |
Volker Rümelin | e1e6a6f | 2023-02-24 20:05:53 +0100 | [diff] [blame] | 139 | /* |
| 140 | * Allocate one additional audio frame that is needed for upsampling |
| 141 | * if the resample buffer size is small. For large buffer sizes take |
Volker Rümelin | 2f886a3 | 2023-02-24 20:05:55 +0100 | [diff] [blame] | 142 | * care of overflows and truncation. |
Volker Rümelin | e1e6a6f | 2023-02-24 20:05:53 +0100 | [diff] [blame] | 143 | */ |
Volker Rümelin | 2f886a3 | 2023-02-24 20:05:55 +0100 | [diff] [blame] | 144 | samples = samples < SIZE_MAX ? samples + 1 : SIZE_MAX; |
Volker Rümelin | 2c3f9a0 | 2023-02-24 20:05:42 +0100 | [diff] [blame] | 145 | sw->resample_buf.buffer = g_new0(st_sample, samples); |
| 146 | sw->resample_buf.size = samples; |
| 147 | sw->resample_buf.pos = 0; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 148 | |
| 149 | #ifdef DAC |
Volker Rümelin | 148392a | 2023-02-24 20:05:54 +0100 | [diff] [blame] | 150 | sw->rate = st_rate_start(sw->info.freq, hw->info.freq); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 151 | #else |
Volker Rümelin | 148392a | 2023-02-24 20:05:54 +0100 | [diff] [blame] | 152 | sw->rate = st_rate_start(hw->info.freq, sw->info.freq); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 153 | #endif |
Volker Rümelin | 25bf0c2 | 2023-01-21 10:47:29 +0100 | [diff] [blame] | 154 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static int glue (audio_pcm_sw_init_, TYPE) ( |
| 159 | SW *sw, |
| 160 | HW *hw, |
| 161 | const char *name, |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 162 | struct audsettings *as |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 163 | ) |
| 164 | { |
| 165 | int err; |
| 166 | |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 167 | audio_pcm_init_info (&sw->info, as); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 168 | sw->hw = hw; |
| 169 | sw->active = 0; |
| 170 | #ifdef DAC |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 171 | sw->total_hw_samples_mixed = 0; |
| 172 | sw->empty = 1; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 173 | #endif |
| 174 | |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 175 | if (sw->info.is_float) { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 176 | #ifdef DAC |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 177 | sw->conv = mixeng_conv_float[sw->info.nchannels == 2]; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 178 | #else |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 179 | sw->clip = mixeng_clip_float[sw->info.nchannels == 2]; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 180 | #endif |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 181 | } else { |
| 182 | #ifdef DAC |
| 183 | sw->conv = mixeng_conv |
| 184 | #else |
| 185 | sw->clip = mixeng_clip |
| 186 | #endif |
| 187 | [sw->info.nchannels == 2] |
| 188 | [sw->info.is_signed] |
| 189 | [sw->info.swap_endianness] |
| 190 | [audio_bits_to_index(sw->info.bits)]; |
| 191 | } |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 192 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 193 | sw->name = g_strdup (name); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 194 | err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw); |
| 195 | if (err) { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 196 | g_free (sw->name); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 197 | sw->name = NULL; |
| 198 | } |
| 199 | return err; |
| 200 | } |
| 201 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 202 | static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw) |
| 203 | { |
| 204 | glue (audio_pcm_sw_free_resources_, TYPE) (sw); |
Markus Armbruster | fb7da62 | 2014-06-06 18:35:13 +0200 | [diff] [blame] | 205 | g_free (sw->name); |
| 206 | sw->name = NULL; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw) |
| 210 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 211 | QLIST_INSERT_HEAD (&hw->sw_head, sw, entries); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw) |
| 215 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 216 | QLIST_REMOVE (sw, entries); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 217 | } |
| 218 | |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 219 | static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 220 | { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 221 | HW *hw = *hwp; |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 222 | AudioState *s = hw->s; |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 223 | |
| 224 | if (!hw->sw_head.lh_first) { |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 225 | #ifdef DAC |
Zhang Han | 8abf3fe | 2021-01-15 09:24:31 +0800 | [diff] [blame] | 226 | audio_detach_capture(hw); |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 227 | #endif |
Zhang Han | 8abf3fe | 2021-01-15 09:24:31 +0800 | [diff] [blame] | 228 | QLIST_REMOVE(hw, entries); |
| 229 | glue(hw->pcm_ops->fini_, TYPE) (hw); |
| 230 | glue(s->nb_hw_voices_, TYPE) += 1; |
| 231 | glue(audio_pcm_hw_free_resources_ , TYPE) (hw); |
| 232 | g_free(hw); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 233 | *hwp = NULL; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 237 | static HW *glue(audio_pcm_hw_find_any_, TYPE)(AudioState *s, HW *hw) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 238 | { |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 239 | return hw ? hw->entries.le_next : glue (s->hw_head_, TYPE).lh_first; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 242 | 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] | 243 | { |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 244 | while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 245 | if (hw->enabled) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 246 | return hw; |
| 247 | } |
| 248 | } |
| 249 | return NULL; |
| 250 | } |
| 251 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 252 | static HW *glue(audio_pcm_hw_find_specific_, TYPE)(AudioState *s, HW *hw, |
| 253 | struct audsettings *as) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 254 | { |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 255 | while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 256 | if (audio_pcm_info_eq (&hw->info, as)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 257 | return hw; |
| 258 | } |
| 259 | } |
| 260 | return NULL; |
| 261 | } |
| 262 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 263 | static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioState *s, |
| 264 | struct audsettings *as) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 265 | { |
| 266 | HW *hw; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 267 | struct audio_driver *drv = s->drv; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 268 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 269 | if (!glue (s->nb_hw_voices_, TYPE)) { |
| 270 | return NULL; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Alistair Francis | 470bcab | 2018-02-03 09:43:02 +0100 | [diff] [blame] | 273 | if (audio_bug(__func__, !drv)) { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 274 | dolog ("No host audio driver\n"); |
Volker Rümelin | 12f4abf | 2022-09-17 15:16:25 +0200 | [diff] [blame] | 275 | return NULL; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Alistair Francis | 470bcab | 2018-02-03 09:43:02 +0100 | [diff] [blame] | 278 | if (audio_bug(__func__, !drv->pcm_ops)) { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 279 | dolog ("Host audio driver without pcm_ops\n"); |
Volker Rümelin | 12f4abf | 2022-09-17 15:16:25 +0200 | [diff] [blame] | 280 | return NULL; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Volker Rümelin | 3724ab3 | 2023-01-21 10:47:31 +0100 | [diff] [blame] | 283 | /* |
| 284 | * Since glue(s->nb_hw_voices_, TYPE) is != 0, glue(drv->voice_size_, TYPE) |
| 285 | * is guaranteed to be != 0. See the audio_init_nb_voices_* functions. |
| 286 | */ |
| 287 | hw = g_malloc0(glue(drv->voice_size_, TYPE)); |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 288 | hw->s = s; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 289 | hw->pcm_ops = drv->pcm_ops; |
Marc-André Lureau | c01b245 | 2012-04-17 14:32:36 +0200 | [diff] [blame] | 290 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 291 | QLIST_INIT (&hw->sw_head); |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 292 | #ifdef DAC |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 293 | QLIST_INIT (&hw->cap_head); |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 294 | #endif |
Kővágó, Zoltán | 5706db1 | 2015-06-03 23:03:47 +0200 | [diff] [blame] | 295 | if (glue (hw->pcm_ops->init_, TYPE) (hw, as, s->drv_opaque)) { |
Volker Rümelin | 12f4abf | 2022-09-17 15:16:25 +0200 | [diff] [blame] | 296 | goto err0; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Alistair Francis | 470bcab | 2018-02-03 09:43:02 +0100 | [diff] [blame] | 299 | if (audio_bug(__func__, hw->samples <= 0)) { |
Kővágó, Zoltán | 7520462 | 2019-08-19 01:06:58 +0200 | [diff] [blame] | 300 | dolog("hw->samples=%zd\n", hw->samples); |
Volker Rümelin | 12f4abf | 2022-09-17 15:16:25 +0200 | [diff] [blame] | 301 | goto err1; |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 304 | if (hw->info.is_float) { |
Volker Rümelin | 180b044 | 2020-02-02 15:06:41 +0100 | [diff] [blame] | 305 | #ifdef DAC |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 306 | hw->clip = mixeng_clip_float[hw->info.nchannels == 2]; |
Volker Rümelin | 180b044 | 2020-02-02 15:06:41 +0100 | [diff] [blame] | 307 | #else |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 308 | hw->conv = mixeng_conv_float[hw->info.nchannels == 2]; |
Volker Rümelin | 180b044 | 2020-02-02 15:06:41 +0100 | [diff] [blame] | 309 | #endif |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 310 | } else { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 311 | #ifdef DAC |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 312 | hw->clip = mixeng_clip |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 313 | #else |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 314 | hw->conv = mixeng_conv |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 315 | #endif |
Kővágó, Zoltán | ed2a4a7 | 2020-02-02 20:38:07 +0100 | [diff] [blame] | 316 | [hw->info.nchannels == 2] |
| 317 | [hw->info.is_signed] |
| 318 | [hw->info.swap_endianness] |
| 319 | [audio_bits_to_index(hw->info.bits)]; |
| 320 | } |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 321 | |
Kővágó, Zoltán | dc88e38 | 2019-09-19 23:24:20 +0200 | [diff] [blame] | 322 | glue(audio_pcm_hw_alloc_resources_, TYPE)(hw); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 323 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 324 | QLIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries); |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 325 | glue (s->nb_hw_voices_, TYPE) -= 1; |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 326 | #ifdef DAC |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 327 | audio_attach_capture (hw); |
bellard | 8ead62c | 2006-07-04 16:51:32 +0000 | [diff] [blame] | 328 | #endif |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 329 | return hw; |
Volker Rümelin | 12f4abf | 2022-09-17 15:16:25 +0200 | [diff] [blame] | 330 | |
| 331 | err1: |
| 332 | glue (hw->pcm_ops->fini_, TYPE) (hw); |
| 333 | err0: |
| 334 | g_free (hw); |
| 335 | return NULL; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 338 | AudiodevPerDirectionOptions *glue(audio_get_pdo_, TYPE)(Audiodev *dev) |
| 339 | { |
| 340 | switch (dev->driver) { |
| 341 | case AUDIODEV_DRIVER_NONE: |
| 342 | return dev->u.none.TYPE; |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 343 | #ifdef CONFIG_AUDIO_ALSA |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 344 | case AUDIODEV_DRIVER_ALSA: |
| 345 | return qapi_AudiodevAlsaPerDirectionOptions_base(dev->u.alsa.TYPE); |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 346 | #endif |
| 347 | #ifdef CONFIG_AUDIO_COREAUDIO |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 348 | case AUDIODEV_DRIVER_COREAUDIO: |
| 349 | return qapi_AudiodevCoreaudioPerDirectionOptions_base( |
| 350 | dev->u.coreaudio.TYPE); |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 351 | #endif |
| 352 | #ifdef CONFIG_DBUS_DISPLAY |
Marc-André Lureau | 739362d | 2021-03-09 17:15:28 +0400 | [diff] [blame] | 353 | case AUDIODEV_DRIVER_DBUS: |
| 354 | return dev->u.dbus.TYPE; |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 355 | #endif |
| 356 | #ifdef CONFIG_AUDIO_DSOUND |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 357 | case AUDIODEV_DRIVER_DSOUND: |
| 358 | return dev->u.dsound.TYPE; |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 359 | #endif |
| 360 | #ifdef CONFIG_AUDIO_JACK |
Geoffrey McRae | 2e44570 | 2020-04-29 15:53:58 +1000 | [diff] [blame] | 361 | case AUDIODEV_DRIVER_JACK: |
| 362 | return qapi_AudiodevJackPerDirectionOptions_base(dev->u.jack.TYPE); |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 363 | #endif |
| 364 | #ifdef CONFIG_AUDIO_OSS |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 365 | case AUDIODEV_DRIVER_OSS: |
| 366 | return qapi_AudiodevOssPerDirectionOptions_base(dev->u.oss.TYPE); |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 367 | #endif |
| 368 | #ifdef CONFIG_AUDIO_PA |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 369 | case AUDIODEV_DRIVER_PA: |
| 370 | return qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.TYPE); |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 371 | #endif |
Dorinda Bassey | c2d3d1c | 2023-04-17 12:56:54 +0200 | [diff] [blame] | 372 | #ifdef CONFIG_AUDIO_PIPEWIRE |
| 373 | case AUDIODEV_DRIVER_PIPEWIRE: |
| 374 | return qapi_AudiodevPipewirePerDirectionOptions_base(dev->u.pipewire.TYPE); |
| 375 | #endif |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 376 | #ifdef CONFIG_AUDIO_SDL |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 377 | case AUDIODEV_DRIVER_SDL: |
Volker Rümelin | 5a0926c | 2021-01-10 11:02:19 +0100 | [diff] [blame] | 378 | return qapi_AudiodevSdlPerDirectionOptions_base(dev->u.sdl.TYPE); |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 379 | #endif |
| 380 | #ifdef CONFIG_AUDIO_SNDIO |
Alexandre Ratchov | 663df1c | 2022-09-07 15:23:42 +0200 | [diff] [blame] | 381 | case AUDIODEV_DRIVER_SNDIO: |
| 382 | return dev->u.sndio.TYPE; |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 383 | #endif |
| 384 | #ifdef CONFIG_SPICE |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 385 | case AUDIODEV_DRIVER_SPICE: |
| 386 | return dev->u.spice.TYPE; |
Daniel P. Berrangé | 7a92a85 | 2023-01-23 09:39:57 +0100 | [diff] [blame] | 387 | #endif |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 388 | case AUDIODEV_DRIVER_WAV: |
| 389 | return dev->u.wav.TYPE; |
| 390 | |
| 391 | case AUDIODEV_DRIVER__MAX: |
| 392 | break; |
| 393 | } |
| 394 | abort(); |
| 395 | } |
| 396 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 397 | static HW *glue(audio_pcm_hw_add_, TYPE)(AudioState *s, struct audsettings *as) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 398 | { |
| 399 | HW *hw; |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 400 | AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 401 | |
Kővágó, Zoltán | 1930616 | 2019-10-13 21:57:59 +0200 | [diff] [blame] | 402 | if (!pdo->mixing_engine || pdo->fixed_settings) { |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 403 | hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as); |
Kővágó, Zoltán | 1930616 | 2019-10-13 21:57:59 +0200 | [diff] [blame] | 404 | if (!pdo->mixing_engine || hw) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 405 | return hw; |
| 406 | } |
| 407 | } |
| 408 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 409 | hw = glue(audio_pcm_hw_find_specific_, TYPE)(s, NULL, as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 410 | if (hw) { |
| 411 | return hw; |
| 412 | } |
| 413 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 414 | hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 415 | if (hw) { |
| 416 | return hw; |
| 417 | } |
| 418 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 419 | return glue(audio_pcm_hw_find_any_, TYPE)(s, NULL); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 422 | static SW *glue(audio_pcm_create_voice_pair_, TYPE)( |
| 423 | AudioState *s, |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 424 | const char *sw_name, |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 425 | struct audsettings *as |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 426 | ) |
| 427 | { |
| 428 | SW *sw; |
| 429 | HW *hw; |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 430 | struct audsettings hw_as; |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 431 | AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 432 | |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 433 | if (pdo->fixed_settings) { |
| 434 | hw_as = audiodev_to_audsettings(pdo); |
Zhang Han | 6c6886b | 2021-01-15 09:24:25 +0800 | [diff] [blame] | 435 | } else { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 436 | hw_as = *as; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Volker Rümelin | c6b69a8 | 2023-01-21 10:47:32 +0100 | [diff] [blame] | 439 | sw = g_new0(SW, 1); |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 440 | sw->s = s; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 441 | |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 442 | hw = glue(audio_pcm_hw_add_, TYPE)(s, &hw_as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 443 | if (!hw) { |
Volker Rümelin | 90394fe | 2023-01-21 10:47:26 +0100 | [diff] [blame] | 444 | dolog("Could not create a backend for voice `%s'\n", sw_name); |
Volker Rümelin | c6b69a8 | 2023-01-21 10:47:32 +0100 | [diff] [blame] | 445 | goto err1; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw); |
| 449 | |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 450 | if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) { |
Volker Rümelin | c6b69a8 | 2023-01-21 10:47:32 +0100 | [diff] [blame] | 451 | goto err2; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | return sw; |
| 455 | |
Volker Rümelin | c6b69a8 | 2023-01-21 10:47:32 +0100 | [diff] [blame] | 456 | err2: |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 457 | glue (audio_pcm_hw_del_sw_, TYPE) (sw); |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 458 | glue (audio_pcm_hw_gc_, TYPE) (&hw); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 459 | err1: |
Volker Rümelin | c6b69a8 | 2023-01-21 10:47:32 +0100 | [diff] [blame] | 460 | g_free(sw); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 461 | return NULL; |
| 462 | } |
| 463 | |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 464 | static void glue (audio_close_, TYPE) (SW *sw) |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 465 | { |
| 466 | glue (audio_pcm_sw_fini_, TYPE) (sw); |
| 467 | glue (audio_pcm_hw_del_sw_, TYPE) (sw); |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 468 | glue (audio_pcm_hw_gc_, TYPE) (&sw->hw); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 469 | g_free (sw); |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 470 | } |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 471 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 472 | void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 473 | { |
| 474 | if (sw) { |
Alistair Francis | 470bcab | 2018-02-03 09:43:02 +0100 | [diff] [blame] | 475 | if (audio_bug(__func__, !card)) { |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 476 | dolog ("card=%p\n", card); |
Volker Rümelin | 12f4abf | 2022-09-17 15:16:25 +0200 | [diff] [blame] | 477 | return; |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 478 | } |
| 479 | |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 480 | glue (audio_close_, TYPE) (sw); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | SW *glue (AUD_open_, TYPE) ( |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 485 | QEMUSoundCard *card, |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 486 | SW *sw, |
| 487 | const char *name, |
| 488 | void *callback_opaque , |
malc | cb4f03e | 2009-10-15 02:40:17 +0400 | [diff] [blame] | 489 | audio_callback_fn callback_fn, |
malc | 1ea879e | 2008-12-03 22:48:44 +0000 | [diff] [blame] | 490 | struct audsettings *as |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 491 | ) |
| 492 | { |
Kővágó, Zoltán | d1670b2 | 2019-09-11 01:26:18 +0200 | [diff] [blame] | 493 | AudioState *s; |
| 494 | AudiodevPerDirectionOptions *pdo; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 495 | |
Alistair Francis | 470bcab | 2018-02-03 09:43:02 +0100 | [diff] [blame] | 496 | if (audio_bug(__func__, !card || !name || !callback_fn || !as)) { |
malc | 1a7dafc | 2009-05-14 03:11:35 +0400 | [diff] [blame] | 497 | dolog ("card=%p name=%p callback_fn=%p as=%p\n", |
| 498 | card, name, callback_fn, as); |
Volker Rümelin | 12f4abf | 2022-09-17 15:16:25 +0200 | [diff] [blame] | 499 | goto fail; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Kővágó, Zoltán | d1670b2 | 2019-09-11 01:26:18 +0200 | [diff] [blame] | 502 | s = card->state; |
| 503 | pdo = glue(audio_get_pdo_, TYPE)(s->dev); |
| 504 | |
Stefan Weil | 93b6599 | 2012-09-03 09:25:16 +0000 | [diff] [blame] | 505 | ldebug ("open %s, freq %d, nchannels %d, fmt %d\n", |
| 506 | name, as->freq, as->nchannels, as->fmt); |
| 507 | |
Alistair Francis | 470bcab | 2018-02-03 09:43:02 +0100 | [diff] [blame] | 508 | if (audio_bug(__func__, audio_validate_settings(as))) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 509 | audio_print_settings (as); |
Volker Rümelin | 12f4abf | 2022-09-17 15:16:25 +0200 | [diff] [blame] | 510 | goto fail; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Alistair Francis | 470bcab | 2018-02-03 09:43:02 +0100 | [diff] [blame] | 513 | if (audio_bug(__func__, !s->drv)) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 514 | dolog ("Can not open `%s' (no host audio driver)\n", name); |
Volker Rümelin | 12f4abf | 2022-09-17 15:16:25 +0200 | [diff] [blame] | 515 | goto fail; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 516 | } |
| 517 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 518 | if (sw && audio_pcm_info_eq (&sw->info, as)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 519 | return sw; |
| 520 | } |
| 521 | |
Kővágó, Zoltán | 7183022 | 2019-03-08 23:34:15 +0100 | [diff] [blame] | 522 | if (!pdo->fixed_settings && sw) { |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 523 | glue (AUD_close_, TYPE) (card, sw); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 524 | sw = NULL; |
| 525 | } |
| 526 | |
| 527 | if (sw) { |
| 528 | HW *hw = sw->hw; |
| 529 | |
| 530 | if (!hw) { |
Volker Rümelin | b637a61 | 2023-01-21 10:47:27 +0100 | [diff] [blame] | 531 | dolog("Internal logic error: voice `%s' has no backend\n", |
| 532 | SW_NAME(sw)); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 533 | goto fail; |
| 534 | } |
| 535 | |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 536 | glue (audio_pcm_sw_fini_, TYPE) (sw); |
bellard | d929eba | 2006-07-04 21:47:22 +0000 | [diff] [blame] | 537 | if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 538 | goto fail; |
| 539 | } |
Zhang Han | 6c6886b | 2021-01-15 09:24:25 +0800 | [diff] [blame] | 540 | } else { |
Kővágó, Zoltán | 526fb05 | 2019-08-19 01:06:46 +0200 | [diff] [blame] | 541 | sw = glue(audio_pcm_create_voice_pair_, TYPE)(s, name, as); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 542 | if (!sw) { |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 543 | return NULL; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
malc | 7cbb28e | 2009-11-18 19:15:19 +0300 | [diff] [blame] | 547 | sw->card = card; |
| 548 | sw->vol = nominal_volume; |
| 549 | sw->callback.fn = callback_fn; |
| 550 | sw->callback.opaque = callback_opaque; |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 551 | |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 552 | #ifdef DEBUG_AUDIO |
malc | 7cbb28e | 2009-11-18 19:15:19 +0300 | [diff] [blame] | 553 | dolog ("%s\n", name); |
| 554 | audio_pcm_print_info ("hw", &sw->hw->info); |
| 555 | audio_pcm_print_info ("sw", &sw->info); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 556 | #endif |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 557 | |
| 558 | return sw; |
| 559 | |
| 560 | fail: |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 561 | glue (AUD_close_, TYPE) (card, sw); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 562 | return NULL; |
| 563 | } |
| 564 | |
| 565 | int glue (AUD_is_active_, TYPE) (SW *sw) |
| 566 | { |
| 567 | return sw ? sw->active : 0; |
| 568 | } |
| 569 | |
| 570 | void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts) |
| 571 | { |
| 572 | if (!sw) { |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | ts->old_ts = sw->hw->ts_helper; |
| 577 | } |
| 578 | |
bellard | c0fe382 | 2005-11-05 18:55:28 +0000 | [diff] [blame] | 579 | uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts) |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 580 | { |
| 581 | uint64_t delta, cur_ts, old_ts; |
| 582 | |
| 583 | if (!sw) { |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | cur_ts = sw->hw->ts_helper; |
| 588 | old_ts = ts->old_ts; |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 589 | /* dolog ("cur %" PRId64 " old %" PRId64 "\n", cur_ts, old_ts); */ |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 590 | |
| 591 | if (cur_ts >= old_ts) { |
| 592 | delta = cur_ts - old_ts; |
Zhang Han | 6c6886b | 2021-01-15 09:24:25 +0800 | [diff] [blame] | 593 | } else { |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 594 | delta = UINT64_MAX - old_ts + cur_ts; |
| 595 | } |
| 596 | |
| 597 | if (!delta) { |
| 598 | return 0; |
| 599 | } |
| 600 | |
malc | 4f4cc0e | 2009-09-18 08:16:03 +0400 | [diff] [blame] | 601 | return muldiv64 (delta, sw->hw->info.freq, 1000000); |
bellard | 1d14ffa | 2005-10-30 18:58:22 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | #undef TYPE |
| 605 | #undef HW |
| 606 | #undef SW |
bellard | 571ec3d | 2005-11-20 16:24:34 +0000 | [diff] [blame] | 607 | #undef HWBUF |
| 608 | #undef NAME |