Vital
Loading...
Searching...
No Matches
wavetable_component.h
Go to the documentation of this file.
1/*
2Summary:
3WavetableComponent is the base class for elements that produce or modify wavetables. It manages keyframes representing waveform states at certain positions, and supports interpolation between these states using none, linear, or cubic methods. By serializing and deserializing keyframes, it integrates smoothly with preset systems. Interpolation ensures smooth transitions across the wavetable dimension, enabling dynamic and evolving sounds.
4 */
5
6#pragma once
7
8#include "JuceHeader.h"
11#include "json/json.h"
12
13using json = nlohmann::json;
14
15namespace vital {
16 class WaveFrame;
17}
18
33public:
44
49 virtual ~WavetableComponent() { }
50
59 virtual WavetableKeyframe* createKeyframe(int position) = 0;
60
69 virtual void render(vital::WaveFrame* wave_frame, float position) = 0;
70
79
85 virtual json stateToJson();
86
94 virtual void jsonToState(json data);
95
101 virtual void prerender() { }
102
108 virtual bool hasKeyframes() { return true; }
109
113 void reset();
114
124 void interpolate(WavetableKeyframe* dest, float position);
125
133
141 void reposition(WavetableKeyframe* keyframe);
142
148 void remove(WavetableKeyframe* keyframe);
149
155 inline int numFrames() const { return static_cast<int>(keyframes_.size()); }
156
163 inline int indexOf(WavetableKeyframe* keyframe) const {
164 for (int i = 0; i < keyframes_.size(); ++i) {
165 if (keyframes_[i].get() == keyframe)
166 return i;
167 }
168 return -1;
169 }
170
177 inline WavetableKeyframe* getFrameAt(int index) const { return keyframes_[index].get(); }
178
185 int getIndexFromPosition(int position) const;
186
194
203
210
217
218protected:
219 std::vector<std::unique_ptr<WavetableKeyframe>> keyframes_;
221
222 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(WavetableComponent)
223};
ComponentType
Enumerates all known WavetableComponents, including sources and modifiers.
Definition wavetable_component_factory.h:28
A base class representing a component in a wavetable synthesis chain.
Definition wavetable_component.h:32
int numFrames() const
Gets the number of keyframes.
Definition wavetable_component.h:155
virtual WavetableKeyframe * createKeyframe(int position)=0
Creates a new keyframe at a given position.
WavetableKeyframe * getFrameAt(int index) const
Gets a keyframe by index.
Definition wavetable_component.h:177
InterpolationStyle
Defines how interpolation is performed between keyframes.
Definition wavetable_component.h:38
@ kLinear
Linear interpolation between adjacent keyframes.
Definition wavetable_component.h:40
@ kNone
No interpolation, just jumps between keyframes.
Definition wavetable_component.h:39
@ kNumInterpolationStyles
Definition wavetable_component.h:42
@ kCubic
Cubic interpolation for smoother transitions.
Definition wavetable_component.h:41
virtual json stateToJson()
Serializes the component’s state and all keyframes to a JSON object.
Definition wavetable_component.cpp:49
void interpolate(WavetableKeyframe *dest, float position)
Interpolates a destination keyframe at a given position.
Definition wavetable_component.cpp:68
void reset()
Clears all keyframes and inserts a default one at position 0.
Definition wavetable_component.cpp:62
WavetableKeyframe * insertNewKeyframe(int position)
Inserts a new keyframe at the given position, creating and sorting it into the array.
Definition wavetable_component.cpp:8
virtual void jsonToState(json data)
Restores the component’s state from a JSON object.
Definition wavetable_component.cpp:37
virtual void prerender()
Called before rendering to perform any needed precomputation.
Definition wavetable_component.h:101
int getLastKeyframePosition()
Gets the highest position among all keyframes.
Definition wavetable_component.cpp:127
int indexOf(WavetableKeyframe *keyframe) const
Finds the index of a given keyframe.
Definition wavetable_component.h:163
void setInterpolationStyle(InterpolationStyle type)
Sets the global interpolation style.
Definition wavetable_component.h:209
std::vector< std::unique_ptr< WavetableKeyframe > > keyframes_
The list of keyframes sorted by position.
Definition wavetable_component.h:219
WavetableKeyframe * getFrameAtPosition(int position)
Gets a keyframe by position if one matches exactly, or nullptr otherwise.
Definition wavetable_component.cpp:119
InterpolationStyle getInterpolationStyle() const
Gets the current global interpolation style.
Definition wavetable_component.h:216
virtual void render(vital::WaveFrame *wave_frame, float position)=0
Renders the waveform at a given position into a WaveFrame.
WavetableComponent()
Constructs a WavetableComponent with a default linear interpolation style.
Definition wavetable_component.h:48
virtual WavetableComponentFactory::ComponentType getType()=0
Returns the type of this WavetableComponent.
int getIndexFromPosition(int position) const
Finds the insertion index for a given position to keep keyframes sorted.
Definition wavetable_component.cpp:107
void remove(WavetableKeyframe *keyframe)
Removes a specific keyframe from the component.
Definition wavetable_component.cpp:31
virtual bool hasKeyframes()
Indicates whether this component relies on multiple keyframes.
Definition wavetable_component.h:108
virtual ~WavetableComponent()
Definition wavetable_component.h:49
void reposition(WavetableKeyframe *keyframe)
Repositions a keyframe in the keyframe list after its position changed.
Definition wavetable_component.cpp:21
InterpolationStyle interpolation_style_
Current interpolation style (none, linear, cubic).
Definition wavetable_component.h:220
Represents a single state of a waveform at a specific position in a wavetable.
Definition wavetable_keyframe.h:35
Represents a single frame of a wavetable, containing both time-domain and frequency-domain data.
Definition wave_frame.h:16
nlohmann::json json
Definition line_generator.h:7
Contains classes and functions used within the Vital synthesizer framework.