24 static constexpr uint16_t DEFAULT_SAMPLE_RATE = 11025;
26 bool Load(
SoundEntry &sound,
bool new_format, std::vector<std::byte> &data)
const override
31 if (file.
ReadDword() != std::byteswap<uint32_t>(
'RIFF'))
return false;
33 if (file.
ReadDword() != std::byteswap<uint32_t>(
'WAVE'))
return false;
40 if (tag == std::byteswap<uint32_t>(
'fmt ')) {
43 Debug(grf, 0,
"SoundLoader_Wav: Unsupported format {}, expected 1 (uncompressed PCM).", format);
48 if (sound.channels != 1) {
49 Debug(grf, 0,
"SoundLoader_Wav: Unsupported channels {}, expected 1.", sound.channels);
54 if (!new_format) sound.rate = DEFAULT_SAMPLE_RATE;
59 sound.bits_per_sample = file.
ReadWord();
60 if (sound.bits_per_sample != 8 && sound.bits_per_sample != 16) {
61 Debug(grf, 0,
"SoundLoader_Wav: Unsupported bits_per_sample {}, expected 8 or 16.", sound.bits_per_sample);
67 }
else if (tag == std::byteswap<uint32_t>(
'data')) {
68 uint align = sound.channels * sound.bits_per_sample / 8;
69 if (
Align(size, align) != size) {
71 Debug(grf, 0,
"SoundLoader_Wav: Unexpected end of stream.");
75 if (size == 0)
return true;
78 data.reserve(size + align);
83 switch (sound.bits_per_sample) {
86 for (
auto &sample : data) {
87 sample ^= std::byte{0x80};
93 if constexpr (std::endian::native != std::endian::little) {
94 for (
auto it = std::begin(data); it != std::end(data); ) {
95 std::swap(*it++, *it++);
100 default: NOT_REACHED();
A file from which bytes, words and double words are read in (potentially) a random order.
void ReadBlock(void *ptr, size_t size)
Read a block.
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
Wav file (RIFF/WAVE) sound loader.
Base interface for a SoundLoader implementation.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Class related to random access to files.
A number of safeguards to prevent using unsafe methods.
Types related to sound loaders.
Definition of base types and functions in a cross-platform compatible way.