From b0b8c463578f469b99eff61898dcc4a7aadb40a7 Mon Sep 17 00:00:00 2001 From: Tomasz Lauda Date: Wed, 8 Apr 2020 13:43:10 +0200 Subject: [PATCH 1/2] volume: implement functions for getting zero crossing frame Implements functions for getting nearest zero crossing frame. They will be used to reduce zipper noise during volume ramping by updating ramp only on such frames. In the current implementation average value of all channels is used to check sample sign change. Signed-off-by: Tomasz Lauda --- src/audio/volume/volume.c | 164 +++++++++++++++++++++++++++++++++ src/include/sof/audio/volume.h | 15 +++ 2 files changed, 179 insertions(+) diff --git a/src/audio/volume/volume.c b/src/audio/volume/volume.c index 4d6c1702b438..11cd83d36502 100644 --- a/src/audio/volume/volume.c +++ b/src/audio/volume/volume.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -55,6 +56,139 @@ DECLARE_SOF_UUID("volume", volume_uuid, 0xb77e677e, 0x5ff4, 0x4188, DECLARE_SOF_UUID("volume-task", volume_task_uuid, 0x34dc0385, 0xfc2f, 0x4f7f, 0x82, 0xd2, 0x6c, 0xee, 0x44, 0x45, 0x33, 0xe0); +#if CONFIG_FORMAT_S16LE +/** + * \brief Used to find nearest zero crossing frame for 16 bit format. + * \param[in,out] source Source buffer. + * \param[in] frames Number of frames. + * \param[in,out] prev_sum Previous sum of channel samples. + */ +static uint32_t vol_zc_get_s16(const struct audio_stream *source, + uint32_t frames, int64_t *prev_sum) +{ + uint32_t buff_frag = frames * source->channels - 1; + uint32_t curr_frames = frames; + uint32_t channel; + int16_t *src; + int32_t sum; + uint32_t i; + + for (i = 0; i < frames; i++) { + sum = 0; + + for (channel = 0; channel < source->channels; channel++) { + src = audio_stream_read_frag_s16(source, buff_frag); + sum += *src; + buff_frag--; + } + + /* first sign change */ + if ((sum ^ *prev_sum) < 0) + return curr_frames; + + *prev_sum = sum; + curr_frames--; + } + + /* sign change not detected, process all samples */ + return frames; +} + +#endif /* CONFIG_FORMAT_S16LE */ + +#if CONFIG_FORMAT_S24LE +/** + * \brief Used to find nearest zero crossing frame for 24 in 32 bit format. + * \param[in,out] source Source buffer. + * \param[in] frames Number of frames. + * \param[in,out] prev_sum Previous sum of channel samples. + */ +static uint32_t vol_zc_get_s24(const struct audio_stream *source, + uint32_t frames, int64_t *prev_sum) +{ + uint32_t buff_frag = frames * source->channels - 1; + uint32_t curr_frames = frames; + uint32_t channel; + int32_t *src; + int64_t sum; + uint32_t i; + + for (i = 0; i < frames; i++) { + sum = 0; + + for (channel = 0; channel < source->channels; channel++) { + src = audio_stream_read_frag_s32(source, buff_frag); + sum += sign_extend_s24(*src); + buff_frag--; + } + + /* first sign change */ + if ((sum ^ *prev_sum) < 0) + return curr_frames; + + *prev_sum = sum; + curr_frames--; + } + + /* sign change not detected, process all samples */ + return frames; +} + +#endif /* CONFIG_FORMAT_S24LE */ + +#if CONFIG_FORMAT_S32LE +/** + * \brief Used to find nearest zero crossing frame for 32 bit format. + * \param[in,out] source Source buffer. + * \param[in] frames Number of frames. + * \param[in,out] prev_sum Previous sum of channel samples. + */ +static uint32_t vol_zc_get_s32(const struct audio_stream *source, + uint32_t frames, int64_t *prev_sum) +{ + uint32_t buff_frag = frames * source->channels - 1; + uint32_t curr_frames = frames; + uint32_t channel; + int32_t *src; + int64_t sum; + uint32_t i; + + for (i = 0; i < frames; i++) { + sum = 0; + + for (channel = 0; channel < source->channels; channel++) { + src = audio_stream_read_frag_s32(source, buff_frag); + sum += *src; + buff_frag--; + } + + /* first sign change */ + if ((sum ^ *prev_sum) < 0) + return curr_frames; + + *prev_sum = sum; + curr_frames--; + } + + /* sign change not detected, process all samples */ + return frames; +} + +#endif /* CONFIG_FORMAT_S32LE */ + +/** \brief Map of formats with dedicated zc functions. */ +const struct comp_zc_func_map zc_func_map[] = { +#if CONFIG_FORMAT_S16LE + { SOF_IPC_FRAME_S16_LE, vol_zc_get_s16 }, +#endif /* CONFIG_FORMAT_S16LE */ +#if CONFIG_FORMAT_S24LE + { SOF_IPC_FRAME_S24_4LE, vol_zc_get_s24 }, +#endif /* CONFIG_FORMAT_S24LE */ +#if CONFIG_FORMAT_S32LE + { SOF_IPC_FRAME_S32_LE, vol_zc_get_s32 }, +#endif /* CONFIG_FORMAT_S32LE */ +}; + /** * \brief Synchronize host mmap() volume with real value. * \param[in,out] cd Volume component private data. @@ -641,6 +775,29 @@ static int volume_copy(struct comp_dev *dev) return 0; } +/** + * \brief Retrieves volume zero crossing function. + * \param[in,out] dev Volume base component device. + */ +static vol_zc_func vol_get_zc_function(struct comp_dev *dev) +{ + struct comp_buffer *sinkb; + int i; + + sinkb = list_first_item(&dev->bsink_list, struct comp_buffer, + source_list); + + /* map the zc function to frame format */ + for (i = 0; i < ARRAY_SIZE(zc_func_map); i++) { + if (sinkb->stream.frame_fmt != zc_func_map[i].frame_fmt) + continue; + + return zc_func_map[i].func; + } + + return NULL; +} + /** * \brief Prepares volume component for processing. * \param[in,out] dev Volume base component device. @@ -689,6 +846,13 @@ static int volume_prepare(struct comp_dev *dev) goto err; } + cd->zc_get = vol_get_zc_function(dev); + if (!cd->zc_get) { + comp_err(dev, "volume_prepare(): invalid cd->zc_get"); + ret = -EINVAL; + goto err; + } + vol_sync_host(dev, PLATFORM_MAX_CHANNELS); /* Set current volume to min to ensure ramp starts from minimum diff --git a/src/include/sof/audio/volume.h b/src/include/sof/audio/volume.h index 8f21b3944dc9..668e0011942e 100644 --- a/src/include/sof/audio/volume.h +++ b/src/include/sof/audio/volume.h @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -81,6 +82,13 @@ struct sof_ipc_ctrl_value_chan; typedef void (*vol_scale_func)(struct comp_dev *dev, struct audio_stream *sink, const struct audio_stream *source, uint32_t frames); + +/** + * \brief volume interface for function getting nearest zero crossing frame + */ +typedef uint32_t (*vol_zc_func)(const struct audio_stream *source, + uint32_t frames, int64_t *prev_sum); + /** * \brief Volume component private data. * @@ -101,6 +109,7 @@ struct comp_data { bool vol_ramp_active; /**< set if volume is ramped */ bool ramp_started; /**< control ramp launch */ vol_scale_func scale_vol; /**< volume processing function */ + vol_zc_func zc_get; /**< function getting nearest zero crossing frame */ }; /** \brief Volume processing functions map. */ @@ -115,6 +124,12 @@ extern const struct comp_func_map func_map[]; /** \brief Number of processing functions. */ extern const size_t func_count; +/** \brief Volume zero crossing functions map. */ +struct comp_zc_func_map { + uint16_t frame_fmt; /**< frame format */ + vol_zc_func func; /**< volume zc function */ +}; + /** * \brief Retrievies volume processing function. * \param[in,out] dev Volume base component device. From 5486b2fd912b7def8dc0ec3c26e9480acae216d4 Mon Sep 17 00:00:00 2001 From: Tomasz Lauda Date: Thu, 26 Mar 2020 17:48:03 +0100 Subject: [PATCH 2/2] volume: do not schedule ramping as separate task We should not be scheduling ramping as a separate task. This patch makes it a part of copy function, which allows for removal of entire schedule related code. Ramping is done in maximum of 1 ms chunks by using functions for retrieval of nearest zero crossing frame. Signed-off-by: Tomasz Lauda --- src/audio/volume/volume.c | 147 ++++++++++++--------------------- src/include/sof/audio/volume.h | 6 +- 2 files changed, 58 insertions(+), 95 deletions(-) diff --git a/src/audio/volume/volume.c b/src/audio/volume/volume.c index 11cd83d36502..1913ac87ce4e 100644 --- a/src/audio/volume/volume.c +++ b/src/audio/volume/volume.c @@ -28,9 +28,6 @@ #include #include #include -#include -#include -#include #include #include #include @@ -40,6 +37,7 @@ #include #include #include +#include #include #include @@ -52,10 +50,6 @@ static const struct comp_driver comp_volume; DECLARE_SOF_UUID("volume", volume_uuid, 0xb77e677e, 0x5ff4, 0x4188, 0xaf, 0x14, 0xfb, 0xa8, 0xbd, 0xbf, 0x86, 0x82); -/* 34dc0385-fc2f-4f7f-82d2-6cee444533e0 */ -DECLARE_SOF_UUID("volume-task", volume_task_uuid, 0x34dc0385, 0xfc2f, 0x4f7f, - 0x82, 0xd2, 0x6c, 0xee, 0x44, 0x45, 0x33, 0xe0); - #if CONFIG_FORMAT_S16LE /** * \brief Used to find nearest zero crossing frame for 16 bit format. @@ -215,16 +209,12 @@ static void vol_sync_host(struct comp_dev *dev, unsigned int num_channels) /** * \brief Ramps volume changes over time. - * \param[in,out] data Volume base component device. - * \param[in] delay Update time. - * \return Time until next work. + * \param[in,out] dev Volume base component device. */ -static enum task_state vol_work(void *data) +static void volume_ramp(struct comp_dev *dev) { - struct comp_dev *dev = (struct comp_dev *)data; struct comp_data *cd = comp_get_drvdata(dev); int32_t vol; - int again = 0; int i; /* No need to ramp in idle state, jump volume to request. */ @@ -233,7 +223,8 @@ static enum task_state vol_work(void *data) cd->volume[i] = cd->tvolume[i]; vol_sync_host(dev, PLATFORM_MAX_CHANNELS); - return SOF_TASK_STATE_COMPLETED; + cd->ramp_finished = true; + return; } /* The first is set and cleared to indicate ongoing ramp, the @@ -241,7 +232,6 @@ static enum task_state vol_work(void *data) * in stream start. */ cd->vol_ramp_active = true; - cd->ramp_started = true; /* inc/dec each volume if it's not at target for active channels */ for (i = 0; i < cd->channels; i++) { @@ -259,9 +249,10 @@ static enum task_state vol_work(void *data) if (vol >= cd->tvolume[i] || vol >= cd->vol_max) { cd->ramp_increment[i] = 0; cd->volume[i] = cd->tvolume[i]; + cd->ramp_finished = true; + cd->vol_ramp_active = false; } else { cd->volume[i] = vol; - again = 1; } } else { /* ramp down */ @@ -275,9 +266,10 @@ static enum task_state vol_work(void *data) vol <= cd->vol_min) { cd->ramp_increment[i] = 0; cd->volume[i] = cd->tvolume[i]; + cd->ramp_finished = true; + cd->vol_ramp_active = false; } else { cd->volume[i] = vol; - again = 1; } } } @@ -286,44 +278,6 @@ static enum task_state vol_work(void *data) /* sync host with new value */ vol_sync_host(dev, cd->channels); - - /* do we need to continue ramping */ - if (again) - return SOF_TASK_STATE_RESCHEDULE; - - cd->vol_ramp_active = 0; - return SOF_TASK_STATE_COMPLETED; -} - -/** - * \brief Allocates task of volume component. - * \param[in,out] dev Volume base component device. - * \return Error code. - */ -static int vol_task_init(struct comp_dev *dev) -{ - struct comp_data *cd = comp_get_drvdata(dev); - int ret; - - /* initialize task if necessary */ - if (cd->volwork) - return 0; - - cd->volwork = rzalloc(SOF_MEM_ZONE_RUNTIME, 0, SOF_MEM_CAPS_RAM, - sizeof(*cd->volwork)); - if (!cd->volwork) - return -ENOMEM; - - ret = schedule_task_init_ll(cd->volwork, SOF_UUID(volume_task_uuid), - SOF_SCHEDULE_LL_TIMER, - SOF_TASK_PRI_MED, vol_work, dev, - cpu_get_id(), 0); - if (ret < 0) { - rfree(cd->volwork); - return ret; - } - - return 0; } /** @@ -426,12 +380,6 @@ static void volume_free(struct comp_dev *dev) comp_dbg(dev, "volume_free()"); - /* remove scheduling */ - if (cd->volwork) { - schedule_task_free(cd->volwork); - rfree(cd->volwork); - } - rfree(cd); rfree(dev); } @@ -480,6 +428,7 @@ static inline int volume_set_chan(struct comp_dev *dev, int chan, /* Check ramp type */ switch (pga->ramp) { case SOF_VOLUME_LINEAR: + case SOF_VOLUME_LINEAR_ZC: /* Get volume transition delta and absolute value */ delta = cd->tvolume[chan] - cd->volume[chan]; delta_abs = ABS(delta); @@ -524,7 +473,6 @@ static inline int volume_set_chan(struct comp_dev *dev, int chan, cd->ramp_increment[chan]); break; case SOF_VOLUME_LOG: - case SOF_VOLUME_LINEAR_ZC: case SOF_VOLUME_LOG_ZC: default: comp_err(dev, "volume_set_chan(): invalid ramp type %d", @@ -612,12 +560,8 @@ static int volume_ctrl_set_cmd(struct comp_dev *dev, } if (!cd->vol_ramp_active) { - ret = vol_task_init(dev); - if (ret < 0) - return ret; - - schedule_task(cd->volwork, VOL_RAMP_UPDATE_US, - VOL_RAMP_UPDATE_US); + cd->ramp_finished = false; + volume_ramp(dev); } break; @@ -642,12 +586,8 @@ static int volume_ctrl_set_cmd(struct comp_dev *dev, } if (!cd->vol_ramp_active) { - ret = vol_task_init(dev); - if (ret < 0) - return ret; - - schedule_task(cd->volwork, VOL_RAMP_UPDATE_US, - VOL_RAMP_UPDATE_US); + cd->ramp_finished = false; + volume_ramp(dev); } break; @@ -741,17 +681,19 @@ static int volume_trigger(struct comp_dev *dev, int cmd) */ static int volume_copy(struct comp_dev *dev) { + struct sof_ipc_comp_volume *pga = + COMP_GET_IPC(dev, sof_ipc_comp_volume); struct comp_copy_limits c; struct comp_data *cd = comp_get_drvdata(dev); struct comp_buffer *source; struct comp_buffer *sink; + uint32_t source_bytes; + uint32_t sink_bytes; + uint32_t frames; + int64_t prev_sum = 0; comp_dbg(dev, "volume_copy()"); - if (!cd->ramp_started) - schedule_task(cd->volwork, VOL_RAMP_UPDATE_US, - VOL_RAMP_UPDATE_US); - source = list_first_item(&dev->bsource_list, struct comp_buffer, sink_list); sink = list_first_item(&dev->bsink_list, struct comp_buffer, @@ -763,14 +705,36 @@ static int volume_copy(struct comp_dev *dev) comp_dbg(dev, "volume_copy(), source_bytes = 0x%x, sink_bytes = 0x%x", c.source_bytes, c.sink_bytes); - /* copy and scale volume */ - buffer_invalidate(source, c.source_bytes); - cd->scale_vol(dev, &sink->stream, &source->stream, c.frames); - buffer_writeback(sink, c.sink_bytes); + while (c.frames) { + if (cd->ramp_finished || cd->vol_ramp_frames > c.frames) { + /* without ramping process all at once */ + frames = c.frames; + } else if (pga->ramp == SOF_VOLUME_LINEAR_ZC) { + /* with ZC ramping look for next ZC offset */ + frames = cd->zc_get(&source->stream, + cd->vol_ramp_frames, &prev_sum); + } else { + /* without ZC process max ramp chunk */ + frames = cd->vol_ramp_frames; + } + + source_bytes = frames * c.source_frame_bytes; + sink_bytes = frames * c.sink_frame_bytes; + + /* copy and scale volume */ + buffer_invalidate(source, source_bytes); + cd->scale_vol(dev, &sink->stream, &source->stream, frames); + buffer_writeback(sink, sink_bytes); + + /* calculate new free and available */ + comp_update_buffer_produce(sink, sink_bytes); + comp_update_buffer_consume(source, source_bytes); - /* calculate new free and available */ - comp_update_buffer_produce(sink, c.sink_bytes); - comp_update_buffer_consume(source, c.source_bytes); + if (!cd->ramp_finished) + volume_ramp(dev); + + c.frames -= frames; + } return 0; } @@ -856,22 +820,21 @@ static int volume_prepare(struct comp_dev *dev) vol_sync_host(dev, PLATFORM_MAX_CHANNELS); /* Set current volume to min to ensure ramp starts from minimum - * to previous volume request. Copy() checks for ramp started - * and schedules it if it has not yet started as result of + * to previous volume request. Copy() checks for ramp finished + * and executes it if it has not yet finished as result of * driver commands. Ramp is not constant rate to ensure it lasts * for entire topology specified time. */ - cd->ramp_started = false; + cd->ramp_finished = true; + cd->vol_ramp_frames = dev->frames / (dev->period / VOL_RAMP_UPDATE_US); cd->channels = sinkb->stream.channels; for (i = 0; i < cd->channels; i++) { cd->volume[i] = cd->vol_min; volume_set_chan(dev, i, cd->tvolume[i], false); + if (cd->volume[i] != cd->tvolume[i]) + cd->ramp_finished = false; } - ret = vol_task_init(dev); - if (ret < 0) - return ret; - return 0; err: diff --git a/src/include/sof/audio/volume.h b/src/include/sof/audio/volume.h index 668e0011942e..72e98a45c803 100644 --- a/src/include/sof/audio/volume.h +++ b/src/include/sof/audio/volume.h @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -95,7 +94,6 @@ typedef uint32_t (*vol_zc_func)(const struct audio_stream *source, * Gain amplitude value is between 0 (mute) ... 2^16 (0dB) ... 2^24 (~+48dB). */ struct comp_data { - struct task *volwork; /**< volume scheduled work function */ struct sof_ipc_ctrl_value_chan *hvol; /**< host volume readback */ int32_t volume[SOF_IPC_MAX_CHANNELS]; /**< current volume */ int32_t tvolume[SOF_IPC_MAX_CHANNELS]; /**< target volume */ @@ -104,10 +102,12 @@ struct comp_data { int32_t vol_min; /**< minimum volume */ int32_t vol_max; /**< maximum volume */ int32_t vol_ramp_range; /**< max ramp transition */ + /**< max number of frames to process per ramp transition */ + uint32_t vol_ramp_frames; unsigned int channels; /**< current channels count */ bool muted[SOF_IPC_MAX_CHANNELS]; /**< set if channel is muted */ bool vol_ramp_active; /**< set if volume is ramped */ - bool ramp_started; /**< control ramp launch */ + bool ramp_finished; /**< control ramp launch */ vol_scale_func scale_vol; /**< volume processing function */ vol_zc_func zc_get; /**< function getting nearest zero crossing frame */ };