From 96f747d3f35ee8440eec4de98080a5bd8f0a36a5 Mon Sep 17 00:00:00 2001 From: David Reid Date: Thu, 30 Jan 2020 21:23:21 +1000 Subject: [PATCH] Don't allow format and channels to change for biquad and LPF filters. Previously this would result in a glitch, however in the interest of avoiding as many sources of glitching as possible, changing the format and/or channel count of a biquad filter (and by extension, anything derived from it, such as low-pass filters) will now result in an error. --- research/ma_lpf.h | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/research/ma_lpf.h b/research/ma_lpf.h index 8c695cbb..69c4ae69 100644 --- a/research/ma_lpf.h +++ b/research/ma_lpf.h @@ -35,8 +35,8 @@ Filtering can be applied in-place by passing in the same pointer for both the in If you need to change the values of the coefficients, but maintain the values in the registers you can do so with `ma_biquad_reinit()`. This is useful if you need to change the properties of the filter while keeping the values of registers valid to avoid glitching or whatnot. Do not use `ma_biquad_init()` for this -as it will do a full initialization which involves clearing the registers to 0. Note that changing the format or channel count will result in an audible -glitch. +as it will do a full initialization which involves clearing the registers to 0. Note that changing the format or channel count after initialization is invalid +and will result in an error. **************************************************************************************************************************************************************/ @@ -116,7 +116,7 @@ The low-pass filter is implemented as a biquad filter. If you need increase the If you need to change the configuration of the filter, but need to maintain the state of internal registers you can do so with `ma_lpf_reinit()`. This may be useful if you need to change the sample rate and/or cutoff frequency dynamically while maintaing smooth transitions. Note that changing the format or channel -count will result in an audible glitch. +count after initialization is invalid and will result in an error. **************************************************************************************************************************************************************/ @@ -203,6 +203,17 @@ ma_result ma_biquad_reinit(const ma_biquad_config* pConfig, ma_biquad* pBQ) return MA_INVALID_ARGS; } + /* The format cannot be changed after initialization. */ + if (pBQ->format != pConfig->format) { + return MA_INVALID_OPERATION; + } + + /* The channel count cannot be changed after initialization. */ + if (pBQ->channels != pConfig->channels) { + return MA_INVALID_OPERATION; + } + + pBQ->format = pConfig->format; pBQ->channels = pConfig->channels;