From f9e0ed984903b12e64ac943b2cee3340a8aa9c4e Mon Sep 17 00:00:00 2001 From: Job Bautista Date: Sun, 26 Jun 2022 19:20:24 +0800 Subject: Issue #1944 - Fix volume handling in sndio backend. Backported from Mozilla bug 1467882. --- media/libcubeb/src/cubeb_sndio.c | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/media/libcubeb/src/cubeb_sndio.c b/media/libcubeb/src/cubeb_sndio.c index c7ac184465..3deb283e45 100644 --- a/media/libcubeb/src/cubeb_sndio.c +++ b/media/libcubeb/src/cubeb_sndio.c @@ -42,18 +42,35 @@ struct cubeb_stream { uint64_t wrpos; /* number of written frames */ cubeb_data_callback data_cb; /* cb to preapare data */ cubeb_state_callback state_cb; /* cb to notify about state changes */ + float volume; /* current volume */ void *arg; /* user arg to {data,state}_cb */ }; + +static void +s16_setvol(void *ptr, long nsamp, float volume) +{ + int16_t *dst = ptr; + int32_t mult = volume * 32768; + int32_t s; + + while (nsamp-- > 0) { + s = *dst; + s = (s * mult) >> 15; + *(dst++) = s; + } +} + static void -float_to_s16(void *ptr, long nsamp) +float_to_s16(void *ptr, long nsamp, float volume) { int16_t *dst = ptr; float *src = ptr; + float mult = volume * 32768; int s; while (nsamp-- > 0) { - s = lrintf(*(src++) * 32768); + s = lrintf(*(src++) * mult); if (s < -32768) s = -32768; else if (s > 32767) @@ -111,7 +128,9 @@ sndio_mainloop(void *arg) break; } if (s->conv) - float_to_s16(s->buf, nfr * s->pchan); + float_to_s16(s->buf, nfr * s->pchan, s->volume); + else + s16_setvol(s->buf, nfr * s->pchan, s->volume); start = 0; end = nfr * s->bpf; } @@ -260,6 +279,7 @@ sndio_stream_init(cubeb * context, free(s); return CUBEB_ERROR; } + s->volume = 1.; *stream = s; DPR("sndio_stream_init() end, ok\n"); (void)context; @@ -346,7 +366,11 @@ sndio_stream_set_volume(cubeb_stream *s, float volume) { DPR("sndio_stream_set_volume(%f)\n", volume); pthread_mutex_lock(&s->mtx); - sio_setvol(s->hdl, SIO_MAXVOL * volume); + if (volume < 0.) + volume = 0.; + else if (volume > 1.0) + volume = 1.; + s->volume = volume; pthread_mutex_unlock(&s->mtx); return CUBEB_OK; } -- cgit v1.2.3 From 79e782e53f4ad38e924bf891f439f442ae818fe8 Mon Sep 17 00:00:00 2001 From: Job Bautista Date: Mon, 27 Jun 2022 02:14:05 +0800 Subject: Issue #1944 - Follow-up: Make it clear that volume is a float. 1. => 1.0, 0. => 0.0 --- media/libcubeb/src/cubeb_sndio.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/media/libcubeb/src/cubeb_sndio.c b/media/libcubeb/src/cubeb_sndio.c index 3deb283e45..320b4212f5 100644 --- a/media/libcubeb/src/cubeb_sndio.c +++ b/media/libcubeb/src/cubeb_sndio.c @@ -279,7 +279,7 @@ sndio_stream_init(cubeb * context, free(s); return CUBEB_ERROR; } - s->volume = 1.; + s->volume = 1.0; *stream = s; DPR("sndio_stream_init() end, ok\n"); (void)context; @@ -366,10 +366,10 @@ sndio_stream_set_volume(cubeb_stream *s, float volume) { DPR("sndio_stream_set_volume(%f)\n", volume); pthread_mutex_lock(&s->mtx); - if (volume < 0.) - volume = 0.; + if (volume < 0.0) + volume = 0.0; else if (volume > 1.0) - volume = 1.; + volume = 1.0; s->volume = volume; pthread_mutex_unlock(&s->mtx); return CUBEB_OK; -- cgit v1.2.3