OpenTTD Source 20260721-master-g25ec12c62d
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "stdafx.h"
11#include "core/label_type.hpp"
12#include "core/math_func.hpp"
13#include "debug.h"
15#include "sound_type.h"
16#include "soundloader_type.h"
17
18#include "safeguards.h"
19
21
28{
29 WavTag tag;
30 file.ReadBlock(tag.data(), tag.size());
31 return tag;
32}
33
35class SoundLoader_Wav : public SoundLoader {
36public:
37 SoundLoader_Wav() : SoundLoader("wav", "Wav sound loader", 0) {}
38
39 static constexpr uint16_t DEFAULT_SAMPLE_RATE = 11025;
40
41 bool Load(SoundEntry &sound, bool new_format, std::vector<std::byte> &data) const override
42 {
43 RandomAccessFile &file = *sound.file;
44
45 /* Check RIFF/WAVE header. */
46 if (ReadWavTag(file) != WavTag{"RIFF"}) return false;
47 file.ReadDword(); // Skip data size
48 if (ReadWavTag(file) != WavTag{"WAVE"}) return false;
49
50 /* Read riff tags */
51 for (;;) {
52 WavTag tag = ReadWavTag(file);
53 uint32_t size = file.ReadDword();
54
55 if (tag == WavTag{"fmt "}) {
56 uint16_t format = file.ReadWord();
57 if (format != 1) {
58 Debug(grf, 0, "SoundLoader_Wav: Unsupported format {}, expected 1 (uncompressed PCM).", format);
59 return false;
60 }
61
62 sound.channels = file.ReadWord();
63 if (sound.channels != 1) {
64 Debug(grf, 0, "SoundLoader_Wav: Unsupported channels {}, expected 1.", sound.channels);
65 return false;
66 }
67
68 sound.rate = file.ReadDword();
69 if (!new_format) sound.rate = DEFAULT_SAMPLE_RATE; // All old samples should be played at 11025 Hz.
70
71 file.ReadDword(); // avg bytes per second
72 file.ReadWord(); // alignment
73
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);
77 return false;
78 }
79
80 /* We've read 16 bytes of this chunk, we can skip anything extra. */
81 size -= 16;
82 } else if (tag == WavTag{"data"}) {
83 uint align = sound.channels * sound.bits_per_sample / 8;
84 if (Align(size, align) != size) {
85 /* Ensure length is aligned correctly for channels and BPS. */
86 Debug(grf, 0, "SoundLoader_Wav: Unexpected end of stream.");
87 return false;
88 }
89
90 if (size == 0) return true; // No need to continue.
91
92 /* Allocate an extra sample to ensure the runtime resampler doesn't go out of bounds. */
93 data.reserve(size + align);
94 data.resize(size);
95
96 file.ReadBlock(std::data(data), size);
97
98 switch (sound.bits_per_sample) {
99 case 8:
100 /* Convert 8-bit samples from unsigned to signed. */
101 for (auto &sample : data) {
102 sample ^= std::byte{0x80};
103 }
104 break;
105
106 case 16:
107 /* 16-bit samples in wav files are little endian, and may need to be converted to native endian. */
108 if constexpr (std::endian::native != std::endian::little) {
109 for (auto it = std::begin(data); it != std::end(data); /* nothing */) {
110 std::swap(*it++, *it++);
111 }
112 }
113 break;
114
115 default: NOT_REACHED();
116 }
117
118 return true;
119 }
120
121 /* Skip rest of chunk. */
122 if (size > 0) file.SkipBytes(size);
123 }
124
125 return false;
126 }
127
128private:
129 static SoundLoader_Wav instance;
130};
131
132/* 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.
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.
Definition debug.h:37
A type for 4 character labels/tags/ids in files that should be read/shown as is.
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.
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.