21 #define SET_TYPE "sounds" 28 static void OpenBankFile(
const char *filename)
30 memset(_original_sounds, 0,
sizeof(_original_sounds));
33 if (filename == NULL)
return;
40 bool new_format =
HasBit(count, 31);
49 DEBUG(misc, 6,
"Incorrect number of sounds in '%s', ignoring.", filename);
69 if (new_format || strcmp(name,
"Corrupt sound") != 0) {
81 if (!new_format) sound->rate = 11025;
85 FioSeekTo(size - (2 + 2 + 4 + 4 + 2 + 1), SEEK_CUR);
86 }
else if (tag ==
'atad') {
87 sound->file_size = size;
104 sound->bits_per_sample = 8;
113 assert(sound != NULL);
116 if (sound->file_size == 0 || sound->file_size > ((
size_t)-1) - 2)
return false;
118 int8 *mem = MallocT<int8>(sound->file_size + 2);
121 mem[sound->file_size ] = 0;
122 mem[sound->file_size + 1] = 0;
128 if (sound->bits_per_sample == 8) {
129 for (uint i = 0; i != sound->file_size; i++) {
134 #if TTD_ENDIAN == TTD_BIG_ENDIAN 135 if (sound->bits_per_sample == 16) {
136 uint num_samples = sound->file_size / 2;
137 int16 *samples = (int16 *)mem;
138 for (uint i = 0; i < num_samples; i++) {
139 samples[i] =
BSWAP16(samples[i]);
144 assert(sound->bits_per_sample == 8 || sound->bits_per_sample == 16);
145 assert(sound->channels == 1);
146 assert(sound->file_size != 0 && sound->rate != 0);
148 MxSetChannelRawSrc(mc, mem, sound->file_size, sound->rate, sound->bits_per_sample == 16);
153 void InitializeSound()
155 DEBUG(misc, 1,
"Loading sound effects...");
160 static void StartSound(SoundID sound_id,
float pan, uint volume)
162 if (volume == 0)
return;
165 if (sound == NULL)
return;
168 if (sound->rate == 0 && sound->file_slot != 0) {
171 sound->file_slot = 0;
177 if (sound->rate == 0)
return;
180 if (mc == NULL)
return;
182 if (!SetBankSource(mc, sound))
return;
185 volume = sound->volume * volume;
188 MxActivateChannel(mc);
192 static const byte _vol_factor_by_zoom[] = {255, 255, 255, 190, 134, 87};
195 static const byte _sound_base_vol[] = {
196 128, 90, 128, 128, 128, 128, 128, 128,
197 128, 90, 90, 128, 128, 128, 128, 128,
198 128, 128, 128, 80, 128, 128, 128, 128,
199 128, 128, 128, 128, 128, 128, 128, 128,
200 128, 128, 90, 90, 90, 128, 90, 128,
201 128, 90, 128, 128, 128, 90, 128, 128,
202 128, 128, 128, 128, 90, 128, 128, 128,
203 128, 90, 128, 128, 128, 128, 128, 128,
204 128, 128, 90, 90, 90, 128, 128, 128,
208 static const byte _sound_idx[] = {
209 2, 3, 4, 5, 6, 7, 8, 9,
210 10, 11, 12, 13, 14, 15, 16, 17,
211 18, 19, 20, 21, 22, 23, 24, 25,
212 26, 27, 28, 29, 30, 31, 32, 33,
213 34, 35, 36, 37, 38, 39, 40, 0,
214 1, 41, 42, 43, 44, 45, 46, 47,
215 48, 49, 50, 51, 52, 53, 54, 55,
216 56, 57, 58, 59, 60, 61, 62, 63,
217 64, 65, 66, 67, 68, 69, 70, 71,
225 sound[i] = _original_sounds[_sound_idx[i]];
226 sound[i].volume = _sound_base_vol[i];
227 sound[i].priority = 0;
244 FOR_ALL_WINDOWS_FROM_BACK(w) {
252 float panning = (float)screen_x / width;
264 void SndPlayTileFx(SoundID sound,
TileIndex tile)
269 int z = (y < 0 ? 0 : GetSlopePixelZ(x, y));
276 void SndPlayVehicleFx(SoundID sound,
const Vehicle *v)
284 void SndPlayFx(SoundID sound)
295 template <
class T,
size_t Tnum_files,
bool Tsearch_in_tars>
298 template <
class Tbase_set>
304 template <
class Tbase_set>
309 const Tbase_set *best = NULL;
312 if (c->GetNumMissing() != 0)
continue;
315 (best->fallback && !c->fallback) ||
316 best->valid_files < c->valid_files ||
317 (best->valid_files == c->valid_files &&
318 (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.