1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:57:45 +00:00

Audio: Change how volume works

Across the entire audio system, audio now works in 0-1 terms instead of
0-100 as before. Therefore, volume is now a double instead of an int.
The master volume of the AudioServer changes smoothly through a
FadingProperty, preventing clicks. Finally, volume computations are done
with logarithmic scaling, which is more natural for the human ear.

Note that this could be 4-5 different commits, but as they change each
other's code all the time, it makes no sense to split them up.
This commit is contained in:
kleines Filmröllchen 2021-08-27 23:47:09 +02:00 committed by Andreas Kling
parent 2909c3a931
commit 152ec28da0
14 changed files with 190 additions and 45 deletions

View file

@ -40,7 +40,7 @@ Mixer::Mixer(NonnullRefPtr<Core::ConfigFile> config)
pthread_cond_init(&m_pending_cond, nullptr);
m_muted = m_config->read_bool_entry("Master", "Mute", false);
m_main_volume = m_config->read_num_entry("Master", "Volume", 100);
m_main_volume = static_cast<double>(m_config->read_num_entry("Master", "Volume", 100)) / 100.0;
m_sound_thread->start();
}
@ -78,18 +78,23 @@ void Mixer::mix()
Audio::Frame mixed_buffer[1024];
auto mixed_buffer_length = (int)(sizeof(mixed_buffer) / sizeof(Audio::Frame));
m_main_volume.advance_time();
int active_queues = 0;
// Mix the buffers together into the output
for (auto& queue : active_mix_queues) {
if (!queue->client()) {
queue->clear();
continue;
}
++active_queues;
for (int i = 0; i < mixed_buffer_length; ++i) {
auto& mixed_sample = mixed_buffer[i];
Audio::Frame sample;
if (!queue->get_next_sample(sample))
break;
sample.log_multiply(SAMPLE_HEADROOM);
mixed_sample += sample;
}
}
@ -103,7 +108,11 @@ void Mixer::mix()
for (int i = 0; i < mixed_buffer_length; ++i) {
auto& mixed_sample = mixed_buffer[i];
mixed_sample.scale(m_main_volume);
// Even though it's not realistic, the user expects no sound at 0%.
if (m_main_volume < 0.01)
mixed_sample = { 0 };
else
mixed_sample.log_multiply(m_main_volume);
mixed_sample.clip();
LittleEndian<i16> out_sample;
@ -121,20 +130,20 @@ void Mixer::mix()
}
}
void Mixer::set_main_volume(int volume)
void Mixer::set_main_volume(double volume)
{
if (volume < 0)
m_main_volume = 0;
else if (volume > 200)
m_main_volume = 200;
else if (volume > 2)
m_main_volume = 2;
else
m_main_volume = volume;
m_config->write_num_entry("Master", "Volume", volume);
m_config->write_num_entry("Master", "Volume", static_cast<int>(volume * 100));
request_setting_sync();
ClientConnection::for_each([&](ClientConnection& client) {
client.did_change_main_mix_volume({}, m_main_volume);
client.did_change_main_mix_volume({}, main_volume());
});
}