Deluge Firmware 1.3.0
Build date: 2026.08.16
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 }
167 return noteStatus[0] == ArpNoteStatus::PENDING && noteCodeOnPostArp[0] != ARP_NOTE_NONE;
168 }
169 void resetPostArpArrays() {
170 outputMemberChannel.fill(MIDI_CHANNEL_NONE);
171 noteCodeOnPostArp.fill(ARP_NOTE_NONE);
172 noteStatus.fill(ArpNoteStatus::OFF);
173 }
174 int16_t inputCharacteristics[2]{}; // Before arpeggiation. And applying to MIDI input if that's happening. Or,
175 // channel might be MIDI_CHANNEL_NONE.
176 int16_t mpeValues[kNumExpressionDimensions]{};
177 uint8_t velocity{0};
178 uint8_t baseVelocity{0};
179
180 // For note-ons
181 std::array<ArpNoteStatus, ARP_MAX_INSTRUCTION_NOTES> noteStatus{};
182 std::array<uint8_t, ARP_MAX_INSTRUCTION_NOTES> outputMemberChannel{};
183 std::array<int16_t, ARP_MAX_INSTRUCTION_NOTES> noteCodeOnPostArp{};
184};
185
187 int16_t noteCode; // Before arpeggiation
188};
189
190class ArpReturnInstruction {
191public:
192 ArpReturnInstruction() {
193 sampleSyncLengthOn = 0;
194 invertReversed = false;
195 arpNoteOn = nullptr;
196 outputMIDIChannelOff.fill(MIDI_CHANNEL_NONE);
197 noteCodeOffPostArp.fill(ARP_NOTE_NONE);
198 glideOutputMIDIChannelOff.fill(MIDI_CHANNEL_NONE);
199 glideNoteCodeOffPostArp.fill(ARP_NOTE_NONE);
200 }
201
202 // These are only valid if doing a note-on, or when releasing the most recently played with the arp off when other
203 // notes are still playing (e.g. for mono note priority)
204 uint32_t sampleSyncLengthOn; // This defaults to zero, or may be overwritten by the caller to the Arp - and then
205 // the Arp itself may override that.
206
207 // If set to true by the arpeggiator, the reverse SAMPLE flag will be inverted for the next voices to be played
208 // and restored back to normal afterwards
209 bool invertReversed;
210
211 ArpNote* arpNoteOn;
212
213 // And these are only valid if doing a note-off
214 std::array<uint8_t, ARP_MAX_INSTRUCTION_NOTES> outputMIDIChannelOff; // For MPE
215 std::array<int16_t, ARP_MAX_INSTRUCTION_NOTES> noteCodeOffPostArp;
216 std::array<uint8_t, ARP_MAX_INSTRUCTION_NOTES> glideOutputMIDIChannelOff; // For MPE
217 std::array<int16_t, ARP_MAX_INSTRUCTION_NOTES> glideNoteCodeOffPostArp;
218};
219
220class ArpeggiatorBase {
221public:
222 virtual ~ArpeggiatorBase() = default;
223 ArpeggiatorBase() {
224 glideNoteCodeCurrentlyOnPostArp.fill(ARP_NOTE_NONE);
225 outputMIDIChannelForGlideNoteCurrentlyOnPostArp.fill(0);
226 }
227
228 virtual void noteOn(ArpeggiatorSettings* settings, int32_t noteCode, int32_t velocity,
229 ArpReturnInstruction* instruction, int32_t fromMIDIChannel, int16_t const* mpeValues) = 0;
230 virtual void noteOff(ArpeggiatorSettings* settings, int32_t noteCodePreArp, ArpReturnInstruction* instruction) = 0;
233 virtual bool handlePendingNotes(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction);
236 virtual bool hasPendingNotes(ArpeggiatorSettings* settings);
237 void render(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction, int32_t numSamples,
238 uint32_t gateThreshold, uint32_t phaseIncrement);
239 int32_t doTickForward(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction, uint32_t ClipCurrentPos,
240 bool currentlyPlayingReversed);
241 void calculateRandomizerAmounts(ArpeggiatorSettings* settings);
242 virtual bool hasAnyInputNotesActive() = 0;
243 virtual void reset() = 0;
244 virtual ArpType getArpType() = 0;
247 static bool arpIsOff(ArpeggiatorSettings* settings) {
248 return (settings == nullptr) || settings->mode == ArpMode::OFF;
249 }
250 ArpNote active_note; // For the currently active note.
251
252 std::array<int16_t, ARP_MAX_INSTRUCTION_NOTES> glideNoteCodeCurrentlyOnPostArp;
253 std::array<uint8_t, ARP_MAX_INSTRUCTION_NOTES> outputMIDIChannelForGlideNoteCurrentlyOnPostArp;
254 uint32_t gatePos = 0;
255 uint8_t lastVelocity = 0;
256
257protected:
258 void calculateNextNoteAndOrOctave(ArpeggiatorSettings* settings, uint8_t numActiveNotes);
259 void setInitialNoteAndOctave(ArpeggiatorSettings* settings, uint8_t numActiveNotes);
260 void resetBase();
261 void resetRatchet();
262 void executeArpStep(ArpeggiatorSettings* settings, uint8_t numActiveNotes, bool isRatchet,
263 uint32_t maxSequenceLength, uint32_t rhythm, bool* shouldCarryOnRhythmNote,
264 bool* shouldPlayNote, bool* shouldPlayBassNote, bool* shouldPlayRandomStep,
265 bool* shouldPlayReverseNote, bool* shouldPlayChordNote);
266 void increasePatternIndexes(uint8_t numStepRepeats);
267 void increaseSequenceIndexes(uint32_t maxSequenceLength, uint32_t rhythm);
268 void maybeSetupNewRatchet(ArpeggiatorSettings* settings);
269 bool evaluateRhythm(uint32_t rhythm, bool isRatchet);
270 bool evaluateNoteProbability(bool isRatchet);
271 bool evaluateBassProbability(bool isRatchet);
272 bool evaluateSwapProbability(bool isRatchet);
273 bool evaluateReverseProbability(bool isRatchet);
274 bool evaluateChordProbability(bool isRatchet);
275 uint32_t calculateSpreadVelocity(uint8_t velocity, int32_t spreadVelocityForCurrentStep);
276 int32_t getOctaveDirection(ArpeggiatorSettings* settings);
277 virtual void switchNoteOn(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction, bool isRatchet) = 0;
278 void switchAnyNoteOff(ArpReturnInstruction* instruction);
279 bool getRandomProbabilityResult(uint32_t value);
280 int8_t getRandomBipolarProbabilityAmount(uint32_t value);
281 int8_t getRandomWeighted2BitsAmount(uint32_t value);
282
283 bool gateCurrentlyActive = false;
284
285 bool playedFirstArpeggiatedNoteYet = false;
286
287 // Playing state
288 uint32_t notesPlayedFromSequence = 0;
289 uint32_t randomNotesPlayedFromOctave = 0;
290
291 // Sequence state
292 int16_t whichNoteCurrentlyOnPostArp; // As in, the index within our list
293 int8_t currentOctave = 0;
294 int8_t currentDirection = 1;
295 int8_t currentOctaveDirection = 1;
296
297 // Rhythm state
298 uint32_t notesPlayedFromRhythm = 0;
299 uint32_t lastNormalNotePlayedFromRhythm = 0;
300
301 // Locked randomizer state
302 uint32_t notesPlayedFromLockedRandomizer = 0;
303
304 // Note probability state
305 bool lastNormalNotePlayedFromNoteProbability = true;
306
307 // Bass probability state
308 bool lastNormalNotePlayedFromBassProbability = false;
309
310 // Swap probability state
311 bool lastNormalNotePlayedFromSwapProbability = false;
312
313 // Reverse probability state
314 bool lastNormalNotePlayedFromReverseProbability = false;
315
316 // Chord probability state
317 bool lastNormalNotePlayedFromChordProbability = false;
318
319 // Step repeat state
320 int32_t stepRepeatIndex = 0;
321
322 // Ratcheting state
323 uint32_t ratchetNotesIndex = 0;
324 uint32_t ratchetNotesMultiplier = 0;
325 uint32_t ratchetNotesCount = 0;
326 bool isRatcheting = false;
327
328 // Glide state
329 bool glideOnNextNoteOff = false;
330
331 // Calculated randomizer values
332 bool isPlayNoteForCurrentStep = true;
333 bool isPlayBassForCurrentStep = false;
334 bool isPlayRandomStepForCurrentStep = false;
335 bool isPlayReverseForCurrentStep = false;
336 bool isPlayChordForCurrentStep = false;
337 bool isPlayRatchetForCurrentStep = false;
338 bool isPlayGlideForCurrentStep = false;
339 int32_t spreadVelocityForCurrentStep = 0;
340 int32_t spreadGateForCurrentStep = 0;
341 int32_t spreadOctaveForCurrentStep = 0;
342 bool resetLockedRandomizerValuesNextTime = false;
343};
344
345class ArpeggiatorForDrum : public ArpeggiatorBase {
346public:
347 ~ArpeggiatorForDrum() override = default;
348 ArpeggiatorForDrum();
349 void noteOn(ArpeggiatorSettings* settings, int32_t noteCode, int32_t velocity, ArpReturnInstruction* instruction,
350 int32_t fromMIDIChannel, int16_t const* mpeValues) override;
351 void noteOff(ArpeggiatorSettings* settings, int32_t noteCodePreArp, ArpReturnInstruction* instruction) override;
352 void reset() override;
353 ArpType getArpType() override { return ArpType::DRUM; }
354 int16_t noteForDrum{0};
355
356 bool invertReversedFromKitArp{false};
357
358protected:
359 void switchNoteOn(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction, bool isRatchet) override;
360 bool hasAnyInputNotesActive() override;
361};
362
363class Arpeggiator : public ArpeggiatorBase {
364public:
365 virtual ~Arpeggiator() = default;
366 Arpeggiator();
367
368 void reset() override;
369 ArpType getArpType() override { return ArpType::SYNTH; }
370
371 void noteOn(ArpeggiatorSettings* settings, int32_t noteCode, int32_t velocity, ArpReturnInstruction* instruction,
372 int32_t fromMIDIChannel, int16_t const* mpeValues) override;
373 void noteOff(ArpeggiatorSettings* settings, int32_t noteCodePreArp, ArpReturnInstruction* instruction) override;
374 bool handlePendingNotes(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction) override;
375 bool hasPendingNotes(ArpeggiatorSettings* settings) override;
376 bool hasAnyInputNotesActive() override;
377 // This array tracks the notes ordered by noteCode
379 // This array tracks the notes as they were played by the user
380 ResizeableArray notesAsPlayed;
381 // This array tracks the notes as ordered by the Pattern note mode
382 ResizeableArray notesByPattern;
383
384protected:
385 void rearrangePatterntArpNotes(ArpeggiatorSettings* settings);
386 void switchNoteOn(ArpeggiatorSettings* settings, ArpReturnInstruction* instruction, bool isRatchet) override;
387 bool anyPending{false};
388};
389
390class ArpeggiatorForKit : public Arpeggiator {
391public:
392 ArpeggiatorForKit() = default;
393 ArpType getArpType() override { return ArpType::KIT; }
394 void removeDrumIndex(ArpeggiatorSettings* arpSettings, int32_t drumIndex);
395};
Definition arpeggiator.h:190
static bool arpIsOff(ArpeggiatorSettings *settings)
Definition arpeggiator.h:247
virtual bool handlePendingNotes(ArpeggiatorSettings *settings, ArpReturnInstruction *instruction)
Definition arpeggiator.cpp:1465
virtual bool hasPendingNotes(ArpeggiatorSettings *settings)
Definition arpeggiator.cpp:1474
Definition arpeggiator.h:48
bool hasPendingNotes(ArpeggiatorSettings *settings) override
Definition arpeggiator.cpp:516
bool handlePendingNotes(ArpeggiatorSettings *settings, ArpReturnInstruction *instruction) override
Definition arpeggiator.cpp:486
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:186
Definition arpeggiator.h:155
bool isStartablePending()
Definition arpeggiator.h:166