SimpleStepsPage.inc 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. // The Simple dock's Drum Machine tab (round 190lr, DS-39; included by ui/SimpleDock.cpp). BandLab's drum machine dock
  2. // (reference /mnt/e/lynx-work/bandlab190p/shots/09-drum-machine-dock.png): a top row with Length, Swing, the page's own
  3. // ▶ and the patterns A–H with "Add A"; a ruler of beats ("1.1 1.2 …", a bar's first beat in the track colour); the step
  4. // grid, one row per kit sound with its icon and the typing key that plays it; a step at the default velocity in the
  5. // track colour, a softer one darker. The model is core/SimpleSteps.h (a view of the Drums track: nothing new saved).
  6. // Motion: only while the song (or the page's ▶) plays and the page shows, on the main window's 30 Hz tick, and only the
  7. // two columns whose state changed repaint.
  8. class SimpleDock::StepsPage final : public juce::Component,
  9. public juce::SettableTooltipClient,
  10. public demo::CellSource
  11. {
  12. public:
  13. explicit StepsPage (AudioEngine& e) : engine (e)
  14. {
  15. setComponentID ("simple.dock.drumMachine"); // (round 190np: --demo finds it, and its cells, by this ID)
  16. setWantsKeyboardFocus (false);
  17. setRepaintsOnMouseActivity (false);
  18. }
  19. void show (int newTrack)
  20. {
  21. if (newTrack != track)
  22. {
  23. letter = 0;
  24. stopPreview();
  25. }
  26. track = newTrack;
  27. update();
  28. }
  29. void update()
  30. {
  31. const auto& song = engine.getSong();
  32. rows = simplesteps::kitRows (song, track);
  33. pattern = simplesteps::letterPattern (song, track, letter);
  34. if (pattern < 0 && letter != 0)
  35. {
  36. letter = 0;
  37. pattern = simplesteps::letterPattern (song, track, 0);
  38. }
  39. steps = pattern >= 0 ? simplesteps::stepCount (song, pattern) : 16;
  40. resized();
  41. repaint();
  42. }
  43. // the main window's 30 Hz tick, only while the page shows (SimpleDock::tickInputMeter)
  44. void watchPlayback()
  45. {
  46. const auto& transport = engine.getTransport();
  47. const auto playing = transport.playing.load (std::memory_order_relaxed);
  48. if (previewing && ! playing && juce::Time::getMillisecondCounter() - previewStarted > 500) // (the transport may start a block later)
  49. stopPreview();
  50. const auto pos = transport.positionTicks.load (std::memory_order_relaxed);
  51. // BandLab (capture C b): while the song plays, the dock shows the pattern under the playhead and its ▶, letters and
  52. // Add are greyed out
  53. const auto song = playing && ! previewing;
  54. if (song != songPlaying)
  55. {
  56. songPlaying = song;
  57. repaint (topArea);
  58. }
  59. if (song && isShowing())
  60. {
  61. const auto& s = engine.getSong();
  62. for (const auto& c : s.playlist)
  63. if (c.kind == ClipKind::pattern && c.track == track && ! c.muted && pos >= (double) c.start && pos < (double) c.end())
  64. {
  65. if (const auto l = simplesteps::letterOf (s, track, c.pattern); l >= 0 && l != letter)
  66. {
  67. letter = l;
  68. update();
  69. }
  70. break;
  71. }
  72. }
  73. const auto step = isShowing() ? simplesteps::playingStep (engine.getSong(), track, pattern, pos, playing) : -1;
  74. setPlayingStep (step);
  75. }
  76. // tests and the shot route
  77. int rowCountForCheck() const noexcept { return (int) rows.size(); }
  78. int stepCountForCheck() const noexcept { return steps; }
  79. int letterForCheck() const noexcept { return letter; }
  80. int patternForCheck() const noexcept { return pattern; }
  81. int playingStepForCheck() const noexcept { return shownStep; }
  82. bool previewingForCheck() const noexcept { return previewing; }
  83. void clickStepForCheck (int row, int step) { toggle (row, step); }
  84. void pickLetterForCheck (int l) { pickLetter (l); }
  85. void addClipForCheck() { addClip(); }
  86. void setLengthForCheck (int bars) { setLength (bars); }
  87. void setSwingForCheck (float v) { setSwing (v); }
  88. void previewForCheck() { togglePreview(); }
  89. void playingStepForSnapshot (int step) { setPlayingStep (step); }
  90. void velocityForCheck (int row, int step, float v) { setVelocity (row, step, v); }
  91. void velocityToolForSnapshot (bool on) { velocityTool = on; repaint(); }
  92. juce::PopupMenu lengthMenuForSnapshot() const { return lengthMenu(); }
  93. juce::Rectangle<int> gridBoundsForSnapshot() const { return gridArea; }
  94. // round 190np (--demo): a step's cell (row 0 = the bottom row, as drawn), for ui.find / ui.click with "cell"
  95. juce::Rectangle<int> demoCellBounds (int row, int column) const override
  96. {
  97. return row >= 0 && row < (int) rows.size() && column >= 0 && column < steps ? cellBounds (row, column) : juce::Rectangle<int>();
  98. }
  99. // a typing key (BandLab's row keys): plays the row and lights its header; −1 when the key is not one of them
  100. int channelForKey (int keyCode)
  101. {
  102. const auto row = simplesteps::rowForKey (keyCode);
  103. return row >= 0 && row < (int) rows.size() ? rows[(std::size_t) row] : -1;
  104. }
  105. void paint (juce::Graphics& g) override
  106. {
  107. const auto& song = engine.getSong();
  108. const auto colourOfTrack = trackColour();
  109. // ---- the top row
  110. if (g.clipRegionIntersects (topArea))
  111. {
  112. g.setFont (font (type::body, true));
  113. g.setColour (colour::textPrimary);
  114. g.drawText (tr ("Length"), lengthLabel, juce::Justification::centredRight, false);
  115. // BandLab's Edit: the pointer (click steps on / off) | V (drag a step up / down: its velocity)
  116. g.drawText (tr ("Edit"), editLabel, juce::Justification::centredRight, false);
  117. fillRoundRect (g, editBox.toFloat(), editBox.getHeight() * 0.5f, colour::bg4);
  118. for (int k = 0; k < 2; ++k)
  119. {
  120. const auto seg = editBox.withWidth (editBox.getWidth() / 2).translated (k * editBox.getWidth() / 2, 0).reduced (2);
  121. if ((k == 1) == velocityTool)
  122. fillRoundRect (g, seg.toFloat(), seg.getHeight() * 0.5f, colour::bg6);
  123. g.setColour ((k == 1) == velocityTool ? colour::textPrimary : colour::textSecondary);
  124. if (k == 0)
  125. {
  126. juce::Path arrow; // (a pointer)
  127. const auto c = seg.getCentre().toFloat();
  128. arrow.startNewSubPath (c.x - 4.0f, c.y - 6.0f);
  129. arrow.lineTo (c.x + 5.0f, c.y + 1.0f);
  130. arrow.lineTo (c.x + 0.5f, c.y + 1.5f);
  131. arrow.lineTo (c.x - 1.5f, c.y + 6.0f);
  132. arrow.closeSubPath();
  133. g.strokePath (arrow, juce::PathStrokeType (1.3f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  134. }
  135. else
  136. g.drawText ("V", seg, juce::Justification::centred, false);
  137. }
  138. g.drawText (tr ("Swing"), swingLabel, juce::Justification::centredRight, false);
  139. g.drawText (tr ("Patterns"), patternsLabel, juce::Justification::centredRight, false);
  140. // Length: "2 bars ▾"
  141. fillRoundRect (g, lengthBox.toFloat(), radius::md, hover == Part::length ? colour::bg5 : colour::bg4);
  142. g.setColour (colour::textPrimary);
  143. const auto bars = pattern >= 0 ? simplesteps::lengthBars (song, pattern) : 1;
  144. g.drawText (barsText (bars), lengthBox.reduced (10, 0).withTrimmedRight (14), juce::Justification::centredLeft, false);
  145. {
  146. juce::Path down; // (BandLab's ▾)
  147. const auto cx = (float) lengthBox.getRight() - 14.0f, cy = (float) lengthBox.getCentreY();
  148. down.startNewSubPath (cx - 4.0f, cy - 2.0f);
  149. down.lineTo (cx, cy + 2.0f);
  150. down.lineTo (cx + 4.0f, cy - 2.0f);
  151. g.setColour (colour::textSecondary);
  152. g.strokePath (down, juce::PathStrokeType (1.5f, juce::PathStrokeType::curved, juce::PathStrokeType::rounded));
  153. }
  154. // Swing: a slim bar in the track colour with a round thumb, and "60%"
  155. const auto swing = juce::jlimit (0.0f, 1.0f, song.swing);
  156. const auto bar = swingBar.toFloat();
  157. fillRoundRect (g, bar.withSizeKeepingCentre (bar.getWidth(), 3.0f), 1.5f, colour::bg5);
  158. fillRoundRect (g, bar.withSizeKeepingCentre (bar.getWidth(), 3.0f).withWidth (bar.getWidth() * swing), 1.5f, colourOfTrack);
  159. g.setColour (colour::textPrimary);
  160. g.fillEllipse (juce::Rectangle<float> (10.0f, 10.0f).withCentre ({ bar.getX() + bar.getWidth() * swing, bar.getCentreY() }));
  161. g.setFont (font (type::body, true));
  162. g.drawText (juce::String (juce::roundToInt (swing * 100.0f)) + "%", swingValue, juce::Justification::centredLeft, false);
  163. // the page's own ▶ (■ while it plays the pattern)
  164. const auto play = playButton.toFloat();
  165. const auto ink = songPlaying ? colour::textDisabled : colour::textPrimary; // (greyed while the song plays)
  166. g.setColour (hover == Part::play && ! songPlaying ? colour::bg5 : colour::bg4);
  167. g.fillEllipse (play);
  168. if (previewing)
  169. g.setColour (ink), g.fillRect (juce::Rectangle<float> (12.0f, 12.0f).withCentre (play.getCentre())); // (BandLab's ■ 12 x 12)
  170. else
  171. {
  172. juce::Path tri; // (BandLab's ▶ 10 x 12)
  173. const auto c = play.getCentre();
  174. tri.addTriangle (c.x - 4.0f, c.y - 6.0f, c.x - 4.0f, c.y + 6.0f, c.x + 6.0f, c.y);
  175. g.setColour (ink);
  176. g.fillPath (tri);
  177. }
  178. // A … H: the shown one white with dark ink (BandLab), the others on the control colour
  179. const auto with = simplesteps::lettersWithPattern (song, track);
  180. for (int l = 0; l < simplesteps::letterCount; ++l)
  181. {
  182. const auto r = letterBox (l).toFloat();
  183. const auto shown = l == letter;
  184. const auto over = hover == Part::letter && hoverIndex == l && ! songPlaying;
  185. fillRoundRect (g, r, radius::md, shown ? (songPlaying ? colour::textDisabled : over ? colour::textSecondary.brighter (0.4f) : colour::textPrimary)
  186. : over ? colour::bg5 : colour::bg4);
  187. g.setColour (shown ? colour::bg0 : songPlaying ? colour::textDisabled : (with & (1u << l)) != 0 ? colour::textPrimary : colour::textSecondary);
  188. g.setFont (font (type::small, true));
  189. g.drawText (juce::String::charToString ((juce::juce_wchar) ('A' + l)), r.toNearestInt(), juce::Justification::centred, false);
  190. }
  191. fillRoundRect (g, addButton.toFloat(), radius::md, hover == Part::add && ! songPlaying ? colour::bg5 : colour::bg4);
  192. g.setColour (songPlaying ? colour::textDisabled : colour::textPrimary);
  193. g.setFont (font (type::small, true));
  194. g.drawText (tr ("Add {0}", { juce::String::charToString ((juce::juce_wchar) ('A' + letter)) }), addButton, juce::Justification::centred, false);
  195. }
  196. // ---- the ruler: "1.1 1.2 …" at each beat, a bar's first beat in the track colour
  197. const auto stepsPerBeat = (int) (song.meter().beat() / ticksPerStep);
  198. const auto beatsPerBar = juce::jmax (1, (int) (song.barTicks() / juce::jmax<std::int64_t> (1, song.meter().beat())));
  199. if (g.clipRegionIntersects (rulerArea))
  200. {
  201. g.setFont (font (type::caption, true));
  202. for (int s = 0; s < steps; s += juce::jmax (1, stepsPerBeat))
  203. {
  204. const auto beat = s / juce::jmax (1, stepsPerBeat);
  205. const auto x = cellX (s);
  206. const auto first = beat % beatsPerBar == 0;
  207. g.setColour (first ? ui::simplelook::readableOn (colourOfTrack, colour::bg1, 4.5f) : colour::textSecondary);
  208. g.drawText (juce::String (beat / beatsPerBar + 1) + "." + juce::String (beat % beatsPerBar + 1),
  209. juce::Rectangle<int> (x + 2, rulerArea.getY(), 60, rulerArea.getHeight()), juce::Justification::centredLeft, false);
  210. }
  211. }
  212. // ---- the rows: the icon and key, then the steps
  213. const auto& pat = pattern >= 0 ? song.patterns[(std::size_t) pattern] : emptyPattern;
  214. for (int row = 0; row < (int) rows.size(); ++row)
  215. {
  216. const auto y = rowY (row);
  217. const auto head = juce::Rectangle<int> (gridArea.getX(), y, headWidth, rowHeight);
  218. const auto ch = rows[(std::size_t) row];
  219. if (g.clipRegionIntersects (head))
  220. {
  221. const auto lit = row == litRow;
  222. const auto ink = lit ? colourOfTrack : colour::textSecondary;
  223. ui::drawTrackIcon (g, iconFor (song.channels[(std::size_t) ch]), juce::Rectangle<float> (14.0f, 14.0f).withCentre ({ (float) head.getX() + 12.0f, (float) head.getCentreY() }), ink);
  224. if (const auto key = simplesteps::rowKey (row); key != 0)
  225. {
  226. g.setColour (lit ? colourOfTrack : colour::textSecondary);
  227. g.setFont (font (type::small, true));
  228. g.drawText (juce::String::charToString ((juce::juce_wchar) key), head.withTrimmedLeft (26).withWidth (16), juce::Justification::centredLeft, false);
  229. }
  230. }
  231. for (int s = 0; s < steps; ++s)
  232. {
  233. const auto cell = cellBounds (row, s);
  234. if (! g.clipRegionIntersects (cell))
  235. continue;
  236. const auto beat = s / juce::jmax (1, stepsPerBeat);
  237. const auto ground = beat % 2 == 0 ? colour::bg3 : colour::bg2; // (BandLab: every other beat a shade darker)
  238. const auto velocity = stepVelocity (pat, ch, s);
  239. const auto hovered = hover == Part::cell && hoverIndex == row * maxSteps + s;
  240. // BandLab (capture bl-drum-preview-playing.png): the steps the playhead is on turn white; an empty cell under
  241. // the pointer takes a tint of the track colour (bl-drum-hover-cell-off.png)
  242. if (velocity >= 0.0f && s == shownStep)
  243. {
  244. g.setColour (juce::Colours::white);
  245. g.fillRect (cell);
  246. continue;
  247. }
  248. auto fill = velocity < 0.0f ? ground : ground.interpolatedWith (colourOfTrack, simplesteps::shade (velocity));
  249. if (velocity < 0.0f && hovered)
  250. fill = ground.interpolatedWith (colourOfTrack, 0.18f);
  251. g.setColour (fill);
  252. g.fillRect (cell);
  253. if (velocity >= 0.0f)
  254. {
  255. g.setColour (colourOfTrack.brighter (hovered ? 0.6f : 0.3f));
  256. g.drawRect (cell, 1);
  257. }
  258. }
  259. }
  260. if (rows.empty() && g.clipRegionIntersects (gridArea))
  261. {
  262. g.setColour (colour::textSecondary);
  263. g.setFont (font (type::small));
  264. g.drawFittedText (tr ("This drum track has no sounds. Add sounds to its group in the Channel Rack."), gridArea, juce::Justification::centred, 2);
  265. }
  266. }
  267. void resized() override
  268. {
  269. auto r = getLocalBounds().reduced (12, 6);
  270. topArea = r.removeFromTop (30);
  271. auto top = topArea;
  272. // left: Length and Swing (BandLab: Edit, Length, Swing in the middle of the row; the kit picker LYNX has no
  273. // equivalent of sits left of them there)
  274. editLabel = top.removeFromLeft (34);
  275. top.removeFromLeft (8);
  276. editBox = top.removeFromLeft (76).withSizeKeepingCentre (76, 28);
  277. top.removeFromLeft (20);
  278. lengthLabel = top.removeFromLeft (52);
  279. top.removeFromLeft (8);
  280. lengthBox = top.removeFromLeft (92).withSizeKeepingCentre (92, 28);
  281. top.removeFromLeft (20);
  282. swingLabel = top.removeFromLeft (46);
  283. top.removeFromLeft (10);
  284. swingBar = top.removeFromLeft (110).withSizeKeepingCentre (110, 14);
  285. top.removeFromLeft (8);
  286. swingValue = top.removeFromLeft (44);
  287. // right: ▶, Patterns, A … H, Add A
  288. addButton = top.removeFromRight (58).withSizeKeepingCentre (58, 28);
  289. top.removeFromRight (6);
  290. lettersArea = top.removeFromRight (simplesteps::letterCount * 32 + 4).withSizeKeepingCentre (simplesteps::letterCount * 32 + 4, 28);
  291. top.removeFromRight (6);
  292. patternsLabel = top.removeFromRight (64);
  293. top.removeFromRight (8);
  294. playButton = top.removeFromRight (28).withSizeKeepingCentre (28, 28);
  295. r.removeFromTop (8);
  296. rulerArea = r.removeFromTop (16).withTrimmedLeft (headWidth);
  297. r.removeFromTop (2);
  298. gridArea = r;
  299. const auto count = juce::jmax (1, (int) rows.size());
  300. rowHeight = juce::jlimit (10, 24, (gridArea.getHeight() + gap) / count - gap);
  301. rulerArea = rulerArea.withRight (gridArea.getRight());
  302. }
  303. void mouseMove (const juce::MouseEvent& e) override { setHover (e.getPosition()); }
  304. void mouseExit (const juce::MouseEvent&) override { setHover ({ -1, -1 }); }
  305. void mouseDown (const juce::MouseEvent& e) override
  306. {
  307. const auto p = e.getPosition();
  308. paintRow = -1;
  309. if (lengthBox.contains (p))
  310. {
  311. lengthMenu().showMenuAsync (juce::PopupMenu::Options().withTargetComponent (this).withTargetScreenArea (localAreaToGlobal (lengthBox)),
  312. [safe = juce::Component::SafePointer<StepsPage> (this)] (int id) {
  313. if (safe != nullptr && id > 0)
  314. safe->setLength (id);
  315. });
  316. return;
  317. }
  318. if (swingBar.expanded (6, 8).contains (p))
  319. {
  320. draggingSwing = true;
  321. engine.beginUndoGesture();
  322. setSwing (swingAt (p.x));
  323. return;
  324. }
  325. if (editBox.contains (p))
  326. {
  327. velocityTool = p.x >= editBox.getCentreX();
  328. repaint (topArea);
  329. return;
  330. }
  331. if (playButton.contains (p))
  332. return songPlaying ? void() : togglePreview();
  333. for (int l = 0; l < simplesteps::letterCount; ++l)
  334. if (letterBox (l).contains (p))
  335. return songPlaying ? void() : pickLetter (l);
  336. if (addButton.contains (p))
  337. return songPlaying ? void() : addClip();
  338. if (const auto row = rowAt (p.y); row >= 0 && p.x < gridArea.getX() + headWidth && p.x >= gridArea.getX())
  339. return audition (row);
  340. const auto [row, step] = cellAt (p);
  341. if (row < 0 || pattern < 0)
  342. return;
  343. // FL's and BandLab's grid: a click turns the step over; a drag paints that state along the row; right-click clears
  344. const auto& pat = engine.getSong().patterns[(std::size_t) pattern];
  345. if (velocityTool && ! e.mods.isPopupMenu()) // (BandLab's V: a vertical drag on a step sets its velocity)
  346. {
  347. if (stepVelocity (pat, rows[(std::size_t) row], step) >= 0.0f)
  348. {
  349. velocityRow = row, velocityStep = step;
  350. velocityStart = stepVelocity (pat, rows[(std::size_t) row], step);
  351. engine.beginUndoGesture();
  352. }
  353. return;
  354. }
  355. paintOn = ! e.mods.isPopupMenu() && stepVelocity (pat, rows[(std::size_t) row], step) < 0.0f;
  356. paintRow = row;
  357. lastPainted = -1;
  358. engine.beginUndoGesture();
  359. paintStep (step);
  360. if (paintOn)
  361. audition (row);
  362. }
  363. void mouseDrag (const juce::MouseEvent& e) override
  364. {
  365. if (draggingSwing)
  366. return setSwing (swingAt (e.x));
  367. if (velocityRow >= 0)
  368. return setVelocity (velocityRow, velocityStep, juce::jlimit (0.05f, 1.0f, velocityStart - (float) e.getDistanceFromDragStartY() / 100.0f));
  369. if (paintRow < 0)
  370. return;
  371. const auto [row, step] = cellAt ({ e.x, rowY (paintRow) + rowHeight / 2 });
  372. juce::ignoreUnused (row);
  373. if (step >= 0)
  374. paintStep (step);
  375. }
  376. void mouseUp (const juce::MouseEvent&) override
  377. {
  378. if (draggingSwing || paintRow >= 0 || velocityRow >= 0)
  379. engine.endUndoGesture();
  380. velocityRow = velocityStep = -1;
  381. draggingSwing = false;
  382. paintRow = -1;
  383. }
  384. // a row's sound was played (its key, a click on its header): the header lights for a moment
  385. void lightRow (int row)
  386. {
  387. litRow = row;
  388. litAt = juce::Time::getMillisecondCounter();
  389. repaint (gridArea.withWidth (headWidth));
  390. }
  391. private:
  392. enum class Part { none, length, swing, play, letter, add, cell };
  393. static constexpr int headWidth = 56, gap = 2, maxSteps = 1024;
  394. AudioEngine& engine;
  395. int track = -1, letter = 0, pattern = -1, steps = 16, rowHeight = 20;
  396. std::vector<int> rows;
  397. juce::Rectangle<int> topArea, editLabel, editBox, lengthLabel, lengthBox, swingLabel, swingBar, swingValue, playButton, patternsLabel, lettersArea, addButton,
  398. rulerArea, gridArea;
  399. Part hover = Part::none;
  400. int hoverIndex = -1;
  401. int shownStep = -1;
  402. bool previewing = false, draggingSwing = false, paintOn = true, songPlaying = false, velocityTool = false;
  403. int velocityRow = -1, velocityStep = -1;
  404. float velocityStart = 0.78f;
  405. public:
  406. juce::String lastMessage; // (the last bubble's text, for the checks)
  407. private:
  408. int paintRow = -1, lastPainted = -1, litRow = -1;
  409. juce::uint32 litAt = 0, previewStarted = 0;
  410. const Pattern emptyPattern {};
  411. juce::Colour trackColour() const
  412. {
  413. const auto& song = engine.getSong();
  414. const auto own = track >= 0 && track < (int) song.playlistTracks.size() ? song.playlistTracks[(std::size_t) track].colour : 0u;
  415. return juce::Colour (own != 0 ? own : simpletracks::defaultColour (simpletracks::Kind::drums)).withAlpha (1.0f);
  416. }
  417. static juce::String barsText (int bars) { return bars == 1 ? tr ("1 bar") : tr ("{0} bars", { juce::String (bars) }); }
  418. static trackicons::Icon iconFor (const ChannelDef& c)
  419. {
  420. using I = trackicons::Icon;
  421. if (c.icon > 0)
  422. if (const auto own = trackicons::iconFor (c.icon); own != I::none && own != I::tag)
  423. return own;
  424. const auto name = juce::String::fromUTF8 (c.name.c_str()).toLowerCase();
  425. switch (c.instrument)
  426. {
  427. case InstrumentKind::kick: return I::kick;
  428. case InstrumentKind::snare: return I::snare;
  429. case InstrumentKind::hat: return I::hiHat;
  430. case InstrumentKind::clap: return I::clap;
  431. default: break;
  432. }
  433. return name.contains ("kick") || name.contains ("808") ? I::kick
  434. : name.contains ("snare") || name.contains ("rim") ? I::snare
  435. : name.contains ("hat") ? I::hiHat
  436. : name.contains ("clap") || name.contains ("snap") ? I::clap
  437. : name.contains ("crash") || name.contains ("ride") || name.contains ("cymbal") ? I::cymbal
  438. : name.contains ("shaker") ? I::shaker
  439. : name.contains ("tamb") ? I::tambourine
  440. : name.contains ("cowbell") ? I::cowbell
  441. : I::percussion;
  442. }
  443. // the step's velocity (the loudest note starting there), −1 for an empty step
  444. static float stepVelocity (const Pattern& p, int channel, int step)
  445. {
  446. const auto tick = (std::int64_t) step * ticksPerStep;
  447. auto v = -1.0f;
  448. for (const auto& n : p.notes)
  449. {
  450. if (n.start > tick)
  451. break;
  452. if (n.start == tick && n.channel == channel && ! n.muted)
  453. v = juce::jmax (v, n.velocity);
  454. }
  455. return v;
  456. }
  457. int rowY (int row) const { return gridArea.getY() + ((int) rows.size() - 1 - row) * (rowHeight + gap); } // (row 0 at the bottom)
  458. int rowAt (int y) const
  459. {
  460. for (int row = 0; row < (int) rows.size(); ++row)
  461. if (y >= rowY (row) && y < rowY (row) + rowHeight)
  462. return row;
  463. return -1;
  464. }
  465. int cellX (int step) const
  466. {
  467. const auto left = gridArea.getX() + headWidth;
  468. return left + (int) ((std::int64_t) step * (gridArea.getRight() - left + gap) / juce::jmax (1, steps));
  469. }
  470. juce::Rectangle<int> cellBounds (int row, int step) const
  471. {
  472. const auto x = cellX (step);
  473. return { x, rowY (row), juce::jmax (1, cellX (step + 1) - x - gap), rowHeight };
  474. }
  475. std::pair<int, int> cellAt (juce::Point<int> p) const
  476. {
  477. const auto row = rowAt (p.y);
  478. if (row < 0 || p.x < gridArea.getX() + headWidth || p.x >= gridArea.getRight())
  479. return { -1, -1 };
  480. for (int s = 0; s < steps; ++s)
  481. if (p.x >= cellX (s) && p.x < cellX (s + 1))
  482. return { row, s };
  483. return { -1, -1 };
  484. }
  485. juce::Rectangle<int> letterBox (int l) const { return { lettersArea.getX() + l * 32 + 2, lettersArea.getY(), 28, lettersArea.getHeight() }; }
  486. float swingAt (int x) const { return juce::jlimit (0.0f, 1.0f, (float) (x - swingBar.getX()) / (float) juce::jmax (1, swingBar.getWidth())); }
  487. juce::PopupMenu lengthMenu() const
  488. {
  489. juce::PopupMenu m;
  490. const auto bars = pattern >= 0 ? simplesteps::lengthBars (engine.getSong(), pattern) : 1;
  491. for (auto b : simplesteps::lengthChoices)
  492. m.addItem (b, barsText (b), pattern >= 0, b == bars);
  493. return m;
  494. }
  495. void setHover (juce::Point<int> p)
  496. {
  497. auto part = Part::none;
  498. int index = -1;
  499. if (lengthBox.contains (p))
  500. part = Part::length;
  501. else if (playButton.contains (p))
  502. part = Part::play;
  503. else if (addButton.contains (p))
  504. part = Part::add;
  505. else
  506. {
  507. for (int l = 0; l < simplesteps::letterCount && part == Part::none; ++l)
  508. if (letterBox (l).contains (p))
  509. part = Part::letter, index = l;
  510. if (part == Part::none)
  511. if (const auto [row, step] = cellAt (p); row >= 0)
  512. part = Part::cell, index = row * maxSteps + step;
  513. }
  514. if (part == hover && index == hoverIndex)
  515. return;
  516. const auto area = [this] (Part pt, int i) -> juce::Rectangle<int> {
  517. switch (pt)
  518. {
  519. case Part::length: return lengthBox;
  520. case Part::play: return playButton;
  521. case Part::add: return addButton;
  522. case Part::letter: return letterBox (i);
  523. case Part::cell: return cellBounds (i / maxSteps, i % maxSteps).expanded (1);
  524. default: return {};
  525. }
  526. };
  527. repaint (area (hover, hoverIndex));
  528. hover = part;
  529. hoverIndex = index;
  530. repaint (area (hover, hoverIndex));
  531. if (part == Part::play)
  532. setTooltip (previewing ? tr ("Stop the pattern") : tr ("Play this pattern on its own"));
  533. else if (part == Part::add)
  534. setTooltip (tr ("Put this pattern on the track at the song position"));
  535. else if (part == Part::cell && index / maxSteps < (int) rows.size())
  536. setTooltip (juce::String::fromUTF8 (engine.getSong().channels[(std::size_t) rows[(std::size_t) (index / maxSteps)]].name.c_str()));
  537. else
  538. setTooltip ({});
  539. }
  540. void setPlayingStep (int step)
  541. {
  542. if (litRow >= 0 && juce::Time::getMillisecondCounter() - litAt > 150)
  543. {
  544. litRow = -1;
  545. repaint (gridArea.withWidth (headWidth));
  546. }
  547. if (step == shownStep)
  548. return;
  549. const auto column = [this] (int s) { return s < 0 ? juce::Rectangle<int>() : juce::Rectangle<int> (cellX (s), gridArea.getY(), cellX (s + 1) - cellX (s), gridArea.getHeight()); };
  550. repaint (column (shownStep));
  551. shownStep = step;
  552. repaint (column (shownStep));
  553. }
  554. void toggle (int row, int step)
  555. {
  556. if (row < 0 || row >= (int) rows.size() || pattern < 0 || step < 0 || step >= steps)
  557. return;
  558. engine.editSong ([p = pattern, ch = rows[(std::size_t) row], step] (Song& s) {
  559. if (p < (int) s.patterns.size())
  560. s.patterns[(std::size_t) p].toggleStep (ch, step);
  561. });
  562. repaint (cellBounds (row, step).expanded (1));
  563. }
  564. void paintStep (int step)
  565. {
  566. if (step == lastPainted || paintRow < 0 || pattern < 0)
  567. return;
  568. lastPainted = step;
  569. const auto ch = rows[(std::size_t) paintRow];
  570. const auto& pat = engine.getSong().patterns[(std::size_t) pattern];
  571. if ((stepVelocity (pat, ch, step) >= 0.0f) != paintOn)
  572. toggle (paintRow, step);
  573. }
  574. void setVelocity (int row, int step, float v)
  575. {
  576. if (pattern < 0 || row < 0 || row >= (int) rows.size())
  577. return;
  578. engine.editSong ([p = pattern, ch = rows[(std::size_t) row], tick = (std::int64_t) step * ticksPerStep, v] (Song& s) {
  579. if (p < (int) s.patterns.size())
  580. for (auto& n : s.patterns[(std::size_t) p].notes)
  581. if (n.channel == ch && n.start == tick)
  582. n.velocity = v;
  583. });
  584. repaint (cellBounds (row, step).expanded (1));
  585. }
  586. void audition (int row)
  587. {
  588. if (row < 0 || row >= (int) rows.size())
  589. return;
  590. const auto ch = rows[(std::size_t) row];
  591. engine.sendLiveNote (ch, 60, 0.78f, true, false);
  592. engine.sendLiveNote (ch, 60, 0.0f, false, false);
  593. lightRow (row);
  594. }
  595. void pickLetter (int l)
  596. {
  597. if (l < 0 || l >= simplesteps::letterCount)
  598. return;
  599. if (simplesteps::letterPattern (engine.getSong(), track, l) < 0)
  600. engine.editSong ([t = track, l] (Song& s) { simplesteps::ensureLetter (s, t, l); });
  601. letter = l;
  602. const auto wasPreviewing = previewing;
  603. update();
  604. if (wasPreviewing && pattern >= 0)
  605. engine.editSong ([p = pattern] (Song& s) { s.currentPattern = p; }, false); // (the preview goes on with the new letter)
  606. }
  607. void addClip()
  608. {
  609. // BandLab (capture C e): an empty pattern is not put on the track: "Make a beat first before exporting it to a track."
  610. if (pattern < 0 || engine.getSong().patterns[(std::size_t) pattern].notes.empty())
  611. {
  612. auto* bubble = new juce::BubbleMessageComponent (300);
  613. addChildComponent (bubble);
  614. bubble->showAt (addButton, juce::AttributedString (tr ("Make a beat first before exporting it to a track.")), 2500, true, true);
  615. lastMessage = tr ("Make a beat first before exporting it to a track.");
  616. return;
  617. }
  618. const auto at = (std::int64_t) juce::jmax (0.0, engine.getTransport().positionTicks.load (std::memory_order_relaxed));
  619. engine.editSong ([t = track, l = letter, at] (Song& s) {
  620. simplesteps::ensureLetter (s, t, l);
  621. simplesteps::addLetterClip (s, t, l, at);
  622. });
  623. }
  624. void setLength (int bars)
  625. {
  626. if (pattern < 0)
  627. return;
  628. engine.editSong ([p = pattern, bars] (Song& s) { simplesteps::setLengthBars (s, p, bars); });
  629. update();
  630. }
  631. void setSwing (float v)
  632. {
  633. if (std::abs (engine.getSong().swing - v) < 1.0e-4f)
  634. return;
  635. engine.editSong ([v] (Song& s) { s.swing = v; });
  636. repaint (topArea);
  637. }
  638. void togglePreview()
  639. {
  640. if (previewing)
  641. return stopPreview (true);
  642. if (pattern < 0)
  643. return;
  644. if (engine.getTransport().playing.load (std::memory_order_relaxed))
  645. engine.stop();
  646. engine.editSong ([p = pattern] (Song& s) {
  647. s.mode = PlayMode::pattern;
  648. s.currentPattern = p;
  649. }, false);
  650. previewing = true;
  651. previewStarted = juce::Time::getMillisecondCounter();
  652. engine.play();
  653. repaint (playButton);
  654. }
  655. void stopPreview (bool stopTransport = false)
  656. {
  657. if (! previewing)
  658. return;
  659. previewing = false;
  660. if (stopTransport)
  661. engine.stop();
  662. engine.editSong ([] (Song& s) { s.mode = PlayMode::song; }, false); // (the Simple workflow plays the song)
  663. repaint (playButton);
  664. }
  665. };