-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sequencer.cpp
297 lines (254 loc) · 9.16 KB
/
Sequencer.cpp
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
#include "Arduino.h"
#include "Sequencer.h"
Sequencer::Sequencer() {
};
void Sequencer::initialize(uint8_t ch, uint8_t stepCount, uint8_t beatCount, float tempo){
this->channel = ch;
this->stepCount = stepCount;
this->beatCount = beatCount;
this->tempo = tempo;
this->stepTimer = 0;
this->sequenceTimer = 0;
this->instrument = 0;
this->instType = 0;
this->volume = 100;
//for (int i=0; i < 16; i++){
// stepData[i].gateLength = 1;
// stepData[i].velocity = 127;
//// stepData[i].pitch = 24;
//};
beatLength = 60000000/tempo;
calculateStepTimers();
monophonic = true;
};
void Sequencer::initNewSequence(){
stepCount = 16;
beatCount = 4;
quantizeKey = 0;
instrument = 0;
volume = 100;
bank = 0;
instType = 2; //initialized regular instrument
for(int n=0; n < 16; n++){
stepData[n].pitch = 0;
stepData[n].gateLength = 1;
stepData[n].gateType = 0;
stepData[n].velocity = 127;
stepData[n].glide = 0;
}
};
void Sequencer::setTempo(uint16_t tempo){
this->tempo = tempo;
beatLength = 60000000/tempo;
calculateStepTimers();
}
void Sequencer::setStepPitch(uint8_t step, uint8_t pitch){
stepData[step].pitch = pitch;
};
void Sequencer::setGateLength(uint8_t step, uint8_t length){
stepData[step].gateLength = length;
calculateStepTimers();
};
void Sequencer::setStepCount(uint8_t stepCountNew){
stepCount = stepCountNew;
calculateStepTimers();
};
void Sequencer::setBeatCount(uint16_t beatCountNew){
beatCount = beatCountNew;
calculateStepTimers();
};
void Sequencer::setGateType(uint8_t step, uint8_t gate){
stepData[step].gateType = gate;
}
void Sequencer::setStepVelocity(uint8_t step, uint8_t velocity){
stepData[step].velocity = velocity;
};
void Sequencer::setStepGlide(uint8_t step, uint8_t glideTime){
stepData[step].glide = glideTime;
}
void Sequencer::calculateStepTimers(){
uint32_t noteTimerMcsCounter = 0;
stepLength = beatLength*beatCount/stepCount;
// Serial.println(" stepCount: " + String(stepCount) + " stepLength: " + String(stepLength) + " beatLength: " + String(beatLength) + " tempo: " + String(tempo));
for (int stepNum = 0; stepNum < 16; stepNum++){
stepUtil[stepNum].noteTimerMcs = (stepData[stepNum].gateLength*stepLength);
stepUtil[stepNum].beat = floor(noteTimerMcsCounter / beatLength);
stepUtil[stepNum].offset = stepNum*stepLength;
noteTimerMcsCounter = noteTimerMcsCounter + stepUtil[stepNum].noteTimerMcs;
/*
Serial.println( String(channel) + " " + String(stepNum) + " " +
"ntm: " + String(stepData[stepNum].noteTimerMcs) +
"\tbt: " + String(stepData[stepNum].beat) +
"\toff: " + String(stepData[stepNum].offset) +
"\tgaL: " + String(stepData[stepNum].gateLength) +
"\tgaT: " + String(stepData[stepNum].gateType) +
"\tptch: " + String(stepData[stepNum].pitch) +
"\tOC: " + String(noteTimerMcsCounter)
);
*/
}
}
void Sequencer::clockStart(elapsedMicros startTime){
firstBeat = true;
stepTimer = startTime;
};
void Sequencer::beatPulse(uint32_t beatLength){
// this is sent every 24 pulses received from midi clock
// and also when a play or continue command is received.
this->beatLength = beatLength;
calculateStepTimers();
beatTimer = 0;
tempoPulse = true;
if(firstBeat){
activeStep = 0;
beatTracker = 0;
firstBeat = false;
} else {
//beatTracker= positive_modulo(beatTracker + 1, beatCount);
beatTracker = (beatTracker + 1) % beatCount;
}
if (beatTracker == 0) {
for(int i = 0; i < 16; i++){
// reset the note status for notes that have been played.
// leave notes that have not been turned off yet.
// if (stepData[i].noteStatus == 4){
// stepData[i].noteStatus = 0;
// }
}
activeStep = 0;
stepTimer = 0;
if(channel == 1){
// Serial.println("Resetting Sequence Timer " + String(sequenceTimer));
}
sequenceTimer = 0;
// We are resetting the note status when the sequenceTimer is no longer larger than
// the offset value. This means that runSequence needs to run at least once before
// it can reset the note statuses. For the first note, that means it wont ever reset
// so we must reset it manually here.
stepUtil[0].noteStatus = 0;
}
};
void Sequencer::runSequence(NoteDatum *noteData){
// clear noteData from last iteration.
// if (firstBeat){
// Serial.println("firstBeat - runSequenceStart: " + String(sequenceTimer) );
// }
unsigned long timer = micros();
noteData->noteOff = false;
noteData->noteOn = false;
for(int i = 0; i < 16; i++){
noteData->noteOnArray[i] = NULL;
noteData->noteVelArray[i] = NULL;
noteData->noteOffArray[i] = NULL;
}
noteData->channel = 0;
noteData->noteOnStep = 0;
noteData->noteOffStep = 0;
if (stepTimer > stepLength) {
activeStep = positive_modulo(activeStep + 1, stepCount);
stepTimer = 0;
}
for (int stepNum = 0; stepNum < 16; stepNum++){
// set notes to be stopped, and marked as played.
if ( (stepUtil[stepNum].stepTimer > stepUtil[stepNum].noteTimerMcs) && (stepUtil[stepNum].noteStatus == 1) ){
// if the note is playing and has played out the gate length, end the note.
noteData->noteOff = true;
noteData->channel = channel;
noteData->noteOffStep = stepNum;
int n = 0;
for (int f=0; f<16; f++){
if (noteData->noteOffArray[f] == NULL){
noteData->noteOffArray[f] = stepUtil[stepNum].notePlaying;
break;
}
}
stepUtil[stepNum].noteStatus = 4;
}
// set notes to be played
if((stepData[stepNum].gateType != 0 ) ){
// if ( sequenceTimer >= stepData[stepNum].offset || firstBeat ) {
if ( sequenceTimer + 15000>= stepUtil[stepNum].offset ) {
if (stepUtil[stepNum].noteStatus == 0) {
// if (stepNum > activeStep){
// activeStep = stepNum;
// }
/* if(stepData[stepNum].pitch > 0){
Serial.println("third condition: " + String(stepNum) + "\tst:" + String(sequenceTimer) + " slsn:" +String(stepLength*stepNum) + "\tste:" + String(stepTimer) + "\tof:" + String(stepData[stepNum].offset) + "\tns:" + String(stepData[stepNum].noteStatus) + "\tgt:" + String(stepData[stepNum].gateType) + "\tnt:" + String(stepData[stepNum].noteTimerMcs) );
Serial.println("timekeeper: " + String(timekeeper));
} */
// Serial.println("Note should be played!" + String(stepNum));
//shut off any other notes that might still be playing.
for (int stepNum = 0; stepNum < 16; stepNum++){
if(stepUtil[stepNum].noteStatus == 1){
noteData->noteOff = true;
noteData->channel = channel;
noteData->noteOffStep = stepNum;
int n = 0;
for (int f=0; f<16; f++){
if (noteData->noteOffArray[f] == NULL){
noteData->noteOffArray[f] = stepUtil[stepNum].notePlaying;
break;
}
}
stepUtil[stepNum].noteStatus = 4;
}
}
noteData->noteOn = true;
noteData->channel = channel;
noteData->noteOnStep = stepNum;
noteData->triggerTime = micros();
noteData->sequenceTime = sequenceTimer;
noteData->offset = stepUtil[stepNum].offset;
stepUtil[stepNum].stepTimer = 0;
stepUtil[stepNum].noteStatus = 1;
if (quantizeKey == 1){
stepUtil[stepNum].notePlaying = quantizePitch(stepData[stepNum].pitch, aminor, 1);
Serial.println("quantized note: " + String(stepData[stepNum].pitch) + " -> " + String(stepUtil[stepNum].notePlaying));
} else {
stepUtil[stepNum].notePlaying = stepData[stepNum].pitch;
}
for (int i=0; i<16; i++){
if (noteData->noteOnArray[i] == NULL){
noteData->noteOnArray[i] = stepUtil[stepNum].notePlaying;
noteData->noteVelArray[i] = stepData[stepNum].velocity;
break;
}
}
// noteStatus indicates the status of the next note
// 0 indicates not playing, not queued
// 1 indicates the note is currently playing
// 2 indicates the note is currently queued.
// 3 indicates that the note is currently playing and currently queued
// 4 indicates that the note has been played this iteration
// stepData[activeStep].noteStatus = stepData[activeStep].pitch;
}
} else {
if (stepUtil[stepNum].noteStatus == 4){
stepUtil[stepNum].noteStatus = 0;
}
}
}
}
//timekeeper = ((micros() - timer)+9*timekeeper)/10;
}
uint8_t Sequencer::quantizePitch(uint8_t note, uint32_t scale, bool direction){
uint8_t count = 0;
while ( (0b100000000000 >> (note % 12) ) & ~scale ) {
if (direction){
note += 1;
} else {
note -= 1;
}
count += 1;
if (count > 12) {
break; // emergency break if while loop goes OOC
}
}
return note;
}
uint8_t Sequencer::getStepPitch(uint8_t step){
return stepData[step].pitch;
};
int Sequencer::positive_modulo(int i, int n) {
return (i % n + n) % n;
}