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
|
|
|
*/
|
|
|
|
|
|
2019-07-13 19:42:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
#include <AK/ByteString.h>
|
2022-04-23 12:11:32 +02:00
|
|
|
#include <AK/FixedArray.h>
|
2020-02-27 00:53:35 +11:00
|
|
|
#include <AK/OwnPtr.h>
|
2019-07-13 19:42:03 +02:00
|
|
|
#include <AK/RefPtr.h>
|
2022-01-14 01:14:24 +01:00
|
|
|
#include <AK/Span.h>
|
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
|
|
|
#include <AK/StringView.h>
|
2020-12-01 19:55:41 +01:00
|
|
|
#include <LibAudio/Loader.h>
|
2023-10-12 22:36:23 +02:00
|
|
|
#include <LibRIFF/RIFF.h>
|
2019-07-27 14:30:09 +02:00
|
|
|
|
2020-02-06 10:40:02 +01:00
|
|
|
namespace Audio {
|
|
|
|
|
|
2023-03-15 22:44:32 +01:00
|
|
|
// Loader for the WAVE (file extension .wav) uncompressed audio file format.
|
|
|
|
|
// WAVE uses the Microsoft RIFF container.
|
|
|
|
|
// Original RIFF Spec, without later extensions: https://www.aelius.com/njh/wavemetatools/doc/riffmci.pdf
|
|
|
|
|
// More concise WAVE information plus various spec links: http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html
|
2020-12-01 19:55:41 +01:00
|
|
|
class WavLoaderPlugin : public LoaderPlugin {
|
2019-07-13 19:42:03 +02:00
|
|
|
public:
|
2023-01-22 05:09:11 +01:00
|
|
|
explicit WavLoaderPlugin(NonnullOwnPtr<SeekableStream> stream);
|
2023-06-24 17:04:38 +02:00
|
|
|
|
|
|
|
|
static bool sniff(SeekableStream& stream);
|
|
|
|
|
static ErrorOr<NonnullOwnPtr<LoaderPlugin>, LoaderError> create(NonnullOwnPtr<SeekableStream>);
|
2019-07-27 17:20:41 +02: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
|
|
|
virtual ErrorOr<Vector<FixedArray<Sample>>, LoaderError> load_chunks(size_t samples_to_read_from_input) override;
|
2019-11-04 19:39:17 +01: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
|
|
|
virtual MaybeLoaderError reset() override { return seek(0); }
|
2021-06-05 17:14:55 -07:00
|
|
|
|
|
|
|
|
// sample_index 0 is the start of the raw audio sample data
|
|
|
|
|
// within the file/stream.
|
2022-04-22 14:13:34 +02:00
|
|
|
virtual MaybeLoaderError seek(int sample_index) override;
|
2020-12-01 19:55:41 +01:00
|
|
|
|
2022-04-22 14:13:34 +02:00
|
|
|
virtual int loaded_samples() override { return static_cast<int>(m_loaded_samples); }
|
|
|
|
|
virtual int total_samples() override { return static_cast<int>(m_total_samples); }
|
2020-12-01 19:55:41 +01:00
|
|
|
virtual u32 sample_rate() override { return m_sample_rate; }
|
|
|
|
|
virtual u16 num_channels() override { return m_num_channels; }
|
2023-12-16 17:49:34 +03:30
|
|
|
virtual ByteString format_name() override { return "RIFF WAVE (.wav)"; }
|
2021-04-26 17:13:04 +02:00
|
|
|
virtual PcmSampleFormat pcm_format() override { return m_sample_format; }
|
2019-07-28 21:52:30 +02:00
|
|
|
|
2019-07-13 19:42:03 +02:00
|
|
|
private:
|
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 parse_header();
|
2023-10-12 22:36:23 +02:00
|
|
|
MaybeLoaderError load_wav_info_block(Vector<RIFF::OwnedChunk> info_chunks);
|
2020-12-01 19:55:41 +01:00
|
|
|
|
2023-06-27 12:04:29 +01:00
|
|
|
LoaderSamples samples_from_pcm_data(ReadonlyBytes data, size_t samples_to_read) const;
|
2022-04-23 12:11:32 +02:00
|
|
|
template<typename SampleReader>
|
2023-02-10 01:00:18 +01:00
|
|
|
MaybeLoaderError read_samples_from_stream(Stream& stream, SampleReader read_sample, FixedArray<Sample>& samples) const;
|
2022-04-23 12:11:32 +02:00
|
|
|
|
2019-07-27 17:20:41 +02:00
|
|
|
u32 m_sample_rate { 0 };
|
|
|
|
|
u16 m_num_channels { 0 };
|
2021-04-26 17:13:04 +02:00
|
|
|
PcmSampleFormat m_sample_format;
|
2021-06-05 17:14:55 -07:00
|
|
|
size_t m_byte_offset_of_data_samples { 0 };
|
2019-07-28 21:52:30 +02:00
|
|
|
|
2022-04-22 14:13:34 +02:00
|
|
|
size_t m_loaded_samples { 0 };
|
|
|
|
|
size_t m_total_samples { 0 };
|
2019-07-13 19:42:03 +02:00
|
|
|
};
|
2020-02-06 10:40:02 +01:00
|
|
|
|
|
|
|
|
}
|