Load_dsm.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Load_dsm.cpp
  3. * ------------
  4. * Purpose: Digisound Interface Kit (DSIK) Internal Format (DSM v2 / RIFF) module loader
  5. * Notes : 1. There is also another fundamentally different DSIK DSM v1 module format, not handled here.
  6. * MilkyTracker can load it, but the only files of this format seen in the wild are also
  7. * available in their original format, so I did not bother implementing it so far.
  8. *
  9. * 2. Using both PLAY.EXE v1.02 and v2.00, commands not supported in MOD do not seem to do
  10. * anything at all.
  11. * In particular commands 0x11-0x13 handled below are ignored, and no files have been spotted
  12. * in the wild using any commands > 0x0F at all.
  13. * S3M-style retrigger does not seem to exist - it is translated to volume slides by CONV.EXE,
  14. * and J00 in S3M files is not converted either. S3M pattern loops (SBx) are not converted
  15. * properly by CONV.EXE and completely ignored by PLAY.EXE.
  16. * Command 8 (set panning) uses 00-80 for regular panning and A4 for surround, probably
  17. * making DSIK one of the first applications to use this particular encoding scheme still
  18. * used in "extended" S3Ms today.
  19. * Authors: OpenMPT Devs
  20. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  21. */
  22. #include "stdafx.h"
  23. #include "Loaders.h"
  24. OPENMPT_NAMESPACE_BEGIN
  25. struct DSMChunk
  26. {
  27. char magic[4];
  28. uint32le size;
  29. };
  30. MPT_BINARY_STRUCT(DSMChunk, 8)
  31. struct DSMSongHeader
  32. {
  33. char songName[28];
  34. uint16le fileVersion;
  35. uint16le flags;
  36. uint16le orderPos;
  37. uint16le restartPos;
  38. uint16le numOrders;
  39. uint16le numSamples;
  40. uint16le numPatterns;
  41. uint16le numChannels;
  42. uint8le globalVol;
  43. uint8le mastervol;
  44. uint8le speed;
  45. uint8le bpm;
  46. uint8le panPos[16];
  47. uint8le orders[128];
  48. };
  49. MPT_BINARY_STRUCT(DSMSongHeader, 192)
  50. struct DSMSampleHeader
  51. {
  52. char filename[13];
  53. uint16le flags;
  54. uint8le volume;
  55. uint32le length;
  56. uint32le loopStart;
  57. uint32le loopEnd;
  58. uint32le dataPtr; // Interal sample pointer during playback in DSIK
  59. uint32le sampleRate;
  60. char sampleName[28];
  61. // Convert a DSM sample header to OpenMPT's internal sample header.
  62. void ConvertToMPT(ModSample &mptSmp) const
  63. {
  64. mptSmp.Initialize();
  65. mptSmp.filename = mpt::String::ReadBuf(mpt::String::nullTerminated, filename);
  66. mptSmp.nC5Speed = sampleRate;
  67. mptSmp.uFlags.set(CHN_LOOP, (flags & 1) != 0);
  68. mptSmp.nLength = length;
  69. mptSmp.nLoopStart = loopStart;
  70. mptSmp.nLoopEnd = loopEnd;
  71. mptSmp.nVolume = std::min(volume.get(), uint8(64)) * 4;
  72. }
  73. // Retrieve the internal sample format flags for this sample.
  74. SampleIO GetSampleFormat() const
  75. {
  76. SampleIO sampleIO(
  77. SampleIO::_8bit,
  78. SampleIO::mono,
  79. SampleIO::littleEndian,
  80. SampleIO::unsignedPCM);
  81. if(flags & 0x40)
  82. sampleIO |= SampleIO::deltaPCM; // fairlight.dsm by Comrade J
  83. else if(flags & 0x02)
  84. sampleIO |= SampleIO::signedPCM;
  85. if(flags & 0x04)
  86. sampleIO |= SampleIO::_16bit;
  87. return sampleIO;
  88. }
  89. };
  90. MPT_BINARY_STRUCT(DSMSampleHeader, 64)
  91. struct DSMHeader
  92. {
  93. char fileMagic0[4];
  94. char fileMagic1[4];
  95. char fileMagic2[4];
  96. };
  97. MPT_BINARY_STRUCT(DSMHeader, 12)
  98. static bool ValidateHeader(const DSMHeader &fileHeader)
  99. {
  100. if(!std::memcmp(fileHeader.fileMagic0, "RIFF", 4)
  101. && !std::memcmp(fileHeader.fileMagic2, "DSMF", 4))
  102. {
  103. // "Normal" DSM files with RIFF header
  104. // <RIFF> <file size> <DSMF>
  105. return true;
  106. } else if(!std::memcmp(fileHeader.fileMagic0, "DSMF", 4))
  107. {
  108. // DSM files with alternative header
  109. // <DSMF> <4 bytes, usually 4x NUL or RIFF> <file size> <4 bytes, usually DSMF but not always>
  110. return true;
  111. } else
  112. {
  113. return false;
  114. }
  115. }
  116. CSoundFile::ProbeResult CSoundFile::ProbeFileHeaderDSM(MemoryFileReader file, const uint64 *pfilesize)
  117. {
  118. DSMHeader fileHeader;
  119. if(!file.ReadStruct(fileHeader))
  120. {
  121. return ProbeWantMoreData;
  122. }
  123. if(!ValidateHeader(fileHeader))
  124. {
  125. return ProbeFailure;
  126. }
  127. if(std::memcmp(fileHeader.fileMagic0, "DSMF", 4) == 0)
  128. {
  129. if(!file.Skip(4))
  130. {
  131. return ProbeWantMoreData;
  132. }
  133. }
  134. DSMChunk chunkHeader;
  135. if(!file.ReadStruct(chunkHeader))
  136. {
  137. return ProbeWantMoreData;
  138. }
  139. if(std::memcmp(chunkHeader.magic, "SONG", 4))
  140. {
  141. return ProbeFailure;
  142. }
  143. MPT_UNREFERENCED_PARAMETER(pfilesize);
  144. return ProbeSuccess;
  145. }
  146. bool CSoundFile::ReadDSM(FileReader &file, ModLoadingFlags loadFlags)
  147. {
  148. file.Rewind();
  149. DSMHeader fileHeader;
  150. if(!file.ReadStruct(fileHeader))
  151. {
  152. return false;
  153. }
  154. if(!ValidateHeader(fileHeader))
  155. {
  156. return false;
  157. }
  158. if(std::memcmp(fileHeader.fileMagic0, "DSMF", 4) == 0)
  159. {
  160. file.Skip(4);
  161. }
  162. DSMChunk chunkHeader;
  163. if(!file.ReadStruct(chunkHeader))
  164. {
  165. return false;
  166. }
  167. // Technically, the song chunk could be anywhere in the file, but we're going to simplify
  168. // things by not using a chunk header here and just expect it to be right at the beginning.
  169. if(std::memcmp(chunkHeader.magic, "SONG", 4))
  170. {
  171. return false;
  172. }
  173. if(loadFlags == onlyVerifyHeader)
  174. {
  175. return true;
  176. }
  177. DSMSongHeader songHeader;
  178. file.ReadStructPartial(songHeader, chunkHeader.size);
  179. if(songHeader.numOrders > 128 || songHeader.numChannels > 16 || songHeader.numPatterns > 256 || songHeader.restartPos > 128)
  180. {
  181. return false;
  182. }
  183. InitializeGlobals(MOD_TYPE_DSM);
  184. m_modFormat.formatName = U_("DSIK Format");
  185. m_modFormat.type = U_("dsm");
  186. m_modFormat.charset = mpt::Charset::CP437;
  187. m_songName = mpt::String::ReadBuf(mpt::String::maybeNullTerminated, songHeader.songName);
  188. m_nChannels = std::max(songHeader.numChannels.get(), uint16(1));
  189. m_nDefaultSpeed = songHeader.speed;
  190. m_nDefaultTempo.Set(songHeader.bpm);
  191. m_nDefaultGlobalVolume = std::min(songHeader.globalVol.get(), uint8(64)) * 4u;
  192. if(!m_nDefaultGlobalVolume) m_nDefaultGlobalVolume = MAX_GLOBAL_VOLUME;
  193. if(songHeader.mastervol == 0x80)
  194. {
  195. m_nSamplePreAmp = std::min(256u / m_nChannels, 128u);
  196. } else
  197. {
  198. m_nSamplePreAmp = songHeader.mastervol & 0x7F;
  199. }
  200. // Read channel panning
  201. for(CHANNELINDEX chn = 0; chn < 16; chn++)
  202. {
  203. ChnSettings[chn].Reset();
  204. if(songHeader.panPos[chn] <= 0x80)
  205. {
  206. ChnSettings[chn].nPan = songHeader.panPos[chn] * 2;
  207. }
  208. }
  209. ReadOrderFromArray(Order(), songHeader.orders, songHeader.numOrders, 0xFF, 0xFE);
  210. if(songHeader.restartPos < songHeader.numOrders)
  211. Order().SetRestartPos(songHeader.restartPos);
  212. // Read pattern and sample chunks
  213. PATTERNINDEX patNum = 0;
  214. while(file.ReadStruct(chunkHeader))
  215. {
  216. FileReader chunk = file.ReadChunk(chunkHeader.size);
  217. if(!memcmp(chunkHeader.magic, "PATT", 4) && (loadFlags & loadPatternData))
  218. {
  219. // Read pattern
  220. if(!Patterns.Insert(patNum, 64))
  221. {
  222. continue;
  223. }
  224. chunk.Skip(2);
  225. ModCommand dummy = ModCommand::Empty();
  226. ROWINDEX row = 0;
  227. while(chunk.CanRead(1) && row < 64)
  228. {
  229. uint8 flag = chunk.ReadUint8();
  230. if(!flag)
  231. {
  232. row++;
  233. continue;
  234. }
  235. CHANNELINDEX chn = (flag & 0x0F);
  236. ModCommand &m = (chn < GetNumChannels() ? *Patterns[patNum].GetpModCommand(row, chn) : dummy);
  237. if(flag & 0x80)
  238. {
  239. uint8 note = chunk.ReadUint8();
  240. if(note)
  241. {
  242. if(note <= 12 * 9) note += 11 + NOTE_MIN;
  243. m.note = note;
  244. }
  245. }
  246. if(flag & 0x40)
  247. {
  248. m.instr = chunk.ReadUint8();
  249. }
  250. if (flag & 0x20)
  251. {
  252. m.volcmd = VOLCMD_VOLUME;
  253. m.vol = std::min(chunk.ReadUint8(), uint8(64));
  254. }
  255. if(flag & 0x10)
  256. {
  257. auto [command, param] = chunk.ReadArray<uint8, 2>();
  258. switch(command)
  259. {
  260. // Portamentos
  261. case 0x11:
  262. case 0x12:
  263. command &= 0x0F;
  264. break;
  265. // 3D Sound (?)
  266. case 0x13:
  267. command = 'X' - 55;
  268. param = 0x91;
  269. break;
  270. default:
  271. // Volume + Offset (?)
  272. if(command > 0x10)
  273. command = ((command & 0xF0) == 0x20) ? 0x09 : 0xFF;
  274. }
  275. m.command = command;
  276. m.param = param;
  277. ConvertModCommand(m);
  278. }
  279. }
  280. patNum++;
  281. } else if(!memcmp(chunkHeader.magic, "INST", 4) && CanAddMoreSamples())
  282. {
  283. // Read sample
  284. m_nSamples++;
  285. ModSample &sample = Samples[m_nSamples];
  286. DSMSampleHeader sampleHeader;
  287. chunk.ReadStruct(sampleHeader);
  288. sampleHeader.ConvertToMPT(sample);
  289. m_szNames[m_nSamples] = mpt::String::ReadBuf(mpt::String::maybeNullTerminated, sampleHeader.sampleName);
  290. if(loadFlags & loadSampleData)
  291. {
  292. sampleHeader.GetSampleFormat().ReadSample(sample, chunk);
  293. }
  294. }
  295. }
  296. return true;
  297. }
  298. OPENMPT_NAMESPACE_END