From 8331d7cd82b1af5c443c691cbdf89e3404cc2698 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Fri, 24 Feb 2023 21:46:55 +0100 Subject: [PATCH] LibAudio: Use the proper functions to read WAV samples Turns out that, if we don't use functions that ensure reading until the very end of the buffer, we only end up getting the very beginning of samples and fill the rest with uninitialized data. While at it, make sure that we read the data that is little endian as a LittleEndian. --- Userland/Libraries/LibAudio/WavLoader.cpp | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibAudio/WavLoader.cpp b/Userland/Libraries/LibAudio/WavLoader.cpp index d69bd734c6..ccd55f1fb6 100644 --- a/Userland/Libraries/LibAudio/WavLoader.cpp +++ b/Userland/Libraries/LibAudio/WavLoader.cpp @@ -75,13 +75,9 @@ MaybeLoaderError WavLoaderPlugin::read_samples_from_stream(Stream& stream, Sampl // There's no i24 type + we need to do the endianness conversion manually anyways. static ErrorOr read_sample_int24(Stream& stream) { - u8 byte = 0; - TRY(stream.read(Bytes { &byte, 1 })); - i32 sample1 = byte; - TRY(stream.read(Bytes { &byte, 1 })); - i32 sample2 = byte; - TRY(stream.read(Bytes { &byte, 1 })); - i32 sample3 = byte; + i32 sample1 = TRY(stream.read_value()); + i32 sample2 = TRY(stream.read_value()); + i32 sample3 = TRY(stream.read_value()); i32 value = 0; value = sample1; @@ -97,7 +93,7 @@ template static ErrorOr read_sample(Stream& stream) { T sample { 0 }; - TRY(stream.read(Bytes { &sample, sizeof(T) })); + TRY(stream.read_entire_buffer(Bytes { &sample, sizeof(T) })); // Remap integer samples to normalized floating-point range of -1 to 1. if constexpr (IsIntegral) { if constexpr (NumericLimits::is_signed()) { @@ -163,7 +159,7 @@ LoaderSamples WavLoaderPlugin::get_more_samples(size_t max_samples_to_read_from_ pcm_bits_per_sample(m_sample_format), sample_format_name(m_sample_format)); auto sample_data = LOADER_TRY(ByteBuffer::create_zeroed(bytes_to_read)); - LOADER_TRY(m_stream->read(sample_data.bytes())); + LOADER_TRY(m_stream->read_entire_buffer(sample_data.bytes())); // m_loaded_samples should contain the amount of actually loaded samples m_loaded_samples += samples_to_read; @@ -191,22 +187,19 @@ MaybeLoaderError WavLoaderPlugin::parse_header() size_t bytes_read = 0; auto read_u8 = [&]() -> ErrorOr { - u8 value; - LOADER_TRY(m_stream->read(Bytes { &value, 1 })); + u8 value = LOADER_TRY(m_stream->read_value>()); bytes_read += 1; return value; }; auto read_u16 = [&]() -> ErrorOr { - u16 value; - LOADER_TRY(m_stream->read(Bytes { &value, 2 })); + u16 value = LOADER_TRY(m_stream->read_value>()); bytes_read += 2; return value; }; auto read_u32 = [&]() -> ErrorOr { - u32 value; - LOADER_TRY(m_stream->read(Bytes { &value, 4 })); + u32 value = LOADER_TRY(m_stream->read_value>()); bytes_read += 4; return value; };