summaryrefslogtreecommitdiff
path: root/src/relabi
diff options
context:
space:
mode:
authorjulian laplace <julescarbon@gmail.com>2023-05-09 22:56:44 +0200
committerjulian laplace <julescarbon@gmail.com>2023-05-09 22:56:44 +0200
commit8c19ceb702ebf1fdd66d1c71e491a21974e9c638 (patch)
tree879b84f5c00f3e29bd342bd61cf844fffb1c3e3a /src/relabi
parent886f106be26f9d1dc2dc867c89f2964f46a3a065 (diff)
chaos knob
Diffstat (limited to 'src/relabi')
-rw-r--r--src/relabi/canvas.js8
-rw-r--r--src/relabi/index.js45
2 files changed, 35 insertions, 18 deletions
diff --git a/src/relabi/canvas.js b/src/relabi/canvas.js
index 2003a5a..fed4f6d 100644
--- a/src/relabi/canvas.js
+++ b/src/relabi/canvas.js
@@ -3,6 +3,8 @@
* @module src/relabi/canvas.js;
*/
+const NOTE_RADIUS = 7;
+
export default class RelabiCanvas {
/**
* Initialize relabi wave renderer
@@ -209,10 +211,10 @@ export default class RelabiCanvas {
continue;
}
ctx.beginPath();
- ctx.arc(x - 2.5, y, 5, 0, 2 * Math.PI);
+ ctx.arc(x - NOTE_RADIUS / 4, y, NOTE_RADIUS, 0, 2 * Math.PI);
ctx.fillStyle = direction
- ? `rgba(255,128,192,${opacity})`
- : `rgba(192,255,128,${opacity})`;
+ ? `rgba(255,96,128,${opacity})`
+ : `rgba(128,255,96,${opacity})`;
ctx.fill();
}
}
diff --git a/src/relabi/index.js b/src/relabi/index.js
index 8e23d2a..9e649f2 100644
--- a/src/relabi/index.js
+++ b/src/relabi/index.js
@@ -4,6 +4,7 @@
import * as Tone from "tone";
import RelabiCanvas from "./canvas";
+import * as Instruments from "../lib/instruments";
const TWO_PI = 2 * Math.PI;
@@ -19,7 +20,8 @@ const WAVE_SHAPES = {
) -
1,
square: (time) => (time % TWO_PI < Math.PI ? 1 : -1),
- saw: (time) => ((time % TWO_PI) - Math.PI) / Math.PI,
+ saw: (time) => (time % TWO_PI) / Math.PI - 1,
+ reverse_saw: (time) => 1 - (time % TWO_PI) / Math.PI,
};
/**
@@ -29,16 +31,12 @@ export default class Relabi {
/**
* Initialize generator
*/
- constructor({ waves, bounds, parent }) {
+ constructor({ waves, bounds, settings, parent }) {
this.updateTime = 1;
this.steps = 50;
- this.waves = waves || [
- { shape: "triangle", frequency: randrange(0.5, 1.5) },
- { shape: "triangle", frequency: randrange(0.75, 2.25) },
- { shape: "triangle", frequency: randrange(1, 3) },
- { shape: "triangle", frequency: randrange(2, 4) },
- ];
+ this.waves = waves;
this.bounds = bounds;
+ this.settings = settings;
this.previousValue = null;
this.canvas = new RelabiCanvas({ relabi: this, parent });
}
@@ -71,14 +69,17 @@ export default class Relabi {
* Generate relabi events
*/
generate(time) {
- const waveCount = this.waves.length;
let index;
let step;
let value;
let values = [];
+ let previousWaveValue = this.previousWaveValue || 0;
+
+ // Weight individual waves rather than simply averaging them
+ let totalWeight = this.waves.reduce((sum, wave) => sum + wave.weight, 0.0);
// Overshoot the line slightly each time
- let stepCount = this.steps + 20;
+ let stepCount = this.steps;
// Generate several events per second
for (step = 0; step < stepCount; step += 1) {
@@ -89,17 +90,27 @@ export default class Relabi {
value = 0;
// Compute the wave functions for this event
- for (index = 0; index < waveCount; index += 1) {
- const wave = this.waves[index];
- value += WAVE_SHAPES[wave.shape](timeOffset * wave.frequency);
+ for (const wave of this.waves) {
+ const waveOffset =
+ (wave.offset || 0) +
+ (wave.frequency * this.settings.speed) / this.steps +
+ previousWaveValue * this.settings.feedback;
+
+ const waveValue = WAVE_SHAPES[wave.shape](waveOffset);
+ value += waveValue * wave.weight;
+ previousWaveValue = waveValue;
+ wave.offset = waveOffset;
}
// Scale to [-1, 1]
- value /= waveCount;
+ value /= totalWeight;
+ previousWaveValue = value;
values.push([timeOffset, value]);
}
+ this.previousWaveValue = previousWaveValue;
+
return values;
}
@@ -158,6 +169,10 @@ export default class Relabi {
*/
trigger(time, sound) {
// console.log("trigger index", index, time);
- sound.instrument.play(time, sound);
+ if (sound.instrument in Instruments) {
+ Instruments[sound.instrument].play(time, sound);
+ } else {
+ sound.instrument.play(time, sound);
+ }
}
}