1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 02:28:12 +00:00

AudioServer: Make hardware write buffer size flexible

This removes some old cruft to refactor the hardware buffer-related
datastructures into depending on a single constant, which determines the
number of samples per hardware buffer that the audio server mixes. This
is set to 1024 as before, so there are no functional changes.
This commit is contained in:
kleines Filmröllchen 2022-06-15 21:43:07 +02:00 committed by Linus Groh
parent f6af357763
commit 1c23a222b2
2 changed files with 11 additions and 12 deletions

View file

@ -20,8 +20,6 @@
namespace AudioServer {
u8 Mixer::m_zero_filled_buffer[4096];
Mixer::Mixer(NonnullRefPtr<Core::ConfigFile> config)
// FIXME: Allow AudioServer to use other audio channels as well
: m_device(Core::File::construct("/dev/audio/0", this))
@ -74,8 +72,7 @@ void Mixer::mix()
active_mix_queues.remove_all_matching([&](auto& entry) { return !entry->client(); });
Audio::Sample mixed_buffer[1024];
auto mixed_buffer_length = (int)(sizeof(mixed_buffer) / sizeof(Audio::Sample));
Array<Audio::Sample, HARDWARE_BUFFER_SIZE> mixed_buffer;
m_main_volume.advance_time();
@ -89,8 +86,7 @@ void Mixer::mix()
++active_queues;
queue->volume().advance_time();
for (int i = 0; i < mixed_buffer_length; ++i) {
auto& mixed_sample = mixed_buffer[i];
for (auto& mixed_sample : mixed_buffer) {
Audio::Sample sample;
if (!queue->get_next_sample(sample))
break;
@ -103,13 +99,12 @@ void Mixer::mix()
}
if (m_muted) {
m_device->write(m_zero_filled_buffer, sizeof(m_zero_filled_buffer));
m_device->write(m_zero_filled_buffer.data(), static_cast<int>(m_zero_filled_buffer.size()));
} else {
Array<u8, 4096> buffer;
Array<u8, HARDWARE_BUFFER_SIZE_BYTES> buffer;
OutputMemoryStream stream { buffer };
for (int i = 0; i < mixed_buffer_length; ++i) {
auto& mixed_sample = mixed_buffer[i];
for (auto& mixed_sample : mixed_buffer) {
// Even though it's not realistic, the user expects no sound at 0%.
if (m_main_volume < 0.01)
@ -128,7 +123,7 @@ void Mixer::mix()
VERIFY(stream.is_end());
VERIFY(!stream.has_any_error());
m_device->write(stream.data(), stream.size());
m_device->write(stream.data(), static_cast<int>(stream.size()));
}
}
}

View file

@ -29,6 +29,10 @@ namespace AudioServer {
// Headroom, i.e. fixed attenuation for all audio streams.
// This is to prevent clipping when two streams with low headroom (e.g. normalized & compressed) are playing.
constexpr double SAMPLE_HEADROOM = 0.95;
// The size of the buffer in samples that the hardware receives through write() calls to the audio device.
constexpr size_t HARDWARE_BUFFER_SIZE = 1024;
// The hardware buffer size in bytes; there's two channels of 16-bit samples.
constexpr size_t HARDWARE_BUFFER_SIZE_BYTES = HARDWARE_BUFFER_SIZE * 2 * sizeof(i16);
class ConnectionFromClient;
@ -129,7 +133,7 @@ private:
NonnullRefPtr<Core::ConfigFile> m_config;
RefPtr<Core::Timer> m_config_write_timer;
static u8 m_zero_filled_buffer[4096];
Array<u8, HARDWARE_BUFFER_SIZE_BYTES> m_zero_filled_buffer;
void mix();
};