From 9d5819e9e21324f63e7dd400ab20fca2c1aed443 Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Wed, 5 Jul 2023 02:41:12 -0500 Subject: [PATCH] LibAudio: Seek accurately to the target frame in the FLAC loader Instead of using a seek tolerance value to get close enough to the target, we can skip frames forward until we pass the target, then seek back to the previous frame. That puts us in a position to immediately decode the frame containing the target sample. --- Userland/Libraries/LibAudio/FlacLoader.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp index 6306d35d37..ecf509e456 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.cpp +++ b/Userland/Libraries/LibAudio/FlacLoader.cpp @@ -285,7 +285,6 @@ MaybeLoaderError FlacLoaderPlugin::seek(int int_sample_index) return {}; auto maybe_target_seekpoint = m_seektable.seek_point_before(sample_index); - auto const seek_tolerance = (seek_tolerance_ms * m_sample_rate) / 1000; // No seektable or no fitting entry: Perform normal forward read if (!maybe_target_seekpoint.has_value()) { if (sample_index < m_loaded_samples) { @@ -310,11 +309,15 @@ MaybeLoaderError FlacLoaderPlugin::seek(int int_sample_index) } } - // Skip frames until we're within the seek tolerance. - while (sample_index - m_loaded_samples > seek_tolerance) { + // Skip frames until we're just before the target sample. + VERIFY(m_loaded_samples <= sample_index); + size_t frame_start_location; + while (m_loaded_samples <= sample_index) { + frame_start_location = TRY(m_stream->tell()); (void)TRY(next_frame()); m_loaded_samples += m_current_frame->sample_count; } + TRY(m_stream->seek(frame_start_location, SeekMode::SetPosition)); return {}; }