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

LibAudio: Support 32 and 64-bit float WAV files

LibAudio's WavLoader plugin for loading WAV files now supports loading
audio files with 32-bit float or 64-bit float samples.

By supporting these new non-int sample formats, Audio::Buffer now stores
the sample format (out of a list of supported formats) instead of the
raw bit depth. (The bit depth is easily calculated with
pcm_bits_per_sample)
This commit is contained in:
kleines Filmröllchen 2021-04-26 17:13:04 +02:00 committed by Andreas Kling
parent 91c210c39a
commit 563cc17a50
5 changed files with 127 additions and 35 deletions

View file

@ -1,14 +1,41 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Atomic.h> #include <AK/Atomic.h>
#include <AK/Debug.h>
#include <AK/String.h>
#include <LibAudio/Buffer.h> #include <LibAudio/Buffer.h>
namespace Audio { namespace Audio {
u16 pcm_bits_per_sample(PcmSampleFormat format)
{
switch (format) {
case Uint8:
return 8;
case Int16:
return 16;
case Int24:
return 24;
case Float32:
return 32;
case Float64:
return 64;
default:
VERIFY_NOT_REACHED();
}
}
String sample_format_name(PcmSampleFormat format)
{
bool is_float = format == Float32 || format == Float64;
return String::formatted("PCM {}bit {}", pcm_bits_per_sample(format), is_float ? "Float" : "LE");
}
i32 Buffer::allocate_id() i32 Buffer::allocate_id()
{ {
static Atomic<i32> next_id; static Atomic<i32> next_id;
@ -54,6 +81,20 @@ static void read_samples_from_stream(InputMemoryStream& stream, SampleReader rea
} }
} }
static double read_float_sample_64(InputMemoryStream& stream)
{
LittleEndian<double> sample;
stream >> sample;
return double(sample);
}
static double read_float_sample_32(InputMemoryStream& stream)
{
LittleEndian<float> sample;
stream >> sample;
return double(sample);
}
static double read_norm_sample_24(InputMemoryStream& stream) static double read_norm_sample_24(InputMemoryStream& stream)
{ {
u8 byte = 0; u8 byte = 0;
@ -85,27 +126,33 @@ static double read_norm_sample_8(InputMemoryStream& stream)
return double(sample) / NumericLimits<u8>::max(); return double(sample) / NumericLimits<u8>::max();
} }
RefPtr<Buffer> Buffer::from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, int bits_per_sample) RefPtr<Buffer> Buffer::from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format)
{ {
InputMemoryStream stream { data }; InputMemoryStream stream { data };
return from_pcm_stream(stream, resampler, num_channels, bits_per_sample, data.size() / (bits_per_sample / 8)); return from_pcm_stream(stream, resampler, num_channels, sample_format, data.size() / (pcm_bits_per_sample(sample_format) / 8));
} }
RefPtr<Buffer> Buffer::from_pcm_stream(InputMemoryStream& stream, ResampleHelper& resampler, int num_channels, int bits_per_sample, int num_samples) RefPtr<Buffer> Buffer::from_pcm_stream(InputMemoryStream& stream, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format, int num_samples)
{ {
Vector<Frame> fdata; Vector<Frame> fdata;
fdata.ensure_capacity(num_samples); fdata.ensure_capacity(num_samples);
switch (bits_per_sample) { switch (sample_format) {
case 8: case PcmSampleFormat::Uint8:
read_samples_from_stream(stream, read_norm_sample_8, fdata, resampler, num_channels); read_samples_from_stream(stream, read_norm_sample_8, fdata, resampler, num_channels);
break; break;
case 16: case PcmSampleFormat::Int16:
read_samples_from_stream(stream, read_norm_sample_16, fdata, resampler, num_channels); read_samples_from_stream(stream, read_norm_sample_16, fdata, resampler, num_channels);
break; break;
case 24: case PcmSampleFormat::Int24:
read_samples_from_stream(stream, read_norm_sample_24, fdata, resampler, num_channels); read_samples_from_stream(stream, read_norm_sample_24, fdata, resampler, num_channels);
break; break;
case PcmSampleFormat::Float32:
read_samples_from_stream(stream, read_float_sample_32, fdata, resampler, num_channels);
break;
case PcmSampleFormat::Float64:
read_samples_from_stream(stream, read_float_sample_64, fdata, resampler, num_channels);
break;
default: default:
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -8,6 +9,7 @@
#include <AK/ByteBuffer.h> #include <AK/ByteBuffer.h>
#include <AK/MemoryStream.h> #include <AK/MemoryStream.h>
#include <AK/String.h>
#include <AK/Types.h> #include <AK/Types.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/AnonymousBuffer.h> #include <LibCore/AnonymousBuffer.h>
@ -69,6 +71,19 @@ struct Frame {
double right; double right;
}; };
// Supported PCM sample formats.
enum PcmSampleFormat : u8 {
Uint8,
Int16,
Int24,
Float32,
Float64,
};
// Most of the read code only cares about how many bits to read or write
u16 pcm_bits_per_sample(PcmSampleFormat format);
String sample_format_name(PcmSampleFormat format);
// Small helper to resample from one playback rate to another // Small helper to resample from one playback rate to another
// This isn't really "smart", in that we just insert (or drop) samples. // This isn't really "smart", in that we just insert (or drop) samples.
// Should do better... // Should do better...
@ -89,8 +104,8 @@ private:
// A buffer of audio samples, normalized to 44100hz. // A buffer of audio samples, normalized to 44100hz.
class Buffer : public RefCounted<Buffer> { class Buffer : public RefCounted<Buffer> {
public: public:
static RefPtr<Buffer> from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, int bits_per_sample); static RefPtr<Buffer> from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format);
static RefPtr<Buffer> from_pcm_stream(InputMemoryStream& stream, ResampleHelper& resampler, int num_channels, int bits_per_sample, int num_samples); static RefPtr<Buffer> from_pcm_stream(InputMemoryStream& stream, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format, int num_samples);
static NonnullRefPtr<Buffer> create_with_samples(Vector<Frame>&& samples) static NonnullRefPtr<Buffer> create_with_samples(Vector<Frame>&& samples)
{ {
return adopt_ref(*new Buffer(move(samples))); return adopt_ref(*new Buffer(move(samples)));

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2018-2020, the SerenityOS developers. * Copyright (c) 2018-2021, the SerenityOS developers.
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -33,7 +33,7 @@ public:
virtual int total_samples() = 0; virtual int total_samples() = 0;
virtual u32 sample_rate() = 0; virtual u32 sample_rate() = 0;
virtual u16 num_channels() = 0; virtual u16 num_channels() = 0;
virtual u16 bits_per_sample() = 0; virtual PcmSampleFormat pcm_format() = 0;
virtual RefPtr<Core::File> file() = 0; virtual RefPtr<Core::File> file() = 0;
}; };
@ -62,7 +62,7 @@ public:
int total_samples() const { return m_plugin ? m_plugin->total_samples() : 0; } int total_samples() const { return m_plugin ? m_plugin->total_samples() : 0; }
u32 sample_rate() const { return m_plugin ? m_plugin->sample_rate() : 0; } u32 sample_rate() const { return m_plugin ? m_plugin->sample_rate() : 0; }
u16 num_channels() const { return m_plugin ? m_plugin->num_channels() : 0; } u16 num_channels() const { return m_plugin ? m_plugin->num_channels() : 0; }
u16 bits_per_sample() const { return m_plugin ? m_plugin->bits_per_sample() : 0; } u16 bits_per_sample() const { return m_plugin ? pcm_bits_per_sample(m_plugin->pcm_format()) : 0; }
RefPtr<Core::File> file() const { return m_plugin ? m_plugin->file() : nullptr; } RefPtr<Core::File> file() const { return m_plugin ? m_plugin->file() : nullptr; }
private: private:

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -54,17 +55,18 @@ bool WavLoaderPlugin::sniff()
RefPtr<Buffer> WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_input) RefPtr<Buffer> WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_input)
{ {
#if AWAVLOADER_DEBUG #if AWAVLOADER_DEBUG
dbgln("Read WAV of format PCM with num_channels {} sample rate {}, bits per sample {}", m_num_channels, m_sample_rate, m_bits_per_sample); dbgln("Read {} bytes WAV with num_channels {} sample rate {}, bits per sample {}, sample format {}", max_bytes_to_read_from_input, m_num_channels, m_sample_rate, pcm_bits_per_sample(m_sample_format), sample_format_name(m_sample_format));
#endif #endif
size_t samples_to_read = static_cast<int>(max_bytes_to_read_from_input) / (m_num_channels * (m_bits_per_sample / 8)); size_t samples_to_read = static_cast<int>(max_bytes_to_read_from_input) / (m_num_channels * (pcm_bits_per_sample(m_sample_format) / 8));
RefPtr<Buffer> buffer; RefPtr<Buffer> buffer;
if (m_file) { if (m_file) {
auto raw_samples = m_file->read(max_bytes_to_read_from_input); auto raw_samples = m_file->read(max_bytes_to_read_from_input);
if (raw_samples.is_empty()) if (raw_samples.is_empty()) {
return nullptr; return nullptr;
buffer = Buffer::from_pcm_data(raw_samples, *m_resampler, m_num_channels, m_bits_per_sample); }
buffer = Buffer::from_pcm_data(raw_samples, *m_resampler, m_num_channels, m_sample_format);
} else { } else {
buffer = Buffer::from_pcm_stream(*m_stream, *m_resampler, m_num_channels, m_bits_per_sample, samples_to_read); buffer = Buffer::from_pcm_stream(*m_stream, *m_resampler, m_num_channels, m_sample_format, samples_to_read);
} }
//Buffer contains normalized samples, but m_loaded_samples should contain the amount of actually loaded samples //Buffer contains normalized samples, but m_loaded_samples should contain the amount of actually loaded samples
m_loaded_samples += samples_to_read; m_loaded_samples += samples_to_read;
@ -78,7 +80,7 @@ void WavLoaderPlugin::seek(const int position)
return; return;
m_loaded_samples = position; m_loaded_samples = position;
size_t byte_position = position * m_num_channels * (m_bits_per_sample / 8); size_t byte_position = position * m_num_channels * (pcm_bits_per_sample(m_sample_format) / 8);
if (m_file) if (m_file)
m_file->seek(byte_position); m_file->seek(byte_position);
@ -147,7 +149,7 @@ bool WavLoaderPlugin::parse_header()
m_error_string = String::formatted("Parsing failed: {}", msg); \ m_error_string = String::formatted("Parsing failed: {}", msg); \
return {}; \ return {}; \
} \ } \
} while (0); } while (0)
u32 riff = read_u32(); u32 riff = read_u32();
ok = ok && riff == 0x46464952; // "RIFF" ok = ok && riff == 0x46464952; // "RIFF"
@ -156,7 +158,6 @@ bool WavLoaderPlugin::parse_header()
u32 sz = read_u32(); u32 sz = read_u32();
ok = ok && sz < 1024 * 1024 * 1024; // arbitrary ok = ok && sz < 1024 * 1024 * 1024; // arbitrary
CHECK_OK("File size"); CHECK_OK("File size");
VERIFY(sz < 1024 * 1024 * 1024);
u32 wave = read_u32(); u32 wave = read_u32();
ok = ok && wave == 0x45564157; // "WAVE" ok = ok && wave == 0x45564157; // "WAVE"
@ -169,13 +170,11 @@ bool WavLoaderPlugin::parse_header()
u32 fmt_size = read_u32(); u32 fmt_size = read_u32();
ok = ok && fmt_size == 16; ok = ok && fmt_size == 16;
CHECK_OK("FMT size"); CHECK_OK("FMT size");
VERIFY(fmt_size == 16);
u16 audio_format = read_u16(); u16 audio_format = read_u16();
CHECK_OK("Audio format"); // incomplete read check CHECK_OK("Audio format"); // incomplete read check
ok = ok && audio_format == 1; // WAVE_FORMAT_PCM ok = ok && (audio_format == WAVE_FORMAT_PCM || audio_format == WAVE_FORMAT_IEEE_FLOAT);
CHECK_OK("Audio format"); // value check CHECK_OK("Audio format PCM/Float"); // value check
VERIFY(audio_format == 1);
m_num_channels = read_u16(); m_num_channels = read_u16();
ok = ok && (m_num_channels == 1 || m_num_channels == 2); ok = ok && (m_num_channels == 1 || m_num_channels == 2);
@ -185,16 +184,40 @@ bool WavLoaderPlugin::parse_header()
CHECK_OK("Sample rate"); CHECK_OK("Sample rate");
read_u32(); read_u32();
CHECK_OK("Byte rate"); CHECK_OK("Data rate");
read_u16(); read_u16();
CHECK_OK("Block align"); CHECK_OK("Block size");
m_bits_per_sample = read_u16(); u16 bits_per_sample = read_u16();
CHECK_OK("Bits per sample"); // incomplete read check CHECK_OK("Bits per sample"); // incomplete read check
ok = ok && (m_bits_per_sample == 8 || m_bits_per_sample == 16 || m_bits_per_sample == 24); if (audio_format == WAVE_FORMAT_PCM) {
CHECK_OK("Bits per sample"); // value check ok = ok && (bits_per_sample == 8 || bits_per_sample == 16 || bits_per_sample == 24);
VERIFY(m_bits_per_sample == 8 || m_bits_per_sample == 16 || m_bits_per_sample == 24); CHECK_OK("Bits per sample (PCM)"); // value check
// We only support 8-24 bit audio right now because other formats are uncommon
if (bits_per_sample == 8) {
m_sample_format = PcmSampleFormat::Uint8;
} else if (bits_per_sample == 16) {
m_sample_format = PcmSampleFormat::Int16;
} else if (bits_per_sample == 24) {
m_sample_format = PcmSampleFormat::Int24;
}
} else if (audio_format == WAVE_FORMAT_IEEE_FLOAT) {
ok = ok && (bits_per_sample == 32 || bits_per_sample == 64);
CHECK_OK("Bits per sample (Float)"); // value check
// Again, only the common 32 and 64 bit
if (bits_per_sample == 32) {
m_sample_format = PcmSampleFormat::Float32;
} else if (bits_per_sample == 64) {
m_sample_format = PcmSampleFormat::Float64;
}
}
#if AWAVLOADER_DEBUG
dbgln("WAV format {} at {}bit, {} channels, rate {}Hz ", sample_format_name(m_sample_format), pcm_bits_per_sample(m_sample_format), m_num_channels, m_sample_rate);
#endif
// Read chunks until we find DATA // Read chunks until we find DATA
bool found_data = false; bool found_data = false;
@ -223,12 +246,11 @@ bool WavLoaderPlugin::parse_header()
ok = ok && found_data; ok = ok && found_data;
CHECK_OK("Found no data chunk"); CHECK_OK("Found no data chunk");
VERIFY(found_data);
ok = ok && data_sz < maximum_wav_size; ok = ok && data_sz < maximum_wav_size;
CHECK_OK("Data was too large"); CHECK_OK("Data was too large");
int bytes_per_sample = (m_bits_per_sample / 8) * m_num_channels; int bytes_per_sample = (bits_per_sample / 8) * m_num_channels;
m_total_samples = data_sz / bytes_per_sample; m_total_samples = data_sz / bytes_per_sample;
return true; return true;

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, kleines Filmröllchen <malu.bertsch@gmail.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -19,6 +20,13 @@
namespace Audio { namespace Audio {
class Buffer; class Buffer;
// defines for handling the WAV header data
#define WAVE_FORMAT_PCM 0x0001 // PCM
#define WAVE_FORMAT_IEEE_FLOAT 0x0003 // IEEE float
#define WAVE_FORMAT_ALAW 0x0006 // 8-bit ITU-T G.711 A-law
#define WAVE_FORMAT_MULAW 0x0007 // 8-bit ITU-T G.711 µ-law
#define WAVE_FORMAT_EXTENSIBLE 0xFFFE // Determined by SubFormat
// Parses a WAV file and produces an Audio::Buffer. // Parses a WAV file and produces an Audio::Buffer.
class WavLoaderPlugin : public LoaderPlugin { class WavLoaderPlugin : public LoaderPlugin {
public: public:
@ -39,7 +47,7 @@ public:
virtual int total_samples() override { return m_total_samples; } virtual int total_samples() override { return m_total_samples; }
virtual u32 sample_rate() override { return m_sample_rate; } virtual u32 sample_rate() override { return m_sample_rate; }
virtual u16 num_channels() override { return m_num_channels; } virtual u16 num_channels() override { return m_num_channels; }
virtual u16 bits_per_sample() override { return m_bits_per_sample; } virtual PcmSampleFormat pcm_format() override { return m_sample_format; }
virtual RefPtr<Core::File> file() override { return m_file; } virtual RefPtr<Core::File> file() override { return m_file; }
private: private:
@ -53,7 +61,7 @@ private:
u32 m_sample_rate { 0 }; u32 m_sample_rate { 0 };
u16 m_num_channels { 0 }; u16 m_num_channels { 0 };
u16 m_bits_per_sample { 0 }; PcmSampleFormat m_sample_format;
int m_loaded_samples { 0 }; int m_loaded_samples { 0 };
int m_total_samples { 0 }; int m_total_samples { 0 };