OpenTTD Source  1.11.0-beta2
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 "fileio_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_slot == 0) return false;
69 
70  FioSeekToFile(sound->file_slot, sound->file_offset);
71 
72  /* Skip ID for container version >= 2 as we only look at the first
73  * entry and ignore any further entries with the same ID. */
74  if (sound->grf_container_ver >= 2) FioReadDword();
75 
76  /* Format: <num> <FF> <FF> <name_len> <name> '\0' <data> */
77 
78  uint32 num = sound->grf_container_ver >= 2 ? FioReadDword() : FioReadWord();
79  if (FioReadByte() != 0xFF) return false;
80  if (FioReadByte() != 0xFF) return false;
81 
82  uint8 name_len = FioReadByte();
83  char *name = AllocaM(char, name_len + 1);
84  FioReadBlock(name, name_len + 1);
85 
86  /* Test string termination */
87  if (name[name_len] != 0) {
88  DEBUG(grf, 2, "LoadNewGRFSound [%s]: Name not properly terminated", FioGetFilename(sound->file_slot));
89  return false;
90  }
91 
92  DEBUG(grf, 2, "LoadNewGRFSound [%s]: Sound name '%s'...", FioGetFilename(sound->file_slot), name);
93 
94  if (FioReadDword() != BSWAP32('RIFF')) {
95  DEBUG(grf, 1, "LoadNewGRFSound [%s]: Missing RIFF header", FioGetFilename(sound->file_slot));
96  return false;
97  }
98 
99  uint32 total_size = FioReadDword();
100  uint header_size = 11;
101  if (sound->grf_container_ver >= 2) header_size++; // The first FF in the sprite is only counted for container version >= 2.
102  if (total_size + name_len + header_size > num) {
103  DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF was truncated", FioGetFilename(sound->file_slot));
104  return false;
105  }
106 
107  if (FioReadDword() != BSWAP32('WAVE')) {
108  DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF type", FioGetFilename(sound->file_slot));
109  return false;
110  }
111 
112  while (total_size >= 8) {
113  uint32 tag = FioReadDword();
114  uint32 size = FioReadDword();
115  total_size -= 8;
116  if (total_size < size) {
117  DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF", FioGetFilename(sound->file_slot));
118  return false;
119  }
120  total_size -= size;
121 
122  switch (tag) {
123  case ' tmf': // 'fmt '
124  /* Audio format, must be 1 (PCM) */
125  if (size < 16 || FioReadWord() != 1) {
126  DEBUG(grf, 1, "LoadGRFSound [%s]: Invalid audio format", FioGetFilename(sound->file_slot));
127  return false;
128  }
129  sound->channels = FioReadWord();
130  sound->rate = FioReadDword();
131  FioReadDword();
132  FioReadWord();
133  sound->bits_per_sample = FioReadWord();
134 
135  /* The rest will be skipped */
136  size -= 16;
137  break;
138 
139  case 'atad': // 'data'
140  sound->file_size = size;
141  sound->file_offset = FioGetPos();
142 
143  DEBUG(grf, 2, "LoadNewGRFSound [%s]: channels %u, sample rate %u, bits per sample %u, length %u", FioGetFilename(sound->file_slot), sound->channels, sound->rate, sound->bits_per_sample, size);
144  return true; // the fmt chunk has to appear before data, so we are finished
145 
146  default:
147  /* Skip unknown chunks */
148  break;
149  }
150 
151  /* Skip rest of chunk */
152  if (size > 0) FioSkipBytes(size);
153  }
154 
155  DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF does not contain any sound data", FioGetFilename(sound->file_slot));
156 
157  /* Clear everything that was read */
158  MemSetT(sound, 0);
159  return false;
160 }
161 
168 SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
169 {
170  /* Global sound? */
171  if (sound_id < ORIGINAL_SAMPLE_COUNT) return sound_id;
172 
173  sound_id -= ORIGINAL_SAMPLE_COUNT;
174  if (file == nullptr || sound_id >= file->num_sounds) return INVALID_SOUND;
175 
176  return file->sound_offset + sound_id;
177 }
178 
186 {
187  if (!_settings_client.sound.vehicle) return true;
188 
189  const GRFFile *file = v->GetGRF();
190  uint16 callback;
191 
192  /* If the engine has no GRF ID associated it can't ever play any new sounds */
193  if (file == nullptr) return false;
194 
195  /* Check that the vehicle type uses the sound effect callback */
196  if (!HasBit(EngInfo(v->engine_type)->callback_mask, CBM_VEHICLE_SOUND_EFFECT)) return false;
197 
198  callback = GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT, event, 0, v->engine_type, v);
199  /* Play default sound if callback fails */
200  if (callback == CALLBACK_FAILED) return false;
201 
202  callback = GetNewGRFSoundID(file, callback);
203 
204  /* Play no sound, if result is invalid */
205  if (callback == INVALID_SOUND) return true;
206 
207  assert(callback < GetNumSounds());
208  SndPlayVehicleFx(callback, v);
209  return true;
210 }
211 
218 void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
219 {
220  sound_id = GetNewGRFSoundID(file, sound_id);
221  if (sound_id == INVALID_SOUND) return;
222 
223  assert(sound_id < GetNumSounds());
224  SndPlayTileFx(sound_id, tile);
225 }
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:185
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
sound_func.h
FioReadBlock
void FioReadBlock(void *ptr, size_t size)
Read a block.
Definition: fileio.cpp:161
FioReadWord
uint16 FioReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: fileio.cpp:140
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
fileio_func.h
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
GetNewGRFSoundID
SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
Resolve NewGRF sound ID.
Definition: newgrf_sound.cpp:168
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:222
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:1188
FioReadDword
uint32 FioReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition: fileio.cpp:150
FioSkipBytes
void FioSkipBytes(int n)
Skip n bytes ahead in the file.
Definition: fileio.cpp:124
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:218
ClientSettings::sound
SoundSettings sound
sound effect settings
Definition: settings_type.h:570
newgrf_engine.h
CBID_VEHICLE_SOUND_EFFECT
@ CBID_VEHICLE_SOUND_EFFECT
Called to play a special sound effect.
Definition: newgrf_callbacks.h:147
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
Vehicle::engine_type
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:297
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:380
FioGetPos
size_t FioGetPos()
Get position in the current file.
Definition: fileio.cpp:59
FioReadByte
byte FioReadByte()
Read a byte from the file.
Definition: fileio.cpp:107
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:190
ORIGINAL_SAMPLE_COUNT
static const uint ORIGINAL_SAMPLE_COUNT
The number of sounds in the original sample.cat.
Definition: sound_type.h:116
VehicleSoundEvent
VehicleSoundEvent
Events at which a sound might be played.
Definition: newgrf_sound.h:18
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
FioSeekToFile
void FioSeekToFile(uint8 slot, size_t pos)
Switch to a different file and seek to a position.
Definition: fileio.cpp:94
GRFFile
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:105
debug.h
FioGetFilename
const char * FioGetFilename(uint8 slot)
Get the filename associated with a slot.
Definition: fileio.cpp:69
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