Skip to content

Commit

Permalink
add load functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
argarak committed Dec 14, 2023
1 parent b41ef94 commit bb8688b
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 5 deletions.
45 changes: 45 additions & 0 deletions components/arpeggiator.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,51 @@ class Arpeggiator extends LitElement {
return state;
}

loadState(root, scale, sequence) {
this.selectedTrack = 0;

this.allRoots = new Set();
this.root = root;
this.scale = scale;
this.noteRange = this.generateNoteRange(this.root, this.scale);

// holds currently programmed sequencer
this.sequence = {};

this.noteIndicators = this.generateIndicators(this.noteRange.length);

for (let trackIndex = 0; trackIndex < this.numTracks; trackIndex++) {
let trackAlgorithm = null;
for (let algorithm of this.algorithms) {
let hash = util.hashCode(algorithm.fn.toString());
if (hash === sequence[trackIndex].algorithm) {
trackAlgorithm = algorithm;
break;
}
}

if (!trackAlgorithm) {
// TODO error here
}

this.sequence[trackIndex] = {
mod: sequence[trackIndex].mod,
sequence: [],
algorithm: trackAlgorithm,

rangeStart: sequence[trackIndex].rangeStart,
rangeEnd: sequence[trackIndex].rangeEnd,
};
}

this.selectedAlgorithm = util.hashCode(
this.sequence[this.selectedTrack].algorithm.fn.toString(),
);

this.switchTrack(0);
this.requestUpdate();
}

switchTrack(trackIndex) {
this.selectedTrack = trackIndex;
this.selectedAlgorithm = util.hashCode(
Expand Down
69 changes: 65 additions & 4 deletions components/sequencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as sequencerStyle from "/styles/components/sequencer.styl?inline";
import defaultAlgorithms from "../sequence-algorithms.js";
import util from "../util.js";

import Patch from "../patch.js";

/**
* sequencer web component
* creates a sequencer which consists of
Expand Down Expand Up @@ -51,7 +53,9 @@ class Sequencer extends LitElement {
if (value > 64) value = 64;

this.sequence[this.selectedTrack].length = value;
this.sequence[this.selectedTrack].sequence = this.generateSequence();
this.sequence[this.selectedTrack].sequence = this.generateSequence(
this.selectedTrack,
);
this.triggerGrid.value.apply(
this.sequence[this.selectedTrack].sequence,
value,
Expand All @@ -60,7 +64,9 @@ class Sequencer extends LitElement {

_onControlInput(e, modIndex) {
this.sequence[this.selectedTrack].mod[modIndex] = e.target.value;
this.sequence[this.selectedTrack].sequence = this.generateSequence();
this.sequence[this.selectedTrack].sequence = this.generateSequence(
this.selectedTrack,
);
this.triggerGrid.value.apply(
this.sequence[this.selectedTrack].sequence,
this.sequence[this.selectedTrack].length,
Expand Down Expand Up @@ -180,6 +186,61 @@ class Sequencer extends LitElement {
return state;
}

loadState(patches, controls, state) {
this.selectedTrack = 0;

// holds currently programmed sequencer
this.sequence = {};

let defaultLength = 64;

for (let trackIndex = 0; trackIndex < this.numTracks; trackIndex++) {
let trackAlgorithm = null;
for (let algorithm of this.algorithms) {
let hash = util.hashCode(algorithm.fn.toString());
if (hash === state[trackIndex].algorithm) {
trackAlgorithm = algorithm;
break;
}
}

if (!trackAlgorithm) {
// TODO error here
}

const trackPatch = new Patch(patches[trackIndex], trackIndex);
trackPatch.loadControlState(controls[trackIndex]);

this.sequence[trackIndex] = {
mod: state[trackIndex].mod,
patch: trackPatch,
length: state[trackIndex].length,
sequence: [],
algorithm: trackAlgorithm,
index: trackIndex,

mute: false,
solo: false,
};

this.sequence[trackIndex].sequence =
this.generateSequence(trackIndex);
}

this.selectedAlgorithm = util.hashCode(
this.sequence[this.selectedTrack].algorithm.fn.toString(),
);

this.step = defaultLength;
this.switchTrack(0);
this.requestUpdate();

this.triggerGrid.value.apply(
this.sequence[this.selectedTrack].sequence,
this.sequence[this.selectedTrack].length,
);
}

saveState() {
const state = {};

Expand Down Expand Up @@ -341,9 +402,9 @@ class Sequencer extends LitElement {
return options;
}

generateSequence() {
generateSequence(track) {
let sequence = [];
let thisSequence = this.sequence[this.selectedTrack];
let thisSequence = this.sequence[track];
for (let index = 0; index < thisSequence.length; index++) {
sequence.push(
thisSequence.algorithm.fn(
Expand Down
16 changes: 16 additions & 0 deletions patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,22 @@ class Patch {
return state;
}

loadControlState(state) {
for (let module of this.modules) {
let moduleName = module.name;
let controlValues = state[moduleName];

let controls =
moduleName in moduleControls ? moduleControls[moduleName] : [];

for (let control of controls) {
const properties = {};
properties[control.property] = controlValues[control.property];
module.set(properties);
}
}
}

addModule(module) {
this.modules.push(module);
}
Expand Down
23 changes: 22 additions & 1 deletion save-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,28 @@ class SaveManager {
}

static loadProject(project) {
console.log(project);
if (project.version > this.saveVersion) {
// error (version is too new to load!)
return;
}

const sequencer = State.get("sequencer");
const arpeggiator = State.get("arpeggiator");

this.projectName = project.name;
this.author = project.author;

arpeggiator.loadState(
project["root"],
project["scale"],
project["arpeggiator"],
);

sequencer.loadState(
project["patch"],
project["controls"],
project["sequencer"],
);
}

static uploadProject() {
Expand Down

0 comments on commit bb8688b

Please sign in to comment.