Sound Byte Libs 29c5ff3
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
interpolate.hpp
Go to the documentation of this file.
1// sbl/dsp/interpolate.hpp — Interpolation and blending utilities
2//
3// Named functions for common DSP operations that are trivial to write
4// inline but benefit from standardized, readable names.
5//
6// Usage:
7// float mix = sbl::dsp::crossfade(dry, wet, 0.5f); // 50/50 blend
8// float smooth = sbl::dsp::smooth_step(t); // S-curve [0,1]
9
10#pragma once
11
12#include <cstdint>
13#include <cmath>
14
15namespace sbl::dsp {
16
17/**
18 * @brief Linear crossfade between two values
19 *
20 * @param a Value at t=0
21 * @param b Value at t=1
22 * @param t Blend factor [0.0, 1.0]
23 * @return a + (b - a) * t
24 */
25inline constexpr float crossfade(float a, float b, float t) {
26 return a + (b - a) * t;
27}
28
29/**
30 * @brief Hermite smooth step (3rd-order S-curve)
31 *
32 * Maps [0, 1] → [0, 1] with zero derivative at endpoints.
33 * Useful for equal-power-like crossfade curves, smooth transitions,
34 * and perceptually linear parameter morphing.
35 *
36 * @param t Input [0.0, 1.0] (not clamped)
37 * @return t² × (3 - 2t)
38 */
39inline constexpr float smooth_step(float t) {
40 return t * t * (3.0f - 2.0f * t);
41}
42
43/**
44 * @brief Integer crossfade for 16-bit values
45 *
46 * @param a Value at t=0
47 * @param b Value at t=1
48 * @param t Blend factor [0, 65535] (0 = all a, 65535 = all b)
49 * @return Blended value
50 */
51inline constexpr int32_t crossfade_i16(int16_t a, int16_t b, uint16_t t) {
52 return a + ((static_cast<int32_t>(b) - a) * t) / 65535;
53}
54
55// ─── Equal-power crossfade ───────────────────────────────────────────
56
57/**
58 * @brief Equal-power crossfade between two values
59 *
60 * Maintains constant power across the fade (no dip at t=0.5).
61 * Uses sqrt approximation: a*sqrt(1-t) + b*sqrt(t).
62 *
63 * @param a Value at t=0 (dry)
64 * @param b Value at t=1 (wet)
65 * @param t Blend factor [0.0, 1.0]
66 */
67inline float crossfade_equal_power(float a, float b, float t) {
68 return a * sqrtf(1.0f - t) + b * sqrtf(t);
69}
70
71// ─── Block crossfade (constant mix) ─────────────────────────────────
72
73/**
74 * @brief Linear crossfade over a block with constant mix
75 *
76 * out[i] = a[i] * (1-mix) + b[i] * mix. In-place safe (out can alias a or b).
77 */
78inline void crossfade_block(const float* a, const float* b,
79 float* out, float mix, uint16_t frames) {
80 const float ga = 1.0f - mix;
81 for (uint16_t i = 0; i < frames; ++i) {
82 out[i] = a[i] * ga + b[i] * mix;
83 }
84}
85
86/**
87 * @brief Equal-power crossfade over a block with constant mix
88 *
89 * out[i] = a[i] * sqrt(1-mix) + b[i] * sqrt(mix). Gains computed once.
90 */
91inline void crossfade_block_equal_power(const float* a, const float* b,
92 float* out, float mix, uint16_t frames) {
93 const float ga = sqrtf(1.0f - mix);
94 const float gb = sqrtf(mix);
95 for (uint16_t i = 0; i < frames; ++i) {
96 out[i] = a[i] * ga + b[i] * gb;
97 }
98}
99
100} // namespace sbl::dsp
DSP atoms for audio signal processing.
Definition allpass.hpp:22
constexpr float crossfade(float a, float b, float t)
Linear crossfade between two values.
void crossfade_block(const float *a, const float *b, float *out, float mix, uint16_t frames)
Linear crossfade over a block with constant mix.
void crossfade_block_equal_power(const float *a, const float *b, float *out, float mix, uint16_t frames)
Equal-power crossfade over a block with constant mix.
constexpr int32_t crossfade_i16(int16_t a, int16_t b, uint16_t t)
Integer crossfade for 16-bit values.
float crossfade_equal_power(float a, float b, float t)
Equal-power crossfade between two values.
constexpr float smooth_step(float t)
Hermite smooth step (3rd-order S-curve)