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
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
|
/* 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/config-manager.h"
#include "common/file.h"
#include "common/textconsole.h"
#include "agos/agos.h"
#include "agos/midi.h"
#include "agos/drivers/accolade/mididriver.h"
// Miles Audio for Simon 2
#include "audio/miles.h"
#include "gui/message.h"
namespace AGOS {
// MidiParser_S1D is not considered part of the standard
// MidiParser suite, but we still try to mask its details
// and just provide a factory function.
extern MidiParser *MidiParser_createS1D();
MidiPlayer::MidiPlayer() {
// Since initialize() is called every time the music changes,
// this is where we'll initialize stuff that must persist
// between songs.
_driver = 0;
_map_mt32_to_gm = false;
_adlibPatches = NULL;
_enable_sfx = true;
_current = 0;
_musicVolume = 255;
_sfxVolume = 255;
resetVolumeTable();
_paused = false;
_currentTrack = 255;
_loopTrackDefault = false;
_queuedTrack = 255;
_loopQueuedTrack = 0;
_musicMode = kMusicModeDisabled;
}
MidiPlayer::~MidiPlayer() {
stop();
Common::StackLock lock(_mutex);
if (_driver) {
_driver->setTimerCallback(0, 0);
_driver->close();
delete _driver;
}
_driver = NULL;
clearConstructs();
unloadAdlibPatches();
}
int MidiPlayer::open(int gameType, bool isDemo) {
// Don't call open() twice!
assert(!_driver);
Common::String accoladeDriverFilename;
MusicType musicType = MT_INVALID;
switch (gameType) {
case GType_ELVIRA1:
_musicMode = kMusicModeAccolade;
accoladeDriverFilename = "INSTR.DAT";
break;
case GType_ELVIRA2:
case GType_WW:
// Attention: Elvira 2 shipped with INSTR.DAT and MUSIC.DRV
// MUSIC.DRV is the correct one. INSTR.DAT seems to be a left-over
_musicMode = kMusicModeAccolade;
accoladeDriverFilename = "MUSIC.DRV";
break;
case GType_SIMON1:
if (isDemo) {
_musicMode = kMusicModeAccolade;
accoladeDriverFilename = "MUSIC.DRV";
}
break;
case GType_SIMON2:
//_musicMode = kMusicModeMilesAudio;
// currently disabled, because there are a few issues
// MT32 seems to work fine now, AdLib seems to use bad instruments and is also outputting music on
// the right speaker only. The original driver did initialize the panning to 0 and the Simon2 XMIDI
// tracks don't set panning at all. We can reset panning to be centered, which would solve this
// issue, but we still don't know who's setting it in the original interpreter.
break;
default:
break;
}
MidiDriver::DeviceHandle dev;
int ret = 0;
if (_musicMode != kMusicModeDisabled) {
dev = MidiDriver::detectDevice(MDT_MIDI | MDT_ADLIB | MDT_PREFER_MT32);
musicType = MidiDriver::getMusicType(dev);
switch (musicType) {
case MT_ADLIB:
case MT_MT32:
break;
case MT_GM:
if (!ConfMan.getBool("native_mt32")) {
// Not a real MT32 / no MUNT
::GUI::MessageDialog dialog(("You appear to be using a General MIDI device,\n"
"but your game only supports Roland MT32 MIDI.\n"
"We try to map the Roland MT32 instruments to\n"
"General MIDI ones. It is still possible that\n"
"some tracks sound incorrect."));
dialog.runModal();
}
// Switch to MT32 driver in any case
musicType = MT_MT32;
break;
default:
_musicMode = kMusicModeDisabled;
break;
}
}
switch (_musicMode) {
case kMusicModeAccolade: {
// Setup midi driver
switch (musicType) {
case MT_ADLIB:
_driver = MidiDriver_Accolade_AdLib_create(accoladeDriverFilename);
break;
case MT_MT32:
_driver = MidiDriver_Accolade_MT32_create(accoladeDriverFilename);
break;
default:
assert(0);
break;
}
if (!_driver)
return 255;
ret = _driver->open();
if (ret == 0) {
// Reset is done inside our MIDI driver
_driver->setTimerCallback(this, &onTimer);
}
//setTimerRate(_driver->getBaseTempo());
return 0;
}
case kMusicModeMilesAudio: {
switch (musicType) {
case MT_ADLIB: {
Common::File instrumentDataFile;
if (instrumentDataFile.exists("MIDPAK.AD")) {
// if there is a file called MIDPAK.AD, use it directly
warning("SIMON 2: using MIDPAK.AD");
_driver = Audio::MidiDriver_Miles_AdLib_create("MIDPAK.AD", "MIDPAK.AD");
} else {
// if there is no file called MIDPAK.AD, try to extract it from the file SETUP.SHR
// if we didn't do this, the user would be forced to "install" the game instead of simply
// copying all files from CD-ROM.
const byte *instrumentRawData = NULL;
uint32 instrumentRawDataSize = 0;
instrumentRawData = simon2SetupExtractFile("MIDPAK.AD", instrumentRawDataSize);
if (!instrumentRawData)
error("MidiPlayer: could not extract MIDPAK.AD from SETUP.SHR");
// Pass this extracted data to the driver
warning("SIMON 2: using MIDPAK.AD extracted from SETUP.SHR");
_driver = Audio::MidiDriver_Miles_AdLib_create("", "", instrumentRawData, instrumentRawDataSize);
delete[] instrumentRawData;
}
// TODO: not sure what's going wrong with AdLib
// it doesn't seem to matter if we use the regular XMIDI tracks or the 2nd set meant for MT32
break;
}
case MT_MT32:
_driver = Audio::MidiDriver_Miles_MT32_create("");
_nativeMT32 = true; // use 2nd set of XMIDI tracks
break;
case MT_GM:
if (ConfMan.getBool("native_mt32")) {
_driver = Audio::MidiDriver_Miles_MT32_create("");
_nativeMT32 = true; // use 2nd set of XMIDI tracks
}
break;
default:
break;
}
if (!_driver)
return 255;
ret = _driver->open();
if (ret == 0) {
// Reset is done inside our MIDI driver
_driver->setTimerCallback(this, &onTimer);
}
return 0;
}
default:
break;
}
dev = MidiDriver::detectDevice(MDT_ADLIB | MDT_MIDI | (gameType == GType_SIMON1 ? MDT_PREFER_MT32 : MDT_PREFER_GM));
_nativeMT32 = ((MidiDriver::getMusicType(dev) == MT_MT32) || ConfMan.getBool("native_mt32"));
_driver = MidiDriver::createMidi(dev);
if (!_driver)
return 255;
if (_nativeMT32)
_driver->property(MidiDriver::PROP_CHANNEL_MASK, 0x03FE);
/* Disabled due to not sounding right, and low volume level
if (gameType == GType_SIMON1 && MidiDriver::getMusicType(dev) == MT_ADLIB) {
loadAdlibPatches();
}
*/
_map_mt32_to_gm = (gameType != GType_SIMON2 && !_nativeMT32);
ret = _driver->open();
if (ret)
return ret;
_driver->setTimerCallback(this, &onTimer);
if (_nativeMT32)
_driver->sendMT32Reset();
else
_driver->sendGMReset();
return 0;
}
void MidiPlayer::send(uint32 b) {
if (!_current)
return;
if (_musicMode != kMusicModeDisabled) {
// Send directly to Accolade/Miles Audio driver
_driver->send(b);
return;
}
byte channel = (byte)(b & 0x0F);
if ((b & 0xFFF0) == 0x07B0) {
// Adjust volume changes by master music and master sfx volume.
byte volume = (byte)((b >> 16) & 0x7F);
_current->volume[channel] = volume;
if (_current == &_sfx)
volume = volume * _sfxVolume / 255;
else if (_current == &_music)
volume = volume * _musicVolume / 255;
b = (b & 0xFF00FFFF) | (volume << 16);
} else if ((b & 0xF0) == 0xC0) {
if (_map_mt32_to_gm && !_adlibPatches) {
b = (b & 0xFFFF00FF) | (MidiDriver::_mt32ToGm[(b >> 8) & 0xFF] << 8);
}
} else if ((b & 0xFFF0) == 0x007BB0) {
// Only respond to an All Notes Off if this channel
// has already been allocated.
if (!_current->channel[b & 0x0F])
return;
} else if ((b & 0xFFF0) == 0x79B0) {
// "Reset All Controllers". There seems to be some confusion
// about what this message should do to the volume controller.
// See http://www.midi.org/about-midi/rp15.shtml for more
// information.
//
// If I understand it correctly, the current standard indicates
// that the volume should be reset, but the next revision will
// exclude it. On my system, both ALSA and FluidSynth seem to
// reset it, while AdLib does not. Let's follow the majority.
_current->volume[channel] = 127;
}
// Allocate channels if needed
if (!_current->channel[channel])
_current->channel[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
if (_current->channel[channel]) {
if (channel == 9) {
if (_current == &_sfx)
_current->channel[9]->volume(_current->volume[9] * _sfxVolume / 255);
else if (_current == &_music)
_current->channel[9]->volume(_current->volume[9] * _musicVolume / 255);
}
if ((b & 0xF0) == 0xC0 && _adlibPatches) {
// NOTE: In the percussion channel, this function is a
// no-op. Any percussion instruments you hear may
// be the stock ones from adlib.cpp.
_driver->sysEx_customInstrument(_current->channel[channel]->getNumber(), 'ADL ', _adlibPatches + 30 * ((b >> 8) & 0xFF));
} else {
_current->channel[channel]->send(b);
}
if ((b & 0xFFF0) == 0x79B0) {
// We have received a "Reset All Controllers" message
// and passed it on to the MIDI driver. This may or may
// not have affected the volume controller. To ensure
// consistent behavior, explicitly set the volume to
// what we think it should be.
if (_current == &_sfx)
_current->channel[channel]->volume(_current->volume[channel] * _sfxVolume / 255);
else if (_current == &_music)
_current->channel[channel]->volume(_current->volume[channel] * _musicVolume / 255);
}
}
}
void MidiPlayer::metaEvent(byte type, byte *data, uint16 length) {
// Only thing we care about is End of Track.
if (!_current || type != 0x2F) {
return;
} else if (_current == &_sfx) {
clearConstructs(_sfx);
} else if (_current->loopTrack) {
_current->parser->jumpToTick(0);
} else if (_queuedTrack != 255) {
_currentTrack = 255;
byte destination = _queuedTrack;
_queuedTrack = 255;
_current->loopTrack = _loopQueuedTrack;
_loopQueuedTrack = false;
// Remember, we're still inside the locked mutex.
// Have to unlock it before calling jump()
// (which locks it itself), and then relock it
// upon returning.
_mutex.unlock();
startTrack(destination);
_mutex.lock();
} else {
stop();
}
}
void MidiPlayer::onTimer(void *data) {
MidiPlayer *p = (MidiPlayer *)data;
Common::StackLock lock(p->_mutex);
if (!p->_paused) {
if (p->_music.parser && p->_currentTrack != 255) {
p->_current = &p->_music;
p->_music.parser->onTimer();
}
}
if (p->_sfx.parser) {
p->_current = &p->_sfx;
p->_sfx.parser->onTimer();
}
p->_current = 0;
}
void MidiPlayer::startTrack(int track) {
Common::StackLock lock(_mutex);
if (track == _currentTrack)
return;
if (_music.num_songs > 0) {
if (track >= _music.num_songs)
return;
if (_music.parser) {
_current = &_music;
delete _music.parser;
_current = 0;
_music.parser = 0;
}
MidiParser *parser = MidiParser::createParser_SMF();
parser->property (MidiParser::mpMalformedPitchBends, 1);
parser->setMidiDriver(this);
parser->setTimerRate(_driver->getBaseTempo());
if (!parser->loadMusic(_music.songs[track], _music.song_sizes[track])) {
warning("Error reading track %d", track);
delete parser;
parser = 0;
}
_currentTrack = (byte)track;
_music.parser = parser; // That plugs the power cord into the wall
} else if (_music.parser) {
if (!_music.parser->setTrack(track)) {
return;
}
_currentTrack = (byte)track;
_current = &_music;
_music.parser->jumpToTick(0);
_current = 0;
}
}
void MidiPlayer::stop() {
Common::StackLock lock(_mutex);
if (_music.parser) {
_current = &_music;
_music.parser->jumpToTick(0);
}
_current = 0;
_currentTrack = 255;
}
void MidiPlayer::pause(bool b) {
if (_paused == b || !_driver)
return;
_paused = b;
Common::StackLock lock(_mutex);
for (int i = 0; i < 16; ++i) {
if (_music.channel[i])
_music.channel[i]->volume(_paused ? 0 : (_music.volume[i] * _musicVolume / 255));
if (_sfx.channel[i])
_sfx.channel[i]->volume(_paused ? 0 : (_sfx.volume[i] * _sfxVolume / 255));
}
}
void MidiPlayer::setVolume(int musicVol, int sfxVol) {
if (musicVol < 0)
musicVol = 0;
else if (musicVol > 255)
musicVol = 255;
if (sfxVol < 0)
sfxVol = 0;
else if (sfxVol > 255)
sfxVol = 255;
if (_musicVolume == musicVol && _sfxVolume == sfxVol)
return;
_musicVolume = musicVol;
_sfxVolume = sfxVol;
// Now tell all the channels this.
Common::StackLock lock(_mutex);
if (_driver && !_paused) {
for (int i = 0; i < 16; ++i) {
if (_music.channel[i])
_music.channel[i]->volume(_music.volume[i] * _musicVolume / 255);
if (_sfx.channel[i])
_sfx.channel[i]->volume(_sfx.volume[i] * _sfxVolume / 255);
}
}
}
void MidiPlayer::setLoop(bool loop) {
Common::StackLock lock(_mutex);
_loopTrackDefault = loop;
}
void MidiPlayer::queueTrack(int track, bool loop) {
_mutex.lock();
if (_currentTrack == 255) {
_mutex.unlock();
setLoop(loop);
startTrack(track);
} else {
_queuedTrack = track;
_loopQueuedTrack = loop;
_mutex.unlock();
}
}
void MidiPlayer::clearConstructs() {
clearConstructs(_music);
clearConstructs(_sfx);
}
void MidiPlayer::clearConstructs(MusicInfo &info) {
int i;
if (info.num_songs > 0) {
for (i = 0; i < info.num_songs; ++i)
free(info.songs[i]);
info.num_songs = 0;
}
free(info.data);
info.data = 0;
delete info.parser;
info.parser = 0;
if (_driver) {
for (i = 0; i < 16; ++i) {
if (info.channel[i]) {
info.channel[i]->allNotesOff();
info.channel[i]->release();
}
}
}
info.clear();
}
void MidiPlayer::resetVolumeTable() {
int i;
for (i = 0; i < 16; ++i) {
_music.volume[i] = _sfx.volume[i] = 127;
if (_driver)
_driver->send(((_musicVolume >> 1) << 16) | 0x7B0 | i);
}
}
void MidiPlayer::loadAdlibPatches() {
Common::File ibk;
if (!ibk.open("mt_fm.ibk"))
return;
if (ibk.readUint32BE() == 0x49424b1a) {
_adlibPatches = new byte[128 * 30];
byte *ptr = _adlibPatches;
memset(_adlibPatches, 0, 128 * 30);
for (int i = 0; i < 128; i++) {
byte instr[16];
ibk.read(instr, 16);
ptr[0] = instr[0]; // Modulator Sound Characteristics
ptr[1] = instr[2]; // Modulator Scaling/Output Level
ptr[2] = ~instr[4]; // Modulator Attack/Decay
ptr[3] = ~instr[6]; // Modulator Sustain/Release
ptr[4] = instr[8]; // Modulator Wave Select
ptr[5] = instr[1]; // Carrier Sound Characteristics
ptr[6] = instr[3]; // Carrier Scaling/Output Level
ptr[7] = ~instr[5]; // Carrier Attack/Delay
ptr[8] = ~instr[7]; // Carrier Sustain/Release
ptr[9] = instr[9]; // Carrier Wave Select
ptr[10] = instr[10]; // Feedback/Connection
// The remaining six bytes are reserved for future use
ptr += 30;
}
}
}
void MidiPlayer::unloadAdlibPatches() {
delete[] _adlibPatches;
_adlibPatches = NULL;
}
static const int simon1_gmf_size[] = {
8900, 12166, 2848, 3442, 4034, 4508, 7064, 9730, 6014, 4742, 3138,
6570, 5384, 8909, 6457, 16321, 2742, 8968, 4804, 8442, 7717,
9444, 5800, 1381, 5660, 6684, 2456, 4744, 2455, 1177, 1232,
17256, 5103, 8794, 4884, 16
};
void MidiPlayer::loadSMF(Common::File *in, int song, bool sfx) {
Common::StackLock lock(_mutex);
MusicInfo *p = sfx ? &_sfx : &_music;
clearConstructs(*p);
uint32 startpos = in->pos();
byte header[4];
in->read(header, 4);
bool isGMF = !memcmp(header, "GMF\x1", 4);
in->seek(startpos, SEEK_SET);
uint32 size = in->size() - in->pos();
if (isGMF) {
if (sfx) {
// Multiple GMF resources are stored in the SFX files,
// but each one is referenced by a pointer at the
// beginning of the file. Those pointers can be used
// to determine file size.
in->seek(0, SEEK_SET);
uint16 value = in->readUint16LE() >> 2; // Number of resources
if (song != value - 1) {
in->seek(song * 2 + 2, SEEK_SET);
value = in->readUint16LE();
size = value - startpos;
}
in->seek(startpos, SEEK_SET);
} else if (size >= 64000) {
// For GMF resources not in separate
// files, we're going to have to use
// hardcoded size tables.
size = simon1_gmf_size[song];
}
}
// When allocating space, add 4 bytes in case
// this is a GMF and we have to tack on our own
// End of Track event.
p->data = (byte *)calloc(size + 4, 1);
in->read(p->data, size);
uint32 timerRate = _driver->getBaseTempo();
if (isGMF) {
// The GMF header
// 3 BYTES: 'GMF'
// 1 BYTE : Major version
// 1 BYTE : Minor version
// 1 BYTE : Ticks (Ranges from 2 - 8, always 2 for SFX)
// 1 BYTE : Loop control. 0 = no loop, 1 = loop
// In the original, the ticks value indicated how many
// times the music timer was called before it actually
// did something. The larger the value the slower the
// music.
//
// We, on the other hand, have a timer rate which is
// used to control by how much the music advances on
// each onTimer() call. The larger the value, the
// faster the music.
//
// It seems that 4 corresponds to our base tempo, so
// this should be the right way to calculate it.
timerRate = (4 * _driver->getBaseTempo()) / p->data[5];
p->loopTrack = (p->data[6] != 0);
} else {
p->loopTrack = _loopTrackDefault;
}
MidiParser *parser = MidiParser::createParser_SMF();
parser->property(MidiParser::mpMalformedPitchBends, 1);
parser->setMidiDriver(this);
parser->setTimerRate(timerRate);
if (!parser->loadMusic(p->data, size)) {
warning("Error reading track");
delete parser;
parser = 0;
}
if (!sfx) {
_currentTrack = 255;
resetVolumeTable();
}
p->parser = parser; // That plugs the power cord into the wall
}
void MidiPlayer::loadMultipleSMF(Common::File *in, bool sfx) {
// This is a special case for Simon 2 Windows.
// Instead of having multiple sequences as
// separate tracks in a Type 2 file, simon2win
// has multiple songs, each of which is a Type 1
// file. Thus, preceding the songs is a single
// byte specifying how many songs are coming.
// We need to load ALL the songs and then
// treat them as separate tracks -- for the
// purpose of jumps, anyway.
Common::StackLock lock(_mutex);
MusicInfo *p = sfx ? &_sfx : &_music;
clearConstructs(*p);
p->num_songs = in->readByte();
if (p->num_songs > 16) {
warning("playMultipleSMF: %d is too many songs to keep track of", (int)p->num_songs);
return;
}
byte i;
for (i = 0; i < p->num_songs; ++i) {
byte buf[5];
uint32 pos = in->pos();
// Make sure there's a MThd
in->read(buf, 4);
if (memcmp(buf, "MThd", 4) != 0) {
warning("Expected MThd but found '%c%c%c%c' instead", buf[0], buf[1], buf[2], buf[3]);
return;
}
in->seek(in->readUint32BE(), SEEK_CUR);
// Now skip all the MTrk blocks
while (true) {
in->read(buf, 4);
if (memcmp(buf, "MTrk", 4) != 0)
break;
in->seek(in->readUint32BE(), SEEK_CUR);
}
uint32 pos2 = in->pos() - 4;
uint32 size = pos2 - pos;
p->songs[i] = (byte *)calloc(size, 1);
in->seek(pos, SEEK_SET);
in->read(p->songs[i], size);
p->song_sizes[i] = size;
}
p->loopTrack = _loopTrackDefault;
if (!sfx) {
_currentTrack = 255;
resetVolumeTable();
}
}
void MidiPlayer::loadXMIDI(Common::File *in, bool sfx) {
Common::StackLock lock(_mutex);
MusicInfo *p = sfx ? &_sfx : &_music;
clearConstructs(*p);
char buf[4];
uint32 pos = in->pos();
uint32 size = 4;
in->read(buf, 4);
if (!memcmp(buf, "FORM", 4)) {
int i;
for (i = 0; i < 16; ++i) {
if (!memcmp(buf, "CAT ", 4))
break;
size += 2;
memcpy(buf, &buf[2], 2);
in->read(&buf[2], 2);
}
if (memcmp(buf, "CAT ", 4) != 0) {
error("Could not find 'CAT ' tag to determine resource size");
}
size += 4 + in->readUint32BE();
in->seek(pos, 0);
p->data = (byte *)calloc(size, 1);
in->read(p->data, size);
p->loopTrack = _loopTrackDefault;
} else {
error("Expected 'FORM' tag but found '%c%c%c%c' instead", buf[0], buf[1], buf[2], buf[3]);
}
// In the DOS version of Simon the Sorcerer 2, the music contains lots
// of XMIDI callback controller events. As far as we know, they aren't
// actually used, so we disable the callback handler explicitly.
MidiParser *parser = MidiParser::createParser_XMIDI(NULL);
parser->setMidiDriver(this);
parser->setTimerRate(_driver->getBaseTempo());
if (!parser->loadMusic(p->data, size))
error("Error reading track");
if (!sfx) {
_currentTrack = 255;
resetVolumeTable();
}
p->parser = parser; // That plugs the power cord into the wall
}
void MidiPlayer::loadS1D(Common::File *in, bool sfx) {
Common::StackLock lock(_mutex);
MusicInfo *p = sfx ? &_sfx : &_music;
clearConstructs(*p);
uint16 size = in->readUint16LE();
if (size != in->size() - 2) {
error("Size mismatch in MUS file (%ld versus reported %d)", (long)in->size() - 2, (int)size);
}
p->data = (byte *)calloc(size, 1);
in->read(p->data, size);
MidiParser *parser = MidiParser_createS1D();
parser->setMidiDriver(this);
parser->setTimerRate(_driver->getBaseTempo());
if (!parser->loadMusic(p->data, size))
error("Error reading track");
if (!sfx) {
_currentTrack = 255;
resetVolumeTable();
}
p->loopTrack = _loopTrackDefault;
p->parser = parser; // That plugs the power cord into the wall
}
#define MIDI_SETUP_BUNDLE_HEADER_SIZE 56
#define MIDI_SETUP_BUNDLE_FILEHEADER_SIZE 48
#define MIDI_SETUP_BUNDLE_FILENAME_MAX_SIZE 12
// PKWARE data compression is used for storing files within SETUP.SHR
// we need it to be able to get the file MIDPAK.AD, otherwise we would have to require the user
// to "install" the game before being able to actually play it, when using AdLib.
//
// SETUP.SHR file format:
// [bundle file header]
// [compressed file header] [compressed file data]
// * compressed file count
const byte *MidiPlayer::simon2SetupExtractFile(const Common::String &requestedFileName, uint32 &extractedDataSize) {
Common::File *setupBundleStream = new Common::File();
uint32 bundleSize = 0;
uint32 bundleBytesLeft = 0;
byte bundleHeader[MIDI_SETUP_BUNDLE_HEADER_SIZE];
byte bundleFileHeader[MIDI_SETUP_BUNDLE_FILEHEADER_SIZE];
uint16 bundleFileCount = 0;
uint16 bundleFileNr = 0;
Common::String fileName;
uint32 fileCompressedSize = 0;
byte *fileCompressedDataPtr = nullptr;
const byte *extractedDataPtr = nullptr;
extractedDataSize = 0;
if (!setupBundleStream->open("setup.shr"))
error("MidiPlayer: could not open setup.shr");
bundleSize = setupBundleStream->size();
bundleBytesLeft = bundleSize;
if (bundleSize < MIDI_SETUP_BUNDLE_HEADER_SIZE)
error("MidiPlayer: unexpected EOF in setup.shr");
if (setupBundleStream->read(bundleHeader, MIDI_SETUP_BUNDLE_HEADER_SIZE) != MIDI_SETUP_BUNDLE_HEADER_SIZE)
error("MidiPlayer: setup.shr read error");
bundleBytesLeft -= MIDI_SETUP_BUNDLE_HEADER_SIZE;
// Verify header byte
if (bundleHeader[13] != 't')
error("MidiPlayer: setup.shr bundle header data mismatch");
bundleFileCount = READ_LE_UINT16(&bundleHeader[14]);
// Search for requested file
while (bundleFileNr < bundleFileCount) {
if (bundleBytesLeft < sizeof(bundleFileHeader))
error("MidiPlayer: unexpected EOF in setup.shr");
if (setupBundleStream->read(bundleFileHeader, sizeof(bundleFileHeader)) != sizeof(bundleFileHeader))
error("MidiPlayer: setup.shr read error");
bundleBytesLeft -= MIDI_SETUP_BUNDLE_FILEHEADER_SIZE;
// Extract filename from file-header
fileName.clear();
for (byte curPos = 0; curPos < MIDI_SETUP_BUNDLE_FILENAME_MAX_SIZE; curPos++) {
if (!bundleFileHeader[curPos]) // terminating NUL
break;
fileName.insertChar(bundleFileHeader[curPos], curPos);
}
// Get compressed
fileCompressedSize = READ_LE_UINT32(&bundleFileHeader[20]);
if (!fileCompressedSize)
error("MidiPlayer: compressed file is 0 bytes, data corruption?");
if (bundleBytesLeft < fileCompressedSize)
error("MidiPlayer: unexpected EOF in setup.shr");
if (fileName == requestedFileName) {
// requested file found
fileCompressedDataPtr = new byte[fileCompressedSize];
if (setupBundleStream->read(fileCompressedDataPtr, fileCompressedSize) != fileCompressedSize)
error("MidiPlayer: setup.shr read error");
// now extract the data
extractedDataPtr = simon2SetupDecompressFile(fileCompressedDataPtr, fileCompressedSize, extractedDataSize);
break;
}
// skip compressed size
setupBundleStream->skip(fileCompressedSize);
bundleBytesLeft -= fileCompressedSize;
bundleFileNr++;
}
setupBundleStream->close();
delete setupBundleStream;
return extractedDataPtr;
}
static byte simon2SetupBitsMask[9] = {
0,
0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF
};
// gets [bitCount] bits from dataPtr, going from LSB to MSB
inline uint16 MidiPlayer::simon2SetupGetBits(byte bitCount, const byte *&dataPtr, const byte *dataEndPtr, byte &dataBitsLeft) {
byte resultBitsLeft = bitCount;
byte resultBitsPos = 0;
uint16 result = 0;
byte currentByte = *dataPtr;
byte currentBits = 0;
// Get bits of current byte
while (resultBitsLeft) {
if (resultBitsLeft < dataBitsLeft) {
// we need less than we have left
currentBits = (currentByte >> (8 - dataBitsLeft)) & simon2SetupBitsMask[resultBitsLeft];
result |= (currentBits << resultBitsPos);
dataBitsLeft -= resultBitsLeft;
resultBitsLeft = 0;
} else {
// we need as much as we have left or more
resultBitsLeft -= dataBitsLeft;
currentBits = currentByte >> (8 - dataBitsLeft);
result |= (currentBits << resultBitsPos);
resultBitsPos += dataBitsLeft;
// Go to next byte
dataPtr++;
if (dataPtr >= dataEndPtr)
error("MidiPlayer: setup.shr: unexpected end of compressed data stream");
dataBitsLeft = 8;
if (resultBitsLeft) {
currentByte = *dataPtr;
}
}
}
return result;
}
// Decode length from bitstream
inline uint16 MidiPlayer::simon2SetupGetLength(const byte *&dataPtr, const byte *dataEndPtr, byte &dataBitsLeft) {
uint16 bits;
bits = simon2SetupGetBits(2, dataPtr, dataEndPtr, dataBitsLeft);
switch (bits) {
case 3:
return 3; // 11b
case 2:
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits)
return 5; // 011b
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits)
return 6; // 0101b
return 7;
case 1:
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits)
return 2; // 101b
return 4; // 100b
case 0:
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits) {
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits)
return 8; // 0011b
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits)
return 9; // 00101b
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits)
return 11; // 001001b
return 10; // 001000b
} else {
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits) {
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits) {
bits = simon2SetupGetBits(2, dataPtr, dataEndPtr, dataBitsLeft);
return 12 + bits; // 00011XXb
} else {
bits = simon2SetupGetBits(3, dataPtr, dataEndPtr, dataBitsLeft);
return 16 + bits; // 00010XXXb
}
} else {
bits = simon2SetupGetBits(2, dataPtr, dataEndPtr, dataBitsLeft);
switch (bits) {
case 3:
bits = simon2SetupGetBits(4, dataPtr, dataEndPtr, dataBitsLeft);
return 24 + bits; // 000011XXXXb
case 2:
bits = simon2SetupGetBits(6, dataPtr, dataEndPtr, dataBitsLeft);
return 72 + bits; // 000001XXXXXXb
case 1:
bits = simon2SetupGetBits(5, dataPtr, dataEndPtr, dataBitsLeft);
return 40 + bits; // 000010XXXXXb
case 0:
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits) {
bits = simon2SetupGetBits(7, dataPtr, dataEndPtr, dataBitsLeft);
return 136 + bits; // 0000001XXXXXXXXb
} else {
bits = simon2SetupGetBits(8, dataPtr, dataEndPtr, dataBitsLeft);
return 264 + bits; // 0000000XXXXXXXXb
}
default:
break;
}
}
}
break;
default:
break;
}
return 0;
}
// Decode offset from bitstream
inline uint16 MidiPlayer::simon2SetupGetOffset(byte lowOrderBits, const byte *&dataPtr, const byte *dataEndPtr, byte &dataBitsLeft) {
uint16 baseOffset = 0;
uint16 bits = 0;
bits = simon2SetupGetBits(2, dataPtr, dataEndPtr, dataBitsLeft);
switch (bits) {
case 3:
baseOffset = 0; // 11b
break;
case 2:
bits = simon2SetupGetBits(4, dataPtr, dataEndPtr, dataBitsLeft);
if (bits) {
baseOffset = 0x16 - simon2SetupReverseBits(bits, 4);
} else {
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
baseOffset = 0x17 - bits;
}
break;
case 1:
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits) {
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits) {
baseOffset = 0x01;
} else {
baseOffset = 0x02;
}
} else {
bits = simon2SetupGetBits(2, dataPtr, dataEndPtr, dataBitsLeft);
baseOffset = 0x06 - simon2SetupReverseBits(bits, 2);
}
break;
case 0:
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits) {
bits = simon2SetupGetBits(4, dataPtr, dataEndPtr, dataBitsLeft);
baseOffset = 0x27 - simon2SetupReverseBits(bits, 4);
} else {
bits = simon2SetupGetBits(1, dataPtr, dataEndPtr, dataBitsLeft);
if (bits) {
bits = simon2SetupGetBits(3, dataPtr, dataEndPtr, dataBitsLeft);
baseOffset = 0x2F - simon2SetupReverseBits(bits, 3);
} else {
bits = simon2SetupGetBits(4, dataPtr, dataEndPtr, dataBitsLeft);
baseOffset = 0x3F - simon2SetupReverseBits(bits, 4);
}
}
break;
default:
break;
}
bits = simon2SetupGetBits(lowOrderBits, dataPtr, dataEndPtr, dataBitsLeft);
return (baseOffset << lowOrderBits) + bits;
}
inline uint16 MidiPlayer::simon2SetupReverseBits(uint16 bits, byte bitCount) {
uint16 result = 0;
for (byte bitNr = 0; bitNr < bitCount; bitNr++) {
result = (result << 1) | (bits & 0x01);
bits = bits >> 1;
}
return result;
}
#define MIDI_SETUP_BUNDLE_FILE_MAXIMUM_DICTIONARY_SIZE 4096
// Implementation of "PKWARE data compression library" decompression
// Based on information released by Ben Rudiak-Gould in comp.compression on 13.8.2001
const byte *MidiPlayer::simon2SetupDecompressFile(const byte *compressedDataPtr, uint32 compressedDataSize, uint32 &extractedDataSize) {
byte compressedLiteralType = 0;
byte compressedDictionaryType = 0;
uint16 compressedDictionarySize = 0;
const byte *readDataPtr = compressedDataPtr;
const byte *readDataEndPtr = compressedDataPtr + compressedDataSize;
uint32 readBytesLeft = compressedDataSize;
// check if there are at least 3 bytes of compressed data
if (readBytesLeft < 3)
error("MidiPlayer: setup.shr compressed file data not enough bytes");
compressedLiteralType = readDataPtr[0];
compressedDictionaryType = readDataPtr[1];
readDataPtr += 2;
readBytesLeft -= 2;
if (compressedLiteralType != 0)
error("MidiPlayer: setup.shr: unsupported variable length literals");
switch(compressedDictionaryType) {
case 4:
compressedDictionarySize = 1024;
break;
case 5:
compressedDictionarySize = 2048;
break;
case 6:
compressedDictionarySize = 4096;
break;
default:
error("MidiPlayer: setup.shr: invalid dictionary size");
break;
}
byte dataBitsLeft = 8;
byte tokenType = 0;
byte tokenLiteral = 0;
byte tokenLowOrderBits = 0;
uint16 tokenLength = 0;
uint16 tokenOffset = 0;
byte dictionary[MIDI_SETUP_BUNDLE_FILE_MAXIMUM_DICTIONARY_SIZE];
uint16 dictionaryPos = 0;
byte *outputDataPtr = nullptr;
byte *outputPtr = nullptr;
uint32 outputSize = 0;
// First calculate the size of the uncompressed data
do {
tokenType = simon2SetupGetBits(1, readDataPtr, readDataEndPtr, dataBitsLeft);
if (!tokenType) {
// literal
tokenLiteral = simon2SetupGetBits(8, readDataPtr, readDataEndPtr, dataBitsLeft);
outputSize++;
} else {
// length+offset
tokenLength = simon2SetupGetLength(readDataPtr, readDataEndPtr, dataBitsLeft);
if (tokenLength == 519)
break; // end of data
tokenLowOrderBits = (tokenLength == 2) ? 2 : compressedDictionaryType;
tokenOffset = simon2SetupGetOffset(tokenLowOrderBits, readDataPtr, readDataEndPtr, dataBitsLeft);
outputSize += tokenLength;
}
} while (1);
// allocate output buffer
outputDataPtr = new byte[outputSize];
outputPtr = outputDataPtr;
// reset everything
dataBitsLeft = 8;
readDataPtr = compressedDataPtr + 2;
dictionaryPos = 0;
do {
tokenType = simon2SetupGetBits(1, readDataPtr, readDataEndPtr, dataBitsLeft);
if (!tokenType) {
// literal
tokenLiteral = simon2SetupGetBits(8, readDataPtr, readDataEndPtr, dataBitsLeft);
// write literal to output buffer
*outputPtr = tokenLiteral;
outputPtr++;
dictionary[dictionaryPos] = tokenLiteral;
dictionaryPos++;
if (dictionaryPos >= compressedDictionarySize)
dictionaryPos = 0;
} else {
// length+offset
tokenLength = simon2SetupGetLength(readDataPtr, readDataEndPtr, dataBitsLeft);
if (tokenLength == 519)
break; // end of data
tokenLowOrderBits = (tokenLength == 2) ? 2 : compressedDictionaryType;
tokenOffset = simon2SetupGetOffset(tokenLowOrderBits, readDataPtr, readDataEndPtr, dataBitsLeft);
uint16 dictionaryBaseIndex = (dictionaryPos - 1 - tokenOffset) & (compressedDictionarySize - 1);
uint16 dictionaryIndex = dictionaryBaseIndex;
uint16 dictionaryNextIndex = dictionaryPos;
uint16 copyBytesLeft = tokenLength;
while (copyBytesLeft) {
// write byte from dictionary
*outputPtr = dictionary[dictionaryIndex];
outputPtr++;
dictionary[dictionaryNextIndex] = dictionary[dictionaryIndex];
dictionaryNextIndex++; dictionaryIndex++;
if (dictionaryIndex >= dictionaryPos)
dictionaryIndex = dictionaryBaseIndex;
if (dictionaryNextIndex >= compressedDictionarySize)
dictionaryNextIndex = 0;
copyBytesLeft--;
}
dictionaryPos = dictionaryNextIndex;
}
} while (1);
extractedDataSize = outputSize;
return outputDataPtr;
}
} // End of namespace AGOS
|