-
Notifications
You must be signed in to change notification settings - Fork 2
/
STM32-drum.ino
137 lines (106 loc) · 3.37 KB
/
STM32-drum.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
#include "wavetables16.h"
#include "GPIOWriteFast.h"
#include <ADCTouchSensor.h>
#include "globals.h"
#include "patterns.h"
#include "comms.h"
#include "controls.h"
#include "timers.h"
//--------- Setup ----------//
void setup() {
Serial.begin(115200);
setupSPI();
setupTimers();
setupControls();
delay(1000);
}
//--------- Write Buffer and sequence ----------//
void play() {
// everything here optimized for processing speed not code quality - inline faster?
/* -------write sample buffer ------------ */
int32_t sampleTotal = 0; // has to be signed
uint_fast8_t stepCount = 0;
uint32_t tempoCount = 1;
sampleCount[NUM_SAMPLES] = { 0 };
uint16_t MasterSend;
byte MasterReceive;
uint32_t receiveCount = 1;
while(1) {
if (RingCount < BUFFERSIZE_M1) { // if space in ringbuffer
sampleTotal = 0;
for (uint8_t i = 0; i < NUM_SAMPLES; i++) {
if (sampleCount[i]) {
int16_t sample = (wavetables16[i][samplePointer[i]++]) - (1 << 15);//) >> 8;
sample = ((sample * gain[i]) >> 8);
sampleTotal += sample; //
sampleCount[i] --;
}
}
// // hard clip - must be faster than `constrain()`?
// if (sampleTotal < -(1 << 15))
// sampleTotal = -(1 << 15);
// if (sampleTotal > (1 << 15))
// sampleTotal = (1 << 15);
Ringbuffer[RingWrite] = (sampleTotal + (1 << 15));
RingWrite++;
RingCount++;
}
/* -------LED------------------ */
if (tempoCount >= 1 << 17) {
fastWrite(LED, 0);
} else {
fastWrite(LED, 1);
}
/* -------sequencer------------ */
if (MODE == 1) {
if (!(tempoCount--)) { // every "tempo" ticks, do the thing
tempoCount = tempo; // set it back to the tempo ticks
readTouch(); // read touch buttons
MasterReceive = WriteSPI('s', stepCount, livePattern[stepCount]);
trigger = livePattern[stepCount++];
//Serial.print("RECORD ");Serial.print(RECORD);Serial.print(" -- ");Serial.println(buttonTrigger);
// record mode- write pattern basis triggered button
if (RECORD && (buttonTrigger != B00000000)) {
livePattern[stepCount] ^= buttonTrigger;
buttonTrigger = B00000000;
}
if (stepCount > patternLength) stepCount = 0;
// read the pattern bytes, each bit triggers a sample
for (uint8_t i = 0; i < NUM_SAMPLES; i++) {
if (trigger & 1<<i) {
samplePointer[i] = 0;
sampleCount[i] = wavetableLengths16[i]; // number of bytes in sample
}
}
}
}
/* -------player------------ */
else if (MODE == 0){
stepCount = 0;
tempoCount = 1;
for (uint8_t i = 0; i < NUM_SAMPLES; i++) {
if (buttonTrigger & 1<<i) {
//Serial.println("trig");
samplePointer[i] = 0;
sampleCount[i] = wavetableLengths16[i]; // number of bytes in sample
}
}
if (!(receiveCount--)) { // every "tempo" ticks, do the thing
receiveCount = receiveRate; // set it back to the tempo ticks
MasterReceive = WriteSPI('t', 0, 0);
Serial.print("T: ");Serial.println(MasterReceive);
}
}
else if (MODE == 2) {
}
else {
stepCount = 0;
tempoCount = 1;
}
}
/* ----------------------------- */
}
//--------- Main Loop ----------//
void loop() {
play();
}