audiora 1.0.0 → 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 +27 -0
- package/README.md +25 -71
- 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 +26 -7
- package/docs/API.md +0 -343
- package/src/Audiora.ts +0 -293
- 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 -67
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "audiora",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Picture-driven synthesis for the web — maps color and light into pitch, timing, and texture.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"audio",
|
|
@@ -15,25 +15,44 @@
|
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"author": "whosramoss",
|
|
17
17
|
"type": "module",
|
|
18
|
-
"main": "
|
|
19
|
-
"
|
|
18
|
+
"main": "./dist/index.cjs",
|
|
19
|
+
"module": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
20
21
|
"exports": {
|
|
21
|
-
".":
|
|
22
|
+
".": {
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/index.d.cts",
|
|
29
|
+
"default": "./dist/index.cjs"
|
|
30
|
+
}
|
|
31
|
+
}
|
|
22
32
|
},
|
|
23
33
|
"files": [
|
|
24
|
-
"
|
|
25
|
-
"docs",
|
|
34
|
+
"dist",
|
|
26
35
|
"CHANGELOG.md"
|
|
27
36
|
],
|
|
37
|
+
"sideEffects": false,
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsup",
|
|
40
|
+
"build:watch": "tsup --watch",
|
|
41
|
+
"dev": "tsup --watch",
|
|
42
|
+
"typecheck": "tsc --noEmit",
|
|
43
|
+
"prepare": "tsup",
|
|
44
|
+
"prepublishOnly": "npm run build"
|
|
45
|
+
},
|
|
28
46
|
"repository": {
|
|
29
47
|
"type": "git",
|
|
30
48
|
"url": "git+https://github.com/whosramoss/audiora.git"
|
|
31
49
|
},
|
|
32
|
-
"homepage": "https://
|
|
50
|
+
"homepage": "https://audiora.whosramoss.com",
|
|
33
51
|
"bugs": {
|
|
34
52
|
"url": "https://github.com/whosramoss/audiora/issues"
|
|
35
53
|
},
|
|
36
54
|
"devDependencies": {
|
|
55
|
+
"tsup": "^8.0.0",
|
|
37
56
|
"typescript": "^5.5.0"
|
|
38
57
|
}
|
|
39
58
|
}
|
package/docs/API.md
DELETED
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
# API Reference
|
|
2
|
-
|
|
3
|
-
## Package entry points
|
|
4
|
-
|
|
5
|
-
| Import | Description |
|
|
6
|
-
| --------- | ---------------------------------------------------------------------------------------------- |
|
|
7
|
-
| `audiora` | Main entry. Exports the `Audiora` class, presets, and helpers for live + offline picture-driven synthesis. |
|
|
8
|
-
|
|
9
|
-
There are no side-effectful subpaths. Importing `audiora` does not start AudioContext or scan the DOM.
|
|
10
|
-
|
|
11
|
-
---
|
|
12
|
-
|
|
13
|
-
## `audiora` exports
|
|
14
|
-
|
|
15
|
-
```ts
|
|
16
|
-
import {
|
|
17
|
-
Audiora,
|
|
18
|
-
PRESETS,
|
|
19
|
-
DEFAULT_PRESET,
|
|
20
|
-
FORMAT,
|
|
21
|
-
RANGE_KEYS,
|
|
22
|
-
SELECT_KEYS,
|
|
23
|
-
type RenderOptions,
|
|
24
|
-
type ImageSource,
|
|
25
|
-
type LiveOptions,
|
|
26
|
-
type PresetData,
|
|
27
|
-
} from "audiora";
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
| Export | Type | Description |
|
|
31
|
-
| ---------------- | ---------------------------- | -------------------------------------------------------------------------------- |
|
|
32
|
-
| `Audiora` | `class` | Picture-driven synthesis instrument (live playback + offline render). |
|
|
33
|
-
| `PRESETS` | `Record<string, PresetData>` | Built-in parameter patches (`Haze`, `Leap`, `Pulse`, `Bite`, `Spark`, `Raw`). |
|
|
34
|
-
| `DEFAULT_PRESET` | `string` | Recommended starting preset name (`"Haze"`). |
|
|
35
|
-
| `FORMAT` | `Record<string, (v: number) => string>` | Display formatters for numeric params (UI labels). |
|
|
36
|
-
| `RANGE_KEYS` | `(keyof PresetData)[]` | Numeric param keys that use range controls. |
|
|
37
|
-
| `SELECT_KEYS` | `(keyof PresetData)[]` | String/select param keys (`osc_type`, `scale_name`, `scan_mode`, `timing_mode`). |
|
|
38
|
-
| `RenderOptions` | `type` | Options for `toAudioBuffer` / `toBlob`. |
|
|
39
|
-
| `ImageSource` | `type` | `string`, `File`, `Blob`, or `HTMLImageElement`. |
|
|
40
|
-
| `LiveOptions` | `type` | Optional canvases + tween callback for the constructor. |
|
|
41
|
-
| `PresetData` | `type` | Full parameter set that drives synthesis and traversal. |
|
|
42
|
-
|
|
43
|
-
---
|
|
44
|
-
|
|
45
|
-
## Picture → sound mapping
|
|
46
|
-
|
|
47
|
-
Each image is downsampled, split into blocks (`block_w` × `block_h`), and traversed by a scan strategy. Per-block HSL drives the musical event:
|
|
48
|
-
|
|
49
|
-
| Source | Maps to |
|
|
50
|
-
| ---------------- | ----------------------------------------------------------------------- |
|
|
51
|
-
| Hue | Scale degree (pitch), stereo pan, micro-detune |
|
|
52
|
-
| Lightness | Octave, primary velocity weight, filter tracking; timing / note duration when expressive |
|
|
53
|
-
| Saturation | Secondary velocity weight |
|
|
54
|
-
| Spatial position | Traversal path (`scan_mode`) |
|
|
55
|
-
|
|
56
|
-
Bright areas slightly accelerate the system; darker areas slow it down. Timing is intentionally restrained so the result stays coherent rather than mechanical. Filter cutoff follows `filter_base + brightness × filter_track` (when `filter_track ≠ 0`).
|
|
57
|
-
|
|
58
|
-
Cross-origin image URLs need CORS (`crossOrigin = "anonymous"` is set for string sources). Blobs/`File` use object URLs and do not set CORS.
|
|
59
|
-
|
|
60
|
-
---
|
|
61
|
-
|
|
62
|
-
## `Audiora`
|
|
63
|
-
|
|
64
|
-
```js
|
|
65
|
-
import { Audiora, PRESETS } from "audiora";
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
### Constructor
|
|
69
|
-
|
|
70
|
-
```js
|
|
71
|
-
new Audiora(options?)
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
- **options** — Partial `LiveOptions`. If both canvases are provided, visuals attach immediately.
|
|
75
|
-
|
|
76
|
-
```ts
|
|
77
|
-
interface LiveOptions {
|
|
78
|
-
waveCanvas?: HTMLCanvasElement; // waveform / analyzer view
|
|
79
|
-
mapCanvas?: HTMLCanvasElement; // image + scan cursor
|
|
80
|
-
onTweenTick?: (key: string, value: number) => void; // fired while params tween
|
|
81
|
-
}
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### Instance properties
|
|
85
|
-
|
|
86
|
-
| Property | Type | Description |
|
|
87
|
-
| --------- | ------------------ | -------------------------------- |
|
|
88
|
-
| `playing` | `boolean` (getter) | Whether live playback is active. |
|
|
89
|
-
|
|
90
|
-
### Instance methods
|
|
91
|
-
|
|
92
|
-
| Method | Returns | Description |
|
|
93
|
-
| -------------------------------------- | ---------------------- | --------------------------------------------------------------------- |
|
|
94
|
-
| `attachVisuals(waveCanvas, mapCanvas)` | `void` | Starts (or restarts) the render loop on the given canvases. |
|
|
95
|
-
| `resizeVisuals()` | `void` | Recomputes canvas sizes after layout/window resize. |
|
|
96
|
-
| `loadImage(image)` | `Promise<void>` | Loads an `ImageSource`, processes pixels, rebuilds the block grid. |
|
|
97
|
-
| `play()` | `Promise<void>` | Ensures AudioContext, resumes it, starts the live scheduler. No notes until an image grid exists. |
|
|
98
|
-
| `stop()` | `void` | Stops the scheduler, resets sequencer timing, suspends the context. |
|
|
99
|
-
| `warmUp()` | `Promise<void>` | Primes the audio graph (helpful before first gesture-driven `play`). |
|
|
100
|
-
| `getParam(key)` | `string \| number` | Reads one `PresetData` field from live state. |
|
|
101
|
-
| `setParam(key, value)` | `void` | Sets one param and applies side effects (volume, reverb, grid, etc.). |
|
|
102
|
-
| `applyParams(patch, options?)` | `void` | Merges a partial preset. Default tweens numeric UI keys (~1.5s). |
|
|
103
|
-
| `captureParams()` | `Partial<PresetData>` | Snapshot of current live params (for export / cloning). |
|
|
104
|
-
| `toAudioBuffer(options?)` | `Promise<AudioBuffer>` | Offline render via `OfflineAudioContext`. |
|
|
105
|
-
| `toBlob(options?)` | `Promise<Blob>` | Same as `toAudioBuffer`, encoded as WAV `Blob`. |
|
|
106
|
-
|
|
107
|
-
#### `applyParams(patch, options?)`
|
|
108
|
-
|
|
109
|
-
```ts
|
|
110
|
-
applyParams(patch: Partial<PresetData>, options?: { tween?: boolean })
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
- `tween: true` (default) — applies structural fields immediately, then smoothly interpolates `TWEEN_KEYS` (volume, filter, reverb, etc.).
|
|
114
|
-
- `tween: false` — cancels any running tween and applies the full patch immediately, syncing live audio nodes.
|
|
115
|
-
|
|
116
|
-
Changing `scan_mode` resets the read-head position and resyncs the scheduler.
|
|
117
|
-
|
|
118
|
-
#### `toAudioBuffer` / `toBlob`
|
|
119
|
-
|
|
120
|
-
```ts
|
|
121
|
-
interface RenderOptions {
|
|
122
|
-
image?: ImageSource; // required if loadImage was never called
|
|
123
|
-
params?: Partial<PresetData>; // defaults to current live capture
|
|
124
|
-
duration?: number; // seconds, default 10
|
|
125
|
-
sampleRate?: number; // default 44100
|
|
126
|
-
}
|
|
127
|
-
```
|
|
128
|
-
|
|
129
|
-
Throws if no image is available (`options.image` and prior `loadImage` both missing).
|
|
130
|
-
|
|
131
|
-
Stereo output: 2 channels.
|
|
132
|
-
|
|
133
|
-
### Example — live session
|
|
134
|
-
|
|
135
|
-
```js
|
|
136
|
-
const wave = document.getElementById("wave_canvas");
|
|
137
|
-
const map = document.getElementById("map_canvas");
|
|
138
|
-
|
|
139
|
-
const audiora = new Audiora({
|
|
140
|
-
waveCanvas: wave,
|
|
141
|
-
mapCanvas: map,
|
|
142
|
-
onTweenTick: (key, value) => {
|
|
143
|
-
// keep UI sliders in sync during preset morphs
|
|
144
|
-
},
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
await audiora.loadImage("/photo.jpg");
|
|
148
|
-
audiora.applyParams(PRESETS.Haze, { tween: false });
|
|
149
|
-
|
|
150
|
-
await audiora.warmUp(); // optional; after a user gesture
|
|
151
|
-
await audiora.play();
|
|
152
|
-
|
|
153
|
-
// later
|
|
154
|
-
audiora.setParam("bpm", 110);
|
|
155
|
-
audiora.stop();
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
### Example — offline WAV
|
|
159
|
-
|
|
160
|
-
```js
|
|
161
|
-
const blob = await audiora.toBlob({
|
|
162
|
-
image: fileFromInput,
|
|
163
|
-
params: PRESETS.Bite,
|
|
164
|
-
duration: 15,
|
|
165
|
-
sampleRate: 44100,
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
const url = URL.createObjectURL(blob);
|
|
169
|
-
// trigger download or upload
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
---
|
|
173
|
-
|
|
174
|
-
## `PresetData`
|
|
175
|
-
|
|
176
|
-
All live and offline synthesis is driven by this shape:
|
|
177
|
-
|
|
178
|
-
| Key | Type | Role |
|
|
179
|
-
| ------------------ | -------- | -------------------------------------------------------------- |
|
|
180
|
-
| `osc_type` | `string` | Waveform identity. |
|
|
181
|
-
| `scale_name` | `string` | Named scale for pitch quantization. |
|
|
182
|
-
| `base_note` | `number` | Root frequency (Hz). |
|
|
183
|
-
| `bpm` | `number` | Base tempo. |
|
|
184
|
-
| `speed` | `number` | Traversal / scheduling multiplier. |
|
|
185
|
-
| `note_mult` | `number` | Note duration multiplier. |
|
|
186
|
-
| `volume` | `number` | Master level (0–1; gain uses `volume²`). |
|
|
187
|
-
| `vibrato_rate` | `number` | Vibrato LFO rate (Hz). |
|
|
188
|
-
| `vibrato_depth` | `number` | Vibrato depth (cents). |
|
|
189
|
-
| `tremolo_rate` | `number` | Tremolo LFO rate (Hz). |
|
|
190
|
-
| `tremolo_depth` | `number` | Tremolo depth (0–1). |
|
|
191
|
-
| `fifth_lvl` | `number` | Level of fifth partial voice (0–1). |
|
|
192
|
-
| `oct_lvl` | `number` | Level of octave partial voice (0–1). |
|
|
193
|
-
| `filter_base` | `number` | Filter base cutoff (Hz). |
|
|
194
|
-
| `filter_track` | `number` | How strongly block brightness shifts cutoff (Hz; `0` = fixed). |
|
|
195
|
-
| `resonance` | `number` | Filter Q. |
|
|
196
|
-
| `reverb_mix` | `number` | Wet level (0–1). At `≥ 0.99`, dry path is muted (less stereo). |
|
|
197
|
-
| `reverb_time` | `number` | Convolution / decay time (s). |
|
|
198
|
-
| `bass_gain` | `number` | Bass shelf (dB). |
|
|
199
|
-
| `block_w` | `number` | Analysis block width (px). |
|
|
200
|
-
| `block_h` | `number` | Analysis block height (px). |
|
|
201
|
-
| `scan_mode` | `string` | Traversal strategy. |
|
|
202
|
-
| `pan_amount` | `number` | Stereo imaging strength (0–1). |
|
|
203
|
-
| `timing_mode` | `string` | `"expressive"` or `"stable"`. |
|
|
204
|
-
| `timing_intensity` | `number` | How strongly lightness bends timing when expressive (0–1). |
|
|
205
|
-
|
|
206
|
-
`FORMAT` / `RANGE_KEYS` cover the numeric rows above; `SELECT_KEYS` are the four string fields.
|
|
207
|
-
|
|
208
|
-
### `osc_type` values
|
|
209
|
-
|
|
210
|
-
| Value | Behavior |
|
|
211
|
-
| ------------------------------------------- | -------------------------------------------------------------------- |
|
|
212
|
-
| `sine` / `square` / `sawtooth` / `triangle` | Native Web Audio oscillator types. |
|
|
213
|
-
| `pulse` | Custom periodic wave (odd harmonics). |
|
|
214
|
-
| `organ` | Custom harmonic stack. |
|
|
215
|
-
| `bell` | Custom inharmonic-leaning stack; fifth/octave levels are attenuated. |
|
|
216
|
-
|
|
217
|
-
Unknown types fall back to `triangle`.
|
|
218
|
-
|
|
219
|
-
### `scale_name` values
|
|
220
|
-
|
|
221
|
-
Built-in scales (from `SCALES` in core constants):
|
|
222
|
-
|
|
223
|
-
`Major`, `Minor`, `Pentatonic`, `Blues`, `Chromatic`, `Whole Tone`, `Dorian`, `Phrygian`, `Lydian`, `Harmonic Minor`, `In Sen`.
|
|
224
|
-
|
|
225
|
-
### `scan_mode` values
|
|
226
|
-
|
|
227
|
-
| Value | Path |
|
|
228
|
-
| ---------- | ---------------------------------------------------- |
|
|
229
|
-
| `linear` | Left-to-right rows. |
|
|
230
|
-
| `vertical` | Top-to-bottom columns. |
|
|
231
|
-
| `zigzag` | Alternating row direction. |
|
|
232
|
-
| `drift` | Soft irregular walk. |
|
|
233
|
-
| `spiral` | Outward from near center along a precomputed spiral. |
|
|
234
|
-
|
|
235
|
-
Unknown modes fall back to `linear`.
|
|
236
|
-
|
|
237
|
-
### `timing_mode` values
|
|
238
|
-
|
|
239
|
-
| Value | Behavior |
|
|
240
|
-
| ------------ | ----------------------------------------------------------------------- |
|
|
241
|
-
| `expressive` | Lightness modulates interval / duration (scaled by `timing_intensity`). |
|
|
242
|
-
| `stable` | Fixed rhythmic grid; ignores lightness timing bend. |
|
|
243
|
-
|
|
244
|
-
---
|
|
245
|
-
|
|
246
|
-
## `PRESETS`
|
|
247
|
-
|
|
248
|
-
| Name | Character (summary) |
|
|
249
|
-
| ------- | ---------------------------------------------------------------------- |
|
|
250
|
-
| `Haze` | Soft triangle / pentatonic, expressive timing, higher reverb. Default. |
|
|
251
|
-
| `Leap` | Fast square / major, stable timing. |
|
|
252
|
-
| `Pulse` | Square with strong tremolo, stable. |
|
|
253
|
-
| `Bite` | Dark sawtooth / chromatic, high resonance & bass. |
|
|
254
|
-
| `Spark` | High square / chromatic, short notes, fast tempo. |
|
|
255
|
-
| `Raw` | Dry square, large blocks, no effects stack. |
|
|
256
|
-
|
|
257
|
-
Apply with:
|
|
258
|
-
|
|
259
|
-
```js
|
|
260
|
-
audiora.applyParams(PRESETS.Haze);
|
|
261
|
-
// or hard-cut without morph:
|
|
262
|
-
audiora.applyParams(PRESETS.Raw, { tween: false });
|
|
263
|
-
```
|
|
264
|
-
|
|
265
|
-
---
|
|
266
|
-
|
|
267
|
-
## Helpers for UI hosts
|
|
268
|
-
|
|
269
|
-
The demo app (`audiora/www`) uses these exports to bind controls without redefining param lists:
|
|
270
|
-
|
|
271
|
-
```js
|
|
272
|
-
import {
|
|
273
|
-
FORMAT,
|
|
274
|
-
RANGE_KEYS,
|
|
275
|
-
SELECT_KEYS,
|
|
276
|
-
DEFAULT_PRESET,
|
|
277
|
-
PRESETS,
|
|
278
|
-
} from "audiora";
|
|
279
|
-
|
|
280
|
-
for (const key of RANGE_KEYS) {
|
|
281
|
-
// range input id === key; label via FORMAT[key](value)
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
for (const key of SELECT_KEYS) {
|
|
285
|
-
// select id === key
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
audiora.applyParams(PRESETS[DEFAULT_PRESET], { tween: false });
|
|
289
|
-
```
|
|
290
|
-
|
|
291
|
-
| Helper | Use |
|
|
292
|
-
| -------------------- | ----------------------------------------------------- |
|
|
293
|
-
| `RANGE_KEYS` | Iterate numeric sliders. |
|
|
294
|
-
| `SELECT_KEYS` | Iterate `<select>` bindings. |
|
|
295
|
-
| `FORMAT[key](value)` | Human-readable label (`"92"`, `"×1.70"`, `"45%"`, …). |
|
|
296
|
-
| `DEFAULT_PRESET` | Initial highlight / first apply (`"Haze"`). |
|
|
297
|
-
|
|
298
|
-
---
|
|
299
|
-
|
|
300
|
-
## Browser / runtime requirements
|
|
301
|
-
|
|
302
|
-
- **Live playback**: Web Audio API (`AudioContext`), user gesture before `resume` in most browsers.
|
|
303
|
-
- **Offline render**: `OfflineAudioContext`.
|
|
304
|
-
- **Visuals** (optional): 2D canvas contexts for wave + map.
|
|
305
|
-
- **Images**: max analysis path is capped internally (pixel buffer ≤ 512px on the long side; display canvas ≤ 2048px).
|
|
306
|
-
|
|
307
|
-
### Known timing notes
|
|
308
|
-
|
|
309
|
-
- First `play()` may show the scan cursor slightly ahead of audio while the context starts; subsequent plays are stable.
|
|
310
|
-
- The visual cursor leads audio by a small lookahead window by design (avoids audible glitches).
|
|
311
|
-
- Live `play()` without a loaded image keeps the scheduler running but schedules no notes until `loadImage` builds a grid.
|
|
312
|
-
- `reverb_mix ≥ 0.99` mutes the dry path; reduce mix to restore pan spread.
|
|
313
|
-
|
|
314
|
-
---
|
|
315
|
-
|
|
316
|
-
## Import patterns
|
|
317
|
-
|
|
318
|
-
### Library-only (embed in your app)
|
|
319
|
-
|
|
320
|
-
```js
|
|
321
|
-
import { Audiora, PRESETS } from "audiora";
|
|
322
|
-
|
|
323
|
-
const audiora = new Audiora();
|
|
324
|
-
await audiora.loadImage(userFile);
|
|
325
|
-
const wav = await audiora.toBlob({ params: PRESETS.Pulse, duration: 12 });
|
|
326
|
-
```
|
|
327
|
-
|
|
328
|
-
### Live instrument shell
|
|
329
|
-
|
|
330
|
-
```js
|
|
331
|
-
import { Audiora, PRESETS, RANGE_KEYS, SELECT_KEYS } from "audiora";
|
|
332
|
-
|
|
333
|
-
const audiora = new Audiora({
|
|
334
|
-
waveCanvas: document.getElementById("wave_canvas"),
|
|
335
|
-
mapCanvas: document.getElementById("map_canvas"),
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
window.addEventListener("resize", () => audiora.resizeVisuals());
|
|
339
|
-
await audiora.loadImage("/assets/map.jpg");
|
|
340
|
-
audiora.applyParams(PRESETS.Haze, { tween: false });
|
|
341
|
-
```
|
|
342
|
-
|
|
343
|
-
Wire `RANGE_KEYS` / `SELECT_KEYS` to DOM controls calling `setParam` / `applyParams` as in `audiora/www`.
|
package/src/Audiora.ts
DELETED
|
@@ -1,293 +0,0 @@
|
|
|
1
|
-
import { State } from "./core/State";
|
|
2
|
-
import { AudioEngine } from "./core/AudioEngine";
|
|
3
|
-
import { Synthesizer } from "./core/Synthesizer";
|
|
4
|
-
import { Scheduler } from "./core/Scheduler";
|
|
5
|
-
import { Sequencer } from "./core/Scanner";
|
|
6
|
-
import { ImageProcessor, BlockGrid } from "./core/ImageProcessor";
|
|
7
|
-
import { audioBufferToWavBlob } from "./core/wav";
|
|
8
|
-
import { PresetData } from "./core/types";
|
|
9
|
-
import { KEY, SCALES } from "./core/constants";
|
|
10
|
-
import { Tweener, TweenTickHandler } from "./ui/Tweener";
|
|
11
|
-
import { Renderer } from "./ui/Renderer";
|
|
12
|
-
|
|
13
|
-
export type ImageSource = string | File | Blob | HTMLImageElement;
|
|
14
|
-
|
|
15
|
-
export interface RenderOptions {
|
|
16
|
-
image?: ImageSource;
|
|
17
|
-
params?: Partial<PresetData>;
|
|
18
|
-
duration?: number;
|
|
19
|
-
sampleRate?: number;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface LiveOptions {
|
|
23
|
-
waveCanvas?: HTMLCanvasElement;
|
|
24
|
-
mapCanvas?: HTMLCanvasElement;
|
|
25
|
-
onTweenTick?: TweenTickHandler;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const DEFAULT_DURATION_SECONDS = 10;
|
|
29
|
-
const DEFAULT_SAMPLE_RATE = 44100;
|
|
30
|
-
const OUTPUT_CHANNELS = 2;
|
|
31
|
-
|
|
32
|
-
export class Audiora {
|
|
33
|
-
private readonly state = new State();
|
|
34
|
-
private readonly engine = new AudioEngine();
|
|
35
|
-
private readonly imageProcessor = new ImageProcessor();
|
|
36
|
-
private readonly grid = new BlockGrid();
|
|
37
|
-
private readonly seq: Sequencer;
|
|
38
|
-
private readonly synth: Synthesizer;
|
|
39
|
-
private readonly scheduler: Scheduler;
|
|
40
|
-
private readonly tweener: Tweener;
|
|
41
|
-
private renderer: Renderer | null = null;
|
|
42
|
-
private reverbDebounce: ReturnType<typeof setTimeout> | null = null;
|
|
43
|
-
private loadedImage: HTMLImageElement | null = null;
|
|
44
|
-
|
|
45
|
-
constructor(options: LiveOptions = {}) {
|
|
46
|
-
this.seq = new Sequencer(this.grid);
|
|
47
|
-
this.synth = new Synthesizer(this.engine, this.state);
|
|
48
|
-
this.scheduler = new Scheduler(
|
|
49
|
-
this.state,
|
|
50
|
-
this.engine,
|
|
51
|
-
this.synth,
|
|
52
|
-
this.seq,
|
|
53
|
-
this.grid,
|
|
54
|
-
);
|
|
55
|
-
this.tweener = new Tweener(this.state, this.engine, options.onTweenTick);
|
|
56
|
-
|
|
57
|
-
if (options.waveCanvas && options.mapCanvas) {
|
|
58
|
-
this.attachVisuals(options.waveCanvas, options.mapCanvas);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public get playing(): boolean {
|
|
63
|
-
return this.state.playing;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public attachVisuals(
|
|
67
|
-
waveCanvas: HTMLCanvasElement,
|
|
68
|
-
mapCanvas: HTMLCanvasElement,
|
|
69
|
-
): void {
|
|
70
|
-
this.renderer?.stop();
|
|
71
|
-
this.renderer = new Renderer(
|
|
72
|
-
waveCanvas,
|
|
73
|
-
mapCanvas,
|
|
74
|
-
this.engine,
|
|
75
|
-
this.imageProcessor,
|
|
76
|
-
this.grid,
|
|
77
|
-
this.seq,
|
|
78
|
-
);
|
|
79
|
-
this.renderer.start();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
public resizeVisuals(): void {
|
|
83
|
-
this.renderer?.resizeCanvases();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
public async loadImage(image: ImageSource): Promise<void> {
|
|
87
|
-
const img = await resolveImageElement(image);
|
|
88
|
-
this.loadedImage = img;
|
|
89
|
-
this.imageProcessor.processImage(img);
|
|
90
|
-
this.rebuildGrid();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public async play(): Promise<void> {
|
|
94
|
-
this.engine.ensure(this.state);
|
|
95
|
-
if (!this.engine.ctx || this.state.playing) return;
|
|
96
|
-
await this.engine.resume();
|
|
97
|
-
this.state.playing = true;
|
|
98
|
-
this.scheduler.start();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public stop(): void {
|
|
102
|
-
this.state.playing = false;
|
|
103
|
-
this.seq.nextTime = 0;
|
|
104
|
-
this.seq.active = false;
|
|
105
|
-
this.seq.activateAt = 0;
|
|
106
|
-
this.scheduler.stop();
|
|
107
|
-
this.engine.suspend();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
public async warmUp(): Promise<void> {
|
|
111
|
-
await this.engine.warmUp(this.state);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
public getParam(key: keyof PresetData): string | number {
|
|
115
|
-
const value = (this.state as unknown as Record<string, unknown>)[key];
|
|
116
|
-
return value as string | number;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
public setParam(key: keyof PresetData, value: string | number): void {
|
|
120
|
-
if (typeof value === "number") this.state.setNumeric(key, value);
|
|
121
|
-
else this.state.setString(key, value);
|
|
122
|
-
this.applyParamSideEffects(key);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
public applyParams(
|
|
126
|
-
patch: Partial<PresetData>,
|
|
127
|
-
options: { tween?: boolean } = {},
|
|
128
|
-
): void {
|
|
129
|
-
const { tween = true } = options;
|
|
130
|
-
|
|
131
|
-
if (tween) {
|
|
132
|
-
this.state.applyPatch(patch);
|
|
133
|
-
this.rebuildGrid();
|
|
134
|
-
this.tweener.tweenTo(patch);
|
|
135
|
-
} else {
|
|
136
|
-
this.tweener.cancel();
|
|
137
|
-
this.state.applyAll(patch);
|
|
138
|
-
this.rebuildGrid();
|
|
139
|
-
this.syncLiveAudioFromState();
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (patch.scan_mode) {
|
|
143
|
-
this.seq.resetPosition(this.state);
|
|
144
|
-
this.scheduler.resync();
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
public captureParams(): Partial<PresetData> {
|
|
149
|
-
return this.state.capture();
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
public async toAudioBuffer(options: RenderOptions = {}): Promise<AudioBuffer> {
|
|
153
|
-
const {
|
|
154
|
-
duration = DEFAULT_DURATION_SECONDS,
|
|
155
|
-
sampleRate = DEFAULT_SAMPLE_RATE,
|
|
156
|
-
} = options;
|
|
157
|
-
|
|
158
|
-
const state = new State();
|
|
159
|
-
state.applyAll(options.params ?? this.state.capture());
|
|
160
|
-
|
|
161
|
-
const imageProcessor = new ImageProcessor();
|
|
162
|
-
const grid = new BlockGrid();
|
|
163
|
-
const img = options.image
|
|
164
|
-
? await resolveImageElement(options.image)
|
|
165
|
-
: this.loadedImage;
|
|
166
|
-
|
|
167
|
-
if (!img) throw new Error("No image loaded. Pass `image` or call loadImage first.");
|
|
168
|
-
|
|
169
|
-
imageProcessor.processImage(img);
|
|
170
|
-
grid.build(imageProcessor, state);
|
|
171
|
-
|
|
172
|
-
const seq = new Sequencer(grid);
|
|
173
|
-
seq.resetPosition(state);
|
|
174
|
-
|
|
175
|
-
const ctx = new OfflineAudioContext({
|
|
176
|
-
numberOfChannels: OUTPUT_CHANNELS,
|
|
177
|
-
length: Math.max(1, Math.ceil(duration * sampleRate)),
|
|
178
|
-
sampleRate,
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
const engine = new AudioEngine();
|
|
182
|
-
engine.ensureOffline(ctx, state);
|
|
183
|
-
|
|
184
|
-
const synth = new Synthesizer(engine, state);
|
|
185
|
-
const scheduler = new Scheduler(state, engine, synth, seq, grid);
|
|
186
|
-
scheduler.renderRange(0, duration);
|
|
187
|
-
|
|
188
|
-
return ctx.startRendering();
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
public async toBlob(options: RenderOptions = {}): Promise<Blob> {
|
|
192
|
-
const buffer = await this.toAudioBuffer(options);
|
|
193
|
-
return audioBufferToWavBlob(buffer);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
private rebuildGrid(): void {
|
|
197
|
-
this.grid.build(this.imageProcessor, this.state);
|
|
198
|
-
this.seq.clampToGrid();
|
|
199
|
-
this.scheduler.resync();
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
private applyParamSideEffects(key: keyof PresetData): void {
|
|
203
|
-
switch (key) {
|
|
204
|
-
case KEY.volume:
|
|
205
|
-
if (this.engine.master) {
|
|
206
|
-
this.engine.smooth(
|
|
207
|
-
this.engine.master.gain,
|
|
208
|
-
Math.pow(this.state.volume, 2),
|
|
209
|
-
);
|
|
210
|
-
}
|
|
211
|
-
break;
|
|
212
|
-
case KEY.bpm:
|
|
213
|
-
case KEY.speed:
|
|
214
|
-
case KEY.note_mult:
|
|
215
|
-
this.scheduler.resync();
|
|
216
|
-
break;
|
|
217
|
-
case KEY.reverb_mix:
|
|
218
|
-
if (this.engine.wet && this.engine.dry) {
|
|
219
|
-
this.engine.smooth(this.engine.wet.gain, this.state.reverb_mix);
|
|
220
|
-
this.engine.smooth(
|
|
221
|
-
this.engine.dry.gain,
|
|
222
|
-
this.engine.dryVal(this.state.reverb_mix),
|
|
223
|
-
);
|
|
224
|
-
}
|
|
225
|
-
break;
|
|
226
|
-
case KEY.reverb_time:
|
|
227
|
-
if (!this.engine.ctx || !this.engine.conv) return;
|
|
228
|
-
if (this.reverbDebounce) clearTimeout(this.reverbDebounce);
|
|
229
|
-
this.reverbDebounce = setTimeout(
|
|
230
|
-
() => this.engine.updateReverbBuffer(this.state),
|
|
231
|
-
120,
|
|
232
|
-
);
|
|
233
|
-
break;
|
|
234
|
-
case KEY.block_w:
|
|
235
|
-
case KEY.block_h:
|
|
236
|
-
this.rebuildGrid();
|
|
237
|
-
break;
|
|
238
|
-
case KEY.scale_name:
|
|
239
|
-
this.state.scale =
|
|
240
|
-
SCALES.find((s) => s.name === this.state.scale_name) ?? SCALES[0];
|
|
241
|
-
this.rebuildGrid();
|
|
242
|
-
break;
|
|
243
|
-
case KEY.scan_mode:
|
|
244
|
-
this.seq.resetPosition(this.state);
|
|
245
|
-
this.scheduler.resync();
|
|
246
|
-
break;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
private syncLiveAudioFromState(): void {
|
|
251
|
-
if (this.engine.master) {
|
|
252
|
-
this.engine.master.gain.value = Math.pow(this.state.volume, 2);
|
|
253
|
-
}
|
|
254
|
-
if (this.engine.wet && this.engine.dry) {
|
|
255
|
-
this.engine.wet.gain.value = this.state.reverb_mix;
|
|
256
|
-
this.engine.dry.gain.value = this.engine.dryVal(this.state.reverb_mix);
|
|
257
|
-
}
|
|
258
|
-
this.engine.updateReverbBuffer(this.state);
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
async function resolveImageElement(image: ImageSource): Promise<HTMLImageElement> {
|
|
263
|
-
if (image instanceof HTMLImageElement) return waitForImage(image);
|
|
264
|
-
|
|
265
|
-
const isBlobSource = image instanceof Blob;
|
|
266
|
-
const src = isBlobSource ? URL.createObjectURL(image) : image;
|
|
267
|
-
const img = new Image();
|
|
268
|
-
if (!isBlobSource) img.crossOrigin = "anonymous";
|
|
269
|
-
|
|
270
|
-
return new Promise((resolve, reject) => {
|
|
271
|
-
img.onload = () => {
|
|
272
|
-
if (isBlobSource) URL.revokeObjectURL(src);
|
|
273
|
-
resolve(img);
|
|
274
|
-
};
|
|
275
|
-
img.onerror = () => {
|
|
276
|
-
if (isBlobSource) URL.revokeObjectURL(src);
|
|
277
|
-
reject(new Error(`Failed to load image: ${isBlobSource ? "blob" : src}`));
|
|
278
|
-
};
|
|
279
|
-
img.src = src;
|
|
280
|
-
});
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
function waitForImage(img: HTMLImageElement): Promise<HTMLImageElement> {
|
|
284
|
-
if (img.complete && img.naturalWidth > 0) return Promise.resolve(img);
|
|
285
|
-
return new Promise((resolve, reject) => {
|
|
286
|
-
img.addEventListener("load", () => resolve(img), { once: true });
|
|
287
|
-
img.addEventListener(
|
|
288
|
-
"error",
|
|
289
|
-
() => reject(new Error("Failed to load image")),
|
|
290
|
-
{ once: true },
|
|
291
|
-
);
|
|
292
|
-
});
|
|
293
|
-
}
|