Skip to content

Commit

Permalink
issue #26 Notes change wrong on scale change
Browse files Browse the repository at this point in the history
  • Loading branch information
mebitek committed Jan 9, 2024
1 parent 1abf0c1 commit 0667e9c
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 16 deletions.
46 changes: 43 additions & 3 deletions src/apps/sequencer/model/NoteSequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

#include <array>
#include <bitset>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <iostream>
#include <map>

class NoteSequence {
public:
Expand Down Expand Up @@ -271,8 +273,39 @@ class NoteSequence {
// scale

int scale() const { return _scale.get(isRouted(Routing::Target::Scale)); }
void setScale(int scale, bool routed = false) {
_scale.set(clamp(scale, -1, Scale::Count - 1), routed);
void setScale(int s, bool routed = false) {

int pScaleIndex = scale();
auto &pScale = selectedScale(scale());

_scale.set(clamp(s, -1, Scale::Count - 1), routed);

auto &aScale = selectedScale(s);

if (s != -1 && pScaleIndex != -1 && aScale.isChromatic() && aScale.Count > 0) {
for (int i = 0; i < 64; ++i) {

auto pStep = _steps[i];

int rN = pScale.noteIndex(pStep.note(), selectedRootNote(0));
if (rN > 0) {
if (aScale.isNotePresent(rN)) {
int pNoteIndex = aScale.getNoteIndex(rN);
step(i).setNote(pNoteIndex);
} else {
// search nearest note
while (!aScale.isNotePresent(rN)) {
rN--;
}
int pNoteIndex = aScale.getNoteIndex(rN);
step(i).setNote(pNoteIndex);
}
}


}
}

}

int indexedScale() const { return scale() + 1; }
Expand All @@ -282,7 +315,7 @@ class NoteSequence {

void editScale(int value, bool shift) {
if (!isRouted(Routing::Target::Scale)) {
setScale(scale() + value);
setScale(value);
}
}

Expand Down Expand Up @@ -473,6 +506,10 @@ class NoteSequence {
void write(VersionedSerializedWriter &writer) const;
void read(VersionedSerializedReader &reader);

int trackIndex() {
return _trackIndex;
}

private:
void setTrackIndex(int trackIndex) { _trackIndex = trackIndex; }

Expand All @@ -489,6 +526,9 @@ class NoteSequence {

int8_t _trackIndex = -1;
Routable<int8_t> _scale;

int _previousScale;

Routable<int8_t> _rootNote;
Routable<uint16_t> _divisor;
uint8_t _resetMeasure;
Expand Down
49 changes: 46 additions & 3 deletions src/apps/sequencer/model/Project.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Config.h"
#include "Observable.h"
#include "Scale.h"
#include "Types.h"
#include "TimeSignature.h"
#include "ClockSetup.h"
Expand Down Expand Up @@ -133,12 +134,54 @@ class Project {
// scale

int scale() const { return _scale; }
void setScale(int scale) {
_scale = clamp(scale, 0, Scale::Count - 1);
void setScale(int s) {
int pScaleIndex = scale();
auto &pScale = Scale::get(scale());

_scale = clamp(s, 0, Scale::Count - 1);

auto &aScale = Scale::get(s);

if (s != -1 && pScaleIndex != -1 && aScale.isChromatic()) {

for (int trackIndex = 0; trackIndex < 8; ++trackIndex) {
auto &t = track(trackIndex);
switch (t.trackMode()) {
case Track::TrackMode::Note: {
for (auto &seq : t.noteTrack().sequences()) {
if (seq.scale()==-1) {
for (int i = 0; i < 64; ++i) {
auto pStep = seq.step(i);

int rN = pScale.noteIndex(pStep.note(), rootNote());
if (rN > 0) {
if (aScale.isNotePresent(rN)) {
int pNoteIndex = aScale.getNoteIndex(rN);
seq.step(i).setNote(pNoteIndex);
} else {
// search nearest note
while (!aScale.isNotePresent(rN)) {
rN--;
}
int pNoteIndex = aScale.getNoteIndex(rN);
seq.step(i).setNote(pNoteIndex);
}
}
}
}
}
break;
}
default:
break;
}
}
}

}

void editScale(int value, bool shift) {
setScale(scale() + value);
setScale(value);
}

void printScale(StringBuilder &str) const {
Expand Down
20 changes: 20 additions & 0 deletions src/apps/sequencer/model/Scale.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Scale {
virtual bool isChromatic() const = 0;
virtual bool isNotePresent(int note) const = 0;
virtual int getNoteIndex(int note) const = 0;
virtual int noteIndex(int note, int rootNote) const = 0;

virtual void noteName(StringBuilder &str, int note, int rootNote, Format format = Long) const = 0;
virtual float noteToVolts(int note) const = 0;
Expand Down Expand Up @@ -111,6 +112,21 @@ class NoteScale : public Scale {
}
}

int noteIndex(int note, int rootNote) const override {
int octave = roundDownDivide(note, _noteCount);
int noteIndex = 0;
if (isChromatic()) {
noteIndex = _notes[note - octave * _noteCount] / 128 + rootNote;
while (noteIndex >= 12) {
noteIndex -= 12;
octave += 1;
}
} else {
noteIndex = note - octave * _noteCount + 1;
}
return noteIndex;
}

float noteToVolts(int note) const override {
int octave = roundDownDivide(note, _noteCount);
int index = note - octave * _noteCount;
Expand Down Expand Up @@ -168,6 +184,10 @@ class VoltScale : public Scale {
return -1;
}

int noteIndex(int note, int rootNote) const override {
return -1;
}

void noteName(StringBuilder &str, int note, int rootNote, Format format) const override {
switch (format) {
case Short1:
Expand Down
2 changes: 2 additions & 0 deletions src/apps/sequencer/model/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ class Types {
str(names[note]);
}



static void printMidiNote(StringBuilder &str, int midiNote) {
printNote(str, midiNote % 12);
int octave = midiNote / 12 - 1;
Expand Down
12 changes: 12 additions & 0 deletions src/apps/sequencer/model/UserScale.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ class UserScale : public Scale {
return -1;
}

int noteIndex(int note, int rootNote) const override {
int octave = roundDownDivide(note, _size);

int noteIndex = _items[note - octave * _size] + rootNote;
while (noteIndex >= 12) {
noteIndex -= 12;
octave += 1;
}

return noteIndex;
}

void noteName(StringBuilder &str, int note, int rootNote, Format format) const override {
switch (_mode) {
case Mode::Chromatic:
Expand Down
36 changes: 31 additions & 5 deletions src/apps/sequencer/ui/model/NoteSequenceListModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "model/NoteSequence.h"
#include "model/Scale.h"
#include <iostream>

class NoteSequenceListModel : public RoutableListModel {
public:
Expand All @@ -21,7 +22,16 @@ class NoteSequenceListModel : public RoutableListModel {
};

NoteSequenceListModel()
{}
{
_scales[0] = -1;
for (int i = 1; i < 23; ++i) {
_scales[i] = i-1;
}

for (int i = 0; i < 8; ++i) {
_selectedScale[i] = 0;
}
}

void setSequence(NoteSequence *sequence) {
_sequence = sequence;
Expand Down Expand Up @@ -82,6 +92,13 @@ class NoteSequenceListModel : public RoutableListModel {
}
}

void setSelectedScale() {
if (_editScale) {
_sequence->editScale(_scales[_selectedScale[_sequence->trackIndex()]], false);
}
_editScale = !_editScale;
}

private:
static const char *itemName(Item item) {
switch (item) {
Expand Down Expand Up @@ -118,8 +135,11 @@ class NoteSequenceListModel : public RoutableListModel {
case ResetMeasure:
_sequence->printResetMeasure(str);
break;
case Scale:
_sequence->printScale(str);
case Scale: {
int trackIndex = _sequence->trackIndex();
auto name = _scales[_selectedScale[trackIndex]] < 0 ? "Default" : Scale::name(_scales[_selectedScale[trackIndex]]);
str(name);
}
break;
case RootNote:
_sequence->printRootNote(str);
Expand All @@ -146,8 +166,10 @@ class NoteSequenceListModel : public RoutableListModel {
case ResetMeasure:
_sequence->editResetMeasure(value, shift);
break;
case Scale:
_sequence->editScale(value, shift);
case Scale: {
int trackIndex = _sequence->trackIndex();
_selectedScale[trackIndex] = clamp(_selectedScale[trackIndex] + value, 0, 23);
}
break;
case RootNote:
_sequence->editRootNote(value, shift);
Expand Down Expand Up @@ -221,4 +243,8 @@ class NoteSequenceListModel : public RoutableListModel {
}

NoteSequence *_sequence;
private:
std::array<int, 23> _scales;
std::array<int, 8> _selectedScale;
bool _editScale = false;
};
26 changes: 22 additions & 4 deletions src/apps/sequencer/ui/model/ProjectListModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
#include "RoutableListModel.h"

#include "model/Project.h"
#include <iostream>

class ProjectListModel : public RoutableListModel {
public:
ProjectListModel(Project &project) :
_project(project)
{}
{
for (int i = 0; i < 23; ++i) {
_scales[i] = i;
}
}

virtual int rows() const override {
return Last;
Expand Down Expand Up @@ -45,6 +50,13 @@ class ProjectListModel : public RoutableListModel {
}
}

void setSelectedScale() {
if (_editScale) {
_project.editScale(_scales[_selectedScale], false);
}
_editScale = !_editScale;
}

private:
enum Item {
Name,
Expand Down Expand Up @@ -104,8 +116,10 @@ class ProjectListModel : public RoutableListModel {
case SyncMeasure:
_project.printSyncMeasure(str);
break;
case Scale:
_project.printScale(str);
case Scale: {
auto name = _scales[_selectedScale] < 0 ? "Default" : Scale::name(_scales[_selectedScale]);
str(name);
}
break;
case RootNote:
_project.printRootNote(str);
Expand Down Expand Up @@ -150,7 +164,7 @@ class ProjectListModel : public RoutableListModel {
_project.editSyncMeasure(value, shift);
break;
case Scale:
_project.editScale(value, shift);
_selectedScale = clamp(_selectedScale + value, 0, 23);
break;
case RootNote:
_project.editRootNote(value, shift);
Expand Down Expand Up @@ -179,4 +193,8 @@ class ProjectListModel : public RoutableListModel {
}

Project &_project;
private:
std::array<int, 23> _scales;
int _selectedScale = 0;
bool _editScale = false;
};
2 changes: 1 addition & 1 deletion src/apps/sequencer/ui/pages/ListPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ListPage : public BasePage {
virtual void keyPress(KeyPressEvent &event) override;
virtual void encoder(EncoderEvent &event) override;

int selectedRow() const { return _selectedRow; }
virtual int selectedRow() const { return _selectedRow; }
void setSelectedRow(int selectedRow);

bool edit() const { return _edit; }
Expand Down
8 changes: 8 additions & 0 deletions src/apps/sequencer/ui/pages/NoteSequencePage.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "NoteSequencePage.h"

#include "ListPage.h"
#include "Pages.h"

#include "ui/LedPainter.h"
#include "ui/painters/WindowPainter.h"

#include "core/utils/StringBuilder.h"
#include <iostream>

enum class ContextAction {
Init,
Expand Down Expand Up @@ -66,6 +68,12 @@ void NoteSequencePage::keyPress(KeyPressEvent &event) {
if (!event.consumed()) {
ListPage::keyPress(event);
}
if (key.isEncoder()) {
auto row = ListPage::selectedRow();
if (row == 5) {
_listModel.setSelectedScale();
}
}
}

void NoteSequencePage::contextShow() {
Expand Down
Loading

0 comments on commit 0667e9c

Please sign in to comment.