0
|
1 |
#include "stdafx.h"
|
|
2 |
#include "ttd.h"
|
|
3 |
#include "sound.h"
|
|
4 |
#include "vehicle.h"
|
|
5 |
#include "window.h"
|
|
6 |
#include "viewport.h"
|
|
7 |
#include "fileio.h"
|
|
8 |
|
|
9 |
typedef struct {
|
|
10 |
// Mixer
|
|
11 |
Mixer *mx;
|
|
12 |
bool active;
|
|
13 |
|
|
14 |
// pointer to allocated buffer memory
|
|
15 |
void *memory;
|
|
16 |
|
|
17 |
// current position in memory
|
|
18 |
uint32 pos;
|
|
19 |
uint32 frac_pos;
|
|
20 |
uint32 frac_speed;
|
|
21 |
uint32 samples_left;
|
|
22 |
|
|
23 |
// Mixing volume
|
|
24 |
uint volume_left;
|
|
25 |
uint volume_right;
|
|
26 |
|
|
27 |
uint flags;
|
|
28 |
} MixerChannel;
|
|
29 |
|
|
30 |
typedef struct FileEntry FileEntry;
|
|
31 |
struct FileEntry {
|
|
32 |
uint32 file_offset;
|
|
33 |
uint32 file_size;
|
|
34 |
uint16 rate;
|
|
35 |
uint8 bits_per_sample;
|
|
36 |
uint8 channels;
|
|
37 |
};
|
|
38 |
|
|
39 |
struct Mixer {
|
|
40 |
uint32 play_rate;
|
|
41 |
FileEntry *files;
|
|
42 |
uint32 file_count;
|
|
43 |
MixerChannel channels[8];
|
|
44 |
};
|
|
45 |
|
|
46 |
enum {
|
|
47 |
MX_AUTOFREE = 1,
|
|
48 |
// MX_8BIT = 2,
|
|
49 |
// MX_STEREO = 4,
|
|
50 |
MX_UNSIGNED = 8,
|
|
51 |
};
|
|
52 |
|
|
53 |
#define SOUND_SLOT 31
|
|
54 |
|
|
55 |
|
|
56 |
FILE *out;
|
|
57 |
|
|
58 |
static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples)
|
|
59 |
{
|
|
60 |
int8 *b;
|
|
61 |
uint32 frac_pos;
|
|
62 |
uint32 frac_speed;
|
|
63 |
uint volume_left;
|
|
64 |
uint volume_right;
|
|
65 |
|
|
66 |
if (samples > sc->samples_left) samples = sc->samples_left;
|
|
67 |
sc->samples_left -= samples;
|
|
68 |
assert(samples > 0);
|
|
69 |
|
|
70 |
b = (int8*)sc->memory + sc->pos;
|
|
71 |
frac_pos = sc->frac_pos;
|
|
72 |
frac_speed = sc->frac_speed;
|
|
73 |
volume_left = sc->volume_left;
|
|
74 |
volume_right = sc->volume_right;
|
|
75 |
|
|
76 |
if (frac_speed == 0x10000) {
|
|
77 |
// Special case when frac_speed is 0x10000
|
|
78 |
do {
|
|
79 |
buffer[0]+= *b * volume_left >> 8;
|
|
80 |
buffer[1]+= *b * volume_right >> 8;
|
|
81 |
b++;
|
|
82 |
buffer += 2;
|
|
83 |
} while (--samples);
|
|
84 |
} else {
|
|
85 |
do {
|
|
86 |
buffer[0] += *b * volume_left >> 8;
|
|
87 |
buffer[1] += *b * volume_right >> 8;
|
|
88 |
buffer += 2;
|
|
89 |
frac_pos += frac_speed;
|
|
90 |
b += frac_pos >> 16;
|
|
91 |
frac_pos &= 0xffff;
|
|
92 |
} while (--samples);
|
|
93 |
}
|
|
94 |
|
|
95 |
sc->frac_pos = frac_pos;
|
|
96 |
sc->pos = b - (int8*)sc->memory;
|
|
97 |
}
|
|
98 |
|
|
99 |
static void MxCloseChannel(MixerChannel *mc)
|
|
100 |
{
|
|
101 |
void *mem = mc->memory;
|
|
102 |
mc->memory = NULL;
|
|
103 |
mc->active = false;
|
|
104 |
|
|
105 |
if (mc->flags & MX_AUTOFREE)
|
|
106 |
free(mem);
|
|
107 |
}
|
|
108 |
|
|
109 |
void MxMixSamples(Mixer *mx, void *buffer, uint samples)
|
|
110 |
{
|
|
111 |
int i;
|
|
112 |
MixerChannel *mc;
|
|
113 |
|
|
114 |
// Clear the buffer
|
|
115 |
memset(buffer, 0, sizeof(int16)*2*samples);
|
|
116 |
|
|
117 |
// Mix each channel
|
|
118 |
for(i=0,mc=mx->channels; i!=lengthof(mx->channels); i++,mc++) {
|
|
119 |
if (mc->active) {
|
|
120 |
mix_int8_to_int16(mc, (int16*)buffer, samples);
|
|
121 |
if (mc->samples_left == 0) {
|
|
122 |
MxCloseChannel(mc);
|
|
123 |
}
|
|
124 |
}
|
|
125 |
}
|
|
126 |
|
|
127 |
// if (out == NULL)
|
|
128 |
// out = fopen("d:\\dump.raw", "wb");
|
|
129 |
// fwrite(buffer, samples*4, 1, out);
|
|
130 |
}
|
|
131 |
|
|
132 |
static MixerChannel *MxAllocateChannel(Mixer *mx)
|
|
133 |
{
|
|
134 |
int i;
|
|
135 |
MixerChannel *mc;
|
|
136 |
for(i=0,mc=mx->channels; i!=lengthof(mx->channels); i++,mc++)
|
|
137 |
if (!mc->memory) {
|
|
138 |
mc->active = false;
|
|
139 |
mc->mx = mx;
|
|
140 |
return mc;
|
|
141 |
}
|
|
142 |
return NULL;
|
|
143 |
}
|
|
144 |
|
|
145 |
static void MxSetChannelRawSrc(MixerChannel *mc, void *mem, uint size, uint rate, uint flags)
|
|
146 |
{
|
|
147 |
mc->memory = mem;
|
|
148 |
mc->flags = flags;
|
|
149 |
mc->frac_pos = 0;
|
|
150 |
mc->pos = 0;
|
|
151 |
|
|
152 |
mc->frac_speed = (rate<<16) / mc->mx->play_rate;
|
|
153 |
|
|
154 |
// adjust the magnitude to prevent overflow
|
|
155 |
while (size & 0xFFFF0000)
|
|
156 |
size >>= 1, rate = (rate >> 1) + 1;
|
|
157 |
|
|
158 |
mc->samples_left = size * mc->mx->play_rate / rate;
|
|
159 |
}
|
|
160 |
|
|
161 |
static void MxSetChannelVolume(MixerChannel *mc, uint left, uint right)
|
|
162 |
{
|
|
163 |
mc->volume_left = left;
|
|
164 |
mc->volume_right = right;
|
|
165 |
}
|
|
166 |
|
|
167 |
static void MxOpenBankFile(Mixer *mx, const char *filename)
|
|
168 |
{
|
|
169 |
uint count, i;
|
|
170 |
uint32 size, tag;
|
|
171 |
FileEntry *fe;
|
|
172 |
|
|
173 |
FioOpenFile(SOUND_SLOT, filename);
|
|
174 |
mx->file_count = count = FioReadDword() >> 3;
|
|
175 |
fe = mx->files = calloc(sizeof(FileEntry), count);
|
|
176 |
|
|
177 |
FioSeekTo(0, SEEK_SET);
|
|
178 |
|
|
179 |
for(i=0; i!=count; i++,fe++) {
|
|
180 |
fe->file_offset = FioReadDword();
|
|
181 |
fe->file_size = FioReadDword();
|
|
182 |
}
|
|
183 |
|
|
184 |
fe = mx->files;
|
|
185 |
for(i=0; i!=count; i++,fe++) {
|
|
186 |
FioSeekTo(fe->file_offset, SEEK_SET);
|
|
187 |
// Skip past string and the name.
|
|
188 |
FioSeekTo(FioReadByte() + 12, SEEK_CUR);
|
|
189 |
|
|
190 |
// Read riff tags
|
|
191 |
while(true) {
|
|
192 |
tag = FioReadDword();
|
|
193 |
size = FioReadDword();
|
|
194 |
|
|
195 |
if (tag == ' tmf') {
|
|
196 |
FioReadWord(); // wFormatTag
|
|
197 |
fe->channels = FioReadWord(); // wChannels
|
|
198 |
FioReadDword(); // samples per second
|
|
199 |
fe->rate = 11025; // seems like all samples should be played at this rate.
|
|
200 |
FioReadDword(); // avg bytes per second
|
|
201 |
FioReadWord(); // alignment
|
|
202 |
fe->bits_per_sample = FioReadByte(); // bits per sample
|
|
203 |
FioSeekTo(size - (2+2+4+4+2+1), SEEK_CUR);
|
|
204 |
} else if (tag == 'atad') {
|
|
205 |
fe->file_size = size;
|
|
206 |
fe->file_offset = FioGetPos() | (SOUND_SLOT << 24);
|
|
207 |
break;
|
|
208 |
} else {
|
|
209 |
fe->file_size = 0;
|
|
210 |
break;
|
|
211 |
}
|
|
212 |
}
|
|
213 |
}
|
|
214 |
}
|
|
215 |
|
|
216 |
static bool MxSetBankSource(MixerChannel *mc, uint bank)
|
|
217 |
{
|
|
218 |
FileEntry *fe = &mc->mx->files[bank];
|
|
219 |
void *mem;
|
|
220 |
uint i;
|
|
221 |
|
|
222 |
if (fe->file_size == 0)
|
|
223 |
return false;
|
|
224 |
|
|
225 |
mem = malloc(fe->file_size);
|
|
226 |
FioSeekToFile(fe->file_offset);
|
|
227 |
FioReadBlock(mem, fe->file_size);
|
|
228 |
|
|
229 |
for(i=0; i!=fe->file_size; i++) {
|
|
230 |
((byte*)mem)[i] ^= 0x80;
|
|
231 |
}
|
|
232 |
|
|
233 |
assert(fe->bits_per_sample == 8 && fe->channels == 1 && fe->file_size != 0 && fe->rate != 0);
|
|
234 |
|
|
235 |
MxSetChannelRawSrc(mc, mem, fe->file_size, fe->rate, MX_AUTOFREE | MX_UNSIGNED);
|
|
236 |
|
|
237 |
return true;
|
|
238 |
}
|
|
239 |
|
|
240 |
bool MxInitialize(uint rate, const char *filename)
|
|
241 |
{
|
|
242 |
static Mixer mx;
|
|
243 |
_mixer = &mx;
|
|
244 |
mx.play_rate = rate;
|
|
245 |
MxOpenBankFile(&mx, filename);
|
|
246 |
return true;
|
|
247 |
}
|
|
248 |
|
|
249 |
// Low level sound player
|
|
250 |
static void StartSound(uint sound, uint panning, uint volume)
|
|
251 |
{
|
|
252 |
if (volume != 0) {
|
|
253 |
MixerChannel *mc = MxAllocateChannel(_mixer);
|
|
254 |
if (mc == NULL)
|
|
255 |
return;
|
|
256 |
if (MxSetBankSource(mc, sound)) {
|
|
257 |
MxSetChannelVolume(mc, volume << 8, volume << 8);
|
|
258 |
mc->active = true;
|
|
259 |
}
|
|
260 |
}
|
|
261 |
}
|
|
262 |
|
|
263 |
|
|
264 |
static const byte _vol_factor_by_zoom[] = {255, 190, 134};
|
|
265 |
|
|
266 |
static const byte _sound_base_vol[] = {
|
|
267 |
128, 90, 128, 128, 128, 128, 128, 128,
|
|
268 |
128, 90, 90, 128, 128, 128, 128, 128,
|
|
269 |
128, 128, 128, 80, 128, 128, 128, 128,
|
|
270 |
128, 128, 128, 128, 128, 128, 128, 128,
|
|
271 |
128, 128, 90, 90, 90, 128, 90, 128,
|
|
272 |
128, 90, 128, 128, 128, 90, 128, 128,
|
|
273 |
128, 128, 128, 128, 90, 128, 128, 128,
|
|
274 |
128, 90, 128, 128, 128, 128, 128, 128,
|
|
275 |
128, 128, 90, 90, 90, 128, 128, 128,
|
|
276 |
90,
|
|
277 |
};
|
|
278 |
|
|
279 |
static const byte _sound_idx[] = {
|
|
280 |
2, 3, 4, 5, 6, 7, 8, 9,
|
|
281 |
10, 11, 12, 13, 14, 15, 16, 17,
|
|
282 |
18, 19, 20, 21, 22, 23, 24, 25,
|
|
283 |
26, 27, 28, 29, 30, 31, 32, 33,
|
|
284 |
34, 35, 36, 37, 38, 39, 40, 0,
|
|
285 |
1, 41, 42, 43, 44, 45, 46, 47,
|
|
286 |
48, 49, 50, 51, 52, 53, 54, 55,
|
|
287 |
56, 57, 58, 59, 60, 61, 62, 63,
|
|
288 |
64, 65, 66, 67, 68, 69, 70, 71,
|
|
289 |
72,
|
|
290 |
};
|
|
291 |
|
|
292 |
void SndPlayScreenCoordFx(int sound, int x, int y)
|
|
293 |
{
|
|
294 |
Window *w;
|
|
295 |
ViewPort *vp;
|
|
296 |
int left;
|
|
297 |
|
|
298 |
if (msf.effect_vol == 0)
|
|
299 |
return;
|
|
300 |
|
|
301 |
for(w=_windows; w!=_last_window; w++) {
|
|
302 |
if ((vp=w->viewport) &&
|
|
303 |
IS_INSIDE_1D(x, vp->virtual_left, vp->virtual_width) &&
|
|
304 |
IS_INSIDE_1D(y, vp->virtual_top, vp->virtual_height)) {
|
|
305 |
|
|
306 |
left = ((x - vp->virtual_left) >> vp->zoom) + vp->left;
|
|
307 |
StartSound(
|
|
308 |
_sound_idx[sound],
|
|
309 |
clamp(left / 71, 0, 8),
|
|
310 |
(_sound_base_vol[sound] * msf.effect_vol * _vol_factor_by_zoom[vp->zoom]) >> 15
|
|
311 |
);
|
|
312 |
return;
|
|
313 |
}
|
|
314 |
}
|
|
315 |
|
|
316 |
}
|
|
317 |
|
|
318 |
void SndPlayTileFx(int sound, TileIndex tile)
|
|
319 |
{
|
|
320 |
int x = GET_TILE_X(tile)*16;
|
|
321 |
int y = GET_TILE_Y(tile)*16;
|
|
322 |
Point pt = RemapCoords(x,y, GetSlopeZ(x+8, y+8));
|
|
323 |
SndPlayScreenCoordFx(sound, pt.x, pt.y);
|
|
324 |
}
|
|
325 |
|
|
326 |
void SndPlayVehicleFx(int sound, Vehicle *v)
|
|
327 |
{
|
|
328 |
SndPlayScreenCoordFx(sound,
|
|
329 |
(v->left_coord + v->right_coord) >> 1,
|
|
330 |
(v->top_coord + v->bottom_coord) >> 1
|
|
331 |
);
|
|
332 |
}
|
|
333 |
|
|
334 |
void SndPlayFx(int sound)
|
|
335 |
{
|
|
336 |
StartSound(
|
|
337 |
_sound_idx[sound],
|
|
338 |
4,
|
|
339 |
(_sound_base_vol[sound] * msf.effect_vol) >> 7
|
|
340 |
);
|
|
341 |
}
|