-
Notifications
You must be signed in to change notification settings - Fork 1
/
tapsync.ino
289 lines (217 loc) · 6.75 KB
/
tapsync.ino
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
/**
* Attribution:
* http://fuzzysynth.blogspot.com/2015/06/digitech-jam-man.html
* https://github.com/Calde-github/Looperino/blob/master/Looper.ino
* http://freestompboxes.org/viewtopic.php?p=271340#p271340
*
* Huge thanks to the above for the invaluable resource of decoding the JamSync signal
*/
#define DEBUG false
#define OPEN HIGH
#define CLOSED LOW
#define ON HIGH
#define OFF LOW
// JamSync signal packets
const byte JM_LINK[] {0xF0, 0x00, 0x00, 0x10, 0x7F, 0x62, 0x01, 0x00, 0x01, 0x01, 0xF7};
const byte JM_SYNC[] {0xF0, 0x00, 0x00, 0x10, 0x7F, 0x62, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x04, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF7};
const byte JM_STOP[] {0x00, 0xF0, 0x00, 0x00, 0x10, 0x7F, 0x62, 0x01, 0x4A, 0x01, 0x04, 0x46, 0x30, 0x42, 0x02, 0x04, 0x40, 0x03, 0x4E, 0x46, 0x2E, 0x40, 0x04, 0x5C, 0xF7};
// Config
const HardwareSerial *IO = &Serial;
const int BUTTON_PIN = 2;
const int LED_PIN = 3;
const int DEBOUNCE = 20;
const int ACTIVE_THRESHOLD = 1500; // hold 1500ms for activate/deactivate
const int CLEAR_THRESHOLD = 3000; // hold 3000ms for clear
const int MIN_TAPS = 2;
const int MAX_TAP = 2000; // limit tapped in beat length to 2000ms
const int LED_DURATION = 80;
const int JM_LINK_PERIOD = 400;
const long MS_PER_MINUTE = 60000; // 1000ms/s, 60s/min
const int BEATS_PER_MEASURE = 4;
volatile bool active = false;
volatile bool activePressed = false; // block against calling activate() more than once per tap & hold
volatile bool clearedOnce = false; // block against calling clear() more than once per tap & hold
volatile bool previousButtonState = false;
volatile int lit = 0;
volatile byte pressed = OPEN;
volatile long beatMs = 0;
volatile long bpm = 0;
volatile int quarterCount = 0;
volatile int tapCount = 0;
volatile long holdTime = 0;
volatile long lastTapTime = 0;
volatile long prevBpmLoop = 0;
volatile long prevLinkLoop = 0;
volatile long prevTapLoop = 0;
// Utility Methods
// Starts and stops a loop
void activate() {
active = !active;
activePressed = true;
// was just deactivated, we should stop the loop
if (!active && !DEBUG) {
IO->write(JM_STOP, sizeof(JM_STOP));
}
}
// Clears the currently set BPM and deactivates the loop
void clear() {
active = false;
beatMs = 0;
bpm = 0;
clearedOnce = true;
quarterCount = 0;
tapCount = 0;
if (!DEBUG) {
IO->write(JM_STOP, sizeof(JM_STOP));
}
}
bool getLongHold() {
return pressed == CLOSED && holdTime >= CLEAR_THRESHOLD;
}
bool getShortHold() {
return pressed == CLOSED && holdTime >= ACTIVE_THRESHOLD && holdTime < CLEAR_THRESHOLD;
}
void syncSend() {
if (DEBUG) {
IO->println("sending sync packet with new bpm: " + String(bpm));
return;
}
// Copy base JM sync packet
int syncPacketSize = sizeof(JM_SYNC);
byte syncPacket[syncPacketSize];
for (int i = 0; i < syncPacketSize; i++) syncPacket[i] = JM_SYNC[i];
// BPM
syncPacket[7] = 66 + 8 * ((63 < bpm) && (bpm < 128) || bpm > 191) ;
syncPacket[11] = (4 * bpm > 127 && 4 * bpm < 256) * (4 * bpm - 128) +
(2 * bpm > 127 && 2 * bpm < 256) * (2 * bpm - 128) +
(bpm > 127 && bpm < 256) * (bpm - 128);
syncPacket[12] = 1 * (bpm > 127) + 66;
// Measure length
unsigned long loopTime = beatMs * BEATS_PER_MEASURE;
int x = floor(log(loopTime / 2000.0) / log(4.0));
int b163 = (loopTime / (2000.0 * pow(4.0, x))) > 2;
int y = 2 * pow(2, b163) * pow(4, x);
int w = floor(loopTime / y);
syncPacket[15] = 64 + 8 * b163;
syncPacket[20] = 64 + x;
syncPacket[19] = 128 * (0.001 * w - 1);
syncPacket[18] = pow(128.0, 2) * (0.001 * w - 1 - syncPacket[19] / 128.0);
syncPacket[17] = pow(128.0, 3) * (0.001 * w - 1 - syncPacket[19] / 128.0 - syncPacket[18] / pow(128.0, 2));
// Command (SYNC)
syncPacket[21] = 5;
// Checksum XOR
byte z = 0;
for (int i = 7; i < 22; i++) z = z ^ syncPacket[i];
syncPacket[22] = z;
IO->write(syncPacket, syncPacketSize);
}
// Assigns a new BPM value
void setBpm(long lastTime, long currentTime) {
const long beatMillis = currentTime - lastTime;
const long newBpm = floor(MS_PER_MINUTE / beatMillis);
beatMs = beatMillis;
bpm = newBpm;
if (!active) {
active = true;
}
syncSend();
}
// Lifecycle
void blinkLED() {
if (active && (millis() - prevBpmLoop) < LED_DURATION) {
if (quarterCount == 0) {
lit = 255; // blink brightly on the downbeat of a measure
} else {
lit = 10;
}
} else if (pressed == CLOSED) {
lit = 255;
} else if (getShortHold()) {
lit = 40;
} else if (getLongHold()) {
lit = 20;
} else {
lit = 0;
}
analogWrite(LED_PIN, lit);
}
void bpmSend() {
if (!active) return;
const long now = millis();
if (now - prevBpmLoop < beatMs) return;
quarterCount = (quarterCount + 1) % BEATS_PER_MEASURE;
// every downbeat of a measure, send sync
if (quarterCount == 0) syncSend();
prevBpmLoop = now;
}
void handleButtonHold() {
holdTime = millis() - lastTapTime;
// we don't want to reactivate if we don't have a bpm or beatMs set
if (getShortHold() && !activePressed && bpm != 0 && beatMs != 0) {
if (DEBUG) IO->println("activating/deactivating");
// TODO: flash once
activate();
}
if (getLongHold() && !clearedOnce) {
if (DEBUG) IO->println("clearing");
// TODO: flash twice
clear();
}
}
void handleButtonInput() {
const long now = millis();
if (now - prevTapLoop < DEBOUNCE) return;
const bool currentButtonState = digitalRead(BUTTON_PIN);
pressed = currentButtonState;
prevTapLoop = now;
if (previousButtonState != currentButtonState) {
activePressed = false;
clearedOnce = false;
previousButtonState = currentButtonState;
if (pressed == CLOSED) {
handleButtonTap();
}
} else {
if (pressed == CLOSED) {
handleButtonHold();
}
}
}
void handleButtonTap() {
const long now = millis();
const bool resetTaps = now - lastTapTime > MAX_TAP && tapCount >= 0;
tapCount = resetTaps ? 0 : tapCount + 1;
if (tapCount >= MIN_TAPS) {
setBpm(lastTapTime, now);
}
lastTapTime = now;
}
void linkMaintain() {
const long now = millis();
if (now - prevLinkLoop < JM_LINK_PERIOD) return;
if (DEBUG) {
IO->println("maintaining JM link");
} else {
IO->write(JM_LINK, sizeof(JM_LINK));
}
prevLinkLoop = millis();
}
void setup() {
pinMode(LED_PIN, OUTPUT);
analogWrite(LED_PIN, lit);
pinMode(BUTTON_PIN, INPUT);
IO->begin(DEBUG
? 9600 // USB serial debug rate
: 31250); // JamSync serial rate
linkMaintain();
}
void loop() {
// handle raw button input, debounced to ~100ms
handleButtonInput();
// blink LEDs with bpm
blinkLED();
// send a JamSync signal link every ~400ms
linkMaintain();
// send sync packet with BPM every measure (4x bpm)
bpmSend();
}