@hraness/dawg 0.2.0 → 0.4.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 +201 -0
- package/DAWG.md +762 -11
- package/README.md +79 -30
- package/core/chords.ts +1724 -0
- package/core/diff.ts +261 -0
- package/core/euclid.ts +670 -0
- package/core/fx.ts +1065 -0
- package/core/key.ts +43 -0
- package/core/kits.ts +320 -0
- package/core/params.ts +111 -0
- package/core/pitch.ts +60 -0
- package/core/rhythm.ts +287 -0
- package/core/score.ts +1054 -14
- package/core/sdk/eval-child.ts +125 -0
- package/core/sdk/eval.ts +260 -0
- package/core/sdk/print.ts +675 -0
- package/core/sdk/sync-chords.ts +54 -0
- package/core/sdk/v1.ts +4159 -0
- package/core/slug.ts +19 -0
- package/core/synth.ts +1001 -0
- package/package.json +8 -5
- package/src/agent/agent.ts +317 -16
- package/src/agent/brief.ts +50 -5
- package/src/agent/chord-tools.ts +357 -0
- package/src/agent/drum-tools.ts +135 -0
- package/src/agent/gateway.ts +215 -41
- package/src/agent/models.ts +633 -0
- package/src/agent/ops.ts +3 -18
- package/src/agent/pack-tools.ts +369 -0
- package/src/agent/planner.ts +55 -2
- package/src/agent/preview-tool.ts +270 -0
- package/src/agent/provider.ts +234 -106
- package/src/agent/rhythm-tools.ts +145 -0
- package/src/agent/sse.ts +31 -7
- package/src/agent/tools.ts +705 -14
- package/src/agent/usage.ts +296 -0
- package/src/agent/workspace.ts +683 -0
- package/src/agent/xcb-agent.ts +35 -14
- package/src/agent/xcb.ts +233 -21
- package/src/audio/audition.ts +93 -0
- package/src/audio/cache.ts +160 -0
- package/src/audio/click.ts +125 -0
- package/src/audio/effects/bus.ts +147 -0
- package/src/audio/effects/chain.ts +99 -0
- package/src/audio/effects/common.ts +251 -0
- package/src/audio/effects/convolution.ts +301 -0
- package/src/audio/effects/drive.ts +142 -0
- package/src/audio/effects/duck.ts +122 -0
- package/src/audio/effects/dynamics.ts +121 -0
- package/src/audio/effects/filter.ts +319 -0
- package/src/audio/effects/modulation.ts +174 -0
- package/src/audio/effects/space.ts +337 -0
- package/src/audio/engine.ts +430 -33
- package/src/audio/kits.ts +200 -0
- package/src/audio/live.ts +152 -0
- package/src/audio/packs.ts +1787 -0
- package/src/audio/player.ts +19 -4
- package/src/audio/preview.ts +470 -0
- package/src/audio/random.ts +15 -0
- package/src/audio/render-worker.ts +68 -0
- package/src/audio/renderer.ts +174 -0
- package/src/audio/sampler.ts +420 -0
- package/src/audio/samples.ts +1025 -0
- package/src/audio/synth/oscillators.ts +268 -0
- package/src/audio/synth/voice.ts +555 -0
- package/src/audio/synth/zzfx.ts +137 -0
- package/src/audio/wav.ts +550 -334
- package/src/audio/wavetable-maker.ts +717 -0
- package/src/audio/wavetable.ts +624 -0
- package/src/auth/cli.ts +146 -27
- package/src/auth/credentials.ts +167 -41
- package/src/auth/discover.ts +481 -0
- package/src/auth/login.ts +885 -128
- package/src/auth/openrouter.ts +206 -0
- package/src/auth/picker.ts +282 -0
- package/src/auth/runner.ts +25 -2
- package/src/auth/tui.ts +60 -43
- package/src/commands/drums.ts +299 -0
- package/src/commands/edit.ts +192 -0
- package/src/commands/fx.ts +360 -0
- package/src/commands/help.ts +535 -0
- package/src/commands/history.ts +32 -19
- package/src/commands/music.ts +34 -15
- package/src/commands/pack.ts +423 -0
- package/src/commands/rhythm.ts +230 -0
- package/src/commands/sample.ts +462 -0
- package/src/commands/synth.ts +229 -0
- package/src/commands/wavetable.ts +370 -0
- package/src/main.ts +2136 -170
- package/src/media/analyze.ts +364 -0
- package/src/media/backend.ts +253 -0
- package/src/media/cli.ts +186 -0
- package/src/media/download.ts +281 -0
- package/src/media/dsp.ts +281 -0
- package/src/media/import.ts +130 -0
- package/src/media/lyrics.ts +201 -0
- package/src/media/notes.ts +363 -0
- package/src/media/paths.ts +168 -0
- package/src/media/process.ts +226 -0
- package/src/media/registry.ts +9 -0
- package/src/media/sidecar.ts +72 -0
- package/src/media/stemdeck.ts +254 -0
- package/src/media/stems.ts +173 -0
- package/src/media/tools.ts +397 -0
- package/src/media/types.ts +92 -0
- package/src/media/vendor/basic-pitch.ts +261 -0
- package/src/media/vendor/drums.ts +817 -0
- package/src/media/vendor/grid.ts +203 -0
- package/src/media/vendor/util.ts +139 -0
- package/src/media/vendor/wav.ts +233 -0
- package/src/media/wavetable.ts +202 -0
- package/src/project/check.ts +80 -0
- package/src/project/init.ts +253 -0
- package/src/project/sync.ts +432 -0
- package/src/project/typecheck.ts +149 -0
- package/src/render.ts +49 -6
- package/src/session/attach.ts +3 -4
- package/src/session/daemon.ts +25 -8
- package/src/session/delta.ts +249 -0
- package/src/session/naming.ts +6 -38
- package/src/session/port.ts +26 -6
- package/src/session/rebase.ts +38 -8
- package/src/session/store.ts +116 -21
- package/src/tui/audition.ts +501 -0
- package/src/tui/euclid.ts +472 -0
- package/src/tui/menu.ts +2125 -0
- package/src/tui/play-chords.ts +538 -0
- package/src/tui/play-mode.ts +442 -0
- package/src/tui/play-session.ts +984 -0
- package/src/tui/sketch.ts +108 -0
- package/src/web/fetch.ts +340 -0
- package/src/web/http.ts +137 -0
- package/src/web/search.ts +684 -0
- package/tui/activity.ts +42 -3
- package/tui/app.ts +398 -33
- package/tui/drums.ts +44 -0
- package/tui/grammar.ts +281 -0
- package/tui/highway.ts +24 -2
- package/tui/layers.ts +14 -2
- package/tui/play-strip.ts +188 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `synth` prompt command: the focused track's synth voice parameters
|
|
3
|
+
* (core/synth.ts), named as in Strudel.
|
|
4
|
+
*
|
|
5
|
+
* synth list the parameters this track sets
|
|
6
|
+
* synth preset <name> load a preset (instrument + params)
|
|
7
|
+
* synth <param> <value> [<param> <value>…] set (Strudel aliases accepted)
|
|
8
|
+
* synth <param> off unset one parameter (back to default)
|
|
9
|
+
* synth partials 1 0.5 0.33 additive harmonics (also `phases`)
|
|
10
|
+
* synth reset unset every parameter
|
|
11
|
+
* synth zzfx 1,.05,220,,,.1,2 a raw ZzFX array (z_* sound + params)
|
|
12
|
+
*
|
|
13
|
+
* `synth lpf 800 lpenv 3 lpdecay 0.2`, `synth fm 4 fmh 1.5`, `synth adsr
|
|
14
|
+
* 0.01 0.2 0.5 0.3` (Strudel's `adsr` shorthand). Each command is one
|
|
15
|
+
* `updateTrack` revision and one undo step.
|
|
16
|
+
*/
|
|
17
|
+
import { FxValidationError } from "../../core/params.ts";
|
|
18
|
+
import {
|
|
19
|
+
ScoreValidationError,
|
|
20
|
+
updateTrack,
|
|
21
|
+
type TrackScore,
|
|
22
|
+
} from "../../core/score.ts";
|
|
23
|
+
import {
|
|
24
|
+
SYNTH_PARAMS,
|
|
25
|
+
SYNTH_PRESETS,
|
|
26
|
+
SYNTH_SIMPLE,
|
|
27
|
+
isSynthList,
|
|
28
|
+
isSynthPreset,
|
|
29
|
+
normalizeSynth,
|
|
30
|
+
synthParamName,
|
|
31
|
+
ZZFX_ARRAY_LAYOUT,
|
|
32
|
+
zzfxArraySynth,
|
|
33
|
+
type TrackSynth,
|
|
34
|
+
} from "../../core/synth.ts";
|
|
35
|
+
import { parseParamValue } from "./fx.ts";
|
|
36
|
+
|
|
37
|
+
export type SynthCommand =
|
|
38
|
+
| { type: "synth-list" }
|
|
39
|
+
| { type: "synth-reset" }
|
|
40
|
+
| { type: "synth-preset"; preset: string }
|
|
41
|
+
/** A raw ZzFX parameter array (Strudel `zzfx([...])`); empty slots null. */
|
|
42
|
+
| { type: "synth-zzfx"; values: readonly (number | null)[] }
|
|
43
|
+
| {
|
|
44
|
+
type: "synth-set";
|
|
45
|
+
/** `null` unsets a parameter. */
|
|
46
|
+
values: Readonly<
|
|
47
|
+
Record<string, number | string | boolean | readonly number[] | null>
|
|
48
|
+
>;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const ADSR = ["attack", "decay", "sustain", "release"] as const;
|
|
52
|
+
|
|
53
|
+
export function parseSynthCommand(prompt: string): SynthCommand | undefined {
|
|
54
|
+
const words = prompt.trim().split(/\s+/);
|
|
55
|
+
if (words[0]?.toLowerCase() !== "synth") return undefined;
|
|
56
|
+
if (words.length === 1) return { type: "synth-list" };
|
|
57
|
+
if (prompt.length > 1_024) return undefined;
|
|
58
|
+
const rest = words.slice(1).map((word) => word.toLowerCase());
|
|
59
|
+
if (rest.length === 1 && (rest[0] === "reset" || rest[0] === "off"))
|
|
60
|
+
return { type: "synth-reset" };
|
|
61
|
+
if (rest[0] === "preset")
|
|
62
|
+
return rest.length === 2 && isSynthPreset(rest[1]!)
|
|
63
|
+
? { type: "synth-preset", preset: rest[1]! }
|
|
64
|
+
: undefined;
|
|
65
|
+
const list = synthParamName(rest[0]!);
|
|
66
|
+
if (list && isSynthList(list)) {
|
|
67
|
+
if (rest.length === 2 && rest[1] === "off")
|
|
68
|
+
return { type: "synth-set", values: { [list]: null } };
|
|
69
|
+
const numbers = rest.slice(1).map(Number);
|
|
70
|
+
if (numbers.length === 0 || numbers.some((n) => !Number.isFinite(n)))
|
|
71
|
+
return undefined;
|
|
72
|
+
return { type: "synth-set", values: { [list]: numbers } };
|
|
73
|
+
}
|
|
74
|
+
if (rest[0] === "zzfx") return zzfxCommand(words.slice(2).join(" "));
|
|
75
|
+
if (rest[0] === "adsr") {
|
|
76
|
+
// Strudel's `adsr("a:d:s:r")`, as four words or one colon string.
|
|
77
|
+
const parts = rest.length === 2 ? rest[1]!.split(":") : rest.slice(1);
|
|
78
|
+
if (parts.length !== 4) return undefined;
|
|
79
|
+
const values: Record<string, number> = {};
|
|
80
|
+
for (const [index, name] of ADSR.entries()) {
|
|
81
|
+
const value = parseParamValue(SYNTH_PARAMS[name]!, parts[index]!);
|
|
82
|
+
if (typeof value !== "number") return undefined;
|
|
83
|
+
values[name] = value;
|
|
84
|
+
}
|
|
85
|
+
return { type: "synth-set", values };
|
|
86
|
+
}
|
|
87
|
+
if (rest.length % 2 !== 0) return undefined;
|
|
88
|
+
const values: Record<string, number | string | boolean | null> = {};
|
|
89
|
+
for (let index = 0; index < rest.length; index += 2) {
|
|
90
|
+
const name = synthParamName(rest[index]!);
|
|
91
|
+
if (!name || isSynthList(name)) return undefined;
|
|
92
|
+
const word = rest[index + 1]!;
|
|
93
|
+
if (word === "off" || word === "unset") {
|
|
94
|
+
values[name] = null;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
const value = parseParamValue(SYNTH_PARAMS[name]!, word);
|
|
98
|
+
if (value === undefined) return undefined;
|
|
99
|
+
values[name] = value;
|
|
100
|
+
}
|
|
101
|
+
return { type: "synth-set", values };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* `synth zzfx 1,.05,220,,,.1` or the pasted `zzfx(...[,,129,.01])` form:
|
|
106
|
+
* commas keep empty slots; without commas, spaces separate the values.
|
|
107
|
+
*/
|
|
108
|
+
function zzfxCommand(text: string): SynthCommand | undefined {
|
|
109
|
+
const body = text
|
|
110
|
+
.trim()
|
|
111
|
+
.replace(/^zzfx\s*\(/i, "")
|
|
112
|
+
.replace(/\)\s*;?$/, "")
|
|
113
|
+
.replace(/^\.\.\./, "")
|
|
114
|
+
.replace(/^\[/, "")
|
|
115
|
+
.replace(/\]$/, "")
|
|
116
|
+
.trim();
|
|
117
|
+
if (body.length === 0) return undefined;
|
|
118
|
+
const parts = body.includes(",") ? body.split(",") : body.split(/\s+/);
|
|
119
|
+
if (parts.length > ZZFX_ARRAY_LAYOUT.length) return undefined;
|
|
120
|
+
const values: (number | null)[] = [];
|
|
121
|
+
for (const part of parts) {
|
|
122
|
+
const word = part.trim();
|
|
123
|
+
if (word === "") {
|
|
124
|
+
values.push(null);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const value = Number(
|
|
128
|
+
word.startsWith(".") ? `0${word}` : word.replace(/^-\./, "-0."),
|
|
129
|
+
);
|
|
130
|
+
if (!Number.isFinite(value)) return undefined;
|
|
131
|
+
values.push(value);
|
|
132
|
+
}
|
|
133
|
+
return { type: "synth-zzfx", values };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** `attack 0.01 · lpf 800 …` in spec order, or `defaults`. */
|
|
137
|
+
export function describeSynth(synth: TrackSynth | undefined): string {
|
|
138
|
+
if (!synth) return "defaults";
|
|
139
|
+
return Object.entries(synth)
|
|
140
|
+
.map(([key, value]) =>
|
|
141
|
+
Array.isArray(value)
|
|
142
|
+
? `${key} [${value.join(" ")}]`
|
|
143
|
+
: `${key} ${String(value)}`,
|
|
144
|
+
)
|
|
145
|
+
.join(" · ");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export type SynthResult = Readonly<{
|
|
149
|
+
ok: boolean;
|
|
150
|
+
message: string;
|
|
151
|
+
next?: TrackScore;
|
|
152
|
+
kind?: string;
|
|
153
|
+
payload?: Record<string, unknown>;
|
|
154
|
+
}>;
|
|
155
|
+
|
|
156
|
+
export function applySynthCommand(
|
|
157
|
+
score: TrackScore,
|
|
158
|
+
trackId: string,
|
|
159
|
+
command: SynthCommand,
|
|
160
|
+
): SynthResult {
|
|
161
|
+
const track = score.tracks.find((candidate) => candidate.id === trackId);
|
|
162
|
+
if (!track) return { ok: false, message: `no track · ${trackId}` };
|
|
163
|
+
if (track.sampler)
|
|
164
|
+
return {
|
|
165
|
+
ok: false,
|
|
166
|
+
message: `synth · ${trackId} is a sampler track; synth parameters shape synth voices`,
|
|
167
|
+
};
|
|
168
|
+
if (command.type === "synth-list")
|
|
169
|
+
return {
|
|
170
|
+
ok: true,
|
|
171
|
+
message: `synth · ${track.instrument} · ${describeSynth(track.synth)} · basics ${SYNTH_SIMPLE.join(" ")}`,
|
|
172
|
+
};
|
|
173
|
+
let instrument = track.instrument;
|
|
174
|
+
let synth: Record<string, unknown> | null;
|
|
175
|
+
if (command.type === "synth-reset") {
|
|
176
|
+
if (!track.synth) return { ok: true, message: "synth · already defaults" };
|
|
177
|
+
synth = null;
|
|
178
|
+
} else if (command.type === "synth-zzfx") {
|
|
179
|
+
try {
|
|
180
|
+
const sound = zzfxArraySynth(command.values);
|
|
181
|
+
instrument = sound.instrument;
|
|
182
|
+
synth = { ...sound.synth };
|
|
183
|
+
} catch (error) {
|
|
184
|
+
if (error instanceof FxValidationError)
|
|
185
|
+
return { ok: false, message: `synth · ${error.message}` };
|
|
186
|
+
throw error;
|
|
187
|
+
}
|
|
188
|
+
} else if (command.type === "synth-preset") {
|
|
189
|
+
const preset = SYNTH_PRESETS[command.preset]!;
|
|
190
|
+
instrument = preset.instrument;
|
|
191
|
+
synth = { ...preset.synth };
|
|
192
|
+
} else {
|
|
193
|
+
synth = { ...track.synth };
|
|
194
|
+
for (const [key, value] of Object.entries(command.values)) {
|
|
195
|
+
if (value === null) delete synth[key];
|
|
196
|
+
else synth[key] = value;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
let next: TrackScore;
|
|
200
|
+
try {
|
|
201
|
+
const normalized = normalizeSynth(synth);
|
|
202
|
+
next = updateTrack(score, trackId, {
|
|
203
|
+
instrument,
|
|
204
|
+
synth: normalized ?? null,
|
|
205
|
+
});
|
|
206
|
+
} catch (error) {
|
|
207
|
+
if (
|
|
208
|
+
error instanceof ScoreValidationError ||
|
|
209
|
+
error instanceof FxValidationError
|
|
210
|
+
)
|
|
211
|
+
return { ok: false, message: `synth · ${error.message}` };
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
const stored = next.tracks.find((candidate) => candidate.id === trackId);
|
|
215
|
+
return {
|
|
216
|
+
ok: true,
|
|
217
|
+
message:
|
|
218
|
+
command.type === "synth-preset"
|
|
219
|
+
? `synth · preset ${command.preset} · ${stored?.instrument} · ${describeSynth(stored?.synth)}`
|
|
220
|
+
: `synth · ${describeSynth(stored?.synth)}`,
|
|
221
|
+
next,
|
|
222
|
+
kind: "score.synth",
|
|
223
|
+
payload: {
|
|
224
|
+
trackId,
|
|
225
|
+
instrument: stored?.instrument ?? instrument,
|
|
226
|
+
synth: stored?.synth ?? null,
|
|
227
|
+
},
|
|
228
|
+
};
|
|
229
|
+
}
|
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wavetable commands (prompt grammar, menu and agent share these):
|
|
3
|
+
*
|
|
4
|
+
* wt show the focused track's table and parameters
|
|
5
|
+
* wt list built-in tables and the uzu-wavetables sets
|
|
6
|
+
* wt <table> play a table: basic, pwm, formant, harmonics,
|
|
7
|
+
* wt_digital:2 (Strudel's uzu-wavetables), any
|
|
8
|
+
* pack:<pack>/<sound>[:<n>], or a project WAV
|
|
9
|
+
* (vox.wav, wavetables/vox.wav, tracks/x/…/vox.wav;
|
|
10
|
+
* the agent's make_wavetable writes these)
|
|
11
|
+
* wt <0..1> position
|
|
12
|
+
* wtenv|wtattack|wtdecay|wtsustain|wtrelease|wtrate|wtdepth|warp|wtphaserand <n>
|
|
13
|
+
* warpmode <none|asym|bendp|bendm|bendmp|sync|quant>
|
|
14
|
+
*
|
|
15
|
+
* Any of these turns the focused track into a wavetable track. Parsing is
|
|
16
|
+
* pure; picking a pack table pins it (sha256 + url) through the PackStore.
|
|
17
|
+
*/
|
|
18
|
+
import {
|
|
19
|
+
BUILTIN_TABLE_PREFIX,
|
|
20
|
+
WARP_MODES,
|
|
21
|
+
WAVETABLE_INSTRUMENT,
|
|
22
|
+
WAVETABLE_PARAMS,
|
|
23
|
+
SCORE_LIMITS,
|
|
24
|
+
isLocalTableSrc,
|
|
25
|
+
isWavetableInstrument,
|
|
26
|
+
normalizeWavetable,
|
|
27
|
+
wavetableOf,
|
|
28
|
+
type ScoreOperation,
|
|
29
|
+
type TrackScore,
|
|
30
|
+
type TrackWavetable,
|
|
31
|
+
type WarpMode,
|
|
32
|
+
type WavetableParam,
|
|
33
|
+
} from "../../core/score.ts";
|
|
34
|
+
import { createHash } from "node:crypto";
|
|
35
|
+
import { readdirSync } from "node:fs";
|
|
36
|
+
import { readFile, stat } from "node:fs/promises";
|
|
37
|
+
import { join } from "node:path";
|
|
38
|
+
import { trackDirectories } from "../../core/sdk/print.ts";
|
|
39
|
+
import { resolveReadPath } from "../agent/workspace.ts";
|
|
40
|
+
import { PackError, type PackStore } from "../audio/packs.ts";
|
|
41
|
+
import {
|
|
42
|
+
BUILTIN_TABLES,
|
|
43
|
+
BUILTIN_TABLE_NAMES,
|
|
44
|
+
wavetableFromWav,
|
|
45
|
+
} from "../audio/wavetable.ts";
|
|
46
|
+
|
|
47
|
+
/** The pack Strudel's `wt_` sounds come from. */
|
|
48
|
+
export const WAVETABLE_PACK = "uzu-wavetables";
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Table names in uzu-wavetables' strudel.json (read 2026-10), for the menu,
|
|
52
|
+
* which cannot wait on a fetch. `wt list` reads the live manifest.
|
|
53
|
+
*/
|
|
54
|
+
export const UZU_WAVETABLES: Readonly<Record<string, readonly string[]>> =
|
|
55
|
+
Object.freeze({
|
|
56
|
+
wt_digital: ["bad_day", "basique", "crickets", "curses", "echoes"],
|
|
57
|
+
wt_vgame: Array.from({ length: 11 }, (_, i) => `vgame${10 + i}`),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export type WavetableCommand =
|
|
61
|
+
| Readonly<{ kind: "show" }>
|
|
62
|
+
| Readonly<{ kind: "list" }>
|
|
63
|
+
| Readonly<{ kind: "table"; table: string }>
|
|
64
|
+
| Readonly<{ kind: "param"; param: WavetableParam; value: number }>
|
|
65
|
+
| Readonly<{ kind: "warpmode"; mode: WarpMode }>
|
|
66
|
+
| Readonly<{ kind: "usage"; message: string }>;
|
|
67
|
+
|
|
68
|
+
const PARAM_WORDS = Object.keys(WAVETABLE_PARAMS).filter(
|
|
69
|
+
(name) => name !== "wt",
|
|
70
|
+
) as WavetableParam[];
|
|
71
|
+
const NUMBER = /^-?(?:\d+(?:\.\d+)?|\.\d+)$/;
|
|
72
|
+
|
|
73
|
+
export function parseWavetableCommand(
|
|
74
|
+
command: string,
|
|
75
|
+
): WavetableCommand | undefined {
|
|
76
|
+
const parsed = parseWords(command.trim().replace(/^\//, ""));
|
|
77
|
+
// Without a slash, only well-formed commands are ours; anything else
|
|
78
|
+
// ("warp the bass up") is a prompt for the agent.
|
|
79
|
+
if (parsed?.kind === "usage" && !command.trim().startsWith("/"))
|
|
80
|
+
return undefined;
|
|
81
|
+
return parsed;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function parseWords(text: string): WavetableCommand | undefined {
|
|
85
|
+
const words = text.split(/\s+/);
|
|
86
|
+
const head = words[0]!.toLowerCase();
|
|
87
|
+
if (head === "wavetable" || head === "wt") {
|
|
88
|
+
if (words.length === 1) return { kind: "show" };
|
|
89
|
+
if (words.length !== 2)
|
|
90
|
+
return { kind: "usage", message: "wt <table> | wt <0..1> | wt list" };
|
|
91
|
+
const arg = words[1]!;
|
|
92
|
+
if (/^(list|ls)$/i.test(arg)) return { kind: "list" };
|
|
93
|
+
if (NUMBER.test(arg)) {
|
|
94
|
+
const value = Number(arg);
|
|
95
|
+
if (value < 0 || value > 1)
|
|
96
|
+
return { kind: "usage", message: "wt position is 0..1" };
|
|
97
|
+
return { kind: "param", param: "wt", value };
|
|
98
|
+
}
|
|
99
|
+
return { kind: "table", table: arg };
|
|
100
|
+
}
|
|
101
|
+
if (head === "warpmode") {
|
|
102
|
+
const mode = words[1]?.toLowerCase();
|
|
103
|
+
if (words.length !== 2 || !WARP_MODES.includes(mode as WarpMode))
|
|
104
|
+
return {
|
|
105
|
+
kind: "usage",
|
|
106
|
+
message: `warpmode ${WARP_MODES.join("|")}`,
|
|
107
|
+
};
|
|
108
|
+
return { kind: "warpmode", mode: mode as WarpMode };
|
|
109
|
+
}
|
|
110
|
+
if ((PARAM_WORDS as string[]).includes(head)) {
|
|
111
|
+
const param = head as WavetableParam;
|
|
112
|
+
const [min, max] = WAVETABLE_PARAMS[param];
|
|
113
|
+
if (words.length !== 2 || !NUMBER.test(words[1]!))
|
|
114
|
+
return { kind: "usage", message: `${param} <${min}..${max}>` };
|
|
115
|
+
const value = Number(words[1]);
|
|
116
|
+
if (value < min || value > max)
|
|
117
|
+
return { kind: "usage", message: `${param} is ${min}..${max}` };
|
|
118
|
+
return { kind: "param", param, value };
|
|
119
|
+
}
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* `basic` → builtin, `wt_digital:2`/`wt_digital` → the uzu pack, `x/y[:n]`
|
|
125
|
+
* or `pack:x/y[:n]` → that pack sound. Undefined when malformed.
|
|
126
|
+
*/
|
|
127
|
+
export function wavetableSource(name: string): string | undefined {
|
|
128
|
+
const value = name.trim();
|
|
129
|
+
const lower = value.toLowerCase();
|
|
130
|
+
if (lower.startsWith(BUILTIN_TABLE_PREFIX))
|
|
131
|
+
return BUILTIN_TABLES[lower.slice(BUILTIN_TABLE_PREFIX.length)]
|
|
132
|
+
? lower
|
|
133
|
+
: undefined;
|
|
134
|
+
if (BUILTIN_TABLES[lower]) return `${BUILTIN_TABLE_PREFIX}${lower}`;
|
|
135
|
+
if (/^wt_[a-z0-9_]+(?::[0-9]+)?$/i.test(value))
|
|
136
|
+
return `pack:${WAVETABLE_PACK}/${value}`;
|
|
137
|
+
if (
|
|
138
|
+
/^(?:pack:)?[a-z0-9._-]+\/[A-Za-z0-9._-]+(?::[A-Za-z0-9._-]+)?$/.test(value)
|
|
139
|
+
)
|
|
140
|
+
return value.startsWith("pack:") ? value : `pack:${value}`;
|
|
141
|
+
return undefined;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** One operation that sets the track's wavetable (and makes it a wavetable track). */
|
|
145
|
+
export function wavetableOperation(
|
|
146
|
+
score: TrackScore,
|
|
147
|
+
trackId: string,
|
|
148
|
+
next: TrackWavetable,
|
|
149
|
+
): ScoreOperation {
|
|
150
|
+
const wavetable = normalizeWavetable(next)!;
|
|
151
|
+
if (!score.tracks.some((track) => track.id === trackId))
|
|
152
|
+
return {
|
|
153
|
+
type: "addTrack",
|
|
154
|
+
track: {
|
|
155
|
+
id: trackId,
|
|
156
|
+
name: trackId,
|
|
157
|
+
instrument: WAVETABLE_INSTRUMENT,
|
|
158
|
+
wavetable,
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
return {
|
|
162
|
+
type: "updateTrack",
|
|
163
|
+
trackId,
|
|
164
|
+
patch: { instrument: WAVETABLE_INSTRUMENT, wavetable },
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function current(score: TrackScore, trackId: string): TrackWavetable {
|
|
169
|
+
const track = score.tracks.find((item) => item.id === trackId);
|
|
170
|
+
return track ? wavetableOf(track) : wavetableOf({} as never);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Pure parameter edit; the receipt line and the operation. */
|
|
174
|
+
export function wavetableParamEdit(
|
|
175
|
+
score: TrackScore,
|
|
176
|
+
trackId: string,
|
|
177
|
+
command: Extract<WavetableCommand, { kind: "param" | "warpmode" }>,
|
|
178
|
+
): { operation: ScoreOperation; summary: string } {
|
|
179
|
+
const base = current(score, trackId);
|
|
180
|
+
const next: Record<string, unknown> = { ...base };
|
|
181
|
+
let summary: string;
|
|
182
|
+
if (command.kind === "warpmode") {
|
|
183
|
+
next.warpmode = command.mode;
|
|
184
|
+
summary = `warpmode · ${command.mode}`;
|
|
185
|
+
} else {
|
|
186
|
+
next[command.param] = command.value;
|
|
187
|
+
summary = `${command.param} · ${formatNumber(command.value)}`;
|
|
188
|
+
}
|
|
189
|
+
return {
|
|
190
|
+
operation: wavetableOperation(score, trackId, next as TrackWavetable),
|
|
191
|
+
summary,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** Directory project tables live in, under a track's directory. */
|
|
196
|
+
export const LOCAL_TABLE_DIR = "wavetables";
|
|
197
|
+
/** Most project tables `wt list` and the menu show. */
|
|
198
|
+
const MAX_LISTED_TABLES = 200;
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Project wavetables: `tracks/<dir>/wavetables/*.wav`, sorted. Sync and
|
|
202
|
+
* bounded, so the menu can call it while it builds.
|
|
203
|
+
*/
|
|
204
|
+
export function listLocalWavetables(projectRoot: string | undefined): string[] {
|
|
205
|
+
if (!projectRoot) return [];
|
|
206
|
+
const out: string[] = [];
|
|
207
|
+
let dirs: string[];
|
|
208
|
+
try {
|
|
209
|
+
dirs = readdirSync(join(projectRoot, "tracks")).sort();
|
|
210
|
+
} catch {
|
|
211
|
+
return [];
|
|
212
|
+
}
|
|
213
|
+
for (const dir of dirs) {
|
|
214
|
+
if (dir.startsWith(".")) continue;
|
|
215
|
+
let files: string[];
|
|
216
|
+
try {
|
|
217
|
+
files = readdirSync(join(projectRoot, "tracks", dir, LOCAL_TABLE_DIR));
|
|
218
|
+
} catch {
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
for (const file of files.sort())
|
|
222
|
+
if (/\.wav$/i.test(file) && !file.startsWith("."))
|
|
223
|
+
out.push(`tracks/${dir}/${LOCAL_TABLE_DIR}/${file}`);
|
|
224
|
+
if (out.length >= MAX_LISTED_TABLES) break;
|
|
225
|
+
}
|
|
226
|
+
return out.slice(0, MAX_LISTED_TABLES);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** True when a `wt` argument names a project WAV rather than a table name. */
|
|
230
|
+
export function isLocalTableName(name: string): boolean {
|
|
231
|
+
return /\.wav$/i.test(name.trim()) && !name.trim().startsWith("pack:");
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* The project path for a local table argument: `tracks/…` as is, otherwise
|
|
236
|
+
* relative to the track's directory (`vox.wav` → its `wavetables/vox.wav`),
|
|
237
|
+
* falling back to the only project table with that file name.
|
|
238
|
+
*/
|
|
239
|
+
export function localTablePath(
|
|
240
|
+
score: TrackScore,
|
|
241
|
+
trackId: string,
|
|
242
|
+
name: string,
|
|
243
|
+
projectRoot?: string,
|
|
244
|
+
): string {
|
|
245
|
+
const bare = name.trim().replace(/^\.\//, "");
|
|
246
|
+
if (bare.startsWith("tracks/")) return bare;
|
|
247
|
+
const dir = trackDirectories(score).get(trackId) ?? trackId;
|
|
248
|
+
const own = `tracks/${dir}/${bare.includes("/") ? bare : `${LOCAL_TABLE_DIR}/${bare}`}`;
|
|
249
|
+
if (bare.includes("/")) return own;
|
|
250
|
+
const all = listLocalWavetables(projectRoot);
|
|
251
|
+
if (all.includes(own)) return own;
|
|
252
|
+
const matches = all.filter((path) => path.endsWith(`/${bare}`));
|
|
253
|
+
return matches.length === 1 ? matches[0]! : own;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Resolves and pins a table name; pack tables are fetched once to pin them. */
|
|
257
|
+
export async function pickWavetable(
|
|
258
|
+
store: PackStore,
|
|
259
|
+
score: TrackScore,
|
|
260
|
+
trackId: string,
|
|
261
|
+
name: string,
|
|
262
|
+
projectRoot?: string,
|
|
263
|
+
): Promise<{ operation: ScoreOperation; summary: string }> {
|
|
264
|
+
if (isLocalTableName(name)) {
|
|
265
|
+
if (!projectRoot)
|
|
266
|
+
throw new PackError("project tables need an open project");
|
|
267
|
+
const src = localTablePath(score, trackId, name, projectRoot);
|
|
268
|
+
let sha256: string;
|
|
269
|
+
try {
|
|
270
|
+
const resolved = await resolveReadPath(
|
|
271
|
+
{ root: projectRoot, trackSlug: "" },
|
|
272
|
+
src,
|
|
273
|
+
"wavetable",
|
|
274
|
+
);
|
|
275
|
+
const info = await stat(resolved.real);
|
|
276
|
+
if (!info.isFile()) throw new Error("not a file");
|
|
277
|
+
if (info.size > SCORE_LIMITS.maxSampleFileBytes)
|
|
278
|
+
throw new Error("over the 50 MiB file limit");
|
|
279
|
+
const bytes = new Uint8Array(await readFile(resolved.real));
|
|
280
|
+
wavetableFromWav(src, "check", bytes);
|
|
281
|
+
sha256 = createHash("sha256").update(bytes).digest("hex");
|
|
282
|
+
} catch (error) {
|
|
283
|
+
const why = error instanceof Error ? error.message : String(error);
|
|
284
|
+
throw new PackError(
|
|
285
|
+
`${src.slice(0, 80)} · ${why.slice(0, 120)} · wt list shows the project's tables`,
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
const base = current(score, trackId);
|
|
289
|
+
return {
|
|
290
|
+
operation: wavetableOperation(score, trackId, {
|
|
291
|
+
...base,
|
|
292
|
+
table: { src, sha256 },
|
|
293
|
+
}),
|
|
294
|
+
summary: `wavetable · ${describeTable(src)}`,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
const src = wavetableSource(name);
|
|
298
|
+
if (!src)
|
|
299
|
+
throw new PackError(
|
|
300
|
+
`${name.slice(0, 40)} is not a table · wt list shows ${BUILTIN_TABLE_NAMES.join(", ")} and the wt_ sets`,
|
|
301
|
+
);
|
|
302
|
+
const base = current(score, trackId);
|
|
303
|
+
const table = src.startsWith(BUILTIN_TABLE_PREFIX)
|
|
304
|
+
? { src }
|
|
305
|
+
: await store.pin(src);
|
|
306
|
+
return {
|
|
307
|
+
operation: wavetableOperation(score, trackId, { ...base, table }),
|
|
308
|
+
summary: `wavetable · ${describeTable(table.src)}`,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function describeTable(src: string): string {
|
|
313
|
+
if (src.startsWith(BUILTIN_TABLE_PREFIX)) {
|
|
314
|
+
const name = src.slice(BUILTIN_TABLE_PREFIX.length);
|
|
315
|
+
return `${name} (${BUILTIN_TABLES[name]?.title ?? "built-in"})`;
|
|
316
|
+
}
|
|
317
|
+
if (isLocalTableSrc(src)) return `${src.split("/").pop()} (project)`;
|
|
318
|
+
return src.replace(`pack:${WAVETABLE_PACK}/`, "");
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** `wt` reply: table and the parameters that differ from default. */
|
|
322
|
+
export function describeWavetable(score: TrackScore, trackId: string): string {
|
|
323
|
+
const track = score.tracks.find((item) => item.id === trackId);
|
|
324
|
+
if (!track || !isWavetableInstrument(track.instrument))
|
|
325
|
+
return `${trackId} is not a wavetable track · wt basic makes it one`;
|
|
326
|
+
const settings = wavetableOf(track);
|
|
327
|
+
const params = (Object.keys(WAVETABLE_PARAMS) as WavetableParam[])
|
|
328
|
+
.filter((name) => settings[name] !== undefined)
|
|
329
|
+
.map((name) => `${name} ${formatNumber(settings[name]!)}`);
|
|
330
|
+
if (settings.warpmode) params.push(`warpmode ${settings.warpmode}`);
|
|
331
|
+
return `wavetable · ${describeTable(settings.table.src)}${params.length ? ` · ${params.join(" · ")}` : ""}`;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Lines for `wt list`: built-ins, then each uzu set with its table count. */
|
|
335
|
+
export async function wavetableListLines(
|
|
336
|
+
store: PackStore | undefined,
|
|
337
|
+
projectRoot?: string,
|
|
338
|
+
): Promise<string[]> {
|
|
339
|
+
const lines = ["built-in (offline):"];
|
|
340
|
+
for (const name of BUILTIN_TABLE_NAMES)
|
|
341
|
+
lines.push(` ${name.padEnd(10)} ${BUILTIN_TABLES[name]!.title}`);
|
|
342
|
+
const local = listLocalWavetables(projectRoot);
|
|
343
|
+
lines.push("", "project (tracks/<slug>/wavetables/, made by the agent):");
|
|
344
|
+
if (local.length === 0)
|
|
345
|
+
lines.push(
|
|
346
|
+
" none yet · ask the agent to make_wavetable from any audio file",
|
|
347
|
+
);
|
|
348
|
+
for (const path of local) lines.push(` wt ${path}`);
|
|
349
|
+
lines.push("", `${WAVETABLE_PACK} (Strudel wt_ sounds, fetched on use):`);
|
|
350
|
+
try {
|
|
351
|
+
if (!store) throw new PackError("offline");
|
|
352
|
+
const manifest = await store.manifest(WAVETABLE_PACK);
|
|
353
|
+
for (const [sound, entry] of manifest.sounds) {
|
|
354
|
+
if (!sound.startsWith("wt_") || entry.kind !== "list") continue;
|
|
355
|
+
const count = entry.files.length;
|
|
356
|
+
lines.push(
|
|
357
|
+
` ${sound.padEnd(12)} ${count} tables · wt ${sound}:0 … ${sound}:${count - 1}`,
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
} catch {
|
|
361
|
+
lines.push(
|
|
362
|
+
" wt_digital, wt_vgame · wt wt_digital:0 (needs the network once)",
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
return lines;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function formatNumber(value: number): string {
|
|
369
|
+
return String(Math.round(value * 1000) / 1000);
|
|
370
|
+
}
|