1
0

Load_xm.cpp 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. /*
  2. * Load_xm.cpp
  3. * -----------
  4. * Purpose: XM (FastTracker II) module loader / saver
  5. * Notes : (currently none)
  6. * Authors: Olivier Lapicque
  7. * OpenMPT Devs
  8. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  9. */
  10. #include "stdafx.h"
  11. #include "Loaders.h"
  12. #include "../common/version.h"
  13. #include "XMTools.h"
  14. #include "mod_specifications.h"
  15. #ifndef MODPLUG_NO_FILESAVE
  16. #include "mpt/io/base.hpp"
  17. #include "mpt/io/io.hpp"
  18. #include "mpt/io/io_stdstream.hpp"
  19. #include "../common/mptFileIO.h"
  20. #endif
  21. #include "OggStream.h"
  22. #include <algorithm>
  23. #ifdef MODPLUG_TRACKER
  24. #include "../mptrack/TrackerSettings.h" // For super smooth ramping option
  25. #endif // MODPLUG_TRACKER
  26. #include "mpt/audio/span.hpp"
  27. #if defined(MPT_WITH_VORBIS) && defined(MPT_WITH_VORBISFILE)
  28. #include <sstream>
  29. #endif
  30. #if defined(MPT_WITH_VORBIS)
  31. #if MPT_COMPILER_CLANG
  32. #pragma clang diagnostic push
  33. #pragma clang diagnostic ignored "-Wreserved-id-macro"
  34. #endif // MPT_COMPILER_CLANG
  35. #include <vorbis/codec.h>
  36. #if MPT_COMPILER_CLANG
  37. #pragma clang diagnostic pop
  38. #endif // MPT_COMPILER_CLANG
  39. #endif
  40. #if defined(MPT_WITH_VORBISFILE)
  41. #if MPT_COMPILER_CLANG
  42. #pragma clang diagnostic push
  43. #pragma clang diagnostic ignored "-Wreserved-id-macro"
  44. #endif // MPT_COMPILER_CLANG
  45. #include <vorbis/vorbisfile.h>
  46. #if MPT_COMPILER_CLANG
  47. #pragma clang diagnostic pop
  48. #endif // MPT_COMPILER_CLANG
  49. #include "openmpt/soundbase/Copy.hpp"
  50. #endif
  51. #ifdef MPT_WITH_STBVORBIS
  52. #include <stb_vorbis/stb_vorbis.c>
  53. #include "openmpt/soundbase/Copy.hpp"
  54. #endif // MPT_WITH_STBVORBIS
  55. OPENMPT_NAMESPACE_BEGIN
  56. #if defined(MPT_WITH_VORBIS) && defined(MPT_WITH_VORBISFILE)
  57. static size_t VorbisfileFilereaderRead(void *ptr, size_t size, size_t nmemb, void *datasource)
  58. {
  59. FileReader &file = *reinterpret_cast<FileReader*>(datasource);
  60. return file.ReadRaw(mpt::span(mpt::void_cast<std::byte*>(ptr), size * nmemb)).size() / size;
  61. }
  62. static int VorbisfileFilereaderSeek(void *datasource, ogg_int64_t offset, int whence)
  63. {
  64. FileReader &file = *reinterpret_cast<FileReader*>(datasource);
  65. switch(whence)
  66. {
  67. case SEEK_SET:
  68. {
  69. if(!mpt::in_range<FileReader::off_t>(offset))
  70. {
  71. return -1;
  72. }
  73. return file.Seek(mpt::saturate_cast<FileReader::off_t>(offset)) ? 0 : -1;
  74. }
  75. break;
  76. case SEEK_CUR:
  77. {
  78. if(offset < 0)
  79. {
  80. if(offset == std::numeric_limits<ogg_int64_t>::min())
  81. {
  82. return -1;
  83. }
  84. if(!mpt::in_range<FileReader::off_t>(0-offset))
  85. {
  86. return -1;
  87. }
  88. return file.SkipBack(mpt::saturate_cast<FileReader::off_t>(0 - offset)) ? 0 : -1;
  89. } else
  90. {
  91. if(!mpt::in_range<FileReader::off_t>(offset))
  92. {
  93. return -1;
  94. }
  95. return file.Skip(mpt::saturate_cast<FileReader::off_t>(offset)) ? 0 : -1;
  96. }
  97. }
  98. break;
  99. case SEEK_END:
  100. {
  101. if(!mpt::in_range<FileReader::off_t>(offset))
  102. {
  103. return -1;
  104. }
  105. if(!mpt::in_range<FileReader::off_t>(file.GetLength() + offset))
  106. {
  107. return -1;
  108. }
  109. return file.Seek(mpt::saturate_cast<FileReader::off_t>(file.GetLength() + offset)) ? 0 : -1;
  110. }
  111. break;
  112. default:
  113. return -1;
  114. }
  115. }
  116. static long VorbisfileFilereaderTell(void *datasource)
  117. {
  118. FileReader &file = *reinterpret_cast<FileReader*>(datasource);
  119. FileReader::off_t result = file.GetPosition();
  120. if(!mpt::in_range<long>(result))
  121. {
  122. return -1;
  123. }
  124. return static_cast<long>(result);
  125. }
  126. #endif // MPT_WITH_VORBIS && MPT_WITH_VORBISFILE
  127. // Allocate samples for an instrument
  128. static std::vector<SAMPLEINDEX> AllocateXMSamples(CSoundFile &sndFile, SAMPLEINDEX numSamples)
  129. {
  130. LimitMax(numSamples, SAMPLEINDEX(32));
  131. std::vector<SAMPLEINDEX> foundSlots;
  132. foundSlots.reserve(numSamples);
  133. for(SAMPLEINDEX i = 0; i < numSamples; i++)
  134. {
  135. SAMPLEINDEX candidateSlot = sndFile.GetNumSamples() + 1;
  136. if(candidateSlot >= MAX_SAMPLES)
  137. {
  138. // If too many sample slots are needed, try to fill some empty slots first.
  139. for(SAMPLEINDEX j = 1; j <= sndFile.GetNumSamples(); j++)
  140. {
  141. if(sndFile.GetSample(j).HasSampleData())
  142. {
  143. continue;
  144. }
  145. if(!mpt::contains(foundSlots, j))
  146. {
  147. // Empty sample slot that is not occupied by the current instrument. Yay!
  148. candidateSlot = j;
  149. // Remove unused sample from instrument sample assignments
  150. for(INSTRUMENTINDEX ins = 1; ins <= sndFile.GetNumInstruments(); ins++)
  151. {
  152. if(sndFile.Instruments[ins] == nullptr)
  153. {
  154. continue;
  155. }
  156. for(auto &sample : sndFile.Instruments[ins]->Keyboard)
  157. {
  158. if(sample == candidateSlot)
  159. {
  160. sample = 0;
  161. }
  162. }
  163. }
  164. break;
  165. }
  166. }
  167. }
  168. if(candidateSlot >= MAX_SAMPLES)
  169. {
  170. // Still couldn't find any empty sample slots, so look out for existing but unused samples.
  171. std::vector<bool> usedSamples;
  172. SAMPLEINDEX unusedSampleCount = sndFile.DetectUnusedSamples(usedSamples);
  173. if(unusedSampleCount > 0)
  174. {
  175. sndFile.RemoveSelectedSamples(usedSamples);
  176. // Remove unused samples from instrument sample assignments
  177. for(INSTRUMENTINDEX ins = 1; ins <= sndFile.GetNumInstruments(); ins++)
  178. {
  179. if(sndFile.Instruments[ins] == nullptr)
  180. {
  181. continue;
  182. }
  183. for(auto &sample : sndFile.Instruments[ins]->Keyboard)
  184. {
  185. if(sample < usedSamples.size() && !usedSamples[sample])
  186. {
  187. sample = 0;
  188. }
  189. }
  190. }
  191. // New candidate slot is first unused sample slot.
  192. candidateSlot = static_cast<SAMPLEINDEX>(std::find(usedSamples.begin() + 1, usedSamples.end(), false) - usedSamples.begin());
  193. } else
  194. {
  195. // No unused sampel slots: Give up :(
  196. break;
  197. }
  198. }
  199. if(candidateSlot < MAX_SAMPLES)
  200. {
  201. foundSlots.push_back(candidateSlot);
  202. if(candidateSlot > sndFile.GetNumSamples())
  203. {
  204. sndFile.m_nSamples = candidateSlot;
  205. }
  206. }
  207. }
  208. return foundSlots;
  209. }
  210. // Read .XM patterns
  211. static void ReadXMPatterns(FileReader &file, const XMFileHeader &fileHeader, CSoundFile &sndFile)
  212. {
  213. // Reading patterns
  214. sndFile.Patterns.ResizeArray(fileHeader.patterns);
  215. for(PATTERNINDEX pat = 0; pat < fileHeader.patterns; pat++)
  216. {
  217. FileReader::off_t curPos = file.GetPosition();
  218. uint32 headerSize = file.ReadUint32LE();
  219. file.Skip(1); // Pack method (= 0)
  220. ROWINDEX numRows = 64;
  221. if(fileHeader.version == 0x0102)
  222. {
  223. numRows = file.ReadUint8() + 1;
  224. } else
  225. {
  226. numRows = file.ReadUint16LE();
  227. }
  228. // A packed size of 0 indicates a completely empty pattern.
  229. const uint16 packedSize = file.ReadUint16LE();
  230. if(numRows == 0)
  231. numRows = 64;
  232. else if(numRows > MAX_PATTERN_ROWS)
  233. numRows = MAX_PATTERN_ROWS;
  234. file.Seek(curPos + headerSize);
  235. FileReader patternChunk = file.ReadChunk(packedSize);
  236. if(!sndFile.Patterns.Insert(pat, numRows) || packedSize == 0)
  237. {
  238. continue;
  239. }
  240. enum PatternFlags
  241. {
  242. isPackByte = 0x80,
  243. allFlags = 0xFF,
  244. notePresent = 0x01,
  245. instrPresent = 0x02,
  246. volPresent = 0x04,
  247. commandPresent = 0x08,
  248. paramPresent = 0x10,
  249. };
  250. for(auto &m : sndFile.Patterns[pat])
  251. {
  252. uint8 info = patternChunk.ReadUint8();
  253. uint8 vol = 0;
  254. if(info & isPackByte)
  255. {
  256. // Interpret byte as flag set.
  257. if(info & notePresent) m.note = patternChunk.ReadUint8();
  258. } else
  259. {
  260. // Interpret byte as note, read all other pattern fields as well.
  261. m.note = info;
  262. info = allFlags;
  263. }
  264. if(info & instrPresent) m.instr = patternChunk.ReadUint8();
  265. if(info & volPresent) vol = patternChunk.ReadUint8();
  266. if(info & commandPresent) m.command = patternChunk.ReadUint8();
  267. if(info & paramPresent) m.param = patternChunk.ReadUint8();
  268. if(m.note == 97)
  269. {
  270. m.note = NOTE_KEYOFF;
  271. } else if(m.note > 0 && m.note < 97)
  272. {
  273. m.note += 12;
  274. } else
  275. {
  276. m.note = NOTE_NONE;
  277. }
  278. if(m.command | m.param)
  279. {
  280. CSoundFile::ConvertModCommand(m);
  281. } else
  282. {
  283. m.command = CMD_NONE;
  284. }
  285. if(m.instr == 0xFF)
  286. {
  287. m.instr = 0;
  288. }
  289. if(vol >= 0x10 && vol <= 0x50)
  290. {
  291. m.volcmd = VOLCMD_VOLUME;
  292. m.vol = vol - 0x10;
  293. } else if (vol >= 0x60)
  294. {
  295. // Volume commands 6-F translation.
  296. static constexpr ModCommand::VOLCMD volEffTrans[] =
  297. {
  298. VOLCMD_VOLSLIDEDOWN, VOLCMD_VOLSLIDEUP, VOLCMD_FINEVOLDOWN, VOLCMD_FINEVOLUP,
  299. VOLCMD_VIBRATOSPEED, VOLCMD_VIBRATODEPTH, VOLCMD_PANNING, VOLCMD_PANSLIDELEFT,
  300. VOLCMD_PANSLIDERIGHT, VOLCMD_TONEPORTAMENTO,
  301. };
  302. m.volcmd = volEffTrans[(vol - 0x60) >> 4];
  303. m.vol = vol & 0x0F;
  304. if(m.volcmd == VOLCMD_PANNING)
  305. {
  306. m.vol *= 4; // FT2 does indeed not scale panning symmetrically.
  307. }
  308. }
  309. }
  310. }
  311. }
  312. enum TrackerVersions
  313. {
  314. verUnknown = 0x00, // Probably not made with MPT
  315. verOldModPlug = 0x01, // Made with MPT Alpha / Beta
  316. verNewModPlug = 0x02, // Made with MPT (not Alpha / Beta)
  317. verModPlug1_09 = 0x04, // Made with MPT 1.09 or possibly other version
  318. verOpenMPT = 0x08, // Made with OpenMPT
  319. verConfirmed = 0x10, // We are very sure that we found the correct tracker version.
  320. verFT2Generic = 0x20, // "FastTracker v2.00", but FastTracker has NOT been ruled out
  321. verOther = 0x40, // Something we don't know, testing for DigiTrakker.
  322. verFT2Clone = 0x80, // NOT FT2: itype changed between instruments, or \0 found in song title
  323. verDigiTrakker = 0x100, // Probably DigiTrakker
  324. verUNMO3 = 0x200, // TODO: UNMO3-ed XMs are detected as MPT 1.16
  325. verEmptyOrders = 0x400, // Allow empty order list like in OpenMPT (FT2 just plays pattern 0 if the order list is empty according to the header)
  326. };
  327. DECLARE_FLAGSET(TrackerVersions)
  328. static bool ValidateHeader(const XMFileHeader &fileHeader)
  329. {
  330. if(fileHeader.channels == 0
  331. || fileHeader.channels > MAX_BASECHANNELS
  332. || std::memcmp(fileHeader.signature, "Extended Module: ", 17)
  333. )
  334. {
  335. return false;
  336. }
  337. return true;
  338. }
  339. static uint64 GetHeaderMinimumAdditionalSize(const XMFileHeader &fileHeader)
  340. {
  341. return fileHeader.orders + 4 * (fileHeader.patterns + fileHeader.instruments);
  342. }
  343. CSoundFile::ProbeResult CSoundFile::ProbeFileHeaderXM(MemoryFileReader file, const uint64 *pfilesize)
  344. {
  345. XMFileHeader fileHeader;
  346. if(!file.ReadStruct(fileHeader))
  347. {
  348. return ProbeWantMoreData;
  349. }
  350. if(!ValidateHeader(fileHeader))
  351. {
  352. return ProbeFailure;
  353. }
  354. return ProbeAdditionalSize(file, pfilesize, GetHeaderMinimumAdditionalSize(fileHeader));
  355. }
  356. static bool ReadSampleData(ModSample &sample, SampleIO sampleFlags, FileReader &sampleChunk, bool &isOXM)
  357. {
  358. bool unsupportedSample = false;
  359. bool isOGG = false;
  360. if(sampleChunk.CanRead(8))
  361. {
  362. isOGG = true;
  363. sampleChunk.Skip(4);
  364. // In order to avoid false-detecting PCM as OggVorbis as much as possible,
  365. // we parse and verify the complete sample data and only assume OggVorbis,
  366. // if all Ogg checksums are correct a no single byte of non-Ogg data exists.
  367. // The fast-path for regular PCM will only check "OggS" magic and do no other work after failing that check.
  368. while(!sampleChunk.EndOfFile())
  369. {
  370. if(!Ogg::ReadPage(sampleChunk))
  371. {
  372. isOGG = false;
  373. break;
  374. }
  375. }
  376. }
  377. isOXM = isOXM || isOGG;
  378. sampleChunk.Rewind();
  379. if(isOGG)
  380. {
  381. uint32 originalSize = sampleChunk.ReadInt32LE();
  382. FileReader sampleData = sampleChunk.ReadChunk(sampleChunk.BytesLeft());
  383. sample.uFlags.set(CHN_16BIT, sampleFlags.GetBitDepth() >= 16);
  384. sample.uFlags.set(CHN_STEREO, sampleFlags.GetChannelFormat() != SampleIO::mono);
  385. sample.nLength = originalSize / (sample.uFlags[CHN_16BIT] ? 2 : 1) / (sample.uFlags[CHN_STEREO] ? 2 : 1);
  386. #if defined(MPT_WITH_VORBIS) && defined(MPT_WITH_VORBISFILE)
  387. ov_callbacks callbacks = {
  388. &VorbisfileFilereaderRead,
  389. &VorbisfileFilereaderSeek,
  390. NULL,
  391. &VorbisfileFilereaderTell
  392. };
  393. OggVorbis_File vf;
  394. MemsetZero(vf);
  395. if(ov_open_callbacks(&sampleData, &vf, nullptr, 0, callbacks) == 0)
  396. {
  397. if(ov_streams(&vf) == 1)
  398. { // we do not support chained vorbis samples
  399. vorbis_info *vi = ov_info(&vf, -1);
  400. if(vi && vi->rate > 0 && vi->channels > 0)
  401. {
  402. sample.AllocateSample();
  403. SmpLength offset = 0;
  404. int channels = vi->channels;
  405. int current_section = 0;
  406. long decodedSamples = 0;
  407. bool eof = false;
  408. while(!eof && offset < sample.nLength && sample.HasSampleData())
  409. {
  410. float **output = nullptr;
  411. long ret = ov_read_float(&vf, &output, 1024, &current_section);
  412. if(ret == 0)
  413. {
  414. eof = true;
  415. } else if(ret < 0)
  416. {
  417. // stream error, just try to continue
  418. } else
  419. {
  420. decodedSamples = ret;
  421. LimitMax(decodedSamples, mpt::saturate_cast<long>(sample.nLength - offset));
  422. if(decodedSamples > 0 && channels == sample.GetNumChannels())
  423. {
  424. if(sample.uFlags[CHN_16BIT])
  425. {
  426. CopyAudio(mpt::audio_span_interleaved(sample.sample16() + (offset * sample.GetNumChannels()), sample.GetNumChannels(), decodedSamples), mpt::audio_span_planar(output, channels, decodedSamples));
  427. } else
  428. {
  429. CopyAudio(mpt::audio_span_interleaved(sample.sample8() + (offset * sample.GetNumChannels()), sample.GetNumChannels(), decodedSamples), mpt::audio_span_planar(output, channels, decodedSamples));
  430. }
  431. }
  432. offset += decodedSamples;
  433. }
  434. }
  435. } else
  436. {
  437. unsupportedSample = true;
  438. }
  439. } else
  440. {
  441. unsupportedSample = true;
  442. }
  443. ov_clear(&vf);
  444. } else
  445. {
  446. unsupportedSample = true;
  447. }
  448. #elif defined(MPT_WITH_STBVORBIS)
  449. // NOTE/TODO: stb_vorbis does not handle inferred negative PCM sample
  450. // position at stream start. (See
  451. // <https://www.xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-132000A.2>).
  452. // This means that, for remuxed and re-aligned/cutted (at stream start)
  453. // Vorbis files, stb_vorbis will include superfluous samples at the
  454. // beginning. OXM files with this property are yet to be spotted in the
  455. // wild, thus, this behaviour is currently not problematic.
  456. int consumed = 0, error = 0;
  457. stb_vorbis *vorb = nullptr;
  458. FileReader::PinnedView sampleDataView = sampleData.GetPinnedView();
  459. const std::byte* data = sampleDataView.data();
  460. std::size_t dataLeft = sampleDataView.size();
  461. vorb = stb_vorbis_open_pushdata(mpt::byte_cast<const unsigned char*>(data), mpt::saturate_cast<int>(dataLeft), &consumed, &error, nullptr);
  462. sampleData.Skip(consumed);
  463. data += consumed;
  464. dataLeft -= consumed;
  465. if(vorb)
  466. {
  467. // Header has been read, proceed to reading the sample data
  468. sample.AllocateSample();
  469. SmpLength offset = 0;
  470. while((error == VORBIS__no_error || (error == VORBIS_need_more_data && dataLeft > 0))
  471. && offset < sample.nLength && sample.HasSampleData())
  472. {
  473. int channels = 0, decodedSamples = 0;
  474. float **output;
  475. consumed = stb_vorbis_decode_frame_pushdata(vorb, mpt::byte_cast<const unsigned char*>(data), mpt::saturate_cast<int>(dataLeft), &channels, &output, &decodedSamples);
  476. sampleData.Skip(consumed);
  477. data += consumed;
  478. dataLeft -= consumed;
  479. LimitMax(decodedSamples, mpt::saturate_cast<int>(sample.nLength - offset));
  480. if(decodedSamples > 0 && channels == sample.GetNumChannels())
  481. {
  482. if(sample.uFlags[CHN_16BIT])
  483. {
  484. CopyAudio(mpt::audio_span_interleaved(sample.sample16() + (offset * sample.GetNumChannels()), sample.GetNumChannels(), decodedSamples), mpt::audio_span_planar(output, channels, decodedSamples));
  485. } else
  486. {
  487. CopyAudio(mpt::audio_span_interleaved(sample.sample8() + (offset * sample.GetNumChannels()), sample.GetNumChannels(), decodedSamples), mpt::audio_span_planar(output, channels, decodedSamples));
  488. }
  489. }
  490. offset += decodedSamples;
  491. error = stb_vorbis_get_error(vorb);
  492. }
  493. stb_vorbis_close(vorb);
  494. } else
  495. {
  496. unsupportedSample = true;
  497. }
  498. #else // !VORBIS
  499. unsupportedSample = true;
  500. #endif // VORBIS
  501. } else
  502. {
  503. sampleFlags.ReadSample(sample, sampleChunk);
  504. }
  505. return !unsupportedSample;
  506. }
  507. bool CSoundFile::ReadXM(FileReader &file, ModLoadingFlags loadFlags)
  508. {
  509. file.Rewind();
  510. XMFileHeader fileHeader;
  511. if(!file.ReadStruct(fileHeader))
  512. {
  513. return false;
  514. }
  515. if(!ValidateHeader(fileHeader))
  516. {
  517. return false;
  518. }
  519. if(!file.CanRead(mpt::saturate_cast<FileReader::off_t>(GetHeaderMinimumAdditionalSize(fileHeader))))
  520. {
  521. return false;
  522. } else if(loadFlags == onlyVerifyHeader)
  523. {
  524. return true;
  525. }
  526. InitializeGlobals(MOD_TYPE_XM);
  527. InitializeChannels();
  528. m_nMixLevels = MixLevels::Compatible;
  529. FlagSet<TrackerVersions> madeWith(verUnknown);
  530. mpt::ustring madeWithTracker;
  531. bool isMadTracker = false;
  532. if(!memcmp(fileHeader.trackerName, "FastTracker v2.00 ", 20) && fileHeader.size == 276)
  533. {
  534. if(fileHeader.version < 0x0104)
  535. madeWith = verFT2Generic | verConfirmed;
  536. else if(memchr(fileHeader.songName, '\0', 20) != nullptr)
  537. // FT2 pads the song title with spaces, some other trackers use null chars
  538. madeWith = verFT2Clone | verNewModPlug | verEmptyOrders;
  539. else
  540. madeWith = verFT2Generic | verNewModPlug;
  541. } else if(!memcmp(fileHeader.trackerName, "FastTracker v 2.00 ", 20))
  542. {
  543. // MPT 1.0 (exact version to be determined later)
  544. madeWith = verOldModPlug;
  545. } else
  546. {
  547. // Something else!
  548. madeWith = verUnknown | verConfirmed;
  549. madeWithTracker = mpt::ToUnicode(mpt::Charset::CP437, mpt::String::ReadBuf(mpt::String::spacePadded, fileHeader.trackerName));
  550. if(!memcmp(fileHeader.trackerName, "OpenMPT ", 8))
  551. {
  552. madeWith = verOpenMPT | verConfirmed | verEmptyOrders;
  553. } else if(!memcmp(fileHeader.trackerName, "MilkyTracker ", 12))
  554. {
  555. // MilkyTracker prior to version 0.90.87 doesn't set a version string.
  556. // Luckily, starting with v0.90.87, MilkyTracker also implements the FT2 panning scheme.
  557. if(memcmp(fileHeader.trackerName + 12, " ", 8))
  558. {
  559. m_nMixLevels = MixLevels::CompatibleFT2;
  560. }
  561. } else if(!memcmp(fileHeader.trackerName, "Fasttracker II clone", 20))
  562. {
  563. // 8bitbubsy's FT2 clone should be treated exactly like FT2
  564. madeWith = verFT2Generic | verConfirmed;
  565. } else if(!memcmp(fileHeader.trackerName, "MadTracker 2.0\0", 15))
  566. {
  567. // Fix channel 2 in m3_cha.xm
  568. m_playBehaviour.reset(kFT2PortaNoNote);
  569. // Fix arpeggios in kragle_-_happy_day.xm
  570. m_playBehaviour.reset(kFT2Arpeggio);
  571. isMadTracker = true;
  572. } else if(!memcmp(fileHeader.trackerName, "Skale Tracker\0", 14) || !memcmp(fileHeader.trackerName, "Sk@le Tracker\0", 14))
  573. {
  574. m_playBehaviour.reset(kFT2ST3OffsetOutOfRange);
  575. // Fix arpeggios in KAPTENFL.XM
  576. m_playBehaviour.reset(kFT2Arpeggio);
  577. } else if(!memcmp(fileHeader.trackerName, "*Converted ", 11))
  578. {
  579. madeWith = verDigiTrakker;
  580. }
  581. }
  582. m_songName = mpt::String::ReadBuf(mpt::String::spacePadded, fileHeader.songName);
  583. m_nMinPeriod = 1;
  584. m_nMaxPeriod = 31999;
  585. Order().SetRestartPos(fileHeader.restartPos);
  586. m_nChannels = fileHeader.channels;
  587. m_nInstruments = std::min(static_cast<uint16>(fileHeader.instruments), static_cast<uint16>(MAX_INSTRUMENTS - 1));
  588. if(fileHeader.speed)
  589. m_nDefaultSpeed = fileHeader.speed;
  590. if(fileHeader.tempo)
  591. m_nDefaultTempo = Clamp(TEMPO(fileHeader.tempo, 0), ModSpecs::xmEx.GetTempoMin(), ModSpecs::xmEx.GetTempoMax());
  592. m_SongFlags.reset();
  593. m_SongFlags.set(SONG_LINEARSLIDES, (fileHeader.flags & XMFileHeader::linearSlides) != 0);
  594. m_SongFlags.set(SONG_EXFILTERRANGE, (fileHeader.flags & XMFileHeader::extendedFilterRange) != 0);
  595. if(m_SongFlags[SONG_EXFILTERRANGE] && madeWith == (verFT2Generic | verNewModPlug))
  596. {
  597. madeWith = verFT2Clone | verNewModPlug | verConfirmed;
  598. }
  599. ReadOrderFromFile<uint8>(Order(), file, fileHeader.orders);
  600. if(fileHeader.orders == 0 && !madeWith[verEmptyOrders])
  601. {
  602. // Fix lamb_-_dark_lighthouse.xm, which only contains one pattern and an empty order list
  603. Order().assign(1, 0);
  604. }
  605. file.Seek(fileHeader.size + 60);
  606. if(fileHeader.version >= 0x0104)
  607. {
  608. ReadXMPatterns(file, fileHeader, *this);
  609. }
  610. bool isOXM = false;
  611. // In case of XM versions < 1.04, we need to memorize the sample flags for all samples, as they are not stored immediately after the sample headers.
  612. std::vector<SampleIO> sampleFlags;
  613. uint8 sampleReserved = 0;
  614. int instrType = -1;
  615. bool unsupportedSamples = false;
  616. // Reading instruments
  617. for(INSTRUMENTINDEX instr = 1; instr <= m_nInstruments; instr++)
  618. {
  619. // First, try to read instrument header length...
  620. uint32 headerSize = file.ReadUint32LE();
  621. if(headerSize == 0)
  622. {
  623. headerSize = sizeof(XMInstrumentHeader);
  624. }
  625. // Now, read the complete struct.
  626. file.SkipBack(4);
  627. XMInstrumentHeader instrHeader;
  628. file.ReadStructPartial(instrHeader, headerSize);
  629. // Time for some version detection stuff.
  630. if(madeWith == verOldModPlug)
  631. {
  632. madeWith.set(verConfirmed);
  633. if(instrHeader.size == 245)
  634. {
  635. // ModPlug Tracker Alpha
  636. m_dwLastSavedWithVersion = MPT_V("1.00.00.A5");
  637. madeWithTracker = U_("ModPlug Tracker 1.0 alpha");
  638. } else if(instrHeader.size == 263)
  639. {
  640. // ModPlug Tracker Beta (Beta 1 still behaves like Alpha, but Beta 3.3 does it this way)
  641. m_dwLastSavedWithVersion = MPT_V("1.00.00.B3");
  642. madeWithTracker = U_("ModPlug Tracker 1.0 beta");
  643. } else
  644. {
  645. // WTF?
  646. madeWith = (verUnknown | verConfirmed);
  647. }
  648. } else if(instrHeader.numSamples == 0)
  649. {
  650. // Empty instruments make tracker identification pretty easy!
  651. if(instrHeader.size == 263 && instrHeader.sampleHeaderSize == 0 && madeWith[verNewModPlug])
  652. madeWith.set(verConfirmed);
  653. else if(instrHeader.size != 29 && madeWith[verDigiTrakker])
  654. madeWith.reset(verDigiTrakker);
  655. else if(madeWith[verFT2Clone | verFT2Generic] && instrHeader.size != 33)
  656. {
  657. // Sure isn't FT2.
  658. // Note: FT2 NORMALLY writes shdr=40 for all samples, but sometimes it
  659. // just happens to write random garbage there instead. Surprise!
  660. // Note: 4-mat's eternity.xm has an instrument header size of 29.
  661. madeWith = verUnknown;
  662. }
  663. }
  664. if(AllocateInstrument(instr) == nullptr)
  665. {
  666. continue;
  667. }
  668. instrHeader.ConvertToMPT(*Instruments[instr]);
  669. if(instrType == -1)
  670. {
  671. instrType = instrHeader.type;
  672. } else if(instrType != instrHeader.type && madeWith[verFT2Generic])
  673. {
  674. // FT2 writes some random junk for the instrument type field,
  675. // but it's always the SAME junk for every instrument saved.
  676. madeWith.reset(verFT2Generic);
  677. madeWith.set(verFT2Clone);
  678. }
  679. if(instrHeader.numSamples > 0)
  680. {
  681. // Yep, there are some samples associated with this instrument.
  682. if((instrHeader.instrument.midiEnabled | instrHeader.instrument.midiChannel | instrHeader.instrument.midiProgram | instrHeader.instrument.muteComputer) != 0)
  683. {
  684. // Definitely not an old MPT.
  685. madeWith.reset(verOldModPlug | verNewModPlug);
  686. }
  687. // Read sample headers
  688. std::vector<SAMPLEINDEX> sampleSlots = AllocateXMSamples(*this, instrHeader.numSamples);
  689. // Update sample assignment map
  690. for(size_t k = 0 + 12; k < 96 + 12; k++)
  691. {
  692. if(Instruments[instr]->Keyboard[k] < sampleSlots.size())
  693. {
  694. Instruments[instr]->Keyboard[k] = sampleSlots[Instruments[instr]->Keyboard[k]];
  695. }
  696. }
  697. if(fileHeader.version >= 0x0104)
  698. {
  699. sampleFlags.clear();
  700. }
  701. // Need to memorize those if we're going to skip any samples...
  702. std::vector<uint32> sampleSize(instrHeader.numSamples);
  703. // Early versions of Sk@le Tracker set instrHeader.sampleHeaderSize = 0 (IFULOVE.XM)
  704. // cybernostra weekend has instrHeader.sampleHeaderSize = 0x12, which would leave out the sample name, but FT2 still reads the name.
  705. MPT_ASSERT(instrHeader.sampleHeaderSize == 0 || instrHeader.sampleHeaderSize == sizeof(XMSample));
  706. for(SAMPLEINDEX sample = 0; sample < instrHeader.numSamples; sample++)
  707. {
  708. XMSample sampleHeader;
  709. file.ReadStruct(sampleHeader);
  710. sampleFlags.push_back(sampleHeader.GetSampleFormat());
  711. sampleSize[sample] = sampleHeader.length;
  712. sampleReserved |= sampleHeader.reserved;
  713. if(sample < sampleSlots.size())
  714. {
  715. SAMPLEINDEX mptSample = sampleSlots[sample];
  716. sampleHeader.ConvertToMPT(Samples[mptSample]);
  717. instrHeader.instrument.ApplyAutoVibratoToMPT(Samples[mptSample]);
  718. m_szNames[mptSample] = mpt::String::ReadBuf(mpt::String::spacePadded, sampleHeader.name);
  719. if((sampleHeader.flags & 3) == 3 && madeWith[verNewModPlug])
  720. {
  721. // MPT 1.09 and maybe newer / older versions set both loop flags for bidi loops.
  722. madeWith.set(verModPlug1_09);
  723. }
  724. }
  725. }
  726. // Read samples
  727. if(fileHeader.version >= 0x0104)
  728. {
  729. for(SAMPLEINDEX sample = 0; sample < instrHeader.numSamples; sample++)
  730. {
  731. // Sample 15 in dirtysex.xm by J/M/T/M is a 16-bit sample with an odd size of 0x18B according to the header, while the real sample size would be 0x18A.
  732. // Always read as many bytes as specified in the header, even if the sample reader would probably read less bytes.
  733. FileReader sampleChunk = file.ReadChunk(sampleFlags[sample].GetEncoding() != SampleIO::ADPCM ? sampleSize[sample] : (16 + (sampleSize[sample] + 1) / 2));
  734. if(sample < sampleSlots.size() && (loadFlags & loadSampleData))
  735. {
  736. if(!ReadSampleData(Samples[sampleSlots[sample]], sampleFlags[sample], sampleChunk, isOXM))
  737. {
  738. unsupportedSamples = true;
  739. }
  740. }
  741. }
  742. }
  743. }
  744. }
  745. if(sampleReserved == 0 && madeWith[verNewModPlug] && memchr(fileHeader.songName, '\0', sizeof(fileHeader.songName)) != nullptr)
  746. {
  747. // Null-terminated song name: Quite possibly MPT. (could really be an MPT-made file resaved in FT2, though)
  748. madeWith.set(verConfirmed);
  749. }
  750. if(fileHeader.version < 0x0104)
  751. {
  752. // Load Patterns and Samples (Version 1.02 and 1.03)
  753. if(loadFlags & (loadPatternData | loadSampleData))
  754. {
  755. ReadXMPatterns(file, fileHeader, *this);
  756. }
  757. if(loadFlags & loadSampleData)
  758. {
  759. for(SAMPLEINDEX sample = 1; sample <= GetNumSamples(); sample++)
  760. {
  761. sampleFlags[sample - 1].ReadSample(Samples[sample], file);
  762. }
  763. }
  764. }
  765. if(unsupportedSamples)
  766. {
  767. AddToLog(LogWarning, U_("Some compressed samples could not be loaded because they use an unsupported codec."));
  768. }
  769. // Read song comments: "text"
  770. if(file.ReadMagic("text"))
  771. {
  772. m_songMessage.Read(file, file.ReadUint32LE(), SongMessage::leCR);
  773. madeWith.set(verConfirmed);
  774. }
  775. // Read midi config: "MIDI"
  776. bool hasMidiConfig = false;
  777. if(file.ReadMagic("MIDI"))
  778. {
  779. file.ReadStructPartial<MIDIMacroConfigData>(m_MidiCfg, file.ReadUint32LE());
  780. m_MidiCfg.Sanitize();
  781. hasMidiConfig = true;
  782. madeWith.set(verConfirmed);
  783. }
  784. // Read pattern names: "PNAM"
  785. if(file.ReadMagic("PNAM"))
  786. {
  787. const PATTERNINDEX namedPats = std::min(static_cast<PATTERNINDEX>(file.ReadUint32LE() / MAX_PATTERNNAME), Patterns.Size());
  788. for(PATTERNINDEX pat = 0; pat < namedPats; pat++)
  789. {
  790. char patName[MAX_PATTERNNAME];
  791. file.ReadString<mpt::String::maybeNullTerminated>(patName, MAX_PATTERNNAME);
  792. Patterns[pat].SetName(patName);
  793. }
  794. madeWith.set(verConfirmed);
  795. }
  796. // Read channel names: "CNAM"
  797. if(file.ReadMagic("CNAM"))
  798. {
  799. const CHANNELINDEX namedChans = std::min(static_cast<CHANNELINDEX>(file.ReadUint32LE() / MAX_CHANNELNAME), GetNumChannels());
  800. for(CHANNELINDEX chn = 0; chn < namedChans; chn++)
  801. {
  802. file.ReadString<mpt::String::maybeNullTerminated>(ChnSettings[chn].szName, MAX_CHANNELNAME);
  803. }
  804. madeWith.set(verConfirmed);
  805. }
  806. // Read mix plugins information
  807. if(file.CanRead(8))
  808. {
  809. FileReader::off_t oldPos = file.GetPosition();
  810. LoadMixPlugins(file);
  811. if(file.GetPosition() != oldPos)
  812. {
  813. madeWith.set(verConfirmed);
  814. }
  815. }
  816. if(madeWith[verConfirmed])
  817. {
  818. if(madeWith[verModPlug1_09])
  819. {
  820. m_dwLastSavedWithVersion = MPT_V("1.09.00.00");
  821. madeWithTracker = U_("ModPlug Tracker 1.09");
  822. } else if(madeWith[verNewModPlug])
  823. {
  824. m_dwLastSavedWithVersion = MPT_V("1.16.00.00");
  825. madeWithTracker = U_("ModPlug Tracker 1.10 - 1.16");
  826. }
  827. }
  828. if(!memcmp(fileHeader.trackerName, "OpenMPT ", 8))
  829. {
  830. // Hey, I know this tracker!
  831. std::string mptVersion(fileHeader.trackerName + 8, 12);
  832. m_dwLastSavedWithVersion = Version::Parse(mpt::ToUnicode(mpt::Charset::ASCII, mptVersion));
  833. madeWith = verOpenMPT | verConfirmed;
  834. if(m_dwLastSavedWithVersion < MPT_V("1.22.07.19"))
  835. m_nMixLevels = MixLevels::Compatible;
  836. else
  837. m_nMixLevels = MixLevels::CompatibleFT2;
  838. }
  839. if(m_dwLastSavedWithVersion && !madeWith[verOpenMPT])
  840. {
  841. m_nMixLevels = MixLevels::Original;
  842. m_playBehaviour.reset();
  843. }
  844. if(madeWith[verFT2Generic])
  845. {
  846. m_nMixLevels = MixLevels::CompatibleFT2;
  847. if(!hasMidiConfig)
  848. {
  849. // FT2 allows typing in arbitrary unsupported effect letters such as Zxx.
  850. // Prevent these commands from being interpreted as filter commands by erasing the default MIDI Config.
  851. m_MidiCfg.ClearZxxMacros();
  852. }
  853. if(fileHeader.version >= 0x0104 // Old versions of FT2 didn't have (smooth) ramping. Disable it for those versions where we can be sure that there should be no ramping.
  854. #ifdef MODPLUG_TRACKER
  855. && TrackerSettings::Instance().autoApplySmoothFT2Ramping
  856. #endif // MODPLUG_TRACKER
  857. )
  858. {
  859. // apply FT2-style super-soft volume ramping
  860. m_playBehaviour.set(kFT2VolumeRamping);
  861. }
  862. }
  863. if(madeWithTracker.empty())
  864. {
  865. if(madeWith[verDigiTrakker] && sampleReserved == 0 && (instrType ? instrType : -1) == -1)
  866. {
  867. madeWithTracker = U_("DigiTrakker");
  868. } else if(madeWith[verFT2Generic])
  869. {
  870. madeWithTracker = U_("FastTracker 2 or compatible");
  871. } else
  872. {
  873. madeWithTracker = U_("Unknown");
  874. }
  875. }
  876. bool isOpenMPTMade = false; // specific for OpenMPT 1.17+
  877. if(GetNumInstruments())
  878. {
  879. isOpenMPTMade = LoadExtendedInstrumentProperties(file);
  880. }
  881. LoadExtendedSongProperties(file, true, &isOpenMPTMade);
  882. if(isOpenMPTMade && m_dwLastSavedWithVersion < MPT_V("1.17.00.00"))
  883. {
  884. // Up to OpenMPT 1.17.02.45 (r165), it was possible that the "last saved with" field was 0
  885. // when saving a file in OpenMPT for the first time.
  886. m_dwLastSavedWithVersion = MPT_V("1.17.00.00");
  887. }
  888. if(m_dwLastSavedWithVersion >= MPT_V("1.17.00.00"))
  889. {
  890. madeWithTracker = U_("OpenMPT ") + m_dwLastSavedWithVersion.ToUString();
  891. }
  892. // We no longer allow any --- or +++ items in the order list now.
  893. if(m_dwLastSavedWithVersion && m_dwLastSavedWithVersion < MPT_V("1.22.02.02"))
  894. {
  895. if(!Patterns.IsValidPat(0xFE))
  896. Order().RemovePattern(0xFE);
  897. if(!Patterns.IsValidPat(0xFF))
  898. Order().Replace(0xFF, Order.GetInvalidPatIndex());
  899. }
  900. m_modFormat.formatName = MPT_UFORMAT("FastTracker 2 v{}.{}")(fileHeader.version >> 8, mpt::ufmt::hex0<2>(fileHeader.version & 0xFF));
  901. m_modFormat.madeWithTracker = std::move(madeWithTracker);
  902. m_modFormat.charset = (m_dwLastSavedWithVersion || isMadTracker) ? mpt::Charset::Windows1252 : mpt::Charset::CP437;
  903. if(isOXM)
  904. {
  905. m_modFormat.originalFormatName = std::move(m_modFormat.formatName);
  906. m_modFormat.formatName = U_("OggMod FastTracker 2");
  907. m_modFormat.type = U_("oxm");
  908. m_modFormat.originalType = U_("xm");
  909. } else
  910. {
  911. m_modFormat.type = U_("xm");
  912. }
  913. return true;
  914. }
  915. #ifndef MODPLUG_NO_FILESAVE
  916. bool CSoundFile::SaveXM(std::ostream &f, bool compatibilityExport)
  917. {
  918. bool addChannel = false; // avoid odd channel count for FT2 compatibility
  919. XMFileHeader fileHeader;
  920. MemsetZero(fileHeader);
  921. memcpy(fileHeader.signature, "Extended Module: ", 17);
  922. mpt::String::WriteBuf(mpt::String::spacePadded, fileHeader.songName) = m_songName;
  923. fileHeader.eof = 0x1A;
  924. const std::string openMptTrackerName = mpt::ToCharset(GetCharsetFile(), Version::Current().GetOpenMPTVersionString());
  925. mpt::String::WriteBuf(mpt::String::spacePadded, fileHeader.trackerName) = openMptTrackerName;
  926. // Writing song header
  927. fileHeader.version = 0x0104; // XM Format v1.04
  928. fileHeader.size = sizeof(XMFileHeader) - 60; // minus everything before this field
  929. fileHeader.restartPos = Order().GetRestartPos();
  930. fileHeader.channels = m_nChannels;
  931. if((m_nChannels % 2u) && m_nChannels < 32)
  932. {
  933. // Avoid odd channel count for FT2 compatibility
  934. fileHeader.channels++;
  935. addChannel = true;
  936. } else if(compatibilityExport && fileHeader.channels > 32)
  937. {
  938. fileHeader.channels = 32;
  939. }
  940. // Find out number of orders and patterns used.
  941. // +++ and --- patterns are not taken into consideration as FastTracker does not support them.
  942. const ORDERINDEX trimmedLength = Order().GetLengthTailTrimmed();
  943. std::vector<uint8> orderList(trimmedLength);
  944. const ORDERINDEX orderLimit = compatibilityExport ? 256 : uint16_max;
  945. ORDERINDEX numOrders = 0;
  946. PATTERNINDEX numPatterns = Patterns.GetNumPatterns();
  947. bool changeOrderList = false;
  948. for(ORDERINDEX ord = 0; ord < trimmedLength; ord++)
  949. {
  950. PATTERNINDEX pat = Order()[ord];
  951. if(pat == Order.GetIgnoreIndex() || pat == Order.GetInvalidPatIndex() || pat > uint8_max)
  952. {
  953. changeOrderList = true;
  954. } else if(numOrders < orderLimit)
  955. {
  956. orderList[numOrders++] = static_cast<uint8>(pat);
  957. if(pat >= numPatterns)
  958. numPatterns = pat + 1;
  959. }
  960. }
  961. if(changeOrderList)
  962. {
  963. AddToLog(LogWarning, U_("Skip and stop order list items (+++ and ---) are not saved in XM files."));
  964. }
  965. orderList.resize(compatibilityExport ? 256 : numOrders);
  966. fileHeader.orders = numOrders;
  967. fileHeader.patterns = numPatterns;
  968. fileHeader.size += static_cast<uint32>(orderList.size());
  969. uint16 writeInstruments;
  970. if(m_nInstruments > 0)
  971. fileHeader.instruments = writeInstruments = m_nInstruments;
  972. else
  973. fileHeader.instruments = writeInstruments = m_nSamples;
  974. if(m_SongFlags[SONG_LINEARSLIDES]) fileHeader.flags |= XMFileHeader::linearSlides;
  975. if(m_SongFlags[SONG_EXFILTERRANGE] && !compatibilityExport) fileHeader.flags |= XMFileHeader::extendedFilterRange;
  976. fileHeader.flags = fileHeader.flags;
  977. // Fasttracker 2 will happily accept any tempo faster than 255 BPM. XMPlay does also support this, great!
  978. fileHeader.tempo = mpt::saturate_cast<uint16>(m_nDefaultTempo.GetInt());
  979. fileHeader.speed = static_cast<uint16>(Clamp(m_nDefaultSpeed, 1u, 31u));
  980. mpt::IO::Write(f, fileHeader);
  981. // Write processed order list
  982. mpt::IO::Write(f, orderList);
  983. // Writing patterns
  984. #define ASSERT_CAN_WRITE(x) \
  985. if(len > s.size() - x) /*Buffer running out? Make it larger.*/ \
  986. s.resize(s.size() + 10 * 1024, 0);
  987. std::vector<uint8> s(64 * 64 * 5, 0);
  988. for(PATTERNINDEX pat = 0; pat < numPatterns; pat++)
  989. {
  990. uint8 patHead[9] = { 0 };
  991. patHead[0] = 9;
  992. if(!Patterns.IsValidPat(pat))
  993. {
  994. // There's nothing to write... chicken out.
  995. patHead[5] = 64;
  996. mpt::IO::Write(f, patHead);
  997. continue;
  998. }
  999. const uint16 numRows = mpt::saturate_cast<uint16>(Patterns[pat].GetNumRows());
  1000. patHead[5] = static_cast<uint8>(numRows & 0xFF);
  1001. patHead[6] = static_cast<uint8>(numRows >> 8);
  1002. auto p = Patterns[pat].cbegin();
  1003. size_t len = 0;
  1004. // Empty patterns are always loaded as 64-row patterns in FT2, regardless of their real size...
  1005. bool emptyPattern = true;
  1006. for(size_t j = m_nChannels * numRows; j > 0; j--, p++)
  1007. {
  1008. // Don't write more than 32 channels
  1009. if(compatibilityExport && m_nChannels - ((j - 1) % m_nChannels) > 32) continue;
  1010. uint8 note = p->note;
  1011. uint8 command = p->command, param = p->param;
  1012. ModSaveCommand(command, param, true, compatibilityExport);
  1013. if (note >= NOTE_MIN_SPECIAL) note = 97; else
  1014. if ((note <= 12) || (note > 96+12)) note = 0; else
  1015. note -= 12;
  1016. uint8 vol = 0;
  1017. if (p->volcmd != VOLCMD_NONE)
  1018. {
  1019. switch(p->volcmd)
  1020. {
  1021. case VOLCMD_VOLUME: vol = 0x10 + p->vol; break;
  1022. case VOLCMD_VOLSLIDEDOWN: vol = 0x60 + (p->vol & 0x0F); break;
  1023. case VOLCMD_VOLSLIDEUP: vol = 0x70 + (p->vol & 0x0F); break;
  1024. case VOLCMD_FINEVOLDOWN: vol = 0x80 + (p->vol & 0x0F); break;
  1025. case VOLCMD_FINEVOLUP: vol = 0x90 + (p->vol & 0x0F); break;
  1026. case VOLCMD_VIBRATOSPEED: vol = 0xA0 + (p->vol & 0x0F); break;
  1027. case VOLCMD_VIBRATODEPTH: vol = 0xB0 + (p->vol & 0x0F); break;
  1028. case VOLCMD_PANNING: vol = 0xC0 + (p->vol / 4); if (vol > 0xCF) vol = 0xCF; break;
  1029. case VOLCMD_PANSLIDELEFT: vol = 0xD0 + (p->vol & 0x0F); break;
  1030. case VOLCMD_PANSLIDERIGHT: vol = 0xE0 + (p->vol & 0x0F); break;
  1031. case VOLCMD_TONEPORTAMENTO: vol = 0xF0 + (p->vol & 0x0F); break;
  1032. }
  1033. // Those values are ignored in FT2. Don't save them, also to avoid possible problems with other trackers (or MPT itself)
  1034. if(compatibilityExport && p->vol == 0)
  1035. {
  1036. switch(p->volcmd)
  1037. {
  1038. case VOLCMD_VOLUME:
  1039. case VOLCMD_PANNING:
  1040. case VOLCMD_VIBRATODEPTH:
  1041. case VOLCMD_TONEPORTAMENTO:
  1042. case VOLCMD_PANSLIDELEFT: // Doesn't have memory, but does weird things with zero param.
  1043. break;
  1044. default:
  1045. // no memory here.
  1046. vol = 0;
  1047. }
  1048. }
  1049. }
  1050. // no need to fix non-empty patterns
  1051. if(!p->IsEmpty())
  1052. emptyPattern = false;
  1053. // Apparently, completely empty patterns are loaded as empty 64-row patterns in FT2, regardless of their original size.
  1054. // We have to avoid this, so we add a "break to row 0" command in the last row.
  1055. if(j == 1 && emptyPattern && numRows != 64)
  1056. {
  1057. command = 0x0D;
  1058. param = 0;
  1059. }
  1060. if ((note) && (p->instr) && (vol > 0x0F) && (command) && (param))
  1061. {
  1062. s[len++] = note;
  1063. s[len++] = p->instr;
  1064. s[len++] = vol;
  1065. s[len++] = command;
  1066. s[len++] = param;
  1067. } else
  1068. {
  1069. uint8 b = 0x80;
  1070. if (note) b |= 0x01;
  1071. if (p->instr) b |= 0x02;
  1072. if (vol >= 0x10) b |= 0x04;
  1073. if (command) b |= 0x08;
  1074. if (param) b |= 0x10;
  1075. s[len++] = b;
  1076. if (b & 1) s[len++] = note;
  1077. if (b & 2) s[len++] = p->instr;
  1078. if (b & 4) s[len++] = vol;
  1079. if (b & 8) s[len++] = command;
  1080. if (b & 16) s[len++] = param;
  1081. }
  1082. if(addChannel && (j % m_nChannels == 1 || m_nChannels == 1))
  1083. {
  1084. ASSERT_CAN_WRITE(1);
  1085. s[len++] = 0x80;
  1086. }
  1087. ASSERT_CAN_WRITE(5);
  1088. }
  1089. if(emptyPattern && numRows == 64)
  1090. {
  1091. // Be smart when saving empty patterns!
  1092. len = 0;
  1093. }
  1094. // Reaching the limits of file format?
  1095. if(len > uint16_max)
  1096. {
  1097. AddToLog(LogWarning, MPT_UFORMAT("Warning: File format limit was reached. Some pattern data may not get written to file. (pattern {})")(pat));
  1098. len = uint16_max;
  1099. }
  1100. patHead[7] = static_cast<uint8>(len & 0xFF);
  1101. patHead[8] = static_cast<uint8>(len >> 8);
  1102. mpt::IO::Write(f, patHead);
  1103. if(len) mpt::IO::WriteRaw(f, s.data(), len);
  1104. }
  1105. #undef ASSERT_CAN_WRITE
  1106. // Check which samples are referenced by which instruments (for assigning unreferenced samples to instruments)
  1107. std::vector<bool> sampleAssigned(GetNumSamples() + 1, false);
  1108. for(INSTRUMENTINDEX ins = 1; ins <= GetNumInstruments(); ins++)
  1109. {
  1110. if(Instruments[ins] != nullptr)
  1111. {
  1112. Instruments[ins]->GetSamples(sampleAssigned);
  1113. }
  1114. }
  1115. // Writing instruments
  1116. for(INSTRUMENTINDEX ins = 1; ins <= writeInstruments; ins++)
  1117. {
  1118. XMInstrumentHeader insHeader;
  1119. std::vector<SAMPLEINDEX> samples;
  1120. if(GetNumInstruments())
  1121. {
  1122. if(Instruments[ins] != nullptr)
  1123. {
  1124. // Convert instrument
  1125. insHeader.ConvertToXM(*Instruments[ins], compatibilityExport);
  1126. samples = insHeader.instrument.GetSampleList(*Instruments[ins], compatibilityExport);
  1127. if(samples.size() > 0 && samples[0] <= GetNumSamples())
  1128. {
  1129. // Copy over auto-vibrato settings of first sample
  1130. insHeader.instrument.ApplyAutoVibratoToXM(Samples[samples[0]], GetType());
  1131. }
  1132. std::vector<SAMPLEINDEX> additionalSamples;
  1133. // Try to save "instrument-less" samples as well by adding those after the "normal" samples of our sample.
  1134. // We look for unassigned samples directly after the samples assigned to our current instrument, so if
  1135. // e.g. sample 1 is assigned to instrument 1 and samples 2 to 10 aren't assigned to any instrument,
  1136. // we will assign those to sample 1. Any samples before the first referenced sample are going to be lost,
  1137. // but hey, I wrote this mostly for preserving instrument texts in existing modules, where we shouldn't encounter this situation...
  1138. for(auto smp : samples)
  1139. {
  1140. while(++smp <= GetNumSamples()
  1141. && !sampleAssigned[smp]
  1142. && insHeader.numSamples < (compatibilityExport ? 16 : 32))
  1143. {
  1144. sampleAssigned[smp] = true; // Don't want to add this sample again.
  1145. additionalSamples.push_back(smp);
  1146. insHeader.numSamples++;
  1147. }
  1148. }
  1149. samples.insert(samples.end(), additionalSamples.begin(), additionalSamples.end());
  1150. } else
  1151. {
  1152. MemsetZero(insHeader);
  1153. }
  1154. } else
  1155. {
  1156. // Convert samples to instruments
  1157. MemsetZero(insHeader);
  1158. insHeader.numSamples = 1;
  1159. insHeader.instrument.ApplyAutoVibratoToXM(Samples[ins], GetType());
  1160. samples.push_back(ins);
  1161. }
  1162. insHeader.Finalise();
  1163. size_t insHeaderSize = insHeader.size;
  1164. mpt::IO::WritePartial(f, insHeader, insHeaderSize);
  1165. std::vector<SampleIO> sampleFlags(samples.size());
  1166. // Write Sample Headers
  1167. for(SAMPLEINDEX smp = 0; smp < samples.size(); smp++)
  1168. {
  1169. XMSample xmSample;
  1170. if(samples[smp] <= GetNumSamples())
  1171. {
  1172. xmSample.ConvertToXM(Samples[samples[smp]], GetType(), compatibilityExport);
  1173. } else
  1174. {
  1175. MemsetZero(xmSample);
  1176. }
  1177. sampleFlags[smp] = xmSample.GetSampleFormat();
  1178. mpt::String::WriteBuf(mpt::String::spacePadded, xmSample.name) = m_szNames[samples[smp]];
  1179. mpt::IO::Write(f, xmSample);
  1180. }
  1181. // Write Sample Data
  1182. for(SAMPLEINDEX smp = 0; smp < samples.size(); smp++)
  1183. {
  1184. if(samples[smp] <= GetNumSamples())
  1185. {
  1186. sampleFlags[smp].WriteSample(f, Samples[samples[smp]]);
  1187. }
  1188. }
  1189. }
  1190. if(!compatibilityExport)
  1191. {
  1192. // Writing song comments
  1193. if(!m_songMessage.empty())
  1194. {
  1195. uint32 size = mpt::saturate_cast<uint32>(m_songMessage.length());
  1196. mpt::IO::WriteRaw(f, "text", 4);
  1197. mpt::IO::WriteIntLE<uint32>(f, size);
  1198. mpt::IO::WriteRaw(f, m_songMessage.c_str(), size);
  1199. }
  1200. // Writing midi cfg
  1201. if(!m_MidiCfg.IsMacroDefaultSetupUsed())
  1202. {
  1203. mpt::IO::WriteRaw(f, "MIDI", 4);
  1204. mpt::IO::WriteIntLE<uint32>(f, sizeof(MIDIMacroConfigData));
  1205. mpt::IO::Write(f, static_cast<MIDIMacroConfigData &>(m_MidiCfg));
  1206. }
  1207. // Writing Pattern Names
  1208. const PATTERNINDEX numNamedPats = Patterns.GetNumNamedPatterns();
  1209. if(numNamedPats > 0)
  1210. {
  1211. mpt::IO::WriteRaw(f, "PNAM", 4);
  1212. mpt::IO::WriteIntLE<uint32>(f, numNamedPats * MAX_PATTERNNAME);
  1213. for(PATTERNINDEX pat = 0; pat < numNamedPats; pat++)
  1214. {
  1215. char name[MAX_PATTERNNAME];
  1216. mpt::String::WriteBuf(mpt::String::maybeNullTerminated, name) = Patterns[pat].GetName();
  1217. mpt::IO::Write(f, name);
  1218. }
  1219. }
  1220. // Writing Channel Names
  1221. {
  1222. CHANNELINDEX numNamedChannels = 0;
  1223. for(CHANNELINDEX chn = 0; chn < m_nChannels; chn++)
  1224. {
  1225. if (ChnSettings[chn].szName[0]) numNamedChannels = chn + 1;
  1226. }
  1227. // Do it!
  1228. if(numNamedChannels)
  1229. {
  1230. mpt::IO::WriteRaw(f, "CNAM", 4);
  1231. mpt::IO::WriteIntLE<uint32>(f, numNamedChannels * MAX_CHANNELNAME);
  1232. for(CHANNELINDEX chn = 0; chn < numNamedChannels; chn++)
  1233. {
  1234. char name[MAX_CHANNELNAME];
  1235. mpt::String::WriteBuf(mpt::String::maybeNullTerminated, name) = ChnSettings[chn].szName;
  1236. mpt::IO::Write(f, name);
  1237. }
  1238. }
  1239. }
  1240. //Save hacked-on extra info
  1241. SaveMixPlugins(&f);
  1242. if(GetNumInstruments())
  1243. {
  1244. SaveExtendedInstrumentProperties(writeInstruments, f);
  1245. }
  1246. SaveExtendedSongProperties(f);
  1247. }
  1248. return true;
  1249. }
  1250. #endif // MODPLUG_NO_FILESAVE
  1251. OPENMPT_NAMESPACE_END