finalized bw_src_int

This commit is contained in:
Stefano D'Angelo 2023-09-21 12:10:21 +02:00
parent 40adc296a2
commit 9b123a28f2

View File

@ -35,7 +35,12 @@
* <ul>
* <li>Version <strong>1.0.0</strong>:
* <ul>
* <li><code>Fixed frequency response and improved speed.</code></li>
* <li>Added <code>bw_src_int_reset_state_multi()</code> and updated
* C++ API in this regard.</li>
* <li>Now <code>bw_src_int_reset_state()</code> returns the initial
* output value.</li>
* <li>Added overloaded C++ <code>reset()</code> functions taking
* arrays as arguments.</li>
* <li><code>bw_src_int_lim_process()</code> and
* <code>bw_src_int_lim_process_multi()</code> now use
* <code>size_t</code> to count samples and channels.</li>
@ -44,9 +49,11 @@
* <li>Moved C++ code to C header.</li>
* <li>Added overloaded C++ <code>process()</code> function taking
* C-style arrays as arguments.</li>
* <li>Fixed frequency response and improved speed.</li>
* <li>Removed usage of reserved identifiers.</li>
* <li>Clarified that the same buffer cannot be used for both input and
* output.</li>
* <li>Added debugging code.</li>
* </ul>
* </li>
* <li>Version <strong>0.6.0</strong>:
@ -93,26 +100,55 @@ typedef struct bw_src_int_state bw_src_int_state;
*
* #### bw_src_int_init()
* ```>>> */
static inline void bw_src_int_init(bw_src_int_coeffs *BW_RESTRICT coeffs, int ratio);
static inline void bw_src_int_init(
bw_src_int_coeffs * BW_RESTRICT coeffs,
int ratio);
/*! <<<```
* Initializes `coeffs` using the given resampling `ratio`.
*
* If `ratio` is positive, then the sample rate of the output signal will be
* `ratio` times the sample rate of the input signal, otherwise, if it is
* negative, then the sample rate of the output signal will be equal to the
* sample rate of the input signal divided by `-ratio`. `ratio` *MUST NOT* be
* in [`-1`, `1`].
* sample rate of the input signal divided by `-ratio`.
*
* `ratio` must not be `0`.
*
* #### bw_src_int_reset_state()
* ```>>> */
static inline void bw_src_int_reset_state(const bw_src_int_coeffs *BW_RESTRICT coeffs, bw_src_int_state *BW_RESTRICT state, float x_0);
static inline float bw_src_int_reset_state(
const bw_src_int_coeffs * BW_RESTRICT coeffs,
bw_src_int_state * BW_RESTRICT state,
float x_0);
/*! <<<```
* Resets the given `state` to its initial values using the given `coeffs`
* and the quiescent/initial input value `x_0`.
* and the initial input value `x_0`.
*
* Returns the corresponding initial output value.
*
* #### bw_src_int_reset_state_multi()
* ```>>> */
static inline void bw_src_int_reset_state_multi(
const bw_src_int_coeffs * BW_RESTRICT coeffs,
bw_src_int_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,
* if not `NULL`.
*
* #### bw_src_int_process()
* ```>>> */
static inline size_t bw_src_int_process(const bw_src_int_coeffs *BW_RESTRICT coeffs, bw_src_int_state *BW_RESTRICT state, const float *BW_RESTRICT x, float *BW_RESTRICT y, size_t n_in_samples);
static inline size_t bw_src_int_process(
const bw_src_int_coeffs * BW_RESTRICT coeffs,
bw_src_int_state * BW_RESTRICT state,
const float * BW_RESTRICT x,
float * BW_RESTRICT y,
size_t n_in_samples);
/*! <<<```
* Processes the first `n_in_samples` of the input buffer `x` and fills the
* output buffer `y` using `coeffs`, while using and updating `state`.
@ -127,7 +163,14 @@ static inline size_t bw_src_int_process(const bw_src_int_coeffs *BW_RESTRICT coe
*
* #### bw_src_int_process_multi()
* ```>>> */
static inline void bw_src_int_process_multi(const bw_src_int_coeffs *BW_RESTRICT coeffs, bw_src_int_state *BW_RESTRICT const *BW_RESTRICT state, const float * const *BW_RESTRICT x, float *BW_RESTRICT const *BW_RESTRICT y, size_t *BW_RESTRICT n, size_t n_channels, size_t n_in_samples);
static inline void bw_src_int_process_multi(
const bw_src_int_coeffs * BW_RESTRICT coeffs,
bw_src_int_state * BW_RESTRICT const * BW_RESTRICT state,
const float * const * BW_RESTRICT x,
float *BW_RESTRICT const * BW_RESTRICT y,
size_t n_channels,
size_t n_in_samples,
size_t * BW_RESTRICT n_out_samples);
/*! <<<```
* Processes the first `n_in_samples` of the `n_channels` input buffers `x`
* and fills the `n_channels` output buffers `y` using `coeffs`, while using
@ -140,8 +183,36 @@ static inline void bw_src_int_process_multi(const bw_src_int_coeffs *BW_RESTRICT
*
* A given buffer cannot be used both as an input and output buffer.
*
* `n` is filled with the number of generated output samples for each output
* buffer, if not `NULL`.
* `n_out_samples` is filled with the number of generated output samples for
* each output buffer, if not `NULL`.
*
* #### bw_src_int_coeffs_is_valid()
* ```>>> */
static inline char bw_src_int_coeffs_is_valid(
const bw_src_int_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_int_coeffs`.
*
* #### bw_src_int_state_is_valid()
* ```>>> */
static inline char bw_src_int_state_is_valid(
const bw_src_int_coeffs * BW_RESTRICT coeffs,
const bw_src_int_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.
*
* If `coeffs` is not `NULL` extra cross-checks might be performed (`state`
* is supposed to be associated to `coeffs`).
*
* `state` must at least point to a readable memory block of size greater
* than or equal to that of `bw_src_int_state`.
* }}} */
#ifdef __cplusplus
@ -160,23 +231,39 @@ extern "C" {
#endif
struct bw_src_int_coeffs {
int ratio;
float b0;
float ma1;
float ma2;
float ma3;
float ma4;
#ifdef BW_DEBUG_DEEP
uint32_t hash;
uint32_t reset_id;
#endif
// Coefficients
int ratio;
float b0;
float ma1;
float ma2;
float ma3;
float ma4;
};
struct bw_src_int_state {
int i;
float z1;
float z2;
float z3;
float z4;
#ifdef BW_DEBUG_DEEP
uint32_t hash;
uint32_t coeffs_reset_id;
#endif
// States
int i;
float z1;
float z2;
float z3;
float z4;
};
static inline void bw_src_int_init(bw_src_int_coeffs *BW_RESTRICT coeffs, int ratio) {
static inline void bw_src_int_init(
bw_src_int_coeffs * BW_RESTRICT coeffs,
int ratio) {
BW_ASSERT(coeffs != NULL);
coeffs->ratio = ratio;
// 4th-degree Butterworth with cutoff at ratio * Nyquist, using bilinear transform w/ prewarping
const float fc = (float)(ratio >= 0 ? ratio : -ratio);
@ -188,9 +275,23 @@ static inline void bw_src_int_init(bw_src_int_coeffs *BW_RESTRICT coeffs, int ra
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_int_coeffs");
coeffs->reset_id = coeffs->hash + 1;
#endif
BW_ASSERT_DEEP(bw_src_int_coeffs_is_valid(coeffs));
}
static inline void bw_src_int_reset_state(const bw_src_int_coeffs *BW_RESTRICT coeffs, bw_src_int_state *BW_RESTRICT state, float x_0) {
static inline float bw_src_int_reset_state(
const bw_src_int_coeffs * BW_RESTRICT coeffs,
bw_src_int_state * BW_RESTRICT state,
float x_0) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_src_int_coeffs_is_valid(coeffs));
BW_ASSERT(state != NULL);
BW_ASSERT(bw_is_finite(x_0));
if (coeffs->ratio < 0) {
// DF-II
state->z1 = x_0 / (1.f - coeffs->ma1 - coeffs->ma2 - coeffs->ma3 - coeffs->ma4);
@ -206,9 +307,55 @@ static inline void bw_src_int_reset_state(const bw_src_int_coeffs *BW_RESTRICT c
state->z2 = (6.f * coeffs->b0 + coeffs->ma2) * x_0 + state->z3;
state->z1 = (k + coeffs->ma1) * x_0 + state->z2;
}
const float y = x_0;
#ifdef BW_DEBUG_DEEP
state->hash = bw_hash_sdbm("bw_src_int_state");
state->coeffs_reset_id = coeffs->reset_id;
#endif
BW_ASSERT_DEEP(bw_src_int_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(bw_src_int_state_is_valid(coeffs, state));
BW_ASSERT(bw_is_finite(y));
return y;
}
static inline size_t bw_src_int_process(const bw_src_int_coeffs *BW_RESTRICT coeffs, bw_src_int_state *BW_RESTRICT state, const float *BW_RESTRICT x, float *BW_RESTRICT y, size_t n_in_samples) {
static inline void bw_src_int_reset_state_multi(
const bw_src_int_coeffs * BW_RESTRICT coeffs,
bw_src_int_state * BW_RESTRICT const * BW_RESTRICT state,
const float * x_0,
float * y_0,
size_t n_channels) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_src_int_coeffs_is_valid(coeffs));
BW_ASSERT(state != NULL);
BW_ASSERT(x_0 != NULL);
if (y_0 != NULL)
for (size_t i = 0; i < n_channels; i++)
y_0[i] = bw_src_int_reset_state(coeffs, state[i], x_0[i]);
else
for (size_t i = 0; i < n_channels; i++)
bw_src_int_reset_state(coeffs, state[i], x_0[i]);
BW_ASSERT_DEEP(bw_src_int_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(y_0 != NULL ? bw_has_only_finite(y_0, n_channels) : 1);
}
static inline size_t bw_src_int_process(
const bw_src_int_coeffs * BW_RESTRICT coeffs,
bw_src_int_state * BW_RESTRICT state,
const float * BW_RESTRICT x,
float * BW_RESTRICT y,
size_t n_in_samples) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_src_int_coeffs_is_valid(coeffs));
BW_ASSERT(state != NULL);
BW_ASSERT_DEEP(bw_src_int_state_is_valid(coeffs, state));
BW_ASSERT(x != NULL);
BW_ASSERT_DEEP(bw_has_only_finite(x, n_in_samples));
BW_ASSERT(y != NULL);
size_t n = 0;
if (coeffs->ratio < 0) {
for (size_t i = 0; i < n_in_samples; i++) {
@ -250,16 +397,76 @@ static inline size_t bw_src_int_process(const bw_src_int_coeffs *BW_RESTRICT coe
}
}
}
BW_ASSERT_DEEP(bw_src_int_coeffs_is_valid(coeffs));
BW_ASSERT_DEEP(bw_src_int_state_is_valid(coeffs, state));
BW_ASSERT(n <= (coeffs->ratio > 0 ? coeffs->ratio * n_in_samples : n_in_samples / (-coeffs->ratio)) + 1);
return n;
}
static inline void bw_src_int_process_multi(const bw_src_int_coeffs *BW_RESTRICT coeffs, bw_src_int_state *BW_RESTRICT const *BW_RESTRICT state, const float * const *BW_RESTRICT x, float *BW_RESTRICT const *BW_RESTRICT y, size_t *BW_RESTRICT n, size_t n_channels, size_t n_in_samples) {
if (n != NULL)
static inline void bw_src_int_process_multi(
const bw_src_int_coeffs * BW_RESTRICT coeffs,
bw_src_int_state * BW_RESTRICT const * BW_RESTRICT state,
const float * const * BW_RESTRICT x,
float *BW_RESTRICT const * BW_RESTRICT y,
size_t n_channels,
size_t n_in_samples,
size_t * BW_RESTRICT n_out_samples) {
BW_ASSERT(coeffs != NULL);
BW_ASSERT_DEEP(bw_src_int_coeffs_is_valid(coeffs));
BW_ASSERT(state != NULL);
BW_ASSERT(x != NULL);
BW_ASSERT(y != NULL);
if (n_out_samples != NULL)
for (size_t i = 0; i < n_channels; i++)
n[i] = bw_src_int_process(coeffs, state[i], x[i], y[i], n_in_samples);
n_out_samples[i] = bw_src_int_process(coeffs, state[i], x[i], y[i], n_in_samples);
else
for (size_t i = 0; i < n_channels; i++)
bw_src_int_process(coeffs, state[i], x[i], y[i], n_in_samples);
BW_ASSERT_DEEP(bw_src_int_coeffs_is_valid(coeffs));
}
static inline char bw_src_int_coeffs_is_valid(
const bw_src_int_coeffs * BW_RESTRICT coeffs) {
BW_ASSERT(coeffs != NULL);
#ifdef BW_DEBUG_DEEP
if (coeffs->hash != bw_hash_sdbm("bw_src_int_coeffs"))
return 0;
#endif
return coeffs->ratio != 0
&& 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_int_state_is_valid(
const bw_src_int_coeffs * BW_RESTRICT coeffs,
const bw_src_int_state * BW_RESTRICT state) {
BW_ASSERT(state != NULL);
#ifdef BW_DEBUG_DEEP
if (state->hash != bw_hash_sdbm("bw_src_int_state"))
return 0;
if (coeffs != NULL && coeffs->reset_id != state->coeffs_reset_id)
return 0;
#endif
if (coeffs)
if (coeffs->ratio < 0 && (state->i < 0 || state->i >= -coeffs->ratio))
return 0;
return bw_is_finite(state->z1)
&& bw_is_finite(state->z2)
&& bw_is_finite(state->z3)
&& bw_is_finite(state->z4);
}
#ifdef __cplusplus
@ -278,16 +485,34 @@ template<size_t N_CHANNELS>
class SRCInt {
public:
SRCInt(int ratio);
void reset(float x_0 = 0.f);
std::array<size_t, N_CHANNELS> process(
const float * const *x,
float * const *y,
size_t nInSamples);
std::array<size_t, N_CHANNELS> process(
std::array<const float *, N_CHANNELS> x,
std::array<float *, N_CHANNELS> y,
size_t nInSamples);
void reset(
float x0 = 0.f,
float * BW_RESTRICT y0 = nullptr);
void reset(
float x0,
std::array<float, N_CHANNELS> * BW_RESTRICT y0);
void reset(
const float * x0,
float * y0 = nullptr);
void reset(
std::array<float, N_CHANNELS> x0,
std::array<float, N_CHANNELS> * BW_RESTRICT y0 = nullptr);
void process(
const float * const * BW_RESTRICT x,
float * const * BW_RESTRICT y,
size_t nInSamples,
size_t * BW_RESTRICT nOutSamples = nullptr);
void process(
std::array<const float * BW_RESTRICT, N_CHANNELS> x,
std::array<float * BW_RESTRICT, N_CHANNELS> y,
size_t nInSamples,
std::array<size_t, N_CHANNELS> * BW_RESTRICT nOutSamples = nullptr);
/*! <<<...
* }
* ```
@ -299,40 +524,68 @@ public:
* change at any time in future versions. Please, do not use it directly. */
private:
bw_src_int_coeffs coeffs;
bw_src_int_state states[N_CHANNELS];
bw_src_int_state *BW_RESTRICT statesP[N_CHANNELS];
bw_src_int_coeffs coeffs;
bw_src_int_state states[N_CHANNELS];
bw_src_int_state * BW_RESTRICT statesP[N_CHANNELS];
};
template<size_t N_CHANNELS>
inline SRCInt<N_CHANNELS>::SRCInt(int ratio) {
inline SRCInt<N_CHANNELS>::SRCInt(
int ratio) {
bw_src_int_init(&coeffs, ratio);
for (size_t i = 0; i < N_CHANNELS; i++)
statesP[i] = states + i;
}
template<size_t N_CHANNELS>
inline void SRCInt<N_CHANNELS>::reset(float x_0) {
for (size_t i = 0; i < N_CHANNELS; i++)
bw_src_int_reset_state(&coeffs, states + i, x_0);
inline void SRCInt<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_int_reset_state(&coeffs, states + i, x0);
else
for (size_t i = 0; i < N_CHANNELS; i++)
bw_src_int_reset_state(&coeffs, states + i, x0);
}
template<size_t N_CHANNELS>
inline std::array<size_t, N_CHANNELS> SRCInt<N_CHANNELS>::process(
const float * const *x,
float * const *y,
size_t nInSamples) {
std::array<size_t, N_CHANNELS> ret;
bw_src_int_process_multi(&coeffs, statesP, x, y, ret.data(), N_CHANNELS, nInSamples);
return ret;
inline void SRCInt<N_CHANNELS>::reset(
float x0,
std::array<float, N_CHANNELS> * BW_RESTRICT y0) {
reset(x0, y0 != nullptr ? y0->data() : nullptr);
}
template<size_t N_CHANNELS>
inline std::array<size_t, N_CHANNELS> SRCInt<N_CHANNELS>::process(
std::array<const float *, N_CHANNELS> x,
std::array<float *, N_CHANNELS> y,
size_t nInSamples) {
return process(x.data(), y.data(), nInSamples);
inline void SRCInt<N_CHANNELS>::reset(
const float * x0,
float * y0) {
bw_src_int_reset_state_multi(&coeffs, statesP, x0, y0, N_CHANNELS);
}
template<size_t N_CHANNELS>
inline void SRCInt<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);
}
template<size_t N_CHANNELS>
inline void SRCInt<N_CHANNELS>::process(
const float * const * BW_RESTRICT x,
float * const * BW_RESTRICT y,
size_t nInSamples,
size_t * BW_RESTRICT nOutSamples) {
bw_src_int_process_multi(&coeffs, statesP, x, y, N_CHANNELS, nInSamples, nOutSamples);
}
template<size_t N_CHANNELS>
inline void SRCInt<N_CHANNELS>::process(
std::array<const float * BW_RESTRICT, N_CHANNELS> x,
std::array<float * BW_RESTRICT, N_CHANNELS> y,
size_t nInSamples,
std::array<size_t, N_CHANNELS> * BW_RESTRICT nOutSamples) {
process(x.data(), y.data(), nInSamples, nOutSamples ? nOutSamples->data() : nullptr);
}
}