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 }}}
|
2023-10-16 13:34:25 +00:00
|
|
|
* version {{{ 1.1.0 }}}
|
2023-07-21 06:56:27 +00:00
|
|
|
* requires {{{ bw_common bw_math bw_one_pole }}}
|
2022-11-15 23:49:51 +00:00
|
|
|
* description {{{
|
|
|
|
* Linear ADSR envelope generator.
|
|
|
|
*
|
2022-12-03 10:57:09 +00:00
|
|
|
* * In the off phase (gate off and possible previous release phase ended),
|
2022-11-15 23:49:51 +00:00
|
|
|
* the output is `0.f`;
|
|
|
|
* * in the attack phase the output increases from `0.f` to `1.f`;
|
|
|
|
* * in the decay phase the output decreases from `1.f` to the given sustain
|
|
|
|
* level;
|
|
|
|
* * in the sustain phase the output stays constant;
|
|
|
|
* * in the release phase the output decreases from the current level to
|
|
|
|
* `0.f`.
|
|
|
|
*
|
|
|
|
* Attack, decay, and release time parameters are not actually controlling
|
|
|
|
* times but rather slopes. If all phases of the ADSR fully execute, then the
|
|
|
|
* actual times will match the parameter settings.
|
|
|
|
* }}}
|
|
|
|
* changelog {{{
|
|
|
|
* <ul>
|
2023-10-16 13:34:25 +00:00
|
|
|
* <li>Version <strong>1.1.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Added skip_sustain and always_reach_sustain parameters.</li>
|
2024-02-02 16:42:12 +00:00
|
|
|
* <li>Now using <code>BW_NULL</code> and
|
|
|
|
* <code>BW_CXX_NO_ARRAY</code>.</li>
|
2024-02-01 14:12:51 +00:00
|
|
|
* <li>Replaced GCC pragmas to suppress bogus uninitialized variable
|
|
|
|
* warnings with useless harmless statement.</li>
|
2023-10-16 13:34:25 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2023-08-08 08:46:05 +00:00
|
|
|
* <li>Version <strong>1.0.0</strong>:
|
|
|
|
* <ul>
|
2023-08-14 10:22:56 +00:00
|
|
|
* <li>Renamed <code>bw_env_gen_update_state_ctrl()</code> as
|
|
|
|
* <code>bw_env_gen_process_ctrl()</code>.</li>
|
2023-09-27 18:36:11 +00:00
|
|
|
* <li>Added initial gate value to
|
|
|
|
* <code>bw_env_gen_reset_state()</code>.</li>
|
2023-09-20 06:26:17 +00:00
|
|
|
* <li>Added <code>bw_env_gen_reset_state_multi()</code> and updated
|
|
|
|
* C++ API in this regard.</li>
|
|
|
|
* <li>Now <code>bw_env_gen_reset_state()</code> returns the initial
|
|
|
|
* output value.</li>
|
|
|
|
* <li>Added overloaded C++ <code>reset()</code> functions taking
|
|
|
|
* arrays as arguments.</li>
|
2023-08-12 17:46:14 +00:00
|
|
|
* <li><code>bw_env_gen_process()</code> and
|
|
|
|
* <code>bw_env_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-12 17:46:14 +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-12 17:46:14 +00:00
|
|
|
* C-style arrays as arguments.</li>
|
2023-09-20 06:26:17 +00:00
|
|
|
* <li>Faster and more robust implementation.</li>
|
2023-08-12 17:46:14 +00:00
|
|
|
* <li>Removed usage of reserved identifiers.</li>
|
2023-08-30 09:44:59 +00:00
|
|
|
* <li>Added pragmas to silence bogus GCC uninitialized variable
|
|
|
|
* warnings.</li>
|
2023-09-20 06:26:17 +00:00
|
|
|
* <li>Clearly specified parameter validity ranges.</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-06-06 14:17:47 +00:00
|
|
|
* <li>Version <strong>0.5.0</strong>:
|
|
|
|
* <ul>
|
2023-06-07 13:41:20 +00:00
|
|
|
* <li>Added <code>bw_env_follow_process_multi()</code>.</li>
|
2023-06-06 14:17:47 +00:00
|
|
|
* <li>Added <code>gate</code> argument to
|
|
|
|
* <code>bw_env_gen_update_state_ctrl()</code> and
|
|
|
|
* <code>bw_env_gen_process()</code>, and removed gate
|
|
|
|
* parameter.</li>
|
2023-07-13 10:35:37 +00:00
|
|
|
* <li>Added C++ wrapper.</li>
|
2023-06-06 14:17:47 +00:00
|
|
|
* </ul>
|
|
|
|
* </li>
|
2023-04-02 19:54:17 +00:00
|
|
|
* <li>Version <strong>0.4.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Fixed unused parameter warnings.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
2023-02-07 11:44:41 +00:00
|
|
|
* <li>Version <strong>0.3.0</strong>:
|
|
|
|
* <ul>
|
|
|
|
* <li>Avoid a warning related to a potentially uninitialized
|
|
|
|
* variable.</li>
|
|
|
|
* </ul>
|
|
|
|
* </li>
|
2022-11-19 01:25:38 +00:00
|
|
|
* <li>Version <strong>0.2.0</strong>:
|
|
|
|
* <ul>
|
2022-11-29 16:05:46 +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-12 17:46:14 +00:00
|
|
|
#ifndef BW_ENV_GEN_H
|
|
|
|
#define BW_ENV_GEN_H
|
2022-11-15 23:49:51 +00:00
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#include <bw_common.h>
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*! api {{{
|
2022-11-29 16:05:46 +00:00
|
|
|
* #### bw_env_gen_coeffs
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2023-08-12 17:46:14 +00:00
|
|
|
typedef struct bw_env_gen_coeffs bw_env_gen_coeffs;
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 12:07:44 +00:00
|
|
|
* Coefficients and related.
|
2022-11-29 16:05:46 +00:00
|
|
|
*
|
2022-12-03 10:57:09 +00:00
|
|
|
* #### bw_env_gen_state
|
2022-11-29 16:05:46 +00:00
|
|
|
* ```>>> */
|
2023-08-12 17:46:14 +00:00
|
|
|
typedef struct bw_env_gen_state bw_env_gen_state;
|
2022-11-29 16:05:46 +00:00
|
|
|
/*! <<<```
|
2022-12-03 12:07:44 +00:00
|
|
|
* Internal state and related.
|
2022-11-29 16:05:46 +00:00
|
|
|
*
|
|
|
|
* #### bw_env_gen_phase
|
|
|
|
* ```>>> */
|
|
|
|
typedef enum {
|
|
|
|
bw_env_gen_phase_off,
|
|
|
|
bw_env_gen_phase_attack,
|
|
|
|
bw_env_gen_phase_decay,
|
|
|
|
bw_env_gen_phase_sustain,
|
|
|
|
bw_env_gen_phase_release
|
|
|
|
} bw_env_gen_phase;
|
|
|
|
/*! <<<```
|
2022-12-03 10:57:09 +00:00
|
|
|
* Envelope generator phase:
|
|
|
|
* * `bw_env_gen_phase_off`: off phase;
|
|
|
|
* * `bw_env_gen_phase_attack`: attack phase;
|
|
|
|
* * `bw_env_gen_phase_decay`: decay phase;
|
|
|
|
* * `bw_env_gen_phase_sustain`: sustain phase;
|
|
|
|
* * `bw_env_gen_phase_release`: release phase.
|
2022-11-29 16:05:46 +00:00
|
|
|
*
|
2022-11-19 01:25:38 +00:00
|
|
|
* #### bw_env_gen_init()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_init(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 10:57:09 +00:00
|
|
|
* Initializes input parameter values in `coeffs`.
|
2022-11-29 16:05:46 +00:00
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_env_gen_set_sample_rate()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_set_sample_rate(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float sample_rate);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 10:57:09 +00:00
|
|
|
* Sets the `sample_rate` (Hz) value in `coeffs`.
|
|
|
|
*
|
|
|
|
* #### bw_env_gen_reset_coeffs()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_reset_coeffs(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs);
|
2022-12-03 10:57:09 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Resets coefficients in `coeffs` to assume their target values.
|
2022-11-29 16:05:46 +00:00
|
|
|
*
|
|
|
|
* #### bw_env_gen_reset_state()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline float bw_env_gen_reset_state(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT state,
|
|
|
|
char gate_0);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2023-09-20 06:26:17 +00:00
|
|
|
* Resets the given `state` to its initial values using the given `coeffs`
|
|
|
|
* and the initial gate value (`0` for off, non-`0` for on) `gate_0`.
|
|
|
|
*
|
|
|
|
* Returns the corresponding initial output value.
|
|
|
|
*
|
|
|
|
* #### bw_env_gen_reset_state_multi()
|
|
|
|
* ```>>> */
|
|
|
|
static inline void bw_env_gen_reset_state_multi(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT const * BW_RESTRICT state,
|
|
|
|
const char * BW_RESTRICT gate_0,
|
|
|
|
float * BW_RESTRICT 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 gate values (`0` for off,
|
|
|
|
* non-`0` for on) in the `gate_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`.
|
2022-12-03 10:57:09 +00:00
|
|
|
*
|
|
|
|
* #### bw_env_gen_update_coeffs_ctrl()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_update_coeffs_ctrl(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs);
|
2022-12-03 10:57:09 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Triggers control-rate update of coefficients in `coeffs`.
|
|
|
|
*
|
|
|
|
* #### bw_env_gen_update_coeffs_audio()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_update_coeffs_audio(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs);
|
2022-12-03 10:57:09 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Triggers audio-rate update of coefficients in `coeffs`.
|
|
|
|
*
|
2023-08-14 10:22:56 +00:00
|
|
|
* #### bw_env_gen_process_ctrl()
|
2022-12-03 10:57:09 +00:00
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_process_ctrl(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT state,
|
|
|
|
char gate);
|
2022-12-03 10:57:09 +00:00
|
|
|
/*! <<<```
|
2023-06-06 14:17:47 +00:00
|
|
|
* Triggers control-rate update of the internal `state` using `coeffs` and
|
|
|
|
* the given `gate` value (`0` for off, non-`0` for on).
|
2022-12-03 10:57:09 +00:00
|
|
|
*
|
|
|
|
* #### bw_env_gen_process1()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline float bw_env_gen_process1(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT state);
|
2022-12-03 10:57:09 +00:00
|
|
|
/*! <<<```
|
2022-12-04 08:30:16 +00:00
|
|
|
* Generates and returns one sample using `coeffs`, while using and updating
|
|
|
|
* `state` (audio rate only).
|
2022-12-03 10:57:09 +00:00
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_env_gen_process()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_process(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT state,
|
|
|
|
char gate,
|
|
|
|
float * BW_RESTRICT y,
|
|
|
|
size_t n_samples);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2023-06-06 14:17:47 +00:00
|
|
|
* Generates and fills the first `n_samples` of the output buffer `y` using
|
|
|
|
* the given `gate` value (`0` for off, non-`0` for on), while using and
|
|
|
|
* updating both `coeffs` and `state` (control and audio rate).
|
2022-12-03 10:57:09 +00:00
|
|
|
*
|
2024-01-02 10:20:52 +00:00
|
|
|
* `y` may be `BW_NULL`.
|
2022-12-03 10:57:09 +00:00
|
|
|
*
|
2023-06-07 13:41:20 +00:00
|
|
|
* #### bw_env_gen_process_multi()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_process_multi(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT const * BW_RESTRICT state,
|
|
|
|
const char * BW_RESTRICT gate,
|
|
|
|
float * BW_RESTRICT const * BW_RESTRICT y,
|
|
|
|
size_t n_channels,
|
|
|
|
size_t n_samples);
|
2023-06-07 13:41:20 +00:00
|
|
|
/*! <<<```
|
|
|
|
* Generates and fills the first `n_samples` of the `n_channels` output
|
|
|
|
* buffers `y` using the given `n_channels` `gate` values (`0` for off,
|
|
|
|
* non-`0` for on), while using and updating both the common `coeffs` and
|
|
|
|
* each of the `n_channels` `state`s (control and audio rate).
|
|
|
|
*
|
2024-01-02 10:20:52 +00:00
|
|
|
* `y` or any element of `y` may be `BW_NULL`.
|
2023-06-07 13:41:20 +00:00
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_env_gen_set_attack()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_set_attack(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float value);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 10:57:09 +00:00
|
|
|
* Sets the attack time to `value` (s) in `coeffs`.
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
2023-09-20 06:26:17 +00:00
|
|
|
* Valid range: [`0.f`, `60.f`].
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
|
|
|
* Default value: `0.f`.
|
2022-12-03 10:57:09 +00:00
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_env_gen_set_decay()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_set_decay(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float value);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 10:57:09 +00:00
|
|
|
* Sets the decay time to `value` (s) in `coeffs`.
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
2023-09-20 06:26:17 +00:00
|
|
|
* Valid range: [`0.f`, `60.f`].
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
|
|
|
* Default value: `0.f`.
|
2022-12-03 10:57:09 +00:00
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_env_gen_set_sustain()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_set_sustain(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float value);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 10:57:09 +00:00
|
|
|
* Sets the sustain level to `value` in `coeffs`.
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
2023-09-20 06:26:17 +00:00
|
|
|
* Valid range: [`0.f`, `1.f`].
|
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* Default value: `1.f`.
|
2022-12-03 10:57:09 +00:00
|
|
|
*
|
2022-11-15 23:49:51 +00:00
|
|
|
* #### bw_env_gen_set_release()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_set_release(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float value);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 10:57:09 +00:00
|
|
|
* Sets the release time to `value` (s) in `coeffs`.
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
2023-09-20 06:26:17 +00:00
|
|
|
* Valid range: [`0.f`, `60.f`].
|
2022-11-15 23:49:51 +00:00
|
|
|
*
|
|
|
|
* Default value: `0.f`.
|
2022-12-03 10:57:09 +00:00
|
|
|
*
|
2023-10-16 13:34:25 +00:00
|
|
|
* #### bw_env_gen_set_skip_sustain()
|
|
|
|
* ```>>> */
|
|
|
|
static inline void bw_env_gen_set_skip_sustain(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
char value);
|
|
|
|
/*! <<<```
|
|
|
|
* Sets whether the sustain phase should be skipped (non-`0`) or not (`0`).
|
|
|
|
*
|
|
|
|
* Default value: `0` (do not skip).
|
|
|
|
*
|
|
|
|
* #### bw_env_gen_set_always_reach_sustain()
|
|
|
|
* ```>>> */
|
|
|
|
static inline void bw_env_gen_set_always_reach_sustain(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
char value);
|
|
|
|
/*! <<<```
|
|
|
|
* Sets whether the sustain phase should be reached even if gate goes off
|
|
|
|
* before (non-`0`) or not (`0`).
|
|
|
|
*
|
|
|
|
* Default value: `0` (off).
|
|
|
|
*
|
2022-11-29 16:05:46 +00:00
|
|
|
* #### bw_env_gen_get_phase()
|
2022-11-15 23:49:51 +00:00
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline bw_env_gen_phase bw_env_gen_get_phase(
|
|
|
|
const bw_env_gen_state * BW_RESTRICT state);
|
2022-11-15 23:49:51 +00:00
|
|
|
/*! <<<```
|
2022-12-03 10:57:09 +00:00
|
|
|
* Returns the current envelope generator phase as stored in `state`.
|
|
|
|
*
|
|
|
|
* #### bw_env_gen_get_y_z1()
|
|
|
|
* ```>>> */
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline float bw_env_gen_get_y_z1(
|
|
|
|
const bw_env_gen_state * BW_RESTRICT state);
|
2022-12-03 10:57:09 +00:00
|
|
|
/*! <<<```
|
2022-12-03 12:11:08 +00:00
|
|
|
* Returns the last output sample as stored in `state`.
|
2023-09-20 06:26:17 +00:00
|
|
|
*
|
|
|
|
* #### bw_env_gen_coeffs_is_valid()
|
|
|
|
* ```>>> */
|
|
|
|
static inline char bw_env_gen_coeffs_is_valid(
|
|
|
|
const bw_env_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_env_gen_coeffs`.
|
|
|
|
*
|
|
|
|
* #### bw_env_gen_state_is_valid()
|
|
|
|
* ```>>> */
|
|
|
|
static inline char bw_env_gen_state_is_valid(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
const bw_env_gen_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-20 06:26:17 +00:00
|
|
|
*
|
|
|
|
* `state` must at least point to a readable memory block of size greater
|
|
|
|
* than or equal to that of `bw_env_gen_state`.
|
2022-11-15 23:49:51 +00:00
|
|
|
* }}} */
|
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-29 16:05:46 +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_one_pole.h>
|
|
|
|
|
2023-08-12 08:15:04 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
enum bw_env_gen_coeffs_state {
|
|
|
|
bw_env_gen_coeffs_state_invalid,
|
|
|
|
bw_env_gen_coeffs_state_init,
|
|
|
|
bw_env_gen_coeffs_state_set_sample_rate,
|
|
|
|
bw_env_gen_coeffs_state_reset_coeffs
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2023-08-12 17:46:14 +00:00
|
|
|
struct bw_env_gen_coeffs {
|
2023-09-20 06:26:17 +00:00
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
uint32_t hash;
|
|
|
|
enum bw_env_gen_coeffs_state state;
|
|
|
|
uint32_t reset_id;
|
|
|
|
#endif
|
|
|
|
|
2022-11-29 16:05:46 +00:00
|
|
|
// Sub-components
|
2023-09-20 06:26:17 +00:00
|
|
|
bw_one_pole_coeffs smooth_coeffs;
|
2022-11-29 16:05:46 +00:00
|
|
|
|
2022-11-19 01:25:38 +00:00
|
|
|
// Coefficients
|
2023-09-20 06:26:17 +00:00
|
|
|
float k_T;
|
2022-11-19 01:25:38 +00:00
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
uint32_t attack_inc;
|
|
|
|
uint32_t decay_dec;
|
|
|
|
uint32_t sustain_v;
|
|
|
|
uint32_t release_dec;
|
2022-11-19 01:25:38 +00:00
|
|
|
|
|
|
|
// Parameters
|
2023-09-20 06:26:17 +00:00
|
|
|
float attack;
|
|
|
|
float decay;
|
|
|
|
float sustain;
|
|
|
|
float release;
|
2023-10-16 13:34:25 +00:00
|
|
|
char skip_sustain;
|
|
|
|
char always_reach_sustain;
|
2023-09-20 06:26:17 +00:00
|
|
|
int param_changed;
|
2022-11-29 16:05:46 +00:00
|
|
|
};
|
2022-11-19 01:25:38 +00:00
|
|
|
|
2023-08-12 17:46:14 +00:00
|
|
|
struct bw_env_gen_state {
|
2023-09-20 06:26:17 +00:00
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
uint32_t hash;
|
|
|
|
uint32_t coeffs_reset_id;
|
|
|
|
#endif
|
|
|
|
|
2022-11-29 16:05:46 +00:00
|
|
|
bw_env_gen_phase phase;
|
2023-09-20 06:26:17 +00:00
|
|
|
uint32_t v;
|
2022-11-29 16:05:46 +00:00
|
|
|
bw_one_pole_state smooth_state;
|
2022-11-19 01:25:38 +00:00
|
|
|
};
|
|
|
|
|
2023-08-12 17:46:14 +00:00
|
|
|
#define BW_ENV_GEN_PARAM_ATTACK 1
|
|
|
|
#define BW_ENV_GEN_PARAM_DECAY (1<<1)
|
|
|
|
#define BW_ENV_GEN_PARAM_SUSTAIN (1<<2)
|
|
|
|
#define BW_ENV_GEN_PARAM_RELEASE (1<<3)
|
2022-11-29 16:05:46 +00:00
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
#define BW_ENV_V_MAX 4294967040
|
|
|
|
|
|
|
|
static inline void bw_env_gen_init(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
|
2022-11-29 16:05:46 +00:00
|
|
|
bw_one_pole_init(&coeffs->smooth_coeffs);
|
|
|
|
bw_one_pole_set_tau(&coeffs->smooth_coeffs, 0.05f);
|
|
|
|
coeffs->attack = 0.f;
|
|
|
|
coeffs->decay = 0.f;
|
|
|
|
coeffs->sustain = 1.f;
|
|
|
|
coeffs->release = 0.f;
|
2023-10-16 13:34:25 +00:00
|
|
|
coeffs->skip_sustain = 0;
|
|
|
|
coeffs->always_reach_sustain = 0;
|
2024-02-01 14:12:51 +00:00
|
|
|
|
2024-02-01 14:15:47 +00:00
|
|
|
coeffs->param_changed = ~0; // useless, just to make compilers happy about uninitialized variables
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
coeffs->hash = bw_hash_sdbm("bw_env_gen_coeffs");
|
|
|
|
coeffs->state = bw_env_gen_coeffs_state_init;
|
|
|
|
coeffs->reset_id = coeffs->hash + 1;
|
|
|
|
#endif
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state == bw_env_gen_coeffs_state_init);
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_set_sample_rate(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float sample_rate) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
|
|
|
BW_ASSERT(bw_is_finite(sample_rate) && sample_rate > 0.f);
|
|
|
|
|
2022-12-01 16:46:13 +00:00
|
|
|
bw_one_pole_set_sample_rate(&coeffs->smooth_coeffs, sample_rate);
|
|
|
|
bw_one_pole_reset_coeffs(&coeffs->smooth_coeffs);
|
2023-09-20 06:26:17 +00:00
|
|
|
coeffs->k_T = (float)BW_ENV_V_MAX / sample_rate;
|
2022-11-29 16:05:46 +00:00
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
coeffs->state = bw_env_gen_coeffs_state_set_sample_rate;
|
|
|
|
#endif
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state == bw_env_gen_coeffs_state_set_sample_rate);
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_do_update_coeffs_ctrl(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs) {
|
2022-11-29 16:05:46 +00:00
|
|
|
if (coeffs->param_changed) {
|
|
|
|
// 1 ns considered instantaneous
|
2023-08-12 17:46:14 +00:00
|
|
|
if (coeffs->param_changed & BW_ENV_GEN_PARAM_ATTACK)
|
2023-09-20 06:26:17 +00:00
|
|
|
coeffs->attack_inc = coeffs->attack > 1e-9f ? coeffs->k_T * bw_rcpf(coeffs->attack) : UINT32_MAX;
|
2023-08-12 17:46:14 +00:00
|
|
|
if (coeffs->param_changed & (BW_ENV_GEN_PARAM_DECAY | BW_ENV_GEN_PARAM_SUSTAIN))
|
2023-09-20 06:26:17 +00:00
|
|
|
coeffs->decay_dec = coeffs->decay > 1e-9f ? (1.f - coeffs->sustain) * (coeffs->k_T * bw_rcpf(coeffs->decay)) : UINT32_MAX;
|
|
|
|
if (coeffs->param_changed & BW_ENV_GEN_PARAM_SUSTAIN)
|
|
|
|
coeffs->sustain_v = (uint32_t)((float)BW_ENV_V_MAX * coeffs->sustain);
|
2023-08-12 17:46:14 +00:00
|
|
|
if (coeffs->param_changed & (BW_ENV_GEN_PARAM_SUSTAIN | BW_ENV_GEN_PARAM_RELEASE))
|
2023-09-20 06:26:17 +00:00
|
|
|
coeffs->release_dec = coeffs->release > 1e-9f ? coeffs->sustain * (coeffs->k_T * bw_rcpf(coeffs->release)) : UINT32_MAX;
|
|
|
|
coeffs->param_changed = 0;
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_reset_coeffs(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_set_sample_rate);
|
|
|
|
|
|
|
|
coeffs->param_changed = ~0;
|
|
|
|
bw_env_gen_do_update_coeffs_ctrl(coeffs);
|
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
coeffs->state = bw_env_gen_coeffs_state_reset_coeffs;
|
|
|
|
coeffs->reset_id++;
|
|
|
|
#endif
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state == bw_env_gen_coeffs_state_reset_coeffs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float bw_env_gen_reset_state(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT state,
|
2023-09-28 11:31:20 +00:00
|
|
|
char gate_0) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
bw_one_pole_reset_state(&coeffs->smooth_coeffs, &state->smooth_state, coeffs->sustain);
|
2023-09-28 11:31:20 +00:00
|
|
|
if (gate_0) {
|
|
|
|
state->phase = bw_env_gen_phase_sustain;
|
|
|
|
state->v = coeffs->sustain_v;
|
|
|
|
} else {
|
|
|
|
state->phase = bw_env_gen_phase_off;
|
|
|
|
state->v = 0;
|
|
|
|
}
|
2023-09-20 06:26:17 +00:00
|
|
|
const float y = (1.f / (float)BW_ENV_V_MAX) * state->v;
|
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
state->hash = bw_hash_sdbm("bw_env_gen_state");
|
|
|
|
state->coeffs_reset_id = coeffs->reset_id;
|
|
|
|
#endif
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_state_is_valid(coeffs, state));
|
|
|
|
BW_ASSERT(bw_is_finite(y));
|
|
|
|
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_env_gen_reset_state_multi(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT const * BW_RESTRICT state,
|
|
|
|
const char * BW_RESTRICT gate_0,
|
|
|
|
float * BW_RESTRICT y_0,
|
|
|
|
size_t n_channels) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_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(gate_0 != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
|
2024-01-02 10:20:52 +00:00
|
|
|
if (y_0 != BW_NULL)
|
2023-09-20 06:26:17 +00:00
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
|
|
|
y_0[i] = bw_env_gen_reset_state(coeffs, state[i], gate_0[i]);
|
|
|
|
else
|
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
|
|
|
bw_env_gen_reset_state(coeffs, state[i], gate_0[i]);
|
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_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-20 06:26:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_env_gen_update_coeffs_ctrl(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
|
|
|
|
|
|
|
bw_env_gen_do_update_coeffs_ctrl(coeffs);
|
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_env_gen_update_coeffs_audio(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
|
|
|
|
2023-04-02 19:54:17 +00:00
|
|
|
(void)coeffs;
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_process_ctrl(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT state,
|
|
|
|
char gate) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_state_is_valid(coeffs, state));
|
|
|
|
|
2023-06-06 14:17:47 +00:00
|
|
|
if (gate) {
|
2022-11-29 16:05:46 +00:00
|
|
|
if (state->phase == bw_env_gen_phase_off || state->phase == bw_env_gen_phase_release)
|
|
|
|
state->phase = bw_env_gen_phase_attack;
|
|
|
|
} else {
|
2023-10-16 13:34:25 +00:00
|
|
|
if (state->phase == bw_env_gen_phase_sustain || (state->phase != bw_env_gen_phase_off && !coeffs->always_reach_sustain))
|
2022-11-29 16:05:46 +00:00
|
|
|
state->phase = bw_env_gen_phase_release;
|
|
|
|
}
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_state_is_valid(coeffs, state));
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline float bw_env_gen_process1(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT state) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_state_is_valid(coeffs, state));
|
|
|
|
|
|
|
|
uint32_t v = 0;
|
2022-11-29 16:05:46 +00:00
|
|
|
switch (state->phase) {
|
|
|
|
case bw_env_gen_phase_attack:
|
2023-09-20 06:26:17 +00:00
|
|
|
v = state->v + coeffs->attack_inc;
|
|
|
|
if (v == BW_ENV_V_MAX || v <= state->v) {
|
|
|
|
v = BW_ENV_V_MAX;
|
2022-11-29 16:05:46 +00:00
|
|
|
state->phase = bw_env_gen_phase_decay;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case bw_env_gen_phase_decay:
|
2023-09-20 06:26:17 +00:00
|
|
|
v = state->v - coeffs->decay_dec;
|
|
|
|
if (v <= coeffs->sustain_v || v >= state->v) {
|
2022-11-29 16:05:46 +00:00
|
|
|
state->phase = bw_env_gen_phase_sustain;
|
|
|
|
bw_one_pole_reset_state(&coeffs->smooth_coeffs, &state->smooth_state, coeffs->sustain);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case bw_env_gen_phase_sustain:
|
2023-09-20 06:26:17 +00:00
|
|
|
v = (uint32_t)((float)BW_ENV_V_MAX * bw_one_pole_process1(&coeffs->smooth_coeffs, &state->smooth_state, coeffs->sustain));
|
2023-10-16 13:34:25 +00:00
|
|
|
if (coeffs->skip_sustain)
|
|
|
|
state->phase = bw_env_gen_phase_release;
|
2022-11-29 16:05:46 +00:00
|
|
|
break;
|
|
|
|
case bw_env_gen_phase_release:
|
2023-09-20 06:26:17 +00:00
|
|
|
v = state->v - coeffs->release_dec;
|
|
|
|
if (v == 0 || v >= state->v) {
|
|
|
|
v = 0;
|
2022-11-29 16:05:46 +00:00
|
|
|
state->phase = bw_env_gen_phase_off;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case bw_env_gen_phase_off:
|
2023-09-20 06:26:17 +00:00
|
|
|
v = 0;
|
2022-11-29 16:05:46 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-09-20 06:26:17 +00:00
|
|
|
state->v = v;
|
|
|
|
const float y = (1.f / (float)BW_ENV_V_MAX) * v;
|
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_state_is_valid(coeffs, state));
|
|
|
|
BW_ASSERT(bw_is_finite(y));
|
|
|
|
|
|
|
|
return y;
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_process(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT state,
|
|
|
|
char gate,
|
|
|
|
float * BW_RESTRICT y,
|
|
|
|
size_t n_samples) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_state_is_valid(coeffs, state));
|
|
|
|
|
2022-11-29 16:05:46 +00:00
|
|
|
bw_env_gen_update_coeffs_ctrl(coeffs);
|
2023-08-14 10:22:56 +00:00
|
|
|
bw_env_gen_process_ctrl(coeffs, state, gate);
|
2024-01-02 10:20:52 +00:00
|
|
|
if (y != BW_NULL)
|
2023-08-12 17:46:14 +00:00
|
|
|
for (size_t i = 0; i < n_samples; i++)
|
2022-11-30 10:55:25 +00:00
|
|
|
y[i] = bw_env_gen_process1(coeffs, state);
|
|
|
|
else
|
2023-08-12 17:46:14 +00:00
|
|
|
for (size_t i = 0; i < n_samples; i++)
|
2022-11-30 10:55:25 +00:00
|
|
|
bw_env_gen_process1(coeffs, state);
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_state_is_valid(coeffs, state));
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT_DEEP(y != BW_NULL ? bw_has_only_finite(y, n_samples) : 1);
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_process_multi(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
bw_env_gen_state * BW_RESTRICT const * BW_RESTRICT state,
|
|
|
|
const char * BW_RESTRICT gate,
|
|
|
|
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-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_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(gate != BW_NULL);
|
2023-09-25 14:47:17 +00:00
|
|
|
#ifndef BW_NO_DEBUG
|
2024-01-02 10:20:52 +00:00
|
|
|
if (y != BW_NULL)
|
2023-09-25 14:47:17 +00:00
|
|
|
for (size_t i = 0; i < n_channels; i++)
|
|
|
|
for (size_t j = i + 1; j < n_channels; j++)
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(y[i] == BW_NULL || y[j] == BW_NULL || y[i] != y[j]);
|
2023-09-25 14:47:17 +00:00
|
|
|
#endif
|
2023-09-20 06:26:17 +00:00
|
|
|
|
2023-06-07 13:41:20 +00:00
|
|
|
bw_env_gen_update_coeffs_ctrl(coeffs);
|
2023-08-12 17:46:14 +00:00
|
|
|
for (size_t j = 0; j < n_channels; j++)
|
2023-08-14 10:22:56 +00:00
|
|
|
bw_env_gen_process_ctrl(coeffs, state[j], gate[j]);
|
2024-01-02 10:20:52 +00:00
|
|
|
if (y != BW_NULL)
|
2023-08-12 17:46:14 +00:00
|
|
|
for (size_t i = 0; i < n_samples; i++)
|
|
|
|
for (size_t j = 0; j < n_channels; j++) {
|
2023-06-07 13:41:20 +00:00
|
|
|
const float v = bw_env_gen_process1(coeffs, state[j]);
|
2024-01-02 10:20:52 +00:00
|
|
|
if (y[j] != BW_NULL)
|
2023-06-07 13:41:20 +00:00
|
|
|
y[j][i] = v;
|
|
|
|
}
|
|
|
|
else
|
2023-08-12 17:46:14 +00:00
|
|
|
for (size_t i = 0; i < n_samples; i++)
|
|
|
|
for (size_t j = 0; j < n_channels; j++)
|
2023-06-07 13:41:20 +00:00
|
|
|
bw_env_gen_process1(coeffs, state[j]);
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_reset_coeffs);
|
2023-06-07 13:41:20 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_set_attack(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float value) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
|
|
|
BW_ASSERT(bw_is_finite(value));
|
|
|
|
BW_ASSERT(value >= 0.f && value <= 60.f);
|
|
|
|
|
2022-11-29 16:05:46 +00:00
|
|
|
if (coeffs->attack != value) {
|
|
|
|
coeffs->attack = value;
|
2023-08-12 17:46:14 +00:00
|
|
|
coeffs->param_changed |= BW_ENV_GEN_PARAM_ATTACK;
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_set_decay(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float value) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
|
|
|
BW_ASSERT(bw_is_finite(value));
|
|
|
|
BW_ASSERT(value >= 0.f && value <= 60.f);
|
|
|
|
|
2022-11-29 16:05:46 +00:00
|
|
|
if (coeffs->decay != value) {
|
|
|
|
coeffs->decay = value;
|
2023-08-12 17:46:14 +00:00
|
|
|
coeffs->param_changed |= BW_ENV_GEN_PARAM_DECAY;
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_set_sustain(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float value) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
|
|
|
BW_ASSERT(bw_is_finite(value));
|
|
|
|
BW_ASSERT(value >= 0.f && value <= 1.f);
|
|
|
|
|
2022-11-29 16:05:46 +00:00
|
|
|
if (coeffs->sustain != value) {
|
|
|
|
coeffs->sustain = value;
|
2023-08-12 17:46:14 +00:00
|
|
|
coeffs->param_changed |= BW_ENV_GEN_PARAM_SUSTAIN;
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline void bw_env_gen_set_release(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
float value) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
|
|
|
BW_ASSERT(bw_is_finite(value));
|
|
|
|
BW_ASSERT(value >= 0.f && value <= 60.f);
|
|
|
|
|
2022-11-29 16:05:46 +00:00
|
|
|
if (coeffs->release != value) {
|
|
|
|
coeffs->release = value;
|
2023-08-12 17:46:14 +00:00
|
|
|
coeffs->param_changed |= BW_ENV_GEN_PARAM_RELEASE;
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
2022-11-29 16:05:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 13:34:25 +00:00
|
|
|
static inline void bw_env_gen_set_skip_sustain(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
char value) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-10-16 13:34:25 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
|
|
|
|
|
|
|
coeffs->skip_sustain = value;
|
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void bw_env_gen_set_always_reach_sustain(
|
|
|
|
bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
char value) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-10-16 13:34:25 +00:00
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
|
|
|
|
|
|
|
coeffs->always_reach_sustain = value;
|
|
|
|
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_coeffs_is_valid(coeffs));
|
|
|
|
BW_ASSERT_DEEP(coeffs->state >= bw_env_gen_coeffs_state_init);
|
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline bw_env_gen_phase bw_env_gen_get_phase(
|
|
|
|
const bw_env_gen_state * BW_RESTRICT state) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_state_is_valid(BW_NULL, state));
|
2023-09-20 06:26:17 +00:00
|
|
|
|
2022-11-29 16:05:46 +00:00
|
|
|
return state->phase;
|
|
|
|
}
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
static inline float bw_env_gen_get_y_z1(
|
|
|
|
const bw_env_gen_state * BW_RESTRICT state) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
|
|
|
BW_ASSERT_DEEP(bw_env_gen_state_is_valid(BW_NULL, state));
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
const float y = (1.f / (float)BW_ENV_V_MAX) * state->v;
|
|
|
|
|
|
|
|
BW_ASSERT(bw_is_finite(y));
|
|
|
|
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char bw_env_gen_coeffs_is_valid(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(coeffs != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
if (coeffs->hash != bw_hash_sdbm("bw_env_gen_coeffs"))
|
|
|
|
return 0;
|
|
|
|
if (coeffs->state < bw_env_gen_coeffs_state_init || coeffs->state > bw_env_gen_coeffs_state_reset_coeffs)
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!bw_is_finite(coeffs->attack) || coeffs->attack < 0.f || coeffs->attack > 60.f)
|
|
|
|
return 0;
|
|
|
|
if (!bw_is_finite(coeffs->decay) || coeffs->decay < 0.f || coeffs->decay > 60.f)
|
|
|
|
return 0;
|
|
|
|
if (!bw_is_finite(coeffs->sustain) || coeffs->sustain < 0.f || coeffs->sustain > 1.f)
|
|
|
|
return 0;
|
|
|
|
if (!bw_is_finite(coeffs->release) || coeffs->release < 0.f || coeffs->release > 60.f)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
if (coeffs->state >= bw_env_gen_coeffs_state_set_sample_rate && coeffs->k_T <= 0.f)
|
|
|
|
return 0.f;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return bw_one_pole_coeffs_is_valid(&coeffs->smooth_coeffs);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline char bw_env_gen_state_is_valid(
|
|
|
|
const bw_env_gen_coeffs * BW_RESTRICT coeffs,
|
|
|
|
const bw_env_gen_state * BW_RESTRICT state) {
|
2024-01-02 10:20:52 +00:00
|
|
|
BW_ASSERT(state != BW_NULL);
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
#ifdef BW_DEBUG_DEEP
|
|
|
|
if (state->hash != bw_hash_sdbm("bw_env_gen_state"))
|
|
|
|
return 0;
|
|
|
|
|
2024-01-02 10:20:52 +00:00
|
|
|
if (coeffs != BW_NULL && coeffs->reset_id != state->coeffs_reset_id)
|
2023-09-20 06:26:17 +00:00
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return state->phase >= bw_env_gen_phase_off && state->phase <= bw_env_gen_phase_release
|
2024-01-02 10:20:52 +00:00
|
|
|
&& bw_one_pole_state_is_valid(coeffs ? &coeffs->smooth_coeffs : BW_NULL, &state->smooth_state);
|
2022-11-30 10:55:25 +00:00
|
|
|
}
|
|
|
|
|
2023-08-12 17:46:14 +00:00
|
|
|
#undef BW_ENV_GEN_PARAM_ATTACK
|
|
|
|
#undef BW_ENV_GEN_PARAM_DECAY
|
|
|
|
#undef BW_ENV_GEN_PARAM_SUSTAIN
|
|
|
|
#undef BW_ENV_GEN_PARAM_RELEASE
|
2022-11-29 16:05:46 +00:00
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
#undef BW_ENV_V_MAX
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
#ifdef __cplusplus
|
2023-08-12 17:46:14 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
|
|
|
# include <array>
|
|
|
|
#endif
|
2023-08-12 17:46:14 +00:00
|
|
|
|
|
|
|
namespace Brickworks {
|
|
|
|
|
|
|
|
/*** Public C++ API ***/
|
|
|
|
|
|
|
|
/*! api_cpp {{{
|
|
|
|
* ##### Brickworks::EnvGen
|
|
|
|
* ```>>> */
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
class EnvGen {
|
|
|
|
public:
|
|
|
|
EnvGen();
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
void setSampleRate(
|
|
|
|
float sampleRate);
|
|
|
|
|
|
|
|
void reset(
|
|
|
|
char gate0 = 0,
|
|
|
|
float * BW_RESTRICT y0 = nullptr);
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-09-20 06:26:17 +00:00
|
|
|
void reset(
|
|
|
|
char gate0,
|
|
|
|
std::array<float, N_CHANNELS> * BW_RESTRICT y0);
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
void reset(
|
|
|
|
const char * BW_RESTRICT gate0,
|
|
|
|
float * BW_RESTRICT y0 = nullptr);
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-09-20 06:26:17 +00:00
|
|
|
void reset(
|
|
|
|
std::array<char, N_CHANNELS> gate0,
|
|
|
|
std::array<float, N_CHANNELS> * BW_RESTRICT y0 = nullptr);
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-09-20 06:26:17 +00:00
|
|
|
|
2023-08-12 17:46:14 +00:00
|
|
|
void process(
|
2023-09-20 06:26:17 +00:00
|
|
|
const char * BW_RESTRICT gate,
|
|
|
|
float * BW_RESTRICT const * BW_RESTRICT y,
|
|
|
|
size_t nSamples);
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-08-12 17:46:14 +00:00
|
|
|
void process(
|
2023-09-20 06:26:17 +00:00
|
|
|
std::array<char, N_CHANNELS> gate,
|
|
|
|
std::array<float * BW_RESTRICT, N_CHANNELS> y,
|
|
|
|
size_t nSamples);
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
void setAttack(
|
|
|
|
float value);
|
|
|
|
|
|
|
|
void setDecay(
|
|
|
|
float value);
|
|
|
|
|
|
|
|
void setSustain(
|
|
|
|
float value);
|
|
|
|
|
|
|
|
void setRelease(
|
|
|
|
float value);
|
2023-10-16 13:34:25 +00:00
|
|
|
|
|
|
|
void setSkipSustain(
|
|
|
|
bool value);
|
|
|
|
|
|
|
|
void setAlwaysReachSustain(
|
|
|
|
bool value);
|
|
|
|
|
2023-09-20 06:26:17 +00:00
|
|
|
bw_env_gen_phase getPhase(
|
|
|
|
size_t channel);
|
|
|
|
|
|
|
|
float getYZ1(
|
|
|
|
size_t channel);
|
2023-08-12 17:46:14 +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-20 06:26:17 +00:00
|
|
|
bw_env_gen_coeffs coeffs;
|
|
|
|
bw_env_gen_state states[N_CHANNELS];
|
|
|
|
bw_env_gen_state * BW_RESTRICT statesP[N_CHANNELS];
|
2023-08-12 17:46:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline EnvGen<N_CHANNELS>::EnvGen() {
|
|
|
|
bw_env_gen_init(&coeffs);
|
|
|
|
for (size_t i = 0; i < N_CHANNELS; i++)
|
|
|
|
statesP[i] = states + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-20 06:26:17 +00:00
|
|
|
inline void EnvGen<N_CHANNELS>::setSampleRate(
|
|
|
|
float sampleRate) {
|
2023-08-12 17:46:14 +00:00
|
|
|
bw_env_gen_set_sample_rate(&coeffs, sampleRate);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-20 06:26:17 +00:00
|
|
|
inline void EnvGen<N_CHANNELS>::reset(
|
|
|
|
char gate0,
|
|
|
|
float * BW_RESTRICT y0) {
|
2023-08-12 17:46:14 +00:00
|
|
|
bw_env_gen_reset_coeffs(&coeffs);
|
2023-09-20 06:26:17 +00:00
|
|
|
if (y0 != nullptr)
|
|
|
|
for (size_t i = 0; i < N_CHANNELS; i++)
|
|
|
|
y0[i] = bw_env_gen_reset_state(&coeffs, states + i, gate0);
|
|
|
|
else
|
|
|
|
for (size_t i = 0; i < N_CHANNELS; i++)
|
|
|
|
bw_env_gen_reset_state(&coeffs, states + i, gate0);
|
|
|
|
}
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-09-20 06:26:17 +00:00
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void EnvGen<N_CHANNELS>::reset(
|
|
|
|
char gate0,
|
|
|
|
std::array<float, N_CHANNELS> * BW_RESTRICT y0) {
|
|
|
|
reset(gate0, y0 != nullptr ? y0->data() : nullptr);
|
|
|
|
}
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-09-20 06:26:17 +00:00
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void EnvGen<N_CHANNELS>::reset(
|
|
|
|
const char * BW_RESTRICT gate0,
|
|
|
|
float * BW_RESTRICT y0) {
|
|
|
|
bw_env_gen_reset_coeffs(&coeffs);
|
|
|
|
bw_env_gen_reset_state_multi(&coeffs, statesP, gate0, y0, N_CHANNELS);
|
|
|
|
}
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-09-20 06:26:17 +00:00
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void EnvGen<N_CHANNELS>::reset(
|
|
|
|
std::array<char, N_CHANNELS> gate0,
|
|
|
|
std::array<float, N_CHANNELS> * BW_RESTRICT y0) {
|
|
|
|
reset(gate0.data(), y0 != nullptr ? y0->data() : nullptr);
|
2023-08-12 17:46:14 +00:00
|
|
|
}
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-08-12 17:46:14 +00:00
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void EnvGen<N_CHANNELS>::process(
|
2023-09-20 06:26:17 +00:00
|
|
|
const char * BW_RESTRICT gate,
|
|
|
|
float * BW_RESTRICT const * BW_RESTRICT y,
|
|
|
|
size_t nSamples) {
|
2023-08-12 17:46:14 +00:00
|
|
|
bw_env_gen_process_multi(&coeffs, statesP, gate, y, N_CHANNELS, nSamples);
|
|
|
|
}
|
|
|
|
|
2024-02-02 16:42:12 +00:00
|
|
|
#ifndef BW_CXX_NO_ARRAY
|
2023-08-12 17:46:14 +00:00
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void EnvGen<N_CHANNELS>::process(
|
2023-09-20 06:26:17 +00:00
|
|
|
std::array<char, N_CHANNELS> gate,
|
|
|
|
std::array<float * BW_RESTRICT, N_CHANNELS> y,
|
|
|
|
size_t nSamples) {
|
2023-08-12 17:46:14 +00:00
|
|
|
process(gate.data(), y.data(), nSamples);
|
|
|
|
}
|
2024-02-02 16:42:12 +00:00
|
|
|
#endif
|
2023-08-12 17:46:14 +00:00
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-20 06:26:17 +00:00
|
|
|
inline void EnvGen<N_CHANNELS>::setAttack(
|
|
|
|
float value) {
|
2023-08-12 17:46:14 +00:00
|
|
|
bw_env_gen_set_attack(&coeffs, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-20 06:26:17 +00:00
|
|
|
inline void EnvGen<N_CHANNELS>::setDecay(
|
|
|
|
float value) {
|
2023-08-12 17:46:14 +00:00
|
|
|
bw_env_gen_set_decay(&coeffs, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-20 06:26:17 +00:00
|
|
|
inline void EnvGen<N_CHANNELS>::setSustain(
|
|
|
|
float value) {
|
2023-08-12 17:46:14 +00:00
|
|
|
bw_env_gen_set_sustain(&coeffs, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-20 06:26:17 +00:00
|
|
|
inline void EnvGen<N_CHANNELS>::setRelease(
|
|
|
|
float value) {
|
2023-08-12 17:46:14 +00:00
|
|
|
bw_env_gen_set_release(&coeffs, value);
|
|
|
|
}
|
|
|
|
|
2023-10-16 13:34:25 +00:00
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void EnvGen<N_CHANNELS>::setSkipSustain(
|
|
|
|
bool value) {
|
|
|
|
bw_env_gen_set_skip_sustain(&coeffs, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
|
|
|
inline void EnvGen<N_CHANNELS>::setAlwaysReachSustain(
|
|
|
|
bool value) {
|
|
|
|
bw_env_gen_set_always_reach_sustain(&coeffs, value);
|
|
|
|
}
|
|
|
|
|
2023-08-12 17:46:14 +00:00
|
|
|
template<size_t N_CHANNELS>
|
2023-09-20 06:26:17 +00:00
|
|
|
inline bw_env_gen_phase EnvGen<N_CHANNELS>::getPhase(
|
|
|
|
size_t channel) {
|
2023-08-12 17:46:14 +00:00
|
|
|
return bw_env_gen_get_phase(states + channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N_CHANNELS>
|
2023-09-20 06:26:17 +00:00
|
|
|
inline float EnvGen<N_CHANNELS>::getYZ1(
|
|
|
|
size_t channel) {
|
2023-08-12 17:46:14 +00:00
|
|
|
return bw_env_gen_get_y_z1(states + channel);
|
|
|
|
}
|
|
|
|
|
2022-11-15 23:49:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|