OpenTTD Source: src/soundloader.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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"
17#include "random_access_file_type.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<std::byte>>();
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}
ProviderManager
The ProviderManager manages a single Provider-type.
Definition provider_manager.h:21
RandomAccessFile::ReadBlock
void ReadBlock(void *ptr, size_t size)
Read a block.
Definition random_access_file.cpp:145
RandomAccessFile::GetPos
size_t GetPos() const
Get position in the file.
Definition random_access_file.cpp:71
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition random_access_file.cpp:90
RandomAccessFile::ReadByte
uint8_t ReadByte()
Read a byte from the file.
Definition random_access_file.cpp:107
RandomAccessFile::GetSimplifiedFilename
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
Definition random_access_file.cpp:62
SoundLoader
Base interface for a SoundLoader implementation.
Definition soundloader_type.h:17
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
LoadNewGRFSound
bool LoadNewGRFSound(SoundEntry &sound, SoundID sound_id)
Extract meta data from a NewGRF sound.
Definition newgrf_sound.cpp:83
newgrf_sound.h
Functions related to NewGRF provided sounds.
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_func.h
Functions related to sound loaders.
soundloader_type.h
Types related to sound loaders.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
StrMakeValid
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Definition string.cpp:119
string_func.h
Functions related to low-level strings.
SoundEntry
Definition sound_type.h:19