2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-13 12:07:00 +01:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2021-06-25 13:37:38 +02:00
|
|
|
#include "WavLoader.h"
|
2022-04-23 11:08:45 +02:00
|
|
|
#include "LoaderError.h"
|
2021-01-24 15:28:26 +01:00
|
|
|
#include <AK/Debug.h>
|
2022-04-23 12:11:32 +02:00
|
|
|
#include <AK/Endian.h>
|
LibAudio+Userland: Use new audio queue in client-server communication
Previously, we were sending Buffers to the server whenever we had new
audio data for it. This meant that for every audio enqueue action, we
needed to create a new shared memory anonymous buffer, send that
buffer's file descriptor over IPC (+recfd on the other side) and then
map the buffer into the audio server's memory to be able to play it.
This was fine for sending large chunks of audio data, like when playing
existing audio files. However, in the future we want to move to
real-time audio in some applications like Piano. This means that the
size of buffers that are sent need to be very small, as just the size of
a buffer itself is part of the audio latency. If we were to try
real-time audio with the existing system, we would run into problems
really quickly. Dealing with a continuous stream of new anonymous files
like the current audio system is rather expensive, as we need Kernel
help in multiple places. Additionally, every enqueue incurs an IPC call,
which are not optimized for >1000 calls/second (which would be needed
for real-time audio with buffer sizes of ~40 samples). So a fundamental
change in how we handle audio sending in userspace is necessary.
This commit moves the audio sending system onto a shared single producer
circular queue (SSPCQ) (introduced with one of the previous commits).
This queue is intended to live in shared memory and be accessed by
multiple processes at the same time. It was specifically written to
support the audio sending case, so e.g. it only supports a single
producer (the audio client). Now, audio sending follows these general
steps:
- The audio client connects to the audio server.
- The audio client creates a SSPCQ in shared memory.
- The audio client sends the SSPCQ's file descriptor to the audio server
with the set_buffer() IPC call.
- The audio server receives the SSPCQ and maps it.
- The audio client signals start of playback with start_playback().
- At the same time:
- The audio client writes its audio data into the shared-memory queue.
- The audio server reads audio data from the shared-memory queue(s).
Both sides have additional before-queue/after-queue buffers, depending
on the exact application.
- Pausing playback is just an IPC call, nothing happens to the buffer
except that the server stops reading from it until playback is
resumed.
- Muting has nothing to do with whether audio data is read or not.
- When the connection closes, the queues are unmapped on both sides.
This should already improve audio playback performance in a bunch of
places.
Implementation & commit notes:
- Audio loaders don't create LegacyBuffers anymore. LegacyBuffer is kept
for WavLoader, see previous commit message.
- Most intra-process audio data passing is done with FixedArray<Sample>
or Vector<Sample>.
- Improvements to most audio-enqueuing applications. (If necessary I can
try to extract some of the aplay improvements.)
- New APIs on LibAudio/ClientConnection which allows non-realtime
applications to enqueue audio in big chunks like before.
- Removal of status APIs from the audio server connection for
information that can be directly obtained from the shared queue.
- Split the pause playback API into two APIs with more intuitive names.
I know this is a large commit, and you can kinda tell from the commit
message. It's basically impossible to break this up without hacks, so
please forgive me. These are some of the best changes to the audio
subsystem and I hope that that makes up for this :yaktangle: commit.
:yakring:
2022-02-20 13:01:22 +01:00
|
|
|
#include <AK/FixedArray.h>
|
2023-01-25 20:19:05 +01:00
|
|
|
#include <AK/MemoryStream.h>
|
2022-04-23 12:11:32 +02:00
|
|
|
#include <AK/NumericLimits.h>
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
#include <AK/Try.h>
|
2023-02-09 03:02:46 +01:00
|
|
|
#include <LibCore/File.h>
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
namespace Audio {
|
|
|
|
|
|
2022-04-22 14:13:34 +02:00
|
|
|
static constexpr size_t const maximum_wav_size = 1 * GiB; // FIXME: is there a more appropriate size limit?
|
2021-03-16 19:05:59 +02:00
|
|
|
|
2023-01-22 05:09:11 +01:00
|
|
|
WavLoaderPlugin::WavLoaderPlugin(NonnullOwnPtr<SeekableStream> stream)
|
2022-12-05 00:41:23 +01:00
|
|
|
: LoaderPlugin(move(stream))
|
2019-07-13 19:42:03 +02:00
|
|
|
{
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
}
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2023-01-28 20:12:17 +00:00
|
|
|
Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> WavLoaderPlugin::create(StringView path)
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
{
|
2023-02-09 03:02:46 +01:00
|
|
|
auto stream = LOADER_TRY(Core::BufferedFile::create(LOADER_TRY(Core::File::open(path, Core::File::OpenMode::Read))));
|
2022-12-05 00:41:23 +01:00
|
|
|
auto loader = make<WavLoaderPlugin>(move(stream));
|
2022-04-23 11:08:45 +02:00
|
|
|
|
2022-12-05 00:41:23 +01:00
|
|
|
LOADER_TRY(loader->initialize());
|
|
|
|
|
|
|
|
|
|
return loader;
|
2019-07-27 17:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-28 20:12:17 +00:00
|
|
|
Result<NonnullOwnPtr<WavLoaderPlugin>, LoaderError> WavLoaderPlugin::create(Bytes buffer)
|
2020-12-02 15:44:45 +01:00
|
|
|
{
|
2023-01-30 11:05:43 +01:00
|
|
|
auto stream = LOADER_TRY(try_make<FixedMemoryStream>(buffer));
|
2022-12-05 00:41:23 +01:00
|
|
|
auto loader = make<WavLoaderPlugin>(move(stream));
|
|
|
|
|
|
|
|
|
|
LOADER_TRY(loader->initialize());
|
|
|
|
|
|
|
|
|
|
return loader;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MaybeLoaderError WavLoaderPlugin::initialize()
|
|
|
|
|
{
|
|
|
|
|
LOADER_TRY(parse_header());
|
|
|
|
|
|
|
|
|
|
return {};
|
2020-12-02 15:44:45 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-23 12:11:32 +02:00
|
|
|
template<typename SampleReader>
|
2023-02-10 01:00:18 +01:00
|
|
|
MaybeLoaderError WavLoaderPlugin::read_samples_from_stream(Stream& stream, SampleReader read_sample, FixedArray<Sample>& samples) const
|
2022-04-23 12:11:32 +02:00
|
|
|
{
|
|
|
|
|
switch (m_num_channels) {
|
|
|
|
|
case 1:
|
|
|
|
|
for (auto& sample : samples)
|
|
|
|
|
sample = Sample(LOADER_TRY(read_sample(stream)));
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
for (auto& sample : samples) {
|
|
|
|
|
auto left_channel_sample = LOADER_TRY(read_sample(stream));
|
|
|
|
|
auto right_channel_sample = LOADER_TRY(read_sample(stream));
|
|
|
|
|
sample = Sample(left_channel_sample, right_channel_sample);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// There's no i24 type + we need to do the endianness conversion manually anyways.
|
2023-02-10 01:00:18 +01:00
|
|
|
static ErrorOr<double> read_sample_int24(Stream& stream)
|
2022-04-23 12:11:32 +02:00
|
|
|
{
|
2023-02-24 21:46:55 +01:00
|
|
|
i32 sample1 = TRY(stream.read_value<u8>());
|
|
|
|
|
i32 sample2 = TRY(stream.read_value<u8>());
|
|
|
|
|
i32 sample3 = TRY(stream.read_value<u8>());
|
2022-04-23 12:11:32 +02:00
|
|
|
|
|
|
|
|
i32 value = 0;
|
2022-10-14 12:44:26 +02:00
|
|
|
value = sample1;
|
|
|
|
|
value |= sample2 << 8;
|
|
|
|
|
value |= sample3 << 16;
|
|
|
|
|
// Sign extend the value, as it can currently not have the correct sign.
|
|
|
|
|
value = (value << 8) >> 8;
|
|
|
|
|
// Range of value is now -2^23 to 2^23-1 and we can rescale normally.
|
|
|
|
|
return static_cast<double>(value) / static_cast<double>((1 << 23) - 1);
|
2022-04-23 12:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
2023-02-10 01:00:18 +01:00
|
|
|
static ErrorOr<double> read_sample(Stream& stream)
|
2022-04-23 12:11:32 +02:00
|
|
|
{
|
|
|
|
|
T sample { 0 };
|
2023-03-01 15:27:35 +01:00
|
|
|
TRY(stream.read_until_filled(Bytes { &sample, sizeof(T) }));
|
2022-10-14 12:22:18 +02:00
|
|
|
// Remap integer samples to normalized floating-point range of -1 to 1.
|
2022-04-23 12:11:32 +02:00
|
|
|
if constexpr (IsIntegral<T>) {
|
2022-10-14 12:22:18 +02:00
|
|
|
if constexpr (NumericLimits<T>::is_signed()) {
|
|
|
|
|
// Signed integer samples are centered around zero, so this division is enough.
|
|
|
|
|
return static_cast<double>(AK::convert_between_host_and_little_endian(sample)) / static_cast<double>(NumericLimits<T>::max());
|
|
|
|
|
} else {
|
|
|
|
|
// Unsigned integer samples, on the other hand, need to be shifted to center them around zero.
|
|
|
|
|
// The first division therefore remaps to the range 0 to 2.
|
|
|
|
|
return static_cast<double>(AK::convert_between_host_and_little_endian(sample)) / (static_cast<double>(NumericLimits<T>::max()) / 2.0) - 1.0;
|
|
|
|
|
}
|
2022-04-23 12:11:32 +02:00
|
|
|
} else {
|
|
|
|
|
return static_cast<double>(AK::convert_between_host_and_little_endian(sample));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LoaderSamples WavLoaderPlugin::samples_from_pcm_data(Bytes const& data, size_t samples_to_read) const
|
|
|
|
|
{
|
2023-01-28 20:12:17 +00:00
|
|
|
FixedArray<Sample> samples = LOADER_TRY(FixedArray<Sample>::create(samples_to_read));
|
2023-01-30 11:05:43 +01:00
|
|
|
FixedMemoryStream stream { data };
|
2022-04-23 12:11:32 +02:00
|
|
|
|
|
|
|
|
switch (m_sample_format) {
|
|
|
|
|
case PcmSampleFormat::Uint8:
|
2023-01-30 11:05:43 +01:00
|
|
|
TRY(read_samples_from_stream(stream, read_sample<u8>, samples));
|
2022-04-23 12:11:32 +02:00
|
|
|
break;
|
|
|
|
|
case PcmSampleFormat::Int16:
|
2023-01-30 11:05:43 +01:00
|
|
|
TRY(read_samples_from_stream(stream, read_sample<i16>, samples));
|
2022-04-23 12:11:32 +02:00
|
|
|
break;
|
|
|
|
|
case PcmSampleFormat::Int24:
|
2023-01-30 11:05:43 +01:00
|
|
|
TRY(read_samples_from_stream(stream, read_sample_int24, samples));
|
2022-04-23 12:11:32 +02:00
|
|
|
break;
|
|
|
|
|
case PcmSampleFormat::Float32:
|
2023-01-30 11:05:43 +01:00
|
|
|
TRY(read_samples_from_stream(stream, read_sample<float>, samples));
|
2022-04-23 12:11:32 +02:00
|
|
|
break;
|
|
|
|
|
case PcmSampleFormat::Float64:
|
2023-01-30 11:05:43 +01:00
|
|
|
TRY(read_samples_from_stream(stream, read_sample<double>, samples));
|
2022-04-23 12:11:32 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return samples;
|
|
|
|
|
}
|
|
|
|
|
|
LibAudio: Move audio stream buffering into the loader
Before, some loader plugins implemented their own buffering (FLAC&MP3),
some didn't require any (WAV), and some didn't buffer at all (QOA). This
meant that in practice, while you could load arbitrary amounts of
samples from some loader plugins, you couldn't do that with some others.
Also, it was ill-defined how many samples you would actually get back
from a get_more_samples call.
This commit fixes that by introducing a layer of abstraction between the
loader and its plugins (because that's the whole point of having the
extra class!). The plugins now only implement a load_chunks() function,
which is much simpler to implement and allows plugins to play fast and
loose with what they actually return. Basically, they can return many
chunks of samples, where one chunk is simply a convenient block of
samples to load. In fact, some loaders such as FLAC and QOA have
separate internal functions for loading exactly one chunk. The loaders
*should* load as many chunks as necessary for the sample count to be
reached or surpassed (the latter simplifies loading loops in the
implementations, since you don't need to know how large your next chunk
is going to be; a problem for e.g. FLAC). If a plugin has no problems
returning data of arbitrary size (currently WAV), it can return a single
chunk that exactly (or roughly) matches the requested sample count. If a
plugin is at the stream end, it can also return less samples than was
requested! The loader can handle all of these cases and may call into
load_chunk multiple times. If the plugin returns an empty chunk list (or
only empty chunks; again, they can play fast and loose), the loader
takes that as a stream end signal. Otherwise, the loader will always
return exactly as many samples as the user requested. Buffering is
handled by the loader, allowing any underlying plugin to deal with any
weird sample count requirement the user throws at it (looking at you,
SoundPlayer!).
This (not accidentally!) makes QOA work in SoundPlayer.
2023-02-27 00:05:14 +01:00
|
|
|
ErrorOr<Vector<FixedArray<Sample>>, LoaderError> WavLoaderPlugin::load_chunks(size_t samples_to_read_from_input)
|
2019-07-27 17:20:41 +02:00
|
|
|
{
|
2022-04-22 14:13:34 +02:00
|
|
|
auto remaining_samples = m_total_samples - m_loaded_samples;
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
if (remaining_samples <= 0)
|
LibAudio: Move audio stream buffering into the loader
Before, some loader plugins implemented their own buffering (FLAC&MP3),
some didn't require any (WAV), and some didn't buffer at all (QOA). This
meant that in practice, while you could load arbitrary amounts of
samples from some loader plugins, you couldn't do that with some others.
Also, it was ill-defined how many samples you would actually get back
from a get_more_samples call.
This commit fixes that by introducing a layer of abstraction between the
loader and its plugins (because that's the whole point of having the
extra class!). The plugins now only implement a load_chunks() function,
which is much simpler to implement and allows plugins to play fast and
loose with what they actually return. Basically, they can return many
chunks of samples, where one chunk is simply a convenient block of
samples to load. In fact, some loaders such as FLAC and QOA have
separate internal functions for loading exactly one chunk. The loaders
*should* load as many chunks as necessary for the sample count to be
reached or surpassed (the latter simplifies loading loops in the
implementations, since you don't need to know how large your next chunk
is going to be; a problem for e.g. FLAC). If a plugin has no problems
returning data of arbitrary size (currently WAV), it can return a single
chunk that exactly (or roughly) matches the requested sample count. If a
plugin is at the stream end, it can also return less samples than was
requested! The loader can handle all of these cases and may call into
load_chunk multiple times. If the plugin returns an empty chunk list (or
only empty chunks; again, they can play fast and loose), the loader
takes that as a stream end signal. Otherwise, the loader will always
return exactly as many samples as the user requested. Buffering is
handled by the loader, allowing any underlying plugin to deal with any
weird sample count requirement the user throws at it (looking at you,
SoundPlayer!).
This (not accidentally!) makes QOA work in SoundPlayer.
2023-02-27 00:05:14 +01:00
|
|
|
return Vector<FixedArray<Sample>> {};
|
2021-06-05 10:14:03 -07:00
|
|
|
|
2021-06-10 10:59:00 -07:00
|
|
|
// One "sample" contains data from all channels.
|
|
|
|
|
// In the Wave spec, this is also called a block.
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
size_t bytes_per_sample
|
|
|
|
|
= m_num_channels * pcm_bits_per_sample(m_sample_format) / 8;
|
2021-06-10 10:59:00 -07:00
|
|
|
|
LibAudio: Move audio stream buffering into the loader
Before, some loader plugins implemented their own buffering (FLAC&MP3),
some didn't require any (WAV), and some didn't buffer at all (QOA). This
meant that in practice, while you could load arbitrary amounts of
samples from some loader plugins, you couldn't do that with some others.
Also, it was ill-defined how many samples you would actually get back
from a get_more_samples call.
This commit fixes that by introducing a layer of abstraction between the
loader and its plugins (because that's the whole point of having the
extra class!). The plugins now only implement a load_chunks() function,
which is much simpler to implement and allows plugins to play fast and
loose with what they actually return. Basically, they can return many
chunks of samples, where one chunk is simply a convenient block of
samples to load. In fact, some loaders such as FLAC and QOA have
separate internal functions for loading exactly one chunk. The loaders
*should* load as many chunks as necessary for the sample count to be
reached or surpassed (the latter simplifies loading loops in the
implementations, since you don't need to know how large your next chunk
is going to be; a problem for e.g. FLAC). If a plugin has no problems
returning data of arbitrary size (currently WAV), it can return a single
chunk that exactly (or roughly) matches the requested sample count. If a
plugin is at the stream end, it can also return less samples than was
requested! The loader can handle all of these cases and may call into
load_chunk multiple times. If the plugin returns an empty chunk list (or
only empty chunks; again, they can play fast and loose), the loader
takes that as a stream end signal. Otherwise, the loader will always
return exactly as many samples as the user requested. Buffering is
handled by the loader, allowing any underlying plugin to deal with any
weird sample count requirement the user throws at it (looking at you,
SoundPlayer!).
This (not accidentally!) makes QOA work in SoundPlayer.
2023-02-27 00:05:14 +01:00
|
|
|
auto samples_to_read = min(samples_to_read_from_input, remaining_samples);
|
2022-04-22 14:13:34 +02:00
|
|
|
auto bytes_to_read = samples_to_read * bytes_per_sample;
|
2021-06-05 10:14:03 -07:00
|
|
|
|
2021-06-10 10:59:00 -07:00
|
|
|
dbgln_if(AWAVLOADER_DEBUG, "Read {} bytes WAV with num_channels {} sample rate {}, "
|
2021-05-01 21:10:08 +02:00
|
|
|
"bits per sample {}, sample format {}",
|
2021-06-10 10:59:00 -07:00
|
|
|
bytes_to_read, m_num_channels, m_sample_rate,
|
2021-06-05 10:14:03 -07:00
|
|
|
pcm_bits_per_sample(m_sample_format), sample_format_name(m_sample_format));
|
|
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
auto sample_data = LOADER_TRY(ByteBuffer::create_zeroed(bytes_to_read));
|
2023-03-01 15:27:35 +01:00
|
|
|
LOADER_TRY(m_stream->read_until_filled(sample_data.bytes()));
|
2021-06-05 10:14:03 -07:00
|
|
|
|
|
|
|
|
// m_loaded_samples should contain the amount of actually loaded samples
|
2020-12-02 15:44:45 +01:00
|
|
|
m_loaded_samples += samples_to_read;
|
LibAudio: Move audio stream buffering into the loader
Before, some loader plugins implemented their own buffering (FLAC&MP3),
some didn't require any (WAV), and some didn't buffer at all (QOA). This
meant that in practice, while you could load arbitrary amounts of
samples from some loader plugins, you couldn't do that with some others.
Also, it was ill-defined how many samples you would actually get back
from a get_more_samples call.
This commit fixes that by introducing a layer of abstraction between the
loader and its plugins (because that's the whole point of having the
extra class!). The plugins now only implement a load_chunks() function,
which is much simpler to implement and allows plugins to play fast and
loose with what they actually return. Basically, they can return many
chunks of samples, where one chunk is simply a convenient block of
samples to load. In fact, some loaders such as FLAC and QOA have
separate internal functions for loading exactly one chunk. The loaders
*should* load as many chunks as necessary for the sample count to be
reached or surpassed (the latter simplifies loading loops in the
implementations, since you don't need to know how large your next chunk
is going to be; a problem for e.g. FLAC). If a plugin has no problems
returning data of arbitrary size (currently WAV), it can return a single
chunk that exactly (or roughly) matches the requested sample count. If a
plugin is at the stream end, it can also return less samples than was
requested! The loader can handle all of these cases and may call into
load_chunk multiple times. If the plugin returns an empty chunk list (or
only empty chunks; again, they can play fast and loose), the loader
takes that as a stream end signal. Otherwise, the loader will always
return exactly as many samples as the user requested. Buffering is
handled by the loader, allowing any underlying plugin to deal with any
weird sample count requirement the user throws at it (looking at you,
SoundPlayer!).
This (not accidentally!) makes QOA work in SoundPlayer.
2023-02-27 00:05:14 +01:00
|
|
|
Vector<FixedArray<Sample>> samples;
|
|
|
|
|
TRY(samples.try_append(TRY(samples_from_pcm_data(sample_data.bytes(), samples_to_read))));
|
|
|
|
|
return samples;
|
2019-07-13 19:42:03 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-22 14:13:34 +02:00
|
|
|
MaybeLoaderError WavLoaderPlugin::seek(int sample_index)
|
2019-11-04 19:39:17 +01:00
|
|
|
{
|
2021-06-05 17:14:55 -07:00
|
|
|
dbgln_if(AWAVLOADER_DEBUG, "seek sample_index {}", sample_index);
|
2022-04-22 14:13:34 +02:00
|
|
|
if (sample_index < 0 || sample_index >= static_cast<int>(m_total_samples))
|
|
|
|
|
return LoaderError { LoaderError::Category::Internal, m_loaded_samples, "Seek outside the sample range" };
|
2019-11-04 19:39:17 +01:00
|
|
|
|
2022-04-22 14:13:34 +02:00
|
|
|
size_t sample_offset = m_byte_offset_of_data_samples + static_cast<size_t>(sample_index * m_num_channels * (pcm_bits_per_sample(m_sample_format) / 8));
|
2020-12-02 15:44:45 +01:00
|
|
|
|
2023-01-22 05:09:11 +01:00
|
|
|
LOADER_TRY(m_stream->seek(sample_offset, SeekMode::SetPosition));
|
2021-06-10 10:59:00 -07:00
|
|
|
|
|
|
|
|
m_loaded_samples = sample_index;
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
return {};
|
2019-11-04 19:39:17 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-05 17:23:27 -07:00
|
|
|
// Specification reference: http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
MaybeLoaderError WavLoaderPlugin::parse_header()
|
2019-07-13 19:42:03 +02:00
|
|
|
{
|
2021-06-05 09:38:43 -07:00
|
|
|
bool ok = true;
|
2021-06-05 17:14:55 -07:00
|
|
|
size_t bytes_read = 0;
|
2020-12-02 15:44:45 +01:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
auto read_u8 = [&]() -> ErrorOr<u8, LoaderError> {
|
2023-02-24 21:46:55 +01:00
|
|
|
u8 value = LOADER_TRY(m_stream->read_value<LittleEndian<u8>>());
|
2021-06-05 17:14:55 -07:00
|
|
|
bytes_read += 1;
|
2020-12-02 15:44:45 +01:00
|
|
|
return value;
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
auto read_u16 = [&]() -> ErrorOr<u16, LoaderError> {
|
2023-02-24 21:46:55 +01:00
|
|
|
u16 value = LOADER_TRY(m_stream->read_value<LittleEndian<u16>>());
|
2021-06-05 17:14:55 -07:00
|
|
|
bytes_read += 2;
|
2020-12-02 15:44:45 +01:00
|
|
|
return value;
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
auto read_u32 = [&]() -> ErrorOr<u32, LoaderError> {
|
2023-02-24 21:46:55 +01:00
|
|
|
u32 value = LOADER_TRY(m_stream->read_value<LittleEndian<u32>>());
|
2021-06-05 17:14:55 -07:00
|
|
|
bytes_read += 4;
|
2020-12-02 15:44:45 +01:00
|
|
|
return value;
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#define CHECK_OK(category, msg) \
|
|
|
|
|
do { \
|
|
|
|
|
if (!ok) \
|
|
|
|
|
return LoaderError { category, DeprecatedString::formatted("Parsing failed: {}", msg) }; \
|
2021-04-26 17:13:04 +02:00
|
|
|
} while (0)
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
u32 riff = TRY(read_u32());
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && riff == 0x46464952; // "RIFF"
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Format, "RIFF header");
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
u32 sz = TRY(read_u32());
|
2021-06-05 17:23:27 -07:00
|
|
|
ok = ok && sz < maximum_wav_size;
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Format, "File size");
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
u32 wave = TRY(read_u32());
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && wave == 0x45564157; // "WAVE"
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Format, "WAVE header");
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
u32 fmt_id = TRY(read_u32());
|
2021-06-05 17:23:27 -07:00
|
|
|
ok = ok && fmt_id == 0x20746D66; // "fmt "
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Format, "FMT header");
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
u32 fmt_size = TRY(read_u32());
|
2021-06-05 17:23:27 -07:00
|
|
|
ok = ok && (fmt_size == 16 || fmt_size == 18 || fmt_size == 40);
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Format, "FMT size");
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
u16 audio_format = TRY(read_u16());
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Format, "Audio format"); // incomplete read check
|
2021-06-05 17:23:27 -07:00
|
|
|
ok = ok && (audio_format == WAVE_FORMAT_PCM || audio_format == WAVE_FORMAT_IEEE_FLOAT || audio_format == WAVE_FORMAT_EXTENSIBLE);
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Unimplemented, "Audio format PCM/Float"); // value check
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
m_num_channels = TRY(read_u16());
|
2019-07-27 17:20:41 +02:00
|
|
|
ok = ok && (m_num_channels == 1 || m_num_channels == 2);
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Unimplemented, "Channel count");
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
m_sample_rate = TRY(read_u32());
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::IO, "Sample rate");
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
TRY(read_u32());
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::IO, "Data rate");
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
u16 block_size_bytes = TRY(read_u16());
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::IO, "Block size");
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
u16 bits_per_sample = TRY(read_u16());
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::IO, "Bits per sample");
|
2021-06-05 17:23:27 -07:00
|
|
|
|
|
|
|
|
if (audio_format == WAVE_FORMAT_EXTENSIBLE) {
|
|
|
|
|
ok = ok && (fmt_size == 40);
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Format, "Extensible fmt size"); // value check
|
2021-06-05 17:23:27 -07:00
|
|
|
|
|
|
|
|
// Discard everything until the GUID.
|
|
|
|
|
// We've already read 16 bytes from the stream. The GUID starts in another 8 bytes.
|
2022-04-23 11:08:45 +02:00
|
|
|
TRY(read_u32());
|
|
|
|
|
TRY(read_u32());
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::IO, "Discard until GUID");
|
2021-06-05 17:23:27 -07:00
|
|
|
|
|
|
|
|
// Get the underlying audio format from the first two bytes of GUID
|
2022-04-23 11:08:45 +02:00
|
|
|
u16 guid_subformat = TRY(read_u16());
|
2021-06-05 17:23:27 -07:00
|
|
|
ok = ok && (guid_subformat == WAVE_FORMAT_PCM || guid_subformat == WAVE_FORMAT_IEEE_FLOAT);
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Unimplemented, "GUID SubFormat");
|
2021-06-05 17:23:27 -07:00
|
|
|
|
|
|
|
|
audio_format = guid_subformat;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 17:13:04 +02:00
|
|
|
if (audio_format == WAVE_FORMAT_PCM) {
|
|
|
|
|
ok = ok && (bits_per_sample == 8 || bits_per_sample == 16 || bits_per_sample == 24);
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Unimplemented, "Bits per sample (PCM)"); // value check
|
2021-04-26 17:13:04 +02:00
|
|
|
|
|
|
|
|
// We only support 8-24 bit audio right now because other formats are uncommon
|
|
|
|
|
if (bits_per_sample == 8) {
|
|
|
|
|
m_sample_format = PcmSampleFormat::Uint8;
|
|
|
|
|
} else if (bits_per_sample == 16) {
|
|
|
|
|
m_sample_format = PcmSampleFormat::Int16;
|
|
|
|
|
} else if (bits_per_sample == 24) {
|
|
|
|
|
m_sample_format = PcmSampleFormat::Int24;
|
|
|
|
|
}
|
|
|
|
|
} else if (audio_format == WAVE_FORMAT_IEEE_FLOAT) {
|
|
|
|
|
ok = ok && (bits_per_sample == 32 || bits_per_sample == 64);
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Unimplemented, "Bits per sample (Float)"); // value check
|
2021-04-26 17:13:04 +02:00
|
|
|
|
|
|
|
|
// Again, only the common 32 and 64 bit
|
|
|
|
|
if (bits_per_sample == 32) {
|
|
|
|
|
m_sample_format = PcmSampleFormat::Float32;
|
|
|
|
|
} else if (bits_per_sample == 64) {
|
|
|
|
|
m_sample_format = PcmSampleFormat::Float64;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 10:59:00 -07:00
|
|
|
ok = ok && (block_size_bytes == (m_num_channels * (bits_per_sample / 8)));
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Format, "Block size sanity check");
|
2021-06-10 10:59:00 -07:00
|
|
|
|
2021-05-01 21:10:08 +02:00
|
|
|
dbgln_if(AWAVLOADER_DEBUG, "WAV format {} at {} bit, {} channels, rate {}Hz ",
|
|
|
|
|
sample_format_name(m_sample_format), pcm_bits_per_sample(m_sample_format), m_num_channels, m_sample_rate);
|
2019-07-13 19:42:03 +02:00
|
|
|
|
|
|
|
|
// Read chunks until we find DATA
|
|
|
|
|
bool found_data = false;
|
2022-04-22 14:13:34 +02:00
|
|
|
u32 data_size = 0;
|
2020-06-18 20:00:32 +02:00
|
|
|
u8 search_byte = 0;
|
2019-07-14 00:28:30 +02:00
|
|
|
while (true) {
|
2022-04-23 11:08:45 +02:00
|
|
|
search_byte = TRY(read_u8());
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::IO, "Reading byte searching for data");
|
2021-09-06 03:29:52 +04:30
|
|
|
if (search_byte != 0x64) // D
|
2020-06-18 20:00:32 +02:00
|
|
|
continue;
|
|
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
search_byte = TRY(read_u8());
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::IO, "Reading next byte searching for data");
|
2021-09-06 03:29:52 +04:30
|
|
|
if (search_byte != 0x61) // A
|
2020-06-18 20:00:32 +02:00
|
|
|
continue;
|
|
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
u16 search_remaining = TRY(read_u16());
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::IO, "Reading remaining bytes searching for data");
|
2021-09-06 03:29:52 +04:30
|
|
|
if (search_remaining != 0x6174) // TA
|
2020-06-18 20:00:32 +02:00
|
|
|
continue;
|
|
|
|
|
|
2022-04-23 11:08:45 +02:00
|
|
|
data_size = TRY(read_u32());
|
2020-06-18 20:00:32 +02:00
|
|
|
found_data = true;
|
|
|
|
|
break;
|
2019-07-13 19:42:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ok = ok && found_data;
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Format, "Found no data chunk");
|
2019-07-14 00:28:30 +02:00
|
|
|
|
2022-04-22 14:13:34 +02:00
|
|
|
ok = ok && data_size < maximum_wav_size;
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
CHECK_OK(LoaderError::Category::Format, "Data was too large");
|
2019-07-14 00:28:30 +02:00
|
|
|
|
2022-04-22 14:13:34 +02:00
|
|
|
m_total_samples = data_size / block_size_bytes;
|
2019-07-28 21:52:30 +02:00
|
|
|
|
2021-06-05 10:14:03 -07:00
|
|
|
dbgln_if(AWAVLOADER_DEBUG, "WAV data size {}, bytes per sample {}, total samples {}",
|
2022-04-22 14:13:34 +02:00
|
|
|
data_size,
|
2021-06-10 10:59:00 -07:00
|
|
|
block_size_bytes,
|
2021-06-05 10:14:03 -07:00
|
|
|
m_total_samples);
|
|
|
|
|
|
2021-06-05 17:14:55 -07:00
|
|
|
m_byte_offset_of_data_samples = bytes_read;
|
LibAudio: New error propagation API in Loader and Buffer
Previously, a libc-like out-of-line error information was used in the
loader and its plugins. Now, all functions that may fail to do their job
return some sort of Result. The universally-used error type ist the new
LoaderError, which can contain information about the general error
category (such as file format, I/O, unimplemented features), an error
description, and location information, such as file index or sample
index.
Additionally, the loader plugins try to do as little work as possible in
their constructors. Right after being constructed, a user should call
initialize() and check the errors returned from there. (This is done
transparently by Loader itself.) If a constructor caused an error, the
call to initialize should check and return it immediately.
This opportunity was used to rework a lot of the internal error
propagation in both loader classes, especially FlacLoader. Therefore, a
couple of other refactorings may have sneaked in as well.
The adoption of LibAudio users is minimal. Piano's adoption is not
important, as the code will receive major refactoring in the near future
anyways. SoundPlayer's adoption is also less important, as changes to
refactor it are in the works as well. aplay's adoption is the best and
may serve as an example for other users. It also includes new buffering
behavior.
Buffer also gets some attention, making it OOM-safe and thereby also
propagating its errors to the user.
2021-11-27 17:00:19 +01:00
|
|
|
return {};
|
Work on AudioServer
The center of this is now an ABuffer class in LibAudio.
ABuffer contains ASample, which has two channels (left/right) in
floating point for mixing purposes, in 44100hz.
This means that the loaders (AWavLoader in this case) needs to do some
manipulation to get things in the right format, but that we don't need
to care after format loading is done.
While we're at it, do some correctness fixes. PCM data is unsigned if
it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16
bit audio, so give it what it wants.
On top of this, AudioServer now accepts requests to play a buffer.
The IPC mechanism here is pretty much a 1:1 copy-paste from
LibGUI/WindowServer. It can be generalized more in the future, but for
now I want to get AudioServer working decently first :)
Additionally, add a little "aplay" tool to load and play a WAV file. It
will break with large WAVs (run out of memory, heh...) but it's a start.
Future work needs to make AudioServer block buffer submission from
clients until it has played the buffer they are requesting to play.
2019-07-15 12:54:52 +02:00
|
|
|
}
|
|
|
|
|
}
|