blob: af0ae33fedb1e5407e66c2a51bb2f0881976de35 [file] [log] [blame]
bellard85571bc2004-11-07 18:04:02 +00001/*
2 * QEMU Audio subsystem
bellard1d14ffa2005-10-30 18:58:22 +00003 *
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 *
bellard85571bc2004-11-07 18:04:02 +00006 * 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 */
Markus Armbruster0b8fa322019-05-23 16:35:07 +020024
Peter Maydell6086a562016-01-18 17:33:52 +000025#include "qemu/osdep.h"
pbrook87ecb682007-11-17 17:14:51 +000026#include "audio.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020027#include "migration/vmstate.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010028#include "monitor/monitor.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010029#include "qemu/timer.h"
Kővágó, Zoltán71830222019-03-08 23:34:15 +010030#include "qapi/error.h"
Daniel P. Berrangé637d1802023-01-23 09:39:56 +010031#include "qapi/clone-visitor.h"
Kővágó, Zoltán71830222019-03-08 23:34:15 +010032#include "qapi/qobject-input-visitor.h"
33#include "qapi/qapi-visit-audio.h"
Daniel P. Berrangé637d1802023-01-23 09:39:56 +010034#include "qapi/qapi-commands-audio.h"
Paolo Bonzini69a80272023-09-05 11:41:05 +020035#include "qapi/qmp/qdict.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020036#include "qemu/cutils.h"
Paolo Bonzinif6061732023-09-22 19:13:44 +020037#include "qemu/error-report.h"
Volker Rümelinb9ae74e2023-01-21 10:47:25 +010038#include "qemu/log.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020039#include "qemu/module.h"
Claudio Fontana5e03b6d2022-09-08 10:14:41 +020040#include "qemu/help_option.h"
Marc-André Lureau89fc45d2022-03-23 19:57:36 +040041#include "sysemu/sysemu.h"
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +030042#include "sysemu/replay.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020043#include "sysemu/runstate.h"
Gerd Hoffmannf0c45552020-09-16 10:41:16 +020044#include "ui/qemu-spice.h"
Gerd Hoffmann1a961e72018-07-02 18:15:24 +020045#include "trace.h"
bellard85571bc2004-11-07 18:04:02 +000046
bellard1d14ffa2005-10-30 18:58:22 +000047#define AUDIO_CAP "audio"
48#include "audio_int.h"
bellard85571bc2004-11-07 18:04:02 +000049
bellard1d14ffa2005-10-30 18:58:22 +000050/* #define DEBUG_LIVE */
51/* #define DEBUG_OUT */
bellard8ead62c2006-07-04 16:51:32 +000052/* #define DEBUG_CAPTURE */
malc713a98f2009-09-12 02:28:45 +040053/* #define DEBUG_POLL */
bellard1d14ffa2005-10-30 18:58:22 +000054
bellardc0fe3822005-11-05 18:55:28 +000055#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
56
Juan Quintela2358a492009-07-27 16:13:25 +020057
58/* Order of CONFIG_AUDIO_DRIVERS is import.
59 The 1st one is the one used by default, that is the reason
60 that we generate the list.
61*/
Kővágó, Zoltán71830222019-03-08 23:34:15 +010062const char *audio_prio_list[] = {
Gerd Hoffmannd3893a32018-03-06 08:40:47 +010063 "spice",
Juan Quintela2358a492009-07-27 16:13:25 +020064 CONFIG_AUDIO_DRIVERS
Gerd Hoffmannd3893a32018-03-06 08:40:47 +010065 "none",
Kővágó, Zoltán71830222019-03-08 23:34:15 +010066 NULL
bellard1d14ffa2005-10-30 18:58:22 +000067};
bellard85571bc2004-11-07 18:04:02 +000068
Gerd Hoffmannd3893a32018-03-06 08:40:47 +010069static QLIST_HEAD(, audio_driver) audio_drivers;
Paolo Bonzini69a80272023-09-05 11:41:05 +020070static AudiodevListHead audiodevs =
71 QSIMPLEQ_HEAD_INITIALIZER(audiodevs);
72static AudiodevListHead default_audiodevs =
73 QSIMPLEQ_HEAD_INITIALIZER(default_audiodevs);
74
Gerd Hoffmannd3893a32018-03-06 08:40:47 +010075
76void audio_driver_register(audio_driver *drv)
77{
78 QLIST_INSERT_HEAD(&audio_drivers, drv, next);
79}
80
Paolo Bonzini69a80272023-09-05 11:41:05 +020081static audio_driver *audio_driver_lookup(const char *name)
Gerd Hoffmannd3893a32018-03-06 08:40:47 +010082{
83 struct audio_driver *d;
Claudio Fontanac551fb02022-09-29 11:30:33 +020084 Error *local_err = NULL;
85 int rv;
Gerd Hoffmannd3893a32018-03-06 08:40:47 +010086
87 QLIST_FOREACH(d, &audio_drivers, next) {
88 if (strcmp(name, d->name) == 0) {
89 return d;
90 }
91 }
Claudio Fontanac551fb02022-09-29 11:30:33 +020092 rv = audio_module_load(name, &local_err);
93 if (rv > 0) {
94 QLIST_FOREACH(d, &audio_drivers, next) {
95 if (strcmp(name, d->name) == 0) {
96 return d;
97 }
Gerd Hoffmann65ba8692018-03-06 08:40:48 +010098 }
Claudio Fontanac551fb02022-09-29 11:30:33 +020099 } else if (rv < 0) {
100 error_report_err(local_err);
Gerd Hoffmann65ba8692018-03-06 08:40:48 +0100101 }
Gerd Hoffmannd3893a32018-03-06 08:40:47 +0100102 return NULL;
103}
104
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +0200105static QTAILQ_HEAD(AudioStateHead, AudioState) audio_states =
106 QTAILQ_HEAD_INITIALIZER(audio_states);
Paolo Bonzini22f84d42023-10-05 17:09:36 +0200107static AudioState *default_audio_state;
bellardc0fe3822005-11-05 18:55:28 +0000108
Michael Walle00e07672011-01-05 01:05:47 +0100109const struct mixeng_volume nominal_volume = {
Juan Quintela14658cd2009-07-20 20:57:00 +0200110 .mute = 0,
bellard1d14ffa2005-10-30 18:58:22 +0000111#ifdef FLOAT_MIXENG
Juan Quintela14658cd2009-07-20 20:57:00 +0200112 .r = 1.0,
113 .l = 1.0,
bellard1d14ffa2005-10-30 18:58:22 +0000114#else
Juan Quintela14658cd2009-07-20 20:57:00 +0200115 .r = 1ULL << 32,
116 .l = 1ULL << 32,
bellard1d14ffa2005-10-30 18:58:22 +0000117#endif
bellard85571bc2004-11-07 18:04:02 +0000118};
119
bellard1d14ffa2005-10-30 18:58:22 +0000120int audio_bug (const char *funcname, int cond)
bellard85571bc2004-11-07 18:04:02 +0000121{
bellard1d14ffa2005-10-30 18:58:22 +0000122 if (cond) {
123 static int shown;
124
bellard8ead62c2006-07-04 16:51:32 +0000125 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
bellard1d14ffa2005-10-30 18:58:22 +0000126 if (!shown) {
127 shown = 1;
128 AUD_log (NULL, "Save all your work and restart without audio\n");
bellard1d14ffa2005-10-30 18:58:22 +0000129 AUD_log (NULL, "I am sorry\n");
130 }
131 AUD_log (NULL, "Context:\n");
bellard1d14ffa2005-10-30 18:58:22 +0000132 }
133
134 return cond;
135}
bellard1d14ffa2005-10-30 18:58:22 +0000136
thsf941aa22007-02-17 22:19:29 +0000137static inline int audio_bits_to_index (int bits)
138{
139 switch (bits) {
140 case 8:
141 return 0;
142
143 case 16:
144 return 1;
145
146 case 32:
147 return 2;
148
149 default:
150 audio_bug ("bits_to_index", 1);
151 AUD_log (NULL, "invalid bits %d\n", bits);
Volker Rümelin12f4abf2022-09-17 15:16:25 +0200152 return 0;
thsf941aa22007-02-17 22:19:29 +0000153 }
154}
155
bellard541e0842005-11-11 00:04:19 +0000156void AUD_vlog (const char *cap, const char *fmt, va_list ap)
157{
Kővágó, Zoltán06ac27f2015-06-12 14:33:02 +0200158 if (cap) {
159 fprintf(stderr, "%s: ", cap);
bellard541e0842005-11-11 00:04:19 +0000160 }
bellard541e0842005-11-11 00:04:19 +0000161
Kővágó, Zoltán06ac27f2015-06-12 14:33:02 +0200162 vfprintf(stderr, fmt, ap);
bellard541e0842005-11-11 00:04:19 +0000163}
164
bellardfb065182004-11-09 23:09:44 +0000165void AUD_log (const char *cap, const char *fmt, ...)
bellard85571bc2004-11-07 18:04:02 +0000166{
167 va_list ap;
bellard85571bc2004-11-07 18:04:02 +0000168
bellard541e0842005-11-11 00:04:19 +0000169 va_start (ap, fmt);
170 AUD_vlog (cap, fmt, ap);
171 va_end (ap);
bellard85571bc2004-11-07 18:04:02 +0000172}
173
malc1ea879e2008-12-03 22:48:44 +0000174static void audio_print_settings (struct audsettings *as)
bellardc0fe3822005-11-05 18:55:28 +0000175{
176 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
177
178 switch (as->fmt) {
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100179 case AUDIO_FORMAT_S8:
bellardc0fe3822005-11-05 18:55:28 +0000180 AUD_log (NULL, "S8");
181 break;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100182 case AUDIO_FORMAT_U8:
bellardc0fe3822005-11-05 18:55:28 +0000183 AUD_log (NULL, "U8");
184 break;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100185 case AUDIO_FORMAT_S16:
bellardc0fe3822005-11-05 18:55:28 +0000186 AUD_log (NULL, "S16");
187 break;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100188 case AUDIO_FORMAT_U16:
bellardc0fe3822005-11-05 18:55:28 +0000189 AUD_log (NULL, "U16");
190 break;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100191 case AUDIO_FORMAT_S32:
malcd50997f2008-06-22 14:10:45 +0000192 AUD_log (NULL, "S32");
193 break;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100194 case AUDIO_FORMAT_U32:
malcd50997f2008-06-22 14:10:45 +0000195 AUD_log (NULL, "U32");
196 break;
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100197 case AUDIO_FORMAT_F32:
198 AUD_log (NULL, "F32");
199 break;
bellardc0fe3822005-11-05 18:55:28 +0000200 default:
201 AUD_log (NULL, "invalid(%d)", as->fmt);
202 break;
203 }
bellardec36b692006-07-16 18:57:03 +0000204
205 AUD_log (NULL, " endianness=");
bellardd929eba2006-07-04 21:47:22 +0000206 switch (as->endianness) {
207 case 0:
208 AUD_log (NULL, "little");
209 break;
210 case 1:
211 AUD_log (NULL, "big");
212 break;
213 default:
214 AUD_log (NULL, "invalid");
215 break;
216 }
bellardc0fe3822005-11-05 18:55:28 +0000217 AUD_log (NULL, "\n");
218}
219
malc1ea879e2008-12-03 22:48:44 +0000220static int audio_validate_settings (struct audsettings *as)
bellardc0fe3822005-11-05 18:55:28 +0000221{
222 int invalid;
223
Kővágó, Zoltánb5c7db32019-10-13 21:58:03 +0200224 invalid = as->nchannels < 1;
bellardd929eba2006-07-04 21:47:22 +0000225 invalid |= as->endianness != 0 && as->endianness != 1;
bellardc0fe3822005-11-05 18:55:28 +0000226
227 switch (as->fmt) {
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100228 case AUDIO_FORMAT_S8:
229 case AUDIO_FORMAT_U8:
230 case AUDIO_FORMAT_S16:
231 case AUDIO_FORMAT_U16:
232 case AUDIO_FORMAT_S32:
233 case AUDIO_FORMAT_U32:
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100234 case AUDIO_FORMAT_F32:
bellardc0fe3822005-11-05 18:55:28 +0000235 break;
236 default:
237 invalid = 1;
238 break;
239 }
240
241 invalid |= as->freq <= 0;
bellardd929eba2006-07-04 21:47:22 +0000242 return invalid ? -1 : 0;
bellardc0fe3822005-11-05 18:55:28 +0000243}
244
malc1ea879e2008-12-03 22:48:44 +0000245static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
bellard1d14ffa2005-10-30 18:58:22 +0000246{
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100247 int bits = 8;
248 bool is_signed = false, is_float = false;
bellard1d14ffa2005-10-30 18:58:22 +0000249
bellardc0fe3822005-11-05 18:55:28 +0000250 switch (as->fmt) {
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100251 case AUDIO_FORMAT_S8:
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100252 is_signed = true;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100253 /* fall through */
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100254 case AUDIO_FORMAT_U8:
bellard1d14ffa2005-10-30 18:58:22 +0000255 break;
256
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100257 case AUDIO_FORMAT_S16:
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100258 is_signed = true;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100259 /* fall through */
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100260 case AUDIO_FORMAT_U16:
bellard1d14ffa2005-10-30 18:58:22 +0000261 bits = 16;
262 break;
thsf941aa22007-02-17 22:19:29 +0000263
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100264 case AUDIO_FORMAT_F32:
265 is_float = true;
266 /* fall through */
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100267 case AUDIO_FORMAT_S32:
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100268 is_signed = true;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100269 /* fall through */
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100270 case AUDIO_FORMAT_U32:
thsf941aa22007-02-17 22:19:29 +0000271 bits = 32;
272 break;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100273
274 default:
275 abort();
bellard1d14ffa2005-10-30 18:58:22 +0000276 }
bellardc0fe3822005-11-05 18:55:28 +0000277 return info->freq == as->freq
278 && info->nchannels == as->nchannels
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100279 && info->is_signed == is_signed
280 && info->is_float == is_float
bellardd929eba2006-07-04 21:47:22 +0000281 && info->bits == bits
282 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
bellard1d14ffa2005-10-30 18:58:22 +0000283}
284
malc1ea879e2008-12-03 22:48:44 +0000285void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
bellard85571bc2004-11-07 18:04:02 +0000286{
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100287 int bits = 8, mul;
288 bool is_signed = false, is_float = false;
bellard85571bc2004-11-07 18:04:02 +0000289
bellardc0fe3822005-11-05 18:55:28 +0000290 switch (as->fmt) {
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100291 case AUDIO_FORMAT_S8:
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100292 is_signed = true;
Philippe Mathieu-Daudéf7621fd2019-12-18 20:25:21 +0100293 /* fall through */
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100294 case AUDIO_FORMAT_U8:
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200295 mul = 1;
bellard85571bc2004-11-07 18:04:02 +0000296 break;
297
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100298 case AUDIO_FORMAT_S16:
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100299 is_signed = true;
Stefan Weile4634942019-07-18 21:34:41 +0200300 /* fall through */
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100301 case AUDIO_FORMAT_U16:
bellard85571bc2004-11-07 18:04:02 +0000302 bits = 16;
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200303 mul = 2;
thsf941aa22007-02-17 22:19:29 +0000304 break;
305
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100306 case AUDIO_FORMAT_F32:
307 is_float = true;
308 /* fall through */
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100309 case AUDIO_FORMAT_S32:
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100310 is_signed = true;
Stefan Weile4634942019-07-18 21:34:41 +0200311 /* fall through */
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100312 case AUDIO_FORMAT_U32:
thsf941aa22007-02-17 22:19:29 +0000313 bits = 32;
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200314 mul = 4;
bellard85571bc2004-11-07 18:04:02 +0000315 break;
Kővágó, Zoltán85bc5852019-03-08 23:34:13 +0100316
317 default:
318 abort();
bellard85571bc2004-11-07 18:04:02 +0000319 }
320
bellardc0fe3822005-11-05 18:55:28 +0000321 info->freq = as->freq;
bellard1d14ffa2005-10-30 18:58:22 +0000322 info->bits = bits;
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100323 info->is_signed = is_signed;
324 info->is_float = is_float;
bellardc0fe3822005-11-05 18:55:28 +0000325 info->nchannels = as->nchannels;
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200326 info->bytes_per_frame = as->nchannels * mul;
327 info->bytes_per_second = info->freq * info->bytes_per_frame;
bellardd929eba2006-07-04 21:47:22 +0000328 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
bellard85571bc2004-11-07 18:04:02 +0000329}
330
bellard1d14ffa2005-10-30 18:58:22 +0000331void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
bellard85571bc2004-11-07 18:04:02 +0000332{
bellard1d14ffa2005-10-30 18:58:22 +0000333 if (!len) {
334 return;
335 }
336
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100337 if (info->is_signed || info->is_float) {
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200338 memset(buf, 0x00, len * info->bytes_per_frame);
Zhang Han6c6886b2021-01-15 09:24:25 +0800339 } else {
thsf941aa22007-02-17 22:19:29 +0000340 switch (info->bits) {
341 case 8:
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200342 memset(buf, 0x80, len * info->bytes_per_frame);
thsf941aa22007-02-17 22:19:29 +0000343 break;
bellard1d14ffa2005-10-30 18:58:22 +0000344
thsf941aa22007-02-17 22:19:29 +0000345 case 16:
346 {
347 int i;
348 uint16_t *p = buf;
thsf941aa22007-02-17 22:19:29 +0000349 short s = INT16_MAX;
bellard1d14ffa2005-10-30 18:58:22 +0000350
thsf941aa22007-02-17 22:19:29 +0000351 if (info->swap_endianness) {
352 s = bswap16 (s);
353 }
354
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200355 for (i = 0; i < len * info->nchannels; i++) {
thsf941aa22007-02-17 22:19:29 +0000356 p[i] = s;
357 }
bellard1d14ffa2005-10-30 18:58:22 +0000358 }
thsf941aa22007-02-17 22:19:29 +0000359 break;
360
361 case 32:
362 {
363 int i;
364 uint32_t *p = buf;
thsf941aa22007-02-17 22:19:29 +0000365 int32_t s = INT32_MAX;
366
367 if (info->swap_endianness) {
368 s = bswap32 (s);
369 }
370
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +0200371 for (i = 0; i < len * info->nchannels; i++) {
thsf941aa22007-02-17 22:19:29 +0000372 p[i] = s;
373 }
374 }
375 break;
376
377 default:
378 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
379 info->bits);
380 break;
bellard1d14ffa2005-10-30 18:58:22 +0000381 }
382 }
bellard85571bc2004-11-07 18:04:02 +0000383}
384
bellard1d14ffa2005-10-30 18:58:22 +0000385/*
bellard8ead62c2006-07-04 16:51:32 +0000386 * Capture
387 */
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200388static CaptureVoiceOut *audio_pcm_capture_find_specific(AudioState *s,
389 struct audsettings *as)
bellard8ead62c2006-07-04 16:51:32 +0000390{
391 CaptureVoiceOut *cap;
bellard8ead62c2006-07-04 16:51:32 +0000392
393 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
bellardd929eba2006-07-04 21:47:22 +0000394 if (audio_pcm_info_eq (&cap->hw.info, as)) {
bellard8ead62c2006-07-04 16:51:32 +0000395 return cap;
396 }
397 }
398 return NULL;
399}
400
bellardec36b692006-07-16 18:57:03 +0000401static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
402{
403 struct capture_callback *cb;
404
405#ifdef DEBUG_CAPTURE
406 dolog ("notification %d sent\n", cmd);
407#endif
408 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
409 cb->ops.notify (cb->opaque, cmd);
410 }
411}
412
413static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
bellard8ead62c2006-07-04 16:51:32 +0000414{
415 if (cap->hw.enabled != enabled) {
bellardec36b692006-07-16 18:57:03 +0000416 audcnotification_e cmd;
bellard8ead62c2006-07-04 16:51:32 +0000417 cap->hw.enabled = enabled;
bellardec36b692006-07-16 18:57:03 +0000418 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
419 audio_notify_capture (cap, cmd);
bellard8ead62c2006-07-04 16:51:32 +0000420 }
421}
422
423static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
424{
425 HWVoiceOut *hw = &cap->hw;
426 SWVoiceOut *sw;
427 int enabled = 0;
428
bellardec36b692006-07-16 18:57:03 +0000429 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
bellard8ead62c2006-07-04 16:51:32 +0000430 if (sw->active) {
431 enabled = 1;
432 break;
433 }
434 }
bellardec36b692006-07-16 18:57:03 +0000435 audio_capture_maybe_changed (cap, enabled);
bellard8ead62c2006-07-04 16:51:32 +0000436}
437
438static void audio_detach_capture (HWVoiceOut *hw)
439{
bellardec36b692006-07-16 18:57:03 +0000440 SWVoiceCap *sc = hw->cap_head.lh_first;
bellard8ead62c2006-07-04 16:51:32 +0000441
bellardec36b692006-07-16 18:57:03 +0000442 while (sc) {
443 SWVoiceCap *sc1 = sc->entries.le_next;
444 SWVoiceOut *sw = &sc->sw;
445 CaptureVoiceOut *cap = sc->cap;
446 int was_active = sw->active;
447
bellard8ead62c2006-07-04 16:51:32 +0000448 if (sw->rate) {
449 st_rate_stop (sw->rate);
450 sw->rate = NULL;
451 }
452
Blue Swirl72cf2d42009-09-12 07:36:22 +0000453 QLIST_REMOVE (sw, entries);
454 QLIST_REMOVE (sc, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -0500455 g_free (sc);
bellardec36b692006-07-16 18:57:03 +0000456 if (was_active) {
457 /* We have removed soft voice from the capture:
458 this might have changed the overall status of the capture
459 since this might have been the only active voice */
460 audio_recalc_and_notify_capture (cap);
461 }
462 sc = sc1;
bellard8ead62c2006-07-04 16:51:32 +0000463 }
464}
465
malc1a7dafc2009-05-14 03:11:35 +0400466static int audio_attach_capture (HWVoiceOut *hw)
bellard8ead62c2006-07-04 16:51:32 +0000467{
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200468 AudioState *s = hw->s;
bellard8ead62c2006-07-04 16:51:32 +0000469 CaptureVoiceOut *cap;
470
471 audio_detach_capture (hw);
472 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
bellardec36b692006-07-16 18:57:03 +0000473 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +0000474 SWVoiceOut *sw;
bellardec36b692006-07-16 18:57:03 +0000475 HWVoiceOut *hw_cap = &cap->hw;
bellard8ead62c2006-07-04 16:51:32 +0000476
Frediano Ziglioe8d85442019-02-25 15:43:35 +0000477 sc = g_malloc0(sizeof(*sc));
bellard8ead62c2006-07-04 16:51:32 +0000478
bellardec36b692006-07-16 18:57:03 +0000479 sc->cap = cap;
480 sw = &sc->sw;
bellard8ead62c2006-07-04 16:51:32 +0000481 sw->hw = hw_cap;
bellardec36b692006-07-16 18:57:03 +0000482 sw->info = hw->info;
bellard8ead62c2006-07-04 16:51:32 +0000483 sw->empty = 1;
484 sw->active = hw->enabled;
malc83617102012-07-16 18:08:36 +0400485 sw->vol = nominal_volume;
bellard8ead62c2006-07-04 16:51:32 +0000486 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
Blue Swirl72cf2d42009-09-12 07:36:22 +0000487 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
488 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
bellardec36b692006-07-16 18:57:03 +0000489#ifdef DEBUG_CAPTURE
Stefan Weil457b6542013-01-16 18:17:33 +0100490 sw->name = g_strdup_printf ("for %p %d,%d,%d",
491 hw, sw->info.freq, sw->info.bits,
492 sw->info.nchannels);
bellardec36b692006-07-16 18:57:03 +0000493 dolog ("Added %s active = %d\n", sw->name, sw->active);
494#endif
bellard8ead62c2006-07-04 16:51:32 +0000495 if (sw->active) {
bellardec36b692006-07-16 18:57:03 +0000496 audio_capture_maybe_changed (cap, 1);
bellard8ead62c2006-07-04 16:51:32 +0000497 }
498 }
499 return 0;
500}
501
502/*
bellard1d14ffa2005-10-30 18:58:22 +0000503 * Hard voice (capture)
504 */
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200505static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw)
bellard85571bc2004-11-07 18:04:02 +0000506{
bellard1d14ffa2005-10-30 18:58:22 +0000507 SWVoiceIn *sw;
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200508 size_t m = hw->total_samples_captured;
bellard85571bc2004-11-07 18:04:02 +0000509
bellard1d14ffa2005-10-30 18:58:22 +0000510 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
511 if (sw->active) {
Kővágó, Zoltán58935912019-08-19 01:06:54 +0200512 m = MIN (m, sw->total_hw_samples_acquired);
bellard85571bc2004-11-07 18:04:02 +0000513 }
514 }
bellard1d14ffa2005-10-30 18:58:22 +0000515 return m;
bellard85571bc2004-11-07 18:04:02 +0000516}
517
Kővágó, Zoltán3f5bbfc2019-09-19 23:24:19 +0200518static size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw)
bellard85571bc2004-11-07 18:04:02 +0000519{
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200520 size_t live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
Volker Rümelin8dbd3d12023-02-24 20:05:41 +0100521 if (audio_bug(__func__, live > hw->conv_buf.size)) {
522 dolog("live=%zu hw->conv_buf.size=%zu\n", live, hw->conv_buf.size);
Volker Rümelin12f4abf2022-09-17 15:16:25 +0200523 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000524 }
525 return live;
526}
bellard85571bc2004-11-07 18:04:02 +0000527
Volker Rümelin251f1542022-03-01 20:12:59 +0100528static size_t audio_pcm_hw_conv_in(HWVoiceIn *hw, void *pcm_buf, size_t samples)
529{
530 size_t conv = 0;
Volker Rümelin8dbd3d12023-02-24 20:05:41 +0100531 STSampleBuffer *conv_buf = &hw->conv_buf;
Volker Rümelin251f1542022-03-01 20:12:59 +0100532
533 while (samples) {
534 uint8_t *src = advance(pcm_buf, conv * hw->info.bytes_per_frame);
535 size_t proc = MIN(samples, conv_buf->size - conv_buf->pos);
536
Volker Rümelin8dbd3d12023-02-24 20:05:41 +0100537 hw->conv(conv_buf->buffer + conv_buf->pos, src, proc);
Volker Rümelin251f1542022-03-01 20:12:59 +0100538 conv_buf->pos = (conv_buf->pos + proc) % conv_buf->size;
539 samples -= proc;
540 conv += proc;
541 }
542
543 return conv;
544}
545
bellard1d14ffa2005-10-30 18:58:22 +0000546/*
547 * Soft voice (capture)
548 */
Volker Rümelin1c49c5f2023-02-24 20:05:50 +0100549static void audio_pcm_sw_resample_in(SWVoiceIn *sw,
550 size_t frames_in_max, size_t frames_out_max,
551 size_t *total_in, size_t *total_out)
552{
553 HWVoiceIn *hw = sw->hw;
554 struct st_sample *src, *dst;
555 size_t live, rpos, frames_in, frames_out;
556
557 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
558 rpos = audio_ring_posb(hw->conv_buf.pos, live, hw->conv_buf.size);
559
560 /* resample conv_buf from rpos to end of buffer */
561 src = hw->conv_buf.buffer + rpos;
562 frames_in = MIN(frames_in_max, hw->conv_buf.size - rpos);
563 dst = sw->resample_buf.buffer;
564 frames_out = frames_out_max;
565 st_rate_flow(sw->rate, src, dst, &frames_in, &frames_out);
566 rpos += frames_in;
567 *total_in = frames_in;
568 *total_out = frames_out;
569
570 /* resample conv_buf from start of buffer if there are input frames left */
571 if (frames_in_max - frames_in && rpos == hw->conv_buf.size) {
572 src = hw->conv_buf.buffer;
573 frames_in = frames_in_max - frames_in;
574 dst += frames_out;
575 frames_out = frames_out_max - frames_out;
576 st_rate_flow(sw->rate, src, dst, &frames_in, &frames_out);
577 *total_in += frames_in;
578 *total_out += frames_out;
579 }
580}
581
Volker Rümelinfbde1ed2023-02-24 20:05:51 +0100582static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t buf_len)
bellard85571bc2004-11-07 18:04:02 +0000583{
bellard1d14ffa2005-10-30 18:58:22 +0000584 HWVoiceIn *hw = sw->hw;
Volker Rümelina9ea5672023-02-24 20:05:52 +0100585 size_t live, frames_out_max, total_in, total_out;
bellard85571bc2004-11-07 18:04:02 +0000586
bellard1d14ffa2005-10-30 18:58:22 +0000587 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
Volker Rümelin0ceb26a2022-03-01 20:13:00 +0100588 if (!live) {
589 return 0;
590 }
Volker Rümelin8dbd3d12023-02-24 20:05:41 +0100591 if (audio_bug(__func__, live > hw->conv_buf.size)) {
592 dolog("live_in=%zu hw->conv_buf.size=%zu\n", live, hw->conv_buf.size);
Volker Rümelin12f4abf2022-09-17 15:16:25 +0200593 return 0;
bellard85571bc2004-11-07 18:04:02 +0000594 }
bellard1d14ffa2005-10-30 18:58:22 +0000595
Volker Rümelina9ea5672023-02-24 20:05:52 +0100596 frames_out_max = MIN(buf_len / sw->info.bytes_per_frame,
597 sw->resample_buf.size);
bellard1d14ffa2005-10-30 18:58:22 +0000598
Volker Rümelina9ea5672023-02-24 20:05:52 +0100599 audio_pcm_sw_resample_in(sw, live, frames_out_max, &total_in, &total_out);
bellard1d14ffa2005-10-30 18:58:22 +0000600
Volker Rümelin669b9522022-03-01 20:13:05 +0100601 if (!hw->pcm_ops->volume_in) {
Volker Rümelinfbde1ed2023-02-24 20:05:51 +0100602 mixeng_volume(sw->resample_buf.buffer, total_out, &sw->vol);
Marc-André Lureauc01b2452012-04-17 14:32:36 +0200603 }
Volker Rümelinfbde1ed2023-02-24 20:05:51 +0100604 sw->clip(buf, sw->resample_buf.buffer, total_out);
Michael Walle00e07672011-01-05 01:05:47 +0100605
Volker Rümelinfbde1ed2023-02-24 20:05:51 +0100606 sw->total_hw_samples_acquired += total_in;
607 return total_out * sw->info.bytes_per_frame;
bellard1d14ffa2005-10-30 18:58:22 +0000608}
609
610/*
611 * Hard voice (playback)
612 */
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200613static size_t audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
bellard1d14ffa2005-10-30 18:58:22 +0000614{
615 SWVoiceOut *sw;
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200616 size_t m = SIZE_MAX;
bellard1d14ffa2005-10-30 18:58:22 +0000617 int nb_live = 0;
618
619 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
620 if (sw->active || !sw->empty) {
Kővágó, Zoltán58935912019-08-19 01:06:54 +0200621 m = MIN (m, sw->total_hw_samples_mixed);
bellard1d14ffa2005-10-30 18:58:22 +0000622 nb_live += 1;
623 }
624 }
625
626 *nb_livep = nb_live;
627 return m;
628}
629
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200630static size_t audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
bellard1d14ffa2005-10-30 18:58:22 +0000631{
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200632 size_t smin;
malcbdff2532009-09-18 11:37:39 +0400633 int nb_live1;
bellard1d14ffa2005-10-30 18:58:22 +0000634
malcbdff2532009-09-18 11:37:39 +0400635 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
636 if (nb_live) {
637 *nb_live = nb_live1;
bellard1d14ffa2005-10-30 18:58:22 +0000638 }
malcbdff2532009-09-18 11:37:39 +0400639
640 if (nb_live1) {
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200641 size_t live = smin;
bellard1d14ffa2005-10-30 18:58:22 +0000642
Volker Rümelin8dbd3d12023-02-24 20:05:41 +0100643 if (audio_bug(__func__, live > hw->mix_buf.size)) {
644 dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size);
Volker Rümelin12f4abf2022-09-17 15:16:25 +0200645 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000646 }
647 return live;
648 }
malcbdff2532009-09-18 11:37:39 +0400649 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000650}
651
Volker Rümelin98334382022-03-01 20:13:06 +0100652static size_t audio_pcm_hw_get_free(HWVoiceOut *hw)
653{
654 return (hw->pcm_ops->buffer_get_free ? hw->pcm_ops->buffer_get_free(hw) :
655 INT_MAX) / hw->info.bytes_per_frame;
656}
657
Volker Rümelin8e56a172022-03-01 20:12:58 +0100658static void audio_pcm_hw_clip_out(HWVoiceOut *hw, void *pcm_buf, size_t len)
659{
660 size_t clipped = 0;
Volker Rümelin8dbd3d12023-02-24 20:05:41 +0100661 size_t pos = hw->mix_buf.pos;
Volker Rümelin8e56a172022-03-01 20:12:58 +0100662
663 while (len) {
Volker Rümelin8dbd3d12023-02-24 20:05:41 +0100664 st_sample *src = hw->mix_buf.buffer + pos;
Volker Rümelin8e56a172022-03-01 20:12:58 +0100665 uint8_t *dst = advance(pcm_buf, clipped * hw->info.bytes_per_frame);
Volker Rümelin8dbd3d12023-02-24 20:05:41 +0100666 size_t samples_till_end_of_buf = hw->mix_buf.size - pos;
Volker Rümelin8e56a172022-03-01 20:12:58 +0100667 size_t samples_to_clip = MIN(len, samples_till_end_of_buf);
668
669 hw->clip(dst, src, samples_to_clip);
670
Volker Rümelin8dbd3d12023-02-24 20:05:41 +0100671 pos = (pos + samples_to_clip) % hw->mix_buf.size;
Volker Rümelin8e56a172022-03-01 20:12:58 +0100672 len -= samples_to_clip;
673 clipped += samples_to_clip;
674 }
675}
676
bellard1d14ffa2005-10-30 18:58:22 +0000677/*
678 * Soft voice (playback)
679 */
Volker Rümelin8a81abe2023-02-24 20:05:44 +0100680static void audio_pcm_sw_resample_out(SWVoiceOut *sw,
681 size_t frames_in_max, size_t frames_out_max,
682 size_t *total_in, size_t *total_out)
683{
684 HWVoiceOut *hw = sw->hw;
685 struct st_sample *src, *dst;
686 size_t live, wpos, frames_in, frames_out;
687
688 live = sw->total_hw_samples_mixed;
689 wpos = (hw->mix_buf.pos + live) % hw->mix_buf.size;
690
691 /* write to mix_buf from wpos to end of buffer */
692 src = sw->resample_buf.buffer;
693 frames_in = frames_in_max;
694 dst = hw->mix_buf.buffer + wpos;
695 frames_out = MIN(frames_out_max, hw->mix_buf.size - wpos);
696 st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out);
697 wpos += frames_out;
698 *total_in = frames_in;
699 *total_out = frames_out;
700
701 /* write to mix_buf from start of buffer if there are input frames left */
702 if (frames_in_max - frames_in > 0 && wpos == hw->mix_buf.size) {
703 src += frames_in;
704 frames_in = frames_in_max - frames_in;
705 dst = hw->mix_buf.buffer;
706 frames_out = frames_out_max - frames_out;
707 st_rate_flow_mix(sw->rate, src, dst, &frames_in, &frames_out);
708 *total_in += frames_in;
709 *total_out += frames_out;
710 }
711}
712
Volker Rümelind5647bd2023-02-24 20:05:46 +0100713static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t buf_len)
bellard1d14ffa2005-10-30 18:58:22 +0000714{
Volker Rümelind5647bd2023-02-24 20:05:46 +0100715 HWVoiceOut *hw = sw->hw;
Volker Rümelin1a01df32023-02-24 20:05:49 +0100716 size_t live, dead, hw_free, sw_max, fe_max;
717 size_t frames_in_max, frames_out_max, total_in, total_out;
bellard1d14ffa2005-10-30 18:58:22 +0000718
719 live = sw->total_hw_samples_mixed;
Volker Rümelind5647bd2023-02-24 20:05:46 +0100720 if (audio_bug(__func__, live > hw->mix_buf.size)) {
721 dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size);
Volker Rümelin12f4abf2022-09-17 15:16:25 +0200722 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000723 }
724
Volker Rümelind5647bd2023-02-24 20:05:46 +0100725 if (live == hw->mix_buf.size) {
bellardec36b692006-07-16 18:57:03 +0000726#ifdef DEBUG_OUT
Akihiko Odaki0c29b782021-06-16 23:14:11 +0900727 dolog ("%s is full %zu\n", sw->name, live);
bellardec36b692006-07-16 18:57:03 +0000728#endif
bellard1d14ffa2005-10-30 18:58:22 +0000729 return 0;
730 }
731
Volker Rümelind5647bd2023-02-24 20:05:46 +0100732 dead = hw->mix_buf.size - live;
733 hw_free = audio_pcm_hw_get_free(hw);
Volker Rümelin98334382022-03-01 20:13:06 +0100734 hw_free = hw_free > live ? hw_free - live : 0;
Volker Rümelin1a01df32023-02-24 20:05:49 +0100735 frames_out_max = MIN(dead, hw_free);
736 sw_max = st_rate_frames_in(sw->rate, frames_out_max);
Volker Rümeline1e6a6f2023-02-24 20:05:53 +0100737 fe_max = MIN(buf_len / sw->info.bytes_per_frame + sw->resample_buf.pos,
738 sw->resample_buf.size);
Volker Rümelin1a01df32023-02-24 20:05:49 +0100739 frames_in_max = MIN(sw_max, fe_max);
Marc-André Lureauc01b2452012-04-17 14:32:36 +0200740
Volker Rümelin1a01df32023-02-24 20:05:49 +0100741 if (!frames_in_max) {
742 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000743 }
bellard85571bc2004-11-07 18:04:02 +0000744
Volker Rümeline1e6a6f2023-02-24 20:05:53 +0100745 if (frames_in_max > sw->resample_buf.pos) {
746 sw->conv(sw->resample_buf.buffer + sw->resample_buf.pos,
747 buf, frames_in_max - sw->resample_buf.pos);
748 if (!sw->hw->pcm_ops->volume_out) {
749 mixeng_volume(sw->resample_buf.buffer + sw->resample_buf.pos,
750 frames_in_max - sw->resample_buf.pos, &sw->vol);
751 }
Volker Rümelin1a01df32023-02-24 20:05:49 +0100752 }
753
754 audio_pcm_sw_resample_out(sw, frames_in_max, frames_out_max,
Volker Rümelind5647bd2023-02-24 20:05:46 +0100755 &total_in, &total_out);
bellard85571bc2004-11-07 18:04:02 +0000756
Volker Rümelind5647bd2023-02-24 20:05:46 +0100757 sw->total_hw_samples_mixed += total_out;
bellard1d14ffa2005-10-30 18:58:22 +0000758 sw->empty = sw->total_hw_samples_mixed == 0;
759
Volker Rümeline1e6a6f2023-02-24 20:05:53 +0100760 /*
761 * Upsampling may leave one audio frame in the resample buffer. Decrement
762 * total_in by one if there was a leftover frame from the previous resample
763 * pass in the resample buffer. Increment total_in by one if the current
764 * resample pass left one frame in the resample buffer.
765 */
766 if (frames_in_max - total_in == 1) {
767 /* copy one leftover audio frame to the beginning of the buffer */
768 *sw->resample_buf.buffer = *(sw->resample_buf.buffer + total_in);
769 total_in += 1 - sw->resample_buf.pos;
770 sw->resample_buf.pos = 1;
771 } else if (total_in >= sw->resample_buf.pos) {
772 total_in -= sw->resample_buf.pos;
773 sw->resample_buf.pos = 0;
774 }
775
bellard1d14ffa2005-10-30 18:58:22 +0000776#ifdef DEBUG_OUT
777 dolog (
Volker Rümelind5647bd2023-02-24 20:05:46 +0100778 "%s: write size %zu written %zu total mixed %zu\n",
779 SW_NAME(sw),
780 buf_len / sw->info.bytes_per_frame,
781 total_in,
bellardc0fe3822005-11-05 18:55:28 +0000782 sw->total_hw_samples_mixed
bellard1d14ffa2005-10-30 18:58:22 +0000783 );
784#endif
785
Volker Rümelind5647bd2023-02-24 20:05:46 +0100786 return total_in * sw->info.bytes_per_frame;
bellard85571bc2004-11-07 18:04:02 +0000787}
788
bellard1d14ffa2005-10-30 18:58:22 +0000789#ifdef DEBUG_AUDIO
790static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
bellard85571bc2004-11-07 18:04:02 +0000791{
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +0100792 dolog("%s: bits %d, sign %d, float %d, freq %d, nchan %d\n",
793 cap, info->bits, info->is_signed, info->is_float, info->freq,
794 info->nchannels);
bellard85571bc2004-11-07 18:04:02 +0000795}
bellard1d14ffa2005-10-30 18:58:22 +0000796#endif
bellard85571bc2004-11-07 18:04:02 +0000797
bellard1d14ffa2005-10-30 18:58:22 +0000798#define DAC
799#include "audio_template.h"
800#undef DAC
801#include "audio_template.h"
bellard85571bc2004-11-07 18:04:02 +0000802
malc713a98f2009-09-12 02:28:45 +0400803/*
804 * Timer
805 */
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200806static int audio_is_timer_needed(AudioState *s)
malc713a98f2009-09-12 02:28:45 +0400807{
808 HWVoiceIn *hwi = NULL;
809 HWVoiceOut *hwo = NULL;
810
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200811 while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
Zhang Han6c6886b2021-01-15 09:24:25 +0800812 if (!hwo->poll_mode) {
813 return 1;
814 }
malc713a98f2009-09-12 02:28:45 +0400815 }
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200816 while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
Zhang Han6c6886b2021-01-15 09:24:25 +0800817 if (!hwi->poll_mode) {
818 return 1;
819 }
malc713a98f2009-09-12 02:28:45 +0400820 }
821 return 0;
822}
823
malc39deb1e2010-11-18 14:30:12 +0300824static void audio_reset_timer (AudioState *s)
malc713a98f2009-09-12 02:28:45 +0400825{
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200826 if (audio_is_timer_needed(s)) {
Pavel Dovgalyuk1ffc2662017-02-14 10:15:10 +0300827 timer_mod_anticipate_ns(s->ts,
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100828 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks);
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200829 if (!s->timer_running) {
830 s->timer_running = true;
831 s->timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100832 trace_audio_timer_start(s->period_ticks / SCALE_MS);
Gerd Hoffmann1a961e72018-07-02 18:15:24 +0200833 }
834 } else {
835 timer_del(s->ts);
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200836 if (s->timer_running) {
837 s->timer_running = false;
Gerd Hoffmann1a961e72018-07-02 18:15:24 +0200838 trace_audio_timer_stop();
839 }
malc713a98f2009-09-12 02:28:45 +0400840 }
841}
842
malc39deb1e2010-11-18 14:30:12 +0300843static void audio_timer (void *opaque)
844{
Gerd Hoffmann1a961e72018-07-02 18:15:24 +0200845 int64_t now, diff;
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100846 AudioState *s = opaque;
Gerd Hoffmann1a961e72018-07-02 18:15:24 +0200847
848 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200849 diff = now - s->timer_last;
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100850 if (diff > s->period_ticks * 3 / 2) {
Gerd Hoffmann1a961e72018-07-02 18:15:24 +0200851 trace_audio_timer_delayed(diff / SCALE_MS);
852 }
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200853 s->timer_last = now;
Gerd Hoffmann1a961e72018-07-02 18:15:24 +0200854
Kővágó, Zoltán18e2c172019-08-19 01:06:55 +0200855 audio_run(s, "timer");
Kővágó, Zoltán71830222019-03-08 23:34:15 +0100856 audio_reset_timer(s);
malc39deb1e2010-11-18 14:30:12 +0300857}
858
malc713a98f2009-09-12 02:28:45 +0400859/*
860 * Public API
861 */
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200862size_t AUD_write(SWVoiceOut *sw, void *buf, size_t size)
bellard85571bc2004-11-07 18:04:02 +0000863{
Kővágó, Zoltán19306162019-10-13 21:57:59 +0200864 HWVoiceOut *hw;
865
bellard1d14ffa2005-10-30 18:58:22 +0000866 if (!sw) {
867 /* XXX: Consider options */
868 return size;
869 }
Kővágó, Zoltán19306162019-10-13 21:57:59 +0200870 hw = sw->hw;
bellard1d14ffa2005-10-30 18:58:22 +0000871
Kővágó, Zoltán19306162019-10-13 21:57:59 +0200872 if (!hw->enabled) {
bellardc0fe3822005-11-05 18:55:28 +0000873 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
bellard1d14ffa2005-10-30 18:58:22 +0000874 return 0;
875 }
876
Kővágó, Zoltán19306162019-10-13 21:57:59 +0200877 if (audio_get_pdo_out(hw->s->dev)->mixing_engine) {
878 return audio_pcm_sw_write(sw, buf, size);
879 } else {
880 return hw->pcm_ops->write(hw, buf, size);
881 }
bellard85571bc2004-11-07 18:04:02 +0000882}
883
Kővágó, Zoltán75204622019-08-19 01:06:58 +0200884size_t AUD_read(SWVoiceIn *sw, void *buf, size_t size)
bellard85571bc2004-11-07 18:04:02 +0000885{
Kővágó, Zoltán19306162019-10-13 21:57:59 +0200886 HWVoiceIn *hw;
887
bellard1d14ffa2005-10-30 18:58:22 +0000888 if (!sw) {
889 /* XXX: Consider options */
890 return size;
bellard85571bc2004-11-07 18:04:02 +0000891 }
Kővágó, Zoltán19306162019-10-13 21:57:59 +0200892 hw = sw->hw;
bellard1d14ffa2005-10-30 18:58:22 +0000893
Kővágó, Zoltán19306162019-10-13 21:57:59 +0200894 if (!hw->enabled) {
bellardc0fe3822005-11-05 18:55:28 +0000895 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
bellard1d14ffa2005-10-30 18:58:22 +0000896 return 0;
897 }
898
Kővágó, Zoltán19306162019-10-13 21:57:59 +0200899 if (audio_get_pdo_in(hw->s->dev)->mixing_engine) {
900 return audio_pcm_sw_read(sw, buf, size);
901 } else {
902 return hw->pcm_ops->read(hw, buf, size);
903 }
bellard85571bc2004-11-07 18:04:02 +0000904}
905
Volker Rümelin69ac0782020-01-23 08:49:38 +0100906int AUD_get_buffer_size_out(SWVoiceOut *sw)
bellard85571bc2004-11-07 18:04:02 +0000907{
Volker Rümelin69ac0782020-01-23 08:49:38 +0100908 return sw->hw->samples * sw->hw->info.bytes_per_frame;
bellard85571bc2004-11-07 18:04:02 +0000909}
910
bellard1d14ffa2005-10-30 18:58:22 +0000911void AUD_set_active_out (SWVoiceOut *sw, int on)
bellard85571bc2004-11-07 18:04:02 +0000912{
bellard1d14ffa2005-10-30 18:58:22 +0000913 HWVoiceOut *hw;
914
915 if (!sw) {
bellard85571bc2004-11-07 18:04:02 +0000916 return;
bellard85571bc2004-11-07 18:04:02 +0000917 }
918
bellard85571bc2004-11-07 18:04:02 +0000919 hw = sw->hw;
bellard85571bc2004-11-07 18:04:02 +0000920 if (sw->active != on) {
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200921 AudioState *s = sw->s;
bellard1d14ffa2005-10-30 18:58:22 +0000922 SWVoiceOut *temp_sw;
bellardec36b692006-07-16 18:57:03 +0000923 SWVoiceCap *sc;
bellard1d14ffa2005-10-30 18:58:22 +0000924
bellard85571bc2004-11-07 18:04:02 +0000925 if (on) {
926 hw->pending_disable = 0;
927 if (!hw->enabled) {
928 hw->enabled = 1;
malc978dd632009-02-18 20:44:04 +0000929 if (s->vm_running) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200930 if (hw->pcm_ops->enable_out) {
931 hw->pcm_ops->enable_out(hw, true);
932 }
malc39deb1e2010-11-18 14:30:12 +0300933 audio_reset_timer (s);
malc978dd632009-02-18 20:44:04 +0000934 }
bellard1d14ffa2005-10-30 18:58:22 +0000935 }
Zhang Han6c6886b2021-01-15 09:24:25 +0800936 } else {
bellard1d14ffa2005-10-30 18:58:22 +0000937 if (hw->enabled) {
bellard85571bc2004-11-07 18:04:02 +0000938 int nb_active = 0;
bellard1d14ffa2005-10-30 18:58:22 +0000939
940 for (temp_sw = hw->sw_head.lh_first; temp_sw;
941 temp_sw = temp_sw->entries.le_next) {
942 nb_active += temp_sw->active != 0;
943 }
944
945 hw->pending_disable = nb_active == 1;
946 }
947 }
bellardec36b692006-07-16 18:57:03 +0000948
949 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
950 sc->sw.active = hw->enabled;
bellard8ead62c2006-07-04 16:51:32 +0000951 if (hw->enabled) {
bellardec36b692006-07-16 18:57:03 +0000952 audio_capture_maybe_changed (sc->cap, 1);
bellard8ead62c2006-07-04 16:51:32 +0000953 }
954 }
bellard1d14ffa2005-10-30 18:58:22 +0000955 sw->active = on;
956 }
957}
958
959void AUD_set_active_in (SWVoiceIn *sw, int on)
960{
961 HWVoiceIn *hw;
962
963 if (!sw) {
964 return;
965 }
966
967 hw = sw->hw;
968 if (sw->active != on) {
Kővágó, Zoltán526fb052019-08-19 01:06:46 +0200969 AudioState *s = sw->s;
bellard1d14ffa2005-10-30 18:58:22 +0000970 SWVoiceIn *temp_sw;
971
972 if (on) {
973 if (!hw->enabled) {
974 hw->enabled = 1;
malc978dd632009-02-18 20:44:04 +0000975 if (s->vm_running) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200976 if (hw->pcm_ops->enable_in) {
977 hw->pcm_ops->enable_in(hw, true);
978 }
malc39deb1e2010-11-18 14:30:12 +0300979 audio_reset_timer (s);
malc978dd632009-02-18 20:44:04 +0000980 }
bellard1d14ffa2005-10-30 18:58:22 +0000981 }
982 sw->total_hw_samples_acquired = hw->total_samples_captured;
Zhang Han6c6886b2021-01-15 09:24:25 +0800983 } else {
bellard1d14ffa2005-10-30 18:58:22 +0000984 if (hw->enabled) {
985 int nb_active = 0;
986
987 for (temp_sw = hw->sw_head.lh_first; temp_sw;
988 temp_sw = temp_sw->entries.le_next) {
989 nb_active += temp_sw->active != 0;
bellard85571bc2004-11-07 18:04:02 +0000990 }
991
992 if (nb_active == 1) {
bellard1d14ffa2005-10-30 18:58:22 +0000993 hw->enabled = 0;
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +0200994 if (hw->pcm_ops->enable_in) {
995 hw->pcm_ops->enable_in(hw, false);
996 }
bellard85571bc2004-11-07 18:04:02 +0000997 }
998 }
999 }
1000 sw->active = on;
1001 }
1002}
1003
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001004static size_t audio_get_avail (SWVoiceIn *sw)
bellard85571bc2004-11-07 18:04:02 +00001005{
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001006 size_t live;
bellard1d14ffa2005-10-30 18:58:22 +00001007
1008 if (!sw) {
1009 return 0;
1010 }
1011
1012 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001013 if (audio_bug(__func__, live > sw->hw->conv_buf.size)) {
1014 dolog("live=%zu sw->hw->conv_buf.size=%zu\n", live,
1015 sw->hw->conv_buf.size);
Volker Rümelin12f4abf2022-09-17 15:16:25 +02001016 return 0;
bellard1d14ffa2005-10-30 18:58:22 +00001017 }
1018
1019 ldebug (
Volker Rümelina9ea5672023-02-24 20:05:52 +01001020 "%s: get_avail live %zu frontend frames %u\n",
bellardc0fe3822005-11-05 18:55:28 +00001021 SW_NAME (sw),
Volker Rümelina9ea5672023-02-24 20:05:52 +01001022 live, st_rate_frames_out(sw->rate, live)
bellard1d14ffa2005-10-30 18:58:22 +00001023 );
1024
Volker Rümelin0724c572022-09-23 20:36:38 +02001025 return live;
bellard1d14ffa2005-10-30 18:58:22 +00001026}
1027
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001028static size_t audio_get_free(SWVoiceOut *sw)
bellard1d14ffa2005-10-30 18:58:22 +00001029{
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001030 size_t live, dead;
bellard1d14ffa2005-10-30 18:58:22 +00001031
1032 if (!sw) {
1033 return 0;
1034 }
1035
1036 live = sw->total_hw_samples_mixed;
1037
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001038 if (audio_bug(__func__, live > sw->hw->mix_buf.size)) {
1039 dolog("live=%zu sw->hw->mix_buf.size=%zu\n", live,
1040 sw->hw->mix_buf.size);
Volker Rümelin12f4abf2022-09-17 15:16:25 +02001041 return 0;
bellard1d14ffa2005-10-30 18:58:22 +00001042 }
1043
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001044 dead = sw->hw->mix_buf.size - live;
bellard1d14ffa2005-10-30 18:58:22 +00001045
1046#ifdef DEBUG_OUT
Volker Rümelin1a01df32023-02-24 20:05:49 +01001047 dolog("%s: get_free live %zu dead %zu frontend frames %u\n",
1048 SW_NAME(sw), live, dead, st_rate_frames_in(sw->rate, dead));
bellard1d14ffa2005-10-30 18:58:22 +00001049#endif
1050
Volker Rümelin98334382022-03-01 20:13:06 +01001051 return dead;
bellard1d14ffa2005-10-30 18:58:22 +00001052}
1053
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001054static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos,
1055 size_t samples)
bellard8ead62c2006-07-04 16:51:32 +00001056{
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001057 size_t n;
bellard8ead62c2006-07-04 16:51:32 +00001058
1059 if (hw->enabled) {
bellardec36b692006-07-16 18:57:03 +00001060 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +00001061
bellardec36b692006-07-16 18:57:03 +00001062 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1063 SWVoiceOut *sw = &sc->sw;
Volker Rümelin671cca32023-02-24 20:05:47 +01001064 size_t rpos2 = rpos;
bellard8ead62c2006-07-04 16:51:32 +00001065
1066 n = samples;
1067 while (n) {
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001068 size_t till_end_of_hw = hw->mix_buf.size - rpos2;
Volker Rümelin671cca32023-02-24 20:05:47 +01001069 size_t to_read = MIN(till_end_of_hw, n);
1070 size_t live, frames_in, frames_out;
bellard8ead62c2006-07-04 16:51:32 +00001071
Volker Rümelin2c3f9a02023-02-24 20:05:42 +01001072 sw->resample_buf.buffer = hw->mix_buf.buffer + rpos2;
Volker Rümelin671cca32023-02-24 20:05:47 +01001073 sw->resample_buf.size = to_read;
1074 live = sw->total_hw_samples_mixed;
1075
1076 audio_pcm_sw_resample_out(sw,
1077 to_read, sw->hw->mix_buf.size - live,
1078 &frames_in, &frames_out);
1079
1080 sw->total_hw_samples_mixed += frames_out;
1081 sw->empty = sw->total_hw_samples_mixed == 0;
1082
1083 if (to_read - frames_in) {
1084 dolog("Could not mix %zu frames into a capture "
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001085 "buffer, mixed %zu\n",
Volker Rümelin671cca32023-02-24 20:05:47 +01001086 to_read, frames_in);
bellard8ead62c2006-07-04 16:51:32 +00001087 break;
1088 }
Volker Rümelin671cca32023-02-24 20:05:47 +01001089 n -= to_read;
1090 rpos2 = (rpos2 + to_read) % hw->mix_buf.size;
bellard8ead62c2006-07-04 16:51:32 +00001091 }
1092 }
1093 }
1094
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001095 n = MIN(samples, hw->mix_buf.size - rpos);
1096 mixeng_clear(hw->mix_buf.buffer + rpos, n);
1097 mixeng_clear(hw->mix_buf.buffer, samples - n);
bellard8ead62c2006-07-04 16:51:32 +00001098}
1099
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001100static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live)
1101{
1102 size_t clipped = 0;
1103
1104 while (live) {
Volker Rümelinaec6d0d2020-09-20 19:17:23 +02001105 size_t size = live * hw->info.bytes_per_frame;
1106 size_t decr, proc;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001107 void *buf = hw->pcm_ops->get_buffer_out(hw, &size);
Volker Rümelin4c3356f2020-09-20 19:17:22 +02001108
1109 if (size == 0) {
Kővágó, Zoltánfb35c2c2020-02-03 00:02:23 +01001110 break;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001111 }
1112
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +02001113 decr = MIN(size / hw->info.bytes_per_frame, live);
Volker Rümelin4c3356f2020-09-20 19:17:22 +02001114 if (buf) {
1115 audio_pcm_hw_clip_out(hw, buf, decr);
1116 }
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +02001117 proc = hw->pcm_ops->put_buffer_out(hw, buf,
1118 decr * hw->info.bytes_per_frame) /
1119 hw->info.bytes_per_frame;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001120
1121 live -= proc;
1122 clipped += proc;
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001123 hw->mix_buf.pos = (hw->mix_buf.pos + proc) % hw->mix_buf.size;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001124
1125 if (proc == 0 || proc < decr) {
1126 break;
1127 }
1128 }
1129
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +01001130 if (hw->pcm_ops->run_buffer_out) {
1131 hw->pcm_ops->run_buffer_out(hw);
1132 }
1133
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001134 return clipped;
1135}
1136
bellardc0fe3822005-11-05 18:55:28 +00001137static void audio_run_out (AudioState *s)
bellard1d14ffa2005-10-30 18:58:22 +00001138{
1139 HWVoiceOut *hw = NULL;
1140 SWVoiceOut *sw;
1141
Volker Rümelin4d31ff32022-09-23 20:36:29 +02001142 while ((hw = audio_pcm_hw_find_any_enabled_out(s, hw))) {
1143 size_t played, live, prev_rpos;
1144 size_t hw_free = audio_pcm_hw_get_free(hw);
1145 int nb_live;
1146
1147 if (!audio_get_pdo_out(s->dev)->mixing_engine) {
Kővágó, Zoltán19306162019-10-13 21:57:59 +02001148 /* there is exactly 1 sw for each hw with no mixeng */
1149 sw = hw->sw_head.lh_first;
1150
1151 if (hw->pending_disable) {
1152 hw->enabled = 0;
1153 hw->pending_disable = 0;
1154 if (hw->pcm_ops->enable_out) {
1155 hw->pcm_ops->enable_out(hw, false);
1156 }
1157 }
1158
1159 if (sw->active) {
Volker Rümelin7099a6a2022-09-23 20:36:30 +02001160 sw->callback.fn(sw->callback.opaque,
1161 hw_free * sw->info.bytes_per_frame);
Kővágó, Zoltán19306162019-10-13 21:57:59 +02001162 }
Kővágó, Zoltán19306162019-10-13 21:57:59 +02001163
Volker Rümelindd052db2022-09-23 20:36:31 +02001164 if (hw->pcm_ops->run_buffer_out) {
1165 hw->pcm_ops->run_buffer_out(hw);
1166 }
1167
Volker Rümelin4d31ff32022-09-23 20:36:29 +02001168 continue;
1169 }
bellard1d14ffa2005-10-30 18:58:22 +00001170
Volker Rümelina806f952022-03-01 20:13:03 +01001171 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1172 if (sw->active) {
Volker Rümelin98334382022-03-01 20:13:06 +01001173 size_t sw_free = audio_get_free(sw);
1174 size_t free;
1175
1176 if (hw_free > sw->total_hw_samples_mixed) {
Volker Rümelin1a01df32023-02-24 20:05:49 +01001177 free = st_rate_frames_in(sw->rate,
Volker Rümelin98334382022-03-01 20:13:06 +01001178 MIN(sw_free, hw_free - sw->total_hw_samples_mixed));
1179 } else {
1180 free = 0;
1181 }
Volker Rümeline1e6a6f2023-02-24 20:05:53 +01001182 if (free > sw->resample_buf.pos) {
1183 free = MIN(free, sw->resample_buf.size)
1184 - sw->resample_buf.pos;
Volker Rümelinc4e59262022-09-23 20:36:37 +02001185 sw->callback.fn(sw->callback.opaque,
1186 free * sw->info.bytes_per_frame);
Volker Rümelina806f952022-03-01 20:13:03 +01001187 }
1188 }
1189 }
1190
malcbdff2532009-09-18 11:37:39 +04001191 live = audio_pcm_hw_get_live_out (hw, &nb_live);
bellard1d14ffa2005-10-30 18:58:22 +00001192 if (!nb_live) {
1193 live = 0;
bellard85571bc2004-11-07 18:04:02 +00001194 }
bellardc0fe3822005-11-05 18:55:28 +00001195
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001196 if (audio_bug(__func__, live > hw->mix_buf.size)) {
1197 dolog("live=%zu hw->mix_buf.size=%zu\n", live, hw->mix_buf.size);
Volker Rümelin12f4abf2022-09-17 15:16:25 +02001198 continue;
bellard85571bc2004-11-07 18:04:02 +00001199 }
bellard1d14ffa2005-10-30 18:58:22 +00001200
1201 if (hw->pending_disable && !nb_live) {
bellardec36b692006-07-16 18:57:03 +00001202 SWVoiceCap *sc;
bellard1d14ffa2005-10-30 18:58:22 +00001203#ifdef DEBUG_OUT
1204 dolog ("Disabling voice\n");
1205#endif
1206 hw->enabled = 0;
1207 hw->pending_disable = 0;
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +02001208 if (hw->pcm_ops->enable_out) {
1209 hw->pcm_ops->enable_out(hw, false);
1210 }
bellardec36b692006-07-16 18:57:03 +00001211 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1212 sc->sw.active = 0;
1213 audio_recalc_and_notify_capture (sc->cap);
bellard8ead62c2006-07-04 16:51:32 +00001214 }
bellard1d14ffa2005-10-30 18:58:22 +00001215 continue;
1216 }
1217
1218 if (!live) {
Volker Rümelina8a98cf2020-09-20 19:17:27 +02001219 if (hw->pcm_ops->run_buffer_out) {
1220 hw->pcm_ops->run_buffer_out(hw);
1221 }
bellard1d14ffa2005-10-30 18:58:22 +00001222 continue;
1223 }
1224
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001225 prev_rpos = hw->mix_buf.pos;
Kővágó, Zoltán3f5bbfc2019-09-19 23:24:19 +02001226 played = audio_pcm_hw_run_out(hw, live);
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +03001227 replay_audio_out(&played);
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001228 if (audio_bug(__func__, hw->mix_buf.pos >= hw->mix_buf.size)) {
1229 dolog("hw->mix_buf.pos=%zu hw->mix_buf.size=%zu played=%zu\n",
1230 hw->mix_buf.pos, hw->mix_buf.size, played);
1231 hw->mix_buf.pos = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001232 }
1233
1234#ifdef DEBUG_OUT
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001235 dolog("played=%zu\n", played);
bellard1d14ffa2005-10-30 18:58:22 +00001236#endif
1237
1238 if (played) {
1239 hw->ts_helper += played;
bellard8ead62c2006-07-04 16:51:32 +00001240 audio_capture_mix_and_clear (hw, prev_rpos, played);
bellard1d14ffa2005-10-30 18:58:22 +00001241 }
1242
1243 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
bellard1d14ffa2005-10-30 18:58:22 +00001244 if (!sw->active && sw->empty) {
1245 continue;
1246 }
1247
Alistair Francis470bcab2018-02-03 09:43:02 +01001248 if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) {
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001249 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1250 played, sw->total_hw_samples_mixed);
Volker Rümelin12f4abf2022-09-17 15:16:25 +02001251 played = sw->total_hw_samples_mixed;
bellard1d14ffa2005-10-30 18:58:22 +00001252 }
1253
1254 sw->total_hw_samples_mixed -= played;
1255
1256 if (!sw->total_hw_samples_mixed) {
1257 sw->empty = 1;
bellard1d14ffa2005-10-30 18:58:22 +00001258 }
bellard85571bc2004-11-07 18:04:02 +00001259 }
1260 }
bellard1d14ffa2005-10-30 18:58:22 +00001261}
1262
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001263static size_t audio_pcm_hw_run_in(HWVoiceIn *hw, size_t samples)
1264{
1265 size_t conv = 0;
1266
Volker Rümelina2893c82021-01-10 11:02:24 +01001267 if (hw->pcm_ops->run_buffer_in) {
1268 hw->pcm_ops->run_buffer_in(hw);
1269 }
1270
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001271 while (samples) {
1272 size_t proc;
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +02001273 size_t size = samples * hw->info.bytes_per_frame;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001274 void *buf = hw->pcm_ops->get_buffer_in(hw, &size);
1275
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +02001276 assert(size % hw->info.bytes_per_frame == 0);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001277 if (size == 0) {
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001278 break;
1279 }
1280
Volker Rümelin251f1542022-03-01 20:12:59 +01001281 proc = audio_pcm_hw_conv_in(hw, buf, size / hw->info.bytes_per_frame);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001282
1283 samples -= proc;
1284 conv += proc;
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +02001285 hw->pcm_ops->put_buffer_in(hw, buf, proc * hw->info.bytes_per_frame);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001286 }
1287
1288 return conv;
1289}
1290
bellardc0fe3822005-11-05 18:55:28 +00001291static void audio_run_in (AudioState *s)
bellard1d14ffa2005-10-30 18:58:22 +00001292{
1293 HWVoiceIn *hw = NULL;
1294
Kővágó, Zoltán19306162019-10-13 21:57:59 +02001295 if (!audio_get_pdo_in(s->dev)->mixing_engine) {
1296 while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
1297 /* there is exactly 1 sw for each hw with no mixeng */
1298 SWVoiceIn *sw = hw->sw_head.lh_first;
1299 if (sw->active) {
1300 sw->callback.fn(sw->callback.opaque, INT_MAX);
1301 }
1302 }
1303 return;
1304 }
1305
Kővágó, Zoltán526fb052019-08-19 01:06:46 +02001306 while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
bellard1d14ffa2005-10-30 18:58:22 +00001307 SWVoiceIn *sw;
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001308 size_t captured = 0, min;
bellard1d14ffa2005-10-30 18:58:22 +00001309
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +03001310 if (replay_mode != REPLAY_MODE_PLAY) {
Kővágó, Zoltán3f5bbfc2019-09-19 23:24:19 +02001311 captured = audio_pcm_hw_run_in(
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001312 hw, hw->conv_buf.size - audio_pcm_hw_get_live_in(hw));
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +03001313 }
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001314 replay_audio_in(&captured, hw->conv_buf.buffer, &hw->conv_buf.pos,
1315 hw->conv_buf.size);
bellard1d14ffa2005-10-30 18:58:22 +00001316
1317 min = audio_pcm_hw_find_min_in (hw);
1318 hw->total_samples_captured += captured - min;
1319 hw->ts_helper += captured;
1320
1321 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1322 sw->total_hw_samples_acquired -= min;
1323
1324 if (sw->active) {
Volker Rümelin0724c572022-09-23 20:36:38 +02001325 size_t sw_avail = audio_get_avail(sw);
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001326 size_t avail;
bellard1d14ffa2005-10-30 18:58:22 +00001327
Volker Rümelina9ea5672023-02-24 20:05:52 +01001328 avail = st_rate_frames_out(sw->rate, sw_avail);
bellard1d14ffa2005-10-30 18:58:22 +00001329 if (avail > 0) {
Volker Rümelina9ea5672023-02-24 20:05:52 +01001330 avail = MIN(avail, sw->resample_buf.size);
Volker Rümelin0724c572022-09-23 20:36:38 +02001331 sw->callback.fn(sw->callback.opaque,
1332 avail * sw->info.bytes_per_frame);
bellard1d14ffa2005-10-30 18:58:22 +00001333 }
1334 }
1335 }
1336 }
1337}
1338
bellard8ead62c2006-07-04 16:51:32 +00001339static void audio_run_capture (AudioState *s)
1340{
1341 CaptureVoiceOut *cap;
1342
1343 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001344 size_t live, rpos, captured;
bellard8ead62c2006-07-04 16:51:32 +00001345 HWVoiceOut *hw = &cap->hw;
1346 SWVoiceOut *sw;
1347
malcbdff2532009-09-18 11:37:39 +04001348 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001349 rpos = hw->mix_buf.pos;
bellard8ead62c2006-07-04 16:51:32 +00001350 while (live) {
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001351 size_t left = hw->mix_buf.size - rpos;
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001352 size_t to_capture = MIN(live, left);
malc1ea879e2008-12-03 22:48:44 +00001353 struct st_sample *src;
bellard8ead62c2006-07-04 16:51:32 +00001354 struct capture_callback *cb;
1355
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001356 src = hw->mix_buf.buffer + rpos;
bellard8ead62c2006-07-04 16:51:32 +00001357 hw->clip (cap->buf, src, to_capture);
1358 mixeng_clear (src, to_capture);
1359
1360 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1361 cb->ops.capture (cb->opaque, cap->buf,
Kővágó, Zoltán2b9cce82019-10-13 21:58:02 +02001362 to_capture * hw->info.bytes_per_frame);
bellard8ead62c2006-07-04 16:51:32 +00001363 }
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001364 rpos = (rpos + to_capture) % hw->mix_buf.size;
bellard8ead62c2006-07-04 16:51:32 +00001365 live -= to_capture;
1366 }
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001367 hw->mix_buf.pos = rpos;
bellard8ead62c2006-07-04 16:51:32 +00001368
1369 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1370 if (!sw->active && sw->empty) {
1371 continue;
1372 }
1373
Alistair Francis470bcab2018-02-03 09:43:02 +01001374 if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) {
Kővágó, Zoltán75204622019-08-19 01:06:58 +02001375 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1376 captured, sw->total_hw_samples_mixed);
Volker Rümelin12f4abf2022-09-17 15:16:25 +02001377 captured = sw->total_hw_samples_mixed;
bellard8ead62c2006-07-04 16:51:32 +00001378 }
1379
1380 sw->total_hw_samples_mixed -= captured;
1381 sw->empty = sw->total_hw_samples_mixed == 0;
1382 }
1383 }
1384}
1385
Kővágó, Zoltán18e2c172019-08-19 01:06:55 +02001386void audio_run(AudioState *s, const char *msg)
bellard571ec3d2005-11-20 16:24:34 +00001387{
Kővágó, Zoltán18e2c172019-08-19 01:06:55 +02001388 audio_run_out(s);
1389 audio_run_in(s);
1390 audio_run_capture(s);
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001391
malc713a98f2009-09-12 02:28:45 +04001392#ifdef DEBUG_POLL
1393 {
1394 static double prevtime;
1395 double currtime;
1396 struct timeval tv;
bellard571ec3d2005-11-20 16:24:34 +00001397
malc713a98f2009-09-12 02:28:45 +04001398 if (gettimeofday (&tv, NULL)) {
1399 perror ("audio_run: gettimeofday");
1400 return;
1401 }
1402
1403 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1404 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1405 prevtime = currtime;
1406 }
1407#endif
bellard571ec3d2005-11-20 16:24:34 +00001408}
1409
Volker Rümelina2893c82021-01-10 11:02:24 +01001410void audio_generic_run_buffer_in(HWVoiceIn *hw)
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001411{
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001412 if (unlikely(!hw->buf_emul)) {
Volker Rümelin1d8549a2021-01-10 11:02:26 +01001413 hw->size_emul = hw->samples * hw->info.bytes_per_frame;
1414 hw->buf_emul = g_malloc(hw->size_emul);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001415 hw->pos_emul = hw->pending_emul = 0;
1416 }
1417
1418 while (hw->pending_emul < hw->size_emul) {
1419 size_t read_len = MIN(hw->size_emul - hw->pos_emul,
1420 hw->size_emul - hw->pending_emul);
1421 size_t read = hw->pcm_ops->read(hw, hw->buf_emul + hw->pos_emul,
1422 read_len);
1423 hw->pending_emul += read;
Volker Rümelin7ffc90f2019-11-19 07:58:49 +01001424 hw->pos_emul = (hw->pos_emul + read) % hw->size_emul;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001425 if (read < read_len) {
1426 break;
1427 }
1428 }
Volker Rümelina2893c82021-01-10 11:02:24 +01001429}
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001430
Volker Rümelina2893c82021-01-10 11:02:24 +01001431void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size)
1432{
Volker Rümelin18404ff2022-03-01 20:12:57 +01001433 size_t start;
Volker Rümelina2893c82021-01-10 11:02:24 +01001434
Volker Rümelin18404ff2022-03-01 20:12:57 +01001435 start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul);
1436 assert(start < hw->size_emul);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001437
Volker Rümelin599eac42020-01-23 08:49:43 +01001438 *size = MIN(*size, hw->pending_emul);
1439 *size = MIN(*size, hw->size_emul - start);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001440 return hw->buf_emul + start;
1441}
1442
1443void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)
1444{
1445 assert(size <= hw->pending_emul);
1446 hw->pending_emul -= size;
1447}
1448
Volker Rümelin98334382022-03-01 20:13:06 +01001449size_t audio_generic_buffer_get_free(HWVoiceOut *hw)
1450{
1451 if (hw->buf_emul) {
1452 return hw->size_emul - hw->pending_emul;
1453 } else {
1454 return hw->samples * hw->info.bytes_per_frame;
1455 }
1456}
1457
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +01001458void audio_generic_run_buffer_out(HWVoiceOut *hw)
1459{
1460 while (hw->pending_emul) {
Volker Rümelin18404ff2022-03-01 20:12:57 +01001461 size_t write_len, written, start;
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +01001462
Volker Rümelin18404ff2022-03-01 20:12:57 +01001463 start = audio_ring_posb(hw->pos_emul, hw->pending_emul, hw->size_emul);
1464 assert(start < hw->size_emul);
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +01001465
1466 write_len = MIN(hw->pending_emul, hw->size_emul - start);
1467
1468 written = hw->pcm_ops->write(hw, hw->buf_emul + start, write_len);
1469 hw->pending_emul -= written;
1470
1471 if (written < write_len) {
1472 break;
1473 }
1474 }
1475}
1476
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001477void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size)
1478{
1479 if (unlikely(!hw->buf_emul)) {
Volker Rümelin1d8549a2021-01-10 11:02:26 +01001480 hw->size_emul = hw->samples * hw->info.bytes_per_frame;
1481 hw->buf_emul = g_malloc(hw->size_emul);
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001482 hw->pos_emul = hw->pending_emul = 0;
1483 }
1484
1485 *size = MIN(hw->size_emul - hw->pending_emul,
1486 hw->size_emul - hw->pos_emul);
1487 return hw->buf_emul + hw->pos_emul;
1488}
1489
Volker Rümelinfdc8c5f2020-01-23 08:49:39 +01001490size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size)
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001491{
1492 assert(buf == hw->buf_emul + hw->pos_emul &&
1493 size + hw->pending_emul <= hw->size_emul);
1494
1495 hw->pending_emul += size;
1496 hw->pos_emul = (hw->pos_emul + size) % hw->size_emul;
1497
1498 return size;
1499}
1500
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001501size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size)
1502{
Volker Rümelin2d882302020-09-20 19:17:26 +02001503 size_t total = 0;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001504
Volker Rümelin98334382022-03-01 20:13:06 +01001505 if (hw->pcm_ops->buffer_get_free) {
1506 size_t free = hw->pcm_ops->buffer_get_free(hw);
1507
1508 size = MIN(size, free);
1509 }
1510
Volker Rümelin2d882302020-09-20 19:17:26 +02001511 while (total < size) {
1512 size_t dst_size = size - total;
1513 size_t copy_size, proc;
1514 void *dst = hw->pcm_ops->get_buffer_out(hw, &dst_size);
1515
1516 if (dst_size == 0) {
1517 break;
1518 }
1519
1520 copy_size = MIN(size - total, dst_size);
1521 if (dst) {
1522 memcpy(dst, (char *)buf + total, copy_size);
1523 }
1524 proc = hw->pcm_ops->put_buffer_out(hw, dst, copy_size);
1525 total += proc;
1526
1527 if (proc == 0 || proc < copy_size) {
1528 break;
1529 }
1530 }
1531
Volker Rümelin2d882302020-09-20 19:17:26 +02001532 return total;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001533}
1534
1535size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size)
1536{
Volker Rümelinb9896dc2020-09-20 19:17:24 +02001537 size_t total = 0;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001538
Volker Rümelina2893c82021-01-10 11:02:24 +01001539 if (hw->pcm_ops->run_buffer_in) {
1540 hw->pcm_ops->run_buffer_in(hw);
1541 }
1542
Volker Rümelinb9896dc2020-09-20 19:17:24 +02001543 while (total < size) {
1544 size_t src_size = size - total;
1545 void *src = hw->pcm_ops->get_buffer_in(hw, &src_size);
Volker Rümelin8d1439b2020-04-05 09:50:17 +02001546
Volker Rümelinb9896dc2020-09-20 19:17:24 +02001547 if (src_size == 0) {
Volker Rümelinb9896dc2020-09-20 19:17:24 +02001548 break;
1549 }
1550
1551 memcpy((char *)buf + total, src, src_size);
1552 hw->pcm_ops->put_buffer_in(hw, src, src_size);
1553 total += src_size;
1554 }
1555
1556 return total;
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001557}
1558
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001559static int audio_driver_init(AudioState *s, struct audio_driver *drv,
Martin Kletzandercb94ff52023-10-02 16:27:57 +02001560 Audiodev *dev, Error **errp)
bellard1d14ffa2005-10-30 18:58:22 +00001561{
Paolo Bonzinif6061732023-09-22 19:13:44 +02001562 Error *local_err = NULL;
1563
1564 s->drv_opaque = drv->init(dev, &local_err);
bellard1d14ffa2005-10-30 18:58:22 +00001565
bellardc0fe3822005-11-05 18:55:28 +00001566 if (s->drv_opaque) {
Kővágó, Zoltánff095e52019-09-19 23:24:09 +02001567 if (!drv->pcm_ops->get_buffer_in) {
1568 drv->pcm_ops->get_buffer_in = audio_generic_get_buffer_in;
1569 drv->pcm_ops->put_buffer_in = audio_generic_put_buffer_in;
1570 }
1571 if (!drv->pcm_ops->get_buffer_out) {
1572 drv->pcm_ops->get_buffer_out = audio_generic_get_buffer_out;
1573 drv->pcm_ops->put_buffer_out = audio_generic_put_buffer_out;
1574 }
1575
Paolo Bonzini5c63d142023-09-22 18:36:28 +02001576 audio_init_nb_voices_out(s, drv, 1);
1577 audio_init_nb_voices_in(s, drv, 0);
bellardc0fe3822005-11-05 18:55:28 +00001578 s->drv = drv;
bellard85571bc2004-11-07 18:04:02 +00001579 return 0;
Zhang Han6c6886b2021-01-15 09:24:25 +08001580 } else {
Martin Kletzandercb94ff52023-10-02 16:27:57 +02001581 if (local_err) {
1582 error_propagate(errp, local_err);
Paolo Bonzinif6061732023-09-22 19:13:44 +02001583 } else {
Martin Kletzandercb94ff52023-10-02 16:27:57 +02001584 error_setg(errp, "Could not init `%s' audio driver", drv->name);
Gerd Hoffmannfb37ce92019-01-24 12:20:54 +01001585 }
bellard1d14ffa2005-10-30 18:58:22 +00001586 return -1;
1587 }
bellard85571bc2004-11-07 18:04:02 +00001588}
1589
Philippe Mathieu-Daudé538f0492021-01-11 16:20:20 +01001590static void audio_vm_change_state_handler (void *opaque, bool running,
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001591 RunState state)
bellard85571bc2004-11-07 18:04:02 +00001592{
bellardc0fe3822005-11-05 18:55:28 +00001593 AudioState *s = opaque;
bellard1d14ffa2005-10-30 18:58:22 +00001594 HWVoiceOut *hwo = NULL;
1595 HWVoiceIn *hwi = NULL;
bellard85571bc2004-11-07 18:04:02 +00001596
malc978dd632009-02-18 20:44:04 +00001597 s->vm_running = running;
Kővágó, Zoltán526fb052019-08-19 01:06:46 +02001598 while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +02001599 if (hwo->pcm_ops->enable_out) {
1600 hwo->pcm_ops->enable_out(hwo, running);
1601 }
bellard1d14ffa2005-10-30 18:58:22 +00001602 }
1603
Kővágó, Zoltán526fb052019-08-19 01:06:46 +02001604 while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +02001605 if (hwi->pcm_ops->enable_in) {
1606 hwi->pcm_ops->enable_in(hwi, running);
1607 }
bellard85571bc2004-11-07 18:04:02 +00001608 }
malc39deb1e2010-11-18 14:30:12 +03001609 audio_reset_timer (s);
bellard85571bc2004-11-07 18:04:02 +00001610}
1611
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001612static void free_audio_state(AudioState *s)
bellard85571bc2004-11-07 18:04:02 +00001613{
Marc-André Lureaua384c202016-08-01 15:23:43 +04001614 HWVoiceOut *hwo, *hwon;
1615 HWVoiceIn *hwi, *hwin;
bellard85571bc2004-11-07 18:04:02 +00001616
Kővágó, Zoltán526fb052019-08-19 01:06:46 +02001617 QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) {
bellardec36b692006-07-16 18:57:03 +00001618 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +00001619
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +02001620 if (hwo->enabled && hwo->pcm_ops->enable_out) {
1621 hwo->pcm_ops->enable_out(hwo, false);
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001622 }
bellard1d14ffa2005-10-30 18:58:22 +00001623 hwo->pcm_ops->fini_out (hwo);
bellard8ead62c2006-07-04 16:51:32 +00001624
bellardec36b692006-07-16 18:57:03 +00001625 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1626 CaptureVoiceOut *cap = sc->cap;
1627 struct capture_callback *cb;
1628
1629 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1630 cb->ops.destroy (cb->opaque);
1631 }
bellard8ead62c2006-07-04 16:51:32 +00001632 }
Marc-André Lureaua384c202016-08-01 15:23:43 +04001633 QLIST_REMOVE(hwo, entries);
bellard1d14ffa2005-10-30 18:58:22 +00001634 }
1635
Kővágó, Zoltán526fb052019-08-19 01:06:46 +02001636 QLIST_FOREACH_SAFE(hwi, &s->hw_head_in, entries, hwin) {
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +02001637 if (hwi->enabled && hwi->pcm_ops->enable_in) {
1638 hwi->pcm_ops->enable_in(hwi, false);
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001639 }
bellard1d14ffa2005-10-30 18:58:22 +00001640 hwi->pcm_ops->fini_in (hwi);
Marc-André Lureaua384c202016-08-01 15:23:43 +04001641 QLIST_REMOVE(hwi, entries);
bellard85571bc2004-11-07 18:04:02 +00001642 }
bellardc0fe3822005-11-05 18:55:28 +00001643
1644 if (s->drv) {
1645 s->drv->fini (s->drv_opaque);
Marc-André Lureaua384c202016-08-01 15:23:43 +04001646 s->drv = NULL;
bellardc0fe3822005-11-05 18:55:28 +00001647 }
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001648
1649 if (s->dev) {
1650 qapi_free_Audiodev(s->dev);
1651 s->dev = NULL;
1652 }
Kővágó, Zoltáne76ba192019-08-19 01:06:59 +02001653
1654 if (s->ts) {
1655 timer_free(s->ts);
1656 s->ts = NULL;
1657 }
1658
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001659 g_free(s);
1660}
1661
1662void audio_cleanup(void)
1663{
Paolo Bonzini22f84d42023-10-05 17:09:36 +02001664 default_audio_state = NULL;
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001665 while (!QTAILQ_EMPTY(&audio_states)) {
1666 AudioState *s = QTAILQ_FIRST(&audio_states);
1667 QTAILQ_REMOVE(&audio_states, s, list);
1668 free_audio_state(s);
1669 }
bellard85571bc2004-11-07 18:04:02 +00001670}
1671
Dr. David Alan Gilbertda77adb2021-08-09 18:09:56 +01001672static bool vmstate_audio_needed(void *opaque)
1673{
1674 /*
1675 * Never needed, this vmstate only exists in case
1676 * an old qemu sends it to us.
1677 */
1678 return false;
1679}
1680
Juan Quintelad959fce2009-12-02 11:49:34 +01001681static const VMStateDescription vmstate_audio = {
1682 .name = "audio",
1683 .version_id = 1,
1684 .minimum_version_id = 1,
Dr. David Alan Gilbertda77adb2021-08-09 18:09:56 +01001685 .needed = vmstate_audio_needed,
Richard Henderson9673d712023-12-21 14:16:44 +11001686 .fields = (const VMStateField[]) {
Juan Quintelad959fce2009-12-02 11:49:34 +01001687 VMSTATE_END_OF_LIST()
bellard1d14ffa2005-10-30 18:58:22 +00001688 }
Juan Quintelad959fce2009-12-02 11:49:34 +01001689};
bellard85571bc2004-11-07 18:04:02 +00001690
Paolo Bonzinic753bf42023-10-05 11:55:03 +02001691void audio_create_default_audiodevs(void)
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001692{
Paolo Bonzini69a80272023-09-05 11:41:05 +02001693 for (int i = 0; audio_prio_list[i]; i++) {
Paolo Bonzini69a80272023-09-05 11:41:05 +02001694 if (audio_driver_lookup(audio_prio_list[i])) {
1695 QDict *dict = qdict_new();
1696 Audiodev *dev = NULL;
Paolo Bonzini69a80272023-09-05 11:41:05 +02001697 Visitor *v;
1698
1699 qdict_put_str(dict, "driver", audio_prio_list[i]);
1700 qdict_put_str(dict, "id", "#default");
1701
1702 v = qobject_input_visitor_new_keyval(QOBJECT(dict));
1703 qobject_unref(dict);
1704 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
1705 visit_free(v);
1706
Paolo Bonzini8f527a32023-10-05 12:17:28 +02001707 audio_define_default(dev, &error_abort);
Paolo Bonzini69a80272023-09-05 11:41:05 +02001708 }
1709 }
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001710}
1711
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001712/*
1713 * if we have dev, this function was called because of an -audiodev argument =>
1714 * initialize a new state with it
1715 * if dev == NULL => legacy implicit initialization, return the already created
1716 * state or create a new one
1717 */
Martin Kletzandercb94ff52023-10-02 16:27:57 +02001718static AudioState *audio_init(Audiodev *dev, Error **errp)
bellard85571bc2004-11-07 18:04:02 +00001719{
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001720 static bool atexit_registered;
bellard85571bc2004-11-07 18:04:02 +00001721 int done = 0;
Paolo Bonzinie3299632023-09-22 17:46:28 +02001722 const char *drvname;
Paolo Bonzini417f8c82023-09-26 17:49:17 +02001723 VMChangeStateEntry *vmse;
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001724 AudioState *s;
Gerd Hoffmannd3893a32018-03-06 08:40:47 +01001725 struct audio_driver *driver;
Gerd Hoffmannf0c45552020-09-16 10:41:16 +02001726
Paolo Bonzinie3299632023-09-22 17:46:28 +02001727 s = g_new0(AudioState, 1);
1728
1729 QLIST_INIT (&s->hw_head_out);
1730 QLIST_INIT (&s->hw_head_in);
1731 QLIST_INIT (&s->cap_head);
1732 if (!atexit_registered) {
1733 atexit(audio_cleanup);
1734 atexit_registered = true;
1735 }
1736
1737 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
1738
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001739 if (dev) {
1740 /* -audiodev option */
Paolo Bonzinie3299632023-09-22 17:46:28 +02001741 s->dev = dev;
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001742 drvname = AudiodevDriver_str(dev->driver);
Paolo Bonzinie3299632023-09-22 17:46:28 +02001743 driver = audio_driver_lookup(drvname);
1744 if (driver) {
Martin Kletzandercb94ff52023-10-02 16:27:57 +02001745 done = !audio_driver_init(s, driver, dev, errp);
Paolo Bonzinie3299632023-09-22 17:46:28 +02001746 } else {
Michael Tokarev09a36152024-01-03 14:18:00 +03001747 error_setg(errp, "Unknown audio driver `%s'", drvname);
Kővágó, Zoltánaf2041e2019-08-19 01:06:51 +02001748 }
Paolo Bonzinie3299632023-09-22 17:46:28 +02001749 if (!done) {
Paolo Bonzini69a80272023-09-05 11:41:05 +02001750 goto out;
Paolo Bonzinie3299632023-09-22 17:46:28 +02001751 }
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001752 } else {
Paolo Bonzini22f84d42023-10-05 17:09:36 +02001753 assert(!default_audio_state);
Paolo Bonzini69a80272023-09-05 11:41:05 +02001754 for (;;) {
1755 AudiodevListEntry *e = QSIMPLEQ_FIRST(&default_audiodevs);
1756 if (!e) {
Martin Kletzandercb94ff52023-10-02 16:27:57 +02001757 error_setg(errp, "no default audio driver available");
Paolo Bonzini69a80272023-09-05 11:41:05 +02001758 goto out;
bellard1d14ffa2005-10-30 18:58:22 +00001759 }
Paolo Bonzini69a80272023-09-05 11:41:05 +02001760 s->dev = dev = e->dev;
Akihiko Odakie4b9d192023-11-20 20:28:02 +09001761 QSIMPLEQ_REMOVE_HEAD(&default_audiodevs, next);
1762 g_free(e);
Paolo Bonzini69a80272023-09-05 11:41:05 +02001763 drvname = AudiodevDriver_str(dev->driver);
1764 driver = audio_driver_lookup(drvname);
Martin Kletzandercb94ff52023-10-02 16:27:57 +02001765 if (!audio_driver_init(s, driver, dev, NULL)) {
Paolo Bonzini69a80272023-09-05 11:41:05 +02001766 break;
1767 }
Akihiko Odakie4b9d192023-11-20 20:28:02 +09001768 qapi_free_Audiodev(dev);
1769 s->dev = NULL;
bellard85571bc2004-11-07 18:04:02 +00001770 }
bellard85571bc2004-11-07 18:04:02 +00001771 }
bellard1d14ffa2005-10-30 18:58:22 +00001772
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001773 if (dev->timer_period <= 0) {
1774 s->period_ticks = 1;
Paul Brook0d9acba2009-05-12 12:02:38 +01001775 } else {
Volker Rümelin40ad46d2019-12-19 21:34:05 +01001776 s->period_ticks = dev->timer_period * (int64_t)SCALE_US;
bellard1d14ffa2005-10-30 18:58:22 +00001777 }
Paul Brook0d9acba2009-05-12 12:02:38 +01001778
Paolo Bonzini417f8c82023-09-26 17:49:17 +02001779 vmse = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1780 if (!vmse) {
Paul Brook0d9acba2009-05-12 12:02:38 +01001781 dolog ("warning: Could not register change state handler\n"
1782 "(Audio can continue looping even after stopping the VM)\n");
bellard1d14ffa2005-10-30 18:58:22 +00001783 }
1784
Marc-André Lureau0f957c52022-08-22 17:10:21 +04001785 QTAILQ_INSERT_TAIL(&audio_states, s, list);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001786 QLIST_INIT (&s->card_head);
Juan Quintelaa9500912023-10-20 11:07:29 +02001787 vmstate_register_any(NULL, &vmstate_audio, s);
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001788 return s;
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001789
Paolo Bonzini69a80272023-09-05 11:41:05 +02001790out:
1791 free_audio_state(s);
1792 return NULL;
bellard85571bc2004-11-07 18:04:02 +00001793}
bellard8ead62c2006-07-04 16:51:32 +00001794
Paolo Bonzini63a13c02023-10-05 18:42:54 +02001795AudioState *audio_get_default_audio_state(Error **errp)
1796{
1797 if (!default_audio_state) {
1798 default_audio_state = audio_init(NULL, errp);
1799 if (!default_audio_state) {
1800 if (!QSIMPLEQ_EMPTY(&audiodevs)) {
1801 error_append_hint(errp, "Perhaps you wanted to use -audio or set audiodev=%s?\n",
1802 QSIMPLEQ_FIRST(&audiodevs)->dev->id);
1803 }
1804 }
1805 }
1806
1807 return default_audio_state;
1808}
1809
Martin Kletzandercb94ff52023-10-02 16:27:57 +02001810bool AUD_register_card (const char *name, QEMUSoundCard *card, Error **errp)
malc1a7dafc2009-05-14 03:11:35 +04001811{
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001812 if (!card->state) {
Paolo Bonzini63a13c02023-10-05 18:42:54 +02001813 card->state = audio_get_default_audio_state(errp);
1814 if (!card->state) {
1815 return false;
Paolo Bonzinie3299632023-09-22 17:46:28 +02001816 }
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001817 }
1818
Anthony Liguori7267c092011-08-20 22:09:37 -05001819 card->name = g_strdup (name);
malc1a7dafc2009-05-14 03:11:35 +04001820 memset (&card->entries, 0, sizeof (card->entries));
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001821 QLIST_INSERT_HEAD(&card->state->card_head, card, entries);
Martin Kletzandercb94ff52023-10-02 16:27:57 +02001822
1823 return true;
malc1a7dafc2009-05-14 03:11:35 +04001824}
1825
1826void AUD_remove_card (QEMUSoundCard *card)
1827{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001828 QLIST_REMOVE (card, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001829 g_free (card->name);
malc1a7dafc2009-05-14 03:11:35 +04001830}
1831
Volker Rümelin33940dd2022-03-01 20:13:04 +01001832static struct audio_pcm_ops capture_pcm_ops;
malc1a7dafc2009-05-14 03:11:35 +04001833
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001834CaptureVoiceOut *AUD_add_capture(
1835 AudioState *s,
malc1ea879e2008-12-03 22:48:44 +00001836 struct audsettings *as,
bellard8ead62c2006-07-04 16:51:32 +00001837 struct audio_capture_ops *ops,
1838 void *cb_opaque
1839 )
1840{
1841 CaptureVoiceOut *cap;
1842 struct capture_callback *cb;
1843
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001844 if (!s) {
Martin Kletzanderaaa6a6f2022-04-25 10:21:57 +02001845 error_report("Capturing without setting an audiodev is not supported");
1846 abort();
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02001847 }
1848
Kővágó, Zoltán19306162019-10-13 21:57:59 +02001849 if (!audio_get_pdo_out(s->dev)->mixing_engine) {
1850 dolog("Can't capture with mixeng disabled\n");
1851 return NULL;
1852 }
1853
bellardec36b692006-07-16 18:57:03 +00001854 if (audio_validate_settings (as)) {
bellard8ead62c2006-07-04 16:51:32 +00001855 dolog ("Invalid settings were passed when trying to add capture\n");
1856 audio_print_settings (as);
Frediano Ziglioe8d85442019-02-25 15:43:35 +00001857 return NULL;
bellard8ead62c2006-07-04 16:51:32 +00001858 }
1859
Frediano Ziglioe8d85442019-02-25 15:43:35 +00001860 cb = g_malloc0(sizeof(*cb));
bellard8ead62c2006-07-04 16:51:32 +00001861 cb->ops = *ops;
1862 cb->opaque = cb_opaque;
1863
Kővágó, Zoltán526fb052019-08-19 01:06:46 +02001864 cap = audio_pcm_capture_find_specific(s, as);
bellard8ead62c2006-07-04 16:51:32 +00001865 if (cap) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001866 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
Zhang Han6c6886b2021-01-15 09:24:25 +08001867 } else {
bellard8ead62c2006-07-04 16:51:32 +00001868 HWVoiceOut *hw;
bellard8ead62c2006-07-04 16:51:32 +00001869
Frediano Ziglioe8d85442019-02-25 15:43:35 +00001870 cap = g_malloc0(sizeof(*cap));
bellard8ead62c2006-07-04 16:51:32 +00001871
1872 hw = &cap->hw;
Kővágó, Zoltán526fb052019-08-19 01:06:46 +02001873 hw->s = s;
Volker Rümelin33940dd2022-03-01 20:13:04 +01001874 hw->pcm_ops = &capture_pcm_ops;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001875 QLIST_INIT (&hw->sw_head);
1876 QLIST_INIT (&cap->cb_head);
bellard8ead62c2006-07-04 16:51:32 +00001877
1878 /* XXX find a more elegant way */
1879 hw->samples = 4096 * 4;
Kővágó, Zoltándc88e382019-09-19 23:24:20 +02001880 audio_pcm_hw_alloc_resources_out(hw);
bellard8ead62c2006-07-04 16:51:32 +00001881
bellardd929eba2006-07-04 21:47:22 +00001882 audio_pcm_init_info (&hw->info, as);
bellard8ead62c2006-07-04 16:51:32 +00001883
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001884 cap->buf = g_malloc0_n(hw->mix_buf.size, hw->info.bytes_per_frame);
bellard8ead62c2006-07-04 16:51:32 +00001885
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +01001886 if (hw->info.is_float) {
1887 hw->clip = mixeng_clip_float[hw->info.nchannels == 2];
1888 } else {
1889 hw->clip = mixeng_clip
1890 [hw->info.nchannels == 2]
1891 [hw->info.is_signed]
1892 [hw->info.swap_endianness]
1893 [audio_bits_to_index(hw->info.bits)];
1894 }
bellard8ead62c2006-07-04 16:51:32 +00001895
Blue Swirl72cf2d42009-09-12 07:36:22 +00001896 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1897 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
bellard8ead62c2006-07-04 16:51:32 +00001898
Kővágó, Zoltán526fb052019-08-19 01:06:46 +02001899 QLIST_FOREACH(hw, &s->hw_head_out, entries) {
malc1a7dafc2009-05-14 03:11:35 +04001900 audio_attach_capture (hw);
bellard8ead62c2006-07-04 16:51:32 +00001901 }
bellardec36b692006-07-16 18:57:03 +00001902 }
Paolo Bonzini417f8c82023-09-26 17:49:17 +02001903
1904 return cap;
bellardec36b692006-07-16 18:57:03 +00001905}
1906
1907void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1908{
1909 struct capture_callback *cb;
1910
1911 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1912 if (cb->opaque == cb_opaque) {
1913 cb->ops.destroy (cb_opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00001914 QLIST_REMOVE (cb, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001915 g_free (cb);
bellardec36b692006-07-16 18:57:03 +00001916
1917 if (!cap->cb_head.lh_first) {
1918 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
bellarda3c25992006-07-18 21:09:59 +00001919
bellardec36b692006-07-16 18:57:03 +00001920 while (sw) {
bellarda3c25992006-07-18 21:09:59 +00001921 SWVoiceCap *sc = (SWVoiceCap *) sw;
bellardec36b692006-07-16 18:57:03 +00001922#ifdef DEBUG_CAPTURE
1923 dolog ("freeing %s\n", sw->name);
1924#endif
bellarda3c25992006-07-18 21:09:59 +00001925
bellardec36b692006-07-16 18:57:03 +00001926 sw1 = sw->entries.le_next;
1927 if (sw->rate) {
1928 st_rate_stop (sw->rate);
1929 sw->rate = NULL;
1930 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001931 QLIST_REMOVE (sw, entries);
1932 QLIST_REMOVE (sc, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001933 g_free (sc);
bellardec36b692006-07-16 18:57:03 +00001934 sw = sw1;
1935 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00001936 QLIST_REMOVE (cap, entries);
Volker Rümelin8dbd3d12023-02-24 20:05:41 +01001937 g_free(cap->hw.mix_buf.buffer);
Gerd Hoffmann3268a842017-04-28 09:56:12 +02001938 g_free (cap->buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05001939 g_free (cap);
bellardec36b692006-07-16 18:57:03 +00001940 }
1941 return;
1942 }
bellard8ead62c2006-07-04 16:51:32 +00001943 }
1944}
balrog683efdc2008-05-04 10:21:03 +00001945
1946void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
1947{
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +02001948 Volume vol = { .mute = mute, .channels = 2, .vol = { lvol, rvol } };
1949 audio_set_volume_out(sw, &vol);
1950}
1951
1952void audio_set_volume_out(SWVoiceOut *sw, Volume *vol)
1953{
balrog683efdc2008-05-04 10:21:03 +00001954 if (sw) {
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02001955 HWVoiceOut *hw = sw->hw;
1956
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +02001957 sw->vol.mute = vol->mute;
1958 sw->vol.l = nominal_volume.l * vol->vol[0] / 255;
1959 sw->vol.r = nominal_volume.l * vol->vol[vol->channels > 1 ? 1 : 0] /
1960 255;
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02001961
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +02001962 if (hw->pcm_ops->volume_out) {
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +02001963 hw->pcm_ops->volume_out(hw, vol);
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02001964 }
balrog683efdc2008-05-04 10:21:03 +00001965 }
1966}
1967
1968void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
1969{
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +02001970 Volume vol = { .mute = mute, .channels = 2, .vol = { lvol, rvol } };
1971 audio_set_volume_in(sw, &vol);
1972}
1973
1974void audio_set_volume_in(SWVoiceIn *sw, Volume *vol)
1975{
balrog683efdc2008-05-04 10:21:03 +00001976 if (sw) {
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02001977 HWVoiceIn *hw = sw->hw;
1978
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +02001979 sw->vol.mute = vol->mute;
1980 sw->vol.l = nominal_volume.l * vol->vol[0] / 255;
1981 sw->vol.r = nominal_volume.r * vol->vol[vol->channels > 1 ? 1 : 0] /
1982 255;
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02001983
Kővágó, Zoltán571a8c52019-09-19 23:24:22 +02001984 if (hw->pcm_ops->volume_in) {
Kővágó, Zoltáncecc1e72019-10-13 21:58:01 +02001985 hw->pcm_ops->volume_in(hw, vol);
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02001986 }
balrog683efdc2008-05-04 10:21:03 +00001987 }
1988}
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001989
1990void audio_create_pdos(Audiodev *dev)
1991{
1992 switch (dev->driver) {
1993#define CASE(DRIVER, driver, pdo_name) \
1994 case AUDIODEV_DRIVER_##DRIVER: \
Markus Armbrusterceb19c82022-11-04 17:06:49 +01001995 if (!dev->u.driver.in) { \
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001996 dev->u.driver.in = g_malloc0( \
1997 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
Kővágó, Zoltán71830222019-03-08 23:34:15 +01001998 } \
Markus Armbrusterceb19c82022-11-04 17:06:49 +01001999 if (!dev->u.driver.out) { \
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002000 dev->u.driver.out = g_malloc0( \
Kővágó, Zoltán725662d2019-08-26 21:59:01 +02002001 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002002 } \
2003 break
2004
2005 CASE(NONE, none, );
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002006#ifdef CONFIG_AUDIO_ALSA
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002007 CASE(ALSA, alsa, Alsa);
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002008#endif
2009#ifdef CONFIG_AUDIO_COREAUDIO
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002010 CASE(COREAUDIO, coreaudio, Coreaudio);
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002011#endif
2012#ifdef CONFIG_DBUS_DISPLAY
Marc-André Lureau739362d2021-03-09 17:15:28 +04002013 CASE(DBUS, dbus, );
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002014#endif
2015#ifdef CONFIG_AUDIO_DSOUND
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002016 CASE(DSOUND, dsound, );
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002017#endif
2018#ifdef CONFIG_AUDIO_JACK
Geoffrey McRae2e445702020-04-29 15:53:58 +10002019 CASE(JACK, jack, Jack);
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002020#endif
2021#ifdef CONFIG_AUDIO_OSS
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002022 CASE(OSS, oss, Oss);
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002023#endif
2024#ifdef CONFIG_AUDIO_PA
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002025 CASE(PA, pa, Pa);
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002026#endif
Dorinda Basseyc2d3d1c2023-04-17 12:56:54 +02002027#ifdef CONFIG_AUDIO_PIPEWIRE
2028 CASE(PIPEWIRE, pipewire, Pipewire);
2029#endif
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002030#ifdef CONFIG_AUDIO_SDL
Volker Rümelin5a0926c2021-01-10 11:02:19 +01002031 CASE(SDL, sdl, Sdl);
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002032#endif
2033#ifdef CONFIG_AUDIO_SNDIO
Alexandre Ratchov663df1c2022-09-07 15:23:42 +02002034 CASE(SNDIO, sndio, );
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002035#endif
2036#ifdef CONFIG_SPICE
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002037 CASE(SPICE, spice, );
Daniel P. Berrangé7a92a852023-01-23 09:39:57 +01002038#endif
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002039 CASE(WAV, wav, );
2040
2041 case AUDIODEV_DRIVER__MAX:
2042 abort();
2043 };
2044}
2045
2046static void audio_validate_per_direction_opts(
2047 AudiodevPerDirectionOptions *pdo, Error **errp)
2048{
Kővágó, Zoltán19306162019-10-13 21:57:59 +02002049 if (!pdo->has_mixing_engine) {
2050 pdo->has_mixing_engine = true;
2051 pdo->mixing_engine = true;
2052 }
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002053 if (!pdo->has_fixed_settings) {
2054 pdo->has_fixed_settings = true;
Kővágó, Zoltán19306162019-10-13 21:57:59 +02002055 pdo->fixed_settings = pdo->mixing_engine;
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002056 }
2057 if (!pdo->fixed_settings &&
2058 (pdo->has_frequency || pdo->has_channels || pdo->has_format)) {
2059 error_setg(errp,
2060 "You can't use frequency, channels or format with fixed-settings=off");
2061 return;
2062 }
Kővágó, Zoltán19306162019-10-13 21:57:59 +02002063 if (!pdo->mixing_engine && pdo->fixed_settings) {
2064 error_setg(errp, "You can't use fixed-settings without mixeng");
2065 return;
2066 }
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002067
2068 if (!pdo->has_frequency) {
2069 pdo->has_frequency = true;
2070 pdo->frequency = 44100;
2071 }
2072 if (!pdo->has_channels) {
2073 pdo->has_channels = true;
2074 pdo->channels = 2;
2075 }
2076 if (!pdo->has_voices) {
2077 pdo->has_voices = true;
Kővágó, Zoltán19306162019-10-13 21:57:59 +02002078 pdo->voices = pdo->mixing_engine ? 1 : INT_MAX;
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002079 }
2080 if (!pdo->has_format) {
2081 pdo->has_format = true;
2082 pdo->format = AUDIO_FORMAT_S16;
2083 }
2084}
2085
2086static void audio_validate_opts(Audiodev *dev, Error **errp)
2087{
2088 Error *err = NULL;
2089
2090 audio_create_pdos(dev);
2091
2092 audio_validate_per_direction_opts(audio_get_pdo_in(dev), &err);
2093 if (err) {
2094 error_propagate(errp, err);
2095 return;
2096 }
2097
2098 audio_validate_per_direction_opts(audio_get_pdo_out(dev), &err);
2099 if (err) {
2100 error_propagate(errp, err);
2101 return;
2102 }
2103
2104 if (!dev->has_timer_period) {
2105 dev->has_timer_period = true;
2106 dev->timer_period = 10000; /* 100Hz -> 10ms */
2107 }
2108}
2109
Claudio Fontana5e03b6d2022-09-08 10:14:41 +02002110void audio_help(void)
2111{
2112 int i;
2113
2114 printf("Available audio drivers:\n");
2115
2116 for (i = 0; i < AUDIODEV_DRIVER__MAX; i++) {
2117 audio_driver *driver = audio_driver_lookup(AudiodevDriver_str(i));
2118 if (driver) {
2119 printf("%s\n", driver->name);
2120 }
2121 }
2122}
2123
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002124void audio_parse_option(const char *opt)
2125{
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002126 Audiodev *dev = NULL;
2127
Claudio Fontana5e03b6d2022-09-08 10:14:41 +02002128 if (is_help_option(opt)) {
2129 audio_help();
2130 exit(EXIT_SUCCESS);
2131 }
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002132 Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal);
2133 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
2134 visit_free(v);
2135
Paolo Bonzini039a6832022-04-27 12:27:46 +02002136 audio_define(dev);
2137}
2138
2139void audio_define(Audiodev *dev)
2140{
2141 AudiodevListEntry *e;
2142
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002143 audio_validate_opts(dev, &error_fatal);
2144
Markus Armbrusterb21e2382022-03-15 15:41:56 +01002145 e = g_new0(AudiodevListEntry, 1);
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002146 e->dev = dev;
2147 QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next);
2148}
2149
Paolo Bonzini8f527a32023-10-05 12:17:28 +02002150void audio_define_default(Audiodev *dev, Error **errp)
2151{
2152 AudiodevListEntry *e;
2153
2154 audio_validate_opts(dev, errp);
2155
2156 e = g_new0(AudiodevListEntry, 1);
2157 e->dev = dev;
2158 QSIMPLEQ_INSERT_TAIL(&default_audiodevs, e, next);
2159}
2160
Martin Kletzandercb94ff52023-10-02 16:27:57 +02002161void audio_init_audiodevs(void)
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002162{
2163 AudiodevListEntry *e;
2164
2165 QSIMPLEQ_FOREACH(e, &audiodevs, next) {
Martin Kletzandercb94ff52023-10-02 16:27:57 +02002166 audio_init(e->dev, &error_fatal);
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002167 }
2168}
2169
2170audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo)
2171{
2172 return (audsettings) {
2173 .freq = pdo->frequency,
2174 .nchannels = pdo->channels,
2175 .fmt = pdo->format,
2176 .endianness = AUDIO_HOST_ENDIANNESS,
2177 };
2178}
2179
2180int audioformat_bytes_per_sample(AudioFormat fmt)
2181{
2182 switch (fmt) {
2183 case AUDIO_FORMAT_U8:
2184 case AUDIO_FORMAT_S8:
2185 return 1;
2186
2187 case AUDIO_FORMAT_U16:
2188 case AUDIO_FORMAT_S16:
2189 return 2;
2190
2191 case AUDIO_FORMAT_U32:
2192 case AUDIO_FORMAT_S32:
Kővágó, Zoltáned2a4a72020-02-02 20:38:07 +01002193 case AUDIO_FORMAT_F32:
Kővágó, Zoltán71830222019-03-08 23:34:15 +01002194 return 4;
2195
2196 case AUDIO_FORMAT__MAX:
2197 ;
2198 }
2199 abort();
2200}
2201
2202
2203/* frames = freq * usec / 1e6 */
2204int audio_buffer_frames(AudiodevPerDirectionOptions *pdo,
2205 audsettings *as, int def_usecs)
2206{
2207 uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs;
2208 return (as->freq * usecs + 500000) / 1000000;
2209}
2210
2211/* samples = channels * frames = channels * freq * usec / 1e6 */
2212int audio_buffer_samples(AudiodevPerDirectionOptions *pdo,
2213 audsettings *as, int def_usecs)
2214{
2215 return as->nchannels * audio_buffer_frames(pdo, as, def_usecs);
2216}
2217
2218/*
2219 * bytes = bytes_per_sample * samples =
2220 * bytes_per_sample * channels * freq * usec / 1e6
2221 */
2222int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
2223 audsettings *as, int def_usecs)
2224{
2225 return audio_buffer_samples(pdo, as, def_usecs) *
2226 audioformat_bytes_per_sample(as->fmt);
2227}
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02002228
Paolo Bonzini176adaf2023-09-22 17:29:19 +02002229AudioState *audio_state_by_name(const char *name, Error **errp)
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02002230{
2231 AudioState *s;
2232 QTAILQ_FOREACH(s, &audio_states, list) {
2233 assert(s->dev);
2234 if (strcmp(name, s->dev->id) == 0) {
2235 return s;
2236 }
2237 }
Paolo Bonzini176adaf2023-09-22 17:29:19 +02002238 error_setg(errp, "audiodev '%s' not found", name);
Kővágó, Zoltánecd97e92019-08-19 01:06:47 +02002239 return NULL;
2240}
2241
2242const char *audio_get_id(QEMUSoundCard *card)
2243{
2244 if (card->state) {
2245 assert(card->state->dev);
2246 return card->state->dev->id;
2247 } else {
2248 return "";
2249 }
2250}
Kővágó, Zoltán857271a2019-09-19 23:24:21 +02002251
Volker Rümelin37a54d02021-05-17 21:46:03 +02002252const char *audio_application_name(void)
2253{
2254 const char *vm_name;
2255
2256 vm_name = qemu_get_vm_name();
2257 return vm_name ? vm_name : "qemu";
2258}
2259
Kővágó, Zoltán857271a2019-09-19 23:24:21 +02002260void audio_rate_start(RateCtl *rate)
2261{
2262 memset(rate, 0, sizeof(RateCtl));
2263 rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2264}
2265
Volker Rümelin02732642022-09-23 20:36:33 +02002266size_t audio_rate_peek_bytes(RateCtl *rate, struct audio_pcm_info *info)
Kővágó, Zoltán857271a2019-09-19 23:24:21 +02002267{
2268 int64_t now;
2269 int64_t ticks;
2270 int64_t bytes;
Volker Rümelin02732642022-09-23 20:36:33 +02002271 int64_t frames;
Kővágó, Zoltán857271a2019-09-19 23:24:21 +02002272
2273 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2274 ticks = now - rate->start_ticks;
2275 bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
Volker Rümelin02732642022-09-23 20:36:33 +02002276 frames = (bytes - rate->bytes_sent) / info->bytes_per_frame;
2277 if (frames < 0 || frames > 65536) {
2278 AUD_log(NULL, "Resetting rate control (%" PRId64 " frames)\n", frames);
Kővágó, Zoltán857271a2019-09-19 23:24:21 +02002279 audio_rate_start(rate);
Volker Rümelin02732642022-09-23 20:36:33 +02002280 frames = 0;
Kővágó, Zoltán857271a2019-09-19 23:24:21 +02002281 }
2282
Volker Rümelin02732642022-09-23 20:36:33 +02002283 return frames * info->bytes_per_frame;
2284}
2285
2286void audio_rate_add_bytes(RateCtl *rate, size_t bytes_used)
2287{
2288 rate->bytes_sent += bytes_used;
2289}
2290
Volker Rümelin613fe022022-09-23 20:36:36 +02002291size_t audio_rate_get_bytes(RateCtl *rate, struct audio_pcm_info *info,
Volker Rümelin02732642022-09-23 20:36:33 +02002292 size_t bytes_avail)
2293{
2294 size_t bytes;
2295
2296 bytes = audio_rate_peek_bytes(rate, info);
2297 bytes = MIN(bytes, bytes_avail);
2298 audio_rate_add_bytes(rate, bytes);
2299
2300 return bytes;
Kővágó, Zoltán857271a2019-09-19 23:24:21 +02002301}
Daniel P. Berrangé637d1802023-01-23 09:39:56 +01002302
2303AudiodevList *qmp_query_audiodevs(Error **errp)
2304{
2305 AudiodevList *ret = NULL;
2306 AudiodevListEntry *e;
2307 QSIMPLEQ_FOREACH(e, &audiodevs, next) {
2308 QAPI_LIST_PREPEND(ret, QAPI_CLONE(Audiodev, e->dev));
2309 }
2310 return ret;
2311}