probability

This commit is contained in:
Dominic DiTaranto 2026-01-18 16:51:26 -05:00
parent 7b6124ceda
commit 9925d68aa0
3 changed files with 50 additions and 39 deletions

83
Out.cpp
View file

@ -7,46 +7,30 @@ Out::Out(byte pin) {
this->pin = pin;
state = LOW;
divideMode = 1;
modifier = 1;
div = 1;
divString = "";
cycle = 0;
divideMode = 1; // 1 divison | 0 multiplication
modifier = 1; // divide mode modifier (4x, /32, etc)
div = 1; // cycles needed before a pulse based on divide mode and modifier
cycle = 0; // how many cycles have passed since last pulse
divString = ""; // string for screen .. probably does not belong here
dur = 0;
width = 50;
// this needs to dynamically change with width
dur = 0; // how long pulse is on
width = 50; // pulse width
len = 0; // max len a pulse can be on, as determined by width
p = 100; // probability of a pulse
len = 0;
pinMode(pin, OUTPUT);
}
int Out::getState() {
return state;
}
void Out::setLen(unsigned long currentPeriod) {
len = (unsigned long)((double)currentPeriod * (width / 100.0) / 1000.0);
}
void Out::turnOn() {
cycle += 1;
if (cycle == div) {
state = HIGH;
digitalWrite(pin, state);
dur = millis();
cycle = 0;
};
}
void Out::turnOff() {
if (state == HIGH && millis() - dur >= len) {
state = LOW;
digitalWrite(pin, state);
dur = 0;
};
}
void Out::setDiv(int modifier, byte divide = 1) {
if (divide == 1) {
div = ppqn * modifier;
@ -60,20 +44,45 @@ void Out::setDiv(int modifier, byte divide = 1) {
};
void Out::setWidth(int newWidth) {
Serial.println(modifier);
Serial.println(divideMode);
width = newWidth;
if (divideMode == 1) {
len = (unsigned long)((double)(minute / BPM) * (width / 100.0) / 1000.0);
} else {
len = (unsigned long)((double)(minute / BPM / modifier) * (width / 100.0) / 1000.0);
}
Serial.println(len);
Serial.println("=========");
};
int Out::getState() {
return state;
void Out::setP(int prob) {
this->p = prob;
}
void Out::turnOn() {
cycle += 1;
byte pRes = 1;
if (cycle == div) {
Serial.println(p);
if (p < 100) {
long r = random(1, 101);
if (r > p) {
pRes = 0;
}
}
if (pRes == 1) {
state = HIGH;
digitalWrite(pin, state);
dur = millis();
}
cycle = 0;
};
}
void Out::turnOff() {
if (state == HIGH && millis() - dur >= len) {
state = LOW;
digitalWrite(pin, state);
dur = 0;
};
}

2
Out.h
View file

@ -16,6 +16,7 @@ class Out {
int div;
int modifier;
String divString;
int p;
public:
Out(byte pin);
@ -24,6 +25,7 @@ class Out {
void setLen(unsigned long currentPeriod);
void setDiv(int newDiv, byte divide = 1);
void setWidth(int newWidth);
void setP(int prob);
int getState();
};

View file

@ -13,9 +13,9 @@ extern byte ppqn;
TODO:
PRE-DAC:
[ ] Figure out multiplicative beats X2 X4 X8 X16 X32?
[x] Figure out multiplicative beats X2 X4 X8 X16 X32?
[ ] Swing/Phase (same thing)
[ ] Probability
[x] Probability
[ ] Humanization
[ ] Euclidian Rhythms
[ ] Steps - # of steps for a full pattern