monostep/Source/dsp/StepSequencer.h
2026-08-05 23:12:17 +02:00

71 lines
1.4 KiB
C++

#pragma once
#include <array>
#include <cmath>
namespace monostep
{
static constexpr int numSteps = 16;
static constexpr int minSemitone = -12;
static constexpr int maxSemitone = 12;
static constexpr int numRows = maxSemitone - minSemitone + 1;
struct Step
{
bool gate = false;
int semitone = 0; // -12 .. +12
float cents = 0.0f; // -50 .. +50
bool slide = false;
bool accent = false;
};
class StepSequencer
{
public:
Step& step (int i) { return steps[i]; }
const Step& step (int i) const { return steps[i]; }
void clear()
{
for (auto& s : steps)
s = Step{};
}
void shift (int dir)
{
if (dir == 0)
return;
std::array<Step, numSteps> rotated = steps;
for (int i = 0; i < numSteps; ++i)
{
const int from = (i - dir + numSteps) % numSteps;
steps[i] = rotated[from];
}
}
int stepsPerBeatFromRate (int rateIndex) const
{
// 0: 1/4, 1: 1/8, 2: 1/8T, 3: 1/16, 4: 1/32
switch (rateIndex)
{
case 0: return 1;
case 1: return 2;
case 2: return 3;
case 4: return 8;
default: return 4;
}
}
float stepLenPpq (int rateIndex) const
{
return 1.0f / (float) stepsPerBeatFromRate (rateIndex);
}
private:
std::array<Step, numSteps> steps;
};
} // namespace monostep