Brickworks > API documentation > bw_src

Aribtrary-ratio IIR sample rate converter.

Module type: dsp

Version: 1.0.0

Requires:

```
typedef struct bw_src_coeffs bw_src_coeffs;
```

Coefficients and related.

```
typedef struct bw_src_state bw_src_state;
```

Internal state and related.

```
static inline void bw_src_init(
bw_src_coeffs * BW_RESTRICT coeffs,
float ratio);
```

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.

```
static inline float bw_src_reset_state(
const bw_src_coeffs * BW_RESTRICT coeffs,
bw_src_state * BW_RESTRICT state,
float x_0);
```

Resets the given `state`

to its initial values using the given `coeffs`

and the initial input value `x_0`

.

Returns the corresponding initial output value.

```
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, if not `NULL`

.

```
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);
```

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.

`x`

and `y`

must point to different buffers and also `n_in_samples`

. Also, `n_in_samples`

and `n_out_samples`

must be different.

```
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);
```

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.

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.

```
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`

.

```
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.

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_state`

.

```
template<size_t N_CHANNELS>
class SRC {
public:
SRC(
float ratio);
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 * BW_RESTRICT const * BW_RESTRICT x,
float * BW_RESTRICT const * BW_RESTRICT y,
size_t * BW_RESTRICT nInSamples,
size_t * BW_RESTRICT nOutSamples);
void process(
std::array<const float * BW_RESTRICT, N_CHANNELS> x,
std::array<float * BW_RESTRICT, N_CHANNELS> y,
std::array<size_t, N_CHANNELS> & nInSamples,
std::array<size_t, N_CHANNELS> & nOutSamples);
...
}
```

- Version
**1.0.0**:- Added
`bw_src_reset_state_multi()`

and updated C++ API in this regard. - Now
`bw_src_reset_state()`

returns the initial output value. - Added overloaded C++
`reset()`

functions taking arrays as arguments. `bw_src_process()`

and`bw_src_process_multi()`

now use`size_t`

to count samples and channels.- Added more
`const`

and`BW_RESTRICT`

specifiers to input arguments and implementation. - Moved C++ code to C header and fixed C++ API.
- Added overloaded C++
`process()`

function taking C-style arrays as arguments. - Removed usage of reserved identifiers.
- Removed useless computation when upsampling.
- Clarified when the same buffers cannot be used for both input and output.
- Added debugging code.

- Added
- Version
**0.6.0**:- Removed dependency on bw_config.

- Version
**0.5.0**:- Added
`bw_src_process_multi()`

. - Added C++ wrapper.

- Added
- Version
**0.4.0**:- First release.