Vital
Loading...
Searching...
No Matches
FileSource Class Reference

A WavetableComponent that uses an external audio sample as its source. More...

#include <file_source.h>

Inheritance diagram for FileSource:
WavetableComponent

Classes

class  FileSourceKeyframe
 A specific WavetableKeyframe that uses the FileSource’s audio buffer. More...
 
struct  SampleBuffer
 A simple structure holding a buffer of samples loaded from the file source. More...
 

Public Types

enum  FadeStyle {
  kWaveBlend , kNoInterpolate , kTimeInterpolate , kFreqInterpolate ,
  kNumFadeStyles
}
 Different methods to blend or interpolate the loaded audio window into a wavetable frame. More...
 
enum  PhaseStyle { kNone , kClear , kVocode , kNumPhaseStyles }
 Methods for handling phase information in the transformed wave. More...
 
- Public Types inherited from WavetableComponent
enum  InterpolationStyle { kNone , kLinear , kCubic , kNumInterpolationStyles }
 Defines how interpolation is performed between keyframes. More...
 

Public Member Functions

 FileSource ()
 
virtual ~FileSource ()
 
WavetableKeyframecreateKeyframe (int position) override
 Creates a new keyframe at a given position.
 
void render (vital::WaveFrame *wave_frame, float position) override
 Renders the waveform at a given position into a WaveFrame.
 
WavetableComponentFactory::ComponentType getType () override
 Returns the type of this WavetableComponent.
 
json stateToJson () override
 Serializes the component’s state and all keyframes to a JSON object.
 
void jsonToState (json data) override
 Restores the component’s state from a JSON object.
 
FileSourceKeyframegetKeyframe (int index)
 
const SampleBufferbuffer () const
 
FadeStyle getFadeStyle ()
 
PhaseStyle getPhaseStyle ()
 
bool getNormalizeGain ()
 
void setNormalizeGain (bool normalize_gain)
 
void setWindowSize (double window_size)
 
void setFadeStyle (FadeStyle fade_style)
 
void setPhaseStyle (PhaseStyle phase_style)
 
void writePhaseOverrideBuffer ()
 
double getWindowSize ()
 
void loadBuffer (const float *buffer, int size, int sample_rate)
 Loads audio data into the file source buffer.
 
void detectPitch (int max_period=vital::WaveFrame::kWaveformSize)
 Attempts to detect pitch in the loaded sample to determine window size automatically.
 
void detectWaveEditTable ()
 Detects if the source audio can form a WaveEdit-style wavetable (special format).
 
force_inline const float * getDataBuffer ()
 
force_inline const float * getCubicInterpolationBuffer ()
 
- Public Member Functions inherited from WavetableComponent
 WavetableComponent ()
 Constructs a WavetableComponent with a default linear interpolation style.
 
virtual ~WavetableComponent ()
 
virtual void prerender ()
 Called before rendering to perform any needed precomputation.
 
virtual bool hasKeyframes ()
 Indicates whether this component relies on multiple keyframes.
 
void reset ()
 Clears all keyframes and inserts a default one at position 0.
 
void interpolate (WavetableKeyframe *dest, float position)
 Interpolates a destination keyframe at a given position.
 
WavetableKeyframeinsertNewKeyframe (int position)
 Inserts a new keyframe at the given position, creating and sorting it into the array.
 
void reposition (WavetableKeyframe *keyframe)
 Repositions a keyframe in the keyframe list after its position changed.
 
void remove (WavetableKeyframe *keyframe)
 Removes a specific keyframe from the component.
 
int numFrames () const
 Gets the number of keyframes.
 
int indexOf (WavetableKeyframe *keyframe) const
 Finds the index of a given keyframe.
 
WavetableKeyframegetFrameAt (int index) const
 Gets a keyframe by index.
 
int getIndexFromPosition (int position) const
 Finds the insertion index for a given position to keep keyframes sorted.
 
WavetableKeyframegetFrameAtPosition (int position)
 Gets a keyframe by position if one matches exactly, or nullptr otherwise.
 
int getLastKeyframePosition ()
 Gets the highest position among all keyframes.
 
void setInterpolationStyle (InterpolationStyle type)
 Sets the global interpolation style.
 
InterpolationStyle getInterpolationStyle () const
 Gets the current global interpolation style.
 

Static Public Attributes

static constexpr float kMaxFileSourceSamples = 176400
 Maximum number of samples allowed from the file source (in samples).
 
static constexpr int kExtraSaveSamples = 4
 Extra samples saved for safe interpolation and boundary conditions.
 
static constexpr int kExtraBufferSamples = 4
 Additional buffer samples for safe reading beyond boundaries.
 
static constexpr int kPitchDetectMaxPeriod = 8096
 Maximum period for pitch detection to limit CPU usage.
 

Protected Attributes

FileSourceKeyframe compute_frame_
 
WaveSourceKeyframe interpolate_from_frame_
 
WaveSourceKeyframe interpolate_to_frame_
 
SampleBuffer sample_buffer_
 
float overridden_phase_ [vital::WaveFrame::kWaveformSize]
 
FadeStyle fade_style_
 
PhaseStyle phase_style_
 
bool normalize_gain_
 
bool normalize_mult_
 
double window_size_
 
int random_seed_
 
vital::utils::RandomGenerator random_generator_
 
PitchDetector pitch_detector_
 
- Protected Attributes inherited from WavetableComponent
std::vector< std::unique_ptr< WavetableKeyframe > > keyframes_
 The list of keyframes sorted by position.
 
InterpolationStyle interpolation_style_
 Current interpolation style (none, linear, cubic).
 

Detailed Description

A WavetableComponent that uses an external audio sample as its source.

FileSource loads a segment of audio data (e.g., from a sample file) and transforms it into wavetables. It provides multiple interpolation (fade) styles and phase manipulation options for constructing a wavetable frame. Users can set how the audio frames are extracted (window size, fade style), and how phases are handled (e.g., vocoding).

Member Enumeration Documentation

◆ FadeStyle

Different methods to blend or interpolate the loaded audio window into a wavetable frame.

Enumerator
kWaveBlend 

Blend windowed segments into each other.

kNoInterpolate 

Use a single segment, no blending.

kTimeInterpolate 

Interpolate in time domain between cycles.

kFreqInterpolate 

Interpolate in frequency domain between cycles.

kNumFadeStyles 

◆ PhaseStyle

Methods for handling phase information in the transformed wave.

Enumerator
kNone 

Keep phases as-is.

kClear 

Clear phases to a known pattern.

kVocode 

Assign random phases for vocoding-like effect.

kNumPhaseStyles 

Constructor & Destructor Documentation

◆ FileSource()

FileSource::FileSource ( )

◆ ~FileSource()

virtual FileSource::~FileSource ( )
inlinevirtual

Member Function Documentation

◆ buffer()

const SampleBuffer * FileSource::buffer ( ) const
inline

◆ createKeyframe()

WavetableKeyframe * FileSource::createKeyframe ( int position)
overridevirtual

Creates a new keyframe at a given position.

Implemented by subclasses to produce a keyframe type suited to their functionality.

Parameters
positionThe wavetable frame index (0 to kNumOscillatorWaveFrames-1).
Returns
A pointer to the newly created WavetableKeyframe.

Implements WavetableComponent.

◆ detectPitch()

void FileSource::detectPitch ( int max_period = vital::WaveFrame::kWaveformSize)

Attempts to detect pitch in the loaded sample to determine window size automatically.

Parameters
max_periodMaximum period considered for pitch detection.

◆ detectWaveEditTable()

void FileSource::detectWaveEditTable ( )

Detects if the source audio can form a WaveEdit-style wavetable (special format).

◆ getCubicInterpolationBuffer()

force_inline const float * FileSource::getCubicInterpolationBuffer ( )
inline

◆ getDataBuffer()

force_inline const float * FileSource::getDataBuffer ( )
inline

◆ getFadeStyle()

FadeStyle FileSource::getFadeStyle ( )
inline

◆ getKeyframe()

FileSource::FileSourceKeyframe * FileSource::getKeyframe ( int index)

◆ getNormalizeGain()

bool FileSource::getNormalizeGain ( )
inline

◆ getPhaseStyle()

PhaseStyle FileSource::getPhaseStyle ( )
inline

◆ getType()

WavetableComponentFactory::ComponentType FileSource::getType ( )
overridevirtual

Returns the type of this WavetableComponent.

Used to identify the specific component for serialization and UI mapping.

Returns
The component type enumerated in WavetableComponentFactory.

Implements WavetableComponent.

◆ getWindowSize()

double FileSource::getWindowSize ( )
inline

◆ jsonToState()

void FileSource::jsonToState ( json data)
overridevirtual

Restores the component’s state from a JSON object.

Clears existing keyframes and reconstructs them from the provided JSON data.

Parameters
dataThe JSON object containing saved state data.

Reimplemented from WavetableComponent.

◆ loadBuffer()

void FileSource::loadBuffer ( const float * buffer,
int size,
int sample_rate )

Loads audio data into the file source buffer.

Parameters
bufferPointer to the audio samples.
sizeNumber of samples.
sample_rateThe sample rate of the audio data.

◆ render()

void FileSource::render ( vital::WaveFrame * wave_frame,
float position )
overridevirtual

Renders the waveform at a given position into a WaveFrame.

Uses interpolation between keyframes based on the current interpolation style to produce a waveform for the given position.

Parameters
wave_frameThe WaveFrame to fill with the resulting waveform.
positionThe position along the wavetable dimension [0, kNumOscillatorWaveFrames-1].

Implements WavetableComponent.

◆ setFadeStyle()

void FileSource::setFadeStyle ( FadeStyle fade_style)
inline

◆ setNormalizeGain()

void FileSource::setNormalizeGain ( bool normalize_gain)
inline

◆ setPhaseStyle()

void FileSource::setPhaseStyle ( PhaseStyle phase_style)

◆ setWindowSize()

void FileSource::setWindowSize ( double window_size)
inline

◆ stateToJson()

json FileSource::stateToJson ( )
overridevirtual

Serializes the component’s state and all keyframes to a JSON object.

Returns
A JSON object representing the entire state.

Reimplemented from WavetableComponent.

◆ writePhaseOverrideBuffer()

void FileSource::writePhaseOverrideBuffer ( )

Member Data Documentation

◆ compute_frame_

FileSourceKeyframe FileSource::compute_frame_
protected

◆ fade_style_

FadeStyle FileSource::fade_style_
protected

◆ interpolate_from_frame_

WaveSourceKeyframe FileSource::interpolate_from_frame_
protected

◆ interpolate_to_frame_

WaveSourceKeyframe FileSource::interpolate_to_frame_
protected

◆ kExtraBufferSamples

int FileSource::kExtraBufferSamples = 4
staticconstexpr

Additional buffer samples for safe reading beyond boundaries.

◆ kExtraSaveSamples

int FileSource::kExtraSaveSamples = 4
staticconstexpr

Extra samples saved for safe interpolation and boundary conditions.

◆ kMaxFileSourceSamples

float FileSource::kMaxFileSourceSamples = 176400
staticconstexpr

Maximum number of samples allowed from the file source (in samples).

◆ kPitchDetectMaxPeriod

int FileSource::kPitchDetectMaxPeriod = 8096
staticconstexpr

Maximum period for pitch detection to limit CPU usage.

◆ normalize_gain_

bool FileSource::normalize_gain_
protected

◆ normalize_mult_

bool FileSource::normalize_mult_
protected

◆ overridden_phase_

float FileSource::overridden_phase_[vital::WaveFrame::kWaveformSize]
protected

◆ phase_style_

PhaseStyle FileSource::phase_style_
protected

◆ pitch_detector_

PitchDetector FileSource::pitch_detector_
protected

◆ random_generator_

vital::utils::RandomGenerator FileSource::random_generator_
protected

◆ random_seed_

int FileSource::random_seed_
protected

◆ sample_buffer_

SampleBuffer FileSource::sample_buffer_
protected

◆ window_size_

double FileSource::window_size_
protected

The documentation for this class was generated from the following files: