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
crossfade.hpp
Go to the documentation of this file.
1/**
2 * @file crossfade.hpp
3 * @brief Audio crossfades: equal-power per value, and constant-mix over a buffer
4 *
5 * The linear blend and smooth step are domain-neutral and live in
6 * <sbl/math/interpolate.hpp>.
7 */
8
9#ifndef SBL_DSP_MATH_CROSSFADE_HPP_
10#define SBL_DSP_MATH_CROSSFADE_HPP_
11
12#include <cmath>
13#include <cstdint>
14
15#include <sbl/math/clamp.hpp>
17
18namespace sbl::dsp::math {
19
20// Equal-power crossfade
21
22/**
23 * @brief Equal-power crossfade between two values
24 *
25 * Maintains constant power across the fade (no dip at t=0.5).
26 * Returns a*sqrt(1-t) + b*sqrt(t), with sqrtf.
27 *
28 * @param a Value at t=0 (dry)
29 * @param b Value at t=1 (wet)
30 * @param t Blend factor in [0, 1] (not clamped)
31 * @return a*sqrt(1-t) + b*sqrt(t)
32 */
33inline float crossfade_equal_power(float a, float b, float t) { return a * sqrtf(1.0f - t) + b * sqrtf(t); }
34
35/**
36 * @brief Equal-power gains for a crossfade with a zone at each end
37 *
38 * The gains are the cosine and sine of a quarter turn, so their squares sum
39 * to 1. Within `end_zone` of either end of t's travel the nearer side's gain
40 * is exactly 1 and the other's exactly 0, so a control resting near an end,
41 * or a CV with a small offset, leaves none of the other side in.
42 *
43 * @param t Blend factor in [0, 1]; the scaled position is clamped, and a NaN gives the t=0 side
44 * @param end_zone The share of t's travel at each end held at one side, in [0, 0.5)
45 * @param[out] gain_a Gain of the side at t=0, in [0, 1]
46 * @param[out] gain_b Gain of the side at t=1, in [0, 1]
47 */
48inline void crossfade_gains_equal_power(float t, float end_zone, float& gain_a, float& gain_b) {
49 const float x = sbl::math::clamp((t - end_zone) / (1.0f - 2.0f * end_zone), 0.0f, 1.0f);
50 const float angle = 0.5f * sbl::math::PI * x;
51 gain_a = x < 1.0f ? std::cos(angle) : 0.0f;
52 gain_b = x > 0.0f ? std::sin(angle) : 0.0f;
53}
54
55// Buffer crossfade (constant mix)
56
57/**
58 * @brief Linear crossfade over a buffer with constant mix
59 *
60 * out[i] = a[i] * (1-mix) + b[i] * mix. In-place safe (out can alias a or b).
61 *
62 * @param a Buffer at mix=0, `frames` long
63 * @param b Buffer at mix=1, `frames` long
64 * @param[out] out Output buffer, `frames` long
65 * @param mix Blend factor in [0, 1] (not clamped)
66 * @param frames Frame count
67 * @context Audio thread; real-time safe.
68 */
69inline void crossfade(const float* a, const float* b, float* out, float mix, uint16_t frames) {
70 const float ga = 1.0f - mix;
71 for (uint16_t i = 0; i < frames; ++i) {
72 out[i] = a[i] * ga + b[i] * mix;
73 }
74}
75
76/**
77 * @brief Equal-power crossfade over a buffer with constant mix
78 *
79 * out[i] = a[i] * sqrt(1-mix) + b[i] * sqrt(mix). Gains computed once.
80 * In-place safe (out can alias a or b).
81 *
82 * @param a Buffer at mix=0, `frames` long
83 * @param b Buffer at mix=1, `frames` long
84 * @param[out] out Output buffer, `frames` long
85 * @param mix Blend factor in [0, 1] (not clamped)
86 * @param frames Frame count
87 * @context Audio thread; real-time safe.
88 */
89inline void crossfade_equal_power(const float* a, const float* b, float* out, float mix, uint16_t frames) {
90 const float ga = sqrtf(1.0f - mix);
91 const float gb = sqrtf(mix);
92 for (uint16_t i = 0; i < frames; ++i) {
93 out[i] = a[i] * ga + b[i] * gb;
94 }
95}
96
97} // namespace sbl::dsp::math
98
99#endif // SBL_DSP_MATH_CROSSFADE_HPP_
Clamping.
The numbers every layer reaches for.
Math for the audio stack.
Definition crossfade.hpp:18
float crossfade_equal_power(float a, float b, float t)
Equal-power crossfade between two values.
Definition crossfade.hpp:33
void crossfade_gains_equal_power(float t, float end_zone, float &gain_a, float &gain_b)
Equal-power gains for a crossfade with a zone at each end.
Definition crossfade.hpp:48
void crossfade(const float *a, const float *b, float *out, float mix, uint16_t frames)
Linear crossfade over a buffer with constant mix.
Definition crossfade.hpp:69
constexpr T clamp(T x, T lo, T hi)
x held to [lo, hi]; a NaN goes to lo.
Definition clamp.hpp:24
constexpr float PI
Definition constants.hpp:17