aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/sound/drivers/cms.cpp
blob: dbcbf3d43168c80f466fba5c0a993e0f58071751 (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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/* 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 "sci/sound/drivers/mididriver.h"

#include "audio/softsynth/emumidi.h"
#include "audio/softsynth/cms.h"
#include "audio/mixer.h"

#include "common/system.h"

#include "sci/resource.h"

namespace Sci {

// FIXME: We don't seem to be sending the polyphony init data, so disable this for now
#define CMS_DISABLE_VOICE_MAPPING

class MidiDriver_CMS : public MidiDriver_Emulated {
public:
	MidiDriver_CMS(Audio::Mixer *mixer, ResourceManager *resMan)
	    : MidiDriver_Emulated(mixer), _resMan(resMan), _cms(0), _rate(0), _playSwitch(true), _masterVolume(0) {
	}

	int open();
	void close();

	void send(uint32 b);
	uint32 property(int prop, uint32 param);

	MidiChannel *allocateChannel() { return 0; }
	MidiChannel *getPercussionChannel() { return 0; }

	bool isStereo() const { return true; }
	int getRate() const { return _rate; }

	void playSwitch(bool play);
private:
	void generateSamples(int16 *buffer, int len);

	ResourceManager *_resMan;
	CMSEmulator *_cms;

	void writeToChip1(int address, int data);
	void writeToChip2(int address, int data);

	int32 _samplesPerCallback;
	int32 _samplesPerCallbackRemainder;
	int32 _samplesTillCallback;
	int32 _samplesTillCallbackRemainder;

	int _rate;
	bool _playSwitch;
	uint16 _masterVolume;

	uint8 *_patchData;

	struct Channel {
		Channel()
		    : patch(0), volume(0), pan(0x40), hold(0), extraVoices(0),
		      pitchWheel(0x2000), pitchModifier(0), pitchAdditive(false),
		      lastVoiceUsed(0) {
		}

		uint8 patch;
		uint8 volume;
		uint8 pan;
		uint8 hold;
		uint8 extraVoices;
		uint16 pitchWheel;
		uint8 pitchModifier;
		bool pitchAdditive;
		uint8 lastVoiceUsed;
	};

	Channel _channel[16];

	struct Voice {
		Voice() : channel(0xFF), note(0xFF), sustained(0xFF), ticks(0),
		    turnOffTicks(0), patchDataPtr(0), patchDataIndex(0),
		    amplitudeTimer(0), amplitudeModifier(0), turnOff(false),
		    velocity(0) {
		}

		uint8 channel;
		uint8 note;
		uint8 sustained;
		uint16 ticks;
		uint16 turnOffTicks;
		const uint8 *patchDataPtr;
		uint8 patchDataIndex;
		uint8 amplitudeTimer;
		uint8 amplitudeModifier;
		bool turnOff;
		uint8 velocity;
	};

	Voice _voice[12];

	void voiceOn(int voice, int note, int velocity);
	void voiceOff(int voice);

	void noteSend(int voice);

	void noteOn(int channel, int note, int velocity);
	void noteOff(int channel, int note);
	void controlChange(int channel, int control, int value);
	void pitchWheel(int channel, int value);

	void voiceMapping(int channel, int value);
	void bindVoices(int channel, int voices);
	void unbindVoices(int channel, int voices);
	void donateVoices();
	int findVoice(int channel);

	int findVoiceBasic(int channel);

	void updateVoiceAmplitude(int voice);
	void setupVoiceAmplitude(int voice);

	uint8 _octaveRegs[2][3];

	static const int _timerFreq = 60;

	static const int _frequencyTable[];
	static const int _velocityTable[];
};

const int MidiDriver_CMS::_frequencyTable[] = {
	  3,  10,  17,  24,
	 31,  38,  46,  51,
	 58,  64,  71,  77,
	 83,  89,  95, 101,
	107, 113, 119, 124,
	130, 135, 141, 146,
	151, 156, 162, 167,
	172, 177, 182, 186,
	191, 196, 200, 205,
	209, 213, 217, 222,
	226, 230, 234, 238,
	242, 246, 250, 253
};

const int MidiDriver_CMS::_velocityTable[] = {
	 1,  3,  6,  8,  9, 10, 11, 12,
	12, 13, 13, 14, 14, 14, 15, 15,
	 0,  1,  2,  2,  3,  4,  4,  5,
	 6,  6,  7,  8,  8,  9, 10, 10
};

int MidiDriver_CMS::open() {
	if (_cms)
		return MERR_ALREADY_OPEN;

	assert(_resMan);
	Resource *res = _resMan->findResource(ResourceId(kResourceTypePatch, 101), 0);
	if (!res)
		return -1;

	_patchData = new uint8[res->size];
	memcpy(_patchData, res->data, res->size);

	for (uint i = 0; i < ARRAYSIZE(_channel); ++i)
		_channel[i] = Channel();

	for (uint i = 0; i < ARRAYSIZE(_voice); ++i)
		_voice[i] = Voice();

	_rate = _mixer->getOutputRate();
	_cms = new CMSEmulator(_rate);
	assert(_cms);
	_playSwitch = true;
	_masterVolume = 0;

	for (int i = 0; i < 31; ++i) {
		writeToChip1(i, 0);
		writeToChip2(i, 0);
	}

	writeToChip1(0x14, 0xFF);
	writeToChip2(0x14, 0xFF);

	writeToChip1(0x1C, 1);
	writeToChip2(0x1C, 1);

	_samplesPerCallback = getRate() / _timerFreq;
	_samplesPerCallbackRemainder = getRate() % _timerFreq;
	_samplesTillCallback = 0;
	_samplesTillCallbackRemainder = 0;

	int retVal = MidiDriver_Emulated::open();
	if (retVal != 0)
		return retVal;

	_mixer->playStream(Audio::Mixer::kPlainSoundType, &_mixerSoundHandle, this, -1, _mixer->kMaxChannelVolume, 0, DisposeAfterUse::NO);
	return 0;
}

void MidiDriver_CMS::close() {
	_mixer->stopHandle(_mixerSoundHandle);

	delete[] _patchData;
	delete _cms;
	_cms = 0;
}

void MidiDriver_CMS::send(uint32 b) {
	const uint8 command = b & 0xf0;
	const uint8 channel = b & 0xf;
	const uint8 op1 = (b >> 8) & 0xff;
	const uint8 op2 = (b >> 16) & 0xff;

	switch (command) {
	case 0x80:
		noteOff(channel, op1);
		break;

	case 0x90:
		noteOn(channel, op1, op2);
		break;

	case 0xB0:
		controlChange(channel, op1, op2);
		break;

	case 0xC0:
		_channel[channel].patch = op1;
		break;

	case 0xE0:
		pitchWheel(channel, (op1 & 0x7f) | ((op2 & 0x7f) << 7));
		break;

	default:
		break;
	}
}

uint32 MidiDriver_CMS::property(int prop, uint32 param) {
	switch (prop) {
	case MIDI_PROP_MASTER_VOLUME:
		if (param != 0xffff)
			_masterVolume = param;
		return _masterVolume;

	default:
		return MidiDriver_Emulated::property(prop, param);
	}
}

void MidiDriver_CMS::playSwitch(bool play) {
	_playSwitch = play;
}

void MidiDriver_CMS::writeToChip1(int address, int data) {
	_cms->portWrite(0x221, address);
	_cms->portWrite(0x220, data);

	if (address >= 16 && address <= 18)
		_octaveRegs[0][address - 16] = data;
}

void MidiDriver_CMS::writeToChip2(int address, int data) {
	_cms->portWrite(0x223, address);
	_cms->portWrite(0x222, data);

	if (address >= 16 && address <= 18)
		_octaveRegs[1][address - 16] = data;
}

void MidiDriver_CMS::voiceOn(int voiceNr, int note, int velocity) {
	Voice &voice = _voice[voiceNr];
	voice.note = note;
	voice.turnOff = false;
	voice.patchDataIndex = 0;
	voice.amplitudeTimer = 0;
	voice.ticks = 0;
	voice.turnOffTicks = 0;
	voice.patchDataPtr = _patchData + READ_LE_UINT16(&_patchData[_channel[voice.channel].patch * 2]);
	if (velocity)
		velocity = _velocityTable[(velocity >> 3)];
	voice.velocity = velocity;
	noteSend(voiceNr);
}

void MidiDriver_CMS::voiceOff(int voiceNr) {
	Voice &voice = _voice[voiceNr];
	voice.velocity = 0;
	voice.note = 0xFF;
	voice.sustained = 0;
	voice.turnOff = false;
	voice.patchDataIndex = 0;
	voice.amplitudeTimer = 0;
	voice.amplitudeModifier = 0;
	voice.ticks = 0;
	voice.turnOffTicks = 0;

	setupVoiceAmplitude(voiceNr);
}

void MidiDriver_CMS::noteSend(int voiceNr) {
	Voice &voice = _voice[voiceNr];

	int frequency = (CLIP<int>(voice.note, 21, 116) - 21) * 4;
	if (_channel[voice.channel].pitchModifier) {
		int modifier = _channel[voice.channel].pitchModifier;

		if (!_channel[voice.channel].pitchAdditive) {
			if (frequency > modifier)
				frequency -= modifier;
			else
				frequency = 0;
		} else {
			int tempFrequency = 384 - frequency;
			if (modifier < tempFrequency)
				frequency += modifier;
			else
				frequency = 383;
		}
	}

	int chipNumber = 0;
	if (voiceNr >= 6) {
		voiceNr -= 6;
		chipNumber = 1;
	}

	int octave = 0;
	while (frequency >= 48) {
		frequency -= 48;
		++octave;
	}

	frequency = _frequencyTable[frequency];

	if (chipNumber == 1)
		writeToChip2(8 + voiceNr, frequency);
	else
		writeToChip1(8 + voiceNr, frequency);

	uint8 octaveData = _octaveRegs[chipNumber][voiceNr >> 1];

	if (voiceNr & 1) {
		octaveData &= 0x0F;
		octaveData |= (octave << 4);
	} else {
		octaveData &= 0xF0;
		octaveData |= octave;
	}

	if (chipNumber == 1)
		writeToChip2(0x10 + (voiceNr >> 1), octaveData);
	else
		writeToChip1(0x10 + (voiceNr >> 1), octaveData);
}

void MidiDriver_CMS::noteOn(int channel, int note, int velocity) {
	if (note < 21 || note > 116)
		return;

	if (velocity == 0) {
		noteOff(channel, note);
		return;
	}

	for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
		if (_voice[i].channel == channel && _voice[i].note == note) {
			_voice[i].sustained = 0;
			voiceOff(i);
			voiceOn(i, note, velocity);
			return;
		}
	}

#ifdef CMS_DISABLE_VOICE_MAPPING
	int voice = findVoiceBasic(channel);
#else
	int voice = findVoice(channel);
#endif
	if (voice != -1)
		voiceOn(voice, note, velocity);
}

int MidiDriver_CMS::findVoiceBasic(int channel) {
	int voice = -1;
	int oldestVoice = -1;
	int oldestAge = -1;

	// Try to find a voice assigned to this channel that is free (round-robin)
	for (int i = 0; i < ARRAYSIZE(_voice); i++) {
		int v = (_channel[channel].lastVoiceUsed + i + 1) % ARRAYSIZE(_voice);

		if (_voice[v].note == 0xFF) {
			voice = v;
			break;
		}

		// We also keep track of the oldest note in case the search fails
		if (_voice[v].ticks > oldestAge) {
			oldestAge = _voice[v].ticks;
			oldestVoice = v;
		}
	}

	if (voice == -1) {
		if (oldestVoice != -1) {
			voiceOff(oldestVoice);
			voice = oldestVoice;
		} else {
			return -1;
		}
	}

	_voice[voice].channel = channel;
	_channel[channel].lastVoiceUsed = voice;
	return voice;
}

void MidiDriver_CMS::noteOff(int channel, int note) {
	for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
		if (_voice[i].channel == channel && _voice[i].note == note) {
			if (_channel[channel].hold != 0)
				_voice[i].sustained = true;
			else
				_voice[i].turnOff = true;
		}
	}
}

void MidiDriver_CMS::controlChange(int channel, int control, int value) {
	switch (control) {
	case 7:
		if (value) {
			value >>= 3;
			if (!value)
				++value;
		}

		_channel[channel].volume = value;
		break;

	case 10:
		_channel[channel].pan = value;
		break;

	case 64:
		_channel[channel].hold = value;

		if (!value) {
			for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
				if (_voice[i].channel == channel && _voice[i].sustained) {
					_voice[i].sustained = 0;
					_voice[i].turnOff = true;
				}
			}
		}
		break;

	case 75:
#ifndef CMS_DISABLE_VOICE_MAPPING
		voiceMapping(channel, value);
#endif
		break;

	case 123:
		for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
			if (_voice[i].channel == channel && _voice[i].note != 0xFF)
				voiceOff(i);
		}
		break;

	default:
		return;
	}
}

void MidiDriver_CMS::pitchWheel(int channelNr, int value) {
	Channel &channel = _channel[channelNr];
	channel.pitchWheel = value;
	channel.pitchAdditive = false;
	channel.pitchModifier = 0;

	if (value < 0x2000) {
		channel.pitchModifier = (0x2000 - value) / 170;
	} else if (value > 0x2000) {
		channel.pitchModifier = (value - 0x2000) / 170;
		channel.pitchAdditive = true;
	}

	for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
		if (_voice[i].channel == channelNr && _voice[i].note != 0xFF)
			noteSend(i);
	}
}

void MidiDriver_CMS::voiceMapping(int channelNr, int value) {
	int curVoices = 0;

	for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
		if (_voice[i].channel == channelNr)
			++curVoices;
	}

	curVoices += _channel[channelNr].extraVoices;

	if (curVoices == value) {
		return;
	} else if (curVoices < value) {
		bindVoices(channelNr, value - curVoices);
	} else {
		unbindVoices(channelNr, curVoices - value);
		donateVoices();
	}
}

void MidiDriver_CMS::bindVoices(int channel, int voices) {
	for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
		if (_voice[i].channel == 0xFF)
			continue;

		Voice &voice = _voice[i];
		voice.channel = channel;

		if (voice.note != 0xFF)
			voiceOff(i);

		--voices;
		if (voices == 0)
			break;
	}

	_channel[channel].extraVoices += voices;

	// The original called "PatchChange" here, since this just
	// copies the value of _channel[channel].patch to itself
	// it is left out here though.
}

void MidiDriver_CMS::unbindVoices(int channelNr, int voices) {
	Channel &channel = _channel[channelNr];

	if (channel.extraVoices >= voices) {
		channel.extraVoices -= voices;
	} else {
		voices -= channel.extraVoices;
		channel.extraVoices = 0;

		for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
			if (_voice[i].channel == channelNr
			    && _voice[i].note == 0xFF) {
				--voices;
				if (voices == 0)
					return;
			}
		}

		do {
			uint16 voiceTime = 0;
			uint voiceNr = 0;

			for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
				if (_voice[i].channel != channelNr)
					continue;

				uint16 curTime = _voice[i].turnOffTicks;
				if (curTime)
					curTime += 0x8000;
				else
					curTime = _voice[i].ticks;

				if (curTime >= voiceTime) {
					voiceNr = i;
					voiceTime = curTime;
				}
			}

			_voice[voiceNr].sustained = 0;
			voiceOff(voiceNr);
			_voice[voiceNr].channel = 0xFF;
			--voices;
		} while (voices != 0);
	}
}

void MidiDriver_CMS::donateVoices() {
	int freeVoices = 0;

	for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
		if (_voice[i].channel == 0xFF)
			++freeVoices;
	}

	if (!freeVoices)
		return;

	for (uint i = 0; i < ARRAYSIZE(_channel); ++i) {
		Channel &channel = _channel[i];

		if (!channel.extraVoices) {
			continue;
		} else if (channel.extraVoices < freeVoices) {
			freeVoices -= channel.extraVoices;
			channel.extraVoices = 0;
			bindVoices(i, channel.extraVoices);
		} else {
			channel.extraVoices -= freeVoices;
			bindVoices(i, freeVoices);
			return;
		}
	}
}

int MidiDriver_CMS::findVoice(int channelNr) {
	Channel &channel = _channel[channelNr];
	int voiceNr = channel.lastVoiceUsed;

	int newVoice = 0;
	uint16 newVoiceTime = 0;

	bool loopDone = false;
	do {
		++voiceNr;

		if (voiceNr == 12)
			voiceNr = 0;

		Voice &voice = _voice[voiceNr];

		if (voiceNr == channel.lastVoiceUsed)
			loopDone = true;

		if (voice.channel == channelNr) {
			if (voice.note == 0xFF) {
				channel.lastVoiceUsed = voiceNr;
				return voiceNr;
			}

			uint16 curTime = voice.turnOffTicks;
			if (curTime)
				curTime += 0x8000;
			else
				curTime = voice.ticks;

			if (curTime >= newVoiceTime) {
				newVoice = voiceNr;
				newVoiceTime = curTime;
			}
		}
	} while (!loopDone);

	if (newVoiceTime > 0) {
		voiceNr = newVoice;
		_voice[voiceNr].sustained = 0;
		voiceOff(voiceNr);
		channel.lastVoiceUsed = voiceNr;
		return voiceNr;
	} else {
		return -1;
	}
}

void MidiDriver_CMS::updateVoiceAmplitude(int voiceNr) {
	Voice &voice = _voice[voiceNr];

	if (voice.amplitudeTimer != 0 && voice.amplitudeTimer != 254) {
		--voice.amplitudeTimer;
		return;
	} else if (voice.amplitudeTimer == 254) {
		if (!voice.turnOff)
			return;

		voice.amplitudeTimer = 0;
	}

	int nextDataIndex = voice.patchDataIndex;
	uint8 timerData = 0;
	uint8 amplitudeData = voice.patchDataPtr[nextDataIndex];

	if (amplitudeData == 255) {
		timerData = amplitudeData = 0;
		voiceOff(voiceNr);
	} else {
		timerData = voice.patchDataPtr[nextDataIndex + 1];
		nextDataIndex += 2;
	}

	voice.patchDataIndex = nextDataIndex;
	voice.amplitudeTimer = timerData;
	voice.amplitudeModifier = amplitudeData;
}

void MidiDriver_CMS::setupVoiceAmplitude(int voiceNr) {
	Voice &voice = _voice[voiceNr];
	uint amplitude = 0;

	if (_channel[voice.channel].volume && voice.velocity
	    && voice.amplitudeModifier && _masterVolume) {
		amplitude = _channel[voice.channel].volume * voice.velocity;
		amplitude /= 0x0F;
		amplitude *= voice.amplitudeModifier;
		amplitude /= 0x0F;
		amplitude *= _masterVolume;
		amplitude /= 0x0F;

		if (!amplitude)
			++amplitude;
	}

	uint8 amplitudeData = 0;
	int pan = _channel[voice.channel].pan >> 2;
	if (pan >= 16) {
		amplitudeData = (amplitude * (31 - pan) / 0x0F) & 0x0F;
		amplitudeData |= (amplitude << 4);
	} else {
		amplitudeData = (amplitude * pan / 0x0F) & 0x0F;
		amplitudeData <<= 4;
		amplitudeData |= amplitude;
	}

	if (!_playSwitch)
		amplitudeData = 0;

	if (voiceNr >= 6)
		writeToChip2(voiceNr - 6, amplitudeData);
	else
		writeToChip1(voiceNr, amplitudeData);
}

void MidiDriver_CMS::generateSamples(int16 *buffer, int len) {
	while (len) {
		if (!_samplesTillCallback) {
			for (uint i = 0; i < ARRAYSIZE(_voice); ++i) {
				if (_voice[i].note == 0xFF)
					continue;

				++_voice[i].ticks;
				if (_voice[i].turnOff)
					++_voice[i].turnOffTicks;

				updateVoiceAmplitude(i);
				setupVoiceAmplitude(i);
			}

			_samplesTillCallback = _samplesPerCallback;
			_samplesTillCallbackRemainder += _samplesPerCallbackRemainder;
			if (_samplesTillCallbackRemainder >= _timerFreq) {
				_samplesTillCallback++;
				_samplesTillCallbackRemainder -= _timerFreq;
			}
		}

		int32 render = MIN<int32>(len, _samplesTillCallback);
		len -= render;
		_samplesTillCallback -= render;
		_cms->readBuffer(buffer, render);
		buffer += render * 2;
	}
}


class MidiPlayer_CMS : public MidiPlayer {
public:
	MidiPlayer_CMS(SciVersion version) : MidiPlayer(version) {
	}

	int open(ResourceManager *resMan) {
		if (_driver)
			return MidiDriver::MERR_ALREADY_OPEN;

		_driver = new MidiDriver_CMS(g_system->getMixer(), resMan);
		int driverRetVal = _driver->open();
		if (driverRetVal != 0)
			return driverRetVal;

		return 0;
	}

	void close() {
		_driver->setTimerCallback(0, 0);
		_driver->close();
		delete _driver;
		_driver = 0;
	}

	bool hasRhythmChannel() const { return false; }
	byte getPlayId() const { return 9; }
	int getPolyphony() const { return 12; }

	void playSwitch(bool play) { static_cast<MidiDriver_CMS *>(_driver)->playSwitch(play); }
};

MidiPlayer *MidiPlayer_CMS_create(SciVersion version) {
	return new MidiPlayer_CMS(version);
}

} // End of namespace SCI