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 static SmallVector
<SoundEntry
, 8> _sounds
;
27 * Allocate sound slots.
28 * @param num Number of slots to allocate.
29 * @return First allocated slot.
31 SoundEntry
*AllocateSound(uint num
)
33 SoundEntry
*sound
= _sounds
.Append(num
);
34 MemSetT(sound
, 0, num
);
39 void InitializeSoundPool()
43 /* Copy original sound data to the pool */
48 SoundEntry
*GetSound(SoundID index
)
50 if (index
>= _sounds
.Length()) return NULL
;
51 return &_sounds
[index
];
57 return _sounds
.Length();
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_slot
== 0) return false;
70 FioSeekToFile(sound
->file_slot
, sound
->file_offset
);
72 /* Skip ID for container version >= 2 as we only look at the first
73 * entry and ignore any further entries with the same ID. */
74 if (sound
->grf_container_ver
>= 2) FioReadDword();
76 /* Format: <num> <FF> <FF> <name_len> <name> '\0' <data> */
78 uint32 num
= sound
->grf_container_ver
>= 2 ? FioReadDword() : FioReadWord();
79 if (FioReadByte() != 0xFF) return false;
80 if (FioReadByte() != 0xFF) return false;
82 uint8 name_len
= FioReadByte();
83 char *name
= AllocaM(char, name_len
+ 1);
84 FioReadBlock(name
, name_len
+ 1);
86 /* Test string termination */
87 if (name
[name_len
] != 0) {
88 DEBUG(grf
, 2, "LoadNewGRFSound [%s]: Name not properly terminated", FioGetFilename(sound
->file_slot
));
92 DEBUG(grf
, 2, "LoadNewGRFSound [%s]: Sound name '%s'...", FioGetFilename(sound
->file_slot
), name
);
94 if (FioReadDword() != BSWAP32('RIFF')) {
95 DEBUG(grf
, 1, "LoadNewGRFSound [%s]: Missing RIFF header", FioGetFilename(sound
->file_slot
));
99 uint32 total_size
= FioReadDword();
100 uint header_size
= 11;
101 if (sound
->grf_container_ver
>= 2) header_size
++; // The first FF in the sprite is only counted for container version >= 2.
102 if (total_size
+ name_len
+ header_size
> num
) {
103 DEBUG(grf
, 1, "LoadNewGRFSound [%s]: RIFF was truncated", FioGetFilename(sound
->file_slot
));
107 if (FioReadDword() != BSWAP32('WAVE')) {
108 DEBUG(grf
, 1, "LoadNewGRFSound [%s]: Invalid RIFF type", FioGetFilename(sound
->file_slot
));
112 while (total_size
>= 8) {
113 uint32 tag
= FioReadDword();
114 uint32 size
= FioReadDword();
116 if (total_size
< size
) {
117 DEBUG(grf
, 1, "LoadNewGRFSound [%s]: Invalid RIFF", FioGetFilename(sound
->file_slot
));
123 case ' tmf': // 'fmt '
124 /* Audio format, must be 1 (PCM) */
125 if (size
< 16 || FioReadWord() != 1) {
126 DEBUG(grf
, 1, "LoadGRFSound [%s]: Invalid audio format", FioGetFilename(sound
->file_slot
));
129 sound
->channels
= FioReadWord();
130 sound
->rate
= FioReadDword();
133 sound
->bits_per_sample
= FioReadWord();
135 /* The rest will be skipped */
139 case 'atad': // 'data'
140 sound
->file_size
= size
;
141 sound
->file_offset
= FioGetPos();
143 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
);
144 return true; // the fmt chunk has to appear before data, so we are finished
147 /* Skip unknown chunks */
151 /* Skip rest of chunk */
152 if (size
> 0) FioSkipBytes(size
);
155 DEBUG(grf
, 1, "LoadNewGRFSound [%s]: RIFF does not contain any sound data", FioGetFilename(sound
->file_slot
));
157 /* Clear everything that was read */
163 * Resolve NewGRF sound ID.
164 * @param file NewGRF to get sound from.
165 * @param sound_id GRF-specific sound ID. (GRF-local for IDs above ORIGINAL_SAMPLE_COUNT)
166 * @return Translated (global) sound ID, or INVALID_SOUND.
168 SoundID
GetNewGRFSoundID(const GRFFile
*file
, SoundID sound_id
)
171 if (sound_id
< ORIGINAL_SAMPLE_COUNT
) return sound_id
;
173 sound_id
-= ORIGINAL_SAMPLE_COUNT
;
174 if (file
== NULL
|| sound_id
>= file
->num_sounds
) return INVALID_SOUND
;
176 return file
->sound_offset
+ sound_id
;
180 * Checks whether a NewGRF wants to play a different vehicle sound effect.
181 * @param v Vehicle to play sound effect for.
182 * @param event Trigger for the sound effect.
183 * @return false if the default sound effect shall be played instead.
185 bool PlayVehicleSound(const Vehicle
*v
, VehicleSoundEvent event
)
187 if (!_settings_client
.sound
.vehicle
) return true;
189 const GRFFile
*file
= v
->GetGRF();
192 /* If the engine has no GRF ID associated it can't ever play any new sounds */
193 if (file
== NULL
) return false;
195 /* Check that the vehicle type uses the sound effect callback */
196 if (!HasBit(EngInfo(v
->engine_type
)->callback_mask
, CBM_VEHICLE_SOUND_EFFECT
)) return false;
198 callback
= GetVehicleCallback(CBID_VEHICLE_SOUND_EFFECT
, event
, 0, v
->engine_type
, v
);
199 /* Play default sound if callback fails */
200 if (callback
== CALLBACK_FAILED
) return false;
202 callback
= GetNewGRFSoundID(file
, callback
);
204 /* Play no sound, if result is invalid */
205 if (callback
== INVALID_SOUND
) return true;
207 assert(callback
< GetNumSounds());
208 SndPlayVehicleFx(callback
, v
);
213 * Play a NewGRF sound effect at the location of a specific tile.
214 * @param file NewGRF triggering the sound effect.
215 * @param sound_id Sound effect the NewGRF wants to play.
216 * @param tile Location of the effect.
218 void PlayTileSound(const GRFFile
*file
, SoundID sound_id
, TileIndex tile
)
220 sound_id
= GetNewGRFSoundID(file
, sound_id
);
221 if (sound_id
== INVALID_SOUND
) return;
223 assert(sound_id
< GetNumSounds());
224 SndPlayTileFx(sound_id
, tile
);