Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
note_locks.h
1/*
2 * Copyright © 2026 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 "memory/sdram_allocator.h"
21#include <array>
22#include <cstdint>
23#include <optional>
24#include <span>
25#include <vector>
26
43
44namespace deluge::note_locks {
45
47constexpr int32_t kMaxLocksPerNote = 8;
48
51bool isLockableParam(int32_t param);
52
54template <typename... Params>
55constexpr uint64_t paramMask(Params... params) {
56 return ((uint64_t{1} << static_cast<uint32_t>(params)) | ...);
57}
58
63class LockSet {
64public:
65 [[nodiscard]] int32_t size() const { return count_; }
66 [[nodiscard]] bool empty() const { return mask_ == 0; }
67 [[nodiscard]] bool full() const { return count_ == kMaxLocksPerNote; }
68 [[nodiscard]] std::span<const uint8_t> params() const { return {params_.data(), count_}; }
69 [[nodiscard]] std::span<const int32_t> values() const { return {values_.data(), count_}; }
70
72 [[nodiscard]] uint64_t mask() const { return mask_; }
73 [[nodiscard]] bool has(int32_t param) const { return ((mask_ >> param) & 1) != 0; }
74 [[nodiscard]] bool hasAnyOf(uint64_t paramMask) const { return (mask_ & paramMask) != 0; }
75
77 [[nodiscard]] std::optional<int32_t> get(int32_t param) const {
78 if (!has(param)) [[likely]] {
79 return std::nullopt;
80 }
81 int32_t i = 0;
82 while (params_[i] != param) {
83 i++;
84 }
85 return values_[i];
86 }
87
90 bool set(int32_t param, int32_t value);
92 bool remove(int32_t param);
94 bool merge(const LockSet& other);
95 void clear() { *this = LockSet{}; }
96
97 [[nodiscard]] bool operator==(const LockSet& other) const;
98
99private:
100 // The mask comes first, and is all that the checks made on a voice without locks (the usual case) read
101 uint64_t mask_{0};
102 uint8_t count_{0};
103 std::array<uint8_t, kMaxLocksPerNote> params_{};
104 std::array<int32_t, kMaxLocksPerNote> values_{};
105};
106
110bool locksSustain(const LockSet& locks);
111
113class Pool {
114public:
115 Pool() = default;
116 Pool(Pool&&) = default;
117 Pool& operator=(Pool&&) = default;
118 Pool(const Pool&) = delete;
119 Pool& operator=(const Pool&) = delete;
120
123 [[nodiscard]] std::optional<uint16_t> intern(const LockSet& locks);
124
126 [[nodiscard]] LockSet get(uint16_t index) const;
127
128 [[nodiscard]] int32_t numSets() const { return offsets_.empty() ? 0 : static_cast<int32_t>(offsets_.size()) - 1; }
129
130private:
131 template <typename T>
132 using Array = std::vector<T, memory::sdram_allocator<T>>;
133
134 [[nodiscard]] uint16_t lookup(uint32_t hash, const LockSet& locks) const;
135 void addToHash(uint32_t hash, uint16_t index);
136 void rehash(uint32_t newCapacity);
137
138 // Entry i lives at words_[offsets_[i]]: one byte count, then that many param bytes, padded to a word, then the
139 // int32 values. Index 0 is the "no locks" sentinel and has no entry.
140 Array<uint32_t> words_;
141 Array<uint32_t> offsets_; // Including the sentinel, once anything has been interned
142 Array<uint16_t> hash_; // Open addressing, 0 = empty slot. Size a power of two, at most half full.
143};
144
145} // namespace deluge::note_locks
Definition note_locks.h:63
std::optional< int32_t > get(int32_t param) const
The locked value of param, if it's locked.
Definition note_locks.h:77
uint64_t mask() const
Bit p set if param p is locked.
Definition note_locks.h:72
bool set(int32_t param, int32_t value)
Definition note_locks.cpp:36
bool remove(int32_t param)
False if param wasn't locked.
Definition note_locks.cpp:57
bool merge(const LockSet &other)
Sets each of other's locks, on top of these. False if any of them didn't fit.
Definition note_locks.cpp:71
std::optional< uint16_t > intern(const LockSet &locks)
Definition note_locks.cpp:153
LockSet get(uint16_t index) const
Empty for index 0 or an out-of-range index, so a stale index can never read outside the pool.
Definition note_locks.cpp:108
Definition param.cpp:30
How a note's parameter locks (note_locks.h) get from the sequencer to the Voice that plays the note.
Definition note_lock_editor.h:29
bool isLockableParam(int32_t param)
Definition note_locks.cpp:30
constexpr uint64_t paramMask(Params... params)
A bit per param, as LockSet::mask() has them.
Definition note_locks.h:55
bool locksSustain(const LockSet &locks)
Definition note_locks.cpp:83
constexpr int32_t kMaxLocksPerNote
Max locks on one note.
Definition note_locks.h:47