ESPHome  2025.2.0
Public Member Functions | Protected Attributes
esphome::audio::AudioStreamInfo Class Reference

#include <audio.h>

Public Member Functions

 AudioStreamInfo ()
 
 AudioStreamInfo (uint8_t bits_per_sample, uint8_t channels, uint32_t sample_rate)
 
uint8_t get_bits_per_sample () const
 
uint8_t get_channels () const
 
uint32_t get_sample_rate () const
 
uint32_t bytes_to_ms (size_t bytes) const
 Convert bytes to duration in milliseconds. More...
 
uint32_t bytes_to_frames (size_t bytes) const
 Convert bytes to frames. More...
 
uint32_t bytes_to_samples (size_t bytes) const
 Convert bytes to samples. More...
 
size_t frames_to_bytes (uint32_t frames) const
 Converts frames to bytes. More...
 
size_t samples_to_bytes (uint32_t samples) const
 Converts samples to bytes. More...
 
uint32_t ms_to_frames (uint32_t ms) const
 Converts duration to frames. More...
 
uint32_t ms_to_samples (uint32_t ms) const
 Converts duration to samples. More...
 
size_t ms_to_bytes (uint32_t ms) const
 Converts duration to bytes. More...
 
uint32_t frames_to_microseconds (uint32_t frames) const
 Computes the duration, in microseconds, the given amount of frames represents. More...
 
uint32_t frames_to_milliseconds_with_remainder (uint32_t *frames) const
 Computes the duration, in milliseconds, the given amount of frames represents. More...
 
bool operator== (const AudioStreamInfo &rhs) const
 
bool operator!= (const AudioStreamInfo &rhs) const
 

Protected Attributes

uint8_t bits_per_sample_
 
uint8_t channels_
 
uint32_t sample_rate_
 
uint32_t ms_sample_rate_gcd_
 
size_t bytes_per_sample_
 

Detailed Description

Definition at line 11 of file audio.h.

Constructor & Destructor Documentation

◆ AudioStreamInfo() [1/2]

esphome::audio::AudioStreamInfo::AudioStreamInfo ( )
inline

Definition at line 24 of file audio.h.

◆ AudioStreamInfo() [2/2]

esphome::audio::AudioStreamInfo::AudioStreamInfo ( uint8_t  bits_per_sample,
uint8_t  channels,
uint32_t  sample_rate 
)

Definition at line 16 of file audio.cpp.

Member Function Documentation

◆ bytes_to_frames()

uint32_t esphome::audio::AudioStreamInfo::bytes_to_frames ( size_t  bytes) const
inline

Convert bytes to frames.

Parameters
bytesNumber of bytes to convert
Returns
Audio frames that will store bytes bytes.

Definition at line 43 of file audio.h.

◆ bytes_to_ms()

uint32_t esphome::audio::AudioStreamInfo::bytes_to_ms ( size_t  bytes) const
inline

Convert bytes to duration in milliseconds.

Parameters
bytesNumber of bytes to convert
Returns
Duration in milliseconds that will store bytes bytes of audio. May round down for certain sample rates or values of bytes.

Definition at line 36 of file audio.h.

◆ bytes_to_samples()

uint32_t esphome::audio::AudioStreamInfo::bytes_to_samples ( size_t  bytes) const
inline

Convert bytes to samples.

Parameters
bytesNumber of bytes to convert
Returns
Audio samples that will store bytes bytes.

Definition at line 48 of file audio.h.

◆ frames_to_bytes()

size_t esphome::audio::AudioStreamInfo::frames_to_bytes ( uint32_t  frames) const
inline

Converts frames to bytes.

Parameters
framesNumber of frames to convert.
Returns
Number of bytes that will store frames frames of audio.

Definition at line 53 of file audio.h.

◆ frames_to_microseconds()

uint32_t esphome::audio::AudioStreamInfo::frames_to_microseconds ( uint32_t  frames) const

Computes the duration, in microseconds, the given amount of frames represents.

Parameters
framesNumber of audio frames
Returns
Duration in microseconds frames respresents. May be slightly inaccurate due to integer divison rounding for certain sample rates.

Definition at line 22 of file audio.cpp.

◆ frames_to_milliseconds_with_remainder()

uint32_t esphome::audio::AudioStreamInfo::frames_to_milliseconds_with_remainder ( uint32_t *  frames) const

Computes the duration, in milliseconds, the given amount of frames represents.

Avoids accumulating rounding errors by updating frames with the remainder after converting.

Parameters
framesPointer to uint32_t with the number of audio frames. Replaced with the remainder.
Returns
Duration in milliseconds frames represents. Always less than or equal to the actual value due to rounding.

Definition at line 26 of file audio.cpp.

◆ get_bits_per_sample()

uint8_t esphome::audio::AudioStreamInfo::get_bits_per_sample ( ) const
inline

Definition at line 28 of file audio.h.

◆ get_channels()

uint8_t esphome::audio::AudioStreamInfo::get_channels ( ) const
inline

Definition at line 29 of file audio.h.

◆ get_sample_rate()

uint32_t esphome::audio::AudioStreamInfo::get_sample_rate ( ) const
inline

Definition at line 30 of file audio.h.

◆ ms_to_bytes()

size_t esphome::audio::AudioStreamInfo::ms_to_bytes ( uint32_t  ms) const
inline

Converts duration to bytes.

May round down for certain sample rates.

Parameters
msDuration in milliseconds
Returns
Bytes that will store ms milliseconds of audio. May round down for certain sample rates.

Definition at line 73 of file audio.h.

◆ ms_to_frames()

uint32_t esphome::audio::AudioStreamInfo::ms_to_frames ( uint32_t  ms) const
inline

Converts duration to frames.

Parameters
msDuration in milliseconds
Returns
Audio frames that will store ms milliseconds of audio. May round down for certain sample rates.

Definition at line 63 of file audio.h.

◆ ms_to_samples()

uint32_t esphome::audio::AudioStreamInfo::ms_to_samples ( uint32_t  ms) const
inline

Converts duration to samples.

Parameters
msDuration in milliseconds
Returns
Audio samples that will store ms milliseconds of audio. May round down for certain sample rates.

Definition at line 68 of file audio.h.

◆ operator!=()

bool esphome::audio::AudioStreamInfo::operator!= ( const AudioStreamInfo rhs) const
inline

Definition at line 92 of file audio.h.

◆ operator==()

bool esphome::audio::AudioStreamInfo::operator== ( const AudioStreamInfo rhs) const

Definition at line 35 of file audio.cpp.

◆ samples_to_bytes()

size_t esphome::audio::AudioStreamInfo::samples_to_bytes ( uint32_t  samples) const
inline

Converts samples to bytes.

Parameters
samplesNumber of samples to convert.
Returns
Number of bytes that will store samples samples of audio.

Definition at line 58 of file audio.h.

Field Documentation

◆ bits_per_sample_

uint8_t esphome::audio::AudioStreamInfo::bits_per_sample_
protected

Definition at line 95 of file audio.h.

◆ bytes_per_sample_

size_t esphome::audio::AudioStreamInfo::bytes_per_sample_
protected

Definition at line 105 of file audio.h.

◆ channels_

uint8_t esphome::audio::AudioStreamInfo::channels_
protected

Definition at line 96 of file audio.h.

◆ ms_sample_rate_gcd_

uint32_t esphome::audio::AudioStreamInfo::ms_sample_rate_gcd_
protected

Definition at line 101 of file audio.h.

◆ sample_rate_

uint32_t esphome::audio::AudioStreamInfo::sample_rate_
protected

Definition at line 97 of file audio.h.


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