Load_669.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Load_669.cpp
  3. * ------------
  4. * Purpose: 669 Composer / UNIS 669 module loader
  5. * Notes : <opinion humble="false">This is better than Schism's 669 loader</opinion> :)
  6. * (some of this code is "heavily inspired" by Storlek's code from Schism Tracker, and improvements have been made where necessary.)
  7. * Authors: Olivier Lapicque
  8. * OpenMPT Devs
  9. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  10. */
  11. #include "stdafx.h"
  12. #include "Loaders.h"
  13. OPENMPT_NAMESPACE_BEGIN
  14. struct _669FileHeader
  15. {
  16. char magic[2]; // 'if' (0x6669, ha ha) or 'JN'
  17. char songMessage[108]; // Song Message
  18. uint8 samples; // number of samples (1-64)
  19. uint8 patterns; // number of patterns (1-128)
  20. uint8 restartPos;
  21. uint8 orders[128];
  22. uint8 tempoList[128];
  23. uint8 breaks[128];
  24. };
  25. MPT_BINARY_STRUCT(_669FileHeader, 497)
  26. struct _669Sample
  27. {
  28. char filename[13];
  29. uint32le length;
  30. uint32le loopStart;
  31. uint32le loopEnd;
  32. // Convert a 669 sample header to OpenMPT's internal sample header.
  33. void ConvertToMPT(ModSample &mptSmp) const
  34. {
  35. mptSmp.Initialize();
  36. mptSmp.nC5Speed = 8363;
  37. mptSmp.nLength = length;
  38. mptSmp.nLoopStart = loopStart;
  39. mptSmp.nLoopEnd = loopEnd;
  40. if(mptSmp.nLoopEnd > mptSmp.nLength && mptSmp.nLoopStart == 0)
  41. {
  42. mptSmp.nLoopEnd = 0;
  43. }
  44. if(mptSmp.nLoopEnd != 0)
  45. {
  46. mptSmp.uFlags = CHN_LOOP;
  47. mptSmp.SanitizeLoops();
  48. }
  49. }
  50. };
  51. MPT_BINARY_STRUCT(_669Sample, 25)
  52. static bool ValidateHeader(const _669FileHeader &fileHeader)
  53. {
  54. if((std::memcmp(fileHeader.magic, "if", 2) && std::memcmp(fileHeader.magic, "JN", 2))
  55. || fileHeader.samples > 64
  56. || fileHeader.restartPos >= 128
  57. || fileHeader.patterns > 128)
  58. {
  59. return false;
  60. }
  61. for(std::size_t i = 0; i < std::size(fileHeader.breaks); i++)
  62. {
  63. if(fileHeader.orders[i] >= 128 && fileHeader.orders[i] < 0xFE)
  64. return false;
  65. if(fileHeader.orders[i] < 128 && fileHeader.tempoList[i] == 0)
  66. return false;
  67. if(fileHeader.tempoList[i] > 15)
  68. return false;
  69. if(fileHeader.breaks[i] >= 64)
  70. return false;
  71. }
  72. return true;
  73. }
  74. static uint64 GetHeaderMinimumAdditionalSize(const _669FileHeader &fileHeader)
  75. {
  76. return fileHeader.samples * sizeof(_669Sample) + fileHeader.patterns * 1536u;
  77. }
  78. CSoundFile::ProbeResult CSoundFile::ProbeFileHeader669(MemoryFileReader file, const uint64 *pfilesize)
  79. {
  80. _669FileHeader fileHeader;
  81. if(!file.ReadStruct(fileHeader))
  82. {
  83. return ProbeWantMoreData;
  84. }
  85. if(!ValidateHeader(fileHeader))
  86. {
  87. return ProbeFailure;
  88. }
  89. return ProbeAdditionalSize(file, pfilesize, GetHeaderMinimumAdditionalSize(fileHeader));
  90. }
  91. bool CSoundFile::Read669(FileReader &file, ModLoadingFlags loadFlags)
  92. {
  93. _669FileHeader fileHeader;
  94. file.Rewind();
  95. if(!file.ReadStruct(fileHeader))
  96. {
  97. return false;
  98. }
  99. if(!ValidateHeader(fileHeader))
  100. {
  101. return false;
  102. }
  103. if(loadFlags == onlyVerifyHeader)
  104. {
  105. return true;
  106. }
  107. if(!file.CanRead(mpt::saturate_cast<FileReader::off_t>(GetHeaderMinimumAdditionalSize(fileHeader))))
  108. {
  109. return false;
  110. }
  111. InitializeGlobals(MOD_TYPE_669);
  112. m_nMinPeriod = 28 << 2;
  113. m_nMaxPeriod = 1712 << 3;
  114. m_nDefaultTempo.Set(78);
  115. m_nDefaultSpeed = 4;
  116. m_nChannels = 8;
  117. m_playBehaviour.set(kPeriodsAreHertz);
  118. #ifdef MODPLUG_TRACKER
  119. // 669 uses frequencies rather than periods, so linear slides mode will sound better in the higher octaves.
  120. //m_SongFlags.set(SONG_LINEARSLIDES);
  121. #endif // MODPLUG_TRACKER
  122. m_modFormat.formatName = U_("Composer 669");
  123. m_modFormat.type = U_("669");
  124. m_modFormat.madeWithTracker = !memcmp(fileHeader.magic, "if", 2) ? UL_("Composer 669") : UL_("UNIS 669");
  125. m_modFormat.charset = mpt::Charset::CP437;
  126. m_nSamples = fileHeader.samples;
  127. for(SAMPLEINDEX smp = 1; smp <= m_nSamples; smp++)
  128. {
  129. _669Sample sampleHeader;
  130. file.ReadStruct(sampleHeader);
  131. // Since 669 files have very unfortunate magic bytes ("if") and can
  132. // hardly be validated, reject any file with far too big samples.
  133. if(sampleHeader.length >= 0x4000000)
  134. return false;
  135. sampleHeader.ConvertToMPT(Samples[smp]);
  136. m_szNames[smp] = mpt::String::ReadBuf(mpt::String::maybeNullTerminated, sampleHeader.filename);
  137. }
  138. // Copy first song message line into song title
  139. m_songName = mpt::String::ReadBuf(mpt::String::spacePadded, fileHeader.songMessage, 36);
  140. // Song Message
  141. m_songMessage.ReadFixedLineLength(mpt::byte_cast<const std::byte*>(fileHeader.songMessage), 108, 36, 0);
  142. // Reading Orders
  143. ReadOrderFromArray(Order(), fileHeader.orders, std::size(fileHeader.orders), 0xFF, 0xFE);
  144. if(Order()[fileHeader.restartPos] < fileHeader.patterns)
  145. Order().SetRestartPos(fileHeader.restartPos);
  146. // Set up panning
  147. for(CHANNELINDEX chn = 0; chn < 8; chn++)
  148. {
  149. ChnSettings[chn].Reset();
  150. ChnSettings[chn].nPan = (chn & 1) ? 0xD0 : 0x30;
  151. }
  152. // Reading Patterns
  153. Patterns.ResizeArray(fileHeader.patterns);
  154. for(PATTERNINDEX pat = 0; pat < fileHeader.patterns; pat++)
  155. {
  156. if(!(loadFlags & loadPatternData) || !Patterns.Insert(pat, 64))
  157. {
  158. file.Skip(64 * 8 * 3);
  159. continue;
  160. }
  161. static constexpr ModCommand::COMMAND effTrans[] =
  162. {
  163. CMD_PORTAMENTOUP, // Slide up (param * 80) Hz on every tick
  164. CMD_PORTAMENTODOWN, // Slide down (param * 80) Hz on every tick
  165. CMD_TONEPORTAMENTO, // Slide to note by (param * 40) Hz on every tick
  166. CMD_S3MCMDEX, // Add (param * 80) Hz to sample frequency
  167. CMD_VIBRATO, // Add (param * 669) Hz on every other tick
  168. CMD_SPEED, // Set ticks per row
  169. CMD_PANNINGSLIDE, // Extended UNIS 669 effect
  170. CMD_RETRIG, // Extended UNIS 669 effect
  171. };
  172. uint8 effect[8] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  173. for(ROWINDEX row = 0; row < 64; row++)
  174. {
  175. PatternRow m = Patterns[pat].GetRow(row);
  176. for(CHANNELINDEX chn = 0; chn < 8; chn++, m++)
  177. {
  178. const auto [noteInstr, instrVol, effParam] = file.ReadArray<uint8, 3>();
  179. uint8 note = noteInstr >> 2;
  180. uint8 instr = ((noteInstr & 0x03) << 4) | (instrVol >> 4);
  181. uint8 vol = instrVol & 0x0F;
  182. if(noteInstr < 0xFE)
  183. {
  184. m->note = note + 36 + NOTE_MIN;
  185. m->instr = instr + 1;
  186. effect[chn] = 0xFF;
  187. }
  188. if(noteInstr <= 0xFE)
  189. {
  190. m->volcmd = VOLCMD_VOLUME;
  191. m->vol = ((vol * 64 + 8) / 15);
  192. }
  193. if(effParam != 0xFF)
  194. {
  195. effect[chn] = effParam;
  196. }
  197. if((effParam & 0x0F) == 0 && effParam != 0x30)
  198. {
  199. // A param value of 0 resets the effect.
  200. effect[chn] = 0xFF;
  201. }
  202. if(effect[chn] == 0xFF)
  203. {
  204. continue;
  205. }
  206. m->param = effect[chn] & 0x0F;
  207. // Weird stuff happening in corehop.669 with effects > 8... they seem to do the same thing as if the high bit wasn't set, but the sample also behaves strangely.
  208. uint8 command = effect[chn] >> 4;
  209. if(command < static_cast<uint8>(std::size(effTrans)))
  210. {
  211. m->command = effTrans[command];
  212. } else
  213. {
  214. m->command = CMD_NONE;
  215. continue;
  216. }
  217. // Fix some commands
  218. switch(command)
  219. {
  220. case 3:
  221. // D - frequency adjust
  222. #ifdef MODPLUG_TRACKER
  223. // Since we convert to S3M, the finetune command will not quite do what we intend to do (it can adjust the frequency upwards and downwards), so try to approximate it using a fine slide.
  224. m->command = CMD_PORTAMENTOUP;
  225. m->param |= 0xF0;
  226. #else
  227. m->param |= 0x20;
  228. #endif
  229. effect[chn] = 0xFF;
  230. break;
  231. case 4:
  232. // E - frequency vibrato - almost like an arpeggio, but does not arpeggiate by a given note but by a frequency amount.
  233. #ifdef MODPLUG_TRACKER
  234. m->command = CMD_ARPEGGIO;
  235. #endif
  236. m->param |= (m->param << 4);
  237. break;
  238. case 5:
  239. // F - set tempo
  240. // TODO: param 0 is a "super fast tempo" in Unis 669 mode (?)
  241. effect[chn] = 0xFF;
  242. break;
  243. case 6:
  244. // G - subcommands (extended)
  245. switch(m->param)
  246. {
  247. case 0:
  248. // balance fine slide left
  249. m->param = 0x4F;
  250. break;
  251. case 1:
  252. // balance fine slide right
  253. m->param = 0xF4;
  254. break;
  255. default:
  256. m->command = CMD_NONE;
  257. }
  258. break;
  259. }
  260. }
  261. }
  262. // Write pattern break
  263. if(fileHeader.breaks[pat] < 63)
  264. {
  265. Patterns[pat].WriteEffect(EffectWriter(CMD_PATTERNBREAK, 0).Row(fileHeader.breaks[pat]).RetryNextRow());
  266. }
  267. // And of course the speed...
  268. Patterns[pat].WriteEffect(EffectWriter(CMD_SPEED, fileHeader.tempoList[pat]).RetryNextRow());
  269. }
  270. if(loadFlags & loadSampleData)
  271. {
  272. // Reading Samples
  273. const SampleIO sampleIO(
  274. SampleIO::_8bit,
  275. SampleIO::mono,
  276. SampleIO::littleEndian,
  277. SampleIO::unsignedPCM);
  278. for(SAMPLEINDEX n = 1; n <= m_nSamples; n++)
  279. {
  280. sampleIO.ReadSample(Samples[n], file);
  281. }
  282. }
  283. return true;
  284. }
  285. OPENMPT_NAMESPACE_END