LibAudio: Remove the last occurrence of Core::File in the Flac plugin

The design is deeply inspired from what is done in the Wav plugin.
This commit is contained in:
Lucas CHOLLET 2022-10-13 12:13:15 +02:00 committed by Linus Groh
parent f028930033
commit 597a614ce6
Notes: sideshowbarker 2024-07-17 05:47:24 +09:00
2 changed files with 8 additions and 20 deletions

View file

@ -26,33 +26,21 @@
namespace Audio {
FlacLoaderPlugin::FlacLoaderPlugin(StringView path)
: m_file(Core::File::construct(path))
: m_path(path)
{
if (!m_file->open(Core::OpenMode::ReadOnly)) {
m_error = LoaderError { String::formatted("Can't open file: {}", m_file->error_string()) };
return;
}
auto maybe_stream = Core::Stream::BufferedFile::create(MUST(Core::Stream::File::open(path, Core::Stream::OpenMode::Read)), FLAC_BUFFER_SIZE);
if (maybe_stream.is_error())
m_error = LoaderError { "Can't open file stream" };
else
m_stream = maybe_stream.release_value();
}
FlacLoaderPlugin::FlacLoaderPlugin(Bytes& buffer)
: m_backing_memory(buffer)
{
auto maybe_stream = Core::Stream::MemoryStream::construct(buffer);
if (maybe_stream.is_error())
m_error = LoaderError { "Can't open memory stream" };
else
m_stream = maybe_stream.release_value();
}
MaybeLoaderError FlacLoaderPlugin::initialize()
{
if (m_error.has_value())
return m_error.release_value();
if (m_backing_memory.has_value())
m_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(m_backing_memory.value()));
else
m_stream = LOADER_TRY(Core::Stream::File::open(m_path, Core::Stream::OpenMode::Read));
TRY(parse_header());
TRY(reset());

View file

@ -95,8 +95,7 @@ private:
ALWAYS_INLINE ErrorOr<u32, LoaderError> convert_sample_rate_code(u8 sample_rate_code);
ALWAYS_INLINE ErrorOr<PcmSampleFormat, LoaderError> convert_bit_depth_code(u8 bit_depth_code);
RefPtr<Core::File> m_file;
Optional<LoaderError> m_error {};
StringView m_path;
// Data obtained directly from the FLAC metadata: many values have specific bit counts
u32 m_sample_rate { 0 }; // 20 bit
@ -115,6 +114,7 @@ private:
// keep track of the start of the data in the FLAC stream to seek back more easily
u64 m_data_start_location { 0 };
OwnPtr<Core::Stream::SeekableStream> m_stream;
Optional<Bytes> m_backing_memory;
Optional<FlacFrameHeader> m_current_frame;
// Whatever the last get_more_samples() call couldn't return gets stored here.
Vector<Sample, FLAC_BUFFER_SIZE> m_unread_data;