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