2022-11-15 23:49:51 +00:00
|
|
|
/*
|
|
|
|
* Brickworks
|
|
|
|
*
|
2024-01-02 10:20:52 +00:00
|
|
|
* Copyright (C) 2022-2024 Orastron Srl unipersonale
|
2022-11-15 23:49:51 +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
|
2022-12-06 08:01:34 +00:00
|
|
|
* along with Brickworks. If not, see <http://www.gnu.org/licenses/>.
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
|
|
|
* File author: Stefano D'Angelo
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* module_type {{{ dsp }}}
|
2024-01-02 10:20:52 +00:00
|
|
|
* version {{{ 1.0.1 }}}
|
2023-07-21 06:56:27 +00:00
|
|
|
* requires {{{ bw_common bw_math bw_rand }}}
|
2022-11-15 23:49:51 +00:00
|
|
|
* description {{{
|
|
|
|
* Generator of white noise with uniform distribution.
|
2022-12-03 12:07:44 +00:00
|
|
|
*
|
|
|
|
* This module has no internal state, rather its state is stored into a
|
|
|
|
* `uint64_t` value to which the API user supplies a pointer (as in
|
|
|
|
* [bw\_rand](bw_rand)).
|
2022-11-15 23:49:51 +00:00
|
|
|
* }}}
|
|
|
|
* changelog {{{
|
|
|
|
* <ul>
|
2024-01-02 10:20:52 +00:00
|
|
|
* <li>Version <strong>1.0.1</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Now using <code>BW_NULL</code>.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
2023-08-08 08:46:05 +00:00
|
|
|
* <li>Version <strong>1.0.0</strong>:
|
|
|
|
* <ul>
|
2023-09-21 05:59:41 +00:00
|
|
|
* <li>Added <code>bw_noise_gen_reset_coeffs()</code>,
|
|
|
|
* <code>bw_noise_gen_update_coeffs_ctrl()</code>,
|
|
|
|
* and <code>bw_noise_gen_update_coeffs_audio()</code>.</li>
|
2023-08-13 02:55:29 +00:00
|
|
|
* <li><code>bw_noise_gen_process()</code> and
|
|
|
|
* <code>bw_noise_gen_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 02:55:29 +00:00
|
|
|
* <li>Moved C++ code to C header.</li>
|
2023-09-05 09:35:30 +00:00
|
|
|
* <li>Added overloaded C++ <code>process()</code> function taking
|
2023-08-13 02:55:29 +00:00
|
|
|
* C-style arrays as arguments.</li>
|
|
|
|
* <li>Removed usage of reserved identifiers.</li>
|
2023-09-21 05:59:41 +00:00
|
|
|
* <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 07:34:37 +00:00
|
|
|
* <li>Version <strong>0.5.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Added <code>bw_noise_gen_process_multi()</code>.</li>
|
2023-07-13 10:35:37 +00:00
|
|
|
* <li>Added C++ wrapper.</li>
|
2023-07-04 07:34:37 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2022-11-19 01:25:38 +00:00
|
|
|
* <li>Version <strong>0.2.0</strong>:
|
|
|
|
* <ul>
|
2022-11-28 10:50:22 +00:00
|
|
|
* <li>Refactored API.</li>
|
2022-11-19 01:25:38 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2022-11-15 23:49:51 +00:00
|
|
|
* <li>Version <strong>0.1.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>First release.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
|
|
|
* </ul>
|
|
|
|
* }}}
|
|
|
|
*/
|
|
|
|
|
2023-08-13 02:55:29 +00:00
|
|
|
#ifndef BW_NOISE_GEN_H
|
|
|
|
#define BW_NOISE_GEN_H
|
2022-11-15 23:49:51 +00:00
|
|
|
|
|
|
|
#include <bw_common.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*! api {{{
|
2022-11-28 10:50:22 +00:00
|
|
|
* #### bw_noise_gen_coeffs
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2023-08-13 02:55:29 +00:00
|
|
|
typedef struct bw_noise_gen_coeffs bw_noise_gen_coeffs;
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 12:07:44 +00:00
|
|
|
* Coefficients and related.
|
2022-11-28 10:50:22 +00:00
|
|
|
*
|
2022-11-19 01:25:38 +00:00
|
|
|
* #### bw_noise_gen_init()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_init(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
uint64_t * BW_RESTRICT state);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 12:07:44 +00:00
|
|
|
* Initializes input parameter values and sets the `state` pointer to obtain
|
|
|
|
* pseudo-random numbers in `coeffs`.
|
2022-11-28 10:50:22 +00:00
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_noise_gen_set_sample_rate()
|
|
|
|
* ```>>> */
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_set_sample_rate(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float sample_rate);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 12:07:44 +00:00
|
|
|
* Sets the `sample_rate` (Hz) value in `coeffs`.
|
|
|
|
*
|
2023-09-21 05:59:41 +00:00
|
|
|
* #### bw_noise_gen_reset_coeffs()
|
|
|
|
* ```>>> */
|
|
|
|
static inline void bw_noise_gen_reset_coeffs(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs);
|
|
|
|
/*! <<<```
|
|
|
|
* Resets coefficients in `coeffs` to assume their target values.
|
|
|
|
*
|
|
|
|
* #### bw_noise_gen_update_coeffs_ctrl()
|
|
|
|
* ```>>> */
|
|
|
|
static inline void bw_noise_gen_update_coeffs_ctrl(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs);
|
|
|
|
/*! <<<```
|
|
|
|
* Triggers control-rate update of coefficients in `coeffs`.
|
|
|
|
*
|
|
|
|
* #### bw_noise_gen_update_coeffs_audio()
|
|
|
|
* ```>>> */
|
|
|
|
static inline void bw_noise_gen_update_coeffs_audio(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs);
|
|
|
|
/*! <<<```
|
|
|
|
* Triggers audio-rate update of coefficients in `coeffs`.
|
|
|
|
*
|
2022-12-03 12:07:44 +00:00
|
|
|
* #### bw_noise_gen_process1\*()
|
|
|
|
* ```>>> */
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline float bw_noise_gen_process1(
|
|
|
|
const bw_noise_gen_coeffs * BW_RESTRICT coeffs);
|
|
|
|
|
|
|
|
static inline float bw_noise_gen_process1_scaling(
|
|
|
|
const bw_noise_gen_coeffs * BW_RESTRICT coeffs);
|
2022-12-03 12:07:44 +00:00
|
|
|
/*! <<<```
|
2022-12-04 09:34:42 +00:00
|
|
|
* These functions generate and return one sample using `coeffs`, where:
|
2022-12-03 12:07:44 +00:00
|
|
|
* * `bw_noise_gen_process1()` assumes that sample rate scaling is disabled;
|
|
|
|
* * `bw_noise_gen_process1_scaling()` assumes that sample rate scaling is
|
|
|
|
* enabled.
|
|
|
|
*
|
2023-09-21 05:59:41 +00:00
|
|
|
* Whether sample rate scaling is enabled or not is unchecked even for
|
|
|
|
* debugging purposes.
|
|
|
|
*
|
2022-12-04 09:34:42 +00:00
|
|
|
* #### bw_noise_gen_process()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_process(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float * BW_RESTRICT y,
|
|
|
|
size_t n_samples);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 12:07:44 +00:00
|
|
|
* Generates and fills the first `n_samples` of the output buffer `y` using
|
|
|
|
* `coeffs`.
|
|
|
|
*
|
2023-07-04 07:34:37 +00:00
|
|
|
* #### bw_noise_gen_process_multi()
|
|
|
|
* ```>>> */
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_process_multi(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float * BW_RESTRICT const * BW_RESTRICT y,
|
|
|
|
size_t n_channels,
|
|
|
|
size_t n_samples);
|
2023-07-04 07:34:37 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Generates and fills the first `n_samples` of the `n_channels` output
|
|
|
|
* buffers `y` using `coeffs`.
|
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_noise_gen_set_sample_rate_scaling()
|
|
|
|
* ```>>> */
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_set_sample_rate_scaling(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
char value);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Sets whether the output should be scaled (`value` non-`0`) or not (`0`)
|
2022-12-03 12:07:44 +00:00
|
|
|
* according to the sample rate in `coeffs`.
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
2022-12-03 12:07:44 +00:00
|
|
|
* In order to maintain the same perceived loudness at different sample
|
|
|
|
* rates, a white noise signal with uniform distribution should be
|
|
|
|
* accordingly scaled. The 44100 Hz sample rate is used as a reference (that
|
|
|
|
* is, the scaling factor at that sample rate is `1.f`).
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
2022-12-03 12:07:44 +00:00
|
|
|
* Default value: `0` (off).
|
|
|
|
*
|
|
|
|
* #### bw_noise_gen_get_scaling_k()
|
|
|
|
* ```>>> */
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline float bw_noise_gen_get_scaling_k(
|
|
|
|
const bw_noise_gen_coeffs * BW_RESTRICT coeffs);
|
2022-12-03 12:07:44 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Returns the sample rate scaling factor that is applied or would be applied
|
|
|
|
* if sample rate scaling were enabled, as stored in `coeffs`.
|
2023-09-21 05:59:41 +00:00
|
|
|
*
|
2023-09-28 11:31:20 +00:00
|
|
|
* `coeffs` must be at least inthe "sample-rate-set" state.
|
|
|
|
*
|
2023-09-21 05:59:41 +00:00
|
|
|
* #### bw_noise_gen_coeffs_is_valid()
|
|
|
|
* ```>>> */
|
|
|
|
static inline char bw_noise_gen_coeffs_is_valid(
|
|
|
|
const bw_noise_gen_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_noise_gen_coeffs`.
|
2022-12-03 12:07:44 +00:00
|
|
|
* }}} */
|
2022-11-30 19:36:13 +00:00
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-28 10:50:22 +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. */
|
|
|
|
|
|
|
|
#include <bw_math.h>
|
|
|
|
#include <bw_rand.h>
|
2023-08-12 08:15:04 +00:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
enum bw_noise_gen_coeffs_state {
|
|
|
|
bw_noise_gen_coeffs_state_invalid,
|
|
|
|
bw_noise_gen_coeffs_state_init,
|
|
|
|
bw_noise_gen_coeffs_state_set_sample_rate,
|
|
|
|
bw_noise_gen_coeffs_state_reset_coeffs
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2023-08-13 02:55:29 +00:00
|
|
|
struct bw_noise_gen_coeffs {
|
2023-09-21 05:59:41 +00:00
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
uint32_t hash;
|
|
|
|
enum bw_noise_gen_coeffs_state state;
|
|
|
|
#endif
|
|
|
|
|
2022-11-19 01:25:38 +00:00
|
|
|
// Coefficients
|
2023-09-21 05:59:41 +00:00
|
|
|
float scaling_k;
|
2022-11-19 01:25:38 +00:00
|
|
|
|
|
|
|
// Parameters
|
2023-09-21 05:59:41 +00:00
|
|
|
uint64_t * BW_RESTRICT rand_state;
|
|
|
|
char sample_rate_scaling;
|
2022-11-19 01:25:38 +00:00
|
|
|
};
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_init(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
uint64_t * BW_RESTRICT state) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
|
|
|
|
coeffs->rand_state = state;
|
2022-11-28 10:50:22 +00:00
|
|
|
coeffs->sample_rate_scaling = 0;
|
2023-09-21 05:59:41 +00:00
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
coeffs->hash = bw_hash_sdbm("bw_noise_gen_coeffs");
|
|
|
|
coeffs->state = bw_noise_gen_coeffs_state_init;
|
|
|
|
#endif
|
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state == bw_noise_gen_coeffs_state_init);
|
2022-11-28 10:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_set_sample_rate(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float sample_rate) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_init);
|
|
|
|
BW_ASSERT(bw_is_finite(sample_rate) && sample_rate > 0.f);
|
|
|
|
|
2023-08-10 08:17:22 +00:00
|
|
|
coeffs->scaling_k = 0.004761904761904762f * bw_sqrtf(sample_rate);
|
2023-09-21 05:59:41 +00:00
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
coeffs->state = bw_noise_gen_coeffs_state_set_sample_rate;
|
|
|
|
#endif
|
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state == bw_noise_gen_coeffs_state_set_sample_rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_noise_gen_reset_coeffs(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_set_sample_rate);
|
|
|
|
|
|
|
|
(void)coeffs;
|
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
coeffs->state = bw_noise_gen_coeffs_state_reset_coeffs;
|
|
|
|
#endif
|
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state == bw_noise_gen_coeffs_state_reset_coeffs);
|
2022-11-28 10:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_update_coeffs_ctrl(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
|
|
|
|
|
|
|
|
(void)coeffs;
|
2022-11-28 10:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_update_coeffs_audio(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
|
|
|
|
|
|
|
|
(void)coeffs;
|
2022-11-28 10:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline float bw_noise_gen_process1(
|
|
|
|
const bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
|
|
|
|
|
|
|
|
const float y = bw_randf(coeffs->rand_state);
|
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
|
|
|
|
BW_ASSERT(bw_is_finite(y));
|
|
|
|
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float bw_noise_gen_process1_scaling(
|
|
|
|
const bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
|
|
|
|
|
|
|
|
const float y = coeffs->scaling_k * bw_randf(coeffs->rand_state);
|
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
|
|
|
|
BW_ASSERT(bw_is_finite(y));
|
|
|
|
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_noise_gen_process(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float * BW_RESTRICT y,
|
|
|
|
size_t n_samples) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(y != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
|
2022-11-28 10:50:22 +00:00
|
|
|
if (coeffs->sample_rate_scaling)
|
2023-08-13 02:55:29 +00:00
|
|
|
for (size_t i = 0; i < n_samples; i++)
|
2022-11-28 10:50:22 +00:00
|
|
|
y[i] = bw_noise_gen_process1(coeffs);
|
|
|
|
else
|
2023-08-13 02:55:29 +00:00
|
|
|
for (size_t i = 0; i < n_samples; i++)
|
2022-11-28 10:50:22 +00:00
|
|
|
y[i] = bw_noise_gen_process1_scaling(coeffs);
|
2023-09-21 05:59:41 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
|
|
|
|
BW_ASSERT_DEEP(bw_has_only_finite(y, n_samples));
|
2022-11-28 10:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_process_multi(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float * BW_RESTRICT const * BW_RESTRICT y,
|
|
|
|
size_t n_channels,
|
|
|
|
size_t n_samples) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(y != 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(y[i] != y[j]);
|
|
|
|
#endif
|
2023-09-21 05:59:41 +00:00
|
|
|
|
2023-08-13 02:55:29 +00:00
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
2023-07-04 07:34:37 +00:00
|
|
|
bw_noise_gen_process(coeffs, y[i], n_samples);
|
2023-09-21 05:59:41 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_reset_coeffs);
|
2023-07-04 07:34:37 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline void bw_noise_gen_set_sample_rate_scaling(
|
|
|
|
bw_noise_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
char value) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_init);
|
|
|
|
|
2022-11-28 10:50:22 +00:00
|
|
|
coeffs->sample_rate_scaling = value;
|
2023-09-21 05:59:41 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_init);
|
2022-11-28 10:50:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline float bw_noise_gen_get_scaling_k(
|
|
|
|
const bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
BW_ASSERT_DEEP(bw_noise_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_noise_gen_coeffs_state_set_sample_rate);
|
|
|
|
|
2022-11-30 19:36:13 +00:00
|
|
|
return coeffs->scaling_k;
|
|
|
|
}
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
static inline char bw_noise_gen_coeffs_is_valid(
|
|
|
|
const bw_noise_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-21 05:59:41 +00:00
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
if (coeffs->hash != bw_hash_sdbm("bw_noise_gen_coeffs"))
|
|
|
|
return 0;
|
|
|
|
if (coeffs->state < bw_noise_gen_coeffs_state_init || coeffs->state > bw_noise_gen_coeffs_state_reset_coeffs)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
2024-01-02 10:20:52 +00:00
|
|
|
if (coeffs->rand_state == BW_NULL)
|
2023-09-21 05:59:41 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
if (coeffs->state >= bw_noise_gen_coeffs_state_set_sample_rate && coeffs->scaling_k <= 0.f)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
#ifdef __cplusplus
|
2023-08-13 02:55:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
namespace Brickworks {
|
|
|
|
|
|
|
|
/*** Public C++ API ***/
|
|
|
|
|
|
|
|
/*! api_cpp {{{
|
|
|
|
* ##### Brickworks::NoiseGen
|
|
|
|
* ```>>> */
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
class NoiseGen {
|
|
|
|
public:
|
2023-09-21 05:59:41 +00:00
|
|
|
NoiseGen(
|
|
|
|
uint64_t * BW_RESTRICT state);
|
|
|
|
|
|
|
|
void setSampleRate(
|
|
|
|
float sampleRate);
|
|
|
|
|
|
|
|
void reset();
|
2023-08-13 02:55:29 +00:00
|
|
|
|
|
|
|
void process(
|
2023-09-21 05:59:41 +00:00
|
|
|
float * BW_RESTRICT const * BW_RESTRICT y,
|
|
|
|
size_t nSamples);
|
|
|
|
|
2023-08-13 02:55:29 +00:00
|
|
|
void process(
|
2023-09-21 05:59:41 +00:00
|
|
|
std::array<float * BW_RESTRICT, N_CHANNELS> y,
|
|
|
|
size_t nSamples);
|
2023-08-13 02:55:29 +00:00
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
void setSampleRateScaling(
|
|
|
|
bool value);
|
2023-08-13 02:55:29 +00:00
|
|
|
|
|
|
|
float getScalingK();
|
|
|
|
/*! <<<...
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
* }}} */
|
|
|
|
|
|
|
|
/*** 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 05:59:41 +00:00
|
|
|
bw_noise_gen_coeffs coeffs;
|
2023-08-13 02:55:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-21 05:59:41 +00:00
|
|
|
inline NoiseGen<N_CHANNELS>::NoiseGen(
|
|
|
|
uint64_t * BW_RESTRICT state) {
|
2023-08-13 02:55:29 +00:00
|
|
|
bw_noise_gen_init(&coeffs, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-21 05:59:41 +00:00
|
|
|
inline void NoiseGen<N_CHANNELS>::setSampleRate(
|
|
|
|
float sampleRate) {
|
2023-08-13 02:55:29 +00:00
|
|
|
bw_noise_gen_set_sample_rate(&coeffs, sampleRate);
|
|
|
|
}
|
|
|
|
|
2023-09-21 05:59:41 +00:00
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void NoiseGen<N_CHANNELS>::reset() {
|
|
|
|
bw_noise_gen_reset_coeffs(&coeffs);
|
|
|
|
}
|
|
|
|
|
2023-08-13 02:55:29 +00:00
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void NoiseGen<N_CHANNELS>::process(
|
2023-08-14 07:57:06 +00:00
|
|
|
float *BW_RESTRICT const *BW_RESTRICT y,
|
2023-09-21 05:59:41 +00:00
|
|
|
size_t nSamples) {
|
2023-08-13 02:55:29 +00:00
|
|
|
bw_noise_gen_process_multi(&coeffs, y, N_CHANNELS, nSamples);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void NoiseGen<N_CHANNELS>::process(
|
2023-09-21 05:59:41 +00:00
|
|
|
std::array<float * BW_RESTRICT, N_CHANNELS> y,
|
|
|
|
size_t nSamples) {
|
2023-08-13 02:55:29 +00:00
|
|
|
process(y.data(), nSamples);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-21 05:59:41 +00:00
|
|
|
inline void NoiseGen<N_CHANNELS>::setSampleRateScaling(
|
|
|
|
bool value) {
|
2023-08-13 02:55:29 +00:00
|
|
|
bw_noise_gen_set_sample_rate_scaling(&coeffs, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline float NoiseGen<N_CHANNELS>::getScalingK() {
|
|
|
|
return bw_noise_gen_get_scaling_k(&coeffs);
|
|
|
|
}
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|