author | truelight |
Thu, 24 Mar 2005 14:43:40 +0000 | |
changeset 1540 | 6eef12dab5bf |
parent 1498 | 508395d0639a |
child 1891 | 862800791170 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
679
04ca2cd69420
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
337
diff
changeset
|
3 |
#include "map.h" |
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
4 |
#include "mixer.h" |
0 | 5 |
#include "sound.h" |
6 |
#include "vehicle.h" |
|
7 |
#include "window.h" |
|
8 |
#include "viewport.h" |
|
9 |
#include "fileio.h" |
|
10 |
||
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
11 |
typedef struct FileEntry { |
0 | 12 |
uint32 file_offset; |
13 |
uint32 file_size; |
|
14 |
uint16 rate; |
|
15 |
uint8 bits_per_sample; |
|
16 |
uint8 channels; |
|
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
17 |
} FileEntry; |
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; |
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
20 |
static FileEntry* _files; |
0 | 21 |
|
22 |
#define SOUND_SLOT 31 |
|
23 |
||
24 |
||
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
25 |
static void OpenBankFile(const char *filename) |
0 | 26 |
{ |
27 |
uint count, i; |
|
28 |
uint32 size, tag; |
|
29 |
FileEntry *fe; |
|
30 |
||
31 |
FioOpenFile(SOUND_SLOT, filename); |
|
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
32 |
_file_count = count = FioReadDword() >> 3; |
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
33 |
fe = _files = calloc(sizeof(FileEntry), count); |
0 | 34 |
|
35 |
FioSeekTo(0, SEEK_SET); |
|
36 |
||
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
37 |
for (i = 0; i != count; i++) { |
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
38 |
fe[i].file_offset = FioReadDword(); |
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
39 |
fe[i].file_size = FioReadDword(); |
0 | 40 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
41 |
|
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
42 |
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
|
43 |
char name[255]; |
0 | 44 |
|
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
45 |
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
|
46 |
|
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
47 |
// Check for special case, see else case |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
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
|
51 |
|
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
52 |
// Read riff tags |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
53 |
for (;;) { |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
54 |
tag = FioReadDword(); |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
55 |
size = FioReadDword(); |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
56 |
|
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
57 |
if (tag == ' tmf') { |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
58 |
FioReadWord(); // wFormatTag |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
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
|
63 |
FioReadWord(); // alignment |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
64 |
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
|
65 |
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
|
66 |
} 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
|
67 |
fe->file_size = size; |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
68 |
fe->file_offset = FioGetPos() | (SOUND_SLOT << 24); |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
69 |
break; |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
70 |
} else { |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
71 |
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
|
72 |
break; |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
73 |
} |
0 | 74 |
} |
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
75 |
} else { |
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 |
* 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
|
78 |
* (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
|
79 |
* 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
|
80 |
*/ |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
81 |
fe->channels = 1; |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
82 |
fe->rate = 11025; |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
83 |
fe->bits_per_sample = 8; |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
84 |
fe->file_offset = FioGetPos() | (SOUND_SLOT << 24); |
0 | 85 |
} |
86 |
} |
|
87 |
} |
|
88 |
||
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
89 |
static bool SetBankSource(MixerChannel *mc, uint bank) |
0 | 90 |
{ |
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
91 |
FileEntry *fe = &_files[bank]; |
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
92 |
int8 *mem; |
0 | 93 |
uint i; |
94 |
||
95 |
if (fe->file_size == 0) |
|
96 |
return false; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
97 |
|
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
98 |
mem = malloc(fe->file_size); /* XXX unchecked malloc */ |
0 | 99 |
FioSeekToFile(fe->file_offset); |
100 |
FioReadBlock(mem, fe->file_size); |
|
101 |
||
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
102 |
for (i = 0; i != fe->file_size; i++) |
1136 | 103 |
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
|
104 |
|
0 | 105 |
assert(fe->bits_per_sample == 8 && fe->channels == 1 && fe->file_size != 0 && fe->rate != 0); |
106 |
||
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
107 |
MxSetChannelRawSrc(mc, mem, fe->file_size, fe->rate, MX_AUTOFREE); |
0 | 108 |
|
109 |
return true; |
|
110 |
} |
|
111 |
||
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
112 |
bool SoundInitialize(const char *filename) |
0 | 113 |
{ |
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
114 |
OpenBankFile(filename); |
0 | 115 |
return true; |
116 |
} |
|
117 |
||
118 |
// Low level sound player |
|
119 |
static void StartSound(uint sound, uint panning, uint volume) |
|
120 |
{ |
|
121 |
if (volume != 0) { |
|
122 |
MixerChannel *mc = MxAllocateChannel(_mixer); |
|
123 |
if (mc == NULL) |
|
124 |
return; |
|
1496
15d859a626e8
(svn r2000) Split the sound system into backend (mixer.[ch]) and frontend (sound.[ch])
tron
parents:
1136
diff
changeset
|
125 |
if (SetBankSource(mc, sound)) { |
0 | 126 |
MxSetChannelVolume(mc, volume << 8, volume << 8); |
1498
508395d0639a
(svn r2002) Rename MxActivate to MxActivateChannel, which is more appropriate
tron
parents:
1496
diff
changeset
|
127 |
MxActivateChannel(mc); |
0 | 128 |
} |
129 |
} |
|
130 |
} |
|
131 |
||
132 |
||
133 |
static const byte _vol_factor_by_zoom[] = {255, 190, 134}; |
|
134 |
||
135 |
static const byte _sound_base_vol[] = { |
|
136 |
128, 90, 128, 128, 128, 128, 128, 128, |
|
137 |
128, 90, 90, 128, 128, 128, 128, 128, |
|
138 |
128, 128, 128, 80, 128, 128, 128, 128, |
|
139 |
128, 128, 128, 128, 128, 128, 128, 128, |
|
140 |
128, 128, 90, 90, 90, 128, 90, 128, |
|
141 |
128, 90, 128, 128, 128, 90, 128, 128, |
|
142 |
128, 128, 128, 128, 90, 128, 128, 128, |
|
143 |
128, 90, 128, 128, 128, 128, 128, 128, |
|
144 |
128, 128, 90, 90, 90, 128, 128, 128, |
|
145 |
90, |
|
146 |
}; |
|
147 |
||
148 |
static const byte _sound_idx[] = { |
|
149 |
2, 3, 4, 5, 6, 7, 8, 9, |
|
150 |
10, 11, 12, 13, 14, 15, 16, 17, |
|
151 |
18, 19, 20, 21, 22, 23, 24, 25, |
|
152 |
26, 27, 28, 29, 30, 31, 32, 33, |
|
153 |
34, 35, 36, 37, 38, 39, 40, 0, |
|
154 |
1, 41, 42, 43, 44, 45, 46, 47, |
|
155 |
48, 49, 50, 51, 52, 53, 54, 55, |
|
156 |
56, 57, 58, 59, 60, 61, 62, 63, |
|
157 |
64, 65, 66, 67, 68, 69, 70, 71, |
|
158 |
72, |
|
159 |
}; |
|
160 |
||
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
161 |
static void SndPlayScreenCoordFx(SoundFx sound, int x, int y) |
0 | 162 |
{ |
163 |
Window *w; |
|
164 |
ViewPort *vp; |
|
165 |
int left; |
|
166 |
||
167 |
if (msf.effect_vol == 0) |
|
168 |
return; |
|
169 |
||
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
170 |
for (w = _windows; w != _last_window; w++) { |
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
171 |
if ((vp = w->viewport) != NULL && |
0 | 172 |
IS_INSIDE_1D(x, vp->virtual_left, vp->virtual_width) && |
173 |
IS_INSIDE_1D(y, vp->virtual_top, vp->virtual_height)) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
174 |
|
0 | 175 |
left = ((x - vp->virtual_left) >> vp->zoom) + vp->left; |
176 |
StartSound( |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
177 |
_sound_idx[sound], |
0 | 178 |
clamp(left / 71, 0, 8), |
179 |
(_sound_base_vol[sound] * msf.effect_vol * _vol_factor_by_zoom[vp->zoom]) >> 15 |
|
180 |
); |
|
181 |
return; |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
} |
|
186 |
||
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
187 |
void SndPlayTileFx(SoundFx sound, TileIndex tile) |
0 | 188 |
{ |
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
189 |
/* emits sound from center (+ 8) of the tile */ |
926
a6d140a6a4de
(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
|
190 |
int x = TileX(tile) * 16 + 8; |
a6d140a6a4de
(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
|
191 |
int y = TileY(tile) * 16 + 8; |
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
192 |
Point pt = RemapCoords(x, y, GetSlopeZ(x, y)); |
0 | 193 |
SndPlayScreenCoordFx(sound, pt.x, pt.y); |
194 |
} |
|
195 |
||
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
196 |
void SndPlayVehicleFx(SoundFx sound, const Vehicle *v) |
0 | 197 |
{ |
198 |
SndPlayScreenCoordFx(sound, |
|
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
199 |
(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
|
200 |
(v->top_coord + v->bottom_coord) / 2 |
0 | 201 |
); |
202 |
} |
|
203 |
||
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
193
diff
changeset
|
204 |
void SndPlayFx(SoundFx sound) |
0 | 205 |
{ |
206 |
StartSound( |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
207 |
_sound_idx[sound], |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
208 |
4, |
0 | 209 |
(_sound_base_vol[sound] * msf.effect_vol) >> 7 |
210 |
); |
|
211 |
} |