OPLExport.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * OPLExport.cpp
  3. * -------------
  4. * Purpose: Export of OPL register dumps as VGM/VGZ or DRO files
  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 "FileDialog.h"
  11. #include "InputHandler.h"
  12. #include "Mainfrm.h"
  13. #include "Moddoc.h"
  14. #include "ProgressDialog.h"
  15. #include "../soundlib/OPL.h"
  16. #include "../soundlib/Tagging.h"
  17. #include <zlib/zlib.h>
  18. OPENMPT_NAMESPACE_BEGIN
  19. // DRO file header
  20. struct DROHeaderV1
  21. {
  22. static constexpr char droMagic[] = "DBRAWOPL";
  23. char magic[8];
  24. uint16le verHi;
  25. uint16le verLo;
  26. uint32le lengthMs;
  27. uint32le lengthBytes;
  28. uint32le hardwareType;
  29. };
  30. MPT_BINARY_STRUCT(DROHeaderV1, 24);
  31. // VGM file header
  32. struct VGMHeader
  33. {
  34. static constexpr char VgmMagic[] = "Vgm ";
  35. char magic[4];
  36. uint32le eofOffset;
  37. uint32le version;
  38. uint32le sn76489clock;
  39. uint32le ym2413clock;
  40. uint32le gd3Offset;
  41. uint32le totalNumSamples;
  42. uint32le loopOffset;
  43. uint32le loopNumSamples;
  44. uint32le rate;
  45. uint32le someChipClocks[3];
  46. uint32le vgmDataOffset;
  47. uint32le variousChipClocks[9];
  48. uint32le ymf262clock; // 14318180
  49. uint32le evenMoreChipClocks[7];
  50. uint8 volumeModifier;
  51. uint8 reserved[131]; // Various other fields we're not interested in
  52. };
  53. MPT_BINARY_STRUCT(VGMHeader, 256);
  54. // VGM metadata header
  55. struct Gd3Header
  56. {
  57. static constexpr char Gd3Magic[] = "Gd3 ";
  58. char magic[4];
  59. uint32le version;
  60. uint32le size;
  61. };
  62. MPT_BINARY_STRUCT(Gd3Header, 12);
  63. // The OPL register logger and serializer for VGM/VGZ/DRO files
  64. class OPLCapture final : public OPL::IRegisterLogger
  65. {
  66. struct RegisterDump
  67. {
  68. CSoundFile::samplecount_t sampleOffset;
  69. uint8 regLo;
  70. uint8 regHi;
  71. uint8 value;
  72. };
  73. public:
  74. OPLCapture(CSoundFile &sndFile) : m_sndFile{sndFile} {}
  75. void Reset()
  76. {
  77. m_registerDump.clear();
  78. m_prevRegisters.clear();
  79. }
  80. void CaptureAllVoiceRegisters()
  81. {
  82. for(const auto reg : OPL::AllVoiceRegisters())
  83. {
  84. uint8 value = 0;
  85. if(const auto prevValue = m_prevRegisters.find(reg); prevValue != m_prevRegisters.end())
  86. value = prevValue->second;
  87. m_registerDumpAtLoopStart[reg] = value;
  88. }
  89. }
  90. void WriteDRO(std::ostream &f) const
  91. {
  92. DROHeaderV1 header{};
  93. memcpy(header.magic, DROHeaderV1::droMagic, 8);
  94. header.verHi = 0;
  95. header.verLo = 1;
  96. header.lengthMs = Util::muldivr_unsigned(m_sndFile.GetTotalSampleCount(), 1000, m_sndFile.GetSampleRate());
  97. header.lengthBytes = 0;
  98. header.hardwareType = 1; // OPL3
  99. mpt::IO::Write(f, header);
  100. CSoundFile::samplecount_t prevOffset = 0, prevOffsetMs = 0;
  101. bool prevHigh = false;
  102. for(const auto &reg : m_registerDump)
  103. {
  104. if(reg.sampleOffset > prevOffset)
  105. {
  106. uint32 offsetMs = Util::muldivr_unsigned(reg.sampleOffset, 1000, m_sndFile.GetSampleRate());
  107. header.lengthBytes += WriteDRODelay(f, offsetMs - prevOffsetMs);
  108. prevOffset = reg.sampleOffset;
  109. prevOffsetMs = offsetMs;
  110. }
  111. if(const bool isHigh = (reg.regHi == 1); isHigh != prevHigh)
  112. {
  113. prevHigh = isHigh;
  114. mpt::IO::Write(f, mpt::as_byte(2 + reg.regHi));
  115. header.lengthBytes++;
  116. }
  117. if(reg.regLo <= 4)
  118. {
  119. mpt::IO::Write(f, mpt::as_byte(4));
  120. header.lengthBytes++;
  121. }
  122. const uint8 regValue[] = {reg.regLo, reg.value};
  123. mpt::IO::Write(f, regValue);
  124. header.lengthBytes += 2;
  125. }
  126. if(header.lengthMs > prevOffsetMs)
  127. header.lengthBytes += WriteDRODelay(f, header.lengthMs - prevOffsetMs);
  128. MPT_ASSERT(mpt::IO::TellWrite(f) == static_cast<mpt::IO::Offset>(header.lengthBytes + sizeof(header)));
  129. // AdPlug can read some metadata following the register dump, but DroTrimmer panics if it see that data.
  130. // As the metadata is very limited (40 characters per field, unknown 8-bit encoding) we'll leave that feature to the VGM export.
  131. #if 0
  132. mpt::IO::Write(f, mpt::as_byte(0xFF));
  133. mpt::IO::Write(f, mpt::as_byte(0xFF));
  134. char name[40];
  135. mpt::String::WriteBuf(mpt::String::maybeNullTerminated, name) = m_sndFile.m_songName;
  136. mpt::IO::Write(f, mpt::as_byte(0x1A));
  137. mpt::IO::Write(f, name);
  138. if(!m_sndFile.m_songArtist.empty())
  139. {
  140. mpt::String::WriteBuf(mpt::String::maybeNullTerminated, name) = mpt::ToCharset(mpt::Charset::ISO8859_1, m_sndFile.m_songArtist);
  141. mpt::IO::Write(f, mpt::as_byte(0x1B));
  142. mpt::IO::Write(f, name);
  143. }
  144. #endif
  145. mpt::IO::SeekAbsolute(f, 0);
  146. mpt::IO::Write(f, header);
  147. }
  148. void WriteVGZ(std::ostream &f, const CSoundFile::samplecount_t loopStart, const FileTags &fileTags, const mpt::ustring &filename) const
  149. {
  150. std::ostringstream outStream;
  151. WriteVGM(outStream, loopStart, fileTags);
  152. std::string outData = std::move(outStream).str();
  153. z_stream strm{};
  154. strm.avail_in = static_cast<uInt>(outData.size());
  155. strm.next_in = reinterpret_cast<Bytef *>(outData.data());
  156. if(deflateInit2(&strm, Z_BEST_COMPRESSION, Z_DEFLATED, 15 | 16, 9, Z_DEFAULT_STRATEGY) != Z_OK)
  157. throw std::runtime_error{"zlib init failed"};
  158. gz_header gzHeader{};
  159. gzHeader.time = static_cast<uLong>(time(nullptr));
  160. std::string filenameISO = mpt::ToCharset(mpt::Charset::ISO8859_1, filename);
  161. gzHeader.name = reinterpret_cast<Bytef *>(filenameISO.data());
  162. deflateSetHeader(&strm, &gzHeader);
  163. do
  164. {
  165. std::array<Bytef, mpt::IO::BUFFERSIZE_TINY> buffer;
  166. strm.avail_out = static_cast<uInt>(buffer.size());
  167. strm.next_out = buffer.data();
  168. deflate(&strm, Z_FINISH);
  169. mpt::IO::WritePartial(f, buffer, buffer.size() - strm.avail_out);
  170. } while(strm.avail_out == 0);
  171. deflateEnd(&strm);
  172. }
  173. void WriteVGM(std::ostream &f, const CSoundFile::samplecount_t loopStart, const FileTags &fileTags) const
  174. {
  175. VGMHeader header{};
  176. memcpy(header.magic, VGMHeader::VgmMagic, 4);
  177. header.version = 0x160;
  178. header.vgmDataOffset = sizeof(header) - offsetof(VGMHeader, vgmDataOffset);
  179. header.ymf262clock = 14318180;
  180. header.totalNumSamples = static_cast<uint32>(m_sndFile.GetTotalSampleCount());
  181. if(loopStart != Util::MaxValueOfType(loopStart))
  182. header.loopNumSamples = static_cast<uint32>(m_sndFile.GetTotalSampleCount() - loopStart);
  183. mpt::IO::Write(f, header);
  184. bool wroteLoopStart = (header.loopNumSamples == 0);
  185. CSoundFile::samplecount_t prevOffset = 0;
  186. for(const auto &reg : m_registerDump)
  187. {
  188. if(reg.sampleOffset >= loopStart && !wroteLoopStart)
  189. {
  190. WriteVGMDelay(f, loopStart - prevOffset);
  191. prevOffset = loopStart;
  192. header.loopOffset = static_cast<uint32>(mpt::IO::TellWrite(f) - 0x1C);
  193. wroteLoopStart = true;
  194. for(const auto & [loopReg, value] : m_registerDumpAtLoopStart)
  195. {
  196. if(m_prevRegisters.count(loopReg))
  197. {
  198. const uint8 data[] = {static_cast<uint8>(0x5E + (loopReg >> 8)), static_cast<uint8>(loopReg & 0xFF), value};
  199. mpt::IO::Write(f, data);
  200. }
  201. }
  202. }
  203. WriteVGMDelay(f, reg.sampleOffset - prevOffset);
  204. prevOffset = reg.sampleOffset;
  205. const uint8 data[] = {static_cast<uint8>(0x5E + reg.regHi), reg.regLo, reg.value};
  206. mpt::IO::Write(f, data);
  207. }
  208. WriteVGMDelay(f, m_sndFile.GetTotalSampleCount() - prevOffset);
  209. mpt::IO::Write(f, mpt::as_byte(0x66));
  210. header.gd3Offset = static_cast<uint32>(mpt::IO::TellWrite(f) - offsetof(VGMHeader, gd3Offset));
  211. const mpt::ustring tags[] =
  212. {
  213. fileTags.title,
  214. {}, // Song name JP
  215. {}, // Game name EN
  216. {}, // Game name JP
  217. Version::Current().GetOpenMPTVersionString(),
  218. {}, // System name JP
  219. fileTags.artist,
  220. {}, // Author name JP
  221. fileTags.year,
  222. {}, // Person who created the VGM file
  223. mpt::String::Replace(fileTags.comments, U_("\r\n"), U_("\n")),
  224. };
  225. std::ostringstream tagStream;
  226. for(const auto &tag : tags)
  227. {
  228. WriteVGMString(tagStream, mpt::ToWide(tag));
  229. }
  230. const auto tagsData = std::move(tagStream).str();
  231. Gd3Header gd3Header{};
  232. memcpy(gd3Header.magic, Gd3Header::Gd3Magic, 4);
  233. gd3Header.version = 0x100;
  234. gd3Header.size = static_cast<uint32>(tagsData.size());
  235. mpt::IO::Write(f, gd3Header);
  236. mpt::IO::WriteRaw(f, mpt::as_span(tagsData));
  237. header.eofOffset = static_cast<uint32>(mpt::IO::TellWrite(f) - offsetof(VGMHeader, eofOffset));
  238. mpt::IO::SeekAbsolute(f, 0);
  239. mpt::IO::Write(f, header);
  240. }
  241. private:
  242. static uint32 WriteDRODelay(std::ostream &f, uint32 delay)
  243. {
  244. uint32 bytesWritten = 0;
  245. while(delay > 256)
  246. {
  247. uint32 subDelay = std::min(delay, 65536u);
  248. mpt::IO::Write(f, mpt::as_byte(1));
  249. mpt::IO::WriteIntLE(f, static_cast<uint16>(subDelay - 1));
  250. bytesWritten += 3;
  251. delay -= subDelay;
  252. }
  253. if(delay)
  254. {
  255. mpt::IO::Write(f, mpt::as_byte(0));
  256. mpt::IO::WriteIntLE(f, static_cast<uint8>(delay - 1));
  257. bytesWritten += 2;
  258. }
  259. return bytesWritten;
  260. }
  261. static void WriteVGMDelay(std::ostream &f, CSoundFile::samplecount_t delay)
  262. {
  263. while(delay)
  264. {
  265. uint16 subDelay = mpt::saturate_cast<uint16>(delay);
  266. if(subDelay <= 16)
  267. {
  268. mpt::IO::Write(f, mpt::as_byte(0x6F + subDelay));
  269. } else if(subDelay == 735)
  270. {
  271. mpt::IO::Write(f, mpt::as_byte(0x62)); // 1/60th of a second
  272. } else if(subDelay == 882)
  273. {
  274. mpt::IO::Write(f, mpt::as_byte(0x63)); // 1/50th of a second
  275. } else
  276. {
  277. mpt::IO::Write(f, mpt::as_byte(0x61));
  278. mpt::IO::WriteIntLE(f, subDelay);
  279. }
  280. delay -= subDelay;
  281. }
  282. }
  283. static void WriteVGMString(std::ostream &f, const std::wstring &s)
  284. {
  285. std::vector<uint16le> s16le(s.length() + 1);
  286. for(size_t i = 0; i < s.length(); i++)
  287. {
  288. s16le[i] = s[i] ? s[i] : L' ';
  289. }
  290. mpt::IO::Write(f, s16le);
  291. }
  292. void Port(CHANNELINDEX, uint16 reg, uint8 value) override
  293. {
  294. if(const auto prevValue = m_prevRegisters.find(reg); prevValue != m_prevRegisters.end() && prevValue->second == value)
  295. return;
  296. m_registerDump.push_back({m_sndFile.GetTotalSampleCount(), static_cast<uint8>(reg & 0xFF), static_cast<uint8>(reg >> 8), value});
  297. m_prevRegisters[reg] = value;
  298. }
  299. std::vector<RegisterDump> m_registerDump;
  300. std::map<uint16, uint8> m_prevRegisters, m_registerDumpAtLoopStart;
  301. CSoundFile &m_sndFile;
  302. };
  303. class OPLExportDlg : public CProgressDialog
  304. {
  305. private:
  306. enum class ExportFormat
  307. {
  308. VGZ = IDC_RADIO1,
  309. VGM = IDC_RADIO2,
  310. DRO = IDC_RADIO3,
  311. };
  312. static ExportFormat s_format;
  313. OPLCapture m_oplLogger;
  314. CSoundFile &m_sndFile;
  315. CModDoc &m_modDoc;
  316. std::vector<SubSong> m_subSongs;
  317. size_t m_selectedSong = 0;
  318. bool m_conversionRunning = false;
  319. bool m_locked = true;
  320. public:
  321. OPLExportDlg(CModDoc &modDoc, CWnd *parent = nullptr)
  322. : CProgressDialog{parent, IDD_OPLEXPORT}
  323. , m_oplLogger{modDoc.GetSoundFile()}
  324. , m_sndFile{modDoc.GetSoundFile()}
  325. , m_modDoc{modDoc}
  326. , m_subSongs{modDoc.GetSoundFile().GetAllSubSongs()}
  327. {
  328. }
  329. BOOL OnInitDialog() override
  330. {
  331. CProgressDialog::OnInitDialog();
  332. CheckRadioButton(IDC_RADIO1, IDC_RADIO3, static_cast<int>(s_format));
  333. CheckRadioButton(IDC_RADIO4, IDC_RADIO5, IDC_RADIO4);
  334. static_cast<CSpinButtonCtrl *>(GetDlgItem(IDC_SPIN1))->SetRange32(1, static_cast<int>(m_subSongs.size()));
  335. SetDlgItemInt(IDC_EDIT1, static_cast<UINT>(m_selectedSong + 1), FALSE);
  336. if(m_subSongs.size() <= 1)
  337. {
  338. const int controls[] = {IDC_RADIO4, IDC_RADIO5, IDC_EDIT1, IDC_SPIN1};
  339. for(int control : controls)
  340. GetDlgItem(control)->EnableWindow(FALSE);
  341. }
  342. UpdateSubsongName();
  343. OnFormatChanged();
  344. SetDlgItemText(IDC_EDIT2, mpt::ToWin(m_sndFile.GetCharsetFile(), m_sndFile.GetTitle()).c_str());
  345. SetDlgItemText(IDC_EDIT3, mpt::ToWin(m_sndFile.m_songArtist).c_str());
  346. if(!m_sndFile.GetFileHistory().empty())
  347. SetDlgItemText(IDC_EDIT4, mpt::ToWin(mpt::String::Replace(m_sndFile.GetFileHistory().back().AsISO8601().substr(0, 10), U_("-"), U_("/"))).c_str());
  348. SetDlgItemText(IDC_EDIT5, mpt::ToWin(m_sndFile.GetCharsetFile(), m_sndFile.m_songMessage.GetFormatted(SongMessage::leCRLF)).c_str());
  349. m_locked = false;
  350. return TRUE;
  351. }
  352. void OnOK() override
  353. {
  354. mpt::PathString extension = P_("vgz");
  355. s_format = static_cast<ExportFormat>(GetCheckedRadioButton(IDC_RADIO1, IDC_RADIO3));
  356. if(s_format == ExportFormat::DRO)
  357. extension = P_("dro");
  358. else if(s_format == ExportFormat::VGM)
  359. extension = P_("vgm");
  360. FileDialog dlg = SaveFileDialog()
  361. .DefaultExtension(extension)
  362. .DefaultFilename(m_modDoc.GetPathNameMpt().GetFileName().ReplaceExt(P_(".") + extension))
  363. .ExtensionFilter(MPT_UFORMAT("{} Files|*.{}||")(mpt::ToUpperCase(extension.ToUnicode()), extension))
  364. .WorkingDirectory(TrackerSettings::Instance().PathExport.GetWorkingDir());
  365. if(!dlg.Show())
  366. {
  367. OnCancel();
  368. return;
  369. }
  370. TrackerSettings::Instance().PathExport.SetWorkingDir(dlg.GetWorkingDirectory());
  371. DoConversion(dlg.GetFirstFile());
  372. CProgressDialog::OnOK();
  373. }
  374. void OnCancel() override
  375. {
  376. if(m_conversionRunning)
  377. CProgressDialog::OnCancel();
  378. else
  379. CDialog::OnCancel();
  380. }
  381. void Run() override {}
  382. afx_msg void OnFormatChanged()
  383. {
  384. const int controls[] = {IDC_EDIT2, IDC_EDIT3, IDC_EDIT4, IDC_EDIT5};
  385. for(int control : controls)
  386. GetDlgItem(control)->EnableWindow(GetCheckedRadioButton(IDC_RADIO1, IDC_RADIO3) == static_cast<int>(ExportFormat::DRO) ? FALSE : TRUE);
  387. }
  388. afx_msg void OnSubsongChanged()
  389. {
  390. if(m_locked)
  391. return;
  392. CheckRadioButton(IDC_RADIO4, IDC_RADIO5, IDC_RADIO5);
  393. BOOL ok = FALSE;
  394. const auto newSubSong = std::clamp(static_cast<size_t>(GetDlgItemInt(IDC_EDIT1, &ok, FALSE)), size_t(1), m_subSongs.size()) - 1;
  395. if(m_selectedSong == newSubSong || !ok)
  396. return;
  397. m_selectedSong = newSubSong;
  398. UpdateSubsongName();
  399. }
  400. void UpdateSubsongName()
  401. {
  402. const auto subsongText = GetDlgItem(IDC_SUBSONG);
  403. if(subsongText == nullptr || m_selectedSong >= m_subSongs.size())
  404. return;
  405. const auto &song = m_subSongs[m_selectedSong];
  406. const auto sequenceName = m_sndFile.Order(song.sequence).GetName();
  407. const auto startPattern = m_sndFile.Order(song.sequence).PatternAt(song.startOrder);
  408. const auto orderName = startPattern ? startPattern->GetName() : std::string{};
  409. subsongText->SetWindowText(MPT_TFORMAT("Sequence {}{}\nOrder {} to {}{}")(
  410. song.sequence + 1,
  411. sequenceName.empty() ? mpt::tstring{} : MPT_TFORMAT(" ({})")(sequenceName),
  412. song.startOrder,
  413. song.endOrder,
  414. orderName.empty() ? mpt::tstring{} : MPT_TFORMAT(" ({})")(mpt::ToWin(m_sndFile.GetCharsetInternal(), orderName)))
  415. .c_str());
  416. }
  417. void DoConversion(const mpt::PathString &fileName)
  418. {
  419. const int controls[] = {IDC_RADIO1, IDC_RADIO2, IDC_RADIO3, IDC_RADIO4, IDC_RADIO5, IDC_EDIT1, IDC_EDIT2, IDC_EDIT3, IDC_EDIT4, IDC_EDIT5, IDC_SPIN1, IDOK};
  420. for(int control : controls)
  421. GetDlgItem(control)->EnableWindow(FALSE);
  422. BypassInputHandler bih;
  423. CMainFrame::GetMainFrame()->StopMod(&m_modDoc);
  424. FileTags fileTags;
  425. {
  426. CString title, artist, date, notes;
  427. GetDlgItemText(IDC_EDIT2, title);
  428. GetDlgItemText(IDC_EDIT3, artist);
  429. GetDlgItemText(IDC_EDIT4, date);
  430. GetDlgItemText(IDC_EDIT5, notes);
  431. fileTags.title = mpt::ToUnicode(title);
  432. fileTags.artist = mpt::ToUnicode(artist);
  433. fileTags.year = mpt::ToUnicode(date);
  434. fileTags.comments = mpt::ToUnicode(notes);
  435. }
  436. if(IsDlgButtonChecked(IDC_RADIO5))
  437. m_subSongs = {m_subSongs[m_selectedSong]};
  438. SetRange(0, mpt::saturate_round<uint64>(std::accumulate(m_subSongs.begin(), m_subSongs.end(), 0.0, [](double acc, const auto &song) { return acc + song.duration; }) * m_sndFile.GetSampleRate()));
  439. GetDlgItem(IDC_PROGRESS1)->ShowWindow(SW_SHOW);
  440. m_sndFile.m_bIsRendering = true;
  441. const auto origSettings = m_sndFile.m_MixerSettings;
  442. auto newSettings = m_sndFile.m_MixerSettings;
  443. if(s_format != ExportFormat::DRO)
  444. newSettings.gdwMixingFreq = 44100; // required for VGM, DRO doesn't care
  445. m_sndFile.SetMixerSettings(newSettings);
  446. const auto origSequence = m_sndFile.Order.GetCurrentSequenceIndex();
  447. const auto origRepeatCount = m_sndFile.GetRepeatCount();
  448. m_sndFile.SetRepeatCount(0);
  449. auto opl = std::move(m_sndFile.m_opl);
  450. const auto songIndexFmt = mpt::FormatSpec{}.Dec().FillNul().Width(1 + static_cast<int>(std::log10(m_subSongs.size())));
  451. size_t totalSamples = 0;
  452. for(size_t i = 0; i < m_subSongs.size() && !m_abort; i++)
  453. {
  454. const auto &song = m_subSongs[i];
  455. m_sndFile.ResetPlayPos();
  456. m_sndFile.GetLength(eAdjust, GetLengthTarget(song.startOrder, song.startRow).StartPos(song.sequence, 0, 0));
  457. m_sndFile.m_SongFlags.reset(SONG_PLAY_FLAGS);
  458. m_oplLogger.Reset();
  459. m_sndFile.m_opl = std::make_unique<OPL>(m_oplLogger);
  460. auto prevTime = timeGetTime();
  461. CSoundFile::samplecount_t loopStart = std::numeric_limits<CSoundFile::samplecount_t>::max(), subsongSamples = 0;
  462. while(!m_abort)
  463. {
  464. auto count = m_sndFile.ReadOneTick();
  465. if(count == 0)
  466. break;
  467. if(loopStart == Util::MaxValueOfType(loopStart)
  468. && m_sndFile.m_PlayState.m_nCurrentOrder == song.loopStartOrder && m_sndFile.m_PlayState.m_nRow == song.loopStartRow
  469. && (song.loopStartOrder != song.startOrder || song.loopStartRow != song.startRow))
  470. {
  471. loopStart = subsongSamples;
  472. m_oplLogger.CaptureAllVoiceRegisters(); // Make sure all registers are in the correct state when looping back
  473. }
  474. totalSamples += count;
  475. subsongSamples += count;
  476. auto currentTime = timeGetTime();
  477. if(currentTime - prevTime >= 16)
  478. {
  479. prevTime = currentTime;
  480. auto timeSec = subsongSamples / m_sndFile.GetSampleRate();
  481. SetWindowText(MPT_TFORMAT("Exporting Song {} / {}... {}:{}:{}")(i + 1, m_subSongs.size(), timeSec / 3600, mpt::cfmt::dec0<2>((timeSec / 60) % 60), mpt::cfmt::dec0<2>(timeSec % 60)).c_str());
  482. SetProgress(totalSamples);
  483. ProcessMessages();
  484. }
  485. }
  486. if(m_sndFile.m_SongFlags[SONG_BREAKTOROW] && loopStart == Util::MaxValueOfType(loopStart) && song.loopStartOrder == song.startOrder && song.loopStartRow == song.startRow)
  487. loopStart = 0;
  488. mpt::PathString currentFileName = fileName;
  489. if(m_subSongs.size() > 1)
  490. currentFileName = fileName.ReplaceExt(mpt::PathString::FromNative(MPT_TFORMAT(" ({})")(mpt::ufmt::fmt(i + 1, songIndexFmt))) + fileName.GetFileExt());
  491. mpt::SafeOutputFile sf(currentFileName, std::ios::binary, mpt::FlushModeFromBool(TrackerSettings::Instance().MiscFlushFileBuffersOnSave));
  492. mpt::ofstream &f = sf;
  493. try
  494. {
  495. if(!f)
  496. throw std::exception{};
  497. f.exceptions(f.exceptions() | std::ios::badbit | std::ios::failbit);
  498. if(s_format == ExportFormat::DRO)
  499. m_oplLogger.WriteDRO(f);
  500. else if(s_format == ExportFormat::VGM)
  501. m_oplLogger.WriteVGM(f, loopStart, fileTags);
  502. else
  503. m_oplLogger.WriteVGZ(f, loopStart, fileTags, currentFileName.ReplaceExt(P_(".vgm")).GetFullFileName().ToUnicode());
  504. } catch(const std::exception &)
  505. {
  506. Reporting::Error(MPT_UFORMAT("Unable to write to file {}!")(currentFileName));
  507. break;
  508. }
  509. }
  510. // Reset globals to previous values
  511. m_sndFile.m_opl = std::move(opl);
  512. m_sndFile.SetRepeatCount(origRepeatCount);
  513. m_sndFile.Order.SetSequence(origSequence);
  514. m_sndFile.ResetPlayPos();
  515. m_sndFile.SetMixerSettings(origSettings);
  516. m_sndFile.m_bIsRendering = false;
  517. }
  518. DECLARE_MESSAGE_MAP()
  519. };
  520. OPLExportDlg::ExportFormat OPLExportDlg::s_format = OPLExportDlg::ExportFormat::VGZ;
  521. BEGIN_MESSAGE_MAP(OPLExportDlg, CDialog)
  522. //{{AFX_MSG_MAP(OPLExportDlg)
  523. ON_COMMAND(IDC_RADIO1, &OPLExportDlg::OnFormatChanged)
  524. ON_COMMAND(IDC_RADIO2, &OPLExportDlg::OnFormatChanged)
  525. ON_COMMAND(IDC_RADIO3, &OPLExportDlg::OnFormatChanged)
  526. ON_EN_CHANGE(IDC_EDIT1, &OPLExportDlg::OnSubsongChanged)
  527. //}}AFX_MSG_MAP
  528. END_MESSAGE_MAP()
  529. void CModDoc::OnFileOPLExport()
  530. {
  531. bool anyOPL = false;
  532. for(SAMPLEINDEX smp = 1; smp <= GetNumSamples(); smp++)
  533. {
  534. if(m_SndFile.GetSample(smp).uFlags[CHN_ADLIB])
  535. {
  536. anyOPL = true;
  537. break;
  538. }
  539. }
  540. if(!anyOPL)
  541. {
  542. Reporting::Information(_T("This module does not use any OPL instruments."), _T("No OPL Instruments Found"));
  543. return;
  544. }
  545. OPLExportDlg dlg{*this, CMainFrame::GetMainFrame()};
  546. dlg.DoModal();
  547. }
  548. OPENMPT_NAMESPACE_END