2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
2022-01-14 14:45:33 +03:00
|
|
|
* Copyright (c) 2021-2022, kleines Filmröllchen <filmroellchen@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +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 19:00:19 +03:00
|
|
|
#include <AK/Types.h>
|
2020-02-06 17:18:03 +03:00
|
|
|
#include <LibAudio/ClientConnection.h>
|
2020-12-01 22:20:46 +03:00
|
|
|
#include <LibAudio/Loader.h>
|
2020-08-06 00:07:33 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2021-11-26 00:07:03 +03:00
|
|
|
#include <LibMain/Main.h>
|
2022-01-14 14:45:33 +03:00
|
|
|
#include <math.h>
|
2019-09-22 01:17:53 +03:00
|
|
|
#include <stdio.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 13:54:52 +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 19:00:19 +03:00
|
|
|
// The Kernel has issues with very large anonymous buffers.
|
|
|
|
// FIXME: This appears to be fine for now, but it's really a hack.
|
|
|
|
constexpr size_t LOAD_CHUNK_SIZE = 128 * KiB;
|
|
|
|
|
2021-11-26 00:07:03 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
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 13:54:52 +03:00
|
|
|
{
|
2020-08-06 00:07:33 +03:00
|
|
|
const char* path = nullptr;
|
2020-10-14 23:45:19 +03:00
|
|
|
bool should_loop = false;
|
2022-01-14 14:45:33 +03:00
|
|
|
bool show_sample_progress = false;
|
2020-08-06 00:07:33 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2020-12-01 22:20:46 +03:00
|
|
|
args_parser.add_positional_argument(path, "Path to audio file", "path");
|
2020-10-14 23:45:19 +03:00
|
|
|
args_parser.add_option(should_loop, "Loop playback", "loop", 'l');
|
2022-01-14 14:45:33 +03:00
|
|
|
args_parser.add_option(show_sample_progress, "Show playback progress in samples", "sample-progress", 's');
|
2021-11-26 00:07:03 +03:00
|
|
|
args_parser.parse(arguments);
|
2020-08-06 00:07:33 +03:00
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::EventLoop loop;
|
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 13:54:52 +03:00
|
|
|
|
2022-01-14 16:12:49 +03:00
|
|
|
auto audio_client = TRY(Audio::ClientConnection::try_create());
|
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 19:00:19 +03:00
|
|
|
auto maybe_loader = Audio::Loader::create(path);
|
|
|
|
if (maybe_loader.is_error()) {
|
|
|
|
warnln("Failed to load audio file: {}", maybe_loader.error().description);
|
2020-02-09 17:53:10 +03:00
|
|
|
return 1;
|
|
|
|
}
|
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 19:00:19 +03:00
|
|
|
auto loader = maybe_loader.release_value();
|
2019-07-27 18:20:41 +03:00
|
|
|
|
2021-05-31 17:43:25 +03:00
|
|
|
outln("\033[34;1m Playing\033[0m: {}", path);
|
2022-01-14 14:44:41 +03:00
|
|
|
outln("\033[34;1m Format\033[0m: {} {} Hz, {}-bit, {}",
|
|
|
|
loader->format_name(),
|
2020-12-01 22:20:46 +03:00
|
|
|
loader->sample_rate(),
|
|
|
|
loader->bits_per_sample(),
|
|
|
|
loader->num_channels() == 1 ? "Mono" : "Stereo");
|
2021-05-31 17:43:25 +03:00
|
|
|
out("\033[34;1mProgress\033[0m: \033[s");
|
2021-08-19 01:13:26 +03:00
|
|
|
|
|
|
|
auto resampler = Audio::ResampleHelper<double>(loader->sample_rate(), audio_client->get_sample_rate());
|
|
|
|
|
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 19:00:19 +03:00
|
|
|
// If we're downsampling, we need to appropriately load more samples at once.
|
|
|
|
size_t const load_size = static_cast<size_t>(LOAD_CHUNK_SIZE * static_cast<double>(loader->sample_rate()) / static_cast<double>(audio_client->get_sample_rate()));
|
|
|
|
// We assume that the loader can load samples at at least 2x speed (testing confirms 9x-12x for FLAC, 14x for WAV).
|
|
|
|
// Therefore, when the server-side buffer can only play as long as the time it takes us to load a chunk,
|
|
|
|
// we give it new data.
|
|
|
|
int const min_buffer_size = load_size / 2;
|
|
|
|
|
2019-07-27 18:20:41 +03:00
|
|
|
for (;;) {
|
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 19:00:19 +03:00
|
|
|
auto samples = loader->get_more_samples(load_size);
|
|
|
|
if (!samples.is_error()) {
|
|
|
|
if (samples.value()->sample_count() > 0) {
|
|
|
|
// We can read and enqueue more samples
|
|
|
|
out("\033[u");
|
2022-01-14 14:45:33 +03:00
|
|
|
if (show_sample_progress) {
|
|
|
|
out("{}/{}", loader->loaded_samples(), loader->total_samples());
|
|
|
|
} else {
|
|
|
|
auto playing_seconds = static_cast<int>(floor(static_cast<double>(loader->loaded_samples()) / static_cast<double>(loader->sample_rate())));
|
|
|
|
auto playing_minutes = playing_seconds / 60;
|
|
|
|
auto playing_seconds_of_minute = playing_seconds % 60;
|
|
|
|
|
|
|
|
auto total_seconds = static_cast<int>(floor(static_cast<double>(loader->total_samples()) / static_cast<double>(loader->sample_rate())));
|
|
|
|
auto total_minutes = total_seconds / 60;
|
|
|
|
auto total_seconds_of_minute = total_seconds % 60;
|
|
|
|
|
|
|
|
auto remaining_seconds = total_seconds - playing_seconds;
|
|
|
|
auto remaining_minutes = remaining_seconds / 60;
|
|
|
|
auto remaining_seconds_of_minute = remaining_seconds % 60;
|
|
|
|
|
|
|
|
out("\033[1m{:02d}:{:02d}\033[0m [{}{:02d}:{:02d}] -- {:02d}:{:02d}",
|
|
|
|
playing_minutes, playing_seconds_of_minute,
|
|
|
|
remaining_seconds == 0 ? " " : "-",
|
|
|
|
remaining_minutes, remaining_seconds_of_minute,
|
|
|
|
total_minutes, total_seconds_of_minute);
|
|
|
|
}
|
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 19:00:19 +03:00
|
|
|
fflush(stdout);
|
|
|
|
resampler.reset();
|
|
|
|
auto resampled_samples = TRY(Audio::resample_buffer(resampler, *samples.value()));
|
|
|
|
audio_client->async_enqueue(*resampled_samples);
|
|
|
|
} else if (should_loop) {
|
|
|
|
// We're done: now loop
|
|
|
|
auto result = loader->reset();
|
|
|
|
if (result.is_error()) {
|
|
|
|
outln();
|
|
|
|
outln("Error while resetting: {} (at {:x})", result.error().description, result.error().index);
|
|
|
|
}
|
|
|
|
} else if (samples.value()->sample_count() == 0 && audio_client->get_remaining_samples() == 0) {
|
|
|
|
// We're done and the server is done
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (audio_client->get_remaining_samples() > min_buffer_size) {
|
|
|
|
// The server has enough data for now
|
|
|
|
sleep(1);
|
|
|
|
}
|
2019-10-19 20:15:40 +03:00
|
|
|
} else {
|
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 19:00:19 +03:00
|
|
|
outln();
|
|
|
|
outln("Error: {} (at {:x})", samples.error().description, samples.error().index);
|
|
|
|
return 1;
|
2019-10-19 20:15:40 +03:00
|
|
|
}
|
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 13:54:52 +03:00
|
|
|
}
|
2021-05-31 17:43:25 +03:00
|
|
|
outln();
|
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 13:54:52 +03:00
|
|
|
return 0;
|
|
|
|
}
|