Skip to content

system/nxplayer: Add WAV file playback support. - #3840

Open
imeghar2408-max wants to merge 2 commits into
apache:masterfrom
imeghar2408-max:feat/nxplayer-wav-playback
Open

imeghar2408-max wants to merge 2 commits into
apache:masterfrom
imeghar2408-max:feat/nxplayer-wav-playback

Conversation

@imeghar2408-max

Copy link
Copy Markdown
Contributor

Parse RIFF/WAVE headers and play supported PCM 16-bit mono and stereo WAV files through the existing audio playback path.

Validate chunk boundaries, format parameters, and data length before playback. Preserve the existing raw PCM playback path and report read errors during WAV playback cleanup.

Assisted-by: OpenAI:GPT-6

Summary

Add WAV file playback support to system/nxplayer.

The implementation parses RIFF/WAVE headers and supports 16-bit PCM mono and stereo WAV files. It validates chunk boundaries, format parameters, and data length before playback.

Changes

  • Add RIFF/WAVE header parsing for PCM WAV files.
  • Support 16-bit mono and stereo audio.
  • Validate WAV format parameters, chunk boundaries, and data length.
  • Preserve the existing raw PCM playback path.
  • Report read errors during WAV playback cleanup.

Impact

This change adds WAV playback support to nxplayer while preserving existing raw PCM playback through playraw. Invalid or unsupported WAV files are rejected.

Validation

SIM ALSA Build

Command:

make -j1

Result: Build completed successfully, including linking nuttx.

WAV Playback

Tested a valid WAV file:

nxplayer> play /data/test.wav

Result: Audio playback was audible.

Tested a WAV file containing an odd-sized JUNK chunk.

Result: Audio playback was audible.

Malformed WAV Files

Tested a truncated WAV header and an invalid RIFF length.

Observed output for the truncated header:

Error playing file: 22

Result: Malformed WAV files were rejected.

Raw PCM Playback

Command:

nxplayer> playraw /data/test.raw

Result: Audio playback was audible.

Read Error During WAV Playback

Truncated the WAV file while playback was in progress.

Observed output:

ERROR: WAV playback read failed: 5

After entering q, the application returned to the NSH prompt.

Checkpatch

Command:

~/nuttx-work/nuttx-sim-alsa/tools/checkpatch.sh -g HEAD

Output:

✔️ All checks pass.

Comment thread system/nxplayer/nxplayer.c
@imeghar2408-max

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216

I've pushed the refactor in commit b972648c5.

WAV playback now uses g_wav_dec_ops with parser/fill callbacks, and nxplayer_readbuffer() dispatches through the decoder's fill_data callback rather than a separate WAV branch. The existing MP3/SBC and common PCM paths use adapters to the same callback interface.

Validation completed:

  • SIM ALSA build and link succeeded.
  • checkpatch and git diff --check pass.
  • Valid WAV playback works, and malformed WAV headers/RIFF lengths are rejected.
  • Raw PCM playback remains supported.

Could you please take another look?

@imeghar2408-max
imeghar2408-max force-pushed the feat/nxplayer-wav-playback branch 2 times, most recently from 665cd07 to 243b4a4 Compare October 12, 2026 19:17
Comment thread system/nxplayer/nxplayer.c Outdated
Comment thread system/nxplayer/nxplayer.c Outdated
@imeghar2408-max

Copy link
Copy Markdown
Contributor Author

Hi @xiaoxiang781216 , thanks for the feedback!

I've updated the implementation by moving the WAV-specific RIFF/WAVE parsing, format validation, and PCM buffer-filling logic into a dedicated nxplayer_wav.c, integrated with the existing decoder operations framework.

The existing MP3/SBC decoder paths and raw PCM playback path have been kept separate from the WAV-specific changes.

I've also rebuilt and tested the SIM ALSA configuration, including valid WAV playback with odd-sized chunks, raw PCM playback, malformed WAV rejection, and mid-playback truncation handling. checkpatch.sh and git diff --check pass.

Please have another look when you get a chance. Thanks!

Comment thread include/system/nxplayer.h
CODE int (*pre_parse)(int fd, FAR uint32_t *samplerate,
FAR uint8_t *chans, FAR uint8_t *bps);
CODE int (*fill_data)(int fd, FAR struct ap_buffer_s *apb);
CODE int (*pre_parse)(FAR struct nxplayer_s *pplayer, int fd,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why need change the prototype? if the change is really needed, please split the common change to the separated patch, and wave decoder in one and only one patch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The prototype change is needed to pass the player context to decoder callbacks and allow pre_parse to return the selected subformat. The WAV decoder needs this to maintain per-player parsing state and select the appropriate PCM subformat.

I'll separate the generic callback-interface change into its own commit, using adapters to preserve the existing MP3/SBC and common PCM behavior. All WAV-specific decoding and integration will be contained in one separate commit.
thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please remove the adapter and modify the mp3/sbc directly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I removed the adapters and modified the MP3/SBC callbacks directly to use the shared decoder interface.
I've also split the changes into two commits.. waiting for your review:)

Refactor the decoder callback interface shared by NXPlayer. Update
the common, MP3, and SBC implementations to use the common interface,
preparing the player for additional decoder formats.

Tested:
- NuttX SIM build: make -j12

Assisted-by: Copilot
Signed-off-by: Megha Rajput <i.meghar.2408@gmail.com>
Add RIFF/WAV parsing and PCM buffer filling through decoder callbacks. Validate WAV format and data chunks, maintain per-player playback state, and clean up resources. Integrate the WAV decoder into the build while preserving raw PCM playback behavior.

Tested:
- NuttX SIM build: make -j12
- checkpatch.sh: C style checks passed; cmake-format unavailable.

Assisted-by: Copilot

Signed-off-by: Megha Rajput <i.meghar.2408@gmail.com>
@imeghar2408-max
imeghar2408-max force-pushed the feat/nxplayer-wav-playback branch from 7a2c7e3 to ad73730 Compare October 16, 2026 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants