-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperformance_engine.cpp
More file actions
470 lines (397 loc) · 11.7 KB
/
Copy pathperformance_engine.cpp
File metadata and controls
470 lines (397 loc) · 11.7 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#include "performance_engine.h"
#include "synth.h"
namespace {
unsigned long stepIntervalUs(const TransportState &transport, uint8_t step_index) {
const uint16_t bpm = transport.bpm < 40 ? 40 : transport.bpm;
const unsigned long quarter_us = 60000000UL / bpm;
const float base = static_cast<float>(quarter_us) / 4.0f;
const float swing = transport.swing;
if (swing <= 0.001f) {
return static_cast<unsigned long>(base);
}
const bool odd_step = (step_index % 2U) == 1U;
const float swing_offset = 0.5f * swing;
const float multiplier = odd_step ? (1.0f + swing_offset) : (1.0f - swing_offset);
return static_cast<unsigned long>(base * multiplier);
}
uint8_t clampStepLength(uint8_t length) {
if (length < 1) {
return 1;
}
if (length > 16) {
return 16;
}
return length;
}
}
void PerformanceEngine::begin(Synth *synth) {
synth_ = synth;
state_ = &AppState::instance();
next_step_due_us_ = micros();
gate_off_due_us_ = 0;
}
void PerformanceEngine::update() {
if (synth_ == nullptr || state_ == nullptr) {
return;
}
syncTransportState();
releaseGeneratedNoteIfDue();
if (externalClock()) {
// Steps are driven by incoming MIDI clock ticks, not micros(). Here we only
// detect a clock dropout so the indicator clears and the transport stops.
if (state_->transport.ext_clock_present && clock_timing_valid_ &&
static_cast<long>(micros() - last_clock_us_) > 500000L) {
state_->transport.ext_clock_present = false;
clock_timing_valid_ = false;
if (state_->transport.running) {
state_->transport.running = false;
transport_running_cache_ = false;
clearGeneratedNote();
}
state_->markDirty();
}
return;
}
if (!state_->transport.running) {
return;
}
const unsigned long now = micros();
uint8_t safety = 0;
while (static_cast<long>(now - next_step_due_us_) >= 0 && safety < 4) {
advanceTransportStep();
++safety;
}
}
bool PerformanceEngine::externalClock() const {
return state_ != nullptr && state_->transport.clock_source == ClockSource::EXTERNAL;
}
void PerformanceEngine::onMidiClockTick() {
if (state_ == nullptr || !externalClock()) {
return;
}
const unsigned long now = micros();
if (clock_timing_valid_) {
const unsigned long delta = now - last_clock_us_;
if (delta > 0) {
// 24 PPQN: a quarter note spans 24 ticks. BPM = 60e6 / (delta_us * 24).
const float bpm = 60000000.0f / (static_cast<float>(delta) * 24.0f);
if (bpm >= 20.0f && bpm <= 400.0f) {
const float smoothed = 0.8f * static_cast<float>(state_->transport.bpm) + 0.2f * bpm;
state_->transport.bpm = static_cast<uint16_t>(smoothed + 0.5f);
}
}
}
last_clock_us_ = now;
clock_timing_valid_ = true;
state_->transport.ext_clock_present = true;
if (!state_->transport.running) {
return;
}
if (clock_tick_count_ == 0) {
fireStep();
}
clock_tick_count_ = static_cast<uint8_t>((clock_tick_count_ + 1U) % kClocksPerStep);
}
void PerformanceEngine::onMidiStart() {
if (state_ == nullptr || !externalClock()) {
return;
}
state_->transport.step_index = 0;
state_->sequencer.playhead = 0;
clock_tick_count_ = 0;
arp_division_counter_ = 0;
arp_index_ = -1;
state_->transport.running = true;
state_->transport.ext_clock_present = true;
transport_running_cache_ = true;
state_->markDirty();
}
void PerformanceEngine::onMidiContinue() {
if (state_ == nullptr || !externalClock()) {
return;
}
state_->transport.running = true;
state_->transport.ext_clock_present = true;
transport_running_cache_ = true;
state_->markDirty();
}
void PerformanceEngine::onMidiStop() {
if (state_ == nullptr || !externalClock()) {
return;
}
state_->transport.running = false;
transport_running_cache_ = false;
clearGeneratedNote();
state_->markDirty();
}
void PerformanceEngine::onMidiNoteOn(byte, byte note, byte velocity) {
if (state_ == nullptr || synth_ == nullptr) {
return;
}
if (synth_->isSelfTestActive()) {
return;
}
if (state_->sequencer.enabled && state_->transport.running && state_->sequencer.record_armed) {
recordStepFromMidi(note);
synth_->noteOn(note, velocity);
return;
}
addHeldNote(note);
if (state_->arp.enabled) {
if (!state_->transport.running) {
synth_->noteOn(note, velocity);
}
return;
}
if (state_->sequencer.enabled && state_->transport.running) {
return;
}
if (directPlayEnabled()) {
synth_->noteOn(note, velocity);
}
}
void PerformanceEngine::onMidiNoteOff(byte, byte note, byte) {
if (state_ == nullptr || synth_ == nullptr) {
return;
}
if (synth_->isSelfTestActive()) {
return;
}
removeHeldNote(note);
if (state_->sequencer.enabled && state_->transport.running && state_->sequencer.record_armed) {
synth_->noteOff(note);
return;
}
if (state_->arp.enabled) {
if (!state_->transport.running && !state_->arp.latch) {
synth_->noteOff(note);
}
return;
}
if (state_->sequencer.enabled && state_->transport.running) {
return;
}
if (directPlayEnabled()) {
synth_->noteOff(note);
}
}
void PerformanceEngine::onMidiPitchBend(byte, int pitch) {
if (synth_ != nullptr && !synth_->isSelfTestActive()) {
synth_->setPitchBend(pitch);
}
}
void PerformanceEngine::stopTransport() {
if (state_ == nullptr) {
return;
}
state_->transport.running = false;
state_->transport.step_index = 0;
state_->sequencer.playhead = 0;
state_->markDirty();
clearGeneratedNote();
}
void PerformanceEngine::clearHeldNotes() {
held_count_ = 0;
arp_index_ = -1;
clearGeneratedNote();
}
void PerformanceEngine::syncTransportState() {
if (state_ == nullptr) {
return;
}
if (state_->transport.running == transport_running_cache_) {
return;
}
transport_running_cache_ = state_->transport.running;
if (transport_running_cache_) {
next_step_due_us_ = micros();
arp_division_counter_ = 0;
} else {
clearGeneratedNote();
}
}
void PerformanceEngine::advanceTransportStep() {
if (state_ == nullptr) {
return;
}
const unsigned long interval = stepIntervalUs(state_->transport, state_->transport.step_index);
next_step_due_us_ += interval;
fireStep();
}
void PerformanceEngine::fireStep() {
if (state_ == nullptr) {
return;
}
const uint8_t current_step = state_->transport.step_index;
state_->sequencer.playhead = current_step % clampStepLength(state_->sequencer.length);
if (state_->sequencer.enabled) {
handleSequencerStep();
} else if (state_->arp.enabled) {
handleArpStep();
}
state_->transport.step_index =
static_cast<uint8_t>((current_step + 1U) % clampStepLength(state_->sequencer.length));
if (state_->ui.page == PageId::SEQ) {
state_->markDirty();
}
}
void PerformanceEngine::handleSequencerStep() {
const uint8_t step_index = state_->sequencer.playhead;
const SequenceStep &step = state_->sequencer.steps[step_index];
if (!step.active) {
clearGeneratedNote();
return;
}
const unsigned long interval = stepIntervalUs(state_->transport, step_index);
const unsigned long gate_duration =
(interval * static_cast<unsigned long>(step.gate)) / 100UL;
triggerGeneratedNote(step.note, 108, gate_duration);
}
void PerformanceEngine::handleArpStep() {
if (held_count_ == 0) {
clearGeneratedNote();
return;
}
arp_division_counter_ = static_cast<uint8_t>((arp_division_counter_ + 1U) % state_->arp.division);
if (arp_division_counter_ != 0U) {
return;
}
const uint8_t note = nextArpNote();
const unsigned long interval =
stepIntervalUs(state_->transport, state_->transport.step_index) * state_->arp.division;
const unsigned long gate_duration =
(interval * static_cast<unsigned long>(state_->arp.gate)) / 100UL;
triggerGeneratedNote(note, 112, gate_duration);
}
void PerformanceEngine::triggerGeneratedNote(uint8_t note,
uint8_t velocity,
unsigned long duration_us) {
if (synth_ == nullptr) {
return;
}
if (generated_note_active_) {
synth_->noteOff(generated_note_);
}
generated_note_ = note;
generated_note_active_ = true;
synth_->noteOn(note, velocity);
if (duration_us < 1000UL) {
duration_us = 1000UL;
}
gate_off_due_us_ = micros() + duration_us;
}
void PerformanceEngine::releaseGeneratedNoteIfDue() {
if (!generated_note_active_ || synth_ == nullptr) {
return;
}
const unsigned long now = micros();
if (static_cast<long>(now - gate_off_due_us_) < 0) {
return;
}
synth_->noteOff(generated_note_);
generated_note_active_ = false;
}
void PerformanceEngine::clearGeneratedNote() {
if (generated_note_active_ && synth_ != nullptr) {
synth_->noteOff(generated_note_);
}
generated_note_active_ = false;
}
void PerformanceEngine::addHeldNote(uint8_t note) {
for (uint8_t index = 0; index < held_count_; ++index) {
if (held_notes_[index] == note) {
return;
}
}
if (held_count_ >= kHeldCapacity) {
return;
}
held_notes_[held_count_] = note;
++held_count_;
for (uint8_t outer = 0; outer + 1U < held_count_; ++outer) {
for (uint8_t inner = outer + 1U; inner < held_count_; ++inner) {
if (held_notes_[inner] < held_notes_[outer]) {
const uint8_t temp = held_notes_[outer];
held_notes_[outer] = held_notes_[inner];
held_notes_[inner] = temp;
}
}
}
}
void PerformanceEngine::removeHeldNote(uint8_t note) {
if (state_ != nullptr && state_->arp.latch) {
return;
}
for (uint8_t index = 0; index < held_count_; ++index) {
if (held_notes_[index] == note) {
for (uint8_t move = index; move + 1U < held_count_; ++move) {
held_notes_[move] = held_notes_[move + 1U];
}
--held_count_;
if (held_count_ == 0) {
arp_index_ = -1;
} else if (arp_index_ >= held_count_) {
arp_index_ = static_cast<int8_t>(held_count_ - 1U);
}
return;
}
}
}
uint8_t PerformanceEngine::nextArpNote() {
if (held_count_ == 0) {
return 60;
}
switch (state_->arp.mode) {
case ArpMode::DOWN:
if (arp_index_ < 0) {
arp_index_ = held_count_;
}
--arp_index_;
if (arp_index_ < 0) {
arp_index_ = held_count_ - 1;
}
break;
case ArpMode::UP_DOWN:
if (arp_index_ < 0) {
arp_index_ = 0;
} else {
arp_index_ += arp_direction_;
if (arp_index_ >= held_count_) {
arp_index_ = held_count_ > 1 ? held_count_ - 2 : 0;
arp_direction_ = -1;
} else if (arp_index_ < 0) {
arp_index_ = held_count_ > 1 ? 1 : 0;
arp_direction_ = 1;
}
}
break;
case ArpMode::RANDOM:
arp_index_ = random(held_count_);
break;
case ArpMode::UP:
default:
arp_index_ = static_cast<int8_t>((arp_index_ + 1) % held_count_);
break;
}
uint8_t octave_offset = 0;
if (state_->arp.octave_range > 1) {
const uint8_t octave_slot =
static_cast<uint8_t>(state_->transport.step_index % state_->arp.octave_range);
octave_offset = octave_slot * 12U;
}
return static_cast<uint8_t>(min<uint16_t>(held_notes_[arp_index_] + octave_offset, 127));
}
void PerformanceEngine::recordStepFromMidi(uint8_t note) {
const uint8_t index = state_->sequencer.playhead % 16U;
SequenceStep &step = state_->sequencer.steps[index];
step.active = true;
step.note = note;
step.gate = 78;
state_->sequencer.selected_step = index;
state_->markDirty();
}
bool PerformanceEngine::directPlayEnabled() const {
return state_ != nullptr && (!state_->arp.enabled) &&
(!state_->sequencer.enabled || !state_->transport.running);
}
PerformanceEngine performance_engine;