-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapcKey25.cpp
More file actions
339 lines (317 loc) · 12.6 KB
/
apcKey25.cpp
File metadata and controls
339 lines (317 loc) · 12.6 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
#define log_name "apc25"
#include "apcKey25.h"
#include "input_usb.h"
#include "usbMidi.h"
#include "abletonLink.h"
// Only the press-ticks publish hook is needed here; avoid pulling the full
// continuousBuffer.h (which needs Looper.h macros) into this translation unit.
extern volatile unsigned g_pendingPressTicks;
#include "patches/paramSnapshot.h"
#include "patches/RubberBandWrapper.h"
#include <circle/logger.h>
#include <circle/timer.h>
extern RubberBandWrapper *pLivePitchWrapper;
apcKey25 *pTheAPC = 0;
apcKey25::apcKey25()
: m_shift(false), m_cmdHead(0), m_cmdTail(0),
m_nowMs(0), m_bootMs(0), m_lastLedMs(0),
m_transposeLocked(false), m_transposePitch(0), m_pitchWheelOffset(0),
m_driftTarget(0.0f), m_lastDriftMs(0), m_computedRatio(1.0f),
m_liveEngaged(false), m_livePitchSemitones(0.0f), m_liveLedDirty(false),
m_filterHP(0.0f), m_filterLP(1.0f), m_filterRes(0.0f),
m_reverbAmount(0.0f), m_delayAmount(0.0f), m_time(0.5f), m_formant(0.0f),
m_formantResonance(0.0f), m_formantFreq(800.0f)
{
pTheAPC = this;
// Boot with formant at CENTER (0) = pure continuous-reader -12, the CLEAN
// non-grainy path. Off-center crossfades toward the grain path (grainier
// but formant-shiftable). User confirmed the grain path sounds grainy, so
// center is the default good -12.
for (int i = 0; i < LOOPER_NUM_TRACKS; i++)
{
m_looperHoldStart[i] = 0;
m_looperHeld[i] = false;
m_looperClearTriggered[i] = false;
m_looperRecordedOnPress[i] = false;
}
for (int i = 0; i < LOOPER_NUM_PRESETS; i++)
{
m_presetHoldStart[i] = 0;
m_presetHeld[i] = false;
m_presetCaptured[i] = false;
m_presetMask[i] = 0;
m_presetUsed[i] = false;
}
}
u8 apcKey25::_padNote(int row, int col)
{
return (u8)(row * APC_COLS + col);
}
void apcKey25::_sendLed(u8 note, u8 velocity)
{
usbMidiSendNoteOn(note, velocity);
}
void apcKey25::_queueCmd(ApcCmd::Type type, int arg)
{
// SPSC ring push (producer = MIDI ISR). Drop only if full (32 unread) —
// never overwrite an unread command. head==tail means empty.
// Stamp press_ticks HERE: _queueCmd runs inside handleMidi (the MIDI IN
// ISR), so this is the earliest observable moment of the press. One timer
// read, no alloc — ISR-safe (same CTimer::GetClockTicks already used by
// input_usb g_inLastTicks). Backdating uses this as the true press instant.
unsigned head = m_cmdHead;
unsigned next = (head + 1) % APC_CMD_RING;
if (next == m_cmdTail) return; // ring full — drop (should never happen at tap rates)
m_cmdRing[head].type = type;
m_cmdRing[head].arg = arg;
m_cmdRing[head].press_ticks = CTimer::GetClockTicks();
m_cmdHead = next; // publish after fields written
}
void apcKey25::handleMidi(u8 status, u8 data1, u8 data2)
{
u8 msgType = status & 0xF0;
u8 channel = status & 0x0F;
if (msgType == APC_CH_NOTE_ON && data2 > 0)
{
if (data1 == APC_BTN_SHIFT) { m_shift = true; return; }
if (channel == 0 && data1 == 64) {
m_liveEngaged = !m_liveEngaged;
if (!m_liveEngaged) m_livePitchSemitones = 0.0f;
_applyLivePitch();
return;
}
if (channel == 1) {
m_livePitchSemitones = (float)((int)data1 - 60);
m_liveEngaged = true;
_applyLivePitch();
return;
}
if (channel == 2) {
// 0x92 (MIDI ch 3): always-engaged pitch set from note.
// Used by UDP-MIDI inject from host harness for repeatable
// engage-at-pitch testing. Per AGENTS.md spec.
m_livePitchSemitones = (float)((int)data1 - 60);
m_liveEngaged = true;
_applyLivePitch();
return;
}
if (data1 < APC_ROWS * APC_COLS)
{
_onPadPress(data1 / APC_COLS, data1 % APC_COLS);
return;
}
_onButton(data1);
return;
}
if (msgType == APC_CH_NOTE_OFF || (msgType == APC_CH_NOTE_ON && data2 == 0))
{
if (data1 == APC_BTN_SHIFT) { m_shift = false; return; }
if (channel == 0 && data1 == 64) return;
if (channel == 1) { m_transposeLocked = false; return; }
if (data1 < APC_ROWS * APC_COLS)
_onPadRelease(data1 / APC_COLS, data1 % APC_COLS);
return;
}
if (msgType == 0xB0 && data1 == 1)
{
bool inDeadzone = (data2 >= 59 && data2 <= 69);
if (inDeadzone) {
m_liveEngaged = false;
m_livePitchSemitones = 0.0f;
} else {
m_livePitchSemitones = ((float)((int)data2 - 64)) * 12.0f / 63.0f;
m_liveEngaged = true;
}
_applyLivePitch();
return;
}
if (msgType == 0xB0 && data1 == 52)
{
m_livePitchSemitones = (data2 / 127.0f) * 24.0f - 12.0f;
m_liveEngaged = true;
_applyLivePitch();
return;
}
if (msgType == 0xB0 && (data1 == 51 || data1 == 54 || data1 == 55))
{
handleFilterCC(data1, data2);
return;
}
if (msgType == 0xB0 && (data1 == 48 || data1 == 49 || data1 == 50 || data1 == 53 || data1 == 56 || data1 == 57))
{
handleEffectsCC(data1, data2);
return;
}
// Live engine-tuning CCs (UDP-MIDI inject only — no APC knob assigned).
// Use scripts/tune-engine.ps1 or the 3-loopback sweep harness to send
// these and observe results in real-time without rebuilding.
if (msgType == 0xB0 && data1 == 100)
{
// CC100: engine initial read offset (= algorithmic latency).
// data2 0..127 maps log-ish to 32..2048 samples (0.66..43 ms @ 48k).
// Default 192 ≈ data2=48.
int samp = 32 + (int)((float)data2 * (float)data2 * 0.125f);
if (samp > 2048) samp = 2048;
if (pLivePitchWrapper) pLivePitchWrapper->setEngineReadOffset(samp);
CLogger::Get()->Write(log_name, LogNotice, "tune readOffset=%d", samp);
return;
}
if (msgType == 0xB0 && data1 == 101)
{
// CC101: splice xfade scale. data2 0..127 → 0.25..4.0 (log).
// Default 1.0 ≈ data2=64.
float s = 0.25f * powf(16.0f, (float)data2 / 127.0f);
if (pLivePitchWrapper) pLivePitchWrapper->setEngineXfadeScale(s);
CLogger::Get()->Write(log_name, LogNotice, "tune xfadeScale=%.3f", s);
return;
}
if (msgType == 0xB0 && data1 == 102)
{
// CC102: SNAC fidelity threshold. data2 0..127 → 0.30..0.95 linear.
// Default 0.7 ≈ data2=88.
float f = 0.30f + ((float)data2 / 127.0f) * 0.65f;
if (pLivePitchWrapper) pLivePitchWrapper->setEngineFidelity(f);
CLogger::Get()->Write(log_name, LogNotice, "tune fidelity=%.3f", f);
return;
}
if (msgType == 0xB0 && data1 == 103)
{
// CC103: pre-resample bypass. data2 < 64 = off (engine runs normally),
// data2 >= 64 = bypass. When formantDepth=0 the bypass is auto-applied
// anyway, but the toggle lets us A/B the stage on/off explicitly.
bool on = (data2 >= 64);
if (pLivePitchWrapper) pLivePitchWrapper->setEnginePreBypass(on);
CLogger::Get()->Write(log_name, LogNotice, "tune preBypass=%d", on?1:0);
return;
}
if (msgType == 0xB0 && data1 == 104)
{
// CC104: splice integer-period snap. data2 < 64 = off, >= 64 = on.
bool on = (data2 >= 64);
if (pLivePitchWrapper) pLivePitchWrapper->setEngineSpliceSnap(on);
CLogger::Get()->Write(log_name, LogNotice, "tune spliceSnap=%d", on?1:0);
return;
}
if (msgType == 0xB0 && data1 == 105)
{
// CC105: splice value-match refinement. data2 < 64 = off, >= 64 = on.
bool on = (data2 >= 64);
if (pLivePitchWrapper) pLivePitchWrapper->setEngineSpliceMatch(on);
CLogger::Get()->Write(log_name, LogNotice, "tune spliceMatch=%d", on?1:0);
return;
}
if (msgType == 0xB0 && data1 == 106)
{
// CC106: drift low band. data2 0..127 → 1..32 samples (linear).
int s = 1 + (int)(((float)data2 / 127.0f) * 31.0f);
if (pLivePitchWrapper) pLivePitchWrapper->setEngineDriftLow(s);
CLogger::Get()->Write(log_name, LogNotice, "tune driftLow=%d", s);
return;
}
if (msgType == 0xB0 && data1 == 107)
{
// CC107: drift high head. data2 0..127 → 16..1024 samples (linear).
int s = 16 + (int)(((float)data2 / 127.0f) * 1008.0f);
if (pLivePitchWrapper) pLivePitchWrapper->setEngineDriftHigh(s);
CLogger::Get()->Write(log_name, LogNotice, "tune driftHigh=%d", s);
return;
}
}
void apcKey25::update()
{
if (!pTheLooper) return;
// Re-publish snapshot every tick so Core 1 DSP sees fresh link tempo
// and any externally-mutated state without depending on MIDI handlers.
{
LiveParams p;
p.liveEngaged = m_liveEngaged;
p.livePitchSemitones = m_livePitchSemitones;
p.formantNorm = m_formant;
p.linkSynced = linkIsSynced();
p.linkBPM = (float)linkGetBPM();
p.masterLoopBlocks = pTheLooper->m_masterLoopBlocks;
paramSnapshotPublish(p);
}
if (m_liveLedDirty)
{
// Drop-retry: clear dirty ONLY if the frame was actually queued
// (usbMidiSendNoteOn returns false when MIDI OUT is full). Previously
// this used fire-and-forget usbMidiSend() and cleared the flag
// unconditionally, so a dropped frame left the live-engage LED stuck on
// its old state forever. Now a drop leaves m_liveLedDirty set and the
// next tick retries — same consistency guarantee the grid LEDs get from
// sendLedCoalesced's retry-on-drop cache.
if (usbMidiSendNoteOn(0x40, m_liveEngaged ? 127 : 0))
m_liveLedDirty = false;
}
m_nowMs = CTimer::Get()->GetClockTicks() / 1000;
if (m_bootMs == 0) m_bootMs = m_nowMs;
// Drain ALL queued commands this tick (in press order) so nothing waits an
// extra tick or gets dropped — keeps every looper's response prompt + equal.
while (m_cmdTail != m_cmdHead)
{
ApcCmd::Type type = m_cmdRing[m_cmdTail].type;
int arg = m_cmdRing[m_cmdTail].arg;
// Publish the press instant for this command so record start/stop can
// backdate to it (read in the same Core-2 call, single-threaded).
g_pendingPressTicks = m_cmdRing[m_cmdTail].press_ticks;
m_cmdTail = (m_cmdTail + 1) % APC_CMD_RING;
if (type == ApcCmd::TRACK)
{
pTheLooper->command(LOOP_COMMAND_TRACK_BASE + arg);
}
else if (type == ApcCmd::STOP_TRACK)
{
pTheLooper->command(LOOP_COMMAND_STOP_TRACK_BASE + arg);
}
else if (type == ApcCmd::CLEAR_LAYER)
{
pTheLooper->command(LOOP_COMMAND_CLEAR_LAYER_BASE + arg);
}
else if (type == ApcCmd::PRESET_RESTORE)
{
_applyPreset(arg);
}
else if (type == ApcCmd::LOOPER)
{
pTheLooper->command(arg);
}
}
_updateDrift();
// Per-looper long-hold → clear-layer (also clears recording state).
for (int n = 0; n < LOOPER_NUM_TRACKS; n++)
{
if (m_looperHeld[n] && !m_looperClearTriggered[n])
{
if (m_nowMs - m_looperHoldStart[n] >= APC_HOLD_ERASE_MS)
{
m_looperClearTriggered[n] = true;
m_looperHeld[n] = false;
// Hold = stop+clear, ALWAYS ends with the LED off. Route to
// ERASE_TRACK (not CLEAR_LAYER): ERASE_TRACK unconditionally
// clears content + cancels any pending deferred-record and
// never re-arms, whereas CLEAR_LAYER on an empty/just-erased
// track re-arms a pending RECORD when a master grid exists,
// leaving the pad yellow (the "LED didn't disappear" bug).
pTheLooper->command(LOOP_COMMAND_ERASE_TRACK_BASE + n);
}
}
}
// Per-preset long-hold → capture current play-state mask.
for (int p = 0; p < LOOPER_NUM_PRESETS; p++)
{
if (m_presetHeld[p] && !m_presetCaptured[p])
{
if (m_nowMs - m_presetHoldStart[p] >= APC_HOLD_ERASE_MS)
{
m_presetCaptured[p] = true;
m_presetHeld[p] = false;
_capturePreset(p);
}
}
}
if (m_nowMs - m_bootMs < APC_LED_BOOT_DELAY_MS) return;
if (m_nowMs - m_lastLedMs < 33) return;
m_lastLedMs = m_nowMs;
_updateGridLeds();
}