Templates.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include "core/Templates.h"
  2. #include "core/Effects.h"
  3. #include "core/PlaylistOps.h"
  4. #include "core/SynthPresets.h"
  5. #include <algorithm>
  6. #include <optional>
  7. namespace lynx
  8. {
  9. namespace
  10. {
  11. SynthSettings preset (const char* name)
  12. {
  13. for (const auto& [n, s] : synthPresets())
  14. if (n == name)
  15. return s;
  16. return {};
  17. }
  18. ChannelDef synth (const char* name, const char* presetName, int track, float volume = 0.78f)
  19. {
  20. ChannelDef c;
  21. c.name = name;
  22. c.instrument = InstrumentKind::synth;
  23. c.synth = preset (presetName);
  24. c.mixerTrack = track;
  25. c.volume = volume;
  26. return c;
  27. }
  28. void steps (Pattern& p, int channel, std::initializer_list<int> list, float velocity = 0.78f)
  29. {
  30. for (auto s : list)
  31. {
  32. Note n;
  33. n.start = (std::int64_t) s * ticksPerStep;
  34. n.channel = channel;
  35. n.velocity = velocity;
  36. p.notes.push_back (n);
  37. }
  38. }
  39. void notes (Pattern& p, int channel, std::initializer_list<std::array<int, 3>> list)
  40. {
  41. // {step, length in steps, key}
  42. for (const auto& [step, length, key] : list)
  43. {
  44. Note n;
  45. n.start = (std::int64_t) step * ticksPerStep;
  46. n.length = (std::int64_t) length * ticksPerStep;
  47. n.key = key;
  48. n.channel = channel;
  49. p.notes.push_back (n);
  50. }
  51. }
  52. } // namespace
  53. std::vector<std::string> templateNames()
  54. {
  55. return { "Empty", "Basic beat", "Trap with 808", "House" };
  56. }
  57. Song makeTemplate (int index)
  58. {
  59. if (index == 1)
  60. return Song::makeDefault();
  61. if (index == 0)
  62. {
  63. auto s = Song::makeDefault();
  64. s.patterns[0].notes.clear();
  65. return s;
  66. }
  67. auto s = Song::makeDefault();
  68. s.patterns[0].notes.clear();
  69. auto& p = s.patterns[0];
  70. if (index == 2) // trap: 140 BPM, fast hats, 808 with slide
  71. {
  72. s.bpm = 140.0;
  73. p.name = "Trap";
  74. p.lengthTicks = (std::int64_t) ppq * 4 * 2;
  75. steps (p, 0, { 0, 10, 16, 26 }, 0.9f);
  76. steps (p, 3, { 8, 24 });
  77. for (int i = 0; i < 32; i += 2)
  78. steps (p, 2, { i }, i % 4 == 0 ? 0.7f : 0.45f);
  79. steps (p, 2, { 13, 29 }, 0.5f);
  80. s.channels[4] = synth ("808", "808 Bass", 5, 0.85f);
  81. notes (p, 4, { { { 0, 6, 36 } }, { { 10, 4, 36 } }, { { 16, 6, 39 } }, { { 26, 6, 34 } } });
  82. Note glide;
  83. glide.start = 28 * ticksPerStep;
  84. glide.length = 2 * ticksPerStep;
  85. glide.key = 41;
  86. glide.channel = 4;
  87. glide.slide = true;
  88. p.notes.push_back (glide);
  89. s.swing = 0.0f;
  90. }
  91. else // house: 124 BPM, four on the floor, clap, off-beat hats, bass and chord
  92. {
  93. s.bpm = 124.0;
  94. p.name = "House";
  95. steps (p, 0, { 0, 4, 8, 12 }, 0.9f);
  96. steps (p, 1, { 4, 12 });
  97. steps (p, 2, { 2, 6, 10, 14 }, 0.6f);
  98. s.channels[4] = synth ("Bass", "Reese Bass", 5);
  99. notes (p, 4, { { { 2, 2, 33 } }, { { 6, 2, 33 } }, { { 10, 2, 36 } }, { { 14, 2, 31 } } });
  100. s.channels.push_back (synth ("Chord", "Pluck", 6, 0.6f));
  101. for (int key : { 57, 60, 64 })
  102. notes (p, 5, { { { 3, 1, key } }, { { 7, 1, key } }, { { 11, 1, key } } });
  103. s.swing = 0.15f;
  104. }
  105. p.sortNotes();
  106. return s;
  107. }
  108. std::string legalProjectName (const std::string& name)
  109. {
  110. std::string out;
  111. for (auto c : name)
  112. out += std::string ("<>:\"/\\|?*").find (c) != std::string::npos || (unsigned char) c < 32 ? '_' : c;
  113. const auto first = out.find_first_not_of (" .");
  114. if (first == std::string::npos)
  115. return "Untitled";
  116. out.erase (0, first);
  117. while (! out.empty() && (out.back() == ' ' || out.back() == '.'))
  118. out.pop_back();
  119. return out.empty() ? "Untitled" : out;
  120. }
  121. std::string newProjectPath (const NewProjectSetup& setup, const char* extension)
  122. {
  123. if (setup.folder.empty())
  124. return {};
  125. auto folder = setup.folder;
  126. while (folder.size() > 1 && (folder.back() == '/' || folder.back() == '\\'))
  127. folder.pop_back();
  128. const auto name = legalProjectName (setup.name);
  129. return folder + "/" + (setup.ownFolder ? name + "/" : std::string()) + name + extension;
  130. }
  131. void applySetup (Song& song, const NewProjectSetup& setup)
  132. {
  133. song.bpm = std::clamp (setup.bpm, 10.0, 522.0);
  134. song.beatsPerBar = std::clamp (setup.beatsPerBar, 1, 16);
  135. song.info = setup.info;
  136. if (song.info.title.empty() && ! setup.name.empty())
  137. song.info.title = setup.name;
  138. }
  139. std::string TemplateChoice::toText() const
  140. {
  141. if (name.empty())
  142. return {};
  143. switch (kind)
  144. {
  145. case Kind::builtIn: return "builtin:" + name;
  146. case Kind::user: return "user:" + name;
  147. case Kind::lynxDefault: break;
  148. }
  149. return {};
  150. }
  151. TemplateChoice TemplateChoice::parse (const std::string& text)
  152. {
  153. auto take = [&text] (const char* prefix, Kind kind) -> std::optional<TemplateChoice> {
  154. const std::string p (prefix);
  155. if (text.size() <= p.size() || text.compare (0, p.size(), p) != 0)
  156. return std::nullopt;
  157. TemplateChoice c;
  158. c.kind = kind;
  159. c.name = text.substr (p.size());
  160. // (a file name only: a path or a line break in the settings file never reaches the file system)
  161. if (c.name.find_first_of ("/\\\r\n") != std::string::npos || c.name == "." || c.name == "..")
  162. return std::nullopt;
  163. return c;
  164. };
  165. if (auto c = take ("builtin:", Kind::builtIn); c && builtInTemplateIndex (c->name) >= 0)
  166. return *c;
  167. if (auto c = take ("user:", Kind::user))
  168. return *c;
  169. return {};
  170. }
  171. const char* startupProjectText (StartupProject choice) noexcept
  172. {
  173. switch (choice)
  174. {
  175. case StartupProject::emptyProject: return "empty";
  176. case StartupProject::lastProject: return "last";
  177. case StartupProject::defaultTemplate: break;
  178. }
  179. return "template";
  180. }
  181. StartupProject parseStartupProject (const std::string& text) noexcept
  182. {
  183. if (text == "empty")
  184. return StartupProject::emptyProject;
  185. if (text == "last")
  186. return StartupProject::lastProject;
  187. return StartupProject::defaultTemplate;
  188. }
  189. int builtInTemplateIndex (const std::string& name)
  190. {
  191. const auto names = templateNames();
  192. for (int i = 0; i < (int) names.size(); ++i)
  193. if (names[(std::size_t) i] == name)
  194. return i;
  195. return -1;
  196. }
  197. std::string templateDisplayName (const std::string& fileName)
  198. {
  199. auto name = fileName;
  200. const auto slash = name.find_last_of ("/\\");
  201. if (slash != std::string::npos)
  202. name.erase (0, slash + 1);
  203. const auto dot = name.find_last_of ('.');
  204. if (dot != std::string::npos && dot > 0)
  205. name.erase (dot);
  206. return name;
  207. }
  208. std::string NewProjectDefaults::toText() const
  209. {
  210. const char* chain = master == MasterChain::empty ? "empty" : master == MasterChain::clipper ? "clipper" : "default";
  211. return "newInserts=" + std::to_string (inserts) + "\nnewMasterChain=" + chain + "\nnewCleanMixer=" + (cleanMixer ? "1" : "0")
  212. + "\nplaylistTracks=" + std::to_string (playlistTracks) + "\nnewAutoCrossfades=" + (autoCrossfades ? "1" : "0")
  213. + "\nnewLinkTracks=" + (linkTracks ? "1" : "0") + "\n";
  214. }
  215. bool NewProjectDefaults::ownsKey (const std::string& key)
  216. {
  217. return key == "newInserts" || key == "newMasterChain" || key == "newCleanMixer" || key == "playlistTracks" || key == "newAutoCrossfades"
  218. || key == "newLinkTracks";
  219. }
  220. void NewProjectDefaults::readKey (const std::string& key, const std::string& value)
  221. {
  222. auto number = [&value] (int fallback) {
  223. try
  224. {
  225. return std::stoi (value);
  226. }
  227. catch (...)
  228. {
  229. return fallback;
  230. }
  231. };
  232. if (key == "newInserts")
  233. inserts = std::clamp (number (defaultInserts), 0, maxMixerTracks - 1);
  234. else if (key == "newMasterChain")
  235. master = value == "empty" ? MasterChain::empty : value == "clipper" ? MasterChain::clipper : MasterChain::lynxDefault;
  236. else if (key == "newCleanMixer")
  237. cleanMixer = value == "1";
  238. else if (key == "playlistTracks")
  239. playlistTracks = std::clamp (number (16), 1, 500);
  240. else if (key == "newAutoCrossfades")
  241. autoCrossfades = value != "0";
  242. else if (key == "newLinkTracks")
  243. linkTracks = value == "1";
  244. }
  245. void applyNewProjectDefaults (Song& song, const NewProjectDefaults& defaults)
  246. {
  247. const auto inserts = std::clamp (defaults.inserts, 0, maxMixerTracks - 1);
  248. if (song.mixer.empty())
  249. song.mixer = Song::defaultMixer (inserts);
  250. else if ((int) song.mixer.size() > inserts + 1)
  251. song.mixer.resize ((std::size_t) inserts + 1);
  252. else
  253. while ((int) song.mixer.size() < inserts + 1)
  254. {
  255. MixerTrack track;
  256. track.name = "Insert " + std::to_string (song.mixer.size());
  257. song.mixer.push_back (std::move (track));
  258. }
  259. // nothing may point past the last insert
  260. for (auto& c : song.channels)
  261. if (c.mixerTrack > inserts)
  262. c.mixerTrack = 0;
  263. for (auto& m : song.mixer)
  264. m.sends.erase (std::remove_if (m.sends.begin(), m.sends.end(), [inserts] (const MixerSend& s) { return s.target > inserts; }), m.sends.end());
  265. for (auto& p : song.playlistTracks)
  266. if (p.mixerTrack > inserts)
  267. p.mixerTrack = -1;
  268. auto& master = song.mixer[0].effects;
  269. switch (defaults.master)
  270. {
  271. case MasterChain::empty: master.clear(); break;
  272. case MasterChain::clipper:
  273. master.clear();
  274. master.push_back (makeEffectSlot (EffectKind::fruitySoftClipper));
  275. break;
  276. case MasterChain::lynxDefault: break; // the template's own chain
  277. }
  278. song.cleanMixer = defaults.cleanMixer;
  279. song.autoCrossfades = defaults.autoCrossfades;
  280. song.linkTracksToMixer = defaults.linkTracks; // (round 190gz: a template's tracks with clips are linked too)
  281. if (defaults.linkTracks)
  282. autolink::setOn (song, true);
  283. song.playTruncatedNotes = true; // (on in FL's Empty template and new projects)
  284. song.muteRunsEffects = true; // (FL's muted insert, round 190y)
  285. song.patternClipTakeover = true; // (FL's pattern clip takeover, issue #158)
  286. }
  287. NewProjectDefaults& newProjectDefaults()
  288. {
  289. static NewProjectDefaults defaults;
  290. return defaults;
  291. }
  292. Song makeNewProject (int templateIndex)
  293. {
  294. // round 170h2 (user decision): the default new project is FL's way — the channels, their inserts and the Master
  295. // limiter, with EMPTY steps (the four-on-the-floor stays in the "Basic beat" template)
  296. auto song = makeTemplate (templateIndex < 0 ? 0 : templateIndex);
  297. applyNewProjectDefaults (song, newProjectDefaults());
  298. adoptFlPluginLevels (song); // (the built-in templates hold no hosted plugin: every new project gets FL's levels)
  299. return song;
  300. }
  301. bool adoptFlPluginLevels (Song& song)
  302. {
  303. if (! song.flPluginLevels
  304. && std::none_of (song.channels.begin(), song.channels.end(), [] (const ChannelDef& c) {
  305. // (also a plugin not found on this PC: the channel becomes a hosted plugin again when it is installed)
  306. return c.instrument == InstrumentKind::plugin || c.plugin.foreign.format != ForeignPlugin::Format::none;
  307. }))
  308. song.flPluginLevels = true;
  309. return song.flPluginLevels;
  310. }
  311. } // namespace lynx