OpenTTD Source 20241224-master-gee860a5c8e
soundloader_raw.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"
12#include "sound_type.h"
13#include "soundloader_type.h"
14
15#include "safeguards.h"
16
19public:
20 SoundLoader_Raw() : SoundLoader("raw", "Raw PCM loader", INT_MAX) {}
21
22 static constexpr uint16_t RAW_SAMPLE_RATE = 11025;
23 static constexpr uint8_t RAW_SAMPLE_BITS = 8;
24
25 bool Load(SoundEntry &sound, bool new_format, std::vector<uint8_t> &data) override
26 {
27 /* Raw sounds are apecial case for the jackhammer sound (name in Windows sample.cat is "Corrupt sound")
28 * It's not a RIFF file, but raw PCM data.
29 * We no longer compare by name as the same file in the DOS sample.cat does not have a unique name. */
30
31 /* Raw sounds are not permitted in a new format file. */
32 if (new_format) return false;
33
34 sound.channels = 1;
35 sound.rate = RAW_SAMPLE_RATE;
36 sound.bits_per_sample = RAW_SAMPLE_BITS;
37
38 /* Allocate an extra sample to ensure the runtime resampler doesn't go out of bounds.*/
39 data.reserve(sound.file_size + 1);
40 data.resize(sound.file_size);
41 sound.file->ReadBlock(std::data(data), std::size(data));
42
43 /* Convert 8-bit samples from unsigned to signed. */
44 for (auto &sample : data) {
45 sample = sample - 128;
46 }
47
48 return true;
49 }
50};
51
52static SoundLoader_Raw s_sound_loader_raw;
void ReadBlock(void *ptr, size_t size)
Read a block.
Raw PCM sound loader, used as a fallback if other sound loaders fail.
static constexpr uint16_t RAW_SAMPLE_RATE
Sample rate of raw pcm samples.
static constexpr uint8_t RAW_SAMPLE_BITS
Bit depths of raw pcm samples.
Base interface for a SoundLoader implementation.
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.