Vital
Loading...
Searching...
No Matches
vital::WaveFrame Class Reference

Represents a single frame of a wavetable, containing both time-domain and frequency-domain data. More...

#include <wave_frame.h>

Public Member Functions

 WaveFrame ()
 Constructs a WaveFrame with default frequency ratio and sample rate.
 
mono_float getMaxZeroOffset () const
 Retrieves the maximum absolute amplitude in the time-domain waveform.
 
void normalize (bool allow_positive_gain=false)
 Normalizes the time-domain waveform samples to have a maximum absolute value of 1.0.
 
void clear ()
 Clears the waveform data, resetting it to default states.
 
void setFrequencyRatio (float ratio)
 Sets the frequency ratio for this wave frame.
 
void setSampleRate (float rate)
 Sets the sample rate for this wave frame.
 
void multiply (mono_float value)
 Multiplies all samples in both time and frequency domains by a given value.
 
void loadTimeDomain (float *buffer)
 Loads time-domain data from a given buffer and updates the frequency domain accordingly.
 
void addFrom (WaveFrame *source)
 Adds another WaveFrame's data to this one, sample-by-sample, in both time and frequency domains.
 
void copy (const WaveFrame *other)
 Copies another WaveFrame's time and frequency domain data into this one.
 
void toFrequencyDomain ()
 Converts the currently loaded time-domain data into frequency-domain representation.
 
void toTimeDomain ()
 Converts the currently loaded frequency-domain data into time-domain representation.
 
void removedDc ()
 Removes the DC offset from the waveform.
 
float * getFrequencyData ()
 Gets a pointer to the frequency-domain data interpreted as floats.
 

Public Attributes

int index
 The index of this frame in a wavetable.
 
float frequency_ratio
 The frequency ratio for this frame (e.g., for pitch scaling).
 
float sample_rate
 The sample rate associated with this frame.
 
mono_float time_domain [2 *kWaveformSize]
 The time-domain data, extended buffer size for FFT alignment.
 
std::complex< float > frequency_domain [kWaveformSize]
 The frequency-domain representation (complex spectrum).
 

Static Public Attributes

static constexpr int kWaveformBits = 11
 The number of bits that define the size of the waveform (2^kWaveformBits = kWaveformSize).
 
static constexpr int kWaveformSize = 1 << kWaveformBits
 The size of the waveform (number of samples per frame).
 
static constexpr int kNumRealComplex = kWaveformSize / 2 + 1
 The number of real-valued frequency components (half the size + 1).
 
static constexpr int kNumExtraComplex = kWaveformSize - kNumRealComplex
 The number of "extra" complex bins to pad after the real frequency components.
 
static constexpr float kDefaultFrequencyRatio = 1.0f
 The default frequency ratio for a WaveFrame (usually 1.0f).
 
static constexpr float kDefaultSampleRate = 44100.0f
 The default sample rate for a WaveFrame.
 

Detailed Description

Represents a single frame of a wavetable, containing both time-domain and frequency-domain data.

A WaveFrame holds a waveform in both time-domain and frequency-domain forms. It allows for operations such as normalization, clearing, transforming between time and frequency domains, and other utility functions to manipulate or analyze the waveform.

Constructor & Destructor Documentation

◆ WaveFrame()

vital::WaveFrame::WaveFrame ( )
inline

Constructs a WaveFrame with default frequency ratio and sample rate.

The waveform data is initialized to zero, and frequency/time domains are ready for loading or processing.

Member Function Documentation

◆ addFrom()

void vital::WaveFrame::addFrom ( WaveFrame * source)

Adds another WaveFrame's data to this one, sample-by-sample, in both time and frequency domains.

Parameters
sourcePointer to the source WaveFrame whose data will be added to this one.

◆ clear()

void vital::WaveFrame::clear ( )

Clears the waveform data, resetting it to default states.

This sets the frequency ratio, sample rate to defaults, and zeros all frequency_domain and time_domain samples.

◆ copy()

void vital::WaveFrame::copy ( const WaveFrame * other)

Copies another WaveFrame's time and frequency domain data into this one.

Parameters
otherPointer to the WaveFrame whose data will be copied.

◆ getFrequencyData()

float * vital::WaveFrame::getFrequencyData ( )
inline

Gets a pointer to the frequency-domain data interpreted as floats.

Returns
A float pointer to the frequency-domain data (real and imaginary interleaved).

◆ getMaxZeroOffset()

mono_float vital::WaveFrame::getMaxZeroOffset ( ) const

Retrieves the maximum absolute amplitude in the time-domain waveform.

Returns
The maximum absolute sample value found in the time_domain array.

◆ loadTimeDomain()

void vital::WaveFrame::loadTimeDomain ( float * buffer)

Loads time-domain data from a given buffer and updates the frequency domain accordingly.

Parameters
bufferPointer to a float array of length kWaveformSize containing the time-domain samples.

◆ multiply()

void vital::WaveFrame::multiply ( mono_float value)

Multiplies all samples in both time and frequency domains by a given value.

Parameters
valueThe value by which to scale the waveform data.

◆ normalize()

void vital::WaveFrame::normalize ( bool allow_positive_gain = false)

Normalizes the time-domain waveform samples to have a maximum absolute value of 1.0.

Parameters
allow_positive_gainIf true, allows scaling up the waveform if it's below the normalization threshold, otherwise only scales down.

◆ removedDc()

void vital::WaveFrame::removedDc ( )

Removes the DC offset from the waveform.

This modifies the time_domain samples by subtracting the DC component. The DC component is derived from the frequency_domain[0] bin.

Removes DC offset from the waveform by subtracting the imaginary part of frequency_domain[0].

The DC offset is found in the imaginary component of the first frequency bin. Subtracting this value from time_domain samples removes any constant offset.

◆ setFrequencyRatio()

void vital::WaveFrame::setFrequencyRatio ( float ratio)
inline

Sets the frequency ratio for this wave frame.

Parameters
ratioThe new frequency ratio value.

◆ setSampleRate()

void vital::WaveFrame::setSampleRate ( float rate)
inline

Sets the sample rate for this wave frame.

Parameters
rateThe new sample rate.

◆ toFrequencyDomain()

void vital::WaveFrame::toFrequencyDomain ( )

Converts the currently loaded time-domain data into frequency-domain representation.

Converts the time_domain data into the frequency_domain representation using FFT.

This uses a forward real-to-complex FFT. The frequency_domain will then contain complex frequency bins representing the spectrum of the waveform.

◆ toTimeDomain()

void vital::WaveFrame::toTimeDomain ( )

Converts the currently loaded frequency-domain data into time-domain representation.

Converts the frequency_domain data back into the time_domain representation using an inverse FFT.

This restores the waveform from its frequency bins. If modifications were made in the frequency domain, this will produce the corresponding time-domain waveform.

Member Data Documentation

◆ frequency_domain

std::complex<float> vital::WaveFrame::frequency_domain[kWaveformSize]

The frequency-domain representation (complex spectrum).

◆ frequency_ratio

float vital::WaveFrame::frequency_ratio

The frequency ratio for this frame (e.g., for pitch scaling).

◆ index

int vital::WaveFrame::index

The index of this frame in a wavetable.

◆ kDefaultFrequencyRatio

float vital::WaveFrame::kDefaultFrequencyRatio = 1.0f
staticconstexpr

The default frequency ratio for a WaveFrame (usually 1.0f).

◆ kDefaultSampleRate

float vital::WaveFrame::kDefaultSampleRate = 44100.0f
staticconstexpr

The default sample rate for a WaveFrame.

◆ kNumExtraComplex

int vital::WaveFrame::kNumExtraComplex = kWaveformSize - kNumRealComplex
staticconstexpr

The number of "extra" complex bins to pad after the real frequency components.

◆ kNumRealComplex

int vital::WaveFrame::kNumRealComplex = kWaveformSize / 2 + 1
staticconstexpr

The number of real-valued frequency components (half the size + 1).

◆ kWaveformBits

int vital::WaveFrame::kWaveformBits = 11
staticconstexpr

The number of bits that define the size of the waveform (2^kWaveformBits = kWaveformSize).

◆ kWaveformSize

int vital::WaveFrame::kWaveformSize = 1 << kWaveformBits
staticconstexpr

The size of the waveform (number of samples per frame).

◆ sample_rate

float vital::WaveFrame::sample_rate

The sample rate associated with this frame.

◆ time_domain

mono_float vital::WaveFrame::time_domain[2 *kWaveformSize]

The time-domain data, extended buffer size for FFT alignment.


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