ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
esphome::i2s_audio::SPDIFEncoder Class Reference

#include <spdif_encoder.h>

Public Member Functions

bool setup ()
 Initialize the SPDIF working buffer.
 
void set_write_callback (SPDIFBlockCallback callback, void *user_ctx)
 Set callback for normal writes (used when channel is running)
 
void set_preload_callback (SPDIFBlockCallback callback, void *user_ctx)
 Set callback for preload writes (used when preloading to DMA before enabling channel)
 
void set_preload_mode (bool preload)
 Enable or disable preload mode When in preload mode, completed blocks use the preload callback instead of write callback.
 
bool is_preload_mode () const
 Check if currently in preload mode.
 
esp_err_t write (const uint8_t *src, size_t size, TickType_t ticks_to_wait, uint32_t *blocks_sent=nullptr, size_t *bytes_consumed=nullptr)
 Convert PCM audio data to SPDIF BMC encoded data.
 
size_t get_pending_pcm_bytes () const
 Get the number of PCM bytes currently pending in the partial block buffer.
 
uint32_t get_pending_frames () const
 Get the number of PCM frames currently pending in the partial block buffer.
 
bool has_pending_data () const
 Check if there is a partial block pending.
 
esp_err_t flush_with_silence (TickType_t ticks_to_wait)
 Emit one complete SPDIF block: pad any pending partial block with silence and send, or send a full silence block if nothing is pending.
 
void reset ()
 Reset the SPDIF block buffer and position tracking, discarding any partial block.
 
void set_sample_rate (uint32_t sample_rate)
 Set the sample rate for Channel Status Block encoding.
 
uint32_t get_sample_rate () const
 Get the currently configured sample rate.
 

Protected Member Functions

HOT void encode_sample_ (const uint8_t *pcm_sample)
 Encode a single 16-bit PCM sample into the current block position.
 
esp_err_t send_block_ (TickType_t ticks_to_wait)
 Send the completed block via the appropriate callback.
 
void build_channel_status_ ()
 Build the channel status block from current configuration.
 
ESPHOME_ALWAYS_INLINE bool get_channel_status_bit_ (uint8_t frame) const
 Get the channel status bit for a specific frame.
 

Protected Attributes

SPDIFBlockCallback write_callback_ {nullptr}
 
SPDIFBlockCallback preload_callback_ {nullptr}
 
void * write_callback_ctx_ {nullptr}
 
void * preload_callback_ctx_ {nullptr}
 
std::unique_ptr< uint32_t[]> spdif_block_buf_
 
uint32_tspdif_block_ptr_ {nullptr}
 
uint32_t sample_rate_ {48000}
 
uint8_t frame_in_block_ {0}
 
bool is_left_channel_ {true}
 
bool preload_mode_ {false}
 
std::array< uint8_t, 24 > channel_status_ {}
 

Detailed Description

Definition at line 38 of file spdif_encoder.h.

Member Function Documentation

◆ build_channel_status_()

void esphome::i2s_audio::SPDIFEncoder::build_channel_status_ ( )
protected

Build the channel status block from current configuration.

Definition at line 87 of file spdif_encoder.cpp.

◆ encode_sample_()

HOT void esphome::i2s_audio::SPDIFEncoder::encode_sample_ ( const uint8_t * pcm_sample)
protected

Encode a single 16-bit PCM sample into the current block position.

Definition at line 146 of file spdif_encoder.cpp.

◆ flush_with_silence()

esp_err_t esphome::i2s_audio::SPDIFEncoder::flush_with_silence ( TickType_t ticks_to_wait)

Emit one complete SPDIF block: pad any pending partial block with silence and send, or send a full silence block if nothing is pending.

Always produces exactly one block on success.

Parameters
ticks_to_waitTimeout for blocking writes
Returns
esp_err_t as returned from the callback

Definition at line 360 of file spdif_encoder.cpp.

◆ get_channel_status_bit_()

ESPHOME_ALWAYS_INLINE bool esphome::i2s_audio::SPDIFEncoder::get_channel_status_bit_ ( uint8_t frame) const
inlineprotected

Get the channel status bit for a specific frame.

Parameters
frameFrame number (0-191)
Returns
The C bit value for this frame

Definition at line 118 of file spdif_encoder.h.

◆ get_pending_frames()

uint32_t esphome::i2s_audio::SPDIFEncoder::get_pending_frames ( ) const
inline

Get the number of PCM frames currently pending in the partial block buffer.

Returns
Number of pending PCM frames (0 to SPDIF_BLOCK_SAMPLES - 1)

Definition at line 83 of file spdif_encoder.h.

◆ get_pending_pcm_bytes()

size_t esphome::i2s_audio::SPDIFEncoder::get_pending_pcm_bytes ( ) const

Get the number of PCM bytes currently pending in the partial block buffer.

Returns
Number of pending PCM bytes (0 to SPDIF_PCM_BYTES_PER_BLOCK - 1)

Definition at line 298 of file spdif_encoder.cpp.

◆ get_sample_rate()

uint32_t esphome::i2s_audio::SPDIFEncoder::get_sample_rate ( ) const
inline

Get the currently configured sample rate.

Definition at line 103 of file spdif_encoder.h.

◆ has_pending_data()

bool esphome::i2s_audio::SPDIFEncoder::has_pending_data ( ) const
inline

Check if there is a partial block pending.

Definition at line 86 of file spdif_encoder.h.

◆ is_preload_mode()

bool esphome::i2s_audio::SPDIFEncoder::is_preload_mode ( ) const
inline

Check if currently in preload mode.

Definition at line 65 of file spdif_encoder.h.

◆ reset()

void esphome::i2s_audio::SPDIFEncoder::reset ( )

Reset the SPDIF block buffer and position tracking, discarding any partial block.

Definition at line 73 of file spdif_encoder.cpp.

◆ send_block_()

esp_err_t esphome::i2s_audio::SPDIFEncoder::send_block_ ( TickType_t ticks_to_wait)
protected

Send the completed block via the appropriate callback.

Definition at line 271 of file spdif_encoder.cpp.

◆ set_preload_callback()

void esphome::i2s_audio::SPDIFEncoder::set_preload_callback ( SPDIFBlockCallback callback,
void * user_ctx )
inline

Set callback for preload writes (used when preloading to DMA before enabling channel)

Parameters
callbackFunction pointer to call when a block is ready for preload
user_ctxContext pointer passed to callback (typically 'this' pointer of speaker)

Definition at line 55 of file spdif_encoder.h.

◆ set_preload_mode()

void esphome::i2s_audio::SPDIFEncoder::set_preload_mode ( bool preload)
inline

Enable or disable preload mode When in preload mode, completed blocks use the preload callback instead of write callback.

Definition at line 62 of file spdif_encoder.h.

◆ set_sample_rate()

void esphome::i2s_audio::SPDIFEncoder::set_sample_rate ( uint32_t sample_rate)

Set the sample rate for Channel Status Block encoding.

Parameters
sample_rateSample rate in Hz (e.g., 44100, 48000, 96000) Call this before writing audio data to ensure correct channel status.

Definition at line 79 of file spdif_encoder.cpp.

◆ set_write_callback()

void esphome::i2s_audio::SPDIFEncoder::set_write_callback ( SPDIFBlockCallback callback,
void * user_ctx )
inline

Set callback for normal writes (used when channel is running)

Parameters
callbackFunction pointer to call when a block is ready
user_ctxContext pointer passed to callback (typically 'this' pointer of speaker)

Definition at line 47 of file spdif_encoder.h.

◆ setup()

bool esphome::i2s_audio::SPDIFEncoder::setup ( )

Initialize the SPDIF working buffer.

Returns
true if setup was successful, false if allocation failed

Definition at line 58 of file spdif_encoder.cpp.

◆ write()

HOT esp_err_t esphome::i2s_audio::SPDIFEncoder::write ( const uint8_t * src,
size_t size,
TickType_t ticks_to_wait,
uint32_t * blocks_sent = nullptr,
size_t * bytes_consumed = nullptr )

Convert PCM audio data to SPDIF BMC encoded data.

Parameters
srcSource PCM audio data (16-bit stereo)
sizeSize of source data in bytes
ticks_to_waitTimeout for blocking writes
blocks_sentOptional pointer to receive the number of complete SPDIF blocks sent
bytes_consumedOptional pointer to receive the number of PCM bytes consumed from src
Returns
esp_err_t as returned from the callback

Definition at line 309 of file spdif_encoder.cpp.

Field Documentation

◆ channel_status_

std::array<uint8_t, 24> esphome::i2s_audio::SPDIFEncoder::channel_status_ {}
protected

Definition at line 142 of file spdif_encoder.h.

◆ frame_in_block_

uint8_t esphome::i2s_audio::SPDIFEncoder::frame_in_block_ {0}
protected

Definition at line 136 of file spdif_encoder.h.

◆ is_left_channel_

bool esphome::i2s_audio::SPDIFEncoder::is_left_channel_ {true}
protected

Definition at line 137 of file spdif_encoder.h.

◆ preload_callback_

SPDIFBlockCallback esphome::i2s_audio::SPDIFEncoder::preload_callback_ {nullptr}
protected

Definition at line 128 of file spdif_encoder.h.

◆ preload_callback_ctx_

void* esphome::i2s_audio::SPDIFEncoder::preload_callback_ctx_ {nullptr}
protected

Definition at line 130 of file spdif_encoder.h.

◆ preload_mode_

bool esphome::i2s_audio::SPDIFEncoder::preload_mode_ {false}
protected

Definition at line 138 of file spdif_encoder.h.

◆ sample_rate_

uint32_t esphome::i2s_audio::SPDIFEncoder::sample_rate_ {48000}
protected

Definition at line 133 of file spdif_encoder.h.

◆ spdif_block_buf_

std::unique_ptr<uint32_t[]> esphome::i2s_audio::SPDIFEncoder::spdif_block_buf_
protected

Definition at line 131 of file spdif_encoder.h.

◆ spdif_block_ptr_

uint32_t* esphome::i2s_audio::SPDIFEncoder::spdif_block_ptr_ {nullptr}
protected

Definition at line 132 of file spdif_encoder.h.

◆ write_callback_

SPDIFBlockCallback esphome::i2s_audio::SPDIFEncoder::write_callback_ {nullptr}
protected

Definition at line 127 of file spdif_encoder.h.

◆ write_callback_ctx_

void* esphome::i2s_audio::SPDIFEncoder::write_callback_ctx_ {nullptr}
protected

Definition at line 129 of file spdif_encoder.h.


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