Load_far.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Load_far.cpp
  3. * ------------
  4. * Purpose: Farandole (FAR) module loader
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs (partly inspired by Storlek's FAR loader from Schism Tracker)
  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. // FAR File Header
  13. struct FARFileHeader
  14. {
  15. uint8le magic[4];
  16. char songName[40];
  17. uint8le eof[3];
  18. uint16le headerLength;
  19. uint8le version;
  20. uint8le onOff[16];
  21. uint8le editingState[9]; // Stuff we don't care about
  22. uint8le defaultSpeed;
  23. uint8le chnPanning[16];
  24. uint8le patternState[4]; // More stuff we don't care about
  25. uint16le messageLength;
  26. };
  27. MPT_BINARY_STRUCT(FARFileHeader, 98)
  28. struct FAROrderHeader
  29. {
  30. uint8le orders[256];
  31. uint8le numPatterns; // supposed to be "number of patterns stored in the file"; apparently that's wrong
  32. uint8le numOrders;
  33. uint8le restartPos;
  34. uint16le patternSize[256];
  35. };
  36. MPT_BINARY_STRUCT(FAROrderHeader, 771)
  37. // FAR Sample header
  38. struct FARSampleHeader
  39. {
  40. // Sample flags
  41. enum SampleFlags
  42. {
  43. smp16Bit = 0x01,
  44. smpLoop = 0x08,
  45. };
  46. char name[32];
  47. uint32le length;
  48. uint8le finetune;
  49. uint8le volume;
  50. uint32le loopStart;
  51. uint32le loopEnd;
  52. uint8le type;
  53. uint8le loop;
  54. // Convert sample header to OpenMPT's internal format.
  55. void ConvertToMPT(ModSample &mptSmp) const
  56. {
  57. mptSmp.Initialize();
  58. mptSmp.nLength = length;
  59. mptSmp.nLoopStart = loopStart;
  60. mptSmp.nLoopEnd = loopEnd;
  61. mptSmp.nC5Speed = 8363 * 2;
  62. mptSmp.nVolume = volume * 16;
  63. if(type & smp16Bit)
  64. {
  65. mptSmp.nLength /= 2;
  66. mptSmp.nLoopStart /= 2;
  67. mptSmp.nLoopEnd /= 2;
  68. }
  69. if((loop & 8) && mptSmp.nLoopEnd > mptSmp.nLoopStart)
  70. {
  71. mptSmp.uFlags.set(CHN_LOOP);
  72. }
  73. }
  74. // Retrieve the internal sample format flags for this sample.
  75. SampleIO GetSampleFormat() const
  76. {
  77. return SampleIO(
  78. (type & smp16Bit) ? SampleIO::_16bit : SampleIO::_8bit,
  79. SampleIO::mono,
  80. SampleIO::littleEndian,
  81. SampleIO::signedPCM);
  82. }
  83. };
  84. MPT_BINARY_STRUCT(FARSampleHeader, 48)
  85. static bool ValidateHeader(const FARFileHeader &fileHeader)
  86. {
  87. if(std::memcmp(fileHeader.magic, "FAR\xFE", 4) != 0
  88. || std::memcmp(fileHeader.eof, "\x0D\x0A\x1A", 3)
  89. )
  90. {
  91. return false;
  92. }
  93. if(fileHeader.headerLength < sizeof(FARFileHeader))
  94. {
  95. return false;
  96. }
  97. return true;
  98. }
  99. static uint64 GetHeaderMinimumAdditionalSize(const FARFileHeader &fileHeader)
  100. {
  101. return fileHeader.headerLength - sizeof(FARFileHeader);
  102. }
  103. CSoundFile::ProbeResult CSoundFile::ProbeFileHeaderFAR(MemoryFileReader file, const uint64 *pfilesize)
  104. {
  105. FARFileHeader fileHeader;
  106. if(!file.ReadStruct(fileHeader))
  107. {
  108. return ProbeWantMoreData;
  109. }
  110. if(!ValidateHeader(fileHeader))
  111. {
  112. return ProbeFailure;
  113. }
  114. return ProbeAdditionalSize(file, pfilesize, GetHeaderMinimumAdditionalSize(fileHeader));
  115. }
  116. bool CSoundFile::ReadFAR(FileReader &file, ModLoadingFlags loadFlags)
  117. {
  118. file.Rewind();
  119. FARFileHeader fileHeader;
  120. if(!file.ReadStruct(fileHeader))
  121. {
  122. return false;
  123. }
  124. if(!ValidateHeader(fileHeader))
  125. {
  126. return false;
  127. }
  128. if(!file.CanRead(mpt::saturate_cast<FileReader::off_t>(GetHeaderMinimumAdditionalSize(fileHeader))))
  129. {
  130. return false;
  131. }
  132. if(loadFlags == onlyVerifyHeader)
  133. {
  134. return true;
  135. }
  136. // Globals
  137. InitializeGlobals(MOD_TYPE_FAR);
  138. m_nChannels = 16;
  139. m_nSamplePreAmp = 32;
  140. m_nDefaultSpeed = fileHeader.defaultSpeed;
  141. m_nDefaultTempo.Set(80);
  142. m_nDefaultGlobalVolume = MAX_GLOBAL_VOLUME;
  143. m_SongFlags = SONG_LINEARSLIDES;
  144. m_playBehaviour.set(kPeriodsAreHertz);
  145. m_modFormat.formatName = U_("Farandole Composer");
  146. m_modFormat.type = U_("far");
  147. m_modFormat.charset = mpt::Charset::CP437;
  148. m_songName = mpt::String::ReadBuf(mpt::String::maybeNullTerminated, fileHeader.songName);
  149. // Read channel settings
  150. for(CHANNELINDEX chn = 0; chn < 16; chn++)
  151. {
  152. ChnSettings[chn].Reset();
  153. ChnSettings[chn].dwFlags = fileHeader.onOff[chn] ? ChannelFlags(0) : CHN_MUTE;
  154. ChnSettings[chn].nPan = ((fileHeader.chnPanning[chn] & 0x0F) << 4) + 8;
  155. }
  156. // Read song message
  157. if(fileHeader.messageLength != 0)
  158. {
  159. m_songMessage.ReadFixedLineLength(file, fileHeader.messageLength, 132, 0); // 132 characters per line... wow. :)
  160. }
  161. // Read orders
  162. FAROrderHeader orderHeader;
  163. if(!file.ReadStruct(orderHeader))
  164. {
  165. return false;
  166. }
  167. ReadOrderFromArray(Order(), orderHeader.orders, orderHeader.numOrders, 0xFF, 0xFE);
  168. Order().SetRestartPos(orderHeader.restartPos);
  169. file.Seek(fileHeader.headerLength);
  170. // Pattern effect LUT
  171. static constexpr EffectCommand farEffects[] =
  172. {
  173. CMD_NONE,
  174. CMD_PORTAMENTOUP,
  175. CMD_PORTAMENTODOWN,
  176. CMD_TONEPORTAMENTO,
  177. CMD_RETRIG,
  178. CMD_VIBRATO, // depth
  179. CMD_VIBRATO, // speed
  180. CMD_VOLUMESLIDE, // up
  181. CMD_VOLUMESLIDE, // down
  182. CMD_VIBRATO, // sustained (?)
  183. CMD_NONE, // actually slide-to-volume
  184. CMD_S3MCMDEX, // panning
  185. CMD_S3MCMDEX, // note offset => note delay?
  186. CMD_NONE, // fine tempo down
  187. CMD_NONE, // fine tempo up
  188. CMD_SPEED,
  189. };
  190. // Read patterns
  191. for(PATTERNINDEX pat = 0; pat < 256; pat++)
  192. {
  193. if(!orderHeader.patternSize[pat])
  194. {
  195. continue;
  196. }
  197. FileReader patternChunk = file.ReadChunk(orderHeader.patternSize[pat]);
  198. // Calculate pattern length in rows (every event is 4 bytes, and we have 16 channels)
  199. ROWINDEX numRows = (orderHeader.patternSize[pat] - 2) / (16 * 4);
  200. if(!(loadFlags & loadPatternData) || !Patterns.Insert(pat, numRows))
  201. {
  202. continue;
  203. }
  204. // Read break row and unused value (used to be pattern tempo)
  205. ROWINDEX breakRow = patternChunk.ReadUint8();
  206. patternChunk.Skip(1);
  207. if(breakRow > 0 && breakRow < numRows - 2)
  208. {
  209. breakRow++;
  210. } else
  211. {
  212. breakRow = ROWINDEX_INVALID;
  213. }
  214. // Read pattern data
  215. for(ROWINDEX row = 0; row < numRows; row++)
  216. {
  217. PatternRow rowBase = Patterns[pat].GetRow(row);
  218. for(CHANNELINDEX chn = 0; chn < 16; chn++)
  219. {
  220. ModCommand &m = rowBase[chn];
  221. const auto [note, instr, volume, effect] = patternChunk.ReadArray<uint8, 4>();
  222. if(note > 0 && note <= 72)
  223. {
  224. m.note = note + 35 + NOTE_MIN;
  225. m.instr = instr + 1;
  226. }
  227. if(volume > 0 && volume <= 16)
  228. {
  229. m.volcmd = VOLCMD_VOLUME;
  230. m.vol = (volume - 1u) * 64u / 15u;
  231. }
  232. m.param = effect & 0x0F;
  233. switch(effect >> 4)
  234. {
  235. case 0x01:
  236. case 0x02:
  237. m.param |= 0xF0;
  238. break;
  239. case 0x03: // Porta to note (TODO: Parameter is number of rows the portamento should take)
  240. m.param <<= 2;
  241. break;
  242. case 0x04: // Retrig
  243. m.param = 6 / (1 + (m.param & 0xf)) + 1; // ugh?
  244. break;
  245. case 0x06: // Vibrato speed
  246. case 0x07: // Volume slide up
  247. m.param *= 8;
  248. break;
  249. case 0x0A: // Volume-portamento (what!)
  250. m.volcmd = VOLCMD_VOLUME;
  251. m.vol = (m.param << 2) + 4;
  252. break;
  253. case 0x0B: // Panning
  254. m.param |= 0x80;
  255. break;
  256. case 0x0C: // Note offset
  257. m.param = 6 / (1 + m.param) + 1;
  258. m.param |= 0x0D;
  259. }
  260. m.command = farEffects[effect >> 4];
  261. }
  262. }
  263. Patterns[pat].WriteEffect(EffectWriter(CMD_PATTERNBREAK, 0).Row(breakRow).RetryNextRow());
  264. }
  265. if(!(loadFlags & loadSampleData))
  266. {
  267. return true;
  268. }
  269. // Read samples
  270. uint8 sampleMap[8]; // Sample usage bitset
  271. file.ReadArray(sampleMap);
  272. for(SAMPLEINDEX smp = 0; smp < 64; smp++)
  273. {
  274. if(!(sampleMap[smp >> 3] & (1 << (smp & 7))))
  275. {
  276. continue;
  277. }
  278. FARSampleHeader sampleHeader;
  279. if(!file.ReadStruct(sampleHeader))
  280. {
  281. return true;
  282. }
  283. m_nSamples = smp + 1;
  284. ModSample &sample = Samples[m_nSamples];
  285. m_szNames[m_nSamples] = mpt::String::ReadBuf(mpt::String::nullTerminated, sampleHeader.name);
  286. sampleHeader.ConvertToMPT(sample);
  287. sampleHeader.GetSampleFormat().ReadSample(sample, file);
  288. }
  289. return true;
  290. }
  291. OPENMPT_NAMESPACE_END