35class SoundLoader_Wav :
public SoundLoader {
37 SoundLoader_Wav() : SoundLoader(
"wav",
"Wav sound loader", 0) {}
39 static constexpr uint16_t DEFAULT_SAMPLE_RATE = 11025;
41 bool Load(
SoundEntry &sound,
bool new_format, std::vector<std::byte> &data)
const override
55 if (tag ==
WavTag{
"fmt "}) {
58 Debug(grf, 0,
"SoundLoader_Wav: Unsupported format {}, expected 1 (uncompressed PCM).", format);
63 if (sound.channels != 1) {
64 Debug(grf, 0,
"SoundLoader_Wav: Unsupported channels {}, expected 1.", sound.channels);
69 if (!new_format) sound.rate = DEFAULT_SAMPLE_RATE;
74 sound.bits_per_sample = file.
ReadWord();
75 if (sound.bits_per_sample != 8 && sound.bits_per_sample != 16) {
76 Debug(grf, 0,
"SoundLoader_Wav: Unsupported bits_per_sample {}, expected 8 or 16.", sound.bits_per_sample);
82 }
else if (tag ==
WavTag{
"data"}) {
83 uint align = sound.channels * sound.bits_per_sample / 8;
84 if (
Align(size, align) != size) {
86 Debug(grf, 0,
"SoundLoader_Wav: Unexpected end of stream.");
90 if (size == 0)
return true;
93 data.reserve(size + align);
98 switch (sound.bits_per_sample) {
101 for (
auto &sample : data) {
102 sample ^= std::byte{0x80};
108 if constexpr (std::endian::native != std::endian::little) {
109 for (
auto it = std::begin(data); it != std::end(data); ) {
110 std::swap(*it++, *it++);
115 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.
bool Load(SoundEntry &sound, bool new_format, std::vector< std::byte > &data) const override
Load a sound from the file and offset in the given sound entry.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
A type for 4 character labels/tags/ids in files that should be read/shown as is.
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.
WavTag ReadWavTag(RandomAccessFile &file)
Read a WavTag from the file.
Label< struct WavTagTag > WavTag
Label for different tags in the file.
Definition of base types and functions in a cross-platform compatible way.
A four character label/tag/id.