2023-03-15 19:50:39 +00:00
|
|
|
/*
|
|
|
|
* Brickworks
|
|
|
|
*
|
2024-01-02 10:20:52 +00:00
|
|
|
* Copyright (C) 2023, 2024 Orastron Srl unipersonale
|
2023-03-15 19:50:39 +00:00
|
|
|
*
|
|
|
|
* Brickworks is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3 of the License.
|
|
|
|
*
|
|
|
|
* Brickworks is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Brickworks. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* File author: Stefano D'Angelo
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* module_type {{{ dsp }}}
|
2024-02-02 16:42:12 +00:00
|
|
|
* version {{{ 1.1.0 }}}
|
2023-07-21 06:56:27 +00:00
|
|
|
* requires {{{ bw_common bw_math }}}
|
2023-03-15 19:50:39 +00:00
|
|
|
* description {{{
|
|
|
|
* Aribtrary-ratio IIR sample rate converter.
|
|
|
|
* }}}
|
|
|
|
* changelog {{{
|
|
|
|
* <ul>
|
2024-02-02 16:42:12 +00:00
|
|
|
* <li>Version <strong>1.1.0</strong>:
|
2024-01-02 10:20:52 +00:00
|
|
|
* <ul>
|
2024-02-02 16:42:12 +00:00
|
|
|
* <li>Now using <code>BW_NULL</code> and
|
|
|
|
* <code>BW_CXX_NO_ARRAY</code>.</li>
|
2024-01-02 10:20:52 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2023-08-08 08:46:05 +00:00
|
|
|
* <li>Version <strong>1.0.0</strong>:
|
|
|
|
* <ul>
|
2023-09-21 12:32:17 +00:00
|
|
|
* <li>Added <code>bw_src_reset_state_multi()</code> and updated C++
|
|
|
|
* API in this regard.</li>
|
|
|
|
* <li>Now <code>bw_src_reset_state()</code> returns the initial output
|
|
|
|
* value.</li>
|
|
|
|
* <li>Added overloaded C++ <code>reset()</code> functions taking
|
|
|
|
* arrays as arguments.</li>
|
2023-08-13 09:42:06 +00:00
|
|
|
* <li><code>bw_src_process()</code> and
|
|
|
|
* <code>bw_src_process_multi()</code> now use <code>size_t</code>
|
|
|
|
* to count samples and channels.</li>
|
2023-08-14 07:57:06 +00:00
|
|
|
* <li>Added more <code>const</code> and <code>BW_RESTRICT</code>
|
|
|
|
* specifiers to input arguments and implementation.</li>
|
2023-08-13 13:37:15 +00:00
|
|
|
* <li>Moved C++ code to C header and fixed C++ API.</li>
|
2023-09-05 09:35:30 +00:00
|
|
|
* <li>Added overloaded C++ <code>process()</code> function taking
|
2023-08-13 09:42:06 +00:00
|
|
|
* C-style arrays as arguments.</li>
|
|
|
|
* <li>Removed usage of reserved identifiers.</li>
|
2023-08-13 13:37:15 +00:00
|
|
|
* <li>Removed useless computation when upsampling.</li>
|
2023-09-21 12:32:17 +00:00
|
|
|
* <li>Clarified when the same buffers cannot be used for both input
|
|
|
|
* and output.</li>
|
|
|
|
* <li>Added debugging code.</li>
|
2023-08-08 08:46:05 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2023-07-21 06:56:27 +00:00
|
|
|
* <li>Version <strong>0.6.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Removed dependency on bw_config.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
2023-07-04 13:33:18 +00:00
|
|
|
* <li>Version <strong>0.5.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Added <code>bw_src_process_multi()</code>.</li>
|
2023-07-13 10:35:37 +00:00
|
|
|
* <li>Added C++ wrapper.</li>
|
2023-07-04 13:33:18 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2023-03-15 19:50:39 +00:00
|
|
|
* <li>Version <strong>0.4.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>First release.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
|
|
|
* </ul>
|
|
|
|
* }}}
|
|
|
|
*/
|
|
|
|
|
2023-08-13 09:42:06 +00:00
|
|
|
#ifndef BW_SRC_H
|
|
|
|
#define BW_SRC_H
|
2023-03-15 19:50:39 +00:00
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#include <bw_common.h>
|
|
|
|
|
2023-03-15 19:50:39 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*! api {{{
|
|
|
|
* #### bw_src_coeffs
|
|
|
|
* ```>>> */
|
2023-08-13 09:42:06 +00:00
|
|
|
typedef struct bw_src_coeffs bw_src_coeffs;
|
2023-03-15 19:50:39 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Coefficients and related.
|
|
|
|
*
|
2023-04-21 04:12:09 +00:00
|
|
|
* #### bw_src_state
|
2023-03-15 19:50:39 +00:00
|
|
|
* ```>>> */
|
2023-08-13 09:42:06 +00:00
|
|
|
typedef struct bw_src_state bw_src_state;
|
2023-03-15 19:50:39 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Internal state and related.
|
|
|
|
*
|
|
|
|
* #### bw_src_init()
|
|
|
|
* ```>>> */
|
2023-09-21 12:32:17 +00:00
|
|
|
static inline void bw_src_init(
|
|
|
|
bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float ratio);
|
2023-03-15 19:50:39 +00:00
|
|
|
/*! <<<```
|
2023-04-04 13:13:26 +00:00
|
|
|
* Initializes `coeffs` using the given resampling `ratio`.
|
|
|
|
*
|
|
|
|
* `ratio` must be positive and determines the sample rate of the output
|
|
|
|
* signal, which will be equal to `ratio` times the sample rate of the input
|
|
|
|
* signal.
|
2023-03-15 19:50:39 +00:00
|
|
|
*
|
|
|
|
* #### bw_src_reset_state()
|
|
|
|
* ```>>> */
|
2023-09-21 12:32:17 +00:00
|
|
|
static inline float bw_src_reset_state(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_src_state * BW_RESTRICT state,
|
|
|
|
float x_0);
|
2023-03-15 19:50:39 +00:00
|
|
|
/*! <<<```
|
2023-04-04 13:13:26 +00:00
|
|
|
* Resets the given `state` to its initial values using the given `coeffs`
|
2023-09-21 12:32:17 +00:00
|
|
|
* and the initial input value `x_0`.
|
|
|
|
*
|
|
|
|
* Returns the corresponding initial output value.
|
|
|
|
*
|
|
|
|
* #### bw_src_reset_state_multi()
|
|
|
|
* ```>>> */
|
|
|
|
static inline void bw_src_reset_state_multi(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_src_state * BW_RESTRICT const * BW_RESTRICT state,
|
|
|
|
const float * x_0,
|
|
|
|
float * y_0,
|
|
|
|
size_t n_channels);
|
|
|
|
/*! <<<```
|
|
|
|
* Resets each of the `n_channels` `state`s to its initial values using the
|
|
|
|
* given `coeffs` and the corresponding initial input value in the `x_0`
|
|
|
|
* array.
|
|
|
|
*
|
|
|
|
* The corresponding initial output values are written into the `y_0` array,
|
2024-01-02 10:20:52 +00:00
|
|
|
* if not `BW_NULL`.
|
2023-03-15 19:50:39 +00:00
|
|
|
*
|
|
|
|
* #### bw_src_process()
|
|
|
|
* ```>>> */
|
2023-09-21 12:32:17 +00:00
|
|
|
static inline void bw_src_process(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_src_state * BW_RESTRICT state,
|
|
|
|
const float * BW_RESTRICT x,
|
|
|
|
float * BW_RESTRICT y,
|
|
|
|
size_t * BW_RESTRICT n_in_samples,
|
|
|
|
size_t * BW_RESTRICT n_out_samples);
|
2023-03-15 19:50:39 +00:00
|
|
|
/*! <<<```
|
2023-04-04 13:13:26 +00:00
|
|
|
* Processes at most the first `n_in_samples` of the input buffer `x` and
|
|
|
|
* fills the output buffer `y` with at most `n_out_samples` using `coeffs`,
|
|
|
|
* while using and updating `state`.
|
|
|
|
*
|
|
|
|
* After the call `n_in_samples` and `n_out_samples` will contain the actual
|
|
|
|
* number of consumed input samples and generated output samples,
|
|
|
|
* respectively.
|
2023-07-04 13:33:18 +00:00
|
|
|
*
|
2023-09-21 12:32:17 +00:00
|
|
|
* `x` and `y` must point to different buffers and also `n_in_samples`. Also,
|
|
|
|
* `n_in_samples` and `n_out_samples` must be different.
|
2023-08-14 09:49:29 +00:00
|
|
|
*
|
2023-07-04 13:33:18 +00:00
|
|
|
* #### bw_src_process_multi()
|
|
|
|
* ```>>> */
|
2023-09-21 12:32:17 +00:00
|
|
|
static inline void bw_src_process_multi(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_src_state * BW_RESTRICT const * BW_RESTRICT state,
|
|
|
|
const float * BW_RESTRICT const * BW_RESTRICT x,
|
|
|
|
float * BW_RESTRICT const * BW_RESTRICT y,
|
|
|
|
size_t n_channels,
|
|
|
|
size_t * BW_RESTRICT n_in_samples,
|
|
|
|
size_t * BW_RESTRICT n_out_samples);
|
2023-07-04 13:33:18 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Processes at most the first `n_in_samples[i]` of each input buffer `x[i]`
|
|
|
|
* and fills the corresponding output buffer `y[i]` with at most
|
|
|
|
* `n_out_samples[i]` using `coeffs`, while using and updating each
|
|
|
|
* `state[i]`.
|
|
|
|
*
|
|
|
|
* After the call each element in `n_in_samples` and `n_out_samples` will
|
|
|
|
* contain the actual number of consumed input samples and generated output
|
|
|
|
* samples, respectively, for each of the `n_channels` input/output buffer
|
|
|
|
* couples.
|
2023-08-14 09:49:29 +00:00
|
|
|
*
|
2023-09-21 12:32:17 +00:00
|
|
|
* A given buffer cannot be used both as an input and output buffer. Also,
|
|
|
|
* `n_in_samples` and `n_out_samples` must point to non-overlapping memory
|
|
|
|
* areas.
|
|
|
|
*
|
|
|
|
* #### bw_src_coeffs_is_valid()
|
|
|
|
* ```>>> */
|
|
|
|
static inline char bw_src_coeffs_is_valid(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs);
|
|
|
|
/*! <<<```
|
|
|
|
* Tries to determine whether `coeffs` is valid and returns non-`0` if it
|
|
|
|
* seems to be the case and `0` if it is certainly not. False positives are
|
|
|
|
* possible, false negatives are not.
|
|
|
|
*
|
|
|
|
* `coeffs` must at least point to a readable memory block of size greater
|
|
|
|
* than or equal to that of `bw_src_coeffs`.
|
|
|
|
*
|
|
|
|
* #### bw_src_state_is_valid()
|
|
|
|
* ```>>> */
|
|
|
|
static inline char bw_src_state_is_valid(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
const bw_src_state * BW_RESTRICT state);
|
|
|
|
/*! <<<```
|
|
|
|
* Tries to determine whether `state` is valid and returns non-`0` if it
|
|
|
|
* seems to be the case and `0` if it is certainly not. False positives are
|
|
|
|
* possible, false negatives are not.
|
|
|
|
*
|
2024-01-02 10:20:52 +00:00
|
|
|
* If `coeffs` is not `BW_NULL` extra cross-checks might be performed
|
|
|
|
* (`state` is supposed to be associated to `coeffs`).
|
2023-09-21 12:32:17 +00:00
|
|
|
*
|
|
|
|
* `state` must at least point to a readable memory block of size greater
|
|
|
|
* than or equal to that of `bw_src_state`.
|
2023-03-15 19:50:39 +00:00
|
|
|
* }}} */
|
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-03-15 19:50:39 +00:00
|
|
|
/*** Implementation ***/
|
|
|
|
|
|
|
|
/* WARNING: This part of the file is not part of the public API. Its content may
|
|
|
|
* change at any time in future versions. Please, do not use it directly. */
|
|
|
|
|
2023-04-04 13:13:26 +00:00
|
|
|
#include <bw_math.h>
|
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2023-08-13 09:42:06 +00:00
|
|
|
struct bw_src_coeffs {
|
2023-09-21 12:32:17 +00:00
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
uint32_t hash;
|
|
|
|
uint32_t reset_id;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Coefficients
|
|
|
|
float k;
|
|
|
|
float b0;
|
|
|
|
float ma1;
|
|
|
|
float ma2;
|
|
|
|
float ma3;
|
|
|
|
float ma4;
|
2023-03-15 19:50:39 +00:00
|
|
|
};
|
|
|
|
|
2023-08-13 09:42:06 +00:00
|
|
|
struct bw_src_state {
|
2023-09-21 12:32:17 +00:00
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
uint32_t hash;
|
|
|
|
uint32_t coeffs_reset_id;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// States
|
|
|
|
float i;
|
|
|
|
float z1;
|
|
|
|
float z2;
|
|
|
|
float z3;
|
|
|
|
float z4;
|
|
|
|
float xz1;
|
|
|
|
float xz2;
|
|
|
|
float xz3;
|
2023-03-15 19:50:39 +00:00
|
|
|
};
|
|
|
|
|
2023-09-21 12:32:17 +00:00
|
|
|
static inline void bw_src_init(
|
|
|
|
bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float ratio) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT(ratio > 0.f);
|
|
|
|
|
2023-03-23 14:12:10 +00:00
|
|
|
coeffs->k = ratio >= 1.f ? 1.f / ratio : -1.f / ratio;
|
2023-04-04 13:13:26 +00:00
|
|
|
// 4th-degree Butterworth with cutoff at ratio * Nyquist, using bilinear transform w/ prewarping
|
2023-04-04 13:24:33 +00:00
|
|
|
const float fc = bw_minf(ratio >= 1.f ? 1.f / ratio : ratio, 0.9f);
|
2023-08-10 08:17:22 +00:00
|
|
|
const float T = bw_tanf(1.570796326794896f * fc);
|
2023-04-04 13:13:26 +00:00
|
|
|
const float T2 = T * T;
|
|
|
|
const float k = 1.f / (T * (T * (T * (T + 2.613125929752753f) + 3.414213562373095f) + 2.613125929752753f) + 1.f);
|
2023-04-04 13:17:37 +00:00
|
|
|
coeffs->b0 = k * T2 * T2;
|
2023-09-21 12:32:17 +00:00
|
|
|
coeffs->ma1 = k * (T * (T2 * (-5.226251859505504f - 4.f * T) + 5.226251859505504f) + 4.f);
|
|
|
|
coeffs->ma2 = k * ((6.82842712474619f - 6.f * T2) * T2 - 6.f);
|
|
|
|
coeffs->ma3 = k * (T * (T2 * (5.226251859505504f - 4.f * T) - 5.226251859505504f) + 4.f);
|
|
|
|
coeffs->ma4 = k * (T * (T * ((2.613125929752753f - T) * T - 3.414213562373095f) + 2.613125929752753f) - 1.f);
|
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
coeffs->hash = bw_hash_sdbm("bw_src_coeffs");
|
|
|
|
coeffs->reset_id = coeffs->hash + 1;
|
|
|
|
#endif
|
|
|
|
BW_ASSERT_DEEP(bw_src_coeffs_is_valid(coeffs));
|
2023-03-15 19:50:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 12:32:17 +00:00
|
|
|
static inline float bw_src_reset_state(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_src_state * BW_RESTRICT state,
|
|
|
|
float x_0) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_src_coeffs_is_valid(coeffs));
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT(bw_is_finite(x_0));
|
|
|
|
|
2023-03-16 11:01:39 +00:00
|
|
|
if (coeffs->k < 0) {
|
2023-03-15 19:50:39 +00:00
|
|
|
// DF-II
|
2023-09-21 12:32:17 +00:00
|
|
|
state->z1 = x_0 / (1.f - coeffs->ma1 - coeffs->ma2 - coeffs->ma3 - coeffs->ma4);
|
2023-03-15 19:50:39 +00:00
|
|
|
state->z2 = state->z1;
|
|
|
|
state->z3 = state->z2;
|
|
|
|
state->z4 = state->z3;
|
|
|
|
} else {
|
|
|
|
// TDF-II
|
2023-09-21 12:32:17 +00:00
|
|
|
const float k = 4.f * coeffs->b0;
|
|
|
|
state->z4 = (coeffs->b0 + coeffs->ma4) * x_0;
|
|
|
|
state->z3 = (k + coeffs->ma3) * x_0 + state->z4;
|
|
|
|
state->z2 = (6.f * coeffs->b0 + coeffs->ma2) * x_0 + state->z3;
|
|
|
|
state->z1 = (k + coeffs->ma1) * x_0 + state->z2;
|
2023-03-15 19:50:39 +00:00
|
|
|
}
|
2023-03-23 14:58:32 +00:00
|
|
|
state->i = 0.f;
|
2023-08-13 09:42:06 +00:00
|
|
|
state->xz1 = x_0;
|
|
|
|
state->xz2 = x_0;
|
|
|
|
state->xz3 = x_0;
|
2023-09-21 12:32:17 +00:00
|
|
|
const float y = x_0;
|
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
state->hash = bw_hash_sdbm("bw_src_state");
|
|
|
|
state->coeffs_reset_id = coeffs->reset_id;
|
|
|
|
#endif
|
|
|
|
BW_ASSERT_DEEP(bw_src_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(bw_src_state_is_valid(coeffs, state));
|
|
|
|
BW_ASSERT(bw_is_finite(y));
|
|
|
|
|
|
|
|
return y;
|
2023-03-15 19:50:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 12:32:17 +00:00
|
|
|
static inline void bw_src_reset_state_multi(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_src_state * BW_RESTRICT const * BW_RESTRICT state,
|
2023-09-25 14:47:17 +00:00
|
|
|
const float * x_0,
|
|
|
|
float * y_0,
|
|
|
|
size_t n_channels) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_src_coeffs_is_valid(coeffs));
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-25 14:47:17 +00:00
|
|
|
#ifndef BW_NO_DEBUG
|
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
|
|
|
for (size_t j = i + 1; j < n_channels; j++)
|
|
|
|
BW_ASSERT(state[i] != state[j]);
|
|
|
|
#endif
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(x_0 != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
|
2024-01-02 10:20:52 +00:00
|
|
|
if (y_0 != BW_NULL)
|
2023-09-21 12:32:17 +00:00
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
|
|
|
y_0[i] = bw_src_reset_state(coeffs, state[i], x_0[i]);
|
|
|
|
else
|
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
|
|
|
bw_src_reset_state(coeffs, state[i], x_0[i]);
|
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_src_coeffs_is_valid(coeffs));
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT_DEEP(y_0 != BW_NULL ? bw_has_only_finite(y_0, n_channels) : 1);
|
2023-09-21 12:32:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_src_process(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_src_state * BW_RESTRICT state,
|
|
|
|
const float * BW_RESTRICT x,
|
|
|
|
float * BW_RESTRICT y,
|
|
|
|
size_t * BW_RESTRICT n_in_samples,
|
|
|
|
size_t * BW_RESTRICT n_out_samples) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_src_coeffs_is_valid(coeffs));
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_src_state_is_valid(coeffs, state));
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(n_in_samples != BW_NULL);
|
|
|
|
BW_ASSERT(n_out_samples != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT(n_in_samples != n_out_samples);
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(x != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_has_only_finite(x, *n_in_samples));
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(y != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT(x != y);
|
|
|
|
|
2023-08-13 09:42:06 +00:00
|
|
|
size_t i = 0;
|
|
|
|
size_t j = 0;
|
2023-09-21 12:32:17 +00:00
|
|
|
if (coeffs->k < 0.f) {
|
2023-03-15 19:50:39 +00:00
|
|
|
while (i < *n_in_samples && j < *n_out_samples) {
|
|
|
|
// DF-II
|
2023-09-21 12:32:17 +00:00
|
|
|
const float z0 = x[i] + coeffs->ma1 * state->z1 + coeffs->ma2 * state->z2 + coeffs->ma3 * state->z3 + coeffs->ma4 * state->z4;
|
|
|
|
const float o = coeffs->b0 * (z0 + state->z4 + 4.f * (state->z1 + state->z3) + 6.f * state->z2);
|
2023-03-24 15:01:33 +00:00
|
|
|
if (state->i >= 0.f) {
|
2023-03-15 19:50:39 +00:00
|
|
|
// 3rd degree Lagrange interpolation + Horner's rule
|
2023-03-24 15:01:33 +00:00
|
|
|
const float k1 = state->xz1 - state->xz2;
|
|
|
|
const float k2 = 0.333333333333333f * (state->xz3 - o);
|
|
|
|
const float k3 = o - k1;
|
2023-03-16 11:01:39 +00:00
|
|
|
const float k4 = k3 - state->xz1;
|
2023-03-24 15:01:33 +00:00
|
|
|
const float a = k2 - k4 - 0.5f * k4;
|
|
|
|
const float b = k3 - k1 - 0.5f * (state->xz1 + state->xz3);
|
2023-03-15 19:50:39 +00:00
|
|
|
const float c = 0.5f * (k1 + k2);
|
|
|
|
y[j] = o + state->i * (a + state->i * (b + state->i * c));
|
2023-03-24 15:01:33 +00:00
|
|
|
state->i += coeffs->k;
|
2023-03-15 19:50:39 +00:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
state->z4 = state->z3;
|
|
|
|
state->z3 = state->z2;
|
|
|
|
state->z2 = state->z1;
|
|
|
|
state->z1 = z0;
|
|
|
|
state->xz3 = state->xz2;
|
|
|
|
state->xz2 = state->xz1;
|
|
|
|
state->xz1 = o;
|
2023-03-24 15:13:59 +00:00
|
|
|
state->i += 1.f;
|
2023-03-15 19:50:39 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (i < *n_in_samples && j < *n_out_samples) {
|
2023-03-16 11:01:39 +00:00
|
|
|
while (state->i < 1.f && j < *n_out_samples) {
|
2023-03-15 19:50:39 +00:00
|
|
|
// 3rd degree Lagrange interpolation + Horner's rule
|
|
|
|
const float k1 = state->xz2 - state->xz1;
|
|
|
|
const float k2 = 0.333333333333333f * (x[i] - state->xz3);
|
|
|
|
const float k3 = state->xz3 - k1;
|
|
|
|
const float k4 = state->xz2 - k3;
|
2023-03-24 15:13:59 +00:00
|
|
|
const float a = k2 + k4 + 0.5f * k4;
|
2023-03-15 19:50:39 +00:00
|
|
|
const float b = k3 - k1 - 0.5f * (x[i] + state->xz2);
|
|
|
|
const float c = 0.5f * (k1 + k2);
|
|
|
|
const float o = state->xz3 + state->i * (a + state->i * (b + state->i * c));
|
|
|
|
// TDF-II
|
2023-04-04 13:13:26 +00:00
|
|
|
const float v0 = coeffs->b0 * o;
|
2023-09-21 12:32:17 +00:00
|
|
|
const float v1 = 4.f * v0;
|
|
|
|
const float v2 = 6.f * v0;
|
2023-04-04 13:13:26 +00:00
|
|
|
y[j] = v0 + state->z1;
|
2023-09-21 12:32:17 +00:00
|
|
|
state->z1 = v1 + coeffs->ma1 * y[j] + state->z2;
|
|
|
|
state->z2 = v2 + coeffs->ma2 * y[j] + state->z3;
|
|
|
|
state->z3 = v1 + coeffs->ma3 * y[j] + state->z4;
|
|
|
|
state->z4 = v0 + coeffs->ma4 * y[j];
|
2023-03-15 19:50:39 +00:00
|
|
|
state->i += coeffs->k;
|
|
|
|
j++;
|
|
|
|
}
|
2023-03-16 11:01:39 +00:00
|
|
|
if (state->i >= 1.f) {
|
|
|
|
state->xz3 = state->xz2;
|
|
|
|
state->xz2 = state->xz1;
|
|
|
|
state->xz1 = x[i];
|
|
|
|
state->i -= 1.f;
|
|
|
|
i++;
|
|
|
|
}
|
2023-03-15 19:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*n_in_samples = i;
|
2023-03-16 11:01:39 +00:00
|
|
|
*n_out_samples = j;
|
2023-09-21 12:32:17 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_src_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(bw_src_state_is_valid(coeffs, state));
|
|
|
|
BW_ASSERT(coeffs->k < 0.f ? *n_out_samples <= *n_in_samples : *n_out_samples >= *n_in_samples);
|
2023-03-15 19:50:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 12:32:17 +00:00
|
|
|
static inline void bw_src_process_multi(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_src_state * BW_RESTRICT const * BW_RESTRICT state,
|
|
|
|
const float * BW_RESTRICT const * BW_RESTRICT x,
|
|
|
|
float * BW_RESTRICT const * BW_RESTRICT y,
|
|
|
|
size_t n_channels,
|
|
|
|
size_t * BW_RESTRICT n_in_samples,
|
|
|
|
size_t * BW_RESTRICT n_out_samples) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_src_coeffs_is_valid(coeffs));
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-25 14:47:17 +00:00
|
|
|
#ifndef BW_NO_DEBUG
|
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
|
|
|
for (size_t j = i + 1; j < n_channels; j++)
|
|
|
|
BW_ASSERT(state[i] != state[j]);
|
|
|
|
#endif
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(x != BW_NULL);
|
|
|
|
BW_ASSERT(y != BW_NULL);
|
2023-09-25 15:16:42 +00:00
|
|
|
BW_ASSERT((void *)x != (void *)y);
|
2023-09-25 14:47:17 +00:00
|
|
|
#ifndef BW_NO_DEBUG
|
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
|
|
|
for (size_t j = i + 1; j < n_channels; j++)
|
|
|
|
BW_ASSERT(y[i] != y[j]);
|
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
|
|
|
for (size_t j = 0; j < n_channels; j++)
|
|
|
|
BW_ASSERT((void *)x[i] != (void *)y[j]);
|
|
|
|
#endif
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(n_in_samples != BW_NULL);
|
|
|
|
BW_ASSERT(n_out_samples != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
BW_ASSERT(n_in_samples != n_out_samples);
|
|
|
|
|
2023-08-13 09:42:06 +00:00
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
2023-08-13 13:37:15 +00:00
|
|
|
bw_src_process(coeffs, state[i], x[i], y[i], n_in_samples + i, n_out_samples + i);
|
2023-09-21 12:32:17 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_src_coeffs_is_valid(coeffs));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char bw_src_coeffs_is_valid(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
if (coeffs->hash != bw_hash_sdbm("bw_src_coeffs"))
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return bw_is_finite(coeffs->k) && coeffs->k != 0.f
|
|
|
|
&& bw_is_finite(coeffs->b0)
|
|
|
|
&& bw_is_finite(coeffs->ma1)
|
|
|
|
&& bw_is_finite(coeffs->ma2)
|
|
|
|
&& bw_is_finite(coeffs->ma3)
|
|
|
|
&& bw_is_finite(coeffs->ma4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char bw_src_state_is_valid(
|
|
|
|
const bw_src_coeffs * BW_RESTRICT coeffs,
|
|
|
|
const bw_src_state * BW_RESTRICT state) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-21 12:32:17 +00:00
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
if (state->hash != bw_hash_sdbm("bw_src_state"))
|
|
|
|
return 0;
|
|
|
|
|
2024-01-02 10:20:52 +00:00
|
|
|
if (coeffs != BW_NULL && coeffs->reset_id != state->coeffs_reset_id)
|
2023-09-21 12:32:17 +00:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
(void)coeffs;
|
|
|
|
|
|
|
|
return bw_is_finite(state->i)
|
|
|
|
&& bw_is_finite(state->z1)
|
|
|
|
&& bw_is_finite(state->z2)
|
|
|
|
&& bw_is_finite(state->z3)
|
|
|
|
&& bw_is_finite(state->z4)
|
|
|
|
&& bw_is_finite(state->xz1)
|
|
|
|
&& bw_is_finite(state->xz2)
|
|
|
|
&& bw_is_finite(state->xz3);
|
2023-07-04 13:33:18 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 19:50:39 +00:00
|
|
|
#ifdef __cplusplus
|
2023-08-13 09:42:06 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
|
|
|
# include <array>
|
|
|
|
#endif
|
2023-08-13 09:42:06 +00:00
|
|
|
|
|
|
|
namespace Brickworks {
|
|
|
|
|
|
|
|
/*** Public C++ API ***/
|
|
|
|
|
|
|
|
/*! api_cpp {{{
|
|
|
|
* ##### Brickworks::SRC
|
|
|
|
* ```>>> */
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
class SRC {
|
|
|
|
public:
|
2023-09-21 12:32:17 +00:00
|
|
|
SRC(
|
|
|
|
float ratio);
|
|
|
|
|
|
|
|
void reset(
|
|
|
|
float x0 = 0.f,
|
|
|
|
float * BW_RESTRICT y0 = nullptr);
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-09-21 12:32:17 +00:00
|
|
|
void reset(
|
|
|
|
float x0,
|
|
|
|
std::array<float, N_CHANNELS> * BW_RESTRICT y0);
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-09-21 12:32:17 +00:00
|
|
|
|
|
|
|
void reset(
|
|
|
|
const float * x0,
|
|
|
|
float * y0 = nullptr);
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-09-21 12:32:17 +00:00
|
|
|
void reset(
|
|
|
|
std::array<float, N_CHANNELS> x0,
|
|
|
|
std::array<float, N_CHANNELS> * BW_RESTRICT y0 = nullptr);
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-09-21 12:32:17 +00:00
|
|
|
|
2023-08-13 09:42:06 +00:00
|
|
|
void process(
|
2023-09-21 12:32:17 +00:00
|
|
|
const float * BW_RESTRICT const * BW_RESTRICT x,
|
|
|
|
float * BW_RESTRICT const * BW_RESTRICT y,
|
|
|
|
size_t * BW_RESTRICT nInSamples,
|
|
|
|
size_t * BW_RESTRICT nOutSamples);
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-08-13 09:42:06 +00:00
|
|
|
void process(
|
2023-09-21 12:32:17 +00:00
|
|
|
std::array<const float * BW_RESTRICT, N_CHANNELS> x,
|
|
|
|
std::array<float * BW_RESTRICT, N_CHANNELS> y,
|
2023-09-21 15:05:37 +00:00
|
|
|
std::array<size_t, N_CHANNELS> & nInSamples,
|
|
|
|
std::array<size_t, N_CHANNELS> & nOutSamples);
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-08-13 09:42:06 +00:00
|
|
|
/*! <<<...
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
* }}} */
|
|
|
|
|
|
|
|
/*** Implementation ***/
|
|
|
|
|
|
|
|
/* WARNING: This part of the file is not part of the public API. Its content may
|
|
|
|
* change at any time in future versions. Please, do not use it directly. */
|
|
|
|
|
|
|
|
private:
|
2023-09-21 12:32:17 +00:00
|
|
|
bw_src_coeffs coeffs;
|
|
|
|
bw_src_state states[N_CHANNELS];
|
|
|
|
bw_src_state * BW_RESTRICT statesP[N_CHANNELS];
|
2023-08-13 09:42:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-21 12:32:17 +00:00
|
|
|
inline SRC<N_CHANNELS>::SRC(
|
|
|
|
float ratio) {
|
2023-08-13 09:42:06 +00:00
|
|
|
bw_src_init(&coeffs, ratio);
|
|
|
|
for (size_t i = 0; i < N_CHANNELS; i++)
|
|
|
|
statesP[i] = states + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-21 12:32:17 +00:00
|
|
|
inline void SRC<N_CHANNELS>::reset(
|
|
|
|
float x0,
|
|
|
|
float * BW_RESTRICT y0) {
|
|
|
|
if (y0 != nullptr)
|
|
|
|
for (size_t i = 0; i < N_CHANNELS; i++)
|
|
|
|
y0[i] = bw_src_reset_state(&coeffs, states + i, x0);
|
|
|
|
else
|
|
|
|
for (size_t i = 0; i < N_CHANNELS; i++)
|
|
|
|
bw_src_reset_state(&coeffs, states + i, x0);
|
|
|
|
}
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-09-21 12:32:17 +00:00
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void SRC<N_CHANNELS>::reset(
|
|
|
|
float x0,
|
|
|
|
std::array<float, N_CHANNELS> * BW_RESTRICT y0) {
|
|
|
|
reset(x0, y0 != nullptr ? y0->data() : nullptr);
|
|
|
|
}
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-09-21 12:32:17 +00:00
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void SRC<N_CHANNELS>::reset(
|
|
|
|
const float * x0,
|
|
|
|
float * y0) {
|
|
|
|
bw_src_reset_state_multi(&coeffs, statesP, x0, y0, N_CHANNELS);
|
|
|
|
}
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-09-21 12:32:17 +00:00
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void SRC<N_CHANNELS>::reset(
|
|
|
|
std::array<float, N_CHANNELS> x0,
|
|
|
|
std::array<float, N_CHANNELS> * BW_RESTRICT y0) {
|
|
|
|
reset(x0.data(), y0 != nullptr ? y0->data() : nullptr);
|
2023-08-13 09:42:06 +00:00
|
|
|
}
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-08-13 09:42:06 +00:00
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void SRC<N_CHANNELS>::process(
|
2023-09-21 12:32:17 +00:00
|
|
|
const float * BW_RESTRICT const * BW_RESTRICT x,
|
|
|
|
float * BW_RESTRICT const * BW_RESTRICT y,
|
|
|
|
size_t * BW_RESTRICT nInSamples,
|
|
|
|
size_t * BW_RESTRICT nOutSamples) {
|
2023-08-13 09:42:06 +00:00
|
|
|
bw_src_process_multi(coeffs, statesP, x, y, N_CHANNELS, nInSamples, nOutSamples);
|
|
|
|
}
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-08-13 09:42:06 +00:00
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void SRC<N_CHANNELS>::process(
|
2023-09-21 12:32:17 +00:00
|
|
|
std::array<const float * BW_RESTRICT, N_CHANNELS> x,
|
|
|
|
std::array<float * BW_RESTRICT, N_CHANNELS> y,
|
2023-09-21 15:05:37 +00:00
|
|
|
std::array<size_t, N_CHANNELS> & nInSamples,
|
|
|
|
std::array<size_t, N_CHANNELS> & nOutSamples) {
|
2023-08-13 09:42:06 +00:00
|
|
|
process(x.data(), y.data(), nInSamples.data(), nOutSamples.data());
|
|
|
|
}
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-08-13 09:42:06 +00:00
|
|
|
|
2023-03-15 19:50:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|