2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2007-04-12 13:07:15 +00:00
|
|
|
#include "landscape.h"
|
2005-03-12 09:38:03 +00:00
|
|
|
#include "mixer.h"
|
2007-12-29 09:24:26 +00:00
|
|
|
#include "sound_func.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "fileio.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-25 09:48:53 +00:00
|
|
|
#include "core/alloc_func.hpp"
|
2007-12-26 11:45:43 +00:00
|
|
|
#include "map_func.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
|
|
|
|
2005-03-12 09:38:03 +00:00
|
|
|
static uint _file_count;
|
2006-07-26 03:33:12 +00:00
|
|
|
static FileEntry *_files;
|
2007-12-29 09:24:26 +00:00
|
|
|
MusicFileSettings msf;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-11-04 10:02:50 +00:00
|
|
|
// Number of levels of panning per side
|
|
|
|
#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
|
|
|
{
|
2005-08-27 20:54:48 +00:00
|
|
|
uint i;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
FioOpenFile(SOUND_SLOT, filename);
|
2007-09-15 16:01:34 +00:00
|
|
|
uint pos = FioGetPos();
|
2008-01-29 13:27:31 +00:00
|
|
|
uint count = FioReadDword() / 8;
|
|
|
|
|
|
|
|
/* Simple check for the correct number of original sounds. */
|
|
|
|
if (count != 73) {
|
|
|
|
DEBUG(misc, 6, "Incorrect number of sounds in '%s', ignoring.", filename);
|
|
|
|
_file_count = 0;
|
|
|
|
_files = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-01-11 17:29:39 +00:00
|
|
|
FileEntry *fe = CallocT<FileEntry>(count);
|
2005-08-27 20:54:48 +00:00
|
|
|
|
|
|
|
if (fe == NULL) {
|
|
|
|
_file_count = 0;
|
|
|
|
_files = NULL;
|
2005-08-28 10:59:34 +00:00
|
|
|
return;
|
2005-08-27 20:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_file_count = count;
|
|
|
|
_files = fe;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-09-15 16:01:34 +00:00
|
|
|
FioSeekTo(pos, SEEK_SET);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2005-03-12 09:38:03 +00:00
|
|
|
for (i = 0; i != count; i++) {
|
2007-09-13 18:22:34 +00:00
|
|
|
fe[i].file_slot = SOUND_SLOT;
|
2007-09-15 16:01:34 +00:00
|
|
|
fe[i].file_offset = FioReadDword() + pos;
|
2005-03-12 09:38:03 +00:00
|
|
|
fe[i].file_size = FioReadDword();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-11-05 23:12:33 +00:00
|
|
|
for (i = 0; i != count; i++, fe++) {
|
|
|
|
char name[255];
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
FioSeekTo(fe->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
|
|
|
|
if (strcmp(name, "Corrupt sound") != 0) {
|
|
|
|
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
|
|
|
|
fe->channels = FioReadWord(); // wChannels
|
|
|
|
FioReadDword(); // samples per second
|
|
|
|
fe->rate = 11025; // seems like all samples should be played at this rate.
|
|
|
|
FioReadDword(); // avg bytes per second
|
|
|
|
FioReadWord(); // alignment
|
|
|
|
fe->bits_per_sample = FioReadByte(); // bits per sample
|
|
|
|
FioSeekTo(size - (2 + 2 + 4 + 4 + 2 + 1), SEEK_CUR);
|
|
|
|
} else if (tag == 'atad') {
|
|
|
|
fe->file_size = size;
|
2007-09-13 18:22:34 +00:00
|
|
|
fe->file_slot = SOUND_SLOT;
|
|
|
|
fe->file_offset = FioGetPos();
|
2004-11-05 23:12:33 +00:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
fe->file_size = 0;
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
fe->channels = 1;
|
|
|
|
fe->rate = 11025;
|
|
|
|
fe->bits_per_sample = 8;
|
2007-09-13 18:22:34 +00:00
|
|
|
fe->file_slot = SOUND_SLOT;
|
|
|
|
fe->file_offset = FioGetPos();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
uint GetNumOriginalSounds()
|
2006-09-27 18:17:01 +00:00
|
|
|
{
|
|
|
|
return _file_count;
|
|
|
|
}
|
|
|
|
|
2008-01-29 13:27:31 +00:00
|
|
|
static bool SetBankSource(MixerChannel *mc, const FileEntry *fe)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-01-29 13:27:31 +00:00
|
|
|
assert(fe != NULL);
|
2005-08-27 20:54:48 +00:00
|
|
|
|
|
|
|
if (fe->file_size == 0) return false;
|
|
|
|
|
2007-01-11 17:29:39 +00:00
|
|
|
int8 *mem = MallocT<int8>(fe->file_size);
|
2005-08-27 20:54:48 +00:00
|
|
|
if (mem == NULL) return false;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-09-13 18:22:34 +00:00
|
|
|
FioSeekToFile(fe->file_slot, fe->file_offset);
|
2004-08-09 17:04:08 +00:00
|
|
|
FioReadBlock(mem, fe->file_size);
|
|
|
|
|
2008-01-29 13:27:31 +00:00
|
|
|
for (uint i = 0; i != fe->file_size; i++) {
|
2005-01-24 11:32:47 +00:00
|
|
|
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
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
assert(fe->bits_per_sample == 8 && fe->channels == 1 && fe->file_size != 0 && fe->rate != 0);
|
|
|
|
|
2005-03-12 09:38:03 +00:00
|
|
|
MxSetChannelRawSrc(mc, mem, fe->file_size, fe->rate, MX_AUTOFREE);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-03-12 09:38:03 +00:00
|
|
|
bool SoundInitialize(const char *filename)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-03-12 09:38:03 +00:00
|
|
|
OpenBankFile(filename);
|
2004-08-09 17:04:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* Low level sound player */
|
2005-11-04 10:02:50 +00:00
|
|
|
static void StartSound(uint sound, 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
|
|
|
|
|
|
|
const FileEntry *fe = GetSound(sound);
|
|
|
|
if (fe == NULL) return;
|
|
|
|
|
|
|
|
MixerChannel *mc = MxAllocateChannel();
|
2005-08-27 20:54:48 +00:00
|
|
|
if (mc == NULL) return;
|
2008-01-29 13:27:31 +00:00
|
|
|
|
|
|
|
if (!SetBankSource(mc, fe)) return;
|
|
|
|
|
|
|
|
/* Apply the sound effect's own volume. */
|
|
|
|
volume = (fe->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};
|
2007-12-17 22:04:07 +00:00
|
|
|
assert_compile(lengthof(_vol_factor_by_zoom) == ZOOM_LVL_END - ZOOM_LVL_BEGIN);
|
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
|
|
|
{
|
2008-04-07 20:28:58 +00:00
|
|
|
for (uint i = 0; i < _file_count; i++) {
|
2006-09-27 18:17:01 +00:00
|
|
|
FileEntry *orig = &_files[_sound_idx[i]];
|
|
|
|
FileEntry *fe = AllocateFileEntry();
|
|
|
|
|
2006-11-05 08:24:52 +00:00
|
|
|
*fe = *orig;
|
2006-09-27 18:17:01 +00:00
|
|
|
fe->volume = _sound_base_vol[i];
|
|
|
|
fe->priority = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2008-02-17 20:15:20 +00:00
|
|
|
static void SndPlayScreenCoordFx(SoundFx sound, int left, int right, int top, int bottom)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-11-19 22:24:18 +00:00
|
|
|
Window* const *wz;
|
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
|
|
|
|
2006-11-18 16:47:02 +00:00
|
|
|
FOR_ALL_WINDOWS(wz) {
|
|
|
|
const ViewPort *vp = (*wz)->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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-05 23:12:33 +00:00
|
|
|
void SndPlayTileFx(SoundFx 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 */
|
|
|
|
int x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
|
2008-02-17 20:15:20 +00:00
|
|
|
int y = 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
|
|
|
}
|
|
|
|
|
2004-11-05 23:12:33 +00:00
|
|
|
void SndPlayVehicleFx(SoundFx sound, const Vehicle *v)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
SndPlayScreenCoordFx(sound,
|
2008-02-17 20:15:20 +00:00
|
|
|
v->left_coord, v->right_coord,
|
|
|
|
v->top_coord, v->top_coord
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2004-11-05 23:12:33 +00:00
|
|
|
void SndPlayFx(SoundFx 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
|
|
|
}
|