aboutsummaryrefslogtreecommitdiff
path: root/engines/parallaction/sound_br.cpp
blob: e127cf2838a5c70efa3d9cda03530b1a2da50b7f (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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/* 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/stream.h"
#include "common/util.h"

#include "audio/mixer.h"
#include "audio/mididrv.h"
#include "audio/midiparser.h"
#include "audio/mods/protracker.h"
#include "audio/decoders/raw.h"

#include "parallaction/disk.h"
#include "parallaction/parallaction.h"
#include "parallaction/sound.h"


namespace Parallaction {


/*
 * List of calls to the original music driver.
 *
 *
 * 1 set music buffer segment
 * 2 set music buffer offset
 * 3 set music buffer size
 * 4 play/resume
 * 5 stop
 * 6 pause
 * 7 set channel volume
 * 8 set fade in flag
 * 9 set fade out flag
 * 10 set global volume
 * 11 shutdown
 * 12 get status (stopped, playing, paused)
 * 13 set fade volume change rate
 * 14 get global volume
 * 15 get fade in flag
 * 16 get fade out flag
 * 17 set tempo
 * 18 get tempo
 * 19 get fade volume change rate
 * 20 get looping flag
 * 21 set looping flag
 * 22 get version??
 * 23 get version??
 * 24 get busy flag (dsp has pending data)
 */

class MidiParser_MSC : public MidiParser {
protected:
	virtual void parseNextEvent(EventInfo &info);
	virtual bool loadMusic(byte *data, uint32 size);

	uint8  read1(byte *&data) {
		return *data++;
	}

	void parseMetaEvent(EventInfo &info);
	void parseMidiEvent(EventInfo &info);

	bool byte_11C5A;
	uint8 _beats;
	uint8 _lastEvent;
	byte *_trackEnd;

public:
	MidiParser_MSC() : byte_11C5A(false) {
	}
};

void MidiParser_MSC::parseMetaEvent(EventInfo &info) {
	uint8 type = read1(_position._play_pos);
	uint8 len = read1(_position._play_pos);
	info.ext.type = type;
	info.length = len;
	info.ext.data = 0;

	if (type == 0x51) {
		info.ext.data = _position._play_pos;
	} else {
		warning("unknown meta event 0x%02X", type);
		info.ext.type = 0;
	}

	_position._play_pos += len;
}

void MidiParser_MSC::parseMidiEvent(EventInfo &info) {
	uint8 type = info.command();

	switch (type) {
	case 0x8:
	case 0x9:
	case 0xA:
	case 0xB:
	case 0xE:
		info.basic.param1 = read1(_position._play_pos);
		info.basic.param2 = read1(_position._play_pos);
		break;

	case 0xC:
	case 0xD:
		info.basic.param1 = read1(_position._play_pos);
		info.basic.param2 = 0;
		break;

	default:
		warning("Unexpected midi event 0x%02X in midi data", info.event);
	}

	//if ((type == 0xB) && (info.basic.param1 == 64)) info.basic.param2 = 127;

}

void MidiParser_MSC::parseNextEvent(EventInfo &info) {
	info.start = _position._play_pos;

	if (_position._play_pos >= _trackEnd) {
		// fake an end-of-track meta event
		info.delta = 0;
		info.event = 0xFF;
		info.ext.type = 0x2F;
		info.length = 0;
		return;
	}

	info.delta = readVLQ(_position._play_pos);
	info.event = read1(_position._play_pos);

	if (info.event == 0xFF) {
		parseMetaEvent(info);
		return;
	}

	if (info.event < 0x80) {
		_position._play_pos--;
		info.event = _lastEvent;
	}

	parseMidiEvent(info);
	_lastEvent = info.event;

}

bool MidiParser_MSC::loadMusic(byte *data, uint32 size) {
	unloadMusic();

	byte *pos = data;

	if (memcmp("MSCt", pos, 4)) {
		warning("Expected header not found in music file");
		return false;
	}
	pos += 4;

	_beats = read1(pos);
	_ppqn = read2low(pos);

	if (byte_11C5A) {
		// do something with byte_11C4D
	}

	_lastEvent = 0;
	_trackEnd = data + size;

	_num_tracks = 1;
	_tracks[0] = pos;

	setTempo(500000);
	setTrack(0);
	return true;
}


MidiParser *createParser_MSC() {
	return new MidiParser_MSC;
}


class MidiPlayer_MSC : public MidiDriver {
public:

	enum {
		NUM_CHANNELS = 16
	};

	MidiPlayer_MSC(MidiDriver *driver);
	~MidiPlayer_MSC();

	void play(Common::SeekableReadStream *stream);
	void stop();
	void pause(bool p);
	void updateTimer();
	void adjustVolume(int diff);
	void setVolume(int volume);
	int getVolume() const { return _masterVolume; }
	void setLooping(bool loop) { _isLooping = loop; }

	// MidiDriver interface
	int open();
	void close();
	void send(uint32 b);
	void metaEvent(byte type, byte *data, uint16 length);
	void setTimerCallback(void *timerParam, void (*timerProc)(void *)) { }
	uint32 getBaseTempo() { return _driver ? _driver->getBaseTempo() : 0; }
	MidiChannel *allocateChannel() { return 0; }
	MidiChannel *getPercussionChannel() { return 0; }

private:

	static void timerCallback(void *p);
	void setVolumeInternal(int volume);

	Common::Mutex _mutex;
	MidiDriver *_driver;
	MidiParser *_parser;
	uint8 *_midiData;
	bool _isLooping;
	bool _isPlaying;
	bool _paused;

	int _masterVolume;
	MidiChannel *_channels[NUM_CHANNELS];
	uint8 _volume[NUM_CHANNELS];
};



MidiPlayer_MSC::MidiPlayer_MSC(MidiDriver *driver)
	: _driver(driver), _parser(0), _midiData(0), _isLooping(false), _isPlaying(false), _paused(false), _masterVolume(0) {
	assert(_driver);
	memset(_channels, 0, sizeof(_channels));
	for (int i = 0; i < NUM_CHANNELS; i++) {
		_volume[i] = 127;
	}

	open();
}

MidiPlayer_MSC::~MidiPlayer_MSC() {
	close();
}

void MidiPlayer_MSC::play(Common::SeekableReadStream *stream) {
	if (!stream) {
		stop();
		return;
	}

	stop();
	int size = stream->size();
	_midiData = (uint8 *)malloc(size);
	if (_midiData) {
		stream->read(_midiData, size);
		delete stream;
		_mutex.lock();
		_parser->loadMusic(_midiData, size);
		_parser->setTrack(0);
		_isLooping = true;
		_isPlaying = true;
		_mutex.unlock();
	}
}

void MidiPlayer_MSC::stop() {
	_mutex.lock();
	if (_isPlaying) {
		_isPlaying = false;
		_parser->unloadMusic();
		free(_midiData);
		_midiData = 0;
	}
	_mutex.unlock();
}

void MidiPlayer_MSC::pause(bool p) {
	_paused = p;
	setVolumeInternal(_paused ? 0 : _masterVolume);
}

void MidiPlayer_MSC::updateTimer() {
	if (_paused) {
		return;
	}

	Common::StackLock lock(_mutex);
	if (_isPlaying) {
		_parser->onTimer();
	}
}

void MidiPlayer_MSC::adjustVolume(int diff) {
	setVolume(_masterVolume + diff);
}

void MidiPlayer_MSC::setVolume(int volume) {
	_masterVolume = CLIP(volume, 0, 255);
	setVolumeInternal(_masterVolume);
}

void MidiPlayer_MSC::setVolumeInternal(int volume) {
	Common::StackLock lock(_mutex);
	for (int i = 0; i < NUM_CHANNELS; ++i) {
		if (_channels[i]) {
			_channels[i]->volume(_volume[i] * volume / 255);
		}
	}
}

int MidiPlayer_MSC::open() {
	int ret = _driver->open();
	if (ret == 0) {
		_parser = createParser_MSC();
		_parser->setMidiDriver(this);
		_parser->setTimerRate(_driver->getBaseTempo());
		_driver->setTimerCallback(this, &timerCallback);
	}
	return ret;
}

void MidiPlayer_MSC::close() {
	stop();
	_mutex.lock();
	_driver->setTimerCallback(NULL, NULL);
	_driver->close();
	delete _driver;
	_driver = 0;
	_parser->setMidiDriver(NULL);
	delete _parser;
	_mutex.unlock();
}

void MidiPlayer_MSC::send(uint32 b) {
	const byte ch = b & 0x0F;
	byte param2 = (b >> 16) & 0xFF;

	switch (b & 0xFFF0) {
	case 0x07B0: // volume change
		_volume[ch] = param2;
		break;
	}

	if (!_channels[ch]) {
		_channels[ch] = (ch == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
	}
	if (_channels[ch]) {
		_channels[ch]->send(b);
	}
}

void MidiPlayer_MSC::metaEvent(byte type, byte *data, uint16 length) {
	switch (type) {
	case 0x2F: // end of Track
		if (_isLooping) {
			_parser->jumpToTick(0);
		} else {
			stop();
		}
		break;
	default:
		break;
	}
}

void MidiPlayer_MSC::timerCallback(void *p) {
	MidiPlayer_MSC *player = (MidiPlayer_MSC *)p;

	player->updateTimer();
}

DosSoundMan_br::DosSoundMan_br(Parallaction_br *vm, MidiDriver *driver) : SoundMan_br(vm) {
	_midiPlayer = new MidiPlayer_MSC(driver);
	assert(_midiPlayer);
}

DosSoundMan_br::~DosSoundMan_br() {
	delete _midiPlayer;
}

Audio::AudioStream *DosSoundMan_br::loadChannelData(const char *filename, Channel *ch, bool looping) {
	Common::SeekableReadStream *stream = _vm->_disk->loadSound(filename);

	uint32 dataSize = stream->size();
	byte *data = (byte *)malloc(dataSize);
	if (stream->read(data, dataSize) != dataSize)
		error("DosSoundMan_br::loadChannelData: Read failed");

	delete stream;

	// TODO: Confirm sound rate
	int rate = 11025;

	ch->stream = Audio::makeLoopingAudioStream(
			Audio::makeRawStream(data, dataSize, rate, Audio::FLAG_UNSIGNED),
			looping ? 0 : 1);
	return ch->stream;
}

void DosSoundMan_br::playSfx(const char *filename, uint channel, bool looping, int volume) {
	stopSfx(channel);

	if (!_sfxEnabled) {
		return;
	}

	debugC(1, kDebugAudio, "DosSoundMan_br::playSfx(%s, %u, %i, %i)", filename, channel, looping, volume);

	Channel *ch = &_channels[channel];
	Audio::AudioStream *input = loadChannelData(filename, ch, looping);
	_mixer->playStream(Audio::Mixer::kSFXSoundType, &ch->handle, input, -1, volume);
}

void DosSoundMan_br::playMusic() {
	if (_musicFile.empty()) {
		return;
	}

	if (!_musicEnabled) {
		return;
	}

	Common::SeekableReadStream *s = _vm->_disk->loadMusic(_musicFile.c_str());
	assert(s);
	_midiPlayer->play(s);
}

void DosSoundMan_br::stopMusic() {
	_midiPlayer->stop();
}

void DosSoundMan_br::pause(bool p) {
	_midiPlayer->pause(p);
}

AmigaSoundMan_br::AmigaSoundMan_br(Parallaction_br *vm) : SoundMan_br(vm)  {
	_musicStream = 0;
}

AmigaSoundMan_br::~AmigaSoundMan_br() {
	stopMusic();
}

Audio::AudioStream *AmigaSoundMan_br::loadChannelData(const char *filename, Channel *ch, bool looping) {
	Common::SeekableReadStream *stream = _vm->_disk->loadSound(filename);
	Audio::AudioStream *input = 0;

	if (_vm->getFeatures() & GF_DEMO) {
		uint32 dataSize = stream->size();
		int8 *data = (int8*)malloc(dataSize);
		if (stream->read(data, dataSize) != dataSize)
			error("DosSoundMan_br::loadChannelData: Read failed");

		// TODO: Confirm sound rate
		int rate = 11025;
		input = Audio::makeRawStream((byte *)data, dataSize, rate, 0);
	} else {
		input = Audio::make8SVXStream(*stream, looping);
	}

	delete stream;

	ch->stream = input;
	return input;
}

void AmigaSoundMan_br::playSfx(const char *filename, uint channel, bool looping, int volume) {
	if (channel >= NUM_SFX_CHANNELS) {
		warning("unknown sfx channel");
		return;
	}

	stopSfx(channel);

	if (!_sfxEnabled) {
		return;
	}

	debugC(1, kDebugAudio, "AmigaSoundMan_ns::playSfx(%s, %i)", filename, channel);

	Channel *ch = &_channels[channel];
	Audio::AudioStream *input = loadChannelData(filename, ch, looping);

	if (volume == -1) {
		volume = ch->volume;
	}

	_mixer->playStream(Audio::Mixer::kSFXSoundType, &ch->handle, input, -1, volume);
}

void AmigaSoundMan_br::playMusic() {
	stopMusic();

	if (!_musicEnabled) {
		return;
	}

	debugC(1, kDebugAudio, "AmigaSoundMan_ns::playMusic()");

	Common::SeekableReadStream *stream = _vm->_disk->loadMusic(_musicFile.c_str());
	// NOTE: Music files don't always exist
	if (!stream)
		return;

	_musicStream = Audio::makeProtrackerStream(stream);
	delete stream;

	debugC(3, kDebugAudio, "AmigaSoundMan_ns::playMusic(): created new music stream");

	_mixer->playStream(Audio::Mixer::kMusicSoundType, &_musicHandle, _musicStream, -1, Audio::Mixer::kMaxChannelVolume, 0, DisposeAfterUse::NO, false);
}

void AmigaSoundMan_br::stopMusic() {
	debugC(1, kDebugAudio, "AmigaSoundMan_ns::stopMusic()");

	if (_mixer->isSoundHandleActive(_musicHandle)) {
		_mixer->stopHandle(_musicHandle);
		delete _musicStream;
		_musicStream = 0;
	}
}

void AmigaSoundMan_br::pause(bool p) {
	_mixer->pauseHandle(_musicHandle, p);
}

SoundMan_br::SoundMan_br(Parallaction_br *vm) : _vm(vm) {
	_mixer = _vm->_mixer;

	_musicEnabled = true;
	_sfxEnabled = true;
}

SoundMan_br::~SoundMan_br() {
	stopAllSfx();
}

void SoundMan_br::stopAllSfx() {
	stopSfx(0);
	stopSfx(1);
	stopSfx(2);
	stopSfx(3);
}

void SoundMan_br::setMusicFile(const char *name) {
	stopMusic();
	_musicFile = name;
}

void SoundMan_br::stopSfx(uint channel) {
	if (channel >= NUM_SFX_CHANNELS) {
		warning("unknown sfx channel");
		return;
	}

	debugC(1, kDebugAudio, "SoundMan_br::stopSfx(%i)", channel);
	_mixer->stopHandle(_channels[channel].handle);
	_channels[channel].stream = 0;
}

void SoundMan_br::execute(int command, const char *parm) {
	uint32 n = parm ? strtoul(parm, 0, 10) : 0;
	bool b = (n == 1) ? true : false;

	switch (command) {
	case SC_PLAYMUSIC:
		playMusic();
		break;
	case SC_STOPMUSIC:
		stopMusic();
		break;
	case SC_SETMUSICFILE:
		setMusicFile(parm);
		break;

	case SC_PLAYSFX:
		playSfx(parm, _sfxChannel, _sfxLooping, _sfxVolume);
		break;
	case SC_STOPSFX:
		stopSfx(n);
		break;

	case SC_SETSFXCHANNEL:
		_sfxChannel = n;
		break;
	case SC_SETSFXLOOPING:
		_sfxLooping = b;
		break;
	case SC_SETSFXVOLUME:
		_sfxVolume = n;
		break;

	case SC_PAUSE:
		pause(b);
		break;
	}
}

void SoundMan_br::enableSfx(bool enable) {
	if (!enable) {
		stopAllSfx();
	}
	_sfxEnabled = enable;
}

void SoundMan_br::enableMusic(bool enable) {
	if (enable) {
		playMusic();
	} else {
		stopMusic();
	}
	_musicEnabled = enable;
}

bool SoundMan_br::isSfxEnabled() const {
	return _sfxEnabled;
}

bool SoundMan_br::isMusicEnabled() const {
	return _musicEnabled;
}

} // namespace Parallaction