-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock.ino
226 lines (184 loc) · 7.7 KB
/
clock.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
void changeTempo(uint16_t newTempo){
tempo = newTempo;
beatLength = 60000000/tempo;
}
void masterClockFunc(){
// this timer runs once every millisecond
// Serial.println("master Clock Start");
elapsedMicros loopTimer = 0;
avgInterval =((micros() - lastMicros) + 9* avgInterval) / 10;
timerAvg = (lastTimer + 9*timerAvg) /10;
lastMicros = micros();
intervalJitter = (abs(int(avgInterval) - int(lastAvgInterval)));
avgIntervalJitter = (intervalJitter * 9 + avgIntervalJitter) / 10;
lastAvgInterval = avgInterval;
// if (printTimer > 200000) {
// Serial.println("avgInterval: " + String(avgInterval) + "\ttimerAvg: " + String(timerAvg) + "\tavgIntervalJitter: " + String(avgIntervalJitter));
// printTimer = 0;
//}
iter = iter + 1;
// Serial.println("timer loop 2");
if(playing){
if( extClock == false ){
// int clock
if (wasPlaying == false){
Serial.println("TestTimer: " + String(testTimer));
// if playing has just re-started, the master tempo timer and the master beat count must be reset
MIDI.send(Start, 0, 0, 1);
MIDI.sendSongPosition(0);
masterTempoTimer = 0;
masterPulseCount = 0;
internalClockTimer = 0;
startTime = 0;
for (int i=0; i< sequenceCount; i++){
sequence[i].clockStart(startTime);
sequence[i].beatPulse(beatLength);
sequence[i].runSequence(¬eData[i]);
}
} else {
// Serial.print(" b1 ");
for (int i=0; i< sequenceCount; i++){
sequence[i].runSequence(¬eData[i]);
}
// Serial.print(" b2 " );
}
if (internalClockTimer > 60000000/tempo){
// Serial.print(" b4 ");
if (queuePattern != currentPattern) {
changePattern(queuePattern, true, true);
}
for (int i=0; i< sequenceCount; i++){
sequence[i].beatPulse(beatLength);
}
tempoBlip = !tempoBlip;
internalClockTimer = 0;
// Serial.print(" b5 ");
}
} else {
// ext clock sync
for (int i=0; i< sequenceCount; i++){
sequence[i].runSequence(¬eData[i]);
}
}
for (int i=0; i< sequenceCount; i++){
if (noteData[i].noteOff == true){
for (int n=0; n< 16; n++){
if (noteData[i].noteOffArray[n] == NULL){
continue;
}
// noteOn(noteData[i].channel,noteData[i].noteOffArray[n]);
MIDI.sendNoteOff(noteData[i].noteOffArray[n], 64, noteData[i].channel);
sam2695.noteOff(noteData[i].channel, noteData[i].noteOffArray[n]);
// usbMIDI.sendNoteOff(noteData[i].noteOffArray[n], 64, noteData[i].channel);
//Serial.println("noteOff: " + String(noteData[i].noteOffArray[n]) + "\tbt: " + String(sequence[selectedSequence].beatTracker) ) ;
}
}
}
for (int i=0; i< sequenceCount; i++){
if (noteData[i].noteOn == true){
for (int n=0; n< 16; n++){
if (noteData[i].noteOnArray[n] == NULL){
continue;
}
// noteOn(noteData[i].channel,noteData[i].noteOnArray[n]);
//
/*
if (sequence[selectedSequence].quantizeKey == 1){
bool quantized = false;
uint32_t aminor = 0b101011010101;
uint8_t noteToPlay = noteData[i].noteOnArray[n];
uint8_t count = 0;
while ( (0b100000000000 >> (noteToPlay % 12) ) & ~aminor ) {
noteToPlay += 1;
count += 1;
if (count > 12) {
break;
Serial.println("quantize error, breaking after 12 modifications");
}
}
Serial.println("quantized note: "+ String(midiNotes[noteData[i].noteOnArray[n]]) + "to: " + String(midiNotes[noteToPlay]));
MIDI.sendNoteOn(noteToPlay, noteData[i].noteVelArray[n], noteData[i].channel);
} else {
MIDI.sendNoteOn(noteData[i].noteOnArray[n], noteData[i].noteVelArray[n], noteData[i].channel);
}
*/
for (int i=0; i<8; i++){
// ad5676.setVoltage(i, map(noteData[i].noteOnArray[n], 0,127,0,65535 ) );
}
sam2695.noteOn(noteData[i].channel, noteData[i].noteOnArray[n], 127);// noteData[i].noteVelArray[n]);
MIDI.sendNoteOn(noteData[i].noteOnArray[n], noteData[i].noteVelArray[n], noteData[i].channel);
// usbMIDI.sendNoteOn(noteData[i].noteOnArray[n], noteData[i].noteVelArray[n], noteData[i].channel);
// These lines below help to troubleshoot latency and jitter issues, but they cause crashes!
// unsigned long delta = noteData[i].sequenceTime - noteData[i].offset;
// unsigned long roundTripTime = micros() - noteData[i].triggerTime;
//
// avgDelta = (delta + roundTripTime+ 9*avgDelta)/10;
Serial.println(
"noteOn: " + String(noteData[i].noteOnArray[n])
+ "\tbt: " + String(sequence[selectedSequence].beatTracker)
+ "\tch: " + String(noteData[i].channel)
+ "\tseq: " + String(noteData[i].sequenceTime)
+ "\toff: " + String(noteData[i].offset)
// + "\tdelta: " + String(delta)
// + "\trtt: " + String(roundTripTime)
// + "\ttot: " + String(delta+roundTripTime)
// + "\tavgDelta: " + String(avgDelta)
+ "\tstartTime: " + String(startTime));
}
}
}
}
// Serial.println(" b8 ");
wasPlaying = playing;
lastTimer = loopTimer;
}
// every beat,
// run the sequencer to determine what notes
// need to be played over the next beat
// need to know which steps are going to happen
// over the next beat
// we know BPM
// we know the steps of the sequence
// and how long each is.
// beat: 0 1 2 3 4 5 6 7
// step: 0 1 2 3 4 5
// length: 1 1 2 - 2 - 1 1
/// SCENERIO 1: steps are not based on relative beats
// stepCount: 1 measure (4 beats)
// - only 3 beats are populated with notes
// - sequencer will repeat the first beat to fill the space
// if sequence length was 1/2 a measure (2 beats)
// - the step scheduler would need to determine how many steps need to be scheduled
// - only the first 6 steps would play
// length is in 1/16th notes
// beat: 0 1 2 3
// step 0 1 4 2 3 5 6 7 5
// length: 1 1 2 1 1 2 1 1 2
// stepActive: X X X - X - - X -
// stepPitch: 34 34 53 52 43 35 35 32 35
// stepVelocity: 53 51 125 53 64 12 35 59 12
/// SCENERIO 2: steps are based on beats <<<--- Gonna do this one, its easier to start with
// stepCount: 1 measure (4 beats)
// - only 3 beats are populated with notes
// - sequencer will repeat the first beat to fill the space
// if sequence length was 1/2 a measure (2 beats)
// - the step scheduler would need to determine how many steps need to be scheduled
// - only the first 6 steps would play
// length is in 1/16th notes
// beat: 0 1 2 3
// step 0 1 2 3 4 5 6 7 8 9 10
// length: 1 1 2 1 1 1 2 1 1 1 2
// stepActive: X X X - - X - - - X -
// stepPitch: 34 34 53 0 52 43 35 0 35 32 35
// stepVelocity: 53 51 125 0 53 64 12 0 35 59 12
// step scheduler routine needs to figure out what steps
//
// Timer1.setPeriod(50*knob.read());
//color++ ;//knob.read();
// color = color % 255;
/*
activeStep++;
activeStep = activeStep % numSteps;
synth.allNotesOff(0);
synth.noteOn(0, stepPitch[activeStep], 64);
*/