Skip to content

fix: seek disk-streamed MP3s from a seek table instead of decoding from the start - #634

Merged
alnitak merged 1 commit into
alnitak:mainfrom
appelladev:mp3-stream-seek-table
Dec 4, 2026
Merged

alnitak merged 1 commit into
alnitak:mainfrom
appelladev:mp3-stream-seek-table

Conversation

@Fmarschall

Copy link
Copy Markdown

Description

A WavStream voice of an MP3 (LoadMode.disk) seeks with drmp3_seek_to_pcm_frame, and no seek table was bound to the decoder. dr_mp3 then seeks backwards by decoding the file from its first frame up to the target, under the audio mutex. A seek near the end of a song read most of the file, so a seek slider stuttered and every voice dropped out until the seek finished. The docs worked around it by advising LoadMode.memory for MP3s that are sought.

When a voice opens an MP3, it now builds a seek table with drmp3_calculate_seek_points and binds it with drmp3_bind_seek_table. Building the table parses the frame headers, without decoding the audio, in createInstance, which runs outside the audio mutex. The table holds four points a second, at most 16384 (one every 0.7 s for a 3-hour file), 24 bytes each: about 17 KB for a 3-minute song. A seek then decodes at most a quarter of a second. dr_mp3 itself is unchanged; the change is a marked local patch in soloud_wavstream.cpp/.h. Without memory for the table, the voice seeks as before.

wavstream_seek_test now also covers MP3, with 8_bit_mentality.mp3. It does 19 backward seeks from near the end and checks that the voice then plays exactly what a voice reading from the start plays there:

  • before: the 19 seeks read the file 9.49 times, one seek 93.5% of it, so the test fails;
  • after: 0.37 times, one seek 1.9% at most, and the samples are identical.

Creating a voice of that file (206 s) takes about 1.4 ms on an M-series Mac. We found this in an app that plays six disk-streamed MP3 stems of a song in sync. With a first version of this patch, which kept one point per frame, scrubbing on a Pixel 8 became as smooth as with WAVs.

The notes advising LoadMode.memory for sought MP3s are removed from the dartdoc, bindings.h and the generated bindings (comments only), and from the loading and playback skills (versions bumped). There is a CHANGELOG entry under "Unreleased".

Ran:

  • ./test/common/wavstream_seek_test.sh, also with --asan
  • voice_allocation_failure_test.sh, wavstream_missing_file_test.sh
  • flutter test test/common/play_seek_pause_test.dart test/common/audio_source_test.dart test/common/ffigen_bindings_sync_test.dart
  • flutter analyze, dart format

Not run: the full native suites, and the Android and iOS native tests. I did not rebuild the WASM, since I have no Emscripten here. On the web LoadMode.disk is ignored, so the change is not reachable there until #633.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@alnitak

alnitak commented Dec 4, 2026

Copy link
Copy Markdown
Owner

Thanks @Fmarschall, this fixes a long-standing limit, and the test made it easy to check: it fails on main and passes with your change, and the seeks are sample-exact on every MP3 I tried.

Your branch is in a fork owned by an organization, and GitHub does not let maintainers push to those, so I could not add my commits to this PR. I opened #636 instead: your commit is the first one there, unchanged.

What I added on top:

  • The seek table is built once, when the sound is loaded, and its voices share it. With a table per voice, every play() read the whole file twice on the calling thread: 1.3 ms for a 3-minute song, but 75 ms for a 3-hour file. Starting a voice is now as quick as before your change, and a voice no longer keeps its own copy of the table.
  • Merged main (the web now streams LoadMode.disk too, since web: LoadMode.disk streams the compressed data instead of decoding it whole #635) and rebuilt the web binaries.
  • The test also checks that starting a voice does not read the whole file, and that a second voice seeks in the same way.

@alnitak
alnitak merged commit bff5add into alnitak:main Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants