OpenTTD Source: src/newgrf_sound.cpp Source File

OpenTTD Source 12.0-beta1

newgrf_sound.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 "engine_base.h"
12 #include "newgrf.h"
13 #include "newgrf_engine.h"
14 #include "newgrf_sound.h"
15 #include "vehicle_base.h"
16 #include "sound_func.h"
17 #include "random_access_file_type.h"
18 #include "debug.h"
19 #include "settings_type.h"
20
21 #include "safeguards.h"
22
23 static std::vector<SoundEntry> _sounds;
24
25
31 SoundEntry *AllocateSound(uint num)
32 {
33 size_t pos = _sounds.size();
34 _sounds.insert(_sounds.end(), num, SoundEntry());
35 return &_sounds[pos];
36 }
37
38
39 void InitializeSoundPool()
40 {
41 _sounds.clear();
42
43 /* Copy original sound data to the pool */
44 SndCopyToPool();
45 }
46
47
48 SoundEntry *GetSound(SoundID index)
49 {
50 if (index >= _sounds.size()) return nullptr;
51 return &_sounds[index];
52 }
53
54
55 uint GetNumSounds()
56 {
57 return (uint)_sounds.size();
58 }
59
60
66 bool LoadNewGRFSound(SoundEntry *sound)
67 {
68 if (sound->file_offset == SIZE_MAX || sound->file == nullptr) return false;
69
70 RandomAccessFile &file = *sound->file;
71 file.SeekTo(sound->file_offset, SEEK_SET);
72
73 /* Skip ID for container version >= 2 as we only look at the first
74 * entry and ignore any further entries with the same ID. */
75 if (sound->grf_container_ver >= 2) file.ReadDword();
76
77 /* Format: <num> <FF> <FF> <name_len> <name> '\0' <data> */
78
79 uint32 num = sound->grf_container_ver >= 2 ? file.ReadDword() : file.ReadWord();
80 if (file.ReadByte() != 0xFF) return false;
81 if (file.ReadByte() != 0xFF) return false;
82
83 uint8 name_len = file.ReadByte();
84 char *name = AllocaM(char, name_len + 1);
85 file.ReadBlock(name, name_len + 1);
86
87 /* Test string termination */
88 if (name[name_len] != 0) {
89 Debug(grf, 2, "LoadNewGRFSound [{}]: Name not properly terminated", file.GetSimplifiedFilename());
90 return false;
91 }
92
93 Debug(grf, 2, "LoadNewGRFSound [{}]: Sound name '{}'...", file.GetSimplifiedFilename(), name);
94
95 if (file.ReadDword() != BSWAP32('RIFF')) {
96 Debug(grf, 1, "LoadNewGRFSound [{}]: Missing RIFF header", file.GetSimplifiedFilename());
97 return false;
98 }
99
100 uint32 total_size = file.ReadDword();
101 uint header_size = 11;
102 if (sound->grf_container_ver >= 2) header_size++; // The first FF in the sprite is only counted for container version >= 2.
103 if (total_size + name_len + header_size > num) {
104 Debug(grf, 1, "LoadNewGRFSound [{}]: RIFF was truncated", file.GetSimplifiedFilename());
105 return false;
106 }
107
108 if (file.ReadDword() != BSWAP32('WAVE')) {
109 Debug(grf, 1, "LoadNewGRFSound [{}]: Invalid RIFF type", file.GetSimplifiedFilename());
110 return false;
111 }
112
113 while (total_size >= 8) {
114 uint32 tag = file.ReadDword();
115 uint32 size = file.ReadDword();
116 total_size -= 8;
117 if (total_size < size) {
118 Debug(grf, 1, "LoadNewGRFSound [{}]: Invalid RIFF", file.GetSimplifiedFilename());
119 return false;
120 }
121 total_size -= size;
122
123 switch (tag) {
124 case ' tmf': // 'fmt '
125 /* Audio format, must be 1 (PCM) */
126 if (size < 16 || file.ReadWord() != 1) {
127 Debug(grf, 1, "LoadGRFSound [{}]: Invalid audio format", file.GetSimplifiedFilename());
128 return false;
129 }
130 sound->channels = file.ReadWord();
131 sound->rate = file.ReadDword();
132 file.ReadDword();
133 file.ReadWord();
134 sound->bits_per_sample = file.ReadWord();
135
136 /* The rest will be skipped */
137 size -= 16;
138 break;
139
140 case 'atad': // 'data'
141 sound->file_size = size;
142 sound->file_offset = file.GetPos();
143
144 Debug(grf, 2, "LoadNewGRFSound [{}]: channels {}, sample rate {}, bits per sample {}, length {}", file.GetSimplifiedFilename(), sound->channels, sound->rate, sound->bits_per_sample, size);
145 return true; // the fmt chunk has to appear before data, so we are finished
146
147 default:
148 /* Skip unknown chunks */
149 break;
150 }
151
152 /* Skip rest of chunk */
153 if (size > 0) file.SkipBytes(size);
154 }
155
156 Debug(grf, 1, "LoadNewGRFSound [{}]: RIFF does not contain any sound data", file.GetSimplifiedFilename());
157
158 /* Clear everything that was read */
159 MemSetT(sound, 0);
160 return false;
161 }
162
169 SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
170 {
171 /* Global sound? */
172 if (sound_id < ORIGINAL_SAMPLE_COUNT) return sound_id;
173
174 sound_id -= ORIGINAL_SAMPLE_COUNT;
175 if (file == nullptr || sound_id >= file->num_sounds) return INVALID_SOUND;
176
177 return file->sound_offset + sound_id;
178 }
179
186 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
187 {
188 if (!_settings_client.sound.vehicle) return true;
189
190 const GRFFile *file = v->GetGRF();
191 uint16 callback;
192
193 /* If the engine has no GRF ID associated it can't ever play any new sounds */
194 if (file == nullptr) return false;
195
196 /* Check that the vehicle type uses the sound effect callback */
197 if (!HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_SOUND_EFFECT)) return false;
198
199 callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
200 /* Play default sound if callback fails */
201 if (callback == CALLBACK_FAILED) return false;
202
203 callback = GetNewGRFSoundID(file, callback);
204
205 /* Play no sound, if result is invalid */
206 if (callback == INVALID_SOUND) return true;
207
208 assert(callback < GetNumSounds());
209 SndPlayVehicleFx(callback, v);
210 return true;
211 }
212
219 void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
220 {
221 sound_id = GetNewGRFSoundID(file, sound_id);
222 if (sound_id == INVALID_SOUND) return;
223
224 assert(sound_id < GetNumSounds());
225 SndPlayTileFx(sound_id, tile);
226 }
AllocateSound
SoundEntry * AllocateSound(uint num)
Allocate sound slots.
Definition: newgrf_sound.cpp:31
PlayVehicleSound
bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
Checks whether a NewGRF wants to play a different vehicle sound effect.
Definition: newgrf_sound.cpp:186
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
sound_func.h
CBM_VEHICLE_SOUND_EFFECT
@ CBM_VEHICLE_SOUND_EFFECT
Vehicle uses custom sound effects.
Definition: newgrf_callbacks.h:296
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
Vehicle::GetGRF
const GRFFile * GetGRF() const
Retrieve the NewGRF the vehicle is tied to.
Definition: vehicle.cpp:751
vehicle_base.h
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:52
GetNewGRFSoundID
SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
Resolve NewGRF sound ID.
Definition: newgrf_sound.cpp:169
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:221
SoundEntry::grf_container_ver
byte grf_container_ver
NewGRF container version if the sound is from a NewGRF.
Definition: sound_type.h:22
GetVehicleCallback
uint16 GetVehicleCallback(CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v)
Evaluate a newgrf callback for vehicles.
Definition: newgrf_engine.cpp:1155
RandomAccessFile::ReadBlock
void ReadBlock(void *ptr, size_t size)
Read a block.
Definition: random_access_file.cpp:138
RandomAccessFile::ReadByte
byte ReadByte()
Read a byte from the file.
Definition: random_access_file.cpp:100
random_access_file_type.h
RandomAccessFile::SkipBytes
void SkipBytes(int n)
Skip n bytes ahead in the file.
Definition: random_access_file.cpp:148
LoadNewGRFSound
bool LoadNewGRFSound(SoundEntry *sound)
Extract meta data from a NewGRF sound.
Definition: newgrf_sound.cpp:66
PlayTileSound
void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
Play a NewGRF sound effect at the location of a specific tile.
Definition: newgrf_sound.cpp:219
ClientSettings::sound
SoundSettings sound
sound effect settings
Definition: settings_type.h:596
newgrf_engine.h
CBID_VEHICLE_SOUND_EFFECT
@ CBID_VEHICLE_SOUND_EFFECT
Called to play a special sound effect.
Definition: newgrf_callbacks.h:147
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:299
safeguards.h
settings_type.h
SoundEntry
Definition: sound_type.h:13
stdafx.h
BSWAP32
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:390
RandomAccessFile::ReadWord
uint16 ReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: random_access_file.cpp:117
RandomAccessFile::ReadDword
uint32 ReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition: random_access_file.cpp:127
RandomAccessFile::GetPos
size_t GetPos() const
Get position in the file.
Definition: random_access_file.cpp:73
CALLBACK_FAILED
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Definition: newgrf_callbacks.h:404
newgrf_sound.h
newgrf.h
SoundSettings::vehicle
bool vehicle
Play vehicle sound effects.
Definition: settings_type.h:211
ORIGINAL_SAMPLE_COUNT
static const uint ORIGINAL_SAMPLE_COUNT
The number of sounds in the original sample.cat.
Definition: sound_type.h:116
RandomAccessFile::GetSimplifiedFilename
const std::string & GetSimplifiedFilename() const
Get the simplified filename of the opened file.
Definition: random_access_file.cpp:64
RandomAccessFile::SeekTo
void SeekTo(size_t pos, int mode)
Seek in the current file.
Definition: random_access_file.cpp:83
VehicleSoundEvent
VehicleSoundEvent
Events at which a sound might be played.
Definition: newgrf_sound.h:18
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
MemSetT
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
engine_base.h
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
debug.h
RandomAccessFile
A file from which bytes, words and double words are read in (potentially) a random order.
Definition: random_access_file_type.h:23
AllocaM
#define AllocaM(T, num_elements)
alloca() has to be called in the parent function, so define AllocaM() as a macro
Definition: alloc_func.hpp:132