From 5b8895fff0c185178b551c8521b238bc7f767c1a Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Fri, 25 Aug 2023 19:37:39 -0500 Subject: [PATCH] LibAudio: Create MP3 seek table first and then seek sample 0 to play The seek table must locate the first MP3 frame in the file, so it makes sense to locate the samples for the sample table first, then that information to seek to the first frame. --- Userland/Libraries/LibAudio/MP3Loader.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibAudio/MP3Loader.cpp b/Userland/Libraries/LibAudio/MP3Loader.cpp index af749c46bb..ccd1a5bfb5 100644 --- a/Userland/Libraries/LibAudio/MP3Loader.cpp +++ b/Userland/Libraries/LibAudio/MP3Loader.cpp @@ -83,10 +83,11 @@ ErrorOr, LoaderError> MP3LoaderPlugin::create(Nonnul MaybeLoaderError MP3LoaderPlugin::initialize() { - TRY(skip_id3(*m_stream)); m_bitstream = TRY(try_make(MaybeOwned(*m_stream))); - TRY(synchronize()); + TRY(build_seek_table()); + TRY(seek(0)); + TRY(synchronize()); auto header = TRY(read_header()); if (header.id != 1 || header.layer != 3) return LoaderError { LoaderError::Category::Format, "Only MPEG-1 layer 3 supported." }; @@ -95,7 +96,6 @@ MaybeLoaderError MP3LoaderPlugin::initialize() m_num_channels = header.channel_count(); m_loaded_samples = 0; - TRY(build_seek_table()); TRY(seek(0)); return {}; @@ -170,12 +170,14 @@ ErrorOr>, LoaderError> MP3LoaderPlugin::load_chunks(si MaybeLoaderError MP3LoaderPlugin::build_seek_table() { + VERIFY(MUST(m_stream->tell()) == 0); + TRY(skip_id3(*m_stream)); + m_bitstream->align_to_byte_boundary(); + int sample_count = 0; size_t frame_count = 0; m_seek_table = {}; - m_bitstream->align_to_byte_boundary(); - while (!synchronize().is_error()) { auto const frame_pos = -2 + TRY(m_stream->seek(0, SeekMode::FromCurrentPosition));