]>
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 | ||
245 | invalid = as->nchannels != 1 && as->nchannels != 2; | |
d929eba5 | 246 | invalid |= as->endianness != 0 && as->endianness != 1; |
c0fe3827 FB |
247 | |
248 | switch (as->fmt) { | |
85bc5852 KZ |
249 | case AUDIO_FORMAT_S8: |
250 | case AUDIO_FORMAT_U8: | |
251 | case AUDIO_FORMAT_S16: | |
252 | case AUDIO_FORMAT_U16: | |
253 | case AUDIO_FORMAT_S32: | |
254 | case AUDIO_FORMAT_U32: | |
c0fe3827 FB |
255 | break; |
256 | default: | |
257 | invalid = 1; | |
258 | break; | |
259 | } | |
260 | ||
261 | invalid |= as->freq <= 0; | |
d929eba5 | 262 | return invalid ? -1 : 0; |
c0fe3827 FB |
263 | } |
264 | ||
1ea879e5 | 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 | { |
f941aa25 | 302 | int bits = 8, sign = 0, shift = 0; |
1d14ffa9 | 303 | |
c0fe3827 | 304 | switch (as->fmt) { |
85bc5852 | 305 | case AUDIO_FORMAT_S8: |
85571bc7 | 306 | sign = 1; |
85bc5852 | 307 | case AUDIO_FORMAT_U8: |
85571bc7 FB |
308 | break; |
309 | ||
85bc5852 | 310 | case AUDIO_FORMAT_S16: |
85571bc7 | 311 | sign = 1; |
e4634941 | 312 | /* fall through */ |
85bc5852 | 313 | case AUDIO_FORMAT_U16: |
85571bc7 | 314 | bits = 16; |
f941aa25 TS |
315 | shift = 1; |
316 | break; | |
317 | ||
85bc5852 | 318 | case AUDIO_FORMAT_S32: |
f941aa25 | 319 | sign = 1; |
e4634941 | 320 | /* fall through */ |
85bc5852 | 321 | case AUDIO_FORMAT_U32: |
f941aa25 TS |
322 | bits = 32; |
323 | shift = 2; | |
85571bc7 | 324 | break; |
85bc5852 KZ |
325 | |
326 | default: | |
327 | abort(); | |
85571bc7 FB |
328 | } |
329 | ||
c0fe3827 | 330 | info->freq = as->freq; |
1d14ffa9 FB |
331 | info->bits = bits; |
332 | info->sign = sign; | |
c0fe3827 | 333 | info->nchannels = as->nchannels; |
f941aa25 | 334 | info->shift = (as->nchannels == 2) + shift; |
1d14ffa9 FB |
335 | info->align = (1 << info->shift) - 1; |
336 | info->bytes_per_second = info->freq << info->shift; | |
d929eba5 | 337 | info->swap_endianness = (as->endianness != AUDIO_HOST_ENDIANNESS); |
85571bc7 FB |
338 | } |
339 | ||
1d14ffa9 | 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) { | |
e2f909be | 347 | memset (buf, 0x00, len << info->shift); |
85571bc7 FB |
348 | } |
349 | else { | |
f941aa25 TS |
350 | switch (info->bits) { |
351 | case 8: | |
e2f909be | 352 | memset (buf, 0x80, len << info->shift); |
f941aa25 | 353 | break; |
1d14ffa9 | 354 | |
f941aa25 TS |
355 | case 16: |
356 | { | |
357 | int i; | |
358 | uint16_t *p = buf; | |
359 | int shift = info->nchannels - 1; | |
360 | short s = INT16_MAX; | |
361 | ||
362 | if (info->swap_endianness) { | |
363 | s = bswap16 (s); | |
364 | } | |
365 | ||
366 | for (i = 0; i < len << shift; i++) { | |
367 | p[i] = s; | |
368 | } | |
1d14ffa9 | 369 | } |
f941aa25 TS |
370 | break; |
371 | ||
372 | case 32: | |
373 | { | |
374 | int i; | |
375 | uint32_t *p = buf; | |
376 | int shift = info->nchannels - 1; | |
377 | int32_t s = INT32_MAX; | |
378 | ||
379 | if (info->swap_endianness) { | |
380 | s = bswap32 (s); | |
381 | } | |
1d14ffa9 | 382 | |
f941aa25 TS |
383 | for (i = 0; i < len << shift; i++) { |
384 | p[i] = s; | |
385 | } | |
1d14ffa9 | 386 | } |
f941aa25 TS |
387 | break; |
388 | ||
389 | default: | |
390 | AUD_log (NULL, "audio_pcm_info_clear_buf: invalid bits %d\n", | |
391 | info->bits); | |
392 | break; | |
1d14ffa9 | 393 | } |
85571bc7 FB |
394 | } |
395 | } | |
396 | ||
8ead62cf FB |
397 | /* |
398 | * Capture | |
399 | */ | |
00e07679 | 400 | static void noop_conv (struct st_sample *dst, const void *src, int samples) |
8ead62cf FB |
401 | { |
402 | (void) src; | |
403 | (void) dst; | |
404 | (void) samples; | |
8ead62cf FB |
405 | } |
406 | ||
526fb058 KZ |
407 | static CaptureVoiceOut *audio_pcm_capture_find_specific(AudioState *s, |
408 | struct audsettings *as) | |
8ead62cf FB |
409 | { |
410 | CaptureVoiceOut *cap; | |
8ead62cf FB |
411 | |
412 | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) { | |
d929eba5 | 413 | if (audio_pcm_info_eq (&cap->hw.info, as)) { |
8ead62cf FB |
414 | return cap; |
415 | } | |
416 | } | |
417 | return NULL; | |
418 | } | |
419 | ||
ec36b695 | 420 | static void audio_notify_capture (CaptureVoiceOut *cap, audcnotification_e cmd) |
8ead62cf | 421 | { |
ec36b695 FB |
422 | struct capture_callback *cb; |
423 | ||
424 | #ifdef DEBUG_CAPTURE | |
425 | dolog ("notification %d sent\n", cmd); | |
426 | #endif | |
427 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
428 | cb->ops.notify (cb->opaque, cmd); | |
429 | } | |
430 | } | |
8ead62cf | 431 | |
ec36b695 FB |
432 | static void audio_capture_maybe_changed (CaptureVoiceOut *cap, int enabled) |
433 | { | |
434 | if (cap->hw.enabled != enabled) { | |
435 | audcnotification_e cmd; | |
8ead62cf | 436 | cap->hw.enabled = enabled; |
ec36b695 FB |
437 | cmd = enabled ? AUD_CNOTIFY_ENABLE : AUD_CNOTIFY_DISABLE; |
438 | audio_notify_capture (cap, cmd); | |
8ead62cf FB |
439 | } |
440 | } | |
441 | ||
442 | static void audio_recalc_and_notify_capture (CaptureVoiceOut *cap) | |
443 | { | |
444 | HWVoiceOut *hw = &cap->hw; | |
445 | SWVoiceOut *sw; | |
446 | int enabled = 0; | |
447 | ||
ec36b695 | 448 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { |
8ead62cf FB |
449 | if (sw->active) { |
450 | enabled = 1; | |
451 | break; | |
452 | } | |
453 | } | |
ec36b695 | 454 | audio_capture_maybe_changed (cap, enabled); |
8ead62cf FB |
455 | } |
456 | ||
457 | static void audio_detach_capture (HWVoiceOut *hw) | |
458 | { | |
ec36b695 FB |
459 | SWVoiceCap *sc = hw->cap_head.lh_first; |
460 | ||
461 | while (sc) { | |
462 | SWVoiceCap *sc1 = sc->entries.le_next; | |
463 | SWVoiceOut *sw = &sc->sw; | |
464 | CaptureVoiceOut *cap = sc->cap; | |
465 | int was_active = sw->active; | |
8ead62cf | 466 | |
8ead62cf FB |
467 | if (sw->rate) { |
468 | st_rate_stop (sw->rate); | |
469 | sw->rate = NULL; | |
470 | } | |
471 | ||
72cf2d4f BS |
472 | QLIST_REMOVE (sw, entries); |
473 | QLIST_REMOVE (sc, entries); | |
7267c094 | 474 | g_free (sc); |
ec36b695 FB |
475 | if (was_active) { |
476 | /* We have removed soft voice from the capture: | |
477 | this might have changed the overall status of the capture | |
478 | since this might have been the only active voice */ | |
479 | audio_recalc_and_notify_capture (cap); | |
480 | } | |
481 | sc = sc1; | |
8ead62cf FB |
482 | } |
483 | } | |
484 | ||
1a7dafce | 485 | static int audio_attach_capture (HWVoiceOut *hw) |
8ead62cf | 486 | { |
526fb058 | 487 | AudioState *s = hw->s; |
8ead62cf FB |
488 | CaptureVoiceOut *cap; |
489 | ||
490 | audio_detach_capture (hw); | |
491 | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) { | |
ec36b695 | 492 | SWVoiceCap *sc; |
8ead62cf | 493 | SWVoiceOut *sw; |
ec36b695 | 494 | HWVoiceOut *hw_cap = &cap->hw; |
8ead62cf | 495 | |
e8d85444 | 496 | sc = g_malloc0(sizeof(*sc)); |
8ead62cf | 497 | |
ec36b695 FB |
498 | sc->cap = cap; |
499 | sw = &sc->sw; | |
8ead62cf | 500 | sw->hw = hw_cap; |
ec36b695 | 501 | sw->info = hw->info; |
8ead62cf FB |
502 | sw->empty = 1; |
503 | sw->active = hw->enabled; | |
504 | sw->conv = noop_conv; | |
505 | sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq; | |
83617103 | 506 | sw->vol = nominal_volume; |
8ead62cf FB |
507 | sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq); |
508 | if (!sw->rate) { | |
509 | dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw)); | |
7267c094 | 510 | g_free (sw); |
8ead62cf FB |
511 | return -1; |
512 | } | |
72cf2d4f BS |
513 | QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries); |
514 | QLIST_INSERT_HEAD (&hw->cap_head, sc, entries); | |
ec36b695 | 515 | #ifdef DEBUG_CAPTURE |
457b6543 SW |
516 | sw->name = g_strdup_printf ("for %p %d,%d,%d", |
517 | hw, sw->info.freq, sw->info.bits, | |
518 | sw->info.nchannels); | |
ec36b695 FB |
519 | dolog ("Added %s active = %d\n", sw->name, sw->active); |
520 | #endif | |
8ead62cf | 521 | if (sw->active) { |
ec36b695 | 522 | audio_capture_maybe_changed (cap, 1); |
8ead62cf FB |
523 | } |
524 | } | |
525 | return 0; | |
526 | } | |
527 | ||
1d14ffa9 FB |
528 | /* |
529 | * Hard voice (capture) | |
530 | */ | |
7520462b | 531 | static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw) |
85571bc7 | 532 | { |
1d14ffa9 | 533 | SWVoiceIn *sw; |
7520462b | 534 | size_t m = hw->total_samples_captured; |
1d14ffa9 FB |
535 | |
536 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
537 | if (sw->active) { | |
58935915 | 538 | m = MIN (m, sw->total_hw_samples_acquired); |
1d14ffa9 | 539 | } |
85571bc7 | 540 | } |
1d14ffa9 | 541 | return m; |
85571bc7 FB |
542 | } |
543 | ||
7520462b | 544 | size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw) |
85571bc7 | 545 | { |
7520462b KZ |
546 | size_t live = hw->total_samples_captured - audio_pcm_hw_find_min_in (hw); |
547 | if (audio_bug(__func__, live > hw->samples)) { | |
548 | dolog("live=%zu hw->samples=%zu\n", live, hw->samples); | |
1d14ffa9 | 549 | return 0; |
85571bc7 | 550 | } |
1d14ffa9 | 551 | return live; |
85571bc7 FB |
552 | } |
553 | ||
7520462b KZ |
554 | size_t audio_pcm_hw_clip_out(HWVoiceOut *hw, void *pcm_buf, |
555 | size_t live, size_t pending) | |
ddabec73 | 556 | { |
7520462b KZ |
557 | size_t left = hw->samples - pending; |
558 | size_t len = MIN (left, live); | |
559 | size_t clipped = 0; | |
ddabec73 | 560 | |
561 | while (len) { | |
562 | struct st_sample *src = hw->mix_buf + hw->rpos; | |
563 | uint8_t *dst = advance (pcm_buf, hw->rpos << hw->info.shift); | |
7520462b KZ |
564 | size_t samples_till_end_of_buf = hw->samples - hw->rpos; |
565 | size_t samples_to_clip = MIN (len, samples_till_end_of_buf); | |
ddabec73 | 566 | |
567 | hw->clip (dst, src, samples_to_clip); | |
568 | ||
569 | hw->rpos = (hw->rpos + samples_to_clip) % hw->samples; | |
570 | len -= samples_to_clip; | |
571 | clipped += samples_to_clip; | |
572 | } | |
573 | return clipped; | |
574 | } | |
575 | ||
1d14ffa9 FB |
576 | /* |
577 | * Soft voice (capture) | |
578 | */ | |
7520462b | 579 | static size_t audio_pcm_sw_get_rpos_in(SWVoiceIn *sw) |
1d14ffa9 FB |
580 | { |
581 | HWVoiceIn *hw = sw->hw; | |
7520462b KZ |
582 | ssize_t live = hw->total_samples_captured - sw->total_hw_samples_acquired; |
583 | ssize_t rpos; | |
1d14ffa9 | 584 | |
470bcabd | 585 | if (audio_bug(__func__, live < 0 || live > hw->samples)) { |
7520462b | 586 | dolog("live=%zu hw->samples=%zu\n", live, hw->samples); |
1d14ffa9 FB |
587 | return 0; |
588 | } | |
589 | ||
590 | rpos = hw->wpos - live; | |
591 | if (rpos >= 0) { | |
592 | return rpos; | |
85571bc7 FB |
593 | } |
594 | else { | |
1d14ffa9 | 595 | return hw->samples + rpos; |
85571bc7 | 596 | } |
85571bc7 FB |
597 | } |
598 | ||
7520462b | 599 | static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t size) |
85571bc7 | 600 | { |
1d14ffa9 | 601 | HWVoiceIn *hw = sw->hw; |
7520462b | 602 | size_t samples, live, ret = 0, swlim, isamp, osamp, rpos, total = 0; |
1ea879e5 | 603 | struct st_sample *src, *dst = sw->buf; |
1d14ffa9 FB |
604 | |
605 | rpos = audio_pcm_sw_get_rpos_in (sw) % hw->samples; | |
606 | ||
607 | live = hw->total_samples_captured - sw->total_hw_samples_acquired; | |
7520462b KZ |
608 | if (audio_bug(__func__, live > hw->samples)) { |
609 | dolog("live_in=%zu hw->samples=%zu\n", live, hw->samples); | |
1d14ffa9 FB |
610 | return 0; |
611 | } | |
612 | ||
613 | samples = size >> sw->info.shift; | |
614 | if (!live) { | |
615 | return 0; | |
616 | } | |
85571bc7 | 617 | |
1d14ffa9 | 618 | swlim = (live * sw->ratio) >> 32; |
58935915 | 619 | swlim = MIN (swlim, samples); |
85571bc7 | 620 | |
1d14ffa9 FB |
621 | while (swlim) { |
622 | src = hw->conv_buf + rpos; | |
7520462b KZ |
623 | if (hw->wpos > rpos) { |
624 | isamp = hw->wpos - rpos; | |
625 | } else { | |
1d14ffa9 FB |
626 | isamp = hw->samples - rpos; |
627 | } | |
85571bc7 | 628 | |
1d14ffa9 FB |
629 | if (!isamp) { |
630 | break; | |
631 | } | |
632 | osamp = swlim; | |
85571bc7 | 633 | |
1d14ffa9 FB |
634 | st_rate_flow (sw->rate, src, dst, &isamp, &osamp); |
635 | swlim -= osamp; | |
636 | rpos = (rpos + isamp) % hw->samples; | |
637 | dst += osamp; | |
638 | ret += osamp; | |
639 | total += isamp; | |
640 | } | |
85571bc7 | 641 | |
c01b2456 MAL |
642 | if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) { |
643 | mixeng_volume (sw->buf, ret, &sw->vol); | |
644 | } | |
00e07679 | 645 | |
571ec3d6 | 646 | sw->clip (buf, sw->buf, ret); |
1d14ffa9 FB |
647 | sw->total_hw_samples_acquired += total; |
648 | return ret << sw->info.shift; | |
85571bc7 FB |
649 | } |
650 | ||
1d14ffa9 FB |
651 | /* |
652 | * Hard voice (playback) | |
653 | */ | |
7520462b | 654 | static size_t audio_pcm_hw_find_min_out (HWVoiceOut *hw, int *nb_livep) |
1d14ffa9 | 655 | { |
c0fe3827 | 656 | SWVoiceOut *sw; |
7520462b | 657 | size_t m = SIZE_MAX; |
c0fe3827 | 658 | int nb_live = 0; |
85571bc7 | 659 | |
c0fe3827 FB |
660 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { |
661 | if (sw->active || !sw->empty) { | |
58935915 | 662 | m = MIN (m, sw->total_hw_samples_mixed); |
c0fe3827 FB |
663 | nb_live += 1; |
664 | } | |
85571bc7 | 665 | } |
c0fe3827 FB |
666 | |
667 | *nb_livep = nb_live; | |
668 | return m; | |
1d14ffa9 | 669 | } |
85571bc7 | 670 | |
7520462b | 671 | static size_t audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live) |
1d14ffa9 | 672 | { |
7520462b | 673 | size_t smin; |
bdff253c | 674 | int nb_live1; |
85571bc7 | 675 | |
bdff253c | 676 | smin = audio_pcm_hw_find_min_out (hw, &nb_live1); |
677 | if (nb_live) { | |
678 | *nb_live = nb_live1; | |
85571bc7 | 679 | } |
bdff253c | 680 | |
681 | if (nb_live1) { | |
7520462b | 682 | size_t live = smin; |
1d14ffa9 | 683 | |
7520462b KZ |
684 | if (audio_bug(__func__, live > hw->samples)) { |
685 | dolog("live=%zu hw->samples=%zu\n", live, hw->samples); | |
1d14ffa9 | 686 | return 0; |
85571bc7 | 687 | } |
1d14ffa9 | 688 | return live; |
85571bc7 | 689 | } |
bdff253c | 690 | return 0; |
85571bc7 FB |
691 | } |
692 | ||
1d14ffa9 FB |
693 | /* |
694 | * Soft voice (playback) | |
695 | */ | |
7520462b | 696 | static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t size) |
85571bc7 | 697 | { |
7520462b KZ |
698 | size_t hwsamples, samples, isamp, osamp, wpos, live, dead, left, swlim, blck; |
699 | size_t ret = 0, pos = 0, total = 0; | |
85571bc7 | 700 | |
1d14ffa9 FB |
701 | if (!sw) { |
702 | return size; | |
703 | } | |
85571bc7 | 704 | |
1d14ffa9 | 705 | hwsamples = sw->hw->samples; |
85571bc7 | 706 | |
1d14ffa9 | 707 | live = sw->total_hw_samples_mixed; |
7520462b KZ |
708 | if (audio_bug(__func__, live > hwsamples)) { |
709 | dolog("live=%zu hw->samples=%zu\n", live, hwsamples); | |
1d14ffa9 FB |
710 | return 0; |
711 | } | |
85571bc7 | 712 | |
1d14ffa9 | 713 | if (live == hwsamples) { |
ec36b695 FB |
714 | #ifdef DEBUG_OUT |
715 | dolog ("%s is full %d\n", sw->name, live); | |
716 | #endif | |
1d14ffa9 FB |
717 | return 0; |
718 | } | |
85571bc7 | 719 | |
1d14ffa9 FB |
720 | wpos = (sw->hw->rpos + live) % hwsamples; |
721 | samples = size >> sw->info.shift; | |
85571bc7 | 722 | |
1d14ffa9 FB |
723 | dead = hwsamples - live; |
724 | swlim = ((int64_t) dead << 32) / sw->ratio; | |
58935915 | 725 | swlim = MIN (swlim, samples); |
1d14ffa9 | 726 | if (swlim) { |
00e07679 | 727 | sw->conv (sw->buf, buf, swlim); |
c01b2456 MAL |
728 | |
729 | if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) { | |
730 | mixeng_volume (sw->buf, swlim, &sw->vol); | |
731 | } | |
1d14ffa9 FB |
732 | } |
733 | ||
734 | while (swlim) { | |
735 | dead = hwsamples - live; | |
736 | left = hwsamples - wpos; | |
58935915 | 737 | blck = MIN (dead, left); |
1d14ffa9 FB |
738 | if (!blck) { |
739 | break; | |
740 | } | |
741 | isamp = swlim; | |
742 | osamp = blck; | |
743 | st_rate_flow_mix ( | |
744 | sw->rate, | |
745 | sw->buf + pos, | |
746 | sw->hw->mix_buf + wpos, | |
747 | &isamp, | |
748 | &osamp | |
749 | ); | |
750 | ret += isamp; | |
751 | swlim -= isamp; | |
752 | pos += isamp; | |
753 | live += osamp; | |
754 | wpos = (wpos + osamp) % hwsamples; | |
755 | total += osamp; | |
756 | } | |
757 | ||
758 | sw->total_hw_samples_mixed += total; | |
759 | sw->empty = sw->total_hw_samples_mixed == 0; | |
760 | ||
761 | #ifdef DEBUG_OUT | |
762 | dolog ( | |
7520462b | 763 | "%s: write size %zu ret %zu total sw %zu\n", |
c0fe3827 | 764 | SW_NAME (sw), |
1d14ffa9 FB |
765 | size >> sw->info.shift, |
766 | ret, | |
c0fe3827 | 767 | sw->total_hw_samples_mixed |
1d14ffa9 FB |
768 | ); |
769 | #endif | |
770 | ||
771 | return ret << sw->info.shift; | |
85571bc7 FB |
772 | } |
773 | ||
1d14ffa9 FB |
774 | #ifdef DEBUG_AUDIO |
775 | static void audio_pcm_print_info (const char *cap, struct audio_pcm_info *info) | |
85571bc7 | 776 | { |
1d14ffa9 FB |
777 | dolog ("%s: bits %d, sign %d, freq %d, nchan %d\n", |
778 | cap, info->bits, info->sign, info->freq, info->nchannels); | |
85571bc7 | 779 | } |
1d14ffa9 | 780 | #endif |
85571bc7 | 781 | |
1d14ffa9 FB |
782 | #define DAC |
783 | #include "audio_template.h" | |
784 | #undef DAC | |
785 | #include "audio_template.h" | |
786 | ||
713a98f8 | 787 | /* |
788 | * Timer | |
789 | */ | |
526fb058 | 790 | static int audio_is_timer_needed(AudioState *s) |
713a98f8 | 791 | { |
792 | HWVoiceIn *hwi = NULL; | |
793 | HWVoiceOut *hwo = NULL; | |
794 | ||
526fb058 | 795 | while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) { |
713a98f8 | 796 | if (!hwo->poll_mode) return 1; |
797 | } | |
526fb058 | 798 | while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) { |
713a98f8 | 799 | if (!hwi->poll_mode) return 1; |
800 | } | |
801 | return 0; | |
802 | } | |
803 | ||
39deb1e4 | 804 | static void audio_reset_timer (AudioState *s) |
713a98f8 | 805 | { |
526fb058 | 806 | if (audio_is_timer_needed(s)) { |
1ffc2665 | 807 | timer_mod_anticipate_ns(s->ts, |
71830221 | 808 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + s->period_ticks); |
526fb058 KZ |
809 | if (!s->timer_running) { |
810 | s->timer_running = true; | |
811 | s->timer_last = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
71830221 | 812 | trace_audio_timer_start(s->period_ticks / SCALE_MS); |
1a961e78 GH |
813 | } |
814 | } else { | |
815 | timer_del(s->ts); | |
526fb058 KZ |
816 | if (s->timer_running) { |
817 | s->timer_running = false; | |
1a961e78 GH |
818 | trace_audio_timer_stop(); |
819 | } | |
713a98f8 | 820 | } |
821 | } | |
822 | ||
39deb1e4 | 823 | static void audio_timer (void *opaque) |
824 | { | |
1a961e78 | 825 | int64_t now, diff; |
71830221 | 826 | AudioState *s = opaque; |
1a961e78 GH |
827 | |
828 | now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); | |
526fb058 | 829 | diff = now - s->timer_last; |
71830221 | 830 | if (diff > s->period_ticks * 3 / 2) { |
1a961e78 GH |
831 | trace_audio_timer_delayed(diff / SCALE_MS); |
832 | } | |
526fb058 | 833 | s->timer_last = now; |
1a961e78 | 834 | |
18e2c177 | 835 | audio_run(s, "timer"); |
71830221 | 836 | audio_reset_timer(s); |
39deb1e4 | 837 | } |
838 | ||
713a98f8 | 839 | /* |
840 | * Public API | |
841 | */ | |
7520462b | 842 | size_t AUD_write(SWVoiceOut *sw, void *buf, size_t size) |
85571bc7 | 843 | { |
1d14ffa9 FB |
844 | if (!sw) { |
845 | /* XXX: Consider options */ | |
846 | return size; | |
847 | } | |
85571bc7 | 848 | |
1d14ffa9 | 849 | if (!sw->hw->enabled) { |
c0fe3827 | 850 | dolog ("Writing to disabled voice %s\n", SW_NAME (sw)); |
85571bc7 FB |
851 | return 0; |
852 | } | |
853 | ||
1d793fec | 854 | return audio_pcm_sw_write(sw, buf, size); |
1d14ffa9 FB |
855 | } |
856 | ||
7520462b | 857 | size_t AUD_read(SWVoiceIn *sw, void *buf, size_t size) |
1d14ffa9 | 858 | { |
1d14ffa9 FB |
859 | if (!sw) { |
860 | /* XXX: Consider options */ | |
861 | return size; | |
85571bc7 | 862 | } |
1d14ffa9 FB |
863 | |
864 | if (!sw->hw->enabled) { | |
c0fe3827 | 865 | dolog ("Reading from disabled voice %s\n", SW_NAME (sw)); |
1d14ffa9 | 866 | return 0; |
85571bc7 | 867 | } |
1d14ffa9 | 868 | |
1d793fec | 869 | return audio_pcm_sw_read(sw, buf, size); |
85571bc7 FB |
870 | } |
871 | ||
1d14ffa9 | 872 | int AUD_get_buffer_size_out (SWVoiceOut *sw) |
85571bc7 | 873 | { |
c0fe3827 | 874 | return sw->hw->samples << sw->hw->info.shift; |
1d14ffa9 FB |
875 | } |
876 | ||
877 | void AUD_set_active_out (SWVoiceOut *sw, int on) | |
878 | { | |
879 | HWVoiceOut *hw; | |
85571bc7 | 880 | |
1d14ffa9 | 881 | if (!sw) { |
85571bc7 | 882 | return; |
1d14ffa9 | 883 | } |
85571bc7 FB |
884 | |
885 | hw = sw->hw; | |
1d14ffa9 | 886 | if (sw->active != on) { |
526fb058 | 887 | AudioState *s = sw->s; |
1d14ffa9 | 888 | SWVoiceOut *temp_sw; |
ec36b695 | 889 | SWVoiceCap *sc; |
1d14ffa9 FB |
890 | |
891 | if (on) { | |
1d14ffa9 FB |
892 | hw->pending_disable = 0; |
893 | if (!hw->enabled) { | |
894 | hw->enabled = 1; | |
978dd635 | 895 | if (s->vm_running) { |
05d2f2a6 | 896 | hw->pcm_ops->ctl_out(hw, VOICE_ENABLE); |
39deb1e4 | 897 | audio_reset_timer (s); |
978dd635 | 898 | } |
1d14ffa9 | 899 | } |
1d14ffa9 FB |
900 | } |
901 | else { | |
902 | if (hw->enabled) { | |
903 | int nb_active = 0; | |
904 | ||
905 | for (temp_sw = hw->sw_head.lh_first; temp_sw; | |
906 | temp_sw = temp_sw->entries.le_next) { | |
907 | nb_active += temp_sw->active != 0; | |
908 | } | |
909 | ||
910 | hw->pending_disable = nb_active == 1; | |
911 | } | |
85571bc7 | 912 | } |
ec36b695 FB |
913 | |
914 | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) { | |
915 | sc->sw.active = hw->enabled; | |
8ead62cf | 916 | if (hw->enabled) { |
ec36b695 | 917 | audio_capture_maybe_changed (sc->cap, 1); |
8ead62cf FB |
918 | } |
919 | } | |
1d14ffa9 FB |
920 | sw->active = on; |
921 | } | |
922 | } | |
923 | ||
924 | void AUD_set_active_in (SWVoiceIn *sw, int on) | |
925 | { | |
926 | HWVoiceIn *hw; | |
927 | ||
928 | if (!sw) { | |
929 | return; | |
85571bc7 FB |
930 | } |
931 | ||
1d14ffa9 | 932 | hw = sw->hw; |
85571bc7 | 933 | if (sw->active != on) { |
526fb058 | 934 | AudioState *s = sw->s; |
1d14ffa9 FB |
935 | SWVoiceIn *temp_sw; |
936 | ||
85571bc7 | 937 | if (on) { |
85571bc7 FB |
938 | if (!hw->enabled) { |
939 | hw->enabled = 1; | |
978dd635 | 940 | if (s->vm_running) { |
05d2f2a6 | 941 | hw->pcm_ops->ctl_in(hw, VOICE_ENABLE); |
39deb1e4 | 942 | audio_reset_timer (s); |
978dd635 | 943 | } |
85571bc7 | 944 | } |
1d14ffa9 | 945 | sw->total_hw_samples_acquired = hw->total_samples_captured; |
85571bc7 FB |
946 | } |
947 | else { | |
1d14ffa9 | 948 | if (hw->enabled) { |
85571bc7 | 949 | int nb_active = 0; |
1d14ffa9 FB |
950 | |
951 | for (temp_sw = hw->sw_head.lh_first; temp_sw; | |
952 | temp_sw = temp_sw->entries.le_next) { | |
953 | nb_active += temp_sw->active != 0; | |
85571bc7 FB |
954 | } |
955 | ||
956 | if (nb_active == 1) { | |
1d14ffa9 FB |
957 | hw->enabled = 0; |
958 | hw->pcm_ops->ctl_in (hw, VOICE_DISABLE); | |
85571bc7 FB |
959 | } |
960 | } | |
961 | } | |
962 | sw->active = on; | |
963 | } | |
964 | } | |
965 | ||
7520462b | 966 | static size_t audio_get_avail (SWVoiceIn *sw) |
1d14ffa9 | 967 | { |
7520462b | 968 | size_t live; |
1d14ffa9 FB |
969 | |
970 | if (!sw) { | |
971 | return 0; | |
972 | } | |
973 | ||
974 | live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired; | |
7520462b KZ |
975 | if (audio_bug(__func__, live > sw->hw->samples)) { |
976 | dolog("live=%zu sw->hw->samples=%zu\n", live, sw->hw->samples); | |
1d14ffa9 FB |
977 | return 0; |
978 | } | |
979 | ||
980 | ldebug ( | |
26a76461 | 981 | "%s: get_avail live %d ret %" PRId64 "\n", |
c0fe3827 | 982 | SW_NAME (sw), |
1d14ffa9 FB |
983 | live, (((int64_t) live << 32) / sw->ratio) << sw->info.shift |
984 | ); | |
985 | ||
986 | return (((int64_t) live << 32) / sw->ratio) << sw->info.shift; | |
987 | } | |
988 | ||
7520462b | 989 | static size_t audio_get_free(SWVoiceOut *sw) |
1d14ffa9 | 990 | { |
7520462b | 991 | size_t live, dead; |
1d14ffa9 FB |
992 | |
993 | if (!sw) { | |
994 | return 0; | |
995 | } | |
996 | ||
997 | live = sw->total_hw_samples_mixed; | |
998 | ||
7520462b KZ |
999 | if (audio_bug(__func__, live > sw->hw->samples)) { |
1000 | dolog("live=%zu sw->hw->samples=%zu\n", live, sw->hw->samples); | |
c0fe3827 | 1001 | return 0; |
1d14ffa9 FB |
1002 | } |
1003 | ||
1004 | dead = sw->hw->samples - live; | |
1005 | ||
1006 | #ifdef DEBUG_OUT | |
26a76461 | 1007 | dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n", |
c0fe3827 | 1008 | SW_NAME (sw), |
1d14ffa9 | 1009 | live, dead, (((int64_t) dead << 32) / sw->ratio) << sw->info.shift); |
85571bc7 | 1010 | #endif |
1d14ffa9 FB |
1011 | |
1012 | return (((int64_t) dead << 32) / sw->ratio) << sw->info.shift; | |
1013 | } | |
1014 | ||
7520462b KZ |
1015 | static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos, |
1016 | size_t samples) | |
8ead62cf | 1017 | { |
7520462b | 1018 | size_t n; |
8ead62cf FB |
1019 | |
1020 | if (hw->enabled) { | |
ec36b695 | 1021 | SWVoiceCap *sc; |
8ead62cf | 1022 | |
ec36b695 FB |
1023 | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) { |
1024 | SWVoiceOut *sw = &sc->sw; | |
8ead62cf FB |
1025 | int rpos2 = rpos; |
1026 | ||
1027 | n = samples; | |
1028 | while (n) { | |
7520462b KZ |
1029 | size_t till_end_of_hw = hw->samples - rpos2; |
1030 | size_t to_write = MIN(till_end_of_hw, n); | |
1031 | size_t bytes = to_write << hw->info.shift; | |
1032 | size_t written; | |
8ead62cf FB |
1033 | |
1034 | sw->buf = hw->mix_buf + rpos2; | |
1035 | written = audio_pcm_sw_write (sw, NULL, bytes); | |
1036 | if (written - bytes) { | |
7520462b KZ |
1037 | dolog("Could not mix %zu bytes into a capture " |
1038 | "buffer, mixed %zu\n", | |
1039 | bytes, written); | |
8ead62cf FB |
1040 | break; |
1041 | } | |
1042 | n -= to_write; | |
1043 | rpos2 = (rpos2 + to_write) % hw->samples; | |
1044 | } | |
1045 | } | |
1046 | } | |
1047 | ||
7520462b KZ |
1048 | n = MIN(samples, hw->samples - rpos); |
1049 | mixeng_clear(hw->mix_buf + rpos, n); | |
1050 | mixeng_clear(hw->mix_buf, samples - n); | |
8ead62cf FB |
1051 | } |
1052 | ||
c0fe3827 | 1053 | static void audio_run_out (AudioState *s) |
1d14ffa9 FB |
1054 | { |
1055 | HWVoiceOut *hw = NULL; | |
1056 | SWVoiceOut *sw; | |
1057 | ||
526fb058 | 1058 | while ((hw = audio_pcm_hw_find_any_enabled_out(s, hw))) { |
7520462b KZ |
1059 | size_t played, live, prev_rpos, free; |
1060 | int nb_live, cleanup_required; | |
1d14ffa9 | 1061 | |
bdff253c | 1062 | live = audio_pcm_hw_get_live_out (hw, &nb_live); |
1d14ffa9 FB |
1063 | if (!nb_live) { |
1064 | live = 0; | |
1065 | } | |
c0fe3827 | 1066 | |
7520462b KZ |
1067 | if (audio_bug(__func__, live > hw->samples)) { |
1068 | dolog ("live=%zu hw->samples=%zu\n", live, hw->samples); | |
c0fe3827 | 1069 | continue; |
1d14ffa9 FB |
1070 | } |
1071 | ||
1072 | if (hw->pending_disable && !nb_live) { | |
ec36b695 | 1073 | SWVoiceCap *sc; |
1d14ffa9 FB |
1074 | #ifdef DEBUG_OUT |
1075 | dolog ("Disabling voice\n"); | |
85571bc7 | 1076 | #endif |
1d14ffa9 FB |
1077 | hw->enabled = 0; |
1078 | hw->pending_disable = 0; | |
1079 | hw->pcm_ops->ctl_out (hw, VOICE_DISABLE); | |
ec36b695 FB |
1080 | for (sc = hw->cap_head.lh_first; sc; sc = sc->entries.le_next) { |
1081 | sc->sw.active = 0; | |
1082 | audio_recalc_and_notify_capture (sc->cap); | |
8ead62cf | 1083 | } |
1d14ffa9 FB |
1084 | continue; |
1085 | } | |
1086 | ||
1087 | if (!live) { | |
1088 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
1089 | if (sw->active) { | |
1090 | free = audio_get_free (sw); | |
1091 | if (free > 0) { | |
1092 | sw->callback.fn (sw->callback.opaque, free); | |
1093 | } | |
1094 | } | |
1095 | } | |
1096 | continue; | |
1097 | } | |
1098 | ||
8ead62cf | 1099 | prev_rpos = hw->rpos; |
bdff253c | 1100 | played = hw->pcm_ops->run_out (hw, live); |
3d4d16f4 | 1101 | replay_audio_out(&played); |
470bcabd | 1102 | if (audio_bug(__func__, hw->rpos >= hw->samples)) { |
7520462b KZ |
1103 | dolog("hw->rpos=%zu hw->samples=%zu played=%zu\n", |
1104 | hw->rpos, hw->samples, played); | |
1d14ffa9 FB |
1105 | hw->rpos = 0; |
1106 | } | |
1107 | ||
1108 | #ifdef DEBUG_OUT | |
7520462b | 1109 | dolog("played=%zu\n", played); |
85571bc7 | 1110 | #endif |
1d14ffa9 FB |
1111 | |
1112 | if (played) { | |
1113 | hw->ts_helper += played; | |
8ead62cf | 1114 | audio_capture_mix_and_clear (hw, prev_rpos, played); |
1d14ffa9 FB |
1115 | } |
1116 | ||
c0fe3827 | 1117 | cleanup_required = 0; |
1d14ffa9 | 1118 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { |
1d14ffa9 FB |
1119 | if (!sw->active && sw->empty) { |
1120 | continue; | |
1121 | } | |
1122 | ||
470bcabd | 1123 | if (audio_bug(__func__, played > sw->total_hw_samples_mixed)) { |
7520462b KZ |
1124 | dolog("played=%zu sw->total_hw_samples_mixed=%zu\n", |
1125 | played, sw->total_hw_samples_mixed); | |
1d14ffa9 FB |
1126 | played = sw->total_hw_samples_mixed; |
1127 | } | |
1128 | ||
1129 | sw->total_hw_samples_mixed -= played; | |
1130 | ||
1131 | if (!sw->total_hw_samples_mixed) { | |
1132 | sw->empty = 1; | |
c0fe3827 | 1133 | cleanup_required |= !sw->active && !sw->callback.fn; |
1d14ffa9 FB |
1134 | } |
1135 | ||
1136 | if (sw->active) { | |
1137 | free = audio_get_free (sw); | |
1138 | if (free > 0) { | |
1139 | sw->callback.fn (sw->callback.opaque, free); | |
1140 | } | |
1141 | } | |
1142 | } | |
c0fe3827 FB |
1143 | |
1144 | if (cleanup_required) { | |
ec36b695 FB |
1145 | SWVoiceOut *sw1; |
1146 | ||
1147 | sw = hw->sw_head.lh_first; | |
1148 | while (sw) { | |
1149 | sw1 = sw->entries.le_next; | |
c0fe3827 | 1150 | if (!sw->active && !sw->callback.fn) { |
1a7dafce | 1151 | audio_close_out (sw); |
c0fe3827 | 1152 | } |
ec36b695 | 1153 | sw = sw1; |
c0fe3827 FB |
1154 | } |
1155 | } | |
1d14ffa9 FB |
1156 | } |
1157 | } | |
1158 | ||
c0fe3827 | 1159 | static void audio_run_in (AudioState *s) |
1d14ffa9 FB |
1160 | { |
1161 | HWVoiceIn *hw = NULL; | |
1162 | ||
526fb058 | 1163 | while ((hw = audio_pcm_hw_find_any_enabled_in(s, hw))) { |
1d14ffa9 | 1164 | SWVoiceIn *sw; |
7520462b | 1165 | size_t captured = 0, min; |
1d14ffa9 | 1166 | |
3d4d16f4 PD |
1167 | if (replay_mode != REPLAY_MODE_PLAY) { |
1168 | captured = hw->pcm_ops->run_in(hw); | |
1169 | } | |
1170 | replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples); | |
1d14ffa9 FB |
1171 | |
1172 | min = audio_pcm_hw_find_min_in (hw); | |
1173 | hw->total_samples_captured += captured - min; | |
1174 | hw->ts_helper += captured; | |
1175 | ||
1176 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
1177 | sw->total_hw_samples_acquired -= min; | |
1178 | ||
1179 | if (sw->active) { | |
7520462b | 1180 | size_t avail; |
1d14ffa9 FB |
1181 | |
1182 | avail = audio_get_avail (sw); | |
1183 | if (avail > 0) { | |
1184 | sw->callback.fn (sw->callback.opaque, avail); | |
1185 | } | |
1186 | } | |
1187 | } | |
1188 | } | |
1189 | } | |
1190 | ||
8ead62cf FB |
1191 | static void audio_run_capture (AudioState *s) |
1192 | { | |
1193 | CaptureVoiceOut *cap; | |
1194 | ||
1195 | for (cap = s->cap_head.lh_first; cap; cap = cap->entries.le_next) { | |
7520462b | 1196 | size_t live, rpos, captured; |
8ead62cf FB |
1197 | HWVoiceOut *hw = &cap->hw; |
1198 | SWVoiceOut *sw; | |
1199 | ||
bdff253c | 1200 | captured = live = audio_pcm_hw_get_live_out (hw, NULL); |
8ead62cf FB |
1201 | rpos = hw->rpos; |
1202 | while (live) { | |
7520462b KZ |
1203 | size_t left = hw->samples - rpos; |
1204 | size_t to_capture = MIN(live, left); | |
1ea879e5 | 1205 | struct st_sample *src; |
8ead62cf FB |
1206 | struct capture_callback *cb; |
1207 | ||
1208 | src = hw->mix_buf + rpos; | |
1209 | hw->clip (cap->buf, src, to_capture); | |
1210 | mixeng_clear (src, to_capture); | |
1211 | ||
1212 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
1213 | cb->ops.capture (cb->opaque, cap->buf, | |
1214 | to_capture << hw->info.shift); | |
1215 | } | |
1216 | rpos = (rpos + to_capture) % hw->samples; | |
1217 | live -= to_capture; | |
1218 | } | |
1219 | hw->rpos = rpos; | |
1220 | ||
1221 | for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) { | |
1222 | if (!sw->active && sw->empty) { | |
1223 | continue; | |
1224 | } | |
1225 | ||
470bcabd | 1226 | if (audio_bug(__func__, captured > sw->total_hw_samples_mixed)) { |
7520462b KZ |
1227 | dolog("captured=%zu sw->total_hw_samples_mixed=%zu\n", |
1228 | captured, sw->total_hw_samples_mixed); | |
8ead62cf FB |
1229 | captured = sw->total_hw_samples_mixed; |
1230 | } | |
1231 | ||
1232 | sw->total_hw_samples_mixed -= captured; | |
1233 | sw->empty = sw->total_hw_samples_mixed == 0; | |
1234 | } | |
1235 | } | |
1236 | } | |
1237 | ||
18e2c177 | 1238 | void audio_run(AudioState *s, const char *msg) |
571ec3d6 | 1239 | { |
18e2c177 KZ |
1240 | audio_run_out(s); |
1241 | audio_run_in(s); | |
1242 | audio_run_capture(s); | |
571ec3d6 | 1243 | |
713a98f8 | 1244 | #ifdef DEBUG_POLL |
1245 | { | |
1246 | static double prevtime; | |
1247 | double currtime; | |
1248 | struct timeval tv; | |
571ec3d6 | 1249 | |
713a98f8 | 1250 | if (gettimeofday (&tv, NULL)) { |
1251 | perror ("audio_run: gettimeofday"); | |
1252 | return; | |
1253 | } | |
1254 | ||
1255 | currtime = tv.tv_sec + tv.tv_usec * 1e-6; | |
1256 | dolog ("Elapsed since last %s: %f\n", msg, currtime - prevtime); | |
1257 | prevtime = currtime; | |
1258 | } | |
1259 | #endif | |
571ec3d6 FB |
1260 | } |
1261 | ||
71830221 KZ |
1262 | static int audio_driver_init(AudioState *s, struct audio_driver *drv, |
1263 | bool msg, Audiodev *dev) | |
85571bc7 | 1264 | { |
71830221 | 1265 | s->drv_opaque = drv->init(dev); |
1d14ffa9 | 1266 | |
c0fe3827 | 1267 | if (s->drv_opaque) { |
526fb058 KZ |
1268 | audio_init_nb_voices_out(s, drv); |
1269 | audio_init_nb_voices_in(s, drv); | |
c0fe3827 | 1270 | s->drv = drv; |
1d14ffa9 | 1271 | return 0; |
85571bc7 FB |
1272 | } |
1273 | else { | |
fb37ce92 GH |
1274 | if (msg) { |
1275 | dolog("Could not init `%s' audio driver\n", drv->name); | |
1276 | } | |
1d14ffa9 | 1277 | return -1; |
85571bc7 FB |
1278 | } |
1279 | } | |
1280 | ||
9781e040 | 1281 | static void audio_vm_change_state_handler (void *opaque, int running, |
1dfb4dd9 | 1282 | RunState state) |
85571bc7 | 1283 | { |
c0fe3827 | 1284 | AudioState *s = opaque; |
1d14ffa9 FB |
1285 | HWVoiceOut *hwo = NULL; |
1286 | HWVoiceIn *hwi = NULL; | |
541e0844 | 1287 | int op = running ? VOICE_ENABLE : VOICE_DISABLE; |
1d14ffa9 | 1288 | |
978dd635 | 1289 | s->vm_running = running; |
526fb058 | 1290 | while ((hwo = audio_pcm_hw_find_any_enabled_out(s, hwo))) { |
05d2f2a6 | 1291 | hwo->pcm_ops->ctl_out(hwo, op); |
1d14ffa9 | 1292 | } |
85571bc7 | 1293 | |
526fb058 | 1294 | while ((hwi = audio_pcm_hw_find_any_enabled_in(s, hwi))) { |
05d2f2a6 | 1295 | hwi->pcm_ops->ctl_in(hwi, op); |
85571bc7 | 1296 | } |
39deb1e4 | 1297 | audio_reset_timer (s); |
85571bc7 FB |
1298 | } |
1299 | ||
a384c205 MAL |
1300 | static bool is_cleaning_up; |
1301 | ||
1302 | bool audio_is_cleaning_up(void) | |
1303 | { | |
1304 | return is_cleaning_up; | |
1305 | } | |
1306 | ||
ecd97e95 | 1307 | static void free_audio_state(AudioState *s) |
85571bc7 | 1308 | { |
a384c205 MAL |
1309 | HWVoiceOut *hwo, *hwon; |
1310 | HWVoiceIn *hwi, *hwin; | |
1d14ffa9 | 1311 | |
526fb058 | 1312 | QLIST_FOREACH_SAFE(hwo, &s->hw_head_out, entries, hwon) { |
ec36b695 | 1313 | SWVoiceCap *sc; |
8ead62cf | 1314 | |
aeb29b64 JK |
1315 | if (hwo->enabled) { |
1316 | hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE); | |
1317 | } | |
1d14ffa9 | 1318 | hwo->pcm_ops->fini_out (hwo); |
8ead62cf | 1319 | |
ec36b695 FB |
1320 | for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) { |
1321 | CaptureVoiceOut *cap = sc->cap; | |
1322 | struct capture_callback *cb; | |
1323 | ||
1324 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
1325 | cb->ops.destroy (cb->opaque); | |
1326 | } | |
8ead62cf | 1327 | } |
a384c205 | 1328 | QLIST_REMOVE(hwo, entries); |
1d14ffa9 | 1329 | } |
85571bc7 | 1330 | |
526fb058 | 1331 | QLIST_FOREACH_SAFE(hwi, &s->hw_head_in, entries, hwin) { |
aeb29b64 JK |
1332 | if (hwi->enabled) { |
1333 | hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE); | |
1334 | } | |
1d14ffa9 | 1335 | hwi->pcm_ops->fini_in (hwi); |
a384c205 | 1336 | QLIST_REMOVE(hwi, entries); |
85571bc7 | 1337 | } |
c0fe3827 FB |
1338 | |
1339 | if (s->drv) { | |
1340 | s->drv->fini (s->drv_opaque); | |
a384c205 | 1341 | s->drv = NULL; |
c0fe3827 | 1342 | } |
71830221 KZ |
1343 | |
1344 | if (s->dev) { | |
1345 | qapi_free_Audiodev(s->dev); | |
1346 | s->dev = NULL; | |
1347 | } | |
e76ba19a KZ |
1348 | |
1349 | if (s->ts) { | |
1350 | timer_free(s->ts); | |
1351 | s->ts = NULL; | |
1352 | } | |
1353 | ||
ecd97e95 KZ |
1354 | g_free(s); |
1355 | } | |
1356 | ||
1357 | void audio_cleanup(void) | |
1358 | { | |
1359 | is_cleaning_up = true; | |
1360 | while (!QTAILQ_EMPTY(&audio_states)) { | |
1361 | AudioState *s = QTAILQ_FIRST(&audio_states); | |
1362 | QTAILQ_REMOVE(&audio_states, s, list); | |
1363 | free_audio_state(s); | |
1364 | } | |
85571bc7 FB |
1365 | } |
1366 | ||
d959fce9 JQ |
1367 | static const VMStateDescription vmstate_audio = { |
1368 | .name = "audio", | |
1369 | .version_id = 1, | |
1370 | .minimum_version_id = 1, | |
35d08458 | 1371 | .fields = (VMStateField[]) { |
d959fce9 | 1372 | VMSTATE_END_OF_LIST() |
1d14ffa9 | 1373 | } |
d959fce9 | 1374 | }; |
85571bc7 | 1375 | |
71830221 KZ |
1376 | static void audio_validate_opts(Audiodev *dev, Error **errp); |
1377 | ||
1378 | static AudiodevListEntry *audiodev_find( | |
1379 | AudiodevListHead *head, const char *drvname) | |
1380 | { | |
1381 | AudiodevListEntry *e; | |
1382 | QSIMPLEQ_FOREACH(e, head, next) { | |
1383 | if (strcmp(AudiodevDriver_str(e->dev->driver), drvname) == 0) { | |
1384 | return e; | |
1385 | } | |
1386 | } | |
1387 | ||
1388 | return NULL; | |
1389 | } | |
1390 | ||
ecd97e95 KZ |
1391 | /* |
1392 | * if we have dev, this function was called because of an -audiodev argument => | |
1393 | * initialize a new state with it | |
1394 | * if dev == NULL => legacy implicit initialization, return the already created | |
1395 | * state or create a new one | |
1396 | */ | |
af2041ed | 1397 | static AudioState *audio_init(Audiodev *dev, const char *name) |
85571bc7 | 1398 | { |
ecd97e95 | 1399 | static bool atexit_registered; |
1d14ffa9 | 1400 | size_t i; |
85571bc7 | 1401 | int done = 0; |
71830221 | 1402 | const char *drvname = NULL; |
713a98f8 | 1403 | VMChangeStateEntry *e; |
ecd97e95 | 1404 | AudioState *s; |
d3893a39 | 1405 | struct audio_driver *driver; |
71830221 KZ |
1406 | /* silence gcc warning about uninitialized variable */ |
1407 | AudiodevListHead head = QSIMPLEQ_HEAD_INITIALIZER(head); | |
1d14ffa9 | 1408 | |
71830221 KZ |
1409 | if (dev) { |
1410 | /* -audiodev option */ | |
af2041ed | 1411 | legacy_config = false; |
71830221 | 1412 | drvname = AudiodevDriver_str(dev->driver); |
ecd97e95 | 1413 | } else if (!QTAILQ_EMPTY(&audio_states)) { |
af2041ed KZ |
1414 | if (!legacy_config) { |
1415 | dolog("You must specify an audiodev= for the device %s\n", name); | |
1416 | exit(1); | |
1417 | } | |
ecd97e95 | 1418 | return QTAILQ_FIRST(&audio_states); |
71830221 KZ |
1419 | } else { |
1420 | /* legacy implicit initialization */ | |
1421 | head = audio_handle_legacy_opts(); | |
1422 | /* | |
1423 | * In case of legacy initialization, all Audiodevs in the list will have | |
1424 | * the same configuration (except the driver), so it does't matter which | |
1425 | * one we chose. We need an Audiodev to set up AudioState before we can | |
1426 | * init a driver. Also note that dev at this point is still in the | |
1427 | * list. | |
1428 | */ | |
1429 | dev = QSIMPLEQ_FIRST(&head)->dev; | |
1430 | audio_validate_opts(dev, &error_abort); | |
1431 | } | |
ecd97e95 KZ |
1432 | |
1433 | s = g_malloc0(sizeof(AudioState)); | |
71830221 KZ |
1434 | s->dev = dev; |
1435 | ||
72cf2d4f BS |
1436 | QLIST_INIT (&s->hw_head_out); |
1437 | QLIST_INIT (&s->hw_head_in); | |
1438 | QLIST_INIT (&s->cap_head); | |
ecd97e95 KZ |
1439 | if (!atexit_registered) { |
1440 | atexit(audio_cleanup); | |
1441 | atexit_registered = true; | |
1442 | } | |
1443 | QTAILQ_INSERT_TAIL(&audio_states, s, list); | |
571ec3d6 | 1444 | |
bc72ad67 | 1445 | s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s); |
571ec3d6 | 1446 | |
71830221 KZ |
1447 | s->nb_hw_voices_out = audio_get_pdo_out(dev)->voices; |
1448 | s->nb_hw_voices_in = audio_get_pdo_in(dev)->voices; | |
c0fe3827 | 1449 | |
1d14ffa9 | 1450 | if (s->nb_hw_voices_out <= 0) { |
571ec3d6 | 1451 | dolog ("Bogus number of playback voices %d, setting to 1\n", |
1d14ffa9 FB |
1452 | s->nb_hw_voices_out); |
1453 | s->nb_hw_voices_out = 1; | |
1454 | } | |
1455 | ||
1456 | if (s->nb_hw_voices_in <= 0) { | |
571ec3d6 | 1457 | dolog ("Bogus number of capture voices %d, setting to 0\n", |
1d14ffa9 | 1458 | s->nb_hw_voices_in); |
571ec3d6 | 1459 | s->nb_hw_voices_in = 0; |
1d14ffa9 | 1460 | } |
85571bc7 | 1461 | |
85571bc7 | 1462 | if (drvname) { |
d3893a39 GH |
1463 | driver = audio_driver_lookup(drvname); |
1464 | if (driver) { | |
71830221 | 1465 | done = !audio_driver_init(s, driver, true, dev); |
d3893a39 | 1466 | } else { |
85571bc7 FB |
1467 | dolog ("Unknown audio driver `%s'\n", drvname); |
1468 | } | |
71830221 KZ |
1469 | } else { |
1470 | for (i = 0; audio_prio_list[i]; i++) { | |
1471 | AudiodevListEntry *e = audiodev_find(&head, audio_prio_list[i]); | |
d3893a39 | 1472 | driver = audio_driver_lookup(audio_prio_list[i]); |
71830221 KZ |
1473 | |
1474 | if (e && driver) { | |
1475 | s->dev = dev = e->dev; | |
1476 | audio_validate_opts(dev, &error_abort); | |
1477 | done = !audio_driver_init(s, driver, false, dev); | |
1478 | if (done) { | |
1479 | e->dev = NULL; | |
1480 | break; | |
1481 | } | |
1d14ffa9 | 1482 | } |
85571bc7 FB |
1483 | } |
1484 | } | |
71830221 | 1485 | audio_free_audiodev_list(&head); |
85571bc7 | 1486 | |
85571bc7 | 1487 | if (!done) { |
d3893a39 | 1488 | driver = audio_driver_lookup("none"); |
71830221 | 1489 | done = !audio_driver_init(s, driver, false, dev); |
7e274652 MA |
1490 | assert(done); |
1491 | dolog("warning: Using timer based audio emulation\n"); | |
85571bc7 | 1492 | } |
1d14ffa9 | 1493 | |
71830221 KZ |
1494 | if (dev->timer_period <= 0) { |
1495 | s->period_ticks = 1; | |
0d9acba8 | 1496 | } else { |
be1092af | 1497 | s->period_ticks = dev->timer_period * SCALE_US; |
1d14ffa9 | 1498 | } |
0d9acba8 PB |
1499 | |
1500 | e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s); | |
1501 | if (!e) { | |
1502 | dolog ("warning: Could not register change state handler\n" | |
1503 | "(Audio can continue looping even after stopping the VM)\n"); | |
1d14ffa9 FB |
1504 | } |
1505 | ||
72cf2d4f | 1506 | QLIST_INIT (&s->card_head); |
0be71e32 | 1507 | vmstate_register (NULL, 0, &vmstate_audio, s); |
ecd97e95 | 1508 | return s; |
71830221 KZ |
1509 | } |
1510 | ||
1511 | void audio_free_audiodev_list(AudiodevListHead *head) | |
1512 | { | |
1513 | AudiodevListEntry *e; | |
1514 | while ((e = QSIMPLEQ_FIRST(head))) { | |
1515 | QSIMPLEQ_REMOVE_HEAD(head, next); | |
1516 | qapi_free_Audiodev(e->dev); | |
1517 | g_free(e); | |
1518 | } | |
85571bc7 | 1519 | } |
8ead62cf | 1520 | |
1a7dafce | 1521 | void AUD_register_card (const char *name, QEMUSoundCard *card) |
1522 | { | |
ecd97e95 | 1523 | if (!card->state) { |
af2041ed | 1524 | card->state = audio_init(NULL, name); |
ecd97e95 KZ |
1525 | } |
1526 | ||
7267c094 | 1527 | card->name = g_strdup (name); |
1a7dafce | 1528 | memset (&card->entries, 0, sizeof (card->entries)); |
ecd97e95 | 1529 | QLIST_INSERT_HEAD(&card->state->card_head, card, entries); |
1a7dafce | 1530 | } |
1531 | ||
1532 | void AUD_remove_card (QEMUSoundCard *card) | |
1533 | { | |
72cf2d4f | 1534 | QLIST_REMOVE (card, entries); |
7267c094 | 1535 | g_free (card->name); |
1a7dafce | 1536 | } |
1537 | ||
1538 | ||
ecd97e95 KZ |
1539 | CaptureVoiceOut *AUD_add_capture( |
1540 | AudioState *s, | |
1ea879e5 | 1541 | struct audsettings *as, |
8ead62cf FB |
1542 | struct audio_capture_ops *ops, |
1543 | void *cb_opaque | |
1544 | ) | |
1545 | { | |
1546 | CaptureVoiceOut *cap; | |
1547 | struct capture_callback *cb; | |
1548 | ||
ecd97e95 | 1549 | if (!s) { |
af2041ed KZ |
1550 | if (!legacy_config) { |
1551 | dolog("You must specify audiodev when trying to capture\n"); | |
1552 | return NULL; | |
1553 | } | |
1554 | s = audio_init(NULL, NULL); | |
ecd97e95 KZ |
1555 | } |
1556 | ||
ec36b695 | 1557 | if (audio_validate_settings (as)) { |
8ead62cf FB |
1558 | dolog ("Invalid settings were passed when trying to add capture\n"); |
1559 | audio_print_settings (as); | |
e8d85444 | 1560 | return NULL; |
8ead62cf FB |
1561 | } |
1562 | ||
e8d85444 | 1563 | cb = g_malloc0(sizeof(*cb)); |
8ead62cf FB |
1564 | cb->ops = *ops; |
1565 | cb->opaque = cb_opaque; | |
1566 | ||
526fb058 | 1567 | cap = audio_pcm_capture_find_specific(s, as); |
8ead62cf | 1568 | if (cap) { |
72cf2d4f | 1569 | QLIST_INSERT_HEAD (&cap->cb_head, cb, entries); |
ec36b695 | 1570 | return cap; |
8ead62cf FB |
1571 | } |
1572 | else { | |
1573 | HWVoiceOut *hw; | |
1574 | CaptureVoiceOut *cap; | |
1575 | ||
e8d85444 | 1576 | cap = g_malloc0(sizeof(*cap)); |
8ead62cf FB |
1577 | |
1578 | hw = &cap->hw; | |
526fb058 | 1579 | hw->s = s; |
72cf2d4f BS |
1580 | QLIST_INIT (&hw->sw_head); |
1581 | QLIST_INIT (&cap->cb_head); | |
8ead62cf FB |
1582 | |
1583 | /* XXX find a more elegant way */ | |
1584 | hw->samples = 4096 * 4; | |
e8d85444 | 1585 | hw->mix_buf = g_new0(struct st_sample, hw->samples); |
8ead62cf | 1586 | |
d929eba5 | 1587 | audio_pcm_init_info (&hw->info, as); |
8ead62cf | 1588 | |
e8d85444 | 1589 | cap->buf = g_malloc0_n(hw->samples, 1 << hw->info.shift); |
8ead62cf FB |
1590 | |
1591 | hw->clip = mixeng_clip | |
1592 | [hw->info.nchannels == 2] | |
1593 | [hw->info.sign] | |
d929eba5 | 1594 | [hw->info.swap_endianness] |
f941aa25 | 1595 | [audio_bits_to_index (hw->info.bits)]; |
8ead62cf | 1596 | |
72cf2d4f BS |
1597 | QLIST_INSERT_HEAD (&s->cap_head, cap, entries); |
1598 | QLIST_INSERT_HEAD (&cap->cb_head, cb, entries); | |
8ead62cf | 1599 | |
526fb058 | 1600 | QLIST_FOREACH(hw, &s->hw_head_out, entries) { |
1a7dafce | 1601 | audio_attach_capture (hw); |
8ead62cf | 1602 | } |
ec36b695 | 1603 | return cap; |
ec36b695 FB |
1604 | } |
1605 | } | |
1606 | ||
1607 | void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque) | |
1608 | { | |
1609 | struct capture_callback *cb; | |
1610 | ||
1611 | for (cb = cap->cb_head.lh_first; cb; cb = cb->entries.le_next) { | |
1612 | if (cb->opaque == cb_opaque) { | |
1613 | cb->ops.destroy (cb_opaque); | |
72cf2d4f | 1614 | QLIST_REMOVE (cb, entries); |
7267c094 | 1615 | g_free (cb); |
ec36b695 FB |
1616 | |
1617 | if (!cap->cb_head.lh_first) { | |
1618 | SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1; | |
a3c25997 | 1619 | |
ec36b695 | 1620 | while (sw) { |
a3c25997 | 1621 | SWVoiceCap *sc = (SWVoiceCap *) sw; |
ec36b695 FB |
1622 | #ifdef DEBUG_CAPTURE |
1623 | dolog ("freeing %s\n", sw->name); | |
1624 | #endif | |
a3c25997 | 1625 | |
ec36b695 FB |
1626 | sw1 = sw->entries.le_next; |
1627 | if (sw->rate) { | |
1628 | st_rate_stop (sw->rate); | |
1629 | sw->rate = NULL; | |
1630 | } | |
72cf2d4f BS |
1631 | QLIST_REMOVE (sw, entries); |
1632 | QLIST_REMOVE (sc, entries); | |
7267c094 | 1633 | g_free (sc); |
ec36b695 FB |
1634 | sw = sw1; |
1635 | } | |
72cf2d4f | 1636 | QLIST_REMOVE (cap, entries); |
3268a845 GH |
1637 | g_free (cap->hw.mix_buf); |
1638 | g_free (cap->buf); | |
7267c094 | 1639 | g_free (cap); |
ec36b695 FB |
1640 | } |
1641 | return; | |
1642 | } | |
8ead62cf FB |
1643 | } |
1644 | } | |
683efdcb AZ |
1645 | |
1646 | void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol) | |
1647 | { | |
1648 | if (sw) { | |
6c95ab94 MAL |
1649 | HWVoiceOut *hw = sw->hw; |
1650 | ||
683efdcb AZ |
1651 | sw->vol.mute = mute; |
1652 | sw->vol.l = nominal_volume.l * lvol / 255; | |
1653 | sw->vol.r = nominal_volume.r * rvol / 255; | |
6c95ab94 MAL |
1654 | |
1655 | if (hw->pcm_ops->ctl_out) { | |
1656 | hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw); | |
1657 | } | |
683efdcb AZ |
1658 | } |
1659 | } | |
1660 | ||
1661 | void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol) | |
1662 | { | |
1663 | if (sw) { | |
6c95ab94 MAL |
1664 | HWVoiceIn *hw = sw->hw; |
1665 | ||
683efdcb AZ |
1666 | sw->vol.mute = mute; |
1667 | sw->vol.l = nominal_volume.l * lvol / 255; | |
1668 | sw->vol.r = nominal_volume.r * rvol / 255; | |
6c95ab94 MAL |
1669 | |
1670 | if (hw->pcm_ops->ctl_in) { | |
1671 | hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw); | |
1672 | } | |
683efdcb AZ |
1673 | } |
1674 | } | |
71830221 KZ |
1675 | |
1676 | void audio_create_pdos(Audiodev *dev) | |
1677 | { | |
1678 | switch (dev->driver) { | |
1679 | #define CASE(DRIVER, driver, pdo_name) \ | |
1680 | case AUDIODEV_DRIVER_##DRIVER: \ | |
1681 | if (!dev->u.driver.has_in) { \ | |
1682 | dev->u.driver.in = g_malloc0( \ | |
1683 | sizeof(Audiodev##pdo_name##PerDirectionOptions)); \ | |
1684 | dev->u.driver.has_in = true; \ | |
1685 | } \ | |
1686 | if (!dev->u.driver.has_out) { \ | |
1687 | dev->u.driver.out = g_malloc0( \ | |
1688 | sizeof(AudiodevAlsaPerDirectionOptions)); \ | |
1689 | dev->u.driver.has_out = true; \ | |
1690 | } \ | |
1691 | break | |
1692 | ||
1693 | CASE(NONE, none, ); | |
1694 | CASE(ALSA, alsa, Alsa); | |
1695 | CASE(COREAUDIO, coreaudio, Coreaudio); | |
1696 | CASE(DSOUND, dsound, ); | |
1697 | CASE(OSS, oss, Oss); | |
1698 | CASE(PA, pa, Pa); | |
1699 | CASE(SDL, sdl, ); | |
1700 | CASE(SPICE, spice, ); | |
1701 | CASE(WAV, wav, ); | |
1702 | ||
1703 | case AUDIODEV_DRIVER__MAX: | |
1704 | abort(); | |
1705 | }; | |
1706 | } | |
1707 | ||
1708 | static void audio_validate_per_direction_opts( | |
1709 | AudiodevPerDirectionOptions *pdo, Error **errp) | |
1710 | { | |
1711 | if (!pdo->has_fixed_settings) { | |
1712 | pdo->has_fixed_settings = true; | |
1713 | pdo->fixed_settings = true; | |
1714 | } | |
1715 | if (!pdo->fixed_settings && | |
1716 | (pdo->has_frequency || pdo->has_channels || pdo->has_format)) { | |
1717 | error_setg(errp, | |
1718 | "You can't use frequency, channels or format with fixed-settings=off"); | |
1719 | return; | |
1720 | } | |
1721 | ||
1722 | if (!pdo->has_frequency) { | |
1723 | pdo->has_frequency = true; | |
1724 | pdo->frequency = 44100; | |
1725 | } | |
1726 | if (!pdo->has_channels) { | |
1727 | pdo->has_channels = true; | |
1728 | pdo->channels = 2; | |
1729 | } | |
1730 | if (!pdo->has_voices) { | |
1731 | pdo->has_voices = true; | |
1732 | pdo->voices = 1; | |
1733 | } | |
1734 | if (!pdo->has_format) { | |
1735 | pdo->has_format = true; | |
1736 | pdo->format = AUDIO_FORMAT_S16; | |
1737 | } | |
1738 | } | |
1739 | ||
1740 | static void audio_validate_opts(Audiodev *dev, Error **errp) | |
1741 | { | |
1742 | Error *err = NULL; | |
1743 | ||
1744 | audio_create_pdos(dev); | |
1745 | ||
1746 | audio_validate_per_direction_opts(audio_get_pdo_in(dev), &err); | |
1747 | if (err) { | |
1748 | error_propagate(errp, err); | |
1749 | return; | |
1750 | } | |
1751 | ||
1752 | audio_validate_per_direction_opts(audio_get_pdo_out(dev), &err); | |
1753 | if (err) { | |
1754 | error_propagate(errp, err); | |
1755 | return; | |
1756 | } | |
1757 | ||
1758 | if (!dev->has_timer_period) { | |
1759 | dev->has_timer_period = true; | |
1760 | dev->timer_period = 10000; /* 100Hz -> 10ms */ | |
1761 | } | |
1762 | } | |
1763 | ||
1764 | void audio_parse_option(const char *opt) | |
1765 | { | |
1766 | AudiodevListEntry *e; | |
1767 | Audiodev *dev = NULL; | |
1768 | ||
1769 | Visitor *v = qobject_input_visitor_new_str(opt, "driver", &error_fatal); | |
1770 | visit_type_Audiodev(v, NULL, &dev, &error_fatal); | |
1771 | visit_free(v); | |
1772 | ||
1773 | audio_validate_opts(dev, &error_fatal); | |
1774 | ||
1775 | e = g_malloc0(sizeof(AudiodevListEntry)); | |
1776 | e->dev = dev; | |
1777 | QSIMPLEQ_INSERT_TAIL(&audiodevs, e, next); | |
1778 | } | |
1779 | ||
1780 | void audio_init_audiodevs(void) | |
1781 | { | |
1782 | AudiodevListEntry *e; | |
1783 | ||
1784 | QSIMPLEQ_FOREACH(e, &audiodevs, next) { | |
af2041ed | 1785 | audio_init(e->dev, NULL); |
71830221 KZ |
1786 | } |
1787 | } | |
1788 | ||
1789 | audsettings audiodev_to_audsettings(AudiodevPerDirectionOptions *pdo) | |
1790 | { | |
1791 | return (audsettings) { | |
1792 | .freq = pdo->frequency, | |
1793 | .nchannels = pdo->channels, | |
1794 | .fmt = pdo->format, | |
1795 | .endianness = AUDIO_HOST_ENDIANNESS, | |
1796 | }; | |
1797 | } | |
1798 | ||
1799 | int audioformat_bytes_per_sample(AudioFormat fmt) | |
1800 | { | |
1801 | switch (fmt) { | |
1802 | case AUDIO_FORMAT_U8: | |
1803 | case AUDIO_FORMAT_S8: | |
1804 | return 1; | |
1805 | ||
1806 | case AUDIO_FORMAT_U16: | |
1807 | case AUDIO_FORMAT_S16: | |
1808 | return 2; | |
1809 | ||
1810 | case AUDIO_FORMAT_U32: | |
1811 | case AUDIO_FORMAT_S32: | |
1812 | return 4; | |
1813 | ||
1814 | case AUDIO_FORMAT__MAX: | |
1815 | ; | |
1816 | } | |
1817 | abort(); | |
1818 | } | |
1819 | ||
1820 | ||
1821 | /* frames = freq * usec / 1e6 */ | |
1822 | int audio_buffer_frames(AudiodevPerDirectionOptions *pdo, | |
1823 | audsettings *as, int def_usecs) | |
1824 | { | |
1825 | uint64_t usecs = pdo->has_buffer_length ? pdo->buffer_length : def_usecs; | |
1826 | return (as->freq * usecs + 500000) / 1000000; | |
1827 | } | |
1828 | ||
1829 | /* samples = channels * frames = channels * freq * usec / 1e6 */ | |
1830 | int audio_buffer_samples(AudiodevPerDirectionOptions *pdo, | |
1831 | audsettings *as, int def_usecs) | |
1832 | { | |
1833 | return as->nchannels * audio_buffer_frames(pdo, as, def_usecs); | |
1834 | } | |
1835 | ||
1836 | /* | |
1837 | * bytes = bytes_per_sample * samples = | |
1838 | * bytes_per_sample * channels * freq * usec / 1e6 | |
1839 | */ | |
1840 | int audio_buffer_bytes(AudiodevPerDirectionOptions *pdo, | |
1841 | audsettings *as, int def_usecs) | |
1842 | { | |
1843 | return audio_buffer_samples(pdo, as, def_usecs) * | |
1844 | audioformat_bytes_per_sample(as->fmt); | |
1845 | } | |
ecd97e95 KZ |
1846 | |
1847 | AudioState *audio_state_by_name(const char *name) | |
1848 | { | |
1849 | AudioState *s; | |
1850 | QTAILQ_FOREACH(s, &audio_states, list) { | |
1851 | assert(s->dev); | |
1852 | if (strcmp(name, s->dev->id) == 0) { | |
1853 | return s; | |
1854 | } | |
1855 | } | |
1856 | return NULL; | |
1857 | } | |
1858 | ||
1859 | const char *audio_get_id(QEMUSoundCard *card) | |
1860 | { | |
1861 | if (card->state) { | |
1862 | assert(card->state->dev); | |
1863 | return card->state->dev->id; | |
1864 | } else { | |
1865 | return ""; | |
1866 | } | |
1867 | } |