VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/audio_template.h@ 35112

最後變更 在這個檔案從35112是 35112,由 vboxsync 提交於 14 年 前

Device/Audio/Sound_template: incorporated upstream fix to properly calculate DAC(playback) buffer size.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette