blob: 21f7b0b0e679f92b1f34f2f9f81fb7d76738f5f3 [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"
bellard85571bc2004-11-07 18:04:02 +000032
bellard1d14ffa2005-10-30 18:58:22 +000033#define AUDIO_CAP "audio"
34#include "audio_int.h"
bellard85571bc2004-11-07 18:04:02 +000035
bellard1d14ffa2005-10-30 18:58:22 +000036/* #define DEBUG_LIVE */
37/* #define DEBUG_OUT */
bellard8ead62c2006-07-04 16:51:32 +000038/* #define DEBUG_CAPTURE */
malc713a98f2009-09-12 02:28:45 +040039/* #define DEBUG_POLL */
bellard1d14ffa2005-10-30 18:58:22 +000040
bellardc0fe3822005-11-05 18:55:28 +000041#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
42
Juan Quintela2358a492009-07-27 16:13:25 +020043
44/* Order of CONFIG_AUDIO_DRIVERS is import.
45 The 1st one is the one used by default, that is the reason
46 that we generate the list.
47*/
bellard1d14ffa2005-10-30 18:58:22 +000048static struct audio_driver *drvtab[] = {
Gerd Hoffmann3e313752010-11-09 17:29:46 +010049#ifdef CONFIG_SPICE
50 &spice_audio_driver,
51#endif
Juan Quintela2358a492009-07-27 16:13:25 +020052 CONFIG_AUDIO_DRIVERS
bellard1d14ffa2005-10-30 18:58:22 +000053 &no_audio_driver,
54 &wav_audio_driver
55};
bellard85571bc2004-11-07 18:04:02 +000056
bellardc0fe3822005-11-05 18:55:28 +000057struct fixed_settings {
58 int enabled;
59 int nb_voices;
60 int greedy;
malc1ea879e2008-12-03 22:48:44 +000061 struct audsettings settings;
bellardc0fe3822005-11-05 18:55:28 +000062};
bellard1d14ffa2005-10-30 18:58:22 +000063
bellardc0fe3822005-11-05 18:55:28 +000064static struct {
65 struct fixed_settings fixed_out;
66 struct fixed_settings fixed_in;
67 union {
malcc310de82008-11-12 20:36:27 +000068 int hertz;
bellardc0fe3822005-11-05 18:55:28 +000069 int64_t ticks;
70 } period;
malc713a98f2009-09-12 02:28:45 +040071 int try_poll_in;
72 int try_poll_out;
bellardc0fe3822005-11-05 18:55:28 +000073} conf = {
Juan Quintela14658cd2009-07-20 20:57:00 +020074 .fixed_out = { /* DAC fixed settings */
75 .enabled = 1,
76 .nb_voices = 1,
77 .greedy = 1,
78 .settings = {
79 .freq = 44100,
80 .nchannels = 2,
81 .fmt = AUD_FMT_S16,
82 .endianness = AUDIO_HOST_ENDIANNESS,
bellardc0fe3822005-11-05 18:55:28 +000083 }
84 },
bellard1d14ffa2005-10-30 18:58:22 +000085
Juan Quintela14658cd2009-07-20 20:57:00 +020086 .fixed_in = { /* ADC fixed settings */
87 .enabled = 1,
88 .nb_voices = 1,
89 .greedy = 1,
90 .settings = {
91 .freq = 44100,
92 .nchannels = 2,
93 .fmt = AUD_FMT_S16,
94 .endianness = AUDIO_HOST_ENDIANNESS,
bellardc0fe3822005-11-05 18:55:28 +000095 }
96 },
bellard1d14ffa2005-10-30 18:58:22 +000097
Hans de Goede40a814b2013-10-09 21:38:32 +020098 .period = { .hertz = 100 },
malc713a98f2009-09-12 02:28:45 +040099 .try_poll_in = 1,
100 .try_poll_out = 1,
bellard1d14ffa2005-10-30 18:58:22 +0000101};
102
bellardc0fe3822005-11-05 18:55:28 +0000103static AudioState glob_audio_state;
104
Michael Walle00e07672011-01-05 01:05:47 +0100105const struct mixeng_volume nominal_volume = {
Juan Quintela14658cd2009-07-20 20:57:00 +0200106 .mute = 0,
bellard1d14ffa2005-10-30 18:58:22 +0000107#ifdef FLOAT_MIXENG
Juan Quintela14658cd2009-07-20 20:57:00 +0200108 .r = 1.0,
109 .l = 1.0,
bellard1d14ffa2005-10-30 18:58:22 +0000110#else
Juan Quintela14658cd2009-07-20 20:57:00 +0200111 .r = 1ULL << 32,
112 .l = 1ULL << 32,
bellard1d14ffa2005-10-30 18:58:22 +0000113#endif
bellard85571bc2004-11-07 18:04:02 +0000114};
115
bellard1d14ffa2005-10-30 18:58:22 +0000116#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
117#error No its not
118#else
malc82584e22010-01-17 02:03:30 +0300119static void audio_print_options (const char *prefix,
120 struct audio_option *opt);
121
bellard1d14ffa2005-10-30 18:58:22 +0000122int audio_bug (const char *funcname, int cond)
bellard85571bc2004-11-07 18:04:02 +0000123{
bellard1d14ffa2005-10-30 18:58:22 +0000124 if (cond) {
125 static int shown;
126
bellard8ead62c2006-07-04 16:51:32 +0000127 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
bellard1d14ffa2005-10-30 18:58:22 +0000128 if (!shown) {
malc82584e22010-01-17 02:03:30 +0300129 struct audio_driver *d;
130
bellard1d14ffa2005-10-30 18:58:22 +0000131 shown = 1;
132 AUD_log (NULL, "Save all your work and restart without audio\n");
malc155a8ad2009-09-18 11:52:45 +0400133 AUD_log (NULL, "Please send bug report to av1474@comtv.ru\n");
bellard1d14ffa2005-10-30 18:58:22 +0000134 AUD_log (NULL, "I am sorry\n");
malc82584e22010-01-17 02:03:30 +0300135 d = glob_audio_state.drv;
136 if (d) {
137 audio_print_options (d->name, d->options);
138 }
bellard1d14ffa2005-10-30 18:58:22 +0000139 }
140 AUD_log (NULL, "Context:\n");
141
142#if defined AUDIO_BREAKPOINT_ON_BUG
143# if defined HOST_I386
144# if defined __GNUC__
145 __asm__ ("int3");
146# elif defined _MSC_VER
147 _asm _emit 0xcc;
148# else
149 abort ();
150# endif
151# else
152 abort ();
153# endif
154#endif
155 }
156
157 return cond;
158}
159#endif
160
thsf941aa22007-02-17 22:19:29 +0000161static inline int audio_bits_to_index (int bits)
162{
163 switch (bits) {
164 case 8:
165 return 0;
166
167 case 16:
168 return 1;
169
170 case 32:
171 return 2;
172
173 default:
174 audio_bug ("bits_to_index", 1);
175 AUD_log (NULL, "invalid bits %d\n", bits);
176 return 0;
177 }
178}
179
bellardc0fe3822005-11-05 18:55:28 +0000180void *audio_calloc (const char *funcname, int nmemb, size_t size)
181{
182 int cond;
183 size_t len;
184
185 len = nmemb * size;
186 cond = !nmemb || !size;
187 cond |= nmemb < 0;
188 cond |= len < size;
189
190 if (audio_bug ("audio_calloc", cond)) {
191 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
192 funcname);
bellard541e0842005-11-11 00:04:19 +0000193 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
bellardc0fe3822005-11-05 18:55:28 +0000194 return NULL;
195 }
196
Anthony Liguori7267c092011-08-20 22:09:37 -0500197 return g_malloc0 (len);
bellardc0fe3822005-11-05 18:55:28 +0000198}
199
bellard1d14ffa2005-10-30 18:58:22 +0000200static char *audio_alloc_prefix (const char *s)
201{
202 const char qemu_prefix[] = "QEMU_";
aliguori090f1fa2009-02-05 22:05:58 +0000203 size_t len, i;
204 char *r, *u;
bellard1d14ffa2005-10-30 18:58:22 +0000205
206 if (!s) {
207 return NULL;
208 }
209
210 len = strlen (s);
Anthony Liguori7267c092011-08-20 22:09:37 -0500211 r = g_malloc (len + sizeof (qemu_prefix));
bellard1d14ffa2005-10-30 18:58:22 +0000212
aliguori090f1fa2009-02-05 22:05:58 +0000213 u = r + sizeof (qemu_prefix) - 1;
bellard1d14ffa2005-10-30 18:58:22 +0000214
aliguori090f1fa2009-02-05 22:05:58 +0000215 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
216 pstrcat (r, len + sizeof (qemu_prefix), s);
bellard1d14ffa2005-10-30 18:58:22 +0000217
aliguori090f1fa2009-02-05 22:05:58 +0000218 for (i = 0; i < len; ++i) {
219 u[i] = qemu_toupper(u[i]);
bellard1d14ffa2005-10-30 18:58:22 +0000220 }
aliguori090f1fa2009-02-05 22:05:58 +0000221
bellard1d14ffa2005-10-30 18:58:22 +0000222 return r;
223}
224
pbrook9596ebb2007-11-18 01:44:38 +0000225static const char *audio_audfmt_to_string (audfmt_e fmt)
bellard1d14ffa2005-10-30 18:58:22 +0000226{
227 switch (fmt) {
228 case AUD_FMT_U8:
229 return "U8";
230
231 case AUD_FMT_U16:
232 return "U16";
233
234 case AUD_FMT_S8:
235 return "S8";
236
237 case AUD_FMT_S16:
238 return "S16";
thsf941aa22007-02-17 22:19:29 +0000239
240 case AUD_FMT_U32:
241 return "U32";
242
243 case AUD_FMT_S32:
244 return "S32";
bellard1d14ffa2005-10-30 18:58:22 +0000245 }
246
247 dolog ("Bogus audfmt %d returning S16\n", fmt);
248 return "S16";
249}
250
pbrook9596ebb2007-11-18 01:44:38 +0000251static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
252 int *defaultp)
bellard1d14ffa2005-10-30 18:58:22 +0000253{
254 if (!strcasecmp (s, "u8")) {
255 *defaultp = 0;
256 return AUD_FMT_U8;
257 }
258 else if (!strcasecmp (s, "u16")) {
259 *defaultp = 0;
260 return AUD_FMT_U16;
261 }
thsf941aa22007-02-17 22:19:29 +0000262 else if (!strcasecmp (s, "u32")) {
263 *defaultp = 0;
264 return AUD_FMT_U32;
265 }
bellard1d14ffa2005-10-30 18:58:22 +0000266 else if (!strcasecmp (s, "s8")) {
267 *defaultp = 0;
268 return AUD_FMT_S8;
269 }
270 else if (!strcasecmp (s, "s16")) {
271 *defaultp = 0;
272 return AUD_FMT_S16;
273 }
thsf941aa22007-02-17 22:19:29 +0000274 else if (!strcasecmp (s, "s32")) {
275 *defaultp = 0;
276 return AUD_FMT_S32;
277 }
bellard1d14ffa2005-10-30 18:58:22 +0000278 else {
279 dolog ("Bogus audio format `%s' using %s\n",
280 s, audio_audfmt_to_string (defval));
281 *defaultp = 1;
282 return defval;
283 }
284}
285
286static audfmt_e audio_get_conf_fmt (const char *envname,
287 audfmt_e defval,
288 int *defaultp)
289{
290 const char *var = getenv (envname);
291 if (!var) {
292 *defaultp = 1;
293 return defval;
294 }
295 return audio_string_to_audfmt (var, defval, defaultp);
296}
297
298static int audio_get_conf_int (const char *key, int defval, int *defaultp)
299{
300 int val;
bellard85571bc2004-11-07 18:04:02 +0000301 char *strval;
302
303 strval = getenv (key);
304 if (strval) {
bellard1d14ffa2005-10-30 18:58:22 +0000305 *defaultp = 0;
bellard85571bc2004-11-07 18:04:02 +0000306 val = atoi (strval);
bellard1d14ffa2005-10-30 18:58:22 +0000307 return val;
bellard85571bc2004-11-07 18:04:02 +0000308 }
bellard1d14ffa2005-10-30 18:58:22 +0000309 else {
310 *defaultp = 1;
311 return defval;
312 }
bellard85571bc2004-11-07 18:04:02 +0000313}
314
bellard1d14ffa2005-10-30 18:58:22 +0000315static const char *audio_get_conf_str (const char *key,
316 const char *defval,
317 int *defaultp)
bellard85571bc2004-11-07 18:04:02 +0000318{
319 const char *val = getenv (key);
bellard1d14ffa2005-10-30 18:58:22 +0000320 if (!val) {
321 *defaultp = 1;
bellard85571bc2004-11-07 18:04:02 +0000322 return defval;
bellard1d14ffa2005-10-30 18:58:22 +0000323 }
324 else {
325 *defaultp = 0;
bellard85571bc2004-11-07 18:04:02 +0000326 return val;
bellard1d14ffa2005-10-30 18:58:22 +0000327 }
bellard85571bc2004-11-07 18:04:02 +0000328}
329
bellard541e0842005-11-11 00:04:19 +0000330void AUD_vlog (const char *cap, const char *fmt, va_list ap)
331{
Kővágó, Zoltán06ac27f2015-06-12 14:33:02 +0200332 if (cap) {
333 fprintf(stderr, "%s: ", cap);
bellard541e0842005-11-11 00:04:19 +0000334 }
bellard541e0842005-11-11 00:04:19 +0000335
Kővágó, Zoltán06ac27f2015-06-12 14:33:02 +0200336 vfprintf(stderr, fmt, ap);
bellard541e0842005-11-11 00:04:19 +0000337}
338
bellardfb065182004-11-09 23:09:44 +0000339void AUD_log (const char *cap, const char *fmt, ...)
bellard85571bc2004-11-07 18:04:02 +0000340{
341 va_list ap;
bellard85571bc2004-11-07 18:04:02 +0000342
bellard541e0842005-11-11 00:04:19 +0000343 va_start (ap, fmt);
344 AUD_vlog (cap, fmt, ap);
345 va_end (ap);
bellard85571bc2004-11-07 18:04:02 +0000346}
347
bellard1d14ffa2005-10-30 18:58:22 +0000348static void audio_print_options (const char *prefix,
349 struct audio_option *opt)
bellard85571bc2004-11-07 18:04:02 +0000350{
bellard1d14ffa2005-10-30 18:58:22 +0000351 char *uprefix;
352
353 if (!prefix) {
354 dolog ("No prefix specified\n");
355 return;
356 }
357
358 if (!opt) {
359 dolog ("No options\n");
360 return;
361 }
362
363 uprefix = audio_alloc_prefix (prefix);
364
365 for (; opt->name; opt++) {
366 const char *state = "default";
367 printf (" %s_%s: ", uprefix, opt->name);
368
thsfe8f0962007-07-12 10:59:21 +0000369 if (opt->overriddenp && *opt->overriddenp) {
bellard1d14ffa2005-10-30 18:58:22 +0000370 state = "current";
371 }
372
373 switch (opt->tag) {
374 case AUD_OPT_BOOL:
375 {
376 int *intp = opt->valp;
377 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
378 }
379 break;
380
381 case AUD_OPT_INT:
382 {
383 int *intp = opt->valp;
384 printf ("integer, %s = %d\n", state, *intp);
385 }
386 break;
387
388 case AUD_OPT_FMT:
389 {
390 audfmt_e *fmtp = opt->valp;
391 printf (
balrogca9cc282008-01-14 04:24:29 +0000392 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
bellard1d14ffa2005-10-30 18:58:22 +0000393 state,
394 audio_audfmt_to_string (*fmtp)
395 );
396 }
397 break;
398
399 case AUD_OPT_STR:
400 {
401 const char **strp = opt->valp;
402 printf ("string, %s = %s\n",
403 state,
404 *strp ? *strp : "(not set)");
405 }
406 break;
407
408 default:
409 printf ("???\n");
410 dolog ("Bad value tag for option %s_%s %d\n",
411 uprefix, opt->name, opt->tag);
412 break;
413 }
414 printf (" %s\n", opt->descr);
415 }
416
Anthony Liguori7267c092011-08-20 22:09:37 -0500417 g_free (uprefix);
bellard85571bc2004-11-07 18:04:02 +0000418}
419
bellard1d14ffa2005-10-30 18:58:22 +0000420static void audio_process_options (const char *prefix,
421 struct audio_option *opt)
422{
423 char *optname;
424 const char qemu_prefix[] = "QEMU_";
blueswir1363a37d2008-08-21 17:58:08 +0000425 size_t preflen, optlen;
bellard1d14ffa2005-10-30 18:58:22 +0000426
427 if (audio_bug (AUDIO_FUNC, !prefix)) {
428 dolog ("prefix = NULL\n");
429 return;
430 }
431
432 if (audio_bug (AUDIO_FUNC, !opt)) {
433 dolog ("opt = NULL\n");
434 return;
435 }
436
437 preflen = strlen (prefix);
438
439 for (; opt->name; opt++) {
440 size_t len, i;
441 int def;
442
443 if (!opt->valp) {
444 dolog ("Option value pointer for `%s' is not set\n",
445 opt->name);
446 continue;
447 }
448
449 len = strlen (opt->name);
bellardc0fe3822005-11-05 18:55:28 +0000450 /* len of opt->name + len of prefix + size of qemu_prefix
451 * (includes trailing zero) + zero + underscore (on behalf of
452 * sizeof) */
blueswir1363a37d2008-08-21 17:58:08 +0000453 optlen = len + preflen + sizeof (qemu_prefix) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500454 optname = g_malloc (optlen);
bellard1d14ffa2005-10-30 18:58:22 +0000455
blueswir1363a37d2008-08-21 17:58:08 +0000456 pstrcpy (optname, optlen, qemu_prefix);
bellardc0fe3822005-11-05 18:55:28 +0000457
458 /* copy while upper-casing, including trailing zero */
bellard1d14ffa2005-10-30 18:58:22 +0000459 for (i = 0; i <= preflen; ++i) {
blueswir1cd390082008-11-16 13:53:32 +0000460 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
bellard1d14ffa2005-10-30 18:58:22 +0000461 }
blueswir1363a37d2008-08-21 17:58:08 +0000462 pstrcat (optname, optlen, "_");
blueswir1363a37d2008-08-21 17:58:08 +0000463 pstrcat (optname, optlen, opt->name);
bellard1d14ffa2005-10-30 18:58:22 +0000464
465 def = 1;
466 switch (opt->tag) {
467 case AUD_OPT_BOOL:
468 case AUD_OPT_INT:
469 {
470 int *intp = opt->valp;
471 *intp = audio_get_conf_int (optname, *intp, &def);
472 }
473 break;
474
475 case AUD_OPT_FMT:
476 {
477 audfmt_e *fmtp = opt->valp;
478 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
479 }
480 break;
481
482 case AUD_OPT_STR:
483 {
484 const char **strp = opt->valp;
485 *strp = audio_get_conf_str (optname, *strp, &def);
486 }
487 break;
488
489 default:
490 dolog ("Bad value tag for option `%s' - %d\n",
491 optname, opt->tag);
492 break;
493 }
494
thsfe8f0962007-07-12 10:59:21 +0000495 if (!opt->overriddenp) {
496 opt->overriddenp = &opt->overridden;
bellard1d14ffa2005-10-30 18:58:22 +0000497 }
thsfe8f0962007-07-12 10:59:21 +0000498 *opt->overriddenp = !def;
Anthony Liguori7267c092011-08-20 22:09:37 -0500499 g_free (optname);
bellard1d14ffa2005-10-30 18:58:22 +0000500 }
501}
502
malc1ea879e2008-12-03 22:48:44 +0000503static void audio_print_settings (struct audsettings *as)
bellardc0fe3822005-11-05 18:55:28 +0000504{
505 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
506
507 switch (as->fmt) {
508 case AUD_FMT_S8:
509 AUD_log (NULL, "S8");
510 break;
511 case AUD_FMT_U8:
512 AUD_log (NULL, "U8");
513 break;
514 case AUD_FMT_S16:
515 AUD_log (NULL, "S16");
516 break;
517 case AUD_FMT_U16:
518 AUD_log (NULL, "U16");
519 break;
malcd50997f2008-06-22 14:10:45 +0000520 case AUD_FMT_S32:
521 AUD_log (NULL, "S32");
522 break;
523 case AUD_FMT_U32:
524 AUD_log (NULL, "U32");
525 break;
bellardc0fe3822005-11-05 18:55:28 +0000526 default:
527 AUD_log (NULL, "invalid(%d)", as->fmt);
528 break;
529 }
bellardec36b692006-07-16 18:57:03 +0000530
531 AUD_log (NULL, " endianness=");
bellardd929eba2006-07-04 21:47:22 +0000532 switch (as->endianness) {
533 case 0:
534 AUD_log (NULL, "little");
535 break;
536 case 1:
537 AUD_log (NULL, "big");
538 break;
539 default:
540 AUD_log (NULL, "invalid");
541 break;
542 }
bellardc0fe3822005-11-05 18:55:28 +0000543 AUD_log (NULL, "\n");
544}
545
malc1ea879e2008-12-03 22:48:44 +0000546static int audio_validate_settings (struct audsettings *as)
bellardc0fe3822005-11-05 18:55:28 +0000547{
548 int invalid;
549
550 invalid = as->nchannels != 1 && as->nchannels != 2;
bellardd929eba2006-07-04 21:47:22 +0000551 invalid |= as->endianness != 0 && as->endianness != 1;
bellardc0fe3822005-11-05 18:55:28 +0000552
553 switch (as->fmt) {
554 case AUD_FMT_S8:
555 case AUD_FMT_U8:
556 case AUD_FMT_S16:
557 case AUD_FMT_U16:
thsf941aa22007-02-17 22:19:29 +0000558 case AUD_FMT_S32:
559 case AUD_FMT_U32:
bellardc0fe3822005-11-05 18:55:28 +0000560 break;
561 default:
562 invalid = 1;
563 break;
564 }
565
566 invalid |= as->freq <= 0;
bellardd929eba2006-07-04 21:47:22 +0000567 return invalid ? -1 : 0;
bellardc0fe3822005-11-05 18:55:28 +0000568}
569
malc1ea879e2008-12-03 22:48:44 +0000570static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
bellard1d14ffa2005-10-30 18:58:22 +0000571{
572 int bits = 8, sign = 0;
573
bellardc0fe3822005-11-05 18:55:28 +0000574 switch (as->fmt) {
bellard1d14ffa2005-10-30 18:58:22 +0000575 case AUD_FMT_S8:
576 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100577 /* fall through */
bellard1d14ffa2005-10-30 18:58:22 +0000578 case AUD_FMT_U8:
579 break;
580
581 case AUD_FMT_S16:
582 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100583 /* fall through */
bellard1d14ffa2005-10-30 18:58:22 +0000584 case AUD_FMT_U16:
585 bits = 16;
586 break;
thsf941aa22007-02-17 22:19:29 +0000587
588 case AUD_FMT_S32:
589 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100590 /* fall through */
thsf941aa22007-02-17 22:19:29 +0000591 case AUD_FMT_U32:
592 bits = 32;
593 break;
bellard1d14ffa2005-10-30 18:58:22 +0000594 }
bellardc0fe3822005-11-05 18:55:28 +0000595 return info->freq == as->freq
596 && info->nchannels == as->nchannels
bellard1d14ffa2005-10-30 18:58:22 +0000597 && info->sign == sign
bellardd929eba2006-07-04 21:47:22 +0000598 && info->bits == bits
599 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
bellard1d14ffa2005-10-30 18:58:22 +0000600}
601
malc1ea879e2008-12-03 22:48:44 +0000602void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
bellard85571bc2004-11-07 18:04:02 +0000603{
thsf941aa22007-02-17 22:19:29 +0000604 int bits = 8, sign = 0, shift = 0;
bellard85571bc2004-11-07 18:04:02 +0000605
bellardc0fe3822005-11-05 18:55:28 +0000606 switch (as->fmt) {
bellard85571bc2004-11-07 18:04:02 +0000607 case AUD_FMT_S8:
608 sign = 1;
609 case AUD_FMT_U8:
610 break;
611
612 case AUD_FMT_S16:
613 sign = 1;
614 case AUD_FMT_U16:
615 bits = 16;
thsf941aa22007-02-17 22:19:29 +0000616 shift = 1;
617 break;
618
619 case AUD_FMT_S32:
620 sign = 1;
621 case AUD_FMT_U32:
622 bits = 32;
623 shift = 2;
bellard85571bc2004-11-07 18:04:02 +0000624 break;
625 }
626
bellardc0fe3822005-11-05 18:55:28 +0000627 info->freq = as->freq;
bellard1d14ffa2005-10-30 18:58:22 +0000628 info->bits = bits;
629 info->sign = sign;
bellardc0fe3822005-11-05 18:55:28 +0000630 info->nchannels = as->nchannels;
thsf941aa22007-02-17 22:19:29 +0000631 info->shift = (as->nchannels == 2) + shift;
bellard1d14ffa2005-10-30 18:58:22 +0000632 info->align = (1 << info->shift) - 1;
633 info->bytes_per_second = info->freq << info->shift;
bellardd929eba2006-07-04 21:47:22 +0000634 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
bellard85571bc2004-11-07 18:04:02 +0000635}
636
bellard1d14ffa2005-10-30 18:58:22 +0000637void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
bellard85571bc2004-11-07 18:04:02 +0000638{
bellard1d14ffa2005-10-30 18:58:22 +0000639 if (!len) {
640 return;
641 }
642
643 if (info->sign) {
bellarde2f909b2006-08-03 20:39:40 +0000644 memset (buf, 0x00, len << info->shift);
bellard1d14ffa2005-10-30 18:58:22 +0000645 }
646 else {
thsf941aa22007-02-17 22:19:29 +0000647 switch (info->bits) {
648 case 8:
bellarde2f909b2006-08-03 20:39:40 +0000649 memset (buf, 0x80, len << info->shift);
thsf941aa22007-02-17 22:19:29 +0000650 break;
bellard1d14ffa2005-10-30 18:58:22 +0000651
thsf941aa22007-02-17 22:19:29 +0000652 case 16:
653 {
654 int i;
655 uint16_t *p = buf;
656 int shift = info->nchannels - 1;
657 short s = INT16_MAX;
bellard1d14ffa2005-10-30 18:58:22 +0000658
thsf941aa22007-02-17 22:19:29 +0000659 if (info->swap_endianness) {
660 s = bswap16 (s);
661 }
662
663 for (i = 0; i < len << shift; i++) {
664 p[i] = s;
665 }
bellard1d14ffa2005-10-30 18:58:22 +0000666 }
thsf941aa22007-02-17 22:19:29 +0000667 break;
668
669 case 32:
670 {
671 int i;
672 uint32_t *p = buf;
673 int shift = info->nchannels - 1;
674 int32_t s = INT32_MAX;
675
676 if (info->swap_endianness) {
677 s = bswap32 (s);
678 }
679
680 for (i = 0; i < len << shift; i++) {
681 p[i] = s;
682 }
683 }
684 break;
685
686 default:
687 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
688 info->bits);
689 break;
bellard1d14ffa2005-10-30 18:58:22 +0000690 }
691 }
bellard85571bc2004-11-07 18:04:02 +0000692}
693
bellard1d14ffa2005-10-30 18:58:22 +0000694/*
bellard8ead62c2006-07-04 16:51:32 +0000695 * Capture
696 */
Michael Walle00e07672011-01-05 01:05:47 +0100697static void noop_conv (struct st_sample *dst, const void *src, int samples)
bellard8ead62c2006-07-04 16:51:32 +0000698{
699 (void) src;
700 (void) dst;
701 (void) samples;
bellard8ead62c2006-07-04 16:51:32 +0000702}
703
704static CaptureVoiceOut *audio_pcm_capture_find_specific (
malc1ea879e2008-12-03 22:48:44 +0000705 struct audsettings *as
bellard8ead62c2006-07-04 16:51:32 +0000706 )
707{
708 CaptureVoiceOut *cap;
malc1a7dafc2009-05-14 03:11:35 +0400709 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +0000710
711 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
bellardd929eba2006-07-04 21:47:22 +0000712 if (audio_pcm_info_eq (&cap->hw.info, as)) {
bellard8ead62c2006-07-04 16:51:32 +0000713 return cap;
714 }
715 }
716 return NULL;
717}
718
bellardec36b692006-07-16 18:57:03 +0000719static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
720{
721 struct capture_callback *cb;
722
723#ifdef DEBUG_CAPTURE
724 dolog ("notification %d sent\n", cmd);
725#endif
726 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
727 cb->ops.notify (cb->opaque, cmd);
728 }
729}
730
731static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
bellard8ead62c2006-07-04 16:51:32 +0000732{
733 if (cap->hw.enabled != enabled) {
bellardec36b692006-07-16 18:57:03 +0000734 audcnotification_e cmd;
bellard8ead62c2006-07-04 16:51:32 +0000735 cap->hw.enabled = enabled;
bellardec36b692006-07-16 18:57:03 +0000736 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
737 audio_notify_capture (cap, cmd);
bellard8ead62c2006-07-04 16:51:32 +0000738 }
739}
740
741static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
742{
743 HWVoiceOut *hw = &cap->hw;
744 SWVoiceOut *sw;
745 int enabled = 0;
746
bellardec36b692006-07-16 18:57:03 +0000747 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
bellard8ead62c2006-07-04 16:51:32 +0000748 if (sw->active) {
749 enabled = 1;
750 break;
751 }
752 }
bellardec36b692006-07-16 18:57:03 +0000753 audio_capture_maybe_changed (cap, enabled);
bellard8ead62c2006-07-04 16:51:32 +0000754}
755
756static void audio_detach_capture (HWVoiceOut *hw)
757{
bellardec36b692006-07-16 18:57:03 +0000758 SWVoiceCap *sc = hw->cap_head.lh_first;
bellard8ead62c2006-07-04 16:51:32 +0000759
bellardec36b692006-07-16 18:57:03 +0000760 while (sc) {
761 SWVoiceCap *sc1 = sc->entries.le_next;
762 SWVoiceOut *sw = &sc->sw;
763 CaptureVoiceOut *cap = sc->cap;
764 int was_active = sw->active;
765
bellard8ead62c2006-07-04 16:51:32 +0000766 if (sw->rate) {
767 st_rate_stop (sw->rate);
768 sw->rate = NULL;
769 }
770
Blue Swirl72cf2d42009-09-12 07:36:22 +0000771 QLIST_REMOVE (sw, entries);
772 QLIST_REMOVE (sc, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -0500773 g_free (sc);
bellardec36b692006-07-16 18:57:03 +0000774 if (was_active) {
775 /* We have removed soft voice from the capture:
776 this might have changed the overall status of the capture
777 since this might have been the only active voice */
778 audio_recalc_and_notify_capture (cap);
779 }
780 sc = sc1;
bellard8ead62c2006-07-04 16:51:32 +0000781 }
782}
783
malc1a7dafc2009-05-14 03:11:35 +0400784static int audio_attach_capture (HWVoiceOut *hw)
bellard8ead62c2006-07-04 16:51:32 +0000785{
malc1a7dafc2009-05-14 03:11:35 +0400786 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +0000787 CaptureVoiceOut *cap;
788
789 audio_detach_capture (hw);
790 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
bellardec36b692006-07-16 18:57:03 +0000791 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +0000792 SWVoiceOut *sw;
bellardec36b692006-07-16 18:57:03 +0000793 HWVoiceOut *hw_cap = &cap->hw;
bellard8ead62c2006-07-04 16:51:32 +0000794
bellardec36b692006-07-16 18:57:03 +0000795 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
796 if (!sc) {
bellard8ead62c2006-07-04 16:51:32 +0000797 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
bellardec36b692006-07-16 18:57:03 +0000798 sizeof (*sc));
bellard8ead62c2006-07-04 16:51:32 +0000799 return -1;
800 }
801
bellardec36b692006-07-16 18:57:03 +0000802 sc->cap = cap;
803 sw = &sc->sw;
bellard8ead62c2006-07-04 16:51:32 +0000804 sw->hw = hw_cap;
bellardec36b692006-07-16 18:57:03 +0000805 sw->info = hw->info;
bellard8ead62c2006-07-04 16:51:32 +0000806 sw->empty = 1;
807 sw->active = hw->enabled;
808 sw->conv = noop_conv;
809 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
malc83617102012-07-16 18:08:36 +0400810 sw->vol = nominal_volume;
bellard8ead62c2006-07-04 16:51:32 +0000811 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
812 if (!sw->rate) {
813 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
Anthony Liguori7267c092011-08-20 22:09:37 -0500814 g_free (sw);
bellard8ead62c2006-07-04 16:51:32 +0000815 return -1;
816 }
Blue Swirl72cf2d42009-09-12 07:36:22 +0000817 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
818 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
bellardec36b692006-07-16 18:57:03 +0000819#ifdef DEBUG_CAPTURE
Stefan Weil457b6542013-01-16 18:17:33 +0100820 sw->name = g_strdup_printf ("for %p %d,%d,%d",
821 hw, sw->info.freq, sw->info.bits,
822 sw->info.nchannels);
bellardec36b692006-07-16 18:57:03 +0000823 dolog ("Added %s active = %d\n", sw->name, sw->active);
824#endif
bellard8ead62c2006-07-04 16:51:32 +0000825 if (sw->active) {
bellardec36b692006-07-16 18:57:03 +0000826 audio_capture_maybe_changed (cap, 1);
bellard8ead62c2006-07-04 16:51:32 +0000827 }
828 }
829 return 0;
830}
831
832/*
bellard1d14ffa2005-10-30 18:58:22 +0000833 * Hard voice (capture)
834 */
bellardc0fe3822005-11-05 18:55:28 +0000835static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
bellard85571bc2004-11-07 18:04:02 +0000836{
bellard1d14ffa2005-10-30 18:58:22 +0000837 SWVoiceIn *sw;
838 int m = hw->total_samples_captured;
bellard85571bc2004-11-07 18:04:02 +0000839
bellard1d14ffa2005-10-30 18:58:22 +0000840 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
841 if (sw->active) {
842 m = audio_MIN (m, sw->total_hw_samples_acquired);
bellard85571bc2004-11-07 18:04:02 +0000843 }
844 }
bellard1d14ffa2005-10-30 18:58:22 +0000845 return m;
bellard85571bc2004-11-07 18:04:02 +0000846}
847
bellard1d14ffa2005-10-30 18:58:22 +0000848int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
bellard85571bc2004-11-07 18:04:02 +0000849{
bellard1d14ffa2005-10-30 18:58:22 +0000850 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
851 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
852 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
853 return 0;
854 }
855 return live;
856}
bellard85571bc2004-11-07 18:04:02 +0000857
malcddabec72009-09-18 10:59:38 +0400858int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
859 int live, int pending)
860{
861 int left = hw->samples - pending;
862 int len = audio_MIN (left, live);
863 int clipped = 0;
864
865 while (len) {
866 struct st_sample *src = hw->mix_buf + hw->rpos;
867 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
868 int samples_till_end_of_buf = hw->samples - hw->rpos;
869 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
870
871 hw->clip (dst, src, samples_to_clip);
872
873 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
874 len -= samples_to_clip;
875 clipped += samples_to_clip;
876 }
877 return clipped;
878}
879
bellard1d14ffa2005-10-30 18:58:22 +0000880/*
881 * Soft voice (capture)
882 */
bellard1d14ffa2005-10-30 18:58:22 +0000883static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
884{
885 HWVoiceIn *hw = sw->hw;
886 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
887 int rpos;
888
889 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
890 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
891 return 0;
892 }
893
894 rpos = hw->wpos - live;
895 if (rpos >= 0) {
896 return rpos;
897 }
898 else {
899 return hw->samples + rpos;
bellard85571bc2004-11-07 18:04:02 +0000900 }
901}
902
bellard1d14ffa2005-10-30 18:58:22 +0000903int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +0000904{
bellard1d14ffa2005-10-30 18:58:22 +0000905 HWVoiceIn *hw = sw->hw;
906 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
malc1ea879e2008-12-03 22:48:44 +0000907 struct st_sample *src, *dst = sw->buf;
bellard85571bc2004-11-07 18:04:02 +0000908
bellard1d14ffa2005-10-30 18:58:22 +0000909 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
bellard85571bc2004-11-07 18:04:02 +0000910
bellard1d14ffa2005-10-30 18:58:22 +0000911 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
912 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
913 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
914 return 0;
bellard85571bc2004-11-07 18:04:02 +0000915 }
bellard1d14ffa2005-10-30 18:58:22 +0000916
917 samples = size >> sw->info.shift;
918 if (!live) {
919 return 0;
920 }
921
922 swlim = (live * sw->ratio) >> 32;
bellard85571bc2004-11-07 18:04:02 +0000923 swlim = audio_MIN (swlim, samples);
924
bellard1d14ffa2005-10-30 18:58:22 +0000925 while (swlim) {
926 src = hw->conv_buf + rpos;
927 isamp = hw->wpos - rpos;
928 /* XXX: <= ? */
929 if (isamp <= 0) {
930 isamp = hw->samples - rpos;
931 }
932
933 if (!isamp) {
934 break;
935 }
936 osamp = swlim;
937
938 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
939 dolog ("osamp=%d\n", osamp);
bellardc0fe3822005-11-05 18:55:28 +0000940 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000941 }
942
943 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
944 swlim -= osamp;
945 rpos = (rpos + isamp) % hw->samples;
946 dst += osamp;
947 ret += osamp;
948 total += isamp;
949 }
950
Marc-André Lureauc01b2452012-04-17 14:32:36 +0200951 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
952 mixeng_volume (sw->buf, ret, &sw->vol);
953 }
Michael Walle00e07672011-01-05 01:05:47 +0100954
bellard571ec3d2005-11-20 16:24:34 +0000955 sw->clip (buf, sw->buf, ret);
bellard1d14ffa2005-10-30 18:58:22 +0000956 sw->total_hw_samples_acquired += total;
957 return ret << sw->info.shift;
958}
959
960/*
961 * Hard voice (playback)
962 */
963static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
964{
965 SWVoiceOut *sw;
966 int m = INT_MAX;
967 int nb_live = 0;
968
969 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
970 if (sw->active || !sw->empty) {
971 m = audio_MIN (m, sw->total_hw_samples_mixed);
972 nb_live += 1;
973 }
974 }
975
976 *nb_livep = nb_live;
977 return m;
978}
979
malcbdff2532009-09-18 11:37:39 +0400980static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
bellard1d14ffa2005-10-30 18:58:22 +0000981{
982 int smin;
malcbdff2532009-09-18 11:37:39 +0400983 int nb_live1;
bellard1d14ffa2005-10-30 18:58:22 +0000984
malcbdff2532009-09-18 11:37:39 +0400985 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
986 if (nb_live) {
987 *nb_live = nb_live1;
bellard1d14ffa2005-10-30 18:58:22 +0000988 }
malcbdff2532009-09-18 11:37:39 +0400989
990 if (nb_live1) {
bellard1d14ffa2005-10-30 18:58:22 +0000991 int live = smin;
992
993 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
994 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
995 return 0;
996 }
997 return live;
998 }
malcbdff2532009-09-18 11:37:39 +0400999 return 0;
bellard1d14ffa2005-10-30 18:58:22 +00001000}
1001
1002/*
1003 * Soft voice (playback)
1004 */
bellard1d14ffa2005-10-30 18:58:22 +00001005int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1006{
1007 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1008 int ret = 0, pos = 0, total = 0;
1009
1010 if (!sw) {
1011 return size;
1012 }
1013
1014 hwsamples = sw->hw->samples;
1015
1016 live = sw->total_hw_samples_mixed;
1017 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1018 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1019 return 0;
1020 }
1021
1022 if (live == hwsamples) {
bellardec36b692006-07-16 18:57:03 +00001023#ifdef DEBUG_OUT
1024 dolog ("%s is full %d\n", sw->name, live);
1025#endif
bellard1d14ffa2005-10-30 18:58:22 +00001026 return 0;
1027 }
1028
1029 wpos = (sw->hw->rpos + live) % hwsamples;
1030 samples = size >> sw->info.shift;
1031
1032 dead = hwsamples - live;
1033 swlim = ((int64_t) dead << 32) / sw->ratio;
1034 swlim = audio_MIN (swlim, samples);
1035 if (swlim) {
Michael Walle00e07672011-01-05 01:05:47 +01001036 sw->conv (sw->buf, buf, swlim);
Marc-André Lureauc01b2452012-04-17 14:32:36 +02001037
1038 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1039 mixeng_volume (sw->buf, swlim, &sw->vol);
1040 }
bellard1d14ffa2005-10-30 18:58:22 +00001041 }
bellard85571bc2004-11-07 18:04:02 +00001042
1043 while (swlim) {
1044 dead = hwsamples - live;
1045 left = hwsamples - wpos;
1046 blck = audio_MIN (dead, left);
1047 if (!blck) {
bellard85571bc2004-11-07 18:04:02 +00001048 break;
1049 }
1050 isamp = swlim;
1051 osamp = blck;
bellard1d14ffa2005-10-30 18:58:22 +00001052 st_rate_flow_mix (
1053 sw->rate,
1054 sw->buf + pos,
1055 sw->hw->mix_buf + wpos,
1056 &isamp,
1057 &osamp
1058 );
bellard85571bc2004-11-07 18:04:02 +00001059 ret += isamp;
1060 swlim -= isamp;
1061 pos += isamp;
1062 live += osamp;
1063 wpos = (wpos + osamp) % hwsamples;
bellard1d14ffa2005-10-30 18:58:22 +00001064 total += osamp;
bellard85571bc2004-11-07 18:04:02 +00001065 }
1066
bellard1d14ffa2005-10-30 18:58:22 +00001067 sw->total_hw_samples_mixed += total;
1068 sw->empty = sw->total_hw_samples_mixed == 0;
1069
1070#ifdef DEBUG_OUT
1071 dolog (
bellardc0fe3822005-11-05 18:55:28 +00001072 "%s: write size %d ret %d total sw %d\n",
1073 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001074 size >> sw->info.shift,
1075 ret,
bellardc0fe3822005-11-05 18:55:28 +00001076 sw->total_hw_samples_mixed
bellard1d14ffa2005-10-30 18:58:22 +00001077 );
1078#endif
1079
1080 return ret << sw->info.shift;
bellard85571bc2004-11-07 18:04:02 +00001081}
1082
bellard1d14ffa2005-10-30 18:58:22 +00001083#ifdef DEBUG_AUDIO
1084static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
bellard85571bc2004-11-07 18:04:02 +00001085{
bellard1d14ffa2005-10-30 18:58:22 +00001086 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1087 cap, info->bits, info->sign, info->freq, info->nchannels);
bellard85571bc2004-11-07 18:04:02 +00001088}
bellard1d14ffa2005-10-30 18:58:22 +00001089#endif
bellard85571bc2004-11-07 18:04:02 +00001090
bellard1d14ffa2005-10-30 18:58:22 +00001091#define DAC
1092#include "audio_template.h"
1093#undef DAC
1094#include "audio_template.h"
bellard85571bc2004-11-07 18:04:02 +00001095
malc713a98f2009-09-12 02:28:45 +04001096/*
1097 * Timer
1098 */
malc713a98f2009-09-12 02:28:45 +04001099static int audio_is_timer_needed (void)
1100{
1101 HWVoiceIn *hwi = NULL;
1102 HWVoiceOut *hwo = NULL;
1103
1104 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1105 if (!hwo->poll_mode) return 1;
1106 }
1107 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1108 if (!hwi->poll_mode) return 1;
1109 }
1110 return 0;
1111}
1112
malc39deb1e2010-11-18 14:30:12 +03001113static void audio_reset_timer (AudioState *s)
malc713a98f2009-09-12 02:28:45 +04001114{
malc713a98f2009-09-12 02:28:45 +04001115 if (audio_is_timer_needed ()) {
Hans de Goedeb4350de2013-10-09 21:33:44 +02001116 timer_mod (s->ts,
1117 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
malc713a98f2009-09-12 02:28:45 +04001118 }
1119 else {
Alex Blighbc72ad62013-08-21 16:03:08 +01001120 timer_del (s->ts);
malc713a98f2009-09-12 02:28:45 +04001121 }
1122}
1123
malc39deb1e2010-11-18 14:30:12 +03001124static void audio_timer (void *opaque)
1125{
1126 audio_run ("timer");
1127 audio_reset_timer (opaque);
1128}
1129
malc713a98f2009-09-12 02:28:45 +04001130/*
1131 * Public API
1132 */
bellard1d14ffa2005-10-30 18:58:22 +00001133int AUD_write (SWVoiceOut *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +00001134{
bellard1d14ffa2005-10-30 18:58:22 +00001135 if (!sw) {
1136 /* XXX: Consider options */
1137 return size;
1138 }
1139
1140 if (!sw->hw->enabled) {
bellardc0fe3822005-11-05 18:55:28 +00001141 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
bellard1d14ffa2005-10-30 18:58:22 +00001142 return 0;
1143 }
1144
Eduardo Habkost9be38592016-06-13 18:57:58 -03001145 return sw->hw->pcm_ops->write(sw, buf, size);
bellard85571bc2004-11-07 18:04:02 +00001146}
1147
bellard1d14ffa2005-10-30 18:58:22 +00001148int AUD_read (SWVoiceIn *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +00001149{
bellard1d14ffa2005-10-30 18:58:22 +00001150 if (!sw) {
1151 /* XXX: Consider options */
1152 return size;
bellard85571bc2004-11-07 18:04:02 +00001153 }
bellard1d14ffa2005-10-30 18:58:22 +00001154
1155 if (!sw->hw->enabled) {
bellardc0fe3822005-11-05 18:55:28 +00001156 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
bellard1d14ffa2005-10-30 18:58:22 +00001157 return 0;
1158 }
1159
Eduardo Habkost9be38592016-06-13 18:57:58 -03001160 return sw->hw->pcm_ops->read(sw, buf, size);
bellard85571bc2004-11-07 18:04:02 +00001161}
1162
bellard1d14ffa2005-10-30 18:58:22 +00001163int AUD_get_buffer_size_out (SWVoiceOut *sw)
bellard85571bc2004-11-07 18:04:02 +00001164{
bellardc0fe3822005-11-05 18:55:28 +00001165 return sw->hw->samples << sw->hw->info.shift;
bellard85571bc2004-11-07 18:04:02 +00001166}
1167
bellard1d14ffa2005-10-30 18:58:22 +00001168void AUD_set_active_out (SWVoiceOut *sw, int on)
bellard85571bc2004-11-07 18:04:02 +00001169{
bellard1d14ffa2005-10-30 18:58:22 +00001170 HWVoiceOut *hw;
1171
1172 if (!sw) {
bellard85571bc2004-11-07 18:04:02 +00001173 return;
bellard85571bc2004-11-07 18:04:02 +00001174 }
1175
bellard85571bc2004-11-07 18:04:02 +00001176 hw = sw->hw;
bellard85571bc2004-11-07 18:04:02 +00001177 if (sw->active != on) {
malc978dd632009-02-18 20:44:04 +00001178 AudioState *s = &glob_audio_state;
bellard1d14ffa2005-10-30 18:58:22 +00001179 SWVoiceOut *temp_sw;
bellardec36b692006-07-16 18:57:03 +00001180 SWVoiceCap *sc;
bellard1d14ffa2005-10-30 18:58:22 +00001181
bellard85571bc2004-11-07 18:04:02 +00001182 if (on) {
1183 hw->pending_disable = 0;
1184 if (!hw->enabled) {
1185 hw->enabled = 1;
malc978dd632009-02-18 20:44:04 +00001186 if (s->vm_running) {
malc713a98f2009-09-12 02:28:45 +04001187 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
malc39deb1e2010-11-18 14:30:12 +03001188 audio_reset_timer (s);
malc978dd632009-02-18 20:44:04 +00001189 }
bellard1d14ffa2005-10-30 18:58:22 +00001190 }
bellard85571bc2004-11-07 18:04:02 +00001191 }
1192 else {
bellard1d14ffa2005-10-30 18:58:22 +00001193 if (hw->enabled) {
bellard85571bc2004-11-07 18:04:02 +00001194 int nb_active = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001195
1196 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1197 temp_sw = temp_sw->entries.le_next) {
1198 nb_active += temp_sw->active != 0;
1199 }
1200
1201 hw->pending_disable = nb_active == 1;
1202 }
1203 }
bellardec36b692006-07-16 18:57:03 +00001204
1205 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1206 sc->sw.active = hw->enabled;
bellard8ead62c2006-07-04 16:51:32 +00001207 if (hw->enabled) {
bellardec36b692006-07-16 18:57:03 +00001208 audio_capture_maybe_changed (sc->cap, 1);
bellard8ead62c2006-07-04 16:51:32 +00001209 }
1210 }
bellard1d14ffa2005-10-30 18:58:22 +00001211 sw->active = on;
1212 }
1213}
1214
1215void AUD_set_active_in (SWVoiceIn *sw, int on)
1216{
1217 HWVoiceIn *hw;
1218
1219 if (!sw) {
1220 return;
1221 }
1222
1223 hw = sw->hw;
1224 if (sw->active != on) {
malc978dd632009-02-18 20:44:04 +00001225 AudioState *s = &glob_audio_state;
bellard1d14ffa2005-10-30 18:58:22 +00001226 SWVoiceIn *temp_sw;
1227
1228 if (on) {
1229 if (!hw->enabled) {
1230 hw->enabled = 1;
malc978dd632009-02-18 20:44:04 +00001231 if (s->vm_running) {
malc713a98f2009-09-12 02:28:45 +04001232 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
malc39deb1e2010-11-18 14:30:12 +03001233 audio_reset_timer (s);
malc978dd632009-02-18 20:44:04 +00001234 }
bellard1d14ffa2005-10-30 18:58:22 +00001235 }
1236 sw->total_hw_samples_acquired = hw->total_samples_captured;
1237 }
1238 else {
1239 if (hw->enabled) {
1240 int nb_active = 0;
1241
1242 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1243 temp_sw = temp_sw->entries.le_next) {
1244 nb_active += temp_sw->active != 0;
bellard85571bc2004-11-07 18:04:02 +00001245 }
1246
1247 if (nb_active == 1) {
bellard1d14ffa2005-10-30 18:58:22 +00001248 hw->enabled = 0;
1249 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
bellard85571bc2004-11-07 18:04:02 +00001250 }
1251 }
1252 }
1253 sw->active = on;
1254 }
1255}
1256
bellard1d14ffa2005-10-30 18:58:22 +00001257static int audio_get_avail (SWVoiceIn *sw)
bellard85571bc2004-11-07 18:04:02 +00001258{
bellard1d14ffa2005-10-30 18:58:22 +00001259 int live;
1260
1261 if (!sw) {
1262 return 0;
1263 }
1264
1265 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1266 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1267 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1268 return 0;
1269 }
1270
1271 ldebug (
bellard26a76462006-06-25 18:15:32 +00001272 "%s: get_avail live %d ret %" PRId64 "\n",
bellardc0fe3822005-11-05 18:55:28 +00001273 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001274 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1275 );
1276
1277 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1278}
1279
1280static int audio_get_free (SWVoiceOut *sw)
1281{
1282 int live, dead;
1283
1284 if (!sw) {
1285 return 0;
1286 }
1287
1288 live = sw->total_hw_samples_mixed;
1289
1290 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1291 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
bellardc0fe3822005-11-05 18:55:28 +00001292 return 0;
bellard1d14ffa2005-10-30 18:58:22 +00001293 }
1294
1295 dead = sw->hw->samples - live;
1296
1297#ifdef DEBUG_OUT
bellard26a76462006-06-25 18:15:32 +00001298 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
bellardc0fe3822005-11-05 18:55:28 +00001299 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001300 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1301#endif
1302
1303 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1304}
1305
bellard8ead62c2006-07-04 16:51:32 +00001306static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1307{
1308 int n;
1309
1310 if (hw->enabled) {
bellardec36b692006-07-16 18:57:03 +00001311 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +00001312
bellardec36b692006-07-16 18:57:03 +00001313 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1314 SWVoiceOut *sw = &sc->sw;
bellard8ead62c2006-07-04 16:51:32 +00001315 int rpos2 = rpos;
1316
1317 n = samples;
1318 while (n) {
1319 int till_end_of_hw = hw->samples - rpos2;
1320 int to_write = audio_MIN (till_end_of_hw, n);
1321 int bytes = to_write << hw->info.shift;
1322 int written;
1323
1324 sw->buf = hw->mix_buf + rpos2;
1325 written = audio_pcm_sw_write (sw, NULL, bytes);
1326 if (written - bytes) {
bellardec36b692006-07-16 18:57:03 +00001327 dolog ("Could not mix %d bytes into a capture "
1328 "buffer, mixed %d\n",
1329 bytes, written);
bellard8ead62c2006-07-04 16:51:32 +00001330 break;
1331 }
1332 n -= to_write;
1333 rpos2 = (rpos2 + to_write) % hw->samples;
1334 }
1335 }
1336 }
1337
1338 n = audio_MIN (samples, hw->samples - rpos);
1339 mixeng_clear (hw->mix_buf + rpos, n);
1340 mixeng_clear (hw->mix_buf, samples - n);
1341}
1342
bellardc0fe3822005-11-05 18:55:28 +00001343static void audio_run_out (AudioState *s)
bellard1d14ffa2005-10-30 18:58:22 +00001344{
1345 HWVoiceOut *hw = NULL;
1346 SWVoiceOut *sw;
1347
malc1a7dafc2009-05-14 03:11:35 +04001348 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
bellard1d14ffa2005-10-30 18:58:22 +00001349 int played;
bellard8ead62c2006-07-04 16:51:32 +00001350 int live, free, nb_live, cleanup_required, prev_rpos;
bellard1d14ffa2005-10-30 18:58:22 +00001351
malcbdff2532009-09-18 11:37:39 +04001352 live = audio_pcm_hw_get_live_out (hw, &nb_live);
bellard1d14ffa2005-10-30 18:58:22 +00001353 if (!nb_live) {
1354 live = 0;
bellard85571bc2004-11-07 18:04:02 +00001355 }
bellardc0fe3822005-11-05 18:55:28 +00001356
bellard1d14ffa2005-10-30 18:58:22 +00001357 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1358 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
bellardc0fe3822005-11-05 18:55:28 +00001359 continue;
bellard85571bc2004-11-07 18:04:02 +00001360 }
bellard1d14ffa2005-10-30 18:58:22 +00001361
1362 if (hw->pending_disable && !nb_live) {
bellardec36b692006-07-16 18:57:03 +00001363 SWVoiceCap *sc;
bellard1d14ffa2005-10-30 18:58:22 +00001364#ifdef DEBUG_OUT
1365 dolog ("Disabling voice\n");
1366#endif
1367 hw->enabled = 0;
1368 hw->pending_disable = 0;
1369 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
bellardec36b692006-07-16 18:57:03 +00001370 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1371 sc->sw.active = 0;
1372 audio_recalc_and_notify_capture (sc->cap);
bellard8ead62c2006-07-04 16:51:32 +00001373 }
bellard1d14ffa2005-10-30 18:58:22 +00001374 continue;
1375 }
1376
1377 if (!live) {
1378 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1379 if (sw->active) {
1380 free = audio_get_free (sw);
1381 if (free > 0) {
1382 sw->callback.fn (sw->callback.opaque, free);
1383 }
1384 }
1385 }
1386 continue;
1387 }
1388
bellard8ead62c2006-07-04 16:51:32 +00001389 prev_rpos = hw->rpos;
malcbdff2532009-09-18 11:37:39 +04001390 played = hw->pcm_ops->run_out (hw, live);
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +03001391 replay_audio_out(&played);
bellard1d14ffa2005-10-30 18:58:22 +00001392 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1393 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1394 hw->rpos, hw->samples, played);
1395 hw->rpos = 0;
1396 }
1397
1398#ifdef DEBUG_OUT
bellardc0fe3822005-11-05 18:55:28 +00001399 dolog ("played=%d\n", played);
bellard1d14ffa2005-10-30 18:58:22 +00001400#endif
1401
1402 if (played) {
1403 hw->ts_helper += played;
bellard8ead62c2006-07-04 16:51:32 +00001404 audio_capture_mix_and_clear (hw, prev_rpos, played);
bellard1d14ffa2005-10-30 18:58:22 +00001405 }
1406
bellardc0fe3822005-11-05 18:55:28 +00001407 cleanup_required = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001408 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
bellard1d14ffa2005-10-30 18:58:22 +00001409 if (!sw->active && sw->empty) {
1410 continue;
1411 }
1412
1413 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1414 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1415 played, sw->total_hw_samples_mixed);
1416 played = sw->total_hw_samples_mixed;
1417 }
1418
1419 sw->total_hw_samples_mixed -= played;
1420
1421 if (!sw->total_hw_samples_mixed) {
1422 sw->empty = 1;
bellardc0fe3822005-11-05 18:55:28 +00001423 cleanup_required |= !sw->active && !sw->callback.fn;
bellard1d14ffa2005-10-30 18:58:22 +00001424 }
1425
1426 if (sw->active) {
1427 free = audio_get_free (sw);
1428 if (free > 0) {
1429 sw->callback.fn (sw->callback.opaque, free);
1430 }
1431 }
bellard85571bc2004-11-07 18:04:02 +00001432 }
bellardc0fe3822005-11-05 18:55:28 +00001433
1434 if (cleanup_required) {
bellardec36b692006-07-16 18:57:03 +00001435 SWVoiceOut *sw1;
1436
1437 sw = hw->sw_head.lh_first;
1438 while (sw) {
1439 sw1 = sw->entries.le_next;
bellardc0fe3822005-11-05 18:55:28 +00001440 if (!sw->active && !sw->callback.fn) {
malc1a7dafc2009-05-14 03:11:35 +04001441 audio_close_out (sw);
bellardc0fe3822005-11-05 18:55:28 +00001442 }
bellardec36b692006-07-16 18:57:03 +00001443 sw = sw1;
bellardc0fe3822005-11-05 18:55:28 +00001444 }
1445 }
bellard85571bc2004-11-07 18:04:02 +00001446 }
bellard1d14ffa2005-10-30 18:58:22 +00001447}
1448
bellardc0fe3822005-11-05 18:55:28 +00001449static void audio_run_in (AudioState *s)
bellard1d14ffa2005-10-30 18:58:22 +00001450{
1451 HWVoiceIn *hw = NULL;
1452
malc1a7dafc2009-05-14 03:11:35 +04001453 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
bellard1d14ffa2005-10-30 18:58:22 +00001454 SWVoiceIn *sw;
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +03001455 int captured = 0, min;
bellard1d14ffa2005-10-30 18:58:22 +00001456
Pavel Dovgalyuk3d4d16f2017-02-02 08:50:54 +03001457 if (replay_mode != REPLAY_MODE_PLAY) {
1458 captured = hw->pcm_ops->run_in(hw);
1459 }
1460 replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples);
bellard1d14ffa2005-10-30 18:58:22 +00001461
1462 min = audio_pcm_hw_find_min_in (hw);
1463 hw->total_samples_captured += captured - min;
1464 hw->ts_helper += captured;
1465
1466 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1467 sw->total_hw_samples_acquired -= min;
1468
1469 if (sw->active) {
1470 int avail;
1471
1472 avail = audio_get_avail (sw);
1473 if (avail > 0) {
1474 sw->callback.fn (sw->callback.opaque, avail);
1475 }
1476 }
1477 }
1478 }
1479}
1480
bellard8ead62c2006-07-04 16:51:32 +00001481static void audio_run_capture (AudioState *s)
1482{
1483 CaptureVoiceOut *cap;
1484
1485 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1486 int live, rpos, captured;
1487 HWVoiceOut *hw = &cap->hw;
1488 SWVoiceOut *sw;
1489
malcbdff2532009-09-18 11:37:39 +04001490 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
bellard8ead62c2006-07-04 16:51:32 +00001491 rpos = hw->rpos;
1492 while (live) {
1493 int left = hw->samples - rpos;
1494 int to_capture = audio_MIN (live, left);
malc1ea879e2008-12-03 22:48:44 +00001495 struct st_sample *src;
bellard8ead62c2006-07-04 16:51:32 +00001496 struct capture_callback *cb;
1497
1498 src = hw->mix_buf + rpos;
1499 hw->clip (cap->buf, src, to_capture);
1500 mixeng_clear (src, to_capture);
1501
1502 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1503 cb->ops.capture (cb->opaque, cap->buf,
1504 to_capture << hw->info.shift);
1505 }
1506 rpos = (rpos + to_capture) % hw->samples;
1507 live -= to_capture;
1508 }
1509 hw->rpos = rpos;
1510
1511 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1512 if (!sw->active && sw->empty) {
1513 continue;
1514 }
1515
1516 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1517 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1518 captured, sw->total_hw_samples_mixed);
1519 captured = sw->total_hw_samples_mixed;
1520 }
1521
1522 sw->total_hw_samples_mixed -= captured;
1523 sw->empty = sw->total_hw_samples_mixed == 0;
1524 }
1525 }
1526}
1527
malc713a98f2009-09-12 02:28:45 +04001528void audio_run (const char *msg)
bellard571ec3d2005-11-20 16:24:34 +00001529{
malc713a98f2009-09-12 02:28:45 +04001530 AudioState *s = &glob_audio_state;
bellard571ec3d2005-11-20 16:24:34 +00001531
1532 audio_run_out (s);
1533 audio_run_in (s);
bellard8ead62c2006-07-04 16:51:32 +00001534 audio_run_capture (s);
malc713a98f2009-09-12 02:28:45 +04001535#ifdef DEBUG_POLL
1536 {
1537 static double prevtime;
1538 double currtime;
1539 struct timeval tv;
bellard571ec3d2005-11-20 16:24:34 +00001540
malc713a98f2009-09-12 02:28:45 +04001541 if (gettimeofday (&tv, NULL)) {
1542 perror ("audio_run: gettimeofday");
1543 return;
1544 }
1545
1546 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1547 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1548 prevtime = currtime;
1549 }
1550#endif
bellard571ec3d2005-11-20 16:24:34 +00001551}
1552
bellard1d14ffa2005-10-30 18:58:22 +00001553static struct audio_option audio_options[] = {
1554 /* DAC */
malc98f9f482009-08-11 20:48:02 +04001555 {
1556 .name = "DAC_FIXED_SETTINGS",
1557 .tag = AUD_OPT_BOOL,
1558 .valp = &conf.fixed_out.enabled,
1559 .descr = "Use fixed settings for host DAC"
1560 },
1561 {
1562 .name = "DAC_FIXED_FREQ",
1563 .tag = AUD_OPT_INT,
1564 .valp = &conf.fixed_out.settings.freq,
1565 .descr = "Frequency for fixed host DAC"
1566 },
1567 {
1568 .name = "DAC_FIXED_FMT",
1569 .tag = AUD_OPT_FMT,
1570 .valp = &conf.fixed_out.settings.fmt,
1571 .descr = "Format for fixed host DAC"
1572 },
1573 {
1574 .name = "DAC_FIXED_CHANNELS",
1575 .tag = AUD_OPT_INT,
1576 .valp = &conf.fixed_out.settings.nchannels,
1577 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1578 },
1579 {
1580 .name = "DAC_VOICES",
1581 .tag = AUD_OPT_INT,
1582 .valp = &conf.fixed_out.nb_voices,
1583 .descr = "Number of voices for DAC"
1584 },
malc713a98f2009-09-12 02:28:45 +04001585 {
1586 .name = "DAC_TRY_POLL",
1587 .tag = AUD_OPT_BOOL,
1588 .valp = &conf.try_poll_out,
1589 .descr = "Attempt using poll mode for DAC"
1590 },
bellard1d14ffa2005-10-30 18:58:22 +00001591 /* ADC */
malc98f9f482009-08-11 20:48:02 +04001592 {
1593 .name = "ADC_FIXED_SETTINGS",
1594 .tag = AUD_OPT_BOOL,
1595 .valp = &conf.fixed_in.enabled,
1596 .descr = "Use fixed settings for host ADC"
1597 },
1598 {
1599 .name = "ADC_FIXED_FREQ",
1600 .tag = AUD_OPT_INT,
1601 .valp = &conf.fixed_in.settings.freq,
1602 .descr = "Frequency for fixed host ADC"
1603 },
1604 {
1605 .name = "ADC_FIXED_FMT",
1606 .tag = AUD_OPT_FMT,
1607 .valp = &conf.fixed_in.settings.fmt,
1608 .descr = "Format for fixed host ADC"
1609 },
1610 {
1611 .name = "ADC_FIXED_CHANNELS",
1612 .tag = AUD_OPT_INT,
1613 .valp = &conf.fixed_in.settings.nchannels,
1614 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1615 },
1616 {
1617 .name = "ADC_VOICES",
1618 .tag = AUD_OPT_INT,
1619 .valp = &conf.fixed_in.nb_voices,
1620 .descr = "Number of voices for ADC"
1621 },
malc713a98f2009-09-12 02:28:45 +04001622 {
1623 .name = "ADC_TRY_POLL",
1624 .tag = AUD_OPT_BOOL,
Jan Kiszka0a90e342009-09-13 22:24:46 +04001625 .valp = &conf.try_poll_in,
malc713a98f2009-09-12 02:28:45 +04001626 .descr = "Attempt using poll mode for ADC"
1627 },
bellard1d14ffa2005-10-30 18:58:22 +00001628 /* Misc */
malc98f9f482009-08-11 20:48:02 +04001629 {
1630 .name = "TIMER_PERIOD",
1631 .tag = AUD_OPT_INT,
1632 .valp = &conf.period.hertz,
1633 .descr = "Timer period in HZ (0 - use lowest possible)"
1634 },
Juan Quintela2700efa2009-08-11 02:31:16 +02001635 { /* End of list */ }
bellard1d14ffa2005-10-30 18:58:22 +00001636};
1637
bellard571ec3d2005-11-20 16:24:34 +00001638static void audio_pp_nb_voices (const char *typ, int nb)
1639{
1640 switch (nb) {
1641 case 0:
1642 printf ("Does not support %s\n", typ);
1643 break;
1644 case 1:
1645 printf ("One %s voice\n", typ);
1646 break;
1647 case INT_MAX:
1648 printf ("Theoretically supports many %s voices\n", typ);
1649 break;
1650 default:
Stefan Weile7d81002011-12-10 00:19:46 +01001651 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
bellard571ec3d2005-11-20 16:24:34 +00001652 break;
1653 }
1654
1655}
1656
bellard1d14ffa2005-10-30 18:58:22 +00001657void AUD_help (void)
1658{
1659 size_t i;
1660
1661 audio_process_options ("AUDIO", audio_options);
malcb1503cd2008-12-22 20:33:55 +00001662 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
bellard1d14ffa2005-10-30 18:58:22 +00001663 struct audio_driver *d = drvtab[i];
1664 if (d->options) {
1665 audio_process_options (d->name, d->options);
1666 }
1667 }
1668
1669 printf ("Audio options:\n");
1670 audio_print_options ("AUDIO", audio_options);
1671 printf ("\n");
1672
1673 printf ("Available drivers:\n");
1674
malcb1503cd2008-12-22 20:33:55 +00001675 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
bellard1d14ffa2005-10-30 18:58:22 +00001676 struct audio_driver *d = drvtab[i];
1677
1678 printf ("Name: %s\n", d->name);
1679 printf ("Description: %s\n", d->descr);
1680
bellard571ec3d2005-11-20 16:24:34 +00001681 audio_pp_nb_voices ("playback", d->max_voices_out);
1682 audio_pp_nb_voices ("capture", d->max_voices_in);
bellard1d14ffa2005-10-30 18:58:22 +00001683
1684 if (d->options) {
1685 printf ("Options:\n");
1686 audio_print_options (d->name, d->options);
1687 }
1688 else {
1689 printf ("No options\n");
1690 }
1691 printf ("\n");
1692 }
1693
1694 printf (
1695 "Options are settable through environment variables.\n"
1696 "Example:\n"
1697#ifdef _WIN32
1698 " set QEMU_AUDIO_DRV=wav\n"
bellard571ec3d2005-11-20 16:24:34 +00001699 " set QEMU_WAV_PATH=c:\\tune.wav\n"
bellard1d14ffa2005-10-30 18:58:22 +00001700#else
1701 " export QEMU_AUDIO_DRV=wav\n"
1702 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1703 "(for csh replace export with setenv in the above)\n"
1704#endif
1705 " qemu ...\n\n"
1706 );
1707}
1708
bellardc0fe3822005-11-05 18:55:28 +00001709static int audio_driver_init (AudioState *s, struct audio_driver *drv)
bellard1d14ffa2005-10-30 18:58:22 +00001710{
1711 if (drv->options) {
1712 audio_process_options (drv->name, drv->options);
1713 }
bellardc0fe3822005-11-05 18:55:28 +00001714 s->drv_opaque = drv->init ();
bellard1d14ffa2005-10-30 18:58:22 +00001715
bellardc0fe3822005-11-05 18:55:28 +00001716 if (s->drv_opaque) {
malc1a7dafc2009-05-14 03:11:35 +04001717 audio_init_nb_voices_out (drv);
1718 audio_init_nb_voices_in (drv);
bellardc0fe3822005-11-05 18:55:28 +00001719 s->drv = drv;
bellard85571bc2004-11-07 18:04:02 +00001720 return 0;
1721 }
bellard1d14ffa2005-10-30 18:58:22 +00001722 else {
1723 dolog ("Could not init `%s' audio driver\n", drv->name);
1724 return -1;
1725 }
bellard85571bc2004-11-07 18:04:02 +00001726}
1727
aliguori9781e042009-01-22 17:15:29 +00001728static void audio_vm_change_state_handler (void *opaque, int running,
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001729 RunState state)
bellard85571bc2004-11-07 18:04:02 +00001730{
bellardc0fe3822005-11-05 18:55:28 +00001731 AudioState *s = opaque;
bellard1d14ffa2005-10-30 18:58:22 +00001732 HWVoiceOut *hwo = NULL;
1733 HWVoiceIn *hwi = NULL;
bellard541e0842005-11-11 00:04:19 +00001734 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
bellard85571bc2004-11-07 18:04:02 +00001735
malc978dd632009-02-18 20:44:04 +00001736 s->vm_running = running;
malc1a7dafc2009-05-14 03:11:35 +04001737 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
malc713a98f2009-09-12 02:28:45 +04001738 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
bellard1d14ffa2005-10-30 18:58:22 +00001739 }
1740
malc1a7dafc2009-05-14 03:11:35 +04001741 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
malc713a98f2009-09-12 02:28:45 +04001742 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
bellard85571bc2004-11-07 18:04:02 +00001743 }
malc39deb1e2010-11-18 14:30:12 +03001744 audio_reset_timer (s);
bellard85571bc2004-11-07 18:04:02 +00001745}
1746
Marc-André Lureaua384c202016-08-01 15:23:43 +04001747static bool is_cleaning_up;
1748
1749bool audio_is_cleaning_up(void)
1750{
1751 return is_cleaning_up;
1752}
1753
1754void audio_cleanup(void)
bellard85571bc2004-11-07 18:04:02 +00001755{
bellardc0fe3822005-11-05 18:55:28 +00001756 AudioState *s = &glob_audio_state;
Marc-André Lureaua384c202016-08-01 15:23:43 +04001757 HWVoiceOut *hwo, *hwon;
1758 HWVoiceIn *hwi, *hwin;
bellard85571bc2004-11-07 18:04:02 +00001759
Marc-André Lureaua384c202016-08-01 15:23:43 +04001760 is_cleaning_up = true;
1761 QLIST_FOREACH_SAFE(hwo, &glob_audio_state.hw_head_out, entries, hwon) {
bellardec36b692006-07-16 18:57:03 +00001762 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +00001763
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001764 if (hwo->enabled) {
1765 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1766 }
bellard1d14ffa2005-10-30 18:58:22 +00001767 hwo->pcm_ops->fini_out (hwo);
bellard8ead62c2006-07-04 16:51:32 +00001768
bellardec36b692006-07-16 18:57:03 +00001769 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1770 CaptureVoiceOut *cap = sc->cap;
1771 struct capture_callback *cb;
1772
1773 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1774 cb->ops.destroy (cb->opaque);
1775 }
bellard8ead62c2006-07-04 16:51:32 +00001776 }
Marc-André Lureaua384c202016-08-01 15:23:43 +04001777 QLIST_REMOVE(hwo, entries);
bellard1d14ffa2005-10-30 18:58:22 +00001778 }
1779
Marc-André Lureaua384c202016-08-01 15:23:43 +04001780 QLIST_FOREACH_SAFE(hwi, &glob_audio_state.hw_head_in, entries, hwin) {
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001781 if (hwi->enabled) {
1782 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1783 }
bellard1d14ffa2005-10-30 18:58:22 +00001784 hwi->pcm_ops->fini_in (hwi);
Marc-André Lureaua384c202016-08-01 15:23:43 +04001785 QLIST_REMOVE(hwi, entries);
bellard85571bc2004-11-07 18:04:02 +00001786 }
bellardc0fe3822005-11-05 18:55:28 +00001787
1788 if (s->drv) {
1789 s->drv->fini (s->drv_opaque);
Marc-André Lureaua384c202016-08-01 15:23:43 +04001790 s->drv = NULL;
bellardc0fe3822005-11-05 18:55:28 +00001791 }
bellard85571bc2004-11-07 18:04:02 +00001792}
1793
Juan Quintelad959fce2009-12-02 11:49:34 +01001794static const VMStateDescription vmstate_audio = {
1795 .name = "audio",
1796 .version_id = 1,
1797 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +02001798 .fields = (VMStateField[]) {
Juan Quintelad959fce2009-12-02 11:49:34 +01001799 VMSTATE_END_OF_LIST()
bellard1d14ffa2005-10-30 18:58:22 +00001800 }
Juan Quintelad959fce2009-12-02 11:49:34 +01001801};
bellard85571bc2004-11-07 18:04:02 +00001802
malc1a7dafc2009-05-14 03:11:35 +04001803static void audio_init (void)
bellard85571bc2004-11-07 18:04:02 +00001804{
bellard1d14ffa2005-10-30 18:58:22 +00001805 size_t i;
bellard85571bc2004-11-07 18:04:02 +00001806 int done = 0;
1807 const char *drvname;
malc713a98f2009-09-12 02:28:45 +04001808 VMChangeStateEntry *e;
bellardc0fe3822005-11-05 18:55:28 +00001809 AudioState *s = &glob_audio_state;
bellard85571bc2004-11-07 18:04:02 +00001810
Paul Brook0d9acba2009-05-12 12:02:38 +01001811 if (s->drv) {
malc1a7dafc2009-05-14 03:11:35 +04001812 return;
Paul Brook0d9acba2009-05-12 12:02:38 +01001813 }
1814
Blue Swirl72cf2d42009-09-12 07:36:22 +00001815 QLIST_INIT (&s->hw_head_out);
1816 QLIST_INIT (&s->hw_head_in);
1817 QLIST_INIT (&s->cap_head);
Marc-André Lureaua384c202016-08-01 15:23:43 +04001818 atexit(audio_cleanup);
bellard571ec3d2005-11-20 16:24:34 +00001819
Alex Blighbc72ad62013-08-21 16:03:08 +01001820 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
bellard571ec3d2005-11-20 16:24:34 +00001821
bellard1d14ffa2005-10-30 18:58:22 +00001822 audio_process_options ("AUDIO", audio_options);
bellard85571bc2004-11-07 18:04:02 +00001823
bellardc0fe3822005-11-05 18:55:28 +00001824 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1825 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1826
bellard1d14ffa2005-10-30 18:58:22 +00001827 if (s->nb_hw_voices_out <= 0) {
bellard571ec3d2005-11-20 16:24:34 +00001828 dolog ("Bogus number of playback voices %d, setting to 1\n",
bellard1d14ffa2005-10-30 18:58:22 +00001829 s->nb_hw_voices_out);
1830 s->nb_hw_voices_out = 1;
bellard85571bc2004-11-07 18:04:02 +00001831 }
1832
bellard1d14ffa2005-10-30 18:58:22 +00001833 if (s->nb_hw_voices_in <= 0) {
bellard571ec3d2005-11-20 16:24:34 +00001834 dolog ("Bogus number of capture voices %d, setting to 0\n",
bellard1d14ffa2005-10-30 18:58:22 +00001835 s->nb_hw_voices_in);
bellard571ec3d2005-11-20 16:24:34 +00001836 s->nb_hw_voices_in = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001837 }
1838
1839 {
1840 int def;
1841 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1842 }
1843
bellard85571bc2004-11-07 18:04:02 +00001844 if (drvname) {
1845 int found = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001846
malcb1503cd2008-12-22 20:33:55 +00001847 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
bellard85571bc2004-11-07 18:04:02 +00001848 if (!strcmp (drvname, drvtab[i]->name)) {
bellardc0fe3822005-11-05 18:55:28 +00001849 done = !audio_driver_init (s, drvtab[i]);
bellard85571bc2004-11-07 18:04:02 +00001850 found = 1;
1851 break;
1852 }
1853 }
bellard1d14ffa2005-10-30 18:58:22 +00001854
bellard85571bc2004-11-07 18:04:02 +00001855 if (!found) {
1856 dolog ("Unknown audio driver `%s'\n", drvname);
bellard1d14ffa2005-10-30 18:58:22 +00001857 dolog ("Run with -audio-help to list available drivers\n");
bellard85571bc2004-11-07 18:04:02 +00001858 }
1859 }
1860
bellard85571bc2004-11-07 18:04:02 +00001861 if (!done) {
malcb1503cd2008-12-22 20:33:55 +00001862 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
bellard1d14ffa2005-10-30 18:58:22 +00001863 if (drvtab[i]->can_be_default) {
bellardc0fe3822005-11-05 18:55:28 +00001864 done = !audio_driver_init (s, drvtab[i]);
bellard1d14ffa2005-10-30 18:58:22 +00001865 }
bellard85571bc2004-11-07 18:04:02 +00001866 }
1867 }
1868
bellard85571bc2004-11-07 18:04:02 +00001869 if (!done) {
bellardc0fe3822005-11-05 18:55:28 +00001870 done = !audio_driver_init (s, &no_audio_driver);
Markus Armbruster7e274652015-12-17 17:35:20 +01001871 assert(done);
1872 dolog("warning: Using timer based audio emulation\n");
bellard85571bc2004-11-07 18:04:02 +00001873 }
bellard1d14ffa2005-10-30 18:58:22 +00001874
Paul Brook0d9acba2009-05-12 12:02:38 +01001875 if (conf.period.hertz <= 0) {
1876 if (conf.period.hertz < 0) {
1877 dolog ("warning: Timer period is negative - %d "
1878 "treating as zero\n",
1879 conf.period.hertz);
bellard1d14ffa2005-10-30 18:58:22 +00001880 }
Paul Brook0d9acba2009-05-12 12:02:38 +01001881 conf.period.ticks = 1;
1882 } else {
Rutuja Shah73bcb242016-03-21 21:32:30 +05301883 conf.period.ticks = NANOSECONDS_PER_SECOND / conf.period.hertz;
bellard1d14ffa2005-10-30 18:58:22 +00001884 }
Paul Brook0d9acba2009-05-12 12:02:38 +01001885
1886 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1887 if (!e) {
1888 dolog ("warning: Could not register change state handler\n"
1889 "(Audio can continue looping even after stopping the VM)\n");
bellard1d14ffa2005-10-30 18:58:22 +00001890 }
1891
Blue Swirl72cf2d42009-09-12 07:36:22 +00001892 QLIST_INIT (&s->card_head);
Alex Williamson0be71e32010-06-25 11:09:07 -06001893 vmstate_register (NULL, 0, &vmstate_audio, s);
bellard85571bc2004-11-07 18:04:02 +00001894}
bellard8ead62c2006-07-04 16:51:32 +00001895
malc1a7dafc2009-05-14 03:11:35 +04001896void AUD_register_card (const char *name, QEMUSoundCard *card)
1897{
1898 audio_init ();
Anthony Liguori7267c092011-08-20 22:09:37 -05001899 card->name = g_strdup (name);
malc1a7dafc2009-05-14 03:11:35 +04001900 memset (&card->entries, 0, sizeof (card->entries));
Blue Swirl72cf2d42009-09-12 07:36:22 +00001901 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
malc1a7dafc2009-05-14 03:11:35 +04001902}
1903
1904void AUD_remove_card (QEMUSoundCard *card)
1905{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001906 QLIST_REMOVE (card, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001907 g_free (card->name);
malc1a7dafc2009-05-14 03:11:35 +04001908}
1909
1910
bellardec36b692006-07-16 18:57:03 +00001911CaptureVoiceOut *AUD_add_capture (
malc1ea879e2008-12-03 22:48:44 +00001912 struct audsettings *as,
bellard8ead62c2006-07-04 16:51:32 +00001913 struct audio_capture_ops *ops,
1914 void *cb_opaque
1915 )
1916{
malc1a7dafc2009-05-14 03:11:35 +04001917 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +00001918 CaptureVoiceOut *cap;
1919 struct capture_callback *cb;
1920
bellardec36b692006-07-16 18:57:03 +00001921 if (audio_validate_settings (as)) {
bellard8ead62c2006-07-04 16:51:32 +00001922 dolog ("Invalid settings were passed when trying to add capture\n");
1923 audio_print_settings (as);
bellardec36b692006-07-16 18:57:03 +00001924 goto err0;
bellard8ead62c2006-07-04 16:51:32 +00001925 }
1926
1927 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1928 if (!cb) {
1929 dolog ("Could not allocate capture callback information, size %zu\n",
1930 sizeof (*cb));
1931 goto err0;
1932 }
1933 cb->ops = *ops;
1934 cb->opaque = cb_opaque;
1935
malc1a7dafc2009-05-14 03:11:35 +04001936 cap = audio_pcm_capture_find_specific (as);
bellard8ead62c2006-07-04 16:51:32 +00001937 if (cap) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001938 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
bellardec36b692006-07-16 18:57:03 +00001939 return cap;
bellard8ead62c2006-07-04 16:51:32 +00001940 }
1941 else {
1942 HWVoiceOut *hw;
1943 CaptureVoiceOut *cap;
1944
1945 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1946 if (!cap) {
1947 dolog ("Could not allocate capture voice, size %zu\n",
1948 sizeof (*cap));
1949 goto err1;
1950 }
1951
1952 hw = &cap->hw;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001953 QLIST_INIT (&hw->sw_head);
1954 QLIST_INIT (&cap->cb_head);
bellard8ead62c2006-07-04 16:51:32 +00001955
1956 /* XXX find a more elegant way */
1957 hw->samples = 4096 * 4;
1958 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
malc1ea879e2008-12-03 22:48:44 +00001959 sizeof (struct st_sample));
bellard8ead62c2006-07-04 16:51:32 +00001960 if (!hw->mix_buf) {
1961 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1962 hw->samples);
1963 goto err2;
1964 }
1965
bellardd929eba2006-07-04 21:47:22 +00001966 audio_pcm_init_info (&hw->info, as);
bellard8ead62c2006-07-04 16:51:32 +00001967
1968 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1969 if (!cap->buf) {
1970 dolog ("Could not allocate capture buffer "
1971 "(%d samples, each %d bytes)\n",
1972 hw->samples, 1 << hw->info.shift);
1973 goto err3;
1974 }
1975
1976 hw->clip = mixeng_clip
1977 [hw->info.nchannels == 2]
1978 [hw->info.sign]
bellardd929eba2006-07-04 21:47:22 +00001979 [hw->info.swap_endianness]
thsf941aa22007-02-17 22:19:29 +00001980 [audio_bits_to_index (hw->info.bits)];
bellard8ead62c2006-07-04 16:51:32 +00001981
Blue Swirl72cf2d42009-09-12 07:36:22 +00001982 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1983 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
bellard8ead62c2006-07-04 16:51:32 +00001984
Marc-André Lureaua384c202016-08-01 15:23:43 +04001985 QLIST_FOREACH(hw, &glob_audio_state.hw_head_out, entries) {
malc1a7dafc2009-05-14 03:11:35 +04001986 audio_attach_capture (hw);
bellard8ead62c2006-07-04 16:51:32 +00001987 }
bellardec36b692006-07-16 18:57:03 +00001988 return cap;
bellard8ead62c2006-07-04 16:51:32 +00001989
1990 err3:
Anthony Liguori7267c092011-08-20 22:09:37 -05001991 g_free (cap->hw.mix_buf);
bellard8ead62c2006-07-04 16:51:32 +00001992 err2:
Anthony Liguori7267c092011-08-20 22:09:37 -05001993 g_free (cap);
bellard8ead62c2006-07-04 16:51:32 +00001994 err1:
Anthony Liguori7267c092011-08-20 22:09:37 -05001995 g_free (cb);
bellard8ead62c2006-07-04 16:51:32 +00001996 err0:
bellardec36b692006-07-16 18:57:03 +00001997 return NULL;
1998 }
1999}
2000
2001void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
2002{
2003 struct capture_callback *cb;
2004
2005 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2006 if (cb->opaque == cb_opaque) {
2007 cb->ops.destroy (cb_opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002008 QLIST_REMOVE (cb, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002009 g_free (cb);
bellardec36b692006-07-16 18:57:03 +00002010
2011 if (!cap->cb_head.lh_first) {
2012 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
bellarda3c25992006-07-18 21:09:59 +00002013
bellardec36b692006-07-16 18:57:03 +00002014 while (sw) {
bellarda3c25992006-07-18 21:09:59 +00002015 SWVoiceCap *sc = (SWVoiceCap *) sw;
bellardec36b692006-07-16 18:57:03 +00002016#ifdef DEBUG_CAPTURE
2017 dolog ("freeing %s\n", sw->name);
2018#endif
bellarda3c25992006-07-18 21:09:59 +00002019
bellardec36b692006-07-16 18:57:03 +00002020 sw1 = sw->entries.le_next;
2021 if (sw->rate) {
2022 st_rate_stop (sw->rate);
2023 sw->rate = NULL;
2024 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002025 QLIST_REMOVE (sw, entries);
2026 QLIST_REMOVE (sc, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002027 g_free (sc);
bellardec36b692006-07-16 18:57:03 +00002028 sw = sw1;
2029 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002030 QLIST_REMOVE (cap, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002031 g_free (cap);
bellardec36b692006-07-16 18:57:03 +00002032 }
2033 return;
2034 }
bellard8ead62c2006-07-04 16:51:32 +00002035 }
2036}
balrog683efdc2008-05-04 10:21:03 +00002037
2038void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2039{
2040 if (sw) {
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002041 HWVoiceOut *hw = sw->hw;
2042
balrog683efdc2008-05-04 10:21:03 +00002043 sw->vol.mute = mute;
2044 sw->vol.l = nominal_volume.l * lvol / 255;
2045 sw->vol.r = nominal_volume.r * rvol / 255;
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002046
2047 if (hw->pcm_ops->ctl_out) {
2048 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2049 }
balrog683efdc2008-05-04 10:21:03 +00002050 }
2051}
2052
2053void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2054{
2055 if (sw) {
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002056 HWVoiceIn *hw = sw->hw;
2057
balrog683efdc2008-05-04 10:21:03 +00002058 sw->vol.mute = mute;
2059 sw->vol.l = nominal_volume.l * lvol / 255;
2060 sw->vol.r = nominal_volume.r * rvol / 255;
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002061
2062 if (hw->pcm_ops->ctl_in) {
2063 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);
2064 }
balrog683efdc2008-05-04 10:21:03 +00002065 }
2066}