PageRenderTime 56ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/engines/agos/midi.cpp

http://github.com/scummvm/scummvm
C++ | 893 lines | 617 code | 130 blank | 146 comment | 189 complexity | ffa77c29496784f7182e590f53c5f3ea MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  1. /* ScummVM - Graphic Adventure Engine
  2. *
  3. * ScummVM is the legal property of its developers, whose names
  4. * are too numerous to list here. Please refer to the COPYRIGHT
  5. * file distributed with this source distribution.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. *
  21. */
  22. #include "common/config-manager.h"
  23. #include "common/file.h"
  24. #include "common/textconsole.h"
  25. #include "common/memstream.h"
  26. #include "agos/agos.h"
  27. #include "agos/midi.h"
  28. #include "agos/drivers/accolade/mididriver.h"
  29. #include "agos/drivers/accolade/adlib.h"
  30. #include "agos/drivers/accolade/mt32.h"
  31. #include "agos/drivers/simon1/adlib.h"
  32. // Miles Audio for Simon 2
  33. #include "audio/miles.h"
  34. // PKWARE data compression library decompressor required for Simon 2
  35. #include "common/dcl.h"
  36. #include "gui/message.h"
  37. namespace AGOS {
  38. // MidiParser_S1D is not considered part of the standard
  39. // MidiParser suite, but we still try to mask its details
  40. // and just provide a factory function.
  41. extern MidiParser *MidiParser_createS1D();
  42. MidiPlayer::MidiPlayer() {
  43. // Since initialize() is called every time the music changes,
  44. // this is where we'll initialize stuff that must persist
  45. // between songs.
  46. _driver = 0;
  47. _map_mt32_to_gm = false;
  48. _adLibMusic = false;
  49. _enable_sfx = true;
  50. _current = 0;
  51. _musicVolume = 255;
  52. _sfxVolume = 255;
  53. resetVolumeTable();
  54. _paused = false;
  55. _currentTrack = 255;
  56. _loopTrack = 0;
  57. _queuedTrack = 255;
  58. _loopQueuedTrack = 0;
  59. _musicMode = kMusicModeDisabled;
  60. }
  61. MidiPlayer::~MidiPlayer() {
  62. stop();
  63. Common::StackLock lock(_mutex);
  64. if (_driver) {
  65. _driver->setTimerCallback(0, 0);
  66. _driver->close();
  67. delete _driver;
  68. }
  69. _driver = NULL;
  70. clearConstructs();
  71. }
  72. int MidiPlayer::open(int gameType, bool isDemo) {
  73. // Don't call open() twice!
  74. assert(!_driver);
  75. Common::String accoladeDriverFilename;
  76. musicType = MT_INVALID;
  77. switch (gameType) {
  78. case GType_ELVIRA1:
  79. _musicMode = kMusicModeAccolade;
  80. accoladeDriverFilename = "INSTR.DAT";
  81. break;
  82. case GType_ELVIRA2:
  83. case GType_WW:
  84. // Attention: Elvira 2 shipped with INSTR.DAT and MUSIC.DRV
  85. // MUSIC.DRV is the correct one. INSTR.DAT seems to be a left-over
  86. _musicMode = kMusicModeAccolade;
  87. accoladeDriverFilename = "MUSIC.DRV";
  88. break;
  89. case GType_SIMON1:
  90. if (isDemo) {
  91. _musicMode = kMusicModeAccolade;
  92. accoladeDriverFilename = "MUSIC.DRV";
  93. } else if (Common::File::exists("MT_FM.IBK")) {
  94. _musicMode = kMusicModeSimon1;
  95. }
  96. break;
  97. case GType_SIMON2:
  98. //_musicMode = kMusicModeMilesAudio;
  99. // currently disabled, because there are a few issues
  100. // MT32 seems to work fine now, AdLib seems to use bad instruments and is also outputting music on
  101. // the right speaker only. The original driver did initialize the panning to 0 and the Simon2 XMIDI
  102. // tracks don't set panning at all. We can reset panning to be centered, which would solve this
  103. // issue, but we still don't know who's setting it in the original interpreter.
  104. break;
  105. default:
  106. break;
  107. }
  108. MidiDriver::DeviceHandle dev;
  109. int ret = 0;
  110. if (_musicMode != kMusicModeDisabled) {
  111. dev = MidiDriver::detectDevice(MDT_MIDI | MDT_ADLIB | MDT_PREFER_MT32);
  112. musicType = MidiDriver::getMusicType(dev);
  113. switch (musicType) {
  114. case MT_ADLIB:
  115. case MT_MT32:
  116. break;
  117. case MT_GM:
  118. if (!ConfMan.getBool("native_mt32")) {
  119. // Not a real MT32 / no MUNT
  120. ::GUI::MessageDialog dialog(("You appear to be using a General MIDI device,\n"
  121. "but your game only supports Roland MT32 MIDI.\n"
  122. "We try to map the Roland MT32 instruments to\n"
  123. "General MIDI ones. It is still possible that\n"
  124. "some tracks sound incorrect."));
  125. dialog.runModal();
  126. }
  127. // Switch to MT32 driver in any case
  128. musicType = MT_MT32;
  129. break;
  130. default:
  131. _musicMode = kMusicModeDisabled;
  132. break;
  133. }
  134. }
  135. switch (_musicMode) {
  136. case kMusicModeAccolade: {
  137. // Setup midi driver
  138. switch (musicType) {
  139. case MT_ADLIB:
  140. _driver = MidiDriver_Accolade_AdLib_create(accoladeDriverFilename);
  141. break;
  142. case MT_MT32:
  143. _driver = MidiDriver_Accolade_MT32_create(accoladeDriverFilename);
  144. break;
  145. default:
  146. assert(0);
  147. break;
  148. }
  149. if (!_driver)
  150. return 255;
  151. ret = _driver->open();
  152. if (ret == 0) {
  153. // Reset is done inside our MIDI driver
  154. _driver->setTimerCallback(this, &onTimer);
  155. }
  156. //setTimerRate(_driver->getBaseTempo());
  157. return 0;
  158. }
  159. case kMusicModeMilesAudio: {
  160. switch (musicType) {
  161. case MT_ADLIB: {
  162. Common::File instrumentDataFile;
  163. if (instrumentDataFile.exists("MIDPAK.AD")) {
  164. // if there is a file called MIDPAK.AD, use it directly
  165. warning("SIMON 2: using MIDPAK.AD");
  166. _driver = Audio::MidiDriver_Miles_AdLib_create("MIDPAK.AD", "MIDPAK.AD");
  167. } else {
  168. // if there is no file called MIDPAK.AD, try to extract it from the file SETUP.SHR
  169. // if we didn't do this, the user would be forced to "install" the game instead of simply
  170. // copying all files from CD-ROM.
  171. Common::SeekableReadStream *midpakAdLibStream;
  172. midpakAdLibStream = simon2SetupExtractFile("MIDPAK.AD");
  173. if (!midpakAdLibStream)
  174. error("MidiPlayer: could not extract MIDPAK.AD from SETUP.SHR");
  175. // Pass this extracted data to the driver
  176. warning("SIMON 2: using MIDPAK.AD extracted from SETUP.SHR");
  177. _driver = Audio::MidiDriver_Miles_AdLib_create("", "", midpakAdLibStream);
  178. delete midpakAdLibStream;
  179. }
  180. // TODO: not sure what's going wrong with AdLib
  181. // it doesn't seem to matter if we use the regular XMIDI tracks or the 2nd set meant for MT32
  182. break;
  183. }
  184. case MT_MT32:
  185. _driver = Audio::MidiDriver_Miles_MT32_create("");
  186. _nativeMT32 = true; // use 2nd set of XMIDI tracks
  187. break;
  188. case MT_GM:
  189. if (ConfMan.getBool("native_mt32")) {
  190. _driver = Audio::MidiDriver_Miles_MT32_create("");
  191. _nativeMT32 = true; // use 2nd set of XMIDI tracks
  192. }
  193. break;
  194. default:
  195. break;
  196. }
  197. if (!_driver)
  198. return 255;
  199. ret = _driver->open();
  200. if (ret == 0) {
  201. // Reset is done inside our MIDI driver
  202. _driver->setTimerCallback(this, &onTimer);
  203. }
  204. return 0;
  205. }
  206. case kMusicModeSimon1: {
  207. // This only handles the original AdLib driver of Simon1.
  208. if (musicType == MT_ADLIB) {
  209. _adLibMusic = true;
  210. _map_mt32_to_gm = false;
  211. _nativeMT32 = false;
  212. _driver = createMidiDriverSimon1AdLib("MT_FM.IBK");
  213. if (_driver && _driver->open() == 0) {
  214. _driver->setTimerCallback(this, &onTimer);
  215. // Like the original, we enable the rhythm support by default.
  216. _driver->send(0xB0, 0x67, 0x01);
  217. return 0;
  218. }
  219. delete _driver;
  220. _driver = nullptr;
  221. }
  222. _musicMode = kMusicModeDisabled;
  223. }
  224. default:
  225. break;
  226. }
  227. dev = MidiDriver::detectDevice(MDT_ADLIB | MDT_MIDI | (gameType == GType_SIMON1 ? MDT_PREFER_MT32 : MDT_PREFER_GM));
  228. _adLibMusic = (MidiDriver::getMusicType(dev) == MT_ADLIB);
  229. _nativeMT32 = ((MidiDriver::getMusicType(dev) == MT_MT32) || ConfMan.getBool("native_mt32"));
  230. _driver = MidiDriver::createMidi(dev);
  231. if (!_driver)
  232. return 255;
  233. if (_nativeMT32)
  234. _driver->property(MidiDriver::PROP_CHANNEL_MASK, 0x03FE);
  235. _map_mt32_to_gm = (gameType != GType_SIMON2 && !_nativeMT32);
  236. ret = _driver->open();
  237. if (ret)
  238. return ret;
  239. _driver->setTimerCallback(this, &onTimer);
  240. if (_nativeMT32)
  241. _driver->sendMT32Reset();
  242. else
  243. _driver->sendGMReset();
  244. return 0;
  245. }
  246. void MidiPlayer::send(uint32 b) {
  247. if (!_current)
  248. return;
  249. if (_musicMode != kMusicModeDisabled) {
  250. // Handle volume control for Simon1 output.
  251. if (_musicMode == kMusicModeSimon1) {
  252. // The driver does not support any volume control, thus we simply
  253. // scale the velocities on note on for now.
  254. // TODO: We should probably handle this at output level at some
  255. // point. Then we can allow volume changes to affect already
  256. // playing notes too. For now this simple change allows us to
  257. // have some simple volume control though.
  258. if ((b & 0xF0) == 0x90) {
  259. byte volume = (b >> 16) & 0x7F;
  260. if (_current == &_sfx) {
  261. volume = volume * _sfxVolume / 255;
  262. } else if (_current == &_music) {
  263. volume = volume * _musicVolume / 255;
  264. }
  265. b = (b & 0xFF00FFFF) | (volume << 16);
  266. }
  267. }
  268. // Send directly to Accolade/Miles/Simon1 Audio driver
  269. _driver->send(b);
  270. return;
  271. }
  272. byte channel = (byte)(b & 0x0F);
  273. if ((b & 0xFFF0) == 0x07B0) {
  274. // Adjust volume changes by master music and master sfx volume.
  275. byte volume = (byte)((b >> 16) & 0x7F);
  276. _current->volume[channel] = volume;
  277. if (_current == &_sfx)
  278. volume = volume * _sfxVolume / 255;
  279. else if (_current == &_music)
  280. volume = volume * _musicVolume / 255;
  281. b = (b & 0xFF00FFFF) | (volume << 16);
  282. } else if ((b & 0xF0) == 0xC0 && _map_mt32_to_gm) {
  283. b = (b & 0xFFFF00FF) | (MidiDriver::_mt32ToGm[(b >> 8) & 0xFF] << 8);
  284. } else if ((b & 0xFFF0) == 0x007BB0) {
  285. // Only respond to an All Notes Off if this channel
  286. // has already been allocated.
  287. if (!_current->channel[b & 0x0F])
  288. return;
  289. } else if ((b & 0xFFF0) == 0x79B0) {
  290. // "Reset All Controllers". There seems to be some confusion
  291. // about what this message should do to the volume controller.
  292. // See http://www.midi.org/about-midi/rp15.shtml for more
  293. // information.
  294. //
  295. // If I understand it correctly, the current standard indicates
  296. // that the volume should be reset, but the next revision will
  297. // exclude it. On my system, both ALSA and FluidSynth seem to
  298. // reset it, while AdLib does not. Let's follow the majority.
  299. _current->volume[channel] = 127;
  300. }
  301. // Allocate channels if needed
  302. if (!_current->channel[channel])
  303. _current->channel[channel] = (channel == 9) ? _driver->getPercussionChannel() : _driver->allocateChannel();
  304. if (_current->channel[channel]) {
  305. if (channel == 9) {
  306. if (_current == &_sfx)
  307. _current->channel[9]->volume(_current->volume[9] * _sfxVolume / 255);
  308. else if (_current == &_music)
  309. _current->channel[9]->volume(_current->volume[9] * _musicVolume / 255);
  310. }
  311. _current->channel[channel]->send(b);
  312. if ((b & 0xFFF0) == 0x79B0) {
  313. // We have received a "Reset All Controllers" message
  314. // and passed it on to the MIDI driver. This may or may
  315. // not have affected the volume controller. To ensure
  316. // consistent behavior, explicitly set the volume to
  317. // what we think it should be.
  318. if (_current == &_sfx)
  319. _current->channel[channel]->volume(_current->volume[channel] * _sfxVolume / 255);
  320. else if (_current == &_music)
  321. _current->channel[channel]->volume(_current->volume[channel] * _musicVolume / 255);
  322. }
  323. }
  324. }
  325. void MidiPlayer::metaEvent(byte type, byte *data, uint16 length) {
  326. // Only thing we care about is End of Track.
  327. if (!_current || type != 0x2F) {
  328. return;
  329. } else if (_current == &_sfx) {
  330. clearConstructs(_sfx);
  331. } else if (_loopTrack) {
  332. _current->parser->jumpToTick(0);
  333. } else if (_queuedTrack != 255) {
  334. _currentTrack = 255;
  335. byte destination = _queuedTrack;
  336. _queuedTrack = 255;
  337. _loopTrack = _loopQueuedTrack;
  338. _loopQueuedTrack = false;
  339. // Remember, we're still inside the locked mutex.
  340. // Have to unlock it before calling jump()
  341. // (which locks it itself), and then relock it
  342. // upon returning.
  343. _mutex.unlock();
  344. startTrack(destination);
  345. _mutex.lock();
  346. } else {
  347. stop();
  348. }
  349. }
  350. void MidiPlayer::onTimer(void *data) {
  351. MidiPlayer *p = (MidiPlayer *)data;
  352. Common::StackLock lock(p->_mutex);
  353. if (!p->_paused) {
  354. if (p->_music.parser && p->_currentTrack != 255) {
  355. p->_current = &p->_music;
  356. p->_music.parser->onTimer();
  357. }
  358. }
  359. if (p->_sfx.parser) {
  360. p->_current = &p->_sfx;
  361. p->_sfx.parser->onTimer();
  362. }
  363. p->_current = 0;
  364. }
  365. void MidiPlayer::startTrack(int track) {
  366. Common::StackLock lock(_mutex);
  367. if (track == _currentTrack)
  368. return;
  369. if (_music.num_songs > 0) {
  370. if (track >= _music.num_songs)
  371. return;
  372. if (_music.parser) {
  373. _current = &_music;
  374. delete _music.parser;
  375. _current = 0;
  376. _music.parser = 0;
  377. }
  378. MidiParser *parser = MidiParser::createParser_SMF();
  379. parser->property (MidiParser::mpMalformedPitchBends, 1);
  380. parser->setMidiDriver(this);
  381. parser->setTimerRate(_driver->getBaseTempo());
  382. if (!parser->loadMusic(_music.songs[track], _music.song_sizes[track])) {
  383. warning("Error reading track %d", track);
  384. delete parser;
  385. parser = 0;
  386. }
  387. _currentTrack = (byte)track;
  388. _music.parser = parser; // That plugs the power cord into the wall
  389. } else if (_music.parser) {
  390. if (!_music.parser->setTrack(track)) {
  391. return;
  392. }
  393. _currentTrack = (byte)track;
  394. _current = &_music;
  395. _music.parser->jumpToTick(0);
  396. _current = 0;
  397. }
  398. }
  399. void MidiPlayer::stop() {
  400. Common::StackLock lock(_mutex);
  401. if (_music.parser) {
  402. _current = &_music;
  403. _music.parser->jumpToTick(0);
  404. }
  405. _current = 0;
  406. _currentTrack = 255;
  407. }
  408. void MidiPlayer::pause(bool b) {
  409. if (_paused == b || !_driver)
  410. return;
  411. _paused = b;
  412. Common::StackLock lock(_mutex);
  413. // if using the driver Accolade_AdLib call setVolume() to turn off\on the volume on all channels
  414. if (musicType == MT_ADLIB && _musicMode == kMusicModeAccolade) {
  415. static_cast <MidiDriver_Accolade_AdLib*> (_driver)->setVolume(_paused ? 0 : 128);
  416. }
  417. for (int i = 0; i < 16; ++i) {
  418. if (_music.channel[i])
  419. _music.channel[i]->volume(_paused ? 0 : (_music.volume[i] * _musicVolume / 255));
  420. if (_sfx.channel[i])
  421. _sfx.channel[i]->volume(_paused ? 0 : (_sfx.volume[i] * _sfxVolume / 255));
  422. }
  423. }
  424. void MidiPlayer::setVolume(int musicVol, int sfxVol) {
  425. musicVol = CLIP(musicVol, 0, 255);
  426. sfxVol = CLIP(sfxVol, 0, 255);
  427. if (_musicVolume == musicVol && _sfxVolume == sfxVol)
  428. return;
  429. _musicVolume = musicVol;
  430. _sfxVolume = sfxVol;
  431. // Now tell all the channels this.
  432. Common::StackLock lock(_mutex);
  433. if (_driver && !_paused) {
  434. for (int i = 0; i < 16; ++i) {
  435. if (_music.channel[i])
  436. _music.channel[i]->volume(_music.volume[i] * _musicVolume / 255);
  437. if (_sfx.channel[i])
  438. _sfx.channel[i]->volume(_sfx.volume[i] * _sfxVolume / 255);
  439. }
  440. }
  441. }
  442. void MidiPlayer::setLoop(bool loop) {
  443. Common::StackLock lock(_mutex);
  444. _loopTrack = loop;
  445. }
  446. void MidiPlayer::queueTrack(int track, bool loop) {
  447. _mutex.lock();
  448. if (_currentTrack == 255) {
  449. _mutex.unlock();
  450. setLoop(loop);
  451. startTrack(track);
  452. } else {
  453. _queuedTrack = track;
  454. _loopQueuedTrack = loop;
  455. _mutex.unlock();
  456. }
  457. }
  458. void MidiPlayer::clearConstructs() {
  459. clearConstructs(_music);
  460. clearConstructs(_sfx);
  461. }
  462. void MidiPlayer::clearConstructs(MusicInfo &info) {
  463. int i;
  464. if (info.num_songs > 0) {
  465. for (i = 0; i < info.num_songs; ++i)
  466. free(info.songs[i]);
  467. info.num_songs = 0;
  468. }
  469. free(info.data);
  470. info.data = 0;
  471. delete info.parser;
  472. info.parser = 0;
  473. if (_driver) {
  474. for (i = 0; i < 16; ++i) {
  475. if (info.channel[i]) {
  476. info.channel[i]->allNotesOff();
  477. info.channel[i]->release();
  478. }
  479. }
  480. }
  481. info.clear();
  482. }
  483. void MidiPlayer::resetVolumeTable() {
  484. int i;
  485. for (i = 0; i < 16; ++i) {
  486. _music.volume[i] = _sfx.volume[i] = 127;
  487. if (_driver)
  488. _driver->send(((_musicVolume >> 1) << 16) | 0x7B0 | i);
  489. }
  490. }
  491. static const int simon1_gmf_size[] = {
  492. 8900, 12166, 2848, 3442, 4034, 4508, 7064, 9730, 6014, 4742, 3138,
  493. 6570, 5384, 8909, 6457, 16321, 2742, 8968, 4804, 8442, 7717,
  494. 9444, 5800, 1381, 5660, 6684, 2456, 4744, 2455, 1177, 1232,
  495. 17256, 5103, 8794, 4884, 16
  496. };
  497. void MidiPlayer::loadSMF(Common::File *in, int song, bool sfx) {
  498. Common::StackLock lock(_mutex);
  499. MusicInfo *p = sfx ? &_sfx : &_music;
  500. clearConstructs(*p);
  501. uint32 startpos = in->pos();
  502. byte header[4];
  503. in->read(header, 4);
  504. bool isGMF = !memcmp(header, "GMF\x1", 4);
  505. in->seek(startpos, SEEK_SET);
  506. uint32 size = in->size() - in->pos();
  507. if (isGMF) {
  508. if (sfx) {
  509. // Multiple GMF resources are stored in the SFX files,
  510. // but each one is referenced by a pointer at the
  511. // beginning of the file. Those pointers can be used
  512. // to determine file size.
  513. in->seek(0, SEEK_SET);
  514. uint16 value = in->readUint16LE() >> 2; // Number of resources
  515. if (song != value - 1) {
  516. in->seek(song * 2 + 2, SEEK_SET);
  517. value = in->readUint16LE();
  518. size = value - startpos;
  519. }
  520. in->seek(startpos, SEEK_SET);
  521. } else if (size >= 64000) {
  522. // For GMF resources not in separate
  523. // files, we're going to have to use
  524. // hardcoded size tables.
  525. size = simon1_gmf_size[song];
  526. }
  527. }
  528. // When allocating space, add 4 bytes in case
  529. // this is a GMF and we have to tack on our own
  530. // End of Track event.
  531. p->data = (byte *)calloc(size + 4, 1);
  532. in->read(p->data, size);
  533. uint32 timerRate = _driver->getBaseTempo();
  534. if (isGMF) {
  535. // The GMF header
  536. // 3 BYTES: 'GMF'
  537. // 1 BYTE : Major version
  538. // 1 BYTE : Minor version
  539. // 1 BYTE : Ticks (Ranges from 2 - 8, always 2 for SFX)
  540. // 1 BYTE : Loop control. 0 = no loop, 1 = loop (Music only)
  541. if (!sfx) {
  542. // In the original, the ticks value indicated how many
  543. // times the music timer was called before it actually
  544. // did something. The larger the value the slower the
  545. // music.
  546. //
  547. // We, on the other hand, have a timer rate which is
  548. // used to control by how much the music advances on
  549. // each onTimer() call. The larger the value, the
  550. // faster the music.
  551. //
  552. // It seems that 4 corresponds to our base tempo, so
  553. // this should be the right way to calculate it.
  554. timerRate = (4 * _driver->getBaseTempo()) / p->data[5];
  555. // According to bug #1004919 calling setLoop() from
  556. // within a lock causes a lockup, though I have no
  557. // idea when this actually happens.
  558. _loopTrack = (p->data[6] != 0);
  559. }
  560. }
  561. MidiParser *parser = MidiParser::createParser_SMF();
  562. parser->property(MidiParser::mpMalformedPitchBends, 1);
  563. parser->setMidiDriver(this);
  564. parser->setTimerRate(timerRate);
  565. if (!parser->loadMusic(p->data, size)) {
  566. warning("Error reading track");
  567. delete parser;
  568. parser = 0;
  569. }
  570. if (!sfx) {
  571. _currentTrack = 255;
  572. resetVolumeTable();
  573. }
  574. p->parser = parser; // That plugs the power cord into the wall
  575. }
  576. void MidiPlayer::loadMultipleSMF(Common::File *in, bool sfx) {
  577. // This is a special case for Simon 2 Windows.
  578. // Instead of having multiple sequences as
  579. // separate tracks in a Type 2 file, simon2win
  580. // has multiple songs, each of which is a Type 1
  581. // file. Thus, preceding the songs is a single
  582. // byte specifying how many songs are coming.
  583. // We need to load ALL the songs and then
  584. // treat them as separate tracks -- for the
  585. // purpose of jumps, anyway.
  586. Common::StackLock lock(_mutex);
  587. MusicInfo *p = sfx ? &_sfx : &_music;
  588. clearConstructs(*p);
  589. p->num_songs = in->readByte();
  590. if (p->num_songs > 16) {
  591. warning("playMultipleSMF: %d is too many songs to keep track of", (int)p->num_songs);
  592. return;
  593. }
  594. byte i;
  595. for (i = 0; i < p->num_songs; ++i) {
  596. byte buf[5];
  597. uint32 pos = in->pos();
  598. // Make sure there's a MThd
  599. in->read(buf, 4);
  600. if (memcmp(buf, "MThd", 4) != 0) {
  601. warning("Expected MThd but found '%c%c%c%c' instead", buf[0], buf[1], buf[2], buf[3]);
  602. return;
  603. }
  604. in->seek(in->readUint32BE(), SEEK_CUR);
  605. // Now skip all the MTrk blocks
  606. while (true) {
  607. in->read(buf, 4);
  608. if (memcmp(buf, "MTrk", 4) != 0)
  609. break;
  610. in->seek(in->readUint32BE(), SEEK_CUR);
  611. }
  612. uint32 pos2 = in->pos() - 4;
  613. uint32 size = pos2 - pos;
  614. p->songs[i] = (byte *)calloc(size, 1);
  615. in->seek(pos, SEEK_SET);
  616. in->read(p->songs[i], size);
  617. p->song_sizes[i] = size;
  618. }
  619. if (!sfx) {
  620. _currentTrack = 255;
  621. resetVolumeTable();
  622. }
  623. }
  624. void MidiPlayer::loadXMIDI(Common::File *in, bool sfx) {
  625. Common::StackLock lock(_mutex);
  626. MusicInfo *p = sfx ? &_sfx : &_music;
  627. clearConstructs(*p);
  628. char buf[4];
  629. uint32 pos = in->pos();
  630. uint32 size = 4;
  631. in->read(buf, 4);
  632. if (!memcmp(buf, "FORM", 4)) {
  633. int i;
  634. for (i = 0; i < 16; ++i) {
  635. if (!memcmp(buf, "CAT ", 4))
  636. break;
  637. size += 2;
  638. memcpy(buf, &buf[2], 2);
  639. in->read(&buf[2], 2);
  640. }
  641. if (memcmp(buf, "CAT ", 4) != 0) {
  642. error("Could not find 'CAT ' tag to determine resource size");
  643. }
  644. size += 4 + in->readUint32BE();
  645. in->seek(pos, 0);
  646. p->data = (byte *)calloc(size, 1);
  647. in->read(p->data, size);
  648. } else {
  649. error("Expected 'FORM' tag but found '%c%c%c%c' instead", buf[0], buf[1], buf[2], buf[3]);
  650. }
  651. // In the DOS version of Simon the Sorcerer 2, the music contains lots
  652. // of XMIDI callback controller events. As far as we know, they aren't
  653. // actually used, so we disable the callback handler explicitly.
  654. MidiParser *parser = MidiParser::createParser_XMIDI(NULL);
  655. parser->setMidiDriver(this);
  656. parser->setTimerRate(_driver->getBaseTempo());
  657. if (!parser->loadMusic(p->data, size))
  658. error("Error reading track");
  659. if (!sfx) {
  660. _currentTrack = 255;
  661. resetVolumeTable();
  662. }
  663. p->parser = parser; // That plugs the power cord into the wall
  664. }
  665. void MidiPlayer::loadS1D(Common::File *in, bool sfx) {
  666. Common::StackLock lock(_mutex);
  667. MusicInfo *p = sfx ? &_sfx : &_music;
  668. clearConstructs(*p);
  669. uint16 size = in->readUint16LE();
  670. if (size != in->size() - 2) {
  671. error("Size mismatch in MUS file (%ld versus reported %d)", (long)in->size() - 2, (int)size);
  672. }
  673. p->data = (byte *)calloc(size, 1);
  674. in->read(p->data, size);
  675. MidiParser *parser = MidiParser_createS1D();
  676. parser->setMidiDriver(this);
  677. parser->setTimerRate(_driver->getBaseTempo());
  678. if (!parser->loadMusic(p->data, size))
  679. error("Error reading track");
  680. if (!sfx) {
  681. _currentTrack = 255;
  682. resetVolumeTable();
  683. }
  684. p->parser = parser; // That plugs the power cord into the wall
  685. }
  686. #define MIDI_SETUP_BUNDLE_HEADER_SIZE 56
  687. #define MIDI_SETUP_BUNDLE_FILEHEADER_SIZE 48
  688. #define MIDI_SETUP_BUNDLE_FILENAME_MAX_SIZE 12
  689. // PKWARE data compression library (called "DCL" in ScummVM) was used for storing files within SETUP.SHR
  690. // we need it to be able to get the file MIDPAK.AD, otherwise we would have to require the user
  691. // to "install" the game before being able to actually play it, when using AdLib.
  692. //
  693. // SETUP.SHR file format:
  694. // [bundle file header]
  695. // [compressed file header] [compressed file data]
  696. // * compressed file count
  697. Common::SeekableReadStream *MidiPlayer::simon2SetupExtractFile(const Common::String &requestedFileName) {
  698. Common::File *setupBundleStream = new Common::File();
  699. uint32 bundleSize = 0;
  700. uint32 bundleBytesLeft = 0;
  701. byte bundleHeader[MIDI_SETUP_BUNDLE_HEADER_SIZE];
  702. byte bundleFileHeader[MIDI_SETUP_BUNDLE_FILEHEADER_SIZE];
  703. uint16 bundleFileCount = 0;
  704. uint16 bundleFileNr = 0;
  705. Common::String fileName;
  706. uint32 fileCompressedSize = 0;
  707. byte *fileCompressedDataPtr = nullptr;
  708. Common::SeekableReadStream *extractedStream = nullptr;
  709. if (!setupBundleStream->open("setup.shr"))
  710. error("MidiPlayer: could not open setup.shr");
  711. bundleSize = setupBundleStream->size();
  712. bundleBytesLeft = bundleSize;
  713. if (bundleSize < MIDI_SETUP_BUNDLE_HEADER_SIZE)
  714. error("MidiPlayer: unexpected EOF in setup.shr");
  715. if (setupBundleStream->read(bundleHeader, MIDI_SETUP_BUNDLE_HEADER_SIZE) != MIDI_SETUP_BUNDLE_HEADER_SIZE)
  716. error("MidiPlayer: setup.shr read error");
  717. bundleBytesLeft -= MIDI_SETUP_BUNDLE_HEADER_SIZE;
  718. // Verify header byte
  719. if (bundleHeader[13] != 't')
  720. error("MidiPlayer: setup.shr bundle header data mismatch");
  721. bundleFileCount = READ_LE_UINT16(&bundleHeader[14]);
  722. // Search for requested file
  723. while (bundleFileNr < bundleFileCount) {
  724. if (bundleBytesLeft < sizeof(bundleFileHeader))
  725. error("MidiPlayer: unexpected EOF in setup.shr");
  726. if (setupBundleStream->read(bundleFileHeader, sizeof(bundleFileHeader)) != sizeof(bundleFileHeader))
  727. error("MidiPlayer: setup.shr read error");
  728. bundleBytesLeft -= MIDI_SETUP_BUNDLE_FILEHEADER_SIZE;
  729. // Extract filename from file-header
  730. fileName.clear();
  731. for (byte curPos = 0; curPos < MIDI_SETUP_BUNDLE_FILENAME_MAX_SIZE; curPos++) {
  732. if (!bundleFileHeader[curPos]) // terminating NUL
  733. break;
  734. fileName.insertChar(bundleFileHeader[curPos], curPos);
  735. }
  736. // Get compressed
  737. fileCompressedSize = READ_LE_UINT32(&bundleFileHeader[20]);
  738. if (!fileCompressedSize)
  739. error("MidiPlayer: compressed file is 0 bytes, data corruption?");
  740. if (bundleBytesLeft < fileCompressedSize)
  741. error("MidiPlayer: unexpected EOF in setup.shr");
  742. if (fileName == requestedFileName) {
  743. // requested file found
  744. fileCompressedDataPtr = new byte[fileCompressedSize];
  745. if (setupBundleStream->read(fileCompressedDataPtr, fileCompressedSize) != fileCompressedSize)
  746. error("MidiPlayer: setup.shr read error");
  747. Common::MemoryReadStream *compressedStream = nullptr;
  748. compressedStream = new Common::MemoryReadStream(fileCompressedDataPtr, fileCompressedSize);
  749. // we don't know the unpacked size, let decompressor figure it out
  750. extractedStream = Common::decompressDCL(compressedStream);
  751. delete compressedStream;
  752. break;
  753. }
  754. // skip compressed size
  755. setupBundleStream->skip(fileCompressedSize);
  756. bundleBytesLeft -= fileCompressedSize;
  757. bundleFileNr++;
  758. }
  759. setupBundleStream->close();
  760. delete setupBundleStream;
  761. return extractedStream;
  762. }
  763. } // End of namespace AGOS