u/lil2scuzzy
I was hearing 2slimeys voice in my head guiding me while I made this beat. I’m pretty sure I was only his vessel during the matter
The last beat I had made for 2slimey got mixed reviews when I posted it in here. Half of yall don’t know what you’re talking about anyway so here’s another beat for all who have ears to hear
I’ve seen footage performed by a couple buzzers
Idk if it translates properly to Reddit but here’s the code
// =========================
// PINS
// =========================
const int passiveBuzzer = 8;
const int activeBuzzer = 7;
// =========================
// CLOCK
// =========================
unsigned long lastMicros = 0;
unsigned long stepIntervalMicros;
float tempoBPM = 122;
// =========================
// STRUCTURE
// =========================
int globalStep = 0;
// =========================
// SCALE (C# DORIAN)
// =========================
float scale[] = {277, 311, 330, 370, 415, 466, 494};
// =========================
// ARRAYS
// =========================
int melody[128];
// =========================
// STATE
// =========================
int currentFreq = 0;
// =========================
// GATE
// =========================
unsigned long gateOffTime = 0;
int gateLength = 85;
// =========================
// DRUM TIMING
// =========================
unsigned long drumTimer = 0;
int drumHitsRemaining = 0;
int drumType = 0; // 0 kick, 1 snare, 2 hat
unsigned long drumInterval = 0;
// =========================
// TEMPO
// =========================
void updateTempo() {
stepIntervalMicros = (60000000.0 / tempoBPM) / 4.0;
}
// =========================
// CLEAR
// =========================
void clearAll() {
for (int i = 0; i < 128; i++) {
melody[i] = -1;
}
}
// =========================
// FREQ HELPER
// =========================
int getFreq(int deg, int octave) {
float f = scale[deg];
if (octave < 0) f *= 0.5;
if (octave > 0) f *= 2.0;
return (int)f;
}
// =========================
// INTRO (LOOPS)
// =========================
void buildIntro(int offset) {
auto put = [&](int base, float beat, float dur, int deg) {
int start = base + (int)((beat - 1.0) * 4.0);
int end = base + (int)((beat + dur - 1.0) * 4.0);
for (int i = start; i < end; i++) melody[i] = deg;
};
put(offset,1,1.5,0);
put(offset,2.5,1.5,4);
put(offset,4,1.5,6);
put(offset,5.5,2.5,3);
put(offset,8,1,0);
for (int i = 0; i < 32; i++) {
melody[offset + 32 + i] = melody[offset + i];
}
}
// =========================
// SECTION 2 (MELODY)
// =========================
void buildSection2(int offset) {
auto put = [&](float beat, float dur, int deg, int octave) {
int start = offset + (int)((beat - 1.0) * 4.0);
int end = offset + (int)((beat + dur - 1.0) * 4.0);
for (int i = start; i < end; i++) {
melody[i] = deg + octave * 10;
}
};
put(1,0.5,5,0); put(1.5,0.5,4,-1);
put(2,0.5,2,0); put(2.5,0.5,2,0);
put(3,0.5,1,0); put(3.5,0.25,3,-1);
put(4,0.5,2,0); put(4.5,0.5,2,0);
put(5,0.5,1,0); put(5.5,0.25,3,-1);
put(6,0.5,2,0); put(6.5,0.5,2,0);
put(7,1,1,0); put(8,1,3,0);
put(9,0.5,5,0); put(9.5,0.5,4,-1);
put(10,0.5,2,0); put(10.5,0.5,2,0);
put(11,0.5,1,0); put(11.5,0.25,3,-1);
put(12,0.5,2,0); put(12.5,0.5,2,0);
put(13,0.5,1,0); put(13.5,0.25,3,-1);
put(14,0.5,2,0); put(14.5,0.5,2,0);
put(15,1,1,0); put(16,1,3,0);
}
// =========================
// DRUM TRIGGER
// =========================
void triggerDrum(int type) {
drumType = type;
if (type == 0) { // KICK
drumHitsRemaining = 3;
drumInterval = 400;
}
else if (type == 1) { // SNARE
drumHitsRemaining = 6;
drumInterval = 200;
}
else { // HAT
drumHitsRemaining = 10;
drumInterval = 120;
}
drumTimer = micros();
}
// =========================
// SETUP
// =========================
void setup() {
pinMode(passiveBuzzer, OUTPUT);
pinMode(activeBuzzer, OUTPUT);
updateTempo();
clearAll();
buildIntro(0);
buildSection2(64);
}
// =========================
// LOOP
// =========================
void loop() {
unsigned long now = micros();
// =========================
// STEP CLOCK
// =========================
if (now - lastMicros >= stepIntervalMicros) {
lastMicros += stepIntervalMicros;
int step = globalStep % 128;
int s16 = globalStep % 16;
// =========================
// NOTE ENGINE
// =========================
int val = melody[step];
if (val >= 0) {
int deg = val % 10;
int oct = val / 10;
currentFreq = getFreq(deg, oct);
tone(passiveBuzzer, currentFreq);
}
gateOffTime = micros() + (stepIntervalMicros * gateLength / 100);
// =========================
// DRUM PATTERN (REAL GROOVE)
// =========================
if (s16 == 0 || s16 == 8) triggerDrum(0); // kick
else if (s16 == 4 || s16 == 12) triggerDrum(1); // snare
else triggerDrum(2); // hats everywhere else
globalStep++;
}
// =========================
// GATE OFF
// =========================
if (micros() > gateOffTime) {
noTone(passiveBuzzer);
}
// =========================
// DRUM ENGINE
// =========================
if (drumHitsRemaining > 0) {
if (micros() - drumTimer >= drumInterval) {
drumTimer = micros();
digitalWrite(activeBuzzer, HIGH);
if (drumType == 0) delayMicroseconds(300); // kick = longer
else if (drumType == 1) delayMicroseconds(150);
else delayMicroseconds(80);
digitalWrite(activeBuzzer, LOW);
drumHitsRemaining--;
}
}
}