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