-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsequencer.cpp
More file actions
254 lines (222 loc) · 9.06 KB
/
Copy pathsequencer.cpp
File metadata and controls
254 lines (222 loc) · 9.06 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
// ============================================================
// CardputerGroovebox - sequencer.cpp
// ============================================================
#include "sequencer.h"
#include <Arduino.h>
#include <string.h>
Pattern g_patterns[NUM_PATTERNS];
uint8_t g_song[SONG_LENGTH];
uint8_t g_songLoopStart = 0;
SynthTrack g_synths[NUM_SYNTHS];
DrumLane g_drumLanes[NUM_DRUM_LANES];
bool g_synthMute[NUM_SYNTHS] = {false, false, false};
bool g_drumMute = false;
volatile bool g_playing = false;
bool g_recEnabled = false;
bool g_songMode = false;
uint8_t g_playStep = 0;
uint8_t g_playPattern = 0;
uint8_t g_songPos = 0;
uint16_t g_bpm = 128;
uint8_t g_curPattern = 0;
uint8_t g_curTrack = 0;
uint8_t g_curStep = 0;
uint8_t g_curDrumLane = 0;
uint8_t g_curOctave = 4;
extern bool g_needRedraw; // owned by ui.cpp
static uint32_t s_stepUs = 0;
static uint32_t s_lastStepUs = 0;
static uint32_t s_stepPeriod() { return 60000000UL / g_bpm / 4; } // 16th notes
void sequencerInit() {
memset(g_patterns, 0, sizeof(g_patterns));
memset(g_song, SONG_EMPTY, sizeof(g_song));
for (int s = 0; s < NUM_SYNTHS; s++) g_synths[s].init();
g_synths[0].forEach([](SynthVoice& v){ v.oscMode = OSC_SAW; v.fltCutoff = 0.30f;
v.fltReso = 0.45f; v.fltEnvAmt = 0.60f; });
g_synths[1].forEach([](SynthVoice& v){ v.oscMode = OSC_SQR; v.fltCutoff = 0.45f;
v.volume = 0.6f; });
g_synths[2].forEach([](SynthVoice& v){ v.oscMode = OSC_WT; v.wtIndex = 0;
v.fltCutoff = 0.55f; v.volume = 0.5f; });
g_synths[2].setVoices(3); // track 3 ships polyphonic (chords/pads)
// lanes 0-3: 808 KICK SNARE CHAT CLAP | lanes 4-7: 909 KICK SNARE CHAT OHAT
g_drumLanes[0].init(ENG_808, DT_KICK);
g_drumLanes[1].init(ENG_808, DT_SNARE);
g_drumLanes[2].init(ENG_808, DT_HAT_C);
g_drumLanes[3].init(ENG_808, DT_HAT_O_OR_CLAP);
g_drumLanes[4].init(ENG_909, DT_KICK);
g_drumLanes[5].init(ENG_909, DT_SNARE);
g_drumLanes[6].init(ENG_909, DT_HAT_C);
g_drumLanes[7].init(ENG_909, DT_HAT_O_OR_CLAP);
// 909 hats choke each other by default
g_drumLanes[6].chokeGroup = 1;
g_drumLanes[7].chokeGroup = 1;
}
// ---------- triggering ----------
static void triggerLane(uint8_t lane) {
if (lane >= NUM_DRUM_LANES) return;
uint8_t grp = g_drumLanes[lane].chokeGroup;
if (grp != 0) {
for (int i = 0; i < NUM_DRUM_LANES; i++)
if (i != lane && g_drumLanes[i].chokeGroup == grp)
g_drumLanes[i].choke();
}
g_drumLanes[lane].trigger();
}
static void triggerStep(uint8_t step) {
Pattern& p = g_patterns[g_playPattern];
for (int s = 0; s < NUM_SYNTHS; s++) {
if (g_synthMute[s]) continue;
const SynthCell& c = p.synth[s][step];
bool mono = (g_synths[s].voices <= 1);
for (int i = 0; i < (mono ? 1 : MAX_POLY); i++)
if (c.note[i] != NOTE_EMPTY)
g_synths[s].noteOn(noteToFreq(c.note[i], c.oct[i]),
c.accent, mono && c.slide);
}
if (!g_drumMute) {
uint8_t mask = p.drums[step];
for (int l = 0; l < NUM_DRUM_LANES; l++)
if (mask & (1 << l)) triggerLane(l);
}
}
// ---------- transport ----------
void sequencerStart(bool fromTop) {
if (fromTop) {
g_playStep = 0;
if (g_songMode) {
g_songPos = g_songLoopStart;
if (g_song[g_songPos] != SONG_EMPTY) g_playPattern = g_song[g_songPos];
}
}
if (!g_songMode) g_playPattern = g_curPattern;
s_lastStepUs = micros() - s_stepPeriod(); // fire step immediately
g_playing = true;
}
void sequencerStop() {
g_playing = false;
g_playStep = 0;
}
static void songAdvance() {
// find next non-empty slot; wrap to loop start
uint8_t start = g_songPos;
for (int i = 0; i < SONG_LENGTH; i++) {
g_songPos = (g_songPos + 1) % SONG_LENGTH;
if (g_songPos == 0 && start != SONG_LENGTH - 1) g_songPos = g_songLoopStart;
if (g_song[g_songPos] != SONG_EMPTY) { g_playPattern = g_song[g_songPos]; return; }
if (g_songPos == start) break; // nothing found
}
// fallback: stay on current
}
void sequencerTick() {
if (!g_playing) return;
uint32_t now = micros();
if (now - s_lastStepUs < s_stepPeriod()) return;
s_lastStepUs += s_stepPeriod(); // accumulate: no drift
triggerStep(g_playStep);
g_playStep++;
if (g_playStep >= NUM_STEPS) {
g_playStep = 0;
if (g_songMode) songAdvance();
else g_playPattern = g_curPattern; // pattern switch on bar
}
g_needRedraw = true;
}
// ---------- live input ----------
// quantize: current step if within first half, else next step
static uint8_t quantizedStep() {
uint32_t elapsed = micros() - s_lastStepUs;
uint8_t step = g_playStep == 0 ? NUM_STEPS - 1 : g_playStep - 1; // step just triggered
if (elapsed > s_stepPeriod() / 2) step = g_playStep; // round up
return step % NUM_STEPS;
}
void liveSynthNote(uint8_t track, uint8_t note, uint8_t octave, bool accent, bool legato) {
if (track >= NUM_SYNTHS) return;
bool poly = (g_synths[track].voices > 1);
// poly: legato means "chord", not slide
g_synths[track].noteOn(noteToFreq(note, octave), accent, !poly && legato);
if (g_recEnabled) {
if (g_playing) {
uint8_t st = quantizedStep();
SynthCell& c = g_patterns[g_playPattern].synth[track][st];
if (poly && legato && !c.empty()) { c.add(note, octave); if (accent) c.accent = true; }
else c.setMono(note, octave, accent, !poly && legato);
} else {
// step-write: keys held together stack a chord on the same step
if (poly && legato) {
uint8_t prev = (uint8_t)((g_curStep + NUM_STEPS - 1) % NUM_STEPS);
SynthCell& c = g_patterns[g_curPattern].synth[track][prev];
if (!c.empty()) { c.add(note, octave); if (accent) c.accent = true;
g_needRedraw = true; return; }
}
g_patterns[g_curPattern].synth[track][g_curStep].setMono(note, octave, accent, false);
g_curStep = (g_curStep + 1) % NUM_STEPS; // step-write advance
}
g_needRedraw = true;
}
}
void liveDrumHit(uint8_t lane) {
triggerLane(lane);
if (g_recEnabled) {
if (g_playing) {
g_patterns[g_playPattern].drums[quantizedStep()] |= (1 << lane);
} else {
g_patterns[g_curPattern].drums[g_curStep] |= (1 << lane);
g_curStep = (g_curStep + 1) % NUM_STEPS;
}
g_needRedraw = true;
}
}
// ---------- helpers for the one-key layout / sampling ----------
void clonePatternTo(uint8_t dst) {
if (dst >= NUM_PATTERNS) return;
if (dst != g_curPattern) g_patterns[dst] = g_patterns[g_curPattern];
}
void triggerLaneLive(uint8_t lane) {
triggerLane(lane);
}
// ---------- step editing ----------
void toggleDrumAtCursor(uint8_t lane) {
g_patterns[g_curPattern].drums[g_curStep] ^= (1 << lane);
g_needRedraw = true;
}
void writeSynthAtCursor(uint8_t note, uint8_t octave, bool accent) {
if (g_curTrack >= NUM_SYNTHS) return;
g_patterns[g_curPattern].synth[g_curTrack][g_curStep].setMono(note, octave, accent, false);
g_curStep = (g_curStep + 1) % NUM_STEPS;
g_needRedraw = true;
}
void clearCellAtCursor() {
if (g_curTrack < NUM_SYNTHS)
g_patterns[g_curPattern].synth[g_curTrack][g_curStep].clear(g_curOctave);
else
g_patterns[g_curPattern].drums[g_curStep] = 0;
g_needRedraw = true;
}
void clearStepAtPlayhead() {
uint8_t st = g_playStep == 0 ? NUM_STEPS - 1 : g_playStep - 1;
if (g_curTrack < NUM_SYNTHS)
g_patterns[g_playPattern].synth[g_curTrack][st].clear(g_curOctave);
else
g_patterns[g_playPattern].drums[st] = 0;
g_needRedraw = true;
}
// ---------- demo ----------
void loadDemoPattern() {
Pattern& p = g_patterns[g_curPattern];
memset(&p, 0, sizeof(p));
// acid bassline on synth 1
const uint8_t bl[NUM_STEPS] = {1,0,1,0, 4,0,1,0, 1,0,8,0, 6,0,4,0};
const bool acc[NUM_STEPS] = {1,0,0,0, 1,0,0,0, 0,0,1,0, 0,0,1,0};
const bool sld[NUM_STEPS] = {0,0,0,0, 0,0,1,0, 0,0,0,0, 1,0,0,0};
for (int i = 0; i < NUM_STEPS; i++)
if (bl[i]) p.synth[0][i].setMono(bl[i], 2, acc[i], sld[i]);
// poly chord stabs on synth 3 (Am: A-C-E), showing off VOICES=3
p.synth[2][0].setMono(10, 3, false, false); // A3
p.synth[2][0].add(1, 4); p.synth[2][0].add(5, 4); // + C4 E4
p.synth[2][8] = p.synth[2][0];
// 909 kick 4-on-floor (lane 4), 808 clap on 2&4 (lane 3), 909 hats offbeat (lane 6)
for (int i = 0; i < NUM_STEPS; i += 4) p.drums[i] |= (1 << 4);
p.drums[4] |= (1 << 3); p.drums[12] |= (1 << 3);
for (int i = 2; i < NUM_STEPS; i += 4) p.drums[i] |= (1 << 6);
for (int i = 0; i < NUM_STEPS; i += 2) p.drums[i] |= (1 << 2);
}