Audio: Mixeng-free 5.1/7.1 audio support
v4 cover Audio: Mixeng-free 5.1/7.1 audio support
Hi,
This is the newest iteration of my mixeng-free audio patches.
Changes from v3:
* fixed typo in documentation
* fix wavaudio format-string error on 32-bit targets.
Regards,
Zoltan
Kővágó, Zoltán (24):
audio: api for mixeng code free backends
alsaaudio: port to the new audio backend api
coreaudio: port to the new audio backend api
dsoundaudio: port to the new audio backend api
noaudio: port to the new audio backend api
ossaudio: port to the new audio backend api
paaudio: port to the new audio backend api
sdlaudio: port to the new audio backend api
spiceaudio: port to the new audio backend api
wavaudio: port to the new audio backend api
audio: remove remains of the old backend api
audio: unify input and output mixeng buffer management
audio: common rate control code for timer based outputs
audio: split ctl_* functions into enable_* and volume_*
audio: add mixing-engine option (documentation)
audio: make mixeng optional
paaudio: get/put_buffer functions
audio: support more than two channels in volume setting
audio: replace shift in audio_pcm_info with bytes_per_frame
audio: basic support for multichannel audio
paaudio: channel-map option
usb-audio: do not count on avail bytes actually available
usb-audio: support more than two channels of audio
usbaudio: change playback counters to 64 bit
audio/Makefile.objs | 1 - audio/alsaaudio.c | 372 ++++++++------------------ audio/audio.c | 527 +++++++++++++++++++++++++++++-------- audio/audio.h | 10 + audio/audio_int.h | 81 ++++-- audio/audio_pt_int.c | 173 ------------ audio/audio_pt_int.h | 22 -- audio/audio_template.h | 31 ++- audio/coreaudio.c | 143 +++++----- audio/dsound_template.h | 53 ++-- audio/dsoundaudio.c | 359 +++++++------------------ audio/noaudio.c | 78 +++--- audio/ossaudio.c | 360 ++++++++++--------------- audio/paaudio.c | 566 +++++++++++++--------------------------- audio/sdlaudio.c | 104 ++++---- audio/spiceaudio.c | 256 +++++++----------- audio/wavaudio.c | 78 ++---- configure | 5 - hw/usb/dev-audio.c | 459 +++++++++++++++++++++++++++----- qapi/audio.json | 12 +- qemu-options.hx | 15 ++ 21 files changed, 1697 insertions(+), 2008 deletions(-) delete mode 100644 audio/audio_pt_int.c delete mode 100644 audio/audio_pt_int.h
Signature · 1 line
2.23.0
PATCH v4 1/24 audio: api for mixeng code free backends
This will make it possible to skip mixeng with audio playback and recording, allowing us to free ourselves from the limitations of the current mixeng (stereo, int64 samples only). In this case, HW and SW voices will be essentially the same, for every SW voice we will create a HW voice, since we can no longer mix multiple voices together.
Some backends expect us to call a function when we have data ready write()/read() style, while others provide a buffer and expects us to directly write/read it, so for optimal performance audio_pcm_ops provide methods for both cases. Previously backends asked mixeng for more data in run_out/run_it, now instead mixeng or the frontends will call the backends, so that's why two sets of functions required. audio.c contains glue code between the two styles, so backends only ever have to implement one style and frontends are free to call whichever is more convenient for them.
---
Notes:
Changes from v2:
* audio_pcm_hw_run_out: handle NULL result from get_buffer_out
audio/audio.c | 216 ++++++++++++++++++++++++++++++++++++++++- audio/audio_int.h | 43 ++++++-- audio/audio_template.h | 1 + 3 files changed, 251 insertions(+), 9 deletions(-)
audio/audio.c +214 -2
@@ -573,6 +573,25 @@ size_t audio_pcm_hw_clip_out(HWVoiceOut *hw, void *pcm_buf,return clipped; } +static void audio_pcm_hw_clip_out2 (HWVoiceOut *hw,void *pcm_buf,size_t len) +{ +size_t clipped =0 ; +size_t pos = hw->rpos; + +while (len) { +st_sample *src = hw->mix_buf + pos; +uint8_t *dst =advance (pcm_buf, clipped << hw->info.shift); +size_t samples_till_end_of_buf = hw->samples - pos; +size_t samples_to_clip =MIN (len, samples_till_end_of_buf); + + hw->clip (dst, src, samples_to_clip); + + pos = (pos + samples_to_clip) % hw->samples; + len -= samples_to_clip; + clipped += samples_to_clip; + } +} +/* * Soft voice (capture) */
@@ -1050,6 +1069,36 @@ static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos,mixeng_clear (hw->mix_buf, samples - n); } +static size_t audio_pcm_hw_run_out (HWVoiceOut *hw,size_t live) +{ +size_t clipped =0 ; + +while (live) { +size_t size, decr, proc; +void *buf = hw->pcm_ops->get_buffer_out (hw, &size); +if (!buf) { +/* retrying will likely won't help, drop everything. */ + hw->rpos = (hw->rpos + live) % hw->samples; +return clipped + live; + } + + decr =MIN (size >> hw->info.shift, live); +audio_pcm_hw_clip_out2 (hw, buf, decr); + proc = hw->pcm_ops->put_buffer_out (hw, buf, decr << hw->info.shift) >> + hw->info.shift; + + live -= proc; + clipped += proc; + hw->rpos = (hw->rpos + proc) % hw->samples; + +if (proc ==0 || proc < decr) { +break ; + } + } + +return clipped; +} +static void audio_run_out (AudioState *s) {HWVoiceOut *hw =NULL ;
@@ -1097,7 +1146,11 @@ static void audio_run_out (AudioState *s) } prev_rpos = hw->rpos; - played = hw->pcm_ops->run_out (hw, live); +if (hw->pcm_ops->run_out) { + played = hw->pcm_ops->run_out (hw, live); + }else { + played =audio_pcm_hw_run_out (hw, live); + }replay_audio_out (&played);if (audio_bug (__func__, hw->rpos >= hw->samples)) { dolog("hw->rpos=%zu hw->samples=%zu played=%zu\n",
@@ -1156,6 +1209,35 @@ static void audio_run_out (AudioState *s) } } +static size_t audio_pcm_hw_run_in (HWVoiceIn *hw,size_t samples) +{ +size_t conv =0 ; + +while (samples) { +size_t proc; +size_t size = samples << hw->info.shift; +void *buf = hw->pcm_ops->get_buffer_in (hw, &size); + +assert ((size & hw->info.align) ==0 ); +if (size ==0 ) { + hw->pcm_ops->put_buffer_in (hw, buf, size); +break ; + } + + proc =MIN (size >> hw->info.shift, + hw->samples - hw->wpos); + + hw->conv (hw->conv_buf + hw->wpos, buf, proc); + hw->wpos = (hw->wpos + proc) % hw->samples; + + samples -= proc; + conv += proc; + hw->pcm_ops->put_buffer_in (hw, buf, proc << hw->info.shift); + } + +return conv; +} +static void audio_run_in (AudioState *s) {HWVoiceIn *hw =NULL ;
@@ -1165,7 +1247,12 @@ static void audio_run_in (AudioState *s)size_t captured =0 , min;if (replay_mode !=REPLAY_MODE_PLAY ) { - captured = hw->pcm_ops->run_in (hw); +if (hw->pcm_ops->run_in) { + captured = hw->pcm_ops->run_in (hw); + }else { + captured =audio_pcm_hw_run_in ( + hw, hw->samples -audio_pcm_hw_get_live_in (hw)); + } }replay_audio_in (&captured, hw->conv_buf, &hw->wpos, hw->samples);
@@ -1259,12 +1346,137 @@ void audio_run(AudioState *s, const char *msg)#endif } +void *audio_generic_get_buffer_in (HWVoiceIn *hw,size_t *size) +{ +ssize_t start; + +if (unlikely (!hw->buf_emul)) { +size_t calc_size = hw->samples << hw->info.shift; + hw->buf_emul =g_malloc (calc_size); + hw->size_emul = calc_size; + hw->pos_emul = hw->pending_emul =0 ; + } + +while (hw->pending_emul < hw->size_emul) { +size_t read_len =MIN (hw->size_emul - hw->pos_emul, + hw->size_emul - hw->pending_emul); +size_t read = hw->pcm_ops->read (hw, hw->buf_emul + hw->pos_emul, + read_len); + hw->pending_emul += read; +if (read < read_len) { +break ; + } + } + + start = ((ssize_t ) hw->pos_emul) - hw->pending_emul; +if (start <0 ) { + start += hw->size_emul; + } +assert (start >=0 && start < hw->size_emul); + + *size =MIN (hw->pending_emul, hw->size_emul - start); +return hw->buf_emul + start; +} + +void audio_generic_put_buffer_in (HWVoiceIn *hw,void *buf,size_t size) +{ +assert (size <= hw->pending_emul); + hw->pending_emul -= size; +} + +void *audio_generic_get_buffer_out (HWVoiceOut *hw,size_t *size) +{ +if (unlikely (!hw->buf_emul)) { +size_t calc_size = hw->samples << hw->info.shift; + + hw->buf_emul =g_malloc (calc_size); + hw->size_emul = calc_size; + hw->pos_emul = hw->pending_emul =0 ; + } + + *size =MIN (hw->size_emul - hw->pending_emul, + hw->size_emul - hw->pos_emul); +return hw->buf_emul + hw->pos_emul; +} + +size_t audio_generic_put_buffer_out_nowrite (HWVoiceOut *hw,void *buf, +size_t size) +{ +assert (buf == hw->buf_emul + hw->pos_emul && + size + hw->pending_emul <= hw->size_emul); + + hw->pending_emul += size; + hw->pos_emul = (hw->pos_emul + size) % hw->size_emul; + +return size; +} + +size_t audio_generic_put_buffer_out (HWVoiceOut *hw,void *buf,size_t size) +{ +audio_generic_put_buffer_out_nowrite (hw, buf, size); + +while (hw->pending_emul) { +size_t write_len, written; +ssize_t start = ((ssize_t ) hw->pos_emul) - hw->pending_emul; +if (start <0 ) { + start += hw->size_emul; + } +assert (start >=0 && start < hw->size_emul); + + write_len =MIN (hw->pending_emul, hw->size_emul - start); + + written = hw->pcm_ops->write (hw, hw->buf_emul + start, write_len); + hw->pending_emul -= written; + +if (written < write_len) { +break ; + } + } + +/* +* fake we have written everything. non-written data remain in pending_emul, +* so we do not have to clip them multiple times +*/ +return size; +} + +size_t audio_generic_write (HWVoiceOut *hw,void *buf,size_t size) +{ +size_t dst_size, copy_size; +void *dst = hw->pcm_ops->get_buffer_out (hw, &dst_size); + copy_size =MIN (size, dst_size); + +memcpy (dst, buf, copy_size); +return hw->pcm_ops->put_buffer_out (hw, buf, copy_size); +} + +size_t audio_generic_read (HWVoiceIn *hw,void *buf,size_t size) +{ +size_t dst_size, copy_size; +void *dst = hw->pcm_ops->get_buffer_in (hw, &dst_size); + copy_size =MIN (size, dst_size); + +memcpy (dst, buf, copy_size); + hw->pcm_ops->put_buffer_in (hw, buf, copy_size); +return copy_size; +} + +static int audio_driver_init (AudioState *s,struct audio_driver *drv,bool msg,Audiodev *dev) { s->drv_opaque = drv->init (dev);if (s->drv_opaque) { +if (!drv->pcm_ops->get_buffer_in) { + drv->pcm_ops->get_buffer_in = audio_generic_get_buffer_in; + drv->pcm_ops->put_buffer_in = audio_generic_put_buffer_in; + } +if (!drv->pcm_ops->get_buffer_out) { + drv->pcm_ops->get_buffer_out = audio_generic_get_buffer_out; + drv->pcm_ops->put_buffer_out = audio_generic_put_buffer_out; + } +audio_init_nb_voices_out (s, drv);audio_init_nb_voices_in (s, drv); s->drv = drv;
audio/audio_int.h +36 -7
65 lines not on this page · raw message
audio/audio_template.h +1 -0
@@ -71,6 +71,7 @@ static void glue(audio_init_nb_voices_, TYPE)(AudioState *s,static void glue (audio_pcm_hw_free_resources_ ,TYPE ) (HW *hw) { +g_free (hw->buf_emul);g_free (HWBUF );HWBUF =NULL ; }
Signature · 1 line
2.23.0
PATCH v4 2/24 alsaaudio: port to the new audio backend api
In reply to Kővágó, Zoltán--- audio/alsaaudio.c | 302 ++++++++++++---------------------------------- 1 file changed, 80 insertions(+), 222 deletions(-)
audio/alsaaudio.c +80 -222
@@ -44,9 +44,6 @@ struct pollhlp { typedef struct ALSAVoiceOut { HWVoiceOut hw; - int wpos; - int pending; - void *pcm_buf; snd_pcm_t *handle; struct pollhlp pollhlp; Audiodev *dev;
@@ -55,7 +52,6 @@ typedef struct ALSAVoiceOut {typedef struct ALSAVoiceIn {HWVoiceIn hw;snd_pcm_t *handle; -void *pcm_buf;struct pollhlp pollhlp;Audiodev *dev; }ALSAVoiceIn ;
@@ -602,102 +598,64 @@ static int alsa_open(bool in, struct alsa_params_req *req,return -1 ; } -static snd_pcm_sframes_t alsa_get_avail (snd_pcm_t *handle) +static size_t alsa_write (HWVoiceOut *hw,void *buf,size_t len) { -snd_pcm_sframes_t avail; - - avail =snd_pcm_avail_update (handle); -if (avail <0 ) { -if (avail == -EPIPE ) { -if (!alsa_recover (handle)) { - avail =snd_pcm_avail_update (handle); - } - } - -if (avail <0 ) { -alsa_logerr (avail, -"Could not obtain number of available frames\n" ); -return -1 ; - } - } +ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; +size_t pos =0 ; +size_t len_frames = len >> hw->info.shift; + +while (len_frames) { +char *src =advance (buf, pos); +snd_pcm_sframes_t written; + + written =snd_pcm_writei (alsa->handle, src, len_frames); + +if (written <=0 ) { +switch (written) { +case 0 : +trace_alsa_wrote_zero (len_frames); +return pos; + +case -EPIPE : +if (alsa_recover (alsa->handle)) { +alsa_logerr (written,"Failed to write %zu frames\n" , + len_frames); +return pos; + } +trace_alsa_xrun_out (); +continue ; + +case -ESTRPIPE : +/* +* stream is suspended and waiting for an application +* recovery +*/ +if (alsa_resume (alsa->handle)) { +alsa_logerr (written,"Failed to write %zu frames\n" , + len_frames); +return pos; + } +trace_alsa_resume_out (); +continue ; -return avail; -} +case -EAGAIN : +return pos; -static void alsa_write_pending (ALSAVoiceOut *alsa) -{ -HWVoiceOut *hw = &alsa->hw; - -while (alsa->pending) { -int left_till_end_samples = hw->samples - alsa->wpos; -int len =MIN (alsa->pending, left_till_end_samples); -char *src =advance (alsa->pcm_buf, alsa->wpos << hw->info.shift); - -while (len) { -snd_pcm_sframes_t written; - - written =snd_pcm_writei (alsa->handle, src, len); - -if (written <=0 ) { -switch (written) { -case 0 : -trace_alsa_wrote_zero (len); -return ; - -case -EPIPE : -if (alsa_recover (alsa->handle)) { -alsa_logerr (written,"Failed to write %d frames\n" , - len); -return ; - } -trace_alsa_xrun_out (); -continue ; - -case -ESTRPIPE : -/* stream is suspended and waiting for an -application recovery */ -if (alsa_resume (alsa->handle)) { -alsa_logerr (written,"Failed to write %d frames\n" , - len); -return ; - } -trace_alsa_resume_out (); -continue ; - -case -EAGAIN : -return ; - -default : -alsa_logerr (written,"Failed to write %d frames from %p\n" , - len, src); -return ; - } +default : +alsa_logerr (written,"Failed to write %zu frames from %p\n" , + len, src); +return pos; } - - alsa->wpos = (alsa->wpos + written) % hw->samples; - alsa->pending -= written; - len -= written; } - } -} - -static size_t alsa_run_out (HWVoiceOut *hw,size_t live) -{ -ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw; -size_t decr; -snd_pcm_sframes_t avail; - avail =alsa_get_avail (alsa->handle); -if (avail <0 ) { -dolog ("Could not get number of available playback frames\n" ); -return 0 ; + pos += written << hw->info.shift; +if (written < len_frames) { +break ; + } + len_frames -= written; } - decr =MIN (live, avail); - decr =audio_pcm_hw_clip_out (hw, alsa->pcm_buf, decr, alsa->pending); - alsa->pending += decr; -alsa_write_pending (alsa); -return decr; +return pos; }static void alsa_fini_out (HWVoiceOut *hw)
@@ -706,9 +664,6 @@ static void alsa_fini_out (HWVoiceOut *hw)ldebug ("alsa_fini\n" );alsa_anal_close (&alsa->handle, &alsa->pollhlp); - -g_free (alsa->pcm_buf); - alsa->pcm_buf =NULL ; } static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
@@ -737,14 +692,6 @@ static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,audio_pcm_init_info (&hw->info, &obt_as); hw->samples = obt.samples; - alsa->pcm_buf =audio_calloc (__func__, obt.samples,1 << hw->info.shift); -if (!alsa->pcm_buf) { -dolog ("Could not allocate DAC buffer (%zu samples, each %d bytes)\n" , - hw->samples,1 << hw->info.shift); -alsa_anal_close1 (&handle); -return -1 ; - } - alsa->pollhlp.s = hw->s; alsa->handle = handle; alsa->dev = dev;
@@ -839,14 +786,6 @@ static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)audio_pcm_init_info (&hw->info, &obt_as); hw->samples = obt.samples; - alsa->pcm_buf =audio_calloc (__func__, hw->samples,1 << hw->info.shift); -if (!alsa->pcm_buf) { -dolog ("Could not allocate ADC buffer (%zu samples, each %d bytes)\n" , - hw->samples,1 << hw->info.shift); -alsa_anal_close1 (&handle); -return -1 ; - } - alsa->pollhlp.s = hw->s; alsa->handle = handle; alsa->dev = dev;
@@ -858,129 +797,48 @@ static void alsa_fini_in (HWVoiceIn *hw)ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;alsa_anal_close (&alsa->handle, &alsa->pollhlp); - -g_free (alsa->pcm_buf); - alsa->pcm_buf =NULL ; } -static size_t alsa_run_in (HWVoiceIn *hw) +static size_t alsa_read (HWVoiceIn *hw,void *buf,size_t len) {ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw; -int hwshift = hw->info.shift; -int i; -size_t live =audio_pcm_hw_get_live_in (hw); -size_t dead = hw->samples - live; -size_t decr; -struct { -size_t add; -size_t len; - } bufs[2 ] = { - { .add = hw->wpos, .len =0 }, - { .add =0 , .len =0 } - }; -snd_pcm_sframes_t avail; -snd_pcm_uframes_t read_samples =0 ; - -if (!dead) { -return 0 ; - } +size_t pos =0 ; - avail =alsa_get_avail (alsa->handle); -if (avail <0 ) { -dolog ("Could not get number of captured frames\n" ); -return 0 ; - } - -if (!avail) { -snd_pcm_state_t state; - - state =snd_pcm_state (alsa->handle); -switch (state) { -case SND_PCM_STATE_PREPARED : - avail = hw->samples; -break ; -case SND_PCM_STATE_SUSPENDED : -/* stream is suspended and waiting for an application recovery */ -if (alsa_resume (alsa->handle)) { -dolog ("Failed to resume suspended input stream\n" ); -return 0 ; - } -trace_alsa_resume_in (); -break ; -default : -trace_alsa_no_frames (state); -return 0 ; - } - } +while (len) { +void *dst =advance (buf, pos); +snd_pcm_sframes_t nread; - decr =MIN (dead, avail); -if (!decr) { -return 0 ; - } + nread =snd_pcm_readi (alsa->handle, dst, len >> hw->info.shift); -if (hw->wpos + decr > hw->samples) { - bufs[0 ].len = (hw->samples - hw->wpos); - bufs[1 ].len = (decr - (hw->samples - hw->wpos)); - } -else { - bufs[0 ].len = decr; - } +if (nread <=0 ) { +switch (nread) { +case 0 : +trace_alsa_read_zero (len); +return pos;; -for (i =0 ; i <2 ; ++i) { -void *src; -struct st_sample *dst; -snd_pcm_sframes_t nread; -snd_pcm_uframes_t len; - - len = bufs[i].len; - - src =advance (alsa->pcm_buf, bufs[i].add << hwshift); - dst = hw->conv_buf + bufs[i].add; - -while (len) { - nread =snd_pcm_readi (alsa->handle, src, len); - -if (nread <=0 ) { -switch (nread) { -case 0 : -trace_alsa_read_zero (len); - goto exit; - -case -EPIPE : -if (alsa_recover (alsa->handle)) { -alsa_logerr (nread,"Failed to read %ld frames\n" , len); - goto exit; - } -trace_alsa_xrun_in (); -continue ; - -case -EAGAIN : - goto exit; - -default : -alsa_logerr ( - nread, -"Failed to read %ld frames from %p\n" , - len, - src - ); - goto exit; +case -EPIPE : +if (alsa_recover (alsa->handle)) { +alsa_logerr (nread,"Failed to read %zu frames\n" , len); +return pos; } - } +trace_alsa_xrun_in (); +continue ; - hw->conv (dst, src, nread); +case -EAGAIN : +return pos; - src =advance (src, nread << hwshift); - dst += nread; - - read_samples += nread; - len -= nread; +default : +alsa_logerr (nread,"Failed to read %zu frames to %p\n" , + len, dst); +return pos;; + } } + + pos += nread << hw->info.shift; + len -= nread << hw->info.shift; } - exit: - hw->wpos = (hw->wpos + read_samples) % hw->samples; -return read_samples; +return pos; }static int alsa_ctl_in (HWVoiceIn *hw,int cmd, ...)
@@ -1065,12 +923,12 @@ static void alsa_audio_fini (void *opaque)static struct audio_pcm_ops alsa_pcm_ops = { .init_out = alsa_init_out, .fini_out = alsa_fini_out, - .run_out = alsa_run_out, + .write = alsa_write, .ctl_out = alsa_ctl_out, .init_in = alsa_init_in, .fini_in = alsa_fini_in, - .run_in = alsa_run_in, + .read = alsa_read, .ctl_in = alsa_ctl_in, };
Signature · 1 line
2.23.0
PATCH v4 7/24 paaudio: port to the new audio backend api
In reply to Kővágó, Zoltán--- audio/Makefile.objs | 1 - audio/audio_pt_int.c | 173 -------------------- audio/audio_pt_int.h | 22 --- audio/paaudio.c | 372 ++++++------------------------------------- configure | 5 - 5 files changed, 45 insertions(+), 528 deletions(-) delete mode 100644 audio/audio_pt_int.c delete mode 100644 audio/audio_pt_int.h
audio/Makefile.objs +0 -1
@@ -2,7 +2,6 @@ common-obj-y = audio.o audio_legacy.o noaudio.o wavaudio.o mixeng.o common-obj-$(CONFIG_SPICE) += spiceaudio.o common-obj-$(CONFIG_AUDIO_COREAUDIO) += coreaudio.o common-obj-$(CONFIG_AUDIO_DSOUND) += dsoundaudio.o -common-obj-$(CONFIG_AUDIO_PT_INT) += audio_pt_int.o common-obj-$(CONFIG_AUDIO_WIN_INT) += audio_win_int.o common-obj-y += wavcapture.o
audio/audio_pt_int.c deleted +0 -173
173 lines not on this page · raw message
audio/audio_pt_int.h deleted +0 -22
@@ -1,22 +0,0 @@ -#ifndef QEMU_AUDIO_PT_INT_H -#define QEMU_AUDIO_PT_INT_H - -#include <pthread.h> - -struct audio_pt { -const char *drv; -pthread_t thread; -pthread_cond_t cond; -pthread_mutex_t mutex; -}; - -int audio_pt_init (struct audio_pt *,void *(*) (void *),void *, -const char *,const char *); -int audio_pt_fini (struct audio_pt *,const char *); -int audio_pt_lock (struct audio_pt *,const char *); -int audio_pt_unlock (struct audio_pt *,const char *); -int audio_pt_wait (struct audio_pt *,const char *); -int audio_pt_unlock_and_signal (struct audio_pt *,const char *); -int audio_pt_join (struct audio_pt *,void **,const char *); - -#endif /* QEMU_AUDIO_PT_INT_H */
audio/paaudio.c +45 -327
509 lines not on this page · raw message
configure +0 -5
@@ -297,7 +297,6 @@ host_cc="cc" libs_cpu="" libs_softmmu="" libs_tools="" -audio_pt_int="" audio_win_int="" libs_qga="" debug_info="yes"
@@ -3383,7 +3382,6 @@ for drv in $audio_drv_list; do pa | try-pa) if $pkg_config libpulse --exists; then pulse_libs=$($pkg_config libpulse --libs) - audio_pt_int="yes" if test "$drv" = "try-pa"; then audio_drv_list=$(echo "$audio_drv_list" | sed -e 's/try-pa/pa/') fi
@@ -6606,9 +6604,6 @@ echo "PULSE_LIBS=$pulse_libs" >> $config_host_mak echo "COREAUDIO_LIBS=$coreaudio_libs" >> $config_host_mak echo "DSOUND_LIBS=$dsound_libs" >> $config_host_mak echo "OSS_LIBS=$oss_libs" >> $config_host_mak -if test "$audio_pt_int" = "yes" ; then - echo "CONFIG_AUDIO_PT_INT=y" >> $config_host_mak -fi if test "$audio_win_int" = "yes" ; then echo "CONFIG_AUDIO_WIN_INT=y" >> $config_host_mak fi
Signature · 1 line
2.23.0
PATCH v4 5/24 noaudio: port to the new audio backend api
In reply to Kővágó, Zoltán--- audio/noaudio.c | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-)
audio/noaudio.c +17 -26
@@ -41,10 +41,9 @@ typedef struct NoVoiceIn {int64_t old_ticks; } NoVoiceIn; -static size_t no_run_out (HWVoiceOut *hw,size_t live) +static size_t no_write (HWVoiceOut *hw,void *buf,size_t len) {NoVoiceOut *no = (NoVoiceOut *) hw; -size_t decr, samples;int64_t now;int64_t ticks;int64_t bytes;
@@ -52,13 +51,9 @@ static size_t no_run_out(HWVoiceOut *hw, size_t live) now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); ticks = now - no->old_ticks; bytes =muldiv64 (ticks, hw->info.bytes_per_second,NANOSECONDS_PER_SECOND ); - bytes =MIN (bytes,SIZE_MAX ); - samples = bytes >> hw->info.shift; no->old_ticks = now; - decr =MIN (live, samples); - hw->rpos = (hw->rpos + decr) % hw->samples; -return decr; +return MIN (len, bytes); }static int no_init_out (HWVoiceOut *hw,struct audsettings *as,void *drv_opaque)
@@ -92,25 +87,21 @@ static void no_fini_in (HWVoiceIn *hw) (void ) hw; } -static size_t no_run_in (HWVoiceIn *hw) +static size_t no_read (HWVoiceIn *hw,void *buf,size_t size) { +size_t to_clear;NoVoiceIn *no = (NoVoiceIn *) hw; -size_t live =audio_pcm_hw_get_live_in (hw); -size_t dead = hw->samples - live; -size_t samples =0 ; - -if (dead) { -int64_t now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); -int64_t ticks = now - no->old_ticks; -int64_t bytes = -muldiv64 (ticks, hw->info.bytes_per_second,NANOSECONDS_PER_SECOND ); - - no->old_ticks = now; - bytes =MIN (bytes,SIZE_MAX ); - samples = bytes >> hw->info.shift; - samples =MIN (samples, dead); - } -return samples; + +int64_t now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); +int64_t ticks = now - no->old_ticks; +int64_t bytes = +muldiv64 (ticks, hw->info.bytes_per_second,NANOSECONDS_PER_SECOND ); + + no->old_ticks = now; + to_clear =MIN (bytes, size); + +audio_pcm_info_clear_buf (&hw->info, buf, to_clear >> hw->info.shift); +return to_clear; }static int no_ctl_in (HWVoiceIn *hw,int cmd, ...)
@@ -133,12 +124,12 @@ static void no_audio_fini (void *opaque)static struct audio_pcm_ops no_pcm_ops = { .init_out = no_init_out, .fini_out = no_fini_out, - .run_out = no_run_out, + .write = no_write, .ctl_out = no_ctl_out, .init_in = no_init_in, .fini_in = no_fini_in, - .run_in = no_run_in, + .read = no_read, .ctl_in = no_ctl_in };
Signature · 1 line
2.23.0
PATCH v4 3/24 coreaudio: port to the new audio backend api
In reply to Kővágó, Zoltán--- audio/coreaudio.c | 130 ++++++++++++++++++++++++---------------------- 1 file changed, 69 insertions(+), 61 deletions(-)
audio/coreaudio.c +69 -61
@@ -43,9 +43,6 @@ typedef struct coreaudioVoiceOut {UInt32 audioDevicePropertyBufferFrameSize;AudioStreamBasicDescription outputStreamBasicDescription;AudioDeviceIOProcID ioprocid; -size_t live; -size_t decr; -size_t rpos; } coreaudioVoiceOut;#if MAC_OS_X_VERSION_MAX_ALLOWED >=MAC_OS_X_VERSION_10_6
@@ -397,31 +394,29 @@ static int coreaudio_unlock (coreaudioVoiceOut *core, const char *fn_name)return 0 ; } -static size_t coreaudio_run_out (HWVoiceOut *hw,size_t live) -{ -size_t decr; -coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; - -if (coreaudio_lock (core,"coreaudio_run_out" )) { -return 0 ; +#define COREAUDIO_WRAPPER_FUNC (name, ret_type, args_decl, args) \ + static ret_type glue(coreaudio_, name)args_decl \ + { \ + coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; \ + ret_type ret; \ + \ + if (coreaudio_lock(core, "coreaudio_" #name)) { \ + return 0; \ + } \ + \ + ret = glue(audio_generic_, name)args; \ + \ + coreaudio_unlock(core, "coreaudio_" #name); \ + return ret; \ } - -if (core->decr > live) { -ldebug ("core->decr %d live %d core->live %d\n" , - core->decr, - live, - core->live); - } - - decr =MIN (core->decr, live); - core->decr -= decr; - - core->live = live - decr; - hw->rpos = core->rpos; - -coreaudio_unlock (core,"coreaudio_run_out" ); -return decr; -} +COREAUDIO_WRAPPER_FUNC(get_buffer_out, void *, (HWVoiceOut *hw, size_t *size), + (hw, size)) +COREAUDIO_WRAPPER_FUNC (put_buffer_out_nowrite, size_t, + (HWVoiceOut *hw, void *buf, size_t size), + (hw, buf, size)) +COREAUDIO_WRAPPER_FUNC(write, size_t, (HWVoiceOut *hw, void *buf, size_t size), + (hw, buf, size)) +#undef COREAUDIO_WRAPPER_FUNC/* callback to feed audiooutput buffer */ static OSStatus audioDeviceIOProc(
@@ -433,19 +428,11 @@ static OSStatus audioDeviceIOProc(const AudioTimeStamp * inOutputTime, void* hwptr) { - UInt32 frame, frameCount; -float *out = outOutputData->mBuffers[0 ].mData; + UInt32 frameCount, pending_frames; +void *out = outOutputData->mBuffers[0 ].mData;HWVoiceOut *hw = hwptr;coreaudioVoiceOut *core = (coreaudioVoiceOut *) hwptr; -int rpos, live; -struct st_sample *src; -#ifndef FLOAT_MIXENG -#ifdef RECIPROCAL -const float scale =1.f /UINT_MAX ; -#else -const float scale =UINT_MAX ; -#endif -#endif +size_t len;if (coreaudio_lock (core,"audioDeviceIOProc" )) { inInputTime =0 ;
@@ -453,42 +440,51 @@ static OSStatus audioDeviceIOProc( } frameCount = core->audioDevicePropertyBufferFrameSize; - live = core->live; + pending_frames = hw->pending_emul >> hw->info.shift;/* if there are not enough samples, set signal and return */ -if (live < frameCount) { +if (pending_frames < frameCount) { inInputTime =0 ;coreaudio_unlock (core,"audioDeviceIOProc(empty)" );return 0 ; } - rpos = core->rpos; - src = hw->mix_buf + rpos; + len = frameCount << hw->info.shift; +while (len) { +size_t write_len; +ssize_t start = ((ssize_t ) hw->pos_emul) - hw->pending_emul; +if (start <0 ) { + start += hw->size_emul; + } +assert (start >=0 && start < hw->size_emul); -/* fill buffer */ -for (frame =0 ; frame < frameCount; frame++) { -#ifdef FLOAT_MIXENG - *out++ = src[frame].l;/* left channel */ - *out++ = src[frame].r;/* right channel */ -#else -#ifdef RECIPROCAL - *out++ = src[frame].l * scale;/* left channel */ - *out++ = src[frame].r * scale;/* right channel */ -#else - *out++ = src[frame].l / scale;/* left channel */ - *out++ = src[frame].r / scale;/* right channel */ -#endif -#endif - } + write_len =MIN (MIN (hw->pending_emul, len), + hw->size_emul - start); - rpos = (rpos + frameCount) % hw->samples; - core->decr += frameCount; - core->rpos = rpos; +memcpy (out, hw->buf_emul + start, write_len); + hw->pending_emul -= write_len; + len -= write_len; + out += write_len; + }coreaudio_unlock (core,"audioDeviceIOProc" );return 0 ; } +static UInt32 coreaudio_get_flags (struct audio_pcm_info *info, +struct audsettings *as) +{ +UInt32 flags = info->sign ? kAudioFormatFlagIsSignedInteger :0 ; +if (as->endianness) {/* 0 = little, 1 = big */ + flags |= kAudioFormatFlagIsBigEndian; + } + +if (flags ==0 ) {/* must not be 0 */ + flags = kAudioFormatFlagsAreAllClear; + } +return flags; +} +static int coreaudio_init_out (HWVoiceOut *hw,struct audsettings *as,void *drv_opaque) {
@@ -576,6 +572,16 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,/* set Samplerate */ core->outputStreamBasicDescription.mSampleRate = (Float64 ) as->freq; + core->outputStreamBasicDescription.mFormatID = kAudioFormatLinearPCM; + core->outputStreamBasicDescription.mFormatFlags = +coreaudio_get_flags (&hw->info, as); + core->outputStreamBasicDescription.mBytesPerPacket = + core->outputStreamBasicDescription.mBytesPerFrame = + hw->info.nchannels * hw->info.bits /8 ; + core->outputStreamBasicDescription.mFramesPerPacket =1 ; + core->outputStreamBasicDescription.mChannelsPerFrame = hw->info.nchannels; + core->outputStreamBasicDescription.mBitsPerChannel = hw->info.bits; + status =coreaudio_set_streamformat (core->outputDeviceID, &core->outputStreamBasicDescription);if (status != kAudioHardwareNoError) {
@@ -686,7 +692,9 @@ static void coreaudio_audio_fini (void *opaque)static struct audio_pcm_ops coreaudio_pcm_ops = { .init_out = coreaudio_init_out, .fini_out = coreaudio_fini_out, - .run_out = coreaudio_run_out, + .write = coreaudio_write, + .get_buffer_out = coreaudio_get_buffer_out, + .put_buffer_out = coreaudio_put_buffer_out_nowrite, .ctl_out = coreaudio_ctl_out };
Signature · 1 line
2.23.0
PATCH v4 4/24 dsoundaudio: port to the new audio backend api
In reply to Kővágó, Zoltán--- audio/dsound_template.h | 47 ++++--- audio/dsoundaudio.c | 305 +++++++++------------------------------- 2 files changed, 91 insertions(+), 261 deletions(-)
audio/dsound_template.h +24 -23
@@ -29,6 +29,8 @@#define BUFPTR LPDIRECTSOUNDCAPTUREBUFFER#define FIELD dsound_capture_buffer#define FIELD2 dsound_capture +#define HWVOICE HWVoiceIn +#define DSOUNDVOICE DSoundVoiceIn#else #define NAME "playback buffer"#define NAME2 "DirectSound"
@@ -37,6 +39,8 @@#define BUFPTR LPDIRECTSOUNDBUFFER#define FIELD dsound_buffer#define FIELD2 dsound +#define HWVOICE HWVoiceOut +#define DSOUNDVOICE DSoundVoiceOut#endif static int glue (dsound_unlock_, TYPE) (
@@ -72,8 +76,6 @@ static int glue (dsound_lock_, TYPE) ( ) {HRESULT hr; -LPVOID p1 =NULL , p2 =NULL ; -DWORD blen1 =0 , blen2 =0 ;DWORD flag;#ifdef DSBTYPE_IN
@@ -81,7 +83,7 @@ static int glue (dsound_lock_, TYPE) (#else flag = entire ?DSBLOCK_ENTIREBUFFER :0 ;#endif - hr =glue (IFACE , _Lock)(buf, pos, len, &p1, &blen1, &p2, &blen2, flag); + hr =glue (IFACE , _Lock)(buf, pos, len, p1p, blen1p, p2p, blen2p, flag);if (FAILED (hr)) {#ifndef DSBTYPE_IN
@@ -96,34 +98,34 @@ static int glue (dsound_lock_, TYPE) ( goto fail; } -if ((p1 && (blen1 & info->align)) || (p2 && (blen2 & info->align))) { -dolog ("DirectSound returned misaligned buffer %ld %ld\n" , - blen1, blen2); -glue (dsound_unlock_,TYPE ) (buf, p1, p2, blen1, blen2); +if ((p1p && *p1p && (*blen1p & info->align)) || + (p2p && *p2p && (*blen2p & info->align))) { +dolog ("DirectSound returned misaligned buffer %ld %ld\n" , + *blen1p, *blen2p); +glue (dsound_unlock_,TYPE )(buf, *p1p, p2p ? *p2p :NULL , *blen1p, + blen2p ? *blen2p :0 ); goto fail; } -if (!p1 && blen1) { -dolog ("warning: !p1 && blen1=%ld\n" , blen1); - blen1 =0 ; +if (p1p && !*p1p && *blen1p) { +dolog ("warning: !p1 && blen1=%ld\n" , *blen1p); + *blen1p =0 ; } -if (!p2 && blen2) { -dolog ("warning: !p2 && blen2=%ld\n" , blen2); - blen2 =0 ; +if (p2p && !*p2p && *blen2p) { +dolog ("warning: !p2 && blen2=%ld\n" , *blen2p); + *blen2p =0 ; } - *p1p = p1; - *p2p = p2; - *blen1p = blen1; - *blen2p = blen2;return 0 ; fail: *p1p =NULL -1 ; - *p2p =NULL -1 ; *blen1p =-1 ; - *blen2p =-1 ; +if (p2p) { + *p2p =NULL -1 ; + *blen2p =-1 ; + }return -1 ; }
@@ -242,7 +244,6 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as, goto fail0; } - ds->first_time =1 ; obt_as.endianness =0 ;audio_pcm_init_info (&hw->info, &obt_as);
@@ -252,15 +253,13 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as, bc.dwBufferBytes, hw->info.align +1 ); } + hw->size_emul = bc.dwBufferBytes; hw->samples = bc.dwBufferBytes >> hw->info.shift; ds->s = s;#ifdef DEBUG_DSOUND dolog ("caps %ld, desc %ld\n" , bc.dwBufferBytes, bd.dwBufferBytes); - -dolog ("bufsize %d, freq %d, chan %d, fmt %d\n" , - hw->bufsize, settings.freq, settings.nchannels, settings.fmt);#endif return 0 ;
@@ -276,3 +275,5 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,#undef BUFPTR#undef FIELD#undef FIELD2 +#undef HWVOICE +#undef DSOUNDVOICE
audio/dsoundaudio.c +67 -238
384 lines not on this page · raw message
Signature · 1 line
2.23.0
PATCH v4 20/24 audio: basic support for multichannel audio
In reply to Kővágó, ZoltánWhich currently only means removing some checks. Old code won't require more than two channels, but new code will need it.
--- audio/alsaaudio.c | 7 ------- audio/audio.c | 2 +- 2 files changed, 1 insertion(+), 8 deletions(-)
audio/alsaaudio.c +0 -7
@@ -493,13 +493,6 @@ static int alsa_open(bool in, struct alsa_params_req *req, goto err; } -if (nchannels !=1 && nchannels !=2 ) { -alsa_logerr2 (err, typ, -"Can not handle obtained number of channels %d\n" , - nchannels); - goto err; - } -if (apdo->buffer_length) {int dir =0 ;unsigned int btime = apdo->buffer_length;
audio/audio.c +1 -1
@@ -242,7 +242,7 @@ static int audio_validate_settings (struct audsettings *as) {int invalid; - invalid = as->nchannels !=1 && as->nchannels !=2 ; + invalid = as->nchannels <1 ; invalid |= as->endianness !=0 && as->endianness !=1 ;switch (as->fmt) {
Signature · 1 line
2.23.0
PATCH v4 11/24 audio: remove remains of the old backend api
In reply to Kővágó, Zoltán--- audio/audio.c | 42 ++++++------------------------------------ audio/audio_int.h | 7 ------- 2 files changed, 6 insertions(+), 43 deletions(-)
audio/audio.c +6 -36
@@ -541,7 +541,7 @@ static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw)return m; } -size_t audio_pcm_hw_get_live_in (HWVoiceIn *hw) +static size_t audio_pcm_hw_get_live_in (HWVoiceIn *hw) {size_t live = hw->total_samples_captured -audio_pcm_hw_find_min_in (hw);if (audio_bug (__func__, live > hw->samples)) {
@@ -551,29 +551,7 @@ size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw)return live; } -size_t audio_pcm_hw_clip_out (HWVoiceOut *hw,void *pcm_buf, -size_t live,size_t pending) -{ -size_t left = hw->samples - pending; -size_t len =MIN (left, live); -size_t clipped =0 ; - -while (len) { -struct st_sample *src = hw->mix_buf + hw->rpos; -uint8_t *dst =advance (pcm_buf, hw->rpos << hw->info.shift); -size_t samples_till_end_of_buf = hw->samples - hw->rpos; -size_t samples_to_clip =MIN (len, samples_till_end_of_buf); - - hw->clip (dst, src, samples_to_clip); - - hw->rpos = (hw->rpos + samples_to_clip) % hw->samples; - len -= samples_to_clip; - clipped += samples_to_clip; - } -return clipped; -} - -static void audio_pcm_hw_clip_out2 (HWVoiceOut *hw,void *pcm_buf,size_t len) +static void audio_pcm_hw_clip_out (HWVoiceOut *hw,void *pcm_buf,size_t len) {size_t clipped =0 ;size_t pos = hw->rpos;
@@ -1083,7 +1061,7 @@ static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live) } decr =MIN (size >> hw->info.shift, live); -audio_pcm_hw_clip_out2 (hw, buf, decr); +audio_pcm_hw_clip_out (hw, buf, decr); proc = hw->pcm_ops->put_buffer_out (hw, buf, decr << hw->info.shift) >> hw->info.shift;
@@ -1146,11 +1124,7 @@ static void audio_run_out (AudioState *s) } prev_rpos = hw->rpos; -if (hw->pcm_ops->run_out) { - played = hw->pcm_ops->run_out (hw, live); - }else { - played =audio_pcm_hw_run_out (hw, live); - } + played =audio_pcm_hw_run_out (hw, live);replay_audio_out (&played);if (audio_bug (__func__, hw->rpos >= hw->samples)) { dolog("hw->rpos=%zu hw->samples=%zu played=%zu\n",
@@ -1247,12 +1221,8 @@ static void audio_run_in (AudioState *s)size_t captured =0 , min;if (replay_mode !=REPLAY_MODE_PLAY ) { -if (hw->pcm_ops->run_in) { - captured = hw->pcm_ops->run_in (hw); - }else { - captured =audio_pcm_hw_run_in ( - hw, hw->samples -audio_pcm_hw_get_live_in (hw)); - } + captured =audio_pcm_hw_run_in ( + hw, hw->samples -audio_pcm_hw_get_live_in (hw)); }replay_audio_in (&captured, hw->conv_buf, &hw->wpos, hw->samples);
audio/audio_int.h +0 -7
@@ -153,7 +153,6 @@ struct audio_driver {struct audio_pcm_ops {int (*init_out)(HWVoiceOut *hw,audsettings *as,void *drv_opaque);void (*fini_out)(HWVoiceOut *hw); -size_t (*run_out)(HWVoiceOut *hw,size_t live);size_t (*write) (HWVoiceOut *hw,void *buf,size_t size);/* * get a buffer that after later can be passed to put_buffer_out; optional
@@ -171,7 +170,6 @@ struct audio_pcm_ops {int (*init_in) (HWVoiceIn *hw,audsettings *as,void *drv_opaque);void (*fini_in) (HWVoiceIn *hw); -size_t (*run_in)(HWVoiceIn *hw);size_t (*read) (HWVoiceIn *hw,void *buf,size_t size);void *(*get_buffer_in)(HWVoiceIn *hw,size_t *size);void (*put_buffer_in)(HWVoiceIn *hw,void *buf,size_t size);
@@ -237,11 +235,6 @@ audio_driver *audio_driver_lookup(const char *name);void audio_pcm_init_info (struct audio_pcm_info *info,struct audsettings *as);void audio_pcm_info_clear_buf (struct audio_pcm_info *info,void *buf,int len); -size_t audio_pcm_hw_get_live_in (HWVoiceIn *hw); - -size_t audio_pcm_hw_clip_out (HWVoiceOut *hw,void *pcm_buf, -size_t live,size_t pending); -int audio_bug (const char *funcname,int cond);void *audio_calloc (const char *funcname,int nmemb,size_t size);
Signature · 1 line
2.23.0
PATCH v4 6/24 ossaudio: port to the new audio backend api
In reply to Kővágó, Zoltán--- audio/ossaudio.c | 272 +++++++++++++++++------------------------------ 1 file changed, 96 insertions(+), 176 deletions(-)
audio/ossaudio.c +96 -176
@@ -40,19 +40,15 @@typedef struct OSSVoiceOut {HWVoiceOut hw; -void *pcm_buf;int fd; -int wpos;int nfrags;int fragsize;int mmapped; -int pending;Audiodev *dev; }OSSVoiceOut ; typedef struct OSSVoiceIn { HWVoiceIn hw; - void *pcm_buf; int fd; int nfrags; int fragsize;
@@ -371,98 +367,87 @@ static int oss_open(int in, struct oss_params *req, audsettings *as,return -1 ; } -static void oss_write_pending (OSSVoiceOut *oss) +static size_t oss_get_available_bytes (OSSVoiceOut *oss) { -HWVoiceOut *hw = &oss->hw; +int err; +struct count_info cntinfo; +assert (oss->mmapped); -if (oss->mmapped) { -return ; + err =ioctl (oss->fd,SNDCTL_DSP_GETOPTR , &cntinfo); +if (err <0 ) { +oss_logerr (errno,"SNDCTL_DSP_GETOPTR failed\n" ); +return 0 ; } -while (oss->pending) { -int samples_written; -ssize_t bytes_written; -int samples_till_end = hw->samples - oss->wpos; -int samples_to_write =MIN (oss->pending, samples_till_end); -int bytes_to_write = samples_to_write << hw->info.shift; -void *pcm =advance (oss->pcm_buf, oss->wpos << hw->info.shift); +return audio_ring_dist (cntinfo.ptr, oss->hw.pos_emul, oss->hw.size_emul); +} - bytes_written =write (oss->fd, pcm, bytes_to_write); -if (bytes_written <0 ) { -if (errno !=EAGAIN ) { -oss_logerr (errno,"failed to write %d bytes\n" , - bytes_to_write); - } -break ; - } +static void *oss_get_buffer_out (HWVoiceOut *hw,size_t *size) +{ +OSSVoiceOut *oss = (OSSVoiceOut *) hw; +if (oss->mmapped) { + *size =MIN (oss_get_available_bytes (oss), hw->size_emul - hw->pos_emul); +return hw->buf_emul + hw->pos_emul; + }else { +return audio_generic_get_buffer_out (hw, size); + } +} -if (bytes_written & hw->info.align) { -dolog ("misaligned write asked for %d, but got %zd\n" , - bytes_to_write, bytes_written); -return ; - } +static size_t oss_put_buffer_out (HWVoiceOut *hw,void *buf,size_t size) +{ +OSSVoiceOut *oss = (OSSVoiceOut *) hw; +if (oss->mmapped) { +assert (buf == hw->buf_emul + hw->pos_emul && size < hw->size_emul); - samples_written = bytes_written >> hw->info.shift; - oss->pending -= samples_written; - oss->wpos = (oss->wpos + samples_written) % hw->samples; -if (bytes_written - bytes_to_write) { -break ; - } + hw->pos_emul = (hw->pos_emul + size) % hw->size_emul; +return size; + }else { +return audio_generic_put_buffer_out (hw, buf, size); } } -static size_t oss_run_out (HWVoiceOut *hw,size_t live) +static size_t oss_write (HWVoiceOut *hw,void *buf,size_t len) {OSSVoiceOut *oss = (OSSVoiceOut *) hw; -int err; -size_t decr; -struct audio_buf_info abinfo; -struct count_info cntinfo; -size_t bufsize; - - bufsize = hw->samples << hw->info.shift; +size_t pos;if (oss->mmapped) { -int bytes, pos; +size_t total_len; + len =MIN (len,oss_get_available_bytes (oss)); - err =ioctl (oss->fd,SNDCTL_DSP_GETOPTR , &cntinfo); -if (err <0 ) { -oss_logerr (errno,"SNDCTL_DSP_GETOPTR failed\n" ); -return 0 ; - } + total_len = len; +while (len) { +size_t to_copy =MIN (len, hw->size_emul - hw->pos_emul); +memcpy (hw->buf_emul + hw->pos_emul, buf, to_copy); - pos = hw->rpos << hw->info.shift; - bytes =audio_ring_dist (cntinfo.ptr, pos, bufsize); - decr =MIN (bytes >> hw->info.shift, live); - } -else { - err =ioctl (oss->fd,SNDCTL_DSP_GETOSPACE , &abinfo); -if (err <0 ) { -oss_logerr (errno,"SNDCTL_DSP_GETOPTR failed\n" ); -return 0 ; + hw->pos_emul = (hw->pos_emul + to_copy) % hw->pos_emul; + buf += to_copy; + len -= to_copy; } +return total_len; + } -if (abinfo.bytes > bufsize) { -trace_oss_invalid_available_size (abinfo.bytes, bufsize); - abinfo.bytes = bufsize; - } + pos =0 ; +while (len) { +ssize_t bytes_written; +void *pcm =advance (buf, pos); -if (abinfo.bytes <0 ) { -trace_oss_invalid_available_size (abinfo.bytes, bufsize); -return 0 ; + bytes_written =write (oss->fd, pcm, len); +if (bytes_written <0 ) { +if (errno !=EAGAIN ) { +oss_logerr (errno,"failed to write %zu bytes\n" , + len); + } +return pos; } - decr =MIN (abinfo.bytes >> hw->info.shift, live); -if (!decr) { -return 0 ; + pos += bytes_written; +if (bytes_written < len) { +break ; } + len -= bytes_written; } - - decr =audio_pcm_hw_clip_out (hw, oss->pcm_buf, decr, oss->pending); - oss->pending += decr; -oss_write_pending (oss); - -return decr; +return pos; }static void oss_fini_out (HWVoiceOut *hw)
@@ -473,18 +458,13 @@ static void oss_fini_out (HWVoiceOut *hw)ldebug ("oss_fini\n" );oss_anal_close (&oss->fd); -if (oss->pcm_buf) { -if (oss->mmapped) { - err =munmap (oss->pcm_buf, hw->samples << hw->info.shift); -if (err) { -oss_logerr (errno,"Failed to unmap buffer %p, size %zu\n" , - oss->pcm_buf, hw->samples << hw->info.shift); - } - } -else { -g_free (oss->pcm_buf); +if (oss->mmapped && hw->buf_emul) { + err =munmap (hw->buf_emul, hw->size_emul); +if (err) { +oss_logerr (errno,"Failed to unmap buffer %p, size %zu\n" , + hw->buf_emul, hw->size_emul); } - oss->pcm_buf =NULL ; + hw->buf_emul =NULL ; } }
@@ -535,19 +515,20 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as, oss->mmapped =0 ;if (oopts->has_try_mmap && oopts->try_mmap) { - oss->pcm_buf =mmap ( + hw->size_emul = hw->samples << hw->info.shift; + hw->buf_emul =mmap (NULL , - hw->samples << hw->info.shift, + hw->size_emul,PROT_READ |PROT_WRITE ,MAP_SHARED , fd,0 ); -if (oss->pcm_buf ==MAP_FAILED ) { +if (hw->buf_emul ==MAP_FAILED ) {oss_logerr (errno,"Failed to map %zu bytes of DAC\n" , - hw->samples << hw->info.shift); - } -else { + hw->size_emul); + hw->buf_emul =NULL ; + }else {int err;int trig =0 ;if (ioctl (fd,SNDCTL_DSP_SETTRIGGER , &trig) <0 ) {
@@ -567,30 +548,16 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as, }if (!oss->mmapped) { - err =munmap (oss->pcm_buf, hw->samples << hw->info.shift); + err =munmap (hw->buf_emul, hw->size_emul);if (err) {oss_logerr (errno,"Failed to unmap buffer %p size %zu\n" , - oss->pcm_buf, hw->samples << hw->info.shift); + hw->buf_emul, hw->size_emul); } + hw->buf_emul =NULL ; } } } -if (!oss->mmapped) { - oss->pcm_buf =audio_calloc (__func__, - hw->samples, -1 << hw->info.shift); -if (!oss->pcm_buf) { -dolog ( -"Could not allocate DAC buffer (%zu samples, each %d bytes)\n" , - hw->samples, -1 << hw->info.shift - ); -oss_anal_close (&fd); -return -1 ; - } - } - oss->fd = fd; oss->dev = dev;return 0 ;
@@ -618,7 +585,7 @@ static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)return 0 ; } -audio_pcm_info_clear_buf (&hw->info, oss->pcm_buf, hw->samples); +audio_pcm_info_clear_buf (&hw->info, hw->buf_emul, hw->samples); trig =PCM_ENABLE_OUTPUT ;if (ioctl (oss->fd,SNDCTL_DSP_SETTRIGGER , &trig) <0 ) { oss_logerr (
@@ -692,13 +659,6 @@ static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) } hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift; - oss->pcm_buf =audio_calloc (__func__, hw->samples,1 << hw->info.shift); -if (!oss->pcm_buf) { -dolog ("Could not allocate ADC buffer (%zu samples, each %d bytes)\n" , - hw->samples,1 << hw->info.shift); -oss_anal_close (&fd); -return -1 ; - } oss->fd = fd; oss->dev = dev;
@@ -710,78 +670,36 @@ static void oss_fini_in (HWVoiceIn *hw)OSSVoiceIn *oss = (OSSVoiceIn *) hw;oss_anal_close (&oss->fd); - -g_free (oss->pcm_buf); - oss->pcm_buf =NULL ; } -static size_t oss_run_in (HWVoiceIn *hw) +static size_t oss_read (HWVoiceIn *hw,void *buf,size_t len) {OSSVoiceIn *oss = (OSSVoiceIn *) hw; -int hwshift = hw->info.shift; -int i; -size_t live =audio_pcm_hw_get_live_in (hw); -size_t dead = hw->samples - live; -size_t read_samples =0 ; -struct { -size_t add; -size_t len; - } bufs[2 ] = { - { .add = hw->wpos, .len =0 }, - { .add =0 , .len =0 } - }; - -if (!dead) { -return 0 ; - } - -if (hw->wpos + dead > hw->samples) { - bufs[0 ].len = (hw->samples - hw->wpos) << hwshift; - bufs[1 ].len = (dead - (hw->samples - hw->wpos)) << hwshift; - } -else { - bufs[0 ].len = dead << hwshift; - } +size_t pos =0 ; -for (i =0 ; i <2 ; ++i) { +while (len) {ssize_t nread; -if (bufs[i].len) { -void *p =advance (oss->pcm_buf, bufs[i].add << hwshift); - nread =read (oss->fd, p, bufs[i].len); - -if (nread >0 ) { -if (nread & hw->info.align) { -dolog ("warning: Misaligned read %zd (requested %zu), " -"alignment %d\n" , nread, bufs[i].add << hwshift, - hw->info.align +1 ); - } - read_samples += nread >> hwshift; - hw->conv (hw->conv_buf + bufs[i].add, p, nread >> hwshift); - } +void *dst =advance (buf, pos); + nread =read (oss->fd, dst, len); -if (bufs[i].len - nread) { -if (nread ==-1 ) { -switch (errno) { -case EINTR : -case EAGAIN : -break ; -default : -oss_logerr ( - errno, -"Failed to read %zu bytes of audio (to %p)\n" , - bufs[i].len, p - ); -break ; - } - } +if (nread ==-1 ) { +switch (errno) { +case EINTR : +case EAGAIN : +break ; +default : +oss_logerr (errno,"Failed to read %zu bytes of audio (to %p)\n" , + len, dst);break ; } } + + pos += nread; + len -= nread; } - hw->wpos = (hw->wpos + read_samples) % hw->samples; -return read_samples; +return pos; }static int oss_ctl_in (HWVoiceIn *hw,int cmd, ...)
@@ -845,12 +763,14 @@ static void oss_audio_fini (void *opaque)static struct audio_pcm_ops oss_pcm_ops = { .init_out = oss_init_out, .fini_out = oss_fini_out, - .run_out = oss_run_out, + .write = oss_write, + .get_buffer_out = oss_get_buffer_out, + .put_buffer_out = oss_put_buffer_out, .ctl_out = oss_ctl_out, .init_in = oss_init_in, .fini_in = oss_fini_in, - .run_in = oss_run_in, + .read = oss_read, .ctl_in = oss_ctl_in };
Signature · 1 line
2.23.0
PATCH v4 10/24 wavaudio: port to the new audio backend api
In reply to Kővágó, Zoltán---
Notes:
Changes from v3:
* fix compilation on 32-bit targets
audio/wavaudio.c | 54 ++++++++---------------------------------------- 1 file changed, 9 insertions(+), 45 deletions(-)
audio/wavaudio.c +9 -45
@@ -36,52 +36,28 @@ typedef struct WAVVoiceOut {HWVoiceOut hw;FILE *f;int64_t old_ticks; -void *pcm_buf;int total_samples; } WAVVoiceOut; -static size_t wav_run_out (HWVoiceOut *hw,size_t live) +static size_t wav_write_out (HWVoiceOut *hw,void *buf,size_t len) {WAVVoiceOut *wav = (WAVVoiceOut *) hw; -size_t rpos, decr, samples; -uint8_t *dst; -struct st_sample *src;int64_t now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL );int64_t ticks = now - wav->old_ticks;int64_t bytes =muldiv64 (ticks, hw->info.bytes_per_second,NANOSECONDS_PER_SECOND ); -if (bytes >INT_MAX ) { - samples =INT_MAX >> hw->info.shift; - } -else { - samples = bytes >> hw->info.shift; - } - + bytes =MIN (bytes, len); + bytes = bytes >> hw->info.shift << hw->info.shift; wav->old_ticks = now; - decr =MIN (live, samples); - samples = decr; - rpos = hw->rpos; -while (samples) { -int left_till_end_samples = hw->samples - rpos; -int convert_samples =MIN (samples, left_till_end_samples); - - src = hw->mix_buf + rpos; - dst =advance (wav->pcm_buf, rpos << hw->info.shift); - - hw->clip (dst, src, convert_samples); -if (fwrite (dst, convert_samples << hw->info.shift,1 , wav->f) !=1 ) { -dolog ("wav_run_out: fwrite of %d bytes failed\nReaons: %s\n" , - convert_samples << hw->info.shift,strerror (errno)); - } - rpos = (rpos + convert_samples) % hw->samples; - samples -= convert_samples; - wav->total_samples += convert_samples; +if (bytes &&fwrite (buf, bytes,1 , wav->f) !=1 ) { +dolog ("wav_write_out: fwrite of %" PRId64" bytes failed\nReason: %s\n" , + bytes,strerror (errno)); } - hw->rpos = rpos; -return decr; + wav->total_samples += bytes >> hw->info.shift; +return bytes; }/* VICE code: Store number as little endian. */
@@ -137,13 +113,6 @@ static int wav_init_out(HWVoiceOut *hw, struct audsettings *as,audio_pcm_init_info (&hw->info, &wav_as); hw->samples =1024 ; - wav->pcm_buf =audio_calloc (__func__, hw->samples,1 << hw->info.shift); -if (!wav->pcm_buf) { -dolog ("Could not allocate buffer (%zu bytes)\n" , - hw->samples << hw->info.shift); -return -1 ; - } -le_store (hdr +22 , hw->info.nchannels,2 );le_store (hdr +24 , hw->info.freq,4 );le_store (hdr +28 , hw->info.freq << (bits16 + stereo),4 );
@@ -153,8 +122,6 @@ static int wav_init_out(HWVoiceOut *hw, struct audsettings *as,if (!wav->f) {dolog ("Failed to open wave file `%s'\nReason: %s\n" , wav_path,strerror (errno)); -g_free (wav->pcm_buf); - wav->pcm_buf =NULL ;return -1 ; }
@@ -208,9 +175,6 @@ static void wav_fini_out (HWVoiceOut *hw) wav->f, strerror (errno)); } wav->f =NULL ; - -g_free (wav->pcm_buf); - wav->pcm_buf =NULL ; }static int wav_ctl_out (HWVoiceOut *hw,int cmd, ...)
@@ -234,7 +198,7 @@ static void wav_audio_fini (void *opaque)static struct audio_pcm_ops wav_pcm_ops = { .init_out = wav_init_out, .fini_out = wav_fini_out, - .run_out = wav_run_out, + .write = wav_write_out, .ctl_out = wav_ctl_out, };
Signature · 1 line
2.23.0
PATCH v4 18/24 audio: support more than two channels in volume setting
In reply to Kővágó, Zoltán--- audio/audio.c | 30 ++++++++++++++++++++++-------- audio/audio.h | 10 ++++++++++ audio/audio_int.h | 4 ++-- audio/paaudio.c | 20 ++++++++++++-------- audio/spiceaudio.c | 14 ++++++++------ 5 files changed, 54 insertions(+), 24 deletions(-)
audio/audio.c +22 -8
@@ -1891,31 +1891,45 @@ void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque) }void AUD_set_volume_out (SWVoiceOut *sw,int mute,uint8_t lvol,uint8_t rvol) +{ +Volume vol = { .mute = mute, .channels =2 , .vol = { lvol, rvol } }; +audio_set_volume_out (sw, &vol); +} + +void audio_set_volume_out (SWVoiceOut *sw,Volume *vol) {if (sw) {HWVoiceOut *hw = sw->hw; - sw->vol.mute = mute; - sw->vol.l = nominal_volume.l * lvol /255 ; - sw->vol.r = nominal_volume.r * rvol /255 ; + sw->vol.mute = vol->mute; + sw->vol.l = nominal_volume.l * vol->vol[0 ] /255 ; + sw->vol.r = nominal_volume.l * vol->vol[vol->channels >1 ?1 :0 ] / +255 ;if (hw->pcm_ops->volume_out) { - hw->pcm_ops->volume_out (hw, &sw->vol); + hw->pcm_ops->volume_out (hw, vol); } } }void AUD_set_volume_in (SWVoiceIn *sw,int mute,uint8_t lvol,uint8_t rvol) +{ +Volume vol = { .mute = mute, .channels =2 , .vol = { lvol, rvol } }; +audio_set_volume_in (sw, &vol); +} + +void audio_set_volume_in (SWVoiceIn *sw,Volume *vol) {if (sw) {HWVoiceIn *hw = sw->hw; - sw->vol.mute = mute; - sw->vol.l = nominal_volume.l * lvol /255 ; - sw->vol.r = nominal_volume.r * rvol /255 ; + sw->vol.mute = vol->mute; + sw->vol.l = nominal_volume.l * vol->vol[0 ] /255 ; + sw->vol.r = nominal_volume.r * vol->vol[vol->channels >1 ?1 :0 ] / +255 ;if (hw->pcm_ops->volume_in) { - hw->pcm_ops->volume_in (hw, &sw->vol); + hw->pcm_ops->volume_in (hw, vol); } } }
audio/audio.h +10 -0
@@ -124,6 +124,16 @@ uint64_t AUD_get_elapsed_usec_out (SWVoiceOut *sw, QEMUAudioTimeStamp *ts);void AUD_set_volume_out (SWVoiceOut *sw,int mute,uint8_t lvol,uint8_t rvol);void AUD_set_volume_in (SWVoiceIn *sw,int mute,uint8_t lvol,uint8_t rvol); +#define AUDIO_MAX_CHANNELS 16 +typedef struct Volume { +bool mute; +int channels; +uint8_t vol[AUDIO_MAX_CHANNELS ]; +}Volume ; + +void audio_set_volume_out (SWVoiceOut *sw,Volume *vol); +void audio_set_volume_in (SWVoiceIn *sw,Volume *vol); + SWVoiceIn *AUD_open_in ( QEMUSoundCard *card, SWVoiceIn *sw,
audio/audio_int.h +2 -2
@@ -166,7 +166,7 @@ struct audio_pcm_ops {*/ size_t (*put_buffer_out)(HWVoiceOut *hw,void *buf,size_t size);void (*enable_out)(HWVoiceOut *hw,bool enable); -void (*volume_out)(HWVoiceOut *hw,struct mixeng_volume *vol); +void (*volume_out)(HWVoiceOut *hw,Volume *vol);int (*init_in) (HWVoiceIn *hw,audsettings *as,void *drv_opaque);void (*fini_in) (HWVoiceIn *hw);
@@ -174,7 +174,7 @@ struct audio_pcm_ops {void *(*get_buffer_in)(HWVoiceIn *hw,size_t *size);void (*put_buffer_in)(HWVoiceIn *hw,void *buf,size_t size);void (*enable_in)(HWVoiceIn *hw,bool enable); -void (*volume_in)(HWVoiceIn *hw,struct mixeng_volume *vol); +void (*volume_in)(HWVoiceIn *hw,Volume *vol); };void *audio_generic_get_buffer_in (HWVoiceIn *hw,size_t *size);
audio/paaudio.c +12 -8
@@ -531,20 +531,22 @@ static void qpa_fini_in (HWVoiceIn *hw) } } -static void qpa_volume_out (HWVoiceOut *hw,struct mixeng_volume *vol) +static void qpa_volume_out (HWVoiceOut *hw,Volume *vol) {PAVoiceOut *pa = (PAVoiceOut *) hw;pa_operation *op;pa_cvolume v;PAConnection *c = pa->g->conn; +int i;#ifdef PA_CHECK_VERSION /* macro is present in 0.9.16+ */ pa_cvolume_init (&v);/* function is present in 0.9.13+ */ #endif - v.channels =2 ; - v.values[0 ] = ((PA_VOLUME_NORM -PA_VOLUME_MUTED ) * vol->l) /UINT32_MAX ; - v.values[1 ] = ((PA_VOLUME_NORM -PA_VOLUME_MUTED ) * vol->r) /UINT32_MAX ; + v.channels = vol->channels; +for (i =0 ; i < vol->channels; ++i) { + v.values[i] = ((PA_VOLUME_NORM -PA_VOLUME_MUTED ) * vol->vol[i]) /255 ; + }pa_threaded_mainloop_lock (c->mainloop);
@@ -571,20 +573,22 @@ static void qpa_volume_out(HWVoiceOut *hw, struct mixeng_volume *vol)pa_threaded_mainloop_unlock (c->mainloop); } -static void qpa_volume_in (HWVoiceIn *hw,struct mixeng_volume *vol) +static void qpa_volume_in (HWVoiceIn *hw,Volume *vol) {PAVoiceIn *pa = (PAVoiceIn *) hw;pa_operation *op;pa_cvolume v;PAConnection *c = pa->g->conn; +int i;#ifdef PA_CHECK_VERSION pa_cvolume_init (&v);#endif - v.channels =2 ; - v.values[0 ] = ((PA_VOLUME_NORM -PA_VOLUME_MUTED ) * vol->l) /UINT32_MAX ; - v.values[1 ] = ((PA_VOLUME_NORM -PA_VOLUME_MUTED ) * vol->r) /UINT32_MAX ; + v.channels = vol->channels; +for (i =0 ; i < vol->channels; ++i) { + v.values[i] = ((PA_VOLUME_NORM -PA_VOLUME_MUTED ) * vol->vol[i]) /255 ; + }pa_threaded_mainloop_lock (c->mainloop);
audio/spiceaudio.c +8 -6
@@ -179,13 +179,14 @@ static void line_out_enable(HWVoiceOut *hw, bool enable) }#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >=1 ) && (SPICE_INTERFACE_PLAYBACK_MINOR >=2 )) -static void line_out_volume (HWVoiceOut *hw,struct mixeng_volume *vol) +static void line_out_volume (HWVoiceOut *hw,Volume *vol) {SpiceVoiceOut *out =container_of (hw, SpiceVoiceOut, hw);uint16_t svol[2 ]; - svol[0 ] = vol->l / ((1ULL <<16 ) +1 ); - svol[1 ] = vol->r / ((1ULL <<16 ) +1 ); +assert (vol->channels ==2 ); + svol[0 ] = vol->vol[0 ] *257 ; + svol[1 ] = vol->vol[1 ] *257 ;spice_server_playback_set_volume (&out->sin,2 , svol);spice_server_playback_set_mute (&out->sin, vol->mute); }
@@ -262,13 +263,14 @@ static void line_in_enable(HWVoiceIn *hw, bool enable) }#if ((SPICE_INTERFACE_RECORD_MAJOR >=2 ) && (SPICE_INTERFACE_RECORD_MINOR >=2 )) -static void line_in_volume (HWVoiceIn *hw,struct mixeng_volume *vol) +static void line_in_volume (HWVoiceIn *hw,Volume *vol) {SpiceVoiceIn *in =container_of (hw, SpiceVoiceIn, hw);uint16_t svol[2 ]; - svol[0 ] = vol->l / ((1ULL <<16 ) +1 ); - svol[1 ] = vol->r / ((1ULL <<16 ) +1 ); +assert (vol->channels ==2 ); + svol[0 ] = vol->vol[0 ] *257 ; + svol[1 ] = vol->vol[1 ] *257 ;spice_server_record_set_volume (&in->sin,2 , svol);spice_server_record_set_mute (&in->sin, vol->mute); }
Signature · 1 line
2.23.0
PATCH v4 15/24 audio: add mixing-engine option (documentation)
In reply to Kővágó, ZoltánThis will allow us to disable mixeng when we use a decent backend.
Disabling mixeng have a few advantages:
* we no longer convert the audio output from one format to another, when the underlying audio system would just convert it to a third format. We no longer convert, only the underlying system, when needed.
* the underlying system probably has better resampling and sample format converting methods anyway...
* we may support formats that the mixeng currently does not support (S24 or float samples, more than two channels)
* when using an audio server (like pulseaudio) different sound card outputs will show up as separate streams, even if we use only one backend
Disadvantages:
* audio capturing no longer works (wavcapture, and vnc audio extension)
* some backends only support a single playback stream or very picky about the audio format. In this case we can't disable mixeng.
However mixeng is not removed, only made optional, so this shouldn't be a big concern.
---
Notes:
Changes from v1:
* renamed mixeng to mixing-engine
qapi/audio.json | 5 +++++ qemu-options.hx | 6 ++++++ 2 files changed, 11 insertions(+)
qapi/audio.json +5 -0
@@ -11,6 +11,10 @@ # General audio backend options that are used for both playback and # recording. # +# @mixing-engine: use QEMU's mixing engine to mix all streams inside QEMU. When +# set to off, fixed-settings must be also off. Not every backend +# compatible with the off setting (default on, since 4.2) +# # @fixed-settings: use fixed settings for host input/output. When off, # frequency, channels and format must not be # specified (default true)
@@ -31,6 +35,7 @@ ## { 'struct': 'AudiodevPerDirectionOptions', 'data': { + '*mixing-engine': 'bool', '*fixed-settings': 'bool', '*frequency': 'uint32', '*channels': 'uint32',
qemu-options.hx +6 -0
@@ -433,6 +433,7 @@ DEF("audiodev", HAS_ARG, QEMU_OPTION_audiodev, " specifies the audio backend to use\n" " id= identifier of the backend\n" " timer-period= timer period in microseconds\n" + " in|out.mixing-engine= use mixing engine to mix streams inside QEMU\n" " in|out.fixed-settings= use fixed settings for host audio\n" " in|out.frequency= frequency to use with fixed settings\n" " in|out.channels= number of channels to use with fixed settings\n"
@@ -503,6 +504,11 @@ Identifies the audio backend. Sets the timer @var{period} used by the audio subsystem in microseconds. Default is 10000 (10 ms). +@item in|out.mixing-engine=on|off +Use QEMU's mixing engine to mix all streams inside QEMU. When off, +@var{fixed-settings} must be off too. Not every backend is fully +compatible with the off setting. Default is on. + @item in|out.fixed-settings=on|off Use fixed settings for host audio. When off, it will change based on how the guest opens the sound card. In this case you must not specify
Signature · 1 line
2.23.0
PATCH v4 8/24 sdlaudio: port to the new audio backend api
In reply to Kővágó, Zoltán--- audio/sdlaudio.c | 87 +++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 45 deletions(-)
audio/sdlaudio.c +42 -45
@@ -41,8 +41,6 @@typedef struct SDLVoiceOut {HWVoiceOut hw; -size_t live; -size_t decr; }SDLVoiceOut ; static struct SDLAudioState {
@@ -184,62 +182,59 @@ static void sdl_callback (void *opaque, Uint8 *buf, int len)SDLVoiceOut *sdl = opaque;SDLAudioState *s = &glob_sdl;HWVoiceOut *hw = &sdl->hw; -size_t samples = len >> hw->info.shift; -size_t to_mix, decr; -if (s->exit || !sdl->live) { +if (s->exit) {return ; }/* dolog ("in callback samples=%zu live=%zu\n", samples, sdl->live); */ - to_mix =MIN (samples, sdl->live); - decr = to_mix; -while (to_mix) { -size_t chunk =MIN (to_mix, hw->samples - hw->rpos); -struct st_sample *src = hw->mix_buf + hw->rpos; - -/* dolog ("in callback to_mix %zu, chunk %zu\n", to_mix, chunk); */ - hw->clip (buf, src, chunk); - hw->rpos = (hw->rpos + chunk) % hw->samples; - to_mix -= chunk; - buf += chunk << hw->info.shift; +while (hw->pending_emul && len) { +size_t write_len; +ssize_t start = ((ssize_t ) hw->pos_emul) - hw->pending_emul; +if (start <0 ) { + start += hw->size_emul; + } +assert (start >=0 && start < hw->size_emul); + + write_len =MIN (MIN (hw->pending_emul, len), + hw->size_emul - start); + +memcpy (buf, hw->buf_emul + start, write_len); + hw->pending_emul -= write_len; + len -= write_len; + buf += write_len; } - samples -= decr; - sdl->live -= decr; - sdl->decr += decr; -/* dolog ("done len=%zu\n", len); */ - -/* SDL2 does not clear the remaining buffer for us, so do it on our own */ -if (samples) { -memset (buf,0 , samples << hw->info.shift); +/* clear remaining buffer that we couldn't fill with data */ +if (len) { +memset (buf,0 , len); } } -static size_t sdl_run_out (HWVoiceOut *hw,size_t live) -{ -size_t decr; -SDLVoiceOut *sdl = (SDLVoiceOut *) hw; - -SDL_LockAudio (); - -if (sdl->decr > live) { -ldebug ("sdl->decr %d live %d sdl->live %d\n" , - sdl->decr, - live, - sdl->live); +#define SDL_WRAPPER_FUNC (name, ret_type, args_decl, args, fail, unlock) \ + static ret_type glue(sdl_, name)args_decl \ + { \ + ret_type ret; \ + \ + SDL_LockAudio(); \ + \ + ret = glue(audio_generic_, name)args; \ + \ + SDL_UnlockAudio(); \ + return ret; \ } - decr =MIN (sdl->decr, live); - sdl->decr -= decr; - - sdl->live = live; +SDL_WRAPPER_FUNC(get_buffer_out, void *, (HWVoiceOut *hw, size_t *size), + (hw, size), *size = 0, sdl_unlock) +SDL_WRAPPER_FUNC (put_buffer_out_nowrite, size_t, + (HWVoiceOut *hw, void *buf, size_t size), (hw, buf, size), + /*nothing*/, sdl_unlock_and_post) +SDL_WRAPPER_FUNC (write, size_t, + (HWVoiceOut *hw, void *buf, size_t size), (hw, buf, size), + /*nothing*/, sdl_unlock_and_post) -SDL_UnlockAudio (); - -return decr; -} +#undef SDL_WRAPPER_FUNCstatic void sdl_fini_out (HWVoiceOut *hw) {
@@ -336,7 +331,9 @@ static void sdl_audio_fini (void *opaque)static struct audio_pcm_ops sdl_pcm_ops = { .init_out = sdl_init_out, .fini_out = sdl_fini_out, - .run_out = sdl_run_out, + .write = sdl_write, + .get_buffer_out = sdl_get_buffer_out, + .put_buffer_out = sdl_put_buffer_out_nowrite, .ctl_out = sdl_ctl_out, };
Signature · 1 line
2.23.0
PATCH v4 24/24 usbaudio: change playback counters to 64 bit
In reply to Kővágó, ZoltánWith stereo playback, they need about 375 minutes of continuous audio playback to overflow, which is usually not a problem (as stopping and later resuming playback resets the counters). But with 7.1 audio, they only need about 95 minutes to overflow.
After the overflow, the buf->prod % USBAUDIO_PACKET_SIZE(channels) assertion no longer holds true, which will result in overflowing the buffer. With 64 bit variables, it would take about 762000 years to overflow.
--- hw/usb/dev-audio.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
hw/usb/dev-audio.c +8 -6
@@ -578,9 +578,9 @@ static const USBDesc desc_audio_multi = {struct streambuf {uint8_t *data; -uint32_t size; -uint32_t prod; -uint32_t cons; +size_t size; +uint64_t prod; +uint64_t cons; }; static void streambuf_init(struct streambuf *buf, uint32_t size,
@@ -601,7 +601,7 @@ static void streambuf_fini(struct streambuf *buf)static int streambuf_put (struct streambuf *buf,USBPacket *p,uint32_t channels) { -uint32_t free = buf->size - (buf->prod - buf->cons); +int64_t free = buf->size - (buf->prod - buf->cons);if (free <USBAUDIO_PACKET_SIZE (channels)) {return 0 ;
@@ -610,6 +610,8 @@ static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels)return 0 ; } +/* can happen if prod overflows */ +assert (buf->prod %USBAUDIO_PACKET_SIZE (channels) ==0 );usb_packet_copy (p, buf->data + (buf->prod % buf->size),USBAUDIO_PACKET_SIZE (channels)); buf->prod +=USBAUDIO_PACKET_SIZE (channels);
@@ -618,10 +620,10 @@ static int streambuf_put(struct streambuf *buf, USBPacket *p, uint32_t channels)static uint8_t *streambuf_get (struct streambuf *buf,size_t *len) { -uint32_t used = buf->prod - buf->cons; +int64_t used = buf->prod - buf->cons;uint8_t *data; -if (!used) { +if (used <=0 ) { *len =0 ;return NULL ; }
Signature · 1 line
2.23.0
PATCH v4 13/24 audio: common rate control code for timer based outputs
In reply to Kővágó, ZoltánThis commit removes the ad-hoc rate-limiting code from noaudio and wavaudio, and replaces them with a (slightly modified) code from spiceaudio. This way multiple write calls (for example when the circular buffer wraps around) do not cause problems.
--- audio/audio.c | 30 +++++++++++++++++++++++++++ audio/audio_int.h | 9 ++++++++ audio/noaudio.c | 49 ++++++++++++++++++++------------------------ audio/spiceaudio.c | 51 ++++++++-------------------------------------- audio/wavaudio.c | 21 +++++++++---------- 5 files changed, 79 insertions(+), 81 deletions(-)
audio/audio.c +30 -0
@@ -2051,3 +2051,33 @@ const char *audio_get_id(QEMUSoundCard *card)return "" ; } } + +void audio_rate_start (RateCtl *rate) +{ +memset (rate,0 ,sizeof (RateCtl)); + rate->start_ticks =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); +} + +size_t audio_rate_get_bytes (struct audio_pcm_info *info,RateCtl *rate, +size_t bytes_avail) +{ +int64_t now; +int64_t ticks; +int64_t bytes; +int64_t samples; +size_t ret; + + now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); + ticks = now - rate->start_ticks; + bytes =muldiv64 (ticks, info->bytes_per_second,NANOSECONDS_PER_SECOND ); + samples = (bytes - rate->bytes_sent) >> info->shift; +if (samples <0 || samples >65536 ) { +AUD_log (NULL ,"Resetting rate control (%" PRId64" samples)\n" , samples); +audio_rate_start (rate); + samples =0 ; + } + + ret =MIN (samples << info->shift, bytes_avail); + rate->bytes_sent += ret; +return ret; +}
audio/audio_int.h +9 -0
@@ -242,6 +242,15 @@ void *audio_calloc (const char *funcname, int nmemb, size_t size);void audio_run (AudioState *s,const char *msg); +typedef struct RateCtl { +int64_t start_ticks; +int64_t bytes_sent; +}RateCtl ; + +void audio_rate_start (RateCtl *rate); +size_t audio_rate_get_bytes (struct audio_pcm_info *info,RateCtl *rate, +size_t bytes_avail); +#define VOICE_ENABLE 1#define VOICE_DISABLE 2#define VOICE_VOLUME 3
audio/noaudio.c +22 -27
95 lines not on this page · raw message
audio/spiceaudio.c +8 -43
103 lines not on this page · raw message
audio/wavaudio.c +10 -11
@@ -35,21 +35,15 @@typedef struct WAVVoiceOut {HWVoiceOut hw;FILE *f; -int64_t old_ticks; +RateCtl rate;int total_samples; }WAVVoiceOut ;static size_t wav_write_out (HWVoiceOut *hw,void *buf,size_t len) {WAVVoiceOut *wav = (WAVVoiceOut *) hw; -int64_t now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); -int64_t ticks = now - wav->old_ticks; -int64_t bytes = -muldiv64 (ticks, hw->info.bytes_per_second,NANOSECONDS_PER_SECOND ); - - bytes =MIN (bytes, len); - bytes = bytes >> hw->info.shift << hw->info.shift; - wav->old_ticks = now; +int64_t bytes =audio_rate_get_bytes (&hw->info, &wav->rate, len); +assert (bytes >> hw->info.shift << hw->info.shift == bytes);if (bytes &&fwrite (buf, bytes,1 , wav->f) !=1 ) { dolog("wav_write_out: fwrite of %" PRId64 " bytes failed\nReason: %s\n",
@@ -130,6 +124,8 @@ static int wav_init_out(HWVoiceOut *hw, struct audsettings *as, strerror(errno));return -1 ; } + +audio_rate_start (&wav->rate);return 0 ; }
@@ -179,8 +175,11 @@ static void wav_fini_out (HWVoiceOut *hw)static int wav_ctl_out (HWVoiceOut *hw,int cmd, ...) { - (void ) hw; - (void ) cmd; +WAVVoiceOut *wav = (WAVVoiceOut *) hw; + +if (cmd ==VOICE_ENABLE ) { +audio_rate_start (&wav->rate); + }return 0 ; }
Signature · 1 line
2.23.0
PATCH v4 22/24 usb-audio: do not count on avail bytes actually available
In reply to Kővágó, ZoltánThis assumption is no longer true when mixeng is turned off.
--- hw/usb/dev-audio.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-)
hw/usb/dev-audio.c +17 -11
@@ -319,30 +319,31 @@ static int streambuf_put(struct streambuf *buf, USBPacket *p) {uint32_t free = buf->size - (buf->prod - buf->cons); -if (!free) { +if (free <USBAUDIO_PACKET_SIZE ) {return 0 ; }if (p->iov.size !=USBAUDIO_PACKET_SIZE ) {return 0 ; } -assert (free >=USBAUDIO_PACKET_SIZE ); +usb_packet_copy (p, buf->data + (buf->prod % buf->size),USBAUDIO_PACKET_SIZE ); buf->prod +=USBAUDIO_PACKET_SIZE ;return USBAUDIO_PACKET_SIZE ; } -static uint8_t *streambuf_get (struct streambuf *buf) +static uint8_t *streambuf_get (struct streambuf *buf,size_t *len) {uint32_t used = buf->prod - buf->cons;uint8_t *data;if (!used) { + *len =0 ;return NULL ; } -assert (used >=USBAUDIO_PACKET_SIZE ); data = buf->data + (buf->cons % buf->size); - buf->cons +=USBAUDIO_PACKET_SIZE ; + *len =MIN (buf->prod - buf->cons, + buf->size - (buf->cons % buf->size));return data; }
@@ -374,16 +375,21 @@ static void output_callback(void *opaque, int avail)USBAudioState *s = opaque;uint8_t *data; -for (;;) { -if (avail <USBAUDIO_PACKET_SIZE ) { +while (avail) { +size_t written, len; + + data =streambuf_get (&s->out.buf, &len); +if (!data) {return ; } - data =streambuf_get (&s->out.buf); -if (!data) { + + written =AUD_write (s->out.voice, data, len); + avail -= written; + s->out.buf.cons += written; + +if (written < len) {return ; } -AUD_write (s->out.voice, data,USBAUDIO_PACKET_SIZE ); - avail -=USBAUDIO_PACKET_SIZE ; } }
Signature · 1 line
2.23.0
PATCH v4 16/24 audio: make mixeng optional
In reply to Kővágó, ZoltánImplementation of the previously added mixing-engine option.
--- audio/audio.c | 70 ++++++++++++++++++++++++++++++++++++++---- audio/audio_template.h | 20 ++++++++---- 2 files changed, 78 insertions(+), 12 deletions(-)
audio/audio.c +64 -6
@@ -838,32 +838,46 @@ static void audio_timer (void *opaque)*/ size_t AUD_write (SWVoiceOut *sw,void *buf,size_t size) { +HWVoiceOut *hw; +if (!sw) {/* XXX: Consider options */ return size; } + hw = sw->hw; -if (!sw->hw->enabled) { +if (!hw->enabled) {dolog ("Writing to disabled voice %s\n" ,SW_NAME (sw));return 0 ; } -return audio_pcm_sw_write (sw, buf, size); +if (audio_get_pdo_out (hw->s->dev)->mixing_engine) { +return audio_pcm_sw_write (sw, buf, size); + }else { +return hw->pcm_ops->write (hw, buf, size); + } }size_t AUD_read (SWVoiceIn *sw,void *buf,size_t size) { +HWVoiceIn *hw; +if (!sw) {/* XXX: Consider options */ return size; } + hw = sw->hw; -if (!sw->hw->enabled) { +if (!hw->enabled) {dolog ("Reading from disabled voice %s\n" ,SW_NAME (sw));return 0 ; } -return audio_pcm_sw_read (sw, buf, size); +if (audio_get_pdo_in (hw->s->dev)->mixing_engine) { +return audio_pcm_sw_read (sw, buf, size); + }else { +return hw->pcm_ops->read (hw, buf, size); + } }int AUD_get_buffer_size_out (SWVoiceOut *sw)
@@ -1090,6 +1104,26 @@ static void audio_run_out (AudioState *s)HWVoiceOut *hw =NULL ;SWVoiceOut *sw; +if (!audio_get_pdo_out (s->dev)->mixing_engine) { +while ((hw =audio_pcm_hw_find_any_enabled_out (s, hw))) { +/* there is exactly 1 sw for each hw with no mixeng */ + sw = hw->sw_head.lh_first; + +if (hw->pending_disable) { + hw->enabled =0 ; + hw->pending_disable =0 ; +if (hw->pcm_ops->enable_out) { + hw->pcm_ops->enable_out (hw, false); + } + } + +if (sw->active) { + sw->callback.fn (sw->callback.opaque,INT_MAX ); + } + } +return ; + } +while ((hw =audio_pcm_hw_find_any_enabled_out (s, hw))) {size_t played, live, prev_rpos, free;int nb_live, cleanup_required;
@@ -1227,6 +1261,17 @@ static void audio_run_in (AudioState *s) {HWVoiceIn *hw =NULL ; +if (!audio_get_pdo_in (s->dev)->mixing_engine) { +while ((hw =audio_pcm_hw_find_any_enabled_in (s, hw))) { +/* there is exactly 1 sw for each hw with no mixeng */ +SWVoiceIn *sw = hw->sw_head.lh_first; +if (sw->active) { + sw->callback.fn (sw->callback.opaque,INT_MAX ); + } + } +return ; + } +while ((hw =audio_pcm_hw_find_any_enabled_in (s, hw))) {SWVoiceIn *sw;size_t captured =0 , min;
@@ -1751,6 +1796,11 @@ CaptureVoiceOut *AUD_add_capture( s =audio_init (NULL ,NULL ); } +if (!audio_get_pdo_out (s->dev)->mixing_engine) { +dolog ("Can't capture with mixeng disabled\n" ); +return NULL ; + } +if (audio_validate_settings (as)) {dolog ("Invalid settings were passed when trying to add capture\n" );audio_print_settings (as);
@@ -1905,9 +1955,13 @@ void audio_create_pdos(Audiodev *dev)static void audio_validate_per_direction_opts (AudiodevPerDirectionOptions *pdo,Error **errp) { +if (!pdo->has_mixing_engine) { + pdo->has_mixing_engine = true; + pdo->mixing_engine = true; + }if (!pdo->has_fixed_settings) { pdo->has_fixed_settings = true; - pdo->fixed_settings = true; + pdo->fixed_settings = pdo->mixing_engine; }if (!pdo->fixed_settings && (pdo->has_frequency || pdo->has_channels || pdo->has_format)) {
@@ -1915,6 +1969,10 @@ static void audio_validate_per_direction_opts( "You can't use frequency, channels or format with fixed-settings=off");return ; } +if (!pdo->mixing_engine && pdo->fixed_settings) { +error_setg (errp,"You can't use fixed-settings without mixeng" ); +return ; + }if (!pdo->has_frequency) { pdo->has_frequency = true;
@@ -1926,7 +1984,7 @@ static void audio_validate_per_direction_opts( }if (!pdo->has_voices) { pdo->has_voices = true; - pdo->voices =1 ; + pdo->voices = pdo->mixing_engine ?1 :INT_MAX ; }if (!pdo->has_format) { pdo->has_format = true;
audio/audio_template.h +14 -6
@@ -78,13 +78,17 @@ static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)static void glue (audio_pcm_hw_alloc_resources_ ,TYPE )(HW *hw) { -size_t samples = hw->samples; -if (audio_bug (__func__, samples ==0 )) { -dolog ("Attempted to allocate empty buffer\n" ); - } +if (glue (audio_get_pdo_,TYPE )(hw->s->dev)->mixing_engine) { +size_t samples = hw->samples; +if (audio_bug (__func__, samples ==0 )) { +dolog ("Attempted to allocate empty buffer\n" ); + } -HWBUF =g_malloc0 (sizeof (STSampleBuffer) +sizeof (st_sample) * samples); -HWBUF ->size = samples; +HWBUF =g_malloc0 (sizeof (STSampleBuffer) +sizeof (st_sample) * samples); +HWBUF ->size = samples; + }else { +HWBUF =NULL ; + } }static void glue (audio_pcm_sw_free_resources_ ,TYPE ) (SW *sw)
@@ -103,6 +107,10 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw) {int samples; +if (!glue (audio_get_pdo_,TYPE )(sw->s->dev)->mixing_engine) { +return 0 ; + } + samples = ((int64_t ) sw->HWBUF->size <<32 ) / sw->ratio; sw->buf =audio_calloc (__func__, samples,sizeof (struct st_sample ));
Signature · 1 line
2.23.0
PATCH v4 12/24 audio: unify input and output mixeng buffer management
In reply to Kővágó, ZoltánUsage notes: hw->samples became hw->{mix,conv}_buf->size, except before initialization (audio_pcm_hw_alloc_resources_*), hw->samples gives the initial size of the STSampleBuffer. The next commit tries to fix this inconsistency.
--- audio/audio.c | 122 +++++++++++++++++++++-------------------- audio/audio_int.h | 12 ++-- audio/audio_template.h | 19 +++---- audio/ossaudio.c | 3 +- 4 files changed, 80 insertions(+), 76 deletions(-)
audio/audio.c +63 -59
@@ -544,8 +544,8 @@ static size_t audio_pcm_hw_find_min_in (HWVoiceIn *hw)static size_t audio_pcm_hw_get_live_in (HWVoiceIn *hw) {size_t live = hw->total_samples_captured -audio_pcm_hw_find_min_in (hw); -if (audio_bug (__func__, live > hw->samples)) { -dolog ("live=%zu hw->samples=%zu\n" , live, hw->samples); +if (audio_bug (__func__, live > hw->conv_buf->size)) { +dolog ("live=%zu hw->conv_buf->size=%zu\n" , live, hw->conv_buf->size);return 0 ; }return live;
@@ -554,17 +554,17 @@ static size_t audio_pcm_hw_get_live_in(HWVoiceIn *hw)static void audio_pcm_hw_clip_out (HWVoiceOut *hw,void *pcm_buf,size_t len) {size_t clipped =0 ; -size_t pos = hw->rpos; +size_t pos = hw->mix_buf->pos;while (len) { -st_sample *src = hw->mix_buf + pos; +st_sample *src = hw->mix_buf->samples + pos;uint8_t *dst =advance (pcm_buf, clipped << hw->info.shift); -size_t samples_till_end_of_buf = hw->samples - pos; +size_t samples_till_end_of_buf = hw->mix_buf->size - pos;size_t samples_to_clip =MIN (len, samples_till_end_of_buf); hw->clip (dst, src, samples_to_clip); - pos = (pos + samples_to_clip) % hw->samples; + pos = (pos + samples_to_clip) % hw->mix_buf->size; len -= samples_to_clip; clipped += samples_to_clip; }
@@ -579,17 +579,17 @@ static size_t audio_pcm_sw_get_rpos_in(SWVoiceIn *sw)ssize_t live = hw->total_samples_captured - sw->total_hw_samples_acquired;ssize_t rpos; -if (audio_bug (__func__, live <0 || live > hw->samples)) { -dolog ("live=%zu hw->samples=%zu\n" , live, hw->samples); +if (audio_bug (__func__, live <0 || live > hw->conv_buf->size)) { +dolog ("live=%zu hw->conv_buf->size=%zu\n" , live, hw->conv_buf->size);return 0 ; } - rpos = hw->wpos - live; + rpos = hw->conv_buf->pos - live;if (rpos >=0 ) {return rpos; }else { -return hw->samples + rpos; +return hw->conv_buf->size + rpos; } }
@@ -599,11 +599,11 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t size)size_t samples, live, ret =0 , swlim, isamp, osamp, rpos, total =0 ;struct st_sample *src, *dst = sw->buf; - rpos =audio_pcm_sw_get_rpos_in (sw) % hw->samples; + rpos =audio_pcm_sw_get_rpos_in (sw) % hw->conv_buf->size; live = hw->total_samples_captured - sw->total_hw_samples_acquired; -if (audio_bug (__func__, live > hw->samples)) { -dolog ("live_in=%zu hw->samples=%zu\n" , live, hw->samples); +if (audio_bug (__func__, live > hw->conv_buf->size)) { +dolog ("live_in=%zu hw->conv_buf->size=%zu\n" , live, hw->conv_buf->size);return 0 ; }
@@ -616,11 +616,11 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t size) swlim =MIN (swlim, samples);while (swlim) { - src = hw->conv_buf + rpos; -if (hw->wpos > rpos) { - isamp = hw->wpos - rpos; + src = hw->conv_buf->samples + rpos; +if (hw->conv_buf->pos > rpos) { + isamp = hw->conv_buf->pos - rpos; }else { - isamp = hw->samples - rpos; + isamp = hw->conv_buf->size - rpos; }if (!isamp) {
@@ -630,7 +630,7 @@ static size_t audio_pcm_sw_read(SWVoiceIn *sw, void *buf, size_t size)st_rate_flow (sw->rate, src, dst, &isamp, &osamp); swlim -= osamp; - rpos = (rpos + isamp) % hw->samples; + rpos = (rpos + isamp) % hw->conv_buf->size; dst += osamp; ret += osamp; total += isamp;
@@ -678,8 +678,8 @@ static size_t audio_pcm_hw_get_live_out (HWVoiceOut *hw, int *nb_live)if (nb_live1) {size_t live = smin; -if (audio_bug (__func__, live > hw->samples)) { -dolog ("live=%zu hw->samples=%zu\n" , live, hw->samples); +if (audio_bug (__func__, live > hw->mix_buf->size)) { +dolog ("live=%zu hw->mix_buf->size=%zu\n" , live, hw->mix_buf->size);return 0 ; }return live;
@@ -699,11 +699,11 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t size)return size; } - hwsamples = sw->hw->samples; + hwsamples = sw->hw->mix_buf->size; live = sw->total_hw_samples_mixed;if (audio_bug (__func__, live > hwsamples)) { -dolog ("live=%zu hw->samples=%zu\n" , live, hwsamples); +dolog ("live=%zu hw->mix_buf->size=%zu\n" , live, hwsamples);return 0 ; }
@@ -714,7 +714,7 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t size)return 0 ; } - wpos = (sw->hw->rpos + live) % hwsamples; + wpos = (sw->hw->mix_buf->pos + live) % hwsamples; samples = size >> sw->info.shift; dead = hwsamples - live;
@@ -740,7 +740,7 @@ static size_t audio_pcm_sw_write(SWVoiceOut *sw, void *buf, size_t size)st_rate_flow_mix ( sw->rate, sw->buf + pos, - sw->hw->mix_buf + wpos, + sw->hw->mix_buf->samples + wpos, &isamp, &osamp );
@@ -868,7 +868,7 @@ size_t AUD_read(SWVoiceIn *sw, void *buf, size_t size)int AUD_get_buffer_size_out (SWVoiceOut *sw) { -return sw->hw->samples << sw->hw->info.shift; +return sw->hw->mix_buf->size << sw->hw->info.shift; }void AUD_set_active_out (SWVoiceOut *sw,int on)
@@ -969,8 +969,9 @@ static size_t audio_get_avail (SWVoiceIn *sw) } live = sw->hw->total_samples_captured - sw->total_hw_samples_acquired; -if (audio_bug (__func__, live > sw->hw->samples)) { -dolog ("live=%zu sw->hw->samples=%zu\n" , live, sw->hw->samples); +if (audio_bug (__func__, live > sw->hw->conv_buf->size)) { +dolog ("live=%zu sw->hw->conv_buf->size=%zu\n" , live, + sw->hw->conv_buf->size);return 0 ; }
@@ -993,12 +994,13 @@ static size_t audio_get_free(SWVoiceOut *sw) live = sw->total_hw_samples_mixed; -if (audio_bug (__func__, live > sw->hw->samples)) { -dolog ("live=%zu sw->hw->samples=%zu\n" , live, sw->hw->samples); +if (audio_bug (__func__, live > sw->hw->mix_buf->size)) { +dolog ("live=%zu sw->hw->mix_buf->size=%zu\n" , live, + sw->hw->mix_buf->size);return 0 ; } - dead = sw->hw->samples - live; + dead = sw->hw->mix_buf->size - live;#ifdef DEBUG_OUT dolog ("%s: get_free live %d dead %d ret %" PRId64 "\n",
@@ -1023,12 +1025,12 @@ static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos, n = samples;while (n) { -size_t till_end_of_hw = hw->samples - rpos2; +size_t till_end_of_hw = hw->mix_buf->size - rpos2;size_t to_write =MIN (till_end_of_hw, n);size_t bytes = to_write << hw->info.shift;size_t written; - sw->buf = hw->mix_buf + rpos2; + sw->buf = hw->mix_buf->samples + rpos2; written =audio_pcm_sw_write (sw,NULL , bytes);if (written - bytes) { dolog("Could not mix %zu bytes into a capture "
@@ -1037,14 +1039,14 @@ static void audio_capture_mix_and_clear(HWVoiceOut *hw, size_t rpos,break ; } n -= to_write; - rpos2 = (rpos2 + to_write) % hw->samples; + rpos2 = (rpos2 + to_write) % hw->mix_buf->size; } } } - n =MIN (samples, hw->samples - rpos); -mixeng_clear (hw->mix_buf + rpos, n); -mixeng_clear (hw->mix_buf, samples - n); + n =MIN (samples, hw->mix_buf->size - rpos); +mixeng_clear (hw->mix_buf->samples + rpos, n); +mixeng_clear (hw->mix_buf->samples, samples - n); }static size_t audio_pcm_hw_run_out (HWVoiceOut *hw,size_t live)
@@ -1056,7 +1058,7 @@ static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live)void *buf = hw->pcm_ops->get_buffer_out (hw, &size);if (!buf) {/* retrying will likely won't help, drop everything. */ - hw->rpos = (hw->rpos + live) % hw->samples; + hw->mix_buf->pos = (hw->mix_buf->pos + live) % hw->mix_buf->size;return clipped + live; }
@@ -1067,7 +1069,7 @@ static size_t audio_pcm_hw_run_out(HWVoiceOut *hw, size_t live) live -= proc; clipped += proc; - hw->rpos = (hw->rpos + proc) % hw->samples; + hw->mix_buf->pos = (hw->mix_buf->pos + proc) % hw->mix_buf->size;if (proc ==0 || proc < decr) {break ;
@@ -1091,8 +1093,8 @@ static void audio_run_out (AudioState *s) live =0 ; } -if (audio_bug (__func__, live > hw->samples)) { -dolog ("live=%zu hw->samples=%zu\n" , live, hw->samples); +if (audio_bug (__func__, live > hw->mix_buf->size)) { +dolog ("live=%zu hw->mix_buf->size=%zu\n" , live, hw->mix_buf->size);continue ; }
@@ -1123,13 +1125,13 @@ static void audio_run_out (AudioState *s)continue ; } - prev_rpos = hw->rpos; + prev_rpos = hw->mix_buf->pos; played =audio_pcm_hw_run_out (hw, live);replay_audio_out (&played); -if (audio_bug (__func__, hw->rpos >= hw->samples)) { -dolog ("hw->rpos=%zu hw->samples=%zu played=%zu\n" , - hw->rpos, hw->samples, played); - hw->rpos =0 ; +if (audio_bug (__func__, hw->mix_buf->pos >= hw->mix_buf->size)) { +dolog ("hw->mix_buf->pos=%zu hw->mix_buf->size=%zu played=%zu\n" , + hw->mix_buf->pos, hw->mix_buf->size, played); + hw->mix_buf->pos =0 ; }#ifdef DEBUG_OUT
@@ -1186,6 +1188,7 @@ static void audio_run_out (AudioState *s)static size_t audio_pcm_hw_run_in (HWVoiceIn *hw,size_t samples) {size_t conv =0 ; +STSampleBuffer *conv_buf = hw->conv_buf;while (samples) {size_t proc;
@@ -1199,10 +1202,10 @@ static size_t audio_pcm_hw_run_in(HWVoiceIn *hw, size_t samples) } proc =MIN (size >> hw->info.shift, - hw->samples - hw->wpos); + conv_buf->size - conv_buf->pos); - hw->conv (hw->conv_buf + hw->wpos, buf, proc); - hw->wpos = (hw->wpos + proc) % hw->samples; + hw->conv (conv_buf->samples + conv_buf->pos, buf, proc); + conv_buf->pos = (conv_buf->pos + proc) % conv_buf->size; samples -= proc; conv += proc;
@@ -1222,9 +1225,10 @@ static void audio_run_in (AudioState *s)if (replay_mode !=REPLAY_MODE_PLAY ) { captured =audio_pcm_hw_run_in ( - hw, hw->samples -audio_pcm_hw_get_live_in (hw)); + hw, hw->conv_buf->size -audio_pcm_hw_get_live_in (hw)); } -replay_audio_in (&captured, hw->conv_buf, &hw->wpos, hw->samples); +replay_audio_in (&captured, hw->conv_buf->samples, &hw->conv_buf->pos, + hw->conv_buf->size); min =audio_pcm_hw_find_min_in (hw); hw->total_samples_captured += captured - min;
@@ -1255,14 +1259,14 @@ static void audio_run_capture (AudioState *s)SWVoiceOut *sw; captured = live =audio_pcm_hw_get_live_out (hw,NULL ); - rpos = hw->rpos; + rpos = hw->mix_buf->pos;while (live) { -size_t left = hw->samples - rpos; +size_t left = hw->mix_buf->size - rpos;size_t to_capture =MIN (live, left);struct st_sample *src;struct capture_callback *cb; - src = hw->mix_buf + rpos; + src = hw->mix_buf->samples + rpos; hw->clip (cap->buf, src, to_capture);mixeng_clear (src, to_capture);
@@ -1270,10 +1274,10 @@ static void audio_run_capture (AudioState *s) cb->ops.capture (cb->opaque, cap->buf, to_capture << hw->info.shift); } - rpos = (rpos + to_capture) % hw->samples; + rpos = (rpos + to_capture) % hw->mix_buf->size; live -= to_capture; } - hw->rpos = rpos; + hw->mix_buf->pos = rpos;for (sw = hw->sw_head.lh_first; sw; sw = sw->entries.le_next) {if (!sw->active && sw->empty) {
@@ -1321,7 +1325,7 @@ void *audio_generic_get_buffer_in(HWVoiceIn *hw, size_t *size)ssize_t start;if (unlikely (!hw->buf_emul)) { -size_t calc_size = hw->samples << hw->info.shift; +size_t calc_size = hw->conv_buf->size << hw->info.shift; hw->buf_emul =g_malloc (calc_size); hw->size_emul = calc_size; hw->pos_emul = hw->pending_emul =0 ;
@@ -1357,7 +1361,7 @@ void audio_generic_put_buffer_in(HWVoiceIn *hw, void *buf, size_t size)void *audio_generic_get_buffer_out (HWVoiceOut *hw,size_t *size) {if (unlikely (!hw->buf_emul)) { -size_t calc_size = hw->samples << hw->info.shift; +size_t calc_size = hw->mix_buf->size << hw->info.shift; hw->buf_emul =g_malloc (calc_size); hw->size_emul = calc_size;
@@ -1764,11 +1768,11 @@ CaptureVoiceOut *AUD_add_capture(/* XXX find a more elegant way */ hw->samples =4096 *4 ; - hw->mix_buf = g_new0(struct st_sample, hw->samples); +audio_pcm_hw_alloc_resources_out (hw);audio_pcm_init_info (&hw->info, as); - cap->buf =g_malloc0_n (hw->samples,1 << hw->info.shift); + cap->buf =g_malloc0_n (hw->mix_buf->size,1 << hw->info.shift); hw->clip = mixeng_clip [hw->info.nchannels ==2 ]
audio/audio_int.h +7 -5
@@ -52,6 +52,11 @@ struct audio_pcm_info {typedef struct AudioState AudioState ;typedef struct SWVoiceCap SWVoiceCap ; +typedef struct STSampleBuffer { +size_t pos, size; +st_sample samples[]; +}STSampleBuffer ; + typedef struct HWVoiceOut { AudioState *s; int enabled;
@@ -60,11 +65,9 @@ typedef struct HWVoiceOut {struct audio_pcm_info info;f_sample *clip; - -size_t rpos;uint64_t ts_helper; -struct st_sample *mix_buf; +STSampleBuffer *mix_buf;void *buf_emul;size_t pos_emul, pending_emul, size_emul;
@@ -84,11 +87,10 @@ typedef struct HWVoiceIn {t_sample *conv; -size_t wpos;size_t total_samples_captured;uint64_t ts_helper; -struct st_sample *conv_buf; +STSampleBuffer *conv_buf;void *buf_emul;size_t pos_emul, pending_emul, size_emul;
audio/audio_template.h +8 -11
@@ -76,16 +76,15 @@ static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)HWBUF =NULL ; } -static bool glue (audio_pcm_hw_alloc_resources_ ,TYPE )(HW *hw) +static void glue (audio_pcm_hw_alloc_resources_ ,TYPE )(HW *hw) { -HWBUF =audio_calloc (__func__, hw->samples,sizeof (struct st_sample )); -if (!HWBUF ) { -dolog ("Could not allocate " NAME " buffer (%zu samples)\n" , - hw->samples); -return false; +size_t samples = hw->samples; +if (audio_bug (__func__, samples ==0 )) { +dolog ("Attempted to allocate empty buffer\n" ); } -return true; +HWBUF =g_malloc0 (sizeof (STSampleBuffer) +sizeof (st_sample) * samples); +HWBUF ->size = samples; }static void glue (audio_pcm_sw_free_resources_ ,TYPE ) (SW *sw)
@@ -104,7 +103,7 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw) {int samples; - samples = ((int64_t ) sw->hw->samples <<32 ) / sw->ratio; + samples = ((int64_t ) sw->HWBUF->size <<32 ) / sw->ratio; sw->buf =audio_calloc (__func__, samples,sizeof (struct st_sample ));if (!sw->buf) {
@@ -280,9 +279,7 @@ static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioState *s, [hw->info.swap_endianness] [audio_bits_to_index (hw->info.bits)]; -if (!glue (audio_pcm_hw_alloc_resources_,TYPE )(hw)) { - goto err1; - } +glue (audio_pcm_hw_alloc_resources_,TYPE )(hw);QLIST_INSERT_HEAD (&s->glue (hw_head_,TYPE ), hw, entries);glue (s->nb_hw_voices_,TYPE ) -=1 ;
audio/ossaudio.c +2 -1
@@ -585,7 +585,8 @@ static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)return 0 ; } -audio_pcm_info_clear_buf (&hw->info, hw->buf_emul, hw->samples); +audio_pcm_info_clear_buf ( + &hw->info, hw->buf_emul, hw->mix_buf->size); trig =PCM_ENABLE_OUTPUT ;if (ioctl (oss->fd,SNDCTL_DSP_SETTRIGGER , &trig) <0 ) { oss_logerr (
Signature · 1 line
2.23.0
PATCH v4 21/24 paaudio: channel-map option
In reply to Kővágó, ZoltánAdd an option to change the channel map used by pulseaudio. If not specified, falls back to an OSS compatible channel map.
--- audio/paaudio.c | 18 ++++++++++++++---- qapi/audio.json | 7 +++++-- qemu-options.hx | 9 +++++++++ 3 files changed, 28 insertions(+), 6 deletions(-)
audio/paaudio.c +14 -4
@@ -338,17 +338,27 @@ static pa_stream *qpa_simple_new (pa_stream_direction_t dir, const char *dev, const pa_sample_spec *ss, - const pa_channel_map *map, + const char *map, const pa_buffer_attr *attr, int *rerror) { int r;pa_stream *stream;pa_stream_flags_t flags; +pa_channel_map pa_map;pa_threaded_mainloop_lock (c->mainloop); - stream =pa_stream_new (c->context, name, ss, map); +if (map && !pa_channel_map_parse (&pa_map, map)) { +dolog ("Invalid channel map specified: '%s'\n" , map); + map =NULL ; + } +if (!map) { +pa_channel_map_init_extend (&pa_map, ss->channels, +PA_CHANNEL_MAP_OSS ); + } + + stream =pa_stream_new (c->context, name, ss, &pa_map);if (!stream) { goto fail; }
@@ -421,7 +431,7 @@ static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,PA_STREAM_PLAYBACK , ppdo->has_name ? ppdo->name :NULL , &ss, -NULL ,/* channel map */ + ppdo->has_channel_map ? ppdo->channel_map :NULL , &ba,/* buffering attributes */ &error );
@@ -470,7 +480,7 @@ static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)PA_STREAM_RECORD , ppdo->has_name ? ppdo->name :NULL , &ss, -NULL ,/* channel map */ + ppdo->has_channel_map ? ppdo->channel_map :NULL , &ba,/* buffering attributes */ &error );
qapi/audio.json +5 -2
@@ -214,13 +214,16 @@ # @latency: latency you want PulseAudio to achieve in microseconds # (default 15000) # +# @channel-map: channel map to use (default: OSS compatible map, since: 4.2) +# # Since: 4.0 ## { 'struct': 'AudiodevPaPerDirectionOptions', 'base': 'AudiodevPerDirectionOptions', 'data': { - '*name': 'str', - '*latency': 'uint32' } } + '*name': 'str', + '*latency': 'uint32', + '*channel-map': 'str' } } ## # @AudiodevPaOptions:
qemu-options.hx +9 -0
@@ -471,6 +471,7 @@ DEF("audiodev", HAS_ARG, QEMU_OPTION_audiodev, "-audiodev pa,id=id[,prop[=value][,...]]\n" " server= PulseAudio server address\n" " in|out.name= source/sink device name\n" + " in|out.channel-map= channel map to use\n" #endif #ifdef CONFIG_AUDIO_SDL "-audiodev sdl,id=id[,prop[=value][,...]]\n"
@@ -636,6 +637,14 @@ Sets the PulseAudio @var{server} to connect to. @item in|out.name=@var{sink} Use the specified source/sink for recording/playback. +@item in|out.channel-map=@var{map} +Use the specified channel map. The default is an OSS compatible +channel map. Do not forget to escape commas inside the map: + +@example +-audiodev pa,id=example,sink.channel-map=front-left,,front-right +@end example + @end table @item -audiodev sdl,id=@var{id}[,@var{prop}[=@var{value}][,...]]
Signature · 1 line
2.23.0
PATCH v4 14/24 audio: split ctl_* functions into enable_* and volume_*
In reply to Kővágó, ZoltánThis way we no longer need vararg functions, improving compile time error detection. Also now it's possible to check actually what commands are supported, without needing to manually update ctl_caps.
--- audio/alsaaudio.c | 62 ++++++++---------- audio/audio.c | 45 ++++++++----- audio/audio_int.h | 15 ++--- audio/audio_template.h | 1 - audio/coreaudio.c | 13 ++-- audio/dsoundaudio.c | 50 ++++++--------- audio/noaudio.c | 14 ++--- audio/ossaudio.c | 79 +++++++++-------------- audio/paaudio.c | 139 +++++++++++++++++------------------------ audio/sdlaudio.c | 17 +---- audio/spiceaudio.c | 102 +++++++++++++----------------- audio/wavaudio.c | 7 +-- 12 files changed, 223 insertions(+), 321 deletions(-)
audio/alsaaudio.c +25 -37
@@ -731,34 +731,28 @@ static int alsa_voice_ctl (snd_pcm_t *handle, const char *typ, int ctl)return 0 ; } -static int alsa_ctl_out (HWVoiceOut *hw,int cmd, ...) +static void alsa_enable_out (HWVoiceOut *hw,bool enable) {ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.out; -switch (cmd) { -case VOICE_ENABLE : - { -bool poll_mode = apdo->try_poll; +if (enable) { +bool poll_mode = apdo->try_poll; -ldebug ("enabling voice\n" ); -if (poll_mode &&alsa_poll_out (hw)) { - poll_mode =0 ; - } - hw->poll_mode = poll_mode; -return alsa_voice_ctl (alsa->handle,"playback" ,VOICE_CTL_PREPARE ); +ldebug ("enabling voice\n" ); +if (poll_mode &&alsa_poll_out (hw)) { + poll_mode =0 ; } - -case VOICE_DISABLE : -ldebug ("disabling voice\n" ); + hw->poll_mode = poll_mode; +alsa_voice_ctl (alsa->handle,"playback" ,VOICE_CTL_PREPARE ); + }else { +ldebug ("disabling voice\n" );if (hw->poll_mode) { hw->poll_mode =0 ; -alsa_fini_poll (&alsa->pollhlp); +alsa_fini_poll (&alsa->pollhlp); } -return alsa_voice_ctl (alsa->handle,"playback" ,VOICE_CTL_PAUSE ); +alsa_voice_ctl (alsa->handle,"playback" ,VOICE_CTL_PAUSE ); } - -return -1 ; }static int alsa_init_in (HWVoiceIn *hw,struct audsettings *as,void *drv_opaque)
@@ -841,35 +835,29 @@ static size_t alsa_read(HWVoiceIn *hw, void *buf, size_t len)return pos; } -static int alsa_ctl_in (HWVoiceIn *hw,int cmd, ...) +static void alsa_enable_in (HWVoiceIn *hw,bool enable) {ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;AudiodevAlsaPerDirectionOptions *apdo = alsa->dev->u.alsa.in; -switch (cmd) { -case VOICE_ENABLE : - { -bool poll_mode = apdo->try_poll; +if (enable) { +bool poll_mode = apdo->try_poll; -ldebug ("enabling voice\n" ); -if (poll_mode &&alsa_poll_in (hw)) { - poll_mode =0 ; - } - hw->poll_mode = poll_mode; - -return alsa_voice_ctl (alsa->handle,"capture" ,VOICE_CTL_START ); +ldebug ("enabling voice\n" ); +if (poll_mode &&alsa_poll_in (hw)) { + poll_mode =0 ; } + hw->poll_mode = poll_mode; -case VOICE_DISABLE : +alsa_voice_ctl (alsa->handle,"capture" ,VOICE_CTL_START ); + }else {ldebug ("disabling voice\n" );if (hw->poll_mode) { hw->poll_mode =0 ; -alsa_fini_poll (&alsa->pollhlp); +alsa_fini_poll (&alsa->pollhlp); } -return alsa_voice_ctl (alsa->handle,"capture" ,VOICE_CTL_PAUSE ); +alsa_voice_ctl (alsa->handle,"capture" ,VOICE_CTL_PAUSE ); } - -return -1 ; }static void alsa_init_per_direction (AudiodevAlsaPerDirectionOptions *apdo)
@@ -924,12 +912,12 @@ static struct audio_pcm_ops alsa_pcm_ops = { .init_out = alsa_init_out, .fini_out = alsa_fini_out, .write = alsa_write, - .ctl_out = alsa_ctl_out, + .enable_out = alsa_enable_out, .init_in = alsa_init_in, .fini_in = alsa_fini_in, .read = alsa_read, - .ctl_in = alsa_ctl_in, + .enable_in = alsa_enable_in, };static struct audio_driver alsa_audio_driver = {
audio/audio.c +28 -17
117 lines not on this page · raw message
audio/audio_int.h +4 -11
@@ -74,7 +74,6 @@ typedef struct HWVoiceOut {size_t samples; QLIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head; QLIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head; -int ctl_caps;struct audio_pcm_ops *pcm_ops;QLIST_ENTRY (HWVoiceOut) entries; } HWVoiceOut;
@@ -96,7 +95,6 @@ typedef struct HWVoiceIn {size_t samples; QLIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head; -int ctl_caps;struct audio_pcm_ops *pcm_ops;QLIST_ENTRY (HWVoiceIn) entries; } HWVoiceIn;
@@ -148,7 +146,6 @@ struct audio_driver {int max_voices_in;int voice_size_out;int voice_size_in; -int ctl_caps;QLIST_ENTRY (audio_driver) next; };
@@ -168,14 +165,16 @@ struct audio_pcm_ops {* size may be smaller */ size_t (*put_buffer_out)(HWVoiceOut *hw,void *buf,size_t size); -int (*ctl_out) (HWVoiceOut *hw,int cmd, ...); +void (*enable_out)(HWVoiceOut *hw,bool enable); +void (*volume_out)(HWVoiceOut *hw,struct mixeng_volume *vol);int (*init_in) (HWVoiceIn *hw,audsettings *as,void *drv_opaque);void (*fini_in) (HWVoiceIn *hw);size_t (*read) (HWVoiceIn *hw,void *buf,size_t size);void *(*get_buffer_in)(HWVoiceIn *hw,size_t *size);void (*put_buffer_in)(HWVoiceIn *hw,void *buf,size_t size); -int (*ctl_in) (HWVoiceIn *hw,int cmd, ...); +void (*enable_in)(HWVoiceIn *hw,bool enable); +void (*volume_in)(HWVoiceIn *hw,struct mixeng_volume *vol); };void *audio_generic_get_buffer_in (HWVoiceIn *hw,size_t *size);
@@ -251,12 +250,6 @@ void audio_rate_start(RateCtl *rate);size_t audio_rate_get_bytes (struct audio_pcm_info *info,RateCtl *rate,size_t bytes_avail); -#define VOICE_ENABLE 1 -#define VOICE_DISABLE 2 -#define VOICE_VOLUME 3 - -#define VOICE_VOLUME_CAP (1 << VOICE_VOLUME) -static inline size_t audio_ring_dist (size_t dst,size_t src,size_t len) {return (dst >= src) ? (dst - src) : (len - src + dst);
audio/audio_template.h +0 -1
@@ -254,7 +254,6 @@ static HW *glue(audio_pcm_hw_add_new_, TYPE)(AudioState *s, hw->s = s; hw->pcm_ops = drv->pcm_ops; - hw->ctl_caps = drv->ctl_caps;QLIST_INIT (&hw->sw_head);#ifdef DAC
audio/coreaudio.c +4 -9
@@ -648,13 +648,12 @@ static void coreaudio_fini_out (HWVoiceOut *hw) } } -static int coreaudio_ctl_out (HWVoiceOut *hw,int cmd, ...) +static void coreaudio_enable_out (HWVoiceOut *hw,bool enable) {OSStatus status;coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw; -switch (cmd) { -case VOICE_ENABLE : +if (enable) {/* start playback */ if (!isPlaying (core->outputDeviceID)) { status =AudioDeviceStart (core->outputDeviceID, core->ioprocid);
@@ -662,9 +661,7 @@ static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)coreaudio_logerr (status,"Could not resume playback\n" ); } } -break ; - -case VOICE_DISABLE : + }else {/* stop playback */ if (!audio_is_cleaning_up ()) {if (isPlaying (core->outputDeviceID)) {
@@ -675,9 +672,7 @@ static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...) } } } -break ; } -return 0 ; }static void *coreaudio_audio_init (Audiodev *dev)
@@ -695,7 +690,7 @@ static struct audio_pcm_ops coreaudio_pcm_ops = { .write = coreaudio_write, .get_buffer_out = coreaudio_get_buffer_out, .put_buffer_out = coreaudio_put_buffer_out_nowrite, - .ctl_out = coreaudio_ctl_out + .enable_out = coreaudio_enable_out };static struct audio_driver coreaudio_audio_driver = {
audio/dsoundaudio.c +20 -30
140 lines not on this page · raw message
audio/noaudio.c +6 -8
@@ -62,14 +62,13 @@ static void no_fini_out (HWVoiceOut *hw) (void ) hw; } -static int no_ctl_out (HWVoiceOut *hw,int cmd, ...) +static void no_enable_out (HWVoiceOut *hw,bool enable) {NoVoiceOut *no = (NoVoiceOut *) hw; -if (cmd ==VOICE_ENABLE ) { +if (enable) {audio_rate_start (&no->rate); } -return 0 ; }static int no_init_in (HWVoiceIn *hw,struct audsettings *as,void *drv_opaque)
@@ -96,14 +95,13 @@ static size_t no_read(HWVoiceIn *hw, void *buf, size_t size)return bytes; } -static int no_ctl_in (HWVoiceIn *hw,int cmd, ...) +static void no_enable_in (HWVoiceIn *hw,bool enable) {NoVoiceIn *no = (NoVoiceIn *) hw; -if (cmd ==VOICE_ENABLE ) { +if (enable) {audio_rate_start (&no->rate); } -return 0 ; }static void *no_audio_init (Audiodev *dev)
@@ -120,12 +118,12 @@ static struct audio_pcm_ops no_pcm_ops = { .init_out = no_init_out, .fini_out = no_fini_out, .write = no_write, - .ctl_out = no_ctl_out, + .enable_out = no_enable_out, .init_in = no_init_in, .fini_in = no_fini_in, .read = no_read, - .ctl_in = no_ctl_in + .enable_in = no_enable_in };static struct audio_driver no_audio_driver = {
audio/ossaudio.c +31 -48
135 lines not on this page · raw message
audio/paaudio.c +56 -83
177 lines not on this page · raw message
audio/sdlaudio.c +3 -14
@@ -285,20 +285,9 @@ static int sdl_init_out(HWVoiceOut *hw, struct audsettings *as,return 0 ; } -static int sdl_ctl_out (HWVoiceOut *hw,int cmd, ...) +static void sdl_enable_out (HWVoiceOut *hw,bool enable) { - (void ) hw; - -switch (cmd) { -case VOICE_ENABLE : -SDL_PauseAudio (0 ); -break ; - -case VOICE_DISABLE : -SDL_PauseAudio (1 ); -break ; - } -return 0 ; +SDL_PauseAudio (!enable); }static void *sdl_audio_init (Audiodev *dev)
@@ -334,7 +323,7 @@ static struct audio_pcm_ops sdl_pcm_ops = { .write = sdl_write, .get_buffer_out = sdl_get_buffer_out, .put_buffer_out = sdl_put_buffer_out_nowrite, - .ctl_out = sdl_ctl_out, + .enable_out = sdl_enable_out, };static struct audio_driver sdl_audio_driver = {
audio/spiceaudio.c +43 -59
163 lines not on this page · raw message
audio/wavaudio.c +3 -4
@@ -173,14 +173,13 @@ static void wav_fini_out (HWVoiceOut *hw) wav->f =NULL ; } -static int wav_ctl_out (HWVoiceOut *hw,int cmd, ...) +static void wav_enable_out (HWVoiceOut *hw,bool enable) {WAVVoiceOut *wav = (WAVVoiceOut *) hw; -if (cmd ==VOICE_ENABLE ) { +if (enable) {audio_rate_start (&wav->rate); } -return 0 ; }static void *wav_audio_init (Audiodev *dev)
@@ -198,7 +197,7 @@ static struct audio_pcm_ops wav_pcm_ops = { .init_out = wav_init_out, .fini_out = wav_fini_out, .write = wav_write_out, - .ctl_out = wav_ctl_out, + .enable_out = wav_enable_out, };static struct audio_driver wav_audio_driver = {
Signature · 1 line
2.23.0
PATCH v4 19/24 audio: replace shift in audio_pcm_info with bytes_per_frame
In reply to Kővágó, ZoltánThe bit shifting trick worked because the number of bytes per frame was always a power-of-two (since QEMU only supports mono, stereo and 8, 16 and 32 bit samples). But if we want to add support for surround sound, this no longer holds true.
--- audio/alsaaudio.c | 11 +++--- audio/audio.c | 74 ++++++++++++++++++++--------------------- audio/audio_int.h | 3 +- audio/coreaudio.c | 4 +-- audio/dsound_template.h | 10 +++--- audio/dsoundaudio.c | 4 +-- audio/noaudio.c | 2 +- audio/ossaudio.c | 14 ++++---- audio/spiceaudio.c | 3 +- audio/wavaudio.c | 6 ++-- 10 files changed, 66 insertions(+), 65 deletions(-)
audio/alsaaudio.c +6 -5
@@ -602,7 +602,7 @@ static size_t alsa_write(HWVoiceOut *hw, void *buf, size_t len) {ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;size_t pos =0 ; -size_t len_frames = len >> hw->info.shift; +size_t len_frames = len / hw->info.bytes_per_frame;while (len_frames) {char *src =advance (buf, pos);
@@ -648,7 +648,7 @@ static size_t alsa_write(HWVoiceOut *hw, void *buf, size_t len) } } - pos += written << hw->info.shift; + pos += written * hw->info.bytes_per_frame;if (written < len_frames) {break ; }
@@ -802,7 +802,8 @@ static size_t alsa_read(HWVoiceIn *hw, void *buf, size_t len)void *dst =advance (buf, pos);snd_pcm_sframes_t nread; - nread =snd_pcm_readi (alsa->handle, dst, len >> hw->info.shift); + nread =snd_pcm_readi ( + alsa->handle, dst, len / hw->info.bytes_per_frame);if (nread <=0 ) {switch (nread) {
@@ -828,8 +829,8 @@ static size_t alsa_read(HWVoiceIn *hw, void *buf, size_t len) } } - pos += nread << hw->info.shift; - len -= nread << hw->info.shift; + pos += nread * hw->info.bytes_per_frame; + len -= nread * hw->info.bytes_per_frame; }return pos;
audio/audio.c +37 -37
262 lines not on this page · raw message
audio/audio_int.h +1 -2
@@ -43,8 +43,7 @@ struct audio_pcm_info {int sign;int freq;int nchannels; -int align; -int shift; +int bytes_per_frame;int bytes_per_second;int swap_endianness; };
audio/coreaudio.c +2 -2
@@ -440,7 +440,7 @@ static OSStatus audioDeviceIOProc( } frameCount = core->audioDevicePropertyBufferFrameSize; - pending_frames = hw->pending_emul >> hw->info.shift; + pending_frames = hw->pending_emul / hw->info.bytes_per_frame;/* if there are not enough samples, set signal and return */ if (pending_frames < frameCount) {
@@ -449,7 +449,7 @@ static OSStatus audioDeviceIOProc(return 0 ; } - len = frameCount << hw->info.shift; + len = frameCount * hw->info.bytes_per_frame;while (len) {size_t write_len;ssize_t start = ((ssize_t ) hw->pos_emul) - hw->pending_emul;
audio/dsound_template.h +5 -5
@@ -98,8 +98,8 @@ static int glue (dsound_lock_, TYPE) ( goto fail; } -if ((p1p && *p1p && (*blen1p & info->align)) || - (p2p && *p2p && (*blen2p & info->align))) { +if ((p1p && *p1p && (*blen1p % info->bytes_per_frame)) || + (p2p && *p2p && (*blen2p % info->bytes_per_frame))) {dolog ("DirectSound returned misaligned buffer %ld %ld\n" , *blen1p, *blen2p); glue(dsound_unlock_, TYPE)(buf, *p1p, p2p ? *p2p : NULL, *blen1p,
@@ -247,14 +247,14 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as, obt_as.endianness =0 ;audio_pcm_init_info (&hw->info, &obt_as); -if (bc.dwBufferBytes & hw->info.align) { +if (bc.dwBufferBytes % hw->info.bytes_per_frame) {dolog ("GetCaps returned misaligned buffer size %ld, alignment %d\n" , - bc.dwBufferBytes, hw->info.align +1 + bc.dwBufferBytes, hw->info.bytes_per_frame ); } hw->size_emul = bc.dwBufferBytes; - hw->samples = bc.dwBufferBytes >> hw->info.shift; + hw->samples = bc.dwBufferBytes / hw->info.bytes_per_frame; ds->s = s;#ifdef DEBUG_DSOUND
audio/dsoundaudio.c +2 -2
@@ -320,8 +320,8 @@ static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,return ; } - len1 = blen1 >> hw->info.shift; - len2 = blen2 >> hw->info.shift; + len1 = blen1 / hw->info.bytes_per_frame; + len2 = blen2 / hw->info.bytes_per_frame;#ifdef DEBUG_DSOUND dolog ("clear %p,%ld,%ld %p,%ld,%ld\n",
audio/noaudio.c +1 -1
@@ -91,7 +91,7 @@ static size_t no_read(HWVoiceIn *hw, void *buf, size_t size)NoVoiceIn *no = (NoVoiceIn *) hw;int64_t bytes =audio_rate_get_bytes (&hw->info, &no->rate, size); -audio_pcm_info_clear_buf (&hw->info, buf, bytes >> hw->info.shift); +audio_pcm_info_clear_buf (&hw->info, buf, bytes / hw->info.bytes_per_frame);return bytes; }
audio/ossaudio.c +7 -7
@@ -506,16 +506,16 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as, oss->nfrags = obt.nfrags; oss->fragsize = obt.fragsize; -if (obt.nfrags * obt.fragsize & hw->info.align) { +if (obt.nfrags * obt.fragsize % hw->info.bytes_per_frame) {dolog ("warning: Misaligned DAC buffer, size %d, alignment %d\n" , - obt.nfrags * obt.fragsize, hw->info.align +1 ); + obt.nfrags * obt.fragsize, hw->info.bytes_per_frame); } - hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift; + hw->samples = (obt.nfrags * obt.fragsize) / hw->info.bytes_per_frame; oss->mmapped =0 ;if (oopts->has_try_mmap && oopts->try_mmap) { - hw->size_emul = hw->samples << hw->info.shift; + hw->size_emul = hw->samples * hw->info.bytes_per_frame; hw->buf_emul = mmap( NULL, hw->size_emul,
@@ -644,12 +644,12 @@ static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque) oss->nfrags = obt.nfrags; oss->fragsize = obt.fragsize; -if (obt.nfrags * obt.fragsize & hw->info.align) { +if (obt.nfrags * obt.fragsize % hw->info.bytes_per_frame) {dolog ("warning: Misaligned ADC buffer, size %d, alignment %d\n" , - obt.nfrags * obt.fragsize, hw->info.align +1 ); + obt.nfrags * obt.fragsize, hw->info.bytes_per_frame); } - hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift; + hw->samples = (obt.nfrags * obt.fragsize) / hw->info.bytes_per_frame; oss->fd = fd; oss->dev = dev;
audio/spiceaudio.c +2 -1
@@ -131,7 +131,8 @@ static void *line_out_get_buffer(HWVoiceOut *hw, size_t *size)if (out->frame) { *size =audio_rate_get_bytes ( - &hw->info, &out->rate, (out->fsize - out->fpos) << hw->info.shift); + &hw->info, &out->rate, + (out->fsize - out->fpos) * hw->info.bytes_per_frame); }else {audio_rate_start (&out->rate); }
audio/wavaudio.c +3 -3
@@ -43,14 +43,14 @@ static size_t wav_write_out(HWVoiceOut *hw, void *buf, size_t len) {WAVVoiceOut *wav = (WAVVoiceOut *) hw;int64_t bytes =audio_rate_get_bytes (&hw->info, &wav->rate, len); -assert (bytes >> hw->info.shift << hw->info.shift == bytes); +assert (bytes % hw->info.bytes_per_frame ==0 );if (bytes &&fwrite (buf, bytes,1 , wav->f) !=1 ) {dolog ("wav_write_out: fwrite of %" PRId64" bytes failed\nReason: %s\n" , bytes,strerror (errno)); } - wav->total_samples += bytes >> hw->info.shift; + wav->total_samples += bytes / hw->info.bytes_per_frame;return bytes; }
@@ -134,7 +134,7 @@ static void wav_fini_out (HWVoiceOut *hw)WAVVoiceOut *wav = (WAVVoiceOut *) hw;uint8_t rlen[4 ];uint8_t dlen[4 ]; -uint32_t datalen = wav->total_samples << hw->info.shift; +uint32_t datalen = wav->total_samples * hw->info.bytes_per_frame;uint32_t rifflen = datalen +36 ;if (!wav->f) {
Signature · 1 line
2.23.0
Re: [PATCH v4 15/24] audio: add mixing-engine option (documentation)
In reply to Kővágó, ZoltánQuoted 39 lines · Zoltán
This will allow us to disable mixeng when we use a decent backend.
Disabling mixeng have a few advantages:
* we no longer convert the audio output from one format to another, when the underlying audio system would just convert it to a third format. We no longer convert, only the underlying system, when needed.
* the underlying system probably has better resampling and sample format converting methods anyway...
* we may support formats that the mixeng currently does not support (S24 or float samples, more than two channels)
* when using an audio server (like pulseaudio) different sound card outputs will show up as separate streams, even if we use only one backendDisadvantages:
* audio capturing no longer works (wavcapture, and vnc audio extension)
* some backends only support a single playback stream or very picky about the audio format. In this case we can't disable mixeng.However mixeng is not removed, only made optional, so this shouldn't be a big concern.
Signed-off-by Kővágó, Zoltán <[email protected]>---
Notes:
Changes from v1:* renamed mixeng to mixing-engine
qapi/audio.json | 5 +++++ qemu-options.hx | 6 ++++++ 2 files changed, 11 insertions(+)qapi/audio.json +0
-0
qapi/audio.json line 17
Quoted 7 lines
qapi/audio.json +4
-0@@ -11,6 +11,10 @@ # General audio backend options that are used for both playback and # recording. # +# @mixing-engine: use QEMU's mixing engine to mix all streams inside QEMU. When +# set to off, fixed-settings must be also off. Not every backend +# compatible with the off setting (default on, since 4.2) +#
Last sentence no verb.
Which backends are compatible?
What happens when you try the off setting with incompatible backends?
Quoted 35 lines
qapi/audio.json +1
-0# @fixed-settings: use fixed settings for host input/output. When off, # frequency, channels and format must not be # specified (default true)@@ -31,6 +35,7 @@ ## { 'struct': 'AudiodevPerDirectionOptions', 'data': { + '*mixing-engine': 'bool', '*fixed-settings': 'bool', '*frequency': 'uint32', '*channels': 'uint32',qemu-options.hx +6
-0@@ -433,6 +433,7 @@ DEF("audiodev", HAS_ARG, QEMU_OPTION_audiodev, " specifies the audio backend to use\n" " id= identifier of the backend\n" " timer-period= timer period in microseconds\n" + " in|out.mixing-engine= use mixing engine to mix streams inside QEMU\n" " in|out.fixed-settings= use fixed settings for host audio\n" " in|out.frequency= frequency to use with fixed settings\n" " in|out.channels= number of channels to use with fixed settings\n"@@ -503,6 +504,11 @@ Identifies the audio backend. Sets the timer @var{period} used by the audio subsystem in microseconds. Default is 10000 (10 ms). +@item in|out.mixing-engine=on|off +Use QEMU's mixing engine to mix all streams inside QEMU. When off, +@var{fixed-settings} must be off too. Not every backend is fully +compatible with the off setting. Default is on. + @item in|out.fixed-settings=on|off Use fixed settings for host audio. When off, it will change based on how the guest opens the sound card. In this case you must not specify
Re: [PATCH v4 21/24] paaudio: channel-map option
In reply to Kővágó, ZoltánQuoted 98 lines · Zoltán
Add an option to change the channel map used by pulseaudio. If not specified, falls back to an OSS compatible channel map.
Signed-off-by Kővágó, Zoltán <[email protected]>--- audio/paaudio.c | 18 ++++++++++++++---- qapi/audio.json | 7 +++++-- qemu-options.hx | 9 +++++++++ 3 files changed, 28 insertions(+), 6 deletions(-)audio/paaudio.c +14
-4@@ -338,17 +338,27 @@ static pa_stream *qpa_simple_new (pa_stream_direction_t dir, const char *dev, const pa_sample_spec *ss, - const pa_channel_map *map, + const char *map, const pa_buffer_attr *attr, int *rerror) { int r;pa_stream *stream;pa_stream_flags_t flags; +pa_channel_map pa_map;pa_threaded_mainloop_lock (c->mainloop); - stream =pa_stream_new (c->context, name, ss, map); +if (map && !pa_channel_map_parse (&pa_map, map)) { +dolog ("Invalid channel map specified: '%s'\n" , map); + map =NULL ; + } +if (!map) { +pa_channel_map_init_extend (&pa_map, ss->channels, +PA_CHANNEL_MAP_OSS ); + } + + stream =pa_stream_new (c->context, name, ss, &pa_map);if (!stream) { goto fail; }@@ -421,7 +431,7 @@ static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,PA_STREAM_PLAYBACK , ppdo->has_name ? ppdo->name :NULL , &ss, -NULL ,/* channel map */ + ppdo->has_channel_map ? ppdo->channel_map :NULL , &ba,/* buffering attributes */ &error );@@ -470,7 +480,7 @@ static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)PA_STREAM_RECORD , ppdo->has_name ? ppdo->name :NULL , &ss, -NULL ,/* channel map */ + ppdo->has_channel_map ? ppdo->channel_map :NULL , &ba,/* buffering attributes */ &error );qapi/audio.json +5
-2@@ -214,13 +214,16 @@ # @latency: latency you want PulseAudio to achieve in microseconds # (default 15000) # +# @channel-map: channel map to use (default: OSS compatible map, since: 4.2) +# # Since: 4.0 ## { 'struct': 'AudiodevPaPerDirectionOptions', 'base': 'AudiodevPerDirectionOptions', 'data': { - '*name': 'str', - '*latency': 'uint32' } } + '*name': 'str', + '*latency': 'uint32', + '*channel-map': 'str' } } ## # @AudiodevPaOptions:qemu-options.hx +1
-0@@ -471,6 +471,7 @@ DEF("audiodev", HAS_ARG, QEMU_OPTION_audiodev, "-audiodev pa,id=id[,prop[=value][,...]]\n" " server= PulseAudio server address\n" " in|out.name= source/sink device name\n" + " in|out.channel-map= channel map to use\n" #endif #ifdef CONFIG_AUDIO_SDL "-audiodev sdl,id=id[,prop[=value][,...]]\n"
qemu-options.hx Sets the PulseAudio @var{server} to connect to. · line 642
Quoted 6 lines
qemu-options.hx +3
-0@@ -636,6 +637,14 @@ Sets the PulseAudio @var{server} to connect to. @item in|out.name=@var{sink} Use the specified source/sink for recording/playback. +@item in|out.channel-map=@var{map} +Use the specified channel map. The default is an OSS compatible +channel map. Do not forget to escape commas inside the map:
Awkward.
qemu-options.hx Sets the PulseAudio @var{server} to connect to. · line 646
Quoted 4 lines
qemu-options.hx +4
-0+ +@example +-audiodev pa,id=example,sink.channel-map=front-left,,front-right +@end example
Makes me realize new AudiodevPaPerDirectionOptions member @channel-map is a list encoded in a string. QAPI heavily frowns upon encoding stuff in strings. Any reason why you can't (or don't want to) make it ['str']?
Quoted 4 lines
qemu-options.hx +1
-0+ @end table @item -audiodev sdl,id=@var{id}[,@var{prop}[=@var{value}][,...]]
Re: [PATCH v4 15/24] audio: add mixing-engine option (documentation)
In reply to Markus ArmbrusterQuoted 51 lines · Markus Armbruster
Quoted 39 lines · Zoltán
This will allow us to disable mixeng when we use a decent backend.
Disabling mixeng have a few advantages:
* we no longer convert the audio output from one format to another, when the underlying audio system would just convert it to a third format. We no longer convert, only the underlying system, when needed.
* the underlying system probably has better resampling and sample format converting methods anyway...
* we may support formats that the mixeng currently does not support (S24 or float samples, more than two channels)
* when using an audio server (like pulseaudio) different sound card outputs will show up as separate streams, even if we use only one backendDisadvantages:
* audio capturing no longer works (wavcapture, and vnc audio extension)
* some backends only support a single playback stream or very picky about the audio format. In this case we can't disable mixeng.However mixeng is not removed, only made optional, so this shouldn't be a big concern.
Signed-off-by Kővágó, Zoltán <[email protected]>---
Notes:
Changes from v1:* renamed mixeng to mixing-engine
qapi/audio.json | 5 +++++ qemu-options.hx | 6 ++++++ 2 files changed, 11 insertions(+)qapi/audio.json +0
-0qapi/audio.json line 17
Quoted 7 lines
qapi/audio.json +4
-0@@ -11,6 +11,10 @@ # General audio backend options that are used for both playback and # recording. # +# @mixing-engine: use QEMU's mixing engine to mix all streams inside QEMU. When +# set to off, fixed-settings must be also off. Not every backend +# compatible with the off setting (default on, since 4.2) +#Last sentence no verb.
Quoted 1 line
Which backends are compatible?
Actually that's a simplification, it depends on a few things. When mixeng is off, qemu will try to use the same format as the emulated sound card, and if the backend doesn't support that format, it won't work (no audio). Also attaching multiple sound cards to the same audiodev might not work, if the backend doesn't support multiple playback streams. If you use pulseaudio, it'll work without problems, if you use alsa, it depends on your device. If you use a hw: device directly, you'll likely only be able to use one emulated sound card with a few selected audio formats. If you use dmix: (and plug), alsa will handle the conversion and mixing, so it will work no matter what format the emulated sound card uses. With OSS the situation is probably similar, it depends on the kernel/hw what works and what not. wav and spice certainly doesn't support multiple streams. I'm not completely sure about the other backends right now, but I think dsound and coreaudio can handle the necessary sample format conversions and mixing.
Quoted 1 line
What happens when you try the off setting with incompatible backends?
See above.
Quoted 36 lines
Quoted 36 lines
qapi/audio.json +1
-0# @fixed-settings: use fixed settings for host input/output. When off, # frequency, channels and format must not be # specified (default true)@@ -31,6 +35,7 @@ ## { 'struct': 'AudiodevPerDirectionOptions', 'data': { + '*mixing-engine': 'bool', '*fixed-settings': 'bool', '*frequency': 'uint32', '*channels': 'uint32',qemu-options.hx +6
-0@@ -433,6 +433,7 @@ DEF("audiodev", HAS_ARG, QEMU_OPTION_audiodev, " specifies the audio backend to use\n" " id= identifier of the backend\n" " timer-period= timer period in microseconds\n" + " in|out.mixing-engine= use mixing engine to mix streams inside QEMU\n" " in|out.fixed-settings= use fixed settings for host audio\n" " in|out.frequency= frequency to use with fixed settings\n" " in|out.channels= number of channels to use with fixed settings\n"@@ -503,6 +504,11 @@ Identifies the audio backend. Sets the timer @var{period} used by the audio subsystem in microseconds. Default is 10000 (10 ms). +@item in|out.mixing-engine=on|off +Use QEMU's mixing engine to mix all streams inside QEMU. When off, +@var{fixed-settings} must be off too. Not every backend is fully +compatible with the off setting. Default is on. + @item in|out.fixed-settings=on|off Use fixed settings for host audio. When off, it will change based on how the guest opens the sound card. In this case you must not specify
Re: [PATCH v4 21/24] paaudio: channel-map option
In reply to Markus ArmbrusterQuoted 114 lines · Markus Armbruster
Quoted 98 lines · Zoltán
Add an option to change the channel map used by pulseaudio. If not specified, falls back to an OSS compatible channel map.
Signed-off-by Kővágó, Zoltán <[email protected]>--- audio/paaudio.c | 18 ++++++++++++++---- qapi/audio.json | 7 +++++-- qemu-options.hx | 9 +++++++++ 3 files changed, 28 insertions(+), 6 deletions(-)audio/paaudio.c +14
-4@@ -338,17 +338,27 @@ static pa_stream *qpa_simple_new (pa_stream_direction_t dir, const char *dev, const pa_sample_spec *ss, - const pa_channel_map *map, + const char *map, const pa_buffer_attr *attr, int *rerror) { int r;pa_stream *stream;pa_stream_flags_t flags; +pa_channel_map pa_map;pa_threaded_mainloop_lock (c->mainloop); - stream =pa_stream_new (c->context, name, ss, map); +if (map && !pa_channel_map_parse (&pa_map, map)) { +dolog ("Invalid channel map specified: '%s'\n" , map); + map =NULL ; + } +if (!map) { +pa_channel_map_init_extend (&pa_map, ss->channels, +PA_CHANNEL_MAP_OSS ); + } + + stream =pa_stream_new (c->context, name, ss, &pa_map);if (!stream) { goto fail; }@@ -421,7 +431,7 @@ static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,PA_STREAM_PLAYBACK , ppdo->has_name ? ppdo->name :NULL , &ss, -NULL ,/* channel map */ + ppdo->has_channel_map ? ppdo->channel_map :NULL , &ba,/* buffering attributes */ &error );@@ -470,7 +480,7 @@ static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)PA_STREAM_RECORD , ppdo->has_name ? ppdo->name :NULL , &ss, -NULL ,/* channel map */ + ppdo->has_channel_map ? ppdo->channel_map :NULL , &ba,/* buffering attributes */ &error );qapi/audio.json +5
-2@@ -214,13 +214,16 @@ # @latency: latency you want PulseAudio to achieve in microseconds # (default 15000) # +# @channel-map: channel map to use (default: OSS compatible map, since: 4.2) +# # Since: 4.0 ## { 'struct': 'AudiodevPaPerDirectionOptions', 'base': 'AudiodevPerDirectionOptions', 'data': { - '*name': 'str', - '*latency': 'uint32' } } + '*name': 'str', + '*latency': 'uint32', + '*channel-map': 'str' } } ## # @AudiodevPaOptions:qemu-options.hx +1
-0@@ -471,6 +471,7 @@ DEF("audiodev", HAS_ARG, QEMU_OPTION_audiodev, "-audiodev pa,id=id[,prop[=value][,...]]\n" " server= PulseAudio server address\n" " in|out.name= source/sink device name\n" + " in|out.channel-map= channel map to use\n" #endif #ifdef CONFIG_AUDIO_SDL "-audiodev sdl,id=id[,prop[=value][,...]]\n"qemu-options.hx Sets the PulseAudio @var{server} to connect to. · line 642
Quoted 6 lines
qemu-options.hx +3
-0@@ -636,6 +637,14 @@ Sets the PulseAudio @var{server} to connect to. @item in|out.name=@var{sink} Use the specified source/sink for recording/playback. +@item in|out.channel-map=@var{map} +Use the specified channel map. The default is an OSS compatible +channel map. Do not forget to escape commas inside the map:Awkward.
qemu-options.hx Sets the PulseAudio @var{server} to connect to. · line 646
Quoted 4 lines
qemu-options.hx +4
-0+ +@example +-audiodev pa,id=example,sink.channel-map=front-left,,front-right +@end example
Quoted 4 lines
Makes me realize new AudiodevPaPerDirectionOptions member @channel-map is a list encoded in a string. QAPI heavily frowns upon encoding stuff in strings. Any reason why you can't (or don't want to) make it ['str']?
Hmm, I don't think it's used too frequently on structs parsed by qapi opts visitor. What would be the command line format in that case? Something like this?
-audiodev pa,id=example,sink.channel-map=front-left,sink.channel-map=front-right
I think it's simply a string because while conceptually it's a string, we don't try to interpret it, we just pass the string to pa_channel_map_parse. Of course we could take a list and instead either rebuild the string or reimplement half of pa_channel_map_parse by manually calling pa_channel_position_from_string. Oh now that I looked again at the pulseaudio docs, channel-map doesn't have to be a list, it can be also a "well-known mapping name".
Quoted 5 lines
Quoted 5 lines
qemu-options.hx +1
-0+ @end table @item -audiodev sdl,id=@var{id}[,@var{prop}[=@var{value}][,...]]
Re: [PATCH v4 15/24] audio: add mixing-engine option (documentation)
In reply to Zoltán KővágóQuoted 70 lines · Zoltán Kővágó
Quoted 50 lines · Markus Armbruster
Quoted 38 lines · Zoltán
This will allow us to disable mixeng when we use a decent backend.
Disabling mixeng have a few advantages:
* we no longer convert the audio output from one format to another, when the underlying audio system would just convert it to a third format. We no longer convert, only the underlying system, when needed.
* the underlying system probably has better resampling and sample format converting methods anyway...
* we may support formats that the mixeng currently does not support (S24 or float samples, more than two channels)
* when using an audio server (like pulseaudio) different sound card outputs will show up as separate streams, even if we use only one backendDisadvantages:
* audio capturing no longer works (wavcapture, and vnc audio extension)
* some backends only support a single playback stream or very picky about the audio format. In this case we can't disable mixeng.However mixeng is not removed, only made optional, so this shouldn't be a big concern.
Signed-off-by Kővágó, Zoltán <[email protected]>---
Notes:
Changes from v1:
* renamed mixeng to mixing-engineqapi/audio.json | 5 +++++ qemu-options.hx | 6 ++++++ 2 files changed, 11 insertions(+)qapi/audio.json +0
-0qapi/audio.json line 17
Quoted 7 lines
qapi/audio.json +4
-0@@ -11,6 +11,10 @@ # General audio backend options that are used for both playback and # recording. # +# @mixing-engine: use QEMU's mixing engine to mix all streams inside QEMU. When +# set to off, fixed-settings must be also off. Not every backend +# compatible with the off setting (default on, since 4.2) +#Last sentence no verb.
Quoted 1 line
Which backends are compatible?
Actually that's a simplification, it depends on a few things. When mixeng is off, qemu will try to use the same format as the emulated sound card, and if the backend doesn't support that format, it won't work (no audio). Also attaching multiple sound cards to the same audiodev might not work, if the backend doesn't support multiple playback streams. If you use pulseaudio, it'll work without problems, if you use alsa, it depends on your device. If you use a hw: device directly, you'll likely only be able to use one emulated sound card with a few selected audio formats. If you use dmix: (and plug), alsa will handle the conversion and mixing, so it will work no matter what format the emulated sound card uses. With OSS the situation is probably similar, it depends on the kernel/hw what works and what not. wav and spice certainly doesn't support multiple streams. I'm not completely sure about the other backends right now, but I think dsound and coreaudio can handle the necessary sample format conversions and mixing.
Quoted 2 lines
Quoted 1 line
What happens when you try the off setting with incompatible backends?
See above.
What happens *exactly*?
I'm asking because I'm concerned about the user experience. When a user asks for a combination of things QEMU can't provide, such as mixeng off with an incompatible backend, QEMU should fail with a suitable error message. Does it?
Sometimes rejecting non-working configurations is impractical. Is it here?
If yes, we should call out the problematic configurations in documentation.
[...]
Re: [PATCH v4 21/24] paaudio: channel-map option
In reply to Zoltán KővágóQuoted 116 lines · Zoltán Kővágó
Quoted 110 lines · Markus Armbruster
Quoted 95 lines · Zoltán
Add an option to change the channel map used by pulseaudio. If not specified, falls back to an OSS compatible channel map.
Signed-off-by Kővágó, Zoltán <[email protected]>--- audio/paaudio.c | 18 ++++++++++++++---- qapi/audio.json | 7 +++++-- qemu-options.hx | 9 +++++++++ 3 files changed, 28 insertions(+), 6 deletions(-)audio/paaudio.c +14
-3@@ -338,17 +338,27 @@ static pa_stream *qpa_simple_new (pa_stream_direction_t dir, const char *dev, const pa_sample_spec *ss, - const pa_channel_map *map, + const char *map, const pa_buffer_attr *attr, int *rerror) { int r;pa_stream *stream;pa_stream_flags_t flags; +pa_channel_map pa_map;pa_threaded_mainloop_lock (c->mainloop); - stream =pa_stream_new (c->context, name, ss, map); +if (map && !pa_channel_map_parse (&pa_map, map)) { +dolog ("Invalid channel map specified: '%s'\n" , map); + map =NULL ; + } +if (!map) { +pa_channel_map_init_extend (&pa_map, ss->channels, +PA_CHANNEL_MAP_OSS ); + } + + stream =pa_stream_new (c->context, name, ss, &pa_map);if (!stream) { goto fail; }@@ -421,7 +431,7 @@ static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,PA_STREAM_PLAYBACK , ppdo->has_name ? ppdo->name :NULL , &ss, -NULL ,/* channel map */ + ppdo->has_channel_map ? ppdo->channel_map :NULL , &ba,/* buffering attributes */ &error );@@ -470,7 +480,7 @@ static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)PA_STREAM_RECORD , ppdo->has_name ? ppdo->name :NULL , &ss, -NULL ,/* channel map */ + ppdo->has_channel_map ? ppdo->channel_map :NULL , &ba,/* buffering attributes */ &error );qapi/audio.json +5
-2@@ -214,13 +214,16 @@ # @latency: latency you want PulseAudio to achieve in microseconds # (default 15000) # +# @channel-map: channel map to use (default: OSS compatible map, since: 4.2) +# # Since: 4.0 ## { 'struct': 'AudiodevPaPerDirectionOptions', 'base': 'AudiodevPerDirectionOptions', 'data': { - '*name': 'str', - '*latency': 'uint32' } } + '*name': 'str', + '*latency': 'uint32', + '*channel-map': 'str' } } ## # @AudiodevPaOptions:qemu-options.hx +1
-0@@ -471,6 +471,7 @@ DEF("audiodev", HAS_ARG, QEMU_OPTION_audiodev, "-audiodev pa,id=id[,prop[=value][,...]]\n" " server= PulseAudio server address\n" " in|out.name= source/sink device name\n" + " in|out.channel-map= channel map to use\n" #endif #ifdef CONFIG_AUDIO_SDL "-audiodev sdl,id=id[,prop[=value][,...]]\n"qemu-options.hx Sets the PulseAudio @var{server} to connect to. · line 641
Quoted 5 lines
qemu-options.hx +2
-0@@ -636,6 +637,14 @@ Sets the PulseAudio @var{server} to connect to. @item in|out.name=@var{sink} Use the specified source/sink for recording/playback. +@item in|out.channel-map=@var{map} +Use the specified channel map. The default is an OSS compatible +channel map. Do not forget to escape commas inside the map:Awkward.
qemu-options.hx Sets the PulseAudio @var{server} to connect to. · line 646
Quoted 4 lines
qemu-options.hx +4
-0+ +@example +-audiodev pa,id=example,sink.channel-map=front-left,,front-right +@end exampleQuoted 4 lines
Makes me realize new AudiodevPaPerDirectionOptions member @channel-map is a list encoded in a string. QAPI heavily frowns upon encoding stuff in strings. Any reason why you can't (or don't want to) make it ['str']?
Quoted 6 lines
Hmm, I don't think it's used too frequently on structs parsed by qapi opts visitor. What would be the command line format in that case? Something like this?
-audiodev pa,id=example,sink.channel-map=front-left,sink.channel-map=front-right
Let's talk concepts before details. I'll get back to this below.
Quoted 5 lines
I think it's simply a string because while conceptually it's a string, we don't try to interpret it, we just pass the string to
pa_channel_map_parse. Of course we could take a list and instead either rebuild the string or reimplement half ofpa_channel_map_parseby manually callingpa_channel_position_from_string.
Precedence: BlockdevOptionsRbd member @auth-client-required. Under the hood, this is Ceph configuration option auth-client-required. Semantically a list of well-known values. rados_conf_set() takes it encoded in a string. We could have plumbed that straight through QAPI by making @auth-client-required a 'str'. Instead, we made it ['RbdAuthMode']. rbd.c has to encode the list in a string for rados_conf_set().
Why did we bother? Besides the dogmatic reason "do not encode stuff in strings in QAPI", there's a pragmatic one: introspection. @auth-client-required's type ['RbdAuthMode'] tells exactly what the acceptable values are. If Ceph ever grows another mode, QEMU support for it will be visible in introspection: enum RbdAuthMode will have another value.
Baking the acceptable modes into QEMU means new modes need a QEMU update to work. New modes that just work without a QEMU update feel unlikely. If we're wrong, and a QEMU update is impractical, you can still work around the limitation with a Ceph configuration file.
Which of these considerations other than "do not encode in strings" apply to PA I can't say.
Let's turn the question around: is there any reason to break the "do not encode in strings" rule other than "it saves a few lines of code"?
Quoted 2 lines
Oh now that I looked again at the pulseaudio docs, channel-map doesn't have to be a list, it can be also a "well-known mapping name".
Unambiguous because the well-known mapping names are not valid channel position list members.
Semantially, this is a disjoint union of well-known mapping names and channel position lists.
The tools QAPI provides for disjoint unions are union and alternate types. In this case, an alternate of 'str' and ['str'] would do. Well-known mapping names use the 'str' branch, channel position lists the ['str'] branch.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
qemu-options.hx +1
-0+ @end table @item -audiodevsdl,id=@var{id}[,@var{prop}[=@var{value}][,...]]
I promised you to get back to command line syntax.
The opts visitor uses repeated keys for lists, just like you guessed. It supports only a subset of the QAPI types, and has weird magic for integer lists.
-audiodev actually uses the QObject input visitor, which is more general and somewhat less magical. Its CLI syntax for lists is even wordier, though. I'd expect something like
-audiodev pa,id=example,sink.channel-map.0=front-left,sink.channel-map.1=front-right
Aside: CLI QAPIfication will have to find a way to accomodate the opts visitor's syntax and quirks, or break compatibility.
Re: [PATCH v4 21/24] paaudio: channel-map option
Hi,
Quoted 5 lines
Quoted 2 lines
Oh now that I looked again at the pulseaudio docs, channel-map doesn't have to be a list, it can be also a "well-known mapping name".
Unambiguous because the well-known mapping names are not valid channel position list members.
Do we have well-known mapping names for the common use cases? So maybe just support these?
cheers,
Gerd
Re: [PATCH v4 21/24] paaudio: channel-map option
Quoted 5 lines · Gerd Hoffmann
Hi,Quoted 3 lines
Quoted 2 lines
Oh now that I looked again at the pulseaudio docs, channel-map doesn't have to be a list, it can be also a "well-known mapping name".
Quoted 9 lines
Quoted 2 lines
Unambiguous because the well-known mapping names are not valid channel position list members.
Do we have well-known mapping names for the common use cases? So maybe just support these?
cheers,
Gerd
It's surprisingly hard to figure out what are these "well-known names", I could only find them in the source:
https://github.com/michaelwu/pulseaudio/blob/16e3dccfa88455ebd329de27a98a3d979a8bdc8e/src/pulse/channelmap.c#L538
It doesn't provide much over the "auto detect channel maps from the number of channels" pulseaudio already has (the only ambiguous case is surround-41 vs surround-50). IIRC I originally added this feature because USB audio and pulseaudio didn't agree about the order of channels, for example in 5.1 pa defaults to left,right,rear-left,rear-right,front-center,lfe while USB audio expects left,right,center,lfe,rear-left,rear-right order. You can't specify this with the well-known names.
Regards,
Zoltan
Re: [PATCH v4 15/24] audio: add mixing-engine option (documentation)
In reply to Markus ArmbrusterQuoted 73 lines · Markus Armbruster
Quoted 71 lines · Zoltán Kővágó
Quoted 50 lines · Markus Armbruster
Quoted 38 lines · Zoltán
This will allow us to disable mixeng when we use a decent backend.
Disabling mixeng have a few advantages:
* we no longer convert the audio output from one format to another, when the underlying audio system would just convert it to a third format. We no longer convert, only the underlying system, when needed.
* the underlying system probably has better resampling and sample format converting methods anyway...
* we may support formats that the mixeng currently does not support (S24 or float samples, more than two channels)
* when using an audio server (like pulseaudio) different sound card outputs will show up as separate streams, even if we use only one backendDisadvantages:
* audio capturing no longer works (wavcapture, and vnc audio extension)
* some backends only support a single playback stream or very picky about the audio format. In this case we can't disable mixeng.However mixeng is not removed, only made optional, so this shouldn't be a big concern.
Signed-off-by Kővágó, Zoltán <[email protected]>---
Notes:
Changes from v1:
* renamed mixeng to mixing-engineqapi/audio.json | 5 +++++ qemu-options.hx | 6 ++++++ 2 files changed, 11 insertions(+)qapi/audio.json +0
-0qapi/audio.json line 17
Quoted 7 lines
qapi/audio.json +4
-0@@ -11,6 +11,10 @@ # General audio backend options that are used for both playback and # recording. # +# @mixing-engine: use QEMU's mixing engine to mix all streams inside QEMU. When +# set to off, fixed-settings must be also off. Not every backend +# compatible with the off setting (default on, since 4.2) +#Last sentence no verb.
Quoted 1 line
Which backends are compatible?
Actually that's a simplification, it depends on a few things. When mixeng is off, qemu will try to use the same format as the emulated sound card, and if the backend doesn't support that format, it won't work (no audio). Also attaching multiple sound cards to the same audiodev might not work, if the backend doesn't support multiple playback streams. If you use pulseaudio, it'll work without problems, if you use alsa, it depends on your device. If you use a hw: device directly, you'll likely only be able to use one emulated sound card with a few selected audio formats. If you use dmix: (and plug), alsa will handle the conversion and mixing, so it will work no matter what format the emulated sound card uses. With OSS the situation is probably similar, it depends on the kernel/hw what works and what not. wav and spice certainly doesn't support multiple streams. I'm not completely sure about the other backends right now, but I think dsound and coreaudio can handle the necessary sample format conversions and mixing.
Quoted 1 line
What happens when you try the off setting with incompatible backends?
Quoted 8 lines
Quoted 1 line
See above.
What happens *exactly*?
I'm asking because I'm concerned about the user experience. When a user asks for a combination of things QEMU can't provide, such as mixeng off with an incompatible backend, QEMU should fail with a suitable error message. Does it?
Error handling is not the best in the audio subsystem, if something fails it generally just prints a warning to the console and continues, and something will happen... For example, this is what happens when I try to open one hw device twice. I ran qemu with:
-audiodev alsa,id=foo,in.dev=hw:1,,0,out.mixing-engine=off,out.dev=hw:1,,0 -device piix4-usb-uhci -device usb-audio,audiodev=foo -device AC97,audiodev=foo
When the guest tried to initialize the AC97 card, I got an error:
alsa: Could not initialize DAC
alsa: Failed to open `hw:1,0':
alsa: Reason: Device or resource busy
And it just continued. And the sound worked, but with wrong sample rate (AC97 wants 44100 Hz, but USB audio previously opened the alsa device with 48000 Hz). I'll fix this bug in the next revision, audio_pcm_hw_add_* shouldn't fall back to other HWs without mixeng. But even with that, the result will be that one emulated sound card will work and the other won't.
It's not ideal, but fixing it would require a lot of effort. Right now, if you specify an invalid audiodev for alsa (even with mixeng), it'll just print an error to the console and continue without audio.
Quoted 2 lines
Sometimes rejecting non-working configurations is impractical. Is it here?
I think it is. It depends on the backend, its settings, the frontend (emulated sound card), and how the guest uses it. We currently don't know what formats does a backend support, what formats can a frontend produce, and even if we would know that, just because a frontend can produce a format that the backend doesn't understand doesn't mean that it will actually do it. For example, right now with this patch series applied, usb-audio can produce 7.1 audio. If we want to be strict, it means we can only use it with backends that support at least 8 channels, even if the user only wants to use stereo audio.
Quoted 2 lines
If yes, we should call out the problematic configurations in documentation.
I think we should rather list known working configurations, and leave the others as "try at your own risk" because there's too many things that can go wrong. (pulseaudio will work, alsa with dmix too. Need to check coreaudio, dsound and oss. spice and wavcapture won't work.)
Regards,
Zoltan
Re: [PATCH v4 21/24] paaudio: channel-map option
In reply to Zoltán KővágóHi,
Quoted 5 lines
vs surround-50). IIRC I originally added this feature because USB audio and pulseaudio didn't agree about the order of channels, for example in 5.1 pa defaults to left,right,rear-left,rear-right,front-center,lfe while USB audio expects left,right,center,lfe,rear-left,rear-right order. You can't specify this with the well-known names.
Shouldn't backend and frontend be able to negotiate a channel map without user invention?
Initially, with only usb-audio supporting 5.1/7.1 we can simply declare the usb-audio channel map as the one which is used. When other devices are added some day (hda-surround?) we might need a ctl function so the frontend can set the channel map (similar to the way volume is handled today).
cheers,
Gerd
Re: [PATCH v4 15/24] audio: add mixing-engine option (documentation)
In reply to Zoltán KővágóQuoted 106 lines · Zoltán Kővágó
Quoted 73 lines · Markus Armbruster
Quoted 71 lines · Zoltán Kővágó
Quoted 50 lines · Markus Armbruster
Quoted 38 lines · Zoltán
This will allow us to disable mixeng when we use a decent backend.
Disabling mixeng have a few advantages:
* we no longer convert the audio output from one format to another, when the underlying audio system would just convert it to a third format. We no longer convert, only the underlying system, when needed.
* the underlying system probably has better resampling and sample format converting methods anyway...
* we may support formats that the mixeng currently does not support (S24 or float samples, more than two channels)
* when using an audio server (like pulseaudio) different sound card outputs will show up as separate streams, even if we use only one backendDisadvantages:
* audio capturing no longer works (wavcapture, and vnc audio extension)
* some backends only support a single playback stream or very picky about the audio format. In this case we can't disable mixeng.However mixeng is not removed, only made optional, so this shouldn't be a big concern.
Signed-off-by Kővágó, Zoltán <[email protected]>---
Notes:
Changes from v1:
* renamed mixeng to mixing-engineqapi/audio.json | 5 +++++ qemu-options.hx | 6 ++++++ 2 files changed, 11 insertions(+)qapi/audio.json +0
-0qapi/audio.json line 17
Quoted 7 lines
qapi/audio.json +4
-0@@ -11,6 +11,10 @@ # General audio backend options that are used for both playback and # recording. # +# @mixing-engine: use QEMU's mixing engine to mix all streams inside QEMU. When +# set to off, fixed-settings must be also off. Not every backend +# compatible with the off setting (default on, since 4.2) +#Last sentence no verb.
Quoted 1 line
Which backends are compatible?
Actually that's a simplification, it depends on a few things. When mixeng is off, qemu will try to use the same format as the emulated sound card, and if the backend doesn't support that format, it won't work (no audio). Also attaching multiple sound cards to the same audiodev might not work, if the backend doesn't support multiple playback streams. If you use pulseaudio, it'll work without problems, if you use alsa, it depends on your device. If you use a hw: device directly, you'll likely only be able to use one emulated sound card with a few selected audio formats. If you use dmix: (and plug), alsa will handle the conversion and mixing, so it will work no matter what format the emulated sound card uses. With OSS the situation is probably similar, it depends on the kernel/hw what works and what not. wav and spice certainly doesn't support multiple streams. I'm not completely sure about the other backends right now, but I think dsound and coreaudio can handle the necessary sample format conversions and mixing.
Quoted 1 line
What happens when you try the off setting with incompatible backends?
Quoted 8 lines
Quoted 1 line
See above.
What happens *exactly*?
I'm asking because I'm concerned about the user experience. When a user asks for a combination of things QEMU can't provide, such as mixeng off with an incompatible backend, QEMU should fail with a suitable error message. Does it?
Error handling is not the best in the audio subsystem, if something fails it generally just prints a warning to the console and continues, and something will happen... For example, this is what happens when I try to open one hw device twice. I ran qemu with:
-audiodev alsa,id=foo,in.dev=hw:1,,0,out.mixing-engine=off,out.dev=hw:1,,0 -device piix4-usb-uhci -device usb-audio,audiodev=foo -device AC97,audiodev=foo
When the guest tried to initialize the AC97 card, I got an error:
alsa: Could not initialize DAC
alsa: Failed to open `hw:1,0':
alsa: Reason: Device or resource busyAnd it just continued. And the sound worked, but with wrong sample rate (AC97 wants 44100 Hz, but USB audio previously opened the alsa device with 48000 Hz). I'll fix this bug in the next revision, audio_pcm_hw_add_* shouldn't fall back to other HWs without mixeng. But even with that, the result will be that one emulated sound card will work and the other won't.
Quoted 3 lines
It's not ideal, but fixing it would require a lot of effort. Right now, if you specify an invalid audiodev for alsa (even with mixeng), it'll just print an error to the console and continue without audio.
Should we document this general error handling deficiency somehow?
Quoted 13 lines
Quoted 2 lines
Sometimes rejecting non-working configurations is impractical. Is it here?
I think it is. It depends on the backend, its settings, the frontend (emulated sound card), and how the guest uses it. We currently don't know what formats does a backend support, what formats can a frontend produce, and even if we would know that, just because a frontend can produce a format that the backend doesn't understand doesn't mean that it will actually do it. For example, right now with this patch series applied, usb-audio can produce 7.1 audio. If we want to be strict, it means we can only use it with backends that support at least 8 channels, even if the user only wants to use stereo audio.
Quoted 7 lines
Quoted 2 lines
If yes, we should call out the problematic configurations in documentation.
I think we should rather list known working configurations, and leave the others as "try at your own risk" because there's too many things that can go wrong. (pulseaudio will work, alsa with dmix too. Need to check coreaudio, dsound and oss. spice and wavcapture won't work.)
Far from ideal, but better than nothing.
Possibly naive idea: what about automatically falling back to mixeng on when mixeng off doesn't work? Requires detecting "doesn't work", which I understand just isn't there. Any other reasons why this couldn't be done? Way out of scope for this series, of course.
Re: [PATCH v4 15/24] audio: add mixing-engine option (documentation)
Quoted 108 lines · Markus Armbruster
Quoted 106 lines · Zoltán Kővágó
Quoted 73 lines · Markus Armbruster
Quoted 71 lines · Zoltán Kővágó
Quoted 50 lines · Markus Armbruster
Quoted 38 lines · Zoltán
This will allow us to disable mixeng when we use a decent backend.
Disabling mixeng have a few advantages:
* we no longer convert the audio output from one format to another, when the underlying audio system would just convert it to a third format. We no longer convert, only the underlying system, when needed.
* the underlying system probably has better resampling and sample format converting methods anyway...
* we may support formats that the mixeng currently does not support (S24 or float samples, more than two channels)
* when using an audio server (like pulseaudio) different sound card outputs will show up as separate streams, even if we use only one backendDisadvantages:
* audio capturing no longer works (wavcapture, and vnc audio extension)
* some backends only support a single playback stream or very picky about the audio format. In this case we can't disable mixeng.However mixeng is not removed, only made optional, so this shouldn't be a big concern.
Signed-off-by Kővágó, Zoltán <[email protected]>---
Notes:
Changes from v1:
* renamed mixeng to mixing-engine
qapi/audio.json| 5 +++++
qemu-options.hx | 6 ++++++
2 files changed, 11 insertions(+)qapi/audio.json +0
-0qapi/audio.json line 17
Quoted 7 lines
qapi/audio.json +4
-0@@ -11,6 +11,10 @@ # General audio backend options that are used for both playback and # recording. # +# @mixing-engine: use QEMU's mixing engine to mix all streams inside QEMU. When +# set to off, fixed-settings must be also off. Not every backend +# compatible with the off setting (default on, since 4.2) +#Last sentence no verb.
Quoted 1 line
Which backends are compatible?
Actually that's a simplification, it depends on a few things. When mixeng is off, qemu will try to use the same format as the emulated sound card, and if the backend doesn't support that format, it won't work (no audio). Also attaching multiple sound cards to the same audiodev might not work, if the backend doesn't support multiple playback streams. If you use pulseaudio, it'll work without problems, if you use alsa, it depends on your device. If you use a hw: device directly, you'll likely only be able to use one emulated sound card with a few selected audio formats. If you use dmix: (and plug), alsa will handle the conversion and mixing, so it will work no matter what format the emulated sound card uses. With OSS the situation is probably similar, it depends on the kernel/hw what works and what not. wav and spice certainly doesn't support multiple streams. I'm not completely sure about the other backends right now, but I think dsound and coreaudio can handle the necessary sample format conversions and mixing.
Quoted 1 line
What happens when you try the off setting with incompatible backends?
Quoted 8 lines
Quoted 1 line
See above.
What happens *exactly*?
I'm asking because I'm concerned about the user experience. When a user asks for a combination of things QEMU can't provide, such as mixeng off with an incompatible backend, QEMU should fail with a suitable error message. Does it?
Error handling is not the best in the audio subsystem, if something fails it generally just prints a warning to the console and continues, and something will happen... For example, this is what happens when I try to open one hw device twice. I ran qemu with:
-audiodev alsa,id=foo,in.dev=hw:1,,0,out.mixing-engine=off,out.dev=hw:1,,0 -device piix4-usb-uhci -device usb-audio,audiodev=foo -device AC97,audiodev=foo
When the guest tried to initialize the AC97 card, I got an error:
alsa: Could not initialize DAC
alsa: Failed to open `hw:1,0':
alsa: Reason: Device or resource busyAnd it just continued. And the sound worked, but with wrong sample rate (AC97 wants 44100 Hz, but USB audio previously opened the alsa device with 48000 Hz). I'll fix this bug in the next revision, audio_pcm_hw_add_* shouldn't fall back to other HWs without mixeng. But even with that, the result will be that one emulated sound card will work and the other won't.
Quoted 5 lines
Quoted 3 lines
It's not ideal, but fixing it would require a lot of effort. Right now, if you specify an invalid audiodev for alsa (even with mixeng), it'll just print an error to the console and continue without audio.
Should we document this general error handling deficiency somehow?
It could be useful to mention it, especially for people who write script and expect qemu to fail if they specify an invalid parameter. I'm not sure how in-depth this explanation should be however.
Quoted 21 lines
Quoted 13 lines
Quoted 2 lines
Sometimes rejecting non-working configurations is impractical. Is it here?
I think it is. It depends on the backend, its settings, the frontend (emulated sound card), and how the guest uses it. We currently don't know what formats does a backend support, what formats can a frontend produce, and even if we would know that, just because a frontend can produce a format that the backend doesn't understand doesn't mean that it will actually do it. For example, right now with this patch series applied, usb-audio can produce 7.1 audio. If we want to be strict, it means we can only use it with backends that support at least 8 channels, even if the user only wants to use stereo audio.
Quoted 7 lines
Quoted 2 lines
If yes, we should call out the problematic configurations in documentation.
I think we should rather list known working configurations, and leave the others as "try at your own risk" because there's too many things that can go wrong. (pulseaudio will work, alsa with dmix too. Need to check coreaudio, dsound and oss. spice and wavcapture won't work.)
Quoted 6 lines
Far from ideal, but better than nothing.
Possibly naive idea: what about automatically falling back to mixeng on when mixeng off doesn't work? Requires detecting "doesn't work", which I understand just isn't there. Any other reasons why this couldn't be done? Way out of scope for this series, of course.
There are two cases when mixeng can help: a) when the frontend requires a format not supported by the backend, and b) when multiple frontends wants to use the same backend, but the backend doesn't support multiple channels. This fallback logic would help with a) but it would be much harder to solve b) (I'm not saying it's impossible, but it would require us to convert the already running stream back to mixeng). However, since the goal of this setting is to get rid of limitations of mixeng, currently you only want to turn it off if when you want to use 5.1 or 7.1 audio, where falling back to mixeng is not an option. (The other use case was when using pulseaudio, you wanted different pa streams per emulated sound card, but since you can have multiple -audiodevs now, it's no longer necessary to disable mixeng.)
The ideal solution is probably porting as much as possible to gstreamer, but this is even more out of scope:
https://wiki.qemu.org/Internships/ProjectIdeas/AudioGStreamer
Re: [PATCH v4 15/24] audio: add mixing-engine option (documentation)
Quoted 115 lines · Zoltán Kővágó
Quoted 108 lines · Markus Armbruster
Quoted 106 lines · Zoltán Kővágó
Quoted 73 lines · Markus Armbruster
Quoted 71 lines · Zoltán Kővágó
Quoted 50 lines · Markus Armbruster
Quoted 38 lines · Zoltán
This will allow us to disable mixeng when we use a decent backend.
Disabling mixeng have a few advantages:
* we no longer convert the audio output from one format to another, when the underlying audio system would just convert it to a third format. We no longer convert, only the underlying system, when needed.
* the underlying system probably has better resampling and sample format converting methods anyway...
* we may support formats that the mixeng currently does not support (S24 or float samples, more than two channels)
* when using an audio server (like pulseaudio) different sound card outputs will show up as separate streams, even if we use only one backendDisadvantages:
* audio capturing no longer works (wavcapture, and vnc audio extension)
* some backends only support a single playback stream or very picky about the audio format. In this case we can't disable mixeng.However mixeng is not removed, only made optional, so this shouldn't be a big concern.
Signed-off-by Kővágó, Zoltán <[email protected]>---
Notes:
Changes from v1:
* renamed mixeng to mixing-engine
qapi/audio.json| 5 +++++
qemu-options.hx | 6 ++++++
2 files changed, 11 insertions(+)qapi/audio.json +0
-0qapi/audio.json line 17
Quoted 7 lines
qapi/audio.json +4
-0@@ -11,6 +11,10 @@ # General audio backend options that are used for both playback and # recording. # +# @mixing-engine: use QEMU's mixing engine to mix all streams inside QEMU. When +# set to off, fixed-settings must be also off. Not every backend +# compatible with the off setting (default on, since 4.2) +#Last sentence no verb.
Quoted 1 line
Which backends are compatible?
Actually that's a simplification, it depends on a few things. When mixeng is off, qemu will try to use the same format as the emulated sound card, and if the backend doesn't support that format, it won't work (no audio). Also attaching multiple sound cards to the same audiodev might not work, if the backend doesn't support multiple playback streams. If you use pulseaudio, it'll work without problems, if you use alsa, it depends on your device. If you use a hw: device directly, you'll likely only be able to use one emulated sound card with a few selected audio formats. If you use dmix: (and plug), alsa will handle the conversion and mixing, so it will work no matter what format the emulated sound card uses. With OSS the situation is probably similar, it depends on the kernel/hw what works and what not. wav and spice certainly doesn't support multiple streams. I'm not completely sure about the other backends right now, but I think dsound and coreaudio can handle the necessary sample format conversions and mixing.
Quoted 1 line
What happens when you try the off setting with incompatible backends?
Quoted 8 lines
Quoted 1 line
See above.
What happens *exactly*?
I'm asking because I'm concerned about the user experience. When a user asks for a combination of things QEMU can't provide, such as mixeng off with an incompatible backend, QEMU should fail with a suitable error message. Does it?
Error handling is not the best in the audio subsystem, if something fails it generally just prints a warning to the console and continues, and something will happen... For example, this is what happens when I try to open one hw device twice. I ran qemu with:
-audiodev alsa,id=foo,in.dev=hw:1,,0,out.mixing-engine=off,out.dev=hw:1,,0 -device piix4-usb-uhci -device usb-audio,audiodev=foo -device AC97,audiodev=foo
When the guest tried to initialize the AC97 card, I got an error:
alsa: Could not initialize DAC
alsa: Failed to open `hw:1,0':
alsa: Reason: Device or resource busyAnd it just continued. And the sound worked, but with wrong sample rate (AC97 wants 44100 Hz, but USB audio previously opened the alsa device with 48000 Hz). I'll fix this bug in the next revision, audio_pcm_hw_add_* shouldn't fall back to other HWs without mixeng. But even with that, the result will be that one emulated sound card will work and the other won't.
Quoted 5 lines
Quoted 3 lines
It's not ideal, but fixing it would require a lot of effort. Right now, if you specify an invalid audiodev for alsa (even with mixeng), it'll just print an error to the console and continue without audio.
Should we document this general error handling deficiency somehow?
Quoted 3 lines
It could be useful to mention it, especially for people who write script and expect qemu to fail if they specify an invalid parameter. I'm not sure how in-depth this explanation should be however.
Common issue when writing documentation.
One way to tackle it: start unspecific, then add specifics until it feels useful.
The other direction can also work: write up everything, then cut until it feels useful. Makes sense mostly when you need to write up everything anyway, say for a developer audience, or even just for yourself. Nothing promotes clear thinking like writing does[*].
Quoted 28 lines
Quoted 21 lines
Quoted 13 lines
Quoted 2 lines
Sometimes rejecting non-working configurations is impractical. Is it here?
I think it is. It depends on the backend, its settings, the frontend (emulated sound card), and how the guest uses it. We currently don't know what formats does a backend support, what formats can a frontend produce, and even if we would know that, just because a frontend can produce a format that the backend doesn't understand doesn't mean that it will actually do it. For example, right now with this patch series applied, usb-audio can produce 7.1 audio. If we want to be strict, it means we can only use it with backends that support at least 8 channels, even if the user only wants to use stereo audio.
Quoted 7 lines
Quoted 2 lines
If yes, we should call out the problematic configurations in documentation.
I think we should rather list known working configurations, and leave the others as "try at your own risk" because there's too many things that can go wrong. (pulseaudio will work, alsa with dmix too. Need to check coreaudio, dsound and oss. spice and wavcapture won't work.)
Quoted 6 lines
Far from ideal, but better than nothing.
Possibly naive idea: what about automatically falling back to mixeng on when mixeng off doesn't work? Requires detecting "doesn't work", which I understand just isn't there. Any other reasons why this couldn't be done? Way out of scope for this series, of course.
Quoted 12 lines
There are two cases when mixeng can help: a) when the frontend requires a format not supported by the backend, and b) when multiple frontends wants to use the same backend, but the backend doesn't support multiple channels. This fallback logic would help with a) but it would be much harder to solve b) (I'm not saying it's impossible, but it would require us to convert the already running stream back to mixeng). However, since the goal of this setting is to get rid of limitations of mixeng, currently you only want to turn it off if when you want to use 5.1 or 7.1 audio, where falling back to mixeng is not an option. (The other use case was when using pulseaudio, you wanted different pa streams per emulated sound card, but since you can have multiple -audiodevs now, it's no longer necessary to disable mixeng.)
"Currently you only want to turn it off if when you want to use 5.1 or 7.1 audio" is useful advice. I figure it should be worked into documentation if it's not already there.
Suggest to record the remainder of your answer for posterity by working it into the commit message, along with this thought:
Quoted 3 lines
The ideal solution is probably porting as much as possible to gstreamer, but this is even more out of scope:
https://wiki.qemu.org/Internships/ProjectIdeas/AudioGStreamer
Thanks!
[*] "If you're thinking without writing, you only think you're thinking." Leslie Lamport