color-synth

a synth that generates colors instead of sounds
Log | Files | Refs | README

commit b68a4ccbf90f742749cd28c497f75950a7c3bc4c
parent f4a5f72284046c308acb7aad7c5109d8a05e8677
Author: massi <mdsiboldi@gmail.com>
Date:   Wed, 21 Jun 2023 18:47:57 -0700

back to where we were, minus windowing stuff

Diffstat:
Msrc/routes/+page.svelte | 129++++++++++++++++++++++++++++++-------------------------------------------------
1 file changed, 49 insertions(+), 80 deletions(-)

diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte @@ -18,17 +18,6 @@ return () => resizeObserver.unobserve(cvs); }); - let t = 0; - - const DELTA = 0.00001; - function step() { - t = (t + DELTA) % 100; // lol - } - - enum BindingKind { - OscBinding - } - type UnitMap = Map<UnitId, Unit>; type UnitStateMap = Map<UnitId, UnitState>; type UnitState = any; @@ -38,8 +27,8 @@ // uses rate and amount to output a sine wave going that fast and loud. TBD: what units the values are, etc. type OscUnit = { kind: 'osc'; - rate: Binding[]; - amount: Binding[]; + rate: UnitId; + amount: UnitId; }; // outputs number as it is @@ -48,21 +37,14 @@ value: number; }; - type Unit = OscUnit | ConstUnit; - - type Binding = { - scale: number; - source: UnitId; - }; - - type Grid = { - rows: Binding[]; - cols: Binding[]; + type CombinatorUnit = { + kind: 'combinator'; + sources: UnitId[]; }; - type OldBinding = () => number; + type Unit = OscUnit | ConstUnit | CombinatorUnit; - let id = 1; + let id = 0; let units: UnitMap = new Map(); let unitState: UnitStateMap = new Map(); function addUnit(unit: Unit) { @@ -109,79 +91,66 @@ switch (unit.kind) { case 'osc': { const position = getUnitState(id); - return (Math.sin(2 * Math.PI * position) - 0.5) * r(unit.amount); + return (Math.sin(2 * Math.PI * position) - 0.5) * 2 * v(unit.amount); } case 'const': { return unit.value; } + case 'combinator': { + return unit.sources.reduce((a, b) => a + v(b), 0); + } } } - function r(bindings: Binding[]): number { - return bindings.reduce((a, b) => a + b.scale * v(b.source), 0); - } - - function update(...ids: UnitId[]) { + function update() { // update all unit states - ids = ids || units.keys(); + const ids = units.keys(); for (let id of ids) { const unit = getUnit(id); switch (unit.kind) { case 'osc': const position = getUnitState(id); - const rate = r(unit.rate); + const rate = v(unit.rate); setUnitState(id, position + rate); } } } - function mkConst(value: number): UnitId { - return addUnit({ - kind: 'const', - value - }); - } - - function mkOsc(rate: Binding[], amount: Binding[]): UnitId { - return addUnit({ - kind: 'osc', - rate, - amount - }); - } - - function c(value: number): Binding { - return { scale: 1, source: mkConst(value) }; - } - - function mkBinding(source: UnitId): Binding { - return { - scale: 1, - source - }; - } - - function o(rate: Binding[], amount: Binding[]): Binding { - return mkBinding(mkOsc(rate, amount)); - } - - //const red = mkOsc([c(0.1), o([c(0.1)], [c(0.01)])], [c(100)]); - const red = mkOsc([c(189)], [c(50)]); - const green = mkConst(20); - const blue = mkOsc([c(0.01)], [c(100)]); - - console.log(units.get(red)); - - //function mkOsc(inputs: { rate: OldBinding; amount: OldBinding }): OldBinding { - // let { rate, amount } = inputs; - // return () => (Math.sin(2 * Math.PI * t * rate()) - 0.5 + Math.random() / 20) * amount(); - //} - - function combinatorBinding(...bindings: OldBinding[]): OldBinding { - return () => bindings.reduce((a, b) => a + b(), 0); - } + const mk = { + c: function mkConst(value: number): UnitId { + return addUnit({ + kind: 'const', + value + }); + }, + osc: function mkOsc(rate: UnitId | number, amount: UnitId | number): UnitId { + return addUnit({ + kind: 'osc', + rate: typeof rate === 'number' ? mk.c(rate) : rate, + amount: typeof amount === 'number' ? mk.c(amount) : amount + }); + }, + add: function com(...ids: UnitId[]) { + return addUnit({ + kind: 'combinator', + sources: ids + }); + } + }; - let nextT = null; + const red = mk.add( + mk.c(100), // + mk.osc(mk.osc(0.00100001, 0.1), 10) // + ); + const green = mk.add( + mk.c(100), // + mk.osc(mk.osc(0.001000011, 0.1), 10) // + ); + const blue = mk.add( + mk.c(100), // + mk.osc(mk.osc(0.001000012, 0.1), 10) // + ); + //const blue = mk.osc(mk.c(12321.2832), mk.c(55)); function drawSquares(ctx: CanvasRenderingContext2D) { if (!cvs) { @@ -200,7 +169,7 @@ color[0] = v(red); color[1] = v(green); color[2] = v(blue); - update(red, green, blue); + update(); ctx.fillStyle = `rgb(${color.join(', ')})`; let x = col * paneWidth; let y = row * paneHeight;