NoteEdit.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #pragma once
  2. // Note editing operations used by the Piano Roll (selection, copy/paste,
  3. // quantize...). They work with indices into Pattern::notes. No JUCE, testable.
  4. #include <algorithm>
  5. #include <cstdint>
  6. #include <string>
  7. #include <vector>
  8. #include "core/Song.h"
  9. namespace lynx::noteedit
  10. {
  11. // Sorts items with a `start` field (notes, clips) and updates `tracked` (indices) to the new positions.
  12. template <typename T>
  13. void sortByStartTracking (std::vector<T>& items, std::vector<int>& tracked)
  14. {
  15. std::vector<int> order (items.size());
  16. for (std::size_t i = 0; i < order.size(); ++i)
  17. order[i] = (int) i;
  18. std::stable_sort (order.begin(), order.end(),
  19. [&] (int a, int b) { return items[(std::size_t) a].start < items[(std::size_t) b].start; });
  20. std::vector<int> newIndex (order.size());
  21. std::vector<T> sorted;
  22. sorted.reserve (order.size());
  23. for (std::size_t j = 0; j < order.size(); ++j)
  24. {
  25. newIndex[(std::size_t) order[j]] = (int) j;
  26. sorted.push_back (items[(std::size_t) order[j]]);
  27. }
  28. items = std::move (sorted);
  29. for (auto& i : tracked)
  30. if (i >= 0 && i < (int) newIndex.size())
  31. i = newIndex[(std::size_t) i];
  32. }
  33. // Sorts the notes by start and updates `tracked` (indices) to the new positions.
  34. void sortTracking (Pattern& pattern, std::vector<int>& tracked);
  35. // Removes the notes at the given indices (in any order, duplicates ignored).
  36. void remove (Pattern& pattern, std::vector<int> indices);
  37. // Copies of the notes at the given indices, in start order.
  38. std::vector<Note> copy (const Pattern& pattern, const std::vector<int>& indices);
  39. // Appends `notes` shifted by `offset` ticks; channel >= 0 forces the channel.
  40. // Returns the indices of the new notes (the vector is not re-sorted).
  41. std::vector<int> paste (Pattern& pattern, const std::vector<Note>& notes, std::int64_t offset, int channel = -1);
  42. // Start of the first note and end of the last (0, 0 if empty).
  43. void span (const std::vector<Note>& notes, std::int64_t& start, std::int64_t& end);
  44. // Duplicates the notes right after themselves (like FL's Ctrl+B): the offset is the
  45. // span length rounded up to a multiple of `grid`. Returns the new indices.
  46. std::vector<int> duplicate (Pattern& pattern, const std::vector<int>& indices, std::int64_t grid);
  47. // Rounds the notes' start to the nearest multiple of `grid` (length kept).
  48. void quantize (Pattern& pattern, const std::vector<int>& indices, std::int64_t grid);
  49. // Rounds start and end to the nearest multiple of `grid` (the length stays at least one grid unit).
  50. void quantizeFull (Pattern& pattern, const std::vector<int>& indices, std::int64_t grid);
  51. // Moves the notes by (deltaTicks, deltaKeys), clamping so none leaves [0, ...) and [0, 127].
  52. // Returns the offset actually applied.
  53. void move (Pattern& pattern, const std::vector<int>& indices, std::int64_t& deltaTicks, int& deltaKeys);
  54. // --- quick note operations (each is one undo step in the Piano Roll) -----------------
  55. // Splits note `index` at `tick` (absolute) when the tick is strictly inside it. The original keeps the
  56. // head; the tail is appended. Returns the tail's index, or -1 when nothing was split.
  57. int split (Pattern& pattern, int index, std::int64_t tick);
  58. // Chop to grid: cuts every note at each multiple of `grid` that falls inside it. Returns the indices of
  59. // all the resulting pieces (originals included; the vector is not re-sorted).
  60. std::vector<int> chop (Pattern& pattern, const std::vector<int>& indices, std::int64_t grid);
  61. // Join: merges notes of the same channel and key that touch (end == next start) or overlap into one
  62. // note (the first one's properties are kept). Returns the number of notes removed.
  63. int join (Pattern& pattern, const std::vector<int>& indices);
  64. // Legato: each note (per channel) lasts until the next later start among the given notes; notes that
  65. // start together (a chord) end together. The last ones keep their length.
  66. void legato (Pattern& pattern, const std::vector<int>& indices);
  67. // Shrink to grid: every length becomes one grid unit.
  68. void shrinkToGrid (Pattern& pattern, const std::vector<int>& indices, std::int64_t grid);
  69. // Rotate within the bar: moves the notes `steps` grid units (negative = left), wrapping inside the
  70. // whole bars the notes start in (`bar` = ticks per bar).
  71. void rotate (Pattern& pattern, const std::vector<int>& indices, std::int64_t grid, std::int64_t bar, int steps);
  72. // --- grid --------------------------------------------------------------------------
  73. // The multiple of `step` at or before `tick` (step <= 1: whole ticks).
  74. std::int64_t snapDown (double tick, std::int64_t step);
  75. // The candidate closest to `tick` when it lies within `maxDistance` ticks; otherwise snapDown (tick, fallbackStep).
  76. std::int64_t snapNearest (double tick, const std::vector<std::int64_t>& candidates, double maxDistance, std::int64_t fallbackStep);
  77. // The finest of the usual grid steps (1/4 step, 1/2 step, step, beat, bar, then whole multiples of bars)
  78. // whose width on screen is at least `minPixels`, i.e. the lines the editor draws ("Screen" grid).
  79. std::int64_t screenStep (double pixelsPerTick, std::int64_t bar, double minPixels = 16.0);
  80. // "bar:step:tick" (bar and step from 1, tick from 0; 16 steps per 4/4 bar) <-> ticks.
  81. std::string formatTime (std::int64_t ticks, int beatsPerBar, bool isLength = false); // lengths count bars and steps from 0
  82. bool parseTime (const std::string& text, int beatsPerBar, std::int64_t& ticks, bool isLength = false);
  83. // --- Playlist clips ------------------------------------------------------------------
  84. // Moves the clip's left edge to `newStart` without moving its content (the offset follows). The start
  85. // cannot go before the content's beginning (offset 0) nor past end - minLength.
  86. void trimStart (Clip& clip, std::int64_t newStart, std::int64_t minLength);
  87. // Slip: moves the content under the clip by `delta` ticks (positive = later); the clip stays put.
  88. // The offset never goes below 0.
  89. void slip (Clip& clip, std::int64_t delta);
  90. // Inserts `length` ticks of silence at `at`: clips starting at or after it (and markers) move right.
  91. void insertTime (std::vector<Clip>& clips, std::vector<Marker>& markers, std::int64_t at, std::int64_t length);
  92. // Removes [from, to) from the timeline: what lies inside is deleted, clips crossing an edge are cut,
  93. // and everything after `to` (clips and markers) moves left.
  94. void deleteTime (std::vector<Clip>& clips, std::vector<Marker>& markers, std::int64_t from, std::int64_t to);
  95. // Indices of the clips that overlap [from, to).
  96. std::vector<int> clipsInTime (const std::vector<Clip>& clips, std::int64_t from, std::int64_t to);
  97. // Repeats the time selection [from, to) right after itself: copies of `sources` (or, if empty, of the
  98. // clips that start inside it) shifted by its length. Returns the new indices.
  99. std::vector<int> repeatTime (std::vector<Clip>& clips, std::int64_t from, std::int64_t to, std::vector<int> sources);
  100. // --- ripple editing and time-selection moves (docs/WORKFLOW_FL.md P2) -------------------------
  101. // Ripple delete: removes the clips and closes each gap: on the same track, the clips after a removed
  102. // clip move left by its length.
  103. void rippleDelete (std::vector<Clip>& clips, std::vector<int> indices);
  104. // Ripple resize: clip `index` gets `newLength` (at least 1) and every later clip on its track moves by the
  105. // change, so nothing after it overlaps or opens a gap.
  106. void rippleResize (std::vector<Clip>& clips, int index, std::int64_t newLength);
  107. // Moves (or copies) what lies inside the time selection [from, to) by `delta` ticks: clips crossing its
  108. // edges are cut there first, so only the part inside moves. Returns the indices of the moved clips (or
  109. // of the copies). The start never goes below 0.
  110. std::vector<int> moveTimeRange (std::vector<Clip>& clips, std::int64_t from, std::int64_t to, std::int64_t delta, bool copy);
  111. // --- Playlist tracks and clips (docs/WORKFLOW_FL.md P2: clip and track menus) ------------------
  112. // Cuts clip `index` at every multiple of `grid` (song time) inside it. Returns the indices of all pieces.
  113. std::vector<int> chopClip (std::vector<Clip>& clips, int index, std::int64_t grid);
  114. // Inserts an empty track at `track` (the clips there and below move down); every arrangement follows.
  115. void insertTrack (Song& song, int track);
  116. // Removes track `track` and its clips (the tracks below move up), in every arrangement.
  117. void removeTrack (Song& song, int track);
  118. // Swaps two tracks (their clips and headers), in every arrangement.
  119. void swapTracks (Song& song, int a, int b);
  120. // Solo: `track` plays alone (every other track muted); on the only unmuted track it unmutes all.
  121. void soloTrack (Song& song, int track, int trackCount);
  122. // --- Playlist track groups (FL's "Group with above track", REA round 104) -----------------------------
  123. // A track with `grouped` set belongs to the group of the nearest ungrouped track above it: the header.
  124. bool isGroupHeader (const Song& song, int track); // the next track is grouped with it
  125. int groupHeaderOf (const Song& song, int track); // the header of the track's group (itself for a header), -1 if none
  126. std::vector<int> groupMembers (const Song& song, int header); // the grouped tracks under a header
  127. bool isFoldedAway (const Song& song, int track); // a member of a collapsed group (drawn as a thin row)
  128. void groupWithAbove (Song& song, int track); // no-op on track 0
  129. void ungroupTrack (Song& song, int track); // a member leaves its group (headers stay)
  130. void setGroupCollapsed (Song& song, int track, bool collapsed); // the group of `track`
  131. void colourGroup (Song& song, int track); // "Auto color group": members take the header's colour
  132. } // namespace lynx::noteedit