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