sound.c
author tron
Fri, 04 Nov 2005 16:12:48 +0000
changeset 2598 574b5774149c
parent 2594 c65db1a9f630
child 2977 0240dd4704a7
permissions -rw-r--r--
(svn r3135) Use symbolic names in the tables, which hold the information about accepted goods of industry tiles, instead of magic numbers
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1498
diff changeset
     4
#include "openttd.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 1891
diff changeset
     5
#include "functions.h"
679
e959706a3e4d (svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents: 337
diff changeset
     6
#include "map.h"
1496
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
     7
#include "mixer.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     8
#include "sound.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     9
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "window.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "viewport.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "fileio.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    14
typedef struct FileEntry {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
	uint32 file_offset;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
	uint32 file_size;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
	uint16 rate;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
	uint8 bits_per_sample;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
	uint8 channels;
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    20
} FileEntry;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
1496
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    22
static uint _file_count;
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    23
static FileEntry* _files;
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
#define SOUND_SLOT 31
2594
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
    26
// Number of levels of panning per side
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
    27
#define PANNING_LEVELS 16
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
1496
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    30
static void OpenBankFile(const char *filename)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
{
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    32
	FileEntry* fe;
fafe059a4472 (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
	uint count;
fafe059a4472 (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
	uint i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
	FioOpenFile(SOUND_SLOT, filename);
2371
fafe059a4472 (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
	count = FioReadDword() / 8;
fafe059a4472 (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
	fe = calloc(sizeof(*fe), count);
fafe059a4472 (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
fafe059a4472 (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
	if (fe == NULL) {
fafe059a4472 (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
		_file_count = 0;
fafe059a4472 (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
		_files = NULL;
2372
f751e366f618 (svn r2898) Fix typo in r2897
tron
parents: 2371
diff changeset
    43
		return;
2371
fafe059a4472 (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
	}
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    45
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    46
	_file_count = count;
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    47
	_files = fe;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
	FioSeekTo(0, SEEK_SET);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
1496
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    51
	for (i = 0; i != count; i++) {
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    52
		fe[i].file_offset = FioReadDword();
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
    53
		fe[i].file_size = FioReadDword();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
    55
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    56
	for (i = 0; i != count; i++, fe++) {
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    57
		char name[255];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    59
		FioSeekTo(fe->file_offset, SEEK_SET);
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    60
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    61
		// Check for special case, see else case
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    62
		FioReadBlock(name, FioReadByte()); // Read the name of the sound
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    63
		if (strcmp(name, "Corrupt sound") != 0) {
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    64
			FioSeekTo(12, SEEK_CUR); // Skip past RIFF header
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    65
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    66
			// Read riff tags
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    67
			for (;;) {
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    68
				uint32 tag = FioReadDword();
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
    69
				uint32 size = FioReadDword();
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    70
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    71
				if (tag == ' tmf') {
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    72
					FioReadWord(); // wFormatTag
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    73
					fe->channels = FioReadWord(); // wChannels
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    74
					FioReadDword();   // samples per second
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    75
					fe->rate = 11025; // seems like all samples should be played at this rate.
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    76
					FioReadDword();   // avg bytes per second
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    77
					FioReadWord();    // alignment
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    78
					fe->bits_per_sample = FioReadByte(); // bits per sample
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    79
					FioSeekTo(size - (2 + 2 + 4 + 4 + 2 + 1), SEEK_CUR);
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    80
				} else if (tag == 'atad') {
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    81
					fe->file_size = size;
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    82
					fe->file_offset = FioGetPos() | (SOUND_SLOT << 24);
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    83
					break;
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    84
				} else {
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    85
					fe->file_size = 0;
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    86
					break;
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    87
				}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
			}
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    89
		} else {
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    90
			/*
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    91
			 * Special case for the jackhammer sound
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    92
			 * (name in sample.cat is "Corrupt sound")
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    93
			 * It's no RIFF file, but raw PCM data
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    94
			 */
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    95
			fe->channels = 1;
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    96
			fe->rate = 11025;
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    97
			fe->bits_per_sample = 8;
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
    98
			fe->file_offset = FioGetPos() | (SOUND_SLOT << 24);
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
1496
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   103
static bool SetBankSource(MixerChannel *mc, uint bank)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
{
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   105
	FileEntry* fe;
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   106
	int8* mem;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
	uint i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   109
	if (bank >= _file_count) return false;
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   110
	fe = &_files[bank];
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   111
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   112
	if (fe->file_size == 0) return false;
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   113
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   114
	mem = malloc(fe->file_size);
fafe059a4472 (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 (mem == NULL) return false;
fafe059a4472 (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
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
	FioSeekToFile(fe->file_offset);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   118
	FioReadBlock(mem, fe->file_size);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   120
	for (i = 0; i != fe->file_size; i++)
1136
052a24d24813 (svn r1637) -Fix: VS6 warning
darkvater
parents: 926
diff changeset
   121
		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
   122
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
	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
   124
1496
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   125
	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
   126
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
1496
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   130
bool SoundInitialize(const char *filename)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
{
1496
3d0b86f5dcb8 (svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents: 1136
diff changeset
   132
	OpenBankFile(filename);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
	return true;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
// Low level sound player
2594
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   137
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
   138
{
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   139
	MixerChannel* mc;
2594
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   140
	uint left_vol, right_vol;
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   141
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   142
	if (volume == 0) return;
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   143
	mc = MxAllocateChannel(_mixer);
fafe059a4472 (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
	if (mc == NULL) return;
fafe059a4472 (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 (!SetBankSource(mc, sound)) return;
2594
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   146
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   147
	panning = clamp(panning, -PANNING_LEVELS, PANNING_LEVELS);
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   148
	left_vol = (volume * PANNING_LEVELS) - (volume * panning);
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   149
	right_vol = (volume * PANNING_LEVELS) + (volume * panning);
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   150
	MxSetChannelVolume(mc, left_vol * 128 / PANNING_LEVELS, right_vol * 128 / PANNING_LEVELS);
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   151
	MxActivateChannel(mc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
static const byte _vol_factor_by_zoom[] = {255, 190, 134};
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
static const byte _sound_base_vol[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
	128,  90, 128, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
	128,  90,  90, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
	128, 128, 128,  80, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
	128, 128, 128, 128, 128, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
	128, 128,  90,  90,  90, 128,  90, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
	128,  90, 128, 128, 128,  90, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
	128, 128, 128, 128,  90, 128, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
	128,  90, 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, 128, 128,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
	 90,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
static const byte _sound_idx[] = {
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   171
	 2,  3,  4,  5,  6,  7,  8,  9,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   172
	10, 11, 12, 13, 14, 15, 16, 17,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   173
	18, 19, 20, 21, 22, 23, 24, 25,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
	26, 27, 28, 29, 30, 31, 32, 33,
2371
fafe059a4472 (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
	34, 35, 36, 37, 38, 39, 40,  0,
fafe059a4472 (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
	 1, 41, 42, 43, 44, 45, 46, 47,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
	48, 49, 50, 51, 52, 53, 54, 55,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
	56, 57, 58, 59, 60, 61, 62, 63,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
	64, 65, 66, 67, 68, 69, 70, 71,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
	72,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   183
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
   184
{
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   185
	const Window* w;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   187
	if (msf.effect_vol == 0) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   189
	for (w = _windows; w != _last_window; w++) {
2371
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   190
		const ViewPort* vp = w->viewport;
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   191
fafe059a4472 (svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes
tron
parents: 2186
diff changeset
   192
		if (vp != NULL &&
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
				IS_INSIDE_1D(x, vp->virtual_left, vp->virtual_width) &&
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   194
				IS_INSIDE_1D(y, vp->virtual_top, vp->virtual_height)) {
2594
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   195
			int left = (x - vp->virtual_left);
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   196
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
			StartSound(
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   198
				_sound_idx[sound],
2594
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   199
				left / (vp->virtual_width / ((PANNING_LEVELS << 1) + 1)) - PANNING_LEVELS,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
				(_sound_base_vol[sound] * msf.effect_vol * _vol_factor_by_zoom[vp->zoom]) >> 15
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
			);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
			return;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   208
void SndPlayTileFx(SoundFx sound, TileIndex tile)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
{
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   210
	/* emits sound from center (+ 8) of the tile */
926
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
   211
	int x = TileX(tile) * 16 + 8;
bd4312619522 (svn r1414) Move TileIndex, TILE_MASK and GET_TILE_[XY] to map.h and turn the latter into inline functions names Tile[XY]
tron
parents: 679
diff changeset
   212
	int y = TileY(tile) * 16 + 8;
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   213
	Point pt = RemapCoords(x, y, GetSlopeZ(x, y));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
	SndPlayScreenCoordFx(sound, pt.x, pt.y);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   216
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   217
void SndPlayVehicleFx(SoundFx sound, const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
	SndPlayScreenCoordFx(sound,
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   220
		(v->left_coord + v->right_coord) / 2,
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   221
		(v->top_coord + v->bottom_coord) / 2
0
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
337
66647f97e7c0 (svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents: 193
diff changeset
   225
void SndPlayFx(SoundFx sound)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
	StartSound(
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 0
diff changeset
   228
		_sound_idx[sound],
2594
c65db1a9f630 (svn r3131) Enable panning of audio relative to screen position.
peter1138
parents: 2372
diff changeset
   229
		0,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   230
		(_sound_base_vol[sound] * msf.effect_vol) >> 7
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
	);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
}