modcommand.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. /*
  2. * modcommand.cpp
  3. * --------------
  4. * Purpose: Various functions for writing effects to patterns, converting ModCommands, etc.
  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 "Sndfile.h"
  11. #include "mod_specifications.h"
  12. #include "Tables.h"
  13. OPENMPT_NAMESPACE_BEGIN
  14. const EffectType effectTypes[] =
  15. {
  16. EFFECT_TYPE_NORMAL, EFFECT_TYPE_NORMAL, EFFECT_TYPE_PITCH, EFFECT_TYPE_PITCH,
  17. EFFECT_TYPE_PITCH, EFFECT_TYPE_PITCH, EFFECT_TYPE_VOLUME, EFFECT_TYPE_VOLUME,
  18. EFFECT_TYPE_VOLUME, EFFECT_TYPE_PANNING, EFFECT_TYPE_NORMAL, EFFECT_TYPE_VOLUME,
  19. EFFECT_TYPE_GLOBAL, EFFECT_TYPE_VOLUME, EFFECT_TYPE_GLOBAL, EFFECT_TYPE_NORMAL,
  20. EFFECT_TYPE_GLOBAL, EFFECT_TYPE_GLOBAL, EFFECT_TYPE_NORMAL, EFFECT_TYPE_NORMAL,
  21. EFFECT_TYPE_NORMAL, EFFECT_TYPE_VOLUME, EFFECT_TYPE_VOLUME, EFFECT_TYPE_GLOBAL,
  22. EFFECT_TYPE_GLOBAL, EFFECT_TYPE_NORMAL, EFFECT_TYPE_PITCH, EFFECT_TYPE_PANNING,
  23. EFFECT_TYPE_PITCH, EFFECT_TYPE_PANNING, EFFECT_TYPE_NORMAL, EFFECT_TYPE_NORMAL,
  24. EFFECT_TYPE_NORMAL, EFFECT_TYPE_NORMAL, EFFECT_TYPE_NORMAL, EFFECT_TYPE_PITCH,
  25. EFFECT_TYPE_PITCH, EFFECT_TYPE_NORMAL, EFFECT_TYPE_PITCH, EFFECT_TYPE_PITCH,
  26. EFFECT_TYPE_PITCH, EFFECT_TYPE_PITCH, EFFECT_TYPE_NORMAL, EFFECT_TYPE_NORMAL,
  27. EFFECT_TYPE_NORMAL, EFFECT_TYPE_NORMAL,
  28. };
  29. static_assert(std::size(effectTypes) == MAX_EFFECTS);
  30. const EffectType volumeEffectTypes[] =
  31. {
  32. EFFECT_TYPE_NORMAL, EFFECT_TYPE_VOLUME, EFFECT_TYPE_PANNING, EFFECT_TYPE_VOLUME,
  33. EFFECT_TYPE_VOLUME, EFFECT_TYPE_VOLUME, EFFECT_TYPE_VOLUME, EFFECT_TYPE_PITCH,
  34. EFFECT_TYPE_PITCH, EFFECT_TYPE_PANNING, EFFECT_TYPE_PANNING, EFFECT_TYPE_PITCH,
  35. EFFECT_TYPE_PITCH, EFFECT_TYPE_PITCH, EFFECT_TYPE_NORMAL, EFFECT_TYPE_NORMAL,
  36. };
  37. static_assert(std::size(volumeEffectTypes) == MAX_VOLCMDS);
  38. EffectType ModCommand::GetEffectType(COMMAND cmd)
  39. {
  40. if(cmd < std::size(effectTypes))
  41. return effectTypes[cmd];
  42. else
  43. return EFFECT_TYPE_NORMAL;
  44. }
  45. EffectType ModCommand::GetVolumeEffectType(VOLCMD volcmd)
  46. {
  47. if(volcmd < std::size(volumeEffectTypes))
  48. return volumeEffectTypes[volcmd];
  49. else
  50. return EFFECT_TYPE_NORMAL;
  51. }
  52. // Convert an Exx command (MOD) to Sxx command (S3M)
  53. void ModCommand::ExtendedMODtoS3MEffect()
  54. {
  55. if(command != CMD_MODCMDEX)
  56. return;
  57. command = CMD_S3MCMDEX;
  58. switch(param & 0xF0)
  59. {
  60. case 0x00: command = CMD_NONE; break; // No filter control
  61. case 0x10: command = CMD_PORTAMENTOUP; param |= 0xF0; break;
  62. case 0x20: command = CMD_PORTAMENTODOWN; param |= 0xF0; break;
  63. case 0x30: param = (param & 0x0F) | 0x10; break;
  64. case 0x40: param = (param & 0x03) | 0x30; break;
  65. case 0x50: param = (param & 0x0F) | 0x20; break;
  66. case 0x60: param = (param & 0x0F) | 0xB0; break;
  67. case 0x70: param = (param & 0x03) | 0x40; break;
  68. case 0x90: command = CMD_RETRIG; param = (param & 0x0F); break;
  69. case 0xA0: if(param & 0x0F) { command = CMD_VOLUMESLIDE; param = (param << 4) | 0x0F; } else command = CMD_NONE; break;
  70. case 0xB0: if(param & 0x0F) { command = CMD_VOLUMESLIDE; param = 0xF0 | static_cast<PARAM>(std::min(param & 0x0F, 0x0E)); } else command = CMD_NONE; break;
  71. case 0xC0: if(param == 0xC0) { command = CMD_NONE; note = NOTE_NOTECUT; } break; // this does different things in IT and ST3
  72. case 0xD0: if(param == 0xD0) { command = CMD_NONE; } break; // ditto
  73. // rest are the same or handled elsewhere
  74. }
  75. }
  76. // Convert an Sxx command (S3M) to Exx command (MOD)
  77. void ModCommand::ExtendedS3MtoMODEffect()
  78. {
  79. if(command != CMD_S3MCMDEX)
  80. return;
  81. command = CMD_MODCMDEX;
  82. switch(param & 0xF0)
  83. {
  84. case 0x10: param = (param & 0x0F) | 0x30; break;
  85. case 0x20: param = (param & 0x0F) | 0x50; break;
  86. case 0x30: param = (param & 0x0F) | 0x40; break;
  87. case 0x40: param = (param & 0x0F) | 0x70; break;
  88. case 0x50: command = CMD_XFINEPORTAUPDOWN; break; // map to unused X5x
  89. case 0x60: command = CMD_XFINEPORTAUPDOWN; break; // map to unused X6x
  90. case 0x80: command = CMD_PANNING8; param = (param & 0x0F) * 0x11; break; // FT2 does actually not support E8x
  91. case 0x90: command = CMD_XFINEPORTAUPDOWN; break; // map to unused X9x
  92. case 0xA0: command = CMD_XFINEPORTAUPDOWN; break; // map to unused XAx
  93. case 0xB0: param = (param & 0x0F) | 0x60; break;
  94. case 0x70: command = CMD_NONE; break; // No NNA / envelope control in MOD/XM format
  95. // rest are the same or handled elsewhere
  96. }
  97. }
  98. // Convert a mod command from one format to another.
  99. void ModCommand::Convert(MODTYPE fromType, MODTYPE toType, const CSoundFile &sndFile)
  100. {
  101. if(fromType == toType)
  102. {
  103. return;
  104. }
  105. if(fromType == MOD_TYPE_MTM)
  106. {
  107. // Special MTM fixups.
  108. // Retrigger with param 0
  109. if(command == CMD_MODCMDEX && param == 0x90)
  110. {
  111. command = CMD_NONE;
  112. } else if(command == CMD_VIBRATO)
  113. {
  114. // Vibrato is approximately half as deep compared to MOD/S3M.
  115. uint8 speed = (param & 0xF0);
  116. uint8 depth = (param & 0x0F) >> 1;
  117. param = speed | depth;
  118. }
  119. // Apart from these special fixups, do a regular conversion from MOD.
  120. fromType = MOD_TYPE_MOD;
  121. }
  122. if(command == CMD_DIGIREVERSESAMPLE && toType != MOD_TYPE_DIGI)
  123. {
  124. command = CMD_S3MCMDEX;
  125. param = 0x9F;
  126. }
  127. // helper variables
  128. const bool oldTypeIsMOD = (fromType == MOD_TYPE_MOD), oldTypeIsXM = (fromType == MOD_TYPE_XM),
  129. oldTypeIsS3M = (fromType == MOD_TYPE_S3M), oldTypeIsIT = (fromType == MOD_TYPE_IT),
  130. oldTypeIsMPT = (fromType == MOD_TYPE_MPT), oldTypeIsMOD_XM = (oldTypeIsMOD || oldTypeIsXM),
  131. oldTypeIsS3M_IT_MPT = (oldTypeIsS3M || oldTypeIsIT || oldTypeIsMPT),
  132. oldTypeIsIT_MPT = (oldTypeIsIT || oldTypeIsMPT);
  133. const bool newTypeIsMOD = (toType == MOD_TYPE_MOD), newTypeIsXM = (toType == MOD_TYPE_XM),
  134. newTypeIsS3M = (toType == MOD_TYPE_S3M), newTypeIsIT = (toType == MOD_TYPE_IT),
  135. newTypeIsMPT = (toType == MOD_TYPE_MPT), newTypeIsMOD_XM = (newTypeIsMOD || newTypeIsXM),
  136. newTypeIsS3M_IT_MPT = (newTypeIsS3M || newTypeIsIT || newTypeIsMPT),
  137. newTypeIsIT_MPT = (newTypeIsIT || newTypeIsMPT);
  138. const CModSpecifications &newSpecs = CSoundFile::GetModSpecifications(toType);
  139. //////////////////////////
  140. // Convert 8-bit Panning
  141. if(command == CMD_PANNING8)
  142. {
  143. if(newTypeIsS3M)
  144. {
  145. param = (param + 1) >> 1;
  146. } else if(oldTypeIsS3M)
  147. {
  148. if(param == 0xA4)
  149. {
  150. // surround remap
  151. command = static_cast<COMMAND>((toType & (MOD_TYPE_IT | MOD_TYPE_MPT)) ? CMD_S3MCMDEX : CMD_XFINEPORTAUPDOWN);
  152. param = 0x91;
  153. } else
  154. {
  155. param = mpt::saturate_cast<PARAM>(param * 2u);
  156. }
  157. }
  158. } // End if(command == CMD_PANNING8)
  159. // Re-map \xx to Zxx if the new format only knows the latter command.
  160. if(command == CMD_SMOOTHMIDI && !newSpecs.HasCommand(CMD_SMOOTHMIDI) && newSpecs.HasCommand(CMD_MIDI))
  161. {
  162. command = CMD_MIDI;
  163. }
  164. ///////////////////////////////////////////////////////////////////////////////////////
  165. // MPTM to anything: Convert param control, extended envelope control, note delay+cut
  166. if(oldTypeIsMPT)
  167. {
  168. if(IsPcNote())
  169. {
  170. COMMAND newCmd = static_cast<COMMAND>(note == NOTE_PC ? CMD_MIDI : CMD_SMOOTHMIDI);
  171. if(!newSpecs.HasCommand(newCmd))
  172. {
  173. newCmd = CMD_MIDI; // assuming that this was CMD_SMOOTHMIDI
  174. if(!newSpecs.HasCommand(newCmd))
  175. {
  176. newCmd = CMD_NONE;
  177. }
  178. }
  179. param = static_cast<PARAM>(std::min(static_cast<uint16>(maxColumnValue), GetValueEffectCol()) * 0x7F / maxColumnValue);
  180. command = newCmd; // might be removed later
  181. volcmd = VOLCMD_NONE;
  182. note = NOTE_NONE;
  183. instr = 0;
  184. }
  185. if((command == CMD_S3MCMDEX) && ((param & 0xF0) == 0x70) && ((param & 0x0F) > 0x0C))
  186. {
  187. // Extended pitch envelope control commands
  188. param = 0x7C;
  189. } else if(command == CMD_DELAYCUT)
  190. {
  191. command = CMD_S3MCMDEX; // When converting to MOD/XM, this will be converted to CMD_MODCMDEX later
  192. param = 0xD0 | (param >> 4); // Preserve delay nibble
  193. } else if(command == CMD_FINETUNE || command == CMD_FINETUNE_SMOOTH)
  194. {
  195. // Convert finetune from +/-128th of a semitone to (extra-)fine portamento (assumes linear slides, plus we're missing the actual pitch wheel depth of the instrument)
  196. if(param < 0x80)
  197. {
  198. command = CMD_PORTAMENTODOWN;
  199. param = 0x80 - param;
  200. } else if(param > 0x80)
  201. {
  202. command = CMD_PORTAMENTOUP;
  203. param -= 0x80;
  204. }
  205. if(param <= 30)
  206. param = 0xE0 | ((param + 1u) / 2u);
  207. else
  208. param = 0xF0 | std::min(static_cast<PARAM>((param + 7u) / 8u), PARAM(15));
  209. }
  210. } // End if(oldTypeIsMPT)
  211. /////////////////////////////////////////
  212. // Convert MOD / XM to S3M / IT / MPTM
  213. if(oldTypeIsMOD_XM && newTypeIsS3M_IT_MPT)
  214. {
  215. switch(command)
  216. {
  217. case CMD_ARPEGGIO:
  218. if(!param) command = CMD_NONE; // 000 does nothing in MOD/XM
  219. break;
  220. case CMD_MODCMDEX:
  221. ExtendedMODtoS3MEffect();
  222. break;
  223. case CMD_VOLUME:
  224. // Effect column volume command overrides the volume column in XM.
  225. if(volcmd == VOLCMD_NONE || volcmd == VOLCMD_VOLUME)
  226. {
  227. volcmd = VOLCMD_VOLUME;
  228. vol = param;
  229. if(vol > 64) vol = 64;
  230. command = CMD_NONE;
  231. param = 0;
  232. } else if(volcmd == VOLCMD_PANNING)
  233. {
  234. std::swap(vol, param);
  235. volcmd = VOLCMD_VOLUME;
  236. if(vol > 64) vol = 64;
  237. command = CMD_S3MCMDEX;
  238. param = 0x80 | (param / 4); // XM volcol panning is actually 4-Bit, so we can use 4-Bit panning here.
  239. }
  240. break;
  241. case CMD_PORTAMENTOUP:
  242. if(param > 0xDF) param = 0xDF;
  243. break;
  244. case CMD_PORTAMENTODOWN:
  245. if(param > 0xDF) param = 0xDF;
  246. break;
  247. case CMD_XFINEPORTAUPDOWN:
  248. switch(param & 0xF0)
  249. {
  250. case 0x10: command = CMD_PORTAMENTOUP; param = (param & 0x0F) | 0xE0; break;
  251. case 0x20: command = CMD_PORTAMENTODOWN; param = (param & 0x0F) | 0xE0; break;
  252. case 0x50:
  253. case 0x60:
  254. case 0x70:
  255. case 0x90:
  256. case 0xA0:
  257. command = CMD_S3MCMDEX;
  258. // Surround remap (this is the "official" command)
  259. if(toType & MOD_TYPE_S3M && param == 0x91)
  260. {
  261. command = CMD_PANNING8;
  262. param = 0xA4;
  263. }
  264. break;
  265. }
  266. break;
  267. case CMD_KEYOFF:
  268. if(note == NOTE_NONE)
  269. {
  270. note = newTypeIsS3M ? NOTE_NOTECUT : NOTE_KEYOFF;
  271. command = CMD_S3MCMDEX;
  272. if(param == 0)
  273. instr = 0;
  274. param = 0xD0 | (param & 0x0F);
  275. }
  276. break;
  277. case CMD_PANNINGSLIDE:
  278. // swap L/R, convert to fine slide
  279. if(param & 0xF0)
  280. {
  281. param = 0xF0 | std::min(PARAM(0x0E), static_cast<PARAM>(param >> 4));
  282. } else
  283. {
  284. param = 0x0F | (std::min(PARAM(0x0E), static_cast<PARAM>(param & 0x0F)) << 4);
  285. }
  286. break;
  287. default:
  288. break;
  289. }
  290. } // End if(oldTypeIsMOD_XM && newTypeIsS3M_IT_MPT)
  291. /////////////////////////////////////////
  292. // Convert S3M / IT / MPTM to MOD / XM
  293. else if(oldTypeIsS3M_IT_MPT && newTypeIsMOD_XM)
  294. {
  295. if(note == NOTE_NOTECUT)
  296. {
  297. // convert note cut to C00 if possible or volume command otherwise (MOD/XM has no real way of cutting notes that cannot be "undone" by volume commands)
  298. note = NOTE_NONE;
  299. if(command == CMD_NONE || !newTypeIsXM)
  300. {
  301. command = CMD_VOLUME;
  302. param = 0;
  303. } else
  304. {
  305. volcmd = VOLCMD_VOLUME;
  306. vol = 0;
  307. }
  308. } else if(note == NOTE_FADE)
  309. {
  310. // convert note fade to note off
  311. note = NOTE_KEYOFF;
  312. }
  313. switch(command)
  314. {
  315. case CMD_S3MCMDEX:
  316. ExtendedS3MtoMODEffect();
  317. break;
  318. case CMD_TONEPORTAVOL: // Can't do fine slides and portamento/vibrato at the same time :(
  319. case CMD_VIBRATOVOL: // ditto
  320. if(volcmd == VOLCMD_NONE && (((param & 0xF0) && ((param & 0x0F) == 0x0F)) || ((param & 0x0F) && ((param & 0xF0) == 0xF0))))
  321. {
  322. // Try to salvage portamento/vibrato
  323. if(command == CMD_TONEPORTAVOL)
  324. volcmd = VOLCMD_TONEPORTAMENTO;
  325. else if(command == CMD_VIBRATOVOL)
  326. volcmd = VOLCMD_VIBRATODEPTH;
  327. vol = 0;
  328. }
  329. [[fallthrough]];
  330. case CMD_VOLUMESLIDE:
  331. if((param & 0xF0) && ((param & 0x0F) == 0x0F))
  332. {
  333. command = CMD_MODCMDEX;
  334. param = (param >> 4) | 0xA0;
  335. } else if((param & 0x0F) && ((param & 0xF0) == 0xF0))
  336. {
  337. command = CMD_MODCMDEX;
  338. param = (param & 0x0F) | 0xB0;
  339. }
  340. break;
  341. case CMD_PORTAMENTOUP:
  342. if(param >= 0xF0)
  343. {
  344. command = CMD_MODCMDEX;
  345. param = (param & 0x0F) | 0x10;
  346. } else if(param >= 0xE0)
  347. {
  348. if(newTypeIsXM)
  349. {
  350. command = CMD_XFINEPORTAUPDOWN;
  351. param = 0x10 | (param & 0x0F);
  352. } else
  353. {
  354. command = CMD_MODCMDEX;
  355. param = (((param & 0x0F) + 3) >> 2) | 0x10;
  356. }
  357. } else
  358. {
  359. command = CMD_PORTAMENTOUP;
  360. }
  361. break;
  362. case CMD_PORTAMENTODOWN:
  363. if(param >= 0xF0)
  364. {
  365. command = CMD_MODCMDEX;
  366. param = (param & 0x0F) | 0x20;
  367. } else if(param >= 0xE0)
  368. {
  369. if(newTypeIsXM)
  370. {
  371. command = CMD_XFINEPORTAUPDOWN;
  372. param = 0x20 | (param & 0x0F);
  373. } else
  374. {
  375. command = CMD_MODCMDEX;
  376. param = (((param & 0x0F) + 3) >> 2) | 0x20;
  377. }
  378. } else
  379. {
  380. command = CMD_PORTAMENTODOWN;
  381. }
  382. break;
  383. case CMD_TEMPO:
  384. if(param < 0x20) command = CMD_NONE; // no tempo slides
  385. break;
  386. case CMD_PANNINGSLIDE:
  387. // swap L/R, convert fine slides to normal slides
  388. if((param & 0x0F) == 0x0F && (param & 0xF0))
  389. {
  390. param = (param >> 4);
  391. } else if((param & 0xF0) == 0xF0 && (param & 0x0F))
  392. {
  393. param = (param & 0x0F) << 4;
  394. } else if(param & 0x0F)
  395. {
  396. param = 0xF0;
  397. } else if(param & 0xF0)
  398. {
  399. param = 0x0F;
  400. } else
  401. {
  402. param = 0;
  403. }
  404. break;
  405. case CMD_RETRIG:
  406. // Retrig: Q0y doesn't change volume in IT/S3M, but R0y in XM takes the last x parameter
  407. if(param != 0 && (param & 0xF0) == 0)
  408. {
  409. param |= 0x80;
  410. }
  411. break;
  412. default:
  413. break;
  414. }
  415. } // End if(oldTypeIsS3M_IT_MPT && newTypeIsMOD_XM)
  416. ///////////////////////
  417. // Convert IT to S3M
  418. else if(oldTypeIsIT_MPT && newTypeIsS3M)
  419. {
  420. if(note == NOTE_KEYOFF || note == NOTE_FADE)
  421. note = NOTE_NOTECUT;
  422. switch(command)
  423. {
  424. case CMD_S3MCMDEX:
  425. switch(param & 0xF0)
  426. {
  427. case 0x70: command = CMD_NONE; break; // No NNA / envelope control in S3M format
  428. case 0x90:
  429. if(param == 0x91)
  430. {
  431. // surround remap (this is the "official" command)
  432. command = CMD_PANNING8;
  433. param = 0xA4;
  434. } else if(param == 0x90)
  435. {
  436. command = CMD_PANNING8;
  437. param = 0x40;
  438. }
  439. break;
  440. }
  441. break;
  442. case CMD_GLOBALVOLUME:
  443. param = (std::min(PARAM(0x80), param) + 1) / 2u;
  444. break;
  445. default:
  446. break;
  447. }
  448. } // End if(oldTypeIsIT_MPT && newTypeIsS3M)
  449. //////////////////////
  450. // Convert IT to XM
  451. if(oldTypeIsIT_MPT && newTypeIsXM)
  452. {
  453. switch(command)
  454. {
  455. case CMD_VIBRATO:
  456. // With linear slides, strength is roughly doubled.
  457. param = (param & 0xF0) | (((param & 0x0F) + 1) / 2u);
  458. break;
  459. case CMD_GLOBALVOLUME:
  460. param = (std::min(PARAM(0x80), param) + 1) / 2u;
  461. break;
  462. }
  463. } // End if(oldTypeIsIT_MPT && newTypeIsXM)
  464. //////////////////////
  465. // Convert XM to IT
  466. if(oldTypeIsXM && newTypeIsIT_MPT)
  467. {
  468. switch(command)
  469. {
  470. case CMD_VIBRATO:
  471. // With linear slides, strength is roughly halved.
  472. param = (param & 0xF0) | std::min(static_cast<PARAM>((param & 0x0F) * 2u), PARAM(15));
  473. break;
  474. case CMD_GLOBALVOLUME:
  475. param = std::min(PARAM(0x40), param) * 2u;
  476. break;
  477. }
  478. } // End if(oldTypeIsIT_MPT && newTypeIsXM)
  479. ///////////////////////////////////
  480. // MOD / XM Speed/Tempo limits
  481. if(newTypeIsMOD_XM)
  482. {
  483. switch(command)
  484. {
  485. case CMD_SPEED:
  486. param = std::min(param, PARAM(0x1F));
  487. break;
  488. break;
  489. case CMD_TEMPO:
  490. param = std::max(param, PARAM(0x20));
  491. break;
  492. }
  493. }
  494. ///////////////////////////////////////////////////////////////////////
  495. // Convert MOD to anything - adjust effect memory, remove Invert Loop
  496. if(oldTypeIsMOD)
  497. {
  498. switch(command)
  499. {
  500. case CMD_TONEPORTAVOL: // lacks memory -> 500 is the same as 300
  501. if(param == 0x00)
  502. command = CMD_TONEPORTAMENTO;
  503. break;
  504. case CMD_VIBRATOVOL: // lacks memory -> 600 is the same as 400
  505. if(param == 0x00)
  506. command = CMD_VIBRATO;
  507. break;
  508. case CMD_PORTAMENTOUP: // lacks memory -> remove
  509. case CMD_PORTAMENTODOWN:
  510. case CMD_VOLUMESLIDE:
  511. if(param == 0x00)
  512. command = CMD_NONE;
  513. break;
  514. case CMD_MODCMDEX: // This would turn into "Set Active Macro", so let's better remove it
  515. case CMD_S3MCMDEX:
  516. if((param & 0xF0) == 0xF0)
  517. command = CMD_NONE;
  518. break;
  519. }
  520. } // End if(oldTypeIsMOD && newTypeIsXM)
  521. /////////////////////////////////////////////////////////////////////
  522. // Convert anything to MOD - remove volume column, remove Set Macro
  523. if(newTypeIsMOD)
  524. {
  525. // convert note off events
  526. if(IsSpecialNote())
  527. {
  528. note = NOTE_NONE;
  529. // no effect present, so just convert note off to volume 0
  530. if(command == CMD_NONE)
  531. {
  532. command = CMD_VOLUME;
  533. param = 0;
  534. // EDx effect present, so convert it to ECx
  535. } else if((command == CMD_MODCMDEX) && ((param & 0xF0) == 0xD0))
  536. {
  537. param = 0xC0 | (param & 0x0F);
  538. }
  539. }
  540. if(command != CMD_NONE) switch(command)
  541. {
  542. case CMD_RETRIG: // MOD only has E9x
  543. command = CMD_MODCMDEX;
  544. param = 0x90 | (param & 0x0F);
  545. break;
  546. case CMD_MODCMDEX: // This would turn into "Invert Loop", so let's better remove it
  547. if((param & 0xF0) == 0xF0) command = CMD_NONE;
  548. break;
  549. }
  550. if(command == CMD_NONE) switch(volcmd)
  551. {
  552. case VOLCMD_VOLUME:
  553. command = CMD_VOLUME;
  554. param = vol;
  555. break;
  556. case VOLCMD_PANNING:
  557. command = CMD_PANNING8;
  558. param = vol < 64 ? vol << 2 : 255;
  559. break;
  560. case VOLCMD_VOLSLIDEDOWN:
  561. command = CMD_VOLUMESLIDE;
  562. param = vol;
  563. break;
  564. case VOLCMD_VOLSLIDEUP:
  565. command = CMD_VOLUMESLIDE;
  566. param = vol << 4;
  567. break;
  568. case VOLCMD_FINEVOLDOWN:
  569. command = CMD_MODCMDEX;
  570. param = 0xB0 | vol;
  571. break;
  572. case VOLCMD_FINEVOLUP:
  573. command = CMD_MODCMDEX;
  574. param = 0xA0 | vol;
  575. break;
  576. case VOLCMD_PORTADOWN:
  577. command = CMD_PORTAMENTODOWN;
  578. param = vol << 2;
  579. break;
  580. case VOLCMD_PORTAUP:
  581. command = CMD_PORTAMENTOUP;
  582. param = vol << 2;
  583. break;
  584. case VOLCMD_TONEPORTAMENTO:
  585. command = CMD_TONEPORTAMENTO;
  586. param = vol << 2;
  587. break;
  588. case VOLCMD_VIBRATODEPTH:
  589. command = CMD_VIBRATO;
  590. param = vol;
  591. break;
  592. case VOLCMD_VIBRATOSPEED:
  593. command = CMD_VIBRATO;
  594. param = vol << 4;
  595. break;
  596. }
  597. volcmd = VOLCMD_NONE;
  598. } // End if(newTypeIsMOD)
  599. ///////////////////////////////////////////////////
  600. // Convert anything to S3M - adjust volume column
  601. if(newTypeIsS3M)
  602. {
  603. if(command == CMD_NONE) switch(volcmd)
  604. {
  605. case VOLCMD_VOLSLIDEDOWN:
  606. command = CMD_VOLUMESLIDE;
  607. param = vol;
  608. volcmd = VOLCMD_NONE;
  609. break;
  610. case VOLCMD_VOLSLIDEUP:
  611. command = CMD_VOLUMESLIDE;
  612. param = vol << 4;
  613. volcmd = VOLCMD_NONE;
  614. break;
  615. case VOLCMD_FINEVOLDOWN:
  616. command = CMD_VOLUMESLIDE;
  617. param = 0xF0 | vol;
  618. volcmd = VOLCMD_NONE;
  619. break;
  620. case VOLCMD_FINEVOLUP:
  621. command = CMD_VOLUMESLIDE;
  622. param = (vol << 4) | 0x0F;
  623. volcmd = VOLCMD_NONE;
  624. break;
  625. case VOLCMD_PORTADOWN:
  626. command = CMD_PORTAMENTODOWN;
  627. param = vol << 2;
  628. volcmd = VOLCMD_NONE;
  629. break;
  630. case VOLCMD_PORTAUP:
  631. command = CMD_PORTAMENTOUP;
  632. param = vol << 2;
  633. volcmd = VOLCMD_NONE;
  634. break;
  635. case VOLCMD_TONEPORTAMENTO:
  636. command = CMD_TONEPORTAMENTO;
  637. param = vol << 2;
  638. volcmd = VOLCMD_NONE;
  639. break;
  640. case VOLCMD_VIBRATODEPTH:
  641. command = CMD_VIBRATO;
  642. param = vol;
  643. volcmd = VOLCMD_NONE;
  644. break;
  645. case VOLCMD_VIBRATOSPEED:
  646. command = CMD_VIBRATO;
  647. param = vol << 4;
  648. volcmd = VOLCMD_NONE;
  649. break;
  650. case VOLCMD_PANSLIDELEFT:
  651. command = CMD_PANNINGSLIDE;
  652. param = vol << 4;
  653. volcmd = VOLCMD_NONE;
  654. break;
  655. case VOLCMD_PANSLIDERIGHT:
  656. command = CMD_PANNINGSLIDE;
  657. param = vol;
  658. volcmd = VOLCMD_NONE;
  659. break;
  660. }
  661. } // End if(newTypeIsS3M)
  662. ////////////////////////////////////////////////////////////////////////
  663. // Convert anything to XM - adjust volume column, breaking EDx command
  664. if(newTypeIsXM)
  665. {
  666. // remove EDx if no note is next to it, or it will retrigger the note in FT2 mode
  667. if(command == CMD_MODCMDEX && (param & 0xF0) == 0xD0 && note == NOTE_NONE)
  668. {
  669. command = CMD_NONE;
  670. param = 0;
  671. }
  672. if(IsSpecialNote())
  673. {
  674. // Instrument numbers next to Note Off reset instrument settings
  675. instr = 0;
  676. if(command == CMD_MODCMDEX && (param & 0xF0) == 0xD0)
  677. {
  678. // Note Off + Note Delay does nothing when using envelopes.
  679. note = NOTE_NONE;
  680. command = CMD_KEYOFF;
  681. param &= 0x0F;
  682. }
  683. }
  684. // Convert some commands which behave differently or don't exist
  685. if(command == CMD_NONE) switch(volcmd)
  686. {
  687. case VOLCMD_PORTADOWN:
  688. command = CMD_PORTAMENTODOWN;
  689. param = vol << 2;
  690. volcmd = VOLCMD_NONE;
  691. break;
  692. case VOLCMD_PORTAUP:
  693. command = CMD_PORTAMENTOUP;
  694. param = vol << 2;
  695. volcmd = VOLCMD_NONE;
  696. break;
  697. case VOLCMD_TONEPORTAMENTO:
  698. command = CMD_TONEPORTAMENTO;
  699. param = ImpulseTrackerPortaVolCmd[vol & 0x0F];
  700. volcmd = VOLCMD_NONE;
  701. break;
  702. }
  703. } // End if(newTypeIsXM)
  704. ///////////////////////////////////////////////////
  705. // Convert anything to IT - adjust volume column
  706. if(newTypeIsIT_MPT)
  707. {
  708. // Convert some commands which behave differently or don't exist
  709. if(!oldTypeIsIT_MPT && command == CMD_NONE) switch(volcmd)
  710. {
  711. case VOLCMD_PANSLIDELEFT:
  712. command = CMD_PANNINGSLIDE;
  713. param = vol << 4;
  714. volcmd = VOLCMD_NONE;
  715. break;
  716. case VOLCMD_PANSLIDERIGHT:
  717. command = CMD_PANNINGSLIDE;
  718. param = vol;
  719. volcmd = VOLCMD_NONE;
  720. break;
  721. case VOLCMD_VIBRATOSPEED:
  722. command = CMD_VIBRATO;
  723. param = vol << 4;
  724. volcmd = VOLCMD_NONE;
  725. break;
  726. case VOLCMD_TONEPORTAMENTO:
  727. command = CMD_TONEPORTAMENTO;
  728. param = vol << 4;
  729. volcmd = VOLCMD_NONE;
  730. break;
  731. }
  732. switch(volcmd)
  733. {
  734. case VOLCMD_VOLSLIDEDOWN:
  735. case VOLCMD_VOLSLIDEUP:
  736. case VOLCMD_FINEVOLDOWN:
  737. case VOLCMD_FINEVOLUP:
  738. case VOLCMD_PORTADOWN:
  739. case VOLCMD_PORTAUP:
  740. case VOLCMD_TONEPORTAMENTO:
  741. case VOLCMD_VIBRATODEPTH:
  742. // OpenMPT-specific commands
  743. case VOLCMD_OFFSET:
  744. vol = std::min(vol, VOL(9));
  745. break;
  746. }
  747. } // End if(newTypeIsIT_MPT)
  748. // Fix volume column offset for formats that don't have it.
  749. if(volcmd == VOLCMD_OFFSET && !newSpecs.HasVolCommand(VOLCMD_OFFSET) && (command == CMD_NONE || command == CMD_OFFSET || !newSpecs.HasCommand(command)))
  750. {
  751. const ModCommand::PARAM oldOffset = (command == CMD_OFFSET) ? param : 0;
  752. command = CMD_OFFSET;
  753. volcmd = VOLCMD_NONE;
  754. SAMPLEINDEX smp = instr;
  755. if(smp > 0 && smp <= sndFile.GetNumInstruments() && IsNote() && sndFile.Instruments[smp] != nullptr)
  756. smp = sndFile.Instruments[smp]->Keyboard[note - NOTE_MIN];
  757. if(smp > 0 && smp <= sndFile.GetNumSamples() && vol <= std::size(ModSample().cues))
  758. {
  759. const ModSample &sample = sndFile.GetSample(smp);
  760. if(vol == 0)
  761. param = mpt::saturate_cast<ModCommand::PARAM>(Util::muldivr_unsigned(sample.nLength, oldOffset, 65536u));
  762. else
  763. param = mpt::saturate_cast<ModCommand::PARAM>((sample.cues[vol - 1] + (oldOffset * 256u) + 128u) / 256u);
  764. } else
  765. {
  766. param = vol << 3;
  767. }
  768. }
  769. if((command == CMD_REVERSEOFFSET || command == CMD_OFFSETPERCENTAGE) && !newSpecs.HasCommand(command))
  770. {
  771. command = CMD_OFFSET;
  772. }
  773. if(!newSpecs.HasNote(note))
  774. note = NOTE_NONE;
  775. // ensure the commands really exist in this format
  776. if(!newSpecs.HasCommand(command))
  777. command = CMD_NONE;
  778. if(!newSpecs.HasVolCommand(volcmd))
  779. volcmd = VOLCMD_NONE;
  780. }
  781. bool ModCommand::IsContinousCommand(const CSoundFile &sndFile) const
  782. {
  783. switch(command)
  784. {
  785. case CMD_ARPEGGIO:
  786. case CMD_TONEPORTAMENTO:
  787. case CMD_VIBRATO:
  788. case CMD_TREMOLO:
  789. case CMD_RETRIG:
  790. case CMD_TREMOR:
  791. case CMD_FINEVIBRATO:
  792. case CMD_PANBRELLO:
  793. case CMD_SMOOTHMIDI:
  794. case CMD_NOTESLIDEUP:
  795. case CMD_NOTESLIDEDOWN:
  796. case CMD_NOTESLIDEUPRETRIG:
  797. case CMD_NOTESLIDEDOWNRETRIG:
  798. return true;
  799. case CMD_PORTAMENTOUP:
  800. case CMD_PORTAMENTODOWN:
  801. if(!param && sndFile.GetType() == MOD_TYPE_MOD)
  802. return false;
  803. if(sndFile.GetType() & (MOD_TYPE_MOD | MOD_TYPE_XM | MOD_TYPE_MT2 | MOD_TYPE_MED | MOD_TYPE_AMF0 | MOD_TYPE_DIGI | MOD_TYPE_STP | MOD_TYPE_DTM))
  804. return true;
  805. if(param >= 0xF0)
  806. return false;
  807. if(param >= 0xE0 && sndFile.GetType() != MOD_TYPE_DBM)
  808. return false;
  809. return true;
  810. case CMD_VOLUMESLIDE:
  811. case CMD_TONEPORTAVOL:
  812. case CMD_VIBRATOVOL:
  813. case CMD_GLOBALVOLSLIDE:
  814. case CMD_CHANNELVOLSLIDE:
  815. case CMD_PANNINGSLIDE:
  816. if(!param && sndFile.GetType() == MOD_TYPE_MOD)
  817. return false;
  818. if(sndFile.GetType() & (MOD_TYPE_MOD | MOD_TYPE_XM | MOD_TYPE_AMF0 | MOD_TYPE_MED | MOD_TYPE_DIGI))
  819. return true;
  820. if((param & 0xF0) == 0xF0 && (param & 0x0F))
  821. return false;
  822. if((param & 0x0F) == 0x0F && (param & 0xF0))
  823. return false;
  824. return true;
  825. case CMD_TEMPO:
  826. return (param < 0x20);
  827. default:
  828. return false;
  829. }
  830. }
  831. bool ModCommand::IsContinousVolColCommand() const
  832. {
  833. switch(volcmd)
  834. {
  835. case VOLCMD_VOLSLIDEUP:
  836. case VOLCMD_VOLSLIDEDOWN:
  837. case VOLCMD_VIBRATOSPEED:
  838. case VOLCMD_VIBRATODEPTH:
  839. case VOLCMD_PANSLIDELEFT:
  840. case VOLCMD_PANSLIDERIGHT:
  841. case VOLCMD_TONEPORTAMENTO:
  842. case VOLCMD_PORTAUP:
  843. case VOLCMD_PORTADOWN:
  844. return true;
  845. default:
  846. return false;
  847. }
  848. }
  849. bool ModCommand::IsSlideUpDownCommand() const
  850. {
  851. switch(command)
  852. {
  853. case CMD_VOLUMESLIDE:
  854. case CMD_TONEPORTAVOL:
  855. case CMD_VIBRATOVOL:
  856. case CMD_GLOBALVOLSLIDE:
  857. case CMD_CHANNELVOLSLIDE:
  858. case CMD_PANNINGSLIDE:
  859. return true;
  860. default:
  861. return false;
  862. }
  863. }
  864. bool ModCommand::IsGlobalCommand(COMMAND command, PARAM param)
  865. {
  866. switch(command)
  867. {
  868. case CMD_POSITIONJUMP:
  869. case CMD_PATTERNBREAK:
  870. case CMD_SPEED:
  871. case CMD_TEMPO:
  872. case CMD_GLOBALVOLUME:
  873. case CMD_GLOBALVOLSLIDE:
  874. case CMD_MIDI:
  875. case CMD_SMOOTHMIDI:
  876. case CMD_DBMECHO:
  877. return true;
  878. case CMD_MODCMDEX:
  879. switch(param & 0xF0)
  880. {
  881. case 0x00: // LED Filter
  882. case 0x60: // Pattern Loop
  883. case 0xE0: // Row Delay
  884. return true;
  885. default:
  886. return false;
  887. }
  888. case CMD_XFINEPORTAUPDOWN:
  889. case CMD_S3MCMDEX:
  890. switch(param & 0xF0)
  891. {
  892. case 0x60: // Tick Delay
  893. case 0x90: // Sound Control
  894. case 0xB0: // Pattern Loop
  895. case 0xE0: // Row Delay
  896. return true;
  897. default:
  898. return false;
  899. }
  900. default:
  901. return false;
  902. }
  903. }
  904. // "Importance" of every FX command. Table is used for importing from formats with multiple effect colums
  905. // and is approximately the same as in SchismTracker.
  906. size_t ModCommand::GetEffectWeight(COMMAND cmd)
  907. {
  908. // Effect weights, sorted from lowest to highest weight.
  909. static constexpr COMMAND weights[] =
  910. {
  911. CMD_NONE,
  912. CMD_DUMMY,
  913. CMD_XPARAM,
  914. CMD_SETENVPOSITION,
  915. CMD_KEYOFF,
  916. CMD_TREMOLO,
  917. CMD_FINEVIBRATO,
  918. CMD_VIBRATO,
  919. CMD_XFINEPORTAUPDOWN,
  920. CMD_FINETUNE,
  921. CMD_FINETUNE_SMOOTH,
  922. CMD_PANBRELLO,
  923. CMD_S3MCMDEX,
  924. CMD_MODCMDEX,
  925. CMD_DELAYCUT,
  926. CMD_MIDI,
  927. CMD_SMOOTHMIDI,
  928. CMD_PANNINGSLIDE,
  929. CMD_PANNING8,
  930. CMD_NOTESLIDEUPRETRIG,
  931. CMD_NOTESLIDEUP,
  932. CMD_NOTESLIDEDOWNRETRIG,
  933. CMD_NOTESLIDEDOWN,
  934. CMD_PORTAMENTOUP,
  935. CMD_PORTAMENTODOWN,
  936. CMD_VOLUMESLIDE,
  937. CMD_VIBRATOVOL,
  938. CMD_VOLUME,
  939. CMD_DIGIREVERSESAMPLE,
  940. CMD_REVERSEOFFSET,
  941. CMD_OFFSETPERCENTAGE,
  942. CMD_OFFSET,
  943. CMD_TREMOR,
  944. CMD_RETRIG,
  945. CMD_ARPEGGIO,
  946. CMD_TONEPORTAMENTO,
  947. CMD_TONEPORTAVOL,
  948. CMD_DBMECHO,
  949. CMD_GLOBALVOLSLIDE,
  950. CMD_CHANNELVOLUME,
  951. CMD_GLOBALVOLSLIDE,
  952. CMD_GLOBALVOLUME,
  953. CMD_TEMPO,
  954. CMD_SPEED,
  955. CMD_POSITIONJUMP,
  956. CMD_PATTERNBREAK,
  957. };
  958. static_assert(std::size(weights) == MAX_EFFECTS);
  959. for(size_t i = 0; i < std::size(weights); i++)
  960. {
  961. if(weights[i] == cmd)
  962. {
  963. return i;
  964. }
  965. }
  966. // Invalid / unknown command.
  967. return 0;
  968. }
  969. // Try to convert a fx column command (&effect) into a volume column command.
  970. // Returns true if successful.
  971. // Some commands can only be converted by losing some precision.
  972. // If moving the command into the volume column is more important than accuracy, use force = true.
  973. // (Code translated from SchismTracker and mainly supposed to be used with loaders ported from this tracker)
  974. bool ModCommand::ConvertVolEffect(uint8 &effect, uint8 &param, bool force)
  975. {
  976. switch(effect)
  977. {
  978. case CMD_NONE:
  979. effect = VOLCMD_NONE;
  980. return true;
  981. case CMD_VOLUME:
  982. effect = VOLCMD_VOLUME;
  983. param = std::min(param, PARAM(64));
  984. break;
  985. case CMD_PORTAMENTOUP:
  986. // if not force, reject when dividing causes loss of data in LSB, or if the final value is too
  987. // large to fit. (volume column Ex/Fx are four times stronger than effect column)
  988. if(!force && ((param & 3) || param >= 0xE0))
  989. return false;
  990. param /= 4;
  991. effect = VOLCMD_PORTAUP;
  992. break;
  993. case CMD_PORTAMENTODOWN:
  994. if(!force && ((param & 3) || param >= 0xE0))
  995. return false;
  996. param /= 4;
  997. effect = VOLCMD_PORTADOWN;
  998. break;
  999. case CMD_TONEPORTAMENTO:
  1000. if(param >= 0xF0)
  1001. {
  1002. // hack for people who can't type F twice :)
  1003. effect = VOLCMD_TONEPORTAMENTO;
  1004. param = 9;
  1005. return true;
  1006. }
  1007. for(uint8 n = 0; n < 10; n++)
  1008. {
  1009. if(force
  1010. ? (param <= ImpulseTrackerPortaVolCmd[n])
  1011. : (param == ImpulseTrackerPortaVolCmd[n]))
  1012. {
  1013. effect = VOLCMD_TONEPORTAMENTO;
  1014. param = n;
  1015. return true;
  1016. }
  1017. }
  1018. return false;
  1019. case CMD_VIBRATO:
  1020. if(force)
  1021. param = std::min(static_cast<PARAM>(param & 0x0F), PARAM(9));
  1022. else if((param & 0x0F) > 9 || (param & 0xF0) != 0)
  1023. return false;
  1024. param &= 0x0F;
  1025. effect = VOLCMD_VIBRATODEPTH;
  1026. break;
  1027. case CMD_FINEVIBRATO:
  1028. if(force)
  1029. param = 0;
  1030. else if(param)
  1031. return false;
  1032. effect = VOLCMD_VIBRATODEPTH;
  1033. break;
  1034. case CMD_PANNING8:
  1035. if(param == 255)
  1036. param = 64;
  1037. else
  1038. param /= 4;
  1039. effect = VOLCMD_PANNING;
  1040. break;
  1041. case CMD_VOLUMESLIDE:
  1042. if(param == 0)
  1043. return false;
  1044. if((param & 0xF) == 0) // Dx0 / Cx
  1045. {
  1046. param >>= 4;
  1047. effect = VOLCMD_VOLSLIDEUP;
  1048. } else if((param & 0xF0) == 0) // D0x / Dx
  1049. {
  1050. effect = VOLCMD_VOLSLIDEDOWN;
  1051. } else if((param & 0xF) == 0xF) // DxF / Ax
  1052. {
  1053. param >>= 4;
  1054. effect = VOLCMD_FINEVOLUP;
  1055. } else if((param & 0xF0) == 0xF0) // DFx / Bx
  1056. {
  1057. param &= 0xF;
  1058. effect = VOLCMD_FINEVOLDOWN;
  1059. } else // ???
  1060. {
  1061. return false;
  1062. }
  1063. break;
  1064. case CMD_S3MCMDEX:
  1065. switch (param >> 4)
  1066. {
  1067. case 8:
  1068. effect = VOLCMD_PANNING;
  1069. param = ((param & 0xF) << 2) + 2;
  1070. return true;
  1071. case 0: case 1: case 2: case 0xF:
  1072. if(force)
  1073. {
  1074. effect = param = 0;
  1075. return true;
  1076. }
  1077. break;
  1078. default:
  1079. break;
  1080. }
  1081. return false;
  1082. default:
  1083. return false;
  1084. }
  1085. return true;
  1086. }
  1087. // Try to combine two commands into one. Returns true on success and the combined command is placed in eff1 / param1.
  1088. bool ModCommand::CombineEffects(uint8 &eff1, uint8 &param1, uint8 &eff2, uint8 &param2)
  1089. {
  1090. if(eff1 == CMD_VOLUMESLIDE && (eff2 == CMD_VIBRATO || eff2 == CMD_TONEPORTAVOL) && param2 == 0)
  1091. {
  1092. // Merge commands
  1093. if(eff2 == CMD_VIBRATO)
  1094. {
  1095. eff1 = CMD_VIBRATOVOL;
  1096. } else
  1097. {
  1098. eff1 = CMD_TONEPORTAVOL;
  1099. }
  1100. eff2 = CMD_NONE;
  1101. return true;
  1102. } else if(eff2 == CMD_VOLUMESLIDE && (eff1 == CMD_VIBRATO || eff1 == CMD_TONEPORTAVOL) && param1 == 0)
  1103. {
  1104. // Merge commands
  1105. if(eff1 == CMD_VIBRATO)
  1106. {
  1107. eff1 = CMD_VIBRATOVOL;
  1108. } else
  1109. {
  1110. eff1 = CMD_TONEPORTAVOL;
  1111. }
  1112. param1 = param2;
  1113. eff2 = CMD_NONE;
  1114. return true;
  1115. } else if(eff1 == CMD_OFFSET && eff2 == CMD_S3MCMDEX && param2 == 0x9F)
  1116. {
  1117. // Reverse offset
  1118. eff1 = CMD_REVERSEOFFSET;
  1119. eff2 = CMD_NONE;
  1120. return true;
  1121. } else if(eff1 == CMD_S3MCMDEX && param1 == 0x9F && eff2 == CMD_OFFSET)
  1122. {
  1123. // Reverse offset
  1124. eff1 = CMD_REVERSEOFFSET;
  1125. param1 = param2;
  1126. eff2 = CMD_NONE;
  1127. return true;
  1128. } else
  1129. {
  1130. return false;
  1131. }
  1132. }
  1133. std::pair<EffectCommand, ModCommand::PARAM> ModCommand::TwoRegularCommandsToMPT(uint8 &effect1, uint8 &param1, uint8 &effect2, uint8 &param2)
  1134. {
  1135. for(uint8 n = 0; n < 4; n++)
  1136. {
  1137. if(ModCommand::ConvertVolEffect(effect1, param1, (n > 1)))
  1138. {
  1139. return {CMD_NONE, ModCommand::PARAM(0)};
  1140. }
  1141. std::swap(effect1, effect2);
  1142. std::swap(param1, param2);
  1143. }
  1144. // Can only keep one command :(
  1145. if(GetEffectWeight(static_cast<COMMAND>(effect1)) > GetEffectWeight(static_cast<COMMAND>(effect2)))
  1146. {
  1147. std::swap(effect1, effect2);
  1148. std::swap(param1, param2);
  1149. }
  1150. std::pair<EffectCommand, PARAM> lostCommand = {static_cast<EffectCommand>(effect1), param1};
  1151. effect1 = VOLCMD_NONE;
  1152. param1 = 0;
  1153. return lostCommand;
  1154. }
  1155. OPENMPT_NAMESPACE_END