Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
velocity_edit.h
1/*
2 * Copyright © 2026 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20// helpers shared by the velocity editor (velocity.cpp) and the folded velocity editor (velocity_fold.cpp)
21
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"
27
28class Action;
29
30namespace velocity_edit {
31
33int32_t getVelocityFromY(int32_t y);
35bool velocityReachesY(int32_t velocity, int32_t y);
36
38template <typename F>
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)) {
43 fn(note);
44 }
45}
46
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()); });
59 });
60 if (!maxVelocity) {
61 return;
62 }
63 float scale = static_cast<float>(targetVelocity) / static_cast<float>(maxVelocity);
64
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);
69 noteRow->changeNotesAcrossAllScreens(note->pos, modelStackWithNoteRow, action,
70 CORRESPONDING_NOTES_SET_VELOCITY,
71 std::clamp<int32_t>(newVelocity, 1, 127));
72 });
73 });
74}
75
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); });
80}
81
84class Ramp : public AutomationEditorLayout {
85public:
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;
93 }
94
97 template <typename IsStep, typename F>
98 void forEachStep(IsStep&& isStep, F&& fn) const {
99 int32_t steps[kDisplayWidth];
100 int32_t numSteps = 0;
101 for (int32_t x = leftX; x <= rightX; x++) {
102 if (isStep(x)) {
103 steps[numSteps++] = x;
104 }
105 }
106 for (int32_t step = 0; step < numSteps; step++) {
107 int32_t velocity = leftVelocity;
108 if (numSteps > 1) {
109 float fraction = static_cast<float>(step) / static_cast<float>(numSteps - 1);
110 velocity += static_cast<int32_t>(std::round((rightVelocity - leftVelocity) * fraction));
111 }
112 fn(steps[step], std::clamp<int32_t>(velocity, 1, 127));
113 }
114 }
115
117 void renderValues(int32_t x) {
118 if (display->haveOLED()) {
119 renderDisplay(leftVelocity, rightVelocity);
120 }
121 else {
122 renderDisplay(leftX == x ? leftVelocity : rightVelocity);
123 }
124 }
125
126private:
127 int32_t leftX{0};
128 int32_t rightX{0};
129 int32_t leftVelocity{0};
130 int32_t rightVelocity{0};
131};
132
133} // namespace velocity_edit
Definition action.h:78
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