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/>.
10 /** @file newgrf_sound.cpp Handling NewGRF provided sounds. */
13 #include "engine_base.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"
21 #include "settings_type.h"
23 #include "safeguards.h"
25 static SmallVector
<SoundEntry
, 8> _sounds
;
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
);
41 void InitializeSoundPool()
45 /* Copy original sound data to the pool */
50 SoundEntry
*GetSound(SoundID index
)
52 if (index
>= _sounds
.Length()) return NULL
;
53 return &_sounds
[index
];
59 return _sounds
.Length();
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
));
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
));
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
));
109 if (FioReadDword() != BSWAP32('WAVE')) {
110 DEBUG(grf
, 1, "LoadNewGRFSound [%s]: Invalid RIFF type", FioGetFilename(sound
->file_slot
));
114 while (total_size
>= 8) {
115 uint32 tag
= FioReadDword();
116 uint32 size
= FioReadDword();
118 if (total_size
< size
) {
119 DEBUG(grf
, 1, "LoadNewGRFSound [%s]: Invalid RIFF", FioGetFilename(sound
->file_slot
));
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
));
131 sound
->channels
= FioReadWord();
132 sound
->rate
= FioReadDword();
135 sound
->bits_per_sample
= FioReadWord();
137 /* The rest will be skipped */
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
149 /* Skip unknown chunks */
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 */
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
)
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();
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
);
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
);