Skip to content

Commit

Permalink
Set up basic sequencer API
Browse files Browse the repository at this point in the history
  • Loading branch information
zxqx committed Sep 27, 2016
1 parent 4cdd563 commit 7ed6b47
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
51 changes: 51 additions & 0 deletions src/modules/Sequencer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import notesToFrequencies from 'notes-to-frequencies';

/**
* A 64-step sequencer with grid and recording modes
*/
export default class Sequencer {
constructor(length) {
this._sequence = new Array(length);
this._tempo = 120;
this._currentIndex = 0;
}

setTempo(bpm) {
this._tempo = bpm
}

getMillisecondsPerStep() {
return (60000 / this._tempo) / 4;
}

setNoteAtIndex(index, note) {
this._sequence[index] = note;
}

trigger(callbacks) {
this._onTriggerCallbacks = callbacks;
}

play() {
const index = this._currentIndex;
const note = this._sequence[index];
const frequency = notesToFrequencies(note);

if (note) {
this._onTriggerCallbacks.forEach(cb => cb(frequency));
}

setTimeout(::this.play, this.getMillisecondsPerStep());

if (index === this._sequence.length - 1) {
this._currentIndex = 0;
}
else {
this._currentIndex++;
}
}

clear() {
this._sequence = new Array(16);
}
}
19 changes: 18 additions & 1 deletion src/stock-synths/ThreeOscSynth.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import FrequencyAnalyzer from '../modules/FrequencyAnalyzer.js';
import Ringmod from '../modules/Ringmod.js';
import Chorus from '../modules/Chorus.js';
import Panner from '../modules/Panner.js';
import Sequencer from '../modules/Sequencer.js';

export default class ThreeOscSynth {
constructor() {
Expand All @@ -31,8 +32,9 @@ export default class ThreeOscSynth {
this.ringmod = new Ringmod();
this.chorus = new Chorus();
this.panner = new Panner();
this.sequencer = new Sequencer(16);

const { synth, midi, oscGroup, filter, mixer, vca, envelope, lfo, delay, eq3, computerKeyboard, frequencyAnalyzer, ringmod, chorus, panner } = this;
const { synth, midi, oscGroup, filter, mixer, vca, envelope, lfo, delay, eq3, computerKeyboard, frequencyAnalyzer, ringmod, chorus, panner, sequencer } = this;
const { osc1, osc2, osc3 } = oscGroup;

synth.addModule(oscGroup);
Expand Down Expand Up @@ -69,6 +71,21 @@ export default class ThreeOscSynth {
envelope::envelope.triggerRelease
]);

envelope.setAttack(0);
envelope.setSustain(1);
envelope.setDecay(0.2);
envelope.setRelease(0.2);

sequencer.trigger([
osc1::osc1.setFrequency,
envelope::envelope.triggerADS
]);

sequencer.setNoteAtIndex(0, 'C4');
sequencer.setNoteAtIndex(4, 'C#4');
sequencer.setNoteAtIndex(8, 'D4');
sequencer.setNoteAtIndex(12, 'C#4');

midi.trigger(osc1::osc1.setFrequency);
}
}

0 comments on commit 7ed6b47

Please sign in to comment.