1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:47: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:
Robin Burchell 2019-07-15 12:54:52 +02:00 committed by Andreas Kling
parent 3db9706e57
commit 2df6f0e87f
19 changed files with 873 additions and 141 deletions

View file

@ -0,0 +1,68 @@
#pragma once
#include <AK/RefCounted.h>
#include <AK/ByteBuffer.h>
#include <AK/Types.h>
#include <AK/Vector.h>
// A single sample in an audio buffer.
// Values are floating point, and should range from -1.0 to +1.0
struct ASample {
ASample()
: left(0)
, right(0)
{}
// For mono
ASample(float left)
: left(left)
, right(left)
{}
// For stereo
ASample(float left, float right)
: left(left)
, right(right)
{}
void clamp()
{
if (left > 1)
left = 1;
else if (left < -1)
left = -1;
if (right > 1)
right = 1;
else if (right < -1)
right = -1;
}
ASample& operator+=(const ASample& other)
{
left += other.left;
right += other.right;
return *this;
}
float left;
float right;
};
// A buffer of audio samples, normalized to 44100hz.
class ABuffer : public RefCounted<ABuffer> {
public:
static RefPtr<ABuffer> from_pcm_data(ByteBuffer& data, int num_channels, int bits_per_sample, int source_rate);
ABuffer(Vector<ASample>& samples)
: m_samples(samples)
{}
const Vector<ASample>& samples() const { return m_samples; }
Vector<ASample>& samples() { return m_samples; }
const void* data() const { return m_samples.data(); }
int size_in_bytes() const { return m_samples.size() * sizeof(ASample); }
private:
Vector<ASample> m_samples;
};