src/sound.cpp
author smatz
Mon, 17 Dec 2007 22:04:07 +0000
changeset 8095 f834186120af
parent 7954 57b51c69c072
child 8106 e6790dd9e750
permissions -rw-r--r--
(svn r11656) -Codechange: add ZOOM_LVL_BEGIN and postfix operators so ZoomLevel can be used in some iterations
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6247
diff changeset
     3
/** @file sound.cpp */
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6247
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1498
diff changeset
     6
#include "openttd.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 1891
diff changeset
     7
#include "functions.h"
6453
226bcddeba32 (svn r9609) -Codechange: Move some function prototypes out of functions.h and into landscape.h, and add a few where they didn't exist.
maedhros
parents: 6420
diff changeset
     8
#include "landscape.h"
679
04ca2cd69420 (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 337
diff changeset
     9
#include "map.h"
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    10
#include "mixer.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "sound.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include "window.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "viewport.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "fileio.h"
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
    16
#include "newgrf_sound.h"
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
    17
#include "helpers.hpp"
7805
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7591
diff changeset
    18
#include "fios.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    20
static uint _file_count;
4171
5c6e60c392c3 (svn r5609) CodeChange : Apply coding style
belugas
parents: 3898
diff changeset
    21
static FileEntry *_files;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
    23
// Number of levels of panning per side
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
    24
#define PANNING_LEVELS 16
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    27
static void OpenBankFile(const char *filename)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
{
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    29
	uint count;
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    30
	uint i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
	FioOpenFile(SOUND_SLOT, filename);
7591
06202df3799e (svn r11118) -Fix: sample.cat can now be in a tar-file too, how nice!
truelight
parents: 7570
diff changeset
    33
	uint pos = FioGetPos();
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    34
	count = FioReadDword() / 8;
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
    35
	FileEntry *fe = CallocT<FileEntry>(count);
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    36
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    37
	if (fe == NULL) {
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    38
		_file_count = 0;
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    39
		_files = NULL;
2372
152ed96759ba (svn r2898) Fix typo in r2897
tron
parents: 2371
diff changeset
    40
		return;
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    41
	}
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    42
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    43
	_file_count = count;
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    44
	_files = fe;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
7591
06202df3799e (svn r11118) -Fix: sample.cat can now be in a tar-file too, how nice!
truelight
parents: 7570
diff changeset
    46
	FioSeekTo(pos, SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    48
	for (i = 0; i != count; i++) {
7570
5d5d9b6af0ef (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 6891
diff changeset
    49
		fe[i].file_slot = SOUND_SLOT;
7591
06202df3799e (svn r11118) -Fix: sample.cat can now be in a tar-file too, how nice!
truelight
parents: 7570
diff changeset
    50
		fe[i].file_offset = FioReadDword() + pos;
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    51
		fe[i].file_size = FioReadDword();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
    53
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    54
	for (i = 0; i != count; i++, fe++) {
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    55
		char name[255];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    57
		FioSeekTo(fe->file_offset, SEEK_SET);
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    58
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6247
diff changeset
    59
		/* Check for special case, see else case */
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    60
		FioReadBlock(name, FioReadByte()); // Read the name of the sound
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    61
		if (strcmp(name, "Corrupt sound") != 0) {
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    62
			FioSeekTo(12, SEEK_CUR); // Skip past RIFF header
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    63
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6247
diff changeset
    64
			/* Read riff tags */
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    65
			for (;;) {
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    66
				uint32 tag = FioReadDword();
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    67
				uint32 size = FioReadDword();
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    68
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    69
				if (tag == ' tmf') {
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    70
					FioReadWord(); // wFormatTag
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    71
					fe->channels = FioReadWord(); // wChannels
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    72
					FioReadDword();   // samples per second
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    73
					fe->rate = 11025; // seems like all samples should be played at this rate.
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    74
					FioReadDword();   // avg bytes per second
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    75
					FioReadWord();    // alignment
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    76
					fe->bits_per_sample = FioReadByte(); // bits per sample
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    77
					FioSeekTo(size - (2 + 2 + 4 + 4 + 2 + 1), SEEK_CUR);
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    78
				} else if (tag == 'atad') {
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    79
					fe->file_size = size;
7570
5d5d9b6af0ef (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 6891
diff changeset
    80
					fe->file_slot = SOUND_SLOT;
5d5d9b6af0ef (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 6891
diff changeset
    81
					fe->file_offset = FioGetPos();
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    82
					break;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    83
				} else {
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    84
					fe->file_size = 0;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    85
					break;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    86
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
			}
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    88
		} else {
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    89
			/*
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    90
			 * Special case for the jackhammer sound
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    91
			 * (name in sample.cat is "Corrupt sound")
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    92
			 * It's no RIFF file, but raw PCM data
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    93
			 */
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    94
			fe->channels = 1;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    95
			fe->rate = 11025;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    96
			fe->bits_per_sample = 8;
7570
5d5d9b6af0ef (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 6891
diff changeset
    97
			fe->file_slot = SOUND_SLOT;
5d5d9b6af0ef (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 6891
diff changeset
    98
			fe->file_offset = FioGetPos();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   103
uint GetNumOriginalSounds()
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   104
{
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   105
	return _file_count;
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   106
}
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   107
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   108
static bool SetBankSource(MixerChannel *mc, uint bank)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
{
4171
5c6e60c392c3 (svn r5609) CodeChange : Apply coding style
belugas
parents: 3898
diff changeset
   110
	const FileEntry *fe;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
	uint i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   113
	if (bank >= GetNumSounds()) return false;
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   114
	fe = GetSound(bank);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   115
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   116
	if (fe->file_size == 0) return false;
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   117
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
   118
	int8 *mem = MallocT<int8>(fe->file_size);
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   119
	if (mem == NULL) return false;
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   120
7570
5d5d9b6af0ef (svn r11095) -Codechange: don't abuse 'file_pos' by storing the file_slot in it too, but use a nice seperate variable for it
truelight
parents: 6891
diff changeset
   121
	FioSeekToFile(fe->file_slot, fe->file_offset);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
	FioReadBlock(mem, fe->file_size);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   124
	for (i = 0; i != fe->file_size; i++)
1136
186d65f60c82 (svn r1637) -Fix: VS6 warning
darkvater
parents: 926
diff changeset
   125
		mem[i] += -128; // Convert unsigned sound data to signed
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   126
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
	assert(fe->bits_per_sample == 8 && fe->channels == 1 && fe->file_size != 0 && fe->rate != 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   129
	MxSetChannelRawSrc(mc, mem, fe->file_size, fe->rate, MX_AUTOFREE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   134
bool SoundInitialize(const char *filename)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
{
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   136
	OpenBankFile(filename);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
6420
456c275f3313 (svn r9556) -Documentation: doxygen and comment-style changes. 'R', 'S'.. The end of the preliminary work is near
belugas
parents: 6247
diff changeset
   140
/* Low level sound player */
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   141
static void StartSound(uint sound, int panning, uint volume)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
{
4171
5c6e60c392c3 (svn r5609) CodeChange : Apply coding style
belugas
parents: 3898
diff changeset
   143
	MixerChannel *mc;
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   144
	uint left_vol, right_vol;
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   145
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   146
	if (volume == 0) return;
2977
97c1c76b499a (svn r3552) Remove the global variable _mixer
tron
parents: 2594
diff changeset
   147
	mc = MxAllocateChannel();
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   148
	if (mc == NULL) return;
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   149
	if (!SetBankSource(mc, sound)) return;
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   150
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7805
diff changeset
   151
	panning = Clamp(panning, -PANNING_LEVELS, PANNING_LEVELS);
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   152
	left_vol = (volume * PANNING_LEVELS) - (volume * panning);
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   153
	right_vol = (volume * PANNING_LEVELS) + (volume * panning);
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   154
	MxSetChannelVolume(mc, left_vol * 128 / PANNING_LEVELS, right_vol * 128 / PANNING_LEVELS);
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   155
	MxActivateChannel(mc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
6853
011888c570df (svn r10093) -Revert: removed 16x zoom-out as it is broken beyond repair
truelight
parents: 6654
diff changeset
   159
static const byte _vol_factor_by_zoom[] = {255, 190, 134, 87};
8095
f834186120af (svn r11656) -Codechange: add ZOOM_LVL_BEGIN and postfix operators so ZoomLevel can be used in some iterations
smatz
parents: 7954
diff changeset
   160
assert_compile(lengthof(_vol_factor_by_zoom) == ZOOM_LVL_END - ZOOM_LVL_BEGIN);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
static const byte _sound_base_vol[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
	128,  90, 128, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
	128,  90,  90, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
	128, 128, 128,  80, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
	128, 128, 128, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
	128, 128,  90,  90,  90, 128,  90, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
	128,  90, 128, 128, 128,  90, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
	128, 128, 128, 128,  90, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
	128,  90, 128, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
	128, 128,  90,  90,  90, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
	 90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
static const byte _sound_idx[] = {
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   176
	 2,  3,  4,  5,  6,  7,  8,  9,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
	10, 11, 12, 13, 14, 15, 16, 17,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
	18, 19, 20, 21, 22, 23, 24, 25,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
	26, 27, 28, 29, 30, 31, 32, 33,
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   180
	34, 35, 36, 37, 38, 39, 40,  0,
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   181
	 1, 41, 42, 43, 44, 45, 46, 47,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
	48, 49, 50, 51, 52, 53, 54, 55,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
	56, 57, 58, 59, 60, 61, 62, 63,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   184
	64, 65, 66, 67, 68, 69, 70, 71,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
	72,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   188
void SndCopyToPool()
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   189
{
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   190
	uint i;
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   191
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   192
	for (i = 0; i < _file_count; i++) {
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   193
		FileEntry *orig = &_files[_sound_idx[i]];
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   194
		FileEntry *fe = AllocateFileEntry();
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   195
5024
5e98bf731469 (svn r7065) Use simple assignment instead of memcpy()
tron
parents: 4656
diff changeset
   196
		*fe = *orig;
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   197
		fe->volume = _sound_base_vol[i];
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   198
		fe->priority = 0;
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   199
	}
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   200
}
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   201
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   202
static void SndPlayScreenCoordFx(SoundFx sound, int x, int y)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
{
5137
54425dc8b5b3 (svn r7219) -Fix: Several warnings by gcc introduced in r7206 which MSVC found not of a problem. Thanks Tron
Darkvater
parents: 5124
diff changeset
   204
	Window* const *wz;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   206
	if (msf.effect_vol == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
5124
2ca62776430e (svn r7205) -Fix [FS#350, SF#1560913]: Window allocation and deletion messed with the actual window
Darkvater
parents: 5024
diff changeset
   208
	FOR_ALL_WINDOWS(wz) {
2ca62776430e (svn r7205) -Fix [FS#350, SF#1560913]: Window allocation and deletion messed with the actual window
Darkvater
parents: 5024
diff changeset
   209
		const ViewPort *vp = (*wz)->viewport;
2371
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   210
a8344121fa3c (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   211
		if (vp != NULL &&
7954
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7922
diff changeset
   212
				IsInsideBS(x, vp->virtual_left, vp->virtual_width) &&
57b51c69c072 (svn r11510) -Codechange: merge the IS_*INSIDE* functions and rename them fitting to the naming style
skidd13
parents: 7922
diff changeset
   213
				IsInsideBS(y, vp->virtual_top, vp->virtual_height)) {
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   214
			int left = (x - vp->virtual_left);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   215
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   216
			StartSound(
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   217
				sound,
6891
824359de2f5e (svn r10138) -Fix: when you got a sufficiently small resolution, there is a possibility for a division by zero when a sound is played.
rubidium
parents: 6853
diff changeset
   218
				left / max(1, vp->virtual_width / ((PANNING_LEVELS << 1) + 1)) - PANNING_LEVELS,
8095
f834186120af (svn r11656) -Codechange: add ZOOM_LVL_BEGIN and postfix operators so ZoomLevel can be used in some iterations
smatz
parents: 7954
diff changeset
   219
				(GetSound(sound)->volume * msf.effect_vol * _vol_factor_by_zoom[vp->zoom - ZOOM_LVL_BEGIN]) >> 15
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
			);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   227
void SndPlayTileFx(SoundFx sound, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   228
{
3645
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3421
diff changeset
   229
	/* emits sound from center of the tile */
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3421
diff changeset
   230
	int x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3421
diff changeset
   231
	int y = TileY(tile) * TILE_SIZE + TILE_SIZE / 2;
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   232
	Point pt = RemapCoords(x, y, GetSlopeZ(x, y));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
	SndPlayScreenCoordFx(sound, pt.x, pt.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   236
void SndPlayVehicleFx(SoundFx sound, const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
	SndPlayScreenCoordFx(sound,
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   239
		(v->left_coord + v->right_coord) / 2,
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   240
		(v->top_coord + v->bottom_coord) / 2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
	);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   244
void SndPlayFx(SoundFx sound)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
	StartSound(
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   247
		sound,
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   248
		0,
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   249
		(GetSound(sound)->volume * msf.effect_vol) >> 7
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
	);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   251
}