Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
envelope_menu.h
1/*
2 * Copyright (c) 2014-2023 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#include "gui/menu_item/horizontal_menu.h"
20#include "hid/display/oled.h"
21#include "segment.h"
22
23using namespace deluge::hid::display;
24
25namespace deluge::gui::menu_item::envelope {
26class EnvelopeMenu final : public HorizontalMenu, FormattedTitle {
27public:
29 EnvelopeMenu(l10n::String newName, std::span<MenuItem*> newItems, int32_t envelopeIndex)
30 : HorizontalMenu(newName, newItems, FIXED), FormattedTitle(newName, envelopeIndex + 1),
31 envelope_index_(envelopeIndex) {}
32
33 [[nodiscard]] std::string_view getName() const override { return FormattedTitle::title(); }
34 [[nodiscard]] std::string_view getTitle() const override { return FormattedTitle::title(); }
35
36 void renderMenuItems(std::span<MenuItem*> items, const MenuItem* currentItem) override {
37 // Only the page of segments gets the graph
38 if (items.size() < 4 || items[0] != getItems()[0]) {
39 HorizontalMenu::renderMenuItems(items, currentItem);
40 return;
41 }
42
43 using modulation::EnvelopeMode;
44 const auto& config = soundEditor.currentSound->envelopeConfig[envelope_index_];
45
46 // Get the values in 0-50 range
47 const int32_t attack = static_cast<Segment&>(*items[0]).getValue();
48 const int32_t decay = static_cast<Segment&>(*items[1]).getValue(); // hold in AHD
49 const int32_t sustain = static_cast<Segment&>(*items[2]).getValue();
50 const int32_t release = static_cast<Segment&>(*items[3]).getValue(); // decay in AHD
51
52 // Constants
53 constexpr int32_t padding_x = 4;
54 constexpr int32_t start_x = padding_x;
55 constexpr uint8_t start_y = OLED_MAIN_TOPMOST_PIXEL + kTextTitleSizeY + 7;
56 constexpr uint8_t end_y = OLED_MAIN_HEIGHT_PIXELS - 6;
57 constexpr int32_t draw_width = OLED_MAIN_WIDTH_PIXELS - 2 * padding_x;
58 constexpr uint8_t draw_height = end_y - start_y;
59 constexpr float max_segment_width = draw_width / 4;
60 constexpr int32_t base_y = start_y + draw_height;
61 constexpr float end_x = start_x + draw_width;
62
63 auto level_y = [&](float level) { return static_cast<int32_t>(round(base_y - level * draw_height)); };
64
65 // Widths
66 const float attack_width = attack / 50.0f * max_segment_width;
67 const float decay_normalized = sigmoidLikeCurve(decay, 50.0f, 8.0f); // Maps 0-50 to 0-1 range with steep start
68 const float decay_width = decay_normalized * max_segment_width;
69
70 const float attack_x = round(start_x + attack_width);
71 const float sustain_level = (config.mode == EnvelopeMode::AHD) ? 0.f : sustain / 50.0f;
72 const int32_t sustain_y = level_y(sustain_level);
73
74 oled_canvas::Canvas& image = OLED::main;
75 const int32_t selected_pos = std::distance(items.begin(), std::ranges::find(items, currentItem));
76 selected_x_ = -1, selected_y_ = -1;
77
78 // AR rises to its level; every other mode to full
79 const float attack_level = (config.mode == EnvelopeMode::AR) ? sustain_level : 1.f;
80 const int32_t attack_y = level_y(attack_level);
81 drawSegment(start_x, 0.f, attack_x, attack_level, config.shape.attack);
82
83 switch (config.mode) {
84 case EnvelopeMode::AHD: {
85 const float hold_x = round(attack_x + decay_width);
86 // The decay gets the room the release would have had, so the line ends where the release knob says
87 const float decay_end_x = round(hold_x + release / 50.0f * (end_x - hold_x));
88 image.drawLine(attack_x, start_y, hold_x, start_y);
89 drawSegment(hold_x, 1.f, decay_end_x, 0.f, config.shape.decay);
90 image.drawLine(decay_end_x, base_y, end_x, base_y);
91
92 drawDottedLine(attack_x, attack_x > start_x + 3, start_y);
93 drawDottedLine(hold_x, hold_x - attack_x > 4, start_y);
94
95 drawTransitionIndicator(attack_x, start_y, selected_pos == 0);
96 drawTransitionIndicator(hold_x, start_y, selected_pos == 1);
97 drawTransitionIndicator(decay_end_x, base_y, selected_pos == 3);
98 break;
99 }
100
101 case EnvelopeMode::AR: {
102 // Up to the level while held, down on release
103 constexpr float sustain_x = start_x + max_segment_width * 3;
104 const float release_x = round(sustain_x + release / 50.0f * (end_x - sustain_x));
105 image.drawLine(attack_x, sustain_y, sustain_x, sustain_y);
106 drawSegment(sustain_x, sustain_level, release_x, 0.f, config.shape.release);
107 image.drawLine(release_x, base_y, end_x, base_y);
108 drawDottedLine(sustain_x, true, sustain_y);
109
110 drawTransitionIndicator(attack_x, attack_y, selected_pos == 0);
111 drawTransitionIndicator(attack_x + (sustain_x - attack_x) / 2, sustain_y, selected_pos == 2);
112 drawTransitionIndicator(release_x, base_y, selected_pos == 3);
113 break;
114 }
115
116 case EnvelopeMode::MPE: {
117 // Up to full with the input fully on, resting at the level with it off, down on release
118 constexpr float sustain_x = start_x + max_segment_width * 3;
119 const float release_x = round(sustain_x + release / 50.0f * (end_x - sustain_x));
120 image.drawLine(attack_x, start_y, sustain_x, start_y);
121 drawSegment(sustain_x, 1.f, release_x, 0.f, config.shape.release);
122 image.drawLine(release_x, base_y, end_x, base_y);
123
124 // The level the envelope rests at with no input
125 for (int32_t x = start_x; x <= sustain_x; x += 3) {
126 image.drawPixel(x, sustain_y);
127 }
128 drawDottedLine(sustain_x, true, start_y);
129
130 drawTransitionIndicator(attack_x, start_y, selected_pos == 0);
131 drawTransitionIndicator(start_x + (sustain_x - start_x) / 2, sustain_y, selected_pos == 2);
132 drawTransitionIndicator(release_x, base_y, selected_pos == 3);
133 break;
134 }
135
136 default: {
137 const float decay_x = round(attack_x + decay_width);
138 constexpr float sustain_x = start_x + max_segment_width * 3; // Fixed sustainX position
139 // Make release x dynamic, right of sustain
140 const float release_x = round(sustain_x + release / 50.0f * (end_x - sustain_x));
141
142 drawSegment(attack_x, 1.f, decay_x, sustain_level, config.shape.decay);
143 image.drawLine(decay_x, sustain_y, sustain_x, sustain_y);
144 drawSegment(sustain_x, sustain_level, release_x, 0.f, config.shape.release);
145 image.drawLine(release_x, base_y, end_x, base_y);
146
147 // reduce a messy look when lines are close to each other by omitting the line
148 drawDottedLine(attack_x, attack_x > start_x + 3, start_y);
149 drawDottedLine(decay_x, decay_x - attack_x > 4, start_y);
150 drawDottedLine(sustain_x, true, sustain_y > start_y ? start_y : sustain_y);
151
152 drawTransitionIndicator(attack_x, start_y, selected_pos == 0);
153 drawTransitionIndicator(decay_x, sustain_y, selected_pos == 1);
154 drawTransitionIndicator(decay_x + (sustain_x - decay_x) / 2, sustain_y, selected_pos == 2);
155 drawTransitionIndicator(release_x, base_y, selected_pos == 3);
156 break;
157 }
158 }
159 }
160
161private:
162 int32_t envelope_index_;
163 int32_t selected_x_, selected_y_;
164
165 static constexpr int32_t kGraphTopY = OLED_MAIN_TOPMOST_PIXEL + kTextTitleSizeY + 7;
166 static constexpr int32_t kGraphBaseY = OLED_MAIN_HEIGHT_PIXELS - 6;
167
169 static void drawSegment(float x0, float from, float x1, float to, const modulation::EnvelopeCurve& curve) {
170 oled_canvas::Canvas& image = OLED::main;
171 auto y = [](float level) {
172 return static_cast<int32_t>(round(kGraphBaseY - level * (kGraphBaseY - kGraphTopY)));
173 };
174 constexpr int32_t kSteps = 12;
175 int32_t last_x = static_cast<int32_t>(x0);
176 int32_t last_y = y(from);
177 for (int32_t i = 1; i <= kSteps; i++) {
178 float t = static_cast<float>(i) / kSteps;
179 int32_t this_x = static_cast<int32_t>(round(x0 + (x1 - x0) * t));
180 int32_t this_y = y(from + (to - from) * curve.rising(t));
181 image.drawLine(last_x, last_y, this_x, this_y);
182 last_x = this_x;
183 last_y = this_y;
184 }
185 }
186
188 static void drawDottedLine(float x, bool draw, int32_t top_y) {
189 if (!draw) {
190 return;
191 }
192 for (int32_t y = OLED_MAIN_VISIBLE_HEIGHT + 1; y >= top_y - 2; y -= 4) {
193 OLED::main.drawPixel(static_cast<int32_t>(x), y);
194 }
195 }
196
197 void drawTransitionIndicator(const float center_x, const float center_y, const bool is_selected) {
198 oled_canvas::Canvas& image = OLED::main;
199
200 const int32_t ix = static_cast<int32_t>(center_x);
201 const int32_t iy = static_cast<int32_t>(center_y);
202
203 if (!is_selected && ix == selected_x_ && iy == selected_y_) {
204 // Overlap occurred, skip drawing
205 return;
206 }
207
208 // Clear region inside
209 constexpr int32_t square_size = 2;
210 constexpr int32_t innerSquareSize = square_size - 1;
211 for (int32_t x = ix - innerSquareSize; x <= ix + innerSquareSize; x++) {
212 for (int32_t y = iy - innerSquareSize; y <= iy + innerSquareSize; y++) {
213 image.clearPixel(x, y);
214 }
215 }
216
217 if (is_selected) {
218 // Invert region inside to highlight selection
219 selected_x_ = ix, selected_y_ = iy;
220 image.invertArea(ix - innerSquareSize, square_size * 2 - 1, iy - innerSquareSize, iy + innerSquareSize);
221 }
222
223 // Draw a transition square
224 image.drawRectangle(ix - square_size, iy - square_size, ix + square_size, iy + square_size);
225 }
226};
227
228} // namespace deluge::gui::menu_item::envelope
Base class for all menu items.
Definition menu_item.h:52
EnvelopeMenu(l10n::String newName, std::span< MenuItem * > newItems, int32_t envelopeIndex)
Items: attack, decay, sustain, release segments, then any settings (mode, curve) on the next page.
Definition envelope_menu.h:29
static void drawDottedLine(float x, bool draw, int32_t top_y)
Vertical dotted line marking a stage transition, from the bottom of the screen up to top_y.
Definition envelope_menu.h:188
std::string_view getTitle() const override
Get the title to be used when rendering on OLED, both as a deluge::gui::menu_item::Submenu and when d...
Definition envelope_menu.h:34
std::string_view getName() const override
Get the actual name for use on OLED for deluge::gui::menu_item::Submenu s.
Definition envelope_menu.h:33
static void drawSegment(float x0, float from, float x1, float to, const modulation::EnvelopeCurve &curve)
A stage from level from at x0 to level to at x1, bent like the envelope's curve.
Definition envelope_menu.h:169
Definition envelope_curve.h:53
constexpr float rising(float x) const
The rising mirror image: 0 at x = 0, 1 at x = 1. With k > 0 it's fast at the start,...
Definition envelope_curve.h:68