1
0

libopenmpt_impl.cpp 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162
  1. /*
  2. * libopenmpt_impl.cpp
  3. * -------------------
  4. * Purpose: libopenmpt private interface implementation
  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 "common/stdafx.h"
  10. #include "libopenmpt_internal.h"
  11. #include "libopenmpt.hpp"
  12. #include "libopenmpt_impl.hpp"
  13. #include <algorithm>
  14. #include <iostream>
  15. #include <istream>
  16. #include <iterator>
  17. #include <limits>
  18. #include <ostream>
  19. #include <cmath>
  20. #include <cstdlib>
  21. #include <cstring>
  22. #include "mpt/audio/span.hpp"
  23. #include "mpt/base/algorithm.hpp"
  24. #include "mpt/base/saturate_cast.hpp"
  25. #include "mpt/base/saturate_round.hpp"
  26. #include "mpt/format/default_integer.hpp"
  27. #include "mpt/format/default_floatingpoint.hpp"
  28. #include "mpt/format/default_string.hpp"
  29. #include "mpt/io_read/callbackstream.hpp"
  30. #include "mpt/io_read/filecursor_callbackstream.hpp"
  31. #include "mpt/io_read/filecursor_memory.hpp"
  32. #include "mpt/io_read/filecursor_stdstream.hpp"
  33. #include "mpt/mutex/mutex.hpp"
  34. #include "mpt/parse/parse.hpp"
  35. #include "mpt/string/types.hpp"
  36. #include "mpt/string/utility.hpp"
  37. #include "mpt/string_transcode/transcode.hpp"
  38. #include "common/version.h"
  39. #include "common/misc_util.h"
  40. #include "common/Dither.h"
  41. #include "common/FileReader.h"
  42. #include "common/Logging.h"
  43. #include "soundlib/Sndfile.h"
  44. #include "soundlib/mod_specifications.h"
  45. #include "soundlib/AudioReadTarget.h"
  46. #if MPT_OS_WINDOWS && MPT_OS_WINDOWS_WINRT
  47. #include <windows.h>
  48. #endif // MPT_OS_WINDOWS && MPT_OS_WINDOWS_WINRT
  49. OPENMPT_NAMESPACE_BEGIN
  50. #if !defined(MPT_BUILD_SILENCE_LIBOPENMPT_CONFIGURATION_WARNINGS)
  51. #if MPT_OS_WINDOWS && MPT_OS_WINDOWS_WINRT
  52. #if defined(NTDDI_VERSION)
  53. #if (NTDDI_VERSION < 0x06020000)
  54. MPT_WARNING("Warning: libopenmpt for WinRT is built with reduced functionality. Please #define NTDDI_VERSION 0x0602000.")
  55. #endif
  56. #elif defined(_WIN32_WINNT)
  57. #if (_WIN32_WINNT < 0x0602)
  58. MPT_WARNING("Warning: libopenmpt for WinRT is built with reduced functionality. Please #define _WIN32_WINNT 0x0602.")
  59. #endif // _WIN32_WINNT
  60. #endif // _WIN32_WINNT
  61. #endif // MPT_OS_WINDOWS && MPT_OS_WINDOWS_WINRT
  62. #if defined(MPT_BUILD_MSVC) || defined(MPT_BUILD_VCPKG)
  63. #if MPT_OS_WINDOWS_WINRT
  64. #pragma comment(lib, "ole32.lib")
  65. #else
  66. #pragma comment(lib, "rpcrt4.lib")
  67. #endif
  68. #endif // MPT_BUILD_MSVC
  69. #if MPT_PLATFORM_MULTITHREADED && MPT_MUTEX_NONE
  70. MPT_WARNING("Warning: libopenmpt built in non thread-safe mode because mutexes are not supported by the C++ standard library available.")
  71. #endif // MPT_MUTEX_NONE
  72. #if (defined(__MINGW32__) || defined(__MINGW64__)) && !defined(_GLIBCXX_HAS_GTHREADS)
  73. #if defined(MPT_WITH_MINGWSTDTHREADS)
  74. MPT_WARNING("Warning: Building with mingw-std-threads is deprecated because this is not supported with GCC 11 or later.")
  75. #else // !MINGWSTDTHREADS
  76. MPT_WARNING("Warning: Platform (Windows) supports multi-threading, however the toolchain (MinGW/GCC) does not. The resulting libopenmpt may not be thread-safe. This is a MinGW/GCC issue. You can avoid this warning by using a MinGW toolchain built with posix threading model as opposed to win32 threading model.")
  77. #endif // MINGWSTDTHREADS
  78. #endif // MINGW
  79. #if MPT_CLANG_AT_LEAST(5,0,0) && MPT_CLANG_BEFORE(11,0,0) && defined(__powerpc__) && !defined(__powerpc64__)
  80. MPT_WARNING("Warning: libopenmpt is known to trigger bad code generation with Clang 5..10 on powerpc (32bit) when using -O3. See <https://bugs.llvm.org/show_bug.cgi?id=46683>.")
  81. #endif
  82. #endif // !MPT_BUILD_SILENCE_LIBOPENMPT_CONFIGURATION_WARNINGS
  83. #if defined(MPT_ASSERT_HANDLER_NEEDED) && !defined(ENABLE_TESTS)
  84. MPT_NOINLINE void AssertHandler(const mpt::source_location &loc, const char *expr, const char *msg) {
  85. if(msg) {
  86. mpt::log::GlobalLogger().SendLogMessage(loc, LogError, "ASSERT",
  87. MPT_USTRING("ASSERTION FAILED: ") + mpt::ToUnicode(mpt::CharsetSource, msg) + MPT_USTRING(" (") + mpt::ToUnicode(mpt::CharsetSource, expr) + MPT_USTRING(")")
  88. );
  89. } else {
  90. mpt::log::GlobalLogger().SendLogMessage(loc, LogError, "ASSERT",
  91. MPT_USTRING("ASSERTION FAILED: ") + mpt::ToUnicode(mpt::CharsetSource, expr)
  92. );
  93. }
  94. #if defined(MPT_BUILD_FATAL_ASSERTS)
  95. std::abort();
  96. #endif // MPT_BUILD_FATAL_ASSERTS
  97. }
  98. #endif // MPT_ASSERT_HANDLER_NEEDED && !ENABLE_TESTS
  99. OPENMPT_NAMESPACE_END
  100. // assume OPENMPT_NAMESPACE is OpenMPT
  101. namespace openmpt {
  102. namespace version {
  103. std::uint32_t get_library_version() {
  104. return OPENMPT_API_VERSION;
  105. }
  106. std::uint32_t get_core_version() {
  107. return OpenMPT::Version::Current().GetRawVersion();
  108. }
  109. static std::string get_library_version_string() {
  110. std::string str;
  111. const OpenMPT::SourceInfo sourceInfo = OpenMPT::SourceInfo::Current();
  112. str += mpt::format_value_default<std::string>(OPENMPT_API_VERSION_MAJOR);
  113. str += ".";
  114. str += mpt::format_value_default<std::string>(OPENMPT_API_VERSION_MINOR);
  115. str += ".";
  116. str += mpt::format_value_default<std::string>(OPENMPT_API_VERSION_PATCH);
  117. if ( std::string(OPENMPT_API_VERSION_PREREL).length() > 0 ) {
  118. str += OPENMPT_API_VERSION_PREREL;
  119. }
  120. std::vector<std::string> fields;
  121. if ( sourceInfo.Revision() ) {
  122. fields.push_back( "r" + mpt::format_value_default<std::string>( sourceInfo.Revision() ) );
  123. }
  124. if ( sourceInfo.IsDirty() ) {
  125. fields.push_back( "modified" );
  126. } else if ( sourceInfo.HasMixedRevisions() ) {
  127. fields.push_back( "mixed" );
  128. }
  129. if ( sourceInfo.IsPackage() ) {
  130. fields.push_back( "pkg" );
  131. }
  132. if ( !fields.empty() ) {
  133. str += "+";
  134. str += OpenMPT::mpt::String::Combine( fields, std::string(".") );
  135. }
  136. return str;
  137. }
  138. static std::string get_library_features_string() {
  139. return mpt::transcode<std::string>( mpt::common_encoding::utf8, mpt::trim(OpenMPT::Build::GetBuildFeaturesString()));
  140. }
  141. static std::string get_core_version_string() {
  142. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::Build::GetVersionStringExtended());
  143. }
  144. static std::string get_source_url_string() {
  145. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::SourceInfo::Current().GetUrlWithRevision());
  146. }
  147. static std::string get_source_date_string() {
  148. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::SourceInfo::Current().Date());
  149. }
  150. static std::string get_source_revision_string() {
  151. const OpenMPT::SourceInfo sourceInfo = OpenMPT::SourceInfo::Current();
  152. return sourceInfo.Revision() ? mpt::format_value_default<std::string>(sourceInfo.Revision()) : std::string();
  153. }
  154. static std::string get_build_string() {
  155. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::Build::GetBuildDateString());
  156. }
  157. static std::string get_build_compiler_string() {
  158. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::Build::GetBuildCompilerString());
  159. }
  160. static std::string get_credits_string() {
  161. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::Build::GetFullCreditsString());
  162. }
  163. static std::string get_contact_string() {
  164. return mpt::transcode<std::string>( mpt::common_encoding::utf8, MPT_USTRING("Forum: ") + OpenMPT::Build::GetURL(OpenMPT::Build::Url::Forum));
  165. }
  166. static std::string get_license_string() {
  167. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::Build::GetLicenseString());
  168. }
  169. static std::string get_url_string() {
  170. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::Build::GetURL(OpenMPT::Build::Url::Website));
  171. }
  172. static std::string get_support_forum_url_string() {
  173. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::Build::GetURL(OpenMPT::Build::Url::Forum));
  174. }
  175. static std::string get_bugtracker_url_string() {
  176. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::Build::GetURL(OpenMPT::Build::Url::Bugtracker));
  177. }
  178. std::string get_string( const std::string & key ) {
  179. if ( key == "" ) {
  180. return std::string();
  181. } else if ( key == "library_version" ) {
  182. return get_library_version_string();
  183. } else if ( key == "library_version_major" ) {
  184. return mpt::format_value_default<std::string>(OPENMPT_API_VERSION_MAJOR);
  185. } else if ( key == "library_version_minor" ) {
  186. return mpt::format_value_default<std::string>(OPENMPT_API_VERSION_MINOR);
  187. } else if ( key == "library_version_patch" ) {
  188. return mpt::format_value_default<std::string>(OPENMPT_API_VERSION_PATCH);
  189. } else if ( key == "library_version_prerel" ) {
  190. return mpt::format_value_default<std::string>(OPENMPT_API_VERSION_PREREL);
  191. } else if ( key == "library_version_is_release" ) {
  192. return ( std::string(OPENMPT_API_VERSION_PREREL).length() == 0 ) ? "1" : "0";
  193. } else if ( key == "library_features" ) {
  194. return get_library_features_string();
  195. } else if ( key == "core_version" ) {
  196. return get_core_version_string();
  197. } else if ( key == "source_url" ) {
  198. return get_source_url_string();
  199. } else if ( key == "source_date" ) {
  200. return get_source_date_string();
  201. } else if ( key == "source_revision" ) {
  202. return get_source_revision_string();
  203. } else if ( key == "source_is_modified" ) {
  204. return OpenMPT::SourceInfo::Current().IsDirty() ? "1" : "0";
  205. } else if ( key == "source_has_mixed_revision" ) {
  206. return OpenMPT::SourceInfo::Current().HasMixedRevisions() ? "1" : "0";
  207. } else if ( key == "source_is_package" ) {
  208. return OpenMPT::SourceInfo::Current().IsPackage() ? "1" : "0";
  209. } else if ( key == "build" ) {
  210. return get_build_string();
  211. } else if ( key == "build_compiler" ) {
  212. return get_build_compiler_string();
  213. } else if ( key == "credits" ) {
  214. return get_credits_string();
  215. } else if ( key == "contact" ) {
  216. return get_contact_string();
  217. } else if ( key == "license" ) {
  218. return get_license_string();
  219. } else if ( key == "url" ) {
  220. return get_url_string();
  221. } else if ( key == "support_forum_url" ) {
  222. return get_support_forum_url_string();
  223. } else if ( key == "bugtracker_url" ) {
  224. return get_bugtracker_url_string();
  225. } else {
  226. return std::string();
  227. }
  228. }
  229. } // namespace version
  230. log_interface::log_interface() {
  231. return;
  232. }
  233. log_interface::~log_interface() {
  234. return;
  235. }
  236. std_ostream_log::std_ostream_log( std::ostream & dst ) : destination(dst) {
  237. return;
  238. }
  239. std_ostream_log::~std_ostream_log() {
  240. return;
  241. }
  242. void std_ostream_log::log( const std::string & message ) const {
  243. destination.flush();
  244. destination << message << std::endl;
  245. destination.flush();
  246. }
  247. class log_forwarder : public OpenMPT::ILog {
  248. private:
  249. log_interface & destination;
  250. public:
  251. log_forwarder( log_interface & dest ) : destination(dest) {
  252. return;
  253. }
  254. private:
  255. void AddToLog( OpenMPT::LogLevel level, const mpt::ustring & text ) const override {
  256. destination.log( mpt::transcode<std::string>( mpt::common_encoding::utf8, LogLevelToString( level ) + MPT_USTRING(": ") + text ) );
  257. }
  258. }; // class log_forwarder
  259. class loader_log : public OpenMPT::ILog {
  260. private:
  261. mutable std::vector<std::pair<OpenMPT::LogLevel,std::string> > m_Messages;
  262. public:
  263. std::vector<std::pair<OpenMPT::LogLevel,std::string> > GetMessages() const;
  264. private:
  265. void AddToLog( OpenMPT::LogLevel level, const mpt::ustring & text ) const override;
  266. }; // class loader_log
  267. std::vector<std::pair<OpenMPT::LogLevel,std::string> > loader_log::GetMessages() const {
  268. return m_Messages;
  269. }
  270. void loader_log::AddToLog( OpenMPT::LogLevel level, const mpt::ustring & text ) const {
  271. m_Messages.push_back( std::make_pair( level, mpt::transcode<std::string>( mpt::common_encoding::utf8, text ) ) );
  272. }
  273. void module_impl::PushToCSoundFileLog( const std::string & text ) const {
  274. m_sndFile->AddToLog( OpenMPT::LogError, mpt::transcode<mpt::ustring>( mpt::common_encoding::utf8, text ) );
  275. }
  276. void module_impl::PushToCSoundFileLog( int loglevel, const std::string & text ) const {
  277. m_sndFile->AddToLog( static_cast<OpenMPT::LogLevel>( loglevel ), mpt::transcode<mpt::ustring>( mpt::common_encoding::utf8, text ) );
  278. }
  279. module_impl::subsong_data::subsong_data( double duration, std::int32_t start_row, std::int32_t start_order, std::int32_t sequence )
  280. : duration(duration)
  281. , start_row(start_row)
  282. , start_order(start_order)
  283. , sequence(sequence)
  284. {
  285. return;
  286. }
  287. static OpenMPT::ResamplingMode filterlength_to_resamplingmode(std::int32_t length) {
  288. OpenMPT::ResamplingMode result = OpenMPT::SRCMODE_SINC8LP;
  289. if ( length == 0 ) {
  290. result = OpenMPT::SRCMODE_SINC8LP;
  291. } else if ( length >= 8 ) {
  292. result = OpenMPT::SRCMODE_SINC8LP;
  293. } else if ( length >= 3 ) {
  294. result = OpenMPT::SRCMODE_CUBIC;
  295. } else if ( length >= 2 ) {
  296. result = OpenMPT::SRCMODE_LINEAR;
  297. } else if ( length >= 1 ) {
  298. result = OpenMPT::SRCMODE_NEAREST;
  299. } else {
  300. throw openmpt::exception("negative filter length");
  301. }
  302. return result;
  303. }
  304. static std::int32_t resamplingmode_to_filterlength(OpenMPT::ResamplingMode mode) {
  305. switch ( mode ) {
  306. case OpenMPT::SRCMODE_NEAREST:
  307. return 1;
  308. break;
  309. case OpenMPT::SRCMODE_LINEAR:
  310. return 2;
  311. break;
  312. case OpenMPT::SRCMODE_CUBIC:
  313. return 4;
  314. break;
  315. case OpenMPT::SRCMODE_SINC8:
  316. case OpenMPT::SRCMODE_SINC8LP:
  317. case OpenMPT::SRCMODE_DEFAULT:
  318. return 8;
  319. default:
  320. throw openmpt::exception("unknown interpolation filter length set internally");
  321. break;
  322. }
  323. }
  324. template < typename sample_type >
  325. static inline std::size_t valid_channels( sample_type * const * buffers, std::size_t max_channels ) {
  326. std::size_t channel;
  327. for ( channel = 0; channel < max_channels; ++channel ) {
  328. if ( !buffers[ channel ] ) {
  329. break;
  330. }
  331. }
  332. return channel;
  333. }
  334. static OpenMPT::Resampling::AmigaFilter translate_amiga_filter_type( module_impl::amiga_filter_type amiga_type ) {
  335. switch (amiga_type ) {
  336. case module_impl::amiga_filter_type::a500:
  337. return OpenMPT::Resampling::AmigaFilter::A500;
  338. case module_impl::amiga_filter_type::a1200:
  339. case module_impl::amiga_filter_type::auto_filter:
  340. default:
  341. return OpenMPT::Resampling::AmigaFilter::A1200;
  342. case module_impl::amiga_filter_type::unfiltered:
  343. return OpenMPT::Resampling::AmigaFilter::Unfiltered;
  344. }
  345. }
  346. static void ramping_to_mixersettings( OpenMPT::MixerSettings & settings, int ramping ) {
  347. if ( ramping == -1 ) {
  348. settings.SetVolumeRampUpMicroseconds( OpenMPT::MixerSettings().GetVolumeRampUpMicroseconds() );
  349. settings.SetVolumeRampDownMicroseconds( OpenMPT::MixerSettings().GetVolumeRampDownMicroseconds() );
  350. } else if ( ramping <= 0 ) {
  351. settings.SetVolumeRampUpMicroseconds( 0 );
  352. settings.SetVolumeRampDownMicroseconds( 0 );
  353. } else {
  354. settings.SetVolumeRampUpMicroseconds( ramping * 1000 );
  355. settings.SetVolumeRampDownMicroseconds( ramping * 1000 );
  356. }
  357. }
  358. static void mixersettings_to_ramping( int & ramping, const OpenMPT::MixerSettings & settings ) {
  359. std::int32_t ramp_us = std::max( settings.GetVolumeRampUpMicroseconds(), settings.GetVolumeRampDownMicroseconds() );
  360. if ( ( settings.GetVolumeRampUpMicroseconds() == OpenMPT::MixerSettings().GetVolumeRampUpMicroseconds() ) && ( settings.GetVolumeRampDownMicroseconds() == OpenMPT::MixerSettings().GetVolumeRampDownMicroseconds() ) ) {
  361. ramping = -1;
  362. } else if ( ramp_us <= 0 ) {
  363. ramping = 0;
  364. } else {
  365. ramping = ( ramp_us + 500 ) / 1000;
  366. }
  367. }
  368. std::string module_impl::mod_string_to_utf8( const std::string & encoded ) const {
  369. return OpenMPT::mpt::ToCharset( OpenMPT::mpt::Charset::UTF8, m_sndFile->GetCharsetInternal(), encoded );
  370. }
  371. void module_impl::apply_mixer_settings( std::int32_t samplerate, int channels ) {
  372. bool samplerate_changed = static_cast<std::int32_t>( m_sndFile->m_MixerSettings.gdwMixingFreq ) != samplerate;
  373. bool channels_changed = static_cast<int>( m_sndFile->m_MixerSettings.gnChannels ) != channels;
  374. if ( samplerate_changed || channels_changed ) {
  375. OpenMPT::MixerSettings mixersettings = m_sndFile->m_MixerSettings;
  376. std::int32_t volrampin_us = mixersettings.GetVolumeRampUpMicroseconds();
  377. std::int32_t volrampout_us = mixersettings.GetVolumeRampDownMicroseconds();
  378. mixersettings.gdwMixingFreq = samplerate;
  379. mixersettings.gnChannels = channels;
  380. mixersettings.SetVolumeRampUpMicroseconds( volrampin_us );
  381. mixersettings.SetVolumeRampDownMicroseconds( volrampout_us );
  382. m_sndFile->SetMixerSettings( mixersettings );
  383. } else if ( !m_mixer_initialized ) {
  384. m_sndFile->InitPlayer( true );
  385. }
  386. if ( samplerate_changed ) {
  387. m_sndFile->SuspendPlugins();
  388. m_sndFile->ResumePlugins();
  389. }
  390. m_mixer_initialized = true;
  391. }
  392. void module_impl::apply_libopenmpt_defaults() {
  393. set_render_param( module::RENDER_STEREOSEPARATION_PERCENT, 100 );
  394. m_sndFile->Order.SetSequence( 0 );
  395. }
  396. module_impl::subsongs_type module_impl::get_subsongs() const {
  397. std::vector<subsong_data> subsongs;
  398. if ( m_sndFile->Order.GetNumSequences() == 0 ) {
  399. throw openmpt::exception("module contains no songs");
  400. }
  401. for ( OpenMPT::SEQUENCEINDEX seq = 0; seq < m_sndFile->Order.GetNumSequences(); ++seq ) {
  402. const std::vector<OpenMPT::GetLengthType> lengths = m_sndFile->GetLength( OpenMPT::eNoAdjust, OpenMPT::GetLengthTarget( true ).StartPos( seq, 0, 0 ) );
  403. for ( const auto & l : lengths ) {
  404. subsongs.push_back( subsong_data( l.duration, l.startRow, l.startOrder, seq ) );
  405. }
  406. }
  407. return subsongs;
  408. }
  409. void module_impl::init_subsongs( subsongs_type & subsongs ) const {
  410. subsongs = get_subsongs();
  411. }
  412. bool module_impl::has_subsongs_inited() const {
  413. return !m_subsongs.empty();
  414. }
  415. void module_impl::ctor( const std::map< std::string, std::string > & ctls ) {
  416. m_sndFile = std::make_unique<OpenMPT::CSoundFile>();
  417. m_loaded = false;
  418. m_mixer_initialized = false;
  419. m_Dithers = std::make_unique<OpenMPT::DithersWrapperOpenMPT>( OpenMPT::mpt::global_prng(), OpenMPT::DithersWrapperOpenMPT::DefaultDither, 4 );
  420. m_LogForwarder = std::make_unique<log_forwarder>( *m_Log );
  421. m_sndFile->SetCustomLog( m_LogForwarder.get() );
  422. m_current_subsong = 0;
  423. m_currentPositionSeconds = 0.0;
  424. m_Gain = 1.0f;
  425. m_ctl_play_at_end = song_end_action::fadeout_song;
  426. m_ctl_load_skip_samples = false;
  427. m_ctl_load_skip_patterns = false;
  428. m_ctl_load_skip_plugins = false;
  429. m_ctl_load_skip_subsongs_init = false;
  430. m_ctl_seek_sync_samples = false;
  431. // init member variables that correspond to ctls
  432. for ( const auto & ctl : ctls ) {
  433. ctl_set( ctl.first, ctl.second, false );
  434. }
  435. }
  436. void module_impl::load( const OpenMPT::FileCursor & file, const std::map< std::string, std::string > & ctls ) {
  437. loader_log loaderlog;
  438. m_sndFile->SetCustomLog( &loaderlog );
  439. {
  440. int load_flags = OpenMPT::CSoundFile::loadCompleteModule;
  441. if ( m_ctl_load_skip_samples ) {
  442. load_flags &= ~OpenMPT::CSoundFile::loadSampleData;
  443. }
  444. if ( m_ctl_load_skip_patterns ) {
  445. load_flags &= ~OpenMPT::CSoundFile::loadPatternData;
  446. }
  447. if ( m_ctl_load_skip_plugins ) {
  448. load_flags &= ~(OpenMPT::CSoundFile::loadPluginData | OpenMPT::CSoundFile::loadPluginInstance);
  449. }
  450. if ( !m_sndFile->Create( file, static_cast<OpenMPT::CSoundFile::ModLoadingFlags>( load_flags ) ) ) {
  451. throw openmpt::exception("error loading file");
  452. }
  453. if ( !m_ctl_load_skip_subsongs_init ) {
  454. init_subsongs( m_subsongs );
  455. }
  456. m_loaded = true;
  457. }
  458. m_sndFile->SetCustomLog( m_LogForwarder.get() );
  459. std::vector<std::pair<OpenMPT::LogLevel,std::string> > loaderMessages = loaderlog.GetMessages();
  460. for ( const auto & msg : loaderMessages ) {
  461. PushToCSoundFileLog( msg.first, msg.second );
  462. m_loaderMessages.push_back( mpt::transcode<std::string>( mpt::common_encoding::utf8, LogLevelToString( msg.first ) ) + std::string(": ") + msg.second );
  463. }
  464. // init CSoundFile state that corresponds to ctls
  465. for ( const auto & ctl : ctls ) {
  466. ctl_set( ctl.first, ctl.second, false );
  467. }
  468. }
  469. bool module_impl::is_loaded() const {
  470. return m_loaded;
  471. }
  472. std::size_t module_impl::read_wrapper( std::size_t count, std::int16_t * left, std::int16_t * right, std::int16_t * rear_left, std::int16_t * rear_right ) {
  473. m_sndFile->ResetMixStat();
  474. m_sndFile->m_bIsRendering = ( m_ctl_play_at_end != song_end_action::fadeout_song );
  475. std::size_t count_read = 0;
  476. std::int16_t * const buffers[4] = { left, right, rear_left, rear_right };
  477. OpenMPT::AudioTargetBufferWithGain<mpt::audio_span_planar<std::int16_t>> target( mpt::audio_span_planar<std::int16_t>( buffers, valid_channels( buffers, std::size( buffers ) ), count ), *m_Dithers, m_Gain );
  478. while ( count > 0 ) {
  479. std::size_t count_chunk = m_sndFile->Read(
  480. static_cast<OpenMPT::CSoundFile::samplecount_t>( std::min( static_cast<std::uint64_t>( count ), static_cast<std::uint64_t>( std::numeric_limits<OpenMPT::CSoundFile::samplecount_t>::max() / 2 / 4 / 4 ) ) ), // safety margin / samplesize / channels
  481. target
  482. );
  483. if ( count_chunk == 0 ) {
  484. break;
  485. }
  486. count -= count_chunk;
  487. count_read += count_chunk;
  488. }
  489. if ( count_read == 0 && m_ctl_play_at_end == song_end_action::continue_song ) {
  490. // This is the song end, but allow the song or loop to restart on the next call
  491. m_sndFile->m_SongFlags.reset(OpenMPT::SONG_ENDREACHED);
  492. }
  493. return count_read;
  494. }
  495. std::size_t module_impl::read_wrapper( std::size_t count, float * left, float * right, float * rear_left, float * rear_right ) {
  496. m_sndFile->ResetMixStat();
  497. m_sndFile->m_bIsRendering = ( m_ctl_play_at_end != song_end_action::fadeout_song );
  498. std::size_t count_read = 0;
  499. float * const buffers[4] = { left, right, rear_left, rear_right };
  500. OpenMPT::AudioTargetBufferWithGain<mpt::audio_span_planar<float>> target( mpt::audio_span_planar<float>( buffers, valid_channels( buffers, std::size( buffers ) ), count ), *m_Dithers, m_Gain );
  501. while ( count > 0 ) {
  502. std::size_t count_chunk = m_sndFile->Read(
  503. static_cast<OpenMPT::CSoundFile::samplecount_t>( std::min( static_cast<std::uint64_t>( count ), static_cast<std::uint64_t>( std::numeric_limits<OpenMPT::CSoundFile::samplecount_t>::max() / 2 / 4 / 4 ) ) ), // safety margin / samplesize / channels
  504. target
  505. );
  506. if ( count_chunk == 0 ) {
  507. break;
  508. }
  509. count -= count_chunk;
  510. count_read += count_chunk;
  511. }
  512. if ( count_read == 0 && m_ctl_play_at_end == song_end_action::continue_song ) {
  513. // This is the song end, but allow the song or loop to restart on the next call
  514. m_sndFile->m_SongFlags.reset(OpenMPT::SONG_ENDREACHED);
  515. }
  516. return count_read;
  517. }
  518. std::size_t module_impl::read_interleaved_wrapper( std::size_t count, std::size_t channels, std::int16_t * interleaved ) {
  519. m_sndFile->ResetMixStat();
  520. m_sndFile->m_bIsRendering = ( m_ctl_play_at_end != song_end_action::fadeout_song );
  521. std::size_t count_read = 0;
  522. OpenMPT::AudioTargetBufferWithGain<mpt::audio_span_interleaved<std::int16_t>> target( mpt::audio_span_interleaved<std::int16_t>( interleaved, channels, count ), *m_Dithers, m_Gain );
  523. while ( count > 0 ) {
  524. std::size_t count_chunk = m_sndFile->Read(
  525. static_cast<OpenMPT::CSoundFile::samplecount_t>( std::min( static_cast<std::uint64_t>( count ), static_cast<std::uint64_t>( std::numeric_limits<OpenMPT::CSoundFile::samplecount_t>::max() / 2 / 4 / 4 ) ) ), // safety margin / samplesize / channels
  526. target
  527. );
  528. if ( count_chunk == 0 ) {
  529. break;
  530. }
  531. count -= count_chunk;
  532. count_read += count_chunk;
  533. }
  534. if ( count_read == 0 && m_ctl_play_at_end == song_end_action::continue_song ) {
  535. // This is the song end, but allow the song or loop to restart on the next call
  536. m_sndFile->m_SongFlags.reset(OpenMPT::SONG_ENDREACHED);
  537. }
  538. return count_read;
  539. }
  540. std::size_t module_impl::read_interleaved_wrapper( std::size_t count, std::size_t channels, float * interleaved ) {
  541. m_sndFile->ResetMixStat();
  542. m_sndFile->m_bIsRendering = ( m_ctl_play_at_end != song_end_action::fadeout_song );
  543. std::size_t count_read = 0;
  544. OpenMPT::AudioTargetBufferWithGain<mpt::audio_span_interleaved<float>> target( mpt::audio_span_interleaved<float>( interleaved, channels, count ), *m_Dithers, m_Gain );
  545. while ( count > 0 ) {
  546. std::size_t count_chunk = m_sndFile->Read(
  547. static_cast<OpenMPT::CSoundFile::samplecount_t>( std::min( static_cast<std::uint64_t>( count ), static_cast<std::uint64_t>( std::numeric_limits<OpenMPT::CSoundFile::samplecount_t>::max() / 2 / 4 / 4 ) ) ), // safety margin / samplesize / channels
  548. target
  549. );
  550. if ( count_chunk == 0 ) {
  551. break;
  552. }
  553. count -= count_chunk;
  554. count_read += count_chunk;
  555. }
  556. if ( count_read == 0 && m_ctl_play_at_end == song_end_action::continue_song ) {
  557. // This is the song end, but allow the song or loop to restart on the next call
  558. m_sndFile->m_SongFlags.reset(OpenMPT::SONG_ENDREACHED);
  559. }
  560. return count_read;
  561. }
  562. std::vector<std::string> module_impl::get_supported_extensions() {
  563. std::vector<std::string> retval;
  564. std::vector<const char *> extensions = OpenMPT::CSoundFile::GetSupportedExtensions( false );
  565. std::copy( extensions.begin(), extensions.end(), std::back_insert_iterator<std::vector<std::string> >( retval ) );
  566. return retval;
  567. }
  568. bool module_impl::is_extension_supported( std::string_view extension ) {
  569. return OpenMPT::CSoundFile::IsExtensionSupported( extension );
  570. }
  571. /// <summary>
  572. /// From version: 0.7.0
  573. /// Hakan DANISIK
  574. /// </summary>
  575. /// <param name="extension"></param>
  576. /// <returns></returns>
  577. std::string module_impl::get_tracker_name( const std::string & extension ) {
  578. std::string lowercase_ext = extension;
  579. std::transform( lowercase_ext.begin(), lowercase_ext.end(), lowercase_ext.begin(), tolower );
  580. return OpenMPT::CSoundFile::ExtensionToTracker( lowercase_ext );
  581. }
  582. double module_impl::could_open_probability( const OpenMPT::FileCursor & file, double effort, std::unique_ptr<log_interface> log ) {
  583. try {
  584. if ( effort >= 0.8 ) {
  585. std::unique_ptr<OpenMPT::CSoundFile> sndFile = std::make_unique<OpenMPT::CSoundFile>();
  586. std::unique_ptr<log_forwarder> logForwarder = std::make_unique<log_forwarder>( *log );
  587. sndFile->SetCustomLog( logForwarder.get() );
  588. if ( !sndFile->Create( file, OpenMPT::CSoundFile::loadCompleteModule ) ) {
  589. return 0.0;
  590. }
  591. sndFile->Destroy();
  592. return 1.0;
  593. } else if ( effort >= 0.6 ) {
  594. std::unique_ptr<OpenMPT::CSoundFile> sndFile = std::make_unique<OpenMPT::CSoundFile>();
  595. std::unique_ptr<log_forwarder> logForwarder = std::make_unique<log_forwarder>( *log );
  596. sndFile->SetCustomLog( logForwarder.get() );
  597. if ( !sndFile->Create( file, OpenMPT::CSoundFile::loadNoPatternOrPluginData ) ) {
  598. return 0.0;
  599. }
  600. sndFile->Destroy();
  601. return 0.8;
  602. } else if ( effort >= 0.2 ) {
  603. std::unique_ptr<OpenMPT::CSoundFile> sndFile = std::make_unique<OpenMPT::CSoundFile>();
  604. std::unique_ptr<log_forwarder> logForwarder = std::make_unique<log_forwarder>( *log );
  605. sndFile->SetCustomLog( logForwarder.get() );
  606. if ( !sndFile->Create( file, OpenMPT::CSoundFile::onlyVerifyHeader ) ) {
  607. return 0.0;
  608. }
  609. sndFile->Destroy();
  610. return 0.6;
  611. } else if ( effort >= 0.1 ) {
  612. OpenMPT::FileCursor::PinnedView view = file.GetPinnedView( probe_file_header_get_recommended_size() );
  613. int probe_file_header_result = probe_file_header( probe_file_header_flags_default2, view.data(), view.size(), file.GetLength() );
  614. double result = 0.0;
  615. switch ( probe_file_header_result ) {
  616. case probe_file_header_result_success:
  617. result = 0.6;
  618. break;
  619. case probe_file_header_result_failure:
  620. result = 0.0;
  621. break;
  622. case probe_file_header_result_wantmoredata:
  623. result = 0.3;
  624. break;
  625. default:
  626. throw openmpt::exception("");
  627. break;
  628. }
  629. return result;
  630. } else {
  631. return 0.2;
  632. }
  633. } catch ( ... ) {
  634. return 0.0;
  635. }
  636. }
  637. double module_impl::could_open_probability( callback_stream_wrapper stream, double effort, std::unique_ptr<log_interface> log ) {
  638. mpt::IO::CallbackStream fstream;
  639. fstream.stream = stream.stream;
  640. fstream.read = stream.read;
  641. fstream.seek = stream.seek;
  642. fstream.tell = stream.tell;
  643. return could_open_probability( mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( fstream ), effort, std::move(log) );
  644. }
  645. double module_impl::could_open_probability( std::istream & stream, double effort, std::unique_ptr<log_interface> log ) {
  646. return could_open_probability(mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( stream ), effort, std::move(log) );
  647. }
  648. std::size_t module_impl::probe_file_header_get_recommended_size() {
  649. return OpenMPT::CSoundFile::ProbeRecommendedSize;
  650. }
  651. int module_impl::probe_file_header( std::uint64_t flags, const std::byte * data, std::size_t size, std::uint64_t filesize ) {
  652. int result = 0;
  653. switch ( OpenMPT::CSoundFile::Probe( static_cast<OpenMPT::CSoundFile::ProbeFlags>( flags ), mpt::span<const std::byte>( data, size ), &filesize ) ) {
  654. case OpenMPT::CSoundFile::ProbeSuccess:
  655. result = probe_file_header_result_success;
  656. break;
  657. case OpenMPT::CSoundFile::ProbeFailure:
  658. result = probe_file_header_result_failure;
  659. break;
  660. case OpenMPT::CSoundFile::ProbeWantMoreData:
  661. result = probe_file_header_result_wantmoredata;
  662. break;
  663. default:
  664. throw exception("internal error");
  665. break;
  666. }
  667. return result;
  668. }
  669. int module_impl::probe_file_header( std::uint64_t flags, const std::uint8_t * data, std::size_t size, std::uint64_t filesize ) {
  670. int result = 0;
  671. switch ( OpenMPT::CSoundFile::Probe( static_cast<OpenMPT::CSoundFile::ProbeFlags>( flags ), mpt::span<const std::byte>( mpt::byte_cast<const std::byte*>( data ), size ), &filesize ) ) {
  672. case OpenMPT::CSoundFile::ProbeSuccess:
  673. result = probe_file_header_result_success;
  674. break;
  675. case OpenMPT::CSoundFile::ProbeFailure:
  676. result = probe_file_header_result_failure;
  677. break;
  678. case OpenMPT::CSoundFile::ProbeWantMoreData:
  679. result = probe_file_header_result_wantmoredata;
  680. break;
  681. default:
  682. throw exception("internal error");
  683. break;
  684. }
  685. return result;
  686. }
  687. int module_impl::probe_file_header( std::uint64_t flags, const void * data, std::size_t size, std::uint64_t filesize ) {
  688. int result = 0;
  689. switch ( OpenMPT::CSoundFile::Probe( static_cast<OpenMPT::CSoundFile::ProbeFlags>( flags ), mpt::span<const std::byte>( mpt::void_cast<const std::byte*>( data ), size ), &filesize ) ) {
  690. case OpenMPT::CSoundFile::ProbeSuccess:
  691. result = probe_file_header_result_success;
  692. break;
  693. case OpenMPT::CSoundFile::ProbeFailure:
  694. result = probe_file_header_result_failure;
  695. break;
  696. case OpenMPT::CSoundFile::ProbeWantMoreData:
  697. result = probe_file_header_result_wantmoredata;
  698. break;
  699. default:
  700. throw exception("internal error");
  701. break;
  702. }
  703. return result;
  704. }
  705. int module_impl::probe_file_header( std::uint64_t flags, const std::byte * data, std::size_t size ) {
  706. int result = 0;
  707. switch ( OpenMPT::CSoundFile::Probe( static_cast<OpenMPT::CSoundFile::ProbeFlags>( flags ), mpt::span<const std::byte>( data, size ), nullptr ) ) {
  708. case OpenMPT::CSoundFile::ProbeSuccess:
  709. result = probe_file_header_result_success;
  710. break;
  711. case OpenMPT::CSoundFile::ProbeFailure:
  712. result = probe_file_header_result_failure;
  713. break;
  714. case OpenMPT::CSoundFile::ProbeWantMoreData:
  715. result = probe_file_header_result_wantmoredata;
  716. break;
  717. default:
  718. throw exception("internal error");
  719. break;
  720. }
  721. return result;
  722. }
  723. int module_impl::probe_file_header( std::uint64_t flags, const std::uint8_t * data, std::size_t size ) {
  724. int result = 0;
  725. switch ( OpenMPT::CSoundFile::Probe( static_cast<OpenMPT::CSoundFile::ProbeFlags>( flags ), mpt::span<const std::byte>( mpt::byte_cast<const std::byte*>( data ), size ), nullptr ) ) {
  726. case OpenMPT::CSoundFile::ProbeSuccess:
  727. result = probe_file_header_result_success;
  728. break;
  729. case OpenMPT::CSoundFile::ProbeFailure:
  730. result = probe_file_header_result_failure;
  731. break;
  732. case OpenMPT::CSoundFile::ProbeWantMoreData:
  733. result = probe_file_header_result_wantmoredata;
  734. break;
  735. default:
  736. throw exception("internal error");
  737. break;
  738. }
  739. return result;
  740. }
  741. int module_impl::probe_file_header( std::uint64_t flags, const void * data, std::size_t size ) {
  742. int result = 0;
  743. switch ( OpenMPT::CSoundFile::Probe( static_cast<OpenMPT::CSoundFile::ProbeFlags>( flags ), mpt::span<const std::byte>( mpt::void_cast<const std::byte*>( data ), size ), nullptr ) ) {
  744. case OpenMPT::CSoundFile::ProbeSuccess:
  745. result = probe_file_header_result_success;
  746. break;
  747. case OpenMPT::CSoundFile::ProbeFailure:
  748. result = probe_file_header_result_failure;
  749. break;
  750. case OpenMPT::CSoundFile::ProbeWantMoreData:
  751. result = probe_file_header_result_wantmoredata;
  752. break;
  753. default:
  754. throw exception("internal error");
  755. break;
  756. }
  757. return result;
  758. }
  759. int module_impl::probe_file_header( std::uint64_t flags, std::istream & stream ) {
  760. int result = 0;
  761. char buffer[ PROBE_RECOMMENDED_SIZE ];
  762. OpenMPT::MemsetZero( buffer );
  763. std::size_t size_read = 0;
  764. std::size_t size_toread = OpenMPT::CSoundFile::ProbeRecommendedSize;
  765. if ( stream.bad() ) {
  766. throw exception("error reading stream");
  767. }
  768. const bool seekable = mpt::IO::FileDataStdStream::IsSeekable( stream );
  769. const std::uint64_t filesize = ( seekable ? mpt::IO::FileDataStdStream::GetLength( stream ) : 0 );
  770. while ( ( size_toread > 0 ) && stream ) {
  771. stream.read( buffer + size_read, size_toread );
  772. if ( stream.bad() ) {
  773. throw exception("error reading stream");
  774. } else if ( stream.eof() ) {
  775. // normal
  776. } else if ( stream.fail() ) {
  777. throw exception("error reading stream");
  778. } else {
  779. // normal
  780. }
  781. std::size_t read_count = static_cast<std::size_t>( stream.gcount() );
  782. size_read += read_count;
  783. size_toread -= read_count;
  784. }
  785. switch ( OpenMPT::CSoundFile::Probe( static_cast<OpenMPT::CSoundFile::ProbeFlags>( flags ), mpt::span<const std::byte>( mpt::byte_cast<const std::byte*>( buffer ), size_read ), seekable ? &filesize : nullptr ) ) {
  786. case OpenMPT::CSoundFile::ProbeSuccess:
  787. result = probe_file_header_result_success;
  788. break;
  789. case OpenMPT::CSoundFile::ProbeFailure:
  790. result = probe_file_header_result_failure;
  791. break;
  792. case OpenMPT::CSoundFile::ProbeWantMoreData:
  793. result = probe_file_header_result_wantmoredata;
  794. break;
  795. default:
  796. throw exception("internal error");
  797. break;
  798. }
  799. return result;
  800. }
  801. int module_impl::probe_file_header( std::uint64_t flags, callback_stream_wrapper stream ) {
  802. int result = 0;
  803. char buffer[ PROBE_RECOMMENDED_SIZE ];
  804. OpenMPT::MemsetZero( buffer );
  805. std::size_t size_read = 0;
  806. std::size_t size_toread = OpenMPT::CSoundFile::ProbeRecommendedSize;
  807. if ( !stream.read ) {
  808. throw exception("error reading stream");
  809. }
  810. mpt::IO::CallbackStream fstream;
  811. fstream.stream = stream.stream;
  812. fstream.read = stream.read;
  813. fstream.seek = stream.seek;
  814. fstream.tell = stream.tell;
  815. const bool seekable = mpt::IO::FileDataCallbackStream::IsSeekable( fstream );
  816. const std::uint64_t filesize = ( seekable ? mpt::IO::FileDataCallbackStream::GetLength( fstream ) : 0 );
  817. while ( size_toread > 0 ) {
  818. std::size_t read_count = stream.read( stream.stream, buffer + size_read, size_toread );
  819. size_read += read_count;
  820. size_toread -= read_count;
  821. if ( read_count == 0 ) { // eof
  822. break;
  823. }
  824. }
  825. switch ( OpenMPT::CSoundFile::Probe( static_cast<OpenMPT::CSoundFile::ProbeFlags>( flags ), mpt::span<const std::byte>( mpt::byte_cast<const std::byte*>( buffer ), size_read ), seekable ? &filesize : nullptr ) ) {
  826. case OpenMPT::CSoundFile::ProbeSuccess:
  827. result = probe_file_header_result_success;
  828. break;
  829. case OpenMPT::CSoundFile::ProbeFailure:
  830. result = probe_file_header_result_failure;
  831. break;
  832. case OpenMPT::CSoundFile::ProbeWantMoreData:
  833. result = probe_file_header_result_wantmoredata;
  834. break;
  835. default:
  836. throw exception("internal error");
  837. break;
  838. }
  839. return result;
  840. }
  841. module_impl::module_impl( callback_stream_wrapper stream, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : m_Log(std::move(log)) {
  842. ctor( ctls );
  843. mpt::IO::CallbackStream fstream;
  844. fstream.stream = stream.stream;
  845. fstream.read = stream.read;
  846. fstream.seek = stream.seek;
  847. fstream.tell = stream.tell;
  848. load( mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( fstream ), ctls );
  849. apply_libopenmpt_defaults();
  850. }
  851. module_impl::module_impl( std::istream & stream, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : m_Log(std::move(log)) {
  852. ctor( ctls );
  853. load( mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( stream ), ctls );
  854. apply_libopenmpt_defaults();
  855. }
  856. module_impl::module_impl( const std::vector<std::byte> & data, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : m_Log(std::move(log)) {
  857. ctor( ctls );
  858. load( mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( mpt::as_span( data ) ), ctls );
  859. apply_libopenmpt_defaults();
  860. }
  861. module_impl::module_impl( const std::vector<std::uint8_t> & data, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : m_Log(std::move(log)) {
  862. ctor( ctls );
  863. load( mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( mpt::as_span( data ) ), ctls );
  864. apply_libopenmpt_defaults();
  865. }
  866. module_impl::module_impl( const std::vector<char> & data, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : m_Log(std::move(log)) {
  867. ctor( ctls );
  868. load( mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( mpt::byte_cast< mpt::span< const std::byte > >( mpt::as_span( data ) ) ), ctls );
  869. apply_libopenmpt_defaults();
  870. }
  871. module_impl::module_impl( const std::byte * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : m_Log(std::move(log)) {
  872. ctor( ctls );
  873. load( mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( mpt::as_span( data, size ) ), ctls );
  874. apply_libopenmpt_defaults();
  875. }
  876. module_impl::module_impl( const std::uint8_t * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : m_Log(std::move(log)) {
  877. ctor( ctls );
  878. load( mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( mpt::as_span( data, size ) ), ctls );
  879. apply_libopenmpt_defaults();
  880. }
  881. module_impl::module_impl( const char * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : m_Log(std::move(log)) {
  882. ctor( ctls );
  883. load( mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( mpt::byte_cast< mpt::span< const std::byte > >( mpt::as_span( data, size ) ) ), ctls );
  884. apply_libopenmpt_defaults();
  885. }
  886. module_impl::module_impl( const void * data, std::size_t size, std::unique_ptr<log_interface> log, const std::map< std::string, std::string > & ctls ) : m_Log(std::move(log)) {
  887. ctor( ctls );
  888. load( mpt::IO::make_FileCursor<OpenMPT::mpt::PathString>( mpt::as_span( mpt::void_cast< const std::byte * >( data ), size ) ), ctls );
  889. apply_libopenmpt_defaults();
  890. }
  891. module_impl::~module_impl() {
  892. m_sndFile->Destroy();
  893. }
  894. std::int32_t module_impl::get_render_param( int param ) const {
  895. std::int32_t result = 0;
  896. switch ( param ) {
  897. case module::RENDER_MASTERGAIN_MILLIBEL: {
  898. result = static_cast<std::int32_t>( 1000.0f * 2.0f * std::log10( m_Gain ) );
  899. } break;
  900. case module::RENDER_STEREOSEPARATION_PERCENT: {
  901. result = m_sndFile->m_MixerSettings.m_nStereoSeparation * 100 / OpenMPT::MixerSettings::StereoSeparationScale;
  902. } break;
  903. case module::RENDER_INTERPOLATIONFILTER_LENGTH: {
  904. result = resamplingmode_to_filterlength( m_sndFile->m_Resampler.m_Settings.SrcMode );
  905. } break;
  906. case module::RENDER_VOLUMERAMPING_STRENGTH: {
  907. int ramping = 0;
  908. mixersettings_to_ramping( ramping, m_sndFile->m_MixerSettings );
  909. result = ramping;
  910. } break;
  911. default: throw openmpt::exception("unknown render param"); break;
  912. }
  913. return result;
  914. }
  915. void module_impl::set_render_param( int param, std::int32_t value ) {
  916. switch ( param ) {
  917. case module::RENDER_MASTERGAIN_MILLIBEL: {
  918. m_Gain = static_cast<float>( std::pow( 10.0f, value * 0.001f * 0.5f ) );
  919. } break;
  920. case module::RENDER_STEREOSEPARATION_PERCENT: {
  921. std::int32_t newvalue = value * OpenMPT::MixerSettings::StereoSeparationScale / 100;
  922. if ( newvalue != static_cast<std::int32_t>( m_sndFile->m_MixerSettings.m_nStereoSeparation ) ) {
  923. OpenMPT::MixerSettings settings = m_sndFile->m_MixerSettings;
  924. settings.m_nStereoSeparation = newvalue;
  925. m_sndFile->SetMixerSettings( settings );
  926. }
  927. } break;
  928. case module::RENDER_INTERPOLATIONFILTER_LENGTH: {
  929. OpenMPT::CResamplerSettings newsettings = m_sndFile->m_Resampler.m_Settings;
  930. newsettings.SrcMode = filterlength_to_resamplingmode( value );
  931. if ( newsettings != m_sndFile->m_Resampler.m_Settings ) {
  932. m_sndFile->SetResamplerSettings( newsettings );
  933. }
  934. } break;
  935. case module::RENDER_VOLUMERAMPING_STRENGTH: {
  936. OpenMPT::MixerSettings newsettings = m_sndFile->m_MixerSettings;
  937. ramping_to_mixersettings( newsettings, value );
  938. if ( m_sndFile->m_MixerSettings.VolumeRampUpMicroseconds != newsettings.VolumeRampUpMicroseconds || m_sndFile->m_MixerSettings.VolumeRampDownMicroseconds != newsettings.VolumeRampDownMicroseconds ) {
  939. m_sndFile->SetMixerSettings( newsettings );
  940. }
  941. } break;
  942. default: throw openmpt::exception("unknown render param"); break;
  943. }
  944. }
  945. std::size_t module_impl::read( std::int32_t samplerate, std::size_t count, std::int16_t * mono ) {
  946. if ( !mono ) {
  947. throw openmpt::exception("null pointer");
  948. }
  949. apply_mixer_settings( samplerate, 1 );
  950. count = read_wrapper( count, mono, nullptr, nullptr, nullptr );
  951. m_currentPositionSeconds += static_cast<double>( count ) / static_cast<double>( samplerate );
  952. return count;
  953. }
  954. std::size_t module_impl::read( std::int32_t samplerate, std::size_t count, std::int16_t * left, std::int16_t * right ) {
  955. if ( !left || !right ) {
  956. throw openmpt::exception("null pointer");
  957. }
  958. apply_mixer_settings( samplerate, 2 );
  959. count = read_wrapper( count, left, right, nullptr, nullptr );
  960. m_currentPositionSeconds += static_cast<double>( count ) / static_cast<double>( samplerate );
  961. return count;
  962. }
  963. std::size_t module_impl::read( std::int32_t samplerate, std::size_t count, std::int16_t * left, std::int16_t * right, std::int16_t * rear_left, std::int16_t * rear_right ) {
  964. if ( !left || !right || !rear_left || !rear_right ) {
  965. throw openmpt::exception("null pointer");
  966. }
  967. apply_mixer_settings( samplerate, 4 );
  968. count = read_wrapper( count, left, right, rear_left, rear_right );
  969. m_currentPositionSeconds += static_cast<double>( count ) / static_cast<double>( samplerate );
  970. return count;
  971. }
  972. std::size_t module_impl::read( std::int32_t samplerate, std::size_t count, float * mono ) {
  973. if ( !mono ) {
  974. throw openmpt::exception("null pointer");
  975. }
  976. apply_mixer_settings( samplerate, 1 );
  977. count = read_wrapper( count, mono, nullptr, nullptr, nullptr );
  978. m_currentPositionSeconds += static_cast<double>( count ) / static_cast<double>( samplerate );
  979. return count;
  980. }
  981. std::size_t module_impl::read( std::int32_t samplerate, std::size_t count, float * left, float * right ) {
  982. if ( !left || !right ) {
  983. throw openmpt::exception("null pointer");
  984. }
  985. apply_mixer_settings( samplerate, 2 );
  986. count = read_wrapper( count, left, right, nullptr, nullptr );
  987. m_currentPositionSeconds += static_cast<double>( count ) / static_cast<double>( samplerate );
  988. return count;
  989. }
  990. std::size_t module_impl::read( std::int32_t samplerate, std::size_t count, float * left, float * right, float * rear_left, float * rear_right ) {
  991. if ( !left || !right || !rear_left || !rear_right ) {
  992. throw openmpt::exception("null pointer");
  993. }
  994. apply_mixer_settings( samplerate, 4 );
  995. count = read_wrapper( count, left, right, rear_left, rear_right );
  996. m_currentPositionSeconds += static_cast<double>( count ) / static_cast<double>( samplerate );
  997. return count;
  998. }
  999. std::size_t module_impl::read_interleaved_stereo( std::int32_t samplerate, std::size_t count, std::int16_t * interleaved_stereo ) {
  1000. if ( !interleaved_stereo ) {
  1001. throw openmpt::exception("null pointer");
  1002. }
  1003. apply_mixer_settings( samplerate, 2 );
  1004. count = read_interleaved_wrapper( count, 2, interleaved_stereo );
  1005. m_currentPositionSeconds += static_cast<double>( count ) / static_cast<double>( samplerate );
  1006. return count;
  1007. }
  1008. std::size_t module_impl::read_interleaved_quad( std::int32_t samplerate, std::size_t count, std::int16_t * interleaved_quad ) {
  1009. if ( !interleaved_quad ) {
  1010. throw openmpt::exception("null pointer");
  1011. }
  1012. apply_mixer_settings( samplerate, 4 );
  1013. count = read_interleaved_wrapper( count, 4, interleaved_quad );
  1014. m_currentPositionSeconds += static_cast<double>( count ) / static_cast<double>( samplerate );
  1015. return count;
  1016. }
  1017. std::size_t module_impl::read_interleaved_stereo( std::int32_t samplerate, std::size_t count, float * interleaved_stereo ) {
  1018. if ( !interleaved_stereo ) {
  1019. throw openmpt::exception("null pointer");
  1020. }
  1021. apply_mixer_settings( samplerate, 2 );
  1022. count = read_interleaved_wrapper( count, 2, interleaved_stereo );
  1023. m_currentPositionSeconds += static_cast<double>( count ) / static_cast<double>( samplerate );
  1024. return count;
  1025. }
  1026. std::size_t module_impl::read_interleaved_quad( std::int32_t samplerate, std::size_t count, float * interleaved_quad ) {
  1027. if ( !interleaved_quad ) {
  1028. throw openmpt::exception("null pointer");
  1029. }
  1030. apply_mixer_settings( samplerate, 4 );
  1031. count = read_interleaved_wrapper( count, 4, interleaved_quad );
  1032. m_currentPositionSeconds += static_cast<double>( count ) / static_cast<double>( samplerate );
  1033. return count;
  1034. }
  1035. double module_impl::get_duration_seconds() const {
  1036. std::unique_ptr<subsongs_type> subsongs_temp = has_subsongs_inited() ? std::unique_ptr<subsongs_type>() : std::make_unique<subsongs_type>( get_subsongs() );
  1037. const subsongs_type & subsongs = has_subsongs_inited() ? m_subsongs : *subsongs_temp;
  1038. if ( m_current_subsong == all_subsongs ) {
  1039. // Play all subsongs consecutively.
  1040. double total_duration = 0.0;
  1041. for ( const auto & subsong : subsongs ) {
  1042. total_duration += subsong.duration;
  1043. }
  1044. return total_duration;
  1045. }
  1046. return subsongs[m_current_subsong].duration;
  1047. }
  1048. void module_impl::select_subsong( std::int32_t subsong ) {
  1049. std::unique_ptr<subsongs_type> subsongs_temp = has_subsongs_inited() ? std::unique_ptr<subsongs_type>() : std::make_unique<subsongs_type>( get_subsongs() );
  1050. const subsongs_type & subsongs = has_subsongs_inited() ? m_subsongs : *subsongs_temp;
  1051. if ( subsong != all_subsongs && ( subsong < 0 || subsong >= static_cast<std::int32_t>( subsongs.size() ) ) ) {
  1052. throw openmpt::exception("invalid subsong");
  1053. }
  1054. m_current_subsong = subsong;
  1055. m_sndFile->m_SongFlags.set( OpenMPT::SONG_PLAYALLSONGS, subsong == all_subsongs );
  1056. if ( subsong == all_subsongs ) {
  1057. subsong = 0;
  1058. }
  1059. m_sndFile->Order.SetSequence( static_cast<OpenMPT::SEQUENCEINDEX>( subsongs[subsong].sequence ) );
  1060. set_position_order_row( subsongs[subsong].start_order, subsongs[subsong].start_row );
  1061. m_currentPositionSeconds = 0.0;
  1062. }
  1063. std::int32_t module_impl::get_selected_subsong() const {
  1064. return m_current_subsong;
  1065. }
  1066. void module_impl::set_repeat_count( std::int32_t repeat_count ) {
  1067. m_sndFile->SetRepeatCount( repeat_count );
  1068. }
  1069. std::int32_t module_impl::get_repeat_count() const {
  1070. return m_sndFile->GetRepeatCount();
  1071. }
  1072. double module_impl::get_position_seconds() const {
  1073. return m_currentPositionSeconds;
  1074. }
  1075. double module_impl::set_position_seconds( double seconds ) {
  1076. std::unique_ptr<subsongs_type> subsongs_temp = has_subsongs_inited() ? std::unique_ptr<subsongs_type>() : std::make_unique<subsongs_type>( get_subsongs() );
  1077. const subsongs_type & subsongs = has_subsongs_inited() ? m_subsongs : *subsongs_temp;
  1078. const subsong_data * subsong = 0;
  1079. double base_seconds = 0.0;
  1080. if ( m_current_subsong == all_subsongs ) {
  1081. // When playing all subsongs, find out which subsong this time would belong to.
  1082. subsong = &subsongs.back();
  1083. for ( std::size_t i = 0; i < subsongs.size(); ++i ) {
  1084. if ( base_seconds + subsongs[i].duration > seconds ) {
  1085. subsong = &subsongs[i];
  1086. break;
  1087. }
  1088. base_seconds += subsong->duration;
  1089. }
  1090. seconds -= base_seconds;
  1091. } else {
  1092. subsong = &subsongs[m_current_subsong];
  1093. }
  1094. m_sndFile->SetCurrentOrder( static_cast<OpenMPT::ORDERINDEX>( subsong->start_order ) );
  1095. OpenMPT::GetLengthType t = m_sndFile->GetLength( m_ctl_seek_sync_samples ? OpenMPT::eAdjustSamplePositions : OpenMPT::eAdjust, OpenMPT::GetLengthTarget( seconds ).StartPos( static_cast<OpenMPT::SEQUENCEINDEX>( subsong->sequence ), static_cast<OpenMPT::ORDERINDEX>( subsong->start_order ), static_cast<OpenMPT::ROWINDEX>( subsong->start_row ) ) ).back();
  1096. m_sndFile->m_PlayState.m_nNextOrder = m_sndFile->m_PlayState.m_nCurrentOrder = t.targetReached ? t.lastOrder : t.endOrder;
  1097. m_sndFile->m_PlayState.m_nNextRow = t.targetReached ? t.lastRow : t.endRow;
  1098. m_sndFile->m_PlayState.m_nTickCount = OpenMPT::CSoundFile::TICKS_ROW_FINISHED;
  1099. m_currentPositionSeconds = base_seconds + t.duration;
  1100. return m_currentPositionSeconds;
  1101. }
  1102. double module_impl::set_position_order_row( std::int32_t order, std::int32_t row ) {
  1103. if ( order < 0 || order >= m_sndFile->Order().GetLengthTailTrimmed() ) {
  1104. return m_currentPositionSeconds;
  1105. }
  1106. OpenMPT::PATTERNINDEX pattern = m_sndFile->Order()[order];
  1107. if ( m_sndFile->Patterns.IsValidIndex( pattern ) ) {
  1108. if ( row < 0 || row >= static_cast<std::int32_t>( m_sndFile->Patterns[pattern].GetNumRows() ) ) {
  1109. return m_currentPositionSeconds;
  1110. }
  1111. } else {
  1112. row = 0;
  1113. }
  1114. m_sndFile->m_PlayState.m_nCurrentOrder = static_cast<OpenMPT::ORDERINDEX>( order );
  1115. m_sndFile->SetCurrentOrder( static_cast<OpenMPT::ORDERINDEX>( order ) );
  1116. m_sndFile->m_PlayState.m_nNextRow = static_cast<OpenMPT::ROWINDEX>( row );
  1117. m_sndFile->m_PlayState.m_nTickCount = OpenMPT::CSoundFile::TICKS_ROW_FINISHED;
  1118. m_currentPositionSeconds = m_sndFile->GetLength( m_ctl_seek_sync_samples ? OpenMPT::eAdjustSamplePositions : OpenMPT::eAdjust, OpenMPT::GetLengthTarget( static_cast<OpenMPT::ORDERINDEX>( order ), static_cast<OpenMPT::ROWINDEX>( row ) ) ).back().duration;
  1119. return m_currentPositionSeconds;
  1120. }
  1121. std::vector<std::string> module_impl::get_metadata_keys() const {
  1122. return
  1123. {
  1124. "type",
  1125. "type_long",
  1126. "originaltype",
  1127. "originaltype_long",
  1128. "container",
  1129. "container_long",
  1130. "tracker",
  1131. "artist",
  1132. "title",
  1133. "date",
  1134. "message",
  1135. "message_raw",
  1136. "warnings",
  1137. };
  1138. }
  1139. std::string module_impl::get_message_instruments() const {
  1140. std::string retval;
  1141. std::string tmp;
  1142. bool valid = false;
  1143. for ( OpenMPT::INSTRUMENTINDEX i = 1; i <= m_sndFile->GetNumInstruments(); ++i ) {
  1144. std::string instname = m_sndFile->GetInstrumentName( i );
  1145. if ( !instname.empty() ) {
  1146. valid = true;
  1147. }
  1148. tmp += instname;
  1149. tmp += "\n";
  1150. }
  1151. if ( valid ) {
  1152. retval = tmp;
  1153. }
  1154. return retval;
  1155. }
  1156. std::string module_impl::get_message_samples() const {
  1157. std::string retval;
  1158. std::string tmp;
  1159. bool valid = false;
  1160. for ( OpenMPT::SAMPLEINDEX i = 1; i <= m_sndFile->GetNumSamples(); ++i ) {
  1161. std::string samplename = m_sndFile->GetSampleName( i );
  1162. if ( !samplename.empty() ) {
  1163. valid = true;
  1164. }
  1165. tmp += samplename;
  1166. tmp += "\n";
  1167. }
  1168. if ( valid ) {
  1169. retval = tmp;
  1170. }
  1171. return retval;
  1172. }
  1173. std::string module_impl::get_metadata( const std::string & key ) const {
  1174. if ( key == std::string("type") ) {
  1175. return mpt::transcode<std::string>( mpt::common_encoding::utf8, m_sndFile->m_modFormat.type );
  1176. } else if ( key == std::string("type_long") ) {
  1177. return mpt::transcode<std::string>( mpt::common_encoding::utf8, m_sndFile->m_modFormat.formatName );
  1178. } else if ( key == std::string("originaltype") ) {
  1179. return mpt::transcode<std::string>( mpt::common_encoding::utf8, m_sndFile->m_modFormat.originalType );
  1180. } else if ( key == std::string("originaltype_long") ) {
  1181. return mpt::transcode<std::string>( mpt::common_encoding::utf8, m_sndFile->m_modFormat.originalFormatName );
  1182. } else if ( key == std::string("container") ) {
  1183. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::CSoundFile::ModContainerTypeToString( m_sndFile->GetContainerType() ) );
  1184. } else if ( key == std::string("container_long") ) {
  1185. return mpt::transcode<std::string>( mpt::common_encoding::utf8, OpenMPT::CSoundFile::ModContainerTypeToTracker( m_sndFile->GetContainerType() ) );
  1186. } else if ( key == std::string("tracker") ) {
  1187. return mpt::transcode<std::string>( mpt::common_encoding::utf8, m_sndFile->m_modFormat.madeWithTracker );
  1188. } else if ( key == std::string("artist") ) {
  1189. return mpt::transcode<std::string>( mpt::common_encoding::utf8, m_sndFile->m_songArtist );
  1190. } else if ( key == std::string("title") ) {
  1191. return mod_string_to_utf8( m_sndFile->GetTitle() );
  1192. } else if ( key == std::string("date") ) {
  1193. if ( m_sndFile->GetFileHistory().empty() || !m_sndFile->GetFileHistory().back().HasValidDate() ) {
  1194. return std::string();
  1195. }
  1196. return mpt::transcode<std::string>( mpt::common_encoding::utf8, m_sndFile->GetFileHistory().back().AsISO8601() );
  1197. } else if ( key == std::string("message") ) {
  1198. std::string retval = m_sndFile->m_songMessage.GetFormatted( OpenMPT::SongMessage::leLF );
  1199. if ( retval.empty() ) {
  1200. switch ( m_sndFile->GetMessageHeuristic() ) {
  1201. case OpenMPT::ModMessageHeuristicOrder::Instruments:
  1202. retval = get_message_instruments();
  1203. break;
  1204. case OpenMPT::ModMessageHeuristicOrder::Samples:
  1205. retval = get_message_samples();
  1206. break;
  1207. case OpenMPT::ModMessageHeuristicOrder::InstrumentsSamples:
  1208. if ( retval.empty() ) {
  1209. retval = get_message_instruments();
  1210. }
  1211. if ( retval.empty() ) {
  1212. retval = get_message_samples();
  1213. }
  1214. break;
  1215. case OpenMPT::ModMessageHeuristicOrder::SamplesInstruments:
  1216. if ( retval.empty() ) {
  1217. retval = get_message_samples();
  1218. }
  1219. if ( retval.empty() ) {
  1220. retval = get_message_instruments();
  1221. }
  1222. break;
  1223. case OpenMPT::ModMessageHeuristicOrder::BothInstrumentsSamples:
  1224. {
  1225. std::string message_instruments = get_message_instruments();
  1226. std::string message_samples = get_message_samples();
  1227. if ( !message_instruments.empty() ) {
  1228. retval += std::move( message_instruments );
  1229. }
  1230. if ( !message_samples.empty() ) {
  1231. retval += std::move( message_samples );
  1232. }
  1233. }
  1234. break;
  1235. case OpenMPT::ModMessageHeuristicOrder::BothSamplesInstruments:
  1236. {
  1237. std::string message_instruments = get_message_instruments();
  1238. std::string message_samples = get_message_samples();
  1239. if ( !message_samples.empty() ) {
  1240. retval += std::move( message_samples );
  1241. }
  1242. if ( !message_instruments.empty() ) {
  1243. retval += std::move( message_instruments );
  1244. }
  1245. }
  1246. break;
  1247. }
  1248. }
  1249. return mod_string_to_utf8( retval );
  1250. } else if ( key == std::string("message_raw") ) {
  1251. std::string retval = m_sndFile->m_songMessage.GetFormatted( OpenMPT::SongMessage::leLF );
  1252. return mod_string_to_utf8( retval );
  1253. } else if ( key == std::string("warnings") ) {
  1254. std::string retval;
  1255. bool first = true;
  1256. for ( const auto & msg : m_loaderMessages ) {
  1257. if ( !first ) {
  1258. retval += "\n";
  1259. } else {
  1260. first = false;
  1261. }
  1262. retval += msg;
  1263. }
  1264. return retval;
  1265. }
  1266. return "";
  1267. }
  1268. double module_impl::get_current_estimated_bpm() const {
  1269. return m_sndFile->GetCurrentBPM();
  1270. }
  1271. std::int32_t module_impl::get_current_speed() const {
  1272. return m_sndFile->m_PlayState.m_nMusicSpeed;
  1273. }
  1274. std::int32_t module_impl::get_current_tempo() const {
  1275. return static_cast<std::int32_t>( m_sndFile->m_PlayState.m_nMusicTempo.GetInt() );
  1276. }
  1277. std::int32_t module_impl::get_current_order() const {
  1278. return m_sndFile->GetCurrentOrder();
  1279. }
  1280. std::int32_t module_impl::get_current_pattern() const {
  1281. std::int32_t order = m_sndFile->GetCurrentOrder();
  1282. if ( order < 0 || order >= m_sndFile->Order().GetLengthTailTrimmed() ) {
  1283. return m_sndFile->GetCurrentPattern();
  1284. }
  1285. std::int32_t pattern = m_sndFile->Order()[order];
  1286. if ( !m_sndFile->Patterns.IsValidIndex( static_cast<OpenMPT::PATTERNINDEX>( pattern ) ) ) {
  1287. return -1;
  1288. }
  1289. return pattern;
  1290. }
  1291. std::int32_t module_impl::get_current_row() const {
  1292. return m_sndFile->m_PlayState.m_nRow;
  1293. }
  1294. std::int32_t module_impl::get_current_playing_channels() const {
  1295. return m_sndFile->GetMixStat();
  1296. }
  1297. float module_impl::get_current_channel_vu_mono( std::int32_t channel ) const {
  1298. if ( channel < 0 || channel >= m_sndFile->GetNumChannels() ) {
  1299. return 0.0f;
  1300. }
  1301. const float left = m_sndFile->m_PlayState.Chn[channel].nLeftVU * (1.0f/128.0f);
  1302. const float right = m_sndFile->m_PlayState.Chn[channel].nRightVU * (1.0f/128.0f);
  1303. return std::sqrt(left*left + right*right);
  1304. }
  1305. float module_impl::get_current_channel_vu_left( std::int32_t channel ) const {
  1306. if ( channel < 0 || channel >= m_sndFile->GetNumChannels() ) {
  1307. return 0.0f;
  1308. }
  1309. return m_sndFile->m_PlayState.Chn[channel].dwFlags[OpenMPT::CHN_SURROUND] ? 0.0f : m_sndFile->m_PlayState.Chn[channel].nLeftVU * (1.0f/128.0f);
  1310. }
  1311. float module_impl::get_current_channel_vu_right( std::int32_t channel ) const {
  1312. if ( channel < 0 || channel >= m_sndFile->GetNumChannels() ) {
  1313. return 0.0f;
  1314. }
  1315. return m_sndFile->m_PlayState.Chn[channel].dwFlags[OpenMPT::CHN_SURROUND] ? 0.0f : m_sndFile->m_PlayState.Chn[channel].nRightVU * (1.0f/128.0f);
  1316. }
  1317. float module_impl::get_current_channel_vu_rear_left( std::int32_t channel ) const {
  1318. if ( channel < 0 || channel >= m_sndFile->GetNumChannels() ) {
  1319. return 0.0f;
  1320. }
  1321. return m_sndFile->m_PlayState.Chn[channel].dwFlags[OpenMPT::CHN_SURROUND] ? m_sndFile->m_PlayState.Chn[channel].nLeftVU * (1.0f/128.0f) : 0.0f;
  1322. }
  1323. float module_impl::get_current_channel_vu_rear_right( std::int32_t channel ) const {
  1324. if ( channel < 0 || channel >= m_sndFile->GetNumChannels() ) {
  1325. return 0.0f;
  1326. }
  1327. return m_sndFile->m_PlayState.Chn[channel].dwFlags[OpenMPT::CHN_SURROUND] ? m_sndFile->m_PlayState.Chn[channel].nRightVU * (1.0f/128.0f) : 0.0f;
  1328. }
  1329. std::int32_t module_impl::get_num_subsongs() const {
  1330. std::unique_ptr<subsongs_type> subsongs_temp = has_subsongs_inited() ? std::unique_ptr<subsongs_type>() : std::make_unique<subsongs_type>( get_subsongs() );
  1331. const subsongs_type & subsongs = has_subsongs_inited() ? m_subsongs : *subsongs_temp;
  1332. return static_cast<std::int32_t>( subsongs.size() );
  1333. }
  1334. std::int32_t module_impl::get_num_channels() const {
  1335. return m_sndFile->GetNumChannels();
  1336. }
  1337. std::int32_t module_impl::get_num_orders() const {
  1338. return m_sndFile->Order().GetLengthTailTrimmed();
  1339. }
  1340. std::int32_t module_impl::get_num_patterns() const {
  1341. return m_sndFile->Patterns.GetNumPatterns();
  1342. }
  1343. std::int32_t module_impl::get_num_instruments() const {
  1344. return m_sndFile->GetNumInstruments();
  1345. }
  1346. std::int32_t module_impl::get_num_samples() const {
  1347. return m_sndFile->GetNumSamples();
  1348. }
  1349. std::vector<std::string> module_impl::get_subsong_names() const {
  1350. std::vector<std::string> retval;
  1351. std::unique_ptr<subsongs_type> subsongs_temp = has_subsongs_inited() ? std::unique_ptr<subsongs_type>() : std::make_unique<subsongs_type>( get_subsongs() );
  1352. const subsongs_type & subsongs = has_subsongs_inited() ? m_subsongs : *subsongs_temp;
  1353. retval.reserve( subsongs.size() );
  1354. for ( const auto & subsong : subsongs ) {
  1355. const auto & order = m_sndFile->Order( static_cast<OpenMPT::SEQUENCEINDEX>( subsong.sequence ) );
  1356. retval.push_back( mpt::transcode<std::string>( mpt::common_encoding::utf8, order.GetName() ) );
  1357. if ( retval.back().empty() ) {
  1358. // use first pattern name instead
  1359. if ( order.IsValidPat( static_cast<OpenMPT::SEQUENCEINDEX>( subsong.start_order ) ) )
  1360. retval.back() = OpenMPT::mpt::ToCharset( OpenMPT::mpt::Charset::UTF8, m_sndFile->GetCharsetInternal(), m_sndFile->Patterns[ order[ subsong.start_order ] ].GetName() );
  1361. }
  1362. }
  1363. return retval;
  1364. }
  1365. std::vector<std::string> module_impl::get_channel_names() const {
  1366. std::vector<std::string> retval;
  1367. for ( OpenMPT::CHANNELINDEX i = 0; i < m_sndFile->GetNumChannels(); ++i ) {
  1368. retval.push_back( mod_string_to_utf8( m_sndFile->ChnSettings[i].szName ) );
  1369. }
  1370. return retval;
  1371. }
  1372. std::vector<std::string> module_impl::get_order_names() const {
  1373. std::vector<std::string> retval;
  1374. OpenMPT::ORDERINDEX num_orders = m_sndFile->Order().GetLengthTailTrimmed();
  1375. retval.reserve( num_orders );
  1376. for ( OpenMPT::ORDERINDEX i = 0; i < num_orders; ++i ) {
  1377. OpenMPT::PATTERNINDEX pat = m_sndFile->Order()[i];
  1378. if ( m_sndFile->Patterns.IsValidIndex( pat ) ) {
  1379. retval.push_back( mod_string_to_utf8( m_sndFile->Patterns[ m_sndFile->Order()[i] ].GetName() ) );
  1380. } else {
  1381. if ( pat == m_sndFile->Order.GetIgnoreIndex() ) {
  1382. retval.push_back( "+++ skip" );
  1383. } else if ( pat == m_sndFile->Order.GetInvalidPatIndex() ) {
  1384. retval.push_back( "--- stop" );
  1385. } else {
  1386. retval.push_back( "???" );
  1387. }
  1388. }
  1389. }
  1390. return retval;
  1391. }
  1392. std::vector<std::string> module_impl::get_pattern_names() const {
  1393. std::vector<std::string> retval;
  1394. retval.reserve( m_sndFile->Patterns.GetNumPatterns() );
  1395. for ( OpenMPT::PATTERNINDEX i = 0; i < m_sndFile->Patterns.GetNumPatterns(); ++i ) {
  1396. retval.push_back( mod_string_to_utf8( m_sndFile->Patterns[i].GetName() ) );
  1397. }
  1398. return retval;
  1399. }
  1400. std::vector<std::string> module_impl::get_instrument_names() const {
  1401. std::vector<std::string> retval;
  1402. retval.reserve( m_sndFile->GetNumInstruments() );
  1403. for ( OpenMPT::INSTRUMENTINDEX i = 1; i <= m_sndFile->GetNumInstruments(); ++i ) {
  1404. retval.push_back( mod_string_to_utf8( m_sndFile->GetInstrumentName( i ) ) );
  1405. }
  1406. return retval;
  1407. }
  1408. std::vector<std::string> module_impl::get_sample_names() const {
  1409. std::vector<std::string> retval;
  1410. retval.reserve( m_sndFile->GetNumSamples() );
  1411. for ( OpenMPT::SAMPLEINDEX i = 1; i <= m_sndFile->GetNumSamples(); ++i ) {
  1412. retval.push_back( mod_string_to_utf8( m_sndFile->GetSampleName( i ) ) );
  1413. }
  1414. return retval;
  1415. }
  1416. std::int32_t module_impl::get_order_pattern( std::int32_t o ) const {
  1417. if ( o < 0 || o >= m_sndFile->Order().GetLengthTailTrimmed() ) {
  1418. return -1;
  1419. }
  1420. return m_sndFile->Order()[o];
  1421. }
  1422. std::int32_t module_impl::get_pattern_num_rows( std::int32_t p ) const {
  1423. if ( !mpt::is_in_range( p, std::numeric_limits<OpenMPT::PATTERNINDEX>::min(), std::numeric_limits<OpenMPT::PATTERNINDEX>::max() ) || !m_sndFile->Patterns.IsValidPat( static_cast<OpenMPT::PATTERNINDEX>( p ) ) ) {
  1424. return 0;
  1425. }
  1426. return m_sndFile->Patterns[p].GetNumRows();
  1427. }
  1428. std::uint8_t module_impl::get_pattern_row_channel_command( std::int32_t p, std::int32_t r, std::int32_t c, int cmd ) const {
  1429. if ( !mpt::is_in_range( p, std::numeric_limits<OpenMPT::PATTERNINDEX>::min(), std::numeric_limits<OpenMPT::PATTERNINDEX>::max() ) || !m_sndFile->Patterns.IsValidPat( static_cast<OpenMPT::PATTERNINDEX>( p ) ) ) {
  1430. return 0;
  1431. }
  1432. const OpenMPT::CPattern & pattern = m_sndFile->Patterns[p];
  1433. if ( r < 0 || r >= static_cast<std::int32_t>( pattern.GetNumRows() ) ) {
  1434. return 0;
  1435. }
  1436. if ( c < 0 || c >= m_sndFile->GetNumChannels() ) {
  1437. return 0;
  1438. }
  1439. if ( cmd < module::command_note || cmd > module::command_parameter ) {
  1440. return 0;
  1441. }
  1442. const OpenMPT::ModCommand & cell = *pattern.GetpModCommand( static_cast<OpenMPT::ROWINDEX>( r ), static_cast<OpenMPT::CHANNELINDEX>( c ) );
  1443. switch ( cmd ) {
  1444. case module::command_note: return cell.note; break;
  1445. case module::command_instrument: return cell.instr; break;
  1446. case module::command_volumeffect: return cell.volcmd; break;
  1447. case module::command_effect: return cell.command; break;
  1448. case module::command_volume: return cell.vol; break;
  1449. case module::command_parameter: return cell.param; break;
  1450. }
  1451. return 0;
  1452. }
  1453. /*
  1454. highlight chars explained:
  1455. : empty/space
  1456. . : empty/dot
  1457. n : generic note
  1458. m : special note
  1459. i : generic instrument
  1460. u : generic volume column effect
  1461. v : generic volume column parameter
  1462. e : generic effect column effect
  1463. f : generic effect column parameter
  1464. */
  1465. std::pair< std::string, std::string > module_impl::format_and_highlight_pattern_row_channel_command( std::int32_t p, std::int32_t r, std::int32_t c, int cmd ) const {
  1466. if ( !mpt::is_in_range( p, std::numeric_limits<OpenMPT::PATTERNINDEX>::min(), std::numeric_limits<OpenMPT::PATTERNINDEX>::max() ) || !m_sndFile->Patterns.IsValidPat( static_cast<OpenMPT::PATTERNINDEX>( p ) ) ) {
  1467. return std::make_pair( std::string(), std::string() );
  1468. }
  1469. const OpenMPT::CPattern & pattern = m_sndFile->Patterns[p];
  1470. if ( r < 0 || r >= static_cast<std::int32_t>( pattern.GetNumRows() ) ) {
  1471. return std::make_pair( std::string(), std::string() );
  1472. }
  1473. if ( c < 0 || c >= m_sndFile->GetNumChannels() ) {
  1474. return std::make_pair( std::string(), std::string() );
  1475. }
  1476. if ( cmd < module::command_note || cmd > module::command_parameter ) {
  1477. return std::make_pair( std::string(), std::string() );
  1478. }
  1479. const OpenMPT::ModCommand & cell = *pattern.GetpModCommand( static_cast<OpenMPT::ROWINDEX>( r ), static_cast<OpenMPT::CHANNELINDEX>( c ) );
  1480. // clang-format off
  1481. switch ( cmd ) {
  1482. case module::command_note:
  1483. return std::make_pair(
  1484. ( cell.IsNote() || cell.IsSpecialNote() ) ? mpt::transcode<std::string>( mpt::common_encoding::utf8, m_sndFile->GetNoteName( cell.note, cell.instr ) ) : std::string("...")
  1485. ,
  1486. ( cell.IsNote() ) ? std::string("nnn") : cell.IsSpecialNote() ? std::string("mmm") : std::string("...")
  1487. );
  1488. break;
  1489. case module::command_instrument:
  1490. return std::make_pair(
  1491. cell.instr ? OpenMPT::mpt::afmt::HEX0<2>( cell.instr ) : std::string("..")
  1492. ,
  1493. cell.instr ? std::string("ii") : std::string("..")
  1494. );
  1495. break;
  1496. case module::command_volumeffect:
  1497. return std::make_pair(
  1498. cell.IsPcNote() ? std::string(" ") : cell.volcmd != OpenMPT::VOLCMD_NONE ? std::string( 1, m_sndFile->GetModSpecifications().GetVolEffectLetter( cell.volcmd ) ) : std::string(" ")
  1499. ,
  1500. cell.IsPcNote() ? std::string(" ") : cell.volcmd != OpenMPT::VOLCMD_NONE ? std::string("u") : std::string(" ")
  1501. );
  1502. break;
  1503. case module::command_volume:
  1504. return std::make_pair(
  1505. cell.IsPcNote() ? OpenMPT::mpt::afmt::HEX0<2>( cell.GetValueVolCol() & 0xff ) : cell.volcmd != OpenMPT::VOLCMD_NONE ? OpenMPT::mpt::afmt::HEX0<2>( cell.vol ) : std::string("..")
  1506. ,
  1507. cell.IsPcNote() ? std::string("vv") : cell.volcmd != OpenMPT::VOLCMD_NONE ? std::string("vv") : std::string("..")
  1508. );
  1509. break;
  1510. case module::command_effect:
  1511. return std::make_pair(
  1512. cell.IsPcNote() ? OpenMPT::mpt::afmt::HEX0<1>( ( cell.GetValueEffectCol() & 0x0f00 ) > 16 ) : cell.command != OpenMPT::CMD_NONE ? std::string( 1, m_sndFile->GetModSpecifications().GetEffectLetter( cell.command ) ) : std::string(".")
  1513. ,
  1514. cell.IsPcNote() ? std::string("e") : cell.command != OpenMPT::CMD_NONE ? std::string("e") : std::string(".")
  1515. );
  1516. break;
  1517. case module::command_parameter:
  1518. return std::make_pair(
  1519. cell.IsPcNote() ? OpenMPT::mpt::afmt::HEX0<2>( cell.GetValueEffectCol() & 0x00ff ) : cell.command != OpenMPT::CMD_NONE ? OpenMPT::mpt::afmt::HEX0<2>( cell.param ) : std::string("..")
  1520. ,
  1521. cell.IsPcNote() ? std::string("ff") : cell.command != OpenMPT::CMD_NONE ? std::string("ff") : std::string("..")
  1522. );
  1523. break;
  1524. }
  1525. // clang-format on
  1526. return std::make_pair( std::string(), std::string() );
  1527. }
  1528. std::string module_impl::format_pattern_row_channel_command( std::int32_t p, std::int32_t r, std::int32_t c, int cmd ) const {
  1529. return format_and_highlight_pattern_row_channel_command( p, r, c, cmd ).first;
  1530. }
  1531. std::string module_impl::highlight_pattern_row_channel_command( std::int32_t p, std::int32_t r, std::int32_t c, int cmd ) const {
  1532. return format_and_highlight_pattern_row_channel_command( p, r, c, cmd ).second;
  1533. }
  1534. std::pair< std::string, std::string > module_impl::format_and_highlight_pattern_row_channel( std::int32_t p, std::int32_t r, std::int32_t c, std::size_t width, bool pad ) const {
  1535. std::string text = pad ? std::string( width, ' ' ) : std::string();
  1536. std::string high = pad ? std::string( width, ' ' ) : std::string();
  1537. if ( !mpt::is_in_range( p, std::numeric_limits<OpenMPT::PATTERNINDEX>::min(), std::numeric_limits<OpenMPT::PATTERNINDEX>::max() ) || !m_sndFile->Patterns.IsValidPat( static_cast<OpenMPT::PATTERNINDEX>( p ) ) ) {
  1538. return std::make_pair( text, high );
  1539. }
  1540. const OpenMPT::CPattern & pattern = m_sndFile->Patterns[p];
  1541. if ( r < 0 || r >= static_cast<std::int32_t>( pattern.GetNumRows() ) ) {
  1542. return std::make_pair( text, high );
  1543. }
  1544. if ( c < 0 || c >= m_sndFile->GetNumChannels() ) {
  1545. return std::make_pair( text, high );
  1546. }
  1547. // 0000000001111
  1548. // 1234567890123
  1549. // "NNN IIvVV EFF"
  1550. const OpenMPT::ModCommand & cell = *pattern.GetpModCommand( static_cast<OpenMPT::ROWINDEX>( r ), static_cast<OpenMPT::CHANNELINDEX>( c ) );
  1551. text.clear();
  1552. high.clear();
  1553. // clang-format off
  1554. text += ( cell.IsNote() || cell.IsSpecialNote() ) ? mpt::transcode<std::string>( mpt::common_encoding::utf8, m_sndFile->GetNoteName( cell.note, cell.instr ) ) : std::string("...");
  1555. high += ( cell.IsNote() ) ? std::string("nnn") : cell.IsSpecialNote() ? std::string("mmm") : std::string("...");
  1556. if ( ( width == 0 ) || ( width >= 6 ) ) {
  1557. text += std::string(" ");
  1558. high += std::string(" ");
  1559. text += cell.instr ? OpenMPT::mpt::afmt::HEX0<2>( cell.instr ) : std::string("..");
  1560. high += cell.instr ? std::string("ii") : std::string("..");
  1561. }
  1562. if ( ( width == 0 ) || ( width >= 9 ) ) {
  1563. text += cell.IsPcNote() ? std::string(" ") + OpenMPT::mpt::afmt::HEX0<2>( cell.GetValueVolCol() & 0xff ) : cell.volcmd != OpenMPT::VOLCMD_NONE ? std::string( 1, m_sndFile->GetModSpecifications().GetVolEffectLetter( cell.volcmd ) ) + OpenMPT::mpt::afmt::HEX0<2>( cell.vol ) : std::string(" ..");
  1564. high += cell.IsPcNote() ? std::string(" vv") : cell.volcmd != OpenMPT::VOLCMD_NONE ? std::string("uvv") : std::string(" ..");
  1565. }
  1566. if ( ( width == 0 ) || ( width >= 13 ) ) {
  1567. text += std::string(" ");
  1568. high += std::string(" ");
  1569. text += cell.IsPcNote() ? OpenMPT::mpt::afmt::HEX0<3>( cell.GetValueEffectCol() & 0x0fff ) : cell.command != OpenMPT::CMD_NONE ? std::string( 1, m_sndFile->GetModSpecifications().GetEffectLetter( cell.command ) ) + OpenMPT::mpt::afmt::HEX0<2>( cell.param ) : std::string("...");
  1570. high += cell.IsPcNote() ? std::string("eff") : cell.command != OpenMPT::CMD_NONE ? std::string("eff") : std::string("...");
  1571. }
  1572. if ( ( width != 0 ) && ( text.length() > width ) ) {
  1573. text = text.substr( 0, width );
  1574. } else if ( ( width != 0 ) && pad ) {
  1575. text += std::string( width - text.length(), ' ' );
  1576. }
  1577. if ( ( width != 0 ) && ( high.length() > width ) ) {
  1578. high = high.substr( 0, width );
  1579. } else if ( ( width != 0 ) && pad ) {
  1580. high += std::string( width - high.length(), ' ' );
  1581. }
  1582. // clang-format on
  1583. return std::make_pair( text, high );
  1584. }
  1585. std::string module_impl::format_pattern_row_channel( std::int32_t p, std::int32_t r, std::int32_t c, std::size_t width, bool pad ) const {
  1586. return format_and_highlight_pattern_row_channel( p, r, c, width, pad ).first;
  1587. }
  1588. std::string module_impl::highlight_pattern_row_channel( std::int32_t p, std::int32_t r, std::int32_t c, std::size_t width, bool pad ) const {
  1589. return format_and_highlight_pattern_row_channel( p, r, c, width, pad ).second;
  1590. }
  1591. std::pair<const module_impl::ctl_info *, const module_impl::ctl_info *> module_impl::get_ctl_infos() const {
  1592. static constexpr ctl_info ctl_infos[] = {
  1593. { "load.skip_samples", ctl_type::boolean },
  1594. { "load.skip_patterns", ctl_type::boolean },
  1595. { "load.skip_plugins", ctl_type::boolean },
  1596. { "load.skip_subsongs_init", ctl_type::boolean },
  1597. { "seek.sync_samples", ctl_type::boolean },
  1598. { "subsong", ctl_type::integer },
  1599. { "play.tempo_factor", ctl_type::floatingpoint },
  1600. { "play.pitch_factor", ctl_type::floatingpoint },
  1601. { "play.at_end", ctl_type::text },
  1602. { "render.resampler.emulate_amiga", ctl_type::boolean },
  1603. { "render.resampler.emulate_amiga_type", ctl_type::text },
  1604. { "render.opl.volume_factor", ctl_type::floatingpoint },
  1605. { "dither", ctl_type::integer }
  1606. };
  1607. return std::make_pair(std::begin(ctl_infos), std::end(ctl_infos));
  1608. }
  1609. std::vector<std::string> module_impl::get_ctls() const {
  1610. std::vector<std::string> result;
  1611. auto ctl_infos = get_ctl_infos();
  1612. result.reserve(std::distance(ctl_infos.first, ctl_infos.second));
  1613. for ( std::ptrdiff_t i = 0; i < std::distance(ctl_infos.first, ctl_infos.second); ++i ) {
  1614. result.push_back(ctl_infos.first[i].name);
  1615. }
  1616. return result;
  1617. }
  1618. std::string module_impl::ctl_get( std::string ctl, bool throw_if_unknown ) const {
  1619. if ( !ctl.empty() ) {
  1620. // cppcheck false-positive
  1621. // cppcheck-suppress containerOutOfBounds
  1622. char rightmost = ctl.back();
  1623. if ( rightmost == '!' || rightmost == '?' ) {
  1624. if ( rightmost == '!' ) {
  1625. throw_if_unknown = true;
  1626. } else if ( rightmost == '?' ) {
  1627. throw_if_unknown = false;
  1628. }
  1629. ctl = ctl.substr( 0, ctl.length() - 1 );
  1630. }
  1631. }
  1632. auto found_ctl = std::find_if(get_ctl_infos().first, get_ctl_infos().second, [&](const ctl_info & info) -> bool { return info.name == ctl; });
  1633. if ( found_ctl == get_ctl_infos().second ) {
  1634. if ( ctl == "" ) {
  1635. throw openmpt::exception("empty ctl");
  1636. } else if ( throw_if_unknown ) {
  1637. throw openmpt::exception("unknown ctl: " + ctl);
  1638. } else {
  1639. return std::string();
  1640. }
  1641. }
  1642. std::string result;
  1643. switch ( found_ctl->type ) {
  1644. case ctl_type::boolean:
  1645. return mpt::format_value_default<std::string>( ctl_get_boolean( ctl, throw_if_unknown ) );
  1646. break;
  1647. case ctl_type::integer:
  1648. return mpt::format_value_default<std::string>( ctl_get_integer( ctl, throw_if_unknown ) );
  1649. break;
  1650. case ctl_type::floatingpoint:
  1651. return mpt::format_value_default<std::string>( ctl_get_floatingpoint( ctl, throw_if_unknown ) );
  1652. break;
  1653. case ctl_type::text:
  1654. return ctl_get_text( ctl, throw_if_unknown );
  1655. break;
  1656. }
  1657. return result;
  1658. }
  1659. bool module_impl::ctl_get_boolean( std::string_view ctl, bool throw_if_unknown ) const {
  1660. if ( !ctl.empty() ) {
  1661. // cppcheck false-positive
  1662. // cppcheck-suppress containerOutOfBounds
  1663. char rightmost = ctl.back();
  1664. if ( rightmost == '!' || rightmost == '?' ) {
  1665. if ( rightmost == '!' ) {
  1666. throw_if_unknown = true;
  1667. } else if ( rightmost == '?' ) {
  1668. throw_if_unknown = false;
  1669. }
  1670. ctl = ctl.substr( 0, ctl.length() - 1 );
  1671. }
  1672. }
  1673. auto found_ctl = std::find_if(get_ctl_infos().first, get_ctl_infos().second, [&](const ctl_info & info) -> bool { return info.name == ctl; });
  1674. if ( found_ctl == get_ctl_infos().second ) {
  1675. if ( ctl == "" ) {
  1676. throw openmpt::exception("empty ctl");
  1677. } else if ( throw_if_unknown ) {
  1678. throw openmpt::exception("unknown ctl: " + std::string(ctl));
  1679. } else {
  1680. return false;
  1681. }
  1682. }
  1683. if ( found_ctl->type != ctl_type::boolean ) {
  1684. throw openmpt::exception("wrong ctl value type");
  1685. }
  1686. if ( ctl == "" ) {
  1687. throw openmpt::exception("empty ctl");
  1688. } else if ( ctl == "load.skip_samples" || ctl == "load_skip_samples" ) {
  1689. return m_ctl_load_skip_samples;
  1690. } else if ( ctl == "load.skip_patterns" || ctl == "load_skip_patterns" ) {
  1691. return m_ctl_load_skip_patterns;
  1692. } else if ( ctl == "load.skip_plugins" ) {
  1693. return m_ctl_load_skip_plugins;
  1694. } else if ( ctl == "load.skip_subsongs_init" ) {
  1695. return m_ctl_load_skip_subsongs_init;
  1696. } else if ( ctl == "seek.sync_samples" ) {
  1697. return m_ctl_seek_sync_samples;
  1698. } else if ( ctl == "render.resampler.emulate_amiga" ) {
  1699. return ( m_sndFile->m_Resampler.m_Settings.emulateAmiga != OpenMPT::Resampling::AmigaFilter::Off );
  1700. } else {
  1701. MPT_ASSERT_NOTREACHED();
  1702. return false;
  1703. }
  1704. }
  1705. std::int64_t module_impl::ctl_get_integer( std::string_view ctl, bool throw_if_unknown ) const {
  1706. if ( !ctl.empty() ) {
  1707. // cppcheck false-positive
  1708. // cppcheck-suppress containerOutOfBounds
  1709. char rightmost = ctl.back();
  1710. if ( rightmost == '!' || rightmost == '?' ) {
  1711. if ( rightmost == '!' ) {
  1712. throw_if_unknown = true;
  1713. } else if ( rightmost == '?' ) {
  1714. throw_if_unknown = false;
  1715. }
  1716. ctl = ctl.substr( 0, ctl.length() - 1 );
  1717. }
  1718. }
  1719. auto found_ctl = std::find_if(get_ctl_infos().first, get_ctl_infos().second, [&](const ctl_info & info) -> bool { return info.name == ctl; });
  1720. if ( found_ctl == get_ctl_infos().second ) {
  1721. if ( ctl == "" ) {
  1722. throw openmpt::exception("empty ctl");
  1723. } else if ( throw_if_unknown ) {
  1724. throw openmpt::exception("unknown ctl: " + std::string(ctl));
  1725. } else {
  1726. return 0;
  1727. }
  1728. }
  1729. if ( found_ctl->type != ctl_type::integer ) {
  1730. throw openmpt::exception("wrong ctl value type");
  1731. }
  1732. if ( ctl == "" ) {
  1733. throw openmpt::exception("empty ctl");
  1734. } else if ( ctl == "subsong" ) {
  1735. return get_selected_subsong();
  1736. } else if ( ctl == "dither" ) {
  1737. return static_cast<std::int64_t>( m_Dithers->GetMode() );
  1738. } else {
  1739. MPT_ASSERT_NOTREACHED();
  1740. return 0;
  1741. }
  1742. }
  1743. double module_impl::ctl_get_floatingpoint( std::string_view ctl, bool throw_if_unknown ) const {
  1744. if ( !ctl.empty() ) {
  1745. // cppcheck false-positive
  1746. // cppcheck-suppress containerOutOfBounds
  1747. char rightmost = ctl.back();
  1748. if ( rightmost == '!' || rightmost == '?' ) {
  1749. if ( rightmost == '!' ) {
  1750. throw_if_unknown = true;
  1751. } else if ( rightmost == '?' ) {
  1752. throw_if_unknown = false;
  1753. }
  1754. ctl = ctl.substr( 0, ctl.length() - 1 );
  1755. }
  1756. }
  1757. auto found_ctl = std::find_if(get_ctl_infos().first, get_ctl_infos().second, [&](const ctl_info & info) -> bool { return info.name == ctl; });
  1758. if ( found_ctl == get_ctl_infos().second ) {
  1759. if ( ctl == "" ) {
  1760. throw openmpt::exception("empty ctl");
  1761. } else if ( throw_if_unknown ) {
  1762. throw openmpt::exception("unknown ctl: " + std::string(ctl));
  1763. } else {
  1764. return 0.0;
  1765. }
  1766. }
  1767. if ( found_ctl->type != ctl_type::floatingpoint ) {
  1768. throw openmpt::exception("wrong ctl value type");
  1769. }
  1770. if ( ctl == "" ) {
  1771. throw openmpt::exception("empty ctl");
  1772. } else if ( ctl == "play.tempo_factor" ) {
  1773. if ( !is_loaded() ) {
  1774. return 1.0;
  1775. }
  1776. return 65536.0 / m_sndFile->m_nTempoFactor;
  1777. } else if ( ctl == "play.pitch_factor" ) {
  1778. if ( !is_loaded() ) {
  1779. return 1.0;
  1780. }
  1781. return m_sndFile->m_nFreqFactor / 65536.0;
  1782. } else if ( ctl == "render.opl.volume_factor" ) {
  1783. return static_cast<double>( m_sndFile->m_OPLVolumeFactor ) / static_cast<double>( OpenMPT::CSoundFile::m_OPLVolumeFactorScale );
  1784. } else {
  1785. MPT_ASSERT_NOTREACHED();
  1786. return 0.0;
  1787. }
  1788. }
  1789. std::string module_impl::ctl_get_text( std::string_view ctl, bool throw_if_unknown ) const {
  1790. if ( !ctl.empty() ) {
  1791. // cppcheck false-positive
  1792. // cppcheck-suppress containerOutOfBounds
  1793. char rightmost = ctl.back();
  1794. if ( rightmost == '!' || rightmost == '?' ) {
  1795. if ( rightmost == '!' ) {
  1796. throw_if_unknown = true;
  1797. } else if ( rightmost == '?' ) {
  1798. throw_if_unknown = false;
  1799. }
  1800. ctl = ctl.substr( 0, ctl.length() - 1 );
  1801. }
  1802. }
  1803. auto found_ctl = std::find_if(get_ctl_infos().first, get_ctl_infos().second, [&](const ctl_info & info) -> bool { return info.name == ctl; });
  1804. if ( found_ctl == get_ctl_infos().second ) {
  1805. if ( ctl == "" ) {
  1806. throw openmpt::exception("empty ctl");
  1807. } else if ( throw_if_unknown ) {
  1808. throw openmpt::exception("unknown ctl: " + std::string(ctl));
  1809. } else {
  1810. return std::string();
  1811. }
  1812. }
  1813. if ( ctl == "" ) {
  1814. throw openmpt::exception("empty ctl");
  1815. } else if ( ctl == "play.at_end" ) {
  1816. switch ( m_ctl_play_at_end )
  1817. {
  1818. case song_end_action::fadeout_song:
  1819. return "fadeout";
  1820. case song_end_action::continue_song:
  1821. return "continue";
  1822. case song_end_action::stop_song:
  1823. return "stop";
  1824. default:
  1825. return std::string();
  1826. }
  1827. } else if ( ctl == "render.resampler.emulate_amiga_type" ) {
  1828. switch ( m_ctl_render_resampler_emulate_amiga_type ) {
  1829. case amiga_filter_type::a500:
  1830. return "a500";
  1831. case amiga_filter_type::a1200:
  1832. return "a1200";
  1833. case amiga_filter_type::unfiltered:
  1834. return "unfiltered";
  1835. case amiga_filter_type::auto_filter:
  1836. return "auto";
  1837. default:
  1838. return std::string();
  1839. }
  1840. } else {
  1841. MPT_ASSERT_NOTREACHED();
  1842. return std::string();
  1843. }
  1844. }
  1845. void module_impl::ctl_set( std::string ctl, const std::string & value, bool throw_if_unknown ) {
  1846. if ( !ctl.empty() ) {
  1847. // cppcheck false-positive
  1848. // cppcheck-suppress containerOutOfBounds
  1849. char rightmost = ctl.back();
  1850. if ( rightmost == '!' || rightmost == '?' ) {
  1851. if ( rightmost == '!' ) {
  1852. throw_if_unknown = true;
  1853. } else if ( rightmost == '?' ) {
  1854. throw_if_unknown = false;
  1855. }
  1856. ctl = ctl.substr( 0, ctl.length() - 1 );
  1857. }
  1858. }
  1859. auto found_ctl = std::find_if(get_ctl_infos().first, get_ctl_infos().second, [&](const ctl_info & info) -> bool { return info.name == ctl; });
  1860. if ( found_ctl == get_ctl_infos().second ) {
  1861. if ( ctl == "" ) {
  1862. throw openmpt::exception("empty ctl: := " + value);
  1863. } else if ( throw_if_unknown ) {
  1864. throw openmpt::exception("unknown ctl: " + ctl + " := " + value);
  1865. } else {
  1866. return;
  1867. }
  1868. }
  1869. switch ( found_ctl->type ) {
  1870. case ctl_type::boolean:
  1871. ctl_set_boolean( ctl, mpt::ConvertStringTo<bool>( value ), throw_if_unknown );
  1872. break;
  1873. case ctl_type::integer:
  1874. ctl_set_integer( ctl, mpt::ConvertStringTo<std::int64_t>( value ), throw_if_unknown );
  1875. break;
  1876. case ctl_type::floatingpoint:
  1877. ctl_set_floatingpoint( ctl, mpt::ConvertStringTo<double>( value ), throw_if_unknown );
  1878. break;
  1879. case ctl_type::text:
  1880. ctl_set_text( ctl, value, throw_if_unknown );
  1881. break;
  1882. }
  1883. }
  1884. void module_impl::ctl_set_boolean( std::string_view ctl, bool value, bool throw_if_unknown ) {
  1885. if ( !ctl.empty() ) {
  1886. // cppcheck false-positive
  1887. // cppcheck-suppress containerOutOfBounds
  1888. char rightmost = ctl.back();
  1889. if ( rightmost == '!' || rightmost == '?' ) {
  1890. if ( rightmost == '!' ) {
  1891. throw_if_unknown = true;
  1892. } else if ( rightmost == '?' ) {
  1893. throw_if_unknown = false;
  1894. }
  1895. ctl = ctl.substr( 0, ctl.length() - 1 );
  1896. }
  1897. }
  1898. auto found_ctl = std::find_if(get_ctl_infos().first, get_ctl_infos().second, [&](const ctl_info & info) -> bool { return info.name == ctl; });
  1899. if ( found_ctl == get_ctl_infos().second ) {
  1900. if ( ctl == "" ) {
  1901. throw openmpt::exception("empty ctl: := " + mpt::format_value_default<std::string>( value ) );
  1902. } else if ( throw_if_unknown ) {
  1903. throw openmpt::exception("unknown ctl: " + std::string(ctl) + " := " + mpt::format_value_default<std::string>(value));
  1904. } else {
  1905. return;
  1906. }
  1907. }
  1908. if ( ctl == "" ) {
  1909. throw openmpt::exception("empty ctl: := " + mpt::format_value_default<std::string>( value ) );
  1910. } else if ( ctl == "load.skip_samples" || ctl == "load_skip_samples" ) {
  1911. m_ctl_load_skip_samples = value;
  1912. } else if ( ctl == "load.skip_patterns" || ctl == "load_skip_patterns" ) {
  1913. m_ctl_load_skip_patterns = value;
  1914. } else if ( ctl == "load.skip_plugins" ) {
  1915. m_ctl_load_skip_plugins = value;
  1916. } else if ( ctl == "load.skip_subsongs_init" ) {
  1917. m_ctl_load_skip_subsongs_init = value;
  1918. } else if ( ctl == "seek.sync_samples" ) {
  1919. m_ctl_seek_sync_samples = value;
  1920. } else if ( ctl == "render.resampler.emulate_amiga" ) {
  1921. OpenMPT::CResamplerSettings newsettings = m_sndFile->m_Resampler.m_Settings;
  1922. const bool enabled = value;
  1923. if ( enabled )
  1924. newsettings.emulateAmiga = translate_amiga_filter_type( m_ctl_render_resampler_emulate_amiga_type );
  1925. else
  1926. newsettings.emulateAmiga = OpenMPT::Resampling::AmigaFilter::Off;
  1927. if ( newsettings != m_sndFile->m_Resampler.m_Settings ) {
  1928. m_sndFile->SetResamplerSettings( newsettings );
  1929. }
  1930. } else {
  1931. MPT_ASSERT_NOTREACHED();
  1932. }
  1933. }
  1934. void module_impl::ctl_set_integer( std::string_view ctl, std::int64_t value, bool throw_if_unknown ) {
  1935. if ( !ctl.empty() ) {
  1936. // cppcheck false-positive
  1937. // cppcheck-suppress containerOutOfBounds
  1938. char rightmost = ctl.back();
  1939. if ( rightmost == '!' || rightmost == '?' ) {
  1940. if ( rightmost == '!' ) {
  1941. throw_if_unknown = true;
  1942. } else if ( rightmost == '?' ) {
  1943. throw_if_unknown = false;
  1944. }
  1945. ctl = ctl.substr( 0, ctl.length() - 1 );
  1946. }
  1947. }
  1948. auto found_ctl = std::find_if(get_ctl_infos().first, get_ctl_infos().second, [&](const ctl_info & info) -> bool { return info.name == ctl; });
  1949. if ( found_ctl == get_ctl_infos().second ) {
  1950. if ( ctl == "" ) {
  1951. throw openmpt::exception("empty ctl: := " + mpt::format_value_default<std::string>( value ) );
  1952. } else if ( throw_if_unknown ) {
  1953. throw openmpt::exception("unknown ctl: " + std::string(ctl) + " := " + mpt::format_value_default<std::string>(value));
  1954. } else {
  1955. return;
  1956. }
  1957. }
  1958. if ( ctl == "" ) {
  1959. throw openmpt::exception("empty ctl: := " + mpt::format_value_default<std::string>( value ) );
  1960. } else if ( ctl == "subsong" ) {
  1961. select_subsong( mpt::saturate_cast<std::int32_t>( value ) );
  1962. } else if ( ctl == "dither" ) {
  1963. std::size_t dither = mpt::saturate_cast<std::size_t>( value );
  1964. if ( dither >= OpenMPT::DithersOpenMPT::GetNumDithers() ) {
  1965. dither = OpenMPT::DithersOpenMPT::GetDefaultDither();
  1966. }
  1967. m_Dithers->SetMode( dither );
  1968. } else {
  1969. MPT_ASSERT_NOTREACHED();
  1970. }
  1971. }
  1972. void module_impl::ctl_set_floatingpoint( std::string_view ctl, double value, bool throw_if_unknown ) {
  1973. if ( !ctl.empty() ) {
  1974. // cppcheck false-positive
  1975. // cppcheck-suppress containerOutOfBounds
  1976. char rightmost = ctl.back();
  1977. if ( rightmost == '!' || rightmost == '?' ) {
  1978. if ( rightmost == '!' ) {
  1979. throw_if_unknown = true;
  1980. } else if ( rightmost == '?' ) {
  1981. throw_if_unknown = false;
  1982. }
  1983. ctl = ctl.substr( 0, ctl.length() - 1 );
  1984. }
  1985. }
  1986. auto found_ctl = std::find_if(get_ctl_infos().first, get_ctl_infos().second, [&](const ctl_info & info) -> bool { return info.name == ctl; });
  1987. if ( found_ctl == get_ctl_infos().second ) {
  1988. if ( ctl == "" ) {
  1989. throw openmpt::exception("empty ctl: := " + mpt::format_value_default<std::string>( value ) );
  1990. } else if ( throw_if_unknown ) {
  1991. throw openmpt::exception("unknown ctl: " + std::string(ctl) + " := " + mpt::format_value_default<std::string>(value));
  1992. } else {
  1993. return;
  1994. }
  1995. }
  1996. if ( ctl == "" ) {
  1997. throw openmpt::exception("empty ctl: := " + mpt::format_value_default<std::string>( value ) );
  1998. } else if ( ctl == "play.tempo_factor" ) {
  1999. if ( !is_loaded() ) {
  2000. return;
  2001. }
  2002. double factor = value;
  2003. if ( factor <= 0.0 || factor > 4.0 ) {
  2004. throw openmpt::exception("invalid tempo factor");
  2005. }
  2006. m_sndFile->m_nTempoFactor = mpt::saturate_round<uint32_t>( 65536.0 / factor );
  2007. m_sndFile->RecalculateSamplesPerTick();
  2008. } else if ( ctl == "play.pitch_factor" ) {
  2009. if ( !is_loaded() ) {
  2010. return;
  2011. }
  2012. double factor = value;
  2013. if ( factor <= 0.0 || factor > 4.0 ) {
  2014. throw openmpt::exception("invalid pitch factor");
  2015. }
  2016. m_sndFile->m_nFreqFactor = mpt::saturate_round<uint32_t>( 65536.0 * factor );
  2017. m_sndFile->RecalculateSamplesPerTick();
  2018. } else if ( ctl == "render.opl.volume_factor" ) {
  2019. m_sndFile->m_OPLVolumeFactor = mpt::saturate_round<std::int32_t>( value * static_cast<double>( OpenMPT::CSoundFile::m_OPLVolumeFactorScale ) );
  2020. } else {
  2021. MPT_ASSERT_NOTREACHED();
  2022. }
  2023. }
  2024. void module_impl::ctl_set_text( std::string_view ctl, std::string_view value, bool throw_if_unknown ) {
  2025. if ( !ctl.empty() ) {
  2026. // cppcheck false-positive
  2027. // cppcheck-suppress containerOutOfBounds
  2028. char rightmost = ctl.back();
  2029. if ( rightmost == '!' || rightmost == '?' ) {
  2030. if ( rightmost == '!' ) {
  2031. throw_if_unknown = true;
  2032. } else if ( rightmost == '?' ) {
  2033. throw_if_unknown = false;
  2034. }
  2035. ctl = ctl.substr( 0, ctl.length() - 1 );
  2036. }
  2037. }
  2038. auto found_ctl = std::find_if(get_ctl_infos().first, get_ctl_infos().second, [&](const ctl_info & info) -> bool { return info.name == ctl; });
  2039. if ( found_ctl == get_ctl_infos().second ) {
  2040. if ( ctl == "" ) {
  2041. throw openmpt::exception("empty ctl: := " + std::string( value ) );
  2042. } else if ( throw_if_unknown ) {
  2043. throw openmpt::exception("unknown ctl: " + std::string(ctl) + " := " + std::string(value));
  2044. } else {
  2045. return;
  2046. }
  2047. }
  2048. if ( ctl == "" ) {
  2049. throw openmpt::exception("empty ctl: := " + std::string( value ) );
  2050. } else if ( ctl == "play.at_end" ) {
  2051. if ( value == "fadeout" ) {
  2052. m_ctl_play_at_end = song_end_action::fadeout_song;
  2053. } else if(value == "continue") {
  2054. m_ctl_play_at_end = song_end_action::continue_song;
  2055. } else if(value == "stop") {
  2056. m_ctl_play_at_end = song_end_action::stop_song;
  2057. } else {
  2058. throw openmpt::exception("unknown song end action:" + std::string(value));
  2059. }
  2060. } else if ( ctl == "render.resampler.emulate_amiga_type" ) {
  2061. if ( value == "a500" ) {
  2062. m_ctl_render_resampler_emulate_amiga_type = amiga_filter_type::a500;
  2063. } else if ( value == "a1200" ) {
  2064. m_ctl_render_resampler_emulate_amiga_type = amiga_filter_type::a1200;
  2065. } else if ( value == "unfiltered" ) {
  2066. m_ctl_render_resampler_emulate_amiga_type = amiga_filter_type::unfiltered;
  2067. } else if ( value == "auto" ) {
  2068. m_ctl_render_resampler_emulate_amiga_type = amiga_filter_type::auto_filter;
  2069. } else {
  2070. throw openmpt::exception( "invalid amiga filter type" );
  2071. }
  2072. if ( m_sndFile->m_Resampler.m_Settings.emulateAmiga != OpenMPT::Resampling::AmigaFilter::Off ) {
  2073. OpenMPT::CResamplerSettings newsettings = m_sndFile->m_Resampler.m_Settings;
  2074. newsettings.emulateAmiga = translate_amiga_filter_type( m_ctl_render_resampler_emulate_amiga_type );
  2075. if ( newsettings != m_sndFile->m_Resampler.m_Settings ) {
  2076. m_sndFile->SetResamplerSettings( newsettings );
  2077. }
  2078. }
  2079. } else {
  2080. MPT_ASSERT_NOTREACHED();
  2081. }
  2082. }
  2083. } // namespace openmpt