1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:07:44 +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,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;