blob: 1ace47f51071d615d47e17fd5ce4a86392d67944 [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 */
Peter Maydell6086a562016-01-18 17:33:52 +000024#include "qemu/osdep.h"
pbrook87ecb682007-11-17 17:14:51 +000025#include "hw/hw.h"
26#include "audio.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010027#include "monitor/monitor.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/timer.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010029#include "sysemu/sysemu.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020030#include "qemu/cutils.h"
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +030031#include "sysemu/replay.h"
Gerd Hoffmann1a961e72018-07-02 18:15:24 +020032#include "trace.h"
bellard85571bc2004-11-07 18:04:02 +000033
bellard1d14ffa2005-10-30 18:58:22 +000034#define AUDIO_CAP "audio"
35#include "audio_int.h"
bellard85571bc2004-11-07 18:04:02 +000036
bellard1d14ffa2005-10-30 18:58:22 +000037/* #define DEBUG_LIVE */
38/* #define DEBUG_OUT */
bellard8ead62c2006-07-04 16:51:32 +000039/* #define DEBUG_CAPTURE */
malc713a98f2009-09-12 02:28:45 +040040/* #define DEBUG_POLL */
bellard1d14ffa2005-10-30 18:58:22 +000041
bellardc0fe3822005-11-05 18:55:28 +000042#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
43
Juan Quintela2358a492009-07-27 16:13:25 +020044
45/* Order of CONFIG_AUDIO_DRIVERS is import.
46 The 1st one is the one used by default, that is the reason
47 that we generate the list.
48*/
Gerd Hoffmannd3893a32018-03-06 08:40:47 +010049static const char *audio_prio_list[] = {
50 "spice",
Juan Quintela2358a492009-07-27 16:13:25 +020051 CONFIG_AUDIO_DRIVERS
Gerd Hoffmannd3893a32018-03-06 08:40:47 +010052 "none",
53 "wav",
bellard1d14ffa2005-10-30 18:58:22 +000054};
bellard85571bc2004-11-07 18:04:02 +000055
Gerd Hoffmannd3893a32018-03-06 08:40:47 +010056static QLIST_HEAD(, audio_driver) audio_drivers;
57
58void audio_driver_register(audio_driver *drv)
59{
60 QLIST_INSERT_HEAD(&audio_drivers, drv, next);
61}
62
63audio_driver *audio_driver_lookup(const char *name)
64{
65 struct audio_driver *d;
66
67 QLIST_FOREACH(d, &audio_drivers, next) {
68 if (strcmp(name, d->name) == 0) {
69 return d;
70 }
71 }
Gerd Hoffmann65ba8692018-03-06 08:40:48 +010072
73 audio_module_load_one(name);
74 QLIST_FOREACH(d, &audio_drivers, next) {
75 if (strcmp(name, d->name) == 0) {
76 return d;
77 }
78 }
79
Gerd Hoffmannd3893a32018-03-06 08:40:47 +010080 return NULL;
81}
82
Gerd Hoffmann65ba8692018-03-06 08:40:48 +010083static void audio_module_load_all(void)
84{
85 int i;
86
87 for (i = 0; i < ARRAY_SIZE(audio_prio_list); i++) {
88 audio_driver_lookup(audio_prio_list[i]);
89 }
90}
91
bellardc0fe3822005-11-05 18:55:28 +000092struct fixed_settings {
93 int enabled;
94 int nb_voices;
95 int greedy;
malc1ea879e2008-12-03 22:48:44 +000096 struct audsettings settings;
bellardc0fe3822005-11-05 18:55:28 +000097};
bellard1d14ffa2005-10-30 18:58:22 +000098
bellardc0fe3822005-11-05 18:55:28 +000099static struct {
100 struct fixed_settings fixed_out;
101 struct fixed_settings fixed_in;
102 union {
malcc310de82008-11-12 20:36:27 +0000103 int hertz;
bellardc0fe3822005-11-05 18:55:28 +0000104 int64_t ticks;
105 } period;
malc713a98f2009-09-12 02:28:45 +0400106 int try_poll_in;
107 int try_poll_out;
bellardc0fe3822005-11-05 18:55:28 +0000108} conf = {
Juan Quintela14658cd2009-07-20 20:57:00 +0200109 .fixed_out = { /* DAC fixed settings */
110 .enabled = 1,
111 .nb_voices = 1,
112 .greedy = 1,
113 .settings = {
114 .freq = 44100,
115 .nchannels = 2,
116 .fmt = AUD_FMT_S16,
117 .endianness = AUDIO_HOST_ENDIANNESS,
bellardc0fe3822005-11-05 18:55:28 +0000118 }
119 },
bellard1d14ffa2005-10-30 18:58:22 +0000120
Juan Quintela14658cd2009-07-20 20:57:00 +0200121 .fixed_in = { /* ADC fixed settings */
122 .enabled = 1,
123 .nb_voices = 1,
124 .greedy = 1,
125 .settings = {
126 .freq = 44100,
127 .nchannels = 2,
128 .fmt = AUD_FMT_S16,
129 .endianness = AUDIO_HOST_ENDIANNESS,
bellardc0fe3822005-11-05 18:55:28 +0000130 }
131 },
bellard1d14ffa2005-10-30 18:58:22 +0000132
Hans de Goede40a814b2013-10-09 21:38:32 +0200133 .period = { .hertz = 100 },
malc713a98f2009-09-12 02:28:45 +0400134 .try_poll_in = 1,
135 .try_poll_out = 1,
bellard1d14ffa2005-10-30 18:58:22 +0000136};
137
bellardc0fe3822005-11-05 18:55:28 +0000138static AudioState glob_audio_state;
139
Michael Walle00e07672011-01-05 01:05:47 +0100140const struct mixeng_volume nominal_volume = {
Juan Quintela14658cd2009-07-20 20:57:00 +0200141 .mute = 0,
bellard1d14ffa2005-10-30 18:58:22 +0000142#ifdef FLOAT_MIXENG
Juan Quintela14658cd2009-07-20 20:57:00 +0200143 .r = 1.0,
144 .l = 1.0,
bellard1d14ffa2005-10-30 18:58:22 +0000145#else
Juan Quintela14658cd2009-07-20 20:57:00 +0200146 .r = 1ULL << 32,
147 .l = 1ULL << 32,
bellard1d14ffa2005-10-30 18:58:22 +0000148#endif
bellard85571bc2004-11-07 18:04:02 +0000149};
150
bellard1d14ffa2005-10-30 18:58:22 +0000151#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
152#error No its not
153#else
malc82584e22010-01-17 02:03:30 +0300154static void audio_print_options (const char *prefix,
155 struct audio_option *opt);
156
bellard1d14ffa2005-10-30 18:58:22 +0000157int audio_bug (const char *funcname, int cond)
bellard85571bc2004-11-07 18:04:02 +0000158{
bellard1d14ffa2005-10-30 18:58:22 +0000159 if (cond) {
160 static int shown;
161
bellard8ead62c2006-07-04 16:51:32 +0000162 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
bellard1d14ffa2005-10-30 18:58:22 +0000163 if (!shown) {
malc82584e22010-01-17 02:03:30 +0300164 struct audio_driver *d;
165
bellard1d14ffa2005-10-30 18:58:22 +0000166 shown = 1;
167 AUD_log (NULL, "Save all your work and restart without audio\n");
malc155a8ad2009-09-18 11:52:45 +0400168 AUD_log (NULL, "Please send bug report to av1474@comtv.ru\n");
bellard1d14ffa2005-10-30 18:58:22 +0000169 AUD_log (NULL, "I am sorry\n");
malc82584e22010-01-17 02:03:30 +0300170 d = glob_audio_state.drv;
171 if (d) {
172 audio_print_options (d->name, d->options);
173 }
bellard1d14ffa2005-10-30 18:58:22 +0000174 }
175 AUD_log (NULL, "Context:\n");
176
177#if defined AUDIO_BREAKPOINT_ON_BUG
178# if defined HOST_I386
179# if defined __GNUC__
180 __asm__ ("int3");
181# elif defined _MSC_VER
182 _asm _emit 0xcc;
183# else
184 abort ();
185# endif
186# else
187 abort ();
188# endif
189#endif
190 }
191
192 return cond;
193}
194#endif
195
thsf941aa22007-02-17 22:19:29 +0000196static inline int audio_bits_to_index (int bits)
197{
198 switch (bits) {
199 case 8:
200 return 0;
201
202 case 16:
203 return 1;
204
205 case 32:
206 return 2;
207
208 default:
209 audio_bug ("bits_to_index", 1);
210 AUD_log (NULL, "invalid bits %d\n", bits);
211 return 0;
212 }
213}
214
bellardc0fe3822005-11-05 18:55:28 +0000215void *audio_calloc (const char *funcname, int nmemb, size_t size)
216{
217 int cond;
218 size_t len;
219
220 len = nmemb * size;
221 cond = !nmemb || !size;
222 cond |= nmemb < 0;
223 cond |= len < size;
224
225 if (audio_bug ("audio_calloc", cond)) {
226 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
227 funcname);
bellard541e0842005-11-11 00:04:19 +0000228 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
bellardc0fe3822005-11-05 18:55:28 +0000229 return NULL;
230 }
231
Anthony Liguori7267c092011-08-20 22:09:37 -0500232 return g_malloc0 (len);
bellardc0fe3822005-11-05 18:55:28 +0000233}
234
bellard1d14ffa2005-10-30 18:58:22 +0000235static char *audio_alloc_prefix (const char *s)
236{
237 const char qemu_prefix[] = "QEMU_";
aliguori090f1fa2009-02-05 22:05:58 +0000238 size_t len, i;
239 char *r, *u;
bellard1d14ffa2005-10-30 18:58:22 +0000240
241 if (!s) {
242 return NULL;
243 }
244
245 len = strlen (s);
Anthony Liguori7267c092011-08-20 22:09:37 -0500246 r = g_malloc (len + sizeof (qemu_prefix));
bellard1d14ffa2005-10-30 18:58:22 +0000247
aliguori090f1fa2009-02-05 22:05:58 +0000248 u = r + sizeof (qemu_prefix) - 1;
bellard1d14ffa2005-10-30 18:58:22 +0000249
aliguori090f1fa2009-02-05 22:05:58 +0000250 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
251 pstrcat (r, len + sizeof (qemu_prefix), s);
bellard1d14ffa2005-10-30 18:58:22 +0000252
aliguori090f1fa2009-02-05 22:05:58 +0000253 for (i = 0; i < len; ++i) {
254 u[i] = qemu_toupper(u[i]);
bellard1d14ffa2005-10-30 18:58:22 +0000255 }
aliguori090f1fa2009-02-05 22:05:58 +0000256
bellard1d14ffa2005-10-30 18:58:22 +0000257 return r;
258}
259
pbrook9596ebb2007-11-18 01:44:38 +0000260static const char *audio_audfmt_to_string (audfmt_e fmt)
bellard1d14ffa2005-10-30 18:58:22 +0000261{
262 switch (fmt) {
263 case AUD_FMT_U8:
264 return "U8";
265
266 case AUD_FMT_U16:
267 return "U16";
268
269 case AUD_FMT_S8:
270 return "S8";
271
272 case AUD_FMT_S16:
273 return "S16";
thsf941aa22007-02-17 22:19:29 +0000274
275 case AUD_FMT_U32:
276 return "U32";
277
278 case AUD_FMT_S32:
279 return "S32";
bellard1d14ffa2005-10-30 18:58:22 +0000280 }
281
282 dolog ("Bogus audfmt %d returning S16\n", fmt);
283 return "S16";
284}
285
pbrook9596ebb2007-11-18 01:44:38 +0000286static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
287 int *defaultp)
bellard1d14ffa2005-10-30 18:58:22 +0000288{
289 if (!strcasecmp (s, "u8")) {
290 *defaultp = 0;
291 return AUD_FMT_U8;
292 }
293 else if (!strcasecmp (s, "u16")) {
294 *defaultp = 0;
295 return AUD_FMT_U16;
296 }
thsf941aa22007-02-17 22:19:29 +0000297 else if (!strcasecmp (s, "u32")) {
298 *defaultp = 0;
299 return AUD_FMT_U32;
300 }
bellard1d14ffa2005-10-30 18:58:22 +0000301 else if (!strcasecmp (s, "s8")) {
302 *defaultp = 0;
303 return AUD_FMT_S8;
304 }
305 else if (!strcasecmp (s, "s16")) {
306 *defaultp = 0;
307 return AUD_FMT_S16;
308 }
thsf941aa22007-02-17 22:19:29 +0000309 else if (!strcasecmp (s, "s32")) {
310 *defaultp = 0;
311 return AUD_FMT_S32;
312 }
bellard1d14ffa2005-10-30 18:58:22 +0000313 else {
314 dolog ("Bogus audio format `%s' using %s\n",
315 s, audio_audfmt_to_string (defval));
316 *defaultp = 1;
317 return defval;
318 }
319}
320
321static audfmt_e audio_get_conf_fmt (const char *envname,
322 audfmt_e defval,
323 int *defaultp)
324{
325 const char *var = getenv (envname);
326 if (!var) {
327 *defaultp = 1;
328 return defval;
329 }
330 return audio_string_to_audfmt (var, defval, defaultp);
331}
332
333static int audio_get_conf_int (const char *key, int defval, int *defaultp)
334{
335 int val;
bellard85571bc2004-11-07 18:04:02 +0000336 char *strval;
337
338 strval = getenv (key);
Nia Alarie441b3452018-03-16 14:40:47 +0000339 if (strval && !qemu_strtoi(strval, NULL, 10, &val)) {
bellard1d14ffa2005-10-30 18:58:22 +0000340 *defaultp = 0;
bellard1d14ffa2005-10-30 18:58:22 +0000341 return val;
bellard85571bc2004-11-07 18:04:02 +0000342 }
bellard1d14ffa2005-10-30 18:58:22 +0000343 else {
344 *defaultp = 1;
345 return defval;
346 }
bellard85571bc2004-11-07 18:04:02 +0000347}
348
bellard1d14ffa2005-10-30 18:58:22 +0000349static const char *audio_get_conf_str (const char *key,
350 const char *defval,
351 int *defaultp)
bellard85571bc2004-11-07 18:04:02 +0000352{
353 const char *val = getenv (key);
bellard1d14ffa2005-10-30 18:58:22 +0000354 if (!val) {
355 *defaultp = 1;
bellard85571bc2004-11-07 18:04:02 +0000356 return defval;
bellard1d14ffa2005-10-30 18:58:22 +0000357 }
358 else {
359 *defaultp = 0;
bellard85571bc2004-11-07 18:04:02 +0000360 return val;
bellard1d14ffa2005-10-30 18:58:22 +0000361 }
bellard85571bc2004-11-07 18:04:02 +0000362}
363
bellard541e0842005-11-11 00:04:19 +0000364void AUD_vlog (const char *cap, const char *fmt, va_list ap)
365{
Kővágó, Zoltán06ac27f2015-06-12 14:33:02 +0200366 if (cap) {
367 fprintf(stderr, "%s: ", cap);
bellard541e0842005-11-11 00:04:19 +0000368 }
bellard541e0842005-11-11 00:04:19 +0000369
Kővágó, Zoltán06ac27f2015-06-12 14:33:02 +0200370 vfprintf(stderr, fmt, ap);
bellard541e0842005-11-11 00:04:19 +0000371}
372
bellardfb065182004-11-09 23:09:44 +0000373void AUD_log (const char *cap, const char *fmt, ...)
bellard85571bc2004-11-07 18:04:02 +0000374{
375 va_list ap;
bellard85571bc2004-11-07 18:04:02 +0000376
bellard541e0842005-11-11 00:04:19 +0000377 va_start (ap, fmt);
378 AUD_vlog (cap, fmt, ap);
379 va_end (ap);
bellard85571bc2004-11-07 18:04:02 +0000380}
381
bellard1d14ffa2005-10-30 18:58:22 +0000382static void audio_print_options (const char *prefix,
383 struct audio_option *opt)
bellard85571bc2004-11-07 18:04:02 +0000384{
bellard1d14ffa2005-10-30 18:58:22 +0000385 char *uprefix;
386
387 if (!prefix) {
388 dolog ("No prefix specified\n");
389 return;
390 }
391
392 if (!opt) {
393 dolog ("No options\n");
394 return;
395 }
396
397 uprefix = audio_alloc_prefix (prefix);
398
399 for (; opt->name; opt++) {
400 const char *state = "default";
401 printf (" %s_%s: ", uprefix, opt->name);
402
thsfe8f0962007-07-12 10:59:21 +0000403 if (opt->overriddenp && *opt->overriddenp) {
bellard1d14ffa2005-10-30 18:58:22 +0000404 state = "current";
405 }
406
407 switch (opt->tag) {
408 case AUD_OPT_BOOL:
409 {
410 int *intp = opt->valp;
411 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
412 }
413 break;
414
415 case AUD_OPT_INT:
416 {
417 int *intp = opt->valp;
418 printf ("integer, %s = %d\n", state, *intp);
419 }
420 break;
421
422 case AUD_OPT_FMT:
423 {
424 audfmt_e *fmtp = opt->valp;
425 printf (
balrogca9cc282008-01-14 04:24:29 +0000426 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
bellard1d14ffa2005-10-30 18:58:22 +0000427 state,
428 audio_audfmt_to_string (*fmtp)
429 );
430 }
431 break;
432
433 case AUD_OPT_STR:
434 {
435 const char **strp = opt->valp;
436 printf ("string, %s = %s\n",
437 state,
438 *strp ? *strp : "(not set)");
439 }
440 break;
441
442 default:
443 printf ("???\n");
444 dolog ("Bad value tag for option %s_%s %d\n",
445 uprefix, opt->name, opt->tag);
446 break;
447 }
448 printf (" %s\n", opt->descr);
449 }
450
Anthony Liguori7267c092011-08-20 22:09:37 -0500451 g_free (uprefix);
bellard85571bc2004-11-07 18:04:02 +0000452}
453
bellard1d14ffa2005-10-30 18:58:22 +0000454static void audio_process_options (const char *prefix,
455 struct audio_option *opt)
456{
457 char *optname;
458 const char qemu_prefix[] = "QEMU_";
blueswir1363a37d2008-08-21 17:58:08 +0000459 size_t preflen, optlen;
bellard1d14ffa2005-10-30 18:58:22 +0000460
Alistair Francis470bcab2018-02-03 09:43:02 +0100461 if (audio_bug(__func__, !prefix)) {
bellard1d14ffa2005-10-30 18:58:22 +0000462 dolog ("prefix = NULL\n");
463 return;
464 }
465
Alistair Francis470bcab2018-02-03 09:43:02 +0100466 if (audio_bug(__func__, !opt)) {
bellard1d14ffa2005-10-30 18:58:22 +0000467 dolog ("opt = NULL\n");
468 return;
469 }
470
471 preflen = strlen (prefix);
472
473 for (; opt->name; opt++) {
474 size_t len, i;
475 int def;
476
477 if (!opt->valp) {
478 dolog ("Option value pointer for `%s' is not set\n",
479 opt->name);
480 continue;
481 }
482
483 len = strlen (opt->name);
bellardc0fe3822005-11-05 18:55:28 +0000484 /* len of opt->name + len of prefix + size of qemu_prefix
485 * (includes trailing zero) + zero + underscore (on behalf of
486 * sizeof) */
blueswir1363a37d2008-08-21 17:58:08 +0000487 optlen = len + preflen + sizeof (qemu_prefix) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500488 optname = g_malloc (optlen);
bellard1d14ffa2005-10-30 18:58:22 +0000489
blueswir1363a37d2008-08-21 17:58:08 +0000490 pstrcpy (optname, optlen, qemu_prefix);
bellardc0fe3822005-11-05 18:55:28 +0000491
492 /* copy while upper-casing, including trailing zero */
bellard1d14ffa2005-10-30 18:58:22 +0000493 for (i = 0; i <= preflen; ++i) {
blueswir1cd390082008-11-16 13:53:32 +0000494 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
bellard1d14ffa2005-10-30 18:58:22 +0000495 }
blueswir1363a37d2008-08-21 17:58:08 +0000496 pstrcat (optname, optlen, "_");
blueswir1363a37d2008-08-21 17:58:08 +0000497 pstrcat (optname, optlen, opt->name);
bellard1d14ffa2005-10-30 18:58:22 +0000498
499 def = 1;
500 switch (opt->tag) {
501 case AUD_OPT_BOOL:
502 case AUD_OPT_INT:
503 {
504 int *intp = opt->valp;
505 *intp = audio_get_conf_int (optname, *intp, &def);
506 }
507 break;
508
509 case AUD_OPT_FMT:
510 {
511 audfmt_e *fmtp = opt->valp;
512 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
513 }
514 break;
515
516 case AUD_OPT_STR:
517 {
518 const char **strp = opt->valp;
519 *strp = audio_get_conf_str (optname, *strp, &def);
520 }
521 break;
522
523 default:
524 dolog ("Bad value tag for option `%s' - %d\n",
525 optname, opt->tag);
526 break;
527 }
528
thsfe8f0962007-07-12 10:59:21 +0000529 if (!opt->overriddenp) {
530 opt->overriddenp = &opt->overridden;
bellard1d14ffa2005-10-30 18:58:22 +0000531 }
thsfe8f0962007-07-12 10:59:21 +0000532 *opt->overriddenp = !def;
Anthony Liguori7267c092011-08-20 22:09:37 -0500533 g_free (optname);
bellard1d14ffa2005-10-30 18:58:22 +0000534 }
535}
536
malc1ea879e2008-12-03 22:48:44 +0000537static void audio_print_settings (struct audsettings *as)
bellardc0fe3822005-11-05 18:55:28 +0000538{
539 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
540
541 switch (as->fmt) {
542 case AUD_FMT_S8:
543 AUD_log (NULL, "S8");
544 break;
545 case AUD_FMT_U8:
546 AUD_log (NULL, "U8");
547 break;
548 case AUD_FMT_S16:
549 AUD_log (NULL, "S16");
550 break;
551 case AUD_FMT_U16:
552 AUD_log (NULL, "U16");
553 break;
malcd50997f2008-06-22 14:10:45 +0000554 case AUD_FMT_S32:
555 AUD_log (NULL, "S32");
556 break;
557 case AUD_FMT_U32:
558 AUD_log (NULL, "U32");
559 break;
bellardc0fe3822005-11-05 18:55:28 +0000560 default:
561 AUD_log (NULL, "invalid(%d)", as->fmt);
562 break;
563 }
bellardec36b692006-07-16 18:57:03 +0000564
565 AUD_log (NULL, " endianness=");
bellardd929eba2006-07-04 21:47:22 +0000566 switch (as->endianness) {
567 case 0:
568 AUD_log (NULL, "little");
569 break;
570 case 1:
571 AUD_log (NULL, "big");
572 break;
573 default:
574 AUD_log (NULL, "invalid");
575 break;
576 }
bellardc0fe3822005-11-05 18:55:28 +0000577 AUD_log (NULL, "\n");
578}
579
malc1ea879e2008-12-03 22:48:44 +0000580static int audio_validate_settings (struct audsettings *as)
bellardc0fe3822005-11-05 18:55:28 +0000581{
582 int invalid;
583
584 invalid = as->nchannels != 1 && as->nchannels != 2;
bellardd929eba2006-07-04 21:47:22 +0000585 invalid |= as->endianness != 0 && as->endianness != 1;
bellardc0fe3822005-11-05 18:55:28 +0000586
587 switch (as->fmt) {
588 case AUD_FMT_S8:
589 case AUD_FMT_U8:
590 case AUD_FMT_S16:
591 case AUD_FMT_U16:
thsf941aa22007-02-17 22:19:29 +0000592 case AUD_FMT_S32:
593 case AUD_FMT_U32:
bellardc0fe3822005-11-05 18:55:28 +0000594 break;
595 default:
596 invalid = 1;
597 break;
598 }
599
600 invalid |= as->freq <= 0;
bellardd929eba2006-07-04 21:47:22 +0000601 return invalid ? -1 : 0;
bellardc0fe3822005-11-05 18:55:28 +0000602}
603
malc1ea879e2008-12-03 22:48:44 +0000604static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
bellard1d14ffa2005-10-30 18:58:22 +0000605{
606 int bits = 8, sign = 0;
607
bellardc0fe3822005-11-05 18:55:28 +0000608 switch (as->fmt) {
bellard1d14ffa2005-10-30 18:58:22 +0000609 case AUD_FMT_S8:
610 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100611 /* fall through */
bellard1d14ffa2005-10-30 18:58:22 +0000612 case AUD_FMT_U8:
613 break;
614
615 case AUD_FMT_S16:
616 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100617 /* fall through */
bellard1d14ffa2005-10-30 18:58:22 +0000618 case AUD_FMT_U16:
619 bits = 16;
620 break;
thsf941aa22007-02-17 22:19:29 +0000621
622 case AUD_FMT_S32:
623 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100624 /* fall through */
thsf941aa22007-02-17 22:19:29 +0000625 case AUD_FMT_U32:
626 bits = 32;
627 break;
bellard1d14ffa2005-10-30 18:58:22 +0000628 }
bellardc0fe3822005-11-05 18:55:28 +0000629 return info->freq == as->freq
630 && info->nchannels == as->nchannels
bellard1d14ffa2005-10-30 18:58:22 +0000631 && info->sign == sign
bellardd929eba2006-07-04 21:47:22 +0000632 && info->bits == bits
633 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
bellard1d14ffa2005-10-30 18:58:22 +0000634}
635
malc1ea879e2008-12-03 22:48:44 +0000636void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
bellard85571bc2004-11-07 18:04:02 +0000637{
thsf941aa22007-02-17 22:19:29 +0000638 int bits = 8, sign = 0, shift = 0;
bellard85571bc2004-11-07 18:04:02 +0000639
bellardc0fe3822005-11-05 18:55:28 +0000640 switch (as->fmt) {
bellard85571bc2004-11-07 18:04:02 +0000641 case AUD_FMT_S8:
642 sign = 1;
643 case AUD_FMT_U8:
644 break;
645
646 case AUD_FMT_S16:
647 sign = 1;
648 case AUD_FMT_U16:
649 bits = 16;
thsf941aa22007-02-17 22:19:29 +0000650 shift = 1;
651 break;
652
653 case AUD_FMT_S32:
654 sign = 1;
655 case AUD_FMT_U32:
656 bits = 32;
657 shift = 2;
bellard85571bc2004-11-07 18:04:02 +0000658 break;
659 }
660
bellardc0fe3822005-11-05 18:55:28 +0000661 info->freq = as->freq;
bellard1d14ffa2005-10-30 18:58:22 +0000662 info->bits = bits;
663 info->sign = sign;
bellardc0fe3822005-11-05 18:55:28 +0000664 info->nchannels = as->nchannels;
thsf941aa22007-02-17 22:19:29 +0000665 info->shift = (as->nchannels == 2) + shift;
bellard1d14ffa2005-10-30 18:58:22 +0000666 info->align = (1 << info->shift) - 1;
667 info->bytes_per_second = info->freq << info->shift;
bellardd929eba2006-07-04 21:47:22 +0000668 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
bellard85571bc2004-11-07 18:04:02 +0000669}
670
bellard1d14ffa2005-10-30 18:58:22 +0000671void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
bellard85571bc2004-11-07 18:04:02 +0000672{
bellard1d14ffa2005-10-30 18:58:22 +0000673 if (!len) {
674 return;
675 }
676
677 if (info->sign) {
bellarde2f909b2006-08-03 20:39:40 +0000678 memset (buf, 0x00, len << info->shift);
bellard1d14ffa2005-10-30 18:58:22 +0000679 }
680 else {
thsf941aa22007-02-17 22:19:29 +0000681 switch (info->bits) {
682 case 8:
bellarde2f909b2006-08-03 20:39:40 +0000683 memset (buf, 0x80, len << info->shift);
thsf941aa22007-02-17 22:19:29 +0000684 break;
bellard1d14ffa2005-10-30 18:58:22 +0000685
thsf941aa22007-02-17 22:19:29 +0000686 case 16:
687 {
688 int i;
689 uint16_t *p = buf;
690 int shift = info->nchannels - 1;
691 short s = INT16_MAX;
bellard1d14ffa2005-10-30 18:58:22 +0000692
thsf941aa22007-02-17 22:19:29 +0000693 if (info->swap_endianness) {
694 s = bswap16 (s);
695 }
696
697 for (i = 0; i < len << shift; i++) {
698 p[i] = s;
699 }
bellard1d14ffa2005-10-30 18:58:22 +0000700 }
thsf941aa22007-02-17 22:19:29 +0000701 break;
702
703 case 32:
704 {
705 int i;
706 uint32_t *p = buf;
707 int shift = info->nchannels - 1;
708 int32_t s = INT32_MAX;
709
710 if (info->swap_endianness) {
711 s = bswap32 (s);
712 }
713
714 for (i = 0; i < len << shift; i++) {
715 p[i] = s;
716 }
717 }
718 break;
719
720 default:
721 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
722 info->bits);
723 break;
bellard1d14ffa2005-10-30 18:58:22 +0000724 }
725 }
bellard85571bc2004-11-07 18:04:02 +0000726}
727
bellard1d14ffa2005-10-30 18:58:22 +0000728/*
bellard8ead62c2006-07-04 16:51:32 +0000729 * Capture
730 */
Michael Walle00e07672011-01-05 01:05:47 +0100731static void noop_conv (struct st_sample *dst, const void *src, int samples)
bellard8ead62c2006-07-04 16:51:32 +0000732{
733 (void) src;
734 (void) dst;
735 (void) samples;
bellard8ead62c2006-07-04 16:51:32 +0000736}
737
738static CaptureVoiceOut *audio_pcm_capture_find_specific (
malc1ea879e2008-12-03 22:48:44 +0000739 struct audsettings *as
bellard8ead62c2006-07-04 16:51:32 +0000740 )
741{
742 CaptureVoiceOut *cap;
malc1a7dafc2009-05-14 03:11:35 +0400743 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +0000744
745 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
bellardd929eba2006-07-04 21:47:22 +0000746 if (audio_pcm_info_eq (&cap->hw.info, as)) {
bellard8ead62c2006-07-04 16:51:32 +0000747 return cap;
748 }
749 }
750 return NULL;
751}
752
bellardec36b692006-07-16 18:57:03 +0000753static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
754{
755 struct capture_callback *cb;
756
757#ifdef DEBUG_CAPTURE
758 dolog ("notification %d sent\n", cmd);
759#endif
760 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
761 cb->ops.notify (cb->opaque, cmd);
762 }
763}
764
765static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
bellard8ead62c2006-07-04 16:51:32 +0000766{
767 if (cap->hw.enabled != enabled) {
bellardec36b692006-07-16 18:57:03 +0000768 audcnotification_e cmd;
bellard8ead62c2006-07-04 16:51:32 +0000769 cap->hw.enabled = enabled;
bellardec36b692006-07-16 18:57:03 +0000770 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
771 audio_notify_capture (cap, cmd);
bellard8ead62c2006-07-04 16:51:32 +0000772 }
773}
774
775static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
776{
777 HWVoiceOut *hw = &cap->hw;
778 SWVoiceOut *sw;
779 int enabled = 0;
780
bellardec36b692006-07-16 18:57:03 +0000781 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
bellard8ead62c2006-07-04 16:51:32 +0000782 if (sw->active) {
783 enabled = 1;
784 break;
785 }
786 }
bellardec36b692006-07-16 18:57:03 +0000787 audio_capture_maybe_changed (cap, enabled);
bellard8ead62c2006-07-04 16:51:32 +0000788}
789
790static void audio_detach_capture (HWVoiceOut *hw)
791{
bellardec36b692006-07-16 18:57:03 +0000792 SWVoiceCap *sc = hw->cap_head.lh_first;
bellard8ead62c2006-07-04 16:51:32 +0000793
bellardec36b692006-07-16 18:57:03 +0000794 while (sc) {
795 SWVoiceCap *sc1 = sc->entries.le_next;
796 SWVoiceOut *sw = &sc->sw;
797 CaptureVoiceOut *cap = sc->cap;
798 int was_active = sw->active;
799
bellard8ead62c2006-07-04 16:51:32 +0000800 if (sw->rate) {
801 st_rate_stop (sw->rate);
802 sw->rate = NULL;
803 }
804
Blue Swirl72cf2d42009-09-12 07:36:22 +0000805 QLIST_REMOVE (sw, entries);
806 QLIST_REMOVE (sc, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -0500807 g_free (sc);
bellardec36b692006-07-16 18:57:03 +0000808 if (was_active) {
809 /* We have removed soft voice from the capture:
810 this might have changed the overall status of the capture
811 since this might have been the only active voice */
812 audio_recalc_and_notify_capture (cap);
813 }
814 sc = sc1;
bellard8ead62c2006-07-04 16:51:32 +0000815 }
816}
817
malc1a7dafc2009-05-14 03:11:35 +0400818static int audio_attach_capture (HWVoiceOut *hw)
bellard8ead62c2006-07-04 16:51:32 +0000819{
malc1a7dafc2009-05-14 03:11:35 +0400820 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +0000821 CaptureVoiceOut *cap;
822
823 audio_detach_capture (hw);
824 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
bellardec36b692006-07-16 18:57:03 +0000825 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +0000826 SWVoiceOut *sw;
bellardec36b692006-07-16 18:57:03 +0000827 HWVoiceOut *hw_cap = &cap->hw;
bellard8ead62c2006-07-04 16:51:32 +0000828
Alistair Francis470bcab2018-02-03 09:43:02 +0100829 sc = audio_calloc(__func__, 1, sizeof(*sc));
bellardec36b692006-07-16 18:57:03 +0000830 if (!sc) {
bellard8ead62c2006-07-04 16:51:32 +0000831 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
bellardec36b692006-07-16 18:57:03 +0000832 sizeof (*sc));
bellard8ead62c2006-07-04 16:51:32 +0000833 return -1;
834 }
835
bellardec36b692006-07-16 18:57:03 +0000836 sc->cap = cap;
837 sw = &sc->sw;
bellard8ead62c2006-07-04 16:51:32 +0000838 sw->hw = hw_cap;
bellardec36b692006-07-16 18:57:03 +0000839 sw->info = hw->info;
bellard8ead62c2006-07-04 16:51:32 +0000840 sw->empty = 1;
841 sw->active = hw->enabled;
842 sw->conv = noop_conv;
843 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
malc83617102012-07-16 18:08:36 +0400844 sw->vol = nominal_volume;
bellard8ead62c2006-07-04 16:51:32 +0000845 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
846 if (!sw->rate) {
847 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
Anthony Liguori7267c092011-08-20 22:09:37 -0500848 g_free (sw);
bellard8ead62c2006-07-04 16:51:32 +0000849 return -1;
850 }
Blue Swirl72cf2d42009-09-12 07:36:22 +0000851 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
852 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
bellardec36b692006-07-16 18:57:03 +0000853#ifdef DEBUG_CAPTURE
Stefan Weil457b6542013-01-16 18:17:33 +0100854 sw->name = g_strdup_printf ("for %p %d,%d,%d",
855 hw, sw->info.freq, sw->info.bits,
856 sw->info.nchannels);
bellardec36b692006-07-16 18:57:03 +0000857 dolog ("Added %s active = %d\n", sw->name, sw->active);
858#endif
bellard8ead62c2006-07-04 16:51:32 +0000859 if (sw->active) {
bellardec36b692006-07-16 18:57:03 +0000860 audio_capture_maybe_changed (cap, 1);
bellard8ead62c2006-07-04 16:51:32 +0000861 }
862 }
863 return 0;
864}
865
866/*
bellard1d14ffa2005-10-30 18:58:22 +0000867 * Hard voice (capture)
868 */
bellardc0fe3822005-11-05 18:55:28 +0000869static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
bellard85571bc2004-11-07 18:04:02 +0000870{
bellard1d14ffa2005-10-30 18:58:22 +0000871 SWVoiceIn *sw;
872 int m = hw->total_samples_captured;
bellard85571bc2004-11-07 18:04:02 +0000873
bellard1d14ffa2005-10-30 18:58:22 +0000874 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
875 if (sw->active) {
876 m = audio_MIN (m, sw->total_hw_samples_acquired);
bellard85571bc2004-11-07 18:04:02 +0000877 }
878 }
bellard1d14ffa2005-10-30 18:58:22 +0000879 return m;
bellard85571bc2004-11-07 18:04:02 +0000880}
881
bellard1d14ffa2005-10-30 18:58:22 +0000882int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
bellard85571bc2004-11-07 18:04:02 +0000883{
bellard1d14ffa2005-10-30 18:58:22 +0000884 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
Alistair Francis470bcab2018-02-03 09:43:02 +0100885 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
bellard1d14ffa2005-10-30 18:58:22 +0000886 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
887 return 0;
888 }
889 return live;
890}
bellard85571bc2004-11-07 18:04:02 +0000891
malcddabec72009-09-18 10:59:38 +0400892int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
893 int live, int pending)
894{
895 int left = hw->samples - pending;
896 int len = audio_MIN (left, live);
897 int clipped = 0;
898
899 while (len) {
900 struct st_sample *src = hw->mix_buf + hw->rpos;
901 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
902 int samples_till_end_of_buf = hw->samples - hw->rpos;
903 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
904
905 hw->clip (dst, src, samples_to_clip);
906
907 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
908 len -= samples_to_clip;
909 clipped += samples_to_clip;
910 }
911 return clipped;
912}
913
bellard1d14ffa2005-10-30 18:58:22 +0000914/*
915 * Soft voice (capture)
916 */
bellard1d14ffa2005-10-30 18:58:22 +0000917static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
918{
919 HWVoiceIn *hw = sw->hw;
920 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
921 int rpos;
922
Alistair Francis470bcab2018-02-03 09:43:02 +0100923 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
bellard1d14ffa2005-10-30 18:58:22 +0000924 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
925 return 0;
926 }
927
928 rpos = hw->wpos - live;
929 if (rpos >= 0) {
930 return rpos;
931 }
932 else {
933 return hw->samples + rpos;
bellard85571bc2004-11-07 18:04:02 +0000934 }
935}
936
bellard1d14ffa2005-10-30 18:58:22 +0000937int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +0000938{
bellard1d14ffa2005-10-30 18:58:22 +0000939 HWVoiceIn *hw = sw->hw;
940 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
malc1ea879e2008-12-03 22:48:44 +0000941 struct st_sample *src, *dst = sw->buf;
bellard85571bc2004-11-07 18:04:02 +0000942
bellard1d14ffa2005-10-30 18:58:22 +0000943 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
bellard85571bc2004-11-07 18:04:02 +0000944
bellard1d14ffa2005-10-30 18:58:22 +0000945 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
Alistair Francis470bcab2018-02-03 09:43:02 +0100946 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
bellard1d14ffa2005-10-30 18:58:22 +0000947 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
948 return 0;
bellard85571bc2004-11-07 18:04:02 +0000949 }
bellard1d14ffa2005-10-30 18:58:22 +0000950
951 samples = size >> sw->info.shift;
952 if (!live) {
953 return 0;
954 }
955
956 swlim = (live * sw->ratio) >> 32;
bellard85571bc2004-11-07 18:04:02 +0000957 swlim = audio_MIN (swlim, samples);
958
bellard1d14ffa2005-10-30 18:58:22 +0000959 while (swlim) {
960 src = hw->conv_buf + rpos;
961 isamp = hw->wpos - rpos;
962 /* XXX: <= ? */
963 if (isamp <= 0) {
964 isamp = hw->samples - rpos;
965 }
966
967 if (!isamp) {
968 break;
969 }
970 osamp = swlim;
971
Alistair Francis470bcab2018-02-03 09:43:02 +0100972 if (audio_bug(__func__, osamp < 0)) {
bellard1d14ffa2005-10-30 18:58:22 +0000973 dolog ("osamp=%d\n", osamp);
bellardc0fe3822005-11-05 18:55:28 +0000974 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000975 }
976
977 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
978 swlim -= osamp;
979 rpos = (rpos + isamp) % hw->samples;
980 dst += osamp;
981 ret += osamp;
982 total += isamp;
983 }
984
Marc-André Lureauc01b2452012-04-17 14:32:36 +0200985 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
986 mixeng_volume (sw->buf, ret, &sw->vol);
987 }
Michael Walle00e07672011-01-05 01:05:47 +0100988
bellard571ec3d2005-11-20 16:24:34 +0000989 sw->clip (buf, sw->buf, ret);
bellard1d14ffa2005-10-30 18:58:22 +0000990 sw->total_hw_samples_acquired += total;
991 return ret << sw->info.shift;
992}
993
994/*
995 * Hard voice (playback)
996 */
997static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
998{
999 SWVoiceOut *sw;
1000 int m = INT_MAX;
1001 int nb_live = 0;
1002
1003 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1004 if (sw->active || !sw->empty) {
1005 m = audio_MIN (m, sw->total_hw_samples_mixed);
1006 nb_live += 1;
1007 }
1008 }
1009
1010 *nb_livep = nb_live;
1011 return m;
1012}
1013
malcbdff2532009-09-18 11:37:39 +04001014static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
bellard1d14ffa2005-10-30 18:58:22 +00001015{
1016 int smin;
malcbdff2532009-09-18 11:37:39 +04001017 int nb_live1;
bellard1d14ffa2005-10-30 18:58:22 +00001018
malcbdff2532009-09-18 11:37:39 +04001019 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
1020 if (nb_live) {
1021 *nb_live = nb_live1;
bellard1d14ffa2005-10-30 18:58:22 +00001022 }
malcbdff2532009-09-18 11:37:39 +04001023
1024 if (nb_live1) {
bellard1d14ffa2005-10-30 18:58:22 +00001025 int live = smin;
1026
Alistair Francis470bcab2018-02-03 09:43:02 +01001027 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
bellard1d14ffa2005-10-30 18:58:22 +00001028 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
1029 return 0;
1030 }
1031 return live;
1032 }
malcbdff2532009-09-18 11:37:39 +04001033 return 0;
bellard1d14ffa2005-10-30 18:58:22 +00001034}
1035
1036/*
1037 * Soft voice (playback)
1038 */
bellard1d14ffa2005-10-30 18:58:22 +00001039int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1040{
1041 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1042 int ret = 0, pos = 0, total = 0;
1043
1044 if (!sw) {
1045 return size;
1046 }
1047
1048 hwsamples = sw->hw->samples;
1049
1050 live = sw->total_hw_samples_mixed;
Alistair Francis470bcab2018-02-03 09:43:02 +01001051 if (audio_bug(__func__, live < 0 || live > hwsamples)) {
bellard1d14ffa2005-10-30 18:58:22 +00001052 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1053 return 0;
1054 }
1055
1056 if (live == hwsamples) {
bellardec36b692006-07-16 18:57:03 +00001057#ifdef DEBUG_OUT
1058 dolog ("%s is full %d\n", sw->name, live);
1059#endif
bellard1d14ffa2005-10-30 18:58:22 +00001060 return 0;
1061 }
1062
1063 wpos = (sw->hw->rpos + live) % hwsamples;
1064 samples = size >> sw->info.shift;
1065
1066 dead = hwsamples - live;
1067 swlim = ((int64_t) dead << 32) / sw->ratio;
1068 swlim = audio_MIN (swlim, samples);
1069 if (swlim) {
Michael Walle00e07672011-01-05 01:05:47 +01001070 sw->conv (sw->buf, buf, swlim);
Marc-André Lureauc01b2452012-04-17 14:32:36 +02001071
1072 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1073 mixeng_volume (sw->buf, swlim, &sw->vol);
1074 }
bellard1d14ffa2005-10-30 18:58:22 +00001075 }
bellard85571bc2004-11-07 18:04:02 +00001076
1077 while (swlim) {
1078 dead = hwsamples - live;
1079 left = hwsamples - wpos;
1080 blck = audio_MIN (dead, left);
1081 if (!blck) {
bellard85571bc2004-11-07 18:04:02 +00001082 break;
1083 }
1084 isamp = swlim;
1085 osamp = blck;
bellard1d14ffa2005-10-30 18:58:22 +00001086 st_rate_flow_mix (
1087 sw->rate,
1088 sw->buf + pos,
1089 sw->hw->mix_buf + wpos,
1090 &isamp,
1091 &osamp
1092 );
bellard85571bc2004-11-07 18:04:02 +00001093 ret += isamp;
1094 swlim -= isamp;
1095 pos += isamp;
1096 live += osamp;
1097 wpos = (wpos + osamp) % hwsamples;
bellard1d14ffa2005-10-30 18:58:22 +00001098 total += osamp;
bellard85571bc2004-11-07 18:04:02 +00001099 }
1100
bellard1d14ffa2005-10-30 18:58:22 +00001101 sw->total_hw_samples_mixed += total;
1102 sw->empty = sw->total_hw_samples_mixed == 0;
1103
1104#ifdef DEBUG_OUT
1105 dolog (
bellardc0fe3822005-11-05 18:55:28 +00001106 "%s: write size %d ret %d total sw %d\n",
1107 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001108 size >> sw->info.shift,
1109 ret,
bellardc0fe3822005-11-05 18:55:28 +00001110 sw->total_hw_samples_mixed
bellard1d14ffa2005-10-30 18:58:22 +00001111 );
1112#endif
1113
1114 return ret << sw->info.shift;
bellard85571bc2004-11-07 18:04:02 +00001115}
1116
bellard1d14ffa2005-10-30 18:58:22 +00001117#ifdef DEBUG_AUDIO
1118static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
bellard85571bc2004-11-07 18:04:02 +00001119{
bellard1d14ffa2005-10-30 18:58:22 +00001120 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1121 cap, info->bits, info->sign, info->freq, info->nchannels);
bellard85571bc2004-11-07 18:04:02 +00001122}
bellard1d14ffa2005-10-30 18:58:22 +00001123#endif
bellard85571bc2004-11-07 18:04:02 +00001124
bellard1d14ffa2005-10-30 18:58:22 +00001125#define DAC
1126#include "audio_template.h"
1127#undef DAC
1128#include "audio_template.h"
bellard85571bc2004-11-07 18:04:02 +00001129
malc713a98f2009-09-12 02:28:45 +04001130/*
1131 * Timer
1132 */
Gerd Hoffmann1a961e72018-07-02 18:15:24 +02001133
1134static bool audio_timer_running;
1135static uint64_t audio_timer_last;
1136
malc713a98f2009-09-12 02:28:45 +04001137static int audio_is_timer_needed (void)
1138{
1139 HWVoiceIn *hwi = NULL;
1140 HWVoiceOut *hwo = NULL;
1141
1142 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1143 if (!hwo->poll_mode) return 1;
1144 }
1145 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1146 if (!hwi->poll_mode) return 1;
1147 }
1148 return 0;
1149}
1150
malc39deb1e2010-11-18 14:30:12 +03001151static void audio_reset_timer (AudioState *s)
malc713a98f2009-09-12 02:28:45 +04001152{
malc713a98f2009-09-12 02:28:45 +04001153 if (audio_is_timer_needed ()) {
Pavel Dovgalyuk1ffc2662017-02-14 10:15:10 +03001154 timer_mod_anticipate_ns(s->ts,
Hans de Goedeb4350de2013-10-09 21:33:44 +02001155 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
Gerd Hoffmann1a961e72018-07-02 18:15:24 +02001156 if (!audio_timer_running) {
1157 audio_timer_running = true;
1158 audio_timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1159 trace_audio_timer_start(conf.period.ticks / SCALE_MS);
1160 }
1161 } else {
1162 timer_del(s->ts);
1163 if (audio_timer_running) {
1164 audio_timer_running = false;
1165 trace_audio_timer_stop();
1166 }
malc713a98f2009-09-12 02:28:45 +04001167 }
1168}
1169
malc39deb1e2010-11-18 14:30:12 +03001170static void audio_timer (void *opaque)
1171{
Gerd Hoffmann1a961e72018-07-02 18:15:24 +02001172 int64_t now, diff;
1173
1174 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
1175 diff = now - audio_timer_last;
1176 if (diff > conf.period.ticks * 3 / 2) {
1177 trace_audio_timer_delayed(diff / SCALE_MS);
1178 }
1179 audio_timer_last = now;
1180
malc39deb1e2010-11-18 14:30:12 +03001181 audio_run ("timer");
1182 audio_reset_timer (opaque);
1183}
1184
malc713a98f2009-09-12 02:28:45 +04001185/*
1186 * Public API
1187 */
bellard1d14ffa2005-10-30 18:58:22 +00001188int AUD_write (SWVoiceOut *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +00001189{
bellard1d14ffa2005-10-30 18:58:22 +00001190 if (!sw) {
1191 /* XXX: Consider options */
1192 return size;
1193 }
1194
1195 if (!sw->hw->enabled) {
bellardc0fe3822005-11-05 18:55:28 +00001196 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
bellard1d14ffa2005-10-30 18:58:22 +00001197 return 0;
1198 }
1199
Eduardo Habkost9be38592016-06-13 18:57:58 -03001200 return sw->hw->pcm_ops->write(sw, buf, size);
bellard85571bc2004-11-07 18:04:02 +00001201}
1202
bellard1d14ffa2005-10-30 18:58:22 +00001203int AUD_read (SWVoiceIn *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +00001204{
bellard1d14ffa2005-10-30 18:58:22 +00001205 if (!sw) {
1206 /* XXX: Consider options */
1207 return size;
bellard85571bc2004-11-07 18:04:02 +00001208 }
bellard1d14ffa2005-10-30 18:58:22 +00001209
1210 if (!sw->hw->enabled) {
bellardc0fe3822005-11-05 18:55:28 +00001211 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
bellard1d14ffa2005-10-30 18:58:22 +00001212 return 0;
1213 }
1214
Eduardo Habkost9be38592016-06-13 18:57:58 -03001215 return sw->hw->pcm_ops->read(sw, buf, size);
bellard85571bc2004-11-07 18:04:02 +00001216}
1217
bellard1d14ffa2005-10-30 18:58:22 +00001218int AUD_get_buffer_size_out (SWVoiceOut *sw)
bellard85571bc2004-11-07 18:04:02 +00001219{
bellardc0fe3822005-11-05 18:55:28 +00001220 return sw->hw->samples << sw->hw->info.shift;
bellard85571bc2004-11-07 18:04:02 +00001221}
1222
bellard1d14ffa2005-10-30 18:58:22 +00001223void AUD_set_active_out (SWVoiceOut *sw, int on)
bellard85571bc2004-11-07 18:04:02 +00001224{
bellard1d14ffa2005-10-30 18:58:22 +00001225 HWVoiceOut *hw;
1226
1227 if (!sw) {
bellard85571bc2004-11-07 18:04:02 +00001228 return;
bellard85571bc2004-11-07 18:04:02 +00001229 }
1230
bellard85571bc2004-11-07 18:04:02 +00001231 hw = sw->hw;
bellard85571bc2004-11-07 18:04:02 +00001232 if (sw->active != on) {
malc978dd632009-02-18 20:44:04 +00001233 AudioState *s = &glob_audio_state;
bellard1d14ffa2005-10-30 18:58:22 +00001234 SWVoiceOut *temp_sw;
bellardec36b692006-07-16 18:57:03 +00001235 SWVoiceCap *sc;
bellard1d14ffa2005-10-30 18:58:22 +00001236
bellard85571bc2004-11-07 18:04:02 +00001237 if (on) {
1238 hw->pending_disable = 0;
1239 if (!hw->enabled) {
1240 hw->enabled = 1;
malc978dd632009-02-18 20:44:04 +00001241 if (s->vm_running) {
malc713a98f2009-09-12 02:28:45 +04001242 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
malc39deb1e2010-11-18 14:30:12 +03001243 audio_reset_timer (s);
malc978dd632009-02-18 20:44:04 +00001244 }
bellard1d14ffa2005-10-30 18:58:22 +00001245 }
bellard85571bc2004-11-07 18:04:02 +00001246 }
1247 else {
bellard1d14ffa2005-10-30 18:58:22 +00001248 if (hw->enabled) {
bellard85571bc2004-11-07 18:04:02 +00001249 int nb_active = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001250
1251 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1252 temp_sw = temp_sw->entries.le_next) {
1253 nb_active += temp_sw->active != 0;
1254 }
1255
1256 hw->pending_disable = nb_active == 1;
1257 }
1258 }
bellardec36b692006-07-16 18:57:03 +00001259
1260 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1261 sc->sw.active = hw->enabled;
bellard8ead62c2006-07-04 16:51:32 +00001262 if (hw->enabled) {
bellardec36b692006-07-16 18:57:03 +00001263 audio_capture_maybe_changed (sc->cap, 1);
bellard8ead62c2006-07-04 16:51:32 +00001264 }
1265 }
bellard1d14ffa2005-10-30 18:58:22 +00001266 sw->active = on;
1267 }
1268}
1269
1270void AUD_set_active_in (SWVoiceIn *sw, int on)
1271{
1272 HWVoiceIn *hw;
1273
1274 if (!sw) {
1275 return;
1276 }
1277
1278 hw = sw->hw;
1279 if (sw->active != on) {
malc978dd632009-02-18 20:44:04 +00001280 AudioState *s = &glob_audio_state;
bellard1d14ffa2005-10-30 18:58:22 +00001281 SWVoiceIn *temp_sw;
1282
1283 if (on) {
1284 if (!hw->enabled) {
1285 hw->enabled = 1;
malc978dd632009-02-18 20:44:04 +00001286 if (s->vm_running) {
malc713a98f2009-09-12 02:28:45 +04001287 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
malc39deb1e2010-11-18 14:30:12 +03001288 audio_reset_timer (s);
malc978dd632009-02-18 20:44:04 +00001289 }
bellard1d14ffa2005-10-30 18:58:22 +00001290 }
1291 sw->total_hw_samples_acquired = hw->total_samples_captured;
1292 }
1293 else {
1294 if (hw->enabled) {
1295 int nb_active = 0;
1296
1297 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1298 temp_sw = temp_sw->entries.le_next) {
1299 nb_active += temp_sw->active != 0;
bellard85571bc2004-11-07 18:04:02 +00001300 }
1301
1302 if (nb_active == 1) {
bellard1d14ffa2005-10-30 18:58:22 +00001303 hw->enabled = 0;
1304 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
bellard85571bc2004-11-07 18:04:02 +00001305 }
1306 }
1307 }
1308 sw->active = on;
1309 }
1310}
1311
bellard1d14ffa2005-10-30 18:58:22 +00001312static int audio_get_avail (SWVoiceIn *sw)
bellard85571bc2004-11-07 18:04:02 +00001313{
bellard1d14ffa2005-10-30 18:58:22 +00001314 int live;
1315
1316 if (!sw) {
1317 return 0;
1318 }
1319
1320 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
Alistair Francis470bcab2018-02-03 09:43:02 +01001321 if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) {
bellard1d14ffa2005-10-30 18:58:22 +00001322 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1323 return 0;
1324 }
1325
1326 ldebug (
bellard26a76462006-06-25 18:15:32 +00001327 "%s: get_avail live %d ret %" PRId64 "\n",
bellardc0fe3822005-11-05 18:55:28 +00001328 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001329 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1330 );
1331
1332 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1333}
1334
1335static int audio_get_free (SWVoiceOut *sw)
1336{
1337 int live, dead;
1338
1339 if (!sw) {
1340 return 0;
1341 }
1342
1343 live = sw->total_hw_samples_mixed;
1344
Alistair Francis470bcab2018-02-03 09:43:02 +01001345 if (audio_bug(__func__, live < 0 || live > sw->hw->samples)) {
bellard1d14ffa2005-10-30 18:58:22 +00001346 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
bellardc0fe3822005-11-05 18:55:28 +00001347 return 0;
bellard1d14ffa2005-10-30 18:58:22 +00001348 }
1349
1350 dead = sw->hw->samples - live;
1351
1352#ifdef DEBUG_OUT
bellard26a76462006-06-25 18:15:32 +00001353 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
bellardc0fe3822005-11-05 18:55:28 +00001354 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001355 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1356#endif
1357
1358 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1359}
1360
bellard8ead62c2006-07-04 16:51:32 +00001361static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1362{
1363 int n;
1364
1365 if (hw->enabled) {
bellardec36b692006-07-16 18:57:03 +00001366 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +00001367
bellardec36b692006-07-16 18:57:03 +00001368 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1369 SWVoiceOut *sw = &sc->sw;
bellard8ead62c2006-07-04 16:51:32 +00001370 int rpos2 = rpos;
1371
1372 n = samples;
1373 while (n) {
1374 int till_end_of_hw = hw->samples - rpos2;
1375 int to_write = audio_MIN (till_end_of_hw, n);
1376 int bytes = to_write << hw->info.shift;
1377 int written;
1378
1379 sw->buf = hw->mix_buf + rpos2;
1380 written = audio_pcm_sw_write (sw, NULL, bytes);
1381 if (written - bytes) {
bellardec36b692006-07-16 18:57:03 +00001382 dolog ("Could not mix %d bytes into a capture "
1383 "buffer, mixed %d\n",
1384 bytes, written);
bellard8ead62c2006-07-04 16:51:32 +00001385 break;
1386 }
1387 n -= to_write;
1388 rpos2 = (rpos2 + to_write) % hw->samples;
1389 }
1390 }
1391 }
1392
1393 n = audio_MIN (samples, hw->samples - rpos);
1394 mixeng_clear (hw->mix_buf + rpos, n);
1395 mixeng_clear (hw->mix_buf, samples - n);
1396}
1397
bellardc0fe3822005-11-05 18:55:28 +00001398static void audio_run_out (AudioState *s)
bellard1d14ffa2005-10-30 18:58:22 +00001399{
1400 HWVoiceOut *hw = NULL;
1401 SWVoiceOut *sw;
1402
malc1a7dafc2009-05-14 03:11:35 +04001403 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
bellard1d14ffa2005-10-30 18:58:22 +00001404 int played;
bellard8ead62c2006-07-04 16:51:32 +00001405 int live, free, nb_live, cleanup_required, prev_rpos;
bellard1d14ffa2005-10-30 18:58:22 +00001406
malcbdff2532009-09-18 11:37:39 +04001407 live = audio_pcm_hw_get_live_out (hw, &nb_live);
bellard1d14ffa2005-10-30 18:58:22 +00001408 if (!nb_live) {
1409 live = 0;
bellard85571bc2004-11-07 18:04:02 +00001410 }
bellardc0fe3822005-11-05 18:55:28 +00001411
Alistair Francis470bcab2018-02-03 09:43:02 +01001412 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
bellard1d14ffa2005-10-30 18:58:22 +00001413 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
bellardc0fe3822005-11-05 18:55:28 +00001414 continue;
bellard85571bc2004-11-07 18:04:02 +00001415 }
bellard1d14ffa2005-10-30 18:58:22 +00001416
1417 if (hw->pending_disable && !nb_live) {
bellardec36b692006-07-16 18:57:03 +00001418 SWVoiceCap *sc;
bellard1d14ffa2005-10-30 18:58:22 +00001419#ifdef DEBUG_OUT
1420 dolog ("Disabling voice\n");
1421#endif
1422 hw->enabled = 0;
1423 hw->pending_disable = 0;
1424 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
bellardec36b692006-07-16 18:57:03 +00001425 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1426 sc->sw.active = 0;
1427 audio_recalc_and_notify_capture (sc->cap);
bellard8ead62c2006-07-04 16:51:32 +00001428 }
bellard1d14ffa2005-10-30 18:58:22 +00001429 continue;
1430 }
1431
1432 if (!live) {
1433 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1434 if (sw->active) {
1435 free = audio_get_free (sw);
1436 if (free > 0) {
1437 sw->callback.fn (sw->callback.opaque, free);
1438 }
1439 }
1440 }
1441 continue;
1442 }
1443
bellard8ead62c2006-07-04 16:51:32 +00001444 prev_rpos = hw->rpos;
malcbdff2532009-09-18 11:37:39 +04001445 played = hw->pcm_ops->run_out (hw, live);
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +03001446 replay_audio_out(&played);
Alistair Francis470bcab2018-02-03 09:43:02 +01001447 if (audio_bug(__func__, hw->rpos >= hw->samples)) {
bellard1d14ffa2005-10-30 18:58:22 +00001448 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1449 hw->rpos, hw->samples, played);
1450 hw->rpos = 0;
1451 }
1452
1453#ifdef DEBUG_OUT
bellardc0fe3822005-11-05 18:55:28 +00001454 dolog ("played=%d\n", played);
bellard1d14ffa2005-10-30 18:58:22 +00001455#endif
1456
1457 if (played) {
1458 hw->ts_helper += played;
bellard8ead62c2006-07-04 16:51:32 +00001459 audio_capture_mix_and_clear (hw, prev_rpos, played);
bellard1d14ffa2005-10-30 18:58:22 +00001460 }
1461
bellardc0fe3822005-11-05 18:55:28 +00001462 cleanup_required = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001463 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
bellard1d14ffa2005-10-30 18:58:22 +00001464 if (!sw->active && sw->empty) {
1465 continue;
1466 }
1467
Alistair Francis470bcab2018-02-03 09:43:02 +01001468 if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) {
bellard1d14ffa2005-10-30 18:58:22 +00001469 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1470 played, sw->total_hw_samples_mixed);
1471 played = sw->total_hw_samples_mixed;
1472 }
1473
1474 sw->total_hw_samples_mixed -= played;
1475
1476 if (!sw->total_hw_samples_mixed) {
1477 sw->empty = 1;
bellardc0fe3822005-11-05 18:55:28 +00001478 cleanup_required |= !sw->active && !sw->callback.fn;
bellard1d14ffa2005-10-30 18:58:22 +00001479 }
1480
1481 if (sw->active) {
1482 free = audio_get_free (sw);
1483 if (free > 0) {
1484 sw->callback.fn (sw->callback.opaque, free);
1485 }
1486 }
bellard85571bc2004-11-07 18:04:02 +00001487 }
bellardc0fe3822005-11-05 18:55:28 +00001488
1489 if (cleanup_required) {
bellardec36b692006-07-16 18:57:03 +00001490 SWVoiceOut *sw1;
1491
1492 sw = hw->sw_head.lh_first;
1493 while (sw) {
1494 sw1 = sw->entries.le_next;
bellardc0fe3822005-11-05 18:55:28 +00001495 if (!sw->active && !sw->callback.fn) {
malc1a7dafc2009-05-14 03:11:35 +04001496 audio_close_out (sw);
bellardc0fe3822005-11-05 18:55:28 +00001497 }
bellardec36b692006-07-16 18:57:03 +00001498 sw = sw1;
bellardc0fe3822005-11-05 18:55:28 +00001499 }
1500 }
bellard85571bc2004-11-07 18:04:02 +00001501 }
bellard1d14ffa2005-10-30 18:58:22 +00001502}
1503
bellardc0fe3822005-11-05 18:55:28 +00001504static void audio_run_in (AudioState *s)
bellard1d14ffa2005-10-30 18:58:22 +00001505{
1506 HWVoiceIn *hw = NULL;
1507
malc1a7dafc2009-05-14 03:11:35 +04001508 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
bellard1d14ffa2005-10-30 18:58:22 +00001509 SWVoiceIn *sw;
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +03001510 int captured = 0, min;
bellard1d14ffa2005-10-30 18:58:22 +00001511
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +03001512 if (replay_mode != REPLAY_MODE_PLAY) {
1513 captured = hw->pcm_ops->run_in(hw);
1514 }
1515 replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples);
bellard1d14ffa2005-10-30 18:58:22 +00001516
1517 min = audio_pcm_hw_find_min_in (hw);
1518 hw->total_samples_captured += captured - min;
1519 hw->ts_helper += captured;
1520
1521 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1522 sw->total_hw_samples_acquired -= min;
1523
1524 if (sw->active) {
1525 int avail;
1526
1527 avail = audio_get_avail (sw);
1528 if (avail > 0) {
1529 sw->callback.fn (sw->callback.opaque, avail);
1530 }
1531 }
1532 }
1533 }
1534}
1535
bellard8ead62c2006-07-04 16:51:32 +00001536static void audio_run_capture (AudioState *s)
1537{
1538 CaptureVoiceOut *cap;
1539
1540 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1541 int live, rpos, captured;
1542 HWVoiceOut *hw = &cap->hw;
1543 SWVoiceOut *sw;
1544
malcbdff2532009-09-18 11:37:39 +04001545 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
bellard8ead62c2006-07-04 16:51:32 +00001546 rpos = hw->rpos;
1547 while (live) {
1548 int left = hw->samples - rpos;
1549 int to_capture = audio_MIN (live, left);
malc1ea879e2008-12-03 22:48:44 +00001550 struct st_sample *src;
bellard8ead62c2006-07-04 16:51:32 +00001551 struct capture_callback *cb;
1552
1553 src = hw->mix_buf + rpos;
1554 hw->clip (cap->buf, src, to_capture);
1555 mixeng_clear (src, to_capture);
1556
1557 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1558 cb->ops.capture (cb->opaque, cap->buf,
1559 to_capture << hw->info.shift);
1560 }
1561 rpos = (rpos + to_capture) % hw->samples;
1562 live -= to_capture;
1563 }
1564 hw->rpos = rpos;
1565
1566 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1567 if (!sw->active && sw->empty) {
1568 continue;
1569 }
1570
Alistair Francis470bcab2018-02-03 09:43:02 +01001571 if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) {
bellard8ead62c2006-07-04 16:51:32 +00001572 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1573 captured, sw->total_hw_samples_mixed);
1574 captured = sw->total_hw_samples_mixed;
1575 }
1576
1577 sw->total_hw_samples_mixed -= captured;
1578 sw->empty = sw->total_hw_samples_mixed == 0;
1579 }
1580 }
1581}
1582
malc713a98f2009-09-12 02:28:45 +04001583void audio_run (const char *msg)
bellard571ec3d2005-11-20 16:24:34 +00001584{
malc713a98f2009-09-12 02:28:45 +04001585 AudioState *s = &glob_audio_state;
bellard571ec3d2005-11-20 16:24:34 +00001586
1587 audio_run_out (s);
1588 audio_run_in (s);
bellard8ead62c2006-07-04 16:51:32 +00001589 audio_run_capture (s);
malc713a98f2009-09-12 02:28:45 +04001590#ifdef DEBUG_POLL
1591 {
1592 static double prevtime;
1593 double currtime;
1594 struct timeval tv;
bellard571ec3d2005-11-20 16:24:34 +00001595
malc713a98f2009-09-12 02:28:45 +04001596 if (gettimeofday (&tv, NULL)) {
1597 perror ("audio_run: gettimeofday");
1598 return;
1599 }
1600
1601 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1602 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1603 prevtime = currtime;
1604 }
1605#endif
bellard571ec3d2005-11-20 16:24:34 +00001606}
1607
bellard1d14ffa2005-10-30 18:58:22 +00001608static struct audio_option audio_options[] = {
1609 /* DAC */
malc98f9f482009-08-11 20:48:02 +04001610 {
1611 .name = "DAC_FIXED_SETTINGS",
1612 .tag = AUD_OPT_BOOL,
1613 .valp = &conf.fixed_out.enabled,
1614 .descr = "Use fixed settings for host DAC"
1615 },
1616 {
1617 .name = "DAC_FIXED_FREQ",
1618 .tag = AUD_OPT_INT,
1619 .valp = &conf.fixed_out.settings.freq,
1620 .descr = "Frequency for fixed host DAC"
1621 },
1622 {
1623 .name = "DAC_FIXED_FMT",
1624 .tag = AUD_OPT_FMT,
1625 .valp = &conf.fixed_out.settings.fmt,
1626 .descr = "Format for fixed host DAC"
1627 },
1628 {
1629 .name = "DAC_FIXED_CHANNELS",
1630 .tag = AUD_OPT_INT,
1631 .valp = &conf.fixed_out.settings.nchannels,
1632 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1633 },
1634 {
1635 .name = "DAC_VOICES",
1636 .tag = AUD_OPT_INT,
1637 .valp = &conf.fixed_out.nb_voices,
1638 .descr = "Number of voices for DAC"
1639 },
malc713a98f2009-09-12 02:28:45 +04001640 {
1641 .name = "DAC_TRY_POLL",
1642 .tag = AUD_OPT_BOOL,
1643 .valp = &conf.try_poll_out,
1644 .descr = "Attempt using poll mode for DAC"
1645 },
bellard1d14ffa2005-10-30 18:58:22 +00001646 /* ADC */
malc98f9f482009-08-11 20:48:02 +04001647 {
1648 .name = "ADC_FIXED_SETTINGS",
1649 .tag = AUD_OPT_BOOL,
1650 .valp = &conf.fixed_in.enabled,
1651 .descr = "Use fixed settings for host ADC"
1652 },
1653 {
1654 .name = "ADC_FIXED_FREQ",
1655 .tag = AUD_OPT_INT,
1656 .valp = &conf.fixed_in.settings.freq,
1657 .descr = "Frequency for fixed host ADC"
1658 },
1659 {
1660 .name = "ADC_FIXED_FMT",
1661 .tag = AUD_OPT_FMT,
1662 .valp = &conf.fixed_in.settings.fmt,
1663 .descr = "Format for fixed host ADC"
1664 },
1665 {
1666 .name = "ADC_FIXED_CHANNELS",
1667 .tag = AUD_OPT_INT,
1668 .valp = &conf.fixed_in.settings.nchannels,
1669 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1670 },
1671 {
1672 .name = "ADC_VOICES",
1673 .tag = AUD_OPT_INT,
1674 .valp = &conf.fixed_in.nb_voices,
1675 .descr = "Number of voices for ADC"
1676 },
malc713a98f2009-09-12 02:28:45 +04001677 {
1678 .name = "ADC_TRY_POLL",
1679 .tag = AUD_OPT_BOOL,
Jan Kiszka0a90e342009-09-13 22:24:46 +04001680 .valp = &conf.try_poll_in,
malc713a98f2009-09-12 02:28:45 +04001681 .descr = "Attempt using poll mode for ADC"
1682 },
bellard1d14ffa2005-10-30 18:58:22 +00001683 /* Misc */
malc98f9f482009-08-11 20:48:02 +04001684 {
1685 .name = "TIMER_PERIOD",
1686 .tag = AUD_OPT_INT,
1687 .valp = &conf.period.hertz,
1688 .descr = "Timer period in HZ (0 - use lowest possible)"
1689 },
Juan Quintela2700efa2009-08-11 02:31:16 +02001690 { /* End of list */ }
bellard1d14ffa2005-10-30 18:58:22 +00001691};
1692
bellard571ec3d2005-11-20 16:24:34 +00001693static void audio_pp_nb_voices (const char *typ, int nb)
1694{
1695 switch (nb) {
1696 case 0:
1697 printf ("Does not support %s\n", typ);
1698 break;
1699 case 1:
1700 printf ("One %s voice\n", typ);
1701 break;
1702 case INT_MAX:
1703 printf ("Theoretically supports many %s voices\n", typ);
1704 break;
1705 default:
Stefan Weile7d81002011-12-10 00:19:46 +01001706 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
bellard571ec3d2005-11-20 16:24:34 +00001707 break;
1708 }
1709
1710}
1711
bellard1d14ffa2005-10-30 18:58:22 +00001712void AUD_help (void)
1713{
Gerd Hoffmannd3893a32018-03-06 08:40:47 +01001714 struct audio_driver *d;
bellard1d14ffa2005-10-30 18:58:22 +00001715
Gerd Hoffmann65ba8692018-03-06 08:40:48 +01001716 /* make sure we print the help text for modular drivers too */
1717 audio_module_load_all();
1718
bellard1d14ffa2005-10-30 18:58:22 +00001719 audio_process_options ("AUDIO", audio_options);
Gerd Hoffmannd3893a32018-03-06 08:40:47 +01001720 QLIST_FOREACH(d, &audio_drivers, next) {
bellard1d14ffa2005-10-30 18:58:22 +00001721 if (d->options) {
1722 audio_process_options (d->name, d->options);
1723 }
1724 }
1725
1726 printf ("Audio options:\n");
1727 audio_print_options ("AUDIO", audio_options);
1728 printf ("\n");
1729
1730 printf ("Available drivers:\n");
1731
Gerd Hoffmannd3893a32018-03-06 08:40:47 +01001732 QLIST_FOREACH(d, &audio_drivers, next) {
bellard1d14ffa2005-10-30 18:58:22 +00001733
1734 printf ("Name: %s\n", d->name);
1735 printf ("Description: %s\n", d->descr);
1736
bellard571ec3d2005-11-20 16:24:34 +00001737 audio_pp_nb_voices ("playback", d->max_voices_out);
1738 audio_pp_nb_voices ("capture", d->max_voices_in);
bellard1d14ffa2005-10-30 18:58:22 +00001739
1740 if (d->options) {
1741 printf ("Options:\n");
1742 audio_print_options (d->name, d->options);
1743 }
1744 else {
1745 printf ("No options\n");
1746 }
1747 printf ("\n");
1748 }
1749
1750 printf (
1751 "Options are settable through environment variables.\n"
1752 "Example:\n"
1753#ifdef _WIN32
1754 " set QEMU_AUDIO_DRV=wav\n"
bellard571ec3d2005-11-20 16:24:34 +00001755 " set QEMU_WAV_PATH=c:\\tune.wav\n"
bellard1d14ffa2005-10-30 18:58:22 +00001756#else
1757 " export QEMU_AUDIO_DRV=wav\n"
1758 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1759 "(for csh replace export with setenv in the above)\n"
1760#endif
1761 " qemu ...\n\n"
1762 );
1763}
1764
bellardc0fe3822005-11-05 18:55:28 +00001765static int audio_driver_init (AudioState *s, struct audio_driver *drv)
bellard1d14ffa2005-10-30 18:58:22 +00001766{
1767 if (drv->options) {
1768 audio_process_options (drv->name, drv->options);
1769 }
bellardc0fe3822005-11-05 18:55:28 +00001770 s->drv_opaque = drv->init ();
bellard1d14ffa2005-10-30 18:58:22 +00001771
bellardc0fe3822005-11-05 18:55:28 +00001772 if (s->drv_opaque) {
malc1a7dafc2009-05-14 03:11:35 +04001773 audio_init_nb_voices_out (drv);
1774 audio_init_nb_voices_in (drv);
bellardc0fe3822005-11-05 18:55:28 +00001775 s->drv = drv;
bellard85571bc2004-11-07 18:04:02 +00001776 return 0;
1777 }
bellard1d14ffa2005-10-30 18:58:22 +00001778 else {
1779 dolog ("Could not init `%s' audio driver\n", drv->name);
1780 return -1;
1781 }
bellard85571bc2004-11-07 18:04:02 +00001782}
1783
aliguori9781e042009-01-22 17:15:29 +00001784static void audio_vm_change_state_handler (void *opaque, int running,
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001785 RunState state)
bellard85571bc2004-11-07 18:04:02 +00001786{
bellardc0fe3822005-11-05 18:55:28 +00001787 AudioState *s = opaque;
bellard1d14ffa2005-10-30 18:58:22 +00001788 HWVoiceOut *hwo = NULL;
1789 HWVoiceIn *hwi = NULL;
bellard541e0842005-11-11 00:04:19 +00001790 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
bellard85571bc2004-11-07 18:04:02 +00001791
malc978dd632009-02-18 20:44:04 +00001792 s->vm_running = running;
malc1a7dafc2009-05-14 03:11:35 +04001793 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
malc713a98f2009-09-12 02:28:45 +04001794 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
bellard1d14ffa2005-10-30 18:58:22 +00001795 }
1796
malc1a7dafc2009-05-14 03:11:35 +04001797 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
malc713a98f2009-09-12 02:28:45 +04001798 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
bellard85571bc2004-11-07 18:04:02 +00001799 }
malc39deb1e2010-11-18 14:30:12 +03001800 audio_reset_timer (s);
bellard85571bc2004-11-07 18:04:02 +00001801}
1802
Marc-André Lureaua384c202016-08-01 15:23:43 +04001803static bool is_cleaning_up;
1804
1805bool audio_is_cleaning_up(void)
1806{
1807 return is_cleaning_up;
1808}
1809
1810void audio_cleanup(void)
bellard85571bc2004-11-07 18:04:02 +00001811{
bellardc0fe3822005-11-05 18:55:28 +00001812 AudioState *s = &glob_audio_state;
Marc-André Lureaua384c202016-08-01 15:23:43 +04001813 HWVoiceOut *hwo, *hwon;
1814 HWVoiceIn *hwi, *hwin;
bellard85571bc2004-11-07 18:04:02 +00001815
Marc-André Lureaua384c202016-08-01 15:23:43 +04001816 is_cleaning_up = true;
1817 QLIST_FOREACH_SAFE(hwo, &glob_audio_state.hw_head_out, entries, hwon) {
bellardec36b692006-07-16 18:57:03 +00001818 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +00001819
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001820 if (hwo->enabled) {
1821 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1822 }
bellard1d14ffa2005-10-30 18:58:22 +00001823 hwo->pcm_ops->fini_out (hwo);
bellard8ead62c2006-07-04 16:51:32 +00001824
bellardec36b692006-07-16 18:57:03 +00001825 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1826 CaptureVoiceOut *cap = sc->cap;
1827 struct capture_callback *cb;
1828
1829 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1830 cb->ops.destroy (cb->opaque);
1831 }
bellard8ead62c2006-07-04 16:51:32 +00001832 }
Marc-André Lureaua384c202016-08-01 15:23:43 +04001833 QLIST_REMOVE(hwo, entries);
bellard1d14ffa2005-10-30 18:58:22 +00001834 }
1835
Marc-André Lureaua384c202016-08-01 15:23:43 +04001836 QLIST_FOREACH_SAFE(hwi, &glob_audio_state.hw_head_in, entries, hwin) {
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001837 if (hwi->enabled) {
1838 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1839 }
bellard1d14ffa2005-10-30 18:58:22 +00001840 hwi->pcm_ops->fini_in (hwi);
Marc-André Lureaua384c202016-08-01 15:23:43 +04001841 QLIST_REMOVE(hwi, entries);
bellard85571bc2004-11-07 18:04:02 +00001842 }
bellardc0fe3822005-11-05 18:55:28 +00001843
1844 if (s->drv) {
1845 s->drv->fini (s->drv_opaque);
Marc-André Lureaua384c202016-08-01 15:23:43 +04001846 s->drv = NULL;
bellardc0fe3822005-11-05 18:55:28 +00001847 }
bellard85571bc2004-11-07 18:04:02 +00001848}
1849
Juan Quintelad959fce2009-12-02 11:49:34 +01001850static const VMStateDescription vmstate_audio = {
1851 .name = "audio",
1852 .version_id = 1,
1853 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +02001854 .fields = (VMStateField[]) {
Juan Quintelad959fce2009-12-02 11:49:34 +01001855 VMSTATE_END_OF_LIST()
bellard1d14ffa2005-10-30 18:58:22 +00001856 }
Juan Quintelad959fce2009-12-02 11:49:34 +01001857};
bellard85571bc2004-11-07 18:04:02 +00001858
malc1a7dafc2009-05-14 03:11:35 +04001859static void audio_init (void)
bellard85571bc2004-11-07 18:04:02 +00001860{
bellard1d14ffa2005-10-30 18:58:22 +00001861 size_t i;
bellard85571bc2004-11-07 18:04:02 +00001862 int done = 0;
1863 const char *drvname;
malc713a98f2009-09-12 02:28:45 +04001864 VMChangeStateEntry *e;
bellardc0fe3822005-11-05 18:55:28 +00001865 AudioState *s = &glob_audio_state;
Gerd Hoffmannd3893a32018-03-06 08:40:47 +01001866 struct audio_driver *driver;
bellard85571bc2004-11-07 18:04:02 +00001867
Paul Brook0d9acba2009-05-12 12:02:38 +01001868 if (s->drv) {
malc1a7dafc2009-05-14 03:11:35 +04001869 return;
Paul Brook0d9acba2009-05-12 12:02:38 +01001870 }
1871
Blue Swirl72cf2d42009-09-12 07:36:22 +00001872 QLIST_INIT (&s->hw_head_out);
1873 QLIST_INIT (&s->hw_head_in);
1874 QLIST_INIT (&s->cap_head);
Marc-André Lureaua384c202016-08-01 15:23:43 +04001875 atexit(audio_cleanup);
bellard571ec3d2005-11-20 16:24:34 +00001876
Alex Blighbc72ad62013-08-21 16:03:08 +01001877 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
bellard571ec3d2005-11-20 16:24:34 +00001878
bellard1d14ffa2005-10-30 18:58:22 +00001879 audio_process_options ("AUDIO", audio_options);
bellard85571bc2004-11-07 18:04:02 +00001880
bellardc0fe3822005-11-05 18:55:28 +00001881 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1882 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1883
bellard1d14ffa2005-10-30 18:58:22 +00001884 if (s->nb_hw_voices_out <= 0) {
bellard571ec3d2005-11-20 16:24:34 +00001885 dolog ("Bogus number of playback voices %d, setting to 1\n",
bellard1d14ffa2005-10-30 18:58:22 +00001886 s->nb_hw_voices_out);
1887 s->nb_hw_voices_out = 1;
bellard85571bc2004-11-07 18:04:02 +00001888 }
1889
bellard1d14ffa2005-10-30 18:58:22 +00001890 if (s->nb_hw_voices_in <= 0) {
bellard571ec3d2005-11-20 16:24:34 +00001891 dolog ("Bogus number of capture voices %d, setting to 0\n",
bellard1d14ffa2005-10-30 18:58:22 +00001892 s->nb_hw_voices_in);
bellard571ec3d2005-11-20 16:24:34 +00001893 s->nb_hw_voices_in = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001894 }
1895
1896 {
1897 int def;
1898 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1899 }
1900
bellard85571bc2004-11-07 18:04:02 +00001901 if (drvname) {
Gerd Hoffmannd3893a32018-03-06 08:40:47 +01001902 driver = audio_driver_lookup(drvname);
1903 if (driver) {
1904 done = !audio_driver_init(s, driver);
1905 } else {
bellard85571bc2004-11-07 18:04:02 +00001906 dolog ("Unknown audio driver `%s'\n", drvname);
bellard1d14ffa2005-10-30 18:58:22 +00001907 dolog ("Run with -audio-help to list available drivers\n");
bellard85571bc2004-11-07 18:04:02 +00001908 }
1909 }
1910
bellard85571bc2004-11-07 18:04:02 +00001911 if (!done) {
Gerd Hoffmannd3893a32018-03-06 08:40:47 +01001912 for (i = 0; !done && i < ARRAY_SIZE(audio_prio_list); i++) {
1913 driver = audio_driver_lookup(audio_prio_list[i]);
1914 if (driver && driver->can_be_default) {
1915 done = !audio_driver_init(s, driver);
bellard1d14ffa2005-10-30 18:58:22 +00001916 }
bellard85571bc2004-11-07 18:04:02 +00001917 }
1918 }
1919
bellard85571bc2004-11-07 18:04:02 +00001920 if (!done) {
Gerd Hoffmannd3893a32018-03-06 08:40:47 +01001921 driver = audio_driver_lookup("none");
1922 done = !audio_driver_init(s, driver);
Markus Armbruster7e274652015-12-17 17:35:20 +01001923 assert(done);
1924 dolog("warning: Using timer based audio emulation\n");
bellard85571bc2004-11-07 18:04:02 +00001925 }
bellard1d14ffa2005-10-30 18:58:22 +00001926
Paul Brook0d9acba2009-05-12 12:02:38 +01001927 if (conf.period.hertz <= 0) {
1928 if (conf.period.hertz < 0) {
1929 dolog ("warning: Timer period is negative - %d "
1930 "treating as zero\n",
1931 conf.period.hertz);
bellard1d14ffa2005-10-30 18:58:22 +00001932 }
Paul Brook0d9acba2009-05-12 12:02:38 +01001933 conf.period.ticks = 1;
1934 } else {
Rutuja Shah73bcb242016-03-21 21:32:30 +05301935 conf.period.ticks = NANOSECONDS_PER_SECOND / conf.period.hertz;
bellard1d14ffa2005-10-30 18:58:22 +00001936 }
Paul Brook0d9acba2009-05-12 12:02:38 +01001937
1938 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1939 if (!e) {
1940 dolog ("warning: Could not register change state handler\n"
1941 "(Audio can continue looping even after stopping the VM)\n");
bellard1d14ffa2005-10-30 18:58:22 +00001942 }
1943
Blue Swirl72cf2d42009-09-12 07:36:22 +00001944 QLIST_INIT (&s->card_head);
Alex Williamson0be71e32010-06-25 11:09:07 -06001945 vmstate_register (NULL, 0, &vmstate_audio, s);
bellard85571bc2004-11-07 18:04:02 +00001946}
bellard8ead62c2006-07-04 16:51:32 +00001947
malc1a7dafc2009-05-14 03:11:35 +04001948void AUD_register_card (const char *name, QEMUSoundCard *card)
1949{
1950 audio_init ();
Anthony Liguori7267c092011-08-20 22:09:37 -05001951 card->name = g_strdup (name);
malc1a7dafc2009-05-14 03:11:35 +04001952 memset (&card->entries, 0, sizeof (card->entries));
Blue Swirl72cf2d42009-09-12 07:36:22 +00001953 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
malc1a7dafc2009-05-14 03:11:35 +04001954}
1955
1956void AUD_remove_card (QEMUSoundCard *card)
1957{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001958 QLIST_REMOVE (card, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001959 g_free (card->name);
malc1a7dafc2009-05-14 03:11:35 +04001960}
1961
1962
bellardec36b692006-07-16 18:57:03 +00001963CaptureVoiceOut *AUD_add_capture (
malc1ea879e2008-12-03 22:48:44 +00001964 struct audsettings *as,
bellard8ead62c2006-07-04 16:51:32 +00001965 struct audio_capture_ops *ops,
1966 void *cb_opaque
1967 )
1968{
malc1a7dafc2009-05-14 03:11:35 +04001969 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +00001970 CaptureVoiceOut *cap;
1971 struct capture_callback *cb;
1972
bellardec36b692006-07-16 18:57:03 +00001973 if (audio_validate_settings (as)) {
bellard8ead62c2006-07-04 16:51:32 +00001974 dolog ("Invalid settings were passed when trying to add capture\n");
1975 audio_print_settings (as);
bellardec36b692006-07-16 18:57:03 +00001976 goto err0;
bellard8ead62c2006-07-04 16:51:32 +00001977 }
1978
Alistair Francis470bcab2018-02-03 09:43:02 +01001979 cb = audio_calloc(__func__, 1, sizeof(*cb));
bellard8ead62c2006-07-04 16:51:32 +00001980 if (!cb) {
1981 dolog ("Could not allocate capture callback information, size %zu\n",
1982 sizeof (*cb));
1983 goto err0;
1984 }
1985 cb->ops = *ops;
1986 cb->opaque = cb_opaque;
1987
malc1a7dafc2009-05-14 03:11:35 +04001988 cap = audio_pcm_capture_find_specific (as);
bellard8ead62c2006-07-04 16:51:32 +00001989 if (cap) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001990 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
bellardec36b692006-07-16 18:57:03 +00001991 return cap;
bellard8ead62c2006-07-04 16:51:32 +00001992 }
1993 else {
1994 HWVoiceOut *hw;
1995 CaptureVoiceOut *cap;
1996
Alistair Francis470bcab2018-02-03 09:43:02 +01001997 cap = audio_calloc(__func__, 1, sizeof(*cap));
bellard8ead62c2006-07-04 16:51:32 +00001998 if (!cap) {
1999 dolog ("Could not allocate capture voice, size %zu\n",
2000 sizeof (*cap));
2001 goto err1;
2002 }
2003
2004 hw = &cap->hw;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002005 QLIST_INIT (&hw->sw_head);
2006 QLIST_INIT (&cap->cb_head);
bellard8ead62c2006-07-04 16:51:32 +00002007
2008 /* XXX find a more elegant way */
2009 hw->samples = 4096 * 4;
Alistair Francis470bcab2018-02-03 09:43:02 +01002010 hw->mix_buf = audio_calloc(__func__, hw->samples,
2011 sizeof(struct st_sample));
bellard8ead62c2006-07-04 16:51:32 +00002012 if (!hw->mix_buf) {
2013 dolog ("Could not allocate capture mix buffer (%d samples)\n",
2014 hw->samples);
2015 goto err2;
2016 }
2017
bellardd929eba2006-07-04 21:47:22 +00002018 audio_pcm_init_info (&hw->info, as);
bellard8ead62c2006-07-04 16:51:32 +00002019
Alistair Francis470bcab2018-02-03 09:43:02 +01002020 cap->buf = audio_calloc(__func__, hw->samples, 1 << hw->info.shift);
bellard8ead62c2006-07-04 16:51:32 +00002021 if (!cap->buf) {
2022 dolog ("Could not allocate capture buffer "
2023 "(%d samples, each %d bytes)\n",
2024 hw->samples, 1 << hw->info.shift);
2025 goto err3;
2026 }
2027
2028 hw->clip = mixeng_clip
2029 [hw->info.nchannels == 2]
2030 [hw->info.sign]
bellardd929eba2006-07-04 21:47:22 +00002031 [hw->info.swap_endianness]
thsf941aa22007-02-17 22:19:29 +00002032 [audio_bits_to_index (hw->info.bits)];
bellard8ead62c2006-07-04 16:51:32 +00002033
Blue Swirl72cf2d42009-09-12 07:36:22 +00002034 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
2035 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
bellard8ead62c2006-07-04 16:51:32 +00002036
Marc-André Lureaua384c202016-08-01 15:23:43 +04002037 QLIST_FOREACH(hw, &glob_audio_state.hw_head_out, entries) {
malc1a7dafc2009-05-14 03:11:35 +04002038 audio_attach_capture (hw);
bellard8ead62c2006-07-04 16:51:32 +00002039 }
bellardec36b692006-07-16 18:57:03 +00002040 return cap;
bellard8ead62c2006-07-04 16:51:32 +00002041
2042 err3:
Anthony Liguori7267c092011-08-20 22:09:37 -05002043 g_free (cap->hw.mix_buf);
bellard8ead62c2006-07-04 16:51:32 +00002044 err2:
Anthony Liguori7267c092011-08-20 22:09:37 -05002045 g_free (cap);
bellard8ead62c2006-07-04 16:51:32 +00002046 err1:
Anthony Liguori7267c092011-08-20 22:09:37 -05002047 g_free (cb);
bellard8ead62c2006-07-04 16:51:32 +00002048 err0:
bellardec36b692006-07-16 18:57:03 +00002049 return NULL;
2050 }
2051}
2052
2053void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2054{
2055 struct capture_callback *cb;
2056
2057 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2058 if (cb->opaque == cb_opaque) {
2059 cb->ops.destroy (cb_opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002060 QLIST_REMOVE (cb, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002061 g_free (cb);
bellardec36b692006-07-16 18:57:03 +00002062
2063 if (!cap->cb_head.lh_first) {
2064 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
bellarda3c25992006-07-18 21:09:59 +00002065
bellardec36b692006-07-16 18:57:03 +00002066 while (sw) {
bellarda3c25992006-07-18 21:09:59 +00002067 SWVoiceCap *sc = (SWVoiceCap *) sw;
bellardec36b692006-07-16 18:57:03 +00002068#ifdef DEBUG_CAPTURE
2069 dolog ("freeing %s\n", sw->name);
2070#endif
bellarda3c25992006-07-18 21:09:59 +00002071
bellardec36b692006-07-16 18:57:03 +00002072 sw1 = sw->entries.le_next;
2073 if (sw->rate) {
2074 st_rate_stop (sw->rate);
2075 sw->rate = NULL;
2076 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002077 QLIST_REMOVE (sw, entries);
2078 QLIST_REMOVE (sc, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002079 g_free (sc);
bellardec36b692006-07-16 18:57:03 +00002080 sw = sw1;
2081 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002082 QLIST_REMOVE (cap, entries);
Gerd Hoffmann3268a842017-04-28 09:56:12 +02002083 g_free (cap->hw.mix_buf);
2084 g_free (cap->buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05002085 g_free (cap);
bellardec36b692006-07-16 18:57:03 +00002086 }
2087 return;
2088 }
bellard8ead62c2006-07-04 16:51:32 +00002089 }
2090}
balrog683efdc2008-05-04 10:21:03 +00002091
2092void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2093{
2094 if (sw) {
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002095 HWVoiceOut *hw = sw->hw;
2096
balrog683efdc2008-05-04 10:21:03 +00002097 sw->vol.mute = mute;
2098 sw->vol.l = nominal_volume.l * lvol / 255;
2099 sw->vol.r = nominal_volume.r * rvol / 255;
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002100
2101 if (hw->pcm_ops->ctl_out) {
2102 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2103 }
balrog683efdc2008-05-04 10:21:03 +00002104 }
2105}
2106
2107void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2108{
2109 if (sw) {
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002110 HWVoiceIn *hw = sw->hw;
2111
balrog683efdc2008-05-04 10:21:03 +00002112 sw->vol.mute = mute;
2113 sw->vol.l = nominal_volume.l * lvol / 255;
2114 sw->vol.r = nominal_volume.r * rvol / 255;
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002115
2116 if (hw->pcm_ops->ctl_in) {
2117 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);
2118 }
balrog683efdc2008-05-04 10:21:03 +00002119 }
2120}