1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:27:35 +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

@ -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;
}