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
interpolate.hpp
Go to the documentation of this file.
1/**
2 * @file interpolate.hpp
3 * @brief Interpolation: linear blend, smooth step, integer blend
4 * @ingroup math
5 *
6 * @code
7 * float mix = sbl::math::crossfade(a, b, 0.5f); // halfway from a to b
8 * float s = sbl::math::smooth_step(t); // S-curve on [0, 1]
9 * @endcode
10 */
11
12#ifndef SBL_MATH_INTERPOLATE_HPP_
13#define SBL_MATH_INTERPOLATE_HPP_
14
15#include <cstdint>
17
18namespace sbl::math {
19
20/**
21 * @brief Linear crossfade between two values
22 *
23 * @param a Value at t=0
24 * @param b Value at t=1
25 * @param t Blend factor in [0, 1] (not clamped)
26 * @return a + (b - a) * t
27 */
28inline constexpr float crossfade(float a, float b, float t) { return a + (b - a) * t; }
29
30/**
31 * @brief Hermite smooth step (3rd-order S-curve)
32 *
33 * Maps [0, 1] to [0, 1] with zero derivative at the endpoints.
34 *
35 * @param t Input in [0, 1] (not clamped)
36 * @return t^2 * (3 - 2t)
37 */
38inline constexpr float smooth_step(float t) { return t * t * (3.0f - 2.0f * t); }
39
40/**
41 * @brief Integer crossfade for 16-bit values
42 *
43 * @param a Value at t=0
44 * @param b Value at t=1
45 * @param t Blend factor [0, 65535] (0 = all a, 65535 = all b)
46 * @return a + (b - a) * t / 65535, truncated toward zero
47 */
48inline constexpr int32_t crossfade_i16(int16_t a, int16_t b, uint16_t t) {
49 return a + ((static_cast<int32_t>(b) - a) * t) / U16_MAX;
50}
51
52} // namespace sbl::math
53
54#endif // SBL_MATH_INTERPOLATE_HPP_
The numbers every layer reaches for.
Domain-neutral math: constants, clamp, interpolation.
Definition clamp.hpp:13
constexpr float crossfade(float a, float b, float t)
Linear crossfade between two values.
constexpr float smooth_step(float t)
Hermite smooth step (3rd-order S-curve)
constexpr int32_t crossfade_i16(int16_t a, int16_t b, uint16_t t)
Integer crossfade for 16-bit values.
constexpr uint16_t U16_MAX
The full scale of a 16-bit unsigned blend factor.
Definition constants.hpp:26