2 * QEMU Audio subsystem header
4 * Copyright (c) 2005 Vassili Karpov (malc)
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 #define NAME "playback"
27 #define HWBUF hw->mix_buf
32 #define NAME "capture"
36 #define HWBUF hw->conv_buf
39 static void glue(audio_init_nb_voices_, TYPE)(AudioState *s,
40 struct audio_driver *drv, int min_voices)
42 int max_voices = glue (drv->max_voices_, TYPE);
43 size_t voice_size = glue(drv->voice_size_, TYPE);
45 glue (s->nb_hw_voices_, TYPE) = glue(audio_get_pdo_, TYPE)(s->dev)->voices;
46 if (glue (s->nb_hw_voices_, TYPE) > max_voices) {
49 dolog ("Driver `%s' does not support " NAME "\n", drv->name);
52 dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n",
54 glue (s->nb_hw_voices_, TYPE),
57 glue (s->nb_hw_voices_, TYPE) = max_voices;
60 if (glue (s->nb_hw_voices_, TYPE) < min_voices) {
61 dolog ("Bogus number of " NAME " voices %d, setting to %d\n",
62 glue (s->nb_hw_voices_, TYPE),
66 if (audio_bug(__func__, !voice_size && max_voices)) {
67 dolog ("drv=`%s' voice_size=0 max_voices=%d\n",
68 drv->name, max_voices);
69 glue (s->nb_hw_voices_, TYPE) = 0;
72 if (audio_bug(__func__, voice_size && !max_voices)) {
73 dolog("drv=`%s' voice_size=%zu max_voices=0\n",
74 drv->name, voice_size);
78 static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
86 static void glue(audio_pcm_hw_alloc_resources_, TYPE)(HW *hw)
88 if (glue(audio_get_pdo_, TYPE)(hw->s->dev)->mixing_engine) {
89 size_t samples = hw->samples;
90 if (audio_bug(__func__, samples == 0)) {
91 dolog("Attempted to allocate empty buffer\n");
94 HWBUF.buffer = g_new0(st_sample, samples);
103 static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
105 g_free(sw->resample_buf.buffer);
106 sw->resample_buf.buffer = NULL;
107 sw->resample_buf.size = 0;
110 st_rate_stop (sw->rate);
115 static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
120 if (!glue(audio_get_pdo_, TYPE)(sw->s->dev)->mixing_engine) {
124 samples = muldiv64(HWBUF.size, sw->info.freq, hw->info.freq);
127 uint64_t f_be = (uint32_t)hw->info.freq;
129 /* f_fe_min = ceil(1 [frames] * f_be [Hz] / size_be [frames]) */
130 f_fe_min = (f_be + HWBUF.size - 1) / HWBUF.size;
131 qemu_log_mask(LOG_UNIMP,
132 AUDIO_CAP ": The guest selected a " NAME " sample rate"
133 " of %d Hz for %s. Only sample rates >= %" PRIu64 " Hz"
135 sw->info.freq, sw->name, f_fe_min);
140 * Allocate one additional audio frame that is needed for upsampling
141 * if the resample buffer size is small. For large buffer sizes take
142 * care of overflows and truncation.
144 samples = samples < SIZE_MAX ? samples + 1 : SIZE_MAX;
145 sw->resample_buf.buffer = g_new0(st_sample, samples);
146 sw->resample_buf.size = samples;
147 sw->resample_buf.pos = 0;
150 sw->rate = st_rate_start(sw->info.freq, hw->info.freq);
152 sw->rate = st_rate_start(hw->info.freq, sw->info.freq);
158 static int glue (audio_pcm_sw_init_, TYPE) (
162 struct audsettings *as
167 audio_pcm_init_info (&sw->info, as);
171 sw->total_hw_samples_mixed = 0;
175 if (sw->info.is_float) {
177 sw->conv = mixeng_conv_float[sw->info.nchannels == 2];
179 sw->clip = mixeng_clip_float[sw->info.nchannels == 2];
183 sw->conv = mixeng_conv
185 sw->clip = mixeng_clip
187 [sw->info.nchannels == 2]
189 [sw->info.swap_endianness]
190 [audio_bits_to_index(sw->info.bits)];
193 sw->name = g_strdup (name);
194 err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
202 static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
204 glue (audio_pcm_sw_free_resources_, TYPE) (sw);
209 static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
211 QLIST_INSERT_HEAD (&hw->sw_head, sw, entries);
214 static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
216 QLIST_REMOVE (sw, entries);
219 static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
222 AudioState *s = hw->s;
224 if (!hw->sw_head.lh_first) {
226 audio_detach_capture(hw);
228 QLIST_REMOVE(hw, entries);
229 glue(hw->pcm_ops->fini_, TYPE) (hw);
230 glue(s->nb_hw_voices_, TYPE) += 1;
231 glue(audio_pcm_hw_free_resources_ , TYPE) (hw);
237 static HW *glue(audio_pcm_hw_find_any_, TYPE)(AudioState *s, HW *hw)
239 return hw ? hw->entries.le_next : glue (s->hw_head_, TYPE).lh_first;
242 static HW *glue(audio_pcm_hw_find_any_enabled_, TYPE)(AudioState *s, HW *hw)
244 while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) {
252 static HW *glue(audio_pcm_hw_find_specific_, TYPE)(AudioState *s, HW *hw,
253 struct audsettings *as)
255 while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) {
256 if (audio_pcm_info_eq (&hw->info, as)) {
263 static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioState *s,
264 struct audsettings *as)
267 struct audio_driver *drv = s->drv;
269 if (!glue (s->nb_hw_voices_, TYPE)) {
273 if (audio_bug(__func__, !drv)) {
274 dolog ("No host audio driver\n");
278 if (audio_bug(__func__, !drv->pcm_ops)) {
279 dolog ("Host audio driver without pcm_ops\n");
284 * Since glue(s->nb_hw_voices_, TYPE) is != 0, glue(drv->voice_size_, TYPE)
285 * is guaranteed to be != 0. See the audio_init_nb_voices_* functions.
287 hw = g_malloc0(glue(drv->voice_size_, TYPE));
289 hw->pcm_ops = drv->pcm_ops;
291 QLIST_INIT (&hw->sw_head);
293 QLIST_INIT (&hw->cap_head);
295 if (glue (hw->pcm_ops->init_, TYPE) (hw, as, s->drv_opaque)) {
299 if (audio_bug(__func__, hw->samples <= 0)) {
300 dolog("hw->samples=%zd\n", hw->samples);
304 if (hw->info.is_float) {
306 hw->clip = mixeng_clip_float[hw->info.nchannels == 2];
308 hw->conv = mixeng_conv_float[hw->info.nchannels == 2];
312 hw->clip = mixeng_clip
314 hw->conv = mixeng_conv
316 [hw->info.nchannels == 2]
318 [hw->info.swap_endianness]
319 [audio_bits_to_index(hw->info.bits)];
322 glue(audio_pcm_hw_alloc_resources_, TYPE)(hw);
324 QLIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
325 glue (s->nb_hw_voices_, TYPE) -= 1;
327 audio_attach_capture (hw);
332 glue (hw->pcm_ops->fini_, TYPE) (hw);
338 AudiodevPerDirectionOptions *glue(audio_get_pdo_, TYPE)(Audiodev *dev)
340 switch (dev->driver) {
341 case AUDIODEV_DRIVER_NONE:
342 return dev->u.none.TYPE;
343 #ifdef CONFIG_AUDIO_ALSA
344 case AUDIODEV_DRIVER_ALSA:
345 return qapi_AudiodevAlsaPerDirectionOptions_base(dev->u.alsa.TYPE);
347 #ifdef CONFIG_AUDIO_COREAUDIO
348 case AUDIODEV_DRIVER_COREAUDIO:
349 return qapi_AudiodevCoreaudioPerDirectionOptions_base(
350 dev->u.coreaudio.TYPE);
352 #ifdef CONFIG_DBUS_DISPLAY
353 case AUDIODEV_DRIVER_DBUS:
354 return dev->u.dbus.TYPE;
356 #ifdef CONFIG_AUDIO_DSOUND
357 case AUDIODEV_DRIVER_DSOUND:
358 return dev->u.dsound.TYPE;
360 #ifdef CONFIG_AUDIO_JACK
361 case AUDIODEV_DRIVER_JACK:
362 return qapi_AudiodevJackPerDirectionOptions_base(dev->u.jack.TYPE);
364 #ifdef CONFIG_AUDIO_OSS
365 case AUDIODEV_DRIVER_OSS:
366 return qapi_AudiodevOssPerDirectionOptions_base(dev->u.oss.TYPE);
368 #ifdef CONFIG_AUDIO_PA
369 case AUDIODEV_DRIVER_PA:
370 return qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.TYPE);
372 #ifdef CONFIG_AUDIO_PIPEWIRE
373 case AUDIODEV_DRIVER_PIPEWIRE:
374 return qapi_AudiodevPipewirePerDirectionOptions_base(dev->u.pipewire.TYPE);
376 #ifdef CONFIG_AUDIO_SDL
377 case AUDIODEV_DRIVER_SDL:
378 return qapi_AudiodevSdlPerDirectionOptions_base(dev->u.sdl.TYPE);
380 #ifdef CONFIG_AUDIO_SNDIO
381 case AUDIODEV_DRIVER_SNDIO:
382 return dev->u.sndio.TYPE;
385 case AUDIODEV_DRIVER_SPICE:
386 return dev->u.spice.TYPE;
388 case AUDIODEV_DRIVER_WAV:
389 return dev->u.wav.TYPE;
391 case AUDIODEV_DRIVER__MAX:
397 static HW *glue(audio_pcm_hw_add_, TYPE)(AudioState *s, struct audsettings *as)
400 AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
402 if (!pdo->mixing_engine || pdo->fixed_settings) {
403 hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as);
404 if (!pdo->mixing_engine || hw) {
409 hw = glue(audio_pcm_hw_find_specific_, TYPE)(s, NULL, as);
414 hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as);
419 return glue(audio_pcm_hw_find_any_, TYPE)(s, NULL);
422 static SW *glue(audio_pcm_create_voice_pair_, TYPE)(
425 struct audsettings *as
430 struct audsettings hw_as;
431 AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
433 if (pdo->fixed_settings) {
434 hw_as = audiodev_to_audsettings(pdo);
442 hw = glue(audio_pcm_hw_add_, TYPE)(s, &hw_as);
444 dolog("Could not create a backend for voice `%s'\n", sw_name);
448 glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
450 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
457 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
458 glue (audio_pcm_hw_gc_, TYPE) (&hw);
464 static void glue (audio_close_, TYPE) (SW *sw)
466 glue (audio_pcm_sw_fini_, TYPE) (sw);
467 glue (audio_pcm_hw_del_sw_, TYPE) (sw);
468 glue (audio_pcm_hw_gc_, TYPE) (&sw->hw);
472 void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
475 if (audio_bug(__func__, !card)) {
476 dolog ("card=%p\n", card);
480 glue (audio_close_, TYPE) (sw);
484 SW *glue (AUD_open_, TYPE) (
488 void *callback_opaque ,
489 audio_callback_fn callback_fn,
490 struct audsettings *as
494 AudiodevPerDirectionOptions *pdo;
496 if (audio_bug(__func__, !card || !name || !callback_fn || !as)) {
497 dolog ("card=%p name=%p callback_fn=%p as=%p\n",
498 card, name, callback_fn, as);
503 pdo = glue(audio_get_pdo_, TYPE)(s->dev);
505 ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
506 name, as->freq, as->nchannels, as->fmt);
508 if (audio_bug(__func__, audio_validate_settings(as))) {
509 audio_print_settings (as);
513 if (audio_bug(__func__, !s->drv)) {
514 dolog ("Can not open `%s' (no host audio driver)\n", name);
518 if (sw && audio_pcm_info_eq (&sw->info, as)) {
522 if (!pdo->fixed_settings && sw) {
523 glue (AUD_close_, TYPE) (card, sw);
531 dolog("Internal logic error: voice `%s' has no backend\n",
536 glue (audio_pcm_sw_fini_, TYPE) (sw);
537 if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
541 sw = glue(audio_pcm_create_voice_pair_, TYPE)(s, name, as);
548 sw->vol = nominal_volume;
549 sw->callback.fn = callback_fn;
550 sw->callback.opaque = callback_opaque;
553 dolog ("%s\n", name);
554 audio_pcm_print_info ("hw", &sw->hw->info);
555 audio_pcm_print_info ("sw", &sw->info);
561 glue (AUD_close_, TYPE) (card, sw);
565 int glue (AUD_is_active_, TYPE) (SW *sw)
567 return sw ? sw->active : 0;
570 void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
576 ts->old_ts = sw->hw->ts_helper;
579 uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
581 uint64_t delta, cur_ts, old_ts;
587 cur_ts = sw->hw->ts_helper;
589 /* dolog ("cur %" PRId64 " old %" PRId64 "\n", cur_ts, old_ts); */
591 if (cur_ts >= old_ts) {
592 delta = cur_ts - old_ts;
594 delta = UINT64_MAX - old_ts + cur_ts;
601 return muldiv64 (delta, sw->hw->info.freq, 1000000);