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

Holds and manages a single sampled waveform, including stereo or mono data and multiple band-limited versions for different playback rates. More...

#include <sample_source.h>

Classes

struct  SampleData
 Holds the actual audio buffers (left/right) for multiple band-limited versions (both looped and non-looped), and associated metadata like length and sample rate. More...
 

Public Member Functions

 Sample ()
 Default constructor. Initializes the sample with default noise data.
 
void loadSample (const mono_float *buffer, int size, int sample_rate)
 Loads a mono sample from raw float data.
 
void loadSample (const mono_float *left_buffer, const mono_float *right_buffer, int size, int sample_rate)
 Loads a stereo sample from two float arrays (left/right).
 
void setName (const std::string &name)
 Sets the user-facing name of the sample.
 
std::string getName () const
 Returns the user-facing name of the sample.
 
void setLastBrowsedFile (const std::string &path)
 Sets the last browsed file path (if applicable).
 
std::string getLastBrowsedFile () const
 Returns the last browsed file path.
 
force_inline int originalLength () const
 Returns the length of the originally loaded sample in frames.
 
force_inline int upsampleLength ()
 Returns the length of the upsampled data (1 << kUpsampleTimes).
 
force_inline int sampleRate () const
 Returns the sample rate of the originally loaded data.
 
force_inline int activeLength () const
 Returns the active (currently used) sample length in frames, taking into account upsample factor and possibly another data set.
 
force_inline int activeSampleRate () const
 Returns the sample rate of the currently active sample data.
 
force_inline const mono_floatbuffer () const
 Returns a pointer to the (current) left channel buffer at the base upsample level.
 
void init ()
 Generates default data for the sample (e.g., random noise).
 
int getActiveIndex (mono_float delta)
 Determines which band-limited buffer index should be used for a given pitch delta.
 
force_inline const mono_floatgetActiveLeftBuffer (int index)
 Retrieves a pointer to the active left channel buffer at a specific band-limited index.
 
force_inline const mono_floatgetActiveLeftLoopBuffer (int index)
 Retrieves a pointer to the active left loop buffer at a specific band-limited index.
 
force_inline const mono_floatgetActiveRightBuffer (int index)
 Retrieves a pointer to the active right channel buffer at a band-limited index. If the sample is mono, it returns the left buffer.
 
force_inline const mono_floatgetActiveRightLoopBuffer (int index)
 Retrieves a pointer to the active right loop buffer at a band-limited index. If the sample is mono, returns the left loop buffer.
 
force_inline void markUsed ()
 Marks this sample as "in use" by updating the active_audio_data_ pointer.
 
force_inline void markUnused ()
 Marks this sample as "not in use," clearing the active_audio_data_ pointer.
 
json stateToJson ()
 Exports the sample state (metadata and sample data) to a JSON object.
 
void jsonToState (json data)
 Restores the sample's state from a JSON object (including audio data).
 

Static Public Attributes

static constexpr int kDefaultSampleLength = 44100
 Default length for a newly created (noise) sample if none is provided.
 
static constexpr int kUpsampleTimes = 1
 Upsampling factor exponent (i.e., 1 << kUpsampleTimes).
 
static constexpr int kBufferSamples = 4
 Extra buffer samples at start and end to avoid interpolation issues.
 
static constexpr int kMinSize = 4
 Minimum sample size for further downsampling.
 

Protected Attributes

std::string name_
 The user-facing name of the sample.
 
std::string last_browsed_file_
 The last browsed file path for this sample (if any).
 
SampleDatacurrent_data_
 Pointer to the currently loaded data.
 
std::atomic< SampleData * > active_audio_data_
 Atomic pointer to data in active use.
 
std::unique_ptr< SampleDatadata_
 Owned sample data for this sample.
 

Detailed Description

Holds and manages a single sampled waveform, including stereo or mono data and multiple band-limited versions for different playback rates.

The Sample class can load samples from raw float buffers or from a stereo pair. It also supports generating default content (e.g., noise) and can produce a JSON representation for saving/loading states.

Constructor & Destructor Documentation

◆ Sample()

vital::Sample::Sample ( )

Default constructor. Initializes the sample with default noise data.

Member Function Documentation

◆ activeLength()

force_inline int vital::Sample::activeLength ( ) const
inline

Returns the active (currently used) sample length in frames, taking into account upsample factor and possibly another data set.

◆ activeSampleRate()

force_inline int vital::Sample::activeSampleRate ( ) const
inline

Returns the sample rate of the currently active sample data.

◆ buffer()

force_inline const mono_float * vital::Sample::buffer ( ) const
inline

Returns a pointer to the (current) left channel buffer at the base upsample level.

This is primarily for quick access to the default buffer, offset by kBufferSamples.

◆ getActiveIndex()

int vital::Sample::getActiveIndex ( mono_float delta)
inline

Determines which band-limited buffer index should be used for a given pitch delta.

Parameters
deltaFrequency ratio or pitch factor to find appropriate band-limited buffer.
Returns
The index within the active_audio_data_ arrays.

◆ getActiveLeftBuffer()

force_inline const mono_float * vital::Sample::getActiveLeftBuffer ( int index)
inline

Retrieves a pointer to the active left channel buffer at a specific band-limited index.

Parameters
indexThe band-limited index, computed from getActiveIndex().
Returns
A pointer to the float array for that band-limited buffer.

◆ getActiveLeftLoopBuffer()

force_inline const mono_float * vital::Sample::getActiveLeftLoopBuffer ( int index)
inline

Retrieves a pointer to the active left loop buffer at a specific band-limited index.

◆ getActiveRightBuffer()

force_inline const mono_float * vital::Sample::getActiveRightBuffer ( int index)
inline

Retrieves a pointer to the active right channel buffer at a band-limited index. If the sample is mono, it returns the left buffer.

◆ getActiveRightLoopBuffer()

force_inline const mono_float * vital::Sample::getActiveRightLoopBuffer ( int index)
inline

Retrieves a pointer to the active right loop buffer at a band-limited index. If the sample is mono, returns the left loop buffer.

◆ getLastBrowsedFile()

std::string vital::Sample::getLastBrowsedFile ( ) const
inline

Returns the last browsed file path.

◆ getName()

std::string vital::Sample::getName ( ) const
inline

Returns the user-facing name of the sample.

◆ init()

void vital::Sample::init ( )

Generates default data for the sample (e.g., random noise).

◆ jsonToState()

void vital::Sample::jsonToState ( json data)

Restores the sample's state from a JSON object (including audio data).

Parameters
dataA JSON object containing sample data.

◆ loadSample() [1/2]

void vital::Sample::loadSample ( const mono_float * buffer,
int size,
int sample_rate )

Loads a mono sample from raw float data.

Parameters
bufferPointer to the float array containing sample data.
sizeNumber of frames in the buffer.
sample_rateThe sample rate of the data.

◆ loadSample() [2/2]

void vital::Sample::loadSample ( const mono_float * left_buffer,
const mono_float * right_buffer,
int size,
int sample_rate )

Loads a stereo sample from two float arrays (left/right).

Parameters
left_bufferPointer to float array for the left channel.
right_bufferPointer to float array for the right channel.
sizeNumber of frames in each buffer.
sample_rateThe sample rate of the data.

◆ markUnused()

force_inline void vital::Sample::markUnused ( )
inline

Marks this sample as "not in use," clearing the active_audio_data_ pointer.

◆ markUsed()

force_inline void vital::Sample::markUsed ( )
inline

Marks this sample as "in use" by updating the active_audio_data_ pointer.

◆ originalLength()

force_inline int vital::Sample::originalLength ( ) const
inline

Returns the length of the originally loaded sample in frames.

◆ sampleRate()

force_inline int vital::Sample::sampleRate ( ) const
inline

Returns the sample rate of the originally loaded data.

◆ setLastBrowsedFile()

void vital::Sample::setLastBrowsedFile ( const std::string & path)
inline

Sets the last browsed file path (if applicable).

◆ setName()

void vital::Sample::setName ( const std::string & name)
inline

Sets the user-facing name of the sample.

◆ stateToJson()

json vital::Sample::stateToJson ( )

Exports the sample state (metadata and sample data) to a JSON object.

Returns
A JSON object containing the sample's state.

◆ upsampleLength()

force_inline int vital::Sample::upsampleLength ( )
inline

Returns the length of the upsampled data (1 << kUpsampleTimes).

Member Data Documentation

◆ active_audio_data_

std::atomic<SampleData*> vital::Sample::active_audio_data_
protected

Atomic pointer to data in active use.

◆ current_data_

SampleData* vital::Sample::current_data_
protected

Pointer to the currently loaded data.

◆ data_

std::unique_ptr<SampleData> vital::Sample::data_
protected

Owned sample data for this sample.

◆ kBufferSamples

int vital::Sample::kBufferSamples = 4
staticconstexpr

Extra buffer samples at start and end to avoid interpolation issues.

◆ kDefaultSampleLength

int vital::Sample::kDefaultSampleLength = 44100
staticconstexpr

Default length for a newly created (noise) sample if none is provided.

◆ kMinSize

int vital::Sample::kMinSize = 4
staticconstexpr

Minimum sample size for further downsampling.

◆ kUpsampleTimes

int vital::Sample::kUpsampleTimes = 1
staticconstexpr

Upsampling factor exponent (i.e., 1 << kUpsampleTimes).

◆ last_browsed_file_

std::string vital::Sample::last_browsed_file_
protected

The last browsed file path for this sample (if any).

◆ name_

std::string vital::Sample::name_
protected

The user-facing name of the sample.


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