@audiofool/moondog 0.3.0-beta.1 → 0.3.0-beta.3
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/README.md +1 -1
- package/package.json +1 -1
- package/scripts/moondog.mjs +98 -5
- package/src/core/listening-now.mjs +75 -6
- package/src/core/moondog-application.mjs +63 -18
- package/src/i18n/messages/de.mjs +1 -0
- package/src/i18n/messages/en.mjs +2 -0
- package/src/i18n/messages/es.mjs +1 -0
- package/src/i18n/messages/fr.mjs +1 -0
- package/src/i18n/messages/it.mjs +1 -0
- package/src/i18n/messages/ja.mjs +1 -0
- package/src/i18n/messages/ko.mjs +1 -0
- package/src/i18n/messages/pt.mjs +1 -0
- package/src/i18n/messages/zh-Hant.mjs +1 -0
- package/src/i18n/messages/zh.mjs +1 -0
- package/src/i18n/screen/es.mjs +65 -0
- package/src/i18n/screen/ja.mjs +65 -0
- package/src/i18n/screen/pt.mjs +65 -0
- package/src/i18n/screen/zh.mjs +65 -0
- package/src/integrations/apple-music/terminal-preview-player.mjs +10 -0
- package/src/integrations/netease/ncm-cli-player.mjs +12 -6
- package/src/integrations/stable-audio/local-generator.mjs +216 -80
- package/src/integrations/stable-audio/setup.mjs +653 -0
- package/src/integrations/stable-audio/sources.mjs +106 -0
- package/src/runtime/pi/agent-runtime.mjs +28 -10
- package/src/surfaces/cli/command-completions.mjs +15 -1
- package/src/surfaces/cli/tui.mjs +300 -18
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// Makes music on this Mac with a local Stable Audio 3 install (Stability AI's optimized MLX
|
|
2
2
|
// build). The listener turns it on once, in Settings or with /generation; until then nothing
|
|
3
3
|
// runs. Each track is a WAV and a receipt in Moondog's state directory, and nothing leaves the
|
|
4
|
-
// machine:
|
|
4
|
+
// machine: a run never downloads anything, and the receipt keeps no absolute path. Moondog's own
|
|
5
|
+
// install (setup.mjs) downloads only when the listener asks, from the pinned official sources.
|
|
5
6
|
import { spawn } from "node:child_process";
|
|
6
7
|
import { randomBytes, randomInt, randomUUID } from "node:crypto";
|
|
7
8
|
import { unwatchFile, watchFile } from "node:fs";
|
|
@@ -12,10 +13,11 @@ import path from "node:path";
|
|
|
12
13
|
import { resolveMoondogStateDirectory } from "../../core/state-directory.mjs";
|
|
13
14
|
import { streamVariation } from "../../profile/taste-compiler.mjs";
|
|
14
15
|
import { resolveMoondogAuthFile } from "../../runtime/pi/persistent-credential-store.mjs";
|
|
16
|
+
import { DEFAULT_STABLE_AUDIO_MODEL, isStableAudioModel, SHARED_WEIGHTS, STABLE_AUDIO_MODELS } from "./sources.mjs";
|
|
15
17
|
|
|
16
|
-
// The weights
|
|
17
|
-
const
|
|
18
|
-
// Medium
|
|
18
|
+
// The weights a model needs for text to music: the text encoder, the model and its decoder.
|
|
19
|
+
export const stableAudioWeights = (model = DEFAULT_STABLE_AUDIO_MODEL) => [...SHARED_WEIGHTS, ...STABLE_AUDIO_MODELS[model].weights];
|
|
20
|
+
// Medium and Small Music were trained on up to 380 seconds; very short clips come out rough.
|
|
19
21
|
export const GENERATION_SECONDS = Object.freeze({ min: 15, max: 380, default: 120 });
|
|
20
22
|
// A cold start reads about 4.8 GB of weights before the first step; a 380-second track then takes about 35 seconds.
|
|
21
23
|
const generationTimeoutMs = 10 * 60_000;
|
|
@@ -29,6 +31,11 @@ export function defaultStableAudioRoot() {
|
|
|
29
31
|
return path.join(homedir(), "Developer", "stable-audio-3", "optimized", "mlx");
|
|
30
32
|
}
|
|
31
33
|
|
|
34
|
+
/** The install Moondog downloads and manages itself; the MLX build is in its mlx folder. */
|
|
35
|
+
export function resolveStableAudioManagedRoot(environment = process.env) {
|
|
36
|
+
return path.join(resolveMoondogStateDirectory(environment), "stable-audio-3");
|
|
37
|
+
}
|
|
38
|
+
|
|
32
39
|
// Kept next to the playback preference, apart from runtime settings.
|
|
33
40
|
export function resolveGenerationSettingsFile(environment = process.env) {
|
|
34
41
|
return path.join(path.dirname(resolveMoondogAuthFile(environment)), "generation.json");
|
|
@@ -47,41 +54,92 @@ function literalFolder(value) {
|
|
|
47
54
|
return path.isAbsolute(expanded) ? path.resolve(expanded) : null;
|
|
48
55
|
}
|
|
49
56
|
|
|
57
|
+
export async function readSavedSettings(environment) {
|
|
58
|
+
try {
|
|
59
|
+
const document = JSON.parse(await readFile(resolveGenerationSettingsFile(environment), "utf8"));
|
|
60
|
+
return document?.version === 1 ? document : {};
|
|
61
|
+
} catch {
|
|
62
|
+
return {}; // Not set up yet.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
50
66
|
/**
|
|
51
|
-
* The listener's choice: { enabled, path, source }. Generation is off until they
|
|
52
|
-
* MOONDOG_SA3_DIR can point at another install but never turns it on.
|
|
67
|
+
* The listener's choice: { enabled, path, source, model, folder }. Generation is off until they
|
|
68
|
+
* turn it on; MOONDOG_SA3_DIR can point at another install but never turns it on. source is
|
|
69
|
+
* "managed" for Moondog's own install, "settings" or "default" for the listener's own folder
|
|
70
|
+
* (folder, which Moondog only reads), or "environment". model picks the weights in either.
|
|
53
71
|
* A missing or unreadable file reads as off, so an older or newer file never stops the room.
|
|
54
72
|
*/
|
|
55
|
-
export async function readGenerationSettings(environment = process.env) {
|
|
56
|
-
|
|
57
|
-
try {
|
|
58
|
-
document = JSON.parse(await readFile(resolveGenerationSettingsFile(environment), "utf8"));
|
|
59
|
-
} catch { /* Not set up yet. */ }
|
|
60
|
-
const saved = document?.version === 1 ? document : {};
|
|
73
|
+
export async function readGenerationSettings(environment = process.env, { platform = process.platform, arch = process.arch } = {}) {
|
|
74
|
+
const saved = await readSavedSettings(environment);
|
|
61
75
|
const fromEnvironment = literalFolder(environment.MOONDOG_SA3_DIR);
|
|
62
76
|
const fromFile = literalFolder(saved.path);
|
|
77
|
+
const managed = !fromEnvironment && saved.source === "managed";
|
|
78
|
+
const folder = fromFile ?? defaultStableAudioRoot();
|
|
79
|
+
let model = isStableAudioModel(saved.model) ? saved.model : DEFAULT_STABLE_AUDIO_MODEL;
|
|
80
|
+
// A folder Moondog doesn't manage (MOONDOG_SA3_DIR, a typed folder, ~/Developer) runs the saved model
|
|
81
|
+
// only if it has it, otherwise the first model it can run; a model chosen for Moondog's own install
|
|
82
|
+
// waits until that install is chosen again. A folder that runs nothing keeps the saved model.
|
|
83
|
+
if (!managed) {
|
|
84
|
+
for (const name of [...new Set([model, DEFAULT_STABLE_AUDIO_MODEL, ...Object.keys(STABLE_AUDIO_MODELS)])]) {
|
|
85
|
+
if ((await checkStableAudioInstall(fromEnvironment ?? folder, { model: name, platform, arch })).state === "ready") { model = name; break; }
|
|
86
|
+
}
|
|
87
|
+
}
|
|
63
88
|
return {
|
|
64
89
|
enabled: saved.enabled === true,
|
|
65
|
-
path: fromEnvironment ??
|
|
66
|
-
source: fromEnvironment ? "environment" : fromFile ? "settings" : "default",
|
|
90
|
+
path: fromEnvironment ?? (managed ? path.join(resolveStableAudioManagedRoot(environment), "mlx") : folder),
|
|
91
|
+
source: fromEnvironment ? "environment" : managed ? "managed" : fromFile ? "settings" : "default",
|
|
92
|
+
model,
|
|
93
|
+
folder,
|
|
67
94
|
};
|
|
68
95
|
}
|
|
69
96
|
|
|
70
|
-
|
|
71
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Saves what the listener changed: enabled; path, their own folder, which also switches to it;
|
|
99
|
+
* source "managed" or "settings" to switch between Moondog's install and their folder; model.
|
|
100
|
+
* A model chosen without a folder or a source switches to Moondog's install, which holds it.
|
|
101
|
+
* Older Moondogs read the same version 1 file and see only enabled and path.
|
|
102
|
+
*/
|
|
103
|
+
export async function writeGenerationSettings({ enabled, path: folder, source, model } = {}, environment = process.env, { platform = process.platform, arch = process.arch } = {}) {
|
|
104
|
+
// The file's own view: a folder MOONDOG_SA3_DIR pins in this process is never written into it.
|
|
105
|
+
const current = await readGenerationSettings({ ...environment, MOONDOG_SA3_DIR: "" }, { platform, arch });
|
|
106
|
+
const saved = await readSavedSettings(environment);
|
|
72
107
|
const typed = folder === undefined ? undefined : literalFolder(folder);
|
|
73
108
|
if (folder !== undefined && folder !== null && !typed) {
|
|
74
109
|
throw Object.assign(new Error("Give the Stable Audio 3 folder as a full path, or one starting with ~/."), { code: "generation_path_invalid" });
|
|
75
110
|
}
|
|
111
|
+
if (model !== undefined && !isStableAudioModel(model)) {
|
|
112
|
+
throw Object.assign(new Error("Choose medium or small-music."), { code: "generation_model_invalid" });
|
|
113
|
+
}
|
|
114
|
+
const savedFolder = typed ?? literalFolder(saved.path);
|
|
115
|
+
const managed = typed ? false
|
|
116
|
+
: source === "managed" ? true
|
|
117
|
+
: source === "settings" ? false
|
|
118
|
+
: model !== undefined ? true
|
|
119
|
+
: saved.source === "managed";
|
|
120
|
+
// Switching to the listener's own folder without a model keeps the model only if that folder has it;
|
|
121
|
+
// otherwise the first model it can run, so a working install is never checked for weights it lacks.
|
|
122
|
+
let chosen = model ?? current.model;
|
|
123
|
+
if (model === undefined && !managed && (typed || source === "settings")) {
|
|
124
|
+
const folderRoot = savedFolder ?? current.folder;
|
|
125
|
+
const candidates = [current.model, DEFAULT_STABLE_AUDIO_MODEL, ...Object.keys(STABLE_AUDIO_MODELS)];
|
|
126
|
+
// A folder that runs nothing yet gets Medium, the model the install steps Moondog shows download.
|
|
127
|
+
chosen = DEFAULT_STABLE_AUDIO_MODEL;
|
|
128
|
+
for (const name of [...new Set(candidates)]) {
|
|
129
|
+
if ((await checkStableAudioInstall(folderRoot, { model: name, platform, arch })).state === "ready") { chosen = name; break; }
|
|
130
|
+
}
|
|
131
|
+
}
|
|
76
132
|
const document = {
|
|
77
133
|
version: 1,
|
|
78
134
|
enabled: enabled === undefined ? current.enabled : enabled === true,
|
|
79
|
-
...(
|
|
135
|
+
...(savedFolder ? { path: savedFolder } : {}),
|
|
136
|
+
...(managed ? { source: "managed" } : {}),
|
|
137
|
+
model: chosen,
|
|
80
138
|
};
|
|
81
139
|
const file = resolveGenerationSettingsFile(environment);
|
|
82
140
|
await mkdir(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
83
141
|
await writeFile(file, `${JSON.stringify(document, null, 2)}\n`, { encoding: "utf8", mode: 0o600 });
|
|
84
|
-
return readGenerationSettings(environment);
|
|
142
|
+
return readGenerationSettings(environment, { platform, arch });
|
|
85
143
|
}
|
|
86
144
|
|
|
87
145
|
const isFile = async (file, { executable = false, minimumBytes = 0 } = {}) => {
|
|
@@ -97,16 +155,16 @@ const isFile = async (file, { executable = false, minimumBytes = 0 } = {}) => {
|
|
|
97
155
|
};
|
|
98
156
|
|
|
99
157
|
/**
|
|
100
|
-
* Whether an install can make music now, without running it: "ready", "unsupported"
|
|
101
|
-
* Apple silicon), "missing" (no install at the folder) or "incomplete" (no Python
|
|
102
|
-
* or
|
|
158
|
+
* Whether an install can make music now with a model, without running it: "ready", "unsupported"
|
|
159
|
+
* (MLX needs Apple silicon), "missing" (no install at the folder) or "incomplete" (no Python
|
|
160
|
+
* environment or not that model's weights, which Moondog's setup or Stability's ./install.sh fetches).
|
|
103
161
|
*/
|
|
104
|
-
export async function checkStableAudioInstall(root, { platform = process.platform, arch = process.arch } = {}) {
|
|
162
|
+
export async function checkStableAudioInstall(root, { model = DEFAULT_STABLE_AUDIO_MODEL, platform = process.platform, arch = process.arch } = {}) {
|
|
105
163
|
if (platform !== "darwin" || arch !== "arm64") return { state: "unsupported" };
|
|
106
164
|
if (!root || !path.isAbsolute(root)) return { state: "missing" };
|
|
107
165
|
if (!(await isFile(path.join(root, "scripts", "sa3_mlx.py")))) return { state: "missing" };
|
|
108
166
|
if (!(await isFile(path.join(root, ".venv", "bin", "python"), { executable: true }))) return { state: "incomplete", missing: "environment" };
|
|
109
|
-
for (const weight of
|
|
167
|
+
for (const weight of stableAudioWeights(isStableAudioModel(model) ? model : DEFAULT_STABLE_AUDIO_MODEL)) {
|
|
110
168
|
if (!(await isFile(path.join(root, "models", "mlx", weight), { minimumBytes: 1024 }))) return { state: "incomplete", missing: "weights" };
|
|
111
169
|
}
|
|
112
170
|
return { state: "ready" };
|
|
@@ -171,7 +229,7 @@ export function createLocalGenerator({
|
|
|
171
229
|
spawnImpl = spawn,
|
|
172
230
|
now = () => new Date(),
|
|
173
231
|
} = {}) {
|
|
174
|
-
let current = { state: "unknown", enabled: false, install: "unknown", path: null, source: null };
|
|
232
|
+
let current = { state: "unknown", enabled: false, install: "unknown", path: null, source: null, model: DEFAULT_STABLE_AUDIO_MODEL, managed: false };
|
|
175
233
|
let running = null;
|
|
176
234
|
// The tracks made here, newest last, with what happened each time they played: the loop back
|
|
177
235
|
// from listening reads them. Each track's outcomes.jsonl keeps them; the receipt never changes.
|
|
@@ -196,8 +254,8 @@ export function createLocalGenerator({
|
|
|
196
254
|
const check = () => {
|
|
197
255
|
checking ??= (async () => {
|
|
198
256
|
try {
|
|
199
|
-
const settings = await readGenerationSettings(environment);
|
|
200
|
-
const install = await checkStableAudioInstall(settings.path, { platform, arch });
|
|
257
|
+
const settings = await readGenerationSettings(environment, { platform, arch });
|
|
258
|
+
const install = await checkStableAudioInstall(settings.path, { model: settings.model, platform, arch });
|
|
201
259
|
return publish({
|
|
202
260
|
state: !settings.enabled ? "off" : install.state,
|
|
203
261
|
enabled: settings.enabled,
|
|
@@ -205,6 +263,8 @@ export function createLocalGenerator({
|
|
|
205
263
|
...(install.missing ? { missing: install.missing } : {}),
|
|
206
264
|
path: settings.path,
|
|
207
265
|
source: settings.source,
|
|
266
|
+
model: settings.model,
|
|
267
|
+
managed: settings.source === "managed",
|
|
208
268
|
});
|
|
209
269
|
} finally {
|
|
210
270
|
checking = null;
|
|
@@ -233,13 +293,13 @@ export function createLocalGenerator({
|
|
|
233
293
|
};
|
|
234
294
|
|
|
235
295
|
const stopped = () => Object.assign(new Error("Making the track was stopped."), { name: "AbortError", code: "generation_stopped" });
|
|
236
|
-
const run = ({ root, prompt, seconds, seed, out }, { signal, onProgress }) => new Promise((resolve, reject) => {
|
|
296
|
+
const run = ({ root, model, prompt, seconds, seed, out }, { signal, onProgress }) => new Promise((resolve, reject) => {
|
|
237
297
|
// A stop that came while the install was checked starts nothing.
|
|
238
298
|
if (signal?.aborted) return reject(stopped());
|
|
239
299
|
// The Python script itself, not Stability's ./sa3 wrapper: the wrapper wants uv on PATH and a
|
|
240
300
|
// terminal, and its exec would leave a shell between this process and the one to stop.
|
|
241
301
|
const child = spawnImpl(path.join(root, ".venv", "bin", "python"), [
|
|
242
|
-
"scripts/sa3_mlx.py", "--prompt", prompt, "--dit",
|
|
302
|
+
"scripts/sa3_mlx.py", "--prompt", prompt, "--dit", STABLE_AUDIO_MODELS[model].dit, "--decoder", STABLE_AUDIO_MODELS[model].decoder,
|
|
243
303
|
"--seconds", String(seconds), "--seed", String(seed), "--steps", "8", "--out", out,
|
|
244
304
|
], {
|
|
245
305
|
cwd: root,
|
|
@@ -401,9 +461,10 @@ export function createLocalGenerator({
|
|
|
401
461
|
restart(id) {
|
|
402
462
|
if (playing?.id === id) playing = null;
|
|
403
463
|
},
|
|
404
|
-
// A play Moondog cut off as it began, as a stream past its time limit does, says nothing about the
|
|
464
|
+
// A play Moondog cut off as it began, as a stream past its time limit does, says nothing about the
|
|
465
|
+
// listener. Without an id, whatever made track plays now, as when a stream's first track replaces it.
|
|
405
466
|
discard(id) {
|
|
406
|
-
if (playing
|
|
467
|
+
if (playing && (id === undefined || playing.id === id)) playing = null;
|
|
407
468
|
},
|
|
408
469
|
recordReplay(id, by) {
|
|
409
470
|
this.restart(id);
|
|
@@ -413,7 +474,7 @@ export function createLocalGenerator({
|
|
|
413
474
|
busy: () => running !== null,
|
|
414
475
|
check,
|
|
415
476
|
async save(settings) {
|
|
416
|
-
await writeGenerationSettings(settings, environment);
|
|
477
|
+
await writeGenerationSettings(settings, environment, { platform, arch });
|
|
417
478
|
return check();
|
|
418
479
|
},
|
|
419
480
|
subscribe(listener) {
|
|
@@ -468,7 +529,8 @@ export function createLocalGenerator({
|
|
|
468
529
|
await mkdir(temporary, { mode: 0o700 });
|
|
469
530
|
const wav = path.join(temporary, "track.wav");
|
|
470
531
|
onProgress?.({ stage: "starting" });
|
|
471
|
-
const
|
|
532
|
+
const model = isStableAudioModel(status.model) ? status.model : DEFAULT_STABLE_AUDIO_MODEL;
|
|
533
|
+
const measured = await run({ root: status.path, model, prompt, seconds, seed, out: wav }, { signal, onProgress });
|
|
472
534
|
const audio = await wavInfo(wav).catch(() => null);
|
|
473
535
|
if (!audio || audio.duration_seconds <= 0) throw generationError("generation_failed", "Stable Audio 3 finished but wrote no playable track.");
|
|
474
536
|
await chmod(wav, 0o600);
|
|
@@ -481,7 +543,7 @@ export function createLocalGenerator({
|
|
|
481
543
|
title,
|
|
482
544
|
...receipt,
|
|
483
545
|
generator: {
|
|
484
|
-
adapter: "stable-audio-3", adapter_version: 1, model:
|
|
546
|
+
adapter: "stable-audio-3", adapter_version: 1, model: STABLE_AUDIO_MODELS[model].receipt, decoder: STABLE_AUDIO_MODELS[model].decoder,
|
|
485
547
|
prompt, seconds, seed, steps: 8, cfg: 1, elapsed_ms: elapsed, ...(measured ? { measured } : {}),
|
|
486
548
|
license: "Stability AI Community License; outputs belong to the listener",
|
|
487
549
|
},
|
|
@@ -510,12 +572,53 @@ export function createLocalGenerator({
|
|
|
510
572
|
const STREAM_SKIP_MS = 30_000;
|
|
511
573
|
export function createAtmosphereStream({ generator, player, now = Date.now, keepAudio = 3 }) {
|
|
512
574
|
let stream = null;
|
|
575
|
+
// What the surfaces see (view()) and hear (subscribe): the phase, and the end with its reason.
|
|
576
|
+
const listeners = new Set();
|
|
577
|
+
let shown = "null";
|
|
578
|
+
// Events wait their turn, so a listener that stops the stream from inside one never reorders them.
|
|
579
|
+
const events = [];
|
|
580
|
+
let delivering = false;
|
|
581
|
+
const deliver = (event) => {
|
|
582
|
+
events.push(event);
|
|
583
|
+
if (delivering) return;
|
|
584
|
+
delivering = true;
|
|
585
|
+
try {
|
|
586
|
+
while (events.length) {
|
|
587
|
+
const next = events.shift();
|
|
588
|
+
for (const listener of [...listeners]) {
|
|
589
|
+
try { listener(structuredClone(next)); } catch { /* A surface's redraw cannot stop the stream. */ }
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
} finally {
|
|
593
|
+
delivering = false;
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
const viewOf = (current) => current ? {
|
|
597
|
+
// Nothing of it plays yet; it waits in the dry gap for its next track; it makes the next behind one loaded; or it only plays.
|
|
598
|
+
phase: !current.current ? "starting" : current.waiting ? "waiting" : current.making ? "making" : "playing",
|
|
599
|
+
started_at: new Date(current.startedAt).toISOString(), ends_at: new Date(current.endsAt).toISOString(), tracks_made: current.made.length,
|
|
600
|
+
} : null;
|
|
601
|
+
// Called after anything that may change the view; only a real change reaches the listeners.
|
|
602
|
+
const changed = () => {
|
|
603
|
+
const view = viewOf(stream);
|
|
604
|
+
const key = JSON.stringify(view);
|
|
605
|
+
if (key === shown) return;
|
|
606
|
+
shown = key;
|
|
607
|
+
deliver({ view, ended: null });
|
|
608
|
+
};
|
|
609
|
+
// The tracks of a stream that gave way to Moondog's next one, while the player may still hold one of
|
|
610
|
+
// them; a listener's stop before the new stream's first track plays stops them too.
|
|
611
|
+
let heldOver = null;
|
|
513
612
|
const end = (reason) => {
|
|
514
613
|
const ending = stream;
|
|
515
614
|
if (!ending) return null;
|
|
516
615
|
stream = null;
|
|
517
616
|
ending.controller.abort();
|
|
518
617
|
ending.ended = reason;
|
|
618
|
+
heldOver = reason === "replaced" && (ending.current || heldOver)
|
|
619
|
+
? new Set([...(heldOver ?? []), ...ending.made.map((made) => made.id)]) : null;
|
|
620
|
+
shown = "null";
|
|
621
|
+
deliver({ view: null, ended: reason });
|
|
519
622
|
return ending;
|
|
520
623
|
};
|
|
521
624
|
const trackFor = (ending, position) => {
|
|
@@ -564,6 +667,7 @@ export function createAtmosphereStream({ generator, player, now = Date.now, keep
|
|
|
564
667
|
const current = stream;
|
|
565
668
|
if (!current || current.making || current.next || !fits(current)) return;
|
|
566
669
|
current.making = true;
|
|
670
|
+
changed();
|
|
567
671
|
try {
|
|
568
672
|
const { prompt, title, receipt, variation } = trackFor(current, current.made.length);
|
|
569
673
|
const made = await generator.generate({ prompt, seconds: current.seconds, title, receipt }, { signal: current.controller.signal });
|
|
@@ -574,6 +678,8 @@ export function createAtmosphereStream({ generator, player, now = Date.now, keep
|
|
|
574
678
|
if (current.waiting && expired(current)) { end("time"); return; }
|
|
575
679
|
current.made.push({ id: made.id, featured: variation.featured, started: false, seconds: made.duration_seconds });
|
|
576
680
|
current.next = made.id;
|
|
681
|
+
// Made, so what the player reports from here on sees the stream as only playing.
|
|
682
|
+
current.making = false;
|
|
577
683
|
const track = { id: made.id, title: made.title, seconds: made.duration_seconds, file };
|
|
578
684
|
// A queue that ran dry while this was made starts again with it.
|
|
579
685
|
if (current.waiting) { current.waiting = false; player.playGenerated(track); } else player.enqueueGenerated(track);
|
|
@@ -583,6 +689,52 @@ export function createAtmosphereStream({ generator, player, now = Date.now, keep
|
|
|
583
689
|
if (stream === current && error?.code !== "generation_stopped") end("failed");
|
|
584
690
|
} finally {
|
|
585
691
|
current.making = false;
|
|
692
|
+
changed();
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
// What the room's player reports moves the stream along or ends it.
|
|
696
|
+
const move = (event) => {
|
|
697
|
+
const current = stream;
|
|
698
|
+
// Until its first track plays, what the player reports is about the music before it.
|
|
699
|
+
if (!current || !current.current) return;
|
|
700
|
+
const track = event?.track;
|
|
701
|
+
const ours = (id) => current.made.some((made) => made.id === id);
|
|
702
|
+
// A queue or a song that is not one of the stream's tracks is other music, which ends the stream.
|
|
703
|
+
if (event?.type === "queue" || event?.type === "playing") {
|
|
704
|
+
const items = Array.isArray(event.tracks) ? event.tracks : track ? [track] : [];
|
|
705
|
+
if (items.some((item) => !item?.generated || (item.id && !ours(item.id)))) { end("other_music"); return; }
|
|
706
|
+
}
|
|
707
|
+
const isCurrent = track?.id && track.id === current.current.id;
|
|
708
|
+
if (event?.type === "playing" && track?.id && track.id === current.next) {
|
|
709
|
+
// The window may already have moved on to it when the time limit had passed. The generator
|
|
710
|
+
// has begun counting it, and Moondog's own stop must not read as the listener's skip.
|
|
711
|
+
if (expired(current)) { generator.discard?.(track.id); endInTime(); return; }
|
|
712
|
+
leftEarly(current);
|
|
713
|
+
const made = current.made.find((entry) => entry.id === track.id);
|
|
714
|
+
current.current = { id: track.id, heardMs: 0, since: now(), finished: false, seconds: made?.seconds };
|
|
715
|
+
if (made) made.started = true;
|
|
716
|
+
current.next = null;
|
|
717
|
+
current.waiting = false;
|
|
718
|
+
prune(current);
|
|
719
|
+
void makeNext();
|
|
720
|
+
} else if (event?.type === "paused" && isCurrent && current.current.since !== null) {
|
|
721
|
+
current.current.heardMs += now() - current.current.since;
|
|
722
|
+
current.current.since = null;
|
|
723
|
+
} else if (event?.type === "playing" && isCurrent && current.current.since === null) {
|
|
724
|
+
current.current.since = now();
|
|
725
|
+
} else if (event?.type === "skipped" && isCurrent) {
|
|
726
|
+
// A skip past the end of the queue moves on to the next track once it is made.
|
|
727
|
+
leftEarly(current);
|
|
728
|
+
current.current.finished = true;
|
|
729
|
+
if (!current.next) ranDry(current);
|
|
730
|
+
else if (expired(current)) endInTime();
|
|
731
|
+
} else if ((event?.type === "finished" || event?.type === "failed") && isCurrent) {
|
|
732
|
+
// A track that could not play says nothing about the listener; it is passed over like one that ended.
|
|
733
|
+
current.current.finished = true;
|
|
734
|
+
if (!current.next) ranDry(current);
|
|
735
|
+
else if (expired(current)) endInTime();
|
|
736
|
+
} else if (event?.type === "stopped" && !current.waiting && !current.starting) {
|
|
737
|
+
end("stopped");
|
|
586
738
|
}
|
|
587
739
|
};
|
|
588
740
|
return {
|
|
@@ -593,17 +745,25 @@ export function createAtmosphereStream({ generator, player, now = Date.now, keep
|
|
|
593
745
|
return { tracks_made: stream.made.length, started_at: new Date(stream.startedAt).toISOString(),
|
|
594
746
|
ends_at: new Date(stream.endsAt).toISOString(), steering_away_from: [...stream.leaveOut] };
|
|
595
747
|
},
|
|
748
|
+
/** For the surfaces: { phase, started_at, ends_at, tracks_made }, or null when no stream runs. */
|
|
749
|
+
view: () => viewOf(stream),
|
|
750
|
+
/** Hears { view, ended } once for each change: the start, each phase, each track made, and the end with its reason. */
|
|
751
|
+
subscribe(listener) {
|
|
752
|
+
listeners.add(listener);
|
|
753
|
+
return () => listeners.delete(listener);
|
|
754
|
+
},
|
|
596
755
|
/**
|
|
597
756
|
* Starts a stream from a compiled taste and plays its first track; the next is made at once.
|
|
598
757
|
* beforePlay runs once the first track exists, so other music pauses only when there is one to
|
|
599
|
-
* play. Returns the first track. A stream already running gives way to the new one
|
|
758
|
+
* play. Returns the first track. A stream already running gives way to the new one, and its track
|
|
759
|
+
* plays on until the first new one takes over, so nothing goes silent while that is made.
|
|
600
760
|
*/
|
|
601
761
|
async start({ compiled, title, seconds, minutes, receipt = {} }, { signal, onProgress, onTrack, beforePlay } = {}) {
|
|
602
|
-
|
|
603
|
-
if (previous?.current) player.stop?.();
|
|
762
|
+
end("replaced");
|
|
604
763
|
const current = { compiled, title, seconds, receipt, onTrack, startedAt: now(), endsAt: now() + minutes * 60_000,
|
|
605
764
|
made: [], leaveOut: new Set(), current: null, next: null, waiting: false, making: true, controller: new AbortController() };
|
|
606
765
|
stream = current;
|
|
766
|
+
changed();
|
|
607
767
|
const stopped = () => Object.assign(new Error("The stream was stopped before it began."), { code: "generation_stopped" });
|
|
608
768
|
try {
|
|
609
769
|
const { prompt, title: first, receipt: firstReceipt, variation } = trackFor(current, 0);
|
|
@@ -617,67 +777,43 @@ export function createAtmosphereStream({ generator, player, now = Date.now, keep
|
|
|
617
777
|
current.made.push({ id: made.id, featured: variation.featured, started: true, seconds: made.duration_seconds });
|
|
618
778
|
current.current = { id: made.id, heardMs: 0, since: now(), finished: false, seconds: made.duration_seconds };
|
|
619
779
|
current.making = false;
|
|
780
|
+
// The made track it replaces, a single one or the last stream's, was cut off by Moondog, not
|
|
781
|
+
// left by the listener: counted as a skip, it would steer later tracks away from its sound.
|
|
782
|
+
generator.discard?.();
|
|
620
783
|
// Whatever was playing stops for the first track; that stop is not the listener's.
|
|
621
784
|
current.starting = true;
|
|
622
785
|
try { player.playGenerated({ id: made.id, title: made.title, seconds: made.duration_seconds, file }); } finally { current.starting = false; }
|
|
786
|
+
heldOver = null;
|
|
623
787
|
onTrack?.(made);
|
|
624
788
|
void makeNext();
|
|
789
|
+
changed();
|
|
625
790
|
return { made, prompt, variation };
|
|
626
791
|
} catch (error) {
|
|
627
|
-
|
|
792
|
+
// A reply stopped while the first track was made is the listener's own stop, not a failure.
|
|
793
|
+
if (stream === current) end(error?.code === "generation_stopped" ? "asked" : "failed");
|
|
794
|
+
// Why it ended tells the listener turning it off apart from a stop of the reply.
|
|
795
|
+
if (error?.code === "generation_stopped" && error.ended === undefined) error.ended = current.ended ?? null;
|
|
628
796
|
throw error;
|
|
629
797
|
}
|
|
630
798
|
},
|
|
631
799
|
stop(reason = "asked") {
|
|
800
|
+
const held = heldOver;
|
|
632
801
|
const ending = end(reason);
|
|
633
|
-
// Before its first track plays, the player still holds the music from before the stream, which
|
|
634
|
-
|
|
802
|
+
// Before its first track plays, the player still holds the music from before the stream, which
|
|
803
|
+
// stays, unless that is the track of the stream it replaced. A stream replaced by Moondog's next
|
|
804
|
+
// track or stream plays on until that one takes over.
|
|
805
|
+
if (ending && reason !== "replaced" && (ending.current || held)) player.stop?.();
|
|
635
806
|
return ending ? { tracks_made: ending.made.length } : null;
|
|
636
807
|
},
|
|
637
808
|
/** What the room's player reports, which moves the stream along or ends it. */
|
|
638
809
|
observe(event) {
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
const ours = (id) => current.made.some((made) => made.id === id);
|
|
644
|
-
// A queue or a song that is not one of the stream's tracks is other music, which ends the stream.
|
|
645
|
-
if (event?.type === "queue" || event?.type === "playing") {
|
|
646
|
-
const items = Array.isArray(event.tracks) ? event.tracks : track ? [track] : [];
|
|
647
|
-
if (items.some((item) => !item?.generated || (item.id && !ours(item.id)))) { end("other_music"); return; }
|
|
648
|
-
}
|
|
649
|
-
const isCurrent = track?.id && track.id === current.current.id;
|
|
650
|
-
if (event?.type === "playing" && track?.id && track.id === current.next) {
|
|
651
|
-
// The window may already have moved on to it when the time limit had passed. The generator
|
|
652
|
-
// has begun counting it, and Moondog's own stop must not read as the listener's skip.
|
|
653
|
-
if (expired(current)) { generator.discard?.(track.id); endInTime(); return; }
|
|
654
|
-
leftEarly(current);
|
|
655
|
-
const made = current.made.find((entry) => entry.id === track.id);
|
|
656
|
-
current.current = { id: track.id, heardMs: 0, since: now(), finished: false, seconds: made?.seconds };
|
|
657
|
-
if (made) made.started = true;
|
|
658
|
-
current.next = null;
|
|
659
|
-
current.waiting = false;
|
|
660
|
-
prune(current);
|
|
661
|
-
void makeNext();
|
|
662
|
-
} else if (event?.type === "paused" && isCurrent && current.current.since !== null) {
|
|
663
|
-
current.current.heardMs += now() - current.current.since;
|
|
664
|
-
current.current.since = null;
|
|
665
|
-
} else if (event?.type === "playing" && isCurrent && current.current.since === null) {
|
|
666
|
-
current.current.since = now();
|
|
667
|
-
} else if (event?.type === "skipped" && isCurrent) {
|
|
668
|
-
// A skip past the end of the queue moves on to the next track once it is made.
|
|
669
|
-
leftEarly(current);
|
|
670
|
-
current.current.finished = true;
|
|
671
|
-
if (!current.next) ranDry(current);
|
|
672
|
-
else if (expired(current)) endInTime();
|
|
673
|
-
} else if ((event?.type === "finished" || event?.type === "failed") && isCurrent) {
|
|
674
|
-
// A track that could not play says nothing about the listener; it is passed over like one that ended.
|
|
675
|
-
current.current.finished = true;
|
|
676
|
-
if (!current.next) ranDry(current);
|
|
677
|
-
else if (expired(current)) endInTime();
|
|
678
|
-
} else if (event?.type === "stopped" && !current.waiting && !current.starting) {
|
|
679
|
-
end("stopped");
|
|
810
|
+
// Other music, or a stop, means the player no longer holds the replaced stream's tracks.
|
|
811
|
+
if (heldOver) {
|
|
812
|
+
const items = Array.isArray(event?.tracks) ? event.tracks : event?.track ? [event.track] : [];
|
|
813
|
+
if (event?.type === "stopped" || ((event?.type === "queue" || event?.type === "playing") && items.some((item) => !heldOver.has(item?.id)))) heldOver = null;
|
|
680
814
|
}
|
|
815
|
+
move(event);
|
|
816
|
+
changed();
|
|
681
817
|
},
|
|
682
818
|
};
|
|
683
819
|
}
|