OpenTTD Source 20251126-master-g67ded4f980
soundloader_wav.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10#include "stdafx.h"
11#include "core/math_func.hpp"
12#include "debug.h"
14#include "sound_type.h"
15#include "soundloader_type.h"
16
17#include "safeguards.h"
18
21public:
22 SoundLoader_Wav() : SoundLoader("wav", "Wav sound loader", 0) {}
23
24 static constexpr uint16_t DEFAULT_SAMPLE_RATE = 11025;
25
26 bool Load(SoundEntry &sound, bool new_format, std::vector<std::byte> &data) const override
27 {
28 RandomAccessFile &file = *sound.file;
29
30 /* Check RIFF/WAVE header. */
31 if (file.ReadDword() != std::byteswap<uint32_t>('RIFF')) return false;
32 file.ReadDword(); // Skip data size
33 if (file.ReadDword() != std::byteswap<uint32_t>('WAVE')) return false;
34
35 /* Read riff tags */
36 for (;;) {
37 uint32_t tag = file.ReadDword();
38 uint32_t size = file.ReadDword();
39
40 if (tag == std::byteswap<uint32_t>('fmt ')) {
41 uint16_t format = file.ReadWord();
42 if (format != 1) {
43 Debug(grf, 0, "SoundLoader_Wav: Unsupported format {}, expected 1 (uncompressed PCM).", format);
44 return false;
45 }
46
47 sound.channels = file.ReadWord();
48 if (sound.channels != 1) {
49 Debug(grf, 0, "SoundLoader_Wav: Unsupported channels {}, expected 1.", sound.channels);
50 return false;
51 }
52
53 sound.rate = file.ReadDword();
54 if (!new_format) sound.rate = DEFAULT_SAMPLE_RATE; // All old samples should be played at 11025 Hz.
55
56 file.ReadDword(); // avg bytes per second
57 file.ReadWord(); // alignment
58
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);
62 return false;
63 }
64
65 /* We've read 16 bytes of this chunk, we can skip anything extra. */
66 size -= 16;
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) {
70 /* Ensure length is aligned correctly for channels and BPS. */
71 Debug(grf, 0, "SoundLoader_Wav: Unexpected end of stream.");
72 return false;
73 }
74
75 if (size == 0) return true; // No need to continue.
76
77 /* Allocate an extra sample to ensure the runtime resampler doesn't go out of bounds. */
78 data.reserve(size + align);
79 data.resize(size);
80
81 file.ReadBlock(std::data(data), size);
82
83 switch (sound.bits_per_sample) {
84 case 8:
85 /* Convert 8-bit samples from unsigned to signed. */
86 for (auto &sample : data) {
87 sample ^= std::byte{0x80};
88 }
89 break;
90
91 case 16:
92 /* 16-bit samples in wav files are little endian, and may need to be converted to native endian. */
93 if constexpr (std::endian::native != std::endian::little) {
94 for (auto it = std::begin(data); it != std::end(data); /* nothing */) {
95 std::swap(*it++, *it++);
96 }
97 }
98 break;
99
100 default: NOT_REACHED();
101 }
102
103 return true;
104 }
105
106 /* Skip rest of chunk. */
107 if (size > 0) file.SkipBytes(size);
108 }
109
110 return false;
111 }
112
113private:
114 static SoundLoader_Wav instance;
115};
116
117/* static */ SoundLoader_Wav SoundLoader_Wav::instance{};
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.
Definition debug.h:37
Integer math functions.
constexpr T Align(const T x, uint n)
Return the smallest multiple of n equal or greater than x.
Definition math_func.hpp:37
Class related to random access to files.
A number of safeguards to prevent using unsafe methods.
Types related to sounds.
Types related to sound loaders.
Definition of base types and functions in a cross-platform compatible way.