OpenTTD Source  20241124-master-g9399a92a4f
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"
18 #include "debug.h"
19 #include "settings_type.h"
20 
21 #include "safeguards.h"
22 
23 static std::vector<SoundEntry> _sounds;
24 
25 
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 
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_t 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_t name_len = file.ReadByte();
84  std::string name(name_len + 1, '\0');
85  file.ReadBlock(name.data(), 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_t 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_t tag = file.ReadDword();
115  uint32_t 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 
187 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event, bool force)
188 {
189  if (!_settings_client.sound.vehicle && !force) return true;
190 
191  const GRFFile *file = v->GetGRF();
192  uint16_t callback;
193 
194  /* If the engine has no GRF ID associated it can't ever play any new sounds */
195  if (file == nullptr) return false;
196 
197  /* Check that the vehicle type uses the sound effect callback */
198  if (!HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_SOUND_EFFECT)) return false;
199 
200  callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
201  /* Play default sound if callback fails */
202  if (callback == CALLBACK_FAILED) return false;
203 
204  callback = GetNewGRFSoundID(file, callback);
205 
206  /* Play no sound, if result is invalid */
207  if (callback == INVALID_SOUND) return true;
208 
209  assert(callback < GetNumSounds());
210  SndPlayVehicleFx(callback, v);
211  return true;
212 }
213 
220 void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
221 {
222  sound_id = GetNewGRFSoundID(file, sound_id);
223  if (sound_id == INVALID_SOUND) return;
224 
225  assert(sound_id < GetNumSounds());
226  SndPlayTileFx(sound_id, tile);
227 }
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
A file from which bytes, words and double words are read in (potentially) a random order.
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.
uint32_t ReadDword()
Read a double word (32 bits) from the file (in low endian format).
void SkipBytes(size_t n)
Skip n bytes ahead in the file.
uint16_t ReadWord()
Read a word (16 bits) from the file (in low endian format).
Functions related to debugging.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
Base class for engines.
void MemSetT(T *ptr, uint8_t value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:49
Base for the NewGRF implementation.
@ CBID_VEHICLE_SOUND_EFFECT
Called to play a special sound effect.
@ CBM_VEHICLE_SOUND_EFFECT
Vehicle uses custom sound effects.
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
uint16_t GetVehicleCallback(CallbackID callback, uint32_t param1, uint32_t param2, EngineID engine, const Vehicle *v)
Evaluate a newgrf callback for vehicles.
Functions for NewGRF engines.
void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
Play a NewGRF sound effect at the location of a specific tile.
bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event, bool force)
Checks whether a NewGRF wants to play a different vehicle sound effect.
bool LoadNewGRFSound(SoundEntry *sound)
Extract meta data from a NewGRF sound.
SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
Resolve NewGRF sound ID.
SoundEntry * AllocateSound(uint num)
Allocate sound slots.
Functions related to NewGRF provided sounds.
VehicleSoundEvent
Events at which a sound might be played.
Definition: newgrf_sound.h:18
Class related to random access to files.
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
Types related to global configuration settings.
Functions related to sound.
static const uint ORIGINAL_SAMPLE_COUNT
The number of sounds in the original sample.cat.
Definition: sound_type.h:116
Definition of base types and functions in a cross-platform compatible way.
SoundSettings sound
sound effect settings
uint16_t callback_mask
Bitmask of vehicle callbacks that have to be called.
Definition: engine_type.h:160
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:108
uint8_t grf_container_ver
NewGRF container version if the sound is from a NewGRF.
Definition: sound_type.h:22
bool vehicle
Play vehicle sound effects.
Vehicle data structure.
Definition: vehicle_base.h:244
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:323
const GRFFile * GetGRF() const
Retrieve the NewGRF the vehicle is tied to.
Definition: vehicle.cpp:757
Base class for all vehicles.