1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:14:58 +00:00

AudioServer+Userland: Decouple client sample rates from device rate

This change was a long time in the making ever since we obtained sample
rate awareness in the system. Now, each client has its own sample rate,
accessible via new IPC APIs, and the device sample rate is only
accessible via the management interface. AudioServer takes care of
resampling client streams into the device sample rate. Therefore, the
main improvement introduced with this commit is full responsiveness to
sample rate changes; all open audio programs will continue to play at
correct speed with the audio resampled to the new device rate.

The immediate benefits are manifold:
- Gets rid of the legacy hardware sample rate IPC message in the
  non-managing client
- Removes duplicate resampling and sample index rescaling code
  everywhere
- Avoids potential sample index scaling bugs in SoundPlayer (which have
  happened many times before) and fixes a sample index scaling bug in
  aplay
- Removes several FIXMEs
- Reduces amount of sample copying in all applications (especially
  Piano, where this is critical), improving performance
- Reduces number of resampling users, making future API changes (which
  will need to happen for correct resampling to be implemented) easier

I also threw in a simple race condition fix for Piano's audio player
loop.
This commit is contained in:
kleines Filmröllchen 2023-06-24 13:42:06 +02:00 committed by Linus Groh
parent d52a2ff10e
commit b4fbd30b70
20 changed files with 100 additions and 93 deletions

View file

@ -8,7 +8,6 @@
#include <AK/Types.h>
#include <LibAudio/ConnectionToServer.h>
#include <LibAudio/Loader.h>
#include <LibAudio/Resampler.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
#include <LibCore/System.h>
@ -17,8 +16,6 @@
#include <math.h>
#include <stdio.h>
// 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;
ErrorOr<int> serenity_main(Main::Arguments arguments)
@ -62,10 +59,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
loader->num_channels() == 1 ? "Mono" : "Stereo");
out("\033[34;1mProgress\033[0m: \033[s");
auto resampler = Audio::ResampleHelper<Audio::Sample>(loader->sample_rate(), audio_client->get_sample_rate());
// 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()));
audio_client->set_self_sample_rate(loader->sample_rate());
auto print_playback_update = [&]() {
out("\033[u");
@ -94,14 +88,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
};
for (;;) {
auto samples = loader->get_more_samples(load_size);
auto samples = loader->get_more_samples(LOAD_CHUNK_SIZE);
if (!samples.is_error()) {
if (samples.value().size() > 0) {
print_playback_update();
// We can read and enqueue more samples
resampler.reset();
auto resampled_samples = resampler.resample(move(samples.value()));
TRY(audio_client->async_enqueue(move(resampled_samples)));
TRY(audio_client->async_enqueue(samples.release_value()));
} else if (should_loop) {
// We're done: now loop
auto result = loader->reset();
@ -113,7 +105,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
// We're done and the server is done
break;
}
while (audio_client->remaining_samples() > load_size) {
while (audio_client->remaining_samples() > LOAD_CHUNK_SIZE) {
// The server has enough data for now
print_playback_update();
usleep(1'000'000 / 10);