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) {}
33 [[nodiscard]] std::string_view
getName()
const override {
return FormattedTitle::title(); }
34 [[nodiscard]] std::string_view
getTitle()
const override {
return FormattedTitle::title(); }
36 void renderMenuItems(std::span<MenuItem*> items,
const MenuItem* currentItem)
override {
38 if (items.size() < 4 || items[0] != getItems()[0]) {
39 HorizontalMenu::renderMenuItems(items, currentItem);
43 using modulation::EnvelopeMode;
44 const auto& config = soundEditor.currentSound->envelopeConfig[envelope_index_];
47 const int32_t attack =
static_cast<Segment&
>(*items[0]).getValue();
48 const int32_t decay =
static_cast<Segment&
>(*items[1]).getValue();
49 const int32_t sustain =
static_cast<Segment&
>(*items[2]).getValue();
50 const int32_t release =
static_cast<Segment&
>(*items[3]).getValue();
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;
63 auto level_y = [&](
float level) {
return static_cast<int32_t
>(round(base_y - level * draw_height)); };
66 const float attack_width = attack / 50.0f * max_segment_width;
67 const float decay_normalized = sigmoidLikeCurve(decay, 50.0f, 8.0f);
68 const float decay_width = decay_normalized * max_segment_width;
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);
75 const int32_t selected_pos = std::distance(items.begin(), std::ranges::find(items, currentItem));
76 selected_x_ = -1, selected_y_ = -1;
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);
83 switch (config.mode) {
84 case EnvelopeMode::AHD: {
85 const float hold_x = round(attack_x + decay_width);
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);
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);
101 case EnvelopeMode::AR: {
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);
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);
116 case EnvelopeMode::MPE: {
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);
125 for (int32_t x = start_x; x <= sustain_x; x += 3) {
126 image.drawPixel(x, sustain_y);
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);
137 const float decay_x = round(attack_x + decay_width);
138 constexpr float sustain_x = start_x + max_segment_width * 3;
140 const float release_x = round(sustain_x + release / 50.0f * (end_x - sustain_x));
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);
150 drawDottedLine(sustain_x,
true, sustain_y > start_y ? start_y : sustain_y);
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);
162 int32_t envelope_index_;
163 int32_t selected_x_, selected_y_;
165 static constexpr int32_t kGraphTopY = OLED_MAIN_TOPMOST_PIXEL + kTextTitleSizeY + 7;
166 static constexpr int32_t kGraphBaseY = OLED_MAIN_HEIGHT_PIXELS - 6;
171 auto y = [](
float level) {
172 return static_cast<int32_t
>(round(kGraphBaseY - level * (kGraphBaseY - kGraphTopY)));
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);
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);
197 void drawTransitionIndicator(
const float center_x,
const float center_y,
const bool is_selected) {
200 const int32_t ix =
static_cast<int32_t
>(center_x);
201 const int32_t iy =
static_cast<int32_t
>(center_y);
203 if (!is_selected && ix == selected_x_ && iy == selected_y_) {
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);
219 selected_x_ = ix, selected_y_ = iy;
220 image.invertArea(ix - innerSquareSize, square_size * 2 - 1, iy - innerSquareSize, iy + innerSquareSize);
224 image.drawRectangle(ix - square_size, iy - square_size, ix + square_size, iy + square_size);