-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.cpp
More file actions
489 lines (441 loc) · 18 KB
/
Copy pathinput.cpp
File metadata and controls
489 lines (441 loc) · 18 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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// ============================================================
// Microgroove - input.cpp
// Snapshot diffing -> immediate / short / long press dispatch.
// All bindings live in keymap.h.
// Sampling gestures:
// hold AUX (.) 0.5s -> mic records to the current drum lane
// while AUX stays held; release = commit
// hold SONG (n) 0.5s -> (while playing) resample the mix;
// then tap a pad to commit
// ============================================================
#include <M5Cardputer.h>
#include "config.h"
#include "keymap.h"
#include "sequencer.h"
#include "sampler.h"
#include "storage.h"
#include "wavetable.h"
#include "ui.h"
#include "mic_sampler.h"
void inputInit();
void inputUpdate();
#define LONG_PRESS_MS 450
#define HINT_AFTER_MS 140 // progress bar appears after this
#define RPT_DELAY_MS 350
#define RPT_RATE_MS 90
// ---------- snapshot of all 56 keys as synthetic codes ----------
struct KeySnap {
uint8_t codes[16];
uint8_t n = 0;
void add(uint8_t kc) { if (n < sizeof(codes)) codes[n++] = kc; }
bool has(uint8_t kc) const {
for (uint8_t i = 0; i < n; i++) if (codes[i] == kc) return true;
return false;
}
};
static KeySnap s_prev;
uint8_t g_recPadKc = KC_NONE; // key that ends mic capture on release
// pending short/long holds
struct Hold { uint8_t kc; uint8_t act; uint32_t t0; bool fired; };
static Hold s_holds[8];
static uint8_t s_nHolds = 0;
// auto-repeat state (one repeating key at a time is plenty)
static uint8_t s_rptAct = ACT_NONE;
static uint8_t s_rptKc = KC_NONE;
static uint32_t s_rptNext = 0;
static uint16_t s_rptCount = 0;
// ---------- helpers ----------
static bool accentHeld(const KeySnap& s) { return s.has('m'); }
static uint8_t heldPianoCount(const KeySnap& s) {
uint8_t c = 0;
for (uint8_t i = 0; i < s.n; i++) if (pianoSemi(s.codes[i]) >= 0) c++;
return c;
}
static void semiToNote(int8_t semi, uint8_t& note, uint8_t& oct) {
note = (uint8_t)(semi % 12) + 1;
oct = (uint8_t)constrain((int)g_curOctave + semi / 12, 1, 7);
}
// ---------- SOUND page parameter model ----------
#define SYNTH_PARAMS 9
#define DRUM_PARAMS 7
static void adjustSynthParam(SynthTrack& t, uint8_t row, int dir, bool fine) {
float step = fine ? 0.01f : 0.05f;
if (row == 8) { // VOICES 1..MAX_POLY
t.setVoices((uint8_t)constrain((int)t.voices + dir, 1, MAX_POLY));
return;
}
t.forEach([&](SynthVoice& v) {
switch (row) {
case 0: v.oscMode = (OscMode)(((int)v.oscMode + dir + OSC_COUNT) % OSC_COUNT); break;
case 1: if (g_numWavetables)
v.wtIndex = (uint8_t)(((int)v.wtIndex + dir + g_numWavetables) % g_numWavetables);
break;
case 2: v.fltCutoff = constrain(v.fltCutoff + dir * step, 0.0f, 1.0f); break;
case 3: v.fltReso = constrain(v.fltReso + dir * step, 0.0f, 1.0f); break;
case 4: v.fltEnvAmt = constrain(v.fltEnvAmt + dir * step, 0.0f, 1.0f); break;
case 5: v.filtDecRate = constrain(v.filtDecRate + dir * (fine ? 0.00002f : 0.0001f),
0.9950f, 0.99995f); break;
case 6: v.ampDecRate = constrain(v.ampDecRate + dir * (fine ? 0.00001f : 0.00005f),
0.9990f, 0.99999f); break;
case 7: v.volume = constrain(v.volume + dir * step, 0.0f, 1.0f); break;
}
});
}
static void adjustDrumParam(uint8_t lane, uint8_t row, int dir, bool fine) {
DrumLane& d = g_drumLanes[lane];
switch (row) {
case 0: g_curDrumLane = (uint8_t)((lane + dir + NUM_DRUM_LANES) % NUM_DRUM_LANES); break;
case 1: d.engine = (DrumEngine)(((int)d.engine + dir + ENG_COUNT) % ENG_COUNT); break;
case 2:
if (d.engine == ENG_SMPL) {
if (g_numSamples)
d.sampleSlot = (int8_t)((((d.sampleSlot < 0 ? 0 : d.sampleSlot)) + dir
+ g_numSamples) % g_numSamples);
} else {
d.type = (uint8_t)(((int)d.type + dir + DT_COUNT) % DT_COUNT);
}
break;
case 3: d.volume = constrain(d.volume + dir * (fine ? 0.01f : 0.05f), 0.0f, 1.0f); break;
case 4: d.tune = constrain(d.tune + dir * (fine ? 0.1f : 1.0f), -12.0f, 12.0f); break;
case 5: d.decay = constrain(d.decay + dir * (fine ? 0.02f : 0.1f), 0.4f, 2.5f); break;
case 6: d.chokeGroup = (uint8_t)(((int)d.chokeGroup + dir + 4) % 4); break;
}
}
// ---------- arrows, per page ----------
static void arrow(uint8_t act, const KeySnap& now) {
int dx = (act == ACT_LEFT) ? -1 : (act == ACT_RIGHT) ? 1 : 0;
int dy = (act == ACT_UP) ? -1 : (act == ACT_DOWN) ? 1 : 0;
switch (g_curPage) {
case PAGE_PATTERN:
if (dy) {
if (g_curTrack < NUM_SYNTHS) {
// synth rows: down eventually enters drums (top = lane 8, TR order)
if (dy > 0) {
if (g_curTrack + 1 < NUM_SYNTHS) g_curTrack++;
else { g_curTrack = NUM_SYNTHS; g_curDrumLane = NUM_DRUM_LANES - 1; }
} else if (g_curTrack > 0) g_curTrack--;
} else {
// drum grid is drawn kick-at-bottom: visual down = lane-1
if (dy > 0) { if (g_curDrumLane > 0) g_curDrumLane--; }
else {
if (g_curDrumLane < NUM_DRUM_LANES - 1) g_curDrumLane++;
else g_curTrack = NUM_SYNTHS - 1; // exit up into synth 3
}
}
}
if (dx) g_curStep = (uint8_t)((g_curStep + NUM_STEPS + dx) % NUM_STEPS);
break;
case PAGE_SOUND: {
uint8_t rows = (g_curTrack == NUM_SYNTHS) ? DRUM_PARAMS : SYNTH_PARAMS;
if (dy) g_soundParam = (uint8_t)((g_soundParam + rows + dy) % rows);
if (dx) {
bool fine = accentHeld(now);
if (g_curTrack == NUM_SYNTHS) adjustDrumParam(g_curDrumLane, g_soundParam, dx, fine);
else adjustSynthParam(g_synths[g_curTrack], g_soundParam, dx, fine);
}
break;
}
case PAGE_SAMPLE:
if (dy < 0 && g_fileSel > 0) g_fileSel--;
if (dy > 0 && g_fileSel + 1 < g_fileCount) g_fileSel++;
break;
case PAGE_SONG:
if (dy) g_songCursor = (uint8_t)((g_songCursor + SONG_LENGTH + dy * 16) % SONG_LENGTH);
if (dx) g_songCursor = (uint8_t)((g_songCursor + SONG_LENGTH + dx) % SONG_LENGTH);
break;
default: break;
}
g_needRedraw = true;
}
// ---------- immediate actions (key-down) ----------
static void doImmediate(uint8_t act, const KeySnap& now) {
switch (act) {
case ACT_LEFT: case ACT_RIGHT: case ACT_UP: case ACT_DOWN:
arrow(act, now); break;
case ACT_SLIDE:
if (g_curTrack < NUM_SYNTHS) {
SynthCell& c = g_patterns[g_curPattern].synth[g_curTrack][g_curStep];
if (!c.empty()) {
c.slide = !c.slide;
if (c.slide && g_synths[g_curTrack].voices > 1)
uiStatus("SLIDE = MONO ONLY");
g_needRedraw = true;
}
}
break;
case ACT_REC:
g_recEnabled = !g_recEnabled;
uiStatus(g_recEnabled ? "REC ON" : "REC OFF");
g_needRedraw = true; break;
case ACT_ACCENT: break; // pure modifier
default: break;
}
}
// ---------- short actions (release < 450ms) ----------
static void doShort(uint8_t act) {
// track select
if (act >= ACT_TRACK1 && act <= ACT_TRACKD) {
g_curTrack = (uint8_t)(act - ACT_TRACK1); // 0..2, 3 = drums
g_needRedraw = true; return;
}
// pattern keys: context
if (act >= ACT_PAT1 && act <= ACT_PAT8) {
uint8_t k = (uint8_t)(act - ACT_PAT1);
if (g_curPage == PAGE_SAMPLE) {
if (g_fileCount) {
int slot = samplerLoad(g_fileList[g_fileSel]);
if (slot >= 0) {
DrumLane& d = g_drumLanes[k];
d.engine = ENG_SMPL; d.sampleSlot = (int8_t)slot; d.smp.init();
uiStatus("ASSIGNED");
} else uiStatus("LOAD FAILED");
g_needRedraw = true;
}
} else if (g_curPage == PAGE_SONG) {
g_song[g_songCursor] = k;
g_songCursor = (g_songCursor + 1) % SONG_LENGTH;
g_needRedraw = true;
} else {
g_curPattern = k;
if (!g_playing && !g_songMode) g_playPattern = k;
uiStatus("PATTERN"); g_needRedraw = true;
}
return;
}
switch (act) {
case ACT_LOAD: {
char m[24];
snprintf(m, sizeof(m), "P%u%s hold=LOAD", g_curProject + 1,
storageProjectExists(g_curProject) ? "*" : " empty");
uiStatus(m); break;
}
case ACT_SAVE: {
char m[24];
snprintf(m, sizeof(m), "P%u hold=SAVE", g_curProject + 1);
uiStatus(m); break;
}
case ACT_BPM_DN:
g_bpm = (uint16_t)constrain((int)g_bpm - 1, 40, 300);
g_needRedraw = true; break;
case ACT_BPM_UP:
g_bpm = (uint16_t)constrain((int)g_bpm + 1, 40, 300);
g_needRedraw = true; break;
case ACT_PAGE:
g_curPage = (Page)(((int)g_curPage + 1) % PAGE_COUNT);
if (g_curPage == PAGE_SAMPLE) uiScanSampleDir();
g_needRedraw = true; break;
case ACT_PLAY:
if (g_playing) sequencerStop(); else sequencerStart(false);
g_needRedraw = true; break;
case ACT_CLR:
if (g_curPage == PAGE_SONG) g_song[g_songCursor] = SONG_EMPTY;
else clearCellAtCursor();
g_needRedraw = true; break;
case ACT_SONG:
g_songMode = !g_songMode;
uiStatus(g_songMode ? "SONG MODE" : "PATTERN MODE");
g_needRedraw = true; break;
case ACT_AUX:
if (g_curPage == PAGE_SAMPLE && g_fileCount) {
int slot = samplerLoad(g_fileList[g_fileSel]);
if (slot >= 0) g_previewVoice.trigger(slot, 1.0f, 0.9f);
else uiStatus("LOAD FAILED");
g_needRedraw = true;
} else if (g_curPage == PAGE_SONG) {
g_songLoopStart = g_songCursor;
uiStatus("LOOP SET"); g_needRedraw = true;
} else {
uiStatus("hold = MIC SAMPLE");
}
break;
default: break;
}
}
// ---------- long actions (held 450ms) ----------
static void doLong(uint8_t act) {
if (act >= ACT_TRACK1 && act <= ACT_TRACK3) {
uint8_t t = (uint8_t)(act - ACT_TRACK1);
g_synthMute[t] = !g_synthMute[t];
uiStatus(g_synthMute[t] ? "MUTED" : "UNMUTED");
g_needRedraw = true; return;
}
if (act == ACT_TRACKD) {
g_drumMute = !g_drumMute;
uiStatus(g_drumMute ? "DRUMS MUTED" : "DRUMS ON");
g_needRedraw = true; return;
}
if (act >= ACT_PAT1 && act <= ACT_PAT8) {
if (g_curPage == PAGE_SAMPLE || g_curPage == PAGE_SONG) return;
uint8_t k = (uint8_t)(act - ACT_PAT1);
clonePatternTo(k);
g_curPattern = k;
if (!g_playing && !g_songMode) g_playPattern = k;
char m[16]; snprintf(m, sizeof(m), "CLONED>%u", k + 1);
uiStatus(m); g_needRedraw = true; return;
}
switch (act) {
case ACT_LOAD:
uiStatus(storageLoadProject(g_curProject) ? "LOADED" : "LOAD FAILED");
g_needRedraw = true; break;
case ACT_SAVE:
uiStatus(storageSaveProject(g_curProject) ? "SAVED" : "SAVE FAILED");
break;
case ACT_PAGE:
g_curPage = PAGE_PATTERN; g_needRedraw = true; break;
case ACT_PLAY:
sequencerStart(true); g_needRedraw = true; break;
case ACT_CLR:
memset(&g_patterns[g_curPattern], 0, sizeof(Pattern));
uiStatus("PATTERN CLEARED"); g_needRedraw = true; break;
case ACT_BPM_DN:
if (g_curPage == PAGE_SONG) {
if (g_curProject > 0) g_curProject--;
uiStatus("PROJECT");
} else if (g_curOctave > 1) g_curOctave--;
g_needRedraw = true; break;
case ACT_BPM_UP:
if (g_curPage == PAGE_SONG) {
if (g_curProject < NUM_PROJECT_SLOTS - 1) g_curProject++;
uiStatus("PROJECT");
} else if (g_curOctave < 7) g_curOctave++;
g_needRedraw = true; break;
case ACT_SONG: // long SONG = resample the mix
if (g_playing) resampleArm();
else uiStatus("PLAY, THEN HOLD");
break;
case ACT_AUX: // long AUX = mic record
if (micRecStart(g_curDrumLane)) g_recPadKc = (uint8_t)'.';
else uiStatus("MIC BUSY");
break;
default: break;
}
}
// ---------- piano / pads (key-down, latency-critical path) ----------
static void doPiano(uint8_t kc, const KeySnap& now) {
if (g_curPage != PAGE_PATTERN && g_curPage != PAGE_SOUND) return;
if (g_curTrack == NUM_SYNTHS) {
int8_t lane = padLane(kc);
if (lane < 0) return;
g_curDrumLane = (uint8_t)lane;
if (resamplePending()) { resampleCommit(lane); return; }
if (g_curPage == PAGE_SOUND) triggerLaneLive(lane);
else liveDrumHit(lane);
g_needRedraw = true;
return;
}
int8_t semi = pianoSemi(kc);
if (semi < 0) return;
uint8_t note, oct; semiToNote(semi, note, oct);
bool accent = accentHeld(now);
if (g_curPage == PAGE_SOUND) {
g_synths[g_curTrack].noteOn(noteToFreq(note, oct), accent, false); // audition
} else {
bool legato = heldPianoCount(s_prev) >= 1;
liveSynthNote(g_curTrack, note, oct, accent, legato);
}
}
// ---------- main entry ----------
void inputInit() {
s_prev = KeySnap();
s_nHolds = 0; s_rptAct = ACT_NONE;
g_holdProg = 0; g_holdLabel[0] = 0;
}
void inputUpdate() {
M5Cardputer.update();
uint32_t nowMs = millis();
// -- CLR held while live-recording = erase at playhead --
if (g_playing && g_recEnabled && s_prev.has('z')) clearStepAtPlayhead();
// -- auto-repeat --
if (s_rptAct != ACT_NONE && s_prev.has(s_rptKc) && nowMs >= s_rptNext) {
s_rptCount++;
doImmediate(s_rptAct, s_prev);
s_rptNext = nowMs + RPT_RATE_MS;
}
// -- long-press firing + progress bar (runs even without key changes) --
float prog = 0.0f; const char* lbl = "";
for (uint8_t i = 0; i < s_nHolds; i++) {
Hold& h = s_holds[i];
uint32_t held = nowMs - h.t0;
// CLR in erase-mode never fires short/long
if (h.act == ACT_CLR && g_playing && g_recEnabled) { h.fired = true; continue; }
if (!h.fired && held >= LONG_PRESS_MS) { h.fired = true; doLong(h.act); }
if (!h.fired && held >= HINT_AFTER_MS) {
float p = (float)(held - HINT_AFTER_MS) / (LONG_PRESS_MS - HINT_AFTER_MS);
if (p > prog) { prog = p; lbl = actLongName(h.act); }
}
}
// chord: LOAD + SAVE held simultaneously = DEMO
{
int iLoad = -1, iSave = -1;
for (uint8_t i = 0; i < s_nHolds; i++) {
if (s_holds[i].act == ACT_LOAD && !s_holds[i].fired) iLoad = i;
if (s_holds[i].act == ACT_SAVE && !s_holds[i].fired) iSave = i;
}
if (iLoad >= 0 && iSave >= 0) {
s_holds[iLoad].fired = true;
s_holds[iSave].fired = true;
loadDemoPattern();
uiStatus("DEMO");
g_needRedraw = true;
prog = 0; lbl = "";
}
}
// during mic capture the footer bar is the level meter (mic_sampler owns it)
if (!micRecActive() && prog != g_holdProg) {
static uint32_t lastAnim = 0;
g_holdProg = prog;
strncpy(g_holdLabel, lbl, sizeof(g_holdLabel) - 1);
g_holdLabel[sizeof(g_holdLabel) - 1] = 0;
if (nowMs - lastAnim > 40) { lastAnim = nowMs; g_needRedraw = true; }
}
if (!M5Cardputer.Keyboard.isChange()) return;
// -- build snapshot --
Keyboard_Class::KeysState st = M5Cardputer.Keyboard.keysState();
KeySnap now;
if (st.fn) now.add(KC_FN);
if (st.shift) now.add(KC_SHIFT);
if (st.ctrl) now.add(KC_CTRL);
if (st.opt) now.add(KC_OPT);
if (st.alt) now.add(KC_ALT);
if (st.tab) now.add(KC_TAB);
if (st.del) now.add(KC_DEL);
if (st.enter) now.add(KC_ENTER);
if (st.space) now.add(KC_SPACE);
for (auto k : st.word) now.add((uint8_t)normalizeKey(k));
// -- newly pressed --
for (uint8_t i = 0; i < now.n; i++) {
uint8_t kc = now.codes[i];
if (s_prev.has(kc)) continue;
// piano/pads take priority over any action bound to the same key
if (pianoSemi(kc) >= 0 &&
(g_curPage == PAGE_PATTERN || g_curPage == PAGE_SOUND)) {
doPiano(kc, now);
continue;
}
uint8_t act = keyAction(kc);
if (act == ACT_NONE) continue;
if (actImmediate(act)) {
doImmediate(act, now);
if (actRepeats(act)) {
s_rptAct = act; s_rptKc = kc;
s_rptNext = nowMs + RPT_DELAY_MS; s_rptCount = 0;
}
} else if (s_nHolds < 8) {
s_holds[s_nHolds++] = { kc, act, nowMs, false };
}
}
// -- mic capture ends when AUX is released --
if (g_recPadKc != KC_NONE && !now.has(g_recPadKc)) {
g_recPadKc = KC_NONE;
micRecStop();
}
// -- released --
for (uint8_t i = 0; i < s_nHolds; ) {
if (!now.has(s_holds[i].kc)) {
if (!s_holds[i].fired) doShort(s_holds[i].act);
s_holds[i] = s_holds[--s_nHolds];
} else i++;
}
if (s_rptAct != ACT_NONE && !now.has(s_rptKc)) { s_rptAct = ACT_NONE; s_rptCount = 0; }
s_prev = now;
}