19 #define SET_TYPE "sounds"
26 static void OpenBankFile(
const char *filename)
28 memset(_original_sounds, 0,
sizeof(_original_sounds));
31 if (filename ==
nullptr)
return;
38 bool new_format =
HasBit(count, 31);
47 DEBUG(misc, 6,
"Incorrect number of sounds in '%s', ignoring.", filename);
67 if (new_format || strcmp(name,
"Corrupt sound") != 0) {
79 if (!new_format) sound->rate = 11025;
83 FioSeekTo(size - (2 + 2 + 4 + 4 + 2 + 1), SEEK_CUR);
84 }
else if (tag ==
'atad') {
85 sound->file_size = size;
102 sound->bits_per_sample = 8;
111 assert(sound !=
nullptr);
114 if (sound->file_size == 0 || sound->file_size > ((
size_t)-1) - 2)
return false;
116 int8 *mem = MallocT<int8>(sound->file_size + 2);
119 mem[sound->file_size ] = 0;
120 mem[sound->file_size + 1] = 0;
126 if (sound->bits_per_sample == 8) {
127 for (uint i = 0; i != sound->file_size; i++) {
132 #if TTD_ENDIAN == TTD_BIG_ENDIAN
133 if (sound->bits_per_sample == 16) {
134 uint num_samples = sound->file_size / 2;
135 int16 *samples = (int16 *)mem;
136 for (uint i = 0; i < num_samples; i++) {
137 samples[i] =
BSWAP16(samples[i]);
142 assert(sound->bits_per_sample == 8 || sound->bits_per_sample == 16);
143 assert(sound->channels == 1);
144 assert(sound->file_size != 0 && sound->rate != 0);
146 MxSetChannelRawSrc(mc, mem, sound->file_size, sound->rate, sound->bits_per_sample == 16);
151 void InitializeSound()
153 DEBUG(misc, 1,
"Loading sound effects...");
158 static void StartSound(SoundID sound_id,
float pan, uint volume)
160 if (volume == 0)
return;
163 if (sound ==
nullptr)
return;
166 if (sound->rate == 0 && sound->file_slot != 0) {
169 sound->file_slot = 0;
175 if (sound->rate == 0)
return;
178 if (mc ==
nullptr)
return;
180 if (!SetBankSource(mc, sound))
return;
183 volume = sound->volume * volume;
186 MxActivateChannel(mc);
190 static const byte _vol_factor_by_zoom[] = {255, 255, 255, 190, 134, 87};
193 static const byte _sound_base_vol[] = {
194 128, 90, 128, 128, 128, 128, 128, 128,
195 128, 90, 90, 128, 128, 128, 128, 128,
196 128, 128, 128, 80, 128, 128, 128, 128,
197 128, 128, 128, 128, 128, 128, 128, 128,
198 128, 128, 90, 90, 90, 128, 90, 128,
199 128, 90, 128, 128, 128, 90, 128, 128,
200 128, 128, 128, 128, 90, 128, 128, 128,
201 128, 90, 128, 128, 128, 128, 128, 128,
202 128, 128, 90, 90, 90, 128, 128, 128,
206 static const byte _sound_idx[] = {
207 2, 3, 4, 5, 6, 7, 8, 9,
208 10, 11, 12, 13, 14, 15, 16, 17,
209 18, 19, 20, 21, 22, 23, 24, 25,
210 26, 27, 28, 29, 30, 31, 32, 33,
211 34, 35, 36, 37, 38, 39, 40, 0,
212 1, 41, 42, 43, 44, 45, 46, 47,
213 48, 49, 50, 51, 52, 53, 54, 55,
214 56, 57, 58, 59, 60, 61, 62, 63,
215 64, 65, 66, 67, 68, 69, 70, 71,
223 sound[i] = _original_sounds[_sound_idx[i]];
224 sound[i].volume = _sound_base_vol[i];
225 sound[i].priority = 0;
242 FOR_ALL_WINDOWS_FROM_BACK(w) {
250 float panning = (float)screen_x / width;
262 void SndPlayTileFx(SoundID sound,
TileIndex tile)
267 int z = (y < 0 ? 0 : GetSlopePixelZ(x, y));
274 void SndPlayVehicleFx(SoundID sound,
const Vehicle *v)
282 void SndPlayFx(SoundID sound)
293 template <
class T,
size_t Tnum_files,
bool Tsearch_in_tars>
296 template <
class Tbase_set>
302 template <
class Tbase_set>
307 const Tbase_set *best =
nullptr;
310 if (c->GetNumMissing() != 0)
continue;
312 if (best ==
nullptr ||
313 (best->fallback && !c->fallback) ||
314 best->valid_files < c->valid_files ||
315 (best->valid_files == c->valid_files &&
316 (best->shortname == c->shortname && best->version < c->version))) {