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))) {
int virtual_left
Virtual left coordinate.
static Point RemapCoords(int x, int y, int z)
Map 3D world or tile coordinate to equivalent 2D coordinate as used in the viewports and smallmap...
uint16 FioReadWord()
Read a word (16 bits) from the file (in low endian format).
static void SndPlayScreenCoordFx(SoundID sound, int left, int right, int top, int bottom)
Decide 'where' (between left and right speaker) to play the sound effect.
static const uint ORIGINAL_SAMPLE_COUNT
The number of sounds in the original sample.cat.
int virtual_height
height << zoom
MusicSettings music
settings related to music/sound
static uint TileX(TileIndex tile)
Get the X component of a tile.
Subdirectory for all base data (base sets, intro game)
byte FioReadByte()
Read a byte from the file.
static const uint TILE_SIZE
Tile size in world coordinates.
Functions, definitions and such used only by the GUI.
Data structure for an opened window.
bool LoadNewGRFSound(SoundEntry *sound)
Extract meta data from a NewGRF sound.
static const char *const _sound_file_names[]
Names corresponding to the sound set's files.
ClientSettings _settings_client
The current settings for this game.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
void FioSeekTo(size_t pos, int mode)
Seek in the current file.
void MxSetChannelVolume(MixerChannel *mc, uint volume, float pan)
Set volume and pan parameters for a sound.
void FioSeekToFile(uint8 slot, size_t pos)
Switch to a different file and seek to a position.
int virtual_width
width << zoom
void FioReadBlock(void *ptr, size_t size)
Read a block.
#define lengthof(x)
Return the length of an fixed size array.
static T min(const T a, const T b)
Returns the minimum of two values.
SoundEntry * AllocateSound(uint num)
Allocate sound slots.
#define DEBUG(name, level,...)
Output a line of debugging information.
void FioOpenFile(int slot, const char *filename, Subdirectory subdir)
Open a slotted file.
uint32 FioReadDword()
Read a double word (32 bits) from the file (in low endian format).
static T ClrBit(T &x, const uint8 y)
Clears a bit in a variable.
Base class for all vehicles.
Data structure for viewport, display of a part of the world.
Declarations for savegames operations.
uint32 TileIndex
The index/ID of a Tile.
static uint TileY(TileIndex tile)
Get the Y component of a tile.
All data of a sounds set.
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
static uint MapMaxY()
Gets the maximum Y coordinate within the map, including MP_VOID.
Functions related to OTTD's landscape.
static uint16 BSWAP16(uint16 x)
Perform a 16 bits endianness bitswap on x.
Information about a single base set.
Coordinates of a point in 2D.
Functions to mix sound samples.
ZoomLevel zoom
The zoom level of the viewport.
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Functions related to NewGRF provided sounds.
int virtual_top
Virtual top coordinate.
static uint MapMaxX()
Gets the maximum X coordinate within the map, including MP_VOID.
size_t FioGetPos()
Get position in the current file.
ViewportData * viewport
Pointer to viewport data, if present.
Rect coord
NOSAVE: Graphical bounding box of the vehicle, i.e. what to redraw on moves.
byte effect_vol
The requested effects volume.