aboutsummaryrefslogtreecommitdiff
path: root/audio/softsynth/fmtowns_pc98/towns_midi.cpp
blob: 737e97754537bfa8d007eb6c0ac211d5562a1449 (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
/* 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 "audio/softsynth/fmtowns_pc98/towns_midi.h"
#include "common/textconsole.h"

class TownsMidiOutputChannel {
friend class TownsMidiInputChannel;
public:
	TownsMidiOutputChannel(MidiDriver_TOWNS *driver, int chanId);
	~TownsMidiOutputChannel();

	void noteOn(uint8 msb, uint16 lsb);
	void noteOnAdjust(uint8 msb, uint16 lsb);
	void setupProgram(const uint8 *data, uint8 vol1, uint8 vol2);
	
	void connect(TownsMidiInputChannel *chan);
	void disconnect();

	enum CheckPriorityStatus {
		kDisconnected = -3,
		kHighPriority = -2
	};

	int checkPriority(int pri);

private:
	void keyOn();
	void keyOff();
	void internKeyOnFrq(uint16 frq);
	void out(uint8 chan, uint8 reg, uint8 val);

	TownsMidiInputChannel *_midi;
	TownsMidiOutputChannel *_prev;
	TownsMidiOutputChannel *_next;
	uint8 _fld_f;
	uint8 _note;
	uint8 _tl;
	uint8 _noteOffMarker;
	uint8 _fld_12;
	uint8 _fld_13;
	uint8 _prg;
	uint8 _chan;

	uint16 _freq;
	int16 _freqAdjust;

	MidiDriver_TOWNS *_driver;

	static const uint8 _freqMSB[];
	static const uint16 _freqLSB[];
};

class TownsMidiInputChannel : public MidiChannel {
friend class TownsMidiOutputChannel;
public:
	TownsMidiInputChannel(MidiDriver_TOWNS *driver, int chanIndex);
	~TownsMidiInputChannel();

	MidiDriver *device() { return _driver; }
	byte getNumber() { return _chanIndex; }
	bool allocate();
	void release();

	void send(uint32 b);

	void noteOff(byte note);
	void noteOn(byte note, byte velocity);
	void programChange(byte program);
	void pitchBend(int16 bend);
	void controlChange(byte control, byte value);
	void pitchBendFactor(byte value);
	void priority(byte value);
	void sysEx_customInstrument(uint32 type, const byte *instr);

private:
	TownsMidiOutputChannel *_outChan;
	//TownsMidiInputChannel *_prev;
	//TownsMidiInputChannel *_next;
	
	uint8 *_instrument;
	uint8 _prg;
	uint8 _chanIndex;
	uint8 _effectLevel;
	uint8 _priority;
	uint8 _vol;
	uint8 _volEff;
	uint8 _pan;
	uint8 _panEff;
	uint8 _perc;
	uint8 _percS;
	uint8 _fld_22;
	uint8 _pitchBendFactor;

	bool _allocated;

	MidiDriver_TOWNS *_driver;
};

TownsMidiOutputChannel::TownsMidiOutputChannel(MidiDriver_TOWNS *driver, int chanIndex) : _driver(driver), _chan(chanIndex),
	_midi(0), _prev(0), _next(0), _fld_f(0), _note(0), _tl(0), _noteOffMarker(0), _fld_12(0), _fld_13(0), _prg(0), _freq(0), _freqAdjust(0) {
}

TownsMidiOutputChannel::~TownsMidiOutputChannel() {
}

void TownsMidiOutputChannel::noteOn(uint8 msb, uint16 lsb) {
	_freq = (msb << 7) + lsb;
	_freqAdjust = 0;
	internKeyOnFrq(_freq);
}

void TownsMidiOutputChannel::noteOnAdjust(uint8 msb, uint16 lsb) {
	_freq = (msb << 7) + lsb;
	internKeyOnFrq(_freq + _freqAdjust);
}

void TownsMidiOutputChannel::setupProgram(const uint8 *data, uint8 vol1, uint8 vol2) {
	const uint8 *pos = data;

}

void TownsMidiOutputChannel::connect(TownsMidiInputChannel *chan) {
	if (!chan)
		return;
	_midi = chan;
	_next = chan->_outChan;
	_prev = 0;
	chan->_outChan = this;
	if (_next)
		_next->_prev = this;
}

void TownsMidiOutputChannel::disconnect() {
	keyOff();
	TownsMidiOutputChannel *p = _prev;
	TownsMidiOutputChannel *n = _next;

	if (n)
		n->_prev = p;
	if (p)
		p->_next = n;
	else
		_midi->_outChan = n;
	_midi = 0;
}

int TownsMidiOutputChannel::checkPriority(int pri) {
	if (!_midi)
		return kDisconnected;

	if (!_next && pri >= _midi->_priority)
		return _midi->_priority;

	return kHighPriority;
}

void TownsMidiOutputChannel::keyOn() {
	out(_chan, 0x28, 0xf0/*0x30*/ /*???*/);
}

void TownsMidiOutputChannel::keyOff() {
	out(_chan, 0x28, 0);
}

void TownsMidiOutputChannel::internKeyOnFrq(uint16 frq) {
	uint8 t = (frq << 1) >> 8;	
	frq = (_freqMSB[t] << 3) | _freqLSB[t] ;
	out(_chan, 0xa4, frq >> 8);
	out(_chan, 0xa0, frq & 0xff);
	out(_chan, 0x28, 0);
	out(_chan, 0x28, 0xf0/*0x30*/ /*???*/);
}

void TownsMidiOutputChannel::out(uint8 chan, uint8 reg, uint8 val) {
	static const uint8 chanRegOffs[] = { 0, 1, 2, 0, 1, 2 };
	static const uint8 keyValOffs[] = { 0, 1, 2, 4, 5, 6 };

	if (reg == 0x28)
		val = (val & 0xf0) | keyValOffs[chan];
	if (reg < 0x30)
		_driver->_intf->callback(19, 0, reg, val);
	else
		_driver->_intf->callback(19, chan / 3, (reg & ~3) | chanRegOffs[chan], val);
}

const uint8 TownsMidiOutputChannel::_freqMSB[] = {
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
	0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
	0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
	0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
	0x04, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
	0x05, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
	0x06, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x80, 0x81, 0x83, 0x85,
	0x87, 0x88, 0x8A, 0x8C, 0x8E, 0x8F, 0x91, 0x93, 0x95, 0x96, 0x98, 0x9A,
	0x9C, 0x9E, 0x9F, 0xA1, 0xA3, 0xA5, 0xA6, 0xA8, 0xAA, 0xAC, 0xAD, 0xAF,
	0xB1, 0xB3, 0xB4, 0xB6, 0xB8, 0xBA, 0xBC, 0xBD, 0xBF, 0xC1, 0xC3, 0xC4,
	0xC6, 0xC8, 0xCA, 0xCB, 0xCD, 0xCF, 0xD1, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA,
	0xDB, 0xDD, 0xDF, 0xE1, 0xE2, 0xE4, 0xE6, 0xE8, 0xE9, 0xEB, 0xED, 0xEF
};

const uint16 TownsMidiOutputChannel::_freqLSB[] = {
	0x02D6, 0x02D6, 0x02D6, 0x02D6, 0x02D6, 0x02D6, 0x02D6, 0x02D6,
	0x02D6, 0x02D6, 0x02D6, 0x02D6, 0x02D6, 0x02D6, 0x0301, 0x032F,
	0x0360, 0x0393, 0x03C9, 0x0403, 0x0440, 0x0481, 0x04C6, 0x050E,
	0x055B, 0x02D6, 0x0301, 0x032F, 0x0360, 0x0393, 0x03C9, 0x0403,
	0x0440, 0x0481, 0x04C6, 0x050E, 0x055B, 0x02D6, 0x0301, 0x032F,
	0x0360, 0x0393, 0x03C9, 0x0403, 0x0440, 0x0481, 0x04C6, 0x050E,
	0x055B, 0x02D6, 0x0301, 0x032F, 0x0360, 0x0393, 0x03C9, 0x0403,
	0x0440, 0x0481, 0x04C6, 0x050E, 0x055B, 0x02D6, 0x0301, 0x032F,
	0x0360, 0x0393, 0x03C9, 0x0403, 0x0440, 0x0481, 0x04C6, 0x050E,
	0x055B, 0x02D6, 0x0301, 0x032F, 0x0360, 0x0393, 0x03C9, 0x0403,
	0x0440, 0x0481, 0x04C6, 0x050E, 0x055B, 0x02D6, 0x0301, 0x032F,
	0x0360, 0x0393, 0x03C9, 0x0403, 0x0440, 0x0481, 0x04C6, 0x050E,
	0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B,
	0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B,
	0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B,
	0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B, 0x055B
};

TownsMidiInputChannel::TownsMidiInputChannel(MidiDriver_TOWNS *driver, int chanIndex) : MidiChannel(), _driver(driver), _outChan(0), _prg(0), _chanIndex(chanIndex),
	_effectLevel(0), _priority(0), _vol(0), _volEff(0), _pan(0), _panEff(0), _perc(0), _percS(0), _pitchBendFactor(0), _fld_22(0), _allocated(false) {
	_instrument = new uint8[30];
	memset(_instrument, 0, 30);
}

TownsMidiInputChannel::~TownsMidiInputChannel() {
	delete _instrument;
}

bool TownsMidiInputChannel::allocate() {
	if (_allocated)
		return false;
	_allocated = true;
	return true;
}

void TownsMidiInputChannel::release() {
	_allocated = false;
}

void TownsMidiInputChannel::send(uint32 b) {
	_driver->send(b | _chanIndex);
}

void TownsMidiInputChannel::noteOff(byte note) {
	if (!_outChan)
		return;

	if (_outChan->_note != note)
		return;

	if (_fld_22)
		_outChan->_noteOffMarker = 1;
	else
		_outChan->disconnect();
}

void TownsMidiInputChannel::noteOn(byte note, byte velocity) {
	TownsMidiOutputChannel *oc = _driver->allocateOutputChannel(_priority);
	
	if (!oc)
		return;

	oc->connect(this);

	
	int vol1 = 0;
	int vol2 = 0;
	oc->setupProgram(_instrument, vol1, vol2);
	//oc->noteOn(m, l);
	
}

void TownsMidiInputChannel::programChange(byte program) {

}

void TownsMidiInputChannel::pitchBend(int16 bend) {

}

void TownsMidiInputChannel::controlChange(byte control, byte value) {

}

void TownsMidiInputChannel::pitchBendFactor(byte value) {

}

void TownsMidiInputChannel::priority(byte value) {
	_priority = value;
}

void TownsMidiInputChannel::sysEx_customInstrument(uint32 type, const byte *instr) {
	memcpy(_instrument, instr, 30);
}

MidiDriver_TOWNS::MidiDriver_TOWNS(Audio::Mixer *mixer) : _timerBproc(0), _timerBpara(0), _open(false) {
	_intf = new TownsAudioInterface(mixer, this);

	_channels = new TownsMidiInputChannel*[32];
	for (int i = 0; i < 32; i++)
		_channels[i] = new TownsMidiInputChannel(this, i);
	_out = new TownsMidiOutputChannel*[6];
	for (int i = 0; i < 6; i++)
		_out[i] = new TownsMidiOutputChannel(this, i);

	_tickCounter = 0;
	_curChan = 0;
}

MidiDriver_TOWNS::~MidiDriver_TOWNS() {
	close();
	delete _intf;
	setTimerCallback(0, 0);

	for (int i = 0; i < 32; i++)
		delete _channels[i];
	delete[] _channels;
	for (int i = 0; i < 6; i++)
		delete _out[i];
	delete[] _out;
}

int MidiDriver_TOWNS::open() {
	if (_open)
		return MERR_ALREADY_OPEN;

	if (!_intf->init())
		return MERR_CANNOT_CONNECT;

	_intf->callback(0);

	_intf->callback(21, 255, 1);
	_intf->callback(21, 0, 1);
	_intf->callback(22, 255, 221);

	_intf->callback(33, 8);
	_intf->setSoundEffectChanMask(~0x3f);

	_open = true;

	return 0;
}

void MidiDriver_TOWNS::close() {
	_open = false;
}

void MidiDriver_TOWNS::send(uint32 b) {
	byte param2 = (b >> 16) & 0xFF;
	byte param1 = (b >> 8) & 0xFF;
	byte cmd = b & 0xF0;

	/*AdLibPart *part;
	if (chan == 9)
		part = &_percussion;
	else**/
	TownsMidiInputChannel *c = _channels[b & 0x0F];

	switch (cmd) {
	case 0x80:
		c->noteOff(param1);
		break;
	case 0x90:
		if (param2)
			c->noteOn(param1, param2);
		else
			c->noteOff(param1);
		break;
	case 0xB0:
		// supported: 1, 7, 0x40
		c->controlChange(param1, param2);
		break;
	case 0xC0:
		c->programChange(param1);
		break;
	case 0xE0:
		//part->pitchBend((param1 | (param2 << 7)) - 0x2000);
		c->pitchBend((param1 | (param2 << 7)) - 0x2000);
		break;
	case 0xF0:
		warning("MidiDriver_TOWNS: Receiving SysEx command on a send() call");
		break;

	default:
		break;
	}
}

void MidiDriver_TOWNS::setTimerCallback(void *timer_param, Common::TimerManager::TimerProc timer_proc) {
	_timerBproc = timer_proc;
	_timerBpara = timer_param;
}

uint32 MidiDriver_TOWNS::getBaseTempo() {
	return 4167;
}

MidiChannel *MidiDriver_TOWNS::allocateChannel() {
	for (int i = 0; i < 32; ++i) {		
		TownsMidiInputChannel *chan = _channels[i];
		if (chan->allocate())
			return chan;
	}

	return 0;
}

MidiChannel *MidiDriver_TOWNS::getPercussionChannel() {
	return 0;
}

void MidiDriver_TOWNS::timerCallback(int timerId) {
	if (!_open)
		return;

	switch (timerId) {
	case 1:
		if (_timerBproc) {
			_timerBproc(_timerBpara);
			_tickCounter += 10000;
			while (_tickCounter >= 4167) {
				_tickCounter -= 4167;
				//_timerBproc(_timerBpara);
			}
		}
		break;
	default:
		break;
	}
}

TownsMidiOutputChannel *MidiDriver_TOWNS::allocateOutputChannel(int pri) {
	TownsMidiOutputChannel *res = 0;

	for (int i = 0; i < 6; i++) {
		if (++_curChan == 6)
			_curChan = 0;

		int s = _out[i]->checkPriority(pri);
		if (s == TownsMidiOutputChannel::kDisconnected)
			return _out[i];

		if (s != TownsMidiOutputChannel::kHighPriority) {
			pri = s;
			res = _out[i];
		}
	}
	
	if (res)
		res->disconnect();

	return res;
}