src/sound.cpp
author belugas
Wed, 16 Jan 2008 02:53:55 +0000
changeset 8308 bc75a4a27ec9
parent 8224 c5a64d87cc54
child 8439 6913310b9a18
permissions -rw-r--r--
(svn r11873) -Codechange: less a few magical numbers and a tiny bit more comments on town zones
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"
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
     7
#include "landscape.h"
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
     8
#include "mixer.h"
8157
1505def01945 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 8144
diff changeset
     9
#include "sound_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#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
    11
#include "newgrf_sound.h"
7805
7ab20f94cc80 (svn r11355) -Fix [FS#1377]: loading too many GRFs was not handled gracefully causing crashes and such.
rubidium
parents: 7591
diff changeset
    12
#include "fios.h"
8106
e6790dd9e750 (svn r11667) -Codechange: split window.h into a header that defines some 'global' window related types, on that defined 'global' window functions and one that defines functions and types only used by *_gui.cpps.
rubidium
parents: 8095
diff changeset
    13
#include "window_gui.h"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 8106
diff changeset
    14
#include "core/alloc_func.hpp"
8139
4e91c448c409 (svn r11701) -Codechange: removal unnecessary inclusions of map.h (and split map.h).
rubidium
parents: 8131
diff changeset
    15
#include "map_func.h"
8144
65cec0877b78 (svn r11706) -Codechange: split vehicle.h and remove another bunch of useless includes.
rubidium
parents: 8139
diff changeset
    16
#include "vehicle_base.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    18
static uint _file_count;
4171
5c6e60c392c3 (svn r5609) CodeChange : Apply coding style
belugas
parents: 3898
diff changeset
    19
static FileEntry *_files;
8157
1505def01945 (svn r11719) -Codechange: split sound.h in a header with types and one with functions.
rubidium
parents: 8144
diff changeset
    20
MusicFileSettings msf;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
    22
// Number of levels of panning per side
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
    23
#define PANNING_LEVELS 16
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    26
static void OpenBankFile(const char *filename)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
{
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
    28
	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
    29
	uint i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
	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
    32
	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
    33
	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
    34
	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
    35
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
	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
    37
		_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
    38
		_files = NULL;
2372
152ed96759ba (svn r2898) Fix typo in r2897
tron
parents: 2371
diff changeset
    39
		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
    40
	}
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
	_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
    43
	_files = fe;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
7591
06202df3799e (svn r11118) -Fix: sample.cat can now be in a tar-file too, how nice!
truelight
parents: 7570
diff changeset
    45
	FioSeekTo(pos, SEEK_SET);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    47
	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
    48
		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
    49
		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
    50
		fe[i].file_size = FioReadDword();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
    52
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    53
	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
    54
		char name[255];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    56
		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
    57
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
    58
		/* 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
    59
		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
    60
		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
    61
			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
    62
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
    63
			/* 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
    64
			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
    65
				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
    66
				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
    67
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    68
				if (tag == ' tmf') {
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    69
					FioReadWord(); // wFormatTag
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    70
					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
    71
					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
    72
					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
    73
					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
    74
					FioReadWord();    // alignment
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    75
					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
    76
					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
    77
				} 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
    78
					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
    79
					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
    80
					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
    81
					break;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    82
				} else {
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    83
					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
    84
					break;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    85
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
			}
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    87
		} else {
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    88
			/*
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    89
			 * 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
    90
			 * (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
    91
			 * 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
    92
			 */
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    93
			fe->channels = 1;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    94
			fe->rate = 11025;
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    95
			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
    96
			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
    97
			fe->file_offset = FioGetPos();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
		}
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
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   102
uint GetNumOriginalSounds()
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   103
{
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   104
	return _file_count;
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   105
}
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   106
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   107
static bool SetBankSource(MixerChannel *mc, uint bank)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
{
4171
5c6e60c392c3 (svn r5609) CodeChange : Apply coding style
belugas
parents: 3898
diff changeset
   109
	const FileEntry *fe;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
	uint i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   112
	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
   113
	fe = GetSound(bank);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   114
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
   115
	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
   116
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
   117
	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
   118
	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
   119
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
   120
	FioSeekToFile(fe->file_slot, fe->file_offset);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
	FioReadBlock(mem, fe->file_size);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   123
	for (i = 0; i != fe->file_size; i++)
1136
186d65f60c82 (svn r1637) -Fix: VS6 warning
darkvater
parents: 926
diff changeset
   124
		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
   125
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
	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
   127
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   128
	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
   129
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   133
bool SoundInitialize(const char *filename)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
{
1496
15d859a626e8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   135
	OpenBankFile(filename);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
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
   139
/* Low level sound player */
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   140
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
   141
{
4171
5c6e60c392c3 (svn r5609) CodeChange : Apply coding style
belugas
parents: 3898
diff changeset
   142
	MixerChannel *mc;
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   143
	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
   144
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
	if (volume == 0) return;
2977
97c1c76b499a (svn r3552) Remove the global variable _mixer
tron
parents: 2594
diff changeset
   146
	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
   147
	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
   148
	if (!SetBankSource(mc, sound)) return;
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   149
7922
a7e266f966d9 (svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents: 7805
diff changeset
   150
	panning = Clamp(panning, -PANNING_LEVELS, PANNING_LEVELS);
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   151
	left_vol = (volume * PANNING_LEVELS) - (volume * panning);
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   152
	right_vol = (volume * PANNING_LEVELS) + (volume * panning);
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   153
	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
   154
	MxActivateChannel(mc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
}
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
6853
011888c570df (svn r10093) -Revert: removed 16x zoom-out as it is broken beyond repair
truelight
parents: 6654
diff changeset
   158
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
   159
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
   160
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
static const byte _sound_base_vol[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
	128,  90, 128, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
	128,  90,  90, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
	128, 128, 128,  80, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
	128, 128, 128, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
	128, 128,  90,  90,  90, 128,  90, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
	128,  90, 128, 128, 128,  90, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
	128, 128, 128, 128,  90, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
	128,  90, 128, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
	128, 128,  90,  90,  90, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
	 90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
};
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
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
   175
	 2,  3,  4,  5,  6,  7,  8,  9,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
	10, 11, 12, 13, 14, 15, 16, 17,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
	18, 19, 20, 21, 22, 23, 24, 25,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
	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
   179
	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
   180
	 1, 41, 42, 43, 44, 45, 46, 47,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
	48, 49, 50, 51, 52, 53, 54, 55,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
	56, 57, 58, 59, 60, 61, 62, 63,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
	64, 65, 66, 67, 68, 69, 70, 71,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   184
	72,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5609
diff changeset
   187
void SndCopyToPool()
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   188
{
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   189
	uint i;
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   190
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   191
	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
   192
		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
   193
		FileEntry *fe = AllocateFileEntry();
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   194
5024
5e98bf731469 (svn r7065) Use simple assignment instead of memcpy()
tron
parents: 4656
diff changeset
   195
		*fe = *orig;
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   196
		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
   197
		fe->priority = 0;
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   198
	}
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
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   201
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
   202
{
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
   203
	Window* const *wz;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
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
   205
	if (msf.effect_vol == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
5124
2ca62776430e (svn r7205) -Fix [FS#350, SF#1560913]: Window allocation and deletion messed with the actual window
Darkvater
parents: 5024
diff changeset
   207
	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
   208
		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
   209
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
		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
   211
				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
   212
				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
   213
			int left = (x - vp->virtual_left);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   214
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
			StartSound(
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   216
				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
   217
				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
   218
				(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
   219
			);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
		}
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
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   226
void SndPlayTileFx(SoundFx sound, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
{
3645
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3421
diff changeset
   228
	/* emits sound from center of the tile */
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3421
diff changeset
   229
	int x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2;
7f950533d510 (svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents: 3421
diff changeset
   230
	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
   231
	Point pt = RemapCoords(x, y, GetSlopeZ(x, y));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
	SndPlayScreenCoordFx(sound, pt.x, pt.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   235
void SndPlayVehicleFx(SoundFx sound, const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
	SndPlayScreenCoordFx(sound,
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   238
		(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
   239
		(v->top_coord + v->bottom_coord) / 2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
	);
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
337
cbe0c766c947 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   243
void SndPlayFx(SoundFx sound)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
	StartSound(
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   246
		sound,
2594
0e76c769f002 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   247
		0,
4656
9c1d8c4d3e60 (svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents: 4171
diff changeset
   248
		(GetSound(sound)->volume * msf.effect_vol) >> 7
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
	);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
}