summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorNick Miller <nick.miller.83@gmail.com>2021-06-05 09:38:43 -0700
committerAli Mohammad Pur <Ali.mpfard@gmail.com>2021-06-08 00:38:54 +0430
commit3938b565777b5f0efc6651ee5268cb807e8b160f (patch)
tree2b9655ada7c9fabf976f9c90de78ecc551945e0d /Userland
parent06ea31d0d5c0bc4e7b6f07b26bd8163697e22eb7 (diff)
downloadserenity-3938b565777b5f0efc6651ee5268cb807e8b160f.zip
LibAudio+LibCore: Remove unnecessary IODeviceStreamReader.h
IODeviceStreamReader isn't pulling its weight. It's essentially a subset of InputFileStream with only one user (WavLoader). This refactors WavLoader to use InputFileStream instead.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibAudio/WavLoader.cpp61
-rw-r--r--Userland/Libraries/LibAudio/WavLoader.h4
-rw-r--r--Userland/Libraries/LibCore/IODeviceStreamReader.h41
3 files changed, 21 insertions, 85 deletions
diff --git a/Userland/Libraries/LibAudio/WavLoader.cpp b/Userland/Libraries/LibAudio/WavLoader.cpp
index 206526f136..893eedb386 100644
--- a/Userland/Libraries/LibAudio/WavLoader.cpp
+++ b/Userland/Libraries/LibAudio/WavLoader.cpp
@@ -11,7 +11,7 @@
#include <LibAudio/Buffer.h>
#include <LibAudio/WavLoader.h>
#include <LibCore/File.h>
-#include <LibCore/IODeviceStreamReader.h>
+#include <LibCore/FileStream.h>
namespace Audio {
@@ -47,11 +47,6 @@ WavLoaderPlugin::WavLoaderPlugin(const ByteBuffer& buffer)
m_resampler = make<ResampleHelper>(m_sample_rate, 44100);
}
-bool WavLoaderPlugin::sniff()
-{
- return valid;
-}
-
RefPtr<Buffer> WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_input)
{
dbgln_if(AWAVLOADER_DEBUG, "Read {} bytes WAV with num_channels {} sample rate {}, "
@@ -89,58 +84,40 @@ void WavLoaderPlugin::seek(const int position)
m_stream->seek(byte_position);
}
-void WavLoaderPlugin::reset()
-{
- seek(0);
-}
-
bool WavLoaderPlugin::parse_header()
{
- OwnPtr<Core::IODeviceStreamReader> file_stream;
- bool ok = true;
-
+ OwnPtr<Core::InputFileStream> file_stream;
if (m_file)
- file_stream = make<Core::IODeviceStreamReader>(*m_file);
+ file_stream = make<Core::InputFileStream>(*m_file);
+
+ AK::InputStream* const stream =
+ (m_file ?
+ file_stream.ptr() :
+ dynamic_cast<AK::InputStream*>(m_stream.ptr()));
+
+ bool ok = true;
auto read_u8 = [&]() -> u8 {
u8 value;
- if (m_file) {
- *file_stream >> value;
- if (file_stream->handle_read_failure())
- ok = false;
- } else {
- *m_stream >> value;
- if (m_stream->handle_any_error())
- ok = false;
- }
+ *stream >> value;
+ if (stream->handle_any_error())
+ ok = false;
return value;
};
auto read_u16 = [&]() -> u16 {
u16 value;
- if (m_file) {
- *file_stream >> value;
- if (file_stream->handle_read_failure())
- ok = false;
- } else {
- *m_stream >> value;
- if (m_stream->handle_any_error())
- ok = false;
- }
+ *stream >> value;
+ if (stream->handle_any_error())
+ ok = false;
return value;
};
auto read_u32 = [&]() -> u32 {
u32 value;
- if (m_file) {
- *file_stream >> value;
- if (file_stream->handle_read_failure())
- ok = false;
- } else {
- *m_stream >> value;
- if (m_stream->handle_any_error())
- ok = false;
- }
+ *stream >> value;
+ if (stream->handle_any_error())
+ ok = false;
return value;
};
diff --git a/Userland/Libraries/LibAudio/WavLoader.h b/Userland/Libraries/LibAudio/WavLoader.h
index bab6ac00a4..6807e4429d 100644
--- a/Userland/Libraries/LibAudio/WavLoader.h
+++ b/Userland/Libraries/LibAudio/WavLoader.h
@@ -33,14 +33,14 @@ public:
WavLoaderPlugin(const StringView& path);
WavLoaderPlugin(const ByteBuffer& buffer);
- virtual bool sniff() override;
+ virtual bool sniff() override { return valid; }
virtual bool has_error() override { return !m_error_string.is_null(); }
virtual const char* error_string() override { return m_error_string.characters(); }
virtual RefPtr<Buffer> get_more_samples(size_t max_bytes_to_read_from_input = 128 * KiB) override;
- virtual void reset() override;
+ virtual void reset() override { return seek(0); }
virtual void seek(const int position) override;
virtual int loaded_samples() override { return m_loaded_samples; }
diff --git a/Userland/Libraries/LibCore/IODeviceStreamReader.h b/Userland/Libraries/LibCore/IODeviceStreamReader.h
deleted file mode 100644
index 73791bfdcc..0000000000
--- a/Userland/Libraries/LibCore/IODeviceStreamReader.h
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#pragma once
-
-#include <AK/StdLibExtras.h>
-#include <LibCore/IODevice.h>
-
-namespace Core {
-
-class IODeviceStreamReader {
-public:
- IODeviceStreamReader(IODevice& device)
- : m_device(device)
- {
- }
-
- bool handle_read_failure()
- {
- return exchange(m_had_failure, false);
- }
-
- template<typename T>
- IODeviceStreamReader& operator>>(T& value)
- {
- int nread = m_device.read((u8*)&value, sizeof(T));
- VERIFY(nread == sizeof(T));
- if (nread != sizeof(T))
- m_had_failure = true;
- return *this;
- }
-
-private:
- IODevice& m_device;
- bool m_had_failure { false };
-};
-
-}