diff options
author | kleines Filmröllchen <malu.bertsch@gmail.com> | 2021-04-26 17:13:04 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-26 19:08:40 +0200 |
commit | 563cc17a50bda557ccd04d89cade5b4aa4b81e6d (patch) | |
tree | 940c64332911665d5e44f7daa540e53a947ce390 /Userland/Libraries/LibAudio | |
parent | 91c210c39a013f57a12202e6d5fc18fad95eac3b (diff) | |
download | serenity-563cc17a50bda557ccd04d89cade5b4aa4b81e6d.zip |
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)
Diffstat (limited to 'Userland/Libraries/LibAudio')
-rw-r--r-- | Userland/Libraries/LibAudio/Buffer.cpp | 61 | ||||
-rw-r--r-- | Userland/Libraries/LibAudio/Buffer.h | 19 | ||||
-rw-r--r-- | Userland/Libraries/LibAudio/Loader.h | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibAudio/WavLoader.cpp | 64 | ||||
-rw-r--r-- | Userland/Libraries/LibAudio/WavLoader.h | 12 |
5 files changed, 127 insertions, 35 deletions
diff --git a/Userland/Libraries/LibAudio/Buffer.cpp b/Userland/Libraries/LibAudio/Buffer.cpp index 2e6ae9d24c..ce3da09cd8 100644 --- a/Userland/Libraries/LibAudio/Buffer.cpp +++ b/Userland/Libraries/LibAudio/Buffer.cpp @@ -1,14 +1,41 @@ /* * 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 */ #include <AK/Atomic.h> +#include <AK/Debug.h> +#include <AK/String.h> #include <LibAudio/Buffer.h> 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() { 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) { u8 byte = 0; @@ -85,27 +126,33 @@ static double read_norm_sample_8(InputMemoryStream& stream) 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 }; - 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; fdata.ensure_capacity(num_samples); - switch (bits_per_sample) { - case 8: + switch (sample_format) { + case PcmSampleFormat::Uint8: read_samples_from_stream(stream, read_norm_sample_8, fdata, resampler, num_channels); break; - case 16: + case PcmSampleFormat::Int16: read_samples_from_stream(stream, read_norm_sample_16, fdata, resampler, num_channels); break; - case 24: + case PcmSampleFormat::Int24: read_samples_from_stream(stream, read_norm_sample_24, fdata, resampler, num_channels); 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: VERIFY_NOT_REACHED(); } diff --git a/Userland/Libraries/LibAudio/Buffer.h b/Userland/Libraries/LibAudio/Buffer.h index fda5e0ef9c..bcdb9a8279 100644 --- a/Userland/Libraries/LibAudio/Buffer.h +++ b/Userland/Libraries/LibAudio/Buffer.h @@ -1,5 +1,6 @@ /* * 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 */ @@ -8,6 +9,7 @@ #include <AK/ByteBuffer.h> #include <AK/MemoryStream.h> +#include <AK/String.h> #include <AK/Types.h> #include <AK/Vector.h> #include <LibCore/AnonymousBuffer.h> @@ -69,6 +71,19 @@ struct Frame { 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 // This isn't really "smart", in that we just insert (or drop) samples. // Should do better... @@ -89,8 +104,8 @@ private: // A buffer of audio samples, normalized to 44100hz. class Buffer : public RefCounted<Buffer> { public: - static RefPtr<Buffer> from_pcm_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, int bits_per_sample); - 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_data(ReadonlyBytes data, ResampleHelper& resampler, int num_channels, PcmSampleFormat sample_format); + 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) { return adopt_ref(*new Buffer(move(samples))); diff --git a/Userland/Libraries/LibAudio/Loader.h b/Userland/Libraries/LibAudio/Loader.h index 5cd605934d..0050d6acd3 100644 --- a/Userland/Libraries/LibAudio/Loader.h +++ b/Userland/Libraries/LibAudio/Loader.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, the SerenityOS developers. + * Copyright (c) 2018-2021, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ @@ -33,7 +33,7 @@ public: virtual int total_samples() = 0; virtual u32 sample_rate() = 0; virtual u16 num_channels() = 0; - virtual u16 bits_per_sample() = 0; + virtual PcmSampleFormat pcm_format() = 0; virtual RefPtr<Core::File> file() = 0; }; @@ -62,7 +62,7 @@ public: int total_samples() const { return m_plugin ? m_plugin->total_samples() : 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 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; } private: diff --git a/Userland/Libraries/LibAudio/WavLoader.cpp b/Userland/Libraries/LibAudio/WavLoader.cpp index 508a33e12d..b300256e42 100644 --- a/Userland/Libraries/LibAudio/WavLoader.cpp +++ b/Userland/Libraries/LibAudio/WavLoader.cpp @@ -1,5 +1,6 @@ /* * 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 */ @@ -54,17 +55,18 @@ bool WavLoaderPlugin::sniff() RefPtr<Buffer> WavLoaderPlugin::get_more_samples(size_t max_bytes_to_read_from_input) { #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 - 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; if (m_file) { auto raw_samples = m_file->read(max_bytes_to_read_from_input); - if (raw_samples.is_empty()) + if (raw_samples.is_empty()) { 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 { - 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 m_loaded_samples += samples_to_read; @@ -78,7 +80,7 @@ void WavLoaderPlugin::seek(const int position) return; 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) m_file->seek(byte_position); @@ -147,7 +149,7 @@ bool WavLoaderPlugin::parse_header() m_error_string = String::formatted("Parsing failed: {}", msg); \ return {}; \ } \ - } while (0); + } while (0) u32 riff = read_u32(); ok = ok && riff == 0x46464952; // "RIFF" @@ -156,7 +158,6 @@ bool WavLoaderPlugin::parse_header() u32 sz = read_u32(); ok = ok && sz < 1024 * 1024 * 1024; // arbitrary CHECK_OK("File size"); - VERIFY(sz < 1024 * 1024 * 1024); u32 wave = read_u32(); ok = ok && wave == 0x45564157; // "WAVE" @@ -169,13 +170,11 @@ bool WavLoaderPlugin::parse_header() u32 fmt_size = read_u32(); ok = ok && fmt_size == 16; CHECK_OK("FMT size"); - VERIFY(fmt_size == 16); u16 audio_format = read_u16(); - CHECK_OK("Audio format"); // incomplete read check - ok = ok && audio_format == 1; // WAVE_FORMAT_PCM - CHECK_OK("Audio format"); // value check - VERIFY(audio_format == 1); + CHECK_OK("Audio format"); // incomplete read check + ok = ok && (audio_format == WAVE_FORMAT_PCM || audio_format == WAVE_FORMAT_IEEE_FLOAT); + CHECK_OK("Audio format PCM/Float"); // value check m_num_channels = read_u16(); ok = ok && (m_num_channels == 1 || m_num_channels == 2); @@ -185,16 +184,40 @@ bool WavLoaderPlugin::parse_header() CHECK_OK("Sample rate"); read_u32(); - CHECK_OK("Byte rate"); + CHECK_OK("Data rate"); 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 - ok = ok && (m_bits_per_sample == 8 || m_bits_per_sample == 16 || m_bits_per_sample == 24); - CHECK_OK("Bits per sample"); // value check - VERIFY(m_bits_per_sample == 8 || m_bits_per_sample == 16 || m_bits_per_sample == 24); + if (audio_format == WAVE_FORMAT_PCM) { + ok = ok && (bits_per_sample == 8 || bits_per_sample == 16 || 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 bool found_data = false; @@ -223,12 +246,11 @@ bool WavLoaderPlugin::parse_header() ok = ok && found_data; CHECK_OK("Found no data chunk"); - VERIFY(found_data); ok = ok && data_sz < maximum_wav_size; 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; return true; diff --git a/Userland/Libraries/LibAudio/WavLoader.h b/Userland/Libraries/LibAudio/WavLoader.h index c8077dd15f..bab6ac00a4 100644 --- a/Userland/Libraries/LibAudio/WavLoader.h +++ b/Userland/Libraries/LibAudio/WavLoader.h @@ -1,5 +1,6 @@ /* * 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 */ @@ -19,6 +20,13 @@ namespace Audio { 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. class WavLoaderPlugin : public LoaderPlugin { public: @@ -39,7 +47,7 @@ public: virtual int total_samples() override { return m_total_samples; } virtual u32 sample_rate() override { return m_sample_rate; } 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; } private: @@ -53,7 +61,7 @@ private: u32 m_sample_rate { 0 }; u16 m_num_channels { 0 }; - u16 m_bits_per_sample { 0 }; + PcmSampleFormat m_sample_format; int m_loaded_samples { 0 }; int m_total_samples { 0 }; |