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 #include "audio/audiostream.h"
24 #include "audio/decoders/raw.h"
25 #include "audio/mixer.h"
26 #include "audio/mods/protracker.h"
27 #include "common/memstream.h"
28 #include "common/system.h"
29 #include "graphics/cursorman.h"
30 #include "graphics/palette.h"
31 
32 #include "supernova/graphics.h"
33 #include "supernova/resman.h"
34 #include "supernova/screen.h"
35 #include "supernova/supernova.h"
36 
37 namespace Supernova {
38 
39 struct AudioInfo {
40 	int _filenumber;
41 	int _offsetStart;
42 	int _offsetEnd;
43 };
44 
45 static Common::MemoryReadStream *convertToMod(const char *filename, int version = 1);
46 
47 static const AudioInfo audioInfo1[] = {
48 	{44,     0,    -1},
49 	{45,     0,    -1},
50 	{46,     0,  2510},
51 	{46,  2510,  4020},
52 	{46,  4020,    -1},
53 	{47,     0, 24010},
54 	{47, 24010,    -1},
55 	{48,     0,  2510},
56 	{48,  2510, 10520},
57 	{48, 10520, 13530},
58 	{48, 13530,    -1},
59 	{50,     0, 12786},
60 	{50, 12786,    -1},
61 	{51,     0,    -1},
62 	{53,     0,    -1},
63 	{54,     0,  8010},
64 	{54,  8010, 24020},
65 	{54, 24020, 30030},
66 	{54, 30030, 31040},
67 	{54, 31040,    -1},
68 };
69 
70 static const AudioInfo audioInfo2[] = {
71 	{55,     18230,    -1},
72 	{47,     0,     16010},
73 	{47,     16010, 17020},
74 	{49,     8010,     -1},
75 	{49,     0,      8010},
76 	{53,     30020,    -1},
77 	{55,     7010,  17020},
78 	{55,     0,      7010},
79 	{53,     5010,  30020},
80 	{55,     18230,    -1},
81 	{55,     17020, 18230},
82 	{53,     0,      5010},
83 	{47,     17020,    -1},
84 	{51,     9020,     -1},
85 	{51,     0,      6010},
86 	{50,     0,        -1},
87 	{51,     6010,   9020},
88 	{54,     0,        -1},
89 	{48,     0,        -1}
90 };
91 
92 static const byte mouseNormal[64] = {
93 	0xff,0x3f,0xff,0x1f,0xff,0x0f,0xff,0x07,
94 	0xff,0x03,0xff,0x01,0xff,0x00,0x7f,0x00,
95 	0x3f,0x00,0x1f,0x00,0x0f,0x00,0x0f,0x00,
96 	0xff,0x00,0x7f,0x18,0x7f,0x38,0x7f,0xfc,
97 
98 	0x00,0x00,0x00,0x40,0x00,0x60,0x00,0x70,
99 	0x00,0x78,0x00,0x7c,0x00,0x7e,0x00,0x7f,
100 	0x80,0x7f,0xc0,0x7f,0xe0,0x7f,0x00,0x7e,
101 	0x00,0x66,0x00,0x43,0x00,0x03,0x00,0x00
102 };
103 
104 static const byte mouseWait[64] = {
105 	0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x80,
106 	0x01,0x80,0x01,0x80,0x11,0x88,0x31,0x8c,
107 	0x31,0x8c,0x11,0x88,0x01,0x80,0x01,0x80,
108 	0x01,0x80,0x00,0x00,0x00,0x00,0x00,0x00,
109 
110 	0x00,0x00,0xfe,0x7f,0xf4,0x2f,0xf4,0x2f,
111 	0x14,0x28,0x24,0x24,0x44,0x22,0x84,0x21,
112 	0x84,0x21,0xc4,0x23,0xe4,0x27,0x74,0x2e,
113 	0x34,0x2c,0x14,0x28,0xfe,0x7f,0x00,0x00
114 };
115 
116 
ResourceManager(SupernovaEngine * vm)117 ResourceManager::ResourceManager(SupernovaEngine *vm)
118 	: _audioRate(11931)
119 	, _vm(vm) {
120 	if (_vm->_MSPart == 1)
121 		_soundSamples = new Common::ScopedPtr<Audio::SeekableAudioStream>[kAudioNumSamples1];
122 	else if (_vm->_MSPart == 2)
123 		_soundSamples = new Common::ScopedPtr<Audio::SeekableAudioStream>[kAudioNumSamples2];
124 	initGraphics();
125 }
126 
~ResourceManager()127 ResourceManager::~ResourceManager() {
128 	if (_vm->_MSPart == 1) {
129 		for (int i = 0; i < 44; i++)
130 			delete _images[i];
131 	}
132 	if (_vm->_MSPart == 2) {
133 		for (int i = 0; i < 47; i++)
134 			delete _images[i];
135 	}
136 	delete[] _soundSamples;
137 	delete[] _images;
138 }
139 
initGraphics()140 void ResourceManager::initGraphics() {
141 	Screen::initPalette();
142 	initCursorGraphics();
143 	if (_vm->_MSPart == 1)
144 		initImages1();
145 	else if (_vm->_MSPart == 2)
146 		initImages2();
147 }
148 
initCursorGraphics()149 void ResourceManager::initCursorGraphics() {
150 	const uint16 *bufferNormal = reinterpret_cast<const uint16 *>(mouseNormal);
151 	const uint16 *bufferWait = reinterpret_cast<const uint16 *>(mouseWait);
152 	for (uint i = 0; i < sizeof(mouseNormal) / 4; ++i) {
153 		for (uint bit = 0; bit < 16; ++bit) {
154 			uint mask = 0x8000 >> bit;
155 			uint bitIndex = i * 16 + bit;
156 
157 			_cursorNormal[bitIndex] = (READ_LE_UINT16(bufferNormal + i) & mask) ?
158 										 kColorCursorTransparent : kColorBlack;
159 			if (READ_LE_UINT16(bufferNormal + i + 16) & mask)
160 				_cursorNormal[bitIndex] = kColorLightRed;
161 
162 			_cursorWait[bitIndex] = (READ_LE_UINT16(bufferWait + i) & mask) ?
163 									   kColorCursorTransparent : kColorBlack;
164 			if (READ_LE_UINT16(bufferWait + i + 16) & mask)
165 				_cursorWait[bitIndex] = kColorLightRed;
166 		}
167 	}
168 }
169 
initImages1()170 void ResourceManager::initImages1() {
171 	_images = new MSNImage *[kNumImageFiles1];
172 	for (int i = 0; i < kNumImageFiles1; ++i) {
173 		_images[i] = nullptr;
174 	}
175 }
176 
initImages2()177 void ResourceManager::initImages2() {
178 	_images = new MSNImage *[kNumImageFiles2];
179 	for (int i = 0; i < kNumImageFiles2; ++i) {
180 		_images[i] = nullptr;
181 	}
182 }
183 
184 // Sound
185 // Note:
186 //   - samples start with a header of 6 bytes: 01 SS SS 00 AD 00
187 //     where SS SS (LE uint16) is the size of the sound sample + 2
188 //   - samples end with a footer of 4 bytes: 00 00
189 // Skip those in the buffer
loadSound1(AudioId id)190 void ResourceManager::loadSound1(AudioId id) {
191 	Common::File file;
192 	if (!file.open(Common::String::format("msn_data.%03d", audioInfo1[id]._filenumber))) {
193 		error("File %s could not be read!", file.getName());
194 	}
195 
196 	int length = 0;
197 	byte *buffer = nullptr;
198 
199 	if (audioInfo1[id]._offsetEnd == -1) {
200 		file.seek(0, SEEK_END);
201 		length = file.pos() - audioInfo1[id]._offsetStart - 10;
202 	} else {
203 		length = audioInfo1[id]._offsetEnd - audioInfo1[id]._offsetStart - 10;
204 	}
205 	buffer = new byte[length];
206 	file.seek(audioInfo1[id]._offsetStart + 6);
207 	file.read(buffer, length);
208 	file.close();
209 
210 	byte streamFlag = Audio::FLAG_UNSIGNED | Audio::FLAG_LITTLE_ENDIAN;
211 	_soundSamples[id].reset(Audio::makeRawStream(buffer, length, _audioRate,
212 												streamFlag, DisposeAfterUse::YES));
213 }
214 
loadSound2(AudioId id)215 void ResourceManager::loadSound2(AudioId id) {
216 	Common::File file;
217 	if (!file.open(Common::String::format("ms2_data.%03d", audioInfo2[id]._filenumber))) {
218 		error("File %s could not be read!", file.getName());
219 	}
220 
221 	int length = 0;
222 	byte *buffer = nullptr;
223 
224 	if (audioInfo2[id]._offsetEnd == -1) {
225 		file.seek(0, SEEK_END);
226 		length = file.pos() - audioInfo2[id]._offsetStart - 10;
227 	} else {
228 		length = audioInfo2[id]._offsetEnd - audioInfo2[id]._offsetStart - 10;
229 	}
230 	buffer = new byte[length];
231 	file.seek(audioInfo2[id]._offsetStart + 6);
232 	file.read(buffer, length);
233 	file.close();
234 
235 	byte streamFlag = Audio::FLAG_UNSIGNED | Audio::FLAG_LITTLE_ENDIAN;
236 	_soundSamples[id].reset(Audio::makeRawStream(buffer, length, _audioRate,
237 												streamFlag, DisposeAfterUse::YES));
238 }
239 
loadImage(int filenumber)240 void ResourceManager::loadImage(int filenumber) {
241 	if (_vm->_MSPart == 1) {
242 		if (filenumber < 44) {
243 			_images[filenumber] = new MSNImage(_vm);
244 			if (!_images[filenumber]->init(filenumber))
245 				error("Failed reading image file msn_data.%03d", filenumber);
246 		} else {
247 			_images[44] = new MSNImage(_vm);
248 			if (!_images[44]->init(filenumber))
249 				error("Failed reading image file msn_data.%03d", filenumber);
250 		}
251 	} else if (_vm->_MSPart == 2) {
252 		_images[filenumber] = new MSNImage(_vm);
253 		if (!_images[filenumber]->init(filenumber))
254 			error("Failed reading image file ms2_data.%03d", filenumber);
255 	}
256 }
257 
getSoundStream(AudioId index)258 Audio::SeekableAudioStream *ResourceManager::getSoundStream(AudioId index) {
259 	if (!_soundSamples[index]) {
260 		if (_vm->_MSPart == 1)
261 			loadSound1(index);
262 		else if (_vm->_MSPart == 2)
263 			loadSound2(index);
264 	}
265 	Audio::SeekableAudioStream *stream;
266 	stream = _soundSamples[index].get();
267 	stream->rewind();
268 
269 	return stream;
270 }
271 
getSoundStream(MusicId index)272 Audio::AudioStream *ResourceManager::getSoundStream(MusicId index) {
273 	switch (index) {
274 	case kMusicIntro:
275 		if (!_musicIntroBuffer) {
276 			if (_vm->_MSPart == 1)
277 				_musicIntroBuffer.reset(convertToMod("msn_data.052", 1));
278 			else if (_vm->_MSPart == 2)
279 				_musicIntroBuffer.reset(convertToMod("ms2_data.052", 2));
280 		}
281 		_musicIntro.reset(Audio::makeProtrackerStream(_musicIntroBuffer.get()));
282 		return _musicIntro.get();
283 	case kMusicMadMonkeys:
284 		// fall through
285 	case kMusicOutro:
286 		if (!_musicOutroBuffer) {
287 			if (_vm->_MSPart == 1)
288 				_musicOutroBuffer.reset(convertToMod("msn_data.049", 1));
289 			else if (_vm->_MSPart == 2)
290 				_musicOutroBuffer.reset(convertToMod("ms2_data.056", 2));
291 		}
292 		_musicOutro.reset(Audio::makeProtrackerStream(_musicOutroBuffer.get()));
293 		return _musicOutro.get();
294 	default:
295 		error("Invalid music constant in playAudio()");
296 	}
297 }
298 
getSirenStream()299 Audio::AudioStream *ResourceManager::getSirenStream() {
300 	if (!_sirenStream)
301 		initSiren();
302 	return _sirenStream.get();
303 }
304 
getImage(int filenumber)305 MSNImage *ResourceManager::getImage(int filenumber) {
306 	//check array boundaries
307 	if (_vm->_MSPart == 1 && filenumber > 43 && filenumber != 55)
308 		return nullptr;
309 	if (_vm->_MSPart == 2 && filenumber > 46)
310 		return nullptr;
311 
312 	if (filenumber == 55) {
313 		if (!_images[44])
314 			loadImage(filenumber);
315 		return _images[44];
316 	} else {
317 		if (!_images[filenumber])
318 			loadImage(filenumber);
319 		return _images[filenumber];
320 	}
321 }
322 
323 // Generate a tone which minimal length is the length and ends at the end
324 // of sine period
325 // NOTE: Size of the SineTable has to be the same as audioRate and a multiple of 4
generateTone(byte * buffer,int frequency,int length,int audioRate,Common::SineTable & table)326 byte *ResourceManager::generateTone(byte *buffer, int frequency, int length, int audioRate, Common::SineTable &table) {
327 	int i = 0;
328 
329 	// Make sure length is a multiple of audioRate / frequency to end on a full sine wave and not in the middle.
330 	// Also the length we have is a minimum length, so only increase it.
331 	int r = 1 + (length - 1) * frequency / audioRate;
332 	length = (1 + 2 * r * audioRate / frequency) / 2;
333 	for(; i < length; i++) {
334 		buffer[i] = (byte)
335 			((table.at((i * frequency) % audioRate) * 127) + 127);
336 	}
337 	return buffer + length;
338 }
339 
340 // Tones with frequencies between 1500 Hz and 1800 Hz, frequencies go up and down
341 // with a step of 10 Hz.
initSiren()342 void ResourceManager::initSiren() {
343 	int audioRate = 44000;
344 	int length = audioRate / 90; // minimal length of each tone
345 
346 	// * 60 for the minimal length, another 20 * length as a spare, for longer tones
347 	byte *buffer = new byte[length * 80];
348 	byte *pBuffer = buffer;
349 	Common::SineTable table(audioRate);
350 
351 	for (int i = 0; i < 30; i++)
352 		pBuffer = generateTone(pBuffer, 1800 - i * 10, length, audioRate, table);
353 
354 	for (int i = 0; i < 30; i++)
355 		pBuffer = generateTone(pBuffer, 1500 + i * 10, length, audioRate, table);
356 
357 	byte streamFlag = Audio::FLAG_UNSIGNED;
358 
359 	_sirenStream.reset(Audio::makeLoopingAudioStream(
360 			Audio::makeRawStream(buffer, pBuffer - buffer, audioRate,
361 									streamFlag, DisposeAfterUse::YES), 0));
362 }
363 
convertToMod(const char * filename,int version)364 static Common::MemoryReadStream *convertToMod(const char *filename, int version) {
365 	// MSN format
366 	struct {
367 		uint16 seg;
368 		uint16 start;
369 		uint16 end;
370 		uint16 loopStart;
371 		uint16 loopEnd;
372 		char volume;
373 		char dummy[5];
374 	} instr2[22];
375 	int nbInstr2; // 22 for version1, 15 for version 2
376 	int16 songLength;
377 	char arrangement[128];
378 	int16 patternNumber;
379 	int32 note2[28][64][4];
380 
381 	nbInstr2 = ((version == 1) ? 22 : 15);
382 
383 	Common::File msnFile;
384 	msnFile.open(filename);
385 	if (!msnFile.isOpen()) {
386 		warning("Data file '%s' not found", msnFile.getName());
387 		return nullptr;
388 	}
389 
390 	for (int i = 0 ; i < nbInstr2 ; ++i) {
391 		instr2[i].seg = msnFile.readUint16LE();
392 		instr2[i].start = msnFile.readUint16LE();
393 		instr2[i].end = msnFile.readUint16LE();
394 		instr2[i].loopStart = msnFile.readUint16LE();
395 		instr2[i].loopEnd = msnFile.readUint16LE();
396 		instr2[i].volume = msnFile.readByte();
397 		msnFile.read(instr2[i].dummy, 5);
398 	}
399 	songLength = msnFile.readSint16LE();
400 	msnFile.read(arrangement, 128);
401 	patternNumber = msnFile.readSint16LE();
402 	for (int p = 0 ; p < patternNumber ; ++p) {
403 		for (int n = 0 ; n < 64 ; ++n) {
404 			for (int k = 0 ; k < 4 ; ++k) {
405 				note2[p][n][k] = msnFile.readSint32LE();
406 			}
407 		}
408 	}
409 
410 	/* MOD format */
411 	struct {
412 		char iname[22];
413 		uint16 length;
414 		char finetune;
415 		char volume;
416 		uint16 loopStart;
417 		uint16 loopLength;
418 	} instr[31];
419 	int32 note[28][64][4];
420 
421 	// We can't recover some MOD effects since several of them are mapped to 0.
422 	// Assume the MSN effect of value 0 is Arpeggio (MOD effect of value 0).
423 	const char invConvEff[8] = {0, 1, 2, 3, 10, 12, 13 ,15};
424 
425 	// Reminder from convertToMsn
426 	// 31 30 29 28 27 26 25 24 - 23 22 21 20 19 18 17 16 - 15 14 13 12 11 10 09 08 - 07 06 05 04 03 02 01 00
427 	//  h  h  h  h  g  g  g  g    f  f  f  f  e  e  e  e    d  d  d  d  c  c  c  c    b  b  b  b  a  a  a  a
428 	//
429 	// MSN:
430 	//  hhhh             (4 bits) Cleared to 0
431 	//  dddd c           (5 bits) Sample index   | after mapping through convInstr
432 	//        ccc        (3 bits) Effect type    | after mapping through convEff
433 	//  bbbb aaaa        (8 bits) Effect value   | unmodified
434 	//  gggg ffff eeee  (12 bits) Sample period  | unmodified
435 	//
436 	// MS2:
437 	//  hhhh             (4 bits) Cleared to 0
438 	//  dddd             (4 bits) Sample index   | after mapping through convInstr
439 	//  cccc             (4 bits) Effect type    | unmodified
440 	//  bbbb aaaa        (8 bits) Effect value   | unmodified
441 	//  gggg ffff eeee  (12 bits) Sample period  | transformed (0xE000 / p) - 256
442 	//
443 	// MOD:
444 	//  hhhh dddd        (8 bits) Sample index
445 	//  cccc             (4 bits) Effect type for this channel/division
446 	//  bbbb aaaa        (8 bits) Effect value
447 	//  gggg ffff eeee  (12 bits) Sample period
448 
449 	// Can we recover the instruments mapping? I don't think so as part of the original instrument index is cleared.
450 	// And it doesn't really matter as long as we are consistent.
451 	// However we need to make sure 31 (or 15 in MS2) is mapped to 0 in MOD.
452 	// We just add 1 to all other values, and this means a 1 <-> 1 mapping for the instruments
453 	for (int p = 0; p < patternNumber; ++p) {
454 		for (int n = 0; n < 64; ++n) {
455 			for (int k = 0; k < 4; ++k) {
456 				int32* l = &(note[p][n][k]);
457 				*l = note2[p][n][k];
458 				int32 i = 0;
459 				if (nbInstr2 == 22) { // version 1
460 					i = ((*l & 0xF800) >> 11);
461 					int32 e = ((*l & 0x0700) >> 8);
462 					int32 e1 = invConvEff[e];
463 					*l &= 0x0FFF00FF;
464 					*l |= (e1 << 8);
465 				} else { // version 2
466 					int32 h = (*l >> 16);
467 					i = ((*l & 0xF000) >> 12);
468 					*l &= 0x00000FFF;
469 					if (h)
470 						h = 0xE000 / (h + 256);
471 					*l |= (h << 16);
472 					if (i == 15)
473 						i = 31;
474 				}
475 
476 				// Add back index in note
477 				if (i != 31) {
478 					++i;
479 					*l |= ((i & 0x0F) << 12);
480 					*l |= ((i & 0xF0) << 24);
481 				}
482 			}
483 		}
484 	}
485 
486 	for (int i = 0; i < 31; ++i) {
487 		// iname is not stored in the mod file. Just set it to 'instrument#'
488 		// finetune is not stored either. Assume 0.
489 		memset(instr[i].iname, 0, 22);
490 		sprintf(instr[i].iname, "instrument%d", i+1);
491 		instr[i].length = 0;
492 		instr[i].finetune = 0;
493 		instr[i].volume = 0;
494 		instr[i].loopStart = 0;
495 		instr[i].loopLength = 0;
496 
497 		if (i < nbInstr2) {
498 			instr[i].length = ((instr2[i].end - instr2[i].start) >> 1);
499 			instr[i].loopStart = ((instr2[i].loopStart - instr2[i].start) >> 1);
500 			instr[i].loopLength = (( instr2[i].loopEnd - instr2[i].loopStart) >> 1);
501 			instr[i].volume = instr2[i].volume;
502 		}
503 	}
504 
505 	// The ciaaSpeed is kind of useless and not present in the MSN file.
506 	// Traditionally 0x78 in SoundTracker. Was used in NoiseTracker as a restart point.
507 	// ProTracker uses 0x7F. FastTracker uses it as a restart point, whereas ScreamTracker 3 uses 0x7F like ProTracker.
508 	// You can use this to roughly detect which tracker made a MOD, and detection gets more accurate for more obscure MOD types.
509 	char ciaaSpeed = 0x7F;
510 
511 	// The mark cannot be recovered either. Since we have 4 channels and 31 instrument it can be either ID='M.K.' or ID='4CHN'.
512 	// Assume 'M.K.'
513 	const char mark[4] = { 'M', '.', 'K', '.' };
514 
515 	Common::MemoryWriteStreamDynamic buffer(DisposeAfterUse::NO);
516 
517 	buffer.write(msnFile.getName(), 19);
518 	buffer.writeByte(0);
519 
520 	for (int i = 0 ; i < 31 ; ++i) {
521 		buffer.write(instr[i].iname, 22);
522 		buffer.writeUint16BE(instr[i].length);
523 		buffer.writeByte(instr[i].finetune);
524 		buffer.writeByte(instr[i].volume);
525 		buffer.writeUint16BE(instr[i].loopStart);
526 		buffer.writeUint16BE(instr[i].loopLength);
527 	}
528 	buffer.writeByte((char)songLength);
529 	buffer.writeByte(ciaaSpeed);
530 	buffer.write(arrangement, 128);
531 	buffer.write(mark, 4);
532 
533 	for (int p = 0 ; p < patternNumber ; ++p) {
534 		for (int n = 0 ; n < 64 ; ++n) {
535 			for (int k = 0 ; k < 4 ; ++k) {
536 //				buffer.writeUint32BE(*((uint32*)(note[p][n]+k)));
537 				buffer.writeSint32BE(note[p][n][k]);
538 			}
539 		}
540 	}
541 
542 	uint nb;
543 	char buf[4096];
544 	while ((nb = msnFile.read(buf, 4096)) > 0)
545 		buffer.write(buf, nb);
546 
547 	return new Common::MemoryReadStream(buffer.getData(), buffer.size(), DisposeAfterUse::YES);
548 }
549 
getAudioRate()550 int ResourceManager::getAudioRate() {
551 	return _audioRate;
552 }
553 
getCursor(CursorId id) const554 const byte *ResourceManager::getCursor(CursorId id) const {
555 	switch (id) {
556 	case kCursorNormal:
557 		return _cursorNormal;
558 	case kCursorWait:
559 		return _cursorWait;
560 	default:
561 		return nullptr;
562 	}
563 }
564 
565 }
566