5#if defined(USE_ESP32) && defined(USE_I2S_AUDIO_SPDIF_MODE)
10#include <freertos/FreeRTOS.h>
17static constexpr uint8_t SPDIF_BITS_PER_SAMPLE = 64;
19static constexpr uint16_t SPDIF_BLOCK_SAMPLES = 192;
22static constexpr uint8_t EMULATED_BMC_BITS_PER_SAMPLE = SPDIF_BITS_PER_SAMPLE * 2;
23static constexpr uint16_t SPDIF_BLOCK_SIZE_BYTES = SPDIF_BLOCK_SAMPLES * (EMULATED_BMC_BITS_PER_SAMPLE / 8);
24static constexpr uint32_t SPDIF_BLOCK_SIZE_U32 = SPDIF_BLOCK_SIZE_BYTES /
sizeof(
uint32_t);
26static constexpr uint32_t SPDIF_BLOCK_I2S_FRAMES = SPDIF_BLOCK_SIZE_BYTES / 8;
28static constexpr uint16_t SPDIF_PCM_BYTES_PER_BLOCK = SPDIF_BLOCK_SAMPLES * 2 * 2;
74 esp_err_t
write(
const uint8_t *src,
size_t size, TickType_t ticks_to_wait,
uint32_t *blocks_sent =
nullptr,
75 size_t *bytes_consumed =
nullptr);
uint32_t * spdif_block_ptr_
std::unique_ptr< uint32_t[]> spdif_block_buf_
bool is_preload_mode() const
Check if currently in preload mode.
esp_err_t send_block_(TickType_t ticks_to_wait)
Send the completed block via the appropriate callback.
uint32_t get_sample_rate() const
Get the currently configured sample rate.
size_t get_pending_pcm_bytes() const
Get the number of PCM bytes currently pending in the partial block buffer.
std::array< uint8_t, 24 > channel_status_
void * preload_callback_ctx_
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.
void set_sample_rate(uint32_t sample_rate)
Set the sample rate for Channel Status Block encoding.
SPDIFBlockCallback write_callback_
SPDIFBlockCallback preload_callback_
void reset()
Reset the SPDIF block buffer and position tracking, discarding any partial block.
void build_channel_status_()
Build the channel status block from current configuration.
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,...
bool setup()
Initialize the SPDIF working buffer.
ESPHOME_ALWAYS_INLINE bool get_channel_status_bit_(uint8_t frame) const
Get the channel status bit for a specific frame.
void set_write_callback(SPDIFBlockCallback callback, void *user_ctx)
Set callback for normal writes (used when channel is running)
uint32_t get_pending_frames() const
Get the number of PCM frames currently pending in the partial block buffer.
void * write_callback_ctx_
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 instea...
HOT void encode_sample_(const uint8_t *pcm_sample)
Encode a single 16-bit PCM sample into the current block position.
bool has_pending_data() const
Check if there is a partial block pending.
esp_err_t(*)(void *user_ctx, uint32_t *data, size_t size, TickType_t ticks_to_wait) SPDIFBlockCallback
Callback signature for block completion (raw function pointer for minimal overhead)