Fix: Don't allow right-click to close world generation progress window. (#13084)
[openttd-github.git] / src / newgrf_sound.cpp
blobc79ca59ffbbb6b3ff92d83fc89f4f9d54bc23e59
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 */
8 /** @file newgrf_sound.cpp Handling NewGRF provided sounds. */
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"
21 #include "safeguards.h"
23 static std::vector<SoundEntry> _sounds;
26 /**
27 * Allocate sound slots.
28 * @param num Number of slots to allocate.
29 * @return First allocated slot.
31 SoundEntry *AllocateSound(uint num)
33 size_t pos = _sounds.size();
34 _sounds.insert(_sounds.end(), num, SoundEntry());
35 return &_sounds[pos];
39 void InitializeSoundPool()
41 _sounds.clear();
43 /* Copy original sound data to the pool */
44 SndCopyToPool();
48 SoundEntry *GetSound(SoundID index)
50 if (index >= _sounds.size()) return nullptr;
51 return &_sounds[index];
55 uint GetNumSounds()
57 return (uint)_sounds.size();
61 /**
62 * Extract meta data from a NewGRF sound.
63 * @param sound Sound to load.
64 * @return True if a valid sound was loaded.
66 bool LoadNewGRFSound(SoundEntry *sound)
68 if (sound->file_offset == SIZE_MAX || sound->file == nullptr) return false;
70 RandomAccessFile &file = *sound->file;
71 file.SeekTo(sound->file_offset, SEEK_SET);
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();
77 /* Format: <num> <FF> <FF> <name_len> <name> '\0' <data> */
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;
83 uint8_t name_len = file.ReadByte();
84 std::string name(name_len + 1, '\0');
85 file.ReadBlock(name.data(), name_len + 1);
87 /* Test string termination */
88 if (name[name_len] != 0) {
89 Debug(grf, 2, "LoadNewGRFSound [{}]: Name not properly terminated", file.GetSimplifiedFilename());
90 return false;
93 Debug(grf, 2, "LoadNewGRFSound [{}]: Sound name '{}'...", file.GetSimplifiedFilename(), name);
95 if (file.ReadDword() != BSWAP32('RIFF')) {
96 Debug(grf, 1, "LoadNewGRFSound [{}]: Missing RIFF header", file.GetSimplifiedFilename());
97 return false;
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;
108 if (file.ReadDword() != BSWAP32('WAVE')) {
109 Debug(grf, 1, "LoadNewGRFSound [{}]: Invalid RIFF type", file.GetSimplifiedFilename());
110 return false;
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;
121 total_size -= size;
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;
130 sound->channels = file.ReadWord();
131 sound->rate = file.ReadDword();
132 file.ReadDword();
133 file.ReadWord();
134 sound->bits_per_sample = file.ReadWord();
136 /* The rest will be skipped */
137 size -= 16;
138 break;
140 case 'atad': // 'data'
141 sound->file_size = size;
142 sound->file_offset = file.GetPos();
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
147 default:
148 /* Skip unknown chunks */
149 break;
152 /* Skip rest of chunk */
153 if (size > 0) file.SkipBytes(size);
156 Debug(grf, 1, "LoadNewGRFSound [{}]: RIFF does not contain any sound data", file.GetSimplifiedFilename());
158 /* Clear everything that was read */
159 MemSetT(sound, 0);
160 return false;
164 * Resolve NewGRF sound ID.
165 * @param file NewGRF to get sound from.
166 * @param sound_id GRF-specific sound ID. (GRF-local for IDs above ORIGINAL_SAMPLE_COUNT)
167 * @return Translated (global) sound ID, or INVALID_SOUND.
169 SoundID GetNewGRFSoundID(const GRFFile *file, SoundID sound_id)
171 /* Global sound? */
172 if (sound_id < ORIGINAL_SAMPLE_COUNT) return sound_id;
174 sound_id -= ORIGINAL_SAMPLE_COUNT;
175 if (file == nullptr || sound_id >= file->num_sounds) return INVALID_SOUND;
177 return file->sound_offset + sound_id;
181 * Checks whether a NewGRF wants to play a different vehicle sound effect.
182 * @param v Vehicle to play sound effect for.
183 * @param event Trigger for the sound effect.
184 * @param force Should we play the sound effect even if vehicle sound effects are muted?
185 * @return false if the default sound effect shall be played instead.
187 bool PlayVehicleSound(const Vehicle *v, VehicleSoundEvent event, bool force)
189 if (!_settings_client.sound.vehicle && !force) return true;
191 const GRFFile *file = v->GetGRF();
192 uint16_t callback;
194 /* If the engine has no GRF ID associated it can't ever play any new sounds */
195 if (file == nullptr) 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);