audiora 1.0.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 +29 -0
- package/LICENSE +21 -0
- package/README.md +104 -0
- package/docs/API.md +343 -0
- package/package.json +39 -0
- package/src/Audiora.ts +293 -0
- package/src/core/AudioEngine.ts +108 -0
- package/src/core/ImageProcessor.ts +180 -0
- package/src/core/Scanner.ts +183 -0
- package/src/core/Scheduler.ts +105 -0
- package/src/core/State.ts +86 -0
- package/src/core/Synthesizer.ts +196 -0
- package/src/core/constants.ts +70 -0
- package/src/core/presets.ts +166 -0
- package/src/core/types.ts +54 -0
- package/src/core/utils.ts +47 -0
- package/src/core/wav.ts +60 -0
- package/src/index.ts +9 -0
- package/src/ui/Renderer.ts +145 -0
- package/src/ui/Tweener.ts +67 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { PresetData } from "./types";
|
|
2
|
+
|
|
3
|
+
export const PRESETS: Record<string, PresetData> = {
|
|
4
|
+
Haze: {
|
|
5
|
+
osc_type: "triangle",
|
|
6
|
+
scale_name: "Pentatonic",
|
|
7
|
+
base_note: 220,
|
|
8
|
+
bpm: 92,
|
|
9
|
+
speed: 1.7,
|
|
10
|
+
note_mult: 0.55,
|
|
11
|
+
volume: 0.85,
|
|
12
|
+
vibrato_rate: 3.2,
|
|
13
|
+
vibrato_depth: 14,
|
|
14
|
+
tremolo_rate: 2.2,
|
|
15
|
+
tremolo_depth: 0.18,
|
|
16
|
+
fifth_lvl: 0.25,
|
|
17
|
+
oct_lvl: 0.2,
|
|
18
|
+
filter_base: 900,
|
|
19
|
+
filter_track: 2200,
|
|
20
|
+
resonance: 1.7,
|
|
21
|
+
reverb_mix: 0.45,
|
|
22
|
+
reverb_time: 2.4,
|
|
23
|
+
bass_gain: 4,
|
|
24
|
+
block_w: 20,
|
|
25
|
+
block_h: 20,
|
|
26
|
+
scan_mode: "zigzag",
|
|
27
|
+
pan_amount: 0.65,
|
|
28
|
+
timing_mode: "expressive",
|
|
29
|
+
timing_intensity: 0.7,
|
|
30
|
+
},
|
|
31
|
+
Leap: {
|
|
32
|
+
osc_type: "square",
|
|
33
|
+
scale_name: "Major",
|
|
34
|
+
base_note: 330,
|
|
35
|
+
bpm: 150,
|
|
36
|
+
speed: 5,
|
|
37
|
+
note_mult: 0.28,
|
|
38
|
+
volume: 0.7,
|
|
39
|
+
vibrato_rate: 6,
|
|
40
|
+
vibrato_depth: 4,
|
|
41
|
+
tremolo_rate: 12,
|
|
42
|
+
tremolo_depth: 0.25,
|
|
43
|
+
fifth_lvl: 0.5,
|
|
44
|
+
oct_lvl: 0.2,
|
|
45
|
+
filter_base: 1200,
|
|
46
|
+
filter_track: 1500,
|
|
47
|
+
resonance: 0.9,
|
|
48
|
+
reverb_mix: 0.15,
|
|
49
|
+
reverb_time: 1.2,
|
|
50
|
+
bass_gain: 2,
|
|
51
|
+
block_w: 16,
|
|
52
|
+
block_h: 16,
|
|
53
|
+
scan_mode: "zigzag",
|
|
54
|
+
pan_amount: 0.8,
|
|
55
|
+
timing_mode: "stable",
|
|
56
|
+
timing_intensity: 0.0,
|
|
57
|
+
},
|
|
58
|
+
Pulse: {
|
|
59
|
+
osc_type: "square",
|
|
60
|
+
scale_name: "Pentatonic",
|
|
61
|
+
base_note: 220,
|
|
62
|
+
bpm: 100,
|
|
63
|
+
speed: 3,
|
|
64
|
+
note_mult: 0.4,
|
|
65
|
+
volume: 0.7,
|
|
66
|
+
vibrato_rate: 0,
|
|
67
|
+
vibrato_depth: 0,
|
|
68
|
+
tremolo_rate: 8,
|
|
69
|
+
tremolo_depth: 0.6,
|
|
70
|
+
fifth_lvl: 0.4,
|
|
71
|
+
oct_lvl: 0.1,
|
|
72
|
+
filter_base: 800,
|
|
73
|
+
filter_track: 1200,
|
|
74
|
+
resonance: 1.5,
|
|
75
|
+
reverb_mix: 0.25,
|
|
76
|
+
reverb_time: 1.0,
|
|
77
|
+
bass_gain: 3,
|
|
78
|
+
block_w: 12,
|
|
79
|
+
block_h: 12,
|
|
80
|
+
scan_mode: "zigzag",
|
|
81
|
+
pan_amount: 0.6,
|
|
82
|
+
timing_mode: "stable",
|
|
83
|
+
timing_intensity: 0.0,
|
|
84
|
+
},
|
|
85
|
+
Bite: {
|
|
86
|
+
osc_type: "sawtooth",
|
|
87
|
+
scale_name: "Chromatic",
|
|
88
|
+
base_note: 110,
|
|
89
|
+
bpm: 130,
|
|
90
|
+
speed: 4,
|
|
91
|
+
note_mult: 0.3,
|
|
92
|
+
volume: 0.7,
|
|
93
|
+
vibrato_rate: 0,
|
|
94
|
+
vibrato_depth: 0,
|
|
95
|
+
tremolo_rate: 0,
|
|
96
|
+
tremolo_depth: 0,
|
|
97
|
+
fifth_lvl: 0,
|
|
98
|
+
oct_lvl: 0,
|
|
99
|
+
filter_base: 200,
|
|
100
|
+
filter_track: 5500,
|
|
101
|
+
resonance: 8.0,
|
|
102
|
+
reverb_mix: 0.1,
|
|
103
|
+
reverb_time: 0.8,
|
|
104
|
+
bass_gain: 10,
|
|
105
|
+
block_w: 8,
|
|
106
|
+
block_h: 8,
|
|
107
|
+
scan_mode: "linear",
|
|
108
|
+
pan_amount: 0.2,
|
|
109
|
+
timing_mode: "stable",
|
|
110
|
+
timing_intensity: 0.0,
|
|
111
|
+
},
|
|
112
|
+
Spark: {
|
|
113
|
+
osc_type: "square",
|
|
114
|
+
scale_name: "Chromatic",
|
|
115
|
+
base_note: 523,
|
|
116
|
+
bpm: 180,
|
|
117
|
+
speed: 6.0,
|
|
118
|
+
note_mult: 0.08,
|
|
119
|
+
volume: 0.6,
|
|
120
|
+
vibrato_rate: 0,
|
|
121
|
+
vibrato_depth: 0,
|
|
122
|
+
tremolo_rate: 20,
|
|
123
|
+
tremolo_depth: 0.4,
|
|
124
|
+
fifth_lvl: 0.0,
|
|
125
|
+
oct_lvl: 0.0,
|
|
126
|
+
filter_base: 3500,
|
|
127
|
+
filter_track: 5000,
|
|
128
|
+
resonance: 3.0,
|
|
129
|
+
reverb_mix: 0.1,
|
|
130
|
+
reverb_time: 0.5,
|
|
131
|
+
bass_gain: -6,
|
|
132
|
+
block_w: 6,
|
|
133
|
+
block_h: 6,
|
|
134
|
+
scan_mode: "linear",
|
|
135
|
+
pan_amount: 0.3,
|
|
136
|
+
timing_mode: "stable",
|
|
137
|
+
timing_intensity: 0.0,
|
|
138
|
+
},
|
|
139
|
+
Raw: {
|
|
140
|
+
osc_type: "square",
|
|
141
|
+
scale_name: "Major",
|
|
142
|
+
base_note: 220,
|
|
143
|
+
bpm: 110,
|
|
144
|
+
speed: 3.2,
|
|
145
|
+
note_mult: 0.22,
|
|
146
|
+
volume: 0.75,
|
|
147
|
+
vibrato_rate: 0,
|
|
148
|
+
vibrato_depth: 0,
|
|
149
|
+
tremolo_rate: 0,
|
|
150
|
+
tremolo_depth: 0,
|
|
151
|
+
fifth_lvl: 0.0,
|
|
152
|
+
oct_lvl: 0.0,
|
|
153
|
+
filter_base: 2400,
|
|
154
|
+
filter_track: 0,
|
|
155
|
+
resonance: 0.2,
|
|
156
|
+
reverb_mix: 0.0,
|
|
157
|
+
reverb_time: 0.8,
|
|
158
|
+
bass_gain: 0,
|
|
159
|
+
block_w: 40,
|
|
160
|
+
block_h: 40,
|
|
161
|
+
scan_mode: "linear",
|
|
162
|
+
pan_amount: 0.0,
|
|
163
|
+
timing_mode: "stable",
|
|
164
|
+
timing_intensity: 0.0,
|
|
165
|
+
},
|
|
166
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export interface ScaleDefinition {
|
|
2
|
+
name: string;
|
|
3
|
+
intervals: number[];
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface PresetData {
|
|
7
|
+
osc_type: string;
|
|
8
|
+
scale_name: string;
|
|
9
|
+
base_note: number;
|
|
10
|
+
bpm: number;
|
|
11
|
+
speed: number;
|
|
12
|
+
note_mult: number;
|
|
13
|
+
volume: number;
|
|
14
|
+
vibrato_rate: number;
|
|
15
|
+
vibrato_depth: number;
|
|
16
|
+
tremolo_rate: number;
|
|
17
|
+
tremolo_depth: number;
|
|
18
|
+
fifth_lvl: number;
|
|
19
|
+
oct_lvl: number;
|
|
20
|
+
filter_base: number;
|
|
21
|
+
filter_track: number;
|
|
22
|
+
resonance: number;
|
|
23
|
+
reverb_mix: number;
|
|
24
|
+
reverb_time: number;
|
|
25
|
+
bass_gain: number;
|
|
26
|
+
block_w: number;
|
|
27
|
+
block_h: number;
|
|
28
|
+
scan_mode: string;
|
|
29
|
+
pan_amount: number;
|
|
30
|
+
timing_mode: string;
|
|
31
|
+
timing_intensity: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface BlockData {
|
|
35
|
+
degree: number;
|
|
36
|
+
oct: number;
|
|
37
|
+
vel: number;
|
|
38
|
+
l: number;
|
|
39
|
+
pan: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface NoteData {
|
|
43
|
+
freq: number;
|
|
44
|
+
vel: number;
|
|
45
|
+
brightness: number;
|
|
46
|
+
pan: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface Point {
|
|
50
|
+
x: number;
|
|
51
|
+
y: number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type FormatFn = (v: number) => string;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export function clamp(v: number, lo: number, hi: number): number {
|
|
2
|
+
return Math.max(lo, Math.min(hi, v));
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export function midiToFreq(m: number): number {
|
|
6
|
+
return 440 * Math.pow(2, (m - 69) / 12);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function freqToMidi(f: number): number {
|
|
10
|
+
return 69 + 12 * Math.log2(f / 440);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface HSLResult {
|
|
14
|
+
h: number;
|
|
15
|
+
s_hsl: number;
|
|
16
|
+
s_hsv: number;
|
|
17
|
+
l: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function rgbToHsl(r: number, g: number, b: number): HSLResult {
|
|
21
|
+
r /= 255;
|
|
22
|
+
g /= 255;
|
|
23
|
+
b /= 255;
|
|
24
|
+
const max = Math.max(r, g, b);
|
|
25
|
+
const min = Math.min(r, g, b);
|
|
26
|
+
let h = 0;
|
|
27
|
+
let s_hsl = 0;
|
|
28
|
+
const l = (max + min) / 2;
|
|
29
|
+
const d = max - min;
|
|
30
|
+
|
|
31
|
+
if (d) {
|
|
32
|
+
s_hsl = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
33
|
+
switch (max) {
|
|
34
|
+
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
|
|
35
|
+
case g: h = (b - r) / d + 2; break;
|
|
36
|
+
case b: h = (r - g) / d + 4; break;
|
|
37
|
+
}
|
|
38
|
+
h *= 60;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const s_hsv = max === 0 ? 0 : d / max;
|
|
42
|
+
return { h, s_hsl, s_hsv, l: l * 100 };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function easeInOut(t: number): number {
|
|
46
|
+
return t < 0.5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
|
|
47
|
+
}
|
package/src/core/wav.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
const BYTES_PER_SAMPLE = 2;
|
|
2
|
+
const HEADER_SIZE = 44;
|
|
3
|
+
|
|
4
|
+
export function audioBufferToWavBlob(buffer: AudioBuffer): Blob {
|
|
5
|
+
const numChannels = buffer.numberOfChannels;
|
|
6
|
+
const blockAlign = numChannels * BYTES_PER_SAMPLE;
|
|
7
|
+
const dataLength = buffer.length * blockAlign;
|
|
8
|
+
|
|
9
|
+
const arrayBuffer = new ArrayBuffer(HEADER_SIZE + dataLength);
|
|
10
|
+
const view = new DataView(arrayBuffer);
|
|
11
|
+
|
|
12
|
+
writeWavHeader(view, buffer.sampleRate, numChannels, blockAlign, dataLength);
|
|
13
|
+
writeInterleavedSamples(view, buffer, numChannels);
|
|
14
|
+
|
|
15
|
+
return new Blob([arrayBuffer], { type: "audio/wav" });
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function writeWavHeader(
|
|
19
|
+
view: DataView,
|
|
20
|
+
sampleRate: number,
|
|
21
|
+
numChannels: number,
|
|
22
|
+
blockAlign: number,
|
|
23
|
+
dataLength: number
|
|
24
|
+
): void {
|
|
25
|
+
writeString(view, 0, "RIFF");
|
|
26
|
+
view.setUint32(4, 36 + dataLength, true);
|
|
27
|
+
writeString(view, 8, "WAVE");
|
|
28
|
+
writeString(view, 12, "fmt ");
|
|
29
|
+
view.setUint32(16, 16, true);
|
|
30
|
+
view.setUint16(20, 1, true);
|
|
31
|
+
view.setUint16(22, numChannels, true);
|
|
32
|
+
view.setUint32(24, sampleRate, true);
|
|
33
|
+
view.setUint32(28, sampleRate * blockAlign, true);
|
|
34
|
+
view.setUint16(32, blockAlign, true);
|
|
35
|
+
view.setUint16(34, 16, true);
|
|
36
|
+
writeString(view, 36, "data");
|
|
37
|
+
view.setUint32(40, dataLength, true);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function writeInterleavedSamples(view: DataView, buffer: AudioBuffer, numChannels: number): void {
|
|
41
|
+
const channelData: Float32Array[] = [];
|
|
42
|
+
for (let ch = 0; ch < numChannels; ch++) channelData.push(buffer.getChannelData(ch));
|
|
43
|
+
|
|
44
|
+
let offset = HEADER_SIZE;
|
|
45
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
46
|
+
for (let ch = 0; ch < numChannels; ch++) {
|
|
47
|
+
view.setInt16(offset, floatToPcm16(channelData[ch][i]), true);
|
|
48
|
+
offset += BYTES_PER_SAMPLE;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function floatToPcm16(sample: number): number {
|
|
54
|
+
const s = Math.max(-1, Math.min(1, sample));
|
|
55
|
+
return s < 0 ? s * 0x8000 : s * 0x7fff;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function writeString(view: DataView, offset: number, text: string): void {
|
|
59
|
+
for (let i = 0; i < text.length; i++) view.setUint8(offset + i, text.charCodeAt(i));
|
|
60
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { Audiora, type RenderOptions, type ImageSource, type LiveOptions } from "./Audiora";
|
|
2
|
+
export { PRESETS } from "./core/presets";
|
|
3
|
+
export {
|
|
4
|
+
DEFAULT_PRESET,
|
|
5
|
+
FORMAT,
|
|
6
|
+
RANGE_KEYS,
|
|
7
|
+
SELECT_KEYS,
|
|
8
|
+
} from "./core/constants";
|
|
9
|
+
export type { PresetData } from "./core/types";
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { AudioEngine } from "../core/AudioEngine";
|
|
2
|
+
import { ImageProcessor, BlockGrid } from "../core/ImageProcessor";
|
|
3
|
+
import { Sequencer } from "../core/Scanner";
|
|
4
|
+
|
|
5
|
+
export class Renderer {
|
|
6
|
+
private waveBuf: Uint8Array<ArrayBuffer> | null = null;
|
|
7
|
+
private drawT: number = 0;
|
|
8
|
+
private renderRaf: number | null = null;
|
|
9
|
+
|
|
10
|
+
private waveCanvas: HTMLCanvasElement;
|
|
11
|
+
private mapCanvas: HTMLCanvasElement;
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
waveCanvas: HTMLCanvasElement,
|
|
15
|
+
mapCanvas: HTMLCanvasElement,
|
|
16
|
+
private readonly engine: AudioEngine,
|
|
17
|
+
private readonly imageProcessor: ImageProcessor,
|
|
18
|
+
private readonly grid: BlockGrid,
|
|
19
|
+
private readonly seq: Sequencer
|
|
20
|
+
) {
|
|
21
|
+
this.waveCanvas = waveCanvas;
|
|
22
|
+
this.mapCanvas = mapCanvas;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public start(): void {
|
|
26
|
+
this.resizeCanvases();
|
|
27
|
+
this.renderFrame();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
public stop(): void {
|
|
31
|
+
if (this.renderRaf) {
|
|
32
|
+
cancelAnimationFrame(this.renderRaf);
|
|
33
|
+
this.renderRaf = null;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public resizeCanvases(): void {
|
|
38
|
+
const dpr = window.devicePixelRatio || 1;
|
|
39
|
+
[this.waveCanvas, this.mapCanvas].forEach((el) => {
|
|
40
|
+
if (!el) return;
|
|
41
|
+
const r = el.getBoundingClientRect();
|
|
42
|
+
el.width = Math.floor(r.width * dpr);
|
|
43
|
+
el.height = Math.floor(r.height * dpr);
|
|
44
|
+
el.getContext("2d")!.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private renderFrame = (): void => {
|
|
49
|
+
this.renderRaf = requestAnimationFrame(this.renderFrame);
|
|
50
|
+
const now = performance.now();
|
|
51
|
+
if (now - this.drawT < 1000 / 60) return;
|
|
52
|
+
this.drawT = now;
|
|
53
|
+
|
|
54
|
+
const waveSize = this.canvasCssSize(this.waveCanvas);
|
|
55
|
+
const mapSize = this.canvasCssSize(this.mapCanvas);
|
|
56
|
+
this.drawWave(waveSize.w, waveSize.h);
|
|
57
|
+
this.drawMap(mapSize.w, mapSize.h);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
private canvasCssSize(el: HTMLCanvasElement): { w: number; h: number } {
|
|
61
|
+
const r = el.getBoundingClientRect();
|
|
62
|
+
return { w: r.width, h: r.height };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private drawWave(W: number, H: number): void {
|
|
66
|
+
const ctx = this.waveCanvas.getContext("2d")!;
|
|
67
|
+
|
|
68
|
+
ctx.fillStyle = "#0d0d14";
|
|
69
|
+
ctx.fillRect(0, 0, W, H);
|
|
70
|
+
|
|
71
|
+
ctx.strokeStyle = "rgba(255,255,255,0.03)";
|
|
72
|
+
ctx.lineWidth = 1;
|
|
73
|
+
for (let x = 0; x < W; x += 40) {
|
|
74
|
+
ctx.beginPath();
|
|
75
|
+
ctx.moveTo(x + 0.5, 0);
|
|
76
|
+
ctx.lineTo(x + 0.5, H);
|
|
77
|
+
ctx.stroke();
|
|
78
|
+
}
|
|
79
|
+
for (let y = 0; y < H; y += 40) {
|
|
80
|
+
ctx.beginPath();
|
|
81
|
+
ctx.moveTo(0, y + 0.5);
|
|
82
|
+
ctx.lineTo(W, y + 0.5);
|
|
83
|
+
ctx.stroke();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (this.engine.analyser) {
|
|
87
|
+
if (!this.waveBuf || this.waveBuf.length !== this.engine.analyser.fftSize) {
|
|
88
|
+
this.waveBuf = new Uint8Array(this.engine.analyser.fftSize);
|
|
89
|
+
}
|
|
90
|
+
this.engine.analyser.getByteTimeDomainData(this.waveBuf);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const len = this.waveBuf?.length ?? 1024;
|
|
94
|
+
ctx.beginPath();
|
|
95
|
+
ctx.strokeStyle = "#ffffff";
|
|
96
|
+
ctx.lineWidth = 2;
|
|
97
|
+
for (let i = 0; i < len; i++) {
|
|
98
|
+
const v = this.waveBuf ? this.waveBuf[i] / 128.0 : 1.0;
|
|
99
|
+
const x = (i / (len - 1)) * W;
|
|
100
|
+
i === 0 ? ctx.moveTo(x, (v * H) / 2) : ctx.lineTo(x, (v * H) / 2);
|
|
101
|
+
}
|
|
102
|
+
ctx.stroke();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private drawMap(W: number, H: number): void {
|
|
106
|
+
const ctx = this.mapCanvas.getContext("2d")!;
|
|
107
|
+
ctx.clearRect(0, 0, W, H);
|
|
108
|
+
|
|
109
|
+
const src = this.imageProcessor.canvas;
|
|
110
|
+
if (!src) return;
|
|
111
|
+
|
|
112
|
+
const sc = Math.min(W / src.width, H / src.height);
|
|
113
|
+
const dw = src.width * sc;
|
|
114
|
+
const dh = src.height * sc;
|
|
115
|
+
const dx = (W - dw) / 2;
|
|
116
|
+
const dy = (H - dh) / 2;
|
|
117
|
+
|
|
118
|
+
ctx.save();
|
|
119
|
+
ctx.imageSmoothingEnabled = true;
|
|
120
|
+
ctx.drawImage(src, 0, 0, src.width, src.height, dx, dy, dw, dh);
|
|
121
|
+
|
|
122
|
+
if (!this.seq.active && this.seq.activateAt > 0 && this.engine.ctx &&
|
|
123
|
+
this.engine.ctx.currentTime >= this.seq.activateAt) {
|
|
124
|
+
this.seq.active = true;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (this.seq.active && this.grid.cols > 0 && this.grid.list.length > 0) {
|
|
128
|
+
const lt = 1;
|
|
129
|
+
this.seq.renderX = this.seq.renderX + (this.seq.visX - this.seq.renderX) * lt;
|
|
130
|
+
this.seq.renderY = this.seq.renderY + (this.seq.visY - this.seq.renderY) * lt;
|
|
131
|
+
const cw = dw / this.grid.cols;
|
|
132
|
+
const ch = dh / this.grid.rows;
|
|
133
|
+
const lw = Math.max(1.5, Math.min(cw, ch) * 0.06);
|
|
134
|
+
const cx_ = dx + this.seq.renderX * cw;
|
|
135
|
+
const cy_ = dy + this.seq.renderY * ch;
|
|
136
|
+
const rw = cw - 1;
|
|
137
|
+
const rh = ch - 1;
|
|
138
|
+
const light = this.seq.lastL > 50;
|
|
139
|
+
ctx.strokeStyle = light ? "rgba(30,30,30,0.85)" : "rgba(220,220,220,0.85)";
|
|
140
|
+
ctx.lineWidth = lw;
|
|
141
|
+
ctx.strokeRect(cx_ + 0.5, cy_ + 0.5, rw, rh);
|
|
142
|
+
}
|
|
143
|
+
ctx.restore();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { PresetData } from "../core/types";
|
|
2
|
+
import { State } from "../core/State";
|
|
3
|
+
import { AudioEngine } from "../core/AudioEngine";
|
|
4
|
+
import { TWEEN_KEYS, TWEEN_MS } from "../core/constants";
|
|
5
|
+
import { easeInOut } from "../core/utils";
|
|
6
|
+
|
|
7
|
+
export type TweenTickHandler = (key: string, value: number) => void;
|
|
8
|
+
|
|
9
|
+
export class Tweener {
|
|
10
|
+
private raf: number | null = null;
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
private readonly state: State,
|
|
14
|
+
private readonly engine: AudioEngine,
|
|
15
|
+
private readonly onTick?: TweenTickHandler
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
public tweenTo(target: Partial<PresetData>): void {
|
|
19
|
+
if (this.raf) {
|
|
20
|
+
cancelAnimationFrame(this.raf);
|
|
21
|
+
this.raf = null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const from: Record<string, number> = {};
|
|
25
|
+
TWEEN_KEYS.forEach((k) => {
|
|
26
|
+
const tVal = (target as Record<string, unknown>)[k];
|
|
27
|
+
if (typeof tVal === "number" && isFinite(tVal)) {
|
|
28
|
+
const current = this.state.getNumeric(k);
|
|
29
|
+
from[k] = isFinite(current) ? current : tVal;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const t0 = performance.now();
|
|
34
|
+
|
|
35
|
+
const tick = (): void => {
|
|
36
|
+
const t = Math.min((performance.now() - t0) / TWEEN_MS, 1);
|
|
37
|
+
const e = easeInOut(t);
|
|
38
|
+
|
|
39
|
+
TWEEN_KEYS.forEach((k) => {
|
|
40
|
+
if (from[k] === undefined) return;
|
|
41
|
+
const targetVal = (target as Record<string, unknown>)[k] as number;
|
|
42
|
+
const newVal = from[k] + (targetVal - from[k]) * e;
|
|
43
|
+
this.state.setNumeric(k, newVal);
|
|
44
|
+
this.onTick?.(k, newVal);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (isFinite(this.state.volume) && this.engine.master) {
|
|
48
|
+
this.engine.master.gain.value = Math.pow(this.state.volume, 2);
|
|
49
|
+
}
|
|
50
|
+
if (isFinite(this.state.reverb_mix) && this.engine.wet) {
|
|
51
|
+
this.engine.wet.gain.value = this.state.reverb_mix;
|
|
52
|
+
this.engine.dry!.gain.value = this.engine.dryVal(this.state.reverb_mix);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (t < 1) this.raf = requestAnimationFrame(tick);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
this.raf = requestAnimationFrame(tick);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public cancel(): void {
|
|
62
|
+
if (this.raf) {
|
|
63
|
+
cancelAnimationFrame(this.raf);
|
|
64
|
+
this.raf = null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|