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