-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_generator.h
266 lines (223 loc) · 7.09 KB
/
pattern_generator.h
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
// Copyright 2011 Emilie Gillet.
//
// Author: Emilie Gillet ([email protected])
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// -----------------------------------------------------------------------------
//
// Pattern generator.
//
// OUTPUT MODE OUTPUT CLOCK BIT7 BIT6 BIT5 BIT4 BIT3 BIT2 BIT1 BIT0
// DRUMS FALSE RND CLK HHAC SDAC BDAC HH SD BD
// DRUMS TRUE RND CLK CLK BAR ACC HH SD BD
// EUCLIDEAN FALSE RND CLK RST3 RST2 RST1 EUC3 EUC2 EUC1
// EUCLIDEAN TRUE RND CLK CLK STEP RST EUC3 EUC2 EUC1
#ifndef GRIDS_PATTERN_GENERATOR_H_
#define GRIDS_PATTERN_GENERATOR_H_
#include <string.h>
#include "avrlib/base.h"
#include "avrlib/random.h"
#include "grids/hardware_config.h"
namespace grids {
const uint8_t kNumParts = 3;
const uint8_t kPulsesPerStep = 3; // 24 ppqn ; 8 steps per quarter note.
const uint8_t kStepsPerPattern = 32;
const uint8_t kPulseDuration = 48; // 48 ticks of the main clock, roughly 6ms (default is 8 ticks, 1ms)
struct DrumsSettings {
uint8_t x;
uint8_t y;
uint8_t randomness;
};
struct PatternGeneratorSettings {
union Options {
DrumsSettings drums;
uint8_t euclidean_length[kNumParts];
} options;
uint8_t density[kNumParts];
};
enum OutputMode {
OUTPUT_MODE_EUCLIDEAN,
OUTPUT_MODE_DRUMS
};
enum ClockResolution {
CLOCK_RESOLUTION_4_PPQN,
CLOCK_RESOLUTION_8_PPQN,
CLOCK_RESOLUTION_24_PPQN,
CLOCK_RESOLUTION_LAST
};
enum OutputBits {
OUTPUT_BIT_COMMON = 0x08,
OUTPUT_BIT_CLOCK = 0x10,
OUTPUT_BIT_RESET = 0x20
};
struct Options {
ClockResolution clock_resolution;
OutputMode output_mode;
bool output_clock;
bool tap_tempo;
bool gate_mode;
bool swing;
uint8_t pack() const {
uint8_t byte = clock_resolution;
if (!swing) {
byte |= 0x08;
}
if (tap_tempo) {
byte |= 0x10;
}
if (output_clock) {
byte |= 0x20;
}
if (output_mode == OUTPUT_MODE_DRUMS) {
byte |= 0x40;
}
if (!gate_mode) {
byte |= 0x80;
}
return byte;
}
void unpack(uint8_t byte) {
tap_tempo = byte & 0x10;
output_clock = byte & 0x20;
output_mode = byte & 0x40 ? OUTPUT_MODE_DRUMS : OUTPUT_MODE_EUCLIDEAN;
gate_mode = !(byte & 0x80);
swing = !(byte & 0x08);
clock_resolution = static_cast<ClockResolution>(byte & 0x7);
if (clock_resolution >= CLOCK_RESOLUTION_24_PPQN) {
clock_resolution = CLOCK_RESOLUTION_24_PPQN;
}
}
};
class PatternGenerator {
public:
PatternGenerator() { }
~PatternGenerator() { }
static inline void Init() {
LoadSettings();
Reset();
}
static inline void Reset() {
step_ = 0;
pulse_ = 0;
memset(euclidean_step_, 0, sizeof(euclidean_step_));
}
static inline void Retrigger() {
Evaluate();
}
static inline void TickClock(uint8_t num_pulses) {
Evaluate();
beat_ = (step_ & 0x7) == 0;
first_beat_ = step_ == 0;
pulse_ += num_pulses;
// Wrap into ppqn steps.
while (pulse_ >= kPulsesPerStep) {
pulse_ -= kPulsesPerStep;
if (!(step_ & 1)) {
for (uint8_t i = 0; i < kNumParts; ++i) {
++euclidean_step_[i];
}
}
++step_;
}
// Wrap into step sequence steps.
if (step_ >= kStepsPerPattern) {
step_ -= kStepsPerPattern;
}
}
static inline uint8_t state() {
return state_;
}
static inline uint8_t step() { return step_; }
static inline bool swing() { return options_.swing; }
static int8_t swing_amount();
static inline bool output_clock() { return options_.output_clock; }
static inline bool tap_tempo() { return options_.tap_tempo; }
static inline bool gate_mode() { return options_.gate_mode; }
static inline OutputMode output_mode() { return options_.output_mode; }
static inline ClockResolution clock_resolution() { return options_.clock_resolution; }
static void set_swing(uint8_t value) { options_.swing = value; }
static void set_output_clock(uint8_t value) { options_.output_clock = value; }
static void set_tap_tempo(uint8_t value) { options_.tap_tempo = value; }
static void set_output_mode(uint8_t value) {
options_.output_mode = static_cast<OutputMode>(value);
}
static void set_clock_resolution(uint8_t value) {
if (value >= CLOCK_RESOLUTION_24_PPQN) {
value = CLOCK_RESOLUTION_24_PPQN;
}
options_.clock_resolution = static_cast<ClockResolution>(value);
}
static void set_gate_mode(bool gate_mode) {
options_.gate_mode = gate_mode;
}
static inline void IncrementPulseCounter() {
++pulse_duration_counter_;
// Zero all pulses after 1ms.
if (pulse_duration_counter_ >= kPulseDuration && !options_.gate_mode) {
state_ = 0;
// Possible mod: the extra random pulse is not reset, and its behaviour
// is more similar to that of a S&H.
//state_ &= 0x80;
}
}
static inline void ClockFallingEdge() {
if (options_.gate_mode) {
state_ = 0;
}
}
static inline PatternGeneratorSettings* mutable_settings() {
return &settings_;
}
static bool on_first_beat() { return first_beat_; }
static bool on_beat() { return beat_; }
static bool factory_testing() { return factory_testing_ < 5; }
static void SaveSettings();
static inline uint8_t led_pattern() {
uint8_t result = 0;
if (state_ & 1) {
result |= LED_BD;
}
if (state_ & 2) {
result |= LED_SD;
}
if (state_ & 4) {
result |= LED_HH;
}
return result;
}
private:
static void LoadSettings();
static void Evaluate();
static void EvaluateEuclidean();
static void EvaluateDrums();
static uint8_t ReadDrumMap(
uint8_t step,
uint8_t instrument,
uint8_t x,
uint8_t y);
static Options options_;
static uint8_t pulse_;
static uint8_t step_;
static uint8_t euclidean_step_[kNumParts];
static bool first_beat_;
static bool beat_;
static uint8_t state_;
static uint8_t part_perturbation_[kNumParts];
static uint8_t pulse_duration_counter_;
static uint8_t factory_testing_;
static PatternGeneratorSettings settings_;
DISALLOW_COPY_AND_ASSIGN(PatternGenerator);
};
extern PatternGenerator pattern_generator;
} // namespace grids
#endif // GRIDS_PATTERN_GENERATOR_H_