aboutsummaryrefslogtreecommitdiff
path: root/engines/sky/music/gmchannel.cpp
blob: fc35fed391ec680f1632819bc3a8151b9153681f (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
/* 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 "gmchannel.h"
#include "common/util.h"
#include "sound/mididrv.h"

namespace Sky {

GmChannel::GmChannel(uint8 *pMusicData, uint16 startOfData, MidiDriver *pMidiDrv, const byte *pInstMap, const byte *veloTab) {

	_musicData = pMusicData;
	_midiDrv = pMidiDrv;
	_channelData.midiChannelNumber = 0;
	_channelData.loopPoint = startOfData;
	_channelData.eventDataPtr = startOfData;
	_channelData.channelActive = true;
	_channelData.nextEventTime = getNextEventTime();
	_instMap = pInstMap;
	_veloTab = veloTab;

	_musicVolume = 0x7F;
	_currentChannelVolume = 0x7F;
}

GmChannel::~GmChannel(void) {
	stopNote();
}

bool GmChannel::isActive(void) {
	return _channelData.channelActive;
}

void GmChannel::updateVolume(uint16 pVolume) {

	_musicVolume = pVolume;
	if (_musicVolume > 0)
		_musicVolume = (_musicVolume * 2) / 3 + 43;

	uint8 newVol = (_currentChannelVolume * _musicVolume) >> 7;
	_midiDrv->send((0xB0 | _channelData.midiChannelNumber) | 0x700 | (newVol << 16));
}

void GmChannel::stopNote(void) {

	// All Notes Off
	_midiDrv->send((0xB0 | _channelData.midiChannelNumber) | 0x7B00 | 0 | 0x79000000);
	// Reset the Pitch Wheel. See bug #1016556.
	_midiDrv->send((0xE0 | _channelData.midiChannelNumber) | 0x400000);
}

int32 GmChannel::getNextEventTime(void) {

	int32 retV = 0;
	uint8 cnt, lVal = 0;
	for (cnt = 0; cnt < 4; cnt++) {
		lVal = _musicData[_channelData.eventDataPtr];
		_channelData.eventDataPtr++;
		retV = (retV << 7) | (lVal & 0x7F);
		if (!(lVal & 0x80))
			break;
	}
	if (lVal & 0x80) { // should never happen
		return -1;
	} else
		return retV;
}

uint8 GmChannel::process(uint16 aktTime) {

	if (!_channelData.channelActive)
		return 0;

	uint8 returnVal = 0;

	_channelData.nextEventTime -= aktTime;
	uint8 opcode;

	while ((_channelData.nextEventTime < 0) && (_channelData.channelActive)) {
		opcode = _musicData[_channelData.eventDataPtr];
		_channelData.eventDataPtr++;
		if (opcode&0x80) {
			if (opcode == 0xFF) {
				// dummy opcode
			} else if (opcode >= 0x90) {
				switch (opcode&0xF) {
				case 0: com90_caseNoteOff(); break;
				case 1: com90_stopChannel(); break;
				case 2: com90_setupInstrument(); break;
				case 3:
					returnVal = com90_updateTempo();
					break;
				case 5: com90_getPitch(); break;
				case 6: com90_getChannelVolume(); break;
				case 8: com90_loopMusic(); break;
				case 9: com90_keyOff(); break;
				case 11: com90_getChannelPanValue(); break;
				case 12: com90_setLoopPoint(); break;
				case 13: com90_getChannelControl(); break;

				default:
					error("GmChannel: Unknown music opcode 0x%02X", opcode);
					break;
				}
			} else {
				// new midi channel assignment
				_channelData.midiChannelNumber = opcode&0xF;
			}
		} else {
			_channelData.note = opcode;
			byte velocity = _musicData[_channelData.eventDataPtr];
			if (_veloTab)
				velocity = _veloTab[velocity];
			_channelData.eventDataPtr++;
			_midiDrv->send((0x90 | _channelData.midiChannelNumber) | (opcode << 8) | (velocity << 16));
		}
		if (_channelData.channelActive)
			_channelData.nextEventTime += getNextEventTime();
	}
	return returnVal;
}

//- command 90h routines

void GmChannel::com90_caseNoteOff(void) {

	_midiDrv->send((0x90 | _channelData.midiChannelNumber) | (_musicData[_channelData.eventDataPtr] << 8));
	_channelData.eventDataPtr++;
}

void GmChannel::com90_stopChannel(void) {

	stopNote();
	_channelData.channelActive = false;
}

void GmChannel::com90_setupInstrument(void) {
	byte instrument = _musicData[_channelData.eventDataPtr];
	if (_instMap)
		instrument = _instMap[instrument];
	_midiDrv->send((0xC0 | _channelData.midiChannelNumber) | (instrument << 8));
	_channelData.eventDataPtr++;
}

uint8 GmChannel::com90_updateTempo(void) {
	return _musicData[_channelData.eventDataPtr++];
}

void GmChannel::com90_getPitch(void) {

	_midiDrv->send((0xE0 | _channelData.midiChannelNumber) | 0 | (_musicData[_channelData.eventDataPtr] << 16));
	_channelData.eventDataPtr++;
}

void GmChannel::com90_getChannelVolume(void) {

	_currentChannelVolume = _musicData[_channelData.eventDataPtr++];
	uint8 newVol = (uint8)((_currentChannelVolume * _musicVolume) >> 7);
	_midiDrv->send((0xB0 | _channelData.midiChannelNumber) | 0x700 | (newVol << 16));
}

void GmChannel::com90_loopMusic(void) {

	_channelData.eventDataPtr = _channelData.loopPoint;
}

void GmChannel::com90_keyOff(void) {

	_midiDrv->send((0x90 | _channelData.midiChannelNumber) | (_channelData.note << 8) | 0);
}

void GmChannel::com90_setLoopPoint(void) {

	_channelData.loopPoint = _channelData.eventDataPtr;
}

void GmChannel::com90_getChannelPanValue(void) {

	_midiDrv->send((0xB0 | _channelData.midiChannelNumber) | 0x0A00 | (_musicData[_channelData.eventDataPtr] << 16));
	_channelData.eventDataPtr++;
}

void GmChannel::com90_getChannelControl(void) {

	uint8 conNum = _musicData[_channelData.eventDataPtr++];
	uint8 conDat = _musicData[_channelData.eventDataPtr++];
	_midiDrv->send((0xB0 | _channelData.midiChannelNumber) | (conNum << 8) | (conDat << 16));
}

} // End of namespace Sky