(svn r27729) -Codechange: Do not count static NewGRF when checking for the maximum...
[openttd.git] / src / newgrf_sound.cpp
blob0cc113d9df49308dd2931d434e224865ee4bbfaf
1 /* $Id$ */
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 */
10 /** @file newgrf_sound.cpp Handling NewGRF provided sounds. */
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"
23 #include "safeguards.h"
25 static SmallVector<SoundEntry, 8> _sounds;
28 /**
29 * Allocate sound slots.
30 * @param num Number of slots to allocate.
31 * @return First allocated slot.
33 SoundEntry *AllocateSound(uint num)
35 SoundEntry *sound = _sounds.Append(num);
36 MemSetT(sound, 0, num);
37 return sound;
41 void InitializeSoundPool()
43 _sounds.Clear();
45 /* Copy original sound data to the pool */
46 SndCopyToPool();
50 SoundEntry *GetSound(SoundID index)
52 if (index >= _sounds.Length()) return NULL;
53 return &_sounds[index];
57 uint GetNumSounds()
59 return _sounds.Length();
63 /**
64 * Extract meta data from a NewGRF sound.
65 * @param sound Sound to load.
66 * @return True if a valid sound was loaded.
68 bool LoadNewGRFSound(SoundEntry *sound)
70 if (sound->file_offset == SIZE_MAX || sound->file_slot == 0) return false;
72 FioSeekToFile(sound->file_slot, sound->file_offset);
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();
78 /* Format: <num> <FF> <FF> <name_len> <name> '\0' <data> */
80 uint32 num = sound->grf_container_ver >= 2 ? FioReadDword() : FioReadWord();
81 if (FioReadByte() != 0xFF) return false;
82 if (FioReadByte() != 0xFF) return false;
84 uint8 name_len = FioReadByte();
85 char *name = AllocaM(char, name_len + 1);
86 FioReadBlock(name, name_len + 1);
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;
94 DEBUG(grf, 2, "LoadNewGRFSound [%s]: Sound name '%s'...", FioGetFilename(sound->file_slot), name);
96 if (FioReadDword() != BSWAP32('RIFF')) {
97 DEBUG(grf, 1, "LoadNewGRFSound [%s]: Missing RIFF header", FioGetFilename(sound->file_slot));
98 return false;
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;
109 if (FioReadDword() != BSWAP32('WAVE')) {
110 DEBUG(grf, 1, "LoadNewGRFSound [%s]: Invalid RIFF type", FioGetFilename(sound->file_slot));
111 return false;
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;
122 total_size -= size;
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;
131 sound->channels = FioReadWord();
132 sound->rate = FioReadDword();
133 FioReadDword();
134 FioReadWord();
135 sound->bits_per_sample = FioReadWord();
137 /* The rest will be skipped */
138 size -= 16;
139 break;
141 case 'atad': // 'data'
142 sound->file_size = size;
143 sound->file_offset = FioGetPos();
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
148 default:
149 /* Skip unknown chunks */
150 break;
153 /* Skip rest of chunk */
154 if (size > 0) FioSkipBytes(size);
157 DEBUG(grf, 1, "LoadNewGRFSound [%s]: RIFF does not contain any sound data", FioGetFilename(sound->file_slot));
159 /* Clear everything that was read */
160 MemSetT(sound, 0);
161 return false;
165 * Resolve NewGRF sound ID.
166 * @param file NewGRF to get sound from.
167 * @param sound_id GRF-specific sound ID. (GRF-local for IDs above ORIGINAL_SAMPLE_COUNT)
168 * @return Translated (global) sound ID, or INVALID_SOUND.
170 SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
172 /* Global sound? */
173 if (sound_id < ORIGINAL_SAMPLE_COUNT) return sound_id;
175 sound_id -= ORIGINAL_SAMPLE_COUNT;
176 if (file == NULL || sound_id >= file->num_sounds) return INVALID_SOUND;
178 return file->sound_offset + sound_id;
182 * Checks whether a NewGRF wants to play a different vehicle sound effect.
183 * @param v Vehicle to play sound effect for.
184 * @param event Trigger for the sound effect.
185 * @return false if the default sound effect shall be played instead.
187 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event)
189 if (!_settings_client.sound.vehicle) return true;
191 const GRFFile *file = v->GetGRF();
192 uint16 callback;
194 /* If the engine has no GRF ID associated it can't ever play any new sounds */
195 if (file == NULL) return false;
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;
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;
204 callback = GetNewGRFSoundID(file, callback);
206 /* Play no sound, if result is invalid */
207 if (callback == INVALID_SOUND) return true;
209 assert(callback < GetNumSounds());
210 SndPlayVehicleFx(callback, v);
211 return true;
215 * Play a NewGRF sound effect at the location of a specific tile.
216 * @param file NewGRF triggering the sound effect.
217 * @param sound_id Sound effect the NewGRF wants to play.
218 * @param tile Location of the effect.
220 void PlayTileSound(const GRFFile *file, SoundID sound_id, TileIndex tile)
222 sound_id = GetNewGRFSoundID(file, sound_id);
223 if (sound_id == INVALID_SOUND) return;
225 assert(sound_id < GetNumSounds());
226 SndPlayTileFx(sound_id, tile);