|
@@ -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
|
|
|
|
- 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();
|
|
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);
|
|
|
|
- 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
|
|
// 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;
|