Vital
Loading...
Searching...
No Matches
wavetable_keyframe.cpp
Go to the documentation of this file.
1/*
2Summary:
3WavetableKeyframe defines a single point in a wavetable where the waveform configuration is known. By interpolating between keyframes, a component can produce evolving waveforms. This class provides generic interpolation (linear and cubic) and serialization methods, while subclasses specify how waveform data is stored and rendered.
4 */
5
7
8#include "utils.h"
10
11float WavetableKeyframe::linearTween(float point_from, float point_to, float t) {
12 // Uses a simple linear interpolation from utils.
13 return vital::utils::interpolate(point_from, point_to, t);
14}
15
16float WavetableKeyframe::cubicTween(float point_prev, float point_from, float point_to, float point_next,
17 float range_prev, float range, float range_next, float t) {
18 // Computes a cubic interpolation between four points for smoother transitions than linear.
19 float slope_from = 0.0f;
20 float slope_to = 0.0f;
21 if (range_prev > 0.0f)
22 slope_from = (point_to - point_prev) / (1.0f + range_prev / range);
23 if (range_next > 0.0f)
24 slope_to = (point_next - point_from) / (1.0f + range_next / range);
25 float delta = point_to - point_from;
26
27 float movement = linearTween(point_from, point_to, t);
28 float smooth = t * (1.0f - t) * ((1.0f - t) * (slope_from - delta) + t * (delta - slope_to));
29 return movement + smooth;
30}
31
33 // Retrieves the index of this keyframe by asking the owner component.
34 return owner()->indexOf(this);
35}
36
38 return { { "position", position_ } };
39}
40
42 // Restore the position from the JSON data.
43 position_ = data["position"];
44}
int indexOf(WavetableKeyframe *keyframe) const
Finds the index of a given keyframe.
Definition wavetable_component.h:163
static float linearTween(float point_from, float point_to, float t)
Performs linear interpolation between two points.
Definition wavetable_keyframe.cpp:11
static float cubicTween(float point_prev, float point_from, float point_to, float point_next, float range_prev, float range, float range_next, float t)
Performs cubic interpolation taking into account a previous and next point for smoother curves.
Definition wavetable_keyframe.cpp:16
int index()
Gets the index of this keyframe within its owner component.
Definition wavetable_keyframe.cpp:32
WavetableComponent * owner()
Gets the WavetableComponent that owns this keyframe.
Definition wavetable_keyframe.h:152
int position_
The position of this keyframe along the wavetable dimension.
Definition wavetable_keyframe.h:162
virtual json stateToJson()
Serializes the state of this keyframe to a JSON object.
Definition wavetable_keyframe.cpp:37
virtual void jsonToState(json data)
Restores the keyframe's state from a JSON object.
Definition wavetable_keyframe.cpp:41
nlohmann::json json
Definition line_generator.h:7
force_inline poly_float interpolate(poly_float from, poly_float to, mono_float t)
Performs a linear interpolation between two poly_floats using a scalar t in [0..1].
Definition poly_utils.h:182
Provides various utility functions, classes, and constants for audio, math, and general-purpose opera...