Skip to content

[all-devices-app] Integrate Real Audio Playback for POSIX Chime Device#71644

Open
soares-sergio wants to merge 28 commits intoproject-chip:masterfrom
soares-sergio:chime_audio_playback
Open

[all-devices-app] Integrate Real Audio Playback for POSIX Chime Device#71644
soares-sergio wants to merge 28 commits intoproject-chip:masterfrom
soares-sergio:chime_audio_playback

Conversation

@soares-sergio
Copy link
Copy Markdown
Contributor

@soares-sergio soares-sergio commented Apr 17, 2026

[all-devices-app] Integrate Real Audio Playback for POSIX Chime Device

This PR integrates real audio playback into the PosixChimeDevice for the all-devices-app on POSIX platforms (Linux and Darwin). It uses the miniaudio library to generate and play synthetic sounds entirely from memory.

Changes

Audio Integration (miniaudio)

  • Added miniaudio as a git submodule in third_party/miniaudio/repo.
  • Created miniaudio.cpp to host the implementation define.
  • Updated PosixChimeDevice to use miniaudio's engine to play sounds.

In-Memory Sound Synthesis

  • Implemented GenerateWavMemory in PosixChimeDevice.cpp to generate synthetic WAV files in memory buffers (avoiding disk I/O).
  • Created "Ding Dong" (Chime 0) and "Ring Ring" (Chime 1) sounds using additive synthesis (fundamental + harmonics) and exponential decay for natural sound.
  • Pre-loaded decoders and sounds from memory for low-latency playback.

Build System & Compatibility

  • Updated BUILD.gn files for POSIX, Linux, and Darwin to include miniaudio include paths.

Testing

I verified the changes with the following steps:

  1. Build: Verified that the all-devices-app builds successfully for Linux x64 with BoringSSL.
  2. Audio Generation: Confirmed that the in-memory audio buffers are generated correctly (logged size ~86KB per sound).
  3. Functional Test: Ran the application on a local Linux machine with --device chime and used chip-tool to trigger both Chime 0 and Chime 1.
  4. Audio Output: Confirmed that both distinct synthetic sounds played successfully without overlap or cut-off.

test_chime.sh

Helper script to build chip-tool + all-devices-app and run a test that triggers both "Ding Dong" and "Ring Ring".

#!/bin/bash

# Exit on error
set -e

# Kill any leftover instances from previous failed runs
pkill all-devices-app || true

echo "=================================================="
echo "1. Activating Environment"
echo "=================================================="
source scripts/activate.sh

echo "=================================================="
echo "2. Building all-devices-app (Linux x64)"
echo "=================================================="
./scripts/build/build_examples.py --target linux-x64-all-devices-boringssl build

echo "=================================================="
echo "3. Building chip-tool (Linux x64)"
echo "=================================================="
./scripts/build/build_examples.py --target linux-x64-chip-tool-boringssl build

echo "=================================================="
echo "4. Cleaning up previous state"
echo "=================================================="
rm -f /tmp/chip_tool_kvs /tmp/chip_kvs /tmp/chip_factory.ini /tmp/chip_config.ini /tmp/chip_counters.ini

echo "=================================================="
echo "5. Running all-devices-app"
echo "=================================================="
# Run in background, logging to app.log
./out/linux-x64-all-devices-boringssl/all-devices-app --device chime > app.log 2>&1 &
APP_PID=$!

echo "Started all-devices-app with PID $APP_PID. Waiting 5 seconds for initialization..."
sleep 5

echo "=================================================="
echo "6. Testing with chip-tool"
echo "=================================================="
echo "Attempting to pair on network (assumes mDNS works on your laptop)..."
# Default pairing code for all-devices-app is 20202021, using node ID 1
./out/linux-x64-chip-tool-boringssl/chip-tool pairing onnetwork 1 20202021

echo "Playing Chime 0 (Ding Dong)..."
./out/linux-x64-chip-tool-boringssl/chip-tool chime play-chime-sound 1 1 --ChimeID 0

# Wait for Chime 0 to finish playing
echo "Waiting 3 seconds..."
sleep 3

echo "Playing Chime 1 (Ring Ring)..."
./out/linux-x64-chip-tool-boringssl/chip-tool chime play-chime-sound 1 1 --ChimeID 1

# Wait for Chime 1 to finish playing
echo "Waiting 3 seconds..."
sleep 3

echo "=================================================="
echo "7. Cleaning up"
echo "=================================================="
echo "Stopping all-devices-app..."
kill $APP_PID

echo "Done! You can check app.log for the application logs."

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the ChimeDevice to support a dynamic list of sounds, introduces a POSIX-specific implementation using the miniaudio library for in-memory WAV playback, and adjusts BoringSSL build flags. Feedback focuses on ensuring cross-platform correctness by using Little-Endian encoding for WAV headers, implementing an explicit Shutdown method for robust resource management, removing hardcoded sound mappings in the playback logic, and optimizing binary data construction to minimize memory allocations.

Comment thread examples/all-devices-app/posix/PosixChimeDevice.cpp Outdated
Comment thread examples/all-devices-app/posix/PosixChimeDevice.cpp
Comment thread examples/all-devices-app/posix/PosixChimeDevice.cpp Outdated
Comment thread examples/all-devices-app/posix/PosixChimeDevice.cpp Outdated
@mergify mergify bot removed the conflict label Apr 17, 2026
@soares-sergio soares-sergio changed the title Refactor Matter Chime Architecture for POSIX and Integrate Real Audio [all-devices-app] Integrate Real Audio Playback for POSIX Chime Device Apr 17, 2026
@soares-sergio soares-sergio added the changing-git-submodules-on-purpose This PR is changing git submodules on intentionally (and it is not a "commit all" typo/mistake) label Apr 17, 2026
Comment thread examples/all-devices-app/posix/include/PosixChimeDevice.h Outdated
Comment thread examples/all-devices-app/posix/include/PosixChimeDevice.h
Comment thread examples/all-devices-app/posix/include/PosixChimeDevice.h
Comment thread examples/all-devices-app/posix/PosixChimeDevice.cpp Outdated
Comment thread examples/all-devices-app/posix/PosixChimeDevice.cpp Outdated
Comment thread examples/all-devices-app/posix/PosixChimeDevice.cpp Outdated
Comment thread examples/all-devices-app/posix/PosixChimeDevice.cpp Outdated
Comment thread examples/all-devices-app/posix/PosixChimeDevice.cpp Outdated
Comment thread examples/all-devices-app/posix/PosixChimeDevice.cpp Outdated
@soares-sergio
Copy link
Copy Markdown
Contributor Author

I have refactored the implementation to address the remaining review comments:

  1. RAII for SoundResource: Converted SoundResource from a struct to a class. The constructor handles initialization and the destructor handles cleanup automatically.
  2. Use of unique_ptr: Switched mSoundResources to use std::unique_ptr<SoundResource> for better lifetime management and to avoid copying non-trivial miniaudio structures.
  3. Kept mSoundsInitialized: Kept this flag to track if all sounds are ready, but updated it to use the new class members.

All changes are pushed and the build is verified! Please let me know if it looks good now!

@github-actions
Copy link
Copy Markdown

PR #71644: Size comparison from 5f28762 to d2cd884

Full report (10 builds for cc13x4_26x4, cc32xx, nrfconnect, realtek, stm32)
platform target config section 5f28762 d2cd884 change % change
cc13x4_26x4 lighting-app LP_EM_CC1354P10_6 FLASH 775896 775896 0 0.0
RAM 103420 103420 0 0.0
lock-ftd LP_EM_CC1354P10_6 FLASH 788132 788132 0 0.0
RAM 108604 108604 0 0.0
pump-app LP_EM_CC1354P10_6 FLASH 735532 735532 0 0.0
RAM 97444 97444 0 0.0
pump-controller-app LP_EM_CC1354P10_6 FLASH 717916 717916 0 0.0
RAM 97572 97572 0 0.0
cc32xx air-purifier CC3235SF_LAUNCHXL FLASH 560890 560890 0 0.0
RAM 204616 204616 0 0.0
lock CC3235SF_LAUNCHXL FLASH 592794 592794 0 0.0
RAM 204824 204824 0 0.0
nrfconnect all-clusters-app nrf52840dk_nrf52840 FLASH 864244 864244 0 0.0
RAM 162233 162233 0 0.0
realtek light-switch-app rtl8777g FLASH 681032 681032 0 0.0
RAM 101632 101632 0 0.0
lighting-app rtl8777g FLASH 724832 724832 0 0.0
RAM 101964 101964 0 0.0
stm32 light STM32WB5MM-DK FLASH 475476 475476 0 0.0
RAM 141412 141412 0 0.0

@soares-sergio
Copy link
Copy Markdown
Contributor Author

I have checked the remaining review comments and here is the status:

  1. SoundResource Refactoring: I have refactored SoundResource to be a class with RAII. The constructor handles initialization and the destructor handles uninitialization. I also switched to using std::unique_ptr<SoundResource> in the vector to manage their lifetime automatically.
  2. Constants Naming: The GenerateWavMemory function was removed, but I applied your suggestion to the remaining constants: they are now constexpr and kSampleRate has been renamed to kSampleRateHz.
  3. WAV Header Magic Numbers: The function containing this logic was removed.
  4. BufferWriter: The function containing this logic was removed.
  5. SoundResource Lifetime: Addressed by refactoring to a class with RAII and using unique_ptr.

All changes are pushed and build is verified! Please let me know if anything else is needed.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 17, 2026

PR #71644: Size comparison from 5f28762 to dd24f4e

Full report (34 builds for bl602, bl616, bl702, bl702l, cc13x4_26x4, cc32xx, efr32, esp32, nrfconnect, nxp, psoc6, qpg, realtek, stm32, telink)
platform target config section 5f28762 dd24f4e change % change
bl602 lighting-app bl602+mfd+littlefs+rpc FLASH 1090528 1090528 0 0.0
RAM 144850 144850 0 0.0
bl616 lighting-app bl616+thread FLASH 1102012 1102012 0 0.0
RAM 104280 104280 0 0.0
bl616+wifi+shell FLASH 1588900 1588900 0 0.0
RAM 98176 98176 0 0.0
bl702 lighting-app bl702+eth FLASH 1053702 1053702 0 0.0
RAM 108461 108461 0 0.0
bl702l contact-sensor-app bl702l+mfd+littlefs FLASH 892400 892400 0 0.0
RAM 105852 105852 0 0.0
cc13x4_26x4 lighting-app LP_EM_CC1354P10_6 FLASH 775896 775896 0 0.0
RAM 103420 103420 0 0.0
lock-ftd LP_EM_CC1354P10_6 FLASH 788132 788132 0 0.0
RAM 108604 108604 0 0.0
pump-app LP_EM_CC1354P10_6 FLASH 735532 735532 0 0.0
RAM 97444 97444 0 0.0
pump-controller-app LP_EM_CC1354P10_6 FLASH 717916 717916 0 0.0
RAM 97572 97572 0 0.0
cc32xx air-purifier CC3235SF_LAUNCHXL FLASH 560890 560890 0 0.0
RAM 204616 204616 0 0.0
lock CC3235SF_LAUNCHXL FLASH 592794 592794 0 0.0
RAM 204824 204824 0 0.0
efr32 lock-app BRD4187C FLASH 992648 992648 0 0.0
RAM 131272 131272 0 0.0
BRD4338a FLASH 796433 796425 -8 -0.0
RAM 243380 243380 0 0.0
window-app BRD4187C FLASH 1098152 1098152 0 0.0
RAM 130312 130312 0 0.0
esp32 all-clusters-app c3devkit DRAM 98572 98572 0 0.0
FLASH 1601762 1601762 0 0.0
IRAM 93514 93514 0 0.0
nrfconnect all-clusters-app nrf52840dk_nrf52840 FLASH 864244 864244 0 0.0
RAM 162233 162233 0 0.0
nxp contact mcxw71+release FLASH 740336 740336 0 0.0
RAM 67040 67040 0 0.0
psoc6 all-clusters cy8ckit_062s2_43012 FLASH 1714948 1714948 0 0.0
RAM 214180 214180 0 0.0
all-clusters-minimal cy8ckit_062s2_43012 FLASH 1612364 1612364 0 0.0
RAM 211052 211052 0 0.0
light cy8ckit_062s2_43012 FLASH 1466940 1466940 0 0.0
RAM 197092 197092 0 0.0
lock cy8ckit_062s2_43012 FLASH 1499668 1499668 0 0.0
RAM 224828 224828 0 0.0
qpg lighting-app qpg6200+debug FLASH 842700 842700 0 0.0
RAM 127884 127884 0 0.0
lock-app qpg6200+debug FLASH 781300 781300 0 0.0
RAM 118832 118832 0 0.0
realtek light-switch-app rtl8777g FLASH 681032 681032 0 0.0
RAM 101632 101632 0 0.0
lighting-app rtl8777g FLASH 724832 724832 0 0.0
RAM 101964 101964 0 0.0
stm32 light STM32WB5MM-DK FLASH 475476 475476 0 0.0
RAM 141412 141412 0 0.0
telink bridge-app tl7218x FLASH 730658 730658 0 0.0
RAM 95932 95932 0 0.0
light-app-ota-compress-lzma-shell-factory-data tl3218x FLASH 850462 850462 0 0.0
RAM 44348 44348 0 0.0
tl7218x FLASH 841868 841868 0 0.0
RAM 99736 99736 0 0.0
light-switch-app-ota-compress-lzma-factory-data tl7218x_retention FLASH 730460 730460 0 0.0
RAM 56012 56012 0 0.0
light-switch-app-ota-compress-lzma-shell-factory-data tlsr9528a FLASH 793686 793686 0 0.0
RAM 75188 75188 0 0.0
light-switch-app-ota-factory-data tl3218x_retention FLASH 730382 730382 0 0.0
RAM 33492 33492 0 0.0
lighting-app-ota-factory-data tlsr9118bdk40d FLASH 613268 613268 0 0.0
RAM 118404 118404 0 0.0
lighting-app-ota-rpc-factory-data-4mb tlsr9518adk80d FLASH 840002 840006 4 0.0
RAM 97444 97444 0 0.0

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 54.40%. Comparing base (5f28762) to head (dd24f4e).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #71644   +/-   ##
=======================================
  Coverage   54.40%   54.40%           
=======================================
  Files        1582     1582           
  Lines      108395   108395           
  Branches    13384    13384           
=======================================
  Hits        58976    58976           
  Misses      49419    49419           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changing-git-submodules-on-purpose This PR is changing git submodules on intentionally (and it is not a "commit all" typo/mistake) examples review - pending

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants