aboutsummaryrefslogtreecommitdiff
path: root/engines/sword1/music.cpp
blob: 850a861f67cac621c7710153c670355caa803224 (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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* 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.
 *
 * $URL$
 * $Id$
 *
 */


#include "common/endian.h"
#include "common/file.h"
#include "common/util.h"
#include "common/system.h"

#include "sword1/sword1.h"
#include "sword1/music.h"

#include "sound/aiff.h"
#include "sound/flac.h"
#include "sound/mixer.h"
#include "sound/mp3.h"
#include "sound/vorbis.h"
#include "sound/wave.h"
#include "sound/vag.h"

#define SMP_BUFSIZE 8192

namespace Sword1 {

class BaseAudioStream : public Audio::AudioStream {
public:
	BaseAudioStream(Common::SeekableReadStream *source, bool loop);
	virtual ~BaseAudioStream();
	virtual int readBuffer(int16 *buffer, const int numSamples);
	virtual bool isStereo() const { return _isStereo; }
	virtual bool endOfData() const { return (_samplesLeft == 0); }
	virtual int getRate() const { return _rate; }
protected:
	Common::SeekableReadStream	*_sourceStream;
	uint8	*_sampleBuf;
	uint32	_rate;
	bool	_isStereo;
	uint32	_samplesLeft;
	uint16	_bitsPerSample;
	bool	_loop;

	virtual void rewind() = 0;
	virtual void reinit(int size, int rate, byte flags);
};

BaseAudioStream::BaseAudioStream(Common::SeekableReadStream *source, bool loop) {
	_sourceStream = source;
	_sampleBuf = (uint8*)malloc(SMP_BUFSIZE);

	_samplesLeft = 0;
	_isStereo = false;
	_bitsPerSample = 16;
	_rate = 22050;
	_loop = loop;
}

BaseAudioStream::~BaseAudioStream() {
	free(_sampleBuf);
}

void BaseAudioStream::reinit(int size, int rate, byte flags) {
	_isStereo = (flags & Audio::Mixer::FLAG_STEREO) != 0;
	_rate = rate;
	assert(size <= (_sourceStream->size() - _sourceStream->pos()));
	_bitsPerSample = ((flags & Audio::Mixer::FLAG_16BITS) != 0) ? 16 : 8;
	_samplesLeft = (size * 8) / _bitsPerSample;
	if ((_bitsPerSample != 16) && (_bitsPerSample != 8))
		error("BaseAudioStream: unknown sound type");
}

int BaseAudioStream::readBuffer(int16 *buffer, const int numSamples) {
	int retVal = 0;

	while (retVal < numSamples && _samplesLeft > 0) {
		int samples = MIN((int)_samplesLeft, numSamples - retVal);
		retVal += samples;
		_samplesLeft -= samples;
		while (samples > 0) {
			int readBytes = MIN(samples * (_bitsPerSample >> 3), SMP_BUFSIZE);
			_sourceStream->read(_sampleBuf, readBytes);
			if (_bitsPerSample == 16) {
				samples -= (readBytes / 2);
				memcpy(buffer, _sampleBuf, readBytes);
				buffer += (readBytes / 2);
			} else {
				samples -= readBytes;
				int8 *src = (int8*)_sampleBuf;
				while (readBytes--)
					*buffer++ = (int16)*src++ << 8;
			}
		}

		if (!_samplesLeft && _loop) {
			rewind();
		}
	}

	return retVal;
}

class WaveAudioStream : public BaseAudioStream {
public:
	WaveAudioStream(Common::SeekableReadStream *source, bool loop);
	virtual int readBuffer(int16 *buffer, const int numSamples);
private:
	virtual void rewind();
};

WaveAudioStream::WaveAudioStream(Common::SeekableReadStream *source, bool loop) : BaseAudioStream(source, loop) {
	rewind();

	if (_samplesLeft == 0)
		_loop = false;
}

void WaveAudioStream::rewind() {
	int rate, size;
	byte flags;

	_sourceStream->seek(0);

	if (Audio::loadWAVFromStream(*_sourceStream, size, rate, flags)) {
		reinit(size, rate, flags);
	}
}

int WaveAudioStream::readBuffer(int16 *buffer, const int numSamples) {
	int retVal = BaseAudioStream::readBuffer(buffer, numSamples);

	if (_bitsPerSample == 16) {
		for (int i = 0; i < retVal; i++) {
			buffer[i] = (int16)READ_LE_UINT16(buffer + i);
		}
	}

	return retVal;
}

class AiffAudioStream : public BaseAudioStream {
public:
	AiffAudioStream(Common::SeekableReadStream *source, bool loop);
	virtual int readBuffer(int16 *buffer, const int numSamples);
private:
	void rewind();
};

AiffAudioStream::AiffAudioStream(Common::SeekableReadStream *source, bool loop) : BaseAudioStream(source, loop) {
	rewind();

	if (_samplesLeft == 0)
		_loop = false;
}

void AiffAudioStream::rewind() {
	int rate, size;
	byte flags;

	_sourceStream->seek(0);

	if (Audio::loadAIFFFromStream(*_sourceStream, size, rate, flags)) {
		reinit(size, rate, flags);
	}
}

int AiffAudioStream::readBuffer(int16 *buffer, const int numSamples) {
	int retVal = BaseAudioStream::readBuffer(buffer, numSamples);

	if (_bitsPerSample == 16) {
		for (int i = 0; i < retVal; i++) {
			buffer[i] = (int16)READ_BE_UINT16(buffer + i);
		}
	}

	return retVal;
}

// This means fading takes 3 seconds.
#define FADE_LENGTH 3

// These functions are only called from Music, so I'm just going to
// assume that if locking is needed it has already been taken care of.

bool MusicHandle::play(const char *fileBase, bool loop) {
	char fileName[30];
	stop();

	// FIXME: How about using AudioStream::openStreamFile instead of the code below?
	// I.e.:
	//_audioSource = Audio::AudioStream::openStreamFile(fileBase, 0, 0, loop ? 0 : 1);

#ifdef USE_FLAC
	if (!_audioSource) {
		sprintf(fileName, "%s.flac", fileBase);
		if (_file.open(fileName))
			_audioSource = Audio::makeFlacStream(&_file, false, 0, 0, loop ? 0 : 1);
	}
	if (!_audioSource) {
		sprintf(fileName, "%s.fla", fileBase);
		if (_file.open(fileName))
			_audioSource = Audio::makeFlacStream(&_file, false, 0, 0, loop ? 0 : 1);
	}
#endif
#ifdef USE_VORBIS
	if (!_audioSource) {
		sprintf(fileName, "%s.ogg", fileBase);
		if (_file.open(fileName))
			_audioSource = Audio::makeVorbisStream(&_file, false, 0, 0, loop ? 0 : 1);
	}
#endif
#ifdef USE_MAD
	if (!_audioSource) {
		sprintf(fileName, "%s.mp3", fileBase);
		if (_file.open(fileName))
			_audioSource = Audio::makeMP3Stream(&_file, false, 0, 0, loop ? 0 : 1);
	}
#endif
	if (!_audioSource) {
		sprintf(fileName, "%s.wav", fileBase);
		if (_file.open(fileName))
			_audioSource = new WaveAudioStream(&_file, loop);
	}

	if (!_audioSource) {
		sprintf(fileName, "%s.aif", fileBase);
		if (_file.open(fileName))
			_audioSource = new AiffAudioStream(&_file, loop);
	}

	if (!_audioSource)
		return false;

	fadeUp();
	return true;
}

bool MusicHandle::playPSX(uint16 id, bool loop) {
	stop();

	if (!_file.isOpen())
		if (!_file.open("tunes.dat"))
			return false;
		
	Common::File tableFile;
	if (!tableFile.open("tunes.tab"))
		return false;

	tableFile.seek((id - 1) * 8, SEEK_SET);
	uint32 offset = tableFile.readUint32LE() * 0x800;
	uint32 size = tableFile.readUint32LE();

	tableFile.close();

	if (size != 0xffffffff) {
		_file.seek(offset, SEEK_SET);
		_audioSource = new Audio::VagStream(_file.readStream(size), loop);
		fadeUp();
	} else {
		_audioSource = NULL;
		return false;
	}
	
	return true;
}

void MusicHandle::fadeDown() {
	if (streaming()) {
		if (_fading < 0)
			_fading = -_fading;
		else if (_fading == 0)
			_fading = FADE_LENGTH * getRate();
		_fadeSamples = FADE_LENGTH * getRate();
	}
}

void MusicHandle::fadeUp() {
	if (streaming()) {
		if (_fading > 0)
			_fading = -_fading;
		else if (_fading == 0)
			_fading = -1;
		_fadeSamples = FADE_LENGTH * getRate();
	}
}

bool MusicHandle::endOfData() const {
	return !streaming();
}

// if we don't have an audiosource, return some dummy values.
bool MusicHandle::streaming(void) const {
	return (_audioSource) ? (!_audioSource->endOfStream()) : false;
}

bool MusicHandle::isStereo(void) const {
	return (_audioSource) ? _audioSource->isStereo() : false;
}

int MusicHandle::getRate(void) const {
	return (_audioSource) ? _audioSource->getRate() : 11025;
}

int MusicHandle::readBuffer(int16 *buffer, const int numSamples) {
	int totalSamples = 0;
	int16 *bufStart = buffer;
	if (!_audioSource)
		return 0;
	int expectedSamples = numSamples;
	while ((expectedSamples > 0) && _audioSource) { // _audioSource becomes NULL if we reach EOF and aren't looping
		int samplesReturned = _audioSource->readBuffer(buffer, expectedSamples);
		buffer += samplesReturned;
		totalSamples += samplesReturned;
		expectedSamples -= samplesReturned;
		if ((expectedSamples > 0) && _audioSource->endOfData()) {
			debug(2, "Music reached EOF");
			stop();
		}
	}
	// buffer was filled, now do the fading (if necessary)
	int samplePos = 0;
	while ((_fading > 0) && (samplePos < totalSamples)) { // fade down
		--_fading;
		bufStart[samplePos] = (bufStart[samplePos] * _fading) / _fadeSamples;
		samplePos++;
		if (_fading == 0) {
			stop();
			// clear the rest of the buffer
			memset(bufStart + samplePos, 0, (totalSamples - samplePos) * 2);
			return samplePos;
		}
	}
	while ((_fading < 0) && (samplePos < totalSamples)) { // fade up
		bufStart[samplePos] = -(bufStart[samplePos] * --_fading) / _fadeSamples;
		if (_fading <= -_fadeSamples)
			_fading = 0;
	}
	return totalSamples;
}

void MusicHandle::stop() {
	if (_audioSource) {
		delete _audioSource;
		_audioSource = NULL;
	}
	if (_file.isOpen())
		_file.close();
	_fading = 0;
}

Music::Music(Audio::Mixer *pMixer) {
	_mixer = pMixer;
	_sampleRate = pMixer->getOutputRate();
	_converter[0] = NULL;
	_converter[1] = NULL;
	_volumeL = _volumeR = 192;
	_mixer->playInputStream(Audio::Mixer::kPlainSoundType, &_soundHandle, this, -1, Audio::Mixer::kMaxChannelVolume, 0, false, true);
}

Music::~Music() {
	_mixer->stopHandle(_soundHandle);
	delete _converter[0];
	delete _converter[1];
}

void Music::mixer(int16 *buf, uint32 len) {
	Common::StackLock lock(_mutex);
	memset(buf, 0, 2 * len * sizeof(int16));
	for (int i = 0; i < ARRAYSIZE(_handles); i++)
		if (_handles[i].streaming() && _converter[i])
			_converter[i]->flow(_handles[i], buf, len, _volumeL, _volumeR);
}

void Music::setVolume(uint8 volL, uint8 volR) {
	_volumeL = (Audio::st_volume_t)volL;
	_volumeR = (Audio::st_volume_t)volR;
}

void Music::giveVolume(uint8 *volL, uint8 *volR) {
	*volL = (uint8)_volumeL;
	*volR = (uint8)_volumeR;
}

void Music::startMusic(int32 tuneId, int32 loopFlag) {
	if (strlen(_tuneList[tuneId]) > 0) {
		int newStream = 0;
		_mutex.lock();
		if (_handles[0].streaming() && _handles[1].streaming()) {
			int streamToStop;
			// Both streams playing - one must be forced to stop.
			if (!_handles[0].fading() && !_handles[1].fading()) {
				// None of them are fading. Shouldn't happen,
				// so it doesn't matter which one we pick.
				streamToStop = 0;
			} else if (_handles[0].fading() && !_handles[1].fading()) {
				// Stream 0 is fading, so pick that one.
				streamToStop = 0;
			} else if (!_handles[0].fading() && _handles[1].fading()) {
				// Stream 1 is fading, so pick that one.
				streamToStop = 1;
			} else {
				// Both streams are fading. Pick the one that
				// is closest to silent.
				if (ABS(_handles[0].fading()) < ABS(_handles[1].fading()))
					streamToStop = 0;
				else
					streamToStop = 1;
			}
			_handles[streamToStop].stop();
		}
		if (_handles[0].streaming()) {
			_handles[0].fadeDown();
			newStream = 1;
		} else if (_handles[1].streaming()) {
			_handles[1].fadeDown();
			newStream = 0;
		}
		delete _converter[newStream];
		_converter[newStream] = NULL;
		_mutex.unlock();

		/* The handle will load the music file now. It can take a while, so unlock
		   the mutex before, to have the soundthread playing normally.
		   As the corresponding _converter is NULL, the handle will be ignored by the playing thread */
		if (SwordEngine::isPsx()) { ;
			if (_handles[newStream].playPSX(tuneId, loopFlag != 0)) {
				_mutex.lock();
				_converter[newStream] = Audio::makeRateConverter(_handles[newStream].getRate(), _mixer->getOutputRate(), _handles[newStream].isStereo(), false);
				_mutex.unlock();
			}
		} else if (_handles[newStream].play(_tuneList[tuneId], loopFlag != 0)) {
			_mutex.lock();
			_converter[newStream] = Audio::makeRateConverter(_handles[newStream].getRate(), _mixer->getOutputRate(), _handles[newStream].isStereo(), false);
			_mutex.unlock();
		} else {
			if (tuneId != 81) // file 81 was apparently removed from BS.
				warning("Can't find music file %s", _tuneList[tuneId]);
		}
	} else {
		_mutex.lock();
		if (_handles[0].streaming())
			_handles[0].fadeDown();
		if (_handles[1].streaming())
			_handles[1].fadeDown();
		_mutex.unlock();
	}
}

void Music::fadeDown() {
	Common::StackLock lock(_mutex);
	for (int i = 0; i < ARRAYSIZE(_handles); i++)
		if (_handles[i].streaming())
			_handles[i].fadeDown();
}

} // End of namespace Sword1