From dfd48ab6432932119a54181072031010d2e66a47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Sat, 1 Apr 2023 17:21:49 +0200 Subject: [PATCH] LibAudio: Extract MP3 synchronize to static helper function This comes in handy when we want to sniff an MP3 file with a stream outside of the loader. --- Userland/Libraries/LibAudio/MP3Loader.cpp | 15 ++++++++++----- Userland/Libraries/LibAudio/MP3Loader.h | 1 + 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibAudio/MP3Loader.cpp b/Userland/Libraries/LibAudio/MP3Loader.cpp index b73dc28dc2..2867e68a5c 100644 --- a/Userland/Libraries/LibAudio/MP3Loader.cpp +++ b/Userland/Libraries/LibAudio/MP3Loader.cpp @@ -188,21 +188,26 @@ ErrorOr MP3LoaderPlugin::read_header() return header; } -MaybeLoaderError MP3LoaderPlugin::synchronize() +MaybeLoaderError MP3LoaderPlugin::synchronize(BigEndianInputBitStream& stream, size_t sample_index) { size_t one_counter = 0; - while (one_counter < 12 && !m_bitstream->is_eof()) { - bool const bit = LOADER_TRY(m_bitstream->read_bit()); + while (one_counter < 12 && !stream.is_eof()) { + bool const bit = LOADER_TRY(stream.read_bit()); one_counter = bit ? one_counter + 1 : 0; if (!bit) { - m_bitstream->align_to_byte_boundary(); + stream.align_to_byte_boundary(); } } if (one_counter != 12) - return LoaderError { LoaderError::Category::Format, m_loaded_samples, "Failed to synchronize." }; + return LoaderError { LoaderError::Category::Format, sample_index, "Failed to synchronize." }; return {}; } +MaybeLoaderError MP3LoaderPlugin::synchronize() +{ + return MP3LoaderPlugin::synchronize(*m_bitstream, m_loaded_samples); +} + ErrorOr MP3LoaderPlugin::read_next_frame() { // Note: This will spin until we find a correct frame, or we reach eof. diff --git a/Userland/Libraries/LibAudio/MP3Loader.h b/Userland/Libraries/LibAudio/MP3Loader.h index 610ae21b3c..af01bf679e 100644 --- a/Userland/Libraries/LibAudio/MP3Loader.h +++ b/Userland/Libraries/LibAudio/MP3Loader.h @@ -41,6 +41,7 @@ public: private: MaybeLoaderError initialize(); + static MaybeLoaderError synchronize(BigEndianInputBitStream& stream, size_t sample_index); MaybeLoaderError synchronize(); MaybeLoaderError build_seek_table(); ErrorOr read_header();