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