Skip to content

Commit

Permalink
improve visualization and scale support
Browse files Browse the repository at this point in the history
  • Loading branch information
mebitek committed Jan 8, 2024
1 parent 49e47db commit 1f80183
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
18 changes: 10 additions & 8 deletions src/apps/sequencer/engine/StochasticEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,17 @@ void StochasticEngine::triggerStep(uint32_t tick, uint32_t divisor, bool forNext

if (stepIndex < 0) return;

int probability[12];
auto &scale = sequence.selectedScale(_model.project().scale());

int probability[scale.notesPerOctave()];
int sum =0;
for (int i = 0; i < 12; ++i) {
for (int i = 0; i < scale.notesPerOctave(); ++i) {
probability[i] = clamp(sequence.step(i).noteVariationProbability() + _stochasticTrack.noteProbabilityBias(), -1, StochasticSequence::NoteVariationProbability::Max);
sum = sum + probability[i];
}
if (sum==0) { return;}

stepIndex= getNextWeightedPitch(probability, sequence.reseed());
stepIndex= getNextWeightedPitch(probability, sequence.reseed(), scale.notesPerOctave());

auto &step = sequence.step(stepIndex);

Expand Down Expand Up @@ -428,11 +430,11 @@ void StochasticEngine::triggerStep(uint32_t tick, uint32_t divisor, bool forNext
}


int StochasticEngine::getNextWeightedPitch(int *distr, bool reseed) {
int StochasticEngine::getNextWeightedPitch(int *distr, bool reseed, int notesPerOctave) {
int total_weights = 0;

for(int i = 0; i < 12; i++) {
total_weights += distr[i % 12];
for(int i = 0; i < notesPerOctave; i++) {
total_weights += distr[i % notesPerOctave];
}

if (reseed) {
Expand All @@ -441,8 +443,8 @@ int StochasticEngine::getNextWeightedPitch(int *distr, bool reseed) {
}
int rnd = 1 + ( std::rand() % ( (total_weights) - 1 + 1 ) );

for(int i = 0; i < 12; i++) {
int weight = distr[i % 12];
for(int i = 0; i < notesPerOctave; i++) {
int weight = distr[i % notesPerOctave];
if (rnd <= weight && weight > 0) {
return i;
}
Expand Down
3 changes: 2 additions & 1 deletion src/apps/sequencer/engine/StochasticEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class StochasticEngine : public TrackEngine {

void setMonitorStep(int index);

int getNextWeightedPitch(int *distr, bool reseed = false);
int getNextWeightedPitch(int *distr, bool reseed = false, int notesPerOctave = 12);

private:
void triggerStep(uint32_t tick, uint32_t divisor, bool nextStep);
Expand All @@ -62,6 +62,7 @@ class StochasticEngine : public TrackEngine {

TrackLinkData _linkData;

Project _project;
StochasticSequence *_sequence;
const StochasticSequence *_fillSequence;

Expand Down
4 changes: 3 additions & 1 deletion src/apps/sequencer/model/StochasticSequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,9 @@ void StochasticSequence::clearSteps() {
step.clear();
}

for (int i = 0; i < 12; ++i) {


for (int i = 0; i < 64; ++i) {
_steps[i].setGate(false);
_steps[i].setNoteVariationProbability(0);
_steps[i].setNote(i);
Expand Down
35 changes: 25 additions & 10 deletions src/apps/sequencer/ui/pages/StochasticSequenceEditPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void StochasticSequenceEditPage::enter() {
updateMonitorStep();

_showDetail = false;
_section = 0;
}

void StochasticSequenceEditPage::exit() {
Expand Down Expand Up @@ -94,7 +95,11 @@ void StochasticSequenceEditPage::draw(Canvas &canvas) {
const int stepWidth = Width / StepCount;
const int stepOffset = this->stepOffset();

const int loopY = 16;
int stepsToDraw = scale.notesPerOctave();
if (scale.notesPerOctave() % 16 != scale.notesPerOctave() && _section > 0) {
stepsToDraw = scale.notesPerOctave() % 16;
}
const int loopY = stepsToDraw;


// Track Pattern Section on the UI
Expand All @@ -109,14 +114,17 @@ void StochasticSequenceEditPage::draw(Canvas &canvas) {
// draw loop points
canvas.setBlendMode(BlendMode::Set);
canvas.setColor(Color::Bright);
SequencePainter::drawLoopStart(canvas, (sequence.firstStep() - stepOffset) * stepWidth + 1, loopY, stepWidth - 2);
SequencePainter::drawLoopEnd(canvas, (sequence.lastStep() - stepOffset) * stepWidth + 1, loopY, stepWidth - 2);
SequencePainter::drawLoopStart(canvas, ((sequence.firstStep() - stepOffset) * stepWidth + 1)+((16 - stepsToDraw)*stepWidth)/2, loopY, stepWidth - 2);
SequencePainter::drawLoopEnd(canvas, ((sequence.lastStep() - stepOffset) * stepWidth + 1)+((16 - stepsToDraw)*stepWidth)/2, loopY, stepWidth - 2);




for (int i = 0; i < 12; ++i) {
for (int i = 0; i < stepsToDraw; ++i) {
int stepIndex = stepOffset + i;
const auto &step = sequence.step(stepIndex);
auto &step = sequence.step(stepIndex);

int x = i * stepWidth;
int x = (i * stepWidth) + ((16 - stepsToDraw)*stepWidth)/2 ;
int y = 20;

// loop
Expand Down Expand Up @@ -404,22 +412,29 @@ void StochasticSequenceEditPage::keyPress(KeyPressEvent &event) {
}
}


const auto &scale = sequence.selectedScale(_project.scale());
if (key.isLeft()) {
if (key.shiftModifier()) {
sequence.shiftSteps(_stepSelection.selected(), -1);
} else {
setSectionTracking(false);
_section = std::max(0, _section - 1);
_section = std::max(0, (_section - 1) );
}
event.consume();
}
if (key.isRight()) {
if (key.shiftModifier()) {
sequence.shiftSteps(_stepSelection.selected(), 1);
} else {
setSectionTracking(false);
_section = std::min(3, _section + 1);
int stepsToDraw = scale.notesPerOctave();
if (scale.notesPerOctave() % 16 != scale.notesPerOctave() && _section > 0) {
stepsToDraw = scale.notesPerOctave() % 16;
}

if (stepsToDraw > 16) {
setSectionTracking(false);
_section = std::min(3, _section + 1);
}
}
event.consume();
}
Expand Down

0 comments on commit 1f80183

Please sign in to comment.