22#include "gui/views/automation/editor_layout.h"
23#include "hid/display/display.h"
24#include "model/model_stack.h"
25#include "model/note/note.h"
26#include "model/note/note_row.h"
30namespace velocity_edit {
33int32_t getVelocityFromY(int32_t y);
35bool velocityReachesY(int32_t velocity, int32_t y);
39void forNotesInRange(NoteRow* noteRow, int32_t startPos, int32_t endPos, F&& fn) {
40 int32_t noteI = noteRow->notes.search(startPos, GREATER_OR_EQUAL);
41 for (Note* note = noteRow->notes.getElement(noteI); note && note->pos < endPos;
42 note = noteRow->notes.getElement(++noteI)) {
52template <
typename ForEachRow>
53void setRelativeVelocityInRows(Action* action, int32_t startPos, int32_t endPos, int32_t targetVelocity,
54 ForEachRow&& forEachRow) {
55 int32_t maxVelocity = 0;
56 forEachRow([&](ModelStackWithNoteRow* modelStackWithNoteRow) {
57 forNotesInRange(modelStackWithNoteRow->getNoteRow(), startPos, endPos,
58 [&](Note* note) { maxVelocity = std::max<int32_t>(maxVelocity, note->getVelocity()); });
63 float scale =
static_cast<float>(targetVelocity) /
static_cast<float>(maxVelocity);
65 forEachRow([&](ModelStackWithNoteRow* modelStackWithNoteRow) {
66 NoteRow* noteRow = modelStackWithNoteRow->getNoteRow();
67 forNotesInRange(noteRow, startPos, endPos, [&](Note* note) {
68 int32_t newVelocity = std::lround(
static_cast<float>(note->getVelocity()) * scale);
70 CORRESPONDING_NOTES_SET_VELOCITY,
71 std::clamp<int32_t>(newVelocity, 1, 127));
77inline void setRelativeVelocity(Action* action, ModelStackWithNoteRow* modelStackWithNoteRow, int32_t startPos,
78 int32_t endPos, int32_t targetVelocity) {
79 setRelativeVelocityInRows(action, startPos, endPos, targetVelocity, [&](
auto&& fn) { fn(modelStackWithNoteRow); });
84class Ramp :
public AutomationEditorLayout {
86 void setup(int32_t firstX, int32_t firstVelocity, int32_t x, int32_t y) {
87 int32_t newVelocity = getVelocityFromY(y);
88 bool firstIsLeft = firstX < x;
89 leftX = firstIsLeft ? firstX : x;
90 rightX = firstIsLeft ? x : firstX;
91 leftVelocity = firstIsLeft ? firstVelocity : newVelocity;
92 rightVelocity = firstIsLeft ? newVelocity : firstVelocity;
97 template <
typename IsStep,
typename F>
99 int32_t steps[kDisplayWidth];
100 int32_t numSteps = 0;
101 for (int32_t x = leftX; x <= rightX; x++) {
103 steps[numSteps++] = x;
106 for (int32_t step = 0; step < numSteps; step++) {
107 int32_t velocity = leftVelocity;
109 float fraction =
static_cast<float>(step) /
static_cast<float>(numSteps - 1);
110 velocity +=
static_cast<int32_t
>(std::round((rightVelocity - leftVelocity) * fraction));
112 fn(steps[step], std::clamp<int32_t>(velocity, 1, 127));
118 if (display->haveOLED()) {
119 renderDisplay(leftVelocity, rightVelocity);
122 renderDisplay(leftX == x ? leftVelocity : rightVelocity);
129 int32_t leftVelocity{0};
130 int32_t rightVelocity{0};
Error changeNotesAcrossAllScreens(int32_t editPos, ModelStackWithNoteRow *modelStack, Action *action, int32_t changeType, int32_t changeValue)
Definition note_row.cpp:1779
Definition velocity_edit.h:84
void forEachStep(IsStep &&isStep, F &&fn) const
Definition velocity_edit.h:98
void renderValues(int32_t x)
show the velocity at both ends of the ramp, or on 7seg the end at the pad pressed last (x)
Definition velocity_edit.h:117