mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:37:34 +00:00
LibAudio: Add an adjustable buffer size to FlacLoader
This makes it easier to fine-tune the optimal input buffer size.
This commit is contained in:
parent
eb1f00a940
commit
9fa3aa84e1
2 changed files with 16 additions and 13 deletions
|
@ -4,12 +4,12 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "AK/StdLibExtras.h"
|
|
||||||
#include <AK/Debug.h>
|
#include <AK/Debug.h>
|
||||||
#include <AK/FlyString.h>
|
#include <AK/FlyString.h>
|
||||||
#include <AK/Format.h>
|
#include <AK/Format.h>
|
||||||
#include <AK/Math.h>
|
#include <AK/Math.h>
|
||||||
#include <AK/ScopeGuard.h>
|
#include <AK/ScopeGuard.h>
|
||||||
|
#include <AK/StdLibExtras.h>
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <AK/Try.h>
|
#include <AK/Try.h>
|
||||||
|
@ -30,19 +30,15 @@ FlacLoaderPlugin::FlacLoaderPlugin(StringView path)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto maybe_stream = Core::InputFileStream::open_buffered(path);
|
auto maybe_stream = Buffered<Core::InputFileStream, FLAC_BUFFER_SIZE> { MUST(Core::File::open(path, Core::OpenMode::ReadOnly)) };
|
||||||
if (maybe_stream.is_error()) {
|
m_stream = make<FlacInputStream<FLAC_BUFFER_SIZE>>(move(maybe_stream));
|
||||||
m_error = LoaderError { "Can't open file stream" };
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
m_stream = make<FlacInputStream>(maybe_stream.release_value());
|
|
||||||
if (!m_stream)
|
if (!m_stream)
|
||||||
m_error = LoaderError { "Can't open file stream" };
|
m_error = LoaderError { "Can't open file stream" };
|
||||||
}
|
}
|
||||||
|
|
||||||
FlacLoaderPlugin::FlacLoaderPlugin(const ByteBuffer& buffer)
|
FlacLoaderPlugin::FlacLoaderPlugin(const ByteBuffer& buffer)
|
||||||
{
|
{
|
||||||
m_stream = make<FlacInputStream>(InputMemoryStream(buffer));
|
m_stream = make<FlacInputStream<FLAC_BUFFER_SIZE>>(InputMemoryStream(buffer));
|
||||||
if (!m_stream)
|
if (!m_stream)
|
||||||
m_error = LoaderError { "Can't open memory stream" };
|
m_error = LoaderError { "Can't open memory stream" };
|
||||||
}
|
}
|
||||||
|
@ -61,7 +57,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header()
|
||||||
{
|
{
|
||||||
InputBitStream bit_input = [&]() -> InputBitStream {
|
InputBitStream bit_input = [&]() -> InputBitStream {
|
||||||
if (m_file) {
|
if (m_file) {
|
||||||
return InputBitStream(m_stream->get<Buffered<Core::InputFileStream>>());
|
return InputBitStream(m_stream->get<Buffered<Core::InputFileStream, FLAC_BUFFER_SIZE>>());
|
||||||
}
|
}
|
||||||
return InputBitStream(m_stream->get<InputMemoryStream>());
|
return InputBitStream(m_stream->get<InputMemoryStream>());
|
||||||
}();
|
}();
|
||||||
|
|
|
@ -19,15 +19,22 @@
|
||||||
|
|
||||||
namespace Audio {
|
namespace Audio {
|
||||||
|
|
||||||
class FlacInputStream : public Variant<Buffered<Core::InputFileStream>, InputMemoryStream> {
|
// Experimentally determined to be a decent buffer size on i686:
|
||||||
|
// 4K (the default) is slightly worse, and 64K is much worse.
|
||||||
|
// At sufficiently large buffer sizes, the advantage of infrequent read() calls is outweighed by the memmove() overhead.
|
||||||
|
// There was no intensive fine-tuning done to determine this value, so improvements may definitely be possible.
|
||||||
|
constexpr size_t FLAC_BUFFER_SIZE = 8 * KiB;
|
||||||
|
|
||||||
|
template<size_t Size = FLAC_BUFFER_SIZE>
|
||||||
|
class FlacInputStream : public Variant<Buffered<Core::InputFileStream, Size>, InputMemoryStream> {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
using Variant<Buffered<Core::InputFileStream>, InputMemoryStream>::Variant;
|
using Variant<Buffered<Core::InputFileStream, Size>, InputMemoryStream>::Variant;
|
||||||
|
|
||||||
bool seek(size_t pos)
|
bool seek(size_t pos)
|
||||||
{
|
{
|
||||||
return this->visit(
|
return this->visit(
|
||||||
[&](Buffered<Core::InputFileStream>& buffered) {
|
[&](Buffered<Core::InputFileStream, Size>& buffered) {
|
||||||
// Discard the buffer, then seek normally.
|
// Discard the buffer, then seek normally.
|
||||||
if (!buffered.discard_or_error(buffered.buffered()))
|
if (!buffered.discard_or_error(buffered.buffered()))
|
||||||
return false;
|
return false;
|
||||||
|
@ -142,7 +149,7 @@ private:
|
||||||
|
|
||||||
// keep track of the start of the data in the FLAC stream to seek back more easily
|
// keep track of the start of the data in the FLAC stream to seek back more easily
|
||||||
u64 m_data_start_location { 0 };
|
u64 m_data_start_location { 0 };
|
||||||
OwnPtr<FlacInputStream> m_stream;
|
OwnPtr<FlacInputStream<FLAC_BUFFER_SIZE>> m_stream;
|
||||||
Optional<FlacFrameHeader> m_current_frame;
|
Optional<FlacFrameHeader> m_current_frame;
|
||||||
Vector<Sample> m_current_frame_data;
|
Vector<Sample> m_current_frame_data;
|
||||||
u64 m_current_sample_or_frame { 0 };
|
u64 m_current_sample_or_frame { 0 };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue