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
) (
41 struct audio_driver
*drv
44 int max_voices
= glue (drv
->max_voices_
, TYPE
);
45 int voice_size
= glue (drv
->voice_size_
, TYPE
);
47 if (glue (s
->nb_hw_voices_
, TYPE
) > max_voices
) {
50 dolog ("Driver `%s' does not support " NAME
"\n", drv
->name
);
54 dolog ("Driver `%s' does not support %d " NAME
" voices, max %d\n",
56 glue (s
->nb_hw_voices_
, TYPE
),
59 glue (s
->nb_hw_voices_
, TYPE
) = max_voices
;
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;
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
);
74 static void glue (audio_pcm_hw_free_resources_
, TYPE
) (HW
*hw
)
83 static int glue (audio_pcm_hw_alloc_resources_
, TYPE
) (HW
*hw
)
85 HWBUF
= audio_calloc (AUDIO_FUNC
, hw
->samples
, sizeof (st_sample_t
));
87 dolog ("Could not allocate " NAME
" buffer (%d samples)\n",
95 static void glue (audio_pcm_sw_free_resources_
, TYPE
) (SW
*sw
)
102 st_rate_stop (sw
->rate
);
109 static int glue (audio_pcm_sw_alloc_resources_
, TYPE
) (SW
*sw
)
114 samples
= sw
->hw
->samples
;
116 samples
= ((int64_t) sw
->hw
->samples
<< 32) / sw
->ratio
;
119 sw
->buf
= audio_calloc (AUDIO_FUNC
, samples
, sizeof (st_sample_t
));
121 dolog ("Could not allocate buffer for `%s' (%d samples)\n",
122 SW_NAME (sw
), samples
);
127 sw
->rate
= st_rate_start (sw
->info
.freq
, sw
->hw
->info
.freq
);
129 sw
->rate
= st_rate_start (sw
->hw
->info
.freq
, sw
->info
.freq
);
139 static int glue (audio_pcm_sw_init_
, TYPE
) (
149 audio_pcm_init_info (&sw
->info
, as
, audio_need_to_swap_endian (endian
));
153 sw
->ratio
= ((int64_t) sw
->hw
->info
.freq
<< 32) / sw
->info
.freq
;
154 sw
->total_hw_samples_mixed
= 0;
157 sw
->ratio
= ((int64_t) sw
->info
.freq
<< 32) / sw
->hw
->info
.freq
;
161 sw
->conv
= mixeng_conv
163 sw
->clip
= mixeng_clip
165 [sw
->info
.nchannels
== 2]
167 [sw
->info
.swap_endian
]
168 [sw
->info
.bits
== 16];
170 sw
->name
= qemu_strdup (name
);
171 err
= glue (audio_pcm_sw_alloc_resources_
, TYPE
) (sw
);
173 qemu_free (sw
->name
);
179 static void glue (audio_pcm_sw_fini_
, TYPE
) (SW
*sw
)
181 glue (audio_pcm_sw_free_resources_
, TYPE
) (sw
);
183 qemu_free (sw
->name
);
188 static void glue (audio_pcm_hw_add_sw_
, TYPE
) (HW
*hw
, SW
*sw
)
190 LIST_INSERT_HEAD (&hw
->sw_head
, sw
, entries
);
193 static void glue (audio_pcm_hw_del_sw_
, TYPE
) (SW
*sw
)
195 LIST_REMOVE (sw
, entries
);
198 static void glue (audio_pcm_hw_gc_
, TYPE
) (AudioState
*s
, HW
**hwp
)
202 if (!hw
->sw_head
.lh_first
) {
203 LIST_REMOVE (hw
, entries
);
204 glue (s
->nb_hw_voices_
, TYPE
) += 1;
205 glue (audio_pcm_hw_free_resources_
,TYPE
) (hw
);
206 glue (hw
->pcm_ops
->fini_
, TYPE
) (hw
);
212 static HW
*glue (audio_pcm_hw_find_any_
, TYPE
) (AudioState
*s
, HW
*hw
)
214 return hw
? hw
->entries
.le_next
: s
->glue (hw_head_
, TYPE
).lh_first
;
217 static HW
*glue (audio_pcm_hw_find_any_enabled_
, TYPE
) (AudioState
*s
, HW
*hw
)
219 while ((hw
= glue (audio_pcm_hw_find_any_
, TYPE
) (s
, hw
))) {
227 static HW
*glue (audio_pcm_hw_find_specific_
, TYPE
) (
233 while ((hw
= glue (audio_pcm_hw_find_any_
, TYPE
) (s
, hw
))) {
234 if (audio_pcm_info_eq (&hw
->info
, as
)) {
241 static HW
*glue (audio_pcm_hw_add_new_
, TYPE
) (AudioState
*s
, audsettings_t
*as
)
244 struct audio_driver
*drv
= s
->drv
;
246 if (!glue (s
->nb_hw_voices_
, TYPE
)) {
250 if (audio_bug (AUDIO_FUNC
, !drv
)) {
251 dolog ("No host audio driver\n");
255 if (audio_bug (AUDIO_FUNC
, !drv
->pcm_ops
)) {
256 dolog ("Host audio driver without pcm_ops\n");
260 hw
= audio_calloc (AUDIO_FUNC
, 1, glue (drv
->voice_size_
, TYPE
));
262 dolog ("Can not allocate voice `%s' size %d\n",
263 drv
->name
, glue (drv
->voice_size_
, TYPE
));
267 hw
->pcm_ops
= drv
->pcm_ops
;
268 LIST_INIT (&hw
->sw_head
);
270 if (glue (hw
->pcm_ops
->init_
, TYPE
) (hw
, as
)) {
274 if (audio_bug (AUDIO_FUNC
, hw
->samples
<= 0)) {
275 dolog ("hw->samples=%d\n", hw
->samples
);
280 hw
->clip
= mixeng_clip
282 hw
->conv
= mixeng_conv
284 [hw
->info
.nchannels
== 2]
286 [hw
->info
.swap_endian
]
287 [hw
->info
.bits
== 16];
289 if (glue (audio_pcm_hw_alloc_resources_
, TYPE
) (hw
)) {
293 LIST_INSERT_HEAD (&s
->glue (hw_head_
, TYPE
), hw
, entries
);
294 glue (s
->nb_hw_voices_
, TYPE
) -= 1;
298 glue (hw
->pcm_ops
->fini_
, TYPE
) (hw
);
304 static HW
*glue (audio_pcm_hw_add_
, TYPE
) (AudioState
*s
, audsettings_t
*as
)
308 if (glue (conf
.fixed_
, TYPE
).enabled
&& glue (conf
.fixed_
, TYPE
).greedy
) {
309 hw
= glue (audio_pcm_hw_add_new_
, TYPE
) (s
, as
);
315 hw
= glue (audio_pcm_hw_find_specific_
, TYPE
) (s
, NULL
, as
);
320 hw
= glue (audio_pcm_hw_add_new_
, TYPE
) (s
, as
);
325 return glue (audio_pcm_hw_find_any_
, TYPE
) (s
, NULL
);
328 static SW
*glue (audio_pcm_create_voice_pair_
, TYPE
) (
339 if (glue (conf
.fixed_
, TYPE
).enabled
) {
340 hw_as
= glue (conf
.fixed_
, TYPE
).settings
;
346 sw
= audio_calloc (AUDIO_FUNC
, 1, sizeof (*sw
));
348 dolog ("Could not allocate soft voice `%s' (%zu bytes)\n",
349 sw_name
? sw_name
: "unknown", sizeof (*sw
));
353 hw
= glue (audio_pcm_hw_add_
, TYPE
) (s
, &hw_as
);
358 glue (audio_pcm_hw_add_sw_
, TYPE
) (hw
, sw
);
360 if (glue (audio_pcm_sw_init_
, TYPE
) (sw
, hw
, sw_name
, as
, sw_endian
)) {
367 glue (audio_pcm_hw_del_sw_
, TYPE
) (sw
);
368 glue (audio_pcm_hw_gc_
, TYPE
) (s
, &hw
);
375 static void glue (audio_close_
, TYPE
) (AudioState
*s
, SW
*sw
)
377 glue (audio_pcm_sw_fini_
, TYPE
) (sw
);
378 glue (audio_pcm_hw_del_sw_
, TYPE
) (sw
);
379 glue (audio_pcm_hw_gc_
, TYPE
) (s
, &sw
->hw
);
383 void glue (AUD_close_
, TYPE
) (QEMUSoundCard
*card
, SW
*sw
)
386 if (audio_bug (AUDIO_FUNC
, !card
|| !card
->audio
)) {
387 dolog ("card=%p card->audio=%p\n",
388 card
, card
? card
->audio
: NULL
);
392 glue (audio_close_
, TYPE
) (card
->audio
, sw
);
396 SW
*glue (AUD_open_
, TYPE
) (
400 void *callback_opaque
,
401 audio_callback_fn_t callback_fn
,
412 ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
413 name
, as
->freq
, as
->nchannels
, as
->fmt
);
415 if (audio_bug (AUDIO_FUNC
,
416 !card
|| !card
->audio
|| !name
|| !callback_fn
|| !as
)) {
417 dolog ("card=%p card->audio=%p name=%p callback_fn=%p as=%p\n",
418 card
, card
? card
->audio
: NULL
, name
, callback_fn
, as
);
424 if (audio_bug (AUDIO_FUNC
, audio_validate_settigs (as
))) {
425 audio_print_settings (as
);
429 if (audio_bug (AUDIO_FUNC
, !s
->drv
)) {
430 dolog ("Can not open `%s' (no host audio driver)\n", name
);
434 if (sw
&& audio_pcm_info_eq (&sw
->info
, as
)) {
439 if (conf
.plive
&& sw
&& (!sw
->active
&& !sw
->empty
)) {
440 live
= sw
->total_hw_samples_mixed
;
443 dolog ("Replacing voice %s with %d live samples\n", SW_NAME (sw
), live
);
444 dolog ("Old %s freq %d, bits %d, channels %d\n",
445 SW_NAME (sw
), sw
->info
.freq
, sw
->info
.bits
, sw
->info
.nchannels
);
446 dolog ("New %s freq %d, bits %d, channels %d\n",
449 (fmt
== AUD_FMT_S16
|| fmt
== AUD_FMT_U16
) ? 16 : 8,
455 old_sw
->callback
.fn
= NULL
;
461 if (!glue (conf
.fixed_
, TYPE
).enabled
&& sw
) {
462 glue (AUD_close_
, TYPE
) (card
, sw
);
470 dolog ("Internal logic error voice `%s' has no hardware store\n",
475 glue (audio_pcm_sw_fini_
, TYPE
) (sw
);
476 if (glue (audio_pcm_sw_init_
, TYPE
) (sw
, hw
, name
, as
, sw_endian
)) {
481 sw
= glue (audio_pcm_create_voice_pair_
, TYPE
) (s
, name
, as
, sw_endian
);
483 dolog ("Failed to create voice `%s'\n", name
);
489 sw
->vol
= nominal_volume
;
490 sw
->callback
.fn
= callback_fn
;
491 sw
->callback
.opaque
= callback_opaque
;
496 (live
<< old_sw
->info
.shift
)
497 * old_sw
->info
.bytes_per_second
498 / sw
->info
.bytes_per_second
;
501 dolog ("Silence will be mixed %d\n", mixed
);
503 sw
->total_hw_samples_mixed
+= mixed
;
508 dolog ("%s\n", name
);
509 audio_pcm_print_info ("hw", &sw
->hw
->info
);
510 audio_pcm_print_info ("sw", &sw
->info
);
517 glue (AUD_close_
, TYPE
) (card
, sw
);
521 int glue (AUD_is_active_
, TYPE
) (SW
*sw
)
523 return sw
? sw
->active
: 0;
526 void glue (AUD_init_time_stamp_
, TYPE
) (SW
*sw
, QEMUAudioTimeStamp
*ts
)
532 ts
->old_ts
= sw
->hw
->ts_helper
;
535 uint64_t glue (AUD_get_elapsed_usec_
, TYPE
) (SW
*sw
, QEMUAudioTimeStamp
*ts
)
537 uint64_t delta
, cur_ts
, old_ts
;
543 cur_ts
= sw
->hw
->ts_helper
;
545 /* dolog ("cur %lld old %lld\n", cur_ts, old_ts); */
547 if (cur_ts
>= old_ts
) {
548 delta
= cur_ts
- old_ts
;
551 delta
= UINT64_MAX
- old_ts
+ cur_ts
;
558 return (delta
* sw
->hw
->info
.freq
) / 1000000;