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