2020-12-02 11:40:32 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-13 08:07:00 -03:00
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2020-12-02 11:40:32 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-02 11:40:32 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-06-25 08:37:38 -03:00
|
|
|
#include "Buffer.h"
|
2021-01-17 04:43:58 -03:00
|
|
|
#include <AK/Atomic.h>
|
2021-04-26 12:13:04 -03:00
|
|
|
#include <AK/Debug.h>
|
2021-08-16 19:44:42 -03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2021-04-26 12:13:04 -03:00
|
|
|
#include <AK/String.h>
|
2020-12-02 11:40:32 -03:00
|
|
|
|
|
|
|
|
namespace Audio {
|
|
|
|
|
|
2021-01-17 04:43:58 -03:00
|
|
|
i32 Buffer::allocate_id()
|
|
|
|
|
{
|
|
|
|
|
static Atomic<i32> next_id;
|
|
|
|
|
return next_id++;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 11:40:32 -03:00
|
|
|
template<typename SampleReader>
|
2021-09-23 16:16:03 -03:00
|
|
|
static void read_samples_from_stream(InputMemoryStream& stream, SampleReader read_sample, Vector<Sample>& samples, int num_channels)
|
2020-12-02 11:40:32 -03:00
|
|
|
{
|
2021-10-06 11:56:24 -03:00
|
|
|
double left_channel_sample = 0;
|
|
|
|
|
double right_channel_sample = 0;
|
2020-12-02 11:40:32 -03:00
|
|
|
|
|
|
|
|
switch (num_channels) {
|
|
|
|
|
case 1:
|
|
|
|
|
for (;;) {
|
2021-10-06 11:56:24 -03:00
|
|
|
left_channel_sample = read_sample(stream);
|
|
|
|
|
samples.append(Sample(left_channel_sample));
|
2020-12-02 11:40:32 -03:00
|
|
|
|
|
|
|
|
if (stream.handle_any_error()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
for (;;) {
|
2021-10-06 11:56:24 -03:00
|
|
|
left_channel_sample = read_sample(stream);
|
|
|
|
|
right_channel_sample = read_sample(stream);
|
|
|
|
|
samples.append(Sample(left_channel_sample, right_channel_sample));
|
2020-12-02 11:40:32 -03:00
|
|
|
|
|
|
|
|
if (stream.handle_any_error()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-12-02 11:40:32 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-26 12:13:04 -03:00
|
|
|
static double read_float_sample_64(InputMemoryStream& stream)
|
|
|
|
|
{
|
|
|
|
|
LittleEndian<double> sample;
|
|
|
|
|
stream >> sample;
|
|
|
|
|
return double(sample);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double read_float_sample_32(InputMemoryStream& stream)
|
|
|
|
|
{
|
|
|
|
|
LittleEndian<float> sample;
|
|
|
|
|
stream >> sample;
|
|
|
|
|
return double(sample);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-02 11:40:32 -03:00
|
|
|
static double read_norm_sample_24(InputMemoryStream& stream)
|
|
|
|
|
{
|
|
|
|
|
u8 byte = 0;
|
|
|
|
|
stream >> byte;
|
|
|
|
|
u32 sample1 = byte;
|
|
|
|
|
stream >> byte;
|
|
|
|
|
u32 sample2 = byte;
|
|
|
|
|
stream >> byte;
|
|
|
|
|
u32 sample3 = byte;
|
|
|
|
|
|
|
|
|
|
i32 value = 0;
|
|
|
|
|
value = sample1 << 8;
|
|
|
|
|
value |= (sample2 << 16);
|
|
|
|
|
value |= (sample3 << 24);
|
|
|
|
|
return double(value) / NumericLimits<i32>::max();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double read_norm_sample_16(InputMemoryStream& stream)
|
|
|
|
|
{
|
|
|
|
|
LittleEndian<i16> sample;
|
|
|
|
|
stream >> sample;
|
|
|
|
|
return double(sample) / NumericLimits<i16>::max();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static double read_norm_sample_8(InputMemoryStream& stream)
|
|
|
|
|
{
|
|
|
|
|
u8 sample = 0;
|
|
|
|
|
stream >> sample;
|
|
|
|
|
return double(sample) / NumericLimits<u8>::max();
|
|
|
|
|
}
|
2020-12-02 11:44:45 -03: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 13:00:19 -03:00
|
|
|
ErrorOr<NonnullRefPtr<Buffer>> Buffer::from_pcm_data(ReadonlyBytes data, int num_channels, PcmSampleFormat sample_format)
|
2020-12-02 11:40:32 -03:00
|
|
|
{
|
|
|
|
|
InputMemoryStream stream { data };
|
2021-08-18 19:13:26 -03:00
|
|
|
return from_pcm_stream(stream, num_channels, sample_format, data.size() / (pcm_bits_per_sample(sample_format) / 8));
|
2020-12-02 11:40:32 -03: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 13:00:19 -03:00
|
|
|
ErrorOr<NonnullRefPtr<Buffer>> Buffer::from_pcm_stream(InputMemoryStream& stream, int num_channels, PcmSampleFormat sample_format, int num_samples)
|
2020-12-02 11:40:32 -03:00
|
|
|
{
|
2021-09-23 16:16:03 -03:00
|
|
|
Vector<Sample> fdata;
|
2020-12-02 11:40:32 -03:00
|
|
|
fdata.ensure_capacity(num_samples);
|
|
|
|
|
|
2021-04-26 12:13:04 -03:00
|
|
|
switch (sample_format) {
|
|
|
|
|
case PcmSampleFormat::Uint8:
|
2021-08-18 19:13:26 -03:00
|
|
|
read_samples_from_stream(stream, read_norm_sample_8, fdata, num_channels);
|
2020-12-02 11:40:32 -03:00
|
|
|
break;
|
2021-04-26 12:13:04 -03:00
|
|
|
case PcmSampleFormat::Int16:
|
2021-08-18 19:13:26 -03:00
|
|
|
read_samples_from_stream(stream, read_norm_sample_16, fdata, num_channels);
|
2020-12-02 11:40:32 -03:00
|
|
|
break;
|
2021-04-26 12:13:04 -03:00
|
|
|
case PcmSampleFormat::Int24:
|
2021-08-18 19:13:26 -03:00
|
|
|
read_samples_from_stream(stream, read_norm_sample_24, fdata, num_channels);
|
2020-12-02 11:40:32 -03:00
|
|
|
break;
|
2021-04-26 12:13:04 -03:00
|
|
|
case PcmSampleFormat::Float32:
|
2021-08-18 19:13:26 -03:00
|
|
|
read_samples_from_stream(stream, read_float_sample_32, fdata, num_channels);
|
2021-04-26 12:13:04 -03:00
|
|
|
break;
|
|
|
|
|
case PcmSampleFormat::Float64:
|
2021-08-18 19:13:26 -03:00
|
|
|
read_samples_from_stream(stream, read_float_sample_64, fdata, num_channels);
|
2021-04-26 12:13:04 -03:00
|
|
|
break;
|
2020-12-02 11:40:32 -03:00
|
|
|
default:
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-12-02 11:40:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We should handle this in a better way above, but for now --
|
|
|
|
|
// just make sure we're good. Worst case we just write some 0s where they
|
|
|
|
|
// don't belong.
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(!stream.handle_any_error());
|
2020-12-02 11:40:32 -03:00
|
|
|
|
|
|
|
|
return Buffer::create_with_samples(move(fdata));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|