serenity/Userland/Applications/SoundPlayer/PlaybackManager.h
kleines Filmröllchen b4fbd30b70 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.
2023-07-01 23:27:24 +01:00

63 lines
1.9 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/FixedArray.h>
#include <AK/Queue.h>
#include <AK/Vector.h>
#include <LibAudio/ConnectionToServer.h>
#include <LibAudio/Loader.h>
#include <LibAudio/Resampler.h>
#include <LibAudio/Sample.h>
#include <LibCore/Timer.h>
class PlaybackManager final {
public:
PlaybackManager(NonnullRefPtr<Audio::ConnectionToServer>);
~PlaybackManager() = default;
void play();
void stop();
void pause();
void seek(int const position);
void loop(bool);
bool toggle_pause();
void set_loader(NonnullRefPtr<Audio::Loader>&&);
RefPtr<Audio::Loader> loader() const { return m_loader; }
bool is_paused() const { return m_paused; }
float total_length() const { return m_total_length; }
FixedArray<Audio::Sample> const& current_buffer() const { return m_current_buffer; }
NonnullRefPtr<Audio::ConnectionToServer> connection() const { return m_connection; }
Function<void()> on_update;
Function<void()> on_finished_playing;
private:
// Number of buffers we want to always keep enqueued.
static constexpr size_t always_enqueued_buffer_count = 5;
void next_buffer();
void set_paused(bool);
bool m_paused { true };
bool m_loop = { false };
float m_total_length { 0 };
size_t m_samples_to_load_per_buffer { 0 };
RefPtr<Audio::Loader> m_loader { nullptr };
NonnullRefPtr<Audio::ConnectionToServer> m_connection;
FixedArray<Audio::Sample> m_current_buffer;
RefPtr<Core::Timer> m_timer;
// Controls the GUI update rate. A smaller value makes the visualizations nicer.
static constexpr u32 update_rate_ms = 50;
// Number of milliseconds of audio data contained in each audio buffer
static constexpr u32 buffer_size_ms = 100;
};