Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
dry_wet.hpp
Go to the documentation of this file.
1/**
2 * @file dry_wet.hpp
3 * @brief A dry/wet mix of two signals, the mix a port
4 */
5
6#ifndef SBL_DSP_DYNAMICS_DRY_WET_HPP_
7#define SBL_DSP_DYNAMICS_DRY_WET_HPP_
8
9#include <cstdint>
10
13#include <sbl/math/clamp.hpp>
14
15namespace sbl::dsp {
16
17/**
18 * @brief A dry/wet mix of two signals, the mix a port
19 *
20 * out = dry + mix (wet - dry): the dry signal at 0, the wet at 1, a linear
21 * blend between. With a filter's or an effect's output as the wet signal it
22 * is the stage's depth. On the transport the wet signal is the input, and
23 * the dry signal and the mix are ports:
24 *
25 * - dry, a block: the signal before the stage.
26 * - mix, in [0, 1], a block or a scalar. A scalar moves to its value over
27 * RAMP_FRAMES, the first one at once; left off, it holds. A value that is
28 * not a number counts as 0.
29 *
30 * At a mix of exactly 0 the output is the dry signal, bit for bit, and at 1
31 * the wet. Memoryless apart from the mix's ramp. In place is fine: out may
32 * be the wet buffer or the dry one.
33 *
34 * @code
35 * allpass.process(dry, wet, frames, cutoff, q);
36 * depth.process(wet, out, frames, dry, mass);
37 * @endcode
38 */
39class DryWet {
40public:
41 static constexpr uint16_t RAMP_FRAMES = 64; /**< a scalar mix arrives after this many frames */
42
43 /** @brief A mix of 1, the wet signal, until a mix is given; a scalar mix ramps over RAMP_FRAMES. */
45
46 /**
47 * @brief One frame: dry at a mix of 0, wet at 1. The mix is used as given.
48 *
49 * @param dry The dry sample
50 * @param wet The wet sample
51 * @param mix The mix; exactly 0 returns dry and exactly 1 returns wet, bit for bit
52 * @return dry + mix (wet - dry)
53 */
54 static float mix(float dry, float wet, float mix) {
55 if (mix == 0.0f) {
56 return dry;
57 }
58 if (mix == 1.0f) {
59 return wet;
60 }
61 return dry + mix * (wet - dry);
62 }
63
64 /** @brief The ports of the buffer form, in the order it takes them. */
65 static constexpr transport::Port PORTS[] = {
67 {"mix"},
68 };
69
70 /**
71 * @brief The mix over a buffer: the wet signal is the input, dry and mix are ports
72 *
73 * In place is fine: out may be the wet buffer or the dry one.
74 *
75 * @tparam Ports The dry block, then optionally the mix as a block or an arithmetic scalar
76 * @param wet The wet buffer, `frames` long
77 * @param[out] out Output buffer, `frames` long
78 * @param frames Frame count
79 * @param ports dry, a block `frames` long; then mix in [0, 1], clamped, a NaN counting as 0, or left off to hold
80 * @context Audio thread; real-time safe.
81 */
82 template<class... Ports>
83 void process(const float* wet, float* out, uint16_t frames, Ports... ports) {
84 static_assert(sizeof...(Ports) >= 1 && transport::fits_v<DryWet, Ports...>,
85 "SBL ERROR: DryWet takes dry (a block), then mix (a block or a scalar)");
86 const auto dry = transport::arg<0>(ports...);
87 const auto amount = transport::arg<1>(ports...);
88 if constexpr (transport::is_scalar_v<decltype(amount)>) {
89 mix_.head(sbl::math::clamp01(static_cast<float>(amount)));
90 }
91 float m = 0.0f;
92 for (uint16_t i = 0; i < frames; ++i) {
93 if constexpr (transport::is_block_v<decltype(amount)>) {
94 m = sbl::math::clamp01(amount[i]);
95 } else {
96 m = mix_.step();
97 }
98 out[i] = mix(dry[i], wet[i], m);
99 }
100 if constexpr (transport::is_block_v<decltype(amount)>) {
101 if (frames > 0) {
102 mix_.jump(m);
103 }
104 }
105 }
106
107 /** @brief The mix at its target. */
108 void reset() { mix_.jump(mix_.target()); }
109
110private:
111 Ramp mix_{1.0f};
112};
113
114} // namespace sbl::dsp
115
116#endif // SBL_DSP_DYNAMICS_DRY_WET_HPP_
Clamping.
A dry/wet mix of two signals, the mix a port.
Definition dry_wet.hpp:39
static float mix(float dry, float wet, float mix)
One frame: dry at a mix of 0, wet at 1. The mix is used as given.
Definition dry_wet.hpp:54
static constexpr uint16_t RAMP_FRAMES
Definition dry_wet.hpp:41
DryWet()
A mix of 1, the wet signal, until a mix is given; a scalar mix ramps over RAMP_FRAMES.
Definition dry_wet.hpp:44
void process(const float *wet, float *out, uint16_t frames, Ports... ports)
The mix over a buffer: the wet signal is the input, dry and mix are ports.
Definition dry_wet.hpp:83
void reset()
The mix at its target.
Definition dry_wet.hpp:108
static constexpr transport::Port PORTS[]
The ports of the buffer form, in the order it takes them.
Definition dry_wet.hpp:65
A linear ramp to a target over a fixed number of frames.
Definition ramp.hpp:25
float step()
One frame.
Definition ramp.hpp:90
float target() const
The value the ramp heads for.
Definition ramp.hpp:103
void head(float target)
set(), except that the first value given since construction, by head() or jump(), is arrived at at on...
Definition ramp.hpp:64
void jump(float value)
Arrive at a value at once.
Definition ramp.hpp:77
void set_length(uint16_t frames)
Set how many frames a ramp takes.
Definition ramp.hpp:39
The audio stack.
Definition dry_wet.hpp:15
constexpr T clamp01(T x)
x held to [0, 1]; a NaN goes to 0.
Definition clamp.hpp:35
A linear ramp to a target over a fixed number of frames.
The processing call's shape as a checked contract.