johnboiles
(John Boiles)
January 16, 2025, 11:56pm
1
Using my SPDIF digital audio PR I non-intrusively added a Toslink digital audio port to my Home Assistant Voice Preview Edition via the Grove port. The wiring was super simple – just a Grove connector wire I cut and soldered to an optical Toslink transmitter .
I’m now streaming perfect 48KHz digital audio from Music Assistant to our office’s high-end MSB Technology DAC.
This is the config I’m using:
packages:
remote_package_shorthand: github://esphome/home-assistant-voice-pe/home-assistant-voice.yaml@dev
esphome:
name: home-assistant-voice
friendly_name: Home Assistant Voice
name_add_mac_suffix: false
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: true # Required for hidden AP
external_components:
- source: github://pr#8065
components: [ i2s_audio, spdif_audio ]
refresh: 0s
i2s_audio:
- id: !remove i2s_output
- id: i2s_output_spdif
speaker:
- id: !remove i2s_audio_speaker
- id: spdif_speaker
platform: spdif_audio
i2s_audio_id: i2s_output_spdif
data_pin: GPIO1 # Grove port SDA pin
sample_rate: 48000
timeout: never
buffer_duration: 80ms
fill_silence: true
debug: true
switch:
- platform: gpio
pin: GPIO46
id: grove_port_power
restore_mode: ALWAYS_ON
setup_priority: 1001
media_player:
- id: !extend nabu_media_player
name: Media Player
audio_dac: !remove
volume_increment: !remove
volume_min: !remove
volume_max: !remove
You’ll see in this config it’s using two of my PRs to support this. Hopefully this can all get merged in before too long!
esphome:dev
← johnboiles:spdif_speaker
opened 11:10AM - 11 Jan 25 UTC
# What does this implement/fix?
This implements a SPDIF speaker. This is port… ed from [this PR](https://github.com/gnumpi/esphome_audio/pull/59) and is especially useful with the new `nabu_media_player` which can use any speaker as an output. I'm using this via the Home Assistant Voice Preview Edition Grove port to output digital audio to my [MSB Technology](https://msbtechnology.com/) DAC.
All you need to try this is something that can receive optical audio (e.g. soundbar, AV receiver) and an LED (even the debug LED directly on your dev board). Just put your optical cable up to the LED and it plays audio! I've also tried some [real optical transmitter modules](https://www.aliexpress.us/item/2255800972802688.html) (basically just an LED in a connector).
<img width="350" alt="using simple LED" src="https://github.com/user-attachments/assets/0e348c4a-703a-4e40-bd01-79cad92ef3ae">
<img width="350" alt="using Toslink connector" src="https://github.com/user-attachments/assets/d92dac03-15e4-4ab9-9c2c-4bfe2b22a29d">
For the Home Assistant Voice Preview Edition you can directly wire in the [optical transmitters](https://www.aliexpress.us/item/2255800972802688.html) to the Grove connector (config for that also included below)
<img width="350" alt="HA Voice PE with MSB Technology DAC" src="https://github.com/user-attachments/assets/b746f935-9c82-4e42-831d-fbf56402f7d9">
<img width="350" alt="HA Voice PE top view" src="https://github.com/user-attachments/assets/19f6e8e1-adbe-40ba-8257-e32be289a6c4">
<img width="350" alt="Optical transmitter connected via Grove port" src="https://github.com/user-attachments/assets/3581c395-7a69-444b-babe-73bd979bdadd">
<img width="350" alt="Optical transmitter to Grove wiring" src="https://github.com/user-attachments/assets/c2a9117d-3905-4aab-a438-0a38f9de70f2">
This uses (abuses?) the I2S peripheral to stream out a SPDIF bitstream so this had to inherit from `I2SAudioComponent` to avoid an I2S port collision. But I don't think this can go inside `i2s_audio` since AFAIK there's not a way for `i2s_audio` to have two separate speaker components (though LMK if there's a way to handle that!).
## Types of changes
- [ ] Bugfix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Code quality improvements to existing code or addition of tests
- [ ] Other
**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):**
I will happily do this once the implementation is stable.
## Test Environment
- [ ] ESP32
- [x] ESP32 IDF
- [ ] ESP8266
- [ ] RP2040
- [ ] BK72xx
- [ ] RTL87xx
## Example entry for `config.yaml`:
For Home Assistant Voice Preview Edition (using the Grove port):
```yaml
packages:
remote_package_shorthand: github://esphome/home-assistant-voice-pe/home-assistant-voice.yaml@dev
esphome:
name: home-assistant-voice
friendly_name: Home Assistant Voice
name_add_mac_suffix: false
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
fast_connect: true # Required for hidden AP
external_components:
- source: github://pr#8065
components: [ i2s_audio, spdif_audio ]
refresh: 0s
i2s_audio:
- id: !remove i2s_output
- id: i2s_output_spdif
speaker:
- id: !remove i2s_audio_speaker
- id: spdif_speaker
platform: spdif_audio
i2s_audio_id: i2s_output_spdif
data_pin: GPIO1 # Grove port SDA pin
sample_rate: 48000
timeout: never
buffer_duration: 80ms
fill_silence: true
debug: true
switch:
- platform: gpio
pin: GPIO46
id: grove_port_power
restore_mode: ALWAYS_ON
setup_priority: 1001
media_player:
- id: !extend nabu_media_player
name: Media Player
audio_dac: !remove
volume_increment: !remove
volume_min: !remove
volume_max: !remove
```
<details><summary>Example config for a generic ESP32-S3 dev board</summary>
<p>
```yaml
esphome:
name: spdif
esp32:
board: esp32-s3-devkitc-1
variant: esp32s3
flash_size: 16MB
framework:
type: esp-idf
version: recommended
sdkconfig_options:
CONFIG_ESP32S3_DEFAULT_CPU_FREQ_240: "y"
CONFIG_ESP32S3_DATA_CACHE_64KB: "y"
CONFIG_ESP32S3_DATA_CACHE_LINE_64B: "y"
CONFIG_ESP32S3_INSTRUCTION_CACHE_32KB: "y"
CONFIG_ESP32_S3_BOX_BOARD: "y"
CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY: "y"
CONFIG_SPIRAM_TRY_ALLOCATE_WIFI_LWIP: "y"
# Settings based on https://github.com/espressif/esp-adf/issues/297#issuecomment-783811702
CONFIG_ESP32_WIFI_STATIC_RX_BUFFER_NUM: "16"
CONFIG_ESP32_WIFI_DYNAMIC_RX_BUFFER_NUM: "512"
CONFIG_ESP32_WIFI_STATIC_TX_BUFFER: "y"
CONFIG_ESP32_WIFI_TX_BUFFER_TYPE: "0"
CONFIG_ESP32_WIFI_STATIC_TX_BUFFER_NUM: "8"
CONFIG_ESP32_WIFI_CACHE_TX_BUFFER_NUM: "32"
CONFIG_ESP32_WIFI_AMPDU_TX_ENABLED: "y"
CONFIG_ESP32_WIFI_TX_BA_WIN: "16"
CONFIG_ESP32_WIFI_AMPDU_RX_ENABLED: "y"
CONFIG_ESP32_WIFI_RX_BA_WIN: "32"
CONFIG_LWIP_MAX_ACTIVE_TCP: "16"
CONFIG_LWIP_MAX_LISTENING_TCP: "16"
CONFIG_TCP_MAXRTX: "12"
CONFIG_TCP_SYNMAXRTX: "6"
CONFIG_TCP_MSS: "1436"
CONFIG_TCP_MSL: "60000"
CONFIG_TCP_SND_BUF_DEFAULT: "65535"
CONFIG_TCP_WND_DEFAULT: "65535" # Adjusted from linked settings to avoid compilation error
CONFIG_TCP_RECVMBOX_SIZE: "512"
CONFIG_TCP_QUEUE_OOSEQ: "y"
CONFIG_TCP_OVERSIZE_MSS: "y"
CONFIG_LWIP_WND_SCALE: "y"
CONFIG_TCP_RCV_SCALE: "3"
CONFIG_LWIP_TCPIP_RECVMBOX_SIZE: "512"
CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST: "y"
CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY: "y"
CONFIG_MBEDTLS_EXTERNAL_MEM_ALLOC: "y"
CONFIG_MBEDTLS_SSL_PROTO_TLS1_3: "y" # TLS1.3 support isn't enabled by default in IDF 5.1.5
psram:
mode: quad
speed: 80MHz
wifi:
ssid: [YOURSSID]
password: [YOURPASSWORD]
power_save_mode: none
logger:
external_components:
- source:
type: git
url: https://github.com/esphome/home-assistant-voice-pe
ref: dev
components:
- media_player
- nabu
refresh: 0s
- source: github://pr#8065
components: [ i2s_audio, spdif_audio ]
refresh: 0s
i2s_audio:
id: i2s_shared
speaker:
- platform: spdif_audio
id: my_speaker
i2s_audio_id: i2s_shared
data_pin: GPIO13
sample_rate: 48000
buffer_duration: 80ms
fill_silence: true
debug: true
media_player:
- platform: nabu
id: nabu_media_player
name: Media Player
internal: false
speaker: my_speaker
sample_rate: 48000
```
</p>
</details>
## Checklist:
- [x] The code change is tested and works locally.
- [ ] Tests have been added to verify that the new code works (under `tests/` folder).
If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated in [esphome-docs](https://github.com/esphome/esphome-docs).
Add `id` to the `speaker` component by johnboiles · Pull Request #294 · esphome/home-assistant-voice-pe · GitHub (this one is merged now!)
5 Likes