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/>.
8 /** @file newgrf_sound.cpp Handling NewGRF provided sounds. */
11 #include "engine_base.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"
19 #include "settings_type.h"
21 #include "safeguards.h"
23 static std::vector
<SoundEntry
> _sounds
;
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());
39 void InitializeSoundPool()
43 /* Copy original sound data to the pool */
48 SoundEntry
*GetSound(SoundID index
)
50 if (index
>= _sounds
.size()) return nullptr;
51 return &_sounds
[index
];
57 return (uint
)_sounds
.size();
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());
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());
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());
108 if (file
.ReadDword() != BSWAP32('WAVE')) {
109 Debug(grf
, 1, "LoadNewGRFSound [{}]: Invalid RIFF type", file
.GetSimplifiedFilename());
113 while (total_size
>= 8) {
114 uint32_t tag
= file
.ReadDword();
115 uint32_t size
= file
.ReadDword();
117 if (total_size
< size
) {
118 Debug(grf
, 1, "LoadNewGRFSound [{}]: Invalid RIFF", file
.GetSimplifiedFilename());
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());
130 sound
->channels
= file
.ReadWord();
131 sound
->rate
= file
.ReadDword();
134 sound
->bits_per_sample
= file
.ReadWord();
136 /* The rest will be skipped */
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
148 /* Skip unknown chunks */
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 */
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
)
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();
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
);
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
);