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

@ -3,9 +3,9 @@
#include <limits>
#include "AWavLoader.h"
#include "AWavFile.h"
#include "ABuffer.h"
RefPtr<AWavFile> AWavLoader::load_wav(const StringView& path)
RefPtr<ABuffer> AWavLoader::load_wav(const StringView& path)
{
m_error_string = {};
@ -20,7 +20,7 @@ RefPtr<AWavFile> AWavLoader::load_wav(const StringView& path)
}
// TODO: A streaming parser might be better than forcing a ByteBuffer
RefPtr<AWavFile> AWavLoader::parse_wav(ByteBuffer& buffer)
RefPtr<ABuffer> AWavLoader::parse_wav(ByteBuffer& buffer)
{
BufferStream stream(buffer);
@ -62,36 +62,30 @@ RefPtr<AWavFile> AWavLoader::parse_wav(ByteBuffer& buffer)
CHECK_OK("FMT size");
ASSERT(fmt_size == 16);
auto ret = adopt(*new AWavFile);
u16 audio_format; stream >> audio_format;
CHECK_OK("Audio format"); // incomplete read check
ok = ok && audio_format == 1; // WAVE_FORMAT_PCM
ASSERT(audio_format == 1);
CHECK_OK("Audio format"); // value check
ret->m_format = AWavFile::Format::PCM;
u16 num_channels; stream >> num_channels;
ok = ok && (num_channels == 1 || num_channels == 2);
CHECK_OK("Channel count");
ret->m_channel_count = num_channels;
u32 sample_rate; stream >> sample_rate;
CHECK_OK("Sample rate");
ret->m_sample_rate = sample_rate;
u32 byte_rate; stream >> byte_rate;
CHECK_OK("Byte rate");
ret->m_byte_rate = byte_rate;
u16 block_align; stream >> block_align;
CHECK_OK("Block align");
ret->m_block_align = block_align;
u16 bits_per_sample; stream >> bits_per_sample;
CHECK_OK("Bits per sample"); // incomplete read check
ok = ok && (bits_per_sample == 8 || bits_per_sample == 16);
ASSERT(bits_per_sample == 8 || bits_per_sample == 16);
CHECK_OK("Bits per sample"); // value check
ret->m_bits_per_sample = bits_per_sample;
// Read chunks until we find DATA
bool found_data = false;
@ -118,10 +112,110 @@ RefPtr<AWavFile> AWavLoader::parse_wav(ByteBuffer& buffer)
ok = ok && int(data_sz) <= (buffer.size() - stream.offset());
CHECK_OK("Bad DATA (truncated)");
ret->m_sample_data = buffer.slice(stream.offset(), data_sz);
// At this point there should be no read failures!
// Just make sure we're good before we read the data...
ASSERT(!stream.handle_read_failure());
return ret;
auto sample_data = buffer.slice(stream.offset(), data_sz);
dbgprintf("Read WAV of format PCM with num_channels %d sample rate %d, bits per sample %d\n", num_channels, sample_rate, bits_per_sample);
return ABuffer::from_pcm_data(sample_data, num_channels, bits_per_sample, sample_rate);
}
// Small helper to resample from one playback rate to another
// This isn't really "smart", in that we just insert (or drop) samples.
// Should do better...
class AResampleHelper {
public:
AResampleHelper(float source, float target);
bool read_sample();
void prepare();
private:
const float m_ratio;
float m_current_ratio { 0 };
};
AResampleHelper::AResampleHelper(float source, float target)
: m_ratio(source / target)
{
}
void AResampleHelper::prepare()
{
m_current_ratio += m_ratio;
}
bool AResampleHelper::read_sample()
{
if (m_current_ratio > 1) {
m_current_ratio--;
return true;
}
return false;
}
template <typename T>
static void read_samples_from_stream(BufferStream& stream, Vector<ASample>& samples, int num_channels, int source_rate)
{
AResampleHelper resampler(source_rate, 44100);
T sample = 0;
float norm_l = 0;
float norm_r = 0;
switch (num_channels) {
case 1:
while (!stream.handle_read_failure()) {
resampler.prepare();
while (resampler.read_sample()) {
stream >> sample;
norm_l = float(sample) / std::numeric_limits<T>::max();
}
samples.append(ASample(norm_l));
}
break;
case 2:
while (!stream.handle_read_failure()) {
resampler.prepare();
while (resampler.read_sample()) {
stream >> sample;
norm_l = float(sample) / std::numeric_limits<T>::max();
stream >> sample;
norm_r = float(sample) / std::numeric_limits<T>::max();
}
samples.append(ASample(norm_l, norm_r));
}
break;
default:
ASSERT_NOT_REACHED();
}
}
// ### can't const this because BufferStream is non-const
// perhaps we need a reading class separate from the writing one, that can be
// entirely consted.
RefPtr<ABuffer> ABuffer::from_pcm_data(ByteBuffer& data, int num_channels, int bits_per_sample, int source_rate)
{
BufferStream stream(data);
Vector<ASample> fdata;
fdata.ensure_capacity(data.size() * 2);
dbg() << "Reading " << bits_per_sample << " bits and " << num_channels << " channels, total bytes: " << data.size();
switch (bits_per_sample) {
case 8:
read_samples_from_stream<u8>(stream, fdata, num_channels, source_rate);
break;
case 16:
read_samples_from_stream<i16>(stream, fdata, num_channels, source_rate);
break;
default:
ASSERT_NOT_REACHED();
}
// We should handle this in a better way above, but for now --
// just make sure we're good. Worst case we just write some 0s where they
// don't belong.
ASSERT(!stream.handle_read_failure());
return adopt(*new ABuffer(fdata));
}