NativeEngines.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #include "core/NativeEngines.h"
  2. #include <algorithm>
  3. #include "core/Effects.h"
  4. #include "core/FlEffects.h"
  5. #include "core/GrossBeat.h"
  6. #include "core/LynxBanks.h"
  7. #include "core/MidiOut.h"
  8. #include "core/PhysicalModels.h"
  9. #include "flp/Flp.h"
  10. namespace lynx::natives
  11. {
  12. namespace
  13. {
  14. constexpr auto fl = Needs::flStudio;
  15. constexpr auto own = Needs::nothing;
  16. std::string lower (std::string s)
  17. {
  18. std::transform (s.begin(), s.end(), s.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
  19. return s;
  20. }
  21. bool endsWith (const std::string& s, const std::string& tail)
  22. {
  23. return s.size() >= tail.size() && lower (s.substr (s.size() - tail.size())) == tail;
  24. }
  25. // The plugin's name as FL's import knows it (event 0xC9 of a channel / slot)
  26. const char* flPluginName (InstrumentKind kind)
  27. {
  28. switch (kind)
  29. {
  30. case InstrumentKind::toxic: return "Toxic Biohazard";
  31. case InstrumentKind::gms: return "GMS";
  32. case InstrumentKind::morphine: return "Morphine";
  33. default: return "";
  34. }
  35. }
  36. std::vector<std::uint8_t> readPreset (const std::string& path, const FlAccess& access)
  37. {
  38. if (path.empty() || ! access.readFile)
  39. return {};
  40. return access.readFile (path);
  41. }
  42. FlpImportOptions importOptions (const FlAccess& access)
  43. {
  44. FlpImportOptions options;
  45. options.roots = access.roots;
  46. options.fileExists = access.fileExists;
  47. options.readFile = access.readFile;
  48. options.saveEmbedded = access.saveEmbedded;
  49. return options;
  50. }
  51. std::optional<ChannelDef> channelFromPreset (InstrumentKind kind, const InstrumentEngine& engine, const std::string& path, const FlAccess& access)
  52. {
  53. const auto bytes = readPreset (path, access);
  54. if (bytes.empty())
  55. return std::nullopt;
  56. try
  57. {
  58. flp::Project project;
  59. if (! endsWith (path, ".fst")) // Toxic Biohazard's .tbio, GMS' .gmsynth, Morphine's .mrp
  60. {
  61. flp::Channel c;
  62. c.pluginName = flPluginName (kind);
  63. // the state FL saves is "SSLF" + size + the preset file
  64. c.pluginState = { 'S', 'S', 'L', 'F' };
  65. for (int i = 0; i < 4; ++i)
  66. c.pluginState.push_back ((std::uint8_t) (bytes.size() >> (8 * i)));
  67. c.pluginState.insert (c.pluginState.end(), bytes.begin(), bytes.end());
  68. project.channels.push_back (c);
  69. }
  70. else
  71. project = flp::parseProject (bytes, true);
  72. if (project.channels.empty())
  73. return std::nullopt;
  74. auto& c = project.channels.front();
  75. c.name = engine.name;
  76. if (c.type == flp::ChannelType::unknown)
  77. c.type = flp::ChannelType::instrument;
  78. c.mixerTrack = -1;
  79. const auto song = importFlp (project, importOptions (access));
  80. if (song.channels.empty() || song.channels.front().instrument != kind)
  81. return std::nullopt;
  82. auto def = song.channels.front();
  83. def.name = engine.name;
  84. def.mixerTrack = 0;
  85. return def;
  86. }
  87. catch (const flp::Error&)
  88. {
  89. return std::nullopt;
  90. }
  91. }
  92. } // namespace
  93. const std::vector<InstrumentEngine>& instrumentEngines()
  94. {
  95. // name, preset folder (Generators), needs, beta. Engines that play from an empty state (LYNX's own defaults) need
  96. // nothing; the rest start from the user's FL "Default" preset (with its data files: DLL tables, wave folders...).
  97. static const std::vector<InstrumentEngine> list = {
  98. { InstrumentKind::threeosc, "3x Osc", "3x Osc", own, false },
  99. { InstrumentKind::sytrus, "Sytrus", "Sytrus", fl, false },
  100. { InstrumentKind::harmor, "Harmor", "Harmor", fl, true },
  101. { InstrumentKind::harmless, "Harmless", "Harmless", own, false },
  102. { InstrumentKind::gms, "GMS", "GMS", fl, false },
  103. { InstrumentKind::morphine, "Morphine", "Morphine", fl, false },
  104. { InstrumentKind::ogun, "Ogun", "Ogun", fl, false },
  105. { InstrumentKind::toxic, "Toxic Biohazard", "Toxic Biohazard", fl, false },
  106. { InstrumentKind::poizone, "PoiZone", "PoiZone", fl, false },
  107. { InstrumentKind::sakura, "Sakura", "Sakura", fl, false },
  108. { InstrumentKind::kepler, "Kepler", "Kepler", fl, false },
  109. { InstrumentKind::keplerExo, "Kepler Exo", "Kepler Exo", fl, false },
  110. { InstrumentKind::transistorbass, "Transistor Bass", "Transistor Bass", fl, false },
  111. { InstrumentKind::boobass, "BooBass", "", fl, false }, // (its multisample: the user's BooBass_Eng_x64.dll)
  112. { InstrumentKind::simsynth, "SimSynth", "SimSynth", own, false },
  113. { InstrumentKind::minisynth, "MiniSynth", "MiniSynth", fl, false },
  114. { InstrumentKind::dx10, "Fruity DX10", "Fruity DX10", own, false },
  115. { InstrumentKind::plucked, "Plucked!", "Plucked!", own, false },
  116. { InstrumentKind::sawer, "Sawer", "Sawer", fl, false },
  117. { InstrumentKind::beepmap, "BeepMap", "BeepMap", fl, false },
  118. { InstrumentKind::flkeys, "FL Keys", "FL Keys", fl, false },
  119. { InstrumentKind::slayer, "FL Slayer", "FL Slayer", fl, false },
  120. { InstrumentKind::granulizer, "Fruity Granulizer", "Fruity Granulizer", fl, false },
  121. { InstrumentKind::wavetraveller, "Wave Traveller", "Wave Traveller", own, false },
  122. { InstrumentKind::drumaxx, "Drumaxx", "Drumaxx", fl, false },
  123. { InstrumentKind::drumpad, "Drumpad", "Drumpad", fl, false },
  124. { InstrumentKind::fruitkick, "Fruit Kick", "Fruit Kick", fl, false },
  125. { InstrumentKind::bassdrum, "BassDrum", "BassDrum", fl, false },
  126. };
  127. return list;
  128. }
  129. const std::vector<EffectEngine>& effectEngines()
  130. {
  131. static const std::vector<EffectEngine> list = {
  132. { EffectKind::maximus, "Maximus", fl, false },
  133. { EffectKind::vocodex, "Vocodex", fl, false },
  134. { EffectKind::equo, "EQUO", fl, false },
  135. { EffectKind::distructor, "Distructor", fl, false },
  136. { EffectKind::lovePhilter, "Fruity Love Philter", fl, false },
  137. { EffectKind::grossBeat, "Gross Beat", fl, false },
  138. { EffectKind::convolver, "Fruity Convolver", fl, false },
  139. { EffectKind::flPitchShifter, "Pitch Shifter", own, false },
  140. { EffectKind::vintagePhaser, "Vintage Phaser", own, false },
  141. { EffectKind::squeeze, "Fruity Squeeze", own, false },
  142. { EffectKind::rhythmGain, "", own, false },
  143. { EffectKind::newtone, "", own, false }, // out of beta in round 165g (REA_rodada165g-flnewt165)
  144. { EffectKind::newtime, "", own, false }, // out of beta in round 167b (REA_rodada167b)
  145. };
  146. return list;
  147. }
  148. const InstrumentEngine* instrumentEngine (InstrumentKind kind)
  149. {
  150. for (const auto& e : instrumentEngines())
  151. if (e.kind == kind)
  152. return &e;
  153. return nullptr;
  154. }
  155. const EffectEngine* effectEngine (EffectKind kind)
  156. {
  157. for (const auto& e : effectEngines())
  158. if (e.kind == kind)
  159. return &e;
  160. return nullptr;
  161. }
  162. std::vector<std::string> presetCandidates (const std::string& presetFolder, bool generator, const FlAccess& access)
  163. {
  164. std::vector<std::string> out;
  165. if (presetFolder.empty() || access.roots.data.empty())
  166. return out;
  167. const auto folder = std::string ("%FLStudioData%\\Patches\\Plugin presets\\") + (generator ? "Generators\\" : "Effects\\") + presetFolder;
  168. const auto exists = [&access] (const std::string& p) { return ! p.empty() && access.fileExists && access.fileExists (p); };
  169. for (const auto* name : { "Default.fst", "Default.tbio", "Default\\Default.tbio" })
  170. if (const auto path = resolveFlPath (folder + "\\" + name, access.roots); exists (path))
  171. out.push_back (path);
  172. // then the plugin's entry in FL's Plugin database (Generators\<category>\<plugin>.fst): the state FL itself adds
  173. // when the plugin is picked (Drumaxx' "Default Kit", REA_rodada169e §1; FL ships no Default.fst for Drumaxx,
  174. // Drumpad, BassDrum, Sakura...). Before any other preset of the folder.
  175. if (generator && access.listPresets)
  176. {
  177. const auto wanted = lower (presetFolder + ".fst");
  178. for (const auto& path : access.listPresets (resolveFlPath ("%FLStudioData%\\Patches\\Plugin database\\Generators", access.roots)))
  179. {
  180. const auto cut = path.find_last_of ("\\/");
  181. if (lower (cut == std::string::npos ? path : path.substr (cut + 1)) == wanted
  182. && std::find (out.begin(), out.end(), path) == out.end())
  183. {
  184. out.push_back (path);
  185. break;
  186. }
  187. }
  188. }
  189. if (access.listPresets)
  190. for (const auto& path : access.listPresets (resolveFlPath (folder, access.roots)))
  191. if ((endsWith (path, ".fst") || endsWith (path, ".tbio") || endsWith (path, ".gmsynth") || endsWith (path, ".mrp"))
  192. && std::find (out.begin(), out.end(), path) == out.end())
  193. out.push_back (path);
  194. return out;
  195. }
  196. std::string defaultPresetPath (const std::string& presetFolder, bool generator, const FlAccess& access)
  197. {
  198. const auto c = presetCandidates (presetFolder, generator, access);
  199. return c.empty() ? std::string() : c.front();
  200. }
  201. namespace
  202. {
  203. // BooBass has no preset folder: a new one is its three knobs at their defaults (0.5, REA_rodada29 §4) over the
  204. // multisample of the user's own engine DLL
  205. std::string booBassDll (const FlAccess& access)
  206. {
  207. const auto dll = resolveFlPath ("%FLStudioFactoryData%\\Plugins\\Fruity\\Generators\\BooBass\\BooBass_Eng_x64.dll", access.roots);
  208. return ! dll.empty() && access.fileExists && access.fileExists (dll) ? dll : std::string();
  209. }
  210. } // namespace
  211. std::optional<ChannelDef> channelFromPresetFile (InstrumentKind kind, const std::string& path, const FlAccess& access)
  212. {
  213. const auto* engine = instrumentEngine (kind);
  214. return engine != nullptr ? channelFromPreset (kind, *engine, path, access) : std::nullopt;
  215. }
  216. std::string presetFolderOf (InstrumentKind kind)
  217. {
  218. if (kind == InstrumentKind::midiOut)
  219. return "MIDI Out";
  220. const auto* engine = instrumentEngine (kind);
  221. return engine != nullptr ? engine->presetFolder : "";
  222. }
  223. std::optional<ChannelDef> withPreset (const ChannelDef& current, const std::string& path, const std::string& presetName, const FlAccess& access)
  224. {
  225. ChannelDef out;
  226. if (current.instrument == InstrumentKind::midiOut)
  227. {
  228. const auto loaded = midiout::parse (midiout::stateFromFst (readPreset (path, access)));
  229. if (! loaded)
  230. return std::nullopt;
  231. auto state = *loaded;
  232. const auto now = midiout::stateOf (current.pluginState); // (FL keeps where it plays: channel and port)
  233. state.channel = now.channel;
  234. state.port = now.port;
  235. out = current;
  236. out.pluginState = midiout::write (state);
  237. }
  238. else
  239. {
  240. auto def = channelFromPresetFile (current.instrument, path, access);
  241. if (! def)
  242. return std::nullopt;
  243. // a plugin preset holds the plugin's state only: the channel's own settings stay (FL's wrapper loads the
  244. // .fst into the plugin)
  245. out = current;
  246. out.instrument = def->instrument;
  247. out.samplePath = def->samplePath;
  248. out.zones = def->zones;
  249. out.modelParams = def->modelParams;
  250. out.pluginState = def->pluginState;
  251. out.controllerState = def->controllerState;
  252. out.synth = def->synth;
  253. out.keyLabels = def->keyLabels;
  254. }
  255. if (! presetName.empty())
  256. out.name = presetName; // FL names the channel after the preset (measured, round 167o)
  257. return out;
  258. }
  259. bool available (const InstrumentEngine& e, const FlAccess& access)
  260. {
  261. if (e.kind == InstrumentKind::boobass)
  262. return ! booBassDll (access).empty();
  263. // an engine with a LYNX preset bank starts from it without FL (round 168n)
  264. return e.needs == Needs::nothing || banks::hasBank (e.kind) || ! defaultPresetPath (e.presetFolder, true, access).empty();
  265. }
  266. bool available (const EffectEngine& e, const FlAccess& access)
  267. {
  268. return e.needs == Needs::nothing || ! defaultPresetPath (e.presetFolder, false, access).empty();
  269. }
  270. std::optional<ChannelDef> makeChannel (InstrumentKind kind, const FlAccess& access)
  271. {
  272. const auto* engine = instrumentEngine (kind);
  273. if (engine == nullptr)
  274. return std::nullopt;
  275. if (kind == InstrumentKind::boobass)
  276. {
  277. const auto dll = booBassDll (access);
  278. if (dll.empty() || ! booBassDllUsable (dll))
  279. return std::nullopt;
  280. ChannelDef def;
  281. def.name = engine->name;
  282. def.instrument = kind;
  283. def.samplePath = dll;
  284. def.modelParams = { 32768.0f, 32768.0f, 32768.0f };
  285. return def;
  286. }
  287. // the user's FL default preset, imported like an .flp channel (the exact engine with its data files)
  288. int tries = 0;
  289. for (const auto& path : presetCandidates (engine->presetFolder, true, access))
  290. {
  291. if (++tries > 60)
  292. break;
  293. if (auto def = channelFromPreset (kind, *engine, path, access))
  294. return def;
  295. }
  296. // without FL: the first preset of LYNX's own bank (round 168n)
  297. if (auto def = banks::defaultChannel (kind, engine->name))
  298. return def;
  299. if (engine->needs != Needs::nothing)
  300. return std::nullopt;
  301. ChannelDef def; // LYNX's own default: the engine plays from an empty state
  302. def.name = engine->name;
  303. def.instrument = kind;
  304. return def;
  305. }
  306. std::optional<EffectSlot> makeSlot (EffectKind kind, const FlAccess& access, double bpm)
  307. {
  308. const auto* engine = effectEngine (kind);
  309. if (engine == nullptr)
  310. return std::nullopt;
  311. if (const auto path = defaultPresetPath (engine->presetFolder, false, access); ! path.empty())
  312. {
  313. const auto bytes = readPreset (path, access);
  314. try
  315. {
  316. const auto project = flp::parseProject (bytes, true);
  317. if (! project.channels.empty() && ! project.channels.front().pluginState.empty())
  318. {
  319. const FlDataReader data = [&access] (const std::string& relative) -> std::vector<std::uint8_t> {
  320. const auto p = resolveFlPath ("%FLStudioFactoryData%\\" + relative, access.roots);
  321. if (p.empty() || ! access.readFile || (access.fileExists && ! access.fileExists (p)))
  322. return {};
  323. return access.readFile (p);
  324. };
  325. auto slot = effectFromFl (project.channels.front().pluginName.empty() ? engine->presetFolder : project.channels.front().pluginName,
  326. project.channels.front().pluginState, bpm, data);
  327. if (slot && slot->kind == kind)
  328. {
  329. if (kind == EffectKind::grossBeat)
  330. grossbeat::setPpq (slot->state, 96);
  331. return slot;
  332. }
  333. }
  334. }
  335. catch (const flp::Error&)
  336. {
  337. }
  338. }
  339. if (engine->needs != Needs::nothing)
  340. return std::nullopt;
  341. return makeEffectSlot (kind);
  342. }
  343. std::optional<EffectSlot> newEffect (EffectKind kind, const FlAccess& access, double bpm)
  344. {
  345. if (effectEngine (kind) != nullptr)
  346. return makeSlot (kind, access, bpm);
  347. return makeEffectSlot (kind);
  348. }
  349. std::string effectMenuName (EffectKind kind)
  350. {
  351. std::string name = effectInfo (kind).name;
  352. if (const auto* e = effectEngine (kind); e != nullptr && e->beta)
  353. name += " (beta)";
  354. return name;
  355. }
  356. std::string instrumentMenuName (const InstrumentEngine& e)
  357. {
  358. return std::string (e.name) + (e.beta ? " (beta)" : "");
  359. }
  360. } // namespace lynx::natives