ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
spdif_encoder.h
Go to the documentation of this file.
1#pragma once
2
4
5#if defined(USE_ESP32) && defined(USE_I2S_AUDIO_SPDIF_MODE)
6
7#include <array>
8#include <cstdint>
9#include <memory>
10#include <freertos/FreeRTOS.h>
11#include "esp_err.h"
13
14namespace esphome::i2s_audio {
15
16// A SPDIF sample is 64-bits
17static constexpr uint8_t SPDIF_BITS_PER_SAMPLE = 64;
18// Number of samples in a SPDIF block
19static constexpr uint16_t SPDIF_BLOCK_SAMPLES = 192;
20// To emulate bi-phase mark code (BMC) (aka differential Manchester encoding) we send twice
21// as many bits per sample so that we can generate the transitions this encoding requires.
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); // 3072 bytes / 4 = 768
25// I2S frame count for one SPDIF block (for new driver where frame = 8 bytes for 32-bit stereo)
26static constexpr uint32_t SPDIF_BLOCK_I2S_FRAMES = SPDIF_BLOCK_SIZE_BYTES / 8; // 3072 / 8 = 384 frames
27// PCM bytes needed for one complete SPDIF block (192 stereo frames * 2 bytes per sample * 2 channels)
28static constexpr uint16_t SPDIF_PCM_BYTES_PER_BLOCK = SPDIF_BLOCK_SAMPLES * 2 * 2; // = 768 bytes
29
36using SPDIFBlockCallback = esp_err_t (*)(void *user_ctx, uint32_t *data, size_t size, TickType_t ticks_to_wait);
37
39 public:
42 bool setup();
43
47 void set_write_callback(SPDIFBlockCallback callback, void *user_ctx) {
48 this->write_callback_ = callback;
49 this->write_callback_ctx_ = user_ctx;
50 }
51
55 void set_preload_callback(SPDIFBlockCallback callback, void *user_ctx) {
56 this->preload_callback_ = callback;
57 this->preload_callback_ctx_ = user_ctx;
58 }
59
62 void set_preload_mode(bool preload) { this->preload_mode_ = preload; }
63
65 bool is_preload_mode() const { return this->preload_mode_; }
66
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);
76
79 size_t get_pending_pcm_bytes() const;
80
83 uint32_t get_pending_frames() const { return this->get_pending_pcm_bytes() / 4; }
84
86 bool has_pending_data() const { return this->spdif_block_ptr_ != this->spdif_block_buf_.get(); }
87
92 esp_err_t flush_with_silence(TickType_t ticks_to_wait);
93
95 void reset();
96
100 void set_sample_rate(uint32_t sample_rate);
101
103 uint32_t get_sample_rate() const { return this->sample_rate_; }
104
105 protected:
107 HOT void encode_sample_(const uint8_t *pcm_sample);
108
110 esp_err_t send_block_(TickType_t ticks_to_wait);
111
114
118 ESPHOME_ALWAYS_INLINE inline bool get_channel_status_bit_(uint8_t frame) const {
119 // Channel status is 192 bits transmitted over 192 frames
120 // Bit N is transmitted in frame N, LSB-first within each byte
121 return (this->channel_status_[frame >> 3] >> (frame & 7)) & 1;
122 }
123
124 // Member ordering optimized to minimize padding (largest alignment first)
125
126 // 4-byte aligned members (pointers and uint32_t)
129 void *write_callback_ctx_{nullptr};
130 void *preload_callback_ctx_{nullptr};
131 std::unique_ptr<uint32_t[]> spdif_block_buf_; // Working buffer for SPDIF block (heap allocated)
132 uint32_t *spdif_block_ptr_{nullptr}; // Current position in block buffer
133 uint32_t sample_rate_{48000}; // Sample rate for Channel Status Block encoding
134
135 // 1-byte aligned members (grouped together to avoid internal padding)
136 uint8_t frame_in_block_{0}; // 0-191, tracks stereo frame position within block
137 bool is_left_channel_{true}; // Alternates L/R for stereo samples
138 bool preload_mode_{false}; // Whether to use preload callback vs write callback
139
140 // Channel Status Block (192 bits = 24 bytes, transmitted over 192 frames)
141 // Placed last since std::array<uint8_t> has 1-byte alignment
142 std::array<uint8_t, 24> channel_status_{};
143};
144
145} // namespace esphome::i2s_audio
146
147#endif // USE_I2S_AUDIO_SPDIF_MODE
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_
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 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 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)
uint16_t size
Definition helpers.cpp:25
static void uint32_t