equal
deleted
inserted
replaced
3 /** @file mixer.cpp Mixing of sound samples. */ |
3 /** @file mixer.cpp Mixing of sound samples. */ |
4 |
4 |
5 #include "stdafx.h" |
5 #include "stdafx.h" |
6 #include "openttd.h" |
6 #include "openttd.h" |
7 #include "mixer.h" |
7 #include "mixer.h" |
|
8 #include "core/math_func.hpp" |
8 |
9 |
9 struct MixerChannel { |
10 struct MixerChannel { |
10 bool active; |
11 bool active; |
11 |
12 |
12 /* pointer to allocated buffer memory */ |
13 /* pointer to allocated buffer memory */ |
17 uint32 frac_pos; |
18 uint32 frac_pos; |
18 uint32 frac_speed; |
19 uint32 frac_speed; |
19 uint32 samples_left; |
20 uint32 samples_left; |
20 |
21 |
21 /* Mixing volume */ |
22 /* Mixing volume */ |
22 uint volume_left; |
23 int volume_left; |
23 uint volume_right; |
24 int volume_right; |
24 |
25 |
25 uint flags; |
26 uint flags; |
26 }; |
27 }; |
27 |
28 |
28 static MixerChannel _channels[8]; |
29 static MixerChannel _channels[8]; |
29 static uint32 _play_rate; |
30 static uint32 _play_rate; |
30 |
31 |
|
32 /** |
|
33 * The theoretical maximum volume for a single sound sample. Multiple sound |
|
34 * samples should not exceed this limit as it will sound too loud. It also |
|
35 * stops overflowing when too many sounds are played at the same time, which |
|
36 * causes an even worse sound quality. |
|
37 */ |
|
38 static const int MAX_VOLUME = 128 * 128; |
|
39 |
31 |
40 |
32 static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples) |
41 static void mix_int8_to_int16(MixerChannel *sc, int16 *buffer, uint samples) |
33 { |
42 { |
34 int8 *b; |
43 int8 *b; |
35 uint32 frac_pos; |
44 uint32 frac_pos; |
36 uint32 frac_speed; |
45 uint32 frac_speed; |
37 uint volume_left; |
46 int volume_left; |
38 uint volume_right; |
47 int volume_right; |
39 |
48 |
40 if (samples > sc->samples_left) samples = sc->samples_left; |
49 if (samples > sc->samples_left) samples = sc->samples_left; |
41 sc->samples_left -= samples; |
50 sc->samples_left -= samples; |
42 assert(samples > 0); |
51 assert(samples > 0); |
43 |
52 |
48 volume_right = sc->volume_right; |
57 volume_right = sc->volume_right; |
49 |
58 |
50 if (frac_speed == 0x10000) { |
59 if (frac_speed == 0x10000) { |
51 /* Special case when frac_speed is 0x10000 */ |
60 /* Special case when frac_speed is 0x10000 */ |
52 do { |
61 do { |
53 buffer[0] += *b * volume_left >> 8; |
62 buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME); |
54 buffer[1] += *b * volume_right >> 8; |
63 buffer[0] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME); |
55 b++; |
64 b++; |
56 buffer += 2; |
65 buffer += 2; |
57 } while (--samples > 0); |
66 } while (--samples > 0); |
58 } else { |
67 } else { |
59 do { |
68 do { |
60 buffer[0] += *b * volume_left >> 8; |
69 buffer[0] = Clamp(buffer[0] + (*b * volume_left >> 8), -MAX_VOLUME, MAX_VOLUME); |
61 buffer[1] += *b * volume_right >> 8; |
70 buffer[0] = Clamp(buffer[1] + (*b * volume_right >> 8), -MAX_VOLUME, MAX_VOLUME); |
62 buffer += 2; |
71 buffer += 2; |
63 frac_pos += frac_speed; |
72 frac_pos += frac_speed; |
64 b += frac_pos >> 16; |
73 b += frac_pos >> 16; |
65 frac_pos &= 0xffff; |
74 frac_pos &= 0xffff; |
66 } while (--samples > 0); |
75 } while (--samples > 0); |