Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
roc::audio::SampleSpec Class Reference

Sample specification. Describes sample rate and channels. More...

#include <sample_spec.h>

Public Member Functions

 SampleSpec ()
 Construct empty specification. More...
 
 SampleSpec (size_t sample_rate, PcmFormat pcm_fmt, const ChannelSet &channel_set)
 Construct specification with parameters. More...
 
 SampleSpec (size_t sample_rate, PcmFormat pcm_fmt, ChannelLayout channel_layout, ChannelOrder channel_order, ChannelMask channel_mask)
 Construct specification with parameters. More...
 
Equality
bool operator== (const SampleSpec &other) const
 Check two specifications for equality. More...
 
bool operator!= (const SampleSpec &other) const
 Check two specifications for equality. More...
 
Getters and setters
bool is_valid () const
 Check if sample spec has non-zero rate and valid channel set. More...
 
bool is_empty () const
 Check if sample spec has a zero rate, empty channel set, and invalid_format. More...
 
bool is_raw () const
 Check if samples are in raw format. More...
 
void clear ()
 Unset all fields. More...
 
void use_defaults (PcmFormat default_pcm_fmt, ChannelLayout default_channel_layout, ChannelOrder default_channel_order, ChannelMask default_channel_mask, size_t default_sample_rate)
 Set missing fields from provided defaults. More...
 
size_t sample_rate () const
 Get sample rate. More...
 
void set_sample_rate (size_t sample_rate)
 Set sample rate. More...
 
SampleFormat sample_format () const
 Get sample format. More...
 
void set_sample_format (SampleFormat sample_fmt)
 Set sample format. More...
 
PcmFormat pcm_format () const
 Get PCM format. More...
 
void set_pcm_format (PcmFormat pcm_fmt)
 Set PCM format. More...
 
const ChannelSetchannel_set () const
 Get channel set. More...
 
ChannelSetchannel_set ()
 Get mutable channel set. More...
 
void set_channel_set (const ChannelSet &channel_set)
 Set channel set. More...
 
size_t num_channels () const
 Get number enabled channels in channel set. More...
 
Convert number of samples
size_t ns_2_samples_per_chan (core::nanoseconds_t ns_duration) const
 Convert nanoseconds duration to number of samples per channel. More...
 
core::nanoseconds_t samples_per_chan_2_ns (size_t n_samples) const
 Convert number of samples per channel to nanoseconds duration. More...
 
core::nanoseconds_t fract_samples_per_chan_2_ns (float n_samples) const
 Convert (possibly fractional) number samples per channel to nanoseconds duration. More...
 
size_t ns_2_samples_overall (core::nanoseconds_t ns_duration) const
 Convert nanoseconds duration to number of samples for all channels. More...
 
core::nanoseconds_t samples_overall_2_ns (size_t n_samples) const
 Convert number of samples for all channels to nanoseconds duration. More...
 
core::nanoseconds_t fract_samples_overall_2_ns (float n_samples) const
 Convert number of samples (possibly non-integer) to nanoseconds. More...
 
Convert stream timestamps
packet::stream_timestamp_t ns_2_stream_timestamp (core::nanoseconds_t ns_duration) const
 Convert nanoseconds delta to stream timestamp. More...
 
core::nanoseconds_t stream_timestamp_2_ns (packet::stream_timestamp_t sts_duration) const
 Convert stream timestamp to nanoseconds. More...
 
double stream_timestamp_2_ms (packet::stream_timestamp_t sts_duration) const
 Convert stream timestamp to milliseconds. More...
 
packet::stream_timestamp_diff_t ns_2_stream_timestamp_delta (core::nanoseconds_t ns_delta) const
 Convert nanoseconds delta to stream timestamp delta. More...
 
core::nanoseconds_t stream_timestamp_delta_2_ns (packet::stream_timestamp_diff_t sts_delta) const
 Convert stream timestamp delta to nanoseconds delta. More...
 
double stream_timestamp_delta_2_ms (packet::stream_timestamp_diff_t sts_duration) const
 Convert stream timestamp delta to milliseconds. More...
 

Convert byte size

packet::stream_timestamp_t bytes_2_stream_timestamp (size_t n_bytes) const
 Convert byte size to stream timestamp. More...
 
size_t stream_timestamp_2_bytes (packet::stream_timestamp_t duration) const
 Convert stream timestamp to byte size. More...
 
core::nanoseconds_t bytes_2_ns (size_t n_bytes) const
 Convert byte size to nanosecond duration. More...
 
size_t ns_2_bytes (core::nanoseconds_t duration) const
 Convert nanosecond duration to byte size. More...
 

Detailed Description

Sample specification. Describes sample rate and channels.

Definition at line 30 of file sample_spec.h.

Constructor & Destructor Documentation

◆ SampleSpec() [1/3]

roc::audio::SampleSpec::SampleSpec ( )

Construct empty specification.

◆ SampleSpec() [2/3]

roc::audio::SampleSpec::SampleSpec ( size_t  sample_rate,
PcmFormat  pcm_fmt,
const ChannelSet channel_set 
)

Construct specification with parameters.

Note
This constructor sets sample_format() to SampleFormat_Pcm.

◆ SampleSpec() [3/3]

roc::audio::SampleSpec::SampleSpec ( size_t  sample_rate,
PcmFormat  pcm_fmt,
ChannelLayout  channel_layout,
ChannelOrder  channel_order,
ChannelMask  channel_mask 
)

Construct specification with parameters.

Remarks
This is a convenient overload for the case when 32-bit mask is enough to describe channels. Otherwise, use overload that accepts ChannelSet.
Note
This constructor sets sample_format() to SampleFormat_Pcm.

Member Function Documentation

◆ bytes_2_ns()

core::nanoseconds_t roc::audio::SampleSpec::bytes_2_ns ( size_t  n_bytes) const

Convert byte size to nanosecond duration.

Precondition
sample_format() should be PCM.

◆ bytes_2_stream_timestamp()

packet::stream_timestamp_t roc::audio::SampleSpec::bytes_2_stream_timestamp ( size_t  n_bytes) const

Convert byte size to stream timestamp.

Precondition
sample_format() should be PCM.

◆ channel_set() [1/2]

ChannelSet& roc::audio::SampleSpec::channel_set ( )

Get mutable channel set.

◆ channel_set() [2/2]

const ChannelSet& roc::audio::SampleSpec::channel_set ( ) const

Get channel set.

Remarks
Defines sample channels (layout and numbers).

◆ clear()

void roc::audio::SampleSpec::clear ( )

Unset all fields.

◆ fract_samples_overall_2_ns()

core::nanoseconds_t roc::audio::SampleSpec::fract_samples_overall_2_ns ( float  n_samples) const

Convert number of samples (possibly non-integer) to nanoseconds.

Note
In case of overflow, result is saturated.

◆ fract_samples_per_chan_2_ns()

core::nanoseconds_t roc::audio::SampleSpec::fract_samples_per_chan_2_ns ( float  n_samples) const

Convert (possibly fractional) number samples per channel to nanoseconds duration.

Note
In case of overflow, result is saturated.

◆ is_empty()

bool roc::audio::SampleSpec::is_empty ( ) const

Check if sample spec has a zero rate, empty channel set, and invalid_format.

◆ is_raw()

bool roc::audio::SampleSpec::is_raw ( ) const

Check if samples are in raw format.

Returns
true if sample_format() is SampleFormat_Pcm and pcm_format() is Sample_RawFormat (32-bit native-endian floats).

◆ is_valid()

bool roc::audio::SampleSpec::is_valid ( ) const

Check if sample spec has non-zero rate and valid channel set.

◆ ns_2_bytes()

size_t roc::audio::SampleSpec::ns_2_bytes ( core::nanoseconds_t  duration) const

Convert nanosecond duration to byte size.

Precondition
sample_format() should be PCM.

◆ ns_2_samples_overall()

size_t roc::audio::SampleSpec::ns_2_samples_overall ( core::nanoseconds_t  ns_duration) const

Convert nanoseconds duration to number of samples for all channels.

Precondition
ns_duration should not be negative.
Postcondition
result is always multiple of number of channels.
Note
In case of overflow, result is saturated.

◆ ns_2_samples_per_chan()

size_t roc::audio::SampleSpec::ns_2_samples_per_chan ( core::nanoseconds_t  ns_duration) const

Convert nanoseconds duration to number of samples per channel.

Precondition
ns_duration should not be negative.
Note
In case of overflow, result is saturated.

◆ ns_2_stream_timestamp()

packet::stream_timestamp_t roc::audio::SampleSpec::ns_2_stream_timestamp ( core::nanoseconds_t  ns_duration) const

Convert nanoseconds delta to stream timestamp.

Precondition
ns_duration should not be negative.
Remarks
Same as ns_2_samples_per_chan(), but with stream_timestamp_t instead of size_t.

◆ ns_2_stream_timestamp_delta()

packet::stream_timestamp_diff_t roc::audio::SampleSpec::ns_2_stream_timestamp_delta ( core::nanoseconds_t  ns_delta) const

Convert nanoseconds delta to stream timestamp delta.

Remarks
Same as ns_2_samples_per_chan(), but supports negative deltas.

◆ num_channels()

size_t roc::audio::SampleSpec::num_channels ( ) const

Get number enabled channels in channel set.

Remarks
Shorthand for channel_set().num_channels().

◆ operator!=()

bool roc::audio::SampleSpec::operator!= ( const SampleSpec other) const

Check two specifications for equality.

◆ operator==()

bool roc::audio::SampleSpec::operator== ( const SampleSpec other) const

Check two specifications for equality.

◆ pcm_format()

PcmFormat roc::audio::SampleSpec::pcm_format ( ) const

Get PCM format.

Remarks
When sample_format() is set to SampleFormat_Pcm, defines what exact PCM coding and endian are used.

◆ sample_format()

SampleFormat roc::audio::SampleSpec::sample_format ( ) const

Get sample format.

Remarks
Defines how samples are represented in memory. When set to SampleFormat_Pcm, pcm_format() defines what exact PCM coding and endian are used.

◆ sample_rate()

size_t roc::audio::SampleSpec::sample_rate ( ) const

Get sample rate.

Remarks
Defines sample frequency (number of samples per second).

◆ samples_overall_2_ns()

core::nanoseconds_t roc::audio::SampleSpec::samples_overall_2_ns ( size_t  n_samples) const

Convert number of samples for all channels to nanoseconds duration.

Precondition
n_samples should be multiple of number of channels.
Note
In case of overflow, result is saturated.

◆ samples_per_chan_2_ns()

core::nanoseconds_t roc::audio::SampleSpec::samples_per_chan_2_ns ( size_t  n_samples) const

Convert number of samples per channel to nanoseconds duration.

Note
In case of overflow, result is saturated.

◆ set_channel_set()

void roc::audio::SampleSpec::set_channel_set ( const ChannelSet channel_set)

Set channel set.

◆ set_pcm_format()

void roc::audio::SampleSpec::set_pcm_format ( PcmFormat  pcm_fmt)

Set PCM format.

◆ set_sample_format()

void roc::audio::SampleSpec::set_sample_format ( SampleFormat  sample_fmt)

Set sample format.

◆ set_sample_rate()

void roc::audio::SampleSpec::set_sample_rate ( size_t  sample_rate)

Set sample rate.

◆ stream_timestamp_2_bytes()

size_t roc::audio::SampleSpec::stream_timestamp_2_bytes ( packet::stream_timestamp_t  duration) const

Convert stream timestamp to byte size.

Precondition
sample_format() should be PCM.

◆ stream_timestamp_2_ms()

double roc::audio::SampleSpec::stream_timestamp_2_ms ( packet::stream_timestamp_t  sts_duration) const

Convert stream timestamp to milliseconds.

◆ stream_timestamp_2_ns()

core::nanoseconds_t roc::audio::SampleSpec::stream_timestamp_2_ns ( packet::stream_timestamp_t  sts_duration) const

Convert stream timestamp to nanoseconds.

Remarks
Same as samples_per_chan_2_ns(), but with stream_timestamp_t instead of size_t.

◆ stream_timestamp_delta_2_ms()

double roc::audio::SampleSpec::stream_timestamp_delta_2_ms ( packet::stream_timestamp_diff_t  sts_duration) const

Convert stream timestamp delta to milliseconds.

◆ stream_timestamp_delta_2_ns()

core::nanoseconds_t roc::audio::SampleSpec::stream_timestamp_delta_2_ns ( packet::stream_timestamp_diff_t  sts_delta) const

Convert stream timestamp delta to nanoseconds delta.

Remarks
Same as samples_per_chan_2_ns(), but supports negative deltas.

◆ use_defaults()

void roc::audio::SampleSpec::use_defaults ( PcmFormat  default_pcm_fmt,
ChannelLayout  default_channel_layout,
ChannelOrder  default_channel_order,
ChannelMask  default_channel_mask,
size_t  default_sample_rate 
)

Set missing fields from provided defaults.

Remarks
Updates only those fields which don't have values, with corresponding values provided as arguments.

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