aboutsummaryrefslogtreecommitdiff
path: root/engines/titanic/sound/music_room_instrument.cpp
blob: b92329850bfb35d48a5840b73988c53ce20bbe41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "titanic/sound/music_room_instrument.h"
#include "titanic/sound/sound_manager.h"
#include "titanic/core/project_item.h"
#include "titanic/core/game_object.h"

namespace Titanic {

bool CMusicRoomInstrument::_pianoToggle;
int CMusicRoomInstrument::_pianoCtr;
int CMusicRoomInstrument::_bassCtr;
byte *CMusicRoomInstrument::_buffer;
double *CMusicRoomInstrument::_array;
int CMusicRoomInstrument::_arrayIndex;

void CMusicRoomInstrument::init() {
	_pianoToggle = false;
	_pianoCtr = 0;
	_bassCtr = 0;
	_buffer = nullptr;
	_array = nullptr;
	_arrayIndex = 0;
}

void CMusicRoomInstrument::deinit() {
	delete[] _buffer;
	delete[] _array;
	_buffer = nullptr;
}

CMusicRoomInstrument::CMusicRoomInstrument(CProjectItem *project, CSoundManager *soundManager, MusicWaveInstrument instrument) :
		_project(project), _soundManager(soundManager), _instrument(instrument) {
	Common::fill(&_gameObjects[0], &_gameObjects[4], (CGameObject *)nullptr);
	_animTime = 0.0;
	_waveIndex = -1;
	_readPos = 0;
	_readIncrement = 0;
	_size = 0;
	_count = 0;
	_field4C = 0;

	switch (instrument) {
	case MV_PIANO:
		_gameObjects[0] = static_cast<CGameObject *>(_project->findByName("Piano Man"));
		_gameObjects[1] = static_cast<CGameObject *>(_project->findByName("Piano Mouth"));
		_gameObjects[2] = static_cast<CGameObject *>(_project->findByName("Piano Left Arm"));
		_gameObjects[3] = static_cast<CGameObject *>(_project->findByName("Piano Right Arm"));
		_animTime = 0.45;
		break;

	case MV_BASS:
		_gameObjects[0] = static_cast<CGameObject *>(_project->findByName("Bass Player"));
		break;

	case MV_BELLS:
		_gameObjects[0] = static_cast<CGameObject *>(_project->findByName("Tubular Bells"));
		_animTime = 0.4;
		break;
	
	case MV_SNAKE:
		_gameObjects[0] = static_cast<CGameObject *>(_project->findByName("Snake_Hammer"));
		_gameObjects[1] = static_cast<CGameObject *>(_project->findByName("Snake_Glass"));
		_gameObjects[2] = static_cast<CGameObject *>(_project->findByName("Snake_Head"));
		_animTime = 0.17;
		break;
	}
}

void CMusicRoomInstrument::setFilesCount(uint count) {
	assert(_items.empty());
	_items.resize(count);
}

void CMusicRoomInstrument::load(int index, const CString &filename, int v3) {
	assert(!_items[index]._waveFile);
	_items[index]._waveFile = createWaveFile(filename);
	_items[index]._value = v3;
}

CWaveFile *CMusicRoomInstrument::createWaveFile(const CString &name) {
	if (name.empty())
		return nullptr;
	return _soundManager->loadSound(name);
}

void CMusicRoomInstrument::start() {
	if (_gameObjects[0]) {
		switch (_instrument) {
		case MV_PIANO:
			_gameObjects[0]->playMovie(0, 29, MOVIE_STOP_PREVIOUS);
			_gameObjects[2]->loadFrame(14);
			_gameObjects[3]->loadFrame(22);
			break;

		case MV_BELLS:
			_gameObjects[0]->loadFrame(0);
			_gameObjects[0]->movieSetAudioTiming(true);
			break;

		case MV_SNAKE:
			_field4C = 22;
			_gameObjects[1]->playMovie(0, 22, 0);
			_gameObjects[2]->playMovie(0, 35, MOVIE_STOP_PREVIOUS);
			_gameObjects[0]->playMovie(0, 1, MOVIE_STOP_PREVIOUS);
			_gameObjects[0]->playMovie(0, 1, 0);
			_gameObjects[0]->playMovie(0, 1, 0);
			_gameObjects[0]->playMovie(0, 1, 0);
			_gameObjects[0]->playMovie(0, 1, 0);
			break;

		default:
			break;
		}
	}
}

void CMusicRoomInstrument::stop() {
	if (_gameObjects[0]) {
		switch (_instrument) {
		case MV_PIANO:
			_gameObjects[1]->setVisible(false);
			_gameObjects[2]->setVisible(false);
			_gameObjects[3]->setVisible(false);
			_gameObjects[0]->playMovie(29, 58, MOVIE_STOP_PREVIOUS);
			break;

		case MV_BELLS:
			_gameObjects[0]->stopMovie();
			break;

		default:
			break;
		}
	}
}

void CMusicRoomInstrument::update(int val) {
	if (_gameObjects[0]) {
		switch (_instrument) {
		case MV_PIANO:
			_gameObjects[1]->setVisible(true);
			_gameObjects[2]->setVisible(true);
			_gameObjects[3]->setVisible(true);
			_gameObjects[_pianoToggle ? 3 : 2]->playMovie(MOVIE_STOP_PREVIOUS);
			_pianoToggle = !_pianoToggle;

			switch (_pianoCtr) {
			case 0:
				_gameObjects[1]->playMovie(0, 4, MOVIE_STOP_PREVIOUS);
				break;
			case 1:
				_gameObjects[1]->playMovie(4, 8, MOVIE_STOP_PREVIOUS);
				break;
			case 2:
				_gameObjects[1]->playMovie(8, 12, MOVIE_STOP_PREVIOUS);
				break;
			case 3:
				_gameObjects[1]->playMovie(12, 16, MOVIE_STOP_PREVIOUS);
				break;
			default:
				break;
			}

			_pianoCtr = (_pianoCtr + 1) % 4;
			break;

		case MV_BASS:
			switch (_bassCtr) {
			case 0:
				_gameObjects[0]->playMovie(0, 7, MOVIE_STOP_PREVIOUS);
				break;
			case 1:
				_gameObjects[0]->playMovie(7, 14, MOVIE_STOP_PREVIOUS);
				break;
			case 2:
				_gameObjects[0]->playMovie(15, 24, MOVIE_STOP_PREVIOUS);
				break;
			case 3:
				_gameObjects[0]->playMovie(25, 33, MOVIE_STOP_PREVIOUS);
				break;
			default:
				break;
			}

			// WORKAROUND: Original didn't change the selected bass animation
			_bassCtr = (_bassCtr + 1) % 4;
			break;

		case MV_BELLS:
			switch (val) {
			case 60:
				_gameObjects[0]->movieSetAudioTiming(true);
				_gameObjects[0]->playMovie(0, 512, MOVIE_STOP_PREVIOUS);
				_animTime = 0.6;
				break;

			case 62:
				_gameObjects[0]->playMovie(828, 1023, MOVIE_STOP_PREVIOUS);
				_animTime = 0.3;
				break;

			case 63:
				_gameObjects[0]->playMovie(1024, 1085, MOVIE_STOP_PREVIOUS);
				break;

			default:
				break;
			}
			break;

		case MV_SNAKE: {
			_gameObjects[0]->playMovie(0, 7, MOVIE_STOP_PREVIOUS);

			double tempVal = 46.0 - ((double)(val - 14) * 1.43);
			int frameNum = _field4C;
			int frameNum1 = (int)((tempVal - frameNum) * 0.25);
			_gameObjects[1]->playMovie(frameNum1, frameNum1, MOVIE_STOP_PREVIOUS);

			frameNum += frameNum1;
			_gameObjects[1]->playMovie(frameNum, frameNum, 0);

			frameNum += frameNum1;
			_gameObjects[1]->playMovie(frameNum, frameNum, 0);

			_gameObjects[2]->playMovie(45, 49, MOVIE_STOP_PREVIOUS);
			break;
		}

		default:
			break;
		}
	}
}

void CMusicRoomInstrument::clear() {
	_waveIndex = 0;
	_readPos = 0;
	_readIncrement = 0;
	_size = 0;
	_count = 0;
}

void CMusicRoomInstrument::reset(uint total) {
	_waveIndex = -1;
	_readPos = 0;
	_readIncrement = 0;
	_size = total;
	_count = 0;
}

int CMusicRoomInstrument::read(int16 *ptr, uint size) {
	if (!_size)
		return 0;

	if (size >= _size)
		size = _size;

	if (_waveIndex != -1) {
		// Lock the specified wave file for access
		const int16 *data = _items[_waveIndex]._waveFile->lock();
		assert(data);
		const int16 *src = data;

		// Loop through merging data from the wave file into the dest buffer
		for (uint idx = 0; idx < (size / sizeof(int16)); ++idx, _readPos += _readIncrement) {
			uint srcPos = _readPos >> 8;
			if (srcPos >= _count)
				break;

			int16 val = READ_LE_UINT16(src + srcPos);
			*ptr++ += val;
		}

		// Unlock the wave file
		_items[_waveIndex]._waveFile->unlock(data);
	}

	_size -= size;
	return size;
}

void CMusicRoomInstrument::chooseWaveFile(int index, int size) {
	if (!_array)
		setupArray(-36, 36);

	int minDiff = ABS(_items[0]._value - index);
	int waveIndex = 0;

	for (uint idx = 1; idx < _items.size(); ++idx) {
		int diff = ABS(_items[idx]._value - index);
		if (diff < minDiff) {
			minDiff = diff;
			waveIndex = idx;
		}
	}

	const CInstrumentWaveFile &wf = _items[waveIndex];
	int arrIndex = _arrayIndex - wf._value + index;
	uint waveSize = wf._waveFile->size();

	_waveIndex = waveIndex;
	_readPos = 0;
	_readIncrement = (int)(_array[arrIndex] * 256);
	_size = size;
	_count = waveSize / 2;
}

void CMusicRoomInstrument::setupArray(int minVal, int maxVal) {
	// Delete any prior array and recreate it
	delete[] _array;

	int arrSize = maxVal - minVal + 1;
	_array = new double[arrSize];
	_arrayIndex = ABS(minVal);

	// Setup array contents
	_array[_arrayIndex] = 1.0;

	double val = 1.0594634;
	for (int idx = 1; idx <= maxVal; ++idx) {
		_array[_arrayIndex + idx] = val;
		val *= 1.0594634;
	}

	val = 0.94387404038686;
	for (int idx = -1; idx >= minVal; --idx) {
		_array[_arrayIndex + idx] = val;
		val *= 0.94387404038686;
	}
}

} // End of namespace Titanic