-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapcKey25.cpp
More file actions
274 lines (244 loc) · 8.05 KB
/
apcKey25.cpp
File metadata and controls
274 lines (244 loc) · 8.05 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
#define log_name "apc25"
#include "apcKey25.h"
#include "input_usb.h"
#include "usbMidi.h"
#include <circle/logger.h>
#include <circle/timer.h>
apcKey25 *pTheAPC = 0;
apcKey25::apcKey25() : m_shift(false), m_cmdReady(false), m_cmdType(ApcCmd::NONE), m_cmdArg(0), m_nowMs(0), m_bootMs(0), m_lastLedMs(0)
{
pTheAPC = this;
for (int i = 0; i < LOOPER_NUM_TRACKS; i++)
{
m_col1HoldStart[i] = 0;
m_col1Held[i] = false;
m_col1EraseTriggered[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);
}
// Col 0: rec/play/overdub — red=recording, yellow=overdub or pending, green=playing
u8 apcKey25::_trackLedColor(int track)
{
if (track >= LOOPER_NUM_TRACKS) return APC_VEL_LED_OFF;
publicTrack *pTrack = pTheLooper->getPublicTrack(track);
u16 ts = pTrack->getTrackState();
if (ts & TRACK_STATE_RECORDING)
return pTrack->getNumRecordedClips() > 0 ? APC_VEL_LED_YELLOW : APC_VEL_LED_RED;
if (ts & (TRACK_STATE_PENDING_RECORD | TRACK_STATE_PENDING_PLAY | TRACK_STATE_PENDING_STOP))
return APC_VEL_LED_YELLOW;
if (ts & TRACK_STATE_PLAYING)
return APC_VEL_LED_GREEN;
return APC_VEL_LED_OFF;
}
// Col 1: green=has clips unmuted, red=muted, off=empty
u8 apcKey25::_muteLedColor(int track)
{
if (track >= LOOPER_NUM_TRACKS) return APC_VEL_LED_OFF;
publicTrack *pTrack = pTheLooper->getPublicTrack(track);
int layers = pTrack->getNumRecordedClips();
if (layers == 0) return APC_VEL_LED_OFF;
bool stopped = (pTrack->getTrackState() & TRACK_STATE_STOPPED) != 0;
u8 color = APC_VEL_LED_GREEN;
if (layers >= 3) color = APC_VEL_LED_RED;
else if (layers >= 2) color = APC_VEL_LED_YELLOW;
if (stopped) color++;
return color;
}
void apcKey25::_updateGridLeds()
{
for (int row = 0; row < LOOPER_NUM_TRACKS; row++)
{
u8 col0 = _trackLedColor(row);
u8 col1 = _muteLedColor(row);
_sendLed(_padNote(row, 0), col0);
_sendLed(_padNote(row, 1), col1);
}
for (int track = 0; track < LOOPER_NUM_TRACKS; track++)
{
int col = 2 + track;
if (col >= APC_COLS - 1) break;
publicTrack *pTrack = pTheLooper->getPublicTrack(track);
u32 tpeak = pTrack->m_peakLevel;
pTrack->m_peakLevel = 0;
int tvu = 0;
if (tpeak > 50) tvu = 1;
if (tpeak > 200) tvu = 2;
if (tpeak > 1000) tvu = 3;
if (tpeak > 4000) tvu = 4;
if (tpeak > 10000) tvu = 5;
for (int row = 0; row < APC_ROWS; row++)
{
u8 color = APC_VEL_LED_OFF;
if (row < tvu)
color = (row >= 4) ? APC_VEL_LED_RED : APC_VEL_LED_GREEN;
_sendLed(_padNote(row, col), color);
}
}
u32 peak = AudioInputUSB::s_peakLevel;
AudioInputUSB::s_peakLevel = 0;
int inVu = 0;
if (peak > 100) inVu = 1;
if (peak > 500) inVu = 2;
if (peak > 2000) inVu = 3;
if (peak > 5000) inVu = 4;
if (peak > 10000) inVu = 5;
u32 outPeak = pTheLooper->m_outputPeakLevel;
pTheLooper->m_outputPeakLevel = 0;
int outVu = 0;
if (outPeak > 50) outVu = 1;
if (outPeak > 200) outVu = 2;
if (outPeak > 1000) outVu = 3;
if (outPeak > 4000) outVu = 4;
if (outPeak > 10000) outVu = 5;
for (int i = 0; i < 5; i++)
{
u8 color = APC_VEL_LED_OFF;
if (i < inVu)
color = (i >= 4) ? APC_VEL_LED_RED : APC_VEL_LED_GREEN;
else if (i < outVu)
color = (i >= 4) ? APC_VEL_LED_RED : APC_VEL_LED_YELLOW;
_sendLed(0x52 + i, color);
}
bool running = pTheLooper->getRunning();
u16 pending = pTheLooper->getPendingCommand();
_sendLed(APC_BTN_STOP_ALL, running ? (pending == LOOP_COMMAND_STOP ? APC_VEL_LED_YELLOW : APC_VEL_LED_GREEN) : APC_VEL_LED_OFF);
_sendLed(APC_BTN_RECORD, pTheLooper->getDubMode() ? APC_VEL_LED_RED : APC_VEL_LED_OFF);
_sendLed(APC_BTN_PLAY, m_shift ? APC_VEL_LED_YELLOW : APC_VEL_LED_OFF);
}
// Safe to call from interrupt — only sets flags
void apcKey25::_queueCmd(ApcCmd::Type type, int arg)
{
m_cmdType = type;
m_cmdArg = arg;
m_cmdReady = true;
}
void apcKey25::_onPadPress(int row, int col)
{
if (row >= LOOPER_NUM_TRACKS) return;
if (col == 0)
{
_queueCmd(ApcCmd::TRACK, row);
}
else if (col == 1)
{
m_col1Held[row] = true;
m_col1EraseTriggered[row] = false;
m_col1HoldStart[row] = m_nowMs;
}
else if (col >= 2 && col <= 5)
{
int layer = col - 2;
_queueCmd(ApcCmd::LOOPER, LOOP_COMMAND_CLEAR_LAYER_BASE + row * LOOPER_NUM_LAYERS + layer);
}
else if (col == 6)
{
_queueCmd(ApcCmd::LOOPER, LOOP_COMMAND_HALVE_TRACK_BASE + row);
}
else if (col == 7)
{
_queueCmd(ApcCmd::LOOPER, LOOP_COMMAND_DOUBLE_TRACK_BASE + row);
}
}
void apcKey25::_onPadRelease(int row, int col)
{
if (col == 1 && row < LOOPER_NUM_TRACKS)
{
if (m_col1Held[row] && !m_col1EraseTriggered[row])
{
publicTrack *pTrack = pTheLooper->getPublicTrack(row);
if (pTrack->getNumRunningClips())
_queueCmd(ApcCmd::STOP_TRACK, row);
else
_queueCmd(ApcCmd::ERASE_TRACK, row);
}
m_col1Held[row] = false;
}
}
void apcKey25::_onButton(u8 note)
{
if (note == APC_BTN_STOP_ALL)
_queueCmd(ApcCmd::LOOPER, m_shift ? LOOP_COMMAND_STOP_IMMEDIATE : LOOP_COMMAND_STOP);
else if (note == APC_BTN_RECORD)
_queueCmd(ApcCmd::LOOPER, m_shift ? LOOP_COMMAND_ABORT_RECORDING : LOOP_COMMAND_DUB_MODE);
else if (note == APC_BTN_PLAY)
_queueCmd(ApcCmd::LOOPER, m_shift ? LOOP_COMMAND_LOOP_IMMEDIATE : LOOP_COMMAND_CLEAR_ALL);
}
void apcKey25::handleMidi(u8 status, u8 data1, u8 data2)
{
u8 msgType = status & 0xF0;
if (msgType == APC_CH_NOTE_ON && data2 > 0)
{
if (data1 == APC_BTN_SHIFT) { m_shift = true; 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 (data1 < APC_ROWS * APC_COLS)
_onPadRelease(data1 / APC_COLS, data1 % APC_COLS);
return;
}
}
void apcKey25::update()
{
if (!pTheLooper) return;
m_nowMs = CTimer::Get()->GetClockTicks() / 1000;
// Record boot time on first call
if (m_bootMs == 0) m_bootMs = m_nowMs;
// Dispatch queued command from interrupt
if (m_cmdReady)
{
m_cmdReady = false;
ApcCmd::Type type = m_cmdType;
int arg = m_cmdArg;
if (type == ApcCmd::TRACK)
{
u16 ts = pTheLooper->getPublicTrack(arg)->getTrackState();
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::ERASE_TRACK)
{
pTheLooper->command(LOOP_COMMAND_ERASE_TRACK_BASE + arg);
}
else if (type == ApcCmd::LOOPER)
{
pTheLooper->command(arg);
}
}
// Hold-to-erase on col1
for (int row = 0; row < LOOPER_NUM_TRACKS; row++)
{
if (m_col1Held[row] && !m_col1EraseTriggered[row])
{
if (m_nowMs - m_col1HoldStart[row] >= APC_HOLD_ERASE_MS)
{
m_col1EraseTriggered[row] = true;
m_col1Held[row] = false;
pTheLooper->command(LOOP_COMMAND_ERASE_TRACK_BASE + row);
}
}
}
// Wait for APC25 to finish booting before sending LEDs
if (m_nowMs - m_bootMs < APC_LED_BOOT_DELAY_MS) return;
// Throttle LED updates to ~30fps
if (m_nowMs - m_lastLedMs < 33) return;
m_lastLedMs = m_nowMs;
_updateGridLeds();
}