2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file sound.cpp Handling of playing sounds. */
|
2007-04-04 01:35:16 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2005-03-12 09:38:03 +00:00
|
|
|
#include "mixer.h"
|
2006-09-27 18:17:01 +00:00
|
|
|
#include "newgrf_sound.h"
|
2007-10-29 23:02:31 +00:00
|
|
|
#include "fios.h"
|
2007-12-19 19:44:29 +00:00
|
|
|
#include "window_gui.h"
|
2007-12-27 13:35:39 +00:00
|
|
|
#include "vehicle_base.h"
|
2008-01-29 13:27:31 +00:00
|
|
|
#include "debug.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-08-09 19:50:44 +00:00
|
|
|
/* The type of set we're replacing */
|
|
|
|
#define SET_TYPE "sounds"
|
|
|
|
#include "base_media_func.h"
|
|
|
|
|
2009-05-17 19:27:50 +00:00
|
|
|
static SoundEntry _original_sounds[ORIGINAL_SAMPLE_COUNT];
|
2007-12-29 09:24:26 +00:00
|
|
|
MusicFileSettings msf;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/* Number of levels of panning per side */
|
2005-11-04 10:02:50 +00:00
|
|
|
#define PANNING_LEVELS 16
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-12 09:38:03 +00:00
|
|
|
static void OpenBankFile(const char *filename)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-17 17:48:05 +00:00
|
|
|
memset(_original_sounds, 0, sizeof(_original_sounds));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
FioOpenFile(SOUND_SLOT, filename);
|
2008-05-27 21:41:00 +00:00
|
|
|
size_t pos = FioGetPos();
|
2009-08-09 23:04:08 +00:00
|
|
|
uint count = FioReadDword();
|
|
|
|
|
|
|
|
/* The new format has the highest bit always set */
|
|
|
|
bool new_format = HasBit(count, 31);
|
|
|
|
ClrBit(count, 31);
|
|
|
|
count /= 8;
|
2008-01-29 13:27:31 +00:00
|
|
|
|
|
|
|
/* Simple check for the correct number of original sounds. */
|
2008-10-25 10:26:23 +00:00
|
|
|
if (count != ORIGINAL_SAMPLE_COUNT) {
|
|
|
|
/* Corrupt sample data? Just leave the allocated memory as those tell
|
|
|
|
* there is no sound to play (size = 0 due to calloc). Not allocating
|
|
|
|
* the memory disables valid NewGRFs that replace sounds. */
|
2008-01-29 13:27:31 +00:00
|
|
|
DEBUG(misc, 6, "Incorrect number of sounds in '%s', ignoring.", filename);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-09-15 16:01:34 +00:00
|
|
|
FioSeekTo(pos, SEEK_SET);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-10-25 10:26:23 +00:00
|
|
|
for (uint i = 0; i != ORIGINAL_SAMPLE_COUNT; i++) {
|
2009-05-17 17:48:05 +00:00
|
|
|
_original_sounds[i].file_slot = SOUND_SLOT;
|
2009-08-09 23:04:08 +00:00
|
|
|
_original_sounds[i].file_offset = GB(FioReadDword(), 0, 31) + pos;
|
2009-05-17 17:48:05 +00:00
|
|
|
_original_sounds[i].file_size = FioReadDword();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-05-17 17:48:05 +00:00
|
|
|
for (uint i = 0; i != ORIGINAL_SAMPLE_COUNT; i++) {
|
2009-05-17 19:27:50 +00:00
|
|
|
SoundEntry *sound = &_original_sounds[i];
|
2004-11-05 23:12:33 +00:00
|
|
|
char name[255];
|
|
|
|
|
2009-05-17 19:27:50 +00:00
|
|
|
FioSeekTo(sound->file_offset, SEEK_SET);
|
2004-11-05 23:12:33 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Check for special case, see else case */
|
2004-11-05 23:12:33 +00:00
|
|
|
FioReadBlock(name, FioReadByte()); // Read the name of the sound
|
2009-08-09 23:04:08 +00:00
|
|
|
if (new_format || strcmp(name, "Corrupt sound") != 0) {
|
2004-11-05 23:12:33 +00:00
|
|
|
FioSeekTo(12, SEEK_CUR); // Skip past RIFF header
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Read riff tags */
|
2004-11-05 23:12:33 +00:00
|
|
|
for (;;) {
|
2005-08-27 20:54:48 +00:00
|
|
|
uint32 tag = FioReadDword();
|
|
|
|
uint32 size = FioReadDword();
|
2004-11-05 23:12:33 +00:00
|
|
|
|
|
|
|
if (tag == ' tmf') {
|
|
|
|
FioReadWord(); // wFormatTag
|
2009-08-09 23:04:08 +00:00
|
|
|
sound->channels = FioReadWord(); // wChannels
|
|
|
|
sound->rate = FioReadDword(); // samples per second
|
|
|
|
if (!new_format) sound->rate = 11025; // seems like all old samples should be played at this rate.
|
|
|
|
FioReadDword(); // avg bytes per second
|
|
|
|
FioReadWord(); // alignment
|
2009-05-17 19:27:50 +00:00
|
|
|
sound->bits_per_sample = FioReadByte(); // bits per sample
|
2004-11-05 23:12:33 +00:00
|
|
|
FioSeekTo(size - (2 + 2 + 4 + 4 + 2 + 1), SEEK_CUR);
|
|
|
|
} else if (tag == 'atad') {
|
2009-05-17 19:27:50 +00:00
|
|
|
sound->file_size = size;
|
|
|
|
sound->file_slot = SOUND_SLOT;
|
|
|
|
sound->file_offset = FioGetPos();
|
2004-11-05 23:12:33 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2009-05-17 19:27:50 +00:00
|
|
|
sound->file_size = 0;
|
2004-11-05 23:12:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-11-05 23:12:33 +00:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Special case for the jackhammer sound
|
|
|
|
* (name in sample.cat is "Corrupt sound")
|
|
|
|
* It's no RIFF file, but raw PCM data
|
|
|
|
*/
|
2009-05-17 19:27:50 +00:00
|
|
|
sound->channels = 1;
|
|
|
|
sound->rate = 11025;
|
|
|
|
sound->bits_per_sample = 8;
|
|
|
|
sound->file_slot = SOUND_SLOT;
|
|
|
|
sound->file_offset = FioGetPos();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-17 19:27:50 +00:00
|
|
|
static bool SetBankSource(MixerChannel *mc, const SoundEntry *sound)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-05-17 19:27:50 +00:00
|
|
|
assert(sound != NULL);
|
2005-08-27 20:54:48 +00:00
|
|
|
|
2009-05-17 19:27:50 +00:00
|
|
|
if (sound->file_size == 0) return false;
|
2005-08-27 20:54:48 +00:00
|
|
|
|
2009-08-10 17:46:44 +00:00
|
|
|
int8 *mem = MallocT<int8>(sound->file_size + 2);
|
|
|
|
/* Add two extra bytes so rate conversion can read these
|
|
|
|
* without reading out of it's input buffer. */
|
|
|
|
mem[sound->file_size ] = 0;
|
|
|
|
mem[sound->file_size + 1] = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-05-17 19:27:50 +00:00
|
|
|
FioSeekToFile(sound->file_slot, sound->file_offset);
|
|
|
|
FioReadBlock(mem, sound->file_size);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-08-09 23:04:08 +00:00
|
|
|
/* 16-bit PCM WAV files should be signed by default */
|
|
|
|
if (sound->bits_per_sample == 8) {
|
|
|
|
for (uint i = 0; i != sound->file_size; i++) {
|
|
|
|
mem[i] += -128; // Convert unsigned sound data to signed
|
|
|
|
}
|
2008-01-29 13:27:31 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-10-04 18:52:34 +00:00
|
|
|
#if TTD_ENDIAN == TTD_BIG_ENDIAN
|
|
|
|
if (sound->bits_per_sample == 16) {
|
|
|
|
uint num_samples = sound->file_size / 2;
|
|
|
|
int16 *samples = (int16 *)mem;
|
|
|
|
for (uint i = 0; i < num_samples; i++) {
|
|
|
|
samples[i] = BSWAP16(samples[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-08-09 23:04:08 +00:00
|
|
|
assert(sound->bits_per_sample == 8 || sound->bits_per_sample == 16);
|
|
|
|
assert(sound->channels == 1);
|
|
|
|
assert(sound->file_size != 0 && sound->rate != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-08-09 23:04:08 +00:00
|
|
|
MxSetChannelRawSrc(mc, mem, sound->file_size, sound->rate, sound->bits_per_sample == 16);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-08-09 19:50:44 +00:00
|
|
|
void InitializeSound()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-08-09 19:50:44 +00:00
|
|
|
DEBUG(misc, 1, "Loading sound effects...");
|
|
|
|
OpenBankFile(BaseSounds::GetUsedSet()->files->filename);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Low level sound player */
|
2009-05-17 17:17:48 +00:00
|
|
|
static void StartSound(SoundID sound_id, int panning, uint volume)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-08-27 20:54:48 +00:00
|
|
|
if (volume == 0) return;
|
2008-01-29 13:27:31 +00:00
|
|
|
|
2009-05-17 19:27:50 +00:00
|
|
|
const SoundEntry *sound = GetSound(sound_id);
|
|
|
|
if (sound == NULL) return;
|
2008-01-29 13:27:31 +00:00
|
|
|
|
|
|
|
MixerChannel *mc = MxAllocateChannel();
|
2005-08-27 20:54:48 +00:00
|
|
|
if (mc == NULL) return;
|
2008-01-29 13:27:31 +00:00
|
|
|
|
2009-05-17 19:27:50 +00:00
|
|
|
if (!SetBankSource(mc, sound)) return;
|
2008-01-29 13:27:31 +00:00
|
|
|
|
|
|
|
/* Apply the sound effect's own volume. */
|
2009-05-17 19:27:50 +00:00
|
|
|
volume = (sound->volume * volume) / 128;
|
2005-11-04 10:02:50 +00:00
|
|
|
|
2007-11-19 18:38:10 +00:00
|
|
|
panning = Clamp(panning, -PANNING_LEVELS, PANNING_LEVELS);
|
2008-01-29 13:27:31 +00:00
|
|
|
uint left_vol = (volume * PANNING_LEVELS) - (volume * panning);
|
|
|
|
uint right_vol = (volume * PANNING_LEVELS) + (volume * panning);
|
2005-11-04 10:02:50 +00:00
|
|
|
MxSetChannelVolume(mc, left_vol * 128 / PANNING_LEVELS, right_vol * 128 / PANNING_LEVELS);
|
2005-08-27 20:54:48 +00:00
|
|
|
MxActivateChannel(mc);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-11 11:59:36 +00:00
|
|
|
static const byte _vol_factor_by_zoom[] = {255, 190, 134, 87};
|
2008-06-17 17:25:22 +00:00
|
|
|
assert_compile(lengthof(_vol_factor_by_zoom) == ZOOM_LVL_COUNT);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static const byte _sound_base_vol[] = {
|
|
|
|
128, 90, 128, 128, 128, 128, 128, 128,
|
|
|
|
128, 90, 90, 128, 128, 128, 128, 128,
|
|
|
|
128, 128, 128, 80, 128, 128, 128, 128,
|
|
|
|
128, 128, 128, 128, 128, 128, 128, 128,
|
|
|
|
128, 128, 90, 90, 90, 128, 90, 128,
|
|
|
|
128, 90, 128, 128, 128, 90, 128, 128,
|
|
|
|
128, 128, 128, 128, 90, 128, 128, 128,
|
|
|
|
128, 90, 128, 128, 128, 128, 128, 128,
|
|
|
|
128, 128, 90, 90, 90, 128, 128, 128,
|
|
|
|
90,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const byte _sound_idx[] = {
|
2005-08-27 20:54:48 +00:00
|
|
|
2, 3, 4, 5, 6, 7, 8, 9,
|
2004-08-09 17:04:08 +00:00
|
|
|
10, 11, 12, 13, 14, 15, 16, 17,
|
|
|
|
18, 19, 20, 21, 22, 23, 24, 25,
|
|
|
|
26, 27, 28, 29, 30, 31, 32, 33,
|
2005-08-27 20:54:48 +00:00
|
|
|
34, 35, 36, 37, 38, 39, 40, 0,
|
|
|
|
1, 41, 42, 43, 44, 45, 46, 47,
|
2004-08-09 17:04:08 +00:00
|
|
|
48, 49, 50, 51, 52, 53, 54, 55,
|
|
|
|
56, 57, 58, 59, 60, 61, 62, 63,
|
|
|
|
64, 65, 66, 67, 68, 69, 70, 71,
|
|
|
|
72,
|
|
|
|
};
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void SndCopyToPool()
|
2006-09-27 18:17:01 +00:00
|
|
|
{
|
2009-05-17 14:57:14 +00:00
|
|
|
for (uint i = 0; i < ORIGINAL_SAMPLE_COUNT; i++) {
|
2009-05-17 19:27:50 +00:00
|
|
|
SoundEntry *sound = AllocateSound();
|
|
|
|
*sound = _original_sounds[_sound_idx[i]];
|
|
|
|
sound->volume = _sound_base_vol[i];
|
|
|
|
sound->priority = 0;
|
2006-09-27 18:17:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-19 13:05:05 +00:00
|
|
|
/**
|
|
|
|
* Decide 'where' (between left and right speaker) to play the sound effect.
|
|
|
|
* @param sound Sound effect to play
|
|
|
|
* @param left Left edge of virtual coordinates where the sound is produced
|
|
|
|
* @param right Right edge of virtual coordinates where the sound is produced
|
|
|
|
* @param top Top edge of virtual coordinates where the sound is produced
|
|
|
|
* @param bottom Bottom edge of virtual coordinates where the sound is produced
|
|
|
|
*/
|
2009-05-17 17:17:48 +00:00
|
|
|
static void SndPlayScreenCoordFx(SoundID sound, int left, int right, int top, int bottom)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-08-27 20:54:48 +00:00
|
|
|
if (msf.effect_vol == 0) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-01-06 22:37:42 +00:00
|
|
|
const Window *w;
|
|
|
|
FOR_ALL_WINDOWS_FROM_BACK(w) {
|
|
|
|
const ViewPort *vp = w->viewport;
|
2005-08-27 20:54:48 +00:00
|
|
|
|
|
|
|
if (vp != NULL &&
|
2008-02-17 20:15:20 +00:00
|
|
|
left < vp->virtual_left + vp->virtual_width && right > vp->virtual_left &&
|
|
|
|
top < vp->virtual_top + vp->virtual_height && bottom > vp->virtual_top) {
|
|
|
|
int screen_x = (left + right) / 2 - vp->virtual_left;
|
2008-02-18 14:59:30 +00:00
|
|
|
int width = (vp->virtual_width == 0 ? 1 : vp->virtual_width);
|
|
|
|
int panning = (screen_x * PANNING_LEVELS * 2) / width - PANNING_LEVELS;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
StartSound(
|
2006-09-27 18:17:01 +00:00
|
|
|
sound,
|
2008-02-18 14:59:30 +00:00
|
|
|
panning,
|
2008-01-29 13:27:31 +00:00
|
|
|
(msf.effect_vol * _vol_factor_by_zoom[vp->zoom - ZOOM_LVL_BEGIN]) / 256
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-17 17:17:48 +00:00
|
|
|
void SndPlayTileFx(SoundID sound, TileIndex tile)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-04-23 19:35:36 +00:00
|
|
|
/* emits sound from center of the tile */
|
2009-01-21 02:31:55 +00:00
|
|
|
int x = min(MapMaxX() - 1, TileX(tile)) * TILE_SIZE + TILE_SIZE / 2;
|
|
|
|
int y = min(MapMaxY() - 1, TileY(tile)) * TILE_SIZE - TILE_SIZE / 2;
|
2008-02-19 17:45:30 +00:00
|
|
|
uint z = (y < 0 ? 0 : GetSlopeZ(x, y));
|
|
|
|
Point pt = RemapCoords(x, y, z);
|
2008-02-17 20:15:20 +00:00
|
|
|
y += 2 * TILE_SIZE;
|
|
|
|
Point pt2 = RemapCoords(x, y, GetSlopeZ(x, y));
|
|
|
|
SndPlayScreenCoordFx(sound, pt.x, pt2.x, pt.y, pt2.y);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-05-17 17:17:48 +00:00
|
|
|
void SndPlayVehicleFx(SoundID sound, const Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
SndPlayScreenCoordFx(sound,
|
2009-03-11 17:26:58 +00:00
|
|
|
v->coord.left, v->coord.right,
|
|
|
|
v->coord.top, v->coord.bottom
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-05-17 17:17:48 +00:00
|
|
|
void SndPlayFx(SoundID sound)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-01-29 13:27:31 +00:00
|
|
|
StartSound(sound, 0, msf.effect_vol);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2009-08-09 19:50:44 +00:00
|
|
|
|
|
|
|
INSTANTIATE_BASE_MEDIA_METHODS(BaseMedia<SoundsSet>, SoundsSet)
|
|
|
|
|
|
|
|
/** Names corresponding to the sound set's files */
|
2009-09-20 23:11:01 +00:00
|
|
|
static const char * const _sound_file_names[] = { "samples" };
|
2009-08-10 10:25:40 +00:00
|
|
|
|
|
|
|
|
2009-08-09 19:50:44 +00:00
|
|
|
template <class T, size_t Tnum_files>
|
2009-09-20 23:11:01 +00:00
|
|
|
/* static */ const char * const *BaseSet<T, Tnum_files>::file_names = _sound_file_names;
|
2009-08-09 19:50:44 +00:00
|
|
|
|
|
|
|
template <class Tbase_set>
|
|
|
|
/* static */ const char *BaseMedia<Tbase_set>::GetExtension()
|
|
|
|
{
|
|
|
|
return ".obs"; // OpenTTD Base Sounds
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Tbase_set>
|
|
|
|
/* static */ bool BaseMedia<Tbase_set>::DetermineBestSet()
|
|
|
|
{
|
|
|
|
if (BaseMedia<Tbase_set>::used_set != NULL) return true;
|
|
|
|
|
2009-08-20 17:02:44 +00:00
|
|
|
const Tbase_set *best = NULL;
|
2009-08-09 19:50:44 +00:00
|
|
|
for (const Tbase_set *c = BaseMedia<Tbase_set>::available_sets; c != NULL; c = c->next) {
|
2009-08-20 17:02:44 +00:00
|
|
|
/* Skip unuseable sets */
|
|
|
|
if (c->GetNumMissing() != 0) continue;
|
|
|
|
|
|
|
|
if (best == NULL ||
|
|
|
|
best->valid_files < c->valid_files ||
|
|
|
|
(best->valid_files == c->valid_files &&
|
2009-08-09 19:50:44 +00:00
|
|
|
(best->shortname == c->shortname && best->version < c->version))) {
|
|
|
|
best = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BaseMedia<Tbase_set>::used_set = best;
|
|
|
|
return BaseMedia<Tbase_set>::used_set != NULL;
|
|
|
|
}
|
|
|
|
|