| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397 |
- #include "core/NativeEngines.h"
- #include <algorithm>
- #include "core/Effects.h"
- #include "core/FlEffects.h"
- #include "core/GrossBeat.h"
- #include "core/LynxBanks.h"
- #include "core/MidiOut.h"
- #include "core/PhysicalModels.h"
- #include "flp/Flp.h"
- namespace lynx::natives
- {
- namespace
- {
- constexpr auto fl = Needs::flStudio;
- constexpr auto own = Needs::nothing;
- std::string lower (std::string s)
- {
- std::transform (s.begin(), s.end(), s.begin(), [] (unsigned char c) { return (char) std::tolower (c); });
- return s;
- }
- bool endsWith (const std::string& s, const std::string& tail)
- {
- return s.size() >= tail.size() && lower (s.substr (s.size() - tail.size())) == tail;
- }
- // The plugin's name as FL's import knows it (event 0xC9 of a channel / slot)
- const char* flPluginName (InstrumentKind kind)
- {
- switch (kind)
- {
- case InstrumentKind::toxic: return "Toxic Biohazard";
- case InstrumentKind::gms: return "GMS";
- case InstrumentKind::morphine: return "Morphine";
- default: return "";
- }
- }
- std::vector<std::uint8_t> readPreset (const std::string& path, const FlAccess& access)
- {
- if (path.empty() || ! access.readFile)
- return {};
- return access.readFile (path);
- }
- FlpImportOptions importOptions (const FlAccess& access)
- {
- FlpImportOptions options;
- options.roots = access.roots;
- options.fileExists = access.fileExists;
- options.readFile = access.readFile;
- options.saveEmbedded = access.saveEmbedded;
- return options;
- }
- std::optional<ChannelDef> channelFromPreset (InstrumentKind kind, const InstrumentEngine& engine, const std::string& path, const FlAccess& access)
- {
- const auto bytes = readPreset (path, access);
- if (bytes.empty())
- return std::nullopt;
- try
- {
- flp::Project project;
- if (! endsWith (path, ".fst")) // Toxic Biohazard's .tbio, GMS' .gmsynth, Morphine's .mrp
- {
- flp::Channel c;
- c.pluginName = flPluginName (kind);
- // the state FL saves is "SSLF" + size + the preset file
- c.pluginState = { 'S', 'S', 'L', 'F' };
- for (int i = 0; i < 4; ++i)
- c.pluginState.push_back ((std::uint8_t) (bytes.size() >> (8 * i)));
- c.pluginState.insert (c.pluginState.end(), bytes.begin(), bytes.end());
- project.channels.push_back (c);
- }
- else
- project = flp::parseProject (bytes, true);
- if (project.channels.empty())
- return std::nullopt;
- auto& c = project.channels.front();
- c.name = engine.name;
- if (c.type == flp::ChannelType::unknown)
- c.type = flp::ChannelType::instrument;
- c.mixerTrack = -1;
- const auto song = importFlp (project, importOptions (access));
- if (song.channels.empty() || song.channels.front().instrument != kind)
- return std::nullopt;
- auto def = song.channels.front();
- def.name = engine.name;
- def.mixerTrack = 0;
- return def;
- }
- catch (const flp::Error&)
- {
- return std::nullopt;
- }
- }
- } // namespace
- const std::vector<InstrumentEngine>& instrumentEngines()
- {
- // name, preset folder (Generators), needs, beta. Engines that play from an empty state (LYNX's own defaults) need
- // nothing; the rest start from the user's FL "Default" preset (with its data files: DLL tables, wave folders...).
- static const std::vector<InstrumentEngine> list = {
- { InstrumentKind::threeosc, "3x Osc", "3x Osc", own, false },
- { InstrumentKind::sytrus, "Sytrus", "Sytrus", fl, false },
- { InstrumentKind::harmor, "Harmor", "Harmor", fl, true },
- { InstrumentKind::harmless, "Harmless", "Harmless", own, false },
- { InstrumentKind::gms, "GMS", "GMS", fl, false },
- { InstrumentKind::morphine, "Morphine", "Morphine", fl, false },
- { InstrumentKind::ogun, "Ogun", "Ogun", fl, false },
- { InstrumentKind::toxic, "Toxic Biohazard", "Toxic Biohazard", fl, false },
- { InstrumentKind::poizone, "PoiZone", "PoiZone", fl, false },
- { InstrumentKind::sakura, "Sakura", "Sakura", fl, false },
- { InstrumentKind::kepler, "Kepler", "Kepler", fl, false },
- { InstrumentKind::keplerExo, "Kepler Exo", "Kepler Exo", fl, false },
- { InstrumentKind::transistorbass, "Transistor Bass", "Transistor Bass", fl, false },
- { InstrumentKind::boobass, "BooBass", "", fl, false }, // (its multisample: the user's BooBass_Eng_x64.dll)
- { InstrumentKind::simsynth, "SimSynth", "SimSynth", own, false },
- { InstrumentKind::minisynth, "MiniSynth", "MiniSynth", fl, false },
- { InstrumentKind::dx10, "Fruity DX10", "Fruity DX10", own, false },
- { InstrumentKind::plucked, "Plucked!", "Plucked!", own, false },
- { InstrumentKind::sawer, "Sawer", "Sawer", fl, false },
- { InstrumentKind::beepmap, "BeepMap", "BeepMap", fl, false },
- { InstrumentKind::flkeys, "FL Keys", "FL Keys", fl, false },
- { InstrumentKind::slayer, "FL Slayer", "FL Slayer", fl, false },
- { InstrumentKind::granulizer, "Fruity Granulizer", "Fruity Granulizer", fl, false },
- { InstrumentKind::wavetraveller, "Wave Traveller", "Wave Traveller", own, false },
- { InstrumentKind::drumaxx, "Drumaxx", "Drumaxx", fl, false },
- { InstrumentKind::drumpad, "Drumpad", "Drumpad", fl, false },
- { InstrumentKind::fruitkick, "Fruit Kick", "Fruit Kick", fl, false },
- { InstrumentKind::bassdrum, "BassDrum", "BassDrum", fl, false },
- };
- return list;
- }
- const std::vector<EffectEngine>& effectEngines()
- {
- static const std::vector<EffectEngine> list = {
- { EffectKind::maximus, "Maximus", fl, false },
- { EffectKind::vocodex, "Vocodex", fl, false },
- { EffectKind::equo, "EQUO", fl, false },
- { EffectKind::distructor, "Distructor", fl, false },
- { EffectKind::lovePhilter, "Fruity Love Philter", fl, false },
- { EffectKind::grossBeat, "Gross Beat", fl, false },
- { EffectKind::convolver, "Fruity Convolver", fl, false },
- { EffectKind::flPitchShifter, "Pitch Shifter", own, false },
- { EffectKind::vintagePhaser, "Vintage Phaser", own, false },
- { EffectKind::squeeze, "Fruity Squeeze", own, false },
- { EffectKind::rhythmGain, "", own, false },
- { EffectKind::newtone, "", own, false }, // out of beta in round 165g (REA_rodada165g-flnewt165)
- { EffectKind::newtime, "", own, false }, // out of beta in round 167b (REA_rodada167b)
- };
- return list;
- }
- const InstrumentEngine* instrumentEngine (InstrumentKind kind)
- {
- for (const auto& e : instrumentEngines())
- if (e.kind == kind)
- return &e;
- return nullptr;
- }
- const EffectEngine* effectEngine (EffectKind kind)
- {
- for (const auto& e : effectEngines())
- if (e.kind == kind)
- return &e;
- return nullptr;
- }
- std::vector<std::string> presetCandidates (const std::string& presetFolder, bool generator, const FlAccess& access)
- {
- std::vector<std::string> out;
- if (presetFolder.empty() || access.roots.data.empty())
- return out;
- const auto folder = std::string ("%FLStudioData%\\Patches\\Plugin presets\\") + (generator ? "Generators\\" : "Effects\\") + presetFolder;
- const auto exists = [&access] (const std::string& p) { return ! p.empty() && access.fileExists && access.fileExists (p); };
- for (const auto* name : { "Default.fst", "Default.tbio", "Default\\Default.tbio" })
- if (const auto path = resolveFlPath (folder + "\\" + name, access.roots); exists (path))
- out.push_back (path);
- // then the plugin's entry in FL's Plugin database (Generators\<category>\<plugin>.fst): the state FL itself adds
- // when the plugin is picked (Drumaxx' "Default Kit", REA_rodada169e §1; FL ships no Default.fst for Drumaxx,
- // Drumpad, BassDrum, Sakura...). Before any other preset of the folder.
- if (generator && access.listPresets)
- {
- const auto wanted = lower (presetFolder + ".fst");
- for (const auto& path : access.listPresets (resolveFlPath ("%FLStudioData%\\Patches\\Plugin database\\Generators", access.roots)))
- {
- const auto cut = path.find_last_of ("\\/");
- if (lower (cut == std::string::npos ? path : path.substr (cut + 1)) == wanted
- && std::find (out.begin(), out.end(), path) == out.end())
- {
- out.push_back (path);
- break;
- }
- }
- }
- if (access.listPresets)
- for (const auto& path : access.listPresets (resolveFlPath (folder, access.roots)))
- if ((endsWith (path, ".fst") || endsWith (path, ".tbio") || endsWith (path, ".gmsynth") || endsWith (path, ".mrp"))
- && std::find (out.begin(), out.end(), path) == out.end())
- out.push_back (path);
- return out;
- }
- std::string defaultPresetPath (const std::string& presetFolder, bool generator, const FlAccess& access)
- {
- const auto c = presetCandidates (presetFolder, generator, access);
- return c.empty() ? std::string() : c.front();
- }
- namespace
- {
- // BooBass has no preset folder: a new one is its three knobs at their defaults (0.5, REA_rodada29 §4) over the
- // multisample of the user's own engine DLL
- std::string booBassDll (const FlAccess& access)
- {
- const auto dll = resolveFlPath ("%FLStudioFactoryData%\\Plugins\\Fruity\\Generators\\BooBass\\BooBass_Eng_x64.dll", access.roots);
- return ! dll.empty() && access.fileExists && access.fileExists (dll) ? dll : std::string();
- }
- } // namespace
- std::optional<ChannelDef> channelFromPresetFile (InstrumentKind kind, const std::string& path, const FlAccess& access)
- {
- const auto* engine = instrumentEngine (kind);
- return engine != nullptr ? channelFromPreset (kind, *engine, path, access) : std::nullopt;
- }
- std::string presetFolderOf (InstrumentKind kind)
- {
- if (kind == InstrumentKind::midiOut)
- return "MIDI Out";
- const auto* engine = instrumentEngine (kind);
- return engine != nullptr ? engine->presetFolder : "";
- }
- std::optional<ChannelDef> withPreset (const ChannelDef& current, const std::string& path, const std::string& presetName, const FlAccess& access)
- {
- ChannelDef out;
- if (current.instrument == InstrumentKind::midiOut)
- {
- const auto loaded = midiout::parse (midiout::stateFromFst (readPreset (path, access)));
- if (! loaded)
- return std::nullopt;
- auto state = *loaded;
- const auto now = midiout::stateOf (current.pluginState); // (FL keeps where it plays: channel and port)
- state.channel = now.channel;
- state.port = now.port;
- out = current;
- out.pluginState = midiout::write (state);
- }
- else
- {
- auto def = channelFromPresetFile (current.instrument, path, access);
- if (! def)
- return std::nullopt;
- // a plugin preset holds the plugin's state only: the channel's own settings stay (FL's wrapper loads the
- // .fst into the plugin)
- out = current;
- out.instrument = def->instrument;
- out.samplePath = def->samplePath;
- out.zones = def->zones;
- out.modelParams = def->modelParams;
- out.pluginState = def->pluginState;
- out.controllerState = def->controllerState;
- out.synth = def->synth;
- out.keyLabels = def->keyLabels;
- }
- if (! presetName.empty())
- out.name = presetName; // FL names the channel after the preset (measured, round 167o)
- return out;
- }
- bool available (const InstrumentEngine& e, const FlAccess& access)
- {
- if (e.kind == InstrumentKind::boobass)
- return ! booBassDll (access).empty();
- // an engine with a LYNX preset bank starts from it without FL (round 168n)
- return e.needs == Needs::nothing || banks::hasBank (e.kind) || ! defaultPresetPath (e.presetFolder, true, access).empty();
- }
- bool available (const EffectEngine& e, const FlAccess& access)
- {
- return e.needs == Needs::nothing || ! defaultPresetPath (e.presetFolder, false, access).empty();
- }
- std::optional<ChannelDef> makeChannel (InstrumentKind kind, const FlAccess& access)
- {
- const auto* engine = instrumentEngine (kind);
- if (engine == nullptr)
- return std::nullopt;
- if (kind == InstrumentKind::boobass)
- {
- const auto dll = booBassDll (access);
- if (dll.empty() || ! booBassDllUsable (dll))
- return std::nullopt;
- ChannelDef def;
- def.name = engine->name;
- def.instrument = kind;
- def.samplePath = dll;
- def.modelParams = { 32768.0f, 32768.0f, 32768.0f };
- return def;
- }
- // the user's FL default preset, imported like an .flp channel (the exact engine with its data files)
- int tries = 0;
- for (const auto& path : presetCandidates (engine->presetFolder, true, access))
- {
- if (++tries > 60)
- break;
- if (auto def = channelFromPreset (kind, *engine, path, access))
- return def;
- }
- // without FL: the first preset of LYNX's own bank (round 168n)
- if (auto def = banks::defaultChannel (kind, engine->name))
- return def;
- if (engine->needs != Needs::nothing)
- return std::nullopt;
- ChannelDef def; // LYNX's own default: the engine plays from an empty state
- def.name = engine->name;
- def.instrument = kind;
- return def;
- }
- std::optional<EffectSlot> makeSlot (EffectKind kind, const FlAccess& access, double bpm)
- {
- const auto* engine = effectEngine (kind);
- if (engine == nullptr)
- return std::nullopt;
- if (const auto path = defaultPresetPath (engine->presetFolder, false, access); ! path.empty())
- {
- const auto bytes = readPreset (path, access);
- try
- {
- const auto project = flp::parseProject (bytes, true);
- if (! project.channels.empty() && ! project.channels.front().pluginState.empty())
- {
- const FlDataReader data = [&access] (const std::string& relative) -> std::vector<std::uint8_t> {
- const auto p = resolveFlPath ("%FLStudioFactoryData%\\" + relative, access.roots);
- if (p.empty() || ! access.readFile || (access.fileExists && ! access.fileExists (p)))
- return {};
- return access.readFile (p);
- };
- auto slot = effectFromFl (project.channels.front().pluginName.empty() ? engine->presetFolder : project.channels.front().pluginName,
- project.channels.front().pluginState, bpm, data);
- if (slot && slot->kind == kind)
- {
- if (kind == EffectKind::grossBeat)
- grossbeat::setPpq (slot->state, 96);
- return slot;
- }
- }
- }
- catch (const flp::Error&)
- {
- }
- }
- if (engine->needs != Needs::nothing)
- return std::nullopt;
- return makeEffectSlot (kind);
- }
- std::optional<EffectSlot> newEffect (EffectKind kind, const FlAccess& access, double bpm)
- {
- if (effectEngine (kind) != nullptr)
- return makeSlot (kind, access, bpm);
- return makeEffectSlot (kind);
- }
- std::string effectMenuName (EffectKind kind)
- {
- std::string name = effectInfo (kind).name;
- if (const auto* e = effectEngine (kind); e != nullptr && e->beta)
- name += " (beta)";
- return name;
- }
- std::string instrumentMenuName (const InstrumentEngine& e)
- {
- return std::string (e.name) + (e.beta ? " (beta)" : "");
- }
- } // namespace lynx::natives
|