Deluge Firmware 1.3.0
Build date: 2026.02.12
Loading...
Searching...
No Matches
arpeggiator.h
1/*
2 * Copyright © 2017-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
20#include "algorithm"
21#include "definitions_cxx.hpp"
22#include "model/sync.h"
23#include "storage/storage_manager.h"
24#include "util/container/array/ordered_resizeable_array.h"
25#include "util/container/array/resizeable_array.h"
26
27#include <algorithm>
28#include <array>
29#include <cstddef>
30#include <cstdint>
31
32class PostArpTriggerable;
35
36constexpr uint32_t RANDOMIZER_LOCK_MAX_SAVED_VALUES = 16;
37constexpr uint32_t ARP_MAX_INSTRUCTION_NOTES = 4;
38constexpr uint32_t PATTERN_MAX_BUFFER_SIZE = 16;
39
40constexpr int16_t ARP_NOTE_NONE = 32767;
41
42enum class ArpType : uint8_t {
43 SYNTH,
44 DRUM,
45 KIT,
46};
47
48class ArpeggiatorSettings {
49public:
50 ArpeggiatorSettings();
51
52 void updateParamsFromUnpatchedParamSet(UnpatchedParamSet* unpatchedParams);
53
54 void cloneFrom(ArpeggiatorSettings const* other);
55
56 bool readCommonTagsFromFile(Deserializer& reader, char const* tagName, Song* songToConvertSyncLevel);
57
58 bool readNonAudioTagsFromFile(Deserializer& reader, char const* tagName);
59
60 void writeCommonParamsToFile(Serializer& writer, Song* songToConvertSyncLevel);
61
62 void writeNonAudioParamsToFile(Serializer& writer);
63
64 void generateNewNotePattern();
65
66 void updatePresetFromCurrentSettings();
67
68 void updateSettingsFromCurrentPreset();
69
70 uint32_t getPhaseIncrement(int32_t arpRate);
71
72 // Main settings
73 ArpPreset preset{ArpPreset::OFF};
74 ArpMode mode{ArpMode::OFF};
75
76 bool includeInKitArp{true};
77
78 // Sequence settings
79 ArpOctaveMode octaveMode{ArpOctaveMode::UP};
80 ArpNoteMode noteMode{ArpNoteMode::UP};
81
82 // Octave settings
83 uint8_t numOctaves{2};
84
85 // Number of repetitions of each step
86 uint8_t numStepRepeats{1};
87
88 // Chord type (only for kit arpeggiators)
89 uint8_t chordTypeIndex{0};
90
91 // Sync settings
92 SyncLevel syncLevel;
93 SyncType syncType;
94
95 // Arp randomizer lock
96 bool randomizerLock{false};
97
98 // MPE settings
99 ArpMpeModSource mpeVelocity{ArpMpeModSource::OFF};
100
101 // Spread last lock
102 uint32_t lastLockedNoteProbabilityParameterValue{0};
103 uint32_t lastLockedBassProbabilityParameterValue{0};
104 uint32_t lastLockedSwapProbabilityParameterValue{0};
105 uint32_t lastLockedGlideProbabilityParameterValue{0};
106 uint32_t lastLockedReverseProbabilityParameterValue{0};
107 uint32_t lastLockedChordProbabilityParameterValue{0};
108 uint32_t lastLockedRatchetProbabilityParameterValue{0};
109 uint32_t lastLockedSpreadVelocityParameterValue{0};
110 uint32_t lastLockedSpreadGateParameterValue{0};
111 uint32_t lastLockedSpreadOctaveParameterValue{0};
112
113 // Pre-calculated randomized values for each parameter
114 std::array<int8_t, RANDOMIZER_LOCK_MAX_SAVED_VALUES> lockedNoteProbabilityValues;
115 std::array<int8_t, RANDOMIZER_LOCK_MAX_SAVED_VALUES> lockedBassProbabilityValues;
116 std::array<int8_t, RANDOMIZER_LOCK_MAX_SAVED_VALUES> lockedSwapProbabilityValues;
117 std::array<int8_t, RANDOMIZER_LOCK_MAX_SAVED_VALUES> lockedGlideProbabilityValues;
118 std::array<int8_t, RANDOMIZER_LOCK_MAX_SAVED_VALUES> lockedReverseProbabilityValues;
119 std::array<int8_t, RANDOMIZER_LOCK_MAX_SAVED_VALUES> lockedChordProbabilityValues;
120 std::array<int8_t, RANDOMIZER_LOCK_MAX_SAVED_VALUES> lockedRatchetProbabilityValues;
121 std::array<int8_t, RANDOMIZER_LOCK_MAX_SAVED_VALUES> lockedSpreadVelocityValues;
122 std::array<int8_t, RANDOMIZER_LOCK_MAX_SAVED_VALUES> lockedSpreadGateValues;
123 std::array<int8_t, RANDOMIZER_LOCK_MAX_SAVED_VALUES> lockedSpreadOctaveValues;
124
125 // Order for the pattern note mode
126 std::array<int8_t, PATTERN_MAX_BUFFER_SIZE> notePattern;
127
128 // Temporary flags
129 bool flagForceArpRestart{false};
130
131 // Automatable params
132 int32_t rate{0}; // Default to 25 if not set in XML
133 int32_t gate{0}; // Default to 25 if not set in XML
134 uint32_t rhythm{0};
135 uint32_t sequenceLength{0};
136 uint32_t chordPolyphony{0};
137 uint32_t ratchetAmount{0};
138 uint32_t noteProbability{4294967295u}; // Default to 25 if not set in XML
139 uint32_t bassProbability{0};
140 uint32_t swapProbability{0};
141 uint32_t glideProbability{0};
142 uint32_t reverseProbability{0};
143 uint32_t chordProbability{0};
144 uint32_t ratchetProbability{0};
145 uint32_t spreadVelocity{0};
146 uint32_t spreadGate{0};
147 uint32_t spreadOctave{0};
148};
149enum class ArpNoteStatus : uint8_t {
150 OFF,
151 PENDING,
152 PLAYING,
153};
154
155struct ArpNote {
156 ArpNote() {
157 outputMemberChannel.fill(MIDI_CHANNEL_NONE);
158 noteCodeOnPostArp.fill(ARP_NOTE_NONE);
159 noteStatus.fill(ArpNoteStatus::OFF);
160 }
161 bool isPending() {
162 return std::ranges::any_of(noteStatus, [](ArpNoteStatus status) { return status == ArpNoteStatus::PENDING; });
163 }
164 void resetPostArpArrays() {
165 outputMemberChannel.fill(MIDI_CHANNEL_NONE);
166 noteCodeOnPostArp.fill(ARP_NOTE_NONE);
167 noteStatus.fill(ArpNoteStatus::OFF);
168 }
169 int16_t inputCharacteristics[2]{}; // Before arpeggiation. And applying to MIDI input if that's happening. Or,
170 // channel might be MIDI_CHANNEL_NONE.
171 int16_t mpeValues[kNumExpressionDimensions]{};
172 uint8_t velocity{0};
173 uint8_t baseVelocity{0};
174
175 // For note-ons
176 std::array<ArpNoteStatus, ARP_MAX_INSTRUCTION_NOTES> noteStatus{};
177 std::array<uint8_t, ARP_MAX_INSTRUCTION_NOTES> outputMemberChannel{};
178 std::array<int16_t, ARP_MAX_INSTRUCTION_NOTES> noteCodeOnPostArp{};
179};
180
182 int16_t noteCode; // Before arpeggiation
183};
184
185class ArpReturnInstruction {
186public:
187 ArpReturnInstruction() {
188 sampleSyncLengthOn = 0;
189 invertReversed = false;
190 arpNoteOn = nullptr;
191 outputMIDIChannelOff.fill(MIDI_CHANNEL_NONE);
192 noteCodeOffPostArp.fill(ARP_NOTE_NONE);
193 glideOutputMIDIChannelOff.fill(MIDI_CHANNEL_NONE);
194 glideNoteCodeOffPostArp.fill(ARP_NOTE_NONE);
195 }
196
197 // These are only valid if doing a note-on, or when releasing the most recently played with the arp off when other
198 // notes are still playing (e.g. for mono note priority)
199 uint32_t sampleSyncLengthOn; // This defaults to zero, or may be overwritten by the caller to the Arp - and then
200 // the Arp itself may override that.
201
202 // If set to true by the arpeggiator, the reverse SAMPLE flag will be inverted for the next voices to be played
203 // and restored back to normal afterwards
204 bool invertReversed;
205
206 ArpNote* arpNoteOn;
207
208 // And these are only valid if doing a note-off
209 std::array<uint8_t, ARP_MAX_INSTRUCTION_NOTES> outputMIDIChannelOff; // For MPE
210 std::array<int16_t, ARP_MAX_INSTRUCTION_NOTES> noteCodeOffPostArp;
211 std::array<uint8_t, ARP_MAX_INSTRUCTION_NOTES> glideOutputMIDIChannelOff; // For MPE
212 std::array<int16_t, ARP_MAX_INSTRUCTION_NOTES> glideNoteCodeOffPostArp;
213};
214
215class ArpeggiatorBase {
216public:
217 virtual ~ArpeggiatorBase() = default;
218 ArpeggiatorBase() {
219 glideNoteCodeCurrentlyOnPostArp.fill(ARP_NOTE_NONE);
220 outputMIDIChannelForGlideNoteCurrentlyOnPostArp.fill(0);
221 }
222
223 virtual void noteOn(ArpeggiatorSettings* settings, int32_t noteCode, int32_t velocity,
224 ArpReturnInstruction* instruction, int32_t fromMIDIChannel, int16_t const* mpeValues) = 0;
225 virtual void noteOff(ArpeggiatorSettings* settings, int32_t noteCodePreArp, ArpReturnInstruction* instruction) = 0;
228 virtual bool handlePendingNotes(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction);
229 void render(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction, int32_t numSamples,
230 uint32_t gateThreshold, uint32_t phaseIncrement);
231 int32_t doTickForward(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction, uint32_t ClipCurrentPos,
232 bool currentlyPlayingReversed);
233 void calculateRandomizerAmounts(ArpeggiatorSettings* settings);
234 virtual bool hasAnyInputNotesActive() = 0;
235 virtual void reset() = 0;
236 virtual ArpType getArpType() = 0;
237 ArpNote active_note; // For the currently active note.
238
239 std::array<int16_t, ARP_MAX_INSTRUCTION_NOTES> glideNoteCodeCurrentlyOnPostArp;
240 std::array<uint8_t, ARP_MAX_INSTRUCTION_NOTES> outputMIDIChannelForGlideNoteCurrentlyOnPostArp;
241 uint32_t gatePos = 0;
242 uint8_t lastVelocity = 0;
243
244protected:
245 void calculateNextNoteAndOrOctave(ArpeggiatorSettings* settings, uint8_t numActiveNotes);
246 void setInitialNoteAndOctave(ArpeggiatorSettings* settings, uint8_t numActiveNotes);
247 void resetBase();
248 void resetRatchet();
249 void executeArpStep(ArpeggiatorSettings* settings, uint8_t numActiveNotes, bool isRatchet,
250 uint32_t maxSequenceLength, uint32_t rhythm, bool* shouldCarryOnRhythmNote,
251 bool* shouldPlayNote, bool* shouldPlayBassNote, bool* shouldPlayRandomStep,
252 bool* shouldPlayReverseNote, bool* shouldPlayChordNote);
253 void increasePatternIndexes(uint8_t numStepRepeats);
254 void increaseSequenceIndexes(uint32_t maxSequenceLength, uint32_t rhythm);
255 void maybeSetupNewRatchet(ArpeggiatorSettings* settings);
256 bool evaluateRhythm(uint32_t rhythm, bool isRatchet);
257 bool evaluateNoteProbability(bool isRatchet);
258 bool evaluateBassProbability(bool isRatchet);
259 bool evaluateSwapProbability(bool isRatchet);
260 bool evaluateReverseProbability(bool isRatchet);
261 bool evaluateChordProbability(bool isRatchet);
262 uint32_t calculateSpreadVelocity(uint8_t velocity, int32_t spreadVelocityForCurrentStep);
263 int32_t getOctaveDirection(ArpeggiatorSettings* settings);
264 virtual void switchNoteOn(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction, bool isRatchet) = 0;
265 void switchAnyNoteOff(ArpReturnInstruction* instruction);
266 bool getRandomProbabilityResult(uint32_t value);
267 int8_t getRandomBipolarProbabilityAmount(uint32_t value);
268 int8_t getRandomWeighted2BitsAmount(uint32_t value);
269
270 bool gateCurrentlyActive = false;
271
272 bool playedFirstArpeggiatedNoteYet = false;
273
274 // Playing state
275 uint32_t notesPlayedFromSequence = 0;
276 uint32_t randomNotesPlayedFromOctave = 0;
277
278 // Sequence state
279 int16_t whichNoteCurrentlyOnPostArp; // As in, the index within our list
280 int8_t currentOctave = 0;
281 int8_t currentDirection = 1;
282 int8_t currentOctaveDirection = 1;
283
284 // Rhythm state
285 uint32_t notesPlayedFromRhythm = 0;
286 uint32_t lastNormalNotePlayedFromRhythm = 0;
287
288 // Locked randomizer state
289 uint32_t notesPlayedFromLockedRandomizer = 0;
290
291 // Note probability state
292 bool lastNormalNotePlayedFromNoteProbability = true;
293
294 // Bass probability state
295 bool lastNormalNotePlayedFromBassProbability = false;
296
297 // Swap probability state
298 bool lastNormalNotePlayedFromSwapProbability = false;
299
300 // Reverse probability state
301 bool lastNormalNotePlayedFromReverseProbability = false;
302
303 // Chord probability state
304 bool lastNormalNotePlayedFromChordProbability = false;
305
306 // Step repeat state
307 int32_t stepRepeatIndex = 0;
308
309 // Ratcheting state
310 uint32_t ratchetNotesIndex = 0;
311 uint32_t ratchetNotesMultiplier = 0;
312 uint32_t ratchetNotesCount = 0;
313 bool isRatcheting = false;
314
315 // Glide state
316 bool glideOnNextNoteOff = false;
317
318 // Calculated randomizer values
319 bool isPlayNoteForCurrentStep = true;
320 bool isPlayBassForCurrentStep = false;
321 bool isPlayRandomStepForCurrentStep = false;
322 bool isPlayReverseForCurrentStep = false;
323 bool isPlayChordForCurrentStep = false;
324 bool isPlayRatchetForCurrentStep = false;
325 bool isPlayGlideForCurrentStep = false;
326 int32_t spreadVelocityForCurrentStep = 0;
327 int32_t spreadGateForCurrentStep = 0;
328 int32_t spreadOctaveForCurrentStep = 0;
329 bool resetLockedRandomizerValuesNextTime = false;
330};
331
332class ArpeggiatorForDrum : public ArpeggiatorBase {
333public:
334 ~ArpeggiatorForDrum() override = default;
335 ArpeggiatorForDrum();
336 void noteOn(ArpeggiatorSettings* settings, int32_t noteCode, int32_t velocity, ArpReturnInstruction* instruction,
337 int32_t fromMIDIChannel, int16_t const* mpeValues) override;
338 void noteOff(ArpeggiatorSettings* settings, int32_t noteCodePreArp, ArpReturnInstruction* instruction) override;
339 void reset() override;
340 ArpType getArpType() override { return ArpType::DRUM; }
341 int16_t noteForDrum{0};
342
343 bool invertReversedFromKitArp{false};
344
345protected:
346 void switchNoteOn(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction, bool isRatchet) override;
347 bool hasAnyInputNotesActive() override;
348};
349
350class Arpeggiator : public ArpeggiatorBase {
351public:
352 virtual ~Arpeggiator() = default;
353 Arpeggiator();
354
355 void reset() override;
356 ArpType getArpType() override { return ArpType::SYNTH; }
357
358 void noteOn(ArpeggiatorSettings* settings, int32_t noteCode, int32_t velocity, ArpReturnInstruction* instruction,
359 int32_t fromMIDIChannel, int16_t const* mpeValues) override;
360 void noteOff(ArpeggiatorSettings* settings, int32_t noteCodePreArp, ArpReturnInstruction* instruction) override;
361 bool handlePendingNotes(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction) override;
362 bool hasAnyInputNotesActive() override;
363 // This array tracks the notes ordered by noteCode
365 // This array tracks the notes as they were played by the user
366 ResizeableArray notesAsPlayed;
367 // This array tracks the notes as ordered by the Pattern note mode
368 ResizeableArray notesByPattern;
369
370protected:
371 void rearrangePatterntArpNotes(ArpeggiatorSettings* settings);
372 void switchNoteOn(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction, bool isRatchet) override;
373 bool anyPending{false};
374};
375
376class ArpeggiatorForKit : public Arpeggiator {
377public:
378 ArpeggiatorForKit() = default;
379 ArpType getArpType() override { return ArpType::KIT; }
380 void removeDrumIndex(ArpeggiatorSettings* arpSettings, int32_t drumIndex);
381};
Definition arpeggiator.h:185
virtual bool handlePendingNotes(ArpeggiatorSettings *settings, ArpReturnInstruction *instruction)
Definition arpeggiator.cpp:1435
Definition arpeggiator.h:48
bool handlePendingNotes(ArpeggiatorSettings *settings, ArpReturnInstruction *instruction) override
Definition arpeggiator.cpp:476
Definition storage_manager.h:185
Definition ordered_resizeable_array.h:22
Definition param_manager.h:174
Definition resizeable_array.h:28
Definition storage_manager.h:119
Definition song.h:103
Definition param_set.h:98
Definition arpeggiator.h:181
Definition arpeggiator.h:155