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