1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 18:27:39 +00:00

LibAudio: Make Loader::seek() treat its input as a sample index

This fixes a bug where if you try to play a Wave file a second
time (or loop with `aplay -l`), the second time will be pure
noise.

The function `Audio::Loader::seek` is meant to seek to a specific
audio sample, e.g. seek(0) should go to the first audio sample.
However, WavLoader was interpreting seek(0) as the beginning
of the file or stream, which contains non-audio header data.

This fixes the bug by capturing the byte offset of the start of the
audio data, and offseting the raw file/stream seek by that amount.
This commit is contained in:
Nick Miller 2021-06-05 17:14:55 -07:00 committed by Ali Mohammad Pur
parent 2a3090d292
commit 23d5b99fbf
3 changed files with 17 additions and 6 deletions

View file

@ -44,7 +44,10 @@ public:
virtual RefPtr<Buffer> get_more_samples(size_t max_bytes_to_read_from_input = 128 * KiB) override;
virtual void reset() override { return seek(0); }
virtual void seek(const int position) override;
// sample_index 0 is the start of the raw audio sample data
// within the file/stream.
virtual void seek(const int sample_index) override;
virtual int loaded_samples() override { return m_loaded_samples; }
virtual int total_samples() override { return m_total_samples; }
@ -66,6 +69,7 @@ private:
u32 m_sample_rate { 0 };
u16 m_num_channels { 0 };
PcmSampleFormat m_sample_format;
size_t m_byte_offset_of_data_samples { 0 };
int m_loaded_samples { 0 };
int m_total_samples { 0 };