aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/sound/drivers/amigamac.cpp
blob: 0ec4c283f78d1f1298b3b413094308b7d8a3584b (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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/* 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/emumidi.h"
#include "sci/sound/drivers/mididriver.h"
#include "sci/resource.h"

#include "common/file.h"
#include "common/frac.h"
#include "common/memstream.h"
#include "common/util.h"

namespace Sci {

/* #define DEBUG */

class MidiDriver_AmigaMac : public MidiDriver_Emulated {
public:
	enum {
		kVoices = 4
	};

	MidiDriver_AmigaMac(Audio::Mixer *mixer) : MidiDriver_Emulated(mixer), _playSwitch(true), _masterVolume(15) { }
	virtual ~MidiDriver_AmigaMac() { }

	// MidiDriver
	int open();
	void close();
	void send(uint32 b);
	MidiChannel *allocateChannel() { return NULL; }
	MidiChannel *getPercussionChannel() { return NULL; }

	// AudioStream
	bool isStereo() const { return true; }
	int getRate() const { return _mixer->getOutputRate(); }

	// MidiDriver_Emulated
	void generateSamples(int16 *buf, int len);

	void setVolume(byte volume);
	void playSwitch(bool play);
	virtual uint32 property(int prop, uint32 param);

private:
	enum {
		kModeLoop = 1 << 0, // Instrument looping flag
		kModePitch = 1 << 1 // Instrument pitch changes flag
	};

	enum {
		kChannels = 10,
		kBaseFreq = 20000, // Samplerate of the instrument bank
		kPanLeft = 91,
		kPanRight = 164
	};

	struct Channel {
		int instrument;
		int volume;
		int pan;
		uint16 pitch;
	};

	struct Envelope {
		int length; // Phase period length in samples
		int delta; // Velocity delta per period
		int target; // Target velocity
	};

	struct Voice {
		int instrument;
		int note;
		int note_velocity;
		int velocity;
		int envelope;
		int envelope_samples; // Number of samples till next envelope event
		int decay;
		int looping;
		int hw_channel;
		frac_t offset;
		frac_t rate;
	};

	struct InstrumentSample {
		char name[30];
		int mode;
		int size; // Size of non-looping part in bytes
		int loop_size; // Starting offset and size of loop in bytes
		int transpose; // Transpose value in semitones
		Envelope envelope[4]; // Envelope
		int8 *samples;
		int8 *loop;
		int16 startNote;
		int16 endNote;
		bool isUnsigned;
		uint16 baseFreq;
		uint16 baseNote;
		int16 fixedNote;
	};

	class Instrument : public Common::Array<InstrumentSample *> {
	public:
		char name[30];
	};

	struct Bank {
		char name[30];
		uint size;
		Common::Array<Instrument> instruments;
	};

	bool _isSci1;
	bool _playSwitch;
	int _masterVolume;
	int _frequency;
	Envelope _envDecay;
	Bank _bank; // Instrument bank
	double _freqTable[48];

	Channel _channels[MIDI_CHANNELS];
	/* Internal channels */
	Voice _voices[kChannels];

	void setEnvelope(Voice *channel, Envelope *envelope, int phase);
	void setOutputFrac(int voice);
	int interpolate(int8 *samples, frac_t offset, bool isUnsigned);
	void playInstrument(int16 *dest, Voice *channel, int count);
	void changeInstrument(int channel, int instrument);
	void stopChannel(int ch);
	void stopNote(int ch, int note);
	void startNote(int ch, int note, int velocity);
	InstrumentSample *findInstrument(int instrument, int note);
	void pitchWheel(int ch, uint16 pitch);

	bool loadInstrumentsSCI0(Common::File &file);
	bool loadInstrumentsSCI0Mac(Common::SeekableReadStream &file);
	InstrumentSample *readInstrumentSCI0(Common::SeekableReadStream &file, int *id);
	bool loadInstrumentsSCI1(Common::SeekableReadStream &file);
};

void MidiDriver_AmigaMac::setEnvelope(Voice *channel, Envelope *envelope, int phase) {
	channel->envelope = phase;
	channel->envelope_samples = envelope[phase].length;

	if (phase == 0)
		channel->velocity = channel->note_velocity / 2;
	else
		channel->velocity = envelope[phase - 1].target;
}

int MidiDriver_AmigaMac::interpolate(int8 *samples, frac_t offset, bool isUnsigned) {
	int x = fracToInt(offset);

	if (isUnsigned) {
		int s1 = (byte)samples[x] - 0x80;
		int s2 = (byte)samples[x + 1] - 0x80;
		int diff = (s2 - s1) << 8;
		return (s1 << 8) + fracToInt(diff * (offset & FRAC_LO_MASK));
	}
	
	int diff = (samples[x + 1] - samples[x]) << 8;
	return (samples[x] << 8) + fracToInt(diff * (offset & FRAC_LO_MASK));
}

void MidiDriver_AmigaMac::playInstrument(int16 *dest, Voice *channel, int count) {
	int index = 0;
	int vol = _channels[channel->hw_channel].volume;
	InstrumentSample *instrument = findInstrument(channel->instrument, channel->note);

	while (1) {
		/* Available source samples until end of segment */
		frac_t lin_avail;
		uint32 seg_end, rem, i, amount;
		int8 *samples;

		if (channel->looping && instrument->loop) {
			samples = instrument->loop;
			seg_end = instrument->loop_size;
		} else {
			samples = instrument->samples;
			seg_end = instrument->size;
		}

		lin_avail = intToFrac(seg_end) - channel->offset;

		rem = count - index;

		/* Amount of destination samples that we will compute this iteration */
		amount = lin_avail / channel->rate;

		if (lin_avail % channel->rate)
			amount++;

		if (amount > rem)
			amount = rem;

		/* Stop at next envelope event */
		if ((channel->envelope_samples != -1) && (amount > (uint32)channel->envelope_samples))
			amount = channel->envelope_samples;

		for (i = 0; i < amount; i++) {
			dest[index++] = interpolate(samples, channel->offset, instrument->isUnsigned) * channel->velocity / 64 * channel->note_velocity * vol / (127 * 127);
			channel->offset += channel->rate;
		}

		if (channel->envelope_samples != -1)
			channel->envelope_samples -= amount;

		if (channel->envelope_samples == 0) {
			Envelope *envelope;
			int delta, target, velocity;

			if (channel->decay)
				envelope = &_envDecay;
			else
				envelope = &instrument->envelope[channel->envelope];

			delta = envelope->delta;
			target = envelope->target;
			velocity = channel->velocity - envelope->delta;

			/* Check whether we have reached the velocity target for the current phase */
			if ((delta >= 0 && velocity <= target) || (delta < 0 && velocity >= target)) {
				channel->velocity = target;

				/* Stop note after velocity has dropped to 0 */
				if (target == 0) {
					channel->note = -1;
					break;
				} else
					switch (channel->envelope) {
					case 0:
					case 2:
						/* Go to next phase */
						setEnvelope(channel, instrument->envelope, channel->envelope + 1);
						break;
					case 1:
					case 3:
						/* Stop envelope */
						channel->envelope_samples = -1;
						break;
					}
			} else {
				/* We haven't reached the target yet */
				channel->envelope_samples = envelope->length;
				channel->velocity = velocity;
			}
		}

		if (index == count)
			break;

		if ((uint32)fracToInt(channel->offset) >= seg_end) {
			if (instrument->mode & kModeLoop) {
				/* Loop the samples */
				channel->offset -= intToFrac(seg_end);
				channel->looping = 1;
			} else {
				/* All samples have been played */
				channel->note = -1;
				break;
			}
		}
	}
}

void MidiDriver_AmigaMac::changeInstrument(int channel, int instrument) {
#ifdef DEBUG
	if (_bank.instruments[instrument][0])
		debugN("[sfx:seq:amiga] Setting channel %i to \"%s\" (%i)\n", channel, _bank.instruments[instrument]->name, instrument);
	else
		warning("[sfx:seq:amiga] instrument %i does not exist (channel %i)", instrument, channel);
#endif
	_channels[channel].instrument = instrument;
}

void MidiDriver_AmigaMac::stopChannel(int ch) {
	int i;

	/* Start decay phase for note on this hw channel, if any */
	for (i = 0; i < kChannels; i++)
		if (_voices[i].note != -1 && _voices[i].hw_channel == ch && !_voices[i].decay) {
			/* Trigger fast decay envelope */
			_voices[i].decay = 1;
			_voices[i].envelope_samples = _envDecay.length;
			break;
		}
}

void MidiDriver_AmigaMac::pitchWheel(int ch, uint16 pitch) {
	_channels[ch].pitch = pitch;

	for (int i = 0; i < kChannels; i++)
		if (_voices[i].note != -1 && _voices[i].hw_channel == ch)
			setOutputFrac(i);
}

void MidiDriver_AmigaMac::stopNote(int ch, int note) {
	int channel;

	for (channel = 0; channel < kChannels; channel++)
		if (_voices[channel].note == note && _voices[channel].hw_channel == ch && !_voices[channel].decay)
			break;

	if (channel == kChannels) {
#ifdef DEBUG
		warning("[sfx:seq:amiga] cannot stop note %i on channel %i", note, ch);
#endif
		return;
	}

	InstrumentSample *instrument = findInstrument(_voices[channel].instrument, note);

	// FIXME: SCI1 envelope support is not perfect yet

	/* Start the envelope phases for note-off if looping is on and envelope is enabled */
	if ((instrument->mode & kModeLoop) && (instrument->envelope[0].length != 0))
		setEnvelope(&_voices[channel], instrument->envelope, 2);
}

MidiDriver_AmigaMac::InstrumentSample *MidiDriver_AmigaMac::findInstrument(int instrument, int note) {
	if ((uint)instrument >= _bank.instruments.size())
		return 0;

	for (uint32 i = 0; i < _bank.instruments[instrument].size(); i++) {
		InstrumentSample *sample = _bank.instruments[instrument][i];
		if (note >= sample->startNote && note <= sample->endNote)
			return sample;
	}

	return 0;
}

void MidiDriver_AmigaMac::setOutputFrac(int voice) {
	InstrumentSample *instrument = findInstrument(_voices[voice].instrument, _voices[voice].note);

	int fnote = 0;

	if (instrument->fixedNote == -1) {
		fnote = _voices[voice].note;

		// Handle SCI0-style transposing here
		if (!_isSci1)
			fnote += instrument->transpose;

		if (fnote < 0 || fnote > 127) {
			warning("[sfx:seq:amiga] illegal note %i", fnote);
			return;
		}
	} else
		fnote = instrument->fixedNote;

	// Compute rate for note
	int mulFact = 1, divFact = 1;

	fnote -= instrument->baseNote;
	fnote *= 4;
	// FIXME: check how SSCI maps this
	fnote += (_channels[_voices[voice].hw_channel].pitch - 0x2000) / 169; 

	while (fnote < 0) {
		divFact *= 2;
		fnote += 12 * 4;
	}

	while (fnote >= 12 * 4) {
		mulFact *= 2;
		fnote -= 12 * 4;
	}

	double freq = _freqTable[fnote] * instrument->baseFreq * mulFact / divFact;

	// Handle SCI1-style transposing here
	if (instrument->transpose && _isSci1)
		freq = freq + ((_freqTable[4] - 1.0) * freq * (double)instrument->transpose / (double)16);

	_voices[voice].rate = doubleToFrac(freq / _frequency);
}

void MidiDriver_AmigaMac::startNote(int ch, int note, int velocity) {
	int channel;

	if (_channels[ch].instrument < 0 || _channels[ch].instrument > 255) {
		warning("[sfx:seq:amiga] invalid instrument %i on channel %i", _channels[ch].instrument, ch);
		return;
	}

	InstrumentSample *instrument = findInstrument(_channels[ch].instrument, note);

	if (!instrument) {
		warning("[sfx:seq:amiga] instrument %i does not exist", _channels[ch].instrument);
		return;
	}

	for (channel = 0; channel < kChannels; channel++)
		if (_voices[channel].note == -1)
			break;

	if (channel == kChannels) {
		warning("[sfx:seq:amiga] could not find a free channel");
		return;
	}

	stopChannel(ch);

	_voices[channel].instrument = _channels[ch].instrument;
	_voices[channel].note = note;
	_voices[channel].note_velocity = velocity;

	if ((instrument->mode & kModeLoop) && (instrument->envelope[0].length != 0))
		setEnvelope(&_voices[channel], instrument->envelope, 0);
	else {
		_voices[channel].velocity = 64;
		_voices[channel].envelope_samples = -1;
	}

	_voices[channel].offset = 0;
	_voices[channel].hw_channel = ch;
	_voices[channel].decay = 0;
	_voices[channel].looping = 0;
	setOutputFrac(channel);
}

MidiDriver_AmigaMac::InstrumentSample *MidiDriver_AmigaMac::readInstrumentSCI0(Common::SeekableReadStream &file, int *id) {
	byte header[61];

	if (file.read(header, 61) < 61) {
		warning("[sfx:seq:amiga] failed to read instrument header");
		return NULL;
	}

	int seg_size[3];
	seg_size[0] = READ_BE_UINT16(header + 35) * 2;
	seg_size[1] = READ_BE_UINT16(header + 41) * 2;
	seg_size[2] = READ_BE_UINT16(header + 47) * 2;

	InstrumentSample *instrument = new InstrumentSample;

	instrument->startNote = 0;
	instrument->endNote = 127;
	instrument->isUnsigned = false;
	instrument->baseFreq = kBaseFreq;
	instrument->baseNote = 101;
	instrument->fixedNote = 101;

	instrument->mode = header[33];
	instrument->transpose = (int8) header[34];
	for (int i = 0; i < 4; i++) {
		int length = (int8) header[49 + i];

		if (length == 0 && i > 0)
			length = 256;

		instrument->envelope[i].length = length * _frequency / 60;
		instrument->envelope[i].delta = (int8)header[53 + i];
		instrument->envelope[i].target = header[57 + i];
	}
	/* Final target must be 0 */
	instrument->envelope[3].target = 0;

	int loop_offset = READ_BE_UINT32(header + 37) & ~1;
	int size = seg_size[0] + seg_size[1] + seg_size[2];

	*id = READ_BE_UINT16(header);

	strncpy(instrument->name, (char *) header + 2, 29);
	instrument->name[29] = 0;

#ifdef DEBUG
	debugN("[sfx:seq:amiga] Reading instrument %i: \"%s\" (%i bytes)\n",
	          *id, instrument->name, size);
	debugN("                Mode: %02x\n", instrument->mode);
	debugN("                Looping: %s\n", instrument->mode & kModeLoop ? "on" : "off");
	debugN("                Pitch changes: %s\n", instrument->mode & kModePitch ? "on" : "off");
	debugN("                Segment sizes: %i %i %i\n", seg_size[0], seg_size[1], seg_size[2]);
	debugN("                Segment offsets: 0 %i %i\n", loop_offset, read_int32(header + 43));
#endif

	instrument->samples = (int8 *) malloc(size + 1);
	if (file.read(instrument->samples, size) < (unsigned int)size) {
		warning("[sfx:seq:amiga] failed to read instrument samples");
		free(instrument->samples);
		delete instrument;
		return NULL;
	}

	if (instrument->mode & kModePitch)
		instrument->fixedNote = -1;

	if (instrument->mode & kModeLoop) {
		if (loop_offset + seg_size[1] > size) {
#ifdef DEBUG
			warning("[sfx:seq:amiga] looping samples extend %i bytes past end of sample block",
			          loop_offset + seg_size[1] - size);
#endif
			seg_size[1] = size - loop_offset;
		}

		if (seg_size[1] < 0) {
			warning("[sfx:seq:amiga] invalid looping point");
			free(instrument->samples);
			delete instrument;
			return NULL;
		}

		instrument->size = seg_size[0];
		instrument->loop_size = seg_size[1];

		instrument->loop = (int8*)malloc(instrument->loop_size + 1);
		memcpy(instrument->loop, instrument->samples + loop_offset, instrument->loop_size);

		instrument->samples[instrument->size] = instrument->loop[0];
		instrument->loop[instrument->loop_size] = instrument->loop[0];
	} else {
		instrument->loop = NULL;
		instrument->loop_size = 0;
		instrument->size = size;
		instrument->samples[instrument->size] = 0;
	}

	return instrument;
}

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

int MidiDriver_AmigaMac::open() {
	_isSci1 = false;

	for (int i = 0; i < 48; i++)
		_freqTable[i] = pow(2, i / (double)48);

	_frequency = _mixer->getOutputRate();
	_envDecay.length = _frequency / (32 * 64);
	_envDecay.delta = 1;
	_envDecay.target = 0;

	for (uint i = 0; i < kChannels; i++) {
		_voices[i].note = -1;
		_voices[i].hw_channel = 0;
	}

	for (uint i = 0; i < MIDI_CHANNELS; i++) {
		_channels[i].instrument = -1;
		_channels[i].volume = 127;
		_channels[i].pan = (i % 4 == 0 || i % 4 == 3 ? kPanLeft : kPanRight);
		_channels[i].pitch = 0x2000;
	}

	Common::File file;

	if (file.open("bank.001")) {
		if (!loadInstrumentsSCI0(file)) {
			file.close();
			return Common::kUnknownError;
		}
		file.close();
	} else {
		ResourceManager *resMan = g_sci->getResMan();

		Resource *resource = resMan->findResource(ResourceId(kResourceTypePatch, 7), false);
		if (!resource)
			resource = resMan->findResource(ResourceId(kResourceTypePatch, 9), false);

		// If we have a patch by this point, it's SCI1
		if (resource)
			_isSci1 = true;

		// Check for the SCI0 Mac patch
		if (!resource)
			resource = resMan->findResource(ResourceId(kResourceTypePatch, 200), false);

		if (!resource) {
			warning("Could not open patch for Amiga sound driver");
			return Common::kUnknownError;
		}

		Common::MemoryReadStream stream(resource->data, resource->size);

		if (_isSci1) {
			if (!loadInstrumentsSCI1(stream))
				return Common::kUnknownError;
		} else if (!loadInstrumentsSCI0Mac(stream))
			return Common::kUnknownError;
	}
	
	MidiDriver_Emulated::open();

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

	return Common::kNoError;
}

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

	for (uint i = 0; i < _bank.size; i++) {
		for (uint32 j = 0; j < _bank.instruments[i].size(); j++) {
			if (_bank.instruments[i][j]) {
				if (_bank.instruments[i][j]->loop)
					free(_bank.instruments[i][j]->loop);
				free(_bank.instruments[i][j]->samples);
				delete _bank.instruments[i][j];
			}
		}
	}
}

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

void MidiDriver_AmigaMac::setVolume(byte volume_) {
	_masterVolume = volume_;
}

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

	switch (command) {
	case 0x80:
		stopNote(channel, op1);
		break;
	case 0x90:
		if (op2 > 0)
			startNote(channel, op1, op2);
		else
			stopNote(channel, op1);
		break;
	case 0xb0:
		switch (op1) {
		case 0x07:
			_channels[channel].volume = op2;
			break;
		case 0x0a:
#ifdef DEBUG
			warning("[sfx:seq:amiga] ignoring pan 0x%02x event for channel %i", op2, channel);
#endif
			break;
		case 0x7b:
			stopChannel(channel);
			break;
		default:
			warning("[sfx:seq:amiga] unknown control event 0x%02x", op1);
		}
		break;
	case 0xc0:
		changeInstrument(channel, op1);
		break;
	case 0xe0:
		pitchWheel(channel, (op2 << 7) | op1);
		break;
	default:
		warning("[sfx:seq:amiga] unknown event %02x", command);
	}
}

void MidiDriver_AmigaMac::generateSamples(int16 *data, int len) {
	if (len == 0)
		return;

	int16 *buffers = (int16*)malloc(len * 2 * kChannels);

	memset(buffers, 0, len * 2 * kChannels);

	/* Generate samples for all notes */
	for (int i = 0; i < kChannels; i++)
		if (_voices[i].note >= 0)
			playInstrument(buffers + i * len, &_voices[i], len);

	if (isStereo()) {
		for (int j = 0; j < len; j++) {
			int mixedl = 0, mixedr = 0;

			/* Mix and pan */
			for (int i = 0; i < kChannels; i++) {
				mixedl += buffers[i * len + j] * (256 - _channels[_voices[i].hw_channel].pan);
				mixedr += buffers[i * len + j] * _channels[_voices[i].hw_channel].pan;
			}

			/* Adjust volume */
			data[2 * j] = mixedl * _masterVolume >> 13;
			data[2 * j + 1] = mixedr * _masterVolume >> 13;
		}
	} else {
		for (int j = 0; j < len; j++) {
			int mixed = 0;

			/* Mix */
			for (int i = 0; i < kChannels; i++)
				mixed += buffers[i * len + j];

			/* Adjust volume */
			data[j] = mixed * _masterVolume >> 6;
		}
	}

	free(buffers);
}

bool MidiDriver_AmigaMac::loadInstrumentsSCI0(Common::File &file) {
	_isSci1 = false;

	byte header[40];

	if (file.read(header, 40) < 40) {
		warning("[sfx:seq:amiga] failed to read header of file bank.001");
		return false;
	}

	_bank.size = READ_BE_UINT16(header + 38);
	strncpy(_bank.name, (char *) header + 8, 29);
	_bank.name[29] = 0;
#ifdef DEBUG
	debugN("[sfx:seq:amiga] Reading %i instruments from bank \"%s\"\n", _bank.size, _bank.name);
#endif

	for (uint i = 0; i < _bank.size; i++) {
		int id;
		InstrumentSample *instrument = readInstrumentSCI0(file, &id);

		if (!instrument) {
			warning("[sfx:seq:amiga] failed to read bank.001");
			return false;
		}

		if (id < 0 || id > 255) {
			warning("[sfx:seq:amiga] Error: instrument ID out of bounds");
			return false;
		}

		if ((uint)id >= _bank.instruments.size())
			_bank.instruments.resize(id + 1);

		_bank.instruments[id].push_back(instrument);
		memcpy(_bank.instruments[id].name, instrument->name, sizeof(instrument->name));
	}

	return true;
}

bool MidiDriver_AmigaMac::loadInstrumentsSCI0Mac(Common::SeekableReadStream &file) {
	byte header[40];

	if (file.read(header, 40) < 40) {
		warning("[sfx:seq:amiga] failed to read header of file patch.200");
		return false;
	}

	_bank.size = 128;
	strncpy(_bank.name, (char *) header + 8, 29);
	_bank.name[29] = 0;
#ifdef DEBUG
	debugN("[sfx:seq:amiga] Reading %i instruments from bank \"%s\"\n", _bank.size, _bank.name);
#endif

	Common::Array<uint32> instrumentOffsets;
	instrumentOffsets.resize(_bank.size);
	_bank.instruments.resize(_bank.size);

	for (uint32 i = 0; i < _bank.size; i++)
		instrumentOffsets[i] = file.readUint32BE();

	for (uint i = 0; i < _bank.size; i++) {
		// 0 signifies it doesn't exist
		if (instrumentOffsets[i] == 0)
			continue;

		file.seek(instrumentOffsets[i]);

		uint16 id = file.readUint16BE();
		if (id != i)
			error("Instrument number mismatch");

		InstrumentSample *instrument = new InstrumentSample;

		instrument->startNote = 0;
		instrument->endNote = 127;
		instrument->isUnsigned = true;
		instrument->baseFreq = kBaseFreq;
		instrument->baseNote = 101;
		instrument->fixedNote = 101;
		instrument->mode = file.readUint16BE();

		// Read in the offsets
		int32 seg_size[3];
		seg_size[0] = file.readUint32BE();
		seg_size[1] = file.readUint32BE();
		seg_size[2] = file.readUint32BE();

		instrument->transpose = file.readUint16BE();

		for (byte j = 0; j < 4; j++) {
			int length = (int8)file.readByte();

			if (length == 0 && j > 0)
				length = 256;

			instrument->envelope[j].length = length * _frequency / 60;
			instrument->envelope[j].delta = (int8)file.readByte();
			instrument->envelope[j].target = file.readByte();
		}

		// Final target must be 0
		instrument->envelope[3].target = 0;

		file.read(instrument->name, 30);

		if (instrument->mode & kModePitch)
			instrument->fixedNote = -1;

		uint32 size = seg_size[2];
		uint32 loop_offset = seg_size[0];

		instrument->samples = (int8 *)malloc(size + 1);
		if (file.read(instrument->samples, size) < size) {
			warning("[sfx:seq:amiga] failed to read instrument sample");
			free(instrument->samples);
			delete instrument;
			continue;
		}

		if (instrument->mode & kModeLoop) {
			instrument->size = seg_size[0];
			instrument->loop_size = seg_size[1] - seg_size[0];

			instrument->loop = (int8*)malloc(instrument->loop_size + 1);
			memcpy(instrument->loop, instrument->samples + loop_offset, instrument->loop_size);

			instrument->samples[instrument->size] = instrument->loop[0];
			instrument->loop[instrument->loop_size] = instrument->loop[0];
		} else {
			instrument->loop = NULL;
			instrument->loop_size = 0;
			instrument->size = size;
			instrument->samples[instrument->size] = (int8)0x80;
		}

		_bank.instruments[id].push_back(instrument);
		memcpy(_bank.instruments[id].name, instrument->name, sizeof(instrument->name));
	}

	return true;
}

bool MidiDriver_AmigaMac::loadInstrumentsSCI1(Common::SeekableReadStream &file) {
	_bank.size = 128;

	Common::Array<uint32> instrumentOffsets;
	instrumentOffsets.resize(_bank.size);
	_bank.instruments.resize(_bank.size);

	for (uint32 i = 0; i < _bank.size; i++)
		instrumentOffsets[i] = file.readUint32BE();

	for (uint32 i = 0; i < _bank.size; i++) {
		// 0 signifies it doesn't exist
		if (instrumentOffsets[i] == 0)
			continue;

		file.seek(instrumentOffsets[i]);

		// Read in the instrument name
		file.read(_bank.instruments[i].name, 10); // last two bytes are always 0

		for (uint32 j = 0; ; j++) {
			InstrumentSample *sample = new InstrumentSample;
			memset(sample, 0, sizeof(InstrumentSample));

			sample->startNote = file.readSint16BE();

			// startNote being -1 signifies we're done with this instrument
			if (sample->startNote == -1) {
				delete sample;
				break;
			}

			sample->endNote = file.readSint16BE();
			uint32 samplePtr = file.readUint32BE();
			sample->transpose = file.readSint16BE();
			for (int env = 0; env < 3; env++) {
				sample->envelope[env].length = file.readByte() * _frequency / 60;
				sample->envelope[env].delta = (env == 0 ? 10 : -10);
				sample->envelope[env].target = file.readByte();
			}

			sample->envelope[3].length = 0;
			sample->fixedNote = file.readSint16BE();
			int16 loop = file.readSint16BE();
			uint32 nextSamplePos = file.pos();

			file.seek(samplePtr);
			file.read(sample->name, 8);

			sample->isUnsigned = file.readUint16BE() == 0;
			uint16 phase1Offset = file.readUint16BE();
			uint16 phase1End = file.readUint16BE();
			uint16 phase2Offset = file.readUint16BE();
			uint16 phase2End = file.readUint16BE();
			sample->baseNote = file.readUint16BE();
			uint32 periodTableOffset = file.readUint32BE();
			uint32 sampleDataPos = file.pos();

			sample->size = phase1End - phase1Offset + 1;
			sample->loop_size = phase2End - phase2Offset + 1;

			sample->samples = (int8 *)malloc(sample->size + 1);
			file.seek(phase1Offset + sampleDataPos);
			file.read(sample->samples, sample->size);
			sample->samples[sample->size] = (sample->isUnsigned ? (int8)0x80 : 0);

			if (loop == 0 && sample->loop_size > 1) {
				sample->loop = (int8 *)malloc(sample->loop_size + 1);
				file.seek(phase2Offset + sampleDataPos);
				file.read(sample->loop, sample->loop_size);
				sample->mode |= kModeLoop;
				sample->samples[sample->size] = sample->loop[0];
				sample->loop[sample->loop_size] = sample->loop[0];
			}

			_bank.instruments[i].push_back(sample);

			file.seek(periodTableOffset + 0xe0);
			sample->baseFreq = file.readUint16BE();

			file.seek(nextSamplePos);
		}
	}

	return true;
}

class MidiPlayer_AmigaMac : public MidiPlayer {
public:
	MidiPlayer_AmigaMac(SciVersion version) : MidiPlayer(version) { _driver = new MidiDriver_AmigaMac(g_system->getMixer()); }
	byte getPlayId() const;
	int getPolyphony() const { return MidiDriver_AmigaMac::kVoices; }
	bool hasRhythmChannel() const { return false; }
	void setVolume(byte volume) { static_cast<MidiDriver_AmigaMac *>(_driver)->setVolume(volume); }
	void playSwitch(bool play) { static_cast<MidiDriver_AmigaMac *>(_driver)->playSwitch(play); }
	void loadInstrument(int idx, byte *data);
};

MidiPlayer *MidiPlayer_AmigaMac_create(SciVersion version) {
	return new MidiPlayer_AmigaMac(version);
}

byte MidiPlayer_AmigaMac::getPlayId() const {
	if (_version > SCI_VERSION_0_LATE)
		return 0x06;

	return 0x40;
}

} // End of namespace Sci