]> git.proxmox.com Git - mirror_qemu.git/blame - audio/audio.c
dsoundaudio: port to the new audio backend api
[mirror_qemu.git] / audio / audio.c
CommitLineData
85571bc7
FB
1/*
2 * QEMU Audio subsystem
1d14ffa9
FB
3 *
4 * Copyright (c) 2003-2005 Vassili Karpov (malc)
5 *
85571bc7
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
0b8fa32f 24
6086a565 25#include "qemu/osdep.h"
87ecb68b 26#include "audio.h"
d6454270 27#include "migration/vmstate.h"
83c9089e 28#include "monitor/monitor.h"
1de7afc9 29#include "qemu/timer.h"
71830221
KZ
30#include "qapi/error.h"
31#include "qapi/qobject-input-visitor.h"
32#include "qapi/qapi-visit-audio.h"
f348b6d1 33#include "qemu/cutils.h"
0b8fa32f 34#include "qemu/module.h"
3d4d16f4 35#include "sysemu/replay.h"
54d31236 36#include "sysemu/runstate.h"
1a961e78 37#include "trace.h"
85571bc7 38
1d14ffa9
FB
39#define AUDIO_CAP "audio"
40#include "audio_int.h"
85571bc7 41
1d14ffa9
FB
42/* #define DEBUG_LIVE */
43/* #define DEBUG_OUT */
8ead62cf 44/* #define DEBUG_CAPTURE */
713a98f8 45/* #define DEBUG_POLL */
85571bc7 46
c0fe3827
FB
47#define SW_NAME(sw) (sw)->name ? (sw)->name : "unknown"
48
2358a494
JQ
49
50/* Order of CONFIG_AUDIO_DRIVERS is import.
51 The 1st one is the one used by default, that is the reason
52 that we generate the list.
53*/
71830221 54const char *audio_prio_list[] = {
d3893a39 55 "spice",
2358a494 56 CONFIG_AUDIO_DRIVERS
d3893a39
GH
57 "none",
58 "wav",
71830221 59 NULL
1d14ffa9 60};
85571bc7 61
d3893a39 62static QLIST_HEAD(, audio_driver) audio_drivers;
71830221 63static AudiodevListHead audiodevs = QSIMPLEQ_HEAD_INITIALIZER(audiodevs);
d3893a39
GH
64
65void audio_driver_register(audio_driver *drv)
66{
67 QLIST_INSERT_HEAD(&audio_drivers, drv, next);
68}
69
70audio_driver *audio_driver_lookup(const char *name)
71{
72 struct audio_driver *d;
73
74 QLIST_FOREACH(d, &audio_drivers, next) {
75 if (strcmp(name, d->name) == 0) {
76 return d;
77 }
78 }
65ba8696
GH
79
80 audio_module_load_one(name);
81 QLIST_FOREACH(d, &audio_drivers, next) {
82 if (strcmp(name, d->name) == 0) {
83 return d;
84 }
85 }
86
d3893a39
GH
87 return NULL;
88}
89
ecd97e95
KZ
90static QTAILQ_HEAD(AudioStateHead, AudioState) audio_states =
91 QTAILQ_HEAD_INITIALIZER(audio_states);
c0fe3827 92
00e07679 93const struct mixeng_volume nominal_volume = {
14658cd1 94 .mute = 0,
1d14ffa9 95#ifdef FLOAT_MIXENG
14658cd1
JQ
96 .r = 1.0,
97 .l = 1.0,
1d14ffa9 98#else
14658cd1
JQ
99 .r = 1ULL << 32,
100 .l = 1ULL << 32,
1d14ffa9 101#endif
85571bc7
FB
102};
103
af2041ed
KZ
104static bool legacy_config = true;
105
1d14ffa9
FB
106#ifdef AUDIO_IS_FLAWLESS_AND_NO_CHECKS_ARE_REQURIED
107#error No its not
108#else
109int audio_bug (const char *funcname, int cond)
85571bc7 110{
1d14ffa9
FB
111 if (cond) {
112 static int shown;
113
8ead62cf 114 AUD_log (NULL, "A bug was just triggered in %s\n", funcname);
1d14ffa9
FB
115 if (!shown) {
116 shown = 1;
117 AUD_log (NULL, "Save all your work and restart without audio\n");
1d14ffa9
FB
118 AUD_log (NULL, "I am sorry\n");
119 }
120 AUD_log (NULL, "Context:\n");
121
122#if defined AUDIO_BREAKPOINT_ON_BUG
123# if defined HOST_I386
124# if defined __GNUC__
125 __asm__ ("int3");
126# elif defined _MSC_VER
127 _asm _emit 0xcc;
128# else
129 abort ();
130# endif
131# else
132 abort ();
133# endif
134#endif
85571bc7
FB
135 }
136
1d14ffa9 137 return cond;
85571bc7 138}
1d14ffa9 139#endif
85571bc7 140
f941aa25
TS
141static inline int audio_bits_to_index (int bits)
142{
143 switch (bits) {
144 case 8:
145 return 0;
146
147 case 16:
148 return 1;
149
150 case 32:
151 return 2;
152
153 default:
154 audio_bug ("bits_to_index", 1);
155 AUD_log (NULL, "invalid bits %d\n", bits);
156 return 0;
157 }
158}
159
c0fe3827
FB
160void *audio_calloc (const char *funcname, int nmemb, size_t size)
161{
162 int cond;
163 size_t len;
164
165 len = nmemb * size;
166 cond = !nmemb || !size;
167 cond |= nmemb < 0;
168 cond |= len < size;
169
170 if (audio_bug ("audio_calloc", cond)) {
171 AUD_log (NULL, "%s passed invalid arguments to audio_calloc\n",
172 funcname);
541e0844 173 AUD_log (NULL, "nmemb=%d size=%zu (len=%zu)\n", nmemb, size, len);
c0fe3827
FB
174 return NULL;
175 }
176
7267c094 177 return g_malloc0 (len);
c0fe3827
FB
178}
179
541e0844 180void AUD_vlog (const char *cap, const char *fmt, va_list ap)
85571bc7 181{
06ac27f6
KZ
182 if (cap) {
183 fprintf(stderr, "%s: ", cap);
541e0844 184 }
541e0844 185
06ac27f6 186 vfprintf(stderr, fmt, ap);
85571bc7
FB
187}
188
541e0844 189void AUD_log (const char *cap, const char *fmt, ...)
85571bc7 190{
541e0844
FB
191 va_list ap;
192
193 va_start (ap, fmt);
194 AUD_vlog (cap, fmt, ap);
195 va_end (ap);
85571bc7
FB
196}
197
1ea879e5 198static void audio_print_settings (struct audsettings *as)
c0fe3827
FB
199{
200 dolog ("frequency=%d nchannels=%d fmt=", as->freq, as->nchannels);
201
202 switch (as->fmt) {
85bc5852 203 case AUDIO_FORMAT_S8:
c0fe3827
FB
204 AUD_log (NULL, "S8");
205 break;
85bc5852 206 case AUDIO_FORMAT_U8:
c0fe3827
FB
207 AUD_log (NULL, "U8");
208 break;
85bc5852 209 case AUDIO_FORMAT_S16:
c0fe3827
FB
210 AUD_log (NULL, "S16");
211 break;
85bc5852 212 case AUDIO_FORMAT_U16:
c0fe3827
FB
213 AUD_log (NULL, "U16");
214 break;
85bc5852 215 case AUDIO_FORMAT_S32:
d50997f9 216 AUD_log (NULL, "S32");
217 break;
85bc5852 218 case AUDIO_FORMAT_U32:
d50997f9 219 AUD_log (NULL, "U32");
220 break;
c0fe3827
FB
221 default:
222 AUD_log (NULL, "invalid(%d)", as->fmt);
223 break;
224 }
ec36b695
FB
225
226 AUD_log (NULL, " endianness=");
d929eba5
FB
227 switch (as->endianness) {
228 case 0:
229 AUD_log (NULL, "little");
230 break;
231 case 1:
232 AUD_log (NULL, "big");
233 break;
234 default:
235 AUD_log (NULL, "invalid");
236 break;
237 }
c0fe3827
FB
238 AUD_log (NULL, "\n");
239}
240
1ea879e5 241static int audio_validate_settings (struct audsettings *as)
c0fe3827
FB
242{
243 int invalid;
244
245 invalid = as->nchannels != 1 && as->nchannels != 2;
d929eba5 246 invalid |= as->endianness != 0 && as->endianness != 1;
c0fe3827
FB
247
248 switch (as->fmt) {
85bc5852
KZ
249 case AUDIO_FORMAT_S8:
250 case AUDIO_FORMAT_U8:
251 case AUDIO_FORMAT_S16:
252 case AUDIO_FORMAT_U16:
253 case AUDIO_FORMAT_S32:
254 case AUDIO_FORMAT_U32:
c0fe3827
FB
255 break;
256 default:
257 invalid = 1;
258 break;
259 }
260
261 invalid |= as->freq <= 0;
d929eba5 262 return invalid ? -1 : 0;
c0fe3827
FB
263}
264
1ea879e5 265static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *as)
85571bc7 266{
1d14ffa9 267 int bits = 8, sign = 0;
85571bc7 268
c0fe3827 269 switch (as->fmt) {
85bc5852 270 case AUDIO_FORMAT_S8:
1d14ffa9 271 sign = 1;
b4bd0b16 272 /* fall through */
85bc5852 273 case AUDIO_FORMAT_U8:
1d14ffa9
FB
274 break;
275
85bc5852 276 case AUDIO_FORMAT_S16:
1d14ffa9 277 sign = 1;
b4bd0b16 278 /* fall through */
85bc5852 279 case AUDIO_FORMAT_U16:
1d14ffa9
FB
280 bits = 16;
281 break;
f941aa25 282
85bc5852 283 case AUDIO_FORMAT_S32:
f941aa25 284 sign = 1;
b4bd0b16 285 /* fall through */
85bc5852 286 case AUDIO_FORMAT_U32:
f941aa25
TS
287 bits = 32;
288 break;
85bc5852
KZ
289
290 default:
291 abort();
85571bc7 292 }
c0fe3827
FB
293 return info->freq == as->freq
294 && info->nchannels == as->nchannels
1d14ffa9 295 && info->sign == sign
d929eba5
FB
296 && info->bits == bits
297 && info->swap_endianness == (as->endianness != AUDIO_HOST_ENDIANNESS);
1d14ffa9 298}
85571bc7 299
1ea879e5 300void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as)
1d14ffa9 301{
f941aa25 302 int bits = 8, sign = 0, shift = 0;
1d14ffa9 303
c0fe3827 304 switch (as->fmt) {
85bc5852 305 case AUDIO_FORMAT_S8:
85571bc7 306 sign = 1;
85bc5852 307 case AUDIO_FORMAT_U8:
85571bc7
FB
308 break;
309
85bc5852 310 case AUDIO_FORMAT_S16:
85571bc7 311 sign = 1;
e4634941 312 /* fall through */
85bc5852 313 case AUDIO_FORMAT_U16:
85571bc7 314 bits = 16;
f941aa25
TS
315 shift = 1;
316 break;
317
85bc5852 318 case AUDIO_FORMAT_S32:
f941aa25 319 sign = 1;
e4634941 320 /* fall through */
85bc5852 321 case AUDIO_FORMAT_U32:
f941aa25
TS
322 bits = 32;
323 shift = 2;
85571bc7 324 break;
85bc5852
KZ
325
326 default:
327 abort();
85571bc7
FB
328 }
329
c0fe3827 330 info->freq = as->freq;
1d14ffa9
FB
331 info->bits = bits;
332 info->sign = sign;
c0fe3827 333 info->nchannels = as->nchannels;
f941aa25 334 info->shift = (as->nchannels == 2) + shift;
1d14ffa9
FB
335 info->align = (1 << info->shift) - 1;
336 info->bytes_per_second = info->freq << info->shift;
d929eba5 337 info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS);
85571bc7
FB
338}
339
1d14ffa9 340void audio_pcm_info_clear_buf (struct audio_pcm_info *info, void *buf, int len)
85571bc7 341{
1d14ffa9
FB
342 if (!len) {
343 return;
344 }
345
346 if (info->sign) {
e2f909be 347 memset (buf, 0x00, len << info->shift);
85571bc7
FB
348 }
349 else {
f941aa25
TS
350 switch (info->bits) {
351 case 8:
e2f909be 352 memset (buf, 0x80, len << info->shift);
f941aa25 353 break;
1d14ffa9 354
f941aa25
TS
355 case 16:
356 {
357 int i;
358 uint16_t *p = buf;
359 int shift = info->nchannels - 1;
360 short s = INT16_MAX;
361
362 if (info->swap_endianness) {
363 s = bswap16 (s);
364 }
365
366 for (i = 0; i < len << shift; i++) {
367 p[i] = s;
368 }
1d14ffa9 369 }
f941aa25
TS
370 break;
371
372 case 32:
373 {
374 int i;
375 uint32_t *p = buf;
376 int shift = info->nchannels - 1;
377 int32_t s = INT32_MAX;
378
379 if (info->swap_endianness) {
380 s = bswap32 (s);
381 }
1d14ffa9 382
f941aa25
TS
383 for (i = 0; i < len << shift; i++) {
384 p[i] = s;
385 }
1d14ffa9 386 }
f941aa25
TS
387 break;
388
389 default:
390 AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n",
391 info->bits);
392 break;
1d14ffa9 393 }
85571bc7
FB
394 }
395}
396
8ead62cf
FB
397/*
398 * Capture
399 */
00e07679 400static void noop_conv (struct st_sample *dst, const void *src, int samples)
8ead62cf
FB
401{
402 (void) src;
403 (void) dst;
404 (void) samples;
8ead62cf
FB
405}
406
526fb058
KZ
407static CaptureVoiceOut *audio_pcm_capture_find_specific(AudioState *s,
408 struct audsettings *as)
8ead62cf
FB
409{
410 CaptureVoiceOut *cap;
8ead62cf
FB
411
412 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
d929eba5 413 if (audio_pcm_info_eq (&cap->hw.info, as)) {
8ead62cf
FB
414 return cap;
415 }
416 }
417 return NULL;
418}
419
ec36b695 420static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd)
8ead62cf 421{
ec36b695
FB
422 struct capture_callback *cb;
423
424#ifdef DEBUG_CAPTURE
425 dolog ("notification %d sent\n", cmd);
426#endif
427 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
428 cb->ops.notify (cb->opaque, cmd);
429 }
430}
8ead62cf 431
ec36b695
FB
432static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled)
433{
434 if (cap->hw.enabled != enabled) {
435 audcnotification_e cmd;
8ead62cf 436 cap->hw.enabled = enabled;
ec36b695
FB
437 cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE;
438 audio_notify_capture (cap, cmd);
8ead62cf
FB
439 }
440}
441
442static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap)
443{
444 HWVoiceOut *hw = &cap->hw;
445 SWVoiceOut *sw;
446 int enabled = 0;
447
ec36b695 448 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
8ead62cf
FB
449 if (sw->active) {
450 enabled = 1;
451 break;
452 }
453 }
ec36b695 454 audio_capture_maybe_changed (cap, enabled);
8ead62cf
FB
455}
456
457static void audio_detach_capture (HWVoiceOut *hw)
458{
ec36b695
FB
459 SWVoiceCap *sc = hw->cap_head.lh_first;
460
461 while (sc) {
462 SWVoiceCap *sc1 = sc->entries.le_next;
463 SWVoiceOut *sw = &sc->sw;
464 CaptureVoiceOut *cap = sc->cap;
465 int was_active = sw->active;
8ead62cf 466
8ead62cf
FB
467 if (sw->rate) {
468 st_rate_stop (sw->rate);
469 sw->rate = NULL;
470 }
471
72cf2d4f
BS
472 QLIST_REMOVE (sw, entries);
473 QLIST_REMOVE (sc, entries);
7267c094 474 g_free (sc);
ec36b695
FB
475 if (was_active) {
476 /* We have removed soft voice from the capture:
477 this might have changed the overall status of the capture
478 since this might have been the only active voice */
479 audio_recalc_and_notify_capture (cap);
480 }
481 sc = sc1;
8ead62cf
FB
482 }
483}
484
1a7dafce 485static int audio_attach_capture (HWVoiceOut *hw)
8ead62cf 486{
526fb058 487 AudioState *s = hw->s;
8ead62cf
FB
488 CaptureVoiceOut *cap;
489
490 audio_detach_capture (hw);
491 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
ec36b695 492 SWVoiceCap *sc;
8ead62cf 493 SWVoiceOut *sw;
ec36b695 494 HWVoiceOut *hw_cap = &cap->hw;
8ead62cf 495
e8d85444 496 sc = g_malloc0(sizeof(*sc));
8ead62cf 497
ec36b695
FB
498 sc->cap = cap;
499 sw = &sc->sw;
8ead62cf 500 sw->hw = hw_cap;
ec36b695 501 sw->info = hw->info;
8ead62cf
FB
502 sw->empty = 1;
503 sw->active = hw->enabled;
504 sw->conv = noop_conv;
505 sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
83617103 506 sw->vol = nominal_volume;
8ead62cf
FB
507 sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
508 if (!sw->rate) {
509 dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
7267c094 510 g_free (sw);
8ead62cf
FB
511 return -1;
512 }
72cf2d4f
BS
513 QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
514 QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
ec36b695 515#ifdef DEBUG_CAPTURE
457b6543
SW
516 sw->name = g_strdup_printf ("for %p %d,%d,%d",
517 hw, sw->info.freq, sw->info.bits,
518 sw->info.nchannels);
ec36b695
FB
519 dolog ("Added %s active = %d\n", sw->name, sw->active);
520#endif
8ead62cf 521 if (sw->active) {
ec36b695 522 audio_capture_maybe_changed (cap, 1);
8ead62cf
FB
523 }
524 }
525 return 0;
526}
527
1d14ffa9
FB
528/*
529 * Hard voice (capture)
530 */
7520462b 531static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw)
85571bc7 532{
1d14ffa9 533 SWVoiceIn *sw;
7520462b 534 size_t m = hw->total_samples_captured;
1d14ffa9
FB
535
536 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
537 if (sw->active) {
58935915 538 m = MIN (m, sw->total_hw_samples_acquired);
1d14ffa9 539 }
85571bc7 540 }
1d14ffa9 541 return m;
85571bc7
FB
542}
543
7520462b 544size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw)
85571bc7 545{
7520462b
KZ
546 size_t live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw);
547 if (audio_bug(__func__, live > hw->samples)) {
548 dolog("live=%zu hw->samples=%zu\n", live, hw->samples);
1d14ffa9 549 return 0;
85571bc7 550 }
1d14ffa9 551 return live;
85571bc7
FB
552}
553
7520462b
KZ
554size_t audio_pcm_hw_clip_out(HWVoiceOut *hw, void *pcm_buf,
555 size_t live, size_t pending)
ddabec73 556{
7520462b
KZ
557 size_t left = hw->samples - pending;
558 size_t len = MIN (left, live);
559 size_t clipped = 0;
ddabec73 560
561 while (len) {
562 struct st_sample *src = hw->mix_buf + hw->rpos;
563 uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift);
7520462b
KZ
564 size_t samples_till_end_of_buf = hw->samples - hw->rpos;
565 size_t samples_to_clip = MIN (len, samples_till_end_of_buf);
ddabec73 566
567 hw->clip (dst, src, samples_to_clip);
568
569 hw->rpos = (hw->rpos + samples_to_clip) % hw->samples;
570 len -= samples_to_clip;
571 clipped += samples_to_clip;
572 }
573 return clipped;
574}
575
ff095e52
KZ
576static void audio_pcm_hw_clip_out2(HWVoiceOut *hw, void *pcm_buf, size_t len)
577{
578 size_t clipped = 0;
579 size_t pos = hw->rpos;
580
581 while (len) {
582 st_sample *src = hw->mix_buf + pos;
583 uint8_t *dst = advance(pcm_buf, clipped << hw->info.shift);
584 size_t samples_till_end_of_buf = hw->samples - pos;
585 size_t samples_to_clip = MIN(len, samples_till_end_of_buf);
586
587 hw->clip(dst, src, samples_to_clip);
588
589 pos = (pos + samples_to_clip) % hw->samples;
590 len -= samples_to_clip;
591 clipped += samples_to_clip;
592 }
593}
594
1d14ffa9
FB
595/*
596 * Soft voice (capture)
597 */
7520462b 598static size_t audio_pcm_sw_get_rpos_in(SWVoiceIn *sw)
1d14ffa9
FB
599{
600 HWVoiceIn *hw = sw->hw;
7520462b
KZ
601 ssize_t live = hw->total_samples_captured - sw->total_hw_samples_acquired;
602 ssize_t rpos;
1d14ffa9 603
470bcabd 604 if (audio_bug(__func__, live < 0 || live > hw->samples)) {
7520462b 605 dolog("live=%zu hw->samples=%zu\n", live, hw->samples);
1d14ffa9
FB
606 return 0;
607 }
608
609 rpos = hw->wpos - live;
610 if (rpos >= 0) {
611 return rpos;
85571bc7
FB
612 }
613 else {
1d14ffa9 614 return hw->samples + rpos;
85571bc7 615 }
85571bc7
FB
616}
617
7520462b 618static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t size)
85571bc7 619{
1d14ffa9 620 HWVoiceIn *hw = sw->hw;
7520462b 621 size_t samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0;
1ea879e5 622 struct st_sample *src, *dst = sw->buf;
1d14ffa9
FB
623
624 rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples;
625
626 live = hw->total_samples_captured - sw->total_hw_samples_acquired;
7520462b
KZ
627 if (audio_bug(__func__, live > hw->samples)) {
628 dolog("live_in=%zu hw->samples=%zu\n", live, hw->samples);
1d14ffa9
FB
629 return 0;
630 }
631
632 samples = size >> sw->info.shift;
633 if (!live) {
634 return 0;
635 }
85571bc7 636
1d14ffa9 637 swlim = (live * sw->ratio) >> 32;
58935915 638 swlim = MIN (swlim, samples);
85571bc7 639
1d14ffa9
FB
640 while (swlim) {
641 src = hw->conv_buf + rpos;
7520462b
KZ
642 if (hw->wpos > rpos) {
643 isamp = hw->wpos - rpos;
644 } else {
1d14ffa9
FB
645 isamp = hw->samples - rpos;
646 }
85571bc7 647
1d14ffa9
FB
648 if (!isamp) {
649 break;
650 }
651 osamp = swlim;
85571bc7 652
1d14ffa9
FB
653 st_rate_flow (sw->rate, src, dst, &isamp, &osamp);
654 swlim -= osamp;
655 rpos = (rpos + isamp) % hw->samples;
656 dst += osamp;
657 ret += osamp;
658 total += isamp;
659 }
85571bc7 660
c01b2456
MAL
661 if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
662 mixeng_volume (sw->buf, ret, &sw->vol);
663 }
00e07679 664
571ec3d6 665 sw->clip (buf, sw->buf, ret);
1d14ffa9
FB
666 sw->total_hw_samples_acquired += total;
667 return ret << sw->info.shift;
85571bc7
FB
668}
669
1d14ffa9
FB
670/*
671 * Hard voice (playback)
672 */
7520462b 673static size_t audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep)
1d14ffa9 674{
c0fe3827 675 SWVoiceOut *sw;
7520462b 676 size_t m = SIZE_MAX;
c0fe3827 677 int nb_live = 0;
85571bc7 678
c0fe3827
FB
679 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
680 if (sw->active || !sw->empty) {
58935915 681 m = MIN (m, sw->total_hw_samples_mixed);
c0fe3827
FB
682 nb_live += 1;
683 }
85571bc7 684 }
c0fe3827
FB
685
686 *nb_livep = nb_live;
687 return m;
1d14ffa9 688}
85571bc7 689
7520462b 690static size_t audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)
1d14ffa9 691{
7520462b 692 size_t smin;
bdff253c 693 int nb_live1;
85571bc7 694
bdff253c 695 smin = audio_pcm_hw_find_min_out (hw, &nb_live1);
696 if (nb_live) {
697 *nb_live = nb_live1;
85571bc7 698 }
bdff253c 699
700 if (nb_live1) {
7520462b 701 size_t live = smin;
1d14ffa9 702
7520462b
KZ
703 if (audio_bug(__func__, live > hw->samples)) {
704 dolog("live=%zu hw->samples=%zu\n", live, hw->samples);
1d14ffa9 705 return 0;
85571bc7 706 }
1d14ffa9 707 return live;
85571bc7 708 }
bdff253c 709 return 0;
85571bc7
FB
710}
711
1d14ffa9
FB
712/*
713 * Soft voice (playback)
714 */
7520462b 715static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t size)
85571bc7 716{
7520462b
KZ
717 size_t hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck;
718 size_t ret = 0, pos = 0, total = 0;
85571bc7 719
1d14ffa9
FB
720 if (!sw) {
721 return size;
722 }
85571bc7 723
1d14ffa9 724 hwsamples = sw->hw->samples;
85571bc7 725
1d14ffa9 726 live = sw->total_hw_samples_mixed;
7520462b
KZ
727 if (audio_bug(__func__, live > hwsamples)) {
728 dolog("live=%zu hw->samples=%zu\n", live, hwsamples);
1d14ffa9
FB
729 return 0;
730 }
85571bc7 731
1d14ffa9 732 if (live == hwsamples) {
ec36b695
FB
733#ifdef DEBUG_OUT
734 dolog ("%s is full %d\n", sw->name, live);
735#endif
1d14ffa9
FB
736 return 0;
737 }
85571bc7 738
1d14ffa9
FB
739 wpos = (sw->hw->rpos + live) % hwsamples;
740 samples = size >> sw->info.shift;
85571bc7 741
1d14ffa9
FB
742 dead = hwsamples - live;
743 swlim = ((int64_t) dead << 32) / sw->ratio;
58935915 744 swlim = MIN (swlim, samples);
1d14ffa9 745 if (swlim) {
00e07679 746 sw->conv (sw->buf, buf, swlim);
c01b2456
MAL
747
748 if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
749 mixeng_volume (sw->buf, swlim, &sw->vol);
750 }
1d14ffa9
FB
751 }
752
753 while (swlim) {
754 dead = hwsamples - live;
755 left = hwsamples - wpos;
58935915 756 blck = MIN (dead, left);
1d14ffa9
FB
757 if (!blck) {
758 break;
759 }
760 isamp = swlim;
761 osamp = blck;
762 st_rate_flow_mix (
763 sw->rate,
764 sw->buf + pos,
765 sw->hw->mix_buf + wpos,
766 &isamp,
767 &osamp
768 );
769 ret += isamp;
770 swlim -= isamp;
771 pos += isamp;
772 live += osamp;
773 wpos = (wpos + osamp) % hwsamples;
774 total += osamp;
775 }
776
777 sw->total_hw_samples_mixed += total;
778 sw->empty = sw->total_hw_samples_mixed == 0;
779
780#ifdef DEBUG_OUT
781 dolog (
7520462b 782 "%s: write size %zu ret %zu total sw %zu\n",
c0fe3827 783 SW_NAME (sw),
1d14ffa9
FB
784 size >> sw->info.shift,
785 ret,
c0fe3827 786 sw->total_hw_samples_mixed
1d14ffa9
FB
787 );
788#endif
789
790 return ret << sw->info.shift;
85571bc7
FB
791}
792
1d14ffa9
FB
793#ifdef DEBUG_AUDIO
794static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info)
85571bc7 795{
1d14ffa9
FB
796 dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n",
797 cap, info->bits, info->sign, info->freq, info->nchannels);
85571bc7 798}
1d14ffa9 799#endif
85571bc7 800
1d14ffa9
FB
801#define DAC
802#include "audio_template.h"
803#undef DAC
804#include "audio_template.h"
805
713a98f8 806/*
807 * Timer
808 */
526fb058 809static int audio_is_timer_needed(AudioState *s)
713a98f8 810{
811 HWVoiceIn *hwi = NULL;
812 HWVoiceOut *hwo = NULL;
813
526fb058 814 while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
713a98f8 815 if (!hwo->poll_mode) return 1;
816 }
526fb058 817 while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
713a98f8 818 if (!hwi->poll_mode) return 1;
819 }
820 return 0;
821}
822
39deb1e4 823static void audio_reset_timer (AudioState *s)
713a98f8 824{
526fb058 825 if (audio_is_timer_needed(s)) {
1ffc2665 826 timer_mod_anticipate_ns(s->ts,
71830221 827 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks);
526fb058
KZ
828 if (!s->timer_running) {
829 s->timer_running = true;
830 s->timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
71830221 831 trace_audio_timer_start(s->period_ticks / SCALE_MS);
1a961e78
GH
832 }
833 } else {
834 timer_del(s->ts);
526fb058
KZ
835 if (s->timer_running) {
836 s->timer_running = false;
1a961e78
GH
837 trace_audio_timer_stop();
838 }
713a98f8 839 }
840}
841
39deb1e4 842static void audio_timer (void *opaque)
843{
1a961e78 844 int64_t now, diff;
71830221 845 AudioState *s = opaque;
1a961e78
GH
846
847 now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
526fb058 848 diff = now - s->timer_last;
71830221 849 if (diff > s->period_ticks * 3 / 2) {
1a961e78
GH
850 trace_audio_timer_delayed(diff / SCALE_MS);
851 }
526fb058 852 s->timer_last = now;
1a961e78 853
18e2c177 854 audio_run(s, "timer");
71830221 855 audio_reset_timer(s);
39deb1e4 856}
857
713a98f8 858/*
859 * Public API
860 */
7520462b 861size_t AUD_write(SWVoiceOut *sw, void *buf, size_t size)
85571bc7 862{
1d14ffa9
FB
863 if (!sw) {
864 /* XXX: Consider options */
865 return size;
866 }
85571bc7 867
1d14ffa9 868 if (!sw->hw->enabled) {
c0fe3827 869 dolog ("Writing to disabled voice %s\n", SW_NAME (sw));
85571bc7
FB
870 return 0;
871 }
872
1d793fec 873 return audio_pcm_sw_write(sw, buf, size);
1d14ffa9
FB
874}
875
7520462b 876size_t AUD_read(SWVoiceIn *sw, void *buf, size_t size)
1d14ffa9 877{
1d14ffa9
FB
878 if (!sw) {
879 /* XXX: Consider options */
880 return size;
85571bc7 881 }
1d14ffa9
FB
882
883 if (!sw->hw->enabled) {
c0fe3827 884 dolog ("Reading from disabled voice %s\n", SW_NAME (sw));
1d14ffa9 885 return 0;
85571bc7 886 }
1d14ffa9 887
1d793fec 888 return audio_pcm_sw_read(sw, buf, size);
85571bc7
FB
889}
890
1d14ffa9 891int AUD_get_buffer_size_out (SWVoiceOut *sw)
85571bc7 892{
c0fe3827 893 return sw->hw->samples << sw->hw->info.shift;
1d14ffa9
FB
894}
895
896void AUD_set_active_out (SWVoiceOut *sw, int on)
897{
898 HWVoiceOut *hw;
85571bc7 899
1d14ffa9 900 if (!sw) {
85571bc7 901 return;
1d14ffa9 902 }
85571bc7
FB
903
904 hw = sw->hw;
1d14ffa9 905 if (sw->active != on) {
526fb058 906 AudioState *s = sw->s;
1d14ffa9 907 SWVoiceOut *temp_sw;
ec36b695 908 SWVoiceCap *sc;
1d14ffa9
FB
909
910 if (on) {
1d14ffa9
FB
911 hw->pending_disable = 0;
912 if (!hw->enabled) {
913 hw->enabled = 1;
978dd635 914 if (s->vm_running) {
05d2f2a6 915 hw->pcm_ops->ctl_out(hw, VOICE_ENABLE);
39deb1e4 916 audio_reset_timer (s);
978dd635 917 }
1d14ffa9 918 }
1d14ffa9
FB
919 }
920 else {
921 if (hw->enabled) {
922 int nb_active = 0;
923
924 for (temp_sw = hw->sw_head.lh_first; temp_sw;
925 temp_sw = temp_sw->entries.le_next) {
926 nb_active += temp_sw->active != 0;
927 }
928
929 hw->pending_disable = nb_active == 1;
930 }
85571bc7 931 }
ec36b695
FB
932
933 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
934 sc->sw.active = hw->enabled;
8ead62cf 935 if (hw->enabled) {
ec36b695 936 audio_capture_maybe_changed (sc->cap, 1);
8ead62cf
FB
937 }
938 }
1d14ffa9
FB
939 sw->active = on;
940 }
941}
942
943void AUD_set_active_in (SWVoiceIn *sw, int on)
944{
945 HWVoiceIn *hw;
946
947 if (!sw) {
948 return;
85571bc7
FB
949 }
950
1d14ffa9 951 hw = sw->hw;
85571bc7 952 if (sw->active != on) {
526fb058 953 AudioState *s = sw->s;
1d14ffa9
FB
954 SWVoiceIn *temp_sw;
955
85571bc7 956 if (on) {
85571bc7
FB
957 if (!hw->enabled) {
958 hw->enabled = 1;
978dd635 959 if (s->vm_running) {
05d2f2a6 960 hw->pcm_ops->ctl_in(hw, VOICE_ENABLE);
39deb1e4 961 audio_reset_timer (s);
978dd635 962 }
85571bc7 963 }
1d14ffa9 964 sw->total_hw_samples_acquired = hw->total_samples_captured;
85571bc7
FB
965 }
966 else {
1d14ffa9 967 if (hw->enabled) {
85571bc7 968 int nb_active = 0;
1d14ffa9
FB
969
970 for (temp_sw = hw->sw_head.lh_first; temp_sw;
971 temp_sw = temp_sw->entries.le_next) {
972 nb_active += temp_sw->active != 0;
85571bc7
FB
973 }
974
975 if (nb_active == 1) {
1d14ffa9
FB
976 hw->enabled = 0;
977 hw->pcm_ops->ctl_in (hw, VOICE_DISABLE);
85571bc7
FB
978 }
979 }
980 }
981 sw->active = on;
982 }
983}
984
7520462b 985static size_t audio_get_avail (SWVoiceIn *sw)
1d14ffa9 986{
7520462b 987 size_t live;
1d14ffa9
FB
988
989 if (!sw) {
990 return 0;
991 }
992
993 live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired;
7520462b
KZ
994 if (audio_bug(__func__, live > sw->hw->samples)) {
995 dolog("live=%zu sw->hw->samples=%zu\n", live, sw->hw->samples);
1d14ffa9
FB
996 return 0;
997 }
998
999 ldebug (
26a76461 1000 "%s: get_avail live %d ret %" PRId64 "\n",
c0fe3827 1001 SW_NAME (sw),
1d14ffa9
FB
1002 live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift
1003 );
1004
1005 return (((int64_t) live << 32) / sw->ratio) << sw->info.shift;
1006}
1007
7520462b 1008static size_t audio_get_free(SWVoiceOut *sw)
1d14ffa9 1009{
7520462b 1010 size_t live, dead;
1d14ffa9
FB
1011
1012 if (!sw) {
1013 return 0;
1014 }
1015
1016 live = sw->total_hw_samples_mixed;
1017
7520462b
KZ
1018 if (audio_bug(__func__, live > sw->hw->samples)) {
1019 dolog("live=%zu sw->hw->samples=%zu\n", live, sw->hw->samples);
c0fe3827 1020 return 0;
1d14ffa9
FB
1021 }
1022
1023 dead = sw->hw->samples - live;
1024
1025#ifdef DEBUG_OUT
26a76461 1026 dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
c0fe3827 1027 SW_NAME (sw),
1d14ffa9 1028 live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift);
85571bc7 1029#endif
1d14ffa9
FB
1030
1031 return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift;
1032}
1033
7520462b
KZ
1034static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos,
1035 size_t samples)
8ead62cf 1036{
7520462b 1037 size_t n;
8ead62cf
FB
1038
1039 if (hw->enabled) {
ec36b695 1040 SWVoiceCap *sc;
8ead62cf 1041
ec36b695
FB
1042 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1043 SWVoiceOut *sw = &sc->sw;
8ead62cf
FB
1044 int rpos2 = rpos;
1045
1046 n = samples;
1047 while (n) {
7520462b
KZ
1048 size_t till_end_of_hw = hw->samples - rpos2;
1049 size_t to_write = MIN(till_end_of_hw, n);
1050 size_t bytes = to_write << hw->info.shift;
1051 size_t written;
8ead62cf
FB
1052
1053 sw->buf = hw->mix_buf + rpos2;
1054 written = audio_pcm_sw_write (sw, NULL, bytes);
1055 if (written - bytes) {
7520462b
KZ
1056 dolog("Could not mix %zu bytes into a capture "
1057 "buffer, mixed %zu\n",
1058 bytes, written);
8ead62cf
FB
1059 break;
1060 }
1061 n -= to_write;
1062 rpos2 = (rpos2 + to_write) % hw->samples;
1063 }
1064 }
1065 }
1066
7520462b
KZ
1067 n = MIN(samples, hw->samples - rpos);
1068 mixeng_clear(hw->mix_buf + rpos, n);
1069 mixeng_clear(hw->mix_buf, samples - n);
8ead62cf
FB
1070}
1071
ff095e52
KZ
1072static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live)
1073{
1074 size_t clipped = 0;
1075
1076 while (live) {
1077 size_t size, decr, proc;
1078 void *buf = hw->pcm_ops->get_buffer_out(hw, &size);
1079 if (!buf) {
1080 /* retrying will likely won't help, drop everything. */
1081 hw->rpos = (hw->rpos + live) % hw->samples;
1082 return clipped + live;
1083 }
1084
1085 decr = MIN(size >> hw->info.shift, live);
1086 audio_pcm_hw_clip_out2(hw, buf, decr);
1087 proc = hw->pcm_ops->put_buffer_out(hw, buf, decr << hw->info.shift) >>
1088 hw->info.shift;
1089
1090 live -= proc;
1091 clipped += proc;
1092 hw->rpos = (hw->rpos + proc) % hw->samples;
1093
1094 if (proc == 0 || proc < decr) {
1095 break;
1096 }
1097 }
1098
1099 return clipped;
1100}
1101
c0fe3827 1102static void audio_run_out (AudioState *s)
1d14ffa9
FB
1103{
1104 HWVoiceOut *hw = NULL;
1105 SWVoiceOut *sw;
1106
526fb058 1107 while ((hw = audio_pcm_hw_find_any_enabled_out(s, hw))) {
7520462b
KZ
1108 size_t played, live, prev_rpos, free;
1109 int nb_live, cleanup_required;
1d14ffa9 1110
bdff253c 1111 live = audio_pcm_hw_get_live_out (hw, &nb_live);
1d14ffa9
FB
1112 if (!nb_live) {
1113 live = 0;
1114 }
c0fe3827 1115
7520462b
KZ
1116 if (audio_bug(__func__, live > hw->samples)) {
1117 dolog ("live=%zu hw->samples=%zu\n", live, hw->samples);
c0fe3827 1118 continue;
1d14ffa9
FB
1119 }
1120
1121 if (hw->pending_disable && !nb_live) {
ec36b695 1122 SWVoiceCap *sc;
1d14ffa9
FB
1123#ifdef DEBUG_OUT
1124 dolog ("Disabling voice\n");
85571bc7 1125#endif
1d14ffa9
FB
1126 hw->enabled = 0;
1127 hw->pending_disable = 0;
1128 hw->pcm_ops->ctl_out (hw, VOICE_DISABLE);
ec36b695
FB
1129 for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1130 sc->sw.active = 0;
1131 audio_recalc_and_notify_capture (sc->cap);
8ead62cf 1132 }
1d14ffa9
FB
1133 continue;
1134 }
1135
1136 if (!live) {
1137 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1138 if (sw->active) {
1139 free = audio_get_free (sw);
1140 if (free > 0) {
1141 sw->callback.fn (sw->callback.opaque, free);
1142 }
1143 }
1144 }
1145 continue;
1146 }
1147
8ead62cf 1148 prev_rpos = hw->rpos;
ff095e52
KZ
1149 if (hw->pcm_ops->run_out) {
1150 played = hw->pcm_ops->run_out(hw, live);
1151 } else {
1152 played = audio_pcm_hw_run_out(hw, live);
1153 }
3d4d16f4 1154 replay_audio_out(&played);
470bcabd 1155 if (audio_bug(__func__, hw->rpos >= hw->samples)) {
7520462b
KZ
1156 dolog("hw->rpos=%zu hw->samples=%zu played=%zu\n",
1157 hw->rpos, hw->samples, played);
1d14ffa9
FB
1158 hw->rpos = 0;
1159 }
1160
1161#ifdef DEBUG_OUT
7520462b 1162 dolog("played=%zu\n", played);
85571bc7 1163#endif
1d14ffa9
FB
1164
1165 if (played) {
1166 hw->ts_helper += played;
8ead62cf 1167 audio_capture_mix_and_clear (hw, prev_rpos, played);
1d14ffa9
FB
1168 }
1169
c0fe3827 1170 cleanup_required = 0;
1d14ffa9 1171 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1d14ffa9
FB
1172 if (!sw->active && sw->empty) {
1173 continue;
1174 }
1175
470bcabd 1176 if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) {
7520462b
KZ
1177 dolog("played=%zu sw->total_hw_samples_mixed=%zu\n",
1178 played, sw->total_hw_samples_mixed);
1d14ffa9
FB
1179 played = sw->total_hw_samples_mixed;
1180 }
1181
1182 sw->total_hw_samples_mixed -= played;
1183
1184 if (!sw->total_hw_samples_mixed) {
1185 sw->empty = 1;
c0fe3827 1186 cleanup_required |= !sw->active && !sw->callback.fn;
1d14ffa9
FB
1187 }
1188
1189 if (sw->active) {
1190 free = audio_get_free (sw);
1191 if (free > 0) {
1192 sw->callback.fn (sw->callback.opaque, free);
1193 }
1194 }
1195 }
c0fe3827
FB
1196
1197 if (cleanup_required) {
ec36b695
FB
1198 SWVoiceOut *sw1;
1199
1200 sw = hw->sw_head.lh_first;
1201 while (sw) {
1202 sw1 = sw->entries.le_next;
c0fe3827 1203 if (!sw->active && !sw->callback.fn) {
1a7dafce 1204 audio_close_out (sw);
c0fe3827 1205 }
ec36b695 1206 sw = sw1;
c0fe3827
FB
1207 }
1208 }
1d14ffa9
FB
1209 }
1210}
1211
ff095e52
KZ
1212static size_t audio_pcm_hw_run_in(HWVoiceIn *hw, size_t samples)
1213{
1214 size_t conv = 0;
1215
1216 while (samples) {
1217 size_t proc;
1218 size_t size = samples << hw->info.shift;
1219 void *buf = hw->pcm_ops->get_buffer_in(hw, &size);
1220
1221 assert((size & hw->info.align) == 0);
1222 if (size == 0) {
1223 hw->pcm_ops->put_buffer_in(hw, buf, size);
1224 break;
1225 }
1226
1227 proc = MIN(size >> hw->info.shift,
1228 hw->samples - hw->wpos);
1229
1230 hw->conv(hw->conv_buf + hw->wpos, buf, proc);
1231 hw->wpos = (hw->wpos + proc) % hw->samples;
1232
1233 samples -= proc;
1234 conv += proc;
1235 hw->pcm_ops->put_buffer_in(hw, buf, proc << hw->info.shift);
1236 }
1237
1238 return conv;
1239}
1240
c0fe3827 1241static void audio_run_in (AudioState *s)
1d14ffa9
FB
1242{
1243 HWVoiceIn *hw = NULL;
1244
526fb058 1245 while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) {
1d14ffa9 1246 SWVoiceIn *sw;
7520462b 1247 size_t captured = 0, min;
1d14ffa9 1248
3d4d16f4 1249 if (replay_mode != REPLAY_MODE_PLAY) {
ff095e52
KZ
1250 if (hw->pcm_ops->run_in) {
1251 captured = hw->pcm_ops->run_in(hw);
1252 } else {
1253 captured = audio_pcm_hw_run_in(
1254 hw, hw->samples - audio_pcm_hw_get_live_in(hw));
1255 }
3d4d16f4
PD
1256 }
1257 replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples);
1d14ffa9
FB
1258
1259 min = audio_pcm_hw_find_min_in (hw);
1260 hw->total_samples_captured += captured - min;
1261 hw->ts_helper += captured;
1262
1263 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1264 sw->total_hw_samples_acquired -= min;
1265
1266 if (sw->active) {
7520462b 1267 size_t avail;
1d14ffa9
FB
1268
1269 avail = audio_get_avail (sw);
1270 if (avail > 0) {
1271 sw->callback.fn (sw->callback.opaque, avail);
1272 }
1273 }
1274 }
1275 }
1276}
1277
8ead62cf
FB
1278static void audio_run_capture (AudioState *s)
1279{
1280 CaptureVoiceOut *cap;
1281
1282 for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) {
7520462b 1283 size_t live, rpos, captured;
8ead62cf
FB
1284 HWVoiceOut *hw = &cap->hw;
1285 SWVoiceOut *sw;
1286
bdff253c 1287 captured = live = audio_pcm_hw_get_live_out (hw, NULL);
8ead62cf
FB
1288 rpos = hw->rpos;
1289 while (live) {
7520462b
KZ
1290 size_t left = hw->samples - rpos;
1291 size_t to_capture = MIN(live, left);
1ea879e5 1292 struct st_sample *src;
8ead62cf
FB
1293 struct capture_callback *cb;
1294
1295 src = hw->mix_buf + rpos;
1296 hw->clip (cap->buf, src, to_capture);
1297 mixeng_clear (src, to_capture);
1298
1299 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1300 cb->ops.capture (cb->opaque, cap->buf,
1301 to_capture << hw->info.shift);
1302 }
1303 rpos = (rpos + to_capture) % hw->samples;
1304 live -= to_capture;
1305 }
1306 hw->rpos = rpos;
1307
1308 for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {
1309 if (!sw->active && sw->empty) {
1310 continue;
1311 }
1312
470bcabd 1313 if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) {
7520462b
KZ
1314 dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n",
1315 captured, sw->total_hw_samples_mixed);
8ead62cf
FB
1316 captured = sw->total_hw_samples_mixed;
1317 }
1318
1319 sw->total_hw_samples_mixed -= captured;
1320 sw->empty = sw->total_hw_samples_mixed == 0;
1321 }
1322 }
1323}
1324
18e2c177 1325void audio_run(AudioState *s, const char *msg)
571ec3d6 1326{
18e2c177
KZ
1327 audio_run_out(s);
1328 audio_run_in(s);
1329 audio_run_capture(s);
571ec3d6 1330
713a98f8 1331#ifdef DEBUG_POLL
1332 {
1333 static double prevtime;
1334 double currtime;
1335 struct timeval tv;
571ec3d6 1336
713a98f8 1337 if (gettimeofday (&tv, NULL)) {
1338 perror ("audio_run: gettimeofday");
1339 return;
1340 }
1341
1342 currtime = tv.tv_sec + tv.tv_usec * 1e-6;
1343 dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime);
1344 prevtime = currtime;
1345 }
1346#endif
571ec3d6
FB
1347}
1348
ff095e52
KZ
1349void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size)
1350{
1351 ssize_t start;
1352
1353 if (unlikely(!hw->buf_emul)) {
1354 size_t calc_size = hw->samples << hw->info.shift;
1355 hw->buf_emul = g_malloc(calc_size);
1356 hw->size_emul = calc_size;
1357 hw->pos_emul = hw->pending_emul = 0;
1358 }
1359
1360 while (hw->pending_emul < hw->size_emul) {
1361 size_t read_len = MIN(hw->size_emul - hw->pos_emul,
1362 hw->size_emul - hw->pending_emul);
1363 size_t read = hw->pcm_ops->read(hw, hw->buf_emul + hw->pos_emul,
1364 read_len);
1365 hw->pending_emul += read;
1366 if (read < read_len) {
1367 break;
1368 }
1369 }
1370
1371 start = ((ssize_t) hw->pos_emul) - hw->pending_emul;
1372 if (start < 0) {
1373 start += hw->size_emul;
1374 }
1375 assert(start >= 0 && start < hw->size_emul);
1376
1377 *size = MIN(hw->pending_emul, hw->size_emul - start);
1378 return hw->buf_emul + start;
1379}
1380
1381void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)
1382{
1383 assert(size <= hw->pending_emul);
1384 hw->pending_emul -= size;
1385}
1386
1387void *audio_generic_get_buffer_out(HWVoiceOut *hw, size_t *size)
1388{
1389 if (unlikely(!hw->buf_emul)) {
1390 size_t calc_size = hw->samples << hw->info.shift;
1391
1392 hw->buf_emul = g_malloc(calc_size);
1393 hw->size_emul = calc_size;
1394 hw->pos_emul = hw->pending_emul = 0;
1395 }
1396
1397 *size = MIN(hw->size_emul - hw->pending_emul,
1398 hw->size_emul - hw->pos_emul);
1399 return hw->buf_emul + hw->pos_emul;
1400}
1401
1402size_t audio_generic_put_buffer_out_nowrite(HWVoiceOut *hw, void *buf,
1403 size_t size)
1404{
1405 assert(buf == hw->buf_emul + hw->pos_emul &&
1406 size + hw->pending_emul <= hw->size_emul);
1407
1408 hw->pending_emul += size;
1409 hw->pos_emul = (hw->pos_emul + size) % hw->size_emul;
1410
1411 return size;
1412}
1413
1414size_t audio_generic_put_buffer_out(HWVoiceOut *hw, void *buf, size_t size)
1415{
1416 audio_generic_put_buffer_out_nowrite(hw, buf, size);
1417
1418 while (hw->pending_emul) {
1419 size_t write_len, written;
1420 ssize_t start = ((ssize_t) hw->pos_emul) - hw->pending_emul;
1421 if (start < 0) {
1422 start += hw->size_emul;
1423 }
1424 assert(start >= 0 && start < hw->size_emul);
1425
1426 write_len = MIN(hw->pending_emul, hw->size_emul - start);
1427
1428 written = hw->pcm_ops->write(hw, hw->buf_emul + start, write_len);
1429 hw->pending_emul -= written;
1430
1431 if (written < write_len) {
1432 break;
1433 }
1434 }
1435
1436 /*
1437 * fake we have written everything. non-written data remain in pending_emul,
1438 * so we do not have to clip them multiple times
1439 */
1440 return size;
1441}
1442
1443size_t audio_generic_write(HWVoiceOut *hw, void *buf, size_t size)
1444{
1445 size_t dst_size, copy_size;
1446 void *dst = hw->pcm_ops->get_buffer_out(hw, &dst_size);
1447 copy_size = MIN(size, dst_size);
1448
1449 memcpy(dst, buf, copy_size);
1450 return hw->pcm_ops->put_buffer_out(hw, buf, copy_size);
1451}
1452
1453size_t audio_generic_read(HWVoiceIn *hw, void *buf, size_t size)
1454{
1455 size_t dst_size, copy_size;
1456 void *dst = hw->pcm_ops->get_buffer_in(hw, &dst_size);
1457 copy_size = MIN(size, dst_size);
1458
1459 memcpy(dst, buf, copy_size);
1460 hw->pcm_ops->put_buffer_in(hw, buf, copy_size);
1461 return copy_size;
1462}
1463
1464
71830221
KZ
1465static int audio_driver_init(AudioState *s, struct audio_driver *drv,
1466 bool msg, Audiodev *dev)
85571bc7 1467{
71830221 1468 s->drv_opaque = drv->init(dev);
1d14ffa9 1469
c0fe3827 1470 if (s->drv_opaque) {
ff095e52
KZ
1471 if (!drv->pcm_ops->get_buffer_in) {
1472 drv->pcm_ops->get_buffer_in = audio_generic_get_buffer_in;
1473 drv->pcm_ops->put_buffer_in = audio_generic_put_buffer_in;
1474 }
1475 if (!drv->pcm_ops->get_buffer_out) {
1476 drv->pcm_ops->get_buffer_out = audio_generic_get_buffer_out;
1477 drv->pcm_ops->put_buffer_out = audio_generic_put_buffer_out;
1478 }
1479
526fb058
KZ
1480 audio_init_nb_voices_out(s, drv);
1481 audio_init_nb_voices_in(s, drv);
c0fe3827 1482 s->drv = drv;
1d14ffa9 1483 return 0;
85571bc7
FB
1484 }
1485 else {
fb37ce92
GH
1486 if (msg) {
1487 dolog("Could not init `%s' audio driver\n", drv->name);
1488 }
1d14ffa9 1489 return -1;
85571bc7
FB
1490 }
1491}
1492
9781e040 1493static void audio_vm_change_state_handler (void *opaque, int running,
1dfb4dd9 1494 RunState state)
85571bc7 1495{
c0fe3827 1496 AudioState *s = opaque;
1d14ffa9
FB
1497 HWVoiceOut *hwo = NULL;
1498 HWVoiceIn *hwi = NULL;
541e0844 1499 int op = running ? VOICE_ENABLE : VOICE_DISABLE;
1d14ffa9 1500
978dd635 1501 s->vm_running = running;
526fb058 1502 while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) {
05d2f2a6 1503 hwo->pcm_ops->ctl_out(hwo, op);
1d14ffa9 1504 }
85571bc7 1505
526fb058 1506 while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) {
05d2f2a6 1507 hwi->pcm_ops->ctl_in(hwi, op);
85571bc7 1508 }
39deb1e4 1509 audio_reset_timer (s);
85571bc7
FB
1510}
1511
a384c205
MAL
1512static bool is_cleaning_up;
1513
1514bool audio_is_cleaning_up(void)
1515{
1516 return is_cleaning_up;
1517}
1518
ecd97e95 1519static void free_audio_state(AudioState *s)
85571bc7 1520{
a384c205
MAL
1521 HWVoiceOut *hwo, *hwon;
1522 HWVoiceIn *hwi, *hwin;
1d14ffa9 1523
526fb058 1524 QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) {
ec36b695 1525 SWVoiceCap *sc;
8ead62cf 1526
aeb29b64
JK
1527 if (hwo->enabled) {
1528 hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
1529 }
1d14ffa9 1530 hwo->pcm_ops->fini_out (hwo);
8ead62cf 1531
ec36b695
FB
1532 for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
1533 CaptureVoiceOut *cap = sc->cap;
1534 struct capture_callback *cb;
1535
1536 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1537 cb->ops.destroy (cb->opaque);
1538 }
8ead62cf 1539 }
a384c205 1540 QLIST_REMOVE(hwo, entries);
1d14ffa9 1541 }
85571bc7 1542
526fb058 1543 QLIST_FOREACH_SAFE(hwi, &s->hw_head_in, entries, hwin) {
aeb29b64
JK
1544 if (hwi->enabled) {
1545 hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
1546 }
1d14ffa9 1547 hwi->pcm_ops->fini_in (hwi);
a384c205 1548 QLIST_REMOVE(hwi, entries);
85571bc7 1549 }
c0fe3827
FB
1550
1551 if (s->drv) {
1552 s->drv->fini (s->drv_opaque);
a384c205 1553 s->drv = NULL;
c0fe3827 1554 }
71830221
KZ
1555
1556 if (s->dev) {
1557 qapi_free_Audiodev(s->dev);
1558 s->dev = NULL;
1559 }
e76ba19a
KZ
1560
1561 if (s->ts) {
1562 timer_free(s->ts);
1563 s->ts = NULL;
1564 }
1565
ecd97e95
KZ
1566 g_free(s);
1567}
1568
1569void audio_cleanup(void)
1570{
1571 is_cleaning_up = true;
1572 while (!QTAILQ_EMPTY(&audio_states)) {
1573 AudioState *s = QTAILQ_FIRST(&audio_states);
1574 QTAILQ_REMOVE(&audio_states, s, list);
1575 free_audio_state(s);
1576 }
85571bc7
FB
1577}
1578
d959fce9
JQ
1579static const VMStateDescription vmstate_audio = {
1580 .name = "audio",
1581 .version_id = 1,
1582 .minimum_version_id = 1,
35d08458 1583 .fields = (VMStateField[]) {
d959fce9 1584 VMSTATE_END_OF_LIST()
1d14ffa9 1585 }
d959fce9 1586};
85571bc7 1587
71830221
KZ
1588static void audio_validate_opts(Audiodev *dev, Error **errp);
1589
1590static AudiodevListEntry *audiodev_find(
1591 AudiodevListHead *head, const char *drvname)
1592{
1593 AudiodevListEntry *e;
1594 QSIMPLEQ_FOREACH(e, head, next) {
1595 if (strcmp(AudiodevDriver_str(e->dev->driver), drvname) == 0) {
1596 return e;
1597 }
1598 }
1599
1600 return NULL;
1601}
1602
ecd97e95
KZ
1603/*
1604 * if we have dev, this function was called because of an -audiodev argument =>
1605 * initialize a new state with it
1606 * if dev == NULL => legacy implicit initialization, return the already created
1607 * state or create a new one
1608 */
af2041ed 1609static AudioState *audio_init(Audiodev *dev, const char *name)
85571bc7 1610{
ecd97e95 1611 static bool atexit_registered;
1d14ffa9 1612 size_t i;
85571bc7 1613 int done = 0;
71830221 1614 const char *drvname = NULL;
713a98f8 1615 VMChangeStateEntry *e;
ecd97e95 1616 AudioState *s;
d3893a39 1617 struct audio_driver *driver;
71830221
KZ
1618 /* silence gcc warning about uninitialized variable */
1619 AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head);
1d14ffa9 1620
71830221
KZ
1621 if (dev) {
1622 /* -audiodev option */
af2041ed 1623 legacy_config = false;
71830221 1624 drvname = AudiodevDriver_str(dev->driver);
ecd97e95 1625 } else if (!QTAILQ_EMPTY(&audio_states)) {
af2041ed 1626 if (!legacy_config) {
4b3b7793
KZ
1627 dolog("Device %s: audiodev default parameter is deprecated, please "
1628 "specify audiodev=%s\n", name,
1629 QTAILQ_FIRST(&audio_states)->dev->id);
af2041ed 1630 }
ecd97e95 1631 return QTAILQ_FIRST(&audio_states);
71830221
KZ
1632 } else {
1633 /* legacy implicit initialization */
1634 head = audio_handle_legacy_opts();
1635 /*
1636 * In case of legacy initialization, all Audiodevs in the list will have
1637 * the same configuration (except the driver), so it does't matter which
1638 * one we chose. We need an Audiodev to set up AudioState before we can
1639 * init a driver. Also note that dev at this point is still in the
1640 * list.
1641 */
1642 dev = QSIMPLEQ_FIRST(&head)->dev;
1643 audio_validate_opts(dev, &error_abort);
1644 }
ecd97e95
KZ
1645
1646 s = g_malloc0(sizeof(AudioState));
71830221
KZ
1647 s->dev = dev;
1648
72cf2d4f
BS
1649 QLIST_INIT (&s->hw_head_out);
1650 QLIST_INIT (&s->hw_head_in);
1651 QLIST_INIT (&s->cap_head);
ecd97e95
KZ
1652 if (!atexit_registered) {
1653 atexit(audio_cleanup);
1654 atexit_registered = true;
1655 }
1656 QTAILQ_INSERT_TAIL(&audio_states, s, list);
571ec3d6 1657
bc72ad67 1658 s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
571ec3d6 1659
71830221
KZ
1660 s->nb_hw_voices_out = audio_get_pdo_out(dev)->voices;
1661 s->nb_hw_voices_in = audio_get_pdo_in(dev)->voices;
c0fe3827 1662
1d14ffa9 1663 if (s->nb_hw_voices_out <= 0) {
571ec3d6 1664 dolog ("Bogus number of playback voices %d, setting to 1\n",
1d14ffa9
FB
1665 s->nb_hw_voices_out);
1666 s->nb_hw_voices_out = 1;
1667 }
1668
1669 if (s->nb_hw_voices_in <= 0) {
571ec3d6 1670 dolog ("Bogus number of capture voices %d, setting to 0\n",
1d14ffa9 1671 s->nb_hw_voices_in);
571ec3d6 1672 s->nb_hw_voices_in = 0;
1d14ffa9 1673 }
85571bc7 1674
85571bc7 1675 if (drvname) {
d3893a39
GH
1676 driver = audio_driver_lookup(drvname);
1677 if (driver) {
71830221 1678 done = !audio_driver_init(s, driver, true, dev);
d3893a39 1679 } else {
85571bc7
FB
1680 dolog ("Unknown audio driver `%s'\n", drvname);
1681 }
71830221
KZ
1682 } else {
1683 for (i = 0; audio_prio_list[i]; i++) {
1684 AudiodevListEntry *e = audiodev_find(&head, audio_prio_list[i]);
d3893a39 1685 driver = audio_driver_lookup(audio_prio_list[i]);
71830221
KZ
1686
1687 if (e && driver) {
1688 s->dev = dev = e->dev;
1689 audio_validate_opts(dev, &error_abort);
1690 done = !audio_driver_init(s, driver, false, dev);
1691 if (done) {
1692 e->dev = NULL;
1693 break;
1694 }
1d14ffa9 1695 }
85571bc7
FB
1696 }
1697 }
71830221 1698 audio_free_audiodev_list(&head);
85571bc7 1699
85571bc7 1700 if (!done) {
d3893a39 1701 driver = audio_driver_lookup("none");
71830221 1702 done = !audio_driver_init(s, driver, false, dev);
7e274652
MA
1703 assert(done);
1704 dolog("warning: Using timer based audio emulation\n");
85571bc7 1705 }
1d14ffa9 1706
71830221
KZ
1707 if (dev->timer_period <= 0) {
1708 s->period_ticks = 1;
0d9acba8 1709 } else {
be1092af 1710 s->period_ticks = dev->timer_period * SCALE_US;
1d14ffa9 1711 }
0d9acba8
PB
1712
1713 e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
1714 if (!e) {
1715 dolog ("warning: Could not register change state handler\n"
1716 "(Audio can continue looping even after stopping the VM)\n");
1d14ffa9
FB
1717 }
1718
72cf2d4f 1719 QLIST_INIT (&s->card_head);
0be71e32 1720 vmstate_register (NULL, 0, &vmstate_audio, s);
ecd97e95 1721 return s;
71830221
KZ
1722}
1723
1724void audio_free_audiodev_list(AudiodevListHead *head)
1725{
1726 AudiodevListEntry *e;
1727 while ((e = QSIMPLEQ_FIRST(head))) {
1728 QSIMPLEQ_REMOVE_HEAD(head, next);
1729 qapi_free_Audiodev(e->dev);
1730 g_free(e);
1731 }
85571bc7 1732}
8ead62cf 1733
1a7dafce 1734void AUD_register_card (const char *name, QEMUSoundCard *card)
1735{
ecd97e95 1736 if (!card->state) {
af2041ed 1737 card->state = audio_init(NULL, name);
ecd97e95
KZ
1738 }
1739
7267c094 1740 card->name = g_strdup (name);
1a7dafce 1741 memset (&card->entries, 0, sizeof (card->entries));
ecd97e95 1742 QLIST_INSERT_HEAD(&card->state->card_head, card, entries);
1a7dafce 1743}
1744
1745void AUD_remove_card (QEMUSoundCard *card)
1746{
72cf2d4f 1747 QLIST_REMOVE (card, entries);
7267c094 1748 g_free (card->name);
1a7dafce 1749}
1750
1751
ecd97e95
KZ
1752CaptureVoiceOut *AUD_add_capture(
1753 AudioState *s,
1ea879e5 1754 struct audsettings *as,
8ead62cf
FB
1755 struct audio_capture_ops *ops,
1756 void *cb_opaque
1757 )
1758{
1759 CaptureVoiceOut *cap;
1760 struct capture_callback *cb;
1761
ecd97e95 1762 if (!s) {
af2041ed 1763 if (!legacy_config) {
4b3b7793 1764 dolog("Capturing without setting an audiodev is deprecated\n");
af2041ed
KZ
1765 }
1766 s = audio_init(NULL, NULL);
ecd97e95
KZ
1767 }
1768
ec36b695 1769 if (audio_validate_settings (as)) {
8ead62cf
FB
1770 dolog ("Invalid settings were passed when trying to add capture\n");
1771 audio_print_settings (as);
e8d85444 1772 return NULL;
8ead62cf
FB
1773 }
1774
e8d85444 1775 cb = g_malloc0(sizeof(*cb));
8ead62cf
FB
1776 cb->ops = *ops;
1777 cb->opaque = cb_opaque;
1778
526fb058 1779 cap = audio_pcm_capture_find_specific(s, as);
8ead62cf 1780 if (cap) {
72cf2d4f 1781 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
ec36b695 1782 return cap;
8ead62cf
FB
1783 }
1784 else {
1785 HWVoiceOut *hw;
1786 CaptureVoiceOut *cap;
1787
e8d85444 1788 cap = g_malloc0(sizeof(*cap));
8ead62cf
FB
1789
1790 hw = &cap->hw;
526fb058 1791 hw->s = s;
72cf2d4f
BS
1792 QLIST_INIT (&hw->sw_head);
1793 QLIST_INIT (&cap->cb_head);
8ead62cf
FB
1794
1795 /* XXX find a more elegant way */
1796 hw->samples = 4096 * 4;
e8d85444 1797 hw->mix_buf = g_new0(struct st_sample, hw->samples);
8ead62cf 1798
d929eba5 1799 audio_pcm_init_info (&hw->info, as);
8ead62cf 1800
e8d85444 1801 cap->buf = g_malloc0_n(hw->samples, 1 << hw->info.shift);
8ead62cf
FB
1802
1803 hw->clip = mixeng_clip
1804 [hw->info.nchannels == 2]
1805 [hw->info.sign]
d929eba5 1806 [hw->info.swap_endianness]
f941aa25 1807 [audio_bits_to_index (hw->info.bits)];
8ead62cf 1808
72cf2d4f
BS
1809 QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
1810 QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
8ead62cf 1811
526fb058 1812 QLIST_FOREACH(hw, &s->hw_head_out, entries) {
1a7dafce 1813 audio_attach_capture (hw);
8ead62cf 1814 }
ec36b695 1815 return cap;
ec36b695
FB
1816 }
1817}
1818
1819void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
1820{
1821 struct capture_callback *cb;
1822
1823 for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) {
1824 if (cb->opaque == cb_opaque) {
1825 cb->ops.destroy (cb_opaque);
72cf2d4f 1826 QLIST_REMOVE (cb, entries);
7267c094 1827 g_free (cb);
ec36b695
FB
1828
1829 if (!cap->cb_head.lh_first) {
1830 SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
a3c25997 1831
ec36b695 1832 while (sw) {
a3c25997 1833 SWVoiceCap *sc = (SWVoiceCap *) sw;
ec36b695
FB
1834#ifdef DEBUG_CAPTURE
1835 dolog ("freeing %s\n", sw->name);
1836#endif
a3c25997 1837
ec36b695
FB
1838 sw1 = sw->entries.le_next;
1839 if (sw->rate) {
1840 st_rate_stop (sw->rate);
1841 sw->rate = NULL;
1842 }
72cf2d4f
BS
1843 QLIST_REMOVE (sw, entries);
1844 QLIST_REMOVE (sc, entries);
7267c094 1845 g_free (sc);
ec36b695
FB
1846 sw = sw1;
1847 }
72cf2d4f 1848 QLIST_REMOVE (cap, entries);
3268a845
GH
1849 g_free (cap->hw.mix_buf);
1850 g_free (cap->buf);
7267c094 1851 g_free (cap);
ec36b695
FB
1852 }
1853 return;
1854 }
8ead62cf
FB
1855 }
1856}
683efdcb
AZ
1857
1858void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
1859{
1860 if (sw) {
6c95ab94
MAL
1861 HWVoiceOut *hw = sw->hw;
1862
683efdcb
AZ
1863 sw->vol.mute = mute;
1864 sw->vol.l = nominal_volume.l * lvol / 255;
1865 sw->vol.r = nominal_volume.r * rvol / 255;
6c95ab94
MAL
1866
1867 if (hw->pcm_ops->ctl_out) {
1868 hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
1869 }
683efdcb
AZ
1870 }
1871}
1872
1873void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
1874{
1875 if (sw) {
6c95ab94
MAL
1876 HWVoiceIn *hw = sw->hw;
1877
683efdcb
AZ
1878 sw->vol.mute = mute;
1879 sw->vol.l = nominal_volume.l * lvol / 255;
1880 sw->vol.r = nominal_volume.r * rvol / 255;
6c95ab94
MAL
1881
1882 if (hw->pcm_ops->ctl_in) {
1883 hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);
1884 }
683efdcb
AZ
1885 }
1886}
71830221
KZ
1887
1888void audio_create_pdos(Audiodev *dev)
1889{
1890 switch (dev->driver) {
1891#define CASE(DRIVER, driver, pdo_name) \
1892 case AUDIODEV_DRIVER_##DRIVER: \
1893 if (!dev->u.driver.has_in) { \
1894 dev->u.driver.in = g_malloc0( \
1895 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
1896 dev->u.driver.has_in = true; \
1897 } \
1898 if (!dev->u.driver.has_out) { \
1899 dev->u.driver.out = g_malloc0( \
725662d6 1900 sizeof(Audiodev##pdo_name##PerDirectionOptions)); \
71830221
KZ
1901 dev->u.driver.has_out = true; \
1902 } \
1903 break
1904
1905 CASE(NONE, none, );
1906 CASE(ALSA, alsa, Alsa);
1907 CASE(COREAUDIO, coreaudio, Coreaudio);
1908 CASE(DSOUND, dsound, );
1909 CASE(OSS, oss, Oss);
1910 CASE(PA, pa, Pa);
1911 CASE(SDL, sdl, );
1912 CASE(SPICE, spice, );
1913 CASE(WAV, wav, );
1914
1915 case AUDIODEV_DRIVER__MAX:
1916 abort();
1917 };
1918}
1919
1920static void audio_validate_per_direction_opts(
1921 AudiodevPerDirectionOptions *pdo, Error **errp)
1922{
1923 if (!pdo->has_fixed_settings) {
1924 pdo->has_fixed_settings = true;
1925 pdo->fixed_settings = true;
1926 }
1927 if (!pdo->fixed_settings &&
1928 (pdo->has_frequency || pdo->has_channels || pdo->has_format)) {
1929 error_setg(errp,
1930 "You can't use frequency, channels or format with fixed-settings=off");
1931 return;
1932 }
1933
1934 if (!pdo->has_frequency) {
1935 pdo->has_frequency = true;
1936 pdo->frequency = 44100;
1937 }
1938 if (!pdo->has_channels) {
1939 pdo->has_channels = true;
1940 pdo->channels = 2;
1941 }
1942 if (!pdo->has_voices) {
1943 pdo->has_voices = true;
1944 pdo->voices = 1;
1945 }
1946 if (!pdo->has_format) {
1947 pdo->has_format = true;
1948 pdo->format = AUDIO_FORMAT_S16;
1949 }
1950}
1951
1952static void audio_validate_opts(Audiodev *dev, Error **errp)
1953{
1954 Error *err = NULL;
1955
1956 audio_create_pdos(dev);
1957
1958 audio_validate_per_direction_opts(audio_get_pdo_in(dev), &err);
1959 if (err) {
1960 error_propagate(errp, err);
1961 return;
1962 }
1963
1964 audio_validate_per_direction_opts(audio_get_pdo_out(dev), &err);
1965 if (err) {
1966 error_propagate(errp, err);
1967 return;
1968 }
1969
1970 if (!dev->has_timer_period) {
1971 dev->has_timer_period = true;
1972 dev->timer_period = 10000; /* 100Hz -> 10ms */
1973 }
1974}
1975
1976void audio_parse_option(const char *opt)
1977{
1978 AudiodevListEntry *e;
1979 Audiodev *dev = NULL;
1980
1981 Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal);
1982 visit_type_Audiodev(v, NULL, &dev, &error_fatal);
1983 visit_free(v);
1984
1985 audio_validate_opts(dev, &error_fatal);
1986
1987 e = g_malloc0(sizeof(AudiodevListEntry));
1988 e->dev = dev;
1989 QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next);
1990}
1991
1992void audio_init_audiodevs(void)
1993{
1994 AudiodevListEntry *e;
1995
1996 QSIMPLEQ_FOREACH(e, &audiodevs, next) {
af2041ed 1997 audio_init(e->dev, NULL);
71830221
KZ
1998 }
1999}
2000
2001audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo)
2002{
2003 return (audsettings) {
2004 .freq = pdo->frequency,
2005 .nchannels = pdo->channels,
2006 .fmt = pdo->format,
2007 .endianness = AUDIO_HOST_ENDIANNESS,
2008 };
2009}
2010
2011int audioformat_bytes_per_sample(AudioFormat fmt)
2012{
2013 switch (fmt) {
2014 case AUDIO_FORMAT_U8:
2015 case AUDIO_FORMAT_S8:
2016 return 1;
2017
2018 case AUDIO_FORMAT_U16:
2019 case AUDIO_FORMAT_S16:
2020 return 2;
2021
2022 case AUDIO_FORMAT_U32:
2023 case AUDIO_FORMAT_S32:
2024 return 4;
2025
2026 case AUDIO_FORMAT__MAX:
2027 ;
2028 }
2029 abort();
2030}
2031
2032
2033/* frames = freq * usec / 1e6 */
2034int audio_buffer_frames(AudiodevPerDirectionOptions *pdo,
2035 audsettings *as, int def_usecs)
2036{
2037 uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs;
2038 return (as->freq * usecs + 500000) / 1000000;
2039}
2040
2041/* samples = channels * frames = channels * freq * usec / 1e6 */
2042int audio_buffer_samples(AudiodevPerDirectionOptions *pdo,
2043 audsettings *as, int def_usecs)
2044{
2045 return as->nchannels * audio_buffer_frames(pdo, as, def_usecs);
2046}
2047
2048/*
2049 * bytes = bytes_per_sample * samples =
2050 * bytes_per_sample * channels * freq * usec / 1e6
2051 */
2052int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo,
2053 audsettings *as, int def_usecs)
2054{
2055 return audio_buffer_samples(pdo, as, def_usecs) *
2056 audioformat_bytes_per_sample(as->fmt);
2057}
ecd97e95
KZ
2058
2059AudioState *audio_state_by_name(const char *name)
2060{
2061 AudioState *s;
2062 QTAILQ_FOREACH(s, &audio_states, list) {
2063 assert(s->dev);
2064 if (strcmp(name, s->dev->id) == 0) {
2065 return s;
2066 }
2067 }
2068 return NULL;
2069}
2070
2071const char *audio_get_id(QEMUSoundCard *card)
2072{
2073 if (card->state) {
2074 assert(card->state->dev);
2075 return card->state->dev->id;
2076 } else {
2077 return "";
2078 }
2079}