diff options
Diffstat (limited to 'src/relabi/index.js')
| -rw-r--r-- | src/relabi/index.js | 44 |
1 files changed, 20 insertions, 24 deletions
diff --git a/src/relabi/index.js b/src/relabi/index.js index 867bc0d..ad5b11d 100644 --- a/src/relabi/index.js +++ b/src/relabi/index.js @@ -1,6 +1,4 @@ import * as Tone from "tone"; -import kalimba from "../lib/kalimba"; -import { randrange } from "../lib/util"; const TWO_PI = 2 * Math.PI; @@ -19,21 +17,23 @@ const WAVE_FUNCTIONS = { saw: (time) => ((time % TWO_PI) - Math.PI) / Math.PI, }; -class Relabi { +/** + * Relabi generator + */ +export default class Relabi { /** - * Initialize relabi generator + * Initialize generator */ - constructor() { - this.updateTime = 1.0; - this.steps = 100; - this.waves = [ - { type: "sine", frequency: randrange(0.5, 2) }, - { type: "sine", frequency: randrange(0.5, 2) }, - { type: "sine", frequency: randrange(1, 10) }, - { type: "sine", frequency: randrange(5, 10) }, + constructor({ waves, bounds }) { + this.updateTime = 0.5; + this.steps = 50; + this.waves = waves || [ + { type: "sine", frequency: randrange(0.5, 1.5) }, + { type: "sine", frequency: randrange(0.75, 2.25) }, + { type: "sine", frequency: randrange(1, 3) }, + { type: "sine", frequency: randrange(2, 4) }, ]; - this.bounds = [-0.5, 0.5]; - this.frequencies = [220, (220 * 3) / 2, 440, (440 * 3) / 2]; + this.bounds = bounds; } /** @@ -88,13 +88,13 @@ class Relabi { // Compute whether we crossed a boundary, and which direction for (index = 0; index < boundsCount; index += 1) { const bound = this.bounds[index]; - if (value < bound && bound < previousValue) { + if (value < bound.level && bound.level < previousValue) { // Going down - this.trigger(offset, index * 2); + this.trigger(offset, bound.sounds[0]); noteCount += 1; - } else if (value > bound && bound > previousValue) { + } else if (value > bound.level && bound.level > previousValue) { // Going up - this.trigger(offset, index * 2 + 1); + this.trigger(offset, bound.sounds[1]); noteCount += 1; } } @@ -105,17 +105,13 @@ class Relabi { // Store the latest value this.previousValue = value; - - console.log(`Tick ${Math.floor(time)}, played ${noteCount} notes`); } /** * Trigger an event */ - trigger(time, index) { + trigger(time, sound) { // console.log("trigger index", index, time); - kalimba.play(this.frequencies[index], time); + sound.instrument.play(time, sound); } } - -export default Relabi; |
