mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:57:35 +00:00
Work on AudioServer
The center of this is now an ABuffer class in LibAudio. ABuffer contains ASample, which has two channels (left/right) in floating point for mixing purposes, in 44100hz. This means that the loaders (AWavLoader in this case) needs to do some manipulation to get things in the right format, but that we don't need to care after format loading is done. While we're at it, do some correctness fixes. PCM data is unsigned if it's 8 bit, but 16 bit is signed. And /dev/audio also wants signed 16 bit audio, so give it what it wants. On top of this, AudioServer now accepts requests to play a buffer. The IPC mechanism here is pretty much a 1:1 copy-paste from LibGUI/WindowServer. It can be generalized more in the future, but for now I want to get AudioServer working decently first :) Additionally, add a little "aplay" tool to load and play a WAV file. It will break with large WAVs (run out of memory, heh...) but it's a start. Future work needs to make AudioServer block buffer submission from clients until it has played the buffer they are requesting to play.
This commit is contained in:
parent
3db9706e57
commit
2df6f0e87f
19 changed files with 873 additions and 141 deletions
112
Servers/AudioServer/ASMixer.cpp
Normal file
112
Servers/AudioServer/ASMixer.cpp
Normal file
|
@ -0,0 +1,112 @@
|
|||
#include <AK/BufferStream.h>
|
||||
#include <LibCore/CThread.h>
|
||||
|
||||
#include <limits>
|
||||
#include "ASMixer.h"
|
||||
|
||||
ASMixer::ASMixer()
|
||||
: m_device("/dev/audio")
|
||||
{
|
||||
if (!m_device.open(CIODevice::WriteOnly)) {
|
||||
dbgprintf("Can't open audio device: %s\n", m_device.error_string());
|
||||
return;
|
||||
}
|
||||
|
||||
CThread sound_thread([](void* context) -> int {
|
||||
ASMixer* mixer = (ASMixer*)context;
|
||||
mixer->mix();
|
||||
return 0;
|
||||
}, this);
|
||||
}
|
||||
|
||||
void ASMixer::queue(ASClientConnection&, const ABuffer& buffer)
|
||||
{
|
||||
ASSERT(buffer.size_in_bytes());
|
||||
CLocker lock(m_lock);
|
||||
m_pending_mixing.append(ASMixerBuffer(buffer));
|
||||
}
|
||||
|
||||
void ASMixer::mix()
|
||||
{
|
||||
Vector<ASMixerBuffer> active_mix_buffers;
|
||||
|
||||
for (;;) {
|
||||
{
|
||||
CLocker lock(m_lock);
|
||||
for (const auto& buf : m_pending_mixing)
|
||||
active_mix_buffers.append(buf);
|
||||
m_pending_mixing.clear();
|
||||
}
|
||||
|
||||
// ### use a wakeup of some kind rather than this garbage
|
||||
if (active_mix_buffers.size() == 0) {
|
||||
// nothing to mix yet
|
||||
usleep(10000);
|
||||
continue;
|
||||
}
|
||||
|
||||
int max_size = 0;
|
||||
|
||||
for (auto& buffer : active_mix_buffers) {
|
||||
if (buffer.done)
|
||||
continue;
|
||||
ASSERT(buffer.buffer->size_in_bytes()); // zero sized buffer? how?
|
||||
max_size = max(max_size, buffer.buffer->size_in_bytes() - buffer.pos);
|
||||
}
|
||||
|
||||
// ### clear up 'done' buffers more aggressively
|
||||
if (max_size == 0) {
|
||||
active_mix_buffers.clear();
|
||||
continue;
|
||||
}
|
||||
|
||||
max_size = min(1023, max_size);
|
||||
|
||||
Vector<ASample> mixed_buffer;
|
||||
mixed_buffer.resize(max_size);
|
||||
|
||||
// Mix the buffers together into the output
|
||||
for (auto& buffer : active_mix_buffers) {
|
||||
if (buffer.done)
|
||||
continue;
|
||||
auto& samples = buffer.buffer->samples();
|
||||
|
||||
for (int i = 0; i < max_size && buffer.pos < samples.size(); ++buffer.pos, ++i) {
|
||||
auto& mixed_sample = mixed_buffer[i];
|
||||
mixed_sample += samples[buffer.pos];
|
||||
}
|
||||
|
||||
// clear it later
|
||||
if (buffer.pos == samples.size())
|
||||
buffer.done = true;
|
||||
}
|
||||
|
||||
// output the mixed stuff to the device
|
||||
// max_size is 0 indexed, so add 1.
|
||||
const int output_buffer_byte_size = (max_size + 1) * 2 * 2;
|
||||
ASSERT(output_buffer_byte_size == 4096);
|
||||
ByteBuffer buffer(ByteBuffer::create_uninitialized(output_buffer_byte_size));
|
||||
BufferStream stream(buffer);
|
||||
|
||||
for (int i = 0; i < mixed_buffer.size(); ++i) {
|
||||
auto& mixed_sample = mixed_buffer[i];
|
||||
mixed_sample.clamp();
|
||||
|
||||
i16 out_sample;
|
||||
out_sample = mixed_sample.left * std::numeric_limits<i16>::max();
|
||||
stream << out_sample;
|
||||
|
||||
ASSERT(!stream.at_end()); // we should have enough space for both channels in one buffer!
|
||||
out_sample = mixed_sample.right * std::numeric_limits<i16>::max();
|
||||
stream << out_sample;
|
||||
|
||||
ASSERT(!stream.at_end());
|
||||
}
|
||||
|
||||
if (stream.offset() != 0) {
|
||||
buffer.trim(stream.offset());
|
||||
m_device.write(buffer);
|
||||
mixed_buffer.resize(0);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue