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