1
0

MPTHacks.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * MPTHacks.cpp
  3. * ------------
  4. * Purpose: Find out if MOD/XM/S3M/IT modules have MPT-specific hacks and fix them.
  5. * Notes : This is not finished yet. Still need to handle:
  6. * - Out-of-range sample pre-amp settings
  7. * - Comments in XM files
  8. * - Many auto-fix actions (so that the auto-fix mode can actually be used at some point!)
  9. * Maybe there should be two options if hacks are found: Convert the song to MPTM or remove hacks.
  10. * Authors: OpenMPT Devs
  11. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  12. */
  13. #include "stdafx.h"
  14. #include "Moddoc.h"
  15. #include "../soundlib/modsmp_ctrl.h"
  16. #include "../soundlib/mod_specifications.h"
  17. OPENMPT_NAMESPACE_BEGIN
  18. // Find and fix envelopes where two nodes are on the same tick.
  19. bool FindIncompatibleEnvelopes(InstrumentEnvelope &env, bool autofix)
  20. {
  21. bool found = false;
  22. for(uint32 i = 1; i < env.size(); i++)
  23. {
  24. if(env[i].tick <= env[i - 1].tick) // "<=" so we can fix envelopes "on the fly"
  25. {
  26. found = true;
  27. if(autofix)
  28. {
  29. env[i].tick = env[i - 1].tick + 1;
  30. }
  31. }
  32. }
  33. return found;
  34. }
  35. // Go through the module to find out if it contains any hacks introduced by (Open)MPT
  36. bool CModDoc::HasMPTHacks(const bool autofix)
  37. {
  38. const CModSpecifications *originalSpecs = &m_SndFile.GetModSpecifications();
  39. // retrieve original (not hacked) specs.
  40. MODTYPE modType = m_SndFile.GetBestSaveFormat();
  41. switch(modType)
  42. {
  43. case MOD_TYPE_MOD:
  44. originalSpecs = &ModSpecs::mod;
  45. break;
  46. case MOD_TYPE_XM:
  47. originalSpecs = &ModSpecs::xm;
  48. break;
  49. case MOD_TYPE_S3M:
  50. originalSpecs = &ModSpecs::s3m;
  51. break;
  52. case MOD_TYPE_IT:
  53. originalSpecs = &ModSpecs::it;
  54. break;
  55. }
  56. bool foundHacks = false, foundHere = false;
  57. ClearLog();
  58. // Check for plugins
  59. #ifndef NO_PLUGINS
  60. foundHere = false;
  61. for(const auto &plug : m_SndFile.m_MixPlugins)
  62. {
  63. if(plug.IsValidPlugin())
  64. {
  65. foundHere = foundHacks = true;
  66. break;
  67. }
  68. // REQUIRES AUTOFIX
  69. }
  70. if(foundHere)
  71. AddToLog("Found plugins");
  72. #endif // NO_PLUGINS
  73. // Check for invalid order items
  74. if(!originalSpecs->hasIgnoreIndex && mpt::contains(m_SndFile.Order(), m_SndFile.Order.GetIgnoreIndex()))
  75. {
  76. foundHacks = true;
  77. AddToLog("This format does not support separator (+++) patterns");
  78. if(autofix)
  79. {
  80. m_SndFile.Order().RemovePattern(m_SndFile.Order.GetIgnoreIndex());
  81. }
  82. }
  83. if(!originalSpecs->hasStopIndex && m_SndFile.Order().GetLengthFirstEmpty() != m_SndFile.Order().GetLengthTailTrimmed())
  84. {
  85. foundHacks = true;
  86. AddToLog("The pattern sequence should end after the first stop (---) index in this format.");
  87. if(autofix)
  88. {
  89. m_SndFile.Order().RemovePattern(m_SndFile.Order.GetInvalidPatIndex());
  90. }
  91. }
  92. // Global volume
  93. if(modType == MOD_TYPE_XM && m_SndFile.m_nDefaultGlobalVolume != MAX_GLOBAL_VOLUME)
  94. {
  95. foundHacks = true;
  96. AddToLog("XM format does not support default global volume");
  97. if(autofix)
  98. {
  99. GlobalVolumeToPattern();
  100. }
  101. }
  102. // Pattern count
  103. if(m_SndFile.Patterns.GetNumPatterns() > originalSpecs->patternsMax)
  104. {
  105. AddToLog(MPT_AFORMAT("Found too many patterns ({} allowed)")(originalSpecs->patternsMax));
  106. foundHacks = true;
  107. // REQUIRES (INTELLIGENT) AUTOFIX
  108. }
  109. // Check for too big/small patterns
  110. foundHere = false;
  111. for(auto &pat : m_SndFile.Patterns)
  112. {
  113. if(pat.IsValid())
  114. {
  115. const ROWINDEX patSize = pat.GetNumRows();
  116. if(patSize > originalSpecs->patternRowsMax)
  117. {
  118. foundHacks = foundHere = true;
  119. if(autofix)
  120. {
  121. // REQUIRES (INTELLIGENT) AUTOFIX
  122. } else
  123. {
  124. break;
  125. }
  126. } else if(patSize < originalSpecs->patternRowsMin)
  127. {
  128. foundHacks = foundHere = true;
  129. if(autofix)
  130. {
  131. pat.Resize(originalSpecs->patternRowsMin);
  132. pat.WriteEffect(EffectWriter(CMD_PATTERNBREAK, 0).Row(patSize - 1).RetryNextRow());
  133. } else
  134. {
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. if(foundHere)
  141. {
  142. AddToLog(MPT_AFORMAT("Found incompatible pattern lengths (must be between {} and {} rows)")(originalSpecs->patternRowsMin, originalSpecs->patternRowsMax));
  143. }
  144. // Check for invalid pattern commands
  145. foundHere = false;
  146. m_SndFile.Patterns.ForEachModCommand([originalSpecs, &foundHere, autofix, modType] (ModCommand &m)
  147. {
  148. // definitely not perfect yet. :)
  149. // Probably missing: Some extended effect parameters
  150. if(!originalSpecs->HasNote(m.note))
  151. {
  152. foundHere = true;
  153. if(autofix)
  154. m.note = NOTE_NONE;
  155. }
  156. if(!originalSpecs->HasCommand(m.command))
  157. {
  158. foundHere = true;
  159. if(autofix)
  160. m.command = CMD_NONE;
  161. }
  162. if(!originalSpecs->HasVolCommand(m.volcmd))
  163. {
  164. foundHere = true;
  165. if(autofix)
  166. m.volcmd = VOLCMD_NONE;
  167. }
  168. if(modType == MOD_TYPE_XM) // ModPlug XM extensions
  169. {
  170. if(m.command == CMD_XFINEPORTAUPDOWN && m.param >= 0x30)
  171. {
  172. foundHere = true;
  173. if(autofix)
  174. m.command = CMD_NONE;
  175. }
  176. } else if(modType == MOD_TYPE_IT) // ModPlug IT extensions
  177. {
  178. if((m.command == CMD_S3MCMDEX) && ((m.param & 0xF0) == 0x90) && (m.param != 0x91))
  179. {
  180. foundHere = true;
  181. if(autofix)
  182. m.command = CMD_NONE;
  183. }
  184. }
  185. });
  186. if(foundHere)
  187. {
  188. AddToLog("Found invalid pattern commands");
  189. foundHacks = true;
  190. }
  191. // Check for pattern names
  192. const PATTERNINDEX numNamedPatterns = m_SndFile.Patterns.GetNumNamedPatterns();
  193. if(numNamedPatterns > 0 && !originalSpecs->hasPatternNames)
  194. {
  195. AddToLog("Found pattern names");
  196. foundHacks = true;
  197. if(autofix)
  198. {
  199. for(PATTERNINDEX i = 0; i < numNamedPatterns; i++)
  200. {
  201. m_SndFile.Patterns[i].SetName("");
  202. }
  203. }
  204. }
  205. // Check for too many channels
  206. if(m_SndFile.GetNumChannels() > originalSpecs->channelsMax || m_SndFile.GetNumChannels() < originalSpecs->channelsMin)
  207. {
  208. AddToLog(MPT_AFORMAT("Found incompatible channel count (must be between {} and {} channels)")(originalSpecs->channelsMin, originalSpecs->channelsMax));
  209. foundHacks = true;
  210. if(autofix)
  211. {
  212. std::vector<bool> usedChannels;
  213. CheckUsedChannels(usedChannels);
  214. RemoveChannels(usedChannels);
  215. // REQUIRES (INTELLIGENT) AUTOFIX
  216. }
  217. }
  218. // Check for channel names
  219. foundHere = false;
  220. for(CHANNELINDEX i = 0; i < m_SndFile.GetNumChannels(); i++)
  221. {
  222. if(!m_SndFile.ChnSettings[i].szName.empty())
  223. {
  224. foundHere = foundHacks = true;
  225. if(autofix)
  226. m_SndFile.ChnSettings[i].szName = "";
  227. else
  228. break;
  229. }
  230. }
  231. if(foundHere)
  232. AddToLog("Found channel names");
  233. // Check for too many samples
  234. if(m_SndFile.GetNumSamples() > originalSpecs->samplesMax)
  235. {
  236. AddToLog(MPT_AFORMAT("Found too many samples ({} allowed)")(originalSpecs->samplesMax));
  237. foundHacks = true;
  238. // REQUIRES (INTELLIGENT) AUTOFIX
  239. }
  240. // Check for sample extensions
  241. foundHere = false;
  242. for(SAMPLEINDEX i = 1; i <= m_SndFile.GetNumSamples(); i++)
  243. {
  244. ModSample &smp = m_SndFile.GetSample(i);
  245. if(modType == MOD_TYPE_XM && smp.GetNumChannels() > 1)
  246. {
  247. foundHere = foundHacks = true;
  248. if(autofix)
  249. {
  250. ctrlSmp::ConvertToMono(smp, m_SndFile, ctrlSmp::mixChannels);
  251. } else
  252. {
  253. break;
  254. }
  255. }
  256. }
  257. if(foundHere)
  258. AddToLog("Stereo samples are not supported in the original XM format");
  259. // Check for too many instruments
  260. if(m_SndFile.GetNumInstruments() > originalSpecs->instrumentsMax)
  261. {
  262. AddToLog(MPT_AFORMAT("Found too many instruments ({} allowed)")(originalSpecs->instrumentsMax));
  263. foundHacks = true;
  264. // REQUIRES (INTELLIGENT) AUTOFIX
  265. }
  266. // Check for instrument extensions
  267. foundHere = false;
  268. bool foundEnvelopes = false;
  269. for(INSTRUMENTINDEX i = 1; i <= m_SndFile.GetNumInstruments(); i++)
  270. {
  271. ModInstrument *instr = m_SndFile.Instruments[i];
  272. if(instr == nullptr) continue;
  273. // Extended instrument attributes
  274. if(instr->filterMode != FilterMode::Unchanged || instr->nVolRampUp != 0 || instr->resampling != SRCMODE_DEFAULT ||
  275. instr->nCutSwing != 0 || instr->nResSwing != 0 || instr->nMixPlug != 0 || instr->pitchToTempoLock.GetRaw() != 0 ||
  276. instr->nDCT == DuplicateCheckType::Plugin ||
  277. instr->VolEnv.nReleaseNode != ENV_RELEASE_NODE_UNSET ||
  278. instr->PanEnv.nReleaseNode != ENV_RELEASE_NODE_UNSET ||
  279. instr->PitchEnv.nReleaseNode != ENV_RELEASE_NODE_UNSET
  280. )
  281. {
  282. foundHere = foundHacks = true;
  283. if(autofix)
  284. {
  285. instr->filterMode = FilterMode::Unchanged;
  286. instr->nVolRampUp = 0;
  287. instr->resampling = SRCMODE_DEFAULT;
  288. instr->nCutSwing = 0;
  289. instr->nResSwing = 0;
  290. instr->nMixPlug = 0;
  291. instr->pitchToTempoLock.Set(0);
  292. if(instr->nDCT == DuplicateCheckType::Plugin) instr->nDCT = DuplicateCheckType::None;
  293. instr->VolEnv.nReleaseNode = instr->PanEnv.nReleaseNode = instr->PitchEnv.nReleaseNode = ENV_RELEASE_NODE_UNSET;
  294. }
  295. }
  296. // Incompatible envelope shape
  297. foundEnvelopes |= FindIncompatibleEnvelopes(instr->VolEnv, autofix);
  298. foundEnvelopes |= FindIncompatibleEnvelopes(instr->PanEnv, autofix);
  299. foundEnvelopes |= FindIncompatibleEnvelopes(instr->PitchEnv, autofix);
  300. foundHacks |= foundEnvelopes;
  301. }
  302. if(foundHere)
  303. AddToLog("Found MPT instrument extensions");
  304. if(foundEnvelopes)
  305. AddToLog("Two envelope points may not share the same tick.");
  306. // Check for too many orders
  307. if(m_SndFile.Order().GetLengthTailTrimmed() > originalSpecs->ordersMax)
  308. {
  309. AddToLog(MPT_AFORMAT("Found too many orders ({} allowed)")(originalSpecs->ordersMax));
  310. foundHacks = true;
  311. if(autofix)
  312. {
  313. // Can we be more intelligent here and maybe remove stop patterns and such?
  314. m_SndFile.Order().resize(originalSpecs->ordersMax);
  315. }
  316. }
  317. // Check for invalid default tempo
  318. if(m_SndFile.m_nDefaultTempo > originalSpecs->GetTempoMax() || m_SndFile.m_nDefaultTempo < originalSpecs->GetTempoMin())
  319. {
  320. AddToLog(MPT_AFORMAT("Found incompatible default tempo (must be between {} and {})")(originalSpecs->GetTempoMin().GetInt(), originalSpecs->GetTempoMax().GetInt()));
  321. foundHacks = true;
  322. if(autofix)
  323. m_SndFile.m_nDefaultTempo = Clamp(m_SndFile.m_nDefaultTempo, originalSpecs->GetTempoMin(), originalSpecs->GetTempoMax());
  324. }
  325. // Check for invalid default speed
  326. if(m_SndFile.m_nDefaultSpeed > originalSpecs->speedMax || m_SndFile.m_nDefaultSpeed < originalSpecs->speedMin)
  327. {
  328. AddToLog(MPT_AFORMAT("Found incompatible default speed (must be between {} and {})")(originalSpecs->speedMin, originalSpecs->speedMax));
  329. foundHacks = true;
  330. if(autofix)
  331. m_SndFile.m_nDefaultSpeed = Clamp(m_SndFile.m_nDefaultSpeed, originalSpecs->speedMin, originalSpecs->speedMax);
  332. }
  333. // Check for invalid rows per beat / measure values
  334. if(m_SndFile.m_nDefaultRowsPerBeat >= originalSpecs->patternRowsMax || m_SndFile.m_nDefaultRowsPerMeasure >= originalSpecs->patternRowsMax)
  335. {
  336. AddToLog("Found incompatible rows per beat / measure");
  337. foundHacks = true;
  338. if(autofix)
  339. {
  340. m_SndFile.m_nDefaultRowsPerBeat = Clamp(m_SndFile.m_nDefaultRowsPerBeat, 1u, (originalSpecs->patternRowsMax - 1));
  341. m_SndFile.m_nDefaultRowsPerMeasure = Clamp(m_SndFile.m_nDefaultRowsPerMeasure, m_SndFile.m_nDefaultRowsPerBeat, (originalSpecs->patternRowsMax - 1));
  342. }
  343. }
  344. // Find pattern-specific time signatures
  345. if(!originalSpecs->hasPatternSignatures)
  346. {
  347. foundHere = false;
  348. for(auto &pat : m_SndFile.Patterns)
  349. {
  350. if(pat.GetOverrideSignature())
  351. {
  352. if(!foundHere)
  353. AddToLog("Found pattern-specific time signatures");
  354. if(autofix)
  355. pat.RemoveSignature();
  356. foundHacks = foundHere = true;
  357. if(!autofix)
  358. break;
  359. }
  360. }
  361. }
  362. // Check for new tempo modes
  363. if(m_SndFile.m_nTempoMode != TempoMode::Classic)
  364. {
  365. AddToLog("Found incompatible tempo mode (only classic tempo mode allowed)");
  366. foundHacks = true;
  367. if(autofix)
  368. m_SndFile.m_nTempoMode = TempoMode::Classic;
  369. }
  370. // Check for extended filter range flag
  371. if(m_SndFile.m_SongFlags[SONG_EXFILTERRANGE])
  372. {
  373. AddToLog("Found extended filter range");
  374. foundHacks = true;
  375. if(autofix)
  376. m_SndFile.m_SongFlags.reset(SONG_EXFILTERRANGE);
  377. }
  378. // Player flags
  379. if((modType & (MOD_TYPE_XM|MOD_TYPE_IT)) && !m_SndFile.m_playBehaviour[MSF_COMPATIBLE_PLAY])
  380. {
  381. AddToLog("Compatible play is deactivated");
  382. foundHacks = true;
  383. if(autofix)
  384. m_SndFile.SetDefaultPlaybackBehaviour(modType);
  385. }
  386. // Check for restart position where it should not be
  387. for(SEQUENCEINDEX seq = 0; seq < m_SndFile.Order.GetNumSequences(); seq++)
  388. {
  389. if(m_SndFile.Order(seq).GetRestartPos() > 0 && !originalSpecs->hasRestartPos)
  390. {
  391. AddToLog("Found restart position");
  392. foundHacks = true;
  393. if(autofix)
  394. {
  395. m_SndFile.Order.RestartPosToPattern(seq);
  396. }
  397. }
  398. }
  399. if(!originalSpecs->hasArtistName && !m_SndFile.m_songArtist.empty() && !(modType & (MOD_TYPE_MOD | MOD_TYPE_S3M)))
  400. {
  401. AddToLog("Found artist name");
  402. foundHacks = true;
  403. if(autofix)
  404. {
  405. m_SndFile.m_songArtist.clear();
  406. }
  407. }
  408. if(m_SndFile.GetMixLevels() != MixLevels::Compatible && m_SndFile.GetMixLevels() != MixLevels::CompatibleFT2)
  409. {
  410. AddToLog("Found incorrect mix levels (only compatible mix levels allowed)");
  411. foundHacks = true;
  412. if(autofix)
  413. m_SndFile.SetMixLevels(modType == MOD_TYPE_XM ? MixLevels::CompatibleFT2 : MixLevels::Compatible);
  414. }
  415. // Check for extended MIDI macros
  416. if(modType == MOD_TYPE_IT)
  417. {
  418. for(const auto &macro : m_SndFile.m_MidiCfg)
  419. {
  420. for(const auto c : std::string_view{macro})
  421. {
  422. if(c == 's')
  423. {
  424. foundHacks = true;
  425. AddToLog("Found SysEx checksum variable in MIDI macro");
  426. break;
  427. }
  428. }
  429. }
  430. }
  431. if(autofix && foundHacks)
  432. SetModified();
  433. return foundHacks;
  434. }
  435. OPENMPT_NAMESPACE_END