OpenTTD
newgrf_sound.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_sound.cpp 27507 2016-02-08 21:05:57Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #include "stdafx.h"
13 #include "engine_base.h"
14 #include "newgrf.h"
15 #include "newgrf_engine.h"
16 #include "newgrf_sound.h"
17 #include "vehicle_base.h"
18 #include "sound_func.h"
19 #include "fileio_func.h"
20 #include "debug.h"
21 #include "settings_type.h"
22 
23 #include "safeguards.h"
24 
25 static SmallVector<SoundEntry, 8> _sounds;
26 
27 
34 {
35  SoundEntry *sound = _sounds.Append(num);
36  MemSetT(sound, 0, num);
37  return sound;
38 }
39 
40 
41 void InitializeSoundPool()
42 {
43  _sounds.Clear();
44 
45  /* Copy original sound data to the pool */
46  SndCopyToPool();
47 }
48 
49 
50 SoundEntry *GetSound(SoundID index)
51 {
52  if (index >= _sounds.Length()) return NULL;
53  return &_sounds[index];
54 }
55 
56 
57 uint GetNumSounds()
58 {
59  return _sounds.Length();
60 }
61 
62 
69 {
70  if (sound->file_offset == SIZE_MAX || sound->file_slot == 0) return false;
71 
72  FioSeekToFile(sound->file_slot, sound->file_offset);
73 
74  /* Skip ID for container version >= 2 as we only look at the first
75  * entry and ignore any further entries with the same ID. */
76  if (sound->grf_container_ver >= 2) FioReadDword();
77 
78  /* Format: <num> <FF> <FF> <name_len> <name> '\0' <data> */
79 
80  uint32 num = sound->grf_container_ver >= 2 ? FioReadDword() : FioReadWord();
81  if (FioReadByte() != 0xFF) return false;
82  if (FioReadByte() != 0xFF) return false;
83 
84  uint8 name_len = FioReadByte();
85  char *name = AllocaM(char, name_len + 1);
86  FioReadBlock(name, name_len + 1);
87 
88  /* Test string termination */
89  if (name[name_len] != 0) {
90  DEBUG(grf, 2, "LoadNewGRFSound [%s]: Name not properly terminated", FioGetFilename(sound->file_slot));
91  return false;
92  }
93 
94  DEBUG(grf, 2, "LoadNewGRFSound [%s]: Sound name '%s'...", FioGetFilename(sound->file_slot), name);
95 
96  if (FioReadDword() != BSWAP32('RIFF')) {
97  DEBUG(grf, 1, "LoadNewGRFSound [%s]: Missing RIFF header", FioGetFilename(sound->file_slot));
98  return false;
99  }
100 
101  uint32 total_size = FioReadDword();
102  uint header_size = 11;
103  if (sound->grf_container_ver >= 2) header_size++; // The first FF in the sprite is only counted for container version >= 2.
104  if (total_size + name_len + header_size > num) {
105  DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF was truncated", FioGetFilename(sound->file_slot));
106  return false;
107  }
108 
109  if (FioReadDword() != BSWAP32('WAVE')) {
110  DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF type", FioGetFilename(sound->file_slot));
111  return false;
112  }
113 
114  while (total_size >= 8) {
115  uint32 tag = FioReadDword();
116  uint32 size = FioReadDword();
117  total_size -= 8;
118  if (total_size < size) {
119  DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF", FioGetFilename(sound->file_slot));
120  return false;
121  }
122  total_size -= size;
123 
124  switch (tag) {
125  case ' tmf': // 'fmt '
126  /* Audio format, must be 1 (PCM) */
127  if (size < 16 || FioReadWord() != 1) {
128  DEBUG(grf, 1, "LoadGRFSound [%s]: Invalid audio format", FioGetFilename(sound->file_slot));
129  return false;
130  }
131  sound->channels = FioReadWord();
132  sound->rate = FioReadDword();
133  FioReadDword();
134  FioReadWord();
135  sound->bits_per_sample = FioReadWord();
136 
137  /* The rest will be skipped */
138  size -= 16;
139  break;
140 
141  case 'atad': // 'data'
142  sound->file_size = size;
143  sound->file_offset = FioGetPos();
144 
145  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);
146  return true; // the fmt chunk has to appear before data, so we are finished
147 
148  default:
149  /* Skip unknown chunks */
150  break;
151  }
152 
153  /* Skip rest of chunk */
154  if (size > 0) FioSkipBytes(size);
155  }
156 
157  DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF does not contain any sound data", FioGetFilename(sound->file_slot));
158 
159  /* Clear everything that was read */
160  MemSetT(sound, 0);
161  return false;
162 }
163 
170 SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
171 {
172  /* Global sound? */
173  if (sound_id < ORIGINAL_SAMPLE_COUNT) return sound_id;
174 
175  sound_id -= ORIGINAL_SAMPLE_COUNT;
176  if (file == NULL || sound_id >= file->num_sounds) return INVALID_SOUND;
177 
178  return file->sound_offset + sound_id;
179 }
180 
188 {
189  if (!_settings_client.sound.vehicle) return true;
190 
191  const GRFFile *file = v->GetGRF();
192  uint16 callback;
193 
194  /* If the engine has no GRF ID associated it can't ever play any new sounds */
195  if (file == NULL) 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 }
Functions for NewGRF engines.
static const uint CALLBACK_FAILED
Different values for Callback result evaluations.
Functions related to debugging.
uint16 FioReadWord()
Read a word (16 bits) from the file (in low endian format).
Definition: fileio.cpp:166
static const uint ORIGINAL_SAMPLE_COUNT
The number of sounds in the original sample.cat.
Definition: sound_type.h:118
Vehicle data structure.
Definition: vehicle_base.h:212
void Clear()
Remove all items from the list.
Functions for Standard In/Out file operations.
const char * FioGetFilename(uint8 slot)
Get the filename associated with a slot.
Definition: fileio.cpp:78
Simple vector template class.
#define AllocaM(T, num_elements)
alloca() has to be called in the parent function, so define AllocaM() as a macro
Definition: alloc_func.hpp:134
byte FioReadByte()
Read a byte from the file.
Definition: fileio.cpp:133
Vehicle uses custom sound effects.
byte grf_container_ver
NewGRF container version if the sound is from a NewGRF.
Definition: sound_type.h:24
T * Append(uint to_add=1)
Append an item and return it.
uint Length() const
Get the number of items in the list.
Called to play a special sound effect.
SoundSettings sound
sound effect settings
bool LoadNewGRFSound(SoundEntry *sound)
Extract meta data from a NewGRF sound.
void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
Play a NewGRF sound effect at the location of a specific tile.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:76
Types related to global configuration settings.
SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
Resolve NewGRF sound ID.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
void FioSeekToFile(uint8 slot, size_t pos)
Switch to a different file and seek to a position.
Definition: fileio.cpp:115
void FioReadBlock(void *ptr, size_t size)
Read a block.
Definition: fileio.cpp:187
bool vehicle
Play vehicle sound effects.
Functions related to sound.
bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
Checks whether a NewGRF wants to play a different vehicle sound effect.
SoundEntry * AllocateSound(uint num)
Allocate sound slots.
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
uint32 FioReadDword()
Read a double word (32 bits) from the file (in low endian format).
Definition: fileio.cpp:176
Base class for engines.
Base class for all vehicles.
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
void FioSkipBytes(int n)
Skip n bytes ahead in the file.
Definition: fileio.cpp:150
uint16 GetVehicleCallback(CallbackID callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v)
Evaluate a newgrf callback for vehicles.
EngineID engine_type
The type of engine used for this vehicle.
Definition: vehicle_base.h:288
VehicleSoundEvent
Events at which a sound might be played.
Definition: newgrf_sound.h:20
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Functions related to NewGRF provided sounds.
const GRFFile * GetGRF() const
Retrieve the NewGRF the vehicle is tied to.
Definition: vehicle.cpp:753
size_t FioGetPos()
Get position in the current file.
Definition: fileio.cpp:68
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
static void MemSetT(T *ptr, byte value, size_t num=1)
Type-safe version of memset().
Definition: mem_func.hpp:51
Dynamic data of a loaded NewGRF.
Definition: newgrf.h:104
Base for the NewGRF implementation.