Arpeggiator.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "core/Arpeggiator.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. namespace lynx
  5. {
  6. void Arpeggiator::reset() noexcept
  7. {
  8. heldCount = 0;
  9. step = 0;
  10. repeatCount = 0;
  11. sounding = -1;
  12. offTick = -1.0;
  13. nextStep = -1.0;
  14. sliding = false;
  15. }
  16. void Arpeggiator::hold (int key, float velocity) noexcept
  17. {
  18. for (int i = 0; i < heldCount; ++i)
  19. if (heldKeys[(std::size_t) i] == key)
  20. return;
  21. if (heldCount == maxHeld)
  22. return;
  23. // keep sorted by pitch
  24. auto i = heldCount++;
  25. while (i > 0 && heldKeys[(std::size_t) i - 1] > key)
  26. {
  27. heldKeys[(std::size_t) i] = heldKeys[(std::size_t) i - 1];
  28. heldVelocity[(std::size_t) i] = heldVelocity[(std::size_t) i - 1];
  29. --i;
  30. }
  31. heldKeys[(std::size_t) i] = key;
  32. heldVelocity[(std::size_t) i] = velocity;
  33. }
  34. void Arpeggiator::release (int key) noexcept
  35. {
  36. for (int i = 0; i < heldCount; ++i)
  37. if (heldKeys[(std::size_t) i] == key)
  38. {
  39. for (int j = i; j + 1 < heldCount; ++j)
  40. {
  41. heldKeys[(std::size_t) j] = heldKeys[(std::size_t) j + 1];
  42. heldVelocity[(std::size_t) j] = heldVelocity[(std::size_t) j + 1];
  43. }
  44. --heldCount;
  45. return;
  46. }
  47. }
  48. int Arpeggiator::sequenceLength (const ArpSettings& s) const noexcept
  49. {
  50. // one pass over the notes (the held ones, or the chord on the lowest), FL's Repeat times per octave, over `range`
  51. const auto notes = s.chord.empty() ? heldCount : (heldCount > 0 ? (int) s.chord.size() : 0);
  52. return notes * std::clamp (s.range, 1, 4) * std::max (1, s.octaveRepeat);
  53. }
  54. int Arpeggiator::keyAt (const ArpSettings& s, int index) const noexcept
  55. {
  56. const auto notes = s.chord.empty() ? heldCount : (int) s.chord.size();
  57. if (notes <= 0)
  58. return 60;
  59. const auto perOctave = notes * std::max (1, s.octaveRepeat);
  60. const auto octave = index / perOctave;
  61. const auto note = index % notes;
  62. const auto key = s.chord.empty() ? heldKeys[(std::size_t) note] : heldKeys[0] + s.chord[(std::size_t) note];
  63. return std::clamp (key + 12 * octave, 0, 127);
  64. }
  65. int Arpeggiator::nextIndex (const ArpSettings& s) noexcept
  66. {
  67. const auto length = sequenceLength (s);
  68. if (length == 0)
  69. return -1;
  70. auto index = 0;
  71. switch (s.direction)
  72. {
  73. case 2: // down
  74. index = length - 1 - step % length;
  75. break;
  76. case 3: // up and down (without repeating the ends)
  77. {
  78. const auto cycle = std::max (1, 2 * length - 2);
  79. const auto p = step % cycle;
  80. index = p < length ? p : cycle - p;
  81. break;
  82. }
  83. case 5: // up and down, the ends twice (FL's "sticky")
  84. {
  85. const auto cycle = 2 * length;
  86. const auto p = step % cycle;
  87. index = p < length ? p : cycle - 1 - p;
  88. break;
  89. }
  90. case 4: // random
  91. random ^= random << 13;
  92. random ^= random >> 17;
  93. random ^= random << 5;
  94. index = (int) (random % (std::uint32_t) length);
  95. break;
  96. default: // up
  97. index = step % length;
  98. break;
  99. }
  100. if (++repeatCount >= std::max (1, s.repeat))
  101. {
  102. repeatCount = 0;
  103. ++step;
  104. }
  105. return index;
  106. }
  107. void Arpeggiator::process (int channel, const NoteEvent* in, int inCount, double startTick, double tps, int n,
  108. const ArpSettings& s, NoteEvent* out, int& outCount) noexcept
  109. {
  110. outCount = 0;
  111. auto emit = [&] (const NoteEvent& e) {
  112. if (outCount < maxEvents)
  113. out[outCount++] = e;
  114. };
  115. // FL: a note lasts time · gate; past 100 % the next one waits for it (the step is time · max(1, gate)) [EXEC]
  116. const auto gate = std::clamp ((double) s.gate, 0.05, 128.0 / 48.0);
  117. const auto stepTicks = std::max (1.0, (double) s.stepBeats * ppq * std::max (1.0, gate));
  118. const auto noteTicks = std::max (1.0, (double) s.stepBeats * ppq * gate);
  119. const auto endTick = startTick + n * tps;
  120. auto nextInput = 0;
  121. // decision points: releasing the gated note and the steps (FL counts them from the first held note [EXEC: a
  122. // chord off the grid arpeggiates from its own start]), interleaved with the input
  123. auto boundary = nextStep >= 0.0 ? nextStep : endTick + 1.0;
  124. while (true)
  125. {
  126. const auto inputOffset = nextInput < inCount ? in[nextInput].sampleOffset : n;
  127. const auto boundaryOffset = boundary < endTick ? (int) std::floor ((boundary - startTick) / tps) : n;
  128. const auto offOffset = sounding >= 0 && offTick >= 0.0 && offTick < endTick ? (int) std::floor ((std::max (offTick, startTick) - startTick) / tps) : n;
  129. const auto nextOffset = std::min ({ inputOffset, boundaryOffset, offOffset });
  130. if (nextOffset >= n)
  131. break;
  132. if (offOffset == nextOffset && offOffset <= boundaryOffset && sounding >= 0)
  133. {
  134. NoteEvent off;
  135. off.type = NoteEvent::Type::off;
  136. off.key = sounding;
  137. off.channel = channel;
  138. off.sampleOffset = std::clamp (offOffset, 0, n - 1);
  139. emit (off);
  140. sounding = -1;
  141. offTick = -1.0;
  142. continue;
  143. }
  144. if (inputOffset == nextOffset && inputOffset <= boundaryOffset)
  145. {
  146. const auto& e = in[nextInput++];
  147. if (e.type == NoteEvent::Type::on)
  148. {
  149. if (heldCount == 0 && nextStep < 0.0)
  150. boundary = nextStep = startTick + e.sampleOffset * tps; // the first step plays at once
  151. hold (e.key, e.velocity);
  152. }
  153. else if (e.type == NoteEvent::Type::off)
  154. release (e.key);
  155. else
  156. emit (e); // slide, choke...
  157. // everything released: cut the arp note and restart the sequence
  158. if (heldCount == 0)
  159. {
  160. step = 0;
  161. repeatCount = 0;
  162. nextStep = -1.0;
  163. boundary = endTick + 1.0;
  164. sliding = false;
  165. if (sounding >= 0)
  166. {
  167. NoteEvent off = e;
  168. off.type = NoteEvent::Type::off;
  169. off.key = sounding;
  170. emit (off);
  171. sounding = -1;
  172. offTick = -1.0;
  173. }
  174. }
  175. continue;
  176. }
  177. // step boundary: next arp note
  178. if (heldCount > 0)
  179. {
  180. NoteEvent e;
  181. e.channel = channel;
  182. e.sampleOffset = std::clamp (boundaryOffset, 0, n - 1);
  183. const auto index = nextIndex (s);
  184. const auto key = keyAt (s, index);
  185. const auto velocity = heldVelocity[(std::size_t) (s.chord.empty() ? index % heldCount : 0)];
  186. if (s.slide && sliding && sounding >= 0)
  187. {
  188. // FL's Slide: the sounding note glides to the next one (no new attack); it keeps its own key
  189. e.type = NoteEvent::Type::slide;
  190. e.key = key;
  191. e.slideSamples = (float) (std::min (noteTicks, stepTicks) * 0.25 / tps);
  192. emit (e);
  193. }
  194. else
  195. {
  196. if (sounding >= 0)
  197. {
  198. e.type = NoteEvent::Type::off;
  199. e.key = sounding;
  200. emit (e);
  201. }
  202. e.type = NoteEvent::Type::on;
  203. e.key = key;
  204. e.velocity = velocity;
  205. emit (e);
  206. sounding = key;
  207. sliding = s.slide;
  208. }
  209. // slide: the note holds until the chord is released
  210. offTick = s.slide ? -1.0 : boundary + noteTicks;
  211. }
  212. boundary += stepTicks;
  213. nextStep = boundary;
  214. }
  215. }
  216. } // namespace lynx