audiora 1.0.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +7 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +138 -0
- package/dist/index.d.ts +138 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +25 -6
- package/docs/API.md +0 -345
- package/src/Audiora.ts +0 -296
- package/src/core/AudioEngine.ts +0 -108
- package/src/core/ImageProcessor.ts +0 -180
- package/src/core/Scanner.ts +0 -183
- package/src/core/Scheduler.ts +0 -105
- package/src/core/State.ts +0 -86
- package/src/core/Synthesizer.ts +0 -196
- package/src/core/constants.ts +0 -70
- package/src/core/presets.ts +0 -166
- package/src/core/types.ts +0 -54
- package/src/core/utils.ts +0 -47
- package/src/core/wav.ts +0 -60
- package/src/index.ts +0 -9
- package/src/ui/Renderer.ts +0 -145
- package/src/ui/Tweener.ts +0 -71
package/src/core/AudioEngine.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import { State } from "./State";
|
|
2
|
-
import { clamp } from "./utils";
|
|
3
|
-
|
|
4
|
-
export class AudioEngine {
|
|
5
|
-
private _ctx: BaseAudioContext | null = null;
|
|
6
|
-
private _master: GainNode | null = null;
|
|
7
|
-
private _analyser: AnalyserNode | null = null;
|
|
8
|
-
private _conv: ConvolverNode | null = null;
|
|
9
|
-
private _wet: GainNode | null = null;
|
|
10
|
-
private _dry: GainNode | null = null;
|
|
11
|
-
|
|
12
|
-
public get ctx(): BaseAudioContext | null { return this._ctx; }
|
|
13
|
-
public get master(): GainNode | null { return this._master; }
|
|
14
|
-
public get analyser(): AnalyserNode | null { return this._analyser; }
|
|
15
|
-
public get conv(): ConvolverNode | null { return this._conv; }
|
|
16
|
-
public get wet(): GainNode | null { return this._wet; }
|
|
17
|
-
public get dry(): GainNode | null { return this._dry; }
|
|
18
|
-
|
|
19
|
-
public ensure(state: State): void {
|
|
20
|
-
if (this._ctx) return;
|
|
21
|
-
const Ctx = window.AudioContext ?? (window as any).webkitAudioContext;
|
|
22
|
-
if (!Ctx) return;
|
|
23
|
-
this.buildGraph(new Ctx(), state);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public ensureOffline(ctx: OfflineAudioContext, state: State): void {
|
|
27
|
-
this.buildGraph(ctx, state);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
private buildGraph(ctx: BaseAudioContext, state: State): void {
|
|
31
|
-
const master = ctx.createGain();
|
|
32
|
-
const analyser = ctx.createAnalyser();
|
|
33
|
-
const dry = ctx.createGain();
|
|
34
|
-
const wet = ctx.createGain();
|
|
35
|
-
const conv = ctx.createConvolver();
|
|
36
|
-
|
|
37
|
-
master.gain.value = Math.pow(state.volume ?? 0.8, 2);
|
|
38
|
-
analyser.fftSize = 1024;
|
|
39
|
-
analyser.smoothingTimeConstant = 0.88;
|
|
40
|
-
conv.buffer = this.makeImpulse(ctx, state.reverb_time, 3);
|
|
41
|
-
wet.gain.value = state.reverb_mix;
|
|
42
|
-
dry.gain.value = this.dryVal(state.reverb_mix);
|
|
43
|
-
|
|
44
|
-
conv.connect(wet);
|
|
45
|
-
dry.connect(analyser);
|
|
46
|
-
wet.connect(analyser);
|
|
47
|
-
analyser.connect(master);
|
|
48
|
-
master.connect(ctx.destination);
|
|
49
|
-
|
|
50
|
-
this._ctx = ctx;
|
|
51
|
-
this._master = master;
|
|
52
|
-
this._analyser = analyser;
|
|
53
|
-
this._conv = conv;
|
|
54
|
-
this._wet = wet;
|
|
55
|
-
this._dry = dry;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public makeImpulse(ctx: BaseAudioContext, dur: number, decay: number): AudioBuffer {
|
|
59
|
-
const len = Math.max(1, Math.floor(ctx.sampleRate * dur));
|
|
60
|
-
const buf = ctx.createBuffer(2, len, ctx.sampleRate);
|
|
61
|
-
for (let ch = 0; ch < 2; ch++) {
|
|
62
|
-
const d = buf.getChannelData(ch);
|
|
63
|
-
for (let i = 0; i < len; i++) {
|
|
64
|
-
d[i] = (Math.random() * 2 - 1) * Math.pow(1 - i / len, decay);
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return buf;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
public dryVal(mix: number): number {
|
|
71
|
-
return mix >= 0.99 ? 0 : 1 - mix;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
public smooth(param: AudioParam, value: number, tau: number = 0.02): void {
|
|
75
|
-
if (!this._ctx || !isFinite(value)) return;
|
|
76
|
-
param.setTargetAtTime(value, this._ctx.currentTime, tau);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public async resume(): Promise<void> {
|
|
80
|
-
if (this._ctx instanceof AudioContext && this._ctx.state === "suspended") {
|
|
81
|
-
await this._ctx.resume();
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
public suspend(): void {
|
|
86
|
-
if (this._ctx instanceof AudioContext && this._ctx.state === "running") {
|
|
87
|
-
this._ctx.suspend().catch(() => {});
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
public async warmUp(state: State): Promise<void> {
|
|
92
|
-
this.ensure(state);
|
|
93
|
-
if (!(this._ctx instanceof AudioContext)) return;
|
|
94
|
-
await this.resume();
|
|
95
|
-
|
|
96
|
-
const osc = this._ctx.createOscillator();
|
|
97
|
-
const g = this._ctx.createGain();
|
|
98
|
-
g.gain.value = 0.00001;
|
|
99
|
-
osc.connect(g).connect(this._master!);
|
|
100
|
-
osc.start();
|
|
101
|
-
osc.stop(this._ctx.currentTime + 0.15);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
public updateReverbBuffer(state: State): void {
|
|
105
|
-
if (!this._ctx || !this._conv) return;
|
|
106
|
-
this._conv.buffer = this.makeImpulse(this._ctx, state.reverb_time, 3);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import { BlockData, Point, ScaleDefinition } from "./types";
|
|
2
|
-
import { clamp, rgbToHsl } from "./utils";
|
|
3
|
-
import { SCALES } from "./constants";
|
|
4
|
-
import { State } from "./State";
|
|
5
|
-
|
|
6
|
-
export class ImageProcessor {
|
|
7
|
-
private _canvas: HTMLCanvasElement | null = null;
|
|
8
|
-
private _pixels: Uint8ClampedArray | null = null;
|
|
9
|
-
private _w: number = 0;
|
|
10
|
-
private _h: number = 0;
|
|
11
|
-
|
|
12
|
-
public get canvas(): HTMLCanvasElement | null {
|
|
13
|
-
return this._canvas;
|
|
14
|
-
}
|
|
15
|
-
public get pixels(): Uint8ClampedArray | null {
|
|
16
|
-
return this._pixels;
|
|
17
|
-
}
|
|
18
|
-
public get width(): number {
|
|
19
|
-
return this._w;
|
|
20
|
-
}
|
|
21
|
-
public get height(): number {
|
|
22
|
-
return this._h;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
public processImage(img: HTMLImageElement): void {
|
|
26
|
-
const maxD = 2048;
|
|
27
|
-
const ds = Math.min(maxD / img.naturalWidth, maxD / img.naturalHeight, 1);
|
|
28
|
-
const dw = Math.max(1, Math.floor(img.naturalWidth * ds));
|
|
29
|
-
const dh = Math.max(1, Math.floor(img.naturalHeight * ds));
|
|
30
|
-
const cv = document.createElement("canvas");
|
|
31
|
-
cv.width = dw;
|
|
32
|
-
cv.height = dh;
|
|
33
|
-
const cx = cv.getContext("2d")!;
|
|
34
|
-
cx.imageSmoothingEnabled = true;
|
|
35
|
-
cx.drawImage(img, 0, 0, dw, dh);
|
|
36
|
-
this._canvas = cv;
|
|
37
|
-
|
|
38
|
-
const maxA = 512;
|
|
39
|
-
const as = Math.min(maxA / img.naturalWidth, maxA / img.naturalHeight, 1);
|
|
40
|
-
const aw = Math.max(1, Math.floor(img.naturalWidth * as));
|
|
41
|
-
const ah = Math.max(1, Math.floor(img.naturalHeight * as));
|
|
42
|
-
const av = document.createElement("canvas");
|
|
43
|
-
av.width = aw;
|
|
44
|
-
av.height = ah;
|
|
45
|
-
const ax = av.getContext("2d", { willReadFrequently: true })!;
|
|
46
|
-
ax.imageSmoothingEnabled = true;
|
|
47
|
-
ax.drawImage(img, 0, 0, aw, ah);
|
|
48
|
-
|
|
49
|
-
const px = ax.getImageData(0, 0, aw, ah);
|
|
50
|
-
this._pixels = px.data;
|
|
51
|
-
this._w = aw;
|
|
52
|
-
this._h = ah;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
public loadDefault(src: string, onLoad: (img: HTMLImageElement) => void): void {
|
|
56
|
-
const img = new Image();
|
|
57
|
-
img.onload = () => onLoad(img);
|
|
58
|
-
img.onerror = () => {};
|
|
59
|
-
img.src = src;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export class BlockGrid {
|
|
64
|
-
private _list: BlockData[] = [];
|
|
65
|
-
private _cols: number = 0;
|
|
66
|
-
private _rows: number = 0;
|
|
67
|
-
private _spiralPath: Point[] = [];
|
|
68
|
-
|
|
69
|
-
public get list(): BlockData[] {
|
|
70
|
-
return this._list;
|
|
71
|
-
}
|
|
72
|
-
public get cols(): number {
|
|
73
|
-
return this._cols;
|
|
74
|
-
}
|
|
75
|
-
public get rows(): number {
|
|
76
|
-
return this._rows;
|
|
77
|
-
}
|
|
78
|
-
public get spiralPath(): Point[] {
|
|
79
|
-
return this._spiralPath;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public build(imageProcessor: ImageProcessor, state: State): void {
|
|
83
|
-
this._list = [];
|
|
84
|
-
this._cols = 0;
|
|
85
|
-
this._rows = 0;
|
|
86
|
-
|
|
87
|
-
if (!imageProcessor.pixels) return;
|
|
88
|
-
|
|
89
|
-
const sc: ScaleDefinition = state.scale ?? SCALES[0];
|
|
90
|
-
const bw = Math.max(1, Math.floor(state.block_w));
|
|
91
|
-
const bh = Math.max(1, Math.floor(state.block_h));
|
|
92
|
-
const cols = Math.max(1, Math.floor(imageProcessor.width / bw));
|
|
93
|
-
const rows = Math.max(1, Math.floor(imageProcessor.height / bh));
|
|
94
|
-
this._cols = cols;
|
|
95
|
-
this._rows = rows;
|
|
96
|
-
|
|
97
|
-
const nDeg = sc.intervals.length - 1;
|
|
98
|
-
const pixels = imageProcessor.pixels;
|
|
99
|
-
const imgW = imageProcessor.width;
|
|
100
|
-
|
|
101
|
-
for (let ry = 0; ry < rows; ry++) {
|
|
102
|
-
for (let rx = 0; rx < cols; rx++) {
|
|
103
|
-
const x0 = rx * bw;
|
|
104
|
-
const y0 = ry * bh;
|
|
105
|
-
const x1 = Math.min(x0 + bw, imageProcessor.width);
|
|
106
|
-
const y1 = Math.min(y0 + bh, imageProcessor.height);
|
|
107
|
-
let rs = 0,
|
|
108
|
-
gs = 0,
|
|
109
|
-
bs = 0,
|
|
110
|
-
n = 0;
|
|
111
|
-
|
|
112
|
-
for (let y = y0; y < y1; y++) {
|
|
113
|
-
for (let x = x0; x < x1; x++) {
|
|
114
|
-
const i = (y * imgW + x) * 4;
|
|
115
|
-
rs += pixels[i];
|
|
116
|
-
gs += pixels[i + 1];
|
|
117
|
-
bs += pixels[i + 2];
|
|
118
|
-
n++;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
if (n === 0) continue;
|
|
123
|
-
|
|
124
|
-
const { h, s_hsv, l } = rgbToHsl(rs / n, gs / n, bs / n);
|
|
125
|
-
const safeH = isFinite(h) ? h : 0;
|
|
126
|
-
const degree = clamp(
|
|
127
|
-
Math.floor(Math.sqrt(safeH / 360) * nDeg),
|
|
128
|
-
0,
|
|
129
|
-
nDeg,
|
|
130
|
-
);
|
|
131
|
-
const oct = Math.floor((l / 100) * 3) - 1;
|
|
132
|
-
const vel = clamp(0.2 + (l / 100) * 0.5 + s_hsv * 0.3, 0.1, 1);
|
|
133
|
-
const pan = (safeH / 360) * 2 - 1;
|
|
134
|
-
this._list.push({ degree, oct, vel, l, pan });
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
this._spiralPath = this.buildSpiralPath(cols, rows);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
private buildSpiralPath(cols: number, rows: number): Point[] {
|
|
142
|
-
const cx = Math.floor(cols / 2);
|
|
143
|
-
const cy = Math.floor(rows / 2);
|
|
144
|
-
const path: Point[] = [];
|
|
145
|
-
const visited = new Set<string>();
|
|
146
|
-
let x = cx,
|
|
147
|
-
y = cy;
|
|
148
|
-
let step = 1,
|
|
149
|
-
dir = 0;
|
|
150
|
-
const ddx = [1, 0, -1, 0];
|
|
151
|
-
const ddy = [0, 1, 0, -1];
|
|
152
|
-
|
|
153
|
-
while (path.length < cols * rows) {
|
|
154
|
-
for (let leg = 0; leg < 2; leg++) {
|
|
155
|
-
for (let i = 0; i < step; i++) {
|
|
156
|
-
const key = `${x},${y}`;
|
|
157
|
-
if (x >= 0 && x < cols && y >= 0 && y < rows && !visited.has(key)) {
|
|
158
|
-
path.push({ x, y });
|
|
159
|
-
visited.add(key);
|
|
160
|
-
}
|
|
161
|
-
x += ddx[dir];
|
|
162
|
-
y += ddy[dir];
|
|
163
|
-
}
|
|
164
|
-
dir = (dir + 1) % 4;
|
|
165
|
-
}
|
|
166
|
-
step++;
|
|
167
|
-
if (path.length >= cols * rows) break;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (path.length < cols * rows) {
|
|
171
|
-
for (let fy = 0; fy < rows; fy++) {
|
|
172
|
-
for (let fx = 0; fx < cols; fx++) {
|
|
173
|
-
if (!visited.has(`${fx},${fy}`)) path.push({ x: fx, y: fy });
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return path;
|
|
179
|
-
}
|
|
180
|
-
}
|
package/src/core/Scanner.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import { BlockData, NoteData, ScaleDefinition } from "./types";
|
|
2
|
-
import { clamp, midiToFreq, freqToMidi } from "./utils";
|
|
3
|
-
import { SCALES } from "./constants";
|
|
4
|
-
import { State } from "./State";
|
|
5
|
-
import { BlockGrid } from "./ImageProcessor";
|
|
6
|
-
|
|
7
|
-
export abstract class BaseScanStrategy {
|
|
8
|
-
abstract advance(seq: Sequencer, cols: number, rows: number): void;
|
|
9
|
-
abstract reset(seq: Sequencer, grid: BlockGrid): void;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
class LinearScan extends BaseScanStrategy {
|
|
13
|
-
advance(seq: Sequencer, cols: number, rows: number): void {
|
|
14
|
-
seq.x++;
|
|
15
|
-
if (seq.x >= cols) {
|
|
16
|
-
seq.x = 0;
|
|
17
|
-
seq.y = (seq.y + 1) % rows;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
reset(seq: Sequencer, _grid: BlockGrid): void {
|
|
21
|
-
seq.x = 0;
|
|
22
|
-
seq.y = 0;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
class VerticalScan extends BaseScanStrategy {
|
|
27
|
-
advance(seq: Sequencer, cols: number, rows: number): void {
|
|
28
|
-
seq.y++;
|
|
29
|
-
if (seq.y >= rows) {
|
|
30
|
-
seq.y = 0;
|
|
31
|
-
seq.x = (seq.x + 1) % cols;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
reset(seq: Sequencer, _grid: BlockGrid): void {
|
|
35
|
-
seq.x = 0;
|
|
36
|
-
seq.y = 0;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
class ZigzagScan extends BaseScanStrategy {
|
|
41
|
-
advance(seq: Sequencer, cols: number, rows: number): void {
|
|
42
|
-
seq.x += seq.dx;
|
|
43
|
-
if (seq.x >= cols) {
|
|
44
|
-
seq.x = cols - 1;
|
|
45
|
-
seq.dx = -1;
|
|
46
|
-
seq.y = (seq.y + 1) % rows;
|
|
47
|
-
} else if (seq.x < 0) {
|
|
48
|
-
seq.x = 0;
|
|
49
|
-
seq.dx = 1;
|
|
50
|
-
seq.y = (seq.y + 1) % rows;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
reset(seq: Sequencer, _grid: BlockGrid): void {
|
|
54
|
-
seq.x = 0;
|
|
55
|
-
seq.y = 0;
|
|
56
|
-
seq.dx = 1;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
class DriftScan extends BaseScanStrategy {
|
|
61
|
-
advance(seq: Sequencer, cols: number, rows: number): void {
|
|
62
|
-
const l = seq.lastL ?? 50;
|
|
63
|
-
let dx = 0, dy = 0;
|
|
64
|
-
|
|
65
|
-
if (Math.random() < 0.05) {
|
|
66
|
-
seq.x = Math.floor(Math.random() * cols);
|
|
67
|
-
seq.y = Math.floor(Math.random() * rows);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (l > 60) dx = 1;
|
|
72
|
-
else if (l < 40) dx = -1;
|
|
73
|
-
else dy = Math.random() < 0.5 ? 1 : -1;
|
|
74
|
-
|
|
75
|
-
if (Math.random() < 0.15) {
|
|
76
|
-
dx = Math.floor(Math.random() * 3) - 1;
|
|
77
|
-
dy = Math.floor(Math.random() * 3) - 1;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
seq.x = (seq.x + dx + cols) % cols;
|
|
81
|
-
seq.y = (seq.y + dy + rows) % rows;
|
|
82
|
-
}
|
|
83
|
-
reset(seq: Sequencer, _grid: BlockGrid): void {
|
|
84
|
-
seq.x = 0;
|
|
85
|
-
seq.y = 0;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
class SpiralScan extends BaseScanStrategy {
|
|
90
|
-
advance(seq: Sequencer, _cols: number, _rows: number): void {
|
|
91
|
-
const path = seq.grid.spiralPath;
|
|
92
|
-
if (!path.length) return;
|
|
93
|
-
seq.spiralIdx = (seq.spiralIdx + 1) % path.length;
|
|
94
|
-
const sp = path[seq.spiralIdx];
|
|
95
|
-
seq.x = sp.x;
|
|
96
|
-
seq.y = sp.y;
|
|
97
|
-
}
|
|
98
|
-
reset(seq: Sequencer, grid: BlockGrid): void {
|
|
99
|
-
seq.spiralIdx = 0;
|
|
100
|
-
if (grid.spiralPath.length) {
|
|
101
|
-
seq.x = grid.spiralPath[0].x;
|
|
102
|
-
seq.y = grid.spiralPath[0].y;
|
|
103
|
-
} else {
|
|
104
|
-
seq.x = 0;
|
|
105
|
-
seq.y = 0;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export class ScanStrategyFactory {
|
|
111
|
-
private static strategies: Record<string, BaseScanStrategy> = {
|
|
112
|
-
linear: new LinearScan(),
|
|
113
|
-
vertical: new VerticalScan(),
|
|
114
|
-
zigzag: new ZigzagScan(),
|
|
115
|
-
drift: new DriftScan(),
|
|
116
|
-
spiral: new SpiralScan(),
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
public static get(mode: string): BaseScanStrategy {
|
|
120
|
-
return this.strategies[mode] ?? this.strategies["linear"];
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export class Sequencer {
|
|
125
|
-
public x: number = 0;
|
|
126
|
-
public y: number = 0;
|
|
127
|
-
public dx: number = 1;
|
|
128
|
-
public visX: number = 0;
|
|
129
|
-
public visY: number = 0;
|
|
130
|
-
public renderX: number = 0;
|
|
131
|
-
public renderY: number = 0;
|
|
132
|
-
public visIdx: number = 0;
|
|
133
|
-
public nextTime: number = 0;
|
|
134
|
-
public raf: number | null = null;
|
|
135
|
-
public lastL: number = 50;
|
|
136
|
-
public active: boolean = false;
|
|
137
|
-
public spiralIdx: number = 0;
|
|
138
|
-
public activateAt: number = 0;
|
|
139
|
-
|
|
140
|
-
constructor(public readonly grid: BlockGrid) {}
|
|
141
|
-
|
|
142
|
-
public resetPosition(state: State): void {
|
|
143
|
-
const strategy = ScanStrategyFactory.get(state.scan_mode);
|
|
144
|
-
strategy.reset(this, this.grid);
|
|
145
|
-
this.dx = 1;
|
|
146
|
-
this.visX = 0;
|
|
147
|
-
this.visY = 0;
|
|
148
|
-
this.renderX = 0;
|
|
149
|
-
this.renderY = 0;
|
|
150
|
-
this.visIdx = 0;
|
|
151
|
-
this.lastL = 50;
|
|
152
|
-
this.active = false;
|
|
153
|
-
this.spiralIdx = 0;
|
|
154
|
-
this.activateAt = 0;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
public currentBlockIdx(): number {
|
|
158
|
-
const idx = this.y * this.grid.cols + this.x;
|
|
159
|
-
if (!isFinite(idx)) return 0;
|
|
160
|
-
return clamp(idx, 0, this.grid.list.length - 1);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
public advance(state: State): void {
|
|
164
|
-
const cols = this.grid.cols;
|
|
165
|
-
const rows = this.grid.rows;
|
|
166
|
-
if (cols <= 0 || rows <= 0) return;
|
|
167
|
-
const strategy = ScanStrategyFactory.get(state.scan_mode);
|
|
168
|
-
strategy.advance(this, cols, rows);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
public clampToGrid(): void {
|
|
172
|
-
this.x = Math.min(this.x, Math.max(0, this.grid.cols - 1));
|
|
173
|
-
this.y = Math.min(this.y, Math.max(0, this.grid.rows - 1));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
public static mapBlockToNote(block: BlockData, sc: ScaleDefinition, state: State): NoteData {
|
|
177
|
-
const interval = sc.intervals[block.degree % sc.intervals.length] + 12 * block.oct;
|
|
178
|
-
const hueDetune = block.pan * 5;
|
|
179
|
-
const freq = midiToFreq(freqToMidi(state.base_note) + interval + hueDetune / 100);
|
|
180
|
-
const pan = block.pan * state.pan_amount;
|
|
181
|
-
return { freq, vel: block.vel, brightness: block.l / 100, pan };
|
|
182
|
-
}
|
|
183
|
-
}
|
package/src/core/Scheduler.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { State } from "./State";
|
|
2
|
-
import { AudioEngine } from "./AudioEngine";
|
|
3
|
-
import { Synthesizer } from "./Synthesizer";
|
|
4
|
-
import { Sequencer } from "./Scanner";
|
|
5
|
-
import { BlockGrid } from "./ImageProcessor";
|
|
6
|
-
import { SCALES } from "./constants";
|
|
7
|
-
import { clamp, midiToFreq, freqToMidi } from "./utils";
|
|
8
|
-
|
|
9
|
-
export class Scheduler {
|
|
10
|
-
private raf: number | null = null;
|
|
11
|
-
|
|
12
|
-
constructor(
|
|
13
|
-
private readonly state: State,
|
|
14
|
-
private readonly engine: AudioEngine,
|
|
15
|
-
private readonly synth: Synthesizer,
|
|
16
|
-
private readonly seq: Sequencer,
|
|
17
|
-
private readonly grid: BlockGrid
|
|
18
|
-
) {}
|
|
19
|
-
|
|
20
|
-
public start(): void {
|
|
21
|
-
if (!this.engine.ctx) return;
|
|
22
|
-
this.seq.nextTime = this.engine.ctx.currentTime + 0.03;
|
|
23
|
-
if (this.raf) cancelAnimationFrame(this.raf);
|
|
24
|
-
this.tick();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
public stop(): void {
|
|
28
|
-
if (this.raf) {
|
|
29
|
-
cancelAnimationFrame(this.raf);
|
|
30
|
-
this.raf = null;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public resync(): void {
|
|
35
|
-
if (!this.engine.ctx) return;
|
|
36
|
-
const now = this.engine.ctx.currentTime;
|
|
37
|
-
if (this.seq.nextTime < now - 0.02) this.seq.nextTime = now + 0.02;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
public renderRange(startTime: number, endTime: number): void {
|
|
41
|
-
if (!(this.grid.list.length && this.grid.cols && this.grid.rows)) return;
|
|
42
|
-
|
|
43
|
-
const stepDur = this.synth.getStepDur();
|
|
44
|
-
let time = startTime;
|
|
45
|
-
|
|
46
|
-
while (time < endTime) {
|
|
47
|
-
this.playStep(time);
|
|
48
|
-
if (this.state.timing_mode === "expressive") {
|
|
49
|
-
const speedMod = this.synth.getSpeedMod(this.seq.lastL ?? 50);
|
|
50
|
-
time += stepDur / speedMod;
|
|
51
|
-
} else {
|
|
52
|
-
time += stepDur;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
private tick = (): void => {
|
|
58
|
-
if (!this.state.playing || !this.engine.ctx) return;
|
|
59
|
-
|
|
60
|
-
const now = this.engine.ctx.currentTime;
|
|
61
|
-
if (this.seq.nextTime < now - 0.02) this.seq.nextTime = now;
|
|
62
|
-
|
|
63
|
-
const stepDur = this.synth.getStepDur();
|
|
64
|
-
const lookahead = clamp(stepDur * 2.5, 0.12, 0.3);
|
|
65
|
-
|
|
66
|
-
if (this.grid.list.length && this.grid.cols && this.grid.rows) {
|
|
67
|
-
while (this.seq.nextTime < now + lookahead) {
|
|
68
|
-
this.playStep(this.seq.nextTime);
|
|
69
|
-
if (this.state.timing_mode === "expressive") {
|
|
70
|
-
const speedMod = this.synth.getSpeedMod(this.seq.lastL ?? 50);
|
|
71
|
-
this.seq.nextTime += stepDur / speedMod;
|
|
72
|
-
} else {
|
|
73
|
-
this.seq.nextTime += stepDur;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
this.raf = requestAnimationFrame(this.tick);
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
private playStep(time: number): void {
|
|
82
|
-
const sc = this.state.scale ?? SCALES[0];
|
|
83
|
-
|
|
84
|
-
if (this.grid.list.length > 0) {
|
|
85
|
-
const idx = this.seq.currentBlockIdx();
|
|
86
|
-
const block = this.grid.list[idx];
|
|
87
|
-
if (block) {
|
|
88
|
-
this.seq.visIdx = idx;
|
|
89
|
-
this.seq.lastL = block.l;
|
|
90
|
-
const note = Sequencer.mapBlockToNote(block, sc, this.state);
|
|
91
|
-
this.synth.scheduleNote(note.freq, note.vel, note.brightness, note.pan, time);
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
const deg = Math.floor(((this.seq.x % 64) / 64) * (sc.intervals.length - 1));
|
|
95
|
-
const freq = midiToFreq(freqToMidi(this.state.base_note) + sc.intervals[deg]);
|
|
96
|
-
this.seq.lastL = 50;
|
|
97
|
-
this.synth.scheduleNote(freq, 0.6, 0.5, 0, time);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
this.seq.visX = this.seq.x;
|
|
101
|
-
this.seq.visY = this.seq.y;
|
|
102
|
-
if (this.seq.activateAt === 0) this.seq.activateAt = time;
|
|
103
|
-
this.seq.advance(this.state);
|
|
104
|
-
}
|
|
105
|
-
}
|
package/src/core/State.ts
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { PresetData, ScaleDefinition } from "./types";
|
|
2
|
-
import { SCALES, STATE_KEYS } from "./constants";
|
|
3
|
-
|
|
4
|
-
export class State implements PresetData {
|
|
5
|
-
public playing: boolean = false;
|
|
6
|
-
public scale: ScaleDefinition = SCALES[0];
|
|
7
|
-
|
|
8
|
-
public osc_type: string = "triangle";
|
|
9
|
-
public scale_name: string = "Minor";
|
|
10
|
-
public base_note: number = 287;
|
|
11
|
-
public bpm: number = 70;
|
|
12
|
-
public speed: number = 4.2;
|
|
13
|
-
public note_mult: number = 0.15;
|
|
14
|
-
public volume: number = 0.99;
|
|
15
|
-
public vibrato_rate: number = 0;
|
|
16
|
-
public vibrato_depth: number = 50;
|
|
17
|
-
public tremolo_rate: number = 1.8;
|
|
18
|
-
public tremolo_depth: number = 0;
|
|
19
|
-
public fifth_lvl: number = 0.37;
|
|
20
|
-
public oct_lvl: number = 0.63;
|
|
21
|
-
public filter_base: number = 1810;
|
|
22
|
-
public filter_track: number = 0;
|
|
23
|
-
public resonance: number = 2.6;
|
|
24
|
-
public reverb_mix: number = 1.0;
|
|
25
|
-
public reverb_time: number = 3.6;
|
|
26
|
-
public bass_gain: number = 13.5;
|
|
27
|
-
public block_w: number = 23;
|
|
28
|
-
public block_h: number = 13;
|
|
29
|
-
public scan_mode: string = "linear";
|
|
30
|
-
public pan_amount: number = 0.6;
|
|
31
|
-
public timing_mode: string = "expressive";
|
|
32
|
-
public timing_intensity: number = 0.4;
|
|
33
|
-
|
|
34
|
-
public applyPatch(patch: Partial<PresetData>): void {
|
|
35
|
-
if (patch.osc_type) this.osc_type = patch.osc_type;
|
|
36
|
-
if (patch.scale_name) {
|
|
37
|
-
this.scale_name = patch.scale_name;
|
|
38
|
-
this.scale = SCALES.find((s) => s.name === patch.scale_name) ?? SCALES[0];
|
|
39
|
-
}
|
|
40
|
-
if (patch.scan_mode) this.scan_mode = patch.scan_mode;
|
|
41
|
-
if (patch.timing_mode) this.timing_mode = patch.timing_mode;
|
|
42
|
-
if (typeof patch.bpm === "number") this.bpm = patch.bpm;
|
|
43
|
-
if (typeof patch.speed === "number") this.speed = patch.speed;
|
|
44
|
-
if (typeof patch.note_mult === "number") this.note_mult = patch.note_mult;
|
|
45
|
-
if (typeof patch.timing_intensity === "number") this.timing_intensity = patch.timing_intensity;
|
|
46
|
-
if (typeof patch.block_w === "number") this.block_w = patch.block_w;
|
|
47
|
-
if (typeof patch.block_h === "number") this.block_h = patch.block_h;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public applyAll(patch: Partial<PresetData>): void {
|
|
51
|
-
STATE_KEYS.forEach((k) => {
|
|
52
|
-
const value = patch[k];
|
|
53
|
-
if (value === undefined) return;
|
|
54
|
-
if (k === "scale_name" && typeof value === "string") {
|
|
55
|
-
this.scale_name = value;
|
|
56
|
-
this.scale = SCALES.find((s) => s.name === value) ?? SCALES[0];
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
(this as unknown as Record<string, unknown>)[k] = value;
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
public capture(): Partial<PresetData> {
|
|
64
|
-
const snap: Record<string, unknown> = {};
|
|
65
|
-
STATE_KEYS.forEach((k) => {
|
|
66
|
-
snap[k] = (this as unknown as Record<string, unknown>)[k];
|
|
67
|
-
});
|
|
68
|
-
return snap as Partial<PresetData>;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public setNumeric(key: string, value: number): void {
|
|
72
|
-
(this as unknown as Record<string, unknown>)[key] = value;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public getNumeric(key: string): number {
|
|
76
|
-
return (this as unknown as Record<string, unknown>)[key] as number;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public getString(key: string): string {
|
|
80
|
-
return (this as unknown as Record<string, unknown>)[key] as string;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
public setString(key: string, value: string): void {
|
|
84
|
-
(this as unknown as Record<string, unknown>)[key] = value;
|
|
85
|
-
}
|
|
86
|
-
}
|