NoteTools.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #include "core/NoteTools.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <map>
  5. #include <tuple>
  6. namespace lynx::notetools
  7. {
  8. namespace
  9. {
  10. bool valid (const Pattern& p, int i)
  11. {
  12. return i >= 0 && i < (int) p.notes.size();
  13. }
  14. std::vector<int> validSorted (const Pattern& p, std::vector<int> indices)
  15. {
  16. indices.erase (std::remove_if (indices.begin(), indices.end(), [&p] (int i) { return ! valid (p, i); }), indices.end());
  17. std::sort (indices.begin(), indices.end());
  18. indices.erase (std::unique (indices.begin(), indices.end()), indices.end());
  19. return indices;
  20. }
  21. // small deterministic generator (the same seed gives the same result on every compiler)
  22. struct Random
  23. {
  24. std::uint32_t state;
  25. explicit Random (std::uint32_t seed) : state (seed * 2654435761u + 0x6d2b79f5u) {}
  26. float next() noexcept // 0..1
  27. {
  28. state ^= state << 13;
  29. state ^= state >> 17;
  30. state ^= state << 5;
  31. return (float) (state >> 8) / (float) (1u << 24);
  32. }
  33. float bipolar (bool both) noexcept { return both ? next() * 2.0f - 1.0f : -next(); }
  34. };
  35. } // namespace
  36. // --- levels -----------------------------------------------------------------------------------
  37. const char* levelName (Level level)
  38. {
  39. switch (level)
  40. {
  41. case Level::velocity: return TR_NOOP ("Velocity");
  42. case Level::pan: return TR_NOOP ("Pan");
  43. case Level::release: return TR_NOOP ("Release");
  44. case Level::finePitch: return TR_NOOP ("Fine pitch");
  45. case Level::modX: return TR_NOOP ("Mod X");
  46. case Level::modY: return TR_NOOP ("Mod Y");
  47. }
  48. return "";
  49. }
  50. float levelValue (const Note& n, Level level) noexcept
  51. {
  52. switch (level)
  53. {
  54. case Level::velocity: return n.velocity;
  55. case Level::pan: return (n.pan + 1.0f) * 0.5f;
  56. case Level::release: return n.release;
  57. case Level::finePitch: return (n.finePitch + 120.0f) / 240.0f;
  58. case Level::modX: return n.modX;
  59. case Level::modY: return n.modY;
  60. }
  61. return 0.0f;
  62. }
  63. void setLevelValue (Note& n, Level level, float v) noexcept
  64. {
  65. v = std::clamp (v, 0.0f, 1.0f);
  66. switch (level)
  67. {
  68. case Level::velocity: n.velocity = v; break;
  69. case Level::pan: n.pan = v * 2.0f - 1.0f; break;
  70. case Level::release: n.release = v; break;
  71. case Level::finePitch: n.finePitch = v * 240.0f - 120.0f; break;
  72. case Level::modX: n.modX = v; break;
  73. case Level::modY: n.modY = v; break;
  74. }
  75. }
  76. float levelDefault (Level level) noexcept
  77. {
  78. return levelValue (Note {}, level);
  79. }
  80. bool levelIsBipolar (Level level) noexcept
  81. {
  82. return level == Level::pan || level == Level::finePitch;
  83. }
  84. // --- scales -----------------------------------------------------------------------------------
  85. const std::vector<ScaleInfo>& scales()
  86. {
  87. static const std::vector<ScaleInfo> list {
  88. { TR_NOOP ("No scale"), {} },
  89. { TR_NOOP ("Major"), { 0, 2, 4, 5, 7, 9, 11 } },
  90. { TR_NOOP ("Natural minor"), { 0, 2, 3, 5, 7, 8, 10 } },
  91. { TR_NOOP ("Harmonic minor"), { 0, 2, 3, 5, 7, 8, 11 } },
  92. { TR_NOOP ("Major pentatonic"), { 0, 2, 4, 7, 9 } },
  93. { TR_NOOP ("Minor pentatonic"), { 0, 3, 5, 7, 10 } },
  94. { "Blues", { 0, 3, 5, 6, 7, 10 } },
  95. { TR_NOOP ("Dorian"), { 0, 2, 3, 5, 7, 9, 10 } },
  96. { TR_NOOP ("Phrygian"), { 0, 1, 3, 5, 7, 8, 10 } },
  97. };
  98. return list;
  99. }
  100. bool inScale (int key, int root, int scale) noexcept
  101. {
  102. if (scale <= 0 || scale >= (int) scales().size())
  103. return true;
  104. const auto& intervals = scales()[(std::size_t) scale].intervals;
  105. const auto degree = ((key - root) % 12 + 12) % 12;
  106. return std::find (intervals.begin(), intervals.end(), degree) != intervals.end();
  107. }
  108. int snapToScale (int key, int root, int scale) noexcept
  109. {
  110. key = std::clamp (key, 0, 127);
  111. if (scale <= 0 || scale >= (int) scales().size())
  112. return key;
  113. for (int d = 0; d < 12; ++d)
  114. {
  115. if (key - d >= 0 && inScale (key - d, root, scale))
  116. return key - d;
  117. if (key + d <= 127 && inScale (key + d, root, scale))
  118. return key + d;
  119. }
  120. return key;
  121. }
  122. void snapNotesToScale (Pattern& pattern, const std::vector<int>& indices, int root, int scale)
  123. {
  124. for (auto i : validSorted (pattern, indices))
  125. {
  126. auto& n = pattern.notes[(std::size_t) i];
  127. n.key = snapToScale (n.key, root, scale);
  128. }
  129. }
  130. // --- chords -----------------------------------------------------------------------------------
  131. std::vector<std::vector<int>> chordsOf (const Pattern& pattern, const std::vector<int>& indices)
  132. {
  133. std::map<std::tuple<std::int64_t, int>, std::vector<int>> groups; // (start, channel)
  134. for (auto i : validSorted (pattern, indices))
  135. {
  136. const auto& n = pattern.notes[(std::size_t) i];
  137. groups[{ n.start, n.channel }].push_back (i);
  138. }
  139. std::vector<std::vector<int>> out;
  140. for (auto& [key, group] : groups)
  141. {
  142. std::stable_sort (group.begin(), group.end(), [&pattern] (int a, int b) {
  143. return pattern.notes[(std::size_t) a].key < pattern.notes[(std::size_t) b].key;
  144. });
  145. out.push_back (std::move (group));
  146. }
  147. return out;
  148. }
  149. // --- strum ------------------------------------------------------------------------------------
  150. void strum (Pattern& pattern, const std::vector<int>& indices, const StrumOptions& o)
  151. {
  152. const auto chords = chordsOf (pattern, indices);
  153. int chordNumber = 0;
  154. const auto exponent = std::pow (2.0, (double) std::clamp (o.curve, -1.0f, 1.0f) * 2.0);
  155. for (const auto& chord : chords)
  156. {
  157. if (chord.size() < 2)
  158. continue;
  159. auto downward = o.downward;
  160. if (o.alternate && (chordNumber % 2) == 1)
  161. downward = ! downward;
  162. ++chordNumber;
  163. const auto count = (int) chord.size();
  164. const auto total = (double) o.spread * (count - 1);
  165. for (int k = 0; k < count; ++k)
  166. {
  167. auto& n = pattern.notes[(std::size_t) chord[(std::size_t) (downward ? count - 1 - k : k)]];
  168. const auto x = (double) k / (double) (count - 1);
  169. const auto offset = std::max<std::int64_t> (0, (std::int64_t) std::llround (total * std::pow (x, exponent)));
  170. const auto end = n.start + n.length;
  171. n.start += offset;
  172. if (o.keepEnds)
  173. n.length = std::max<std::int64_t> (std::min<std::int64_t> (n.length, 10), end - n.start);
  174. n.velocity = std::clamp (n.velocity + o.velocityStep * (float) k, 0.0f, 1.0f);
  175. }
  176. }
  177. }
  178. // --- arpeggiate -------------------------------------------------------------------------------
  179. std::vector<int> arpeggiate (Pattern& pattern, const std::vector<int>& indices, const ArpeggiateOptions& o)
  180. {
  181. const auto step = std::max<std::int64_t> (1, o.step);
  182. const auto octaves = std::clamp (o.octaves, 1, 4);
  183. const auto noteLength = std::max<std::int64_t> (1, (std::int64_t) std::llround ((double) step * std::clamp (o.gate, 0.05f, 1.0f)));
  184. std::vector<int> doomed;
  185. std::vector<Note> created;
  186. for (const auto& chord : chordsOf (pattern, indices))
  187. {
  188. if (chord.size() < 2 && octaves == 1)
  189. continue;
  190. std::vector<Note> base;
  191. std::int64_t span = 0;
  192. for (auto i : chord)
  193. {
  194. base.push_back (pattern.notes[(std::size_t) i]);
  195. span = std::max (span, pattern.notes[(std::size_t) i].length);
  196. doomed.push_back (i);
  197. }
  198. // the ascending run over the octaves
  199. std::vector<Note> run;
  200. for (int oct = 0; oct < octaves; ++oct)
  201. for (auto n : base)
  202. if (n.key + 12 * oct <= 127)
  203. {
  204. n.key += 12 * oct;
  205. run.push_back (n);
  206. }
  207. std::vector<Note> sequence;
  208. auto down = run;
  209. std::reverse (down.begin(), down.end());
  210. switch (o.direction)
  211. {
  212. case ArpeggiateOptions::down: sequence = down; break;
  213. case ArpeggiateOptions::upDown:
  214. sequence = run;
  215. if (down.size() > 2)
  216. sequence.insert (sequence.end(), down.begin() + 1, down.end() - 1);
  217. break;
  218. case ArpeggiateOptions::downUp:
  219. sequence = down;
  220. if (run.size() > 2)
  221. sequence.insert (sequence.end(), run.begin() + 1, run.end() - 1);
  222. break;
  223. default: sequence = run; break;
  224. }
  225. if (sequence.empty())
  226. continue;
  227. const auto start = base.front().start;
  228. for (std::int64_t t = 0, k = 0; t < span; t += step, ++k)
  229. {
  230. auto n = sequence[(std::size_t) (k % (std::int64_t) sequence.size())];
  231. n.start = start + t;
  232. n.length = std::min (noteLength, span - t);
  233. created.push_back (n);
  234. }
  235. }
  236. std::sort (doomed.begin(), doomed.end());
  237. for (auto it = doomed.rbegin(); it != doomed.rend(); ++it)
  238. pattern.notes.erase (pattern.notes.begin() + *it);
  239. std::vector<int> added;
  240. for (const auto& n : created)
  241. {
  242. added.push_back ((int) pattern.notes.size());
  243. pattern.notes.push_back (n);
  244. }
  245. return added;
  246. }
  247. // --- flip -------------------------------------------------------------------------------------
  248. void flip (Pattern& pattern, const std::vector<int>& indices, bool horizontal, bool vertical)
  249. {
  250. const auto targets = validSorted (pattern, indices);
  251. if (targets.empty())
  252. return;
  253. std::int64_t from = INT64_MAX, to = INT64_MIN;
  254. int low = 127, high = 0;
  255. for (auto i : targets)
  256. {
  257. const auto& n = pattern.notes[(std::size_t) i];
  258. from = std::min (from, n.start);
  259. to = std::max (to, n.start + n.length);
  260. low = std::min (low, n.key);
  261. high = std::max (high, n.key);
  262. }
  263. for (auto i : targets)
  264. {
  265. auto& n = pattern.notes[(std::size_t) i];
  266. if (horizontal)
  267. n.start = from + (to - (n.start + n.length));
  268. if (vertical)
  269. n.key = std::clamp (low + high - n.key, 0, 127);
  270. }
  271. }
  272. // --- randomise --------------------------------------------------------------------------------
  273. void randomise (Pattern& pattern, const std::vector<int>& indices, const RandomiseOptions& o)
  274. {
  275. Random random (o.seed);
  276. for (auto i : validSorted (pattern, indices))
  277. {
  278. auto& n = pattern.notes[(std::size_t) i];
  279. // every note draws the same number of values, so changing one amount doesn't reshuffle the others
  280. const auto rv = random.bipolar (o.bipolar), rp = random.bipolar (true), rf = random.bipolar (true);
  281. const auto rx = random.bipolar (true), ry = random.bipolar (true), rt = random.bipolar (o.bipolar);
  282. const auto rl = random.bipolar (true);
  283. n.velocity = std::clamp (n.velocity + o.velocity * rv, 0.0f, 1.0f);
  284. n.pan = std::clamp (n.pan + 2.0f * o.pan * rp, -1.0f, 1.0f);
  285. n.finePitch = std::clamp (n.finePitch + 120.0f * o.finePitch * rf, -120.0f, 120.0f);
  286. n.modX = std::clamp (n.modX + o.modX * rx, 0.0f, 1.0f);
  287. n.modY = std::clamp (n.modY + o.modY * ry, 0.0f, 1.0f);
  288. if (o.timing > 0)
  289. n.start = std::max<std::int64_t> (0, n.start + (std::int64_t) std::llround ((double) o.timing * (o.bipolar ? rt : -rt)));
  290. if (o.length > 0.0f)
  291. n.length = std::max<std::int64_t> (1, (std::int64_t) std::llround ((double) n.length * (1.0 + (double) o.length * rl)));
  292. }
  293. }
  294. // --- scale levels -----------------------------------------------------------------------------
  295. void scaleLevels (Pattern& pattern, const std::vector<int>& indices, Level level, float scaleStart, float scaleEnd, float offset)
  296. {
  297. const auto targets = validSorted (pattern, indices);
  298. if (targets.empty())
  299. return;
  300. std::int64_t first = INT64_MAX, last = INT64_MIN;
  301. for (auto i : targets)
  302. {
  303. first = std::min (first, pattern.notes[(std::size_t) i].start);
  304. last = std::max (last, pattern.notes[(std::size_t) i].start);
  305. }
  306. const auto centre = levelIsBipolar (level) ? 0.5f : 0.0f;
  307. for (auto i : targets)
  308. {
  309. auto& n = pattern.notes[(std::size_t) i];
  310. const auto t = last > first ? (float) (n.start - first) / (float) (last - first) : 0.0f;
  311. const auto scale = scaleStart + (scaleEnd - scaleStart) * t;
  312. setLevelValue (n, level, centre + (levelValue (n, level) - centre) * scale + offset);
  313. }
  314. }
  315. // --- limit ------------------------------------------------------------------------------------
  316. void limit (Pattern& pattern, const std::vector<int>& indices, const LimitOptions& o)
  317. {
  318. const auto low = std::clamp (std::min (o.lowKey, o.highKey), 0, 127);
  319. const auto high = std::clamp (std::max (o.lowKey, o.highKey), 0, 127);
  320. const auto minV = std::clamp (std::min (o.minVelocity, o.maxVelocity), 0.0f, 1.0f);
  321. const auto maxV = std::clamp (std::max (o.minVelocity, o.maxVelocity), 0.0f, 1.0f);
  322. for (auto i : validSorted (pattern, indices))
  323. {
  324. auto& n = pattern.notes[(std::size_t) i];
  325. if (high - low >= 11) // an octave fits: keep the pitch class
  326. {
  327. while (n.key < low)
  328. n.key += 12;
  329. while (n.key > high)
  330. n.key -= 12;
  331. }
  332. n.key = std::clamp (n.key, low, high);
  333. n.velocity = std::clamp (n.velocity, minV, maxV);
  334. }
  335. }
  336. } // namespace lynx::notetools