Deluge Firmware 1.3.0
Build date: 2025.06.24
Loading...
Searching...
No Matches
direction.h
1/*
2 * Copyright (c) 2014-2025 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#pragma once
18#include "gui/menu_item/selection.h"
19#include "gui/ui/sound_editor.h"
20#include "model/drum/drum.h"
21#include "model/instrument/kit.h"
22#include "model/mod_controllable/mod_controllable_audio.h"
23#include "processing/sound/sound.h"
24#include "processing/sound/sound_drum.h"
25
26#include <hid/display/oled.h>
27
28namespace deluge::gui::menu_item::stutter {
29
30class StutterDirection final : public Selection {
31public:
32 using Selection::Selection;
33
34 enum Direction : uint8_t { USE_SONG_STUTTER = 0, FORWARD, REVERSED, FORWARD_PING_PONG, REVERSED_PING_PONG };
35
36 deluge::vector<std::string_view> getOptions(OptType optType = OptType::FULL) override {
37 using namespace deluge::l10n;
38
39 deluge::vector<std::string_view> result;
40
41 if (showUseSongOption()) {
42 result.push_back(l10n::getView(optType == OptType::SHORT ? String::STRING_FOR_USE_SONG_SHORT
43 : String::STRING_FOR_USE_SONG));
44 }
45 result.push_back(l10n::getView(String::STRING_FOR_FORWARD));
46 result.push_back(l10n::getView(String::STRING_FOR_REVERSED));
47 result.push_back(l10n::getView(String::STRING_FOR_FORWARD_PING_PONG));
48 result.push_back(l10n::getView(String::STRING_FOR_REVERSED_PING_PONG));
49
50 return result;
51 }
52
53 void readCurrentValue() override {
54 const auto* stutter = &soundEditor.currentModControllable->stutterConfig;
55
56 if (showUseSongOption() && stutter->useSongStutter) {
57 setValue(USE_SONG_STUTTER);
58 }
59 else if (stutter->reversed && stutter->pingPong) {
60 setValue(REVERSED_PING_PONG);
61 }
62 else if (stutter->reversed) {
63 setValue(REVERSED);
64 }
65 else if (stutter->pingPong) {
66 setValue(FORWARD_PING_PONG);
67 }
68 else {
69 setValue(FORWARD);
70 }
71 }
72
73 bool usesAffectEntire() override { return true; }
74
75 void writeCurrentValue() override {
76 Direction value = getValue();
77
78 // If affect-entire button held, do whole kit
79 if (currentUIMode == UI_MODE_HOLDING_AFFECT_ENTIRE_IN_SOUND_EDITOR && soundEditor.editingKitRow()) {
80 Kit* kit = getCurrentKit();
81 for (Drum* thisDrum = kit->firstDrum; thisDrum != nullptr; thisDrum = thisDrum->next) {
82 if (thisDrum->type == DrumType::SOUND) {
83 auto* soundDrum = static_cast<SoundDrum*>(thisDrum);
84 applyOptionToStutterConfig(value, soundDrum->stutterConfig);
85 }
86 }
87 }
88 // Or, the normal case of just one sound
89 else {
90 applyOptionToStutterConfig(value, soundEditor.currentModControllable->stutterConfig);
91 }
92 }
93
94private:
95 Direction getValue() {
96 const auto value = Selection::getValue();
97 const auto shift = showUseSongOption() ? 0 : 1;
98 return static_cast<Direction>(value + shift);
99 }
100
101 void setValue(Direction value) {
102 const auto shift = showUseSongOption() ? 0 : 1;
103 return Selection::setValue(value - shift);
104 }
105
106 static bool showUseSongOption() { return !soundEditor.currentModControllable->isSong(); }
107
108 static void applyOptionToStutterConfig(const Direction value, StutterConfig& stutter) {
109 stutter.useSongStutter = value == USE_SONG_STUTTER;
110 stutter.reversed = value == REVERSED || value == REVERSED_PING_PONG;
111 stutter.pingPong = value == FORWARD_PING_PONG || value == REVERSED_PING_PONG;
112
113 if (stutter.useSongStutter) {
114 stutter.quantized = currentSong->globalEffectable.stutterConfig.quantized;
115 stutter.reversed = currentSong->globalEffectable.stutterConfig.reversed;
116 stutter.pingPong = currentSong->globalEffectable.stutterConfig.pingPong;
117 }
118 }
119
120 void getValueForPopup(StringBuf& valueBuf) override {
121 const auto value = Selection::getValue();
122 valueBuf.append(getOptions(OptType::SHORT)[value]);
123 }
124
125 void renderInHorizontalMenu(int32_t startX, int32_t width, int32_t startY, int32_t height) override {
126 using namespace deluge::hid::display;
127 oled_canvas::Canvas& image = OLED::main;
128
129 const auto value = getValue();
130
131 if (value == USE_SONG_STUTTER) {
132 const auto& icon = OLED::songIcon;
133 constexpr int32_t songIconWidth = 9;
134
135 // Draw a song icon centered
136 const int32_t x = startX + ((width - songIconWidth) / 2) - 2;
137 const int32_t y = startY + ((height - songIconWidth) / 2);
138 return image.drawGraphicMultiLine(icon, x, y, songIconWidth);
139 }
140
141 constexpr int32_t numBytesTall = 2;
142 const bool reversed = value == REVERSED || value == REVERSED_PING_PONG;
143 const auto icon =
144 reversed ? reverseBitmap(OLED::stutterDirectionIcon, numBytesTall) : OLED::stutterDirectionIcon;
145
146 constexpr int32_t iconHeight = numBytesTall * 8;
147 const int32_t iconWidth = icon.size() / numBytesTall;
148
149 if (value == FORWARD_PING_PONG || value == REVERSED_PING_PONG) {
150 // Draw the "P" indicator and the icon centered
151 constexpr int32_t iconOffset = 4;
152 image.drawChar('P', startX + 3, startY + 3, 5, kTextSpacingY);
153 image.drawGraphicMultiLine(reversed ? icon.data() : icon.data() + iconOffset * numBytesTall, startX + 11,
154 startY, iconWidth - iconOffset, iconHeight, numBytesTall);
155 }
156 else {
157 // Draw the icon centered
158 const int32_t x = startX + (width - iconWidth) / 2 - 1;
159 image.drawGraphicMultiLine(icon.data(), x, startY, iconWidth, iconHeight, numBytesTall);
160 }
161 }
162
163 static std::vector<uint8_t> reverseBitmap(const std::vector<uint8_t>& bitmap, int32_t numBytesTall) {
164 const int32_t columnCount = bitmap.size() / numBytesTall;
165
166 std::vector<uint8_t> output(bitmap.size());
167
168 for (size_t col = 0; col < columnCount; ++col) {
169 const int32_t inputIndex = col * numBytesTall;
170 const int32_t reversedCol = columnCount - 1 - col;
171 const int32_t outputIndex = reversedCol * numBytesTall;
172
173 for (int32_t byte = 0; byte < numBytesTall; ++byte) {
174 uint8_t b = bitmap[inputIndex + byte];
175 const uint8_t reversed = std::bit_cast<uint8_t>(b);
176 output[outputIndex + byte] = reversed;
177 }
178 }
179
180 return output;
181 }
182};
183
184} // namespace deluge::gui::menu_item::stutter
Definition drum.h:44
Definition kit.h:34
Definition sound_drum.h:28
Definition d_stringbuf.h:16
Definition selection.h:26
void readCurrentValue() override
Like readValueAgain, but does not redraw.
Definition direction.h:53
bool usesAffectEntire() override
Claim support for Kit AFFECT_ENTIRE editing.
Definition direction.h:73
void getValueForPopup(StringBuf &valueBuf) override
Get the parameter value string to show in the popup.
Definition direction.h:120