aboutsummaryrefslogtreecommitdiff
path: root/engines/xeen/music.cpp
blob: 741fe81c311181adbde3045ccb9da4d88c83c167 (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
/* 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 "common/md5.h"
#include "xeen/music.h"
#include "xeen/xeen.h"
#include "xeen/files.h"

namespace Xeen {

#define CALLBACKS_PER_SECOND 60

Music::Music(Audio::Mixer *mixer) : _mixer(mixer), _effectsData(nullptr),
		_musicPtr1(nullptr), _musicPtr2(nullptr), _lowMusicIgnored(false),
		_fieldF(false), _field109(0), _field10B(0), _field114(0), 
		_field115(0), _field117(0) {
	_channels.resize(ADLIB_CHANNEL_COUNT);
	Common::fill(&_field10D[0], &_field10D[7], 0);
	
	_mixer = mixer;
	_opl = OPL::Config::create();
	_opl->init();
	_opl->start(new Common::Functor0Mem<void, Music>(this, &Music::onTimer), CALLBACKS_PER_SECOND);

	loadEffectsData();
}

Music::~Music() {
	_opl->stop();
	delete _opl;
	delete[] _effectsData;
}

void Music::loadEffectsData() {
	File file("admus");
	Common::String md5str = Common::computeStreamMD5AsString(file, 8192);
	
	if (md5str != "be8989a5e868913f0e53963046e3ea13")
		error("Unknown music driver encountered");

	// Load in the driver data
	const int EFFECTS_OFFSET = 0x91D;
	byte *effectsData = new byte[file.size() - EFFECTS_OFFSET];
	file.seek(EFFECTS_OFFSET);
	file.read(effectsData, file.size() - EFFECTS_OFFSET);
	file.close();
	_effectsData = effectsData;

	// Extract the effects offsets
	_effectsOffsets.resize(180);
	for (int idx = 0; idx < 180; ++idx)
		_effectsOffsets[idx] = READ_LE_UINT16(&effectsData[idx * 2]) - EFFECTS_OFFSET;
}

void Music::onTimer() {
	Common::StackLock slock(_driverMutex);
	update();
	flush();
}

void Music::write(int reg, int val) {
	_queue.push(RegisterValue(reg, val));
}

void Music::flush() {
	Common::StackLock slock(_driverMutex);

	while (!_queue.empty()) {
		RegisterValue v = _queue.pop();
		_opl->writeReg(v._regNum, v._value);
	}
}

void Music::update() {
	// TODO
}

void Music::playEffect(uint effectId) {
	if (!_lowMusicIgnored || effectId < 7 || effectId >= 11) {
		if (effectId < _effectsOffsets.size()) {
			_musicPtr1 = _musicPtr2 = &_effectsData[_effectsOffsets[effectId]];
			_field117 = 0;
			_field115 = 0;
			_field114 = 0;
			reset();
			_lowMusicIgnored = true;
		}
	}
}

void Music::reset() {
	if (!_fieldF) {
		_field109 = 0;
		setFrequency(7, 0);
		_channels[7]._outputLevel = 63;
		setOutputLevel(7, 63);
	}

	_field10B = 0;
	setFrequency(8, 0);
	_channels[8]._outputLevel = 63;
	setOutputLevel(8, 63);
}

void Music::setFrequency(byte operatorNum, uint frequency) {
	write(0xA0 + operatorNum, frequency & 0xff);
	write(0xB0 + operatorNum, (frequency >> 8));
}

void Music::setOutputLevel(byte channelNum, uint level) {
	write(0x40 + OPERATOR2_INDEXES[channelNum], level |
		(_channels[channelNum]._scalingValue & 0xC0));
}

const byte Music::OPERATOR1_INDEXES[ADLIB_CHANNEL_COUNT] = {
	0, 1, 2, 8, 9, 0xA, 0x10, 0x11, 0x12
};

const byte Music::OPERATOR2_INDEXES[ADLIB_CHANNEL_COUNT] = {
	3, 4, 5, 0xB, 0xC, 0xD, 0x13, 0x14, 0x15
};

} // End of namespace Xeen