HeldNotes.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #pragma once
  2. // Live notes that something started and must stop again: UI previews (step clicks, the Piano Roll keyboard,
  3. // the typing piano, auditions) and keys held on a MIDI keyboard. Every note-on is matched by exactly one
  4. // note-off for the same channel and key, however the gesture ends (mouse released elsewhere, focus lost,
  5. // window hidden, the selected channel or the scale snap changed while the key was held).
  6. #include <algorithm>
  7. #include <array>
  8. #include <cstddef>
  9. #include <functional>
  10. #include <utility>
  11. #include <vector>
  12. namespace lynx
  13. {
  14. class HeldNotes
  15. {
  16. public:
  17. using Sender = std::function<void (int channel, int key, float velocity, bool on)>;
  18. HeldNotes() = default;
  19. explicit HeldNotes (Sender sender) : send (std::move (sender)) {}
  20. ~HeldNotes() { releaseAll(); }
  21. HeldNotes (const HeldNotes&) = delete;
  22. HeldNotes& operator= (const HeldNotes&) = delete;
  23. void setSender (Sender sender) { send = std::move (sender); }
  24. // Starts a note. `tag` groups notes started together (the typing piano: the key code) so they can be
  25. // released together. A note already held (same channel and key) is stopped first, never doubled.
  26. void press (int channel, int key, float velocity, int tag = 0)
  27. {
  28. if (channel < 0 || key < 0 || key > 127)
  29. return;
  30. release (channel, key);
  31. held.push_back ({ channel, key, tag });
  32. if (send)
  33. send (channel, key, velocity, true);
  34. }
  35. // Stops one note (nothing happens if it is not held).
  36. void release (int channel, int key)
  37. {
  38. for (std::size_t i = 0; i < held.size(); ++i)
  39. if (held[i].channel == channel && held[i].key == key)
  40. {
  41. held.erase (held.begin() + (std::ptrdiff_t) i);
  42. if (send)
  43. send (channel, key, 0.0f, false);
  44. return;
  45. }
  46. }
  47. // Stops every note started with this tag.
  48. void releaseTag (int tag)
  49. {
  50. for (std::size_t i = 0; i < held.size();)
  51. if (held[i].tag == tag)
  52. {
  53. const auto h = held[i];
  54. held.erase (held.begin() + (std::ptrdiff_t) i);
  55. if (send)
  56. send (h.channel, h.key, 0.0f, false);
  57. }
  58. else
  59. ++i;
  60. }
  61. // Stops everything (mouse up, focus lost, hidden, destroyed).
  62. void releaseAll()
  63. {
  64. auto doomed = std::move (held);
  65. held.clear();
  66. if (send)
  67. for (const auto& h : doomed)
  68. send (h.channel, h.key, 0.0f, false);
  69. }
  70. bool isHeld (int channel, int key) const noexcept
  71. {
  72. for (const auto& h : held)
  73. if (h.channel == channel && h.key == key)
  74. return true;
  75. return false;
  76. }
  77. bool hasTag (int tag) const noexcept
  78. {
  79. for (const auto& h : held)
  80. if (h.tag == tag)
  81. return true;
  82. return false;
  83. }
  84. template <typename Fn>
  85. void forEachTag (Fn&& fn) const
  86. {
  87. std::vector<int> tags;
  88. for (const auto& h : held)
  89. if (std::find (tags.begin(), tags.end(), h.tag) == tags.end())
  90. tags.push_back (h.tag);
  91. for (auto t : tags)
  92. fn (t);
  93. }
  94. bool empty() const noexcept { return held.empty(); }
  95. std::size_t size() const noexcept { return held.size(); }
  96. private:
  97. struct Held
  98. {
  99. int channel, key, tag;
  100. };
  101. std::vector<Held> held;
  102. Sender send;
  103. };
  104. // The key a played note really started (after the scale snap), so its note-off stops that same key even when
  105. // the snap changed while the key was held. UI thread.
  106. class KeyLedger
  107. {
  108. public:
  109. void on (int channel, int key, int resolved)
  110. {
  111. entries.push_back ({ channel, key, resolved });
  112. }
  113. // the key the matching note-on started (the oldest one still open), or `fallback` if none
  114. int off (int channel, int key, int fallback)
  115. {
  116. for (std::size_t i = 0; i < entries.size(); ++i)
  117. if (entries[i][0] == channel && entries[i][1] == key)
  118. {
  119. const auto r = entries[i][2];
  120. entries.erase (entries.begin() + (std::ptrdiff_t) i);
  121. return r;
  122. }
  123. return fallback;
  124. }
  125. std::size_t size() const noexcept { return entries.size(); }
  126. private:
  127. std::vector<std::array<int, 3>> entries;
  128. };
  129. // MIDI input: which channels and key each incoming note number started, so the note-off reaches them even if
  130. // the selected channels or the scale snap changed while the key was down. Fixed size, no allocation (the MIDI
  131. // thread uses it).
  132. class InputHeldNotes
  133. {
  134. public:
  135. static constexpr int maxTargets = 17;
  136. struct Target
  137. {
  138. int channel = -1, key = -1;
  139. };
  140. // Remembers the targets of `note`. If the note was already down (no note-off came in between) the previous
  141. // targets are returned in `stale` (they must be stopped first); returns how many.
  142. int noteOn (int note, const Target* targets, int count, Target* stale)
  143. {
  144. if (note < 0 || note > 127)
  145. return 0;
  146. auto& e = notes[(std::size_t) note];
  147. auto staleCount = 0;
  148. for (int i = 0; i < e.count; ++i)
  149. stale[staleCount++] = e.targets[(std::size_t) i];
  150. e.count = count < maxTargets ? count : maxTargets;
  151. for (int i = 0; i < e.count; ++i)
  152. e.targets[(std::size_t) i] = targets[i];
  153. return staleCount;
  154. }
  155. // The targets `note` started (forgotten); returns how many. 0: the note was not down.
  156. int noteOff (int note, Target* out)
  157. {
  158. if (note < 0 || note > 127)
  159. return 0;
  160. auto& e = notes[(std::size_t) note];
  161. const auto count = e.count;
  162. for (int i = 0; i < count; ++i)
  163. out[i] = e.targets[(std::size_t) i];
  164. e.count = 0;
  165. return count;
  166. }
  167. private:
  168. struct Entry
  169. {
  170. std::array<Target, maxTargets> targets {};
  171. int count = 0;
  172. };
  173. std::array<Entry, 128> notes {};
  174. };
  175. } // namespace lynx