2019-07-13 19:42:03 +02:00
|
|
|
#include <LibCore/CFile.h>
|
2019-07-14 00:28:30 +02:00
|
|
|
#include <AK/BufferStream.h>
|
|
|
|
#include <limits>
|
2019-07-13 19:42:03 +02:00
|
|
|
|
|
|
|
#include "AWavLoader.h"
|
|
|
|
#include "AWavFile.h"
|
|
|
|
|
|
|
|
RefPtr<AWavFile> AWavLoader::load_wav(const StringView& path)
|
|
|
|
{
|
|
|
|
m_error_string = {};
|
|
|
|
|
|
|
|
CFile wav(path);
|
|
|
|
if (!wav.open(CIODevice::ReadOnly)) {
|
|
|
|
m_error_string = String::format("Can't open file: %s", wav.error_string());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
auto contents = wav.read_all();
|
2019-07-13 19:42:03 +02:00
|
|
|
return parse_wav(contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: A streaming parser might be better than forcing a ByteBuffer
|
2019-07-14 00:28:30 +02:00
|
|
|
RefPtr<AWavFile> AWavLoader::parse_wav(ByteBuffer& buffer)
|
2019-07-13 19:42:03 +02:00
|
|
|
{
|
2019-07-14 00:28:30 +02:00
|
|
|
BufferStream stream(buffer);
|
2019-07-13 19:42:03 +02:00
|
|
|
|
|
|
|
#define CHECK_OK(msg) \
|
|
|
|
do { \
|
|
|
|
ASSERT(ok); \
|
2019-07-14 00:28:30 +02:00
|
|
|
if (stream.handle_read_failure()) { \
|
|
|
|
m_error_string = String::format("Premature stream EOF at %s", msg); \
|
|
|
|
return {}; \
|
|
|
|
} \
|
2019-07-13 19:42:03 +02:00
|
|
|
if (!ok) { \
|
|
|
|
m_error_string = String::format("Parsing failed: %s", msg); \
|
|
|
|
return {}; \
|
|
|
|
} else { \
|
|
|
|
dbgprintf("%s is OK!\n", msg); \
|
|
|
|
} \
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
bool ok = true;
|
2019-07-14 00:28:30 +02:00
|
|
|
u32 riff; stream >> riff;
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && riff == 0x46464952; // "RIFF"
|
|
|
|
CHECK_OK("RIFF header");
|
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
u32 sz; stream >> sz;
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && sz < 1024 * 1024 * 42; // arbitrary
|
|
|
|
CHECK_OK("File size");
|
2019-07-14 00:28:30 +02:00
|
|
|
ASSERT(sz < 1024 * 1024 * 42);
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
u32 wave; stream >> wave;
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && wave == 0x45564157; // "WAVE"
|
|
|
|
CHECK_OK("WAVE header");
|
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
u32 fmt_id; stream >> fmt_id;
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && fmt_id == 0x20746D66; // "FMT"
|
|
|
|
CHECK_OK("FMT header");
|
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
u32 fmt_size; stream >> fmt_size;
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && fmt_size == 16;
|
|
|
|
CHECK_OK("FMT size");
|
2019-07-14 00:28:30 +02:00
|
|
|
ASSERT(fmt_size == 16);
|
2019-07-13 19:42:03 +02:00
|
|
|
|
|
|
|
auto ret = adopt(*new AWavFile);
|
2019-07-14 00:28:30 +02:00
|
|
|
u16 audio_format; stream >> audio_format;
|
|
|
|
CHECK_OK("Audio format"); // incomplete read check
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && audio_format == 1; // WAVE_FORMAT_PCM
|
|
|
|
ASSERT(audio_format == 1);
|
2019-07-14 00:28:30 +02:00
|
|
|
CHECK_OK("Audio format"); // value check
|
2019-07-13 19:42:03 +02:00
|
|
|
ret->m_format = AWavFile::Format::PCM;
|
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
u16 num_channels; stream >> num_channels;
|
2019-07-13 19:42:03 +02:00
|
|
|
CHECK_OK("Channel count");
|
|
|
|
ret->m_channel_count = num_channels;
|
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
u32 sample_rate; stream >> sample_rate;
|
2019-07-13 19:42:03 +02:00
|
|
|
CHECK_OK("Sample rate");
|
|
|
|
ret->m_sample_rate = sample_rate;
|
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
u32 byte_rate; stream >> byte_rate;
|
2019-07-13 19:42:03 +02:00
|
|
|
CHECK_OK("Byte rate");
|
|
|
|
ret->m_byte_rate = byte_rate;
|
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
u16 block_align; stream >> block_align;
|
2019-07-13 19:42:03 +02:00
|
|
|
CHECK_OK("Block align");
|
|
|
|
ret->m_block_align = block_align;
|
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
u16 bits_per_sample; stream >> bits_per_sample;
|
|
|
|
CHECK_OK("Bits per sample"); // incomplete read check
|
2019-07-13 19:42:03 +02:00
|
|
|
ok = ok && (bits_per_sample == 8 || bits_per_sample == 16);
|
|
|
|
ASSERT(bits_per_sample == 8 || bits_per_sample == 16);
|
2019-07-14 00:28:30 +02:00
|
|
|
CHECK_OK("Bits per sample"); // value check
|
2019-07-13 19:42:03 +02:00
|
|
|
ret->m_bits_per_sample = bits_per_sample;
|
|
|
|
|
|
|
|
// Read chunks until we find DATA
|
|
|
|
bool found_data = false;
|
|
|
|
u32 data_sz = 0;
|
2019-07-14 00:28:30 +02:00
|
|
|
while (true) {
|
|
|
|
u32 chunk_id; stream >> chunk_id;
|
|
|
|
CHECK_OK("Reading chunk ID searching for data");
|
|
|
|
stream >> data_sz;
|
|
|
|
CHECK_OK("Reading chunk size searching for data");
|
2019-07-13 19:42:03 +02:00
|
|
|
if (chunk_id == 0x61746164) { // DATA
|
|
|
|
found_data = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = ok && found_data;
|
|
|
|
CHECK_OK("Found no data chunk");
|
2019-07-14 00:28:30 +02:00
|
|
|
ASSERT(found_data);
|
|
|
|
|
|
|
|
ok = ok && data_sz < std::numeric_limits<int>::max();
|
|
|
|
CHECK_OK("Data was too large");
|
|
|
|
|
|
|
|
// ### consider using BufferStream to do this for us
|
|
|
|
ok = ok && int(data_sz) <= (buffer.size() - stream.offset());
|
|
|
|
CHECK_OK("Bad DATA (truncated)");
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
ret->m_sample_data = buffer.slice(stream.offset(), data_sz);
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2019-07-14 00:28:30 +02:00
|
|
|
// At this point there should be no read failures!
|
|
|
|
ASSERT(!stream.handle_read_failure());
|
2019-07-13 19:42:03 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|