Load_mdl.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. * Load_mdl.cpp
  3. * ------------
  4. * Purpose: Digitrakker (MDL) module loader
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "Loaders.h"
  11. OPENMPT_NAMESPACE_BEGIN
  12. // MDL file header
  13. struct MDLFileHeader
  14. {
  15. char id[4]; // "DMDL"
  16. uint8 version;
  17. };
  18. MPT_BINARY_STRUCT(MDLFileHeader, 5)
  19. // RIFF-style Chunk
  20. struct MDLChunk
  21. {
  22. // 16-Bit chunk identifiers
  23. enum ChunkIdentifiers
  24. {
  25. idInfo = MagicLE("IN"),
  26. idMessage = MagicLE("ME"),
  27. idPats = MagicLE("PA"),
  28. idPatNames = MagicLE("PN"),
  29. idTracks = MagicLE("TR"),
  30. idInstrs = MagicLE("II"),
  31. idVolEnvs = MagicLE("VE"),
  32. idPanEnvs = MagicLE("PE"),
  33. idFreqEnvs = MagicLE("FE"),
  34. idSampleInfo = MagicLE("IS"),
  35. ifSampleData = MagicLE("SA"),
  36. };
  37. uint16le id;
  38. uint32le length;
  39. size_t GetLength() const
  40. {
  41. return length;
  42. }
  43. ChunkIdentifiers GetID() const
  44. {
  45. return static_cast<ChunkIdentifiers>(id.get());
  46. }
  47. };
  48. MPT_BINARY_STRUCT(MDLChunk, 6)
  49. struct MDLInfoBlock
  50. {
  51. char title[32];
  52. char composer[20];
  53. uint16le numOrders;
  54. uint16le restartPos;
  55. uint8le globalVol; // 1...255
  56. uint8le speed; // 1...255
  57. uint8le tempo; // 4...255
  58. uint8le chnSetup[32];
  59. };
  60. MPT_BINARY_STRUCT(MDLInfoBlock, 91)
  61. // Sample header in II block
  62. struct MDLSampleHeader
  63. {
  64. uint8le smpNum;
  65. uint8le lastNote;
  66. uint8le volume;
  67. uint8le volEnvFlags; // 6 bits env #, 2 bits flags
  68. uint8le panning;
  69. uint8le panEnvFlags;
  70. uint16le fadeout;
  71. uint8le vibSpeed;
  72. uint8le vibDepth;
  73. uint8le vibSweep;
  74. uint8le vibType;
  75. uint8le reserved; // zero
  76. uint8le freqEnvFlags;
  77. };
  78. MPT_BINARY_STRUCT(MDLSampleHeader, 14)
  79. struct MDLEnvelope
  80. {
  81. uint8 envNum;
  82. struct
  83. {
  84. uint8 x; // Delta value from last point, 0 means no more points defined
  85. uint8 y; // 0...63
  86. } nodes[15];
  87. uint8 flags;
  88. uint8 loop; // Lower 4 bits = start, upper 4 bits = end
  89. void ConvertToMPT(InstrumentEnvelope &mptEnv) const
  90. {
  91. mptEnv.dwFlags.reset();
  92. mptEnv.clear();
  93. mptEnv.reserve(15);
  94. int16 tick = -nodes[0].x;
  95. for(uint8 n = 0; n < 15; n++)
  96. {
  97. if(!nodes[n].x)
  98. break;
  99. tick += nodes[n].x;
  100. mptEnv.push_back(EnvelopeNode(tick, std::min(nodes[n].y, uint8(64)))); // actually 0-63
  101. }
  102. mptEnv.nLoopStart = (loop & 0x0F);
  103. mptEnv.nLoopEnd = (loop >> 4);
  104. mptEnv.nSustainStart = mptEnv.nSustainEnd = (flags & 0x0F);
  105. if(flags & 0x10) mptEnv.dwFlags.set(ENV_SUSTAIN);
  106. if(flags & 0x20) mptEnv.dwFlags.set(ENV_LOOP);
  107. }
  108. };
  109. MPT_BINARY_STRUCT(MDLEnvelope, 33)
  110. struct MDLPatternHeader
  111. {
  112. uint8le channels;
  113. uint8le lastRow;
  114. char name[16];
  115. };
  116. MPT_BINARY_STRUCT(MDLPatternHeader, 18)
  117. enum
  118. {
  119. MDLNOTE_NOTE = 1 << 0,
  120. MDLNOTE_SAMPLE = 1 << 1,
  121. MDLNOTE_VOLUME = 1 << 2,
  122. MDLNOTE_EFFECTS = 1 << 3,
  123. MDLNOTE_PARAM1 = 1 << 4,
  124. MDLNOTE_PARAM2 = 1 << 5,
  125. };
  126. static constexpr VibratoType MDLVibratoType[] = { VIB_SINE, VIB_RAMP_DOWN, VIB_SQUARE, VIB_SINE };
  127. static constexpr ModCommand::COMMAND MDLEffTrans[] =
  128. {
  129. /* 0 */ CMD_NONE,
  130. /* 1st column only */
  131. /* 1 */ CMD_PORTAMENTOUP,
  132. /* 2 */ CMD_PORTAMENTODOWN,
  133. /* 3 */ CMD_TONEPORTAMENTO,
  134. /* 4 */ CMD_VIBRATO,
  135. /* 5 */ CMD_ARPEGGIO,
  136. /* 6 */ CMD_NONE,
  137. /* Either column */
  138. /* 7 */ CMD_TEMPO,
  139. /* 8 */ CMD_PANNING8,
  140. /* 9 */ CMD_SETENVPOSITION,
  141. /* A */ CMD_NONE,
  142. /* B */ CMD_POSITIONJUMP,
  143. /* C */ CMD_GLOBALVOLUME,
  144. /* D */ CMD_PATTERNBREAK,
  145. /* E */ CMD_S3MCMDEX,
  146. /* F */ CMD_SPEED,
  147. /* 2nd column only */
  148. /* G */ CMD_VOLUMESLIDE, // up
  149. /* H */ CMD_VOLUMESLIDE, // down
  150. /* I */ CMD_RETRIG,
  151. /* J */ CMD_TREMOLO,
  152. /* K */ CMD_TREMOR,
  153. /* L */ CMD_NONE,
  154. };
  155. // receive an MDL effect, give back a 'normal' one.
  156. static void ConvertMDLCommand(uint8 &cmd, uint8 &param)
  157. {
  158. if(cmd >= std::size(MDLEffTrans))
  159. return;
  160. uint8 origCmd = cmd;
  161. cmd = MDLEffTrans[cmd];
  162. switch(origCmd)
  163. {
  164. #ifdef MODPLUG_TRACKER
  165. case 0x07: // Tempo
  166. // MDL supports any nonzero tempo value, but OpenMPT doesn't
  167. param = std::max(param, uint8(0x20));
  168. break;
  169. #endif // MODPLUG_TRACKER
  170. case 0x08: // Panning
  171. param = (param & 0x7F) * 2u;
  172. break;
  173. case 0x0C: // Global volume
  174. param = (param + 1) / 2u;
  175. break;
  176. case 0x0D: // Pattern Break
  177. // Convert from BCD
  178. param = 10 * (param >> 4) + (param & 0x0F);
  179. break;
  180. case 0x0E: // Special
  181. switch(param >> 4)
  182. {
  183. case 0x0: // unused
  184. case 0x3: // unused
  185. case 0x8: // Set Samplestatus (loop type)
  186. cmd = CMD_NONE;
  187. break;
  188. case 0x1: // Pan Slide Left
  189. cmd = CMD_PANNINGSLIDE;
  190. param = (std::min(static_cast<uint8>(param & 0x0F), uint8(0x0E)) << 4) | 0x0F;
  191. break;
  192. case 0x2: // Pan Slide Right
  193. cmd = CMD_PANNINGSLIDE;
  194. param = 0xF0 | std::min(static_cast<uint8>(param & 0x0F), uint8(0x0E));
  195. break;
  196. case 0x4: // Vibrato Waveform
  197. param = 0x30 | (param & 0x0F);
  198. break;
  199. case 0x5: // Set Finetune
  200. cmd = CMD_FINETUNE;
  201. param = (param << 4) ^ 0x80;
  202. break;
  203. case 0x6: // Pattern Loop
  204. param = 0xB0 | (param & 0x0F);
  205. break;
  206. case 0x7: // Tremolo Waveform
  207. param = 0x40 | (param & 0x0F);
  208. break;
  209. case 0x9: // Retrig
  210. cmd = CMD_RETRIG;
  211. param &= 0x0F;
  212. break;
  213. case 0xA: // Global vol slide up
  214. cmd = CMD_GLOBALVOLSLIDE;
  215. param = 0xF0 & (((param & 0x0F) + 1) << 3);
  216. break;
  217. case 0xB: // Global vol slide down
  218. cmd = CMD_GLOBALVOLSLIDE;
  219. param = ((param & 0x0F) + 1) >> 1;
  220. break;
  221. case 0xC: // Note cut
  222. case 0xD: // Note delay
  223. case 0xE: // Pattern delay
  224. // Nothing to change here
  225. break;
  226. case 0xF: // Offset -- further mangled later.
  227. cmd = CMD_OFFSET;
  228. break;
  229. }
  230. break;
  231. case 0x10: // Volslide up
  232. if(param < 0xE0)
  233. {
  234. // 00...DF regular slide - four times more precise than in XM
  235. param >>= 2;
  236. if(param > 0x0F)
  237. param = 0x0F;
  238. param <<= 4;
  239. } else if(param < 0xF0)
  240. {
  241. // E0...EF extra fine slide (on first tick, 4 times finer)
  242. param = (((param & 0x0F) << 2) | 0x0F);
  243. } else
  244. {
  245. // F0...FF regular fine slide (on first tick) - like in XM
  246. param = ((param << 4) | 0x0F);
  247. }
  248. break;
  249. case 0x11: // Volslide down
  250. if(param < 0xE0)
  251. {
  252. // 00...DF regular slide - four times more precise than in XM
  253. param >>= 2;
  254. if(param > 0x0F)
  255. param = 0x0F;
  256. } else if(param < 0xF0)
  257. {
  258. // E0...EF extra fine slide (on first tick, 4 times finer)
  259. param = (((param & 0x0F) >> 2) | 0xF0);
  260. } else
  261. {
  262. // F0...FF regular fine slide (on first tick) - like in XM
  263. }
  264. break;
  265. }
  266. }
  267. // Returns true if command was lost
  268. static bool ImportMDLCommands(ModCommand &m, uint8 vol, uint8 e1, uint8 e2, uint8 p1, uint8 p2)
  269. {
  270. // Map second effect values 1-6 to effects G-L
  271. if(e2 >= 1 && e2 <= 6)
  272. e2 += 15;
  273. ConvertMDLCommand(e1, p1);
  274. ConvertMDLCommand(e2, p2);
  275. /* From the Digitrakker documentation:
  276. * EFx -xx - Set Sample Offset
  277. This is a double-command. It starts the
  278. sample at adress xxx*256.
  279. Example: C-5 01 -- EF1 -23 ->starts sample
  280. 01 at address 12300 (in hex).
  281. Kind of screwy, but I guess it's better than the mess required to do it with IT (which effectively
  282. requires 3 rows in order to set the offset past 0xff00). If we had access to the entire track, we
  283. *might* be able to shove the high offset SAy into surrounding rows (or 2x MPTM #xx), but it wouldn't
  284. always be possible, it'd make the loader a lot uglier, and generally would be more trouble than
  285. it'd be worth to implement.
  286. What's more is, if there's another effect in the second column, it's ALSO processed in addition to the
  287. offset, and the second data byte is shared between the two effects. */
  288. uint32 offset = uint32_max;
  289. uint8 otherCmd = CMD_NONE;
  290. if(e1 == CMD_OFFSET)
  291. {
  292. // EFy -xx => offset yxx00
  293. offset = ((p1 & 0x0F) << 8) | p2;
  294. p1 = (p1 & 0x0F) ? 0xFF : p2;
  295. if(e2 == CMD_OFFSET)
  296. e2 = CMD_NONE;
  297. else
  298. otherCmd = e2;
  299. } else if (e2 == CMD_OFFSET)
  300. {
  301. // --- EFy => offset y0000
  302. offset = (p2 & 0x0F) << 8;
  303. p2 = (p2 & 0x0F) ? 0xFF : 0;
  304. otherCmd = e1;
  305. }
  306. if(offset != uint32_max && offset > 0xFF && ModCommand::GetEffectWeight(otherCmd) < ModCommand::GetEffectWeight(CMD_OFFSET))
  307. {
  308. m.command = CMD_OFFSET;
  309. m.param = static_cast<ModCommand::PARAM>(offset & 0xFF);
  310. m.volcmd = VOLCMD_OFFSET;
  311. m.vol = static_cast<ModCommand::VOL>(offset >> 8);
  312. return otherCmd != CMD_NONE || vol != 0;
  313. }
  314. if(vol)
  315. {
  316. m.volcmd = VOLCMD_VOLUME;
  317. m.vol = (vol + 2) / 4u;
  318. }
  319. // If we have Dxx + G00, or Dxx + H00, combine them into Lxx/Kxx.
  320. ModCommand::CombineEffects(e1, p1, e2, p2);
  321. bool lostCommand = false;
  322. // Try to fit the "best" effect into e2.
  323. if(e1 == CMD_NONE)
  324. {
  325. // Easy
  326. } else if(e2 == CMD_NONE)
  327. {
  328. // Almost as easy
  329. e2 = e1;
  330. p2 = p1;
  331. } else if(e1 == e2 && e1 != CMD_S3MCMDEX)
  332. {
  333. // Digitrakker processes the effects left-to-right, so if both effects are the same, the
  334. // second essentially overrides the first.
  335. } else if(!vol)
  336. {
  337. lostCommand |= (ModCommand::TwoRegularCommandsToMPT(e1, p1, e2, p2).first != CMD_NONE);
  338. m.volcmd = e1;
  339. m.vol = p1;
  340. } else
  341. {
  342. if(ModCommand::GetEffectWeight((ModCommand::COMMAND)e1) > ModCommand::GetEffectWeight((ModCommand::COMMAND)e2))
  343. {
  344. std::swap(e1, e2);
  345. std::swap(p1, p2);
  346. }
  347. lostCommand = true;
  348. }
  349. m.command = e2;
  350. m.param = p2;
  351. return lostCommand;
  352. }
  353. static void MDLReadEnvelopes(FileReader file, std::vector<MDLEnvelope> &envelopes)
  354. {
  355. if(!file.CanRead(1))
  356. return;
  357. envelopes.resize(64);
  358. uint8 numEnvs = file.ReadUint8();
  359. while(numEnvs--)
  360. {
  361. MDLEnvelope mdlEnv;
  362. if(!file.ReadStruct(mdlEnv) || mdlEnv.envNum > 63)
  363. continue;
  364. envelopes[mdlEnv.envNum] = mdlEnv;
  365. }
  366. }
  367. static void CopyEnvelope(InstrumentEnvelope &mptEnv, uint8 flags, std::vector<MDLEnvelope> &envelopes)
  368. {
  369. uint8 envNum = flags & 0x3F;
  370. if(envNum < envelopes.size())
  371. envelopes[envNum].ConvertToMPT(mptEnv);
  372. mptEnv.dwFlags.set(ENV_ENABLED, (flags & 0x80) && !mptEnv.empty());
  373. }
  374. static bool ValidateHeader(const MDLFileHeader &fileHeader)
  375. {
  376. if(std::memcmp(fileHeader.id, "DMDL", 4)
  377. || fileHeader.version >= 0x20)
  378. {
  379. return false;
  380. }
  381. return true;
  382. }
  383. CSoundFile::ProbeResult CSoundFile::ProbeFileHeaderMDL(MemoryFileReader file, const uint64 *pfilesize)
  384. {
  385. MDLFileHeader fileHeader;
  386. if(!file.ReadStruct(fileHeader))
  387. {
  388. return ProbeWantMoreData;
  389. }
  390. if(!ValidateHeader(fileHeader))
  391. {
  392. return ProbeFailure;
  393. }
  394. MPT_UNREFERENCED_PARAMETER(pfilesize);
  395. return ProbeSuccess;
  396. }
  397. bool CSoundFile::ReadMDL(FileReader &file, ModLoadingFlags loadFlags)
  398. {
  399. file.Rewind();
  400. MDLFileHeader fileHeader;
  401. if(!file.ReadStruct(fileHeader))
  402. {
  403. return false;
  404. }
  405. if(!ValidateHeader(fileHeader))
  406. {
  407. return false;
  408. }
  409. if(loadFlags == onlyVerifyHeader)
  410. {
  411. return true;
  412. }
  413. ChunkReader chunkFile(file);
  414. ChunkReader::ChunkList<MDLChunk> chunks = chunkFile.ReadChunks<MDLChunk>(0);
  415. // Read global info
  416. FileReader chunk = chunks.GetChunk(MDLChunk::idInfo);
  417. MDLInfoBlock info;
  418. if(!chunk.IsValid() || !chunk.ReadStruct(info))
  419. {
  420. return false;
  421. }
  422. InitializeGlobals(MOD_TYPE_MDL);
  423. m_SongFlags = SONG_ITCOMPATGXX;
  424. m_playBehaviour.set(kPerChannelGlobalVolSlide);
  425. m_playBehaviour.set(kApplyOffsetWithoutNote);
  426. m_playBehaviour.reset(kITVibratoTremoloPanbrello);
  427. m_playBehaviour.reset(kITSCxStopsSample); // Gate effect in underbeat.mdl
  428. m_modFormat.formatName = U_("Digitrakker");
  429. m_modFormat.type = U_("mdl");
  430. m_modFormat.madeWithTracker = U_("Digitrakker ") + (
  431. (fileHeader.version == 0x11) ? U_("3") // really could be 2.99b - close enough
  432. : (fileHeader.version == 0x10) ? U_("2.3")
  433. : (fileHeader.version == 0x00) ? U_("2.0 - 2.2b") // there was no 1.x release
  434. : U_(""));
  435. m_modFormat.charset = mpt::Charset::CP437;
  436. m_songName = mpt::String::ReadBuf(mpt::String::spacePadded, info.title);
  437. m_songArtist = mpt::ToUnicode(mpt::Charset::CP437, mpt::String::ReadBuf(mpt::String::spacePadded, info.composer));
  438. m_nDefaultGlobalVolume = info.globalVol + 1;
  439. m_nDefaultSpeed = Clamp<uint8, uint8>(info.speed, 1, 255);
  440. m_nDefaultTempo.Set(Clamp<uint8, uint8>(info.tempo, 4, 255));
  441. ReadOrderFromFile<uint8>(Order(), chunk, info.numOrders);
  442. Order().SetRestartPos(info.restartPos);
  443. m_nChannels = 0;
  444. for(CHANNELINDEX c = 0; c < 32; c++)
  445. {
  446. ChnSettings[c].Reset();
  447. ChnSettings[c].nPan = (info.chnSetup[c] & 0x7F) * 2u;
  448. if(ChnSettings[c].nPan == 254)
  449. ChnSettings[c].nPan = 256;
  450. if(info.chnSetup[c] & 0x80)
  451. ChnSettings[c].dwFlags.set(CHN_MUTE);
  452. else
  453. m_nChannels = c + 1;
  454. chunk.ReadString<mpt::String::spacePadded>(ChnSettings[c].szName, 8);
  455. }
  456. // Read song message
  457. chunk = chunks.GetChunk(MDLChunk::idMessage);
  458. m_songMessage.Read(chunk, chunk.GetLength(), SongMessage::leCR);
  459. // Read sample info and data
  460. chunk = chunks.GetChunk(MDLChunk::idSampleInfo);
  461. if(chunk.IsValid())
  462. {
  463. FileReader dataChunk = chunks.GetChunk(MDLChunk::ifSampleData);
  464. uint8 numSamples = chunk.ReadUint8();
  465. for(uint8 smp = 0; smp < numSamples; smp++)
  466. {
  467. const SAMPLEINDEX sampleIndex = chunk.ReadUint8();
  468. if(sampleIndex == 0 || sampleIndex >= MAX_SAMPLES || !chunk.CanRead(32 + 8 + 2 + 12 + 2))
  469. break;
  470. if(sampleIndex > GetNumSamples())
  471. m_nSamples = sampleIndex;
  472. ModSample &sample = Samples[sampleIndex];
  473. sample.Initialize();
  474. sample.Set16BitCuePoints();
  475. chunk.ReadString<mpt::String::spacePadded>(m_szNames[sampleIndex], 32);
  476. chunk.ReadString<mpt::String::spacePadded>(sample.filename, 8);
  477. uint32 c4speed;
  478. if(fileHeader.version < 0x10)
  479. c4speed = chunk.ReadUint16LE();
  480. else
  481. c4speed = chunk.ReadUint32LE();
  482. sample.nC5Speed = c4speed * 2u;
  483. sample.nLength = chunk.ReadUint32LE();
  484. sample.nLoopStart = chunk.ReadUint32LE();
  485. sample.nLoopEnd = chunk.ReadUint32LE();
  486. if(sample.nLoopEnd != 0)
  487. {
  488. sample.uFlags.set(CHN_LOOP);
  489. sample.nLoopEnd += sample.nLoopStart;
  490. }
  491. uint8 volume = chunk.ReadUint8();
  492. if(fileHeader.version < 0x10)
  493. sample.nVolume = volume;
  494. uint8 flags = chunk.ReadUint8();
  495. if(flags & 0x01)
  496. {
  497. sample.uFlags.set(CHN_16BIT);
  498. sample.nLength /= 2u;
  499. sample.nLoopStart /= 2u;
  500. sample.nLoopEnd /= 2u;
  501. }
  502. sample.uFlags.set(CHN_PINGPONGLOOP, (flags & 0x02) != 0);
  503. SampleIO sampleIO(
  504. (flags & 0x01) ? SampleIO::_16bit : SampleIO::_8bit,
  505. SampleIO::mono,
  506. SampleIO::littleEndian,
  507. (flags & 0x0C) ? SampleIO::MDL : SampleIO::signedPCM);
  508. if(loadFlags & loadSampleData)
  509. {
  510. sampleIO.ReadSample(sample, dataChunk);
  511. }
  512. }
  513. }
  514. chunk = chunks.GetChunk(MDLChunk::idInstrs);
  515. if(chunk.IsValid())
  516. {
  517. std::vector<MDLEnvelope> volEnvs, panEnvs, pitchEnvs;
  518. MDLReadEnvelopes(chunks.GetChunk(MDLChunk::idVolEnvs), volEnvs);
  519. MDLReadEnvelopes(chunks.GetChunk(MDLChunk::idPanEnvs), panEnvs);
  520. MDLReadEnvelopes(chunks.GetChunk(MDLChunk::idFreqEnvs), pitchEnvs);
  521. uint8 numInstruments = chunk.ReadUint8();
  522. for(uint8 i = 0; i < numInstruments; i++)
  523. {
  524. const auto [ins, numSamples] = chunk.ReadArray<uint8, 2>();
  525. uint8 firstNote = 0;
  526. ModInstrument *mptIns = nullptr;
  527. if(ins == 0
  528. || !chunk.CanRead(32 + sizeof(MDLSampleHeader) * numSamples)
  529. || (mptIns = AllocateInstrument(ins)) == nullptr)
  530. {
  531. chunk.Skip(32 + sizeof(MDLSampleHeader) * numSamples);
  532. continue;
  533. }
  534. chunk.ReadString<mpt::String::spacePadded>(mptIns->name, 32);
  535. for(uint8 smp = 0; smp < numSamples; smp++)
  536. {
  537. MDLSampleHeader sampleHeader;
  538. chunk.ReadStruct(sampleHeader);
  539. if(sampleHeader.smpNum == 0 || sampleHeader.smpNum > GetNumSamples())
  540. continue;
  541. LimitMax(sampleHeader.lastNote, static_cast<uint8>(std::size(mptIns->Keyboard)));
  542. for(uint8 n = firstNote; n <= sampleHeader.lastNote; n++)
  543. {
  544. mptIns->Keyboard[n] = sampleHeader.smpNum;
  545. }
  546. firstNote = sampleHeader.lastNote + 1;
  547. CopyEnvelope(mptIns->VolEnv, sampleHeader.volEnvFlags, volEnvs);
  548. CopyEnvelope(mptIns->PanEnv, sampleHeader.panEnvFlags, panEnvs);
  549. CopyEnvelope(mptIns->PitchEnv, sampleHeader.freqEnvFlags, pitchEnvs);
  550. mptIns->nFadeOut = (sampleHeader.fadeout + 1u) / 2u;
  551. #ifdef MODPLUG_TRACKER
  552. if((mptIns->VolEnv.dwFlags & (ENV_ENABLED | ENV_LOOP)) == ENV_ENABLED)
  553. {
  554. // Fade-out is only supposed to happen on key-off, not at the end of a volume envelope.
  555. // Fake it by putting a loop at the end.
  556. mptIns->VolEnv.nLoopStart = mptIns->VolEnv.nLoopEnd = static_cast<uint8>(mptIns->VolEnv.size() - 1);
  557. mptIns->VolEnv.dwFlags.set(ENV_LOOP);
  558. }
  559. for(auto &p : mptIns->PitchEnv)
  560. {
  561. // Scale pitch envelope
  562. p.value = (p.value * 6u) / 16u;
  563. }
  564. #endif // MODPLUG_TRACKER
  565. // Samples were already initialized above. Let's hope they are not going to be re-used with different volume / panning / vibrato...
  566. ModSample &mptSmp = Samples[sampleHeader.smpNum];
  567. // This flag literally enables and disables the default volume of a sample. If you disable this flag,
  568. // the sample volume of a previously sample is re-used, even if you put an instrument number next to the note.
  569. if(sampleHeader.volEnvFlags & 0x40)
  570. mptSmp.nVolume = sampleHeader.volume;
  571. else
  572. mptSmp.uFlags.set(SMP_NODEFAULTVOLUME);
  573. mptSmp.nPan = std::min(static_cast<uint16>(sampleHeader.panning * 2), uint16(254));
  574. mptSmp.nVibType = MDLVibratoType[sampleHeader.vibType & 3];
  575. mptSmp.nVibSweep = sampleHeader.vibSweep;
  576. mptSmp.nVibDepth = (sampleHeader.vibDepth + 3u) / 4u;
  577. mptSmp.nVibRate = sampleHeader.vibSpeed;
  578. // Convert to IT-like vibrato sweep
  579. if(mptSmp.nVibSweep != 0)
  580. mptSmp.nVibSweep = mpt::saturate_cast<decltype(mptSmp.nVibSweep)>(Util::muldivr_unsigned(mptSmp.nVibDepth, 256, mptSmp.nVibSweep));
  581. else
  582. mptSmp.nVibSweep = 255;
  583. if(sampleHeader.panEnvFlags & 0x40)
  584. mptSmp.uFlags.set(CHN_PANNING);
  585. }
  586. }
  587. }
  588. // Read pattern tracks
  589. std::vector<FileReader> tracks;
  590. if((loadFlags & loadPatternData) && (chunk = chunks.GetChunk(MDLChunk::idTracks)).IsValid())
  591. {
  592. uint32 numTracks = chunk.ReadUint16LE();
  593. tracks.resize(numTracks + 1);
  594. for(uint32 i = 1; i <= numTracks; i++)
  595. {
  596. tracks[i] = chunk.ReadChunk(chunk.ReadUint16LE());
  597. }
  598. }
  599. // Read actual patterns
  600. if((loadFlags & loadPatternData) && (chunk = chunks.GetChunk(MDLChunk::idPats)).IsValid())
  601. {
  602. PATTERNINDEX numPats = chunk.ReadUint8();
  603. // In case any muted channels contain data, be sure that we import them as well.
  604. for(PATTERNINDEX pat = 0; pat < numPats; pat++)
  605. {
  606. CHANNELINDEX numChans = 32;
  607. if(fileHeader.version >= 0x10)
  608. {
  609. MDLPatternHeader patHead;
  610. chunk.ReadStruct(patHead);
  611. if(patHead.channels > m_nChannels && patHead.channels <= 32)
  612. m_nChannels = patHead.channels;
  613. numChans = patHead.channels;
  614. }
  615. for(CHANNELINDEX chn = 0; chn < numChans; chn++)
  616. {
  617. if(chunk.ReadUint16LE() > 0 && chn >= m_nChannels && chn < 32)
  618. m_nChannels = chn + 1;
  619. }
  620. }
  621. chunk.Seek(1);
  622. Patterns.ResizeArray(numPats);
  623. for(PATTERNINDEX pat = 0; pat < numPats; pat++)
  624. {
  625. CHANNELINDEX numChans = 32;
  626. ROWINDEX numRows = 64;
  627. std::string name;
  628. if(fileHeader.version >= 0x10)
  629. {
  630. MDLPatternHeader patHead;
  631. chunk.ReadStruct(patHead);
  632. numChans = patHead.channels;
  633. numRows = patHead.lastRow + 1;
  634. name = mpt::String::ReadBuf(mpt::String::spacePadded, patHead.name);
  635. }
  636. if(!Patterns.Insert(pat, numRows))
  637. {
  638. chunk.Skip(2 * numChans);
  639. continue;
  640. }
  641. Patterns[pat].SetName(name);
  642. for(CHANNELINDEX chn = 0; chn < numChans; chn++)
  643. {
  644. uint16 trkNum = chunk.ReadUint16LE();
  645. if(!trkNum || trkNum >= tracks.size() || chn >= m_nChannels)
  646. continue;
  647. FileReader &track = tracks[trkNum];
  648. track.Rewind();
  649. ROWINDEX row = 0;
  650. while(row < numRows && track.CanRead(1))
  651. {
  652. ModCommand *m = Patterns[pat].GetpModCommand(row, chn);
  653. uint8 b = track.ReadUint8();
  654. uint8 x = (b >> 2), y = (b & 3);
  655. switch(y)
  656. {
  657. case 0:
  658. // (x + 1) empty notes follow
  659. row += x + 1;
  660. break;
  661. case 1:
  662. // Repeat previous note (x + 1) times
  663. if(row > 0)
  664. {
  665. ModCommand &orig = *Patterns[pat].GetpModCommand(row - 1, chn);
  666. do
  667. {
  668. *m = orig;
  669. m += m_nChannels;
  670. row++;
  671. } while (row < numRows && x--);
  672. }
  673. break;
  674. case 2:
  675. // Copy note from row x
  676. if(row > x)
  677. {
  678. *m = *Patterns[pat].GetpModCommand(x, chn);
  679. }
  680. row++;
  681. break;
  682. case 3:
  683. // New note data
  684. if(x & MDLNOTE_NOTE)
  685. {
  686. b = track.ReadUint8();
  687. m->note = (b > 120) ? static_cast<ModCommand::NOTE>(NOTE_KEYOFF) : static_cast<ModCommand::NOTE>(b);
  688. }
  689. if(x & MDLNOTE_SAMPLE)
  690. {
  691. m->instr = track.ReadUint8();
  692. }
  693. {
  694. uint8 vol = 0, e1 = 0, e2 = 0, p1 = 0, p2 = 0;
  695. if(x & MDLNOTE_VOLUME)
  696. {
  697. vol = track.ReadUint8();
  698. }
  699. if(x & MDLNOTE_EFFECTS)
  700. {
  701. b = track.ReadUint8();
  702. e1 = (b & 0x0F);
  703. e2 = (b >> 4);
  704. }
  705. if(x & MDLNOTE_PARAM1)
  706. p1 = track.ReadUint8();
  707. if(x & MDLNOTE_PARAM2)
  708. p2 = track.ReadUint8();
  709. ImportMDLCommands(*m, vol, e1, e2, p1, p2);
  710. }
  711. row++;
  712. break;
  713. }
  714. }
  715. }
  716. }
  717. }
  718. if((loadFlags & loadPatternData) && (chunk = chunks.GetChunk(MDLChunk::idPatNames)).IsValid())
  719. {
  720. PATTERNINDEX i = 0;
  721. while(i < Patterns.Size() && chunk.CanRead(16))
  722. {
  723. char name[17];
  724. chunk.ReadString<mpt::String::spacePadded>(name, 16);
  725. Patterns[i].SetName(name);
  726. }
  727. }
  728. return true;
  729. }
  730. OPENMPT_NAMESPACE_END