EffectPresets.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "core/EffectPresets.h"
  2. #include <algorithm>
  3. #include <cctype>
  4. #include <cmath>
  5. #include <cstdlib>
  6. #include <map>
  7. #include <sstream>
  8. #include "core/Effects.h"
  9. #include "core/FruityBasicFx.h"
  10. #include "core/EffectPresetTable.inc"
  11. #include "core/EffectBanks168r.inc" // round 168r: LYNX's banks for the FL effects and the simple built-ins
  12. namespace lynx::presets
  13. {
  14. namespace
  15. {
  16. std::string trim (std::string s)
  17. {
  18. const auto notSpace = [] (unsigned char c) { return ! std::isspace (c); };
  19. s.erase (s.begin(), std::find_if (s.begin(), s.end(), notSpace));
  20. s.erase (std::find_if (s.rbegin(), s.rend(), notSpace).base(), s.end());
  21. return s;
  22. }
  23. // lower case without spaces: "Concert Hall" == "concerthall"
  24. std::string folded (const std::string& s)
  25. {
  26. std::string out;
  27. for (const auto c : s)
  28. if (! std::isspace ((unsigned char) c))
  29. out += (char) std::tolower ((unsigned char) c);
  30. return out;
  31. }
  32. // the number a value text shows ("−12.0 dB" → −12, "6.00 kHz" → 6000, "1/4" → none); nullopt without one
  33. std::optional<double> numberIn (const std::string& text)
  34. {
  35. std::string s;
  36. for (std::size_t i = 0; i < text.size(); ++i) // U+2212 minus → '-'
  37. {
  38. if ((unsigned char) text[i] == 0xE2 && i + 2 < text.size() && (unsigned char) text[i + 1] == 0x88 && (unsigned char) text[i + 2] == 0x92)
  39. {
  40. s += '-';
  41. i += 2;
  42. continue;
  43. }
  44. s += text[i];
  45. }
  46. const auto start = s.find_first_of ("+-.0123456789");
  47. if (start == std::string::npos)
  48. return std::nullopt;
  49. char* end = nullptr;
  50. const auto v = std::strtod (s.c_str() + start, &end);
  51. if (end == s.c_str() + start || *end == '/') // none, or a note value ("1/4"): compared as text
  52. return std::nullopt;
  53. const auto rest = folded (std::string (end));
  54. if (rest.rfind ("k", 0) == 0) // kHz
  55. return v * 1000.0;
  56. if (rest.rfind ("s", 0) == 0 && rest.rfind ("st", 0) != 0) // seconds (and "ms" stays ms): 1.5 s = 1500 ms
  57. return v * 1000.0;
  58. return v;
  59. }
  60. constexpr int grid = 2000;
  61. EffectKind kindNamed (const std::string& name)
  62. {
  63. // the exact name first: LYNX's "Frequency shifter" and FL's "Frequency Shifter" differ only in case (round 168r)
  64. for (int k = 0; k <= (int) lastEffectKind; ++k)
  65. if (name == effectInfo ((EffectKind) k).name)
  66. return (EffectKind) k;
  67. for (int k = 0; k <= (int) lastEffectKind; ++k)
  68. if (folded (effectInfo ((EffectKind) k).name) == folded (name))
  69. return (EffectKind) k;
  70. return EffectKind::plugin;
  71. }
  72. struct Table
  73. {
  74. std::map<EffectKind, std::vector<Preset>> presets;
  75. std::map<EffectKind, std::vector<int>> keep;
  76. };
  77. int paramIndexOf (EffectKind kind, const std::string& name);
  78. Table parseTable()
  79. {
  80. Table table;
  81. std::istringstream in (std::string (builtInTable) + effectBanks168r);
  82. std::string line, folder;
  83. auto kind = EffectKind::plugin;
  84. while (std::getline (in, line))
  85. {
  86. if (const auto hash = line.find ('#'); hash != std::string::npos && (hash == 0 || line[hash - 1] == ' '))
  87. line = line.substr (0, hash);
  88. line = trim (line);
  89. if (line.empty())
  90. continue;
  91. if (line.front() == '[' && line.back() == ']')
  92. {
  93. kind = kindNamed (line.substr (1, line.size() - 2));
  94. folder.clear();
  95. continue;
  96. }
  97. if (line.rfind ("keep", 0) == 0 && line.find ('=') != std::string::npos && line.find ('|') == std::string::npos)
  98. {
  99. std::istringstream names (line.substr (line.find ('=') + 1));
  100. std::string name;
  101. while (std::getline (names, name, ';'))
  102. if (const auto p = paramIndexOf (kind, trim (name)); p >= 0)
  103. table.keep[kind].push_back (p);
  104. continue;
  105. }
  106. if (line.rfind ("folder ", 0) == 0)
  107. {
  108. folder = trim (line.substr (7));
  109. continue;
  110. }
  111. const auto bar = line.find ('|');
  112. if (bar == std::string::npos || kind == EffectKind::plugin)
  113. continue;
  114. Preset p;
  115. p.name = trim (line.substr (0, bar));
  116. p.folder = folder;
  117. std::istringstream values (line.substr (bar + 1));
  118. std::string entry;
  119. while (std::getline (values, entry, ';'))
  120. if (const auto eq = entry.find ('='); eq != std::string::npos)
  121. p.values.emplace_back (trim (entry.substr (0, eq)), trim (entry.substr (eq + 1)));
  122. table.presets[kind].push_back (std::move (p));
  123. }
  124. return table;
  125. }
  126. const Table& table()
  127. {
  128. static const auto t = parseTable();
  129. return t;
  130. }
  131. int paramIndexOf (EffectKind kind, const std::string& name)
  132. {
  133. if (kind == EffectKind::plugin)
  134. return -1;
  135. const auto& params = effectInfo (kind).params;
  136. const auto want = folded (name);
  137. for (int i = 0; i < (int) params.size(); ++i)
  138. if (folded (params[(std::size_t) i].name) == want)
  139. return i;
  140. return -1;
  141. }
  142. } // namespace
  143. const std::vector<Preset>& factory (EffectKind kind)
  144. {
  145. static const std::vector<Preset> none;
  146. const auto& t = table().presets;
  147. const auto it = t.find (kind);
  148. return it != t.end() ? it->second : none;
  149. }
  150. const std::vector<int>& keptParams (EffectKind kind)
  151. {
  152. static const std::vector<int> none;
  153. const auto& t = table().keep;
  154. const auto it = t.find (kind);
  155. return it != t.end() ? it->second : none;
  156. }
  157. std::vector<EffectKind> kindsWithFactoryPresets()
  158. {
  159. std::vector<EffectKind> kinds;
  160. for (const auto& [kind, list] : table().presets)
  161. if (! list.empty())
  162. kinds.push_back (kind);
  163. return kinds;
  164. }
  165. int paramByName (EffectKind kind, const std::string& name)
  166. {
  167. return paramIndexOf (kind, name);
  168. }
  169. std::optional<float> valueFromText (EffectKind kind, int param, const std::string& text, double bpm)
  170. {
  171. const auto t = trim (text);
  172. if (t.empty() || param < 0 || param >= (int) effectInfo (kind).params.size())
  173. return std::nullopt;
  174. if (t.front() == '#')
  175. {
  176. char* end = nullptr;
  177. const auto v = std::strtod (t.c_str() + 1, &end);
  178. if (end == t.c_str() + 1)
  179. return std::nullopt;
  180. return (float) std::clamp (v, 0.0, 1.0);
  181. }
  182. const auto want = folded (t);
  183. std::vector<std::string> texts ((std::size_t) grid + 1);
  184. for (int i = 0; i <= grid; ++i)
  185. texts[(std::size_t) i] = formatEffectParam (kind, param, (float) i / (float) grid, bpm);
  186. // an option's text: the middle of the run of values that show it
  187. int first = -1, last = -1;
  188. for (int i = 0; i <= grid; ++i)
  189. if (folded (texts[(std::size_t) i]) == want)
  190. {
  191. if (first < 0) first = i;
  192. last = i;
  193. }
  194. else if (first >= 0)
  195. break;
  196. if (first >= 0)
  197. return (float) (first + last) * 0.5f / (float) grid;
  198. // a number: the value whose shown number is nearest
  199. const auto target = numberIn (t);
  200. if (! target)
  201. return std::nullopt;
  202. int best = -1;
  203. double bestError = 0.0;
  204. for (int i = 0; i <= grid; ++i)
  205. if (const auto n = numberIn (texts[(std::size_t) i]))
  206. {
  207. const auto error = std::abs (*n - *target);
  208. if (best < 0 || error < bestError)
  209. {
  210. best = i;
  211. bestError = error;
  212. }
  213. }
  214. if (best < 0)
  215. return std::nullopt;
  216. // the shown number must be close: within 3 % of the target (or 0.05 absolute near zero)
  217. if (bestError > std::max (0.05, 0.03 * std::abs (*target)))
  218. return std::nullopt;
  219. // the middle of the run of grid points showing that same text (a stepped value's centre)
  220. int lo = best, hi = best;
  221. while (lo > 0 && texts[(std::size_t) lo - 1] == texts[(std::size_t) best]) --lo;
  222. while (hi < grid && texts[(std::size_t) hi + 1] == texts[(std::size_t) best]) ++hi;
  223. return (float) (lo + hi) * 0.5f / (float) grid;
  224. }
  225. EffectParams apply (EffectKind kind, const Preset& preset, double bpm, std::vector<std::string>* unresolved, const EffectParams* current)
  226. {
  227. auto params = makeEffectSlot (kind).params;
  228. if (current != nullptr)
  229. for (const auto p : keptParams (kind))
  230. params[(std::size_t) p] = (*current)[(std::size_t) p];
  231. for (const auto& [name, text] : preset.values)
  232. {
  233. const auto p = paramByName (kind, name);
  234. const auto v = p >= 0 ? valueFromText (kind, p, text, bpm) : std::nullopt;
  235. if (v)
  236. params[(std::size_t) p] = *v;
  237. else if (unresolved != nullptr)
  238. unresolved->push_back (name + "=" + text);
  239. }
  240. return params;
  241. }
  242. namespace
  243. {
  244. const std::string sendSpanKey = "Send to span";
  245. }
  246. std::string toText (EffectKind kind, const std::string& name, const EffectParams& params)
  247. {
  248. std::ostringstream out;
  249. out << "LYNX preset 1\n";
  250. out << "engine = " << effectInfo (kind).name << "\n";
  251. out << "name = " << name << "\n";
  252. const auto& info = effectInfo (kind).params;
  253. // round 167a: Fruity Send's "Send to" over 0..127 when it fits (older LYNX versions read the same insert)
  254. const auto sendSpan = kind == EffectKind::fruitySend && params.size() > 3 ? flbasic::saveSendSpan ({ params[3] }) : 0;
  255. if (sendSpan > 0)
  256. out << sendSpanKey << " = " << sendSpan << "\n";
  257. for (std::size_t i = 0; i < info.size() && i < params.size(); ++i)
  258. {
  259. char value[32];
  260. const auto v = sendSpan > 0 && i == 3 ? flbasic::sendTrackValueInSpan (params[i], sendSpan) : params[i];
  261. std::snprintf (value, sizeof (value), "%.6g", (double) v);
  262. out << info[i].name << " = " << value << "\n";
  263. }
  264. return out.str();
  265. }
  266. std::optional<Loaded> fromText (EffectKind kind, const std::string& text)
  267. {
  268. std::istringstream in (text);
  269. std::string line;
  270. if (! std::getline (in, line) || trim (line).rfind ("LYNX preset", 0) != 0)
  271. return std::nullopt;
  272. Loaded loaded;
  273. loaded.params = makeEffectSlot (kind).params;
  274. bool sameEngine = false;
  275. std::size_t saved = 0;
  276. int sendSpan = flbasic::legacySendSpan; // Fruity Send presets saved before round 166d: "Send to" over 0..127
  277. while (std::getline (in, line))
  278. {
  279. const auto eq = line.find ('=');
  280. if (eq == std::string::npos)
  281. continue;
  282. const auto key = trim (line.substr (0, eq)), value = trim (line.substr (eq + 1));
  283. if (key == "engine")
  284. sameEngine = folded (value) == folded (effectInfo (kind).name);
  285. else if (key == "name")
  286. loaded.name = value;
  287. else if (key == sendSpanKey)
  288. sendSpan = std::atoi (value.c_str());
  289. else if (const auto p = paramByName (kind, key); p >= 0)
  290. {
  291. loaded.params[(std::size_t) p] = (float) std::clamp (std::atof (value.c_str()), 0.0, 1.0);
  292. saved = std::max (saved, (std::size_t) p + 1);
  293. }
  294. }
  295. if (! sameEngine)
  296. return std::nullopt;
  297. if (kind == EffectKind::fruitySend)
  298. loaded.params[3] = flbasic::rescaleSendTrackValue (loaded.params[3], sendSpan);
  299. // round 165w: a preset written before an engine's appended law loads like a project of that time (the old law)
  300. EffectSlot slot = makeEffectSlot (kind);
  301. slot.params = loaded.params;
  302. upgradeLoadedEffectParams (slot, saved);
  303. loaded.params = slot.params;
  304. return loaded;
  305. }
  306. std::string fileNameFor (const std::string& presetName)
  307. {
  308. std::string out;
  309. for (const auto c : trim (presetName))
  310. out += std::string ("\\/:*?\"<>|").find (c) != std::string::npos || (unsigned char) c < 32 ? '_' : c;
  311. out = trim (out);
  312. while (! out.empty() && out.back() == '.')
  313. out.pop_back();
  314. if (out.size() > 64)
  315. out.resize (64);
  316. return out.empty() ? "Preset" : out;
  317. }
  318. } // namespace lynx::presets