OpenTTD Source: src/soundloader_raw.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "random_access_file_type.h"
12#include "sound_type.h"
13#include "soundloader_type.h"
14
15#include "safeguards.h"
16
18 class SoundLoader_Raw : public SoundLoader {
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<std::byte> &data) const 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 ^= std::byte{0x80};
46 }
47
48 return true;
49 }
50
51private:
52 static SoundLoader_Raw instance;
53};
54
55/* static */ SoundLoader_Raw SoundLoader_Raw::instance{};
RandomAccessFile::ReadBlock
void ReadBlock(void *ptr, size_t size)
Read a block.
Definition random_access_file.cpp:145
SoundLoader_Raw
Raw PCM sound loader, used as a fallback if other sound loaders fail.
Definition soundloader_raw.cpp:18
SoundLoader_Raw::RAW_SAMPLE_RATE
static constexpr uint16_t RAW_SAMPLE_RATE
Sample rate of raw pcm samples.
Definition soundloader_raw.cpp:22
SoundLoader_Raw::RAW_SAMPLE_BITS
static constexpr uint8_t RAW_SAMPLE_BITS
Bit depths of raw pcm samples.
Definition soundloader_raw.cpp:23
SoundLoader
Base interface for a SoundLoader implementation.
Definition soundloader_type.h:17
random_access_file_type.h
Class related to random access to files.
safeguards.h
A number of safeguards to prevent using unsafe methods.
sound_type.h
Types related to sounds.
soundloader_type.h
Types related to sound loaders.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
SoundEntry
Definition sound_type.h:19