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