1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 // Based on the Xentax Wiki documentation:
24 // http://wiki.xentax.com/index.php/The_Last_Express_SND
25
26 #include "lastexpress/data/snd.h"
27
28 #include "lastexpress/debug.h"
29
30 #include "audio/decoders/adpcm_intern.h"
31 #include "common/debug.h"
32 #include "common/memstream.h"
33 #include "common/system.h"
34
35 namespace LastExpress {
36
37 #pragma region Sound filters tables
38
39 static const int stepTable[1424] = {
40 0, 0, 0, 0, 128, 256, 384, 512, 0, 0, 0, 0, 128, 256,
41 384, 512, 0, 0, 0, 0, 192, 320, 448, 576, 0, 0, 0, 0,
42 192, 320, 448, 576, 64, 64, 64, 64, 256, 384, 512, 640,
43 64, 64, 64, 64, 256, 384, 512, 640, 128, 128, 128, 128,
44 320, 448, 576, 704, 128, 128, 128, 128, 320, 448, 576,
45 704, 192, 192, 192, 192, 384, 512, 640, 768, 192, 192,
46 192, 192, 384, 512, 640, 768, 256, 256, 256, 256, 448,
47 576, 704, 832, 256, 256, 256, 256, 448, 576, 704, 832,
48 320, 320, 320, 320, 512, 640, 768, 896, 320, 320, 320,
49 320, 512, 640, 768, 896, 384, 384, 384, 384, 576, 704,
50 832, 960, 384, 384, 384, 384, 576, 704, 832, 960, 448,
51 448, 448, 448, 640, 768, 896, 1024, 448, 448, 448, 448,
52 640, 768, 896, 1024, 512, 512, 512, 512, 704, 832, 960,
53 1088, 512, 512, 512, 512, 704, 832, 960, 1088, 576,
54 576, 576, 576, 768, 896, 1024, 1152, 576, 576, 576,
55 576, 768, 896, 1024, 1152, 640, 640, 640, 640, 832,
56 960, 1088, 1216, 640, 640, 640, 640, 832, 960, 1088,
57 1216, 704, 704, 704, 704, 896, 1024, 1152, 1280, 704,
58 704, 704, 704, 896, 1024, 1152, 1280, 768, 768, 768,
59 768, 960, 1088, 1216, 1344, 768, 768, 768, 768, 960,
60 1088, 1216, 1344, 832, 832, 832, 832, 1024, 1152, 1280,
61 1408, 832, 832, 832, 832, 1024, 1152, 1280, 1408, 896,
62 896, 896, 896, 1088, 1216, 1344, 1472, 896, 896, 896,
63 896, 1088, 1216, 1344, 1472, 960, 960, 960, 960, 1152,
64 1280, 1408, 1536, 960, 960, 960, 960, 1152, 1280, 1408,
65 1536, 1024, 1024, 1024, 1024, 1216, 1344, 1472, 1600,
66 1024, 1024, 1024, 1024, 1216, 1344, 1472, 1600, 1088,
67 1088, 1088, 1088, 1280, 1408, 1536, 1664, 1088, 1088,
68 1088, 1088, 1280, 1408, 1536, 1664, 1152, 1152, 1152,
69 1152, 1344, 1472, 1600, 1728, 1152, 1152, 1152, 1152,
70 1344, 1472, 1600, 1728, 1216, 1216, 1216, 1216, 1408,
71 1536, 1664, 1792, 1216, 1216, 1216, 1216, 1408, 1536,
72 1664, 1792, 1280, 1280, 1280, 1280, 1472, 1600, 1728,
73 1856, 1280, 1280, 1280, 1280, 1472, 1600, 1728, 1856,
74 1344, 1344, 1344, 1344, 1536, 1664, 1792, 1920, 1344,
75 1344, 1344, 1344, 1536, 1664, 1792, 1920, 1408, 1408,
76 1408, 1408, 1600, 1728, 1856, 1984, 1408, 1408, 1408,
77 1408, 1600, 1728, 1856, 1984, 1472, 1472, 1472, 1472,
78 1664, 1792, 1920, 2048, 1472, 1472, 1472, 1472, 1664,
79 1792, 1920, 2048, 1536, 1536, 1536, 1536, 1728, 1856,
80 1984, 2112, 1536, 1536, 1536, 1536, 1728, 1856, 1984,
81 2112, 1600, 1600, 1600, 1600, 1792, 1920, 2048, 2176,
82 1600, 1600, 1600, 1600, 1792, 1920, 2048, 2176, 1664,
83 1664, 1664, 1664, 1856, 1984, 2112, 2240, 1664, 1664,
84 1664, 1664, 1856, 1984, 2112, 2240, 1728, 1728, 1728,
85 1728, 1920, 2048, 2176, 2304, 1728, 1728, 1728, 1728,
86 1920, 2048, 2176, 2304, 1792, 1792, 1792, 1792, 1984,
87 2112, 2240, 2368, 1792, 1792, 1792, 1792, 1984, 2112,
88 2240, 2368, 1856, 1856, 1856, 1856, 2048, 2176, 2304,
89 2432, 1856, 1856, 1856, 1856, 2048, 2176, 2304, 2432,
90 1920, 1920, 1920, 1920, 2112, 2240, 2368, 2496, 1920,
91 1920, 1920, 1920, 2112, 2240, 2368, 2496, 1984, 1984,
92 1984, 1984, 2176, 2304, 2432, 2560, 1984, 1984, 1984,
93 1984, 2176, 2304, 2432, 2560, 2048, 2048, 2048, 2048,
94 2240, 2368, 2496, 2624, 2048, 2048, 2048, 2048, 2240,
95 2368, 2496, 2624, 2112, 2112, 2112, 2112, 2304, 2432,
96 2560, 2688, 2112, 2112, 2112, 2112, 2304, 2432, 2560,
97 2688, 2176, 2176, 2176, 2176, 2368, 2496, 2624, 2752,
98 2176, 2176, 2176, 2176, 2368, 2496, 2624, 2752, 2240,
99 2240, 2240, 2240, 2432, 2560, 2688, 2816, 2240, 2240,
100 2240, 2240, 2432, 2560, 2688, 2816, 2304, 2304, 2304,
101 2304, 2496, 2624, 2752, 2880, 2304, 2304, 2304, 2304,
102 2496, 2624, 2752, 2880, 2368, 2368, 2368, 2368, 2560,
103 2688, 2816, 2944, 2368, 2368, 2368, 2368, 2560, 2688,
104 2816, 2944, 2432, 2432, 2432, 2432, 2624, 2752, 2880,
105 3008, 2432, 2432, 2432, 2432, 2624, 2752, 2880, 3008,
106 2496, 2496, 2496, 2496, 2688, 2816, 2944, 3072, 2496,
107 2496, 2496, 2496, 2688, 2816, 2944, 3072, 2560, 2560,
108 2560, 2560, 2752, 2880, 3008, 3136, 2560, 2560, 2560,
109 2560, 2752, 2880, 3008, 3136, 2624, 2624, 2624, 2624,
110 2816, 2944, 3072, 3200, 2624, 2624, 2624, 2624, 2816,
111 2944, 3072, 3200, 2688, 2688, 2688, 2688, 2880, 3008,
112 3136, 3264, 2688, 2688, 2688, 2688, 2880, 3008, 3136,
113 3264, 2752, 2752, 2752, 2752, 2944, 3072, 3200, 3328,
114 2752, 2752, 2752, 2752, 2944, 3072, 3200, 3328, 2816,
115 2816, 2816, 2816, 3008, 3136, 3264, 3392, 2816, 2816,
116 2816, 2816, 3008, 3136, 3264, 3392, 2880, 2880, 2880,
117 2880, 3072, 3200, 3328, 3456, 2880, 2880, 2880, 2880,
118 3072, 3200, 3328, 3456, 2944, 2944, 2944, 2944, 3136,
119 3264, 3392, 3520, 2944, 2944, 2944, 2944, 3136, 3264,
120 3392, 3520, 3008, 3008, 3008, 3008, 3200, 3328, 3456,
121 3584, 3008, 3008, 3008, 3008, 3200, 3328, 3456, 3584,
122 3072, 3072, 3072, 3072, 3264, 3392, 3520, 3648, 3072,
123 3072, 3072, 3072, 3264, 3392, 3520, 3648, 3136, 3136,
124 3136, 3136, 3328, 3456, 3584, 3712, 3136, 3136, 3136,
125 3136, 3328, 3456, 3584, 3712, 3200, 3200, 3200, 3200,
126 3392, 3520, 3648, 3776, 3200, 3200, 3200, 3200, 3392,
127 3520, 3648, 3776, 3264, 3264, 3264, 3264, 3456, 3584,
128 3712, 3840, 3264, 3264, 3264, 3264, 3456, 3584, 3712,
129 3840, 3328, 3328, 3328, 3328, 3520, 3648, 3776, 3904,
130 3328, 3328, 3328, 3328, 3520, 3648, 3776, 3904, 3392,
131 3392, 3392, 3392, 3584, 3712, 3840, 3968, 3392, 3392,
132 3392, 3392, 3584, 3712, 3840, 3968, 3456, 3456, 3456,
133 3456, 3648, 3776, 3904, 4032, 3456, 3456, 3456, 3456,
134 3648, 3776, 3904, 4032, 3520, 3520, 3520, 3520, 3712,
135 3840, 3968, 4096, 3520, 3520, 3520, 3520, 3712, 3840,
136 3968, 4096, 3584, 3584, 3584, 3584, 3776, 3904, 4032,
137 4160, 3584, 3584, 3584, 3584, 3776, 3904, 4032, 4160,
138 3648, 3648, 3648, 3648, 3840, 3968, 4096, 4224, 3648,
139 3648, 3648, 3648, 3840, 3968, 4096, 4224, 3712, 3712,
140 3712, 3712, 3904, 4032, 4160, 4288, 3712, 3712, 3712,
141 3712, 3904, 4032, 4160, 4288, 3776, 3776, 3776, 3776,
142 3968, 4096, 4224, 4352, 3776, 3776, 3776, 3776, 3968,
143 4096, 4224, 4352, 3840, 3840, 3840, 3840, 4032, 4160,
144 4288, 4416, 3840, 3840, 3840, 3840, 4032, 4160, 4288,
145 4416, 3904, 3904, 3904, 3904, 4096, 4224, 4352, 4480,
146 3904, 3904, 3904, 3904, 4096, 4224, 4352, 4480, 3968,
147 3968, 3968, 3968, 4160, 4288, 4416, 4544, 3968, 3968,
148 3968, 3968, 4160, 4288, 4416, 4544, 4032, 4032, 4032,
149 4032, 4224, 4352, 4480, 4608, 4032, 4032, 4032, 4032,
150 4224, 4352, 4480, 4608, 4096, 4096, 4096, 4096, 4288,
151 4416, 4544, 4672, 4096, 4096, 4096, 4096, 4288, 4416,
152 4544, 4672, 4160, 4160, 4160, 4160, 4352, 4480, 4608,
153 4736, 4160, 4160, 4160, 4160, 4352, 4480, 4608, 4736,
154 4224, 4224, 4224, 4224, 4416, 4544, 4672, 4800, 4224,
155 4224, 4224, 4224, 4416, 4544, 4672, 4800, 4288, 4288,
156 4288, 4288, 4480, 4608, 4736, 4864, 4288, 4288, 4288,
157 4288, 4480, 4608, 4736, 4864, 4352, 4352, 4352, 4352,
158 4544, 4672, 4800, 4928, 4352, 4352, 4352, 4352, 4544,
159 4672, 4800, 4928, 4416, 4416, 4416, 4416, 4608, 4736,
160 4864, 4992, 4416, 4416, 4416, 4416, 4608, 4736, 4864,
161 4992, 4480, 4480, 4480, 4480, 4672, 4800, 4928, 5056,
162 4480, 4480, 4480, 4480, 4672, 4800, 4928, 5056, 4544,
163 4544, 4544, 4544, 4736, 4864, 4992, 5120, 4544, 4544,
164 4544, 4544, 4736, 4864, 4992, 5120, 4608, 4608, 4608,
165 4608, 4800, 4928, 5056, 5184, 4608, 4608, 4608, 4608,
166 4800, 4928, 5056, 5184, 4672, 4672, 4672, 4672, 4864,
167 4992, 5120, 5248, 4672, 4672, 4672, 4672, 4864, 4992,
168 5120, 5248, 4736, 4736, 4736, 4736, 4928, 5056, 5184,
169 5312, 4736, 4736, 4736, 4736, 4928, 5056, 5184, 5312,
170 4800, 4800, 4800, 4800, 4992, 5120, 5248, 5376, 4800,
171 4800, 4800, 4800, 4992, 5120, 5248, 5376, 4864, 4864,
172 4864, 4864, 5056, 5184, 5312, 5440, 4864, 4864, 4864,
173 4864, 5056, 5184, 5312, 5440, 4928, 4928, 4928, 4928,
174 5120, 5248, 5376, 5504, 4928, 4928, 4928, 4928, 5120,
175 5248, 5376, 5504, 4992, 4992, 4992, 4992, 5184, 5312,
176 5440, 5568, 4992, 4992, 4992, 4992, 5184, 5312, 5440,
177 5568, 5056, 5056, 5056, 5056, 5248, 5376, 5504, 5632,
178 5056, 5056, 5056, 5056, 5248, 5376, 5504, 5632, 5120,
179 5120, 5120, 5120, 5312, 5440, 5568, 5632, 5120, 5120,
180 5120, 5120, 5312, 5440, 5568, 5632, 5184, 5184, 5184,
181 5184, 5376, 5504, 5632, 5632, 5184, 5184, 5184, 5184,
182 5376, 5504, 5632, 5632, 5248, 5248, 5248, 5248, 5440,
183 5568, 5632, 5632, 5248, 5248, 5248, 5248, 5440, 5568,
184 5632, 5632, 5312, 5312, 5312, 5312, 5504, 5632, 5632,
185 5632, 5312, 5312, 5312, 5312, 5504, 5632, 5632, 5632,
186 5376, 5376, 5376, 5376, 5568, 5632, 5632, 5632, 5376,
187 5376, 5376, 5376, 5568, 5632, 5632, 5632, 5440, 5440,
188 5440, 5440, 5632, 5632, 5632, 5632, 5440, 5440, 5440,
189 5440, 5632, 5632, 5632, 5632, 5504, 5504, 5504, 5504,
190 5632, 5632, 5632, 5632, 5504, 5504, 5504, 5504, 5632,
191 5632, 5632, 5632, 5568, 5568, 5568, 5568, 5632, 5632,
192 5632, 5632, 5568, 5568, 5568, 5568, 5632, 5632, 5632,
193 5632
194 };
195
196 static const int imaTable[1424] = {
197 0, 2, 4, 6, 7, 9, 11, 13, 0, -2, -4, -6, -7, -9, -11,
198 -13, 1, 3, 5, 7, 9, 11, 13, 15, -1, -3, -5, -7, -9,
199 -11, -13, -15, 1, 3, 5, 7, 10, 12, 14, 16, -1, -3, -5,
200 -7, -10, -12, -14, -16, 1, 3, 6, 8, 11, 13, 16, 18,
201 -1, -3, -6, -8, -11, -13, -16, -18, 1, 4, 6, 9, 12,
202 15, 17, 20, -1, -4, -6, -9, -12, -15, -17, -20, 1, 4,
203 7, 10, 13, 16, 19, 22, -1, -4, -7, -10, -13, -16, -19,
204 -22, 1, 4, 8, 11, 14, 17, 21, 24, -1, -4, -8, -11, -14,
205 -17, -21, -24, 1, 5, 8, 12, 15, 19, 22, 26, -1, -5,
206 -8, -12, -15, -19, -22, -26, 2, 6, 10, 14, 18, 22, 26,
207 30, -2, -6, -10, -14, -18, -22, -26, -30, 2, 6, 10,
208 14, 19, 23, 27, 31, -2, -6, -10, -14, -19, -23, -27,
209 -31, 2, 7, 11, 16, 21, 26, 30, 35, -2, -7, -11, -16,
210 -21, -26, -30, -35, 2, 7, 13, 18, 23, 28, 34, 39, -2,
211 -7, -13, -18, -23, -28, -34, -39, 2, 8, 14, 20, 25,
212 31, 37, 43, -2, -8, -14, -20, -25, -31, -37, -43, 3,
213 9, 15, 21, 28, 34, 40, 46, -3, -9, -15, -21, -28, -34,
214 -40, -46, 3, 10, 17, 24, 31, 38, 45, 52, -3, -10, -17,
215 -24, -31, -38, -45, -52, 3, 11, 19, 27, 34, 42, 50,
216 58, -3, -11, -19, -27, -34, -42, -50, -58, 4, 12, 21,
217 29, 38, 46, 55, 63, -4, -12, -21, -29, -38, -46, -55,
218 -63, 4, 13, 23, 32, 41, 50, 60, 69, -4, -13, -23, -32,
219 -41, -50, -60, -69, 5, 15, 25, 35, 46, 56, 66, 76, -5,
220 -15, -25, -35, -46, -56, -66, -76, 5, 16, 28, 39, 50,
221 61, 73, 84, -5, -16, -28, -39, -50, -61, -73, -84, 6,
222 18, 31, 43, 56, 68, 81, 93, -6, -18, -31, -43, -56,
223 -68, -81, -93, 6, 20, 34, 48, 61, 75, 89, 103, -6, -20,
224 -34, -48, -61, -75, -89, -103, 7, 22, 37, 52, 67, 82,
225 97, 112, -7, -22, -37, -52, -67, -82, -97, -112, 8,
226 24, 41, 57, 74, 90, 107, 123, -8, -24, -41, -57, -74,
227 -90, -107, -123, 9, 27, 45, 63, 82, 100, 118, 136, -9,
228 -27, -45, -63, -82, -100, -118, -136, 10, 30, 50, 70,
229 90, 110, 130, 150, -10, -30, -50, -70, -90, -110, -130,
230 -150, 11, 33, 55, 77, 99, 121, 143, 165, -11, -33, -55,
231 -77, -99, -121, -143, -165, 12, 36, 60, 84, 109, 133,
232 157, 181, -12, -36, -60, -84, -109, -133, -157, -181,
233 13, 40, 66, 93, 120, 147, 173, 200, -13, -40, -66, -93,
234 -120, -147, -173, -200, 14, 44, 73, 103, 132, 162, 191,
235 221, -14, -44, -73, -103, -132, -162, -191, -221, 16,
236 48, 81, 113, 146, 178, 211, 243, -16, -48, -81, -113,
237 -146, -178, -211, -243, 17, 53, 89, 125, 160, 196, 232,
238 268, -17, -53, -89, -125, -160, -196, -232, -268, 19,
239 58, 98, 137, 176, 215, 255, 294, -19, -58, -98, -137,
240 -176, -215, -255, -294, 21, 64, 108, 151, 194, 237,
241 281, 324, -21, -64, -108, -151, -194, -237, -281, -324,
242 23, 71, 118, 166, 213, 261, 308, 356, -23, -71, -118,
243 -166, -213, -261, -308, -356, 26, 78, 130, 182, 235,
244 287, 339, 391, -26, -78, -130, -182, -235, -287, -339,
245 -391, 28, 86, 143, 201, 258, 316, 373, 431, -28, -86,
246 -143, -201, -258, -316, -373, -431, 31, 94, 158, 221,
247 284, 347, 411, 474, -31, -94, -158, -221, -284, -347,
248 -411, -474, 34, 104, 174, 244, 313, 383, 453, 523, -34,
249 -104, -174, -244, -313, -383, -453, -523, 38, 115, 191,
250 268, 345, 422, 498, 575, -38, -115, -191, -268, -345,
251 -422, -498, -575, 42, 126, 210, 294, 379, 463, 547,
252 631, -42, -126, -210, -294, -379, -463, -547, -631,
253 46, 139, 231, 324, 417, 510, 602, 695, -46, -139, -231,
254 -324, -417, -510, -602, -695, 51, 153, 255, 357, 459,
255 561, 663, 765, -51, -153, -255, -357, -459, -561, -663,
256 -765, 56, 168, 280, 392, 505, 617, 729, 841, -56, -168,
257 -280, -392, -505, -617, -729, -841, 61, 185, 308, 432,
258 555, 679, 802, 926, -61, -185, -308, -432, -555, -679,
259 -802, -926, 68, 204, 340, 476, 612, 748, 884, 1020,
260 -68, -204, -340, -476, -612, -748, -884, -1020, 74,
261 224, 373, 523, 672, 822, 971, 1121, -74, -224, -373,
262 -523, -672, -822, -971, -1121, 82, 246, 411, 575, 740,
263 904, 1069, 1233, -82, -246, -411, -575, -740, -904,
264 -1069, -1233, 90, 271, 452, 633, 814, 995, 1176, 1357,
265 -90, -271, -452, -633, -814, -995, -1176, -1357, 99,
266 298, 497, 696, 895, 1094, 1293, 1492, -99, -298, -497,
267 -696, -895, -1094, -1293, -1492, 109, 328, 547, 766,
268 985, 1204, 1423, 1642, -109, -328, -547, -766, -985,
269 -1204, -1423, -1642, 120, 361, 601, 842, 1083, 1324,
270 1564, 1805, -120, -361, -601, -842, -1083, -1324, -1564,
271 -1805, 132, 397, 662, 927, 1192, 1457, 1722, 1987, -132,
272 -397, -662, -927, -1192, -1457, -1722, -1987, 145, 437,
273 728, 1020, 1311, 1603, 1894, 2186, -145, -437, -728,
274 -1020, -1311, -1603, -1894, -2186, 160, 480, 801, 1121,
275 1442, 1762, 2083, 2403, -160, -480, -801, -1121, -1442,
276 -1762, -2083, -2403, 176, 529, 881, 1234, 1587, 1940,
277 2292, 2645, -176, -529, -881, -1234, -1587, -1940, -2292,
278 -2645, 194, 582, 970, 1358, 1746, 2134, 2522, 2910,
279 -194, -582, -970, -1358, -1746, -2134, -2522, -2910,
280 213, 640, 1066, 1493, 1920, 2347, 2773, 3200, -213,
281 -640, -1066, -1493, -1920, -2347, -2773, -3200, 234,
282 704, 1173, 1643, 2112, 2582, 3051, 3521, -234, -704,
283 -1173, -1643, -2112, -2582, -3051, -3521, 258, 774,
284 1291, 1807, 2324, 2840, 3357, 3873, -258, -774, -1291,
285 -1807, -2324, -2840, -3357, -3873, 284, 852, 1420, 1988,
286 2556, 3124, 3692, 4260, -284, -852, -1420, -1988, -2556,
287 -3124, -3692, -4260, 312, 937, 1561, 2186, 2811, 3436,
288 4060, 4685, -312, -937, -1561, -2186, -2811, -3436,
289 -4060, -4685, 343, 1030, 1718, 2405, 3092, 3779, 4467,
290 5154, -343, -1030, -1718, -2405, -3092, -3779, -4467,
291 -5154, 378, 1134, 1890, 2646, 3402, 4158, 4914, 5670,
292 -378, -1134, -1890, -2646, -3402, -4158, -4914, -5670,
293 415, 1247, 2079, 2911, 3742, 4574, 5406, 6238, -415,
294 -1247, -2079, -2911, -3742, -4574, -5406, -6238, 457,
295 1372, 2287, 3202, 4117, 5032, 5947, 6862, -457, -1372,
296 -2287, -3202, -4117, -5032, -5947, -6862, 503, 1509,
297 2516, 3522, 4529, 5535, 6542, 7548, -503, -1509, -2516,
298 -3522, -4529, -5535, -6542, -7548, 553, 1660, 2767,
299 3874, 4981, 6088, 7195, 8302, -553, -1660, -2767, -3874,
300 -4981, -6088, -7195, -8302, 608, 1826, 3044, 4262, 5479,
301 6697, 7915, 9133, -608, -1826, -3044, -4262, -5479,
302 -6697, -7915, -9133, 669, 2009, 3348, 4688, 6027, 7367,
303 8706, 10046, -669, -2009, -3348, -4688, -6027, -7367,
304 -8706, -10046, 736, 2210, 3683, 5157, 6630, 8104, 9577,
305 11051, -736, -2210, -3683, -5157, -6630, -8104, -9577,
306 -11051, 810, 2431, 4052, 5673, 7294, 8915, 10536, 12157,
307 -810, -2431, -4052, -5673, -7294, -8915, -10536, -12157,
308 891, 2674, 4457, 6240, 8023, 9806, 11589, 13372, -891,
309 -2674, -4457, -6240, -8023, -9806, -11589, -13372, 980,
310 2941, 4903, 6864, 8825, 10786, 12748, 14709, -980, -2941,
311 -4903, -6864, -8825, -10786, -12748, -14709, 1078, 3236,
312 5393, 7551, 9708, 11866, 14023, 16181, -1078, -3236,
313 -5393, -7551, -9708, -11866, -14023, -16181, 1186, 3559,
314 5933, 8306, 10679, 13052, 15426, 17799, -1186, -3559,
315 -5933, -8306, -10679, -13052, -15426, -17799, 1305,
316 3915, 6526, 9136, 11747, 14357, 16968, 19578, -1305,
317 -3915, -6526, -9136, -11747, -14357, -16968, -19578,
318 1435, 4307, 7179, 10051, 12922, 15794, 18666, 21538,
319 -1435, -4307, -7179, -10051, -12922, -15794, -18666,
320 -21538, 1579, 4738, 7896, 11055, 14214, 17373, 20531,
321 23690, -1579, -4738, -7896, -11055, -14214, -17373,
322 -20531, -23690, 1737, 5212, 8686, 12161, 15636, 19111,
323 22585, 26060, -1737, -5212, -8686, -12161, -15636, -19111,
324 -22585, -26060, 1911, 5733, 9555, 13377, 17200, 21022,
325 24844, 28666, -1911, -5733, -9555, -13377, -17200, -21022,
326 -24844, -28666, 2102, 6306, 10511, 14715, 18920, 23124,
327 27329, 31533, -2102, -6306, -10511, -14715, -18920,
328 -23124, -27329, -31533, 2312, 6937, 11562, 16187, 20812,
329 25437, 30062, 32767, -2312, -6937, -11562, -16187, -20812,
330 -25437, -30062, -32767, 2543, 7631, 12718, 17806, 22893,
331 27981, 32767, 32767, -2543, -7631, -12718, -17806, -22893,
332 -27981, -32767, -32767, 2798, 8394, 13990, 19586, 25183,
333 30779, 32767, 32767, -2798, -8394, -13990, -19586, -25183,
334 -30779, -32767, -32767, 3077, 9233, 15389, 21545, 27700,
335 32767, 32767, 32767, -3077, -9233, -15389, -21545, -27700,
336 -32767, -32767, -32767, 3385, 10157, 16928, 23700, 30471,
337 32767, 32767, 32767, -3385, -10157, -16928, -23700,
338 -30471, -32767, -32767, -32767, 3724, 11172, 18621,
339 26069, 32767, 32767, 32767, 32767, -3724, -11172, -18621,
340 -26069, -32767, -32767, -32767, -32767, 4095, 12287,
341 20479, 28671, 32767, 32767, 32767, 32767, -4095, -12287,
342 -20479, -28671, -32767, -32767, -32767, -32767
343 };
344
345 #pragma endregion
346
347 // Last Express ADPCM is similar to MS IMA mono, but inverts its nibbles
348 // and does not have the 4 byte per channel requirement
349
350 class LastExpress_ADPCMStream : public Audio::ADPCMStream {
351 public:
LastExpress_ADPCMStream(Common::SeekableReadStream * stream,DisposeAfterUse::Flag disposeAfterUse,uint32 size,uint32 blockSize,uint32 volume,bool looped)352 LastExpress_ADPCMStream(Common::SeekableReadStream *stream, DisposeAfterUse::Flag disposeAfterUse, uint32 size, uint32 blockSize, uint32 volume, bool looped) :
353 Audio::ADPCMStream(stream, disposeAfterUse, size, 44100, 1, blockSize) {
354 _currentVolume = 0;
355 _nextVolume = volume;
356 _smoothChangeTarget = volume;
357 _volumeHoldBlocks = 0;
358 _running = true;
359 _looped = looped;
360 }
361
endOfData() const362 bool endOfData() const override {
363 return !_running || (!_looped && Audio::ADPCMStream::endOfData());
364 }
365
seekToBlock(uint32 block)366 void seekToBlock(uint32 block) {
367 reset();
368 _stream->seek(_startpos + _blockAlign * block);
369 }
370
readBuffer(int16 * buffer,const int numSamples)371 int readBuffer(int16 *buffer, const int numSamples) override {
372 int samples = 0;
373 // Temporary data
374 int step = 0;
375 int sample = 0;
376 byte idx = 0;
377
378 assert(numSamples % 2 == 0);
379
380 while (_running && samples < numSamples) {
381 if (Audio::ADPCMStream::endOfData()) {
382 if (!_looped)
383 break;
384 rewind();
385 }
386 if (_blockPos[0] == _blockAlign) {
387 // read block header
388 _status.ima_ch[0].last = _stream->readSint16LE();
389 _status.ima_ch[0].stepIndex = _stream->readSint16LE() << 6;
390 _blockPos[0] = 4;
391
392 // sanity check against broken stream
393 if ((unsigned)_status.ima_ch[0].stepIndex >= ARRAYSIZE(stepTable) * 4) {
394 // the original game sets kSoundFlagDecodeError here and stops playing
395 _status.ima_ch[0].stepIndex = 0;
396 }
397
398 if (!smoothVolumeChangeStep()) {
399 _running = false;
400 break;
401 }
402
403 // Get current volume
404 _currentVolume = _nextVolume;
405 }
406
407 for (; samples < numSamples && _blockPos[0] < _blockAlign && !_stream->eos() && _stream->pos() < _endpos; samples += 2) {
408 byte data = _stream->readByte();
409 _blockPos[0]++;
410
411 // First nibble
412 idx = data >> 4;
413 step = stepTable[idx + _status.ima_ch[0].stepIndex / 4];
414 sample = CLIP<int>(imaTable[idx + _status.ima_ch[0].stepIndex / 4] + _status.ima_ch[0].last, -32767, 32767);
415 buffer[samples] = (sample * _currentVolume) >> 4;
416
417 // Second nibble
418 idx = data & 0xF;
419 _status.ima_ch[0].stepIndex = stepTable[idx + step / 4];
420 _status.ima_ch[0].last = CLIP(imaTable[idx + step / 4] + sample, -32767, 32767);
421 buffer[samples + 1] = (_status.ima_ch[0].last * _currentVolume) >> 4;
422 }
423 }
424
425 return samples;
426 }
427
setVolume(uint32 newVolume)428 void setVolume(uint32 newVolume) { _smoothChangeTarget = _nextVolume = newVolume; }
setVolumeSmoothly(uint32 newVolume)429 void setVolumeSmoothly(uint32 newVolume) { _smoothChangeTarget = newVolume; }
430
431 private:
432 uint32 _currentVolume;
433 uint32 _nextVolume;
434 uint32 _smoothChangeTarget;
435 uint32 _volumeHoldBlocks; // smooth change of volume keeps volume on hold for 4 blocks = 133ms for every value; this is the counter
436 bool _running, _looped;
437
smoothVolumeChangeStep()438 bool smoothVolumeChangeStep() {
439 if (_nextVolume == _smoothChangeTarget)
440 return true; // the original game clears kSoundFlagVolumeChanging here
441 if (_volumeHoldBlocks <= 3) {
442 _volumeHoldBlocks++;
443 return true;
444 }
445 if (_nextVolume < _smoothChangeTarget)
446 ++_nextVolume;
447 else
448 --_nextVolume;
449 _volumeHoldBlocks = 0;
450 return (_nextVolume != 0);
451 }
452 };
453
454 //////////////////////////////////////////////////////////////////////////
455 // Sound
456 //////////////////////////////////////////////////////////////////////////
SimpleSound()457 SimpleSound::SimpleSound() : _size(0), _blocks(0), _blockSize(0) {}
458
~SimpleSound()459 SimpleSound::~SimpleSound() {
460 }
461
462 // Stop the sound
stop() const463 void SimpleSound::stop() const {
464 g_system->getMixer()->stopHandle(_handle);
465 }
466
loadHeader(Common::SeekableReadStream * in)467 void SimpleSound::loadHeader(Common::SeekableReadStream *in) {
468 _size = in->readUint32LE();
469 _blocks = in->readUint16LE();
470 debugC(5, kLastExpressDebugSound, " sound header data: size=\"%d\", %d blocks", _size, _blocks);
471
472 assert (_size % _blocks == 0);
473 _blockSize = _size / _blocks;
474 }
475
makeDecoder(Common::SeekableReadStream * in,uint32 size,uint32 volume,bool looped) const476 LastExpress_ADPCMStream *SimpleSound::makeDecoder(Common::SeekableReadStream *in, uint32 size, uint32 volume, bool looped) const {
477 return new LastExpress_ADPCMStream(in, DisposeAfterUse::YES, size, _blockSize, volume, looped);
478 }
479
play(Audio::AudioStream * as,DisposeAfterUse::Flag autofreeStream)480 void SimpleSound::play(Audio::AudioStream *as, DisposeAfterUse::Flag autofreeStream) {
481 g_system->getMixer()->playStream(Audio::Mixer::kPlainSoundType, &_handle, as,
482 -1, Audio::Mixer::kMaxChannelVolume, 0, autofreeStream);
483 }
484
getTimeMS()485 uint32 SimpleSound::getTimeMS() {
486 return g_system->getMixer()->getSoundElapsedTime(_handle);
487 }
488
489 //////////////////////////////////////////////////////////////////////////
490 // StreamedSound
491 //////////////////////////////////////////////////////////////////////////
StreamedSound()492 StreamedSound::StreamedSound() : _as(NULL), _loaded(false) {}
493
~StreamedSound()494 StreamedSound::~StreamedSound() {
495 stop(); // should execute before disposal of _as, so don't move in ~SimpleSound
496 delete _as;
497 _as = NULL;
498 }
499
load(Common::SeekableReadStream * stream,uint32 volume,bool looped,uint32 startBlock)500 bool StreamedSound::load(Common::SeekableReadStream *stream, uint32 volume, bool looped, uint32 startBlock) {
501 if (!stream)
502 return false;
503
504 g_system->getMixer()->stopHandle(_handle);
505
506 loadHeader(stream);
507
508 if (_as) {
509 stop();
510 delete _as;
511 }
512 // Start decoding the input stream
513 _as = makeDecoder(stream, _size, volume, looped);
514 if (startBlock)
515 _as->seekToBlock(startBlock);
516
517 // Start playing the decoded audio stream
518 play(_as, DisposeAfterUse::NO);
519
520 _loaded = true;
521
522 return true;
523 }
524
isFinished()525 bool StreamedSound::isFinished() {
526 if (!_loaded)
527 return false;
528
529 return !g_system->getMixer()->isSoundHandleActive(_handle);
530 }
531
setVolume(uint32 newVolume)532 void StreamedSound::setVolume(uint32 newVolume) {
533 if (!_as)
534 return;
535
536 _as->setVolume(newVolume);
537 }
538
setVolumeSmoothly(uint32 newVolume)539 void StreamedSound::setVolumeSmoothly(uint32 newVolume) {
540 if (!_as)
541 return;
542
543 _as->setVolumeSmoothly(newVolume);
544 }
545
546 //////////////////////////////////////////////////////////////////////////
547 // StreamedSound
548 //////////////////////////////////////////////////////////////////////////
AppendableSound()549 AppendableSound::AppendableSound() : SimpleSound() {
550 // Create an audio stream where the decoded chunks will be appended
551 _as = Audio::makeQueuingAudioStream(44100, false);
552 _finished = false;
553
554 // Start playing the decoded audio stream
555 play(_as, DisposeAfterUse::YES);
556
557 // Initialize the block size
558 // TODO: get it as an argument?
559 _blockSize = 739;
560 }
561
~AppendableSound()562 AppendableSound::~AppendableSound() {
563 finish();
564 stop();
565
566 _as = NULL;
567 }
568
queueBuffer(const byte * data,uint32 size)569 void AppendableSound::queueBuffer(const byte *data, uint32 size) {
570 Common::MemoryReadStream *buffer = new Common::MemoryReadStream(data, size);
571 queueBuffer(buffer);
572 }
573
queueBuffer(Common::SeekableReadStream * bufferIn)574 void AppendableSound::queueBuffer(Common::SeekableReadStream *bufferIn) {
575 if (!_as)
576 error("[AppendableSound::queueBuffer] Audio stream is invalid");
577
578 // Setup the ADPCM decoder
579 uint32 sizeIn = (uint32)bufferIn->size();
580 LastExpress_ADPCMStream *adpcm = makeDecoder(bufferIn, sizeIn, kVolumeFull, false);
581
582 // Queue the stream
583 _as->queueAudioStream(adpcm);
584 }
585
finish()586 void AppendableSound::finish() {
587 if (!_as)
588 error("[AppendableSound::finish] Audio stream is invalid");
589
590 if (!_finished)
591 _as->finish();
592
593 _finished = true;
594 }
595
isFinished()596 bool AppendableSound::isFinished() {
597 return _as->endOfStream();
598 }
599
600 } // End of namespace LastExpress
601