forked from pichenettes/eurorack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnote_stack.h
executable file
·178 lines (163 loc) · 5.32 KB
/
note_stack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2012 Emilie Gillet.
//
// Author: Emilie Gillet ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// Stack of currently pressed keys.
//
// Currently pressed keys are stored as a linked list. The linked list is used
// as a LIFO stack to allow monosynth-like behaviour. An example of such
// behaviour is:
// player presses and holds C4-> C4 is played.
// player presses and holds C5 (while holding C4) -> C5 is played.
// player presses and holds G4 (while holding C4&C5)-> G4 is played.
// player releases C5 -> G4 is played.
// player releases G4 -> C4 is played.
//
// The nodes used in the linked list are pre-allocated from a pool of 16
// nodes, so the "pointers" (to the root element for example) are not actual
// pointers, but indices of an element in the pool.
//
// Additionally, an array of pointers is stored to allow random access to the
// n-th note, sorted by ascending order of pitch (for arpeggiation).
#ifndef EDGES_NOTE_STACK_H_
#define EDGES_NOTE_STACK_H_
#include "avrlibx/avrlibx.h"
#include <string.h>
namespace edges {
static const uint8_t kFreeSlot = 0xff;
struct NoteEntry {
uint8_t note;
uint8_t velocity;
uint8_t next_ptr; // Base 1.
};
// This looks crazy, but we are more concerned about RAM used than code size here.
template<uint8_t capacity>
class NoteStack {
public:
NoteStack() { }
void Init() { Clear(); }
void NoteOn(uint8_t note, uint8_t velocity) {
// Remove the note from the list first (in case it is already here).
NoteOff(note);
// In case of saturation, remove the least recently played note from the
// stack.
if (size_ == capacity) {
uint8_t least_recent_note;
for (uint8_t i = 1; i <= capacity; ++i) {
if (pool_[i].next_ptr == 0) {
least_recent_note = pool_[i].note;
}
}
NoteOff(least_recent_note);
}
// Now we are ready to insert the new note. Find a free slot to insert it.
uint8_t free_slot;
for (uint8_t i = 1; i <= capacity; ++i) {
if (pool_[i].note == kFreeSlot) {
free_slot = i;
break;
}
}
pool_[free_slot].next_ptr = root_ptr_;
pool_[free_slot].note = note;
pool_[free_slot].velocity = velocity;
root_ptr_ = free_slot;
// The last step consists in inserting the note in the sorted list.
for (uint8_t i = 0; i < size_; ++i) {
if (pool_[sorted_ptr_[i]].note > note) {
for (uint8_t j = size_; j > i; --j) {
sorted_ptr_[j] = sorted_ptr_[j - 1];
}
sorted_ptr_[i] = free_slot;
free_slot = 0;
break;
}
}
if (free_slot) {
sorted_ptr_[size_] = free_slot;
}
++size_;
}
void NoteOff(uint8_t note) {
uint8_t current = root_ptr_;
uint8_t previous = 0;
while (current) {
if (pool_[current].note == note) {
break;
}
previous = current;
current = pool_[current].next_ptr;
}
if (current) {
if (previous) {
pool_[previous].next_ptr = pool_[current].next_ptr;
} else {
root_ptr_ = pool_[current].next_ptr;
}
for (uint8_t i = 0; i < size_; ++i) {
if (sorted_ptr_[i] == current) {
for (uint8_t j = i; j < size_ - 1; ++j) {
sorted_ptr_[j] = sorted_ptr_[j + 1];
}
break;
}
}
pool_[current].next_ptr = 0;
pool_[current].note = kFreeSlot;
pool_[current].velocity = 0;
--size_;
}
}
void Clear() {
size_ = 0;
memset(pool_ + 1, 0, sizeof(NoteEntry) * capacity);
memset(sorted_ptr_ + 1, 0, capacity);
root_ptr_ = 0;
for (uint8_t i = 0; i <= capacity; ++i) {
pool_[i].note = kFreeSlot;
}
}
uint8_t size() const { return size_; }
const NoteEntry& most_recent_note() const { return pool_[root_ptr_]; }
const NoteEntry& least_recent_note() const {
uint8_t current = root_ptr_;
while (current && pool_[current].next_ptr) {
current = pool_[current].next_ptr;
}
return pool_[current];
}
const NoteEntry& played_note(uint8_t index) const {
uint8_t current = root_ptr_;
index = size_ - index - 1;
for (uint8_t i = 0; i < index; ++i) {
current = pool_[current].next_ptr;
}
return pool_[current];
}
const NoteEntry& sorted_note(uint8_t index) const {
return pool_[sorted_ptr_[index]];
}
const NoteEntry& note(uint8_t index) const { return pool_[index]; }
const NoteEntry& dummy() const { return pool_[0]; }
private:
uint8_t size_;
NoteEntry pool_[capacity + 1]; // First element is a dummy node!
uint8_t root_ptr_; // Base 1.
uint8_t sorted_ptr_[capacity + 1]; // Base 1.
DISALLOW_COPY_AND_ASSIGN(NoteStack);
};
} // namespace edges
#endif // EDGES_NOTE_STACK_H_