-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotetracker.cpp
More file actions
271 lines (246 loc) · 6.14 KB
/
Copy pathnotetracker.cpp
File metadata and controls
271 lines (246 loc) · 6.14 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include <stdlib.h>
#include <stdio.h>
#include "Sharp.h"
#include "Utilities.h"
#include "noteset.h"
#include "notetracker.h"
#include "LCD.h"
#include "MIDI.h"
/*
* Keep track of all notes:
* - arpeggio defining notes
* - currently held down
* - released but still playing (latched)
* - melody notes (i.e. above a split)
* - currently held down
* - released but still playing (latched)
*/
notetracker::notetracker()
{
heldnotes = new noteset(N);
sortedheld = new noteset(N);
bypassnotes = new noteset(N);
newnotestoplay = new noteset(N);
notestostop = new noteset(N);
}
notetracker::~notetracker()
{
if (heldnotes)
delete heldnotes;
if (sortedheld)
delete sortedheld;
if (bypassnotes)
delete bypassnotes;
if (newnotestoplay)
delete newnotestoplay;
if (notestostop)
delete notestostop;
}
void notetracker::resettracker()
{
heldnotes->clear();
bypassnotes->clear();
sortedheld->clear();
needUpdateNoteDisplay(heldnotes);
}
byte notetracker::getheldnote(int i, byte channel, bool nolatched)
{
if (i < heldnotes->count)
{
noteinfo note = heldnotes->notes[i];
if (nolatched && note.latched)
return 0;
if (note.channel == channel)
return heldnotes->notes[i].pitch;
}
return 0;
}
int notetracker::isheldnote(unsigned char pitch, unsigned char channel, bool nolatched)
{
for (int i = 0; i < sortedheld->count; ++i)
{
noteinfo note = sortedheld->notes[i];
if (note.pitch > pitch)
return -1;
else if (note.pitch == pitch)
{
if (nolatched && note.latched)
return -1;
else
return note.channel == channel ? i : -1;
}
}
return -1;
}
void notetracker::transposeheldnotes(int offset)
{
for (int i = 0; i < heldnotes->count; ++i)
{
heldnotes->notes[i].pitch += offset;
sortedheld->notes[i].pitch += offset;
}
}
bool notetracker::isbypass(unsigned char pitch, unsigned char channel)
{
int i = bypassnotes->findnote(pitch);
if (i < 0)
return false; // nothing to remove
noteinfo note = bypassnotes->notes[i];
return note.channel == channel;
}
void notetracker::sortnotes()
{
delete sortedheld;
sortedheld = heldnotes->copy();
sortedheld->sort();
}
// a note was played - store it accordingly
bool notetracker::tracknoteon(unsigned char pitch, devicestate *state)
{
notestostop->count = 0;
newnotestoplay->count = 0;
// make sure the pitch is valid
if (pitch == 0 || pitch > 127)
return false;
noteinfo note = makenote(pitch);
if (!state->common.active || note.bypass)
{
newnotestoplay->count = 1;
newnotestoplay->notes[0] = note;
}
if (note.bypass) // the note is either a bypass or a held note
{
bypassnotes->addnote(note);
}
else
{
// do we need to reset the latch?
if (state->common.latched && heldnotes->heldcount == 0)
{
notestostop->count = heldnotes->count;
for (int i = 0; i < heldnotes->count; ++i)
notestostop->notes[i] = heldnotes->notes[i];
// clear all latched notes and start again
heldnotes->clear();
}
heldnotes->addnote(note);
sortnotes();
needUpdateNoteDisplay(sortedheld);
#if DEBUG_SERIAL
Serial.println("Note on");
Serial.println("--------------------------------------------");
dump();
Serial.println("--------------------------------------------");
#endif
return heldnotes->heldcount == 1;
}
return false;
}
// a note was lifted, update the sets
bool notetracker::tracknoteoff(byte pitch, devicestate *state)
{
notestostop->count = 0;
newnotestoplay->count = 0;
// make sure the pitch is valid
if (pitch == 0 || pitch > 127)
return false;
noteinfo note;
int i = bypassnotes->findnote(pitch);
if (i >= 0)
{
note = bypassnotes->notes[i];
notestostop->count = 1;
notestostop->notes[0] = note;
bypassnotes->removenote(pitch);
}
else
{
i = heldnotes->findnote(pitch);
if (i < 0)
{
// Serial.println("### ERROR ### Can't find the note");
return false; // nothing to remove - bug?
}
if (state->common.latched)
{
heldnotes->notes[i].latched = true;
heldnotes->heldcount--;
}
else
{
note = heldnotes->notes[i];
heldnotes->removenote(pitch);
notestostop->count = 1;
notestostop->notes[0] = note;
}
sortnotes();
needUpdateNoteDisplay(sortedheld);
}
#if DEBUG_SERIAL
Serial.println("Note off");
Serial.println("--------------------------------------------");
dump();
Serial.println("--------------------------------------------");
#endif
return true;
}
bool notetracker::haslatched()
{
return heldnotes->count != heldnotes->heldcount;
}
void notetracker::clearlatchednotes()
{
int i;
notestostop->clear();
for (i = 0; i < heldnotes->count; ++i)
{
if (heldnotes->notes[i].latched)
{
notestostop->notes[notestostop->count++] = heldnotes->notes[i];
}
}
for (i = 0; i < notestostop->count; ++i)
{
heldnotes->removenote(notestostop->notes[i].pitch);
}
sortnotes();
}
void notetracker::adjustfornewsplit(unsigned char pitch)
{
// remove bypass and latched notes below pitch
int i;
notestostop->clear();
for (i = 0; i < heldnotes->count; ++i)
{
if (curmode == ARPL)
{
if (heldnotes->notes[i].pitch > pitch && heldnotes->notes[i].latched)
{
notestostop->notes[notestostop->count++] = heldnotes->notes[i];
}
}
else if (curmode == ARPH)
{
if (heldnotes->notes[i].pitch < pitch && heldnotes->notes[i].latched)
{
notestostop->notes[notestostop->count++] = heldnotes->notes[i];
}
}
}
for (i = 0; i < notestostop->count; ++i)
{
heldnotes->removenote(notestostop->notes[i].pitch);
}
}
int notetracker::getheldcount(bool includelatched)
{
return includelatched ? heldnotes->count : heldnotes->heldcount;
}
void notetracker::dump()
{
Serial.println(getnotesetstring("heldnotes", heldnotes));
Serial.println(getnotesetstring("sortedheld", sortedheld));
Serial.println(getnotesetstring("bypassnotes", bypassnotes));
Serial.println(getnotesetstring("newnotestoplay", newnotestoplay));
Serial.println(getnotesetstring("notestostop", notestostop));
}