diff mbox series

[11/12] audio: fix sw->buf size for audio recording

Message ID 20220923183640.8314-11-vr_qemu@t-online.de
State New
Headers show
Series audio: misc. improvements and bug fixes | expand

Commit Message

Volker Rümelin Sept. 23, 2022, 6:36 p.m. UTC
The calculation of the buffer size needed to store audio samples
after resampling is wrong for audio recording. For audio recording
sw->ratio is calculated as

sw->ratio = frontend sample rate / backend sample rate.

From this follows

frontend samples = frontend sample rate / backend sample rate
 * backend samples
frontend samples = sw->ratio * backend samples

In 2 of 3 places in the audio recording code where sw->ratio
is used in a calculation to get the number of frontend frames,
the calculation is wrong. Fix this. The 3rd formula in
audio_pcm_sw_read() is correct.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/71
Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
---
 audio/audio.c          | 2 +-
 audio/audio_template.h | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)

Comments

Marc-André Lureau Sept. 27, 2022, 11:54 a.m. UTC | #1
On Fri, Sep 23, 2022 at 10:48 PM Volker Rümelin <vr_qemu@t-online.de> wrote:

> The calculation of the buffer size needed to store audio samples
> after resampling is wrong for audio recording. For audio recording
> sw->ratio is calculated as
>
> sw->ratio = frontend sample rate / backend sample rate.
>
> From this follows
>
> frontend samples = frontend sample rate / backend sample rate
>  * backend samples
> frontend samples = sw->ratio * backend samples
>
> In 2 of 3 places in the audio recording code where sw->ratio
> is used in a calculation to get the number of frontend frames,
> the calculation is wrong. Fix this. The 3rd formula in
> audio_pcm_sw_read() is correct.
>
> Resolves: https://gitlab.com/qemu-project/qemu/-/issues/71
> Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
>

Would you mind adding the test to qtest?

lgtm
Acked-by: Marc-André Lureau <marcandre.lureau@redhat.com>



> ---
>  audio/audio.c          | 2 +-
>  audio/audio_template.h | 4 ++++
>  2 files changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/audio/audio.c b/audio/audio.c
> index ba0c62b120..60c7472d37 100644
> --- a/audio/audio.c
> +++ b/audio/audio.c
> @@ -995,7 +995,7 @@ void AUD_set_active_in (SWVoiceIn *sw, int on)
>   */
>  static size_t audio_frontend_frames_in(SWVoiceIn *sw, size_t frames_in)
>  {
> -    return ((int64_t)frames_in << 32) / sw->ratio;
> +    return (int64_t)frames_in * sw->ratio >> 32;
>  }
>
>  static size_t audio_get_avail (SWVoiceIn *sw)
> diff --git a/audio/audio_template.h b/audio/audio_template.h
> index 7192b19e73..6a0337ac6b 100644
> --- a/audio/audio_template.h
> +++ b/audio/audio_template.h
> @@ -112,7 +112,11 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE)
> (SW *sw)
>          return 0;
>      }
>
> +#ifdef DAC
>      samples = ((int64_t) sw->HWBUF->size << 32) / sw->ratio;
> +#else
> +    samples = (int64_t)sw->HWBUF->size * sw->ratio >> 32;
> +#endif
>
>      sw->buf = audio_calloc(__func__, samples, sizeof(struct st_sample));
>      if (!sw->buf) {
> --
> 2.35.3
>
>
>
Volker Rümelin Oct. 1, 2022, 12:08 p.m. UTC | #2
Am 27.09.22 um 13:54 schrieb Marc-André Lureau:

>
> On Fri, Sep 23, 2022 at 10:48 PM Volker Rümelin <vr_qemu@t-online.de> 
> wrote:
>
>     The calculation of the buffer size needed to store audio samples
>     after resampling is wrong for audio recording. For audio recording
>     sw->ratio is calculated as
>
>     sw->ratio = frontend sample rate / backend sample rate.
>
>     >From this follows
>
>     frontend samples = frontend sample rate / backend sample rate
>      * backend samples
>     frontend samples = sw->ratio * backend samples
>
>     In 2 of 3 places in the audio recording code where sw->ratio
>     is used in a calculation to get the number of frontend frames,
>     the calculation is wrong. Fix this. The 3rd formula in
>     audio_pcm_sw_read() is correct.
>
>     Resolves: https://gitlab.com/qemu-project/qemu/-/issues/71
>     Signed-off-by: Volker Rümelin <vr_qemu@t-online.de>
>
>
> Would you mind adding the test to qtest?
>
> lgtm
> Acked-by: Marc-André Lureau <marcandre.lureau@redhat.com>
>

Hi Marc-André,

I will give it a try. But it will be a separate patch, because the test 
from issue #71 now checks for the error at 
https://lists.nongnu.org/archive/html/qemu-devel/2022-09/msg02347.html 
and not the one from issue #71.

With best regards,
Volker
diff mbox series

Patch

diff --git a/audio/audio.c b/audio/audio.c
index ba0c62b120..60c7472d37 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -995,7 +995,7 @@  void AUD_set_active_in (SWVoiceIn *sw, int on)
  */
 static size_t audio_frontend_frames_in(SWVoiceIn *sw, size_t frames_in)
 {
-    return ((int64_t)frames_in << 32) / sw->ratio;
+    return (int64_t)frames_in * sw->ratio >> 32;
 }
 
 static size_t audio_get_avail (SWVoiceIn *sw)
diff --git a/audio/audio_template.h b/audio/audio_template.h
index 7192b19e73..6a0337ac6b 100644
--- a/audio/audio_template.h
+++ b/audio/audio_template.h
@@ -112,7 +112,11 @@  static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
         return 0;
     }
 
+#ifdef DAC
     samples = ((int64_t) sw->HWBUF->size << 32) / sw->ratio;
+#else
+    samples = (int64_t)sw->HWBUF->size * sw->ratio >> 32;
+#endif
 
     sw->buf = audio_calloc(__func__, samples, sizeof(struct st_sample));
     if (!sw->buf) {