xref: /qemu/audio/audio_template.h (revision 3c5a5e213e5f08fbfe70728237f7799ac70f5b99)
1 /*
2  * QEMU Audio subsystem header
3  *
4  * Copyright (c) 2005 Vassili Karpov (malc)
5  *
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  */
24 
25 #ifdef DAC
26 #define NAME "playback"
27 #define HWBUF hw->mix_buf
28 #define TYPE out
29 #define HW HWVoiceOut
30 #define SW SWVoiceOut
31 #else
32 #define NAME "capture"
33 #define TYPE in
34 #define HW HWVoiceIn
35 #define SW SWVoiceIn
36 #define HWBUF hw->conv_buf
37 #endif
38 
glue(audio_init_nb_voices_,TYPE)39 static void glue(audio_init_nb_voices_, TYPE)(AudioState *s,
40                                               struct audio_driver *drv, int min_voices)
41 {
42     int max_voices = glue (drv->max_voices_, TYPE);
43     size_t voice_size = glue(drv->voice_size_, TYPE);
44 
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) {
47         if (!max_voices) {
48 #ifdef DAC
49             dolog ("Driver `%s' does not support " NAME "\n", drv->name);
50 #endif
51         } else {
52             dolog ("Driver `%s' does not support %d " NAME " voices, max %d\n",
53                    drv->name,
54                    glue (s->nb_hw_voices_, TYPE),
55                    max_voices);
56         }
57         glue (s->nb_hw_voices_, TYPE) = max_voices;
58     }
59 
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),
63                min_voices);
64     }
65 
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;
70     }
71 
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);
75     }
76 }
77 
glue(audio_pcm_hw_free_resources_,TYPE)78 static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
79 {
80     g_free(hw->buf_emul);
81     g_free(HWBUF.buffer);
82     HWBUF.buffer = NULL;
83     HWBUF.size = 0;
84 }
85 
glue(audio_pcm_hw_alloc_resources_,TYPE)86 static void glue(audio_pcm_hw_alloc_resources_, TYPE)(HW *hw)
87 {
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");
92         }
93 
94         HWBUF.buffer = g_new0(st_sample, samples);
95         HWBUF.size = samples;
96         HWBUF.pos = 0;
97     } else {
98         HWBUF.buffer = NULL;
99         HWBUF.size = 0;
100     }
101 }
102 
glue(audio_pcm_sw_free_resources_,TYPE)103 static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
104 {
105     g_free(sw->resample_buf.buffer);
106     sw->resample_buf.buffer = NULL;
107     sw->resample_buf.size = 0;
108 
109     if (sw->rate) {
110         st_rate_stop (sw->rate);
111     }
112     sw->rate = NULL;
113 }
114 
glue(audio_pcm_sw_alloc_resources_,TYPE)115 static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
116 {
117     HW *hw = sw->hw;
118     uint64_t samples;
119 
120     if (!glue(audio_get_pdo_, TYPE)(sw->s->dev)->mixing_engine) {
121         return 0;
122     }
123 
124     samples = muldiv64(HWBUF.size, sw->info.freq, hw->info.freq);
125     if (samples == 0) {
126         uint64_t f_fe_min;
127         uint64_t f_be = (uint32_t)hw->info.freq;
128 
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"
134                       " are supported.\n",
135                       sw->info.freq, sw->name, f_fe_min);
136         return -1;
137     }
138 
139     /*
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.
143      */
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;
148 
149 #ifdef DAC
150     sw->rate = st_rate_start(sw->info.freq, hw->info.freq);
151 #else
152     sw->rate = st_rate_start(hw->info.freq, sw->info.freq);
153 #endif
154 
155     return 0;
156 }
157 
glue(audio_pcm_sw_init_,TYPE)158 static int glue (audio_pcm_sw_init_, TYPE) (
159     SW *sw,
160     HW *hw,
161     const char *name,
162     struct audsettings *as
163     )
164 {
165     int err;
166 
167     audio_pcm_init_info (&sw->info, as);
168     sw->hw = hw;
169     sw->active = 0;
170 #ifdef DAC
171     sw->total_hw_samples_mixed = 0;
172     sw->empty = 1;
173 #endif
174 
175     if (sw->info.is_float) {
176 #ifdef DAC
177         sw->conv = mixeng_conv_float[sw->info.nchannels == 2]
178             [sw->info.swap_endianness];
179 #else
180         sw->clip = mixeng_clip_float[sw->info.nchannels == 2]
181             [sw->info.swap_endianness];
182 #endif
183     } else {
184 #ifdef DAC
185         sw->conv = mixeng_conv
186 #else
187         sw->clip = mixeng_clip
188 #endif
189             [sw->info.nchannels == 2]
190             [sw->info.is_signed]
191             [sw->info.swap_endianness]
192             [audio_bits_to_index(sw->info.bits)];
193     }
194 
195     sw->name = g_strdup (name);
196     err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
197     if (err) {
198         g_free (sw->name);
199         sw->name = NULL;
200     }
201     return err;
202 }
203 
glue(audio_pcm_sw_fini_,TYPE)204 static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
205 {
206     glue (audio_pcm_sw_free_resources_, TYPE) (sw);
207     g_free (sw->name);
208     sw->name = NULL;
209 }
210 
glue(audio_pcm_hw_add_sw_,TYPE)211 static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
212 {
213     QLIST_INSERT_HEAD (&hw->sw_head, sw, entries);
214 }
215 
glue(audio_pcm_hw_del_sw_,TYPE)216 static void glue (audio_pcm_hw_del_sw_, TYPE) (SW *sw)
217 {
218     QLIST_REMOVE (sw, entries);
219 }
220 
glue(audio_pcm_hw_gc_,TYPE)221 static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
222 {
223     HW *hw = *hwp;
224     AudioState *s = hw->s;
225 
226     if (!hw->sw_head.lh_first) {
227 #ifdef DAC
228         audio_detach_capture(hw);
229 #endif
230         QLIST_REMOVE(hw, entries);
231         glue(hw->pcm_ops->fini_, TYPE) (hw);
232         glue(s->nb_hw_voices_, TYPE) += 1;
233         glue(audio_pcm_hw_free_resources_ , TYPE) (hw);
234         g_free(hw);
235         *hwp = NULL;
236     }
237 }
238 
glue(audio_pcm_hw_find_any_,TYPE)239 static HW *glue(audio_pcm_hw_find_any_, TYPE)(AudioState *s, HW *hw)
240 {
241     return hw ? hw->entries.le_next : glue (s->hw_head_, TYPE).lh_first;
242 }
243 
glue(audio_pcm_hw_find_any_enabled_,TYPE)244 static HW *glue(audio_pcm_hw_find_any_enabled_, TYPE)(AudioState *s, HW *hw)
245 {
246     while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) {
247         if (hw->enabled) {
248             return hw;
249         }
250     }
251     return NULL;
252 }
253 
glue(audio_pcm_hw_find_specific_,TYPE)254 static HW *glue(audio_pcm_hw_find_specific_, TYPE)(AudioState *s, HW *hw,
255                                                    struct audsettings *as)
256 {
257     while ((hw = glue(audio_pcm_hw_find_any_, TYPE)(s, hw))) {
258         if (audio_pcm_info_eq (&hw->info, as)) {
259             return hw;
260         }
261     }
262     return NULL;
263 }
264 
glue(audio_pcm_hw_add_new_,TYPE)265 static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioState *s,
266                                              struct audsettings *as)
267 {
268     HW *hw;
269     struct audio_driver *drv = s->drv;
270 
271     if (!glue (s->nb_hw_voices_, TYPE)) {
272         return NULL;
273     }
274 
275     if (audio_bug(__func__, !drv)) {
276         dolog ("No host audio driver\n");
277         return NULL;
278     }
279 
280     if (audio_bug(__func__, !drv->pcm_ops)) {
281         dolog ("Host audio driver without pcm_ops\n");
282         return NULL;
283     }
284 
285     /*
286      * Since glue(s->nb_hw_voices_, TYPE) is != 0, glue(drv->voice_size_, TYPE)
287      * is guaranteed to be != 0. See the audio_init_nb_voices_* functions.
288      */
289     hw = g_malloc0(glue(drv->voice_size_, TYPE));
290     hw->s = s;
291     hw->pcm_ops = drv->pcm_ops;
292 
293     QLIST_INIT (&hw->sw_head);
294 #ifdef DAC
295     QLIST_INIT (&hw->cap_head);
296 #endif
297     if (glue (hw->pcm_ops->init_, TYPE) (hw, as, s->drv_opaque)) {
298         goto err0;
299     }
300 
301     if (audio_bug(__func__, hw->samples <= 0)) {
302         dolog("hw->samples=%zd\n", hw->samples);
303         goto err1;
304     }
305 
306     if (hw->info.is_float) {
307 #ifdef DAC
308         hw->clip = mixeng_clip_float[hw->info.nchannels == 2]
309             [hw->info.swap_endianness];
310 #else
311         hw->conv = mixeng_conv_float[hw->info.nchannels == 2]
312             [hw->info.swap_endianness];
313 #endif
314     } else {
315 #ifdef DAC
316         hw->clip = mixeng_clip
317 #else
318         hw->conv = mixeng_conv
319 #endif
320             [hw->info.nchannels == 2]
321             [hw->info.is_signed]
322             [hw->info.swap_endianness]
323             [audio_bits_to_index(hw->info.bits)];
324     }
325 
326     glue(audio_pcm_hw_alloc_resources_, TYPE)(hw);
327 
328     QLIST_INSERT_HEAD (&s->glue (hw_head_, TYPE), hw, entries);
329     glue (s->nb_hw_voices_, TYPE) -= 1;
330 #ifdef DAC
331     audio_attach_capture (hw);
332 #endif
333     return hw;
334 
335  err1:
336     glue (hw->pcm_ops->fini_, TYPE) (hw);
337  err0:
338     g_free (hw);
339     return NULL;
340 }
341 
glue(audio_get_pdo_,TYPE)342 AudiodevPerDirectionOptions *glue(audio_get_pdo_, TYPE)(Audiodev *dev)
343 {
344     switch (dev->driver) {
345     case AUDIODEV_DRIVER_NONE:
346         return dev->u.none.TYPE;
347 #ifdef CONFIG_AUDIO_ALSA
348     case AUDIODEV_DRIVER_ALSA:
349         return qapi_AudiodevAlsaPerDirectionOptions_base(dev->u.alsa.TYPE);
350 #endif
351 #ifdef CONFIG_AUDIO_COREAUDIO
352     case AUDIODEV_DRIVER_COREAUDIO:
353         return qapi_AudiodevCoreaudioPerDirectionOptions_base(
354             dev->u.coreaudio.TYPE);
355 #endif
356 #ifdef CONFIG_DBUS_DISPLAY
357     case AUDIODEV_DRIVER_DBUS:
358         return dev->u.dbus.TYPE;
359 #endif
360 #ifdef CONFIG_AUDIO_DSOUND
361     case AUDIODEV_DRIVER_DSOUND:
362         return dev->u.dsound.TYPE;
363 #endif
364 #ifdef CONFIG_AUDIO_JACK
365     case AUDIODEV_DRIVER_JACK:
366         return qapi_AudiodevJackPerDirectionOptions_base(dev->u.jack.TYPE);
367 #endif
368 #ifdef CONFIG_AUDIO_OSS
369     case AUDIODEV_DRIVER_OSS:
370         return qapi_AudiodevOssPerDirectionOptions_base(dev->u.oss.TYPE);
371 #endif
372 #ifdef CONFIG_AUDIO_PA
373     case AUDIODEV_DRIVER_PA:
374         return qapi_AudiodevPaPerDirectionOptions_base(dev->u.pa.TYPE);
375 #endif
376 #ifdef CONFIG_AUDIO_PIPEWIRE
377     case AUDIODEV_DRIVER_PIPEWIRE:
378         return qapi_AudiodevPipewirePerDirectionOptions_base(dev->u.pipewire.TYPE);
379 #endif
380 #ifdef CONFIG_AUDIO_SDL
381     case AUDIODEV_DRIVER_SDL:
382         return qapi_AudiodevSdlPerDirectionOptions_base(dev->u.sdl.TYPE);
383 #endif
384 #ifdef CONFIG_AUDIO_SNDIO
385     case AUDIODEV_DRIVER_SNDIO:
386         return dev->u.sndio.TYPE;
387 #endif
388 #ifdef CONFIG_SPICE
389     case AUDIODEV_DRIVER_SPICE:
390         return dev->u.spice.TYPE;
391 #endif
392     case AUDIODEV_DRIVER_WAV:
393         return dev->u.wav.TYPE;
394 
395     case AUDIODEV_DRIVER__MAX:
396         break;
397     }
398     abort();
399 }
400 
glue(audio_pcm_hw_add_,TYPE)401 static HW *glue(audio_pcm_hw_add_, TYPE)(AudioState *s, struct audsettings *as)
402 {
403     HW *hw;
404     AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
405 
406     if (!pdo->mixing_engine || pdo->fixed_settings) {
407         hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as);
408         if (!pdo->mixing_engine || hw) {
409             return hw;
410         }
411     }
412 
413     hw = glue(audio_pcm_hw_find_specific_, TYPE)(s, NULL, as);
414     if (hw) {
415         return hw;
416     }
417 
418     hw = glue(audio_pcm_hw_add_new_, TYPE)(s, as);
419     if (hw) {
420         return hw;
421     }
422 
423     return glue(audio_pcm_hw_find_any_, TYPE)(s, NULL);
424 }
425 
glue(audio_pcm_create_voice_pair_,TYPE)426 static SW *glue(audio_pcm_create_voice_pair_, TYPE)(
427     AudioState *s,
428     const char *sw_name,
429     struct audsettings *as
430     )
431 {
432     SW *sw;
433     HW *hw;
434     struct audsettings hw_as;
435     AudiodevPerDirectionOptions *pdo = glue(audio_get_pdo_, TYPE)(s->dev);
436 
437     if (pdo->fixed_settings) {
438         hw_as = audiodev_to_audsettings(pdo);
439     } else {
440         hw_as = *as;
441     }
442 
443     sw = g_new0(SW, 1);
444     sw->s = s;
445 
446     hw = glue(audio_pcm_hw_add_, TYPE)(s, &hw_as);
447     if (!hw) {
448         dolog("Could not create a backend for voice `%s'\n", sw_name);
449         goto err1;
450     }
451 
452     glue (audio_pcm_hw_add_sw_, TYPE) (hw, sw);
453 
454     if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, sw_name, as)) {
455         goto err2;
456     }
457 
458     return sw;
459 
460 err2:
461     glue (audio_pcm_hw_del_sw_, TYPE) (sw);
462     glue (audio_pcm_hw_gc_, TYPE) (&hw);
463 err1:
464     g_free(sw);
465     return NULL;
466 }
467 
glue(audio_close_,TYPE)468 static void glue (audio_close_, TYPE) (SW *sw)
469 {
470     glue (audio_pcm_sw_fini_, TYPE) (sw);
471     glue (audio_pcm_hw_del_sw_, TYPE) (sw);
472     glue (audio_pcm_hw_gc_, TYPE) (&sw->hw);
473     g_free (sw);
474 }
475 
glue(AUD_close_,TYPE)476 void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
477 {
478     if (sw) {
479         if (audio_bug(__func__, !card)) {
480             dolog ("card=%p\n", card);
481             return;
482         }
483 
484         glue (audio_close_, TYPE) (sw);
485     }
486 }
487 
glue(AUD_open_,TYPE)488 SW *glue (AUD_open_, TYPE) (
489     QEMUSoundCard *card,
490     SW *sw,
491     const char *name,
492     void *callback_opaque ,
493     audio_callback_fn callback_fn,
494     struct audsettings *as
495     )
496 {
497     AudioState *s;
498     AudiodevPerDirectionOptions *pdo;
499 
500     if (audio_bug(__func__, !card || !name || !callback_fn || !as)) {
501         dolog ("card=%p name=%p callback_fn=%p as=%p\n",
502                card, name, callback_fn, as);
503         goto fail;
504     }
505 
506     s = card->state;
507     pdo = glue(audio_get_pdo_, TYPE)(s->dev);
508 
509     ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
510             name, as->freq, as->nchannels, as->fmt);
511 
512     if (audio_bug(__func__, audio_validate_settings(as))) {
513         audio_print_settings (as);
514         goto fail;
515     }
516 
517     if (audio_bug(__func__, !s->drv)) {
518         dolog ("Can not open `%s' (no host audio driver)\n", name);
519         goto fail;
520     }
521 
522     if (sw && audio_pcm_info_eq (&sw->info, as)) {
523         return sw;
524     }
525 
526     if (!pdo->fixed_settings && sw) {
527         glue (AUD_close_, TYPE) (card, sw);
528         sw = NULL;
529     }
530 
531     if (sw) {
532         HW *hw = sw->hw;
533 
534         if (!hw) {
535             dolog("Internal logic error: voice `%s' has no backend\n",
536                   SW_NAME(sw));
537             goto fail;
538         }
539 
540         glue (audio_pcm_sw_fini_, TYPE) (sw);
541         if (glue (audio_pcm_sw_init_, TYPE) (sw, hw, name, as)) {
542             goto fail;
543         }
544     } else {
545         sw = glue(audio_pcm_create_voice_pair_, TYPE)(s, name, as);
546         if (!sw) {
547             return NULL;
548         }
549     }
550 
551     sw->card = card;
552     sw->vol = nominal_volume;
553     sw->callback.fn = callback_fn;
554     sw->callback.opaque = callback_opaque;
555 
556 #ifdef DEBUG_AUDIO
557     dolog ("%s\n", name);
558     audio_pcm_print_info ("hw", &sw->hw->info);
559     audio_pcm_print_info ("sw", &sw->info);
560 #endif
561 
562     return sw;
563 
564  fail:
565     glue (AUD_close_, TYPE) (card, sw);
566     return NULL;
567 }
568 
glue(AUD_is_active_,TYPE)569 int glue (AUD_is_active_, TYPE) (SW *sw)
570 {
571     return sw ? sw->active : 0;
572 }
573 
glue(AUD_init_time_stamp_,TYPE)574 void glue (AUD_init_time_stamp_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
575 {
576     if (!sw) {
577         return;
578     }
579 
580     ts->old_ts = sw->hw->ts_helper;
581 }
582 
glue(AUD_get_elapsed_usec_,TYPE)583 uint64_t glue (AUD_get_elapsed_usec_, TYPE) (SW *sw, QEMUAudioTimeStamp *ts)
584 {
585     uint64_t delta, cur_ts, old_ts;
586 
587     if (!sw) {
588         return 0;
589     }
590 
591     cur_ts = sw->hw->ts_helper;
592     old_ts = ts->old_ts;
593     /* dolog ("cur %" PRId64 " old %" PRId64 "\n", cur_ts, old_ts); */
594 
595     if (cur_ts >= old_ts) {
596         delta = cur_ts - old_ts;
597     } else {
598         delta = UINT64_MAX - old_ts + cur_ts;
599     }
600 
601     if (!delta) {
602         return 0;
603     }
604 
605     return muldiv64 (delta, sw->hw->info.freq, 1000000);
606 }
607 
608 #undef TYPE
609 #undef HW
610 #undef SW
611 #undef HWBUF
612 #undef NAME
613