OpenTTD Source 20241224-master-gf74b0cf984
soundloader.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 "debug.h"
12#include "sound_type.h"
13#include "soundloader_type.h"
14#include "soundloader_func.h"
15#include "string_func.h"
16#include "newgrf_sound.h"
18
19#include "safeguards.h"
20
21template class ProviderManager<SoundLoader>;
22
23bool LoadSoundData(SoundEntry &sound, bool new_format, SoundID sound_id, const std::string &name)
24{
25 /* Check for valid sound size. */
26 if (sound.file_size == 0 || sound.file_size > SIZE_MAX - 2) return false;
27
28 size_t pos = sound.file->GetPos();
29 sound.data = std::make_shared<std::vector<uint8_t>>();
30 for (auto &loader : ProviderManager<SoundLoader>::GetProviders()) {
31 sound.file->SeekTo(pos, SEEK_SET);
32 if (loader->Load(sound, new_format, *sound.data)) break;
33 }
34
35 if (sound.data->empty()) {
36 Debug(grf, 0, "LoadSound [{}]: Failed to load sound '{}' for slot {}", sound.file->GetSimplifiedFilename(), name, sound_id);
37 return false;
38 }
39
40 assert(sound.bits_per_sample == 8 || sound.bits_per_sample == 16);
41 assert(sound.channels == 1);
42 assert(sound.rate != 0);
43
44 Debug(grf, 2, "LoadSound [{}]: channels {}, sample rate {}, bits per sample {}, length {}", sound.file->GetSimplifiedFilename(), sound.channels, sound.rate, sound.bits_per_sample, sound.file_size);
45
46 /* Mixer always requires an extra sample at the end for the built-in linear resampler. */
47 sound.data->resize(sound.data->size() + sound.channels * sound.bits_per_sample / 8);
48 sound.data->shrink_to_fit();
49
50 return true;
51}
52
53static bool LoadBasesetSound(SoundEntry &sound, bool new_format, SoundID sound_id)
54{
55 sound.file->SeekTo(sound.file_offset, SEEK_SET);
56
57 /* Read name of sound for diagnostics. */
58 size_t name_len = sound.file->ReadByte();
59 std::string name(name_len, '\0');
60 sound.file->ReadBlock(name.data(), name_len);
61
62 return LoadSoundData(sound, new_format, sound_id, StrMakeValid(name));
63}
64
65bool LoadSound(SoundEntry &sound, SoundID sound_id)
66{
67 switch (sound.source) {
68 case SoundSource::BasesetOldFormat: return LoadBasesetSound(sound, false, sound_id);
69 case SoundSource::BasesetNewFormat: return LoadBasesetSound(sound, true, sound_id);
70 case SoundSource::NewGRF: return LoadNewGRFSound(sound, sound_id);
71 default: NOT_REACHED();
72 }
73}
The ProviderManager manages a single Provider-type.
void ReadBlock(void *ptr, size_t size)
Read a block.
size_t GetPos() const
Get position in the file.
void SeekTo(size_t pos, int mode)
Seek in the current file.
uint8_t ReadByte()
Read a byte from the file.
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
Base interface for a SoundLoader implementation.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
bool LoadNewGRFSound(SoundEntry &sound, SoundID sound_id)
Extract meta data from a NewGRF sound.
Functions related to NewGRF provided sounds.
Class related to random access to files.
A number of safeguards to prevent using unsafe methods.
Types related to sounds.
Functions related to sound loaders.
Types related to sound loaders.
Definition of base types and functions in a cross-platform compatible way.
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
Definition string.cpp:107
Functions related to low-level strings.