mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 19:37:34 +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:
parent
d52a2ff10e
commit
b4fbd30b70
20 changed files with 100 additions and 93 deletions
|
@ -8,9 +8,8 @@ endpoint AudioServer
|
|||
get_self_volume() => (double volume)
|
||||
set_self_volume(double volume) => ()
|
||||
|
||||
// FIXME: Decouple client sample rate from device sample rate, then remove this API
|
||||
get_sample_rate() => (u32 sample_rate)
|
||||
|
||||
set_self_sample_rate(u32 sample_rate) => ()
|
||||
get_self_sample_rate() => (u32 sample_rate)
|
||||
// Buffer playback
|
||||
set_buffer(Audio::AudioQueue buffer) => ()
|
||||
clear_buffer() =|
|
||||
|
|
|
@ -40,8 +40,11 @@ void ConnectionFromClient::set_buffer(Audio::AudioQueue const& buffer)
|
|||
did_misbehave("Received an invalid buffer");
|
||||
return;
|
||||
}
|
||||
if (!m_queue)
|
||||
if (!m_queue) {
|
||||
m_queue = m_mixer.create_queue(*this);
|
||||
if (m_saved_sample_rate.has_value())
|
||||
m_queue->set_sample_rate(m_saved_sample_rate.release_value());
|
||||
}
|
||||
|
||||
// This is ugly but we know nobody uses the buffer afterwards anyways.
|
||||
m_queue->set_buffer(make<Audio::AudioQueue>(move(const_cast<Audio::AudioQueue&>(buffer))));
|
||||
|
@ -52,9 +55,19 @@ void ConnectionFromClient::did_change_client_volume(Badge<ClientAudioStream>, do
|
|||
async_client_volume_changed(volume);
|
||||
}
|
||||
|
||||
Messages::AudioServer::GetSampleRateResponse ConnectionFromClient::get_sample_rate()
|
||||
Messages::AudioServer::GetSelfSampleRateResponse ConnectionFromClient::get_self_sample_rate()
|
||||
{
|
||||
return { m_mixer.audiodevice_get_sample_rate() };
|
||||
if (m_queue)
|
||||
return { m_queue->sample_rate() };
|
||||
// Fall back to device sample rate since that would mean no resampling.
|
||||
return m_mixer.audiodevice_get_sample_rate();
|
||||
}
|
||||
void ConnectionFromClient::set_self_sample_rate(u32 sample_rate)
|
||||
{
|
||||
if (m_queue)
|
||||
m_queue->set_sample_rate(sample_rate);
|
||||
else
|
||||
m_saved_sample_rate = sample_rate;
|
||||
}
|
||||
|
||||
Messages::AudioServer::GetSelfVolumeResponse ConnectionFromClient::get_self_volume()
|
||||
|
|
|
@ -40,11 +40,12 @@ private:
|
|||
virtual void pause_playback() override;
|
||||
virtual Messages::AudioServer::IsSelfMutedResponse is_self_muted() override;
|
||||
virtual void set_self_muted(bool) override;
|
||||
// FIXME: Decouple client sample rate from device sample rate, then remove this endpoint
|
||||
virtual Messages::AudioServer::GetSampleRateResponse get_sample_rate() override;
|
||||
virtual Messages::AudioServer::GetSelfSampleRateResponse get_self_sample_rate() override;
|
||||
virtual void set_self_sample_rate(u32 sample_rate) override;
|
||||
|
||||
Mixer& m_mixer;
|
||||
RefPtr<ClientAudioStream> m_queue;
|
||||
Optional<u32> m_saved_sample_rate {};
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -39,6 +39,7 @@ Mixer::Mixer(NonnullRefPtr<Core::ConfigFile> config, NonnullOwnPtr<Core::File> d
|
|||
NonnullRefPtr<ClientAudioStream> Mixer::create_queue(ConnectionFromClient& client)
|
||||
{
|
||||
auto queue = adopt_ref(*new ClientAudioStream(client));
|
||||
queue->set_sample_rate(audiodevice_get_sample_rate());
|
||||
{
|
||||
Threading::MutexLocker const locker(m_pending_mutex);
|
||||
m_pending_mixing.append(*queue);
|
||||
|
@ -83,7 +84,7 @@ void Mixer::mix()
|
|||
|
||||
for (auto& mixed_sample : mixed_buffer) {
|
||||
Audio::Sample sample;
|
||||
if (!queue->get_next_sample(sample))
|
||||
if (!queue->get_next_sample(sample, audiodevice_get_sample_rate()))
|
||||
break;
|
||||
if (queue->is_muted())
|
||||
continue;
|
||||
|
@ -155,15 +156,22 @@ int Mixer::audiodevice_set_sample_rate(u32 sample_rate)
|
|||
int code = ioctl(m_device->fd(), SOUNDCARD_IOCTL_SET_SAMPLE_RATE, sample_rate);
|
||||
if (code != 0)
|
||||
dbgln("Error while setting sample rate to {}: ioctl error: {}", sample_rate, strerror(errno));
|
||||
// Note that the effective sample rate may be different depending on device restrictions.
|
||||
// Therefore, we delete our cache, but for efficency don't immediately read the sample rate back.
|
||||
m_cached_sample_rate = {};
|
||||
return code;
|
||||
}
|
||||
|
||||
u32 Mixer::audiodevice_get_sample_rate() const
|
||||
{
|
||||
if (m_cached_sample_rate.has_value())
|
||||
return m_cached_sample_rate.value();
|
||||
u32 sample_rate = 0;
|
||||
int code = ioctl(m_device->fd(), SOUNDCARD_IOCTL_GET_SAMPLE_RATE, &sample_rate);
|
||||
if (code != 0)
|
||||
dbgln("Error while getting sample rate: ioctl error: {}", strerror(errno));
|
||||
else
|
||||
m_cached_sample_rate = sample_rate;
|
||||
return sample_rate;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include <AK/RefCounted.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
#include <LibAudio/Queue.h>
|
||||
#include <LibAudio/Resampler.h>
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/Timer.h>
|
||||
|
@ -42,7 +43,7 @@ public:
|
|||
explicit ClientAudioStream(ConnectionFromClient&);
|
||||
~ClientAudioStream() = default;
|
||||
|
||||
bool get_next_sample(Audio::Sample& sample)
|
||||
bool get_next_sample(Audio::Sample& sample, u32 audiodevice_sample_rate)
|
||||
{
|
||||
// Note: Even though we only check client state here, we will probably close the client much earlier.
|
||||
if (!is_connected())
|
||||
|
@ -60,7 +61,19 @@ public:
|
|||
|
||||
return false;
|
||||
}
|
||||
m_current_audio_chunk = result.release_value();
|
||||
// FIXME: Our resampler and the way we resample here are bad.
|
||||
// Ideally, we should both do perfect band-corrected resampling,
|
||||
// as well as carry resampling state over between buffers.
|
||||
auto attempted_resample = Audio::ResampleHelper<Audio::Sample> {
|
||||
m_sample_rate == 0 ? audiodevice_sample_rate : m_sample_rate, audiodevice_sample_rate
|
||||
}
|
||||
.try_resample(result.release_value());
|
||||
if (attempted_resample.is_error())
|
||||
return false;
|
||||
|
||||
// If the sample rate changes underneath us, we will still play the existing buffer unchanged until we're done.
|
||||
// This is not a significant problem since the buffers are very small (~100 samples or less).
|
||||
m_current_audio_chunk = attempted_resample.release_value();
|
||||
m_in_chunk_location = 0;
|
||||
}
|
||||
|
||||
|
@ -90,14 +103,21 @@ public:
|
|||
void set_volume(double const volume) { m_volume = volume; }
|
||||
bool is_muted() const { return m_muted; }
|
||||
void set_muted(bool muted) { m_muted = muted; }
|
||||
u32 sample_rate() const { return m_sample_rate; }
|
||||
void set_sample_rate(u32 sample_rate)
|
||||
{
|
||||
dbgln_if(AUDIO_DEBUG, "queue {} got sample rate {} Hz", m_client->client_id(), sample_rate);
|
||||
m_sample_rate = sample_rate;
|
||||
}
|
||||
|
||||
private:
|
||||
OwnPtr<Audio::AudioQueue> m_buffer;
|
||||
Array<Audio::Sample, Audio::AUDIO_BUFFER_SIZE> m_current_audio_chunk;
|
||||
Vector<Audio::Sample> m_current_audio_chunk;
|
||||
size_t m_in_chunk_location;
|
||||
|
||||
bool m_paused { true };
|
||||
bool m_muted { false };
|
||||
u32 m_sample_rate;
|
||||
|
||||
WeakPtr<ConnectionFromClient> m_client;
|
||||
FadingProperty<double> m_volume { 1 };
|
||||
|
@ -137,6 +157,7 @@ private:
|
|||
Threading::ConditionVariable m_mixing_necessary { m_pending_mutex };
|
||||
|
||||
NonnullOwnPtr<Core::File> m_device;
|
||||
mutable Optional<u32> m_cached_sample_rate {};
|
||||
|
||||
NonnullRefPtr<Threading::Thread> m_sound_thread;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue