author | smatz |
Tue, 17 Jun 2008 17:25:22 +0000 | |
changeset 9542 | bc9789270025 |
parent 9390 | 88d36f907e96 |
child 10039 | 1f236afd6cd1 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
9111
48ce04029fe4
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents:
8992
diff
changeset
|
3 |
/** @file sound.cpp Handling of playing sounds. */ |
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
|
4 |
|
0 | 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 | 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" |
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
17 |
#include "debug.h" |
0 | 18 |
|
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
19 |
static uint _file_count; |
4171 | 20 |
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
|
21 |
MusicFileSettings msf; |
0 | 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 | 25 |
|
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 | 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 i; |
0 | 30 |
|
31 |
FioOpenFile(SOUND_SLOT, filename); |
|
9390
88d36f907e96
(svn r13301) -Fix [FS#1997]: resolve more MSVC 9 x64 warnings.
rubidium
parents:
9111
diff
changeset
|
32 |
size_t pos = FioGetPos(); |
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
33 |
uint count = FioReadDword() / 8; |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
34 |
|
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
35 |
/* Simple check for the correct number of original sounds. */ |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
36 |
if (count != 73) { |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
37 |
DEBUG(misc, 6, "Incorrect number of sounds in '%s', ignoring.", filename); |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
38 |
_file_count = 0; |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
39 |
_files = NULL; |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
40 |
return; |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
41 |
} |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
42 |
|
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
|
43 |
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
|
44 |
|
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
|
45 |
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
|
46 |
_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
|
47 |
_files = NULL; |
2372 | 48 |
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
|
49 |
} |
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
|
50 |
|
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
|
51 |
_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
|
52 |
_files = fe; |
0 | 53 |
|
7591
06202df3799e
(svn r11118) -Fix: sample.cat can now be in a tar-file too, how nice!
truelight
parents:
7570
diff
changeset
|
54 |
FioSeekTo(pos, SEEK_SET); |
0 | 55 |
|
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
fe[i].file_size = FioReadDword(); |
0 | 60 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
61 |
|
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
62 |
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
|
63 |
char name[255]; |
0 | 64 |
|
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
65 |
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
|
66 |
|
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
|
67 |
/* 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
|
68 |
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
|
69 |
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
|
70 |
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
|
71 |
|
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
|
72 |
/* 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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
|
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
77 |
if (tag == ' tmf') { |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
78 |
FioReadWord(); // wFormatTag |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
79 |
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
|
80 |
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
|
81 |
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
|
82 |
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
|
83 |
FioReadWord(); // alignment |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
84 |
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
|
85 |
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
|
86 |
} 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
|
87 |
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
|
88 |
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
|
89 |
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
|
90 |
break; |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
91 |
} else { |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
92 |
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
|
93 |
break; |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
94 |
} |
0 | 95 |
} |
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
96 |
} else { |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
97 |
/* |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
98 |
* 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
|
99 |
* (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
|
100 |
* 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
|
101 |
*/ |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
102 |
fe->channels = 1; |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
103 |
fe->rate = 11025; |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
104 |
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
|
105 |
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
|
106 |
fe->file_offset = FioGetPos(); |
0 | 107 |
} |
108 |
} |
|
109 |
} |
|
110 |
||
6247 | 111 |
uint GetNumOriginalSounds() |
4656
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
112 |
{ |
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
113 |
return _file_count; |
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
114 |
} |
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
115 |
|
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
116 |
static bool SetBankSource(MixerChannel *mc, const FileEntry *fe) |
0 | 117 |
{ |
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
118 |
assert(fe != NULL); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
119 |
|
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
|
120 |
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
|
121 |
|
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
|
122 |
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
|
123 |
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
|
124 |
|
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
|
125 |
FioSeekToFile(fe->file_slot, fe->file_offset); |
0 | 126 |
FioReadBlock(mem, fe->file_size); |
127 |
||
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
128 |
for (uint i = 0; i != fe->file_size; i++) { |
1136 | 129 |
mem[i] += -128; // Convert unsigned sound data to signed |
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
130 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
131 |
|
0 | 132 |
assert(fe->bits_per_sample == 8 && fe->channels == 1 && fe->file_size != 0 && fe->rate != 0); |
133 |
||
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
134 |
MxSetChannelRawSrc(mc, mem, fe->file_size, fe->rate, MX_AUTOFREE); |
0 | 135 |
|
136 |
return true; |
|
137 |
} |
|
138 |
||
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
139 |
bool SoundInitialize(const char *filename) |
0 | 140 |
{ |
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
141 |
OpenBankFile(filename); |
0 | 142 |
return true; |
143 |
} |
|
144 |
||
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
|
145 |
/* Low level sound player */ |
2594
0e76c769f002
(svn r3131) Enable panning of audio relative to screen position.
peter1138
parents:
2372
diff
changeset
|
146 |
static void StartSound(uint sound, int panning, uint volume) |
0 | 147 |
{ |
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
148 |
if (volume == 0) 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
|
149 |
|
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
150 |
const FileEntry *fe = GetSound(sound); |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
151 |
if (fe == NULL) return; |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
152 |
|
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
153 |
MixerChannel *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
|
154 |
if (mc == NULL) return; |
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
155 |
|
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
156 |
if (!SetBankSource(mc, fe)) return; |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
157 |
|
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
158 |
/* Apply the sound effect's own volume. */ |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
159 |
volume = (fe->volume * volume) / 128; |
2594
0e76c769f002
(svn r3131) Enable panning of audio relative to screen position.
peter1138
parents:
2372
diff
changeset
|
160 |
|
7922
a7e266f966d9
(svn r11475) -Codechange: rename clamp and clampu to Clamp and ClampU to fit with the coding style
skidd13
parents:
7805
diff
changeset
|
161 |
panning = Clamp(panning, -PANNING_LEVELS, PANNING_LEVELS); |
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
162 |
uint left_vol = (volume * PANNING_LEVELS) - (volume * panning); |
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
163 |
uint right_vol = (volume * PANNING_LEVELS) + (volume * panning); |
2594
0e76c769f002
(svn r3131) Enable panning of audio relative to screen position.
peter1138
parents:
2372
diff
changeset
|
164 |
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
|
165 |
MxActivateChannel(mc); |
0 | 166 |
} |
167 |
||
168 |
||
6853
011888c570df
(svn r10093) -Revert: removed 16x zoom-out as it is broken beyond repair
truelight
parents:
6654
diff
changeset
|
169 |
static const byte _vol_factor_by_zoom[] = {255, 190, 134, 87}; |
9542
bc9789270025
(svn r13551) -Codechange: store offsets to different zoom levels in a distinguished struct instead in the data stream for 8bpp-optimized
smatz
parents:
9390
diff
changeset
|
170 |
assert_compile(lengthof(_vol_factor_by_zoom) == ZOOM_LVL_COUNT); |
0 | 171 |
|
172 |
static const byte _sound_base_vol[] = { |
|
173 |
128, 90, 128, 128, 128, 128, 128, 128, |
|
174 |
128, 90, 90, 128, 128, 128, 128, 128, |
|
175 |
128, 128, 128, 80, 128, 128, 128, 128, |
|
176 |
128, 128, 128, 128, 128, 128, 128, 128, |
|
177 |
128, 128, 90, 90, 90, 128, 90, 128, |
|
178 |
128, 90, 128, 128, 128, 90, 128, 128, |
|
179 |
128, 128, 128, 128, 90, 128, 128, 128, |
|
180 |
128, 90, 128, 128, 128, 128, 128, 128, |
|
181 |
128, 128, 90, 90, 90, 128, 128, 128, |
|
182 |
90, |
|
183 |
}; |
|
184 |
||
185 |
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
|
186 |
2, 3, 4, 5, 6, 7, 8, 9, |
0 | 187 |
10, 11, 12, 13, 14, 15, 16, 17, |
188 |
18, 19, 20, 21, 22, 23, 24, 25, |
|
189 |
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
|
190 |
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
|
191 |
1, 41, 42, 43, 44, 45, 46, 47, |
0 | 192 |
48, 49, 50, 51, 52, 53, 54, 55, |
193 |
56, 57, 58, 59, 60, 61, 62, 63, |
|
194 |
64, 65, 66, 67, 68, 69, 70, 71, |
|
195 |
72, |
|
196 |
}; |
|
197 |
||
6247 | 198 |
void SndCopyToPool() |
4656
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
199 |
{ |
8857
7593af4962e6
(svn r12619) -Codechange: lots of documentation about window related stuff. Patch by Alberth.
rubidium
parents:
8604
diff
changeset
|
200 |
for (uint i = 0; i < _file_count; i++) { |
4656
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
201 |
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
|
202 |
FileEntry *fe = AllocateFileEntry(); |
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
203 |
|
5024
5e98bf731469
(svn r7065) Use simple assignment instead of memcpy()
tron
parents:
4656
diff
changeset
|
204 |
*fe = *orig; |
4656
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
205 |
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
|
206 |
fe->priority = 0; |
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
207 |
} |
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
208 |
} |
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
209 |
|
8992
0b2fb43cc875
(svn r12787) -Documentation: add/fix documentation of some functions. Patch by Alberth.
rubidium
parents:
8857
diff
changeset
|
210 |
/** |
0b2fb43cc875
(svn r12787) -Documentation: add/fix documentation of some functions. Patch by Alberth.
rubidium
parents:
8857
diff
changeset
|
211 |
* Decide 'where' (between left and right speaker) to play the sound effect. |
0b2fb43cc875
(svn r12787) -Documentation: add/fix documentation of some functions. Patch by Alberth.
rubidium
parents:
8857
diff
changeset
|
212 |
* @param sound Sound effect to play |
0b2fb43cc875
(svn r12787) -Documentation: add/fix documentation of some functions. Patch by Alberth.
rubidium
parents:
8857
diff
changeset
|
213 |
* @param left Left edge of virtual coordinates where the sound is produced |
0b2fb43cc875
(svn r12787) -Documentation: add/fix documentation of some functions. Patch by Alberth.
rubidium
parents:
8857
diff
changeset
|
214 |
* @param right Right edge of virtual coordinates where the sound is produced |
0b2fb43cc875
(svn r12787) -Documentation: add/fix documentation of some functions. Patch by Alberth.
rubidium
parents:
8857
diff
changeset
|
215 |
* @param top Top edge of virtual coordinates where the sound is produced |
0b2fb43cc875
(svn r12787) -Documentation: add/fix documentation of some functions. Patch by Alberth.
rubidium
parents:
8857
diff
changeset
|
216 |
* @param bottom Bottom edge of virtual coordinates where the sound is produced |
0b2fb43cc875
(svn r12787) -Documentation: add/fix documentation of some functions. Patch by Alberth.
rubidium
parents:
8857
diff
changeset
|
217 |
*/ |
8588
2674b9a4be81
(svn r12169) -Change [FS#1696]: play sounds when there is only small part of tile/vehicle visible too (original idea by Dominik)
smatz
parents:
8439
diff
changeset
|
218 |
static void SndPlayScreenCoordFx(SoundFx sound, int left, int right, int top, int bottom) |
0 | 219 |
{ |
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
|
220 |
Window* const *wz; |
0 | 221 |
|
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
|
222 |
if (msf.effect_vol == 0) return; |
0 | 223 |
|
5124
2ca62776430e
(svn r7205) -Fix [FS#350, SF#1560913]: Window allocation and deletion messed with the actual window
Darkvater
parents:
5024
diff
changeset
|
224 |
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
|
225 |
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
|
226 |
|
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
|
227 |
if (vp != NULL && |
8588
2674b9a4be81
(svn r12169) -Change [FS#1696]: play sounds when there is only small part of tile/vehicle visible too (original idea by Dominik)
smatz
parents:
8439
diff
changeset
|
228 |
left < vp->virtual_left + vp->virtual_width && right > vp->virtual_left && |
2674b9a4be81
(svn r12169) -Change [FS#1696]: play sounds when there is only small part of tile/vehicle visible too (original idea by Dominik)
smatz
parents:
8439
diff
changeset
|
229 |
top < vp->virtual_top + vp->virtual_height && bottom > vp->virtual_top) { |
2674b9a4be81
(svn r12169) -Change [FS#1696]: play sounds when there is only small part of tile/vehicle visible too (original idea by Dominik)
smatz
parents:
8439
diff
changeset
|
230 |
int screen_x = (left + right) / 2 - vp->virtual_left; |
8595
718ce3c308fb
(svn r12176) -Codechange: one division less when playing sounds (patch by Dominik)
smatz
parents:
8588
diff
changeset
|
231 |
int width = (vp->virtual_width == 0 ? 1 : vp->virtual_width); |
718ce3c308fb
(svn r12176) -Codechange: one division less when playing sounds (patch by Dominik)
smatz
parents:
8588
diff
changeset
|
232 |
int panning = (screen_x * PANNING_LEVELS * 2) / width - PANNING_LEVELS; |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
233 |
|
0 | 234 |
StartSound( |
4656
9c1d8c4d3e60
(svn r6532) - Feature: Add support for NewGRF sound effects. Currently sound priority isn't supported.
peter1138
parents:
4171
diff
changeset
|
235 |
sound, |
8595
718ce3c308fb
(svn r12176) -Codechange: one division less when playing sounds (patch by Dominik)
smatz
parents:
8588
diff
changeset
|
236 |
panning, |
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
237 |
(msf.effect_vol * _vol_factor_by_zoom[vp->zoom - ZOOM_LVL_BEGIN]) / 256 |
0 | 238 |
); |
239 |
return; |
|
240 |
} |
|
241 |
} |
|
242 |
} |
|
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 SndPlayTileFx(SoundFx sound, TileIndex tile) |
0 | 245 |
{ |
3645
7f950533d510
(svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents:
3421
diff
changeset
|
246 |
/* emits sound from center of the tile */ |
7f950533d510
(svn r4554) Replace magic numbers by TILE_{HEIGHT,SIZE}
tron
parents:
3421
diff
changeset
|
247 |
int x = TileX(tile) * TILE_SIZE + TILE_SIZE / 2; |
8588
2674b9a4be81
(svn r12169) -Change [FS#1696]: play sounds when there is only small part of tile/vehicle visible too (original idea by Dominik)
smatz
parents:
8439
diff
changeset
|
248 |
int y = TileY(tile) * TILE_SIZE - TILE_SIZE / 2; |
8604
36135d25a700
(svn r12186) -Fix [FS#1784](r12169): assert when trying to play tile sound at NW border of map (placing buyos, leveling land)
smatz
parents:
8595
diff
changeset
|
249 |
uint z = (y < 0 ? 0 : GetSlopeZ(x, y)); |
36135d25a700
(svn r12186) -Fix [FS#1784](r12169): assert when trying to play tile sound at NW border of map (placing buyos, leveling land)
smatz
parents:
8595
diff
changeset
|
250 |
Point pt = RemapCoords(x, y, z); |
8588
2674b9a4be81
(svn r12169) -Change [FS#1696]: play sounds when there is only small part of tile/vehicle visible too (original idea by Dominik)
smatz
parents:
8439
diff
changeset
|
251 |
y += 2 * TILE_SIZE; |
2674b9a4be81
(svn r12169) -Change [FS#1696]: play sounds when there is only small part of tile/vehicle visible too (original idea by Dominik)
smatz
parents:
8439
diff
changeset
|
252 |
Point pt2 = RemapCoords(x, y, GetSlopeZ(x, y)); |
2674b9a4be81
(svn r12169) -Change [FS#1696]: play sounds when there is only small part of tile/vehicle visible too (original idea by Dominik)
smatz
parents:
8439
diff
changeset
|
253 |
SndPlayScreenCoordFx(sound, pt.x, pt2.x, pt.y, pt2.y); |
0 | 254 |
} |
255 |
||
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
256 |
void SndPlayVehicleFx(SoundFx sound, const Vehicle *v) |
0 | 257 |
{ |
258 |
SndPlayScreenCoordFx(sound, |
|
8588
2674b9a4be81
(svn r12169) -Change [FS#1696]: play sounds when there is only small part of tile/vehicle visible too (original idea by Dominik)
smatz
parents:
8439
diff
changeset
|
259 |
v->left_coord, v->right_coord, |
2674b9a4be81
(svn r12169) -Change [FS#1696]: play sounds when there is only small part of tile/vehicle visible too (original idea by Dominik)
smatz
parents:
8439
diff
changeset
|
260 |
v->top_coord, v->top_coord |
0 | 261 |
); |
262 |
} |
|
263 |
||
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
264 |
void SndPlayFx(SoundFx sound) |
0 | 265 |
{ |
8439
6913310b9a18
(svn r12009) -Fix [FS#1707]: Avoid loading sample.cat if it 'looks' incorrect, and avoid later null pointer dereferences by moving volume lookup deeper.
peter1138
parents:
8224
diff
changeset
|
266 |
StartSound(sound, 0, msf.effect_vol); |
0 | 267 |
} |