Vital
Loading...
Searching...
No Matches
file_source.h
Go to the documentation of this file.
1/*
2Summary:
3FileSource is a wavetable component that constructs wavetables from an external audio file buffer. It supports various interpolation/fade styles for shaping the final waveform and different phase manipulation strategies (e.g., randomizing phases for a vocoder effect). FileSourceKeyframe encapsulates one particular configuration of start position, window fade, and style settings. Together, they enable flexible and creative wavetable generation from raw audio samples.
4 */
5
6#pragma once
7
8#include "JuceHeader.h"
9#include "pitch_detector.h"
10#include "wavetable_component.h"
11#include "wave_frame.h"
12#include "wave_source.h"
13#include "utils.h"
14
24 public:
26 static constexpr float kMaxFileSourceSamples = 176400;
28 static constexpr int kExtraSaveSamples = 4;
30 static constexpr int kExtraBufferSamples = 4;
32 static constexpr int kPitchDetectMaxPeriod = 8096;
33
45
56
60 struct SampleBuffer {
62 std::unique_ptr<float[]> data;
63 int size;
65 };
66
75 public:
81 FileSourceKeyframe(SampleBuffer* sample_buffer);
82 virtual ~FileSourceKeyframe() { }
83
84 void copy(const WavetableKeyframe* keyframe) override;
85 void interpolate(const WavetableKeyframe* from_keyframe,
86 const WavetableKeyframe* to_keyframe, float t) override;
87
95
96 void render(vital::WaveFrame* wave_frame) override;
97 void renderWaveBlend(vital::WaveFrame* wave_frame);
98 void renderNoInterpolate(vital::WaveFrame* wave_frame);
100 void renderFreqInterpolate(vital::WaveFrame* wave_frame);
101 json stateToJson() override;
102 void jsonToState(json data) override;
103
111 double getWindowSize() { return window_size_; }
115 double getWindowFade() { return window_fade_; }
118
119 force_inline void setStartPosition(double start_position) { start_position_ = start_position; }
120 force_inline void setWindowFade(double window_fade) { window_fade_ = window_fade; }
121 force_inline void setWindowSize(double window_size) { window_size_ = window_size; }
122 force_inline void setFadeStyle(FadeStyle fade_style) { fade_style_ = fade_style; }
123 force_inline void setPhaseStyle(PhaseStyle phase_style) { phase_style_ = phase_style; }
126 if (sample_buffer_ == nullptr || sample_buffer_->data == nullptr)
127 return nullptr;
128 return sample_buffer_->data.get() + 1;
129 }
131 if (sample_buffer_ == nullptr)
132 return nullptr;
133 return sample_buffer_->data.get();
134 }
135
136 float getScaledInterpolatedSample(float time);
137
141
145
146 protected:
148 const float* overridden_phase_;
151
157
158 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FileSourceKeyframe)
159 };
160
161 FileSource();
162 virtual ~FileSource() { }
163
164 WavetableKeyframe* createKeyframe(int position) override;
165 void render(vital::WaveFrame* wave_frame, float position) override;
167 json stateToJson() override;
168 void jsonToState(json data) override;
169
170 FileSourceKeyframe* getKeyframe(int index);
171 const SampleBuffer* buffer() const { return &sample_buffer_; }
175
176 void setNormalizeGain(bool normalize_gain) { normalize_gain_ = normalize_gain; }
177 void setWindowSize(double window_size) { window_size_ = window_size; }
178 void setFadeStyle(FadeStyle fade_style) { fade_style_ = fade_style; }
179 void setPhaseStyle(PhaseStyle phase_style);
181 double getWindowSize() { return window_size_; }
182
190 void loadBuffer(const float* buffer, int size, int sample_rate);
191
197 void detectPitch(int max_period = vital::WaveFrame::kWaveformSize);
198
202 void detectWaveEditTable();
203
205 if (sample_buffer_.data == nullptr)
206 return nullptr;
207 return sample_buffer_.data.get() + 1;
208 }
210
211 protected:
215
223
227
228 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(FileSource)
229};
230
A specific WavetableKeyframe that uses the FileSource’s audio buffer.
Definition file_source.h:74
void renderWaveBlend(vital::WaveFrame *wave_frame)
Definition file_source.cpp:119
force_inline void setFadeStyle(FadeStyle fade_style)
Definition file_source.h:122
void setInterpolateFromFrame(WaveSourceKeyframe *frame)
Definition file_source.h:138
virtual ~FileSourceKeyframe()
Definition file_source.h:82
FileSourceKeyframe(SampleBuffer *sample_buffer)
Constructs a keyframe tied to a given SampleBuffer.
Definition file_source.cpp:13
float getNormalizationScale()
Computes the normalization scale factor for the current wave segment.
Definition file_source.cpp:58
double getWindowFadeSamples()
Definition file_source.h:116
double getWindowSize()
Gets the size of the extracted window in samples.
Definition file_source.h:111
force_inline void setWindowFade(double window_fade)
Definition file_source.h:120
double window_size_
Definition file_source.h:154
double window_fade_
Definition file_source.h:153
PhaseStyle phase_style_
Definition file_source.h:156
void setInterpolateToFrame(WaveSourceKeyframe *frame)
Definition file_source.h:142
int getSamplesNeeded()
Definition file_source.h:117
void render(vital::WaveFrame *wave_frame) override
Renders the waveform of this keyframe into a WaveFrame.
Definition file_source.cpp:91
FadeStyle fade_style_
Definition file_source.h:155
const float * overridden_phase_
Definition file_source.h:148
double start_position_
Definition file_source.h:152
void renderFreqInterpolate(vital::WaveFrame *wave_frame)
Definition file_source.cpp:186
force_inline const float * getCubicInterpolationBuffer()
Definition file_source.h:130
void jsonToState(json data) override
Restores the keyframe's state from a JSON object.
Definition file_source.cpp:219
WaveSourceKeyframe * interpolate_from_frame_
Definition file_source.h:149
void renderTimeInterpolate(vital::WaveFrame *wave_frame)
Definition file_source.cpp:164
force_inline const float * getDataBuffer()
Definition file_source.h:125
WaveSourceKeyframe * interpolate_to_frame_
Definition file_source.h:150
double getWindowFade()
Gets the fade size for window blending.
Definition file_source.h:115
float getScaledInterpolatedSample(float time)
Definition file_source.cpp:44
force_inline void setPhaseStyle(PhaseStyle phase_style)
Definition file_source.h:123
void copy(const WavetableKeyframe *keyframe) override
Copies the state from another keyframe of the same type.
Definition file_source.cpp:25
double getStartPosition()
Gets the current start position of the wave segment in samples.
Definition file_source.h:107
void renderNoInterpolate(vital::WaveFrame *wave_frame)
Definition file_source.cpp:148
force_inline void setOverriddenPhaseBuffer(const float *buffer)
Definition file_source.h:124
json stateToJson() override
Serializes the state of this keyframe to a JSON object.
Definition file_source.cpp:211
force_inline void setStartPosition(double start_position)
Definition file_source.h:119
void interpolate(const WavetableKeyframe *from_keyframe, const WavetableKeyframe *to_keyframe, float t) override
Linearly interpolates between two keyframes.
Definition file_source.cpp:32
SampleBuffer * sample_buffer_
Definition file_source.h:147
force_inline void setWindowSize(double window_size)
Definition file_source.h:121
A WavetableComponent that uses an external audio sample as its source.
Definition file_source.h:23
FadeStyle fade_style_
Definition file_source.h:218
int random_seed_
Definition file_source.h:224
FileSourceKeyframe compute_frame_
Definition file_source.h:212
PhaseStyle phase_style_
Definition file_source.h:219
json stateToJson() override
Serializes the component’s state and all keyframes to a JSON object.
Definition file_source.cpp:265
void loadBuffer(const float *buffer, int size, int sample_rate)
Loads audio data into the file source buffer.
Definition file_source.cpp:363
force_inline const float * getDataBuffer()
Definition file_source.h:204
force_inline const float * getCubicInterpolationBuffer()
Definition file_source.h:209
double getWindowSize()
Definition file_source.h:181
void detectPitch(int max_period=vital::WaveFrame::kWaveformSize)
Attempts to detect pitch in the loaded sample to determine window size automatically.
Definition file_source.cpp:376
bool normalize_mult_
Definition file_source.h:221
FadeStyle
Different methods to blend or interpolate the loaded audio window into a wavetable frame.
Definition file_source.h:38
@ kFreqInterpolate
Interpolate in frequency domain between cycles.
Definition file_source.h:42
@ kTimeInterpolate
Interpolate in time domain between cycles.
Definition file_source.h:41
@ kWaveBlend
Blend windowed segments into each other.
Definition file_source.h:39
@ kNoInterpolate
Use a single segment, no blending.
Definition file_source.h:40
@ kNumFadeStyles
Definition file_source.h:43
void render(vital::WaveFrame *wave_frame, float position) override
Renders the waveform at a given position into a WaveFrame.
Definition file_source.cpp:240
void setNormalizeGain(bool normalize_gain)
Definition file_source.h:176
WaveSourceKeyframe interpolate_from_frame_
Definition file_source.h:213
bool normalize_gain_
Definition file_source.h:220
static constexpr int kExtraSaveSamples
Extra samples saved for safe interpolation and boundary conditions.
Definition file_source.h:28
PhaseStyle getPhaseStyle()
Definition file_source.h:173
bool getNormalizeGain()
Definition file_source.h:174
WaveSourceKeyframe interpolate_to_frame_
Definition file_source.h:214
WavetableComponentFactory::ComponentType getType() override
Returns the type of this WavetableComponent.
Definition file_source.cpp:260
void setFadeStyle(FadeStyle fade_style)
Definition file_source.h:178
void setPhaseStyle(PhaseStyle phase_style)
Definition file_source.cpp:337
vital::utils::RandomGenerator random_generator_
Definition file_source.h:225
SampleBuffer sample_buffer_
Definition file_source.h:216
void writePhaseOverrideBuffer()
Definition file_source.cpp:348
void detectWaveEditTable()
Detects if the source audio can form a WaveEdit-style wavetable (special format).
Definition file_source.cpp:384
const SampleBuffer * buffer() const
Definition file_source.h:171
PitchDetector pitch_detector_
Definition file_source.h:226
float overridden_phase_[vital::WaveFrame::kWaveformSize]
Definition file_source.h:217
static constexpr int kExtraBufferSamples
Additional buffer samples for safe reading beyond boundaries.
Definition file_source.h:30
static constexpr int kPitchDetectMaxPeriod
Maximum period for pitch detection to limit CPU usage.
Definition file_source.h:32
double window_size_
Definition file_source.h:222
WavetableKeyframe * createKeyframe(int position) override
Creates a new keyframe at a given position.
Definition file_source.cpp:234
void setWindowSize(double window_size)
Definition file_source.h:177
FileSourceKeyframe * getKeyframe(int index)
Definition file_source.cpp:332
virtual ~FileSource()
Definition file_source.h:162
static constexpr float kMaxFileSourceSamples
Maximum number of samples allowed from the file source (in samples).
Definition file_source.h:26
FileSource()
Definition file_source.cpp:226
void jsonToState(json data) override
Restores the component’s state from a JSON object.
Definition file_source.cpp:294
PhaseStyle
Methods for handling phase information in the transformed wave.
Definition file_source.h:50
@ kNone
Keep phases as-is.
Definition file_source.h:51
@ kNumPhaseStyles
Definition file_source.h:54
@ kVocode
Assign random phases for vocoding-like effect.
Definition file_source.h:53
@ kClear
Clear phases to a known pattern.
Definition file_source.h:52
FadeStyle getFadeStyle()
Definition file_source.h:172
A utility class for estimating the pitch (fundamental period) of a given audio signal segment.
Definition pitch_detector.h:18
A keyframe that holds a single WaveFrame and supports various interpolation methods.
Definition wave_source.h:92
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
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
static constexpr int kWaveformSize
The size of the waveform (number of samples per frame).
Definition wave_frame.h:21
A basic random number generator for producing uniform distributions of floats.
Definition utils.h:61
#define force_inline
Definition common.h:23
nlohmann::json json
Definition line_generator.h:7
A simple structure holding a buffer of samples loaded from the file source.
Definition file_source.h:60
int size
Number of samples in the buffer.
Definition file_source.h:63
int sample_rate
Sample rate of the audio data.
Definition file_source.h:64
std::unique_ptr< float[]> data
Pointer to raw audio data.
Definition file_source.h:62
SampleBuffer()
Definition file_source.h:61
Provides various utility functions, classes, and constants for audio, math, and general-purpose opera...