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; this->pin = pin;
state = LOW; state = LOW;
divideMode = 1; divideMode = 1; // 1 divison | 0 multiplication
modifier = 1; modifier = 1; // divide mode modifier (4x, /32, etc)
div = 1; div = 1; // cycles needed before a pulse based on divide mode and modifier
divString = ""; cycle = 0; // how many cycles have passed since last pulse
cycle = 0; divString = ""; // string for screen .. probably does not belong here
dur = 0; dur = 0; // how long pulse is on
width = 50; width = 50; // pulse width
// this needs to dynamically change with 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); pinMode(pin, OUTPUT);
} }
int Out::getState() {
return state;
}
void Out::setLen(unsigned long currentPeriod) { void Out::setLen(unsigned long currentPeriod) {
len = (unsigned long)((double)currentPeriod * (width / 100.0) / 1000.0); 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) { void Out::setDiv(int modifier, byte divide = 1) {
if (divide == 1) { if (divide == 1) {
div = ppqn * modifier; div = ppqn * modifier;
@ -60,20 +44,45 @@ void Out::setDiv(int modifier, byte divide = 1) {
}; };
void Out::setWidth(int newWidth) { void Out::setWidth(int newWidth) {
Serial.println(modifier);
Serial.println(divideMode);
width = newWidth; width = newWidth;
if (divideMode == 1) { if (divideMode == 1) {
len = (unsigned long)((double)(minute / BPM) * (width / 100.0) / 1000.0); len = (unsigned long)((double)(minute / BPM) * (width / 100.0) / 1000.0);
} else { } else {
len = (unsigned long)((double)(minute / BPM / modifier) * (width / 100.0) / 1000.0); len = (unsigned long)((double)(minute / BPM / modifier) * (width / 100.0) / 1000.0);
} }
Serial.println(len);
Serial.println("=========");
}; };
int Out::getState() { void Out::setP(int prob) {
return state; 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 div;
int modifier; int modifier;
String divString; String divString;
int p;
public: public:
Out(byte pin); Out(byte pin);
@ -24,6 +25,7 @@ class Out {
void setLen(unsigned long currentPeriod); void setLen(unsigned long currentPeriod);
void setDiv(int newDiv, byte divide = 1); void setDiv(int newDiv, byte divide = 1);
void setWidth(int newWidth); void setWidth(int newWidth);
void setP(int prob);
int getState(); int getState();
}; };

View file

@ -13,9 +13,9 @@ extern byte ppqn;
TODO: TODO:
PRE-DAC: 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) [ ] Swing/Phase (same thing)
[ ] Probability [x] Probability
[ ] Humanization [ ] Humanization
[ ] Euclidian Rhythms [ ] Euclidian Rhythms
[ ] Steps - # of steps for a full pattern [ ] Steps - # of steps for a full pattern