blob: 5be4b15fcf2a9c798c69802e1f83a7a1d0a1ac40 [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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "audio.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010026#include "monitor/monitor.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010027#include "qemu/timer.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010028#include "sysemu/sysemu.h"
bellard85571bc2004-11-07 18:04:02 +000029
bellard1d14ffa2005-10-30 18:58:22 +000030#define AUDIO_CAP "audio"
31#include "audio_int.h"
bellard85571bc2004-11-07 18:04:02 +000032
bellard1d14ffa2005-10-30 18:58:22 +000033/* #define DEBUG_LIVE */
34/* #define DEBUG_OUT */
bellard8ead62c2006-07-04 16:51:32 +000035/* #define DEBUG_CAPTURE */
malc713a98f2009-09-12 02:28:45 +040036/* #define DEBUG_POLL */
bellard1d14ffa2005-10-30 18:58:22 +000037
bellardc0fe3822005-11-05 18:55:28 +000038#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
39
Juan Quintela2358a492009-07-27 16:13:25 +020040
41/* Order of CONFIG_AUDIO_DRIVERS is import.
42 The 1st one is the one used by default, that is the reason
43 that we generate the list.
44*/
bellard1d14ffa2005-10-30 18:58:22 +000045static struct audio_driver *drvtab[] = {
Gerd Hoffmann3e313752010-11-09 17:29:46 +010046#ifdef CONFIG_SPICE
47 &spice_audio_driver,
48#endif
Juan Quintela2358a492009-07-27 16:13:25 +020049 CONFIG_AUDIO_DRIVERS
bellard1d14ffa2005-10-30 18:58:22 +000050 &no_audio_driver,
51 &wav_audio_driver
52};
bellard85571bc2004-11-07 18:04:02 +000053
bellardc0fe3822005-11-05 18:55:28 +000054struct fixed_settings {
55 int enabled;
56 int nb_voices;
57 int greedy;
malc1ea879e2008-12-03 22:48:44 +000058 struct audsettings settings;
bellardc0fe3822005-11-05 18:55:28 +000059};
bellard1d14ffa2005-10-30 18:58:22 +000060
bellardc0fe3822005-11-05 18:55:28 +000061static struct {
62 struct fixed_settings fixed_out;
63 struct fixed_settings fixed_in;
64 union {
malcc310de82008-11-12 20:36:27 +000065 int hertz;
bellardc0fe3822005-11-05 18:55:28 +000066 int64_t ticks;
67 } period;
malc713a98f2009-09-12 02:28:45 +040068 int try_poll_in;
69 int try_poll_out;
bellardc0fe3822005-11-05 18:55:28 +000070} conf = {
Juan Quintela14658cd2009-07-20 20:57:00 +020071 .fixed_out = { /* DAC fixed settings */
72 .enabled = 1,
73 .nb_voices = 1,
74 .greedy = 1,
75 .settings = {
76 .freq = 44100,
77 .nchannels = 2,
78 .fmt = AUD_FMT_S16,
79 .endianness = AUDIO_HOST_ENDIANNESS,
bellardc0fe3822005-11-05 18:55:28 +000080 }
81 },
bellard1d14ffa2005-10-30 18:58:22 +000082
Juan Quintela14658cd2009-07-20 20:57:00 +020083 .fixed_in = { /* ADC fixed settings */
84 .enabled = 1,
85 .nb_voices = 1,
86 .greedy = 1,
87 .settings = {
88 .freq = 44100,
89 .nchannels = 2,
90 .fmt = AUD_FMT_S16,
91 .endianness = AUDIO_HOST_ENDIANNESS,
bellardc0fe3822005-11-05 18:55:28 +000092 }
93 },
bellard1d14ffa2005-10-30 18:58:22 +000094
Hans de Goede40a814b2013-10-09 21:38:32 +020095 .period = { .hertz = 100 },
malc713a98f2009-09-12 02:28:45 +040096 .try_poll_in = 1,
97 .try_poll_out = 1,
bellard1d14ffa2005-10-30 18:58:22 +000098};
99
bellardc0fe3822005-11-05 18:55:28 +0000100static AudioState glob_audio_state;
101
Michael Walle00e07672011-01-05 01:05:47 +0100102const struct mixeng_volume nominal_volume = {
Juan Quintela14658cd2009-07-20 20:57:00 +0200103 .mute = 0,
bellard1d14ffa2005-10-30 18:58:22 +0000104#ifdef FLOAT_MIXENG
Juan Quintela14658cd2009-07-20 20:57:00 +0200105 .r = 1.0,
106 .l = 1.0,
bellard1d14ffa2005-10-30 18:58:22 +0000107#else
Juan Quintela14658cd2009-07-20 20:57:00 +0200108 .r = 1ULL << 32,
109 .l = 1ULL << 32,
bellard1d14ffa2005-10-30 18:58:22 +0000110#endif
bellard85571bc2004-11-07 18:04:02 +0000111};
112
bellard1d14ffa2005-10-30 18:58:22 +0000113#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
114#error No its not
115#else
malc82584e22010-01-17 02:03:30 +0300116static void audio_print_options (const char *prefix,
117 struct audio_option *opt);
118
bellard1d14ffa2005-10-30 18:58:22 +0000119int audio_bug (const char *funcname, int cond)
bellard85571bc2004-11-07 18:04:02 +0000120{
bellard1d14ffa2005-10-30 18:58:22 +0000121 if (cond) {
122 static int shown;
123
bellard8ead62c2006-07-04 16:51:32 +0000124 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
bellard1d14ffa2005-10-30 18:58:22 +0000125 if (!shown) {
malc82584e22010-01-17 02:03:30 +0300126 struct audio_driver *d;
127
bellard1d14ffa2005-10-30 18:58:22 +0000128 shown = 1;
129 AUD_log (NULL, "Save all your work and restart without audio\n");
malc155a8ad2009-09-18 11:52:45 +0400130 AUD_log (NULL, "Please send bug report to av1474@comtv.ru\n");
bellard1d14ffa2005-10-30 18:58:22 +0000131 AUD_log (NULL, "I am sorry\n");
malc82584e22010-01-17 02:03:30 +0300132 d = glob_audio_state.drv;
133 if (d) {
134 audio_print_options (d->name, d->options);
135 }
bellard1d14ffa2005-10-30 18:58:22 +0000136 }
137 AUD_log (NULL, "Context:\n");
138
139#if defined AUDIO_BREAKPOINT_ON_BUG
140# if defined HOST_I386
141# if defined __GNUC__
142 __asm__ ("int3");
143# elif defined _MSC_VER
144 _asm _emit 0xcc;
145# else
146 abort ();
147# endif
148# else
149 abort ();
150# endif
151#endif
152 }
153
154 return cond;
155}
156#endif
157
thsf941aa22007-02-17 22:19:29 +0000158static inline int audio_bits_to_index (int bits)
159{
160 switch (bits) {
161 case 8:
162 return 0;
163
164 case 16:
165 return 1;
166
167 case 32:
168 return 2;
169
170 default:
171 audio_bug ("bits_to_index", 1);
172 AUD_log (NULL, "invalid bits %d\n", bits);
173 return 0;
174 }
175}
176
bellardc0fe3822005-11-05 18:55:28 +0000177void *audio_calloc (const char *funcname, int nmemb, size_t size)
178{
179 int cond;
180 size_t len;
181
182 len = nmemb * size;
183 cond = !nmemb || !size;
184 cond |= nmemb < 0;
185 cond |= len < size;
186
187 if (audio_bug ("audio_calloc", cond)) {
188 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
189 funcname);
bellard541e0842005-11-11 00:04:19 +0000190 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
bellardc0fe3822005-11-05 18:55:28 +0000191 return NULL;
192 }
193
Anthony Liguori7267c092011-08-20 22:09:37 -0500194 return g_malloc0 (len);
bellardc0fe3822005-11-05 18:55:28 +0000195}
196
bellard1d14ffa2005-10-30 18:58:22 +0000197static char *audio_alloc_prefix (const char *s)
198{
199 const char qemu_prefix[] = "QEMU_";
aliguori090f1fa2009-02-05 22:05:58 +0000200 size_t len, i;
201 char *r, *u;
bellard1d14ffa2005-10-30 18:58:22 +0000202
203 if (!s) {
204 return NULL;
205 }
206
207 len = strlen (s);
Anthony Liguori7267c092011-08-20 22:09:37 -0500208 r = g_malloc (len + sizeof (qemu_prefix));
bellard1d14ffa2005-10-30 18:58:22 +0000209
aliguori090f1fa2009-02-05 22:05:58 +0000210 u = r + sizeof (qemu_prefix) - 1;
bellard1d14ffa2005-10-30 18:58:22 +0000211
aliguori090f1fa2009-02-05 22:05:58 +0000212 pstrcpy (r, len + sizeof (qemu_prefix), qemu_prefix);
213 pstrcat (r, len + sizeof (qemu_prefix), s);
bellard1d14ffa2005-10-30 18:58:22 +0000214
aliguori090f1fa2009-02-05 22:05:58 +0000215 for (i = 0; i < len; ++i) {
216 u[i] = qemu_toupper(u[i]);
bellard1d14ffa2005-10-30 18:58:22 +0000217 }
aliguori090f1fa2009-02-05 22:05:58 +0000218
bellard1d14ffa2005-10-30 18:58:22 +0000219 return r;
220}
221
pbrook9596ebb2007-11-18 01:44:38 +0000222static const char *audio_audfmt_to_string (audfmt_e fmt)
bellard1d14ffa2005-10-30 18:58:22 +0000223{
224 switch (fmt) {
225 case AUD_FMT_U8:
226 return "U8";
227
228 case AUD_FMT_U16:
229 return "U16";
230
231 case AUD_FMT_S8:
232 return "S8";
233
234 case AUD_FMT_S16:
235 return "S16";
thsf941aa22007-02-17 22:19:29 +0000236
237 case AUD_FMT_U32:
238 return "U32";
239
240 case AUD_FMT_S32:
241 return "S32";
bellard1d14ffa2005-10-30 18:58:22 +0000242 }
243
244 dolog ("Bogus audfmt %d returning S16\n", fmt);
245 return "S16";
246}
247
pbrook9596ebb2007-11-18 01:44:38 +0000248static audfmt_e audio_string_to_audfmt (const char *s, audfmt_e defval,
249 int *defaultp)
bellard1d14ffa2005-10-30 18:58:22 +0000250{
251 if (!strcasecmp (s, "u8")) {
252 *defaultp = 0;
253 return AUD_FMT_U8;
254 }
255 else if (!strcasecmp (s, "u16")) {
256 *defaultp = 0;
257 return AUD_FMT_U16;
258 }
thsf941aa22007-02-17 22:19:29 +0000259 else if (!strcasecmp (s, "u32")) {
260 *defaultp = 0;
261 return AUD_FMT_U32;
262 }
bellard1d14ffa2005-10-30 18:58:22 +0000263 else if (!strcasecmp (s, "s8")) {
264 *defaultp = 0;
265 return AUD_FMT_S8;
266 }
267 else if (!strcasecmp (s, "s16")) {
268 *defaultp = 0;
269 return AUD_FMT_S16;
270 }
thsf941aa22007-02-17 22:19:29 +0000271 else if (!strcasecmp (s, "s32")) {
272 *defaultp = 0;
273 return AUD_FMT_S32;
274 }
bellard1d14ffa2005-10-30 18:58:22 +0000275 else {
276 dolog ("Bogus audio format `%s' using %s\n",
277 s, audio_audfmt_to_string (defval));
278 *defaultp = 1;
279 return defval;
280 }
281}
282
283static audfmt_e audio_get_conf_fmt (const char *envname,
284 audfmt_e defval,
285 int *defaultp)
286{
287 const char *var = getenv (envname);
288 if (!var) {
289 *defaultp = 1;
290 return defval;
291 }
292 return audio_string_to_audfmt (var, defval, defaultp);
293}
294
295static int audio_get_conf_int (const char *key, int defval, int *defaultp)
296{
297 int val;
bellard85571bc2004-11-07 18:04:02 +0000298 char *strval;
299
300 strval = getenv (key);
301 if (strval) {
bellard1d14ffa2005-10-30 18:58:22 +0000302 *defaultp = 0;
bellard85571bc2004-11-07 18:04:02 +0000303 val = atoi (strval);
bellard1d14ffa2005-10-30 18:58:22 +0000304 return val;
bellard85571bc2004-11-07 18:04:02 +0000305 }
bellard1d14ffa2005-10-30 18:58:22 +0000306 else {
307 *defaultp = 1;
308 return defval;
309 }
bellard85571bc2004-11-07 18:04:02 +0000310}
311
bellard1d14ffa2005-10-30 18:58:22 +0000312static const char *audio_get_conf_str (const char *key,
313 const char *defval,
314 int *defaultp)
bellard85571bc2004-11-07 18:04:02 +0000315{
316 const char *val = getenv (key);
bellard1d14ffa2005-10-30 18:58:22 +0000317 if (!val) {
318 *defaultp = 1;
bellard85571bc2004-11-07 18:04:02 +0000319 return defval;
bellard1d14ffa2005-10-30 18:58:22 +0000320 }
321 else {
322 *defaultp = 0;
bellard85571bc2004-11-07 18:04:02 +0000323 return val;
bellard1d14ffa2005-10-30 18:58:22 +0000324 }
bellard85571bc2004-11-07 18:04:02 +0000325}
326
bellard541e0842005-11-11 00:04:19 +0000327void AUD_vlog (const char *cap, const char *fmt, va_list ap)
328{
Kővágó, Zoltán06ac27f2015-06-12 14:33:02 +0200329 if (cap) {
330 fprintf(stderr, "%s: ", cap);
bellard541e0842005-11-11 00:04:19 +0000331 }
bellard541e0842005-11-11 00:04:19 +0000332
Kővágó, Zoltán06ac27f2015-06-12 14:33:02 +0200333 vfprintf(stderr, fmt, ap);
bellard541e0842005-11-11 00:04:19 +0000334}
335
bellardfb065182004-11-09 23:09:44 +0000336void AUD_log (const char *cap, const char *fmt, ...)
bellard85571bc2004-11-07 18:04:02 +0000337{
338 va_list ap;
bellard85571bc2004-11-07 18:04:02 +0000339
bellard541e0842005-11-11 00:04:19 +0000340 va_start (ap, fmt);
341 AUD_vlog (cap, fmt, ap);
342 va_end (ap);
bellard85571bc2004-11-07 18:04:02 +0000343}
344
bellard1d14ffa2005-10-30 18:58:22 +0000345static void audio_print_options (const char *prefix,
346 struct audio_option *opt)
bellard85571bc2004-11-07 18:04:02 +0000347{
bellard1d14ffa2005-10-30 18:58:22 +0000348 char *uprefix;
349
350 if (!prefix) {
351 dolog ("No prefix specified\n");
352 return;
353 }
354
355 if (!opt) {
356 dolog ("No options\n");
357 return;
358 }
359
360 uprefix = audio_alloc_prefix (prefix);
361
362 for (; opt->name; opt++) {
363 const char *state = "default";
364 printf (" %s_%s: ", uprefix, opt->name);
365
thsfe8f0962007-07-12 10:59:21 +0000366 if (opt->overriddenp && *opt->overriddenp) {
bellard1d14ffa2005-10-30 18:58:22 +0000367 state = "current";
368 }
369
370 switch (opt->tag) {
371 case AUD_OPT_BOOL:
372 {
373 int *intp = opt->valp;
374 printf ("boolean, %s = %d\n", state, *intp ? 1 : 0);
375 }
376 break;
377
378 case AUD_OPT_INT:
379 {
380 int *intp = opt->valp;
381 printf ("integer, %s = %d\n", state, *intp);
382 }
383 break;
384
385 case AUD_OPT_FMT:
386 {
387 audfmt_e *fmtp = opt->valp;
388 printf (
balrogca9cc282008-01-14 04:24:29 +0000389 "format, %s = %s, (one of: U8 S8 U16 S16 U32 S32)\n",
bellard1d14ffa2005-10-30 18:58:22 +0000390 state,
391 audio_audfmt_to_string (*fmtp)
392 );
393 }
394 break;
395
396 case AUD_OPT_STR:
397 {
398 const char **strp = opt->valp;
399 printf ("string, %s = %s\n",
400 state,
401 *strp ? *strp : "(not set)");
402 }
403 break;
404
405 default:
406 printf ("???\n");
407 dolog ("Bad value tag for option %s_%s %d\n",
408 uprefix, opt->name, opt->tag);
409 break;
410 }
411 printf (" %s\n", opt->descr);
412 }
413
Anthony Liguori7267c092011-08-20 22:09:37 -0500414 g_free (uprefix);
bellard85571bc2004-11-07 18:04:02 +0000415}
416
bellard1d14ffa2005-10-30 18:58:22 +0000417static void audio_process_options (const char *prefix,
418 struct audio_option *opt)
419{
420 char *optname;
421 const char qemu_prefix[] = "QEMU_";
blueswir1363a37d2008-08-21 17:58:08 +0000422 size_t preflen, optlen;
bellard1d14ffa2005-10-30 18:58:22 +0000423
424 if (audio_bug (AUDIO_FUNC, !prefix)) {
425 dolog ("prefix = NULL\n");
426 return;
427 }
428
429 if (audio_bug (AUDIO_FUNC, !opt)) {
430 dolog ("opt = NULL\n");
431 return;
432 }
433
434 preflen = strlen (prefix);
435
436 for (; opt->name; opt++) {
437 size_t len, i;
438 int def;
439
440 if (!opt->valp) {
441 dolog ("Option value pointer for `%s' is not set\n",
442 opt->name);
443 continue;
444 }
445
446 len = strlen (opt->name);
bellardc0fe3822005-11-05 18:55:28 +0000447 /* len of opt->name + len of prefix + size of qemu_prefix
448 * (includes trailing zero) + zero + underscore (on behalf of
449 * sizeof) */
blueswir1363a37d2008-08-21 17:58:08 +0000450 optlen = len + preflen + sizeof (qemu_prefix) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500451 optname = g_malloc (optlen);
bellard1d14ffa2005-10-30 18:58:22 +0000452
blueswir1363a37d2008-08-21 17:58:08 +0000453 pstrcpy (optname, optlen, qemu_prefix);
bellardc0fe3822005-11-05 18:55:28 +0000454
455 /* copy while upper-casing, including trailing zero */
bellard1d14ffa2005-10-30 18:58:22 +0000456 for (i = 0; i <= preflen; ++i) {
blueswir1cd390082008-11-16 13:53:32 +0000457 optname[i + sizeof (qemu_prefix) - 1] = qemu_toupper(prefix[i]);
bellard1d14ffa2005-10-30 18:58:22 +0000458 }
blueswir1363a37d2008-08-21 17:58:08 +0000459 pstrcat (optname, optlen, "_");
blueswir1363a37d2008-08-21 17:58:08 +0000460 pstrcat (optname, optlen, opt->name);
bellard1d14ffa2005-10-30 18:58:22 +0000461
462 def = 1;
463 switch (opt->tag) {
464 case AUD_OPT_BOOL:
465 case AUD_OPT_INT:
466 {
467 int *intp = opt->valp;
468 *intp = audio_get_conf_int (optname, *intp, &def);
469 }
470 break;
471
472 case AUD_OPT_FMT:
473 {
474 audfmt_e *fmtp = opt->valp;
475 *fmtp = audio_get_conf_fmt (optname, *fmtp, &def);
476 }
477 break;
478
479 case AUD_OPT_STR:
480 {
481 const char **strp = opt->valp;
482 *strp = audio_get_conf_str (optname, *strp, &def);
483 }
484 break;
485
486 default:
487 dolog ("Bad value tag for option `%s' - %d\n",
488 optname, opt->tag);
489 break;
490 }
491
thsfe8f0962007-07-12 10:59:21 +0000492 if (!opt->overriddenp) {
493 opt->overriddenp = &opt->overridden;
bellard1d14ffa2005-10-30 18:58:22 +0000494 }
thsfe8f0962007-07-12 10:59:21 +0000495 *opt->overriddenp = !def;
Anthony Liguori7267c092011-08-20 22:09:37 -0500496 g_free (optname);
bellard1d14ffa2005-10-30 18:58:22 +0000497 }
498}
499
malc1ea879e2008-12-03 22:48:44 +0000500static void audio_print_settings (struct audsettings *as)
bellardc0fe3822005-11-05 18:55:28 +0000501{
502 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
503
504 switch (as->fmt) {
505 case AUD_FMT_S8:
506 AUD_log (NULL, "S8");
507 break;
508 case AUD_FMT_U8:
509 AUD_log (NULL, "U8");
510 break;
511 case AUD_FMT_S16:
512 AUD_log (NULL, "S16");
513 break;
514 case AUD_FMT_U16:
515 AUD_log (NULL, "U16");
516 break;
malcd50997f2008-06-22 14:10:45 +0000517 case AUD_FMT_S32:
518 AUD_log (NULL, "S32");
519 break;
520 case AUD_FMT_U32:
521 AUD_log (NULL, "U32");
522 break;
bellardc0fe3822005-11-05 18:55:28 +0000523 default:
524 AUD_log (NULL, "invalid(%d)", as->fmt);
525 break;
526 }
bellardec36b692006-07-16 18:57:03 +0000527
528 AUD_log (NULL, " endianness=");
bellardd929eba2006-07-04 21:47:22 +0000529 switch (as->endianness) {
530 case 0:
531 AUD_log (NULL, "little");
532 break;
533 case 1:
534 AUD_log (NULL, "big");
535 break;
536 default:
537 AUD_log (NULL, "invalid");
538 break;
539 }
bellardc0fe3822005-11-05 18:55:28 +0000540 AUD_log (NULL, "\n");
541}
542
malc1ea879e2008-12-03 22:48:44 +0000543static int audio_validate_settings (struct audsettings *as)
bellardc0fe3822005-11-05 18:55:28 +0000544{
545 int invalid;
546
547 invalid = as->nchannels != 1 && as->nchannels != 2;
bellardd929eba2006-07-04 21:47:22 +0000548 invalid |= as->endianness != 0 && as->endianness != 1;
bellardc0fe3822005-11-05 18:55:28 +0000549
550 switch (as->fmt) {
551 case AUD_FMT_S8:
552 case AUD_FMT_U8:
553 case AUD_FMT_S16:
554 case AUD_FMT_U16:
thsf941aa22007-02-17 22:19:29 +0000555 case AUD_FMT_S32:
556 case AUD_FMT_U32:
bellardc0fe3822005-11-05 18:55:28 +0000557 break;
558 default:
559 invalid = 1;
560 break;
561 }
562
563 invalid |= as->freq <= 0;
bellardd929eba2006-07-04 21:47:22 +0000564 return invalid ? -1 : 0;
bellardc0fe3822005-11-05 18:55:28 +0000565}
566
malc1ea879e2008-12-03 22:48:44 +0000567static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
bellard1d14ffa2005-10-30 18:58:22 +0000568{
569 int bits = 8, sign = 0;
570
bellardc0fe3822005-11-05 18:55:28 +0000571 switch (as->fmt) {
bellard1d14ffa2005-10-30 18:58:22 +0000572 case AUD_FMT_S8:
573 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100574 /* fall through */
bellard1d14ffa2005-10-30 18:58:22 +0000575 case AUD_FMT_U8:
576 break;
577
578 case AUD_FMT_S16:
579 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100580 /* fall through */
bellard1d14ffa2005-10-30 18:58:22 +0000581 case AUD_FMT_U16:
582 bits = 16;
583 break;
thsf941aa22007-02-17 22:19:29 +0000584
585 case AUD_FMT_S32:
586 sign = 1;
Stefan Weilb4bd0b12012-02-25 14:46:55 +0100587 /* fall through */
thsf941aa22007-02-17 22:19:29 +0000588 case AUD_FMT_U32:
589 bits = 32;
590 break;
bellard1d14ffa2005-10-30 18:58:22 +0000591 }
bellardc0fe3822005-11-05 18:55:28 +0000592 return info->freq == as->freq
593 && info->nchannels == as->nchannels
bellard1d14ffa2005-10-30 18:58:22 +0000594 && info->sign == sign
bellardd929eba2006-07-04 21:47:22 +0000595 && info->bits == bits
596 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
bellard1d14ffa2005-10-30 18:58:22 +0000597}
598
malc1ea879e2008-12-03 22:48:44 +0000599void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
bellard85571bc2004-11-07 18:04:02 +0000600{
thsf941aa22007-02-17 22:19:29 +0000601 int bits = 8, sign = 0, shift = 0;
bellard85571bc2004-11-07 18:04:02 +0000602
bellardc0fe3822005-11-05 18:55:28 +0000603 switch (as->fmt) {
bellard85571bc2004-11-07 18:04:02 +0000604 case AUD_FMT_S8:
605 sign = 1;
606 case AUD_FMT_U8:
607 break;
608
609 case AUD_FMT_S16:
610 sign = 1;
611 case AUD_FMT_U16:
612 bits = 16;
thsf941aa22007-02-17 22:19:29 +0000613 shift = 1;
614 break;
615
616 case AUD_FMT_S32:
617 sign = 1;
618 case AUD_FMT_U32:
619 bits = 32;
620 shift = 2;
bellard85571bc2004-11-07 18:04:02 +0000621 break;
622 }
623
bellardc0fe3822005-11-05 18:55:28 +0000624 info->freq = as->freq;
bellard1d14ffa2005-10-30 18:58:22 +0000625 info->bits = bits;
626 info->sign = sign;
bellardc0fe3822005-11-05 18:55:28 +0000627 info->nchannels = as->nchannels;
thsf941aa22007-02-17 22:19:29 +0000628 info->shift = (as->nchannels == 2) + shift;
bellard1d14ffa2005-10-30 18:58:22 +0000629 info->align = (1 << info->shift) - 1;
630 info->bytes_per_second = info->freq << info->shift;
bellardd929eba2006-07-04 21:47:22 +0000631 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
bellard85571bc2004-11-07 18:04:02 +0000632}
633
bellard1d14ffa2005-10-30 18:58:22 +0000634void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
bellard85571bc2004-11-07 18:04:02 +0000635{
bellard1d14ffa2005-10-30 18:58:22 +0000636 if (!len) {
637 return;
638 }
639
640 if (info->sign) {
bellarde2f909b2006-08-03 20:39:40 +0000641 memset (buf, 0x00, len << info->shift);
bellard1d14ffa2005-10-30 18:58:22 +0000642 }
643 else {
thsf941aa22007-02-17 22:19:29 +0000644 switch (info->bits) {
645 case 8:
bellarde2f909b2006-08-03 20:39:40 +0000646 memset (buf, 0x80, len << info->shift);
thsf941aa22007-02-17 22:19:29 +0000647 break;
bellard1d14ffa2005-10-30 18:58:22 +0000648
thsf941aa22007-02-17 22:19:29 +0000649 case 16:
650 {
651 int i;
652 uint16_t *p = buf;
653 int shift = info->nchannels - 1;
654 short s = INT16_MAX;
bellard1d14ffa2005-10-30 18:58:22 +0000655
thsf941aa22007-02-17 22:19:29 +0000656 if (info->swap_endianness) {
657 s = bswap16 (s);
658 }
659
660 for (i = 0; i < len << shift; i++) {
661 p[i] = s;
662 }
bellard1d14ffa2005-10-30 18:58:22 +0000663 }
thsf941aa22007-02-17 22:19:29 +0000664 break;
665
666 case 32:
667 {
668 int i;
669 uint32_t *p = buf;
670 int shift = info->nchannels - 1;
671 int32_t s = INT32_MAX;
672
673 if (info->swap_endianness) {
674 s = bswap32 (s);
675 }
676
677 for (i = 0; i < len << shift; i++) {
678 p[i] = s;
679 }
680 }
681 break;
682
683 default:
684 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
685 info->bits);
686 break;
bellard1d14ffa2005-10-30 18:58:22 +0000687 }
688 }
bellard85571bc2004-11-07 18:04:02 +0000689}
690
bellard1d14ffa2005-10-30 18:58:22 +0000691/*
bellard8ead62c2006-07-04 16:51:32 +0000692 * Capture
693 */
Michael Walle00e07672011-01-05 01:05:47 +0100694static void noop_conv (struct st_sample *dst, const void *src, int samples)
bellard8ead62c2006-07-04 16:51:32 +0000695{
696 (void) src;
697 (void) dst;
698 (void) samples;
bellard8ead62c2006-07-04 16:51:32 +0000699}
700
701static CaptureVoiceOut *audio_pcm_capture_find_specific (
malc1ea879e2008-12-03 22:48:44 +0000702 struct audsettings *as
bellard8ead62c2006-07-04 16:51:32 +0000703 )
704{
705 CaptureVoiceOut *cap;
malc1a7dafc2009-05-14 03:11:35 +0400706 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +0000707
708 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
bellardd929eba2006-07-04 21:47:22 +0000709 if (audio_pcm_info_eq (&cap->hw.info, as)) {
bellard8ead62c2006-07-04 16:51:32 +0000710 return cap;
711 }
712 }
713 return NULL;
714}
715
bellardec36b692006-07-16 18:57:03 +0000716static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
717{
718 struct capture_callback *cb;
719
720#ifdef DEBUG_CAPTURE
721 dolog ("notification %d sent\n", cmd);
722#endif
723 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
724 cb->ops.notify (cb->opaque, cmd);
725 }
726}
727
728static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
bellard8ead62c2006-07-04 16:51:32 +0000729{
730 if (cap->hw.enabled != enabled) {
bellardec36b692006-07-16 18:57:03 +0000731 audcnotification_e cmd;
bellard8ead62c2006-07-04 16:51:32 +0000732 cap->hw.enabled = enabled;
bellardec36b692006-07-16 18:57:03 +0000733 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
734 audio_notify_capture (cap, cmd);
bellard8ead62c2006-07-04 16:51:32 +0000735 }
736}
737
738static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
739{
740 HWVoiceOut *hw = &cap->hw;
741 SWVoiceOut *sw;
742 int enabled = 0;
743
bellardec36b692006-07-16 18:57:03 +0000744 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
bellard8ead62c2006-07-04 16:51:32 +0000745 if (sw->active) {
746 enabled = 1;
747 break;
748 }
749 }
bellardec36b692006-07-16 18:57:03 +0000750 audio_capture_maybe_changed (cap, enabled);
bellard8ead62c2006-07-04 16:51:32 +0000751}
752
753static void audio_detach_capture (HWVoiceOut *hw)
754{
bellardec36b692006-07-16 18:57:03 +0000755 SWVoiceCap *sc = hw->cap_head.lh_first;
bellard8ead62c2006-07-04 16:51:32 +0000756
bellardec36b692006-07-16 18:57:03 +0000757 while (sc) {
758 SWVoiceCap *sc1 = sc->entries.le_next;
759 SWVoiceOut *sw = &sc->sw;
760 CaptureVoiceOut *cap = sc->cap;
761 int was_active = sw->active;
762
bellard8ead62c2006-07-04 16:51:32 +0000763 if (sw->rate) {
764 st_rate_stop (sw->rate);
765 sw->rate = NULL;
766 }
767
Blue Swirl72cf2d42009-09-12 07:36:22 +0000768 QLIST_REMOVE (sw, entries);
769 QLIST_REMOVE (sc, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -0500770 g_free (sc);
bellardec36b692006-07-16 18:57:03 +0000771 if (was_active) {
772 /* We have removed soft voice from the capture:
773 this might have changed the overall status of the capture
774 since this might have been the only active voice */
775 audio_recalc_and_notify_capture (cap);
776 }
777 sc = sc1;
bellard8ead62c2006-07-04 16:51:32 +0000778 }
779}
780
malc1a7dafc2009-05-14 03:11:35 +0400781static int audio_attach_capture (HWVoiceOut *hw)
bellard8ead62c2006-07-04 16:51:32 +0000782{
malc1a7dafc2009-05-14 03:11:35 +0400783 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +0000784 CaptureVoiceOut *cap;
785
786 audio_detach_capture (hw);
787 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
bellardec36b692006-07-16 18:57:03 +0000788 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +0000789 SWVoiceOut *sw;
bellardec36b692006-07-16 18:57:03 +0000790 HWVoiceOut *hw_cap = &cap->hw;
bellard8ead62c2006-07-04 16:51:32 +0000791
bellardec36b692006-07-16 18:57:03 +0000792 sc = audio_calloc (AUDIO_FUNC, 1, sizeof (*sc));
793 if (!sc) {
bellard8ead62c2006-07-04 16:51:32 +0000794 dolog ("Could not allocate soft capture voice (%zu bytes)\n",
bellardec36b692006-07-16 18:57:03 +0000795 sizeof (*sc));
bellard8ead62c2006-07-04 16:51:32 +0000796 return -1;
797 }
798
bellardec36b692006-07-16 18:57:03 +0000799 sc->cap = cap;
800 sw = &sc->sw;
bellard8ead62c2006-07-04 16:51:32 +0000801 sw->hw = hw_cap;
bellardec36b692006-07-16 18:57:03 +0000802 sw->info = hw->info;
bellard8ead62c2006-07-04 16:51:32 +0000803 sw->empty = 1;
804 sw->active = hw->enabled;
805 sw->conv = noop_conv;
806 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
malc83617102012-07-16 18:08:36 +0400807 sw->vol = nominal_volume;
bellard8ead62c2006-07-04 16:51:32 +0000808 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
809 if (!sw->rate) {
810 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
Anthony Liguori7267c092011-08-20 22:09:37 -0500811 g_free (sw);
bellard8ead62c2006-07-04 16:51:32 +0000812 return -1;
813 }
Blue Swirl72cf2d42009-09-12 07:36:22 +0000814 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
815 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
bellardec36b692006-07-16 18:57:03 +0000816#ifdef DEBUG_CAPTURE
Stefan Weil457b6542013-01-16 18:17:33 +0100817 sw->name = g_strdup_printf ("for %p %d,%d,%d",
818 hw, sw->info.freq, sw->info.bits,
819 sw->info.nchannels);
bellardec36b692006-07-16 18:57:03 +0000820 dolog ("Added %s active = %d\n", sw->name, sw->active);
821#endif
bellard8ead62c2006-07-04 16:51:32 +0000822 if (sw->active) {
bellardec36b692006-07-16 18:57:03 +0000823 audio_capture_maybe_changed (cap, 1);
bellard8ead62c2006-07-04 16:51:32 +0000824 }
825 }
826 return 0;
827}
828
829/*
bellard1d14ffa2005-10-30 18:58:22 +0000830 * Hard voice (capture)
831 */
bellardc0fe3822005-11-05 18:55:28 +0000832static int audio_pcm_hw_find_min_in (HWVoiceIn *hw)
bellard85571bc2004-11-07 18:04:02 +0000833{
bellard1d14ffa2005-10-30 18:58:22 +0000834 SWVoiceIn *sw;
835 int m = hw->total_samples_captured;
bellard85571bc2004-11-07 18:04:02 +0000836
bellard1d14ffa2005-10-30 18:58:22 +0000837 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
838 if (sw->active) {
839 m = audio_MIN (m, sw->total_hw_samples_acquired);
bellard85571bc2004-11-07 18:04:02 +0000840 }
841 }
bellard1d14ffa2005-10-30 18:58:22 +0000842 return m;
bellard85571bc2004-11-07 18:04:02 +0000843}
844
bellard1d14ffa2005-10-30 18:58:22 +0000845int audio_pcm_hw_get_live_in (HWVoiceIn *hw)
bellard85571bc2004-11-07 18:04:02 +0000846{
bellard1d14ffa2005-10-30 18:58:22 +0000847 int live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
848 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
849 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
850 return 0;
851 }
852 return live;
853}
bellard85571bc2004-11-07 18:04:02 +0000854
malcddabec72009-09-18 10:59:38 +0400855int audio_pcm_hw_clip_out (HWVoiceOut *hw, void *pcm_buf,
856 int live, int pending)
857{
858 int left = hw->samples - pending;
859 int len = audio_MIN (left, live);
860 int clipped = 0;
861
862 while (len) {
863 struct st_sample *src = hw->mix_buf + hw->rpos;
864 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
865 int samples_till_end_of_buf = hw->samples - hw->rpos;
866 int samples_to_clip = audio_MIN (len, samples_till_end_of_buf);
867
868 hw->clip (dst, src, samples_to_clip);
869
870 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
871 len -= samples_to_clip;
872 clipped += samples_to_clip;
873 }
874 return clipped;
875}
876
bellard1d14ffa2005-10-30 18:58:22 +0000877/*
878 * Soft voice (capture)
879 */
bellard1d14ffa2005-10-30 18:58:22 +0000880static int audio_pcm_sw_get_rpos_in (SWVoiceIn *sw)
881{
882 HWVoiceIn *hw = sw->hw;
883 int live = hw->total_samples_captured - sw->total_hw_samples_acquired;
884 int rpos;
885
886 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
887 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
888 return 0;
889 }
890
891 rpos = hw->wpos - live;
892 if (rpos >= 0) {
893 return rpos;
894 }
895 else {
896 return hw->samples + rpos;
bellard85571bc2004-11-07 18:04:02 +0000897 }
898}
899
bellard1d14ffa2005-10-30 18:58:22 +0000900int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +0000901{
bellard1d14ffa2005-10-30 18:58:22 +0000902 HWVoiceIn *hw = sw->hw;
903 int samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
malc1ea879e2008-12-03 22:48:44 +0000904 struct st_sample *src, *dst = sw->buf;
bellard85571bc2004-11-07 18:04:02 +0000905
bellard1d14ffa2005-10-30 18:58:22 +0000906 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
bellard85571bc2004-11-07 18:04:02 +0000907
bellard1d14ffa2005-10-30 18:58:22 +0000908 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
909 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
910 dolog ("live_in=%d hw->samples=%d\n", live, hw->samples);
911 return 0;
bellard85571bc2004-11-07 18:04:02 +0000912 }
bellard1d14ffa2005-10-30 18:58:22 +0000913
914 samples = size >> sw->info.shift;
915 if (!live) {
916 return 0;
917 }
918
919 swlim = (live * sw->ratio) >> 32;
bellard85571bc2004-11-07 18:04:02 +0000920 swlim = audio_MIN (swlim, samples);
921
bellard1d14ffa2005-10-30 18:58:22 +0000922 while (swlim) {
923 src = hw->conv_buf + rpos;
924 isamp = hw->wpos - rpos;
925 /* XXX: <= ? */
926 if (isamp <= 0) {
927 isamp = hw->samples - rpos;
928 }
929
930 if (!isamp) {
931 break;
932 }
933 osamp = swlim;
934
935 if (audio_bug (AUDIO_FUNC, osamp < 0)) {
936 dolog ("osamp=%d\n", osamp);
bellardc0fe3822005-11-05 18:55:28 +0000937 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000938 }
939
940 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
941 swlim -= osamp;
942 rpos = (rpos + isamp) % hw->samples;
943 dst += osamp;
944 ret += osamp;
945 total += isamp;
946 }
947
Marc-André Lureauc01b2452012-04-17 14:32:36 +0200948 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
949 mixeng_volume (sw->buf, ret, &sw->vol);
950 }
Michael Walle00e07672011-01-05 01:05:47 +0100951
bellard571ec3d2005-11-20 16:24:34 +0000952 sw->clip (buf, sw->buf, ret);
bellard1d14ffa2005-10-30 18:58:22 +0000953 sw->total_hw_samples_acquired += total;
954 return ret << sw->info.shift;
955}
956
957/*
958 * Hard voice (playback)
959 */
960static int audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
961{
962 SWVoiceOut *sw;
963 int m = INT_MAX;
964 int nb_live = 0;
965
966 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
967 if (sw->active || !sw->empty) {
968 m = audio_MIN (m, sw->total_hw_samples_mixed);
969 nb_live += 1;
970 }
971 }
972
973 *nb_livep = nb_live;
974 return m;
975}
976
malcbdff2532009-09-18 11:37:39 +0400977static int audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
bellard1d14ffa2005-10-30 18:58:22 +0000978{
979 int smin;
malcbdff2532009-09-18 11:37:39 +0400980 int nb_live1;
bellard1d14ffa2005-10-30 18:58:22 +0000981
malcbdff2532009-09-18 11:37:39 +0400982 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
983 if (nb_live) {
984 *nb_live = nb_live1;
bellard1d14ffa2005-10-30 18:58:22 +0000985 }
malcbdff2532009-09-18 11:37:39 +0400986
987 if (nb_live1) {
bellard1d14ffa2005-10-30 18:58:22 +0000988 int live = smin;
989
990 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
991 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
992 return 0;
993 }
994 return live;
995 }
malcbdff2532009-09-18 11:37:39 +0400996 return 0;
bellard1d14ffa2005-10-30 18:58:22 +0000997}
998
999/*
1000 * Soft voice (playback)
1001 */
bellard1d14ffa2005-10-30 18:58:22 +00001002int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
1003{
1004 int hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
1005 int ret = 0, pos = 0, total = 0;
1006
1007 if (!sw) {
1008 return size;
1009 }
1010
1011 hwsamples = sw->hw->samples;
1012
1013 live = sw->total_hw_samples_mixed;
1014 if (audio_bug (AUDIO_FUNC, live < 0 || live > hwsamples)){
1015 dolog ("live=%d hw->samples=%d\n", live, hwsamples);
1016 return 0;
1017 }
1018
1019 if (live == hwsamples) {
bellardec36b692006-07-16 18:57:03 +00001020#ifdef DEBUG_OUT
1021 dolog ("%s is full %d\n", sw->name, live);
1022#endif
bellard1d14ffa2005-10-30 18:58:22 +00001023 return 0;
1024 }
1025
1026 wpos = (sw->hw->rpos + live) % hwsamples;
1027 samples = size >> sw->info.shift;
1028
1029 dead = hwsamples - live;
1030 swlim = ((int64_t) dead << 32) / sw->ratio;
1031 swlim = audio_MIN (swlim, samples);
1032 if (swlim) {
Michael Walle00e07672011-01-05 01:05:47 +01001033 sw->conv (sw->buf, buf, swlim);
Marc-André Lureauc01b2452012-04-17 14:32:36 +02001034
1035 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
1036 mixeng_volume (sw->buf, swlim, &sw->vol);
1037 }
bellard1d14ffa2005-10-30 18:58:22 +00001038 }
bellard85571bc2004-11-07 18:04:02 +00001039
1040 while (swlim) {
1041 dead = hwsamples - live;
1042 left = hwsamples - wpos;
1043 blck = audio_MIN (dead, left);
1044 if (!blck) {
bellard85571bc2004-11-07 18:04:02 +00001045 break;
1046 }
1047 isamp = swlim;
1048 osamp = blck;
bellard1d14ffa2005-10-30 18:58:22 +00001049 st_rate_flow_mix (
1050 sw->rate,
1051 sw->buf + pos,
1052 sw->hw->mix_buf + wpos,
1053 &isamp,
1054 &osamp
1055 );
bellard85571bc2004-11-07 18:04:02 +00001056 ret += isamp;
1057 swlim -= isamp;
1058 pos += isamp;
1059 live += osamp;
1060 wpos = (wpos + osamp) % hwsamples;
bellard1d14ffa2005-10-30 18:58:22 +00001061 total += osamp;
bellard85571bc2004-11-07 18:04:02 +00001062 }
1063
bellard1d14ffa2005-10-30 18:58:22 +00001064 sw->total_hw_samples_mixed += total;
1065 sw->empty = sw->total_hw_samples_mixed == 0;
1066
1067#ifdef DEBUG_OUT
1068 dolog (
bellardc0fe3822005-11-05 18:55:28 +00001069 "%s: write size %d ret %d total sw %d\n",
1070 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001071 size >> sw->info.shift,
1072 ret,
bellardc0fe3822005-11-05 18:55:28 +00001073 sw->total_hw_samples_mixed
bellard1d14ffa2005-10-30 18:58:22 +00001074 );
1075#endif
1076
1077 return ret << sw->info.shift;
bellard85571bc2004-11-07 18:04:02 +00001078}
1079
bellard1d14ffa2005-10-30 18:58:22 +00001080#ifdef DEBUG_AUDIO
1081static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
bellard85571bc2004-11-07 18:04:02 +00001082{
bellard1d14ffa2005-10-30 18:58:22 +00001083 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
1084 cap, info->bits, info->sign, info->freq, info->nchannels);
bellard85571bc2004-11-07 18:04:02 +00001085}
bellard1d14ffa2005-10-30 18:58:22 +00001086#endif
bellard85571bc2004-11-07 18:04:02 +00001087
bellard1d14ffa2005-10-30 18:58:22 +00001088#define DAC
1089#include "audio_template.h"
1090#undef DAC
1091#include "audio_template.h"
bellard85571bc2004-11-07 18:04:02 +00001092
malc713a98f2009-09-12 02:28:45 +04001093/*
1094 * Timer
1095 */
malc713a98f2009-09-12 02:28:45 +04001096static int audio_is_timer_needed (void)
1097{
1098 HWVoiceIn *hwi = NULL;
1099 HWVoiceOut *hwo = NULL;
1100
1101 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
1102 if (!hwo->poll_mode) return 1;
1103 }
1104 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
1105 if (!hwi->poll_mode) return 1;
1106 }
1107 return 0;
1108}
1109
malc39deb1e2010-11-18 14:30:12 +03001110static void audio_reset_timer (AudioState *s)
malc713a98f2009-09-12 02:28:45 +04001111{
malc713a98f2009-09-12 02:28:45 +04001112 if (audio_is_timer_needed ()) {
Hans de Goedeb4350de2013-10-09 21:33:44 +02001113 timer_mod (s->ts,
1114 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
malc713a98f2009-09-12 02:28:45 +04001115 }
1116 else {
Alex Blighbc72ad62013-08-21 16:03:08 +01001117 timer_del (s->ts);
malc713a98f2009-09-12 02:28:45 +04001118 }
1119}
1120
malc39deb1e2010-11-18 14:30:12 +03001121static void audio_timer (void *opaque)
1122{
1123 audio_run ("timer");
1124 audio_reset_timer (opaque);
1125}
1126
malc713a98f2009-09-12 02:28:45 +04001127/*
1128 * Public API
1129 */
bellard1d14ffa2005-10-30 18:58:22 +00001130int AUD_write (SWVoiceOut *sw, void *buf, int size)
bellard85571bc2004-11-07 18:04:02 +00001131{
1132 int bytes;
1133
bellard1d14ffa2005-10-30 18:58:22 +00001134 if (!sw) {
1135 /* XXX: Consider options */
1136 return size;
1137 }
1138
1139 if (!sw->hw->enabled) {
bellardc0fe3822005-11-05 18:55:28 +00001140 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
bellard1d14ffa2005-10-30 18:58:22 +00001141 return 0;
1142 }
1143
bellard85571bc2004-11-07 18:04:02 +00001144 bytes = sw->hw->pcm_ops->write (sw, buf, size);
1145 return bytes;
1146}
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 int bytes;
bellard85571bc2004-11-07 18:04:02 +00001151
bellard1d14ffa2005-10-30 18:58:22 +00001152 if (!sw) {
1153 /* XXX: Consider options */
1154 return size;
bellard85571bc2004-11-07 18:04:02 +00001155 }
bellard1d14ffa2005-10-30 18:58:22 +00001156
1157 if (!sw->hw->enabled) {
bellardc0fe3822005-11-05 18:55:28 +00001158 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
bellard1d14ffa2005-10-30 18:58:22 +00001159 return 0;
1160 }
1161
1162 bytes = sw->hw->pcm_ops->read (sw, buf, size);
1163 return bytes;
bellard85571bc2004-11-07 18:04:02 +00001164}
1165
bellard1d14ffa2005-10-30 18:58:22 +00001166int AUD_get_buffer_size_out (SWVoiceOut *sw)
bellard85571bc2004-11-07 18:04:02 +00001167{
bellardc0fe3822005-11-05 18:55:28 +00001168 return sw->hw->samples << sw->hw->info.shift;
bellard85571bc2004-11-07 18:04:02 +00001169}
1170
bellard1d14ffa2005-10-30 18:58:22 +00001171void AUD_set_active_out (SWVoiceOut *sw, int on)
bellard85571bc2004-11-07 18:04:02 +00001172{
bellard1d14ffa2005-10-30 18:58:22 +00001173 HWVoiceOut *hw;
1174
1175 if (!sw) {
bellard85571bc2004-11-07 18:04:02 +00001176 return;
bellard85571bc2004-11-07 18:04:02 +00001177 }
1178
bellard85571bc2004-11-07 18:04:02 +00001179 hw = sw->hw;
bellard85571bc2004-11-07 18:04:02 +00001180 if (sw->active != on) {
malc978dd632009-02-18 20:44:04 +00001181 AudioState *s = &glob_audio_state;
bellard1d14ffa2005-10-30 18:58:22 +00001182 SWVoiceOut *temp_sw;
bellardec36b692006-07-16 18:57:03 +00001183 SWVoiceCap *sc;
bellard1d14ffa2005-10-30 18:58:22 +00001184
bellard85571bc2004-11-07 18:04:02 +00001185 if (on) {
1186 hw->pending_disable = 0;
1187 if (!hw->enabled) {
1188 hw->enabled = 1;
malc978dd632009-02-18 20:44:04 +00001189 if (s->vm_running) {
malc713a98f2009-09-12 02:28:45 +04001190 hw->pcm_ops->ctl_out (hw, VOICE_ENABLE, conf.try_poll_out);
malc39deb1e2010-11-18 14:30:12 +03001191 audio_reset_timer (s);
malc978dd632009-02-18 20:44:04 +00001192 }
bellard1d14ffa2005-10-30 18:58:22 +00001193 }
bellard85571bc2004-11-07 18:04:02 +00001194 }
1195 else {
bellard1d14ffa2005-10-30 18:58:22 +00001196 if (hw->enabled) {
bellard85571bc2004-11-07 18:04:02 +00001197 int nb_active = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001198
1199 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1200 temp_sw = temp_sw->entries.le_next) {
1201 nb_active += temp_sw->active != 0;
1202 }
1203
1204 hw->pending_disable = nb_active == 1;
1205 }
1206 }
bellardec36b692006-07-16 18:57:03 +00001207
1208 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1209 sc->sw.active = hw->enabled;
bellard8ead62c2006-07-04 16:51:32 +00001210 if (hw->enabled) {
bellardec36b692006-07-16 18:57:03 +00001211 audio_capture_maybe_changed (sc->cap, 1);
bellard8ead62c2006-07-04 16:51:32 +00001212 }
1213 }
bellard1d14ffa2005-10-30 18:58:22 +00001214 sw->active = on;
1215 }
1216}
1217
1218void AUD_set_active_in (SWVoiceIn *sw, int on)
1219{
1220 HWVoiceIn *hw;
1221
1222 if (!sw) {
1223 return;
1224 }
1225
1226 hw = sw->hw;
1227 if (sw->active != on) {
malc978dd632009-02-18 20:44:04 +00001228 AudioState *s = &glob_audio_state;
bellard1d14ffa2005-10-30 18:58:22 +00001229 SWVoiceIn *temp_sw;
1230
1231 if (on) {
1232 if (!hw->enabled) {
1233 hw->enabled = 1;
malc978dd632009-02-18 20:44:04 +00001234 if (s->vm_running) {
malc713a98f2009-09-12 02:28:45 +04001235 hw->pcm_ops->ctl_in (hw, VOICE_ENABLE, conf.try_poll_in);
malc39deb1e2010-11-18 14:30:12 +03001236 audio_reset_timer (s);
malc978dd632009-02-18 20:44:04 +00001237 }
bellard1d14ffa2005-10-30 18:58:22 +00001238 }
1239 sw->total_hw_samples_acquired = hw->total_samples_captured;
1240 }
1241 else {
1242 if (hw->enabled) {
1243 int nb_active = 0;
1244
1245 for (temp_sw = hw->sw_head.lh_first; temp_sw;
1246 temp_sw = temp_sw->entries.le_next) {
1247 nb_active += temp_sw->active != 0;
bellard85571bc2004-11-07 18:04:02 +00001248 }
1249
1250 if (nb_active == 1) {
bellard1d14ffa2005-10-30 18:58:22 +00001251 hw->enabled = 0;
1252 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
bellard85571bc2004-11-07 18:04:02 +00001253 }
1254 }
1255 }
1256 sw->active = on;
1257 }
1258}
1259
bellard1d14ffa2005-10-30 18:58:22 +00001260static int audio_get_avail (SWVoiceIn *sw)
bellard85571bc2004-11-07 18:04:02 +00001261{
bellard1d14ffa2005-10-30 18:58:22 +00001262 int live;
1263
1264 if (!sw) {
1265 return 0;
1266 }
1267
1268 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
1269 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1270 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
1271 return 0;
1272 }
1273
1274 ldebug (
bellard26a76462006-06-25 18:15:32 +00001275 "%s: get_avail live %d ret %" PRId64 "\n",
bellardc0fe3822005-11-05 18:55:28 +00001276 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001277 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1278 );
1279
1280 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1281}
1282
1283static int audio_get_free (SWVoiceOut *sw)
1284{
1285 int live, dead;
1286
1287 if (!sw) {
1288 return 0;
1289 }
1290
1291 live = sw->total_hw_samples_mixed;
1292
1293 if (audio_bug (AUDIO_FUNC, live < 0 || live > sw->hw->samples)) {
1294 dolog ("live=%d sw->hw->samples=%d\n", live, sw->hw->samples);
bellardc0fe3822005-11-05 18:55:28 +00001295 return 0;
bellard1d14ffa2005-10-30 18:58:22 +00001296 }
1297
1298 dead = sw->hw->samples - live;
1299
1300#ifdef DEBUG_OUT
bellard26a76462006-06-25 18:15:32 +00001301 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
bellardc0fe3822005-11-05 18:55:28 +00001302 SW_NAME (sw),
bellard1d14ffa2005-10-30 18:58:22 +00001303 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
1304#endif
1305
1306 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1307}
1308
bellard8ead62c2006-07-04 16:51:32 +00001309static void audio_capture_mix_and_clear (HWVoiceOut *hw, int rpos, int samples)
1310{
1311 int n;
1312
1313 if (hw->enabled) {
bellardec36b692006-07-16 18:57:03 +00001314 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +00001315
bellardec36b692006-07-16 18:57:03 +00001316 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1317 SWVoiceOut *sw = &sc->sw;
bellard8ead62c2006-07-04 16:51:32 +00001318 int rpos2 = rpos;
1319
1320 n = samples;
1321 while (n) {
1322 int till_end_of_hw = hw->samples - rpos2;
1323 int to_write = audio_MIN (till_end_of_hw, n);
1324 int bytes = to_write << hw->info.shift;
1325 int written;
1326
1327 sw->buf = hw->mix_buf + rpos2;
1328 written = audio_pcm_sw_write (sw, NULL, bytes);
1329 if (written - bytes) {
bellardec36b692006-07-16 18:57:03 +00001330 dolog ("Could not mix %d bytes into a capture "
1331 "buffer, mixed %d\n",
1332 bytes, written);
bellard8ead62c2006-07-04 16:51:32 +00001333 break;
1334 }
1335 n -= to_write;
1336 rpos2 = (rpos2 + to_write) % hw->samples;
1337 }
1338 }
1339 }
1340
1341 n = audio_MIN (samples, hw->samples - rpos);
1342 mixeng_clear (hw->mix_buf + rpos, n);
1343 mixeng_clear (hw->mix_buf, samples - n);
1344}
1345
bellardc0fe3822005-11-05 18:55:28 +00001346static void audio_run_out (AudioState *s)
bellard1d14ffa2005-10-30 18:58:22 +00001347{
1348 HWVoiceOut *hw = NULL;
1349 SWVoiceOut *sw;
1350
malc1a7dafc2009-05-14 03:11:35 +04001351 while ((hw = audio_pcm_hw_find_any_enabled_out (hw))) {
bellard1d14ffa2005-10-30 18:58:22 +00001352 int played;
bellard8ead62c2006-07-04 16:51:32 +00001353 int live, free, nb_live, cleanup_required, prev_rpos;
bellard1d14ffa2005-10-30 18:58:22 +00001354
malcbdff2532009-09-18 11:37:39 +04001355 live = audio_pcm_hw_get_live_out (hw, &nb_live);
bellard1d14ffa2005-10-30 18:58:22 +00001356 if (!nb_live) {
1357 live = 0;
bellard85571bc2004-11-07 18:04:02 +00001358 }
bellardc0fe3822005-11-05 18:55:28 +00001359
bellard1d14ffa2005-10-30 18:58:22 +00001360 if (audio_bug (AUDIO_FUNC, live < 0 || live > hw->samples)) {
1361 dolog ("live=%d hw->samples=%d\n", live, hw->samples);
bellardc0fe3822005-11-05 18:55:28 +00001362 continue;
bellard85571bc2004-11-07 18:04:02 +00001363 }
bellard1d14ffa2005-10-30 18:58:22 +00001364
1365 if (hw->pending_disable && !nb_live) {
bellardec36b692006-07-16 18:57:03 +00001366 SWVoiceCap *sc;
bellard1d14ffa2005-10-30 18:58:22 +00001367#ifdef DEBUG_OUT
1368 dolog ("Disabling voice\n");
1369#endif
1370 hw->enabled = 0;
1371 hw->pending_disable = 0;
1372 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
bellardec36b692006-07-16 18:57:03 +00001373 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1374 sc->sw.active = 0;
1375 audio_recalc_and_notify_capture (sc->cap);
bellard8ead62c2006-07-04 16:51:32 +00001376 }
bellard1d14ffa2005-10-30 18:58:22 +00001377 continue;
1378 }
1379
1380 if (!live) {
1381 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1382 if (sw->active) {
1383 free = audio_get_free (sw);
1384 if (free > 0) {
1385 sw->callback.fn (sw->callback.opaque, free);
1386 }
1387 }
1388 }
1389 continue;
1390 }
1391
bellard8ead62c2006-07-04 16:51:32 +00001392 prev_rpos = hw->rpos;
malcbdff2532009-09-18 11:37:39 +04001393 played = hw->pcm_ops->run_out (hw, live);
bellard1d14ffa2005-10-30 18:58:22 +00001394 if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
1395 dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
1396 hw->rpos, hw->samples, played);
1397 hw->rpos = 0;
1398 }
1399
1400#ifdef DEBUG_OUT
bellardc0fe3822005-11-05 18:55:28 +00001401 dolog ("played=%d\n", played);
bellard1d14ffa2005-10-30 18:58:22 +00001402#endif
1403
1404 if (played) {
1405 hw->ts_helper += played;
bellard8ead62c2006-07-04 16:51:32 +00001406 audio_capture_mix_and_clear (hw, prev_rpos, played);
bellard1d14ffa2005-10-30 18:58:22 +00001407 }
1408
bellardc0fe3822005-11-05 18:55:28 +00001409 cleanup_required = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001410 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
bellard1d14ffa2005-10-30 18:58:22 +00001411 if (!sw->active && sw->empty) {
1412 continue;
1413 }
1414
1415 if (audio_bug (AUDIO_FUNC, played > sw->total_hw_samples_mixed)) {
1416 dolog ("played=%d sw->total_hw_samples_mixed=%d\n",
1417 played, sw->total_hw_samples_mixed);
1418 played = sw->total_hw_samples_mixed;
1419 }
1420
1421 sw->total_hw_samples_mixed -= played;
1422
1423 if (!sw->total_hw_samples_mixed) {
1424 sw->empty = 1;
bellardc0fe3822005-11-05 18:55:28 +00001425 cleanup_required |= !sw->active && !sw->callback.fn;
bellard1d14ffa2005-10-30 18:58:22 +00001426 }
1427
1428 if (sw->active) {
1429 free = audio_get_free (sw);
1430 if (free > 0) {
1431 sw->callback.fn (sw->callback.opaque, free);
1432 }
1433 }
bellard85571bc2004-11-07 18:04:02 +00001434 }
bellardc0fe3822005-11-05 18:55:28 +00001435
1436 if (cleanup_required) {
bellardec36b692006-07-16 18:57:03 +00001437 SWVoiceOut *sw1;
1438
1439 sw = hw->sw_head.lh_first;
1440 while (sw) {
1441 sw1 = sw->entries.le_next;
bellardc0fe3822005-11-05 18:55:28 +00001442 if (!sw->active && !sw->callback.fn) {
malc1a7dafc2009-05-14 03:11:35 +04001443 audio_close_out (sw);
bellardc0fe3822005-11-05 18:55:28 +00001444 }
bellardec36b692006-07-16 18:57:03 +00001445 sw = sw1;
bellardc0fe3822005-11-05 18:55:28 +00001446 }
1447 }
bellard85571bc2004-11-07 18:04:02 +00001448 }
bellard1d14ffa2005-10-30 18:58:22 +00001449}
1450
bellardc0fe3822005-11-05 18:55:28 +00001451static void audio_run_in (AudioState *s)
bellard1d14ffa2005-10-30 18:58:22 +00001452{
1453 HWVoiceIn *hw = NULL;
1454
malc1a7dafc2009-05-14 03:11:35 +04001455 while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
bellard1d14ffa2005-10-30 18:58:22 +00001456 SWVoiceIn *sw;
1457 int captured, min;
1458
1459 captured = hw->pcm_ops->run_in (hw);
1460
1461 min = audio_pcm_hw_find_min_in (hw);
1462 hw->total_samples_captured += captured - min;
1463 hw->ts_helper += captured;
1464
1465 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1466 sw->total_hw_samples_acquired -= min;
1467
1468 if (sw->active) {
1469 int avail;
1470
1471 avail = audio_get_avail (sw);
1472 if (avail > 0) {
1473 sw->callback.fn (sw->callback.opaque, avail);
1474 }
1475 }
1476 }
1477 }
1478}
1479
bellard8ead62c2006-07-04 16:51:32 +00001480static void audio_run_capture (AudioState *s)
1481{
1482 CaptureVoiceOut *cap;
1483
1484 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
1485 int live, rpos, captured;
1486 HWVoiceOut *hw = &cap->hw;
1487 SWVoiceOut *sw;
1488
malcbdff2532009-09-18 11:37:39 +04001489 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
bellard8ead62c2006-07-04 16:51:32 +00001490 rpos = hw->rpos;
1491 while (live) {
1492 int left = hw->samples - rpos;
1493 int to_capture = audio_MIN (live, left);
malc1ea879e2008-12-03 22:48:44 +00001494 struct st_sample *src;
bellard8ead62c2006-07-04 16:51:32 +00001495 struct capture_callback *cb;
1496
1497 src = hw->mix_buf + rpos;
1498 hw->clip (cap->buf, src, to_capture);
1499 mixeng_clear (src, to_capture);
1500
1501 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1502 cb->ops.capture (cb->opaque, cap->buf,
1503 to_capture << hw->info.shift);
1504 }
1505 rpos = (rpos + to_capture) % hw->samples;
1506 live -= to_capture;
1507 }
1508 hw->rpos = rpos;
1509
1510 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1511 if (!sw->active && sw->empty) {
1512 continue;
1513 }
1514
1515 if (audio_bug (AUDIO_FUNC, captured > sw->total_hw_samples_mixed)) {
1516 dolog ("captured=%d sw->total_hw_samples_mixed=%d\n",
1517 captured, sw->total_hw_samples_mixed);
1518 captured = sw->total_hw_samples_mixed;
1519 }
1520
1521 sw->total_hw_samples_mixed -= captured;
1522 sw->empty = sw->total_hw_samples_mixed == 0;
1523 }
1524 }
1525}
1526
malc713a98f2009-09-12 02:28:45 +04001527void audio_run (const char *msg)
bellard571ec3d2005-11-20 16:24:34 +00001528{
malc713a98f2009-09-12 02:28:45 +04001529 AudioState *s = &glob_audio_state;
bellard571ec3d2005-11-20 16:24:34 +00001530
1531 audio_run_out (s);
1532 audio_run_in (s);
bellard8ead62c2006-07-04 16:51:32 +00001533 audio_run_capture (s);
malc713a98f2009-09-12 02:28:45 +04001534#ifdef DEBUG_POLL
1535 {
1536 static double prevtime;
1537 double currtime;
1538 struct timeval tv;
bellard571ec3d2005-11-20 16:24:34 +00001539
malc713a98f2009-09-12 02:28:45 +04001540 if (gettimeofday (&tv, NULL)) {
1541 perror ("audio_run: gettimeofday");
1542 return;
1543 }
1544
1545 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1546 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1547 prevtime = currtime;
1548 }
1549#endif
bellard571ec3d2005-11-20 16:24:34 +00001550}
1551
bellard1d14ffa2005-10-30 18:58:22 +00001552static struct audio_option audio_options[] = {
1553 /* DAC */
malc98f9f482009-08-11 20:48:02 +04001554 {
1555 .name = "DAC_FIXED_SETTINGS",
1556 .tag = AUD_OPT_BOOL,
1557 .valp = &conf.fixed_out.enabled,
1558 .descr = "Use fixed settings for host DAC"
1559 },
1560 {
1561 .name = "DAC_FIXED_FREQ",
1562 .tag = AUD_OPT_INT,
1563 .valp = &conf.fixed_out.settings.freq,
1564 .descr = "Frequency for fixed host DAC"
1565 },
1566 {
1567 .name = "DAC_FIXED_FMT",
1568 .tag = AUD_OPT_FMT,
1569 .valp = &conf.fixed_out.settings.fmt,
1570 .descr = "Format for fixed host DAC"
1571 },
1572 {
1573 .name = "DAC_FIXED_CHANNELS",
1574 .tag = AUD_OPT_INT,
1575 .valp = &conf.fixed_out.settings.nchannels,
1576 .descr = "Number of channels for fixed DAC (1 - mono, 2 - stereo)"
1577 },
1578 {
1579 .name = "DAC_VOICES",
1580 .tag = AUD_OPT_INT,
1581 .valp = &conf.fixed_out.nb_voices,
1582 .descr = "Number of voices for DAC"
1583 },
malc713a98f2009-09-12 02:28:45 +04001584 {
1585 .name = "DAC_TRY_POLL",
1586 .tag = AUD_OPT_BOOL,
1587 .valp = &conf.try_poll_out,
1588 .descr = "Attempt using poll mode for DAC"
1589 },
bellard1d14ffa2005-10-30 18:58:22 +00001590 /* ADC */
malc98f9f482009-08-11 20:48:02 +04001591 {
1592 .name = "ADC_FIXED_SETTINGS",
1593 .tag = AUD_OPT_BOOL,
1594 .valp = &conf.fixed_in.enabled,
1595 .descr = "Use fixed settings for host ADC"
1596 },
1597 {
1598 .name = "ADC_FIXED_FREQ",
1599 .tag = AUD_OPT_INT,
1600 .valp = &conf.fixed_in.settings.freq,
1601 .descr = "Frequency for fixed host ADC"
1602 },
1603 {
1604 .name = "ADC_FIXED_FMT",
1605 .tag = AUD_OPT_FMT,
1606 .valp = &conf.fixed_in.settings.fmt,
1607 .descr = "Format for fixed host ADC"
1608 },
1609 {
1610 .name = "ADC_FIXED_CHANNELS",
1611 .tag = AUD_OPT_INT,
1612 .valp = &conf.fixed_in.settings.nchannels,
1613 .descr = "Number of channels for fixed ADC (1 - mono, 2 - stereo)"
1614 },
1615 {
1616 .name = "ADC_VOICES",
1617 .tag = AUD_OPT_INT,
1618 .valp = &conf.fixed_in.nb_voices,
1619 .descr = "Number of voices for ADC"
1620 },
malc713a98f2009-09-12 02:28:45 +04001621 {
1622 .name = "ADC_TRY_POLL",
1623 .tag = AUD_OPT_BOOL,
Jan Kiszka0a90e342009-09-13 22:24:46 +04001624 .valp = &conf.try_poll_in,
malc713a98f2009-09-12 02:28:45 +04001625 .descr = "Attempt using poll mode for ADC"
1626 },
bellard1d14ffa2005-10-30 18:58:22 +00001627 /* Misc */
malc98f9f482009-08-11 20:48:02 +04001628 {
1629 .name = "TIMER_PERIOD",
1630 .tag = AUD_OPT_INT,
1631 .valp = &conf.period.hertz,
1632 .descr = "Timer period in HZ (0 - use lowest possible)"
1633 },
Juan Quintela2700efa2009-08-11 02:31:16 +02001634 { /* End of list */ }
bellard1d14ffa2005-10-30 18:58:22 +00001635};
1636
bellard571ec3d2005-11-20 16:24:34 +00001637static void audio_pp_nb_voices (const char *typ, int nb)
1638{
1639 switch (nb) {
1640 case 0:
1641 printf ("Does not support %s\n", typ);
1642 break;
1643 case 1:
1644 printf ("One %s voice\n", typ);
1645 break;
1646 case INT_MAX:
1647 printf ("Theoretically supports many %s voices\n", typ);
1648 break;
1649 default:
Stefan Weile7d81002011-12-10 00:19:46 +01001650 printf ("Theoretically supports up to %d %s voices\n", nb, typ);
bellard571ec3d2005-11-20 16:24:34 +00001651 break;
1652 }
1653
1654}
1655
bellard1d14ffa2005-10-30 18:58:22 +00001656void AUD_help (void)
1657{
1658 size_t i;
1659
1660 audio_process_options ("AUDIO", audio_options);
malcb1503cd2008-12-22 20:33:55 +00001661 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
bellard1d14ffa2005-10-30 18:58:22 +00001662 struct audio_driver *d = drvtab[i];
1663 if (d->options) {
1664 audio_process_options (d->name, d->options);
1665 }
1666 }
1667
1668 printf ("Audio options:\n");
1669 audio_print_options ("AUDIO", audio_options);
1670 printf ("\n");
1671
1672 printf ("Available drivers:\n");
1673
malcb1503cd2008-12-22 20:33:55 +00001674 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
bellard1d14ffa2005-10-30 18:58:22 +00001675 struct audio_driver *d = drvtab[i];
1676
1677 printf ("Name: %s\n", d->name);
1678 printf ("Description: %s\n", d->descr);
1679
bellard571ec3d2005-11-20 16:24:34 +00001680 audio_pp_nb_voices ("playback", d->max_voices_out);
1681 audio_pp_nb_voices ("capture", d->max_voices_in);
bellard1d14ffa2005-10-30 18:58:22 +00001682
1683 if (d->options) {
1684 printf ("Options:\n");
1685 audio_print_options (d->name, d->options);
1686 }
1687 else {
1688 printf ("No options\n");
1689 }
1690 printf ("\n");
1691 }
1692
1693 printf (
1694 "Options are settable through environment variables.\n"
1695 "Example:\n"
1696#ifdef _WIN32
1697 " set QEMU_AUDIO_DRV=wav\n"
bellard571ec3d2005-11-20 16:24:34 +00001698 " set QEMU_WAV_PATH=c:\\tune.wav\n"
bellard1d14ffa2005-10-30 18:58:22 +00001699#else
1700 " export QEMU_AUDIO_DRV=wav\n"
1701 " export QEMU_WAV_PATH=$HOME/tune.wav\n"
1702 "(for csh replace export with setenv in the above)\n"
1703#endif
1704 " qemu ...\n\n"
1705 );
1706}
1707
bellardc0fe3822005-11-05 18:55:28 +00001708static int audio_driver_init (AudioState *s, struct audio_driver *drv)
bellard1d14ffa2005-10-30 18:58:22 +00001709{
1710 if (drv->options) {
1711 audio_process_options (drv->name, drv->options);
1712 }
bellardc0fe3822005-11-05 18:55:28 +00001713 s->drv_opaque = drv->init ();
bellard1d14ffa2005-10-30 18:58:22 +00001714
bellardc0fe3822005-11-05 18:55:28 +00001715 if (s->drv_opaque) {
malc1a7dafc2009-05-14 03:11:35 +04001716 audio_init_nb_voices_out (drv);
1717 audio_init_nb_voices_in (drv);
bellardc0fe3822005-11-05 18:55:28 +00001718 s->drv = drv;
bellard85571bc2004-11-07 18:04:02 +00001719 return 0;
1720 }
bellard1d14ffa2005-10-30 18:58:22 +00001721 else {
1722 dolog ("Could not init `%s' audio driver\n", drv->name);
1723 return -1;
1724 }
bellard85571bc2004-11-07 18:04:02 +00001725}
1726
aliguori9781e042009-01-22 17:15:29 +00001727static void audio_vm_change_state_handler (void *opaque, int running,
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001728 RunState state)
bellard85571bc2004-11-07 18:04:02 +00001729{
bellardc0fe3822005-11-05 18:55:28 +00001730 AudioState *s = opaque;
bellard1d14ffa2005-10-30 18:58:22 +00001731 HWVoiceOut *hwo = NULL;
1732 HWVoiceIn *hwi = NULL;
bellard541e0842005-11-11 00:04:19 +00001733 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
bellard85571bc2004-11-07 18:04:02 +00001734
malc978dd632009-02-18 20:44:04 +00001735 s->vm_running = running;
malc1a7dafc2009-05-14 03:11:35 +04001736 while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
malc713a98f2009-09-12 02:28:45 +04001737 hwo->pcm_ops->ctl_out (hwo, op, conf.try_poll_out);
bellard1d14ffa2005-10-30 18:58:22 +00001738 }
1739
malc1a7dafc2009-05-14 03:11:35 +04001740 while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
malc713a98f2009-09-12 02:28:45 +04001741 hwi->pcm_ops->ctl_in (hwi, op, conf.try_poll_in);
bellard85571bc2004-11-07 18:04:02 +00001742 }
malc39deb1e2010-11-18 14:30:12 +03001743 audio_reset_timer (s);
bellard85571bc2004-11-07 18:04:02 +00001744}
1745
1746static void audio_atexit (void)
1747{
bellardc0fe3822005-11-05 18:55:28 +00001748 AudioState *s = &glob_audio_state;
bellard1d14ffa2005-10-30 18:58:22 +00001749 HWVoiceOut *hwo = NULL;
1750 HWVoiceIn *hwi = NULL;
bellard85571bc2004-11-07 18:04:02 +00001751
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001752 while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
bellardec36b692006-07-16 18:57:03 +00001753 SWVoiceCap *sc;
bellard8ead62c2006-07-04 16:51:32 +00001754
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001755 if (hwo->enabled) {
1756 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1757 }
bellard1d14ffa2005-10-30 18:58:22 +00001758 hwo->pcm_ops->fini_out (hwo);
bellard8ead62c2006-07-04 16:51:32 +00001759
bellardec36b692006-07-16 18:57:03 +00001760 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1761 CaptureVoiceOut *cap = sc->cap;
1762 struct capture_callback *cb;
1763
1764 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1765 cb->ops.destroy (cb->opaque);
1766 }
bellard8ead62c2006-07-04 16:51:32 +00001767 }
bellard1d14ffa2005-10-30 18:58:22 +00001768 }
1769
Jan Kiszkaaeb29b62012-05-24 12:05:15 -03001770 while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
1771 if (hwi->enabled) {
1772 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1773 }
bellard1d14ffa2005-10-30 18:58:22 +00001774 hwi->pcm_ops->fini_in (hwi);
bellard85571bc2004-11-07 18:04:02 +00001775 }
bellardc0fe3822005-11-05 18:55:28 +00001776
1777 if (s->drv) {
1778 s->drv->fini (s->drv_opaque);
1779 }
bellard85571bc2004-11-07 18:04:02 +00001780}
1781
Juan Quintelad959fce2009-12-02 11:49:34 +01001782static const VMStateDescription vmstate_audio = {
1783 .name = "audio",
1784 .version_id = 1,
1785 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +02001786 .fields = (VMStateField[]) {
Juan Quintelad959fce2009-12-02 11:49:34 +01001787 VMSTATE_END_OF_LIST()
bellard1d14ffa2005-10-30 18:58:22 +00001788 }
Juan Quintelad959fce2009-12-02 11:49:34 +01001789};
bellard85571bc2004-11-07 18:04:02 +00001790
malc1a7dafc2009-05-14 03:11:35 +04001791static void audio_init (void)
bellard85571bc2004-11-07 18:04:02 +00001792{
bellard1d14ffa2005-10-30 18:58:22 +00001793 size_t i;
bellard85571bc2004-11-07 18:04:02 +00001794 int done = 0;
1795 const char *drvname;
malc713a98f2009-09-12 02:28:45 +04001796 VMChangeStateEntry *e;
bellardc0fe3822005-11-05 18:55:28 +00001797 AudioState *s = &glob_audio_state;
bellard85571bc2004-11-07 18:04:02 +00001798
Paul Brook0d9acba2009-05-12 12:02:38 +01001799 if (s->drv) {
malc1a7dafc2009-05-14 03:11:35 +04001800 return;
Paul Brook0d9acba2009-05-12 12:02:38 +01001801 }
1802
Blue Swirl72cf2d42009-09-12 07:36:22 +00001803 QLIST_INIT (&s->hw_head_out);
1804 QLIST_INIT (&s->hw_head_in);
1805 QLIST_INIT (&s->cap_head);
bellard571ec3d2005-11-20 16:24:34 +00001806 atexit (audio_atexit);
1807
Alex Blighbc72ad62013-08-21 16:03:08 +01001808 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
bellard571ec3d2005-11-20 16:24:34 +00001809 if (!s->ts) {
Paul Brook0d9acba2009-05-12 12:02:38 +01001810 hw_error("Could not create audio timer\n");
bellard571ec3d2005-11-20 16:24:34 +00001811 }
1812
bellard1d14ffa2005-10-30 18:58:22 +00001813 audio_process_options ("AUDIO", audio_options);
bellard85571bc2004-11-07 18:04:02 +00001814
bellardc0fe3822005-11-05 18:55:28 +00001815 s->nb_hw_voices_out = conf.fixed_out.nb_voices;
1816 s->nb_hw_voices_in = conf.fixed_in.nb_voices;
1817
bellard1d14ffa2005-10-30 18:58:22 +00001818 if (s->nb_hw_voices_out <= 0) {
bellard571ec3d2005-11-20 16:24:34 +00001819 dolog ("Bogus number of playback voices %d, setting to 1\n",
bellard1d14ffa2005-10-30 18:58:22 +00001820 s->nb_hw_voices_out);
1821 s->nb_hw_voices_out = 1;
bellard85571bc2004-11-07 18:04:02 +00001822 }
1823
bellard1d14ffa2005-10-30 18:58:22 +00001824 if (s->nb_hw_voices_in <= 0) {
bellard571ec3d2005-11-20 16:24:34 +00001825 dolog ("Bogus number of capture voices %d, setting to 0\n",
bellard1d14ffa2005-10-30 18:58:22 +00001826 s->nb_hw_voices_in);
bellard571ec3d2005-11-20 16:24:34 +00001827 s->nb_hw_voices_in = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001828 }
1829
1830 {
1831 int def;
1832 drvname = audio_get_conf_str ("QEMU_AUDIO_DRV", NULL, &def);
1833 }
1834
bellard85571bc2004-11-07 18:04:02 +00001835 if (drvname) {
1836 int found = 0;
bellard1d14ffa2005-10-30 18:58:22 +00001837
malcb1503cd2008-12-22 20:33:55 +00001838 for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
bellard85571bc2004-11-07 18:04:02 +00001839 if (!strcmp (drvname, drvtab[i]->name)) {
bellardc0fe3822005-11-05 18:55:28 +00001840 done = !audio_driver_init (s, drvtab[i]);
bellard85571bc2004-11-07 18:04:02 +00001841 found = 1;
1842 break;
1843 }
1844 }
bellard1d14ffa2005-10-30 18:58:22 +00001845
bellard85571bc2004-11-07 18:04:02 +00001846 if (!found) {
1847 dolog ("Unknown audio driver `%s'\n", drvname);
bellard1d14ffa2005-10-30 18:58:22 +00001848 dolog ("Run with -audio-help to list available drivers\n");
bellard85571bc2004-11-07 18:04:02 +00001849 }
1850 }
1851
bellard85571bc2004-11-07 18:04:02 +00001852 if (!done) {
malcb1503cd2008-12-22 20:33:55 +00001853 for (i = 0; !done && i < ARRAY_SIZE (drvtab); i++) {
bellard1d14ffa2005-10-30 18:58:22 +00001854 if (drvtab[i]->can_be_default) {
bellardc0fe3822005-11-05 18:55:28 +00001855 done = !audio_driver_init (s, drvtab[i]);
bellard1d14ffa2005-10-30 18:58:22 +00001856 }
bellard85571bc2004-11-07 18:04:02 +00001857 }
1858 }
1859
bellard85571bc2004-11-07 18:04:02 +00001860 if (!done) {
bellardc0fe3822005-11-05 18:55:28 +00001861 done = !audio_driver_init (s, &no_audio_driver);
1862 if (!done) {
Paul Brook0d9acba2009-05-12 12:02:38 +01001863 hw_error("Could not initialize audio subsystem\n");
bellard1d14ffa2005-10-30 18:58:22 +00001864 }
1865 else {
bellardc0fe3822005-11-05 18:55:28 +00001866 dolog ("warning: Using timer based audio emulation\n");
bellard1d14ffa2005-10-30 18:58:22 +00001867 }
bellard85571bc2004-11-07 18:04:02 +00001868 }
bellard1d14ffa2005-10-30 18:58:22 +00001869
Paul Brook0d9acba2009-05-12 12:02:38 +01001870 if (conf.period.hertz <= 0) {
1871 if (conf.period.hertz < 0) {
1872 dolog ("warning: Timer period is negative - %d "
1873 "treating as zero\n",
1874 conf.period.hertz);
bellard1d14ffa2005-10-30 18:58:22 +00001875 }
Paul Brook0d9acba2009-05-12 12:02:38 +01001876 conf.period.ticks = 1;
1877 } else {
malc4f4cc0e2009-09-18 08:16:03 +04001878 conf.period.ticks =
1879 muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
bellard1d14ffa2005-10-30 18:58:22 +00001880 }
Paul Brook0d9acba2009-05-12 12:02:38 +01001881
1882 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1883 if (!e) {
1884 dolog ("warning: Could not register change state handler\n"
1885 "(Audio can continue looping even after stopping the VM)\n");
bellard1d14ffa2005-10-30 18:58:22 +00001886 }
1887
Blue Swirl72cf2d42009-09-12 07:36:22 +00001888 QLIST_INIT (&s->card_head);
Alex Williamson0be71e32010-06-25 11:09:07 -06001889 vmstate_register (NULL, 0, &vmstate_audio, s);
bellard85571bc2004-11-07 18:04:02 +00001890}
bellard8ead62c2006-07-04 16:51:32 +00001891
malc1a7dafc2009-05-14 03:11:35 +04001892void AUD_register_card (const char *name, QEMUSoundCard *card)
1893{
1894 audio_init ();
Anthony Liguori7267c092011-08-20 22:09:37 -05001895 card->name = g_strdup (name);
malc1a7dafc2009-05-14 03:11:35 +04001896 memset (&card->entries, 0, sizeof (card->entries));
Blue Swirl72cf2d42009-09-12 07:36:22 +00001897 QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
malc1a7dafc2009-05-14 03:11:35 +04001898}
1899
1900void AUD_remove_card (QEMUSoundCard *card)
1901{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001902 QLIST_REMOVE (card, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001903 g_free (card->name);
malc1a7dafc2009-05-14 03:11:35 +04001904}
1905
1906
bellardec36b692006-07-16 18:57:03 +00001907CaptureVoiceOut *AUD_add_capture (
malc1ea879e2008-12-03 22:48:44 +00001908 struct audsettings *as,
bellard8ead62c2006-07-04 16:51:32 +00001909 struct audio_capture_ops *ops,
1910 void *cb_opaque
1911 )
1912{
malc1a7dafc2009-05-14 03:11:35 +04001913 AudioState *s = &glob_audio_state;
bellard8ead62c2006-07-04 16:51:32 +00001914 CaptureVoiceOut *cap;
1915 struct capture_callback *cb;
1916
bellardec36b692006-07-16 18:57:03 +00001917 if (audio_validate_settings (as)) {
bellard8ead62c2006-07-04 16:51:32 +00001918 dolog ("Invalid settings were passed when trying to add capture\n");
1919 audio_print_settings (as);
bellardec36b692006-07-16 18:57:03 +00001920 goto err0;
bellard8ead62c2006-07-04 16:51:32 +00001921 }
1922
1923 cb = audio_calloc (AUDIO_FUNC, 1, sizeof (*cb));
1924 if (!cb) {
1925 dolog ("Could not allocate capture callback information, size %zu\n",
1926 sizeof (*cb));
1927 goto err0;
1928 }
1929 cb->ops = *ops;
1930 cb->opaque = cb_opaque;
1931
malc1a7dafc2009-05-14 03:11:35 +04001932 cap = audio_pcm_capture_find_specific (as);
bellard8ead62c2006-07-04 16:51:32 +00001933 if (cap) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001934 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
bellardec36b692006-07-16 18:57:03 +00001935 return cap;
bellard8ead62c2006-07-04 16:51:32 +00001936 }
1937 else {
1938 HWVoiceOut *hw;
1939 CaptureVoiceOut *cap;
1940
1941 cap = audio_calloc (AUDIO_FUNC, 1, sizeof (*cap));
1942 if (!cap) {
1943 dolog ("Could not allocate capture voice, size %zu\n",
1944 sizeof (*cap));
1945 goto err1;
1946 }
1947
1948 hw = &cap->hw;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001949 QLIST_INIT (&hw->sw_head);
1950 QLIST_INIT (&cap->cb_head);
bellard8ead62c2006-07-04 16:51:32 +00001951
1952 /* XXX find a more elegant way */
1953 hw->samples = 4096 * 4;
1954 hw->mix_buf = audio_calloc (AUDIO_FUNC, hw->samples,
malc1ea879e2008-12-03 22:48:44 +00001955 sizeof (struct st_sample));
bellard8ead62c2006-07-04 16:51:32 +00001956 if (!hw->mix_buf) {
1957 dolog ("Could not allocate capture mix buffer (%d samples)\n",
1958 hw->samples);
1959 goto err2;
1960 }
1961
bellardd929eba2006-07-04 21:47:22 +00001962 audio_pcm_init_info (&hw->info, as);
bellard8ead62c2006-07-04 16:51:32 +00001963
1964 cap->buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
1965 if (!cap->buf) {
1966 dolog ("Could not allocate capture buffer "
1967 "(%d samples, each %d bytes)\n",
1968 hw->samples, 1 << hw->info.shift);
1969 goto err3;
1970 }
1971
1972 hw->clip = mixeng_clip
1973 [hw->info.nchannels == 2]
1974 [hw->info.sign]
bellardd929eba2006-07-04 21:47:22 +00001975 [hw->info.swap_endianness]
thsf941aa22007-02-17 22:19:29 +00001976 [audio_bits_to_index (hw->info.bits)];
bellard8ead62c2006-07-04 16:51:32 +00001977
Blue Swirl72cf2d42009-09-12 07:36:22 +00001978 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1979 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
bellard8ead62c2006-07-04 16:51:32 +00001980
1981 hw = NULL;
malc1a7dafc2009-05-14 03:11:35 +04001982 while ((hw = audio_pcm_hw_find_any_out (hw))) {
1983 audio_attach_capture (hw);
bellard8ead62c2006-07-04 16:51:32 +00001984 }
bellardec36b692006-07-16 18:57:03 +00001985 return cap;
bellard8ead62c2006-07-04 16:51:32 +00001986
1987 err3:
Anthony Liguori7267c092011-08-20 22:09:37 -05001988 g_free (cap->hw.mix_buf);
bellard8ead62c2006-07-04 16:51:32 +00001989 err2:
Anthony Liguori7267c092011-08-20 22:09:37 -05001990 g_free (cap);
bellard8ead62c2006-07-04 16:51:32 +00001991 err1:
Anthony Liguori7267c092011-08-20 22:09:37 -05001992 g_free (cb);
bellard8ead62c2006-07-04 16:51:32 +00001993 err0:
bellardec36b692006-07-16 18:57:03 +00001994 return NULL;
1995 }
1996}
1997
1998void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1999{
2000 struct capture_callback *cb;
2001
2002 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
2003 if (cb->opaque == cb_opaque) {
2004 cb->ops.destroy (cb_opaque);
Blue Swirl72cf2d42009-09-12 07:36:22 +00002005 QLIST_REMOVE (cb, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002006 g_free (cb);
bellardec36b692006-07-16 18:57:03 +00002007
2008 if (!cap->cb_head.lh_first) {
2009 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
bellarda3c25992006-07-18 21:09:59 +00002010
bellardec36b692006-07-16 18:57:03 +00002011 while (sw) {
bellarda3c25992006-07-18 21:09:59 +00002012 SWVoiceCap *sc = (SWVoiceCap *) sw;
bellardec36b692006-07-16 18:57:03 +00002013#ifdef DEBUG_CAPTURE
2014 dolog ("freeing %s\n", sw->name);
2015#endif
bellarda3c25992006-07-18 21:09:59 +00002016
bellardec36b692006-07-16 18:57:03 +00002017 sw1 = sw->entries.le_next;
2018 if (sw->rate) {
2019 st_rate_stop (sw->rate);
2020 sw->rate = NULL;
2021 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002022 QLIST_REMOVE (sw, entries);
2023 QLIST_REMOVE (sc, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002024 g_free (sc);
bellardec36b692006-07-16 18:57:03 +00002025 sw = sw1;
2026 }
Blue Swirl72cf2d42009-09-12 07:36:22 +00002027 QLIST_REMOVE (cap, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05002028 g_free (cap);
bellardec36b692006-07-16 18:57:03 +00002029 }
2030 return;
2031 }
bellard8ead62c2006-07-04 16:51:32 +00002032 }
2033}
balrog683efdc2008-05-04 10:21:03 +00002034
2035void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
2036{
2037 if (sw) {
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002038 HWVoiceOut *hw = sw->hw;
2039
balrog683efdc2008-05-04 10:21:03 +00002040 sw->vol.mute = mute;
2041 sw->vol.l = nominal_volume.l * lvol / 255;
2042 sw->vol.r = nominal_volume.r * rvol / 255;
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002043
2044 if (hw->pcm_ops->ctl_out) {
2045 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
2046 }
balrog683efdc2008-05-04 10:21:03 +00002047 }
2048}
2049
2050void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
2051{
2052 if (sw) {
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002053 HWVoiceIn *hw = sw->hw;
2054
balrog683efdc2008-05-04 10:21:03 +00002055 sw->vol.mute = mute;
2056 sw->vol.l = nominal_volume.l * lvol / 255;
2057 sw->vol.r = nominal_volume.r * rvol / 255;
Marc-André Lureau6c95ab92012-04-17 14:32:35 +02002058
2059 if (hw->pcm_ops->ctl_in) {
2060 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);
2061 }
balrog683efdc2008-05-04 10:21:03 +00002062 }
2063}