OpenTTD Source 20241224-master-gf74b0cf984
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/bitmath_func.hpp"
12#include "core/math_func.hpp"
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<uint8_t> &data) override
27 {
28 RandomAccessFile &file = *sound.file;
29
30 /* Check RIFF/WAVE header. */
31 if (file.ReadDword() != BSWAP32('RIFF')) return false;
32 file.ReadDword(); // Skip data size
33 if (file.ReadDword() != BSWAP32('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 == BSWAP32('fmt ')) {
41 uint16_t format = file.ReadWord();
42 if (format != 1) return false; // File must be uncompressed PCM
43
44 sound.channels = file.ReadWord();
45 if (sound.channels != 1) return false; // File must be mono.
46
47 sound.rate = file.ReadDword();
48 if (!new_format) sound.rate = DEFAULT_SAMPLE_RATE; // All old samples should be played at 11025 Hz.
49
50 file.ReadDword(); // avg bytes per second
51 file.ReadWord(); // alignment
52
53 sound.bits_per_sample = file.ReadWord();
54 if (sound.bits_per_sample != 8 && sound.bits_per_sample != 16) return false; // File must be 8 or 16 BPS.
55
56 /* We've read 16 bytes of this chunk, we can skip anything extra. */
57 size -= 16;
58 } else if (tag == BSWAP32('data')) {
59 uint align = sound.channels * sound.bits_per_sample / 8;
60 if (Align(size, align) != size) return false; // Ensure length is aligned correctly for channels and BPS.
61
62 if (size == 0) return true; // No need to continue.
63
64 /* Allocate an extra sample to ensure the runtime resampler doesn't go out of bounds. */
65 data.reserve(size + align);
66 data.resize(size);
67
68 file.ReadBlock(std::data(data), size);
69
70 switch (sound.bits_per_sample) {
71 case 8:
72 /* Convert 8-bit samples from unsigned to signed. */
73 for (auto &sample : data) {
74 sample = sample - 128;
75 }
76 break;
77
78 case 16:
79 /* 16-bit samples in wav files are little endian, and may need to be converted to native endian. */
80 if constexpr (std::endian::native != std::endian::little) {
81 for (auto it = std::begin(data); it != std::end(data); /* nothing */) {
82 std::swap(*it++, *it++);
83 }
84 }
85 break;
86
87 default: NOT_REACHED();
88 }
89
90 return true;
91 }
92
93 /* Skip rest of chunk. */
94 if (size > 0) file.SkipBytes(size);
95 }
96
97 return false;
98 }
99};
100
101static SoundLoader_Wav s_sound_loader_wav;
Functions related to bit mathematics.
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
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.
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.