sandbox: fix sound driver
authorHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Sun, 4 Dec 2022 19:48:57 +0000 (20:48 +0100)
committerHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Mon, 5 Dec 2022 16:43:23 +0000 (17:43 +0100)
In the callback function we have to use memcpy(). Otherwise we add
the new samples on top of what is stored in the stream buffer.

If we don't have enough data, zero out the rest of the stream buffer.

Our sampling frequency is 48000. Let the batch size for the callback
function be 960. If we play a multiple of 20 ms, this will always be
a full batch.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
arch/sandbox/cpu/sdl.c

index f4ca36b..2c570ed 100644 (file)
@@ -441,7 +441,6 @@ void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
 {
        struct buf_info *buf;
        int avail;
-       bool have_data = false;
        int i;
 
        for (i = 0; i < 2; i++) {
@@ -453,10 +452,9 @@ void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
                }
                if (avail > len)
                        avail = len;
-               have_data = true;
 
-               SDL_MixAudio(stream, buf->data + buf->pos, avail,
-                            SDL_MIX_MAXVOLUME);
+               memcpy(stream, buf->data + buf->pos, avail);
+               stream += avail;
                buf->pos += avail;
                len -= avail;
 
@@ -466,7 +464,8 @@ void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
                else
                        break;
        }
-       sdl.stopping = !have_data;
+       memset(stream, 0, len);
+       sdl.stopping = !!len;
 }
 
 int sandbox_sdl_sound_init(int rate, int channels)
@@ -484,7 +483,7 @@ int sandbox_sdl_sound_init(int rate, int channels)
        wanted.freq = rate;
        wanted.format = AUDIO_S16;
        wanted.channels = channels;
-       wanted.samples = 1024;  /* Good low-latency value for callback */
+       wanted.samples = 960;  /* Good low-latency value for callback */
        wanted.callback = sandbox_sdl_fill_audio;
        wanted.userdata = NULL;