diff --git a/src/pages/constituent/blur/artwork-controller/_applet.astro b/src/pages/constituent/blur/artwork-controller/_applet.astro index fe74dae..2995250 100644 --- a/src/pages/constituent/blur/artwork-controller/_applet.astro +++ b/src/pages/constituent/blur/artwork-controller/_applet.astro @@ -383,9 +383,11 @@ import "@styles/diffuse/fonts.css"; }; const orchestrator = { - primary: applet("/orchestrator/primary", { - groupId: context.groupId, - }), + queueAudio: applet("/orchestrator/queue-audio", { groupId: context.groupId }), + queueTracks: isMainGroup() + ? applet("/orchestrator/queue-tracks", { groupId: context.groupId }) + : undefined, + processTracks: isMainGroup() ? applet("/orchestrator/process-tracks") : undefined, }; const processor = { @@ -400,15 +402,16 @@ import "@styles/diffuse/fonts.css"; const prog = progress(); const curr = engine.queue.data.now; const audio = curr ? engine.audio.data.items[curr.id] : undefined; + const duration = curr?.stats?.duration ?? audio?.duration; - if (audio) { + if (audio && duration != undefined && !isNaN(duration)) { const p = Temporal.Duration.from({ - milliseconds: Math.round(audio.duration * prog * 1000), + milliseconds: Math.round(duration * prog * 1000), }).round({ largestUnit: "hours", }); - const d = Temporal.Duration.from({ milliseconds: Math.round(audio.duration * 1000) }).round({ + const d = Temporal.Duration.from({ milliseconds: Math.round(duration * 1000) }).round({ largestUnit: "hours", }); @@ -462,18 +465,6 @@ import "@styles/diffuse/fonts.css"; (volume) => setVolume(volume), ); - // ORCHESTRATED - - context.settled().then(async () => { - if (context.isMainInstance()) monitorAudioStuff(); - }); - - async function monitorAudioStuff() { - (await orchestrator.primary).sendAction("monitorAudioEnd", undefined, { - timeoutDuration: 60000, - }); - } - //////////////////////////////////////////// // 🎢 QUEUE //////////////////////////////////////////// @@ -528,54 +519,6 @@ import "@styles/diffuse/fonts.css"; if (cacheId === currCacheId) setArtwork(art); } - // ORCHESTRATED - - context.settled().then(async () => { - if (context.isMainInstance()) monitorQueueStuff(); - }); - - async function monitorQueueStuff() { - (await orchestrator.primary).sendAction("monitorActiveQueueItem", undefined, { - timeoutDuration: 60000, - }); - } - - //////////////////////////////////////////// - // TRACKS - //////////////////////////////////////////// - - // ORCHESTRATED - - context.settled().then(() => { - if (isMainGroup() && context.isMainInstance()) { - return monitorTracksStuff().then(processInputs); - } - }); - - async function monitorTracksStuff() { - (await orchestrator.primary).sendAction("monitorTracks", undefined, { - timeoutDuration: 60000 * 5, - }); - } - - async function processInputs() { - (await orchestrator.primary).sendAction("processInputs", undefined, { - timeoutDuration: 60000 * 60, - }); - } - - //////////////////////////////////////////// - // 🚛 HYDRATE - //////////////////////////////////////////// - - context.unloadHandler = async () => { - if (context.isMainInstance()) { - monitorAudioStuff(); - monitorQueueStuff(); - if (isMainGroup()) monitorTracksStuff(); - } - }; - //////////////////////////////////////////// // UI //////////////////////////////////////////// diff --git a/src/pages/engine/audio/_applet.astro b/src/pages/engine/audio/_applet.astro index d7699a3..f15f4d7 100644 --- a/src/pages/engine/audio/_applet.astro +++ b/src/pages/engine/audio/_applet.astro @@ -14,6 +14,7 @@ // SETUP //////////////////////////////////////////// const context = register(); + const groupId = context.groupId ?? "main"; // Audio elements container const container = document.createElement("div"); @@ -21,7 +22,7 @@ document.body.appendChild(container); // Default volume - const VOLUME_KEY = `@applets/engine/audio/${context.groupId || "main"}/volume`; + const VOLUME_KEY = `@applets/engine/audio/${groupId}/volume`; const vol = localStorage.getItem(VOLUME_KEY); // Initial state @@ -63,6 +64,7 @@ // Unload context.unloadHandler = async () => { + console.log("UNLOADED"); await context.settled(); hydrateItems(); }; diff --git a/src/pages/index.astro b/src/pages/index.astro index cc5fb16..ca8c63b 100644 --- a/src/pages/index.astro +++ b/src/pages/index.astro @@ -46,7 +46,11 @@ const input = [ { url: "input/s3/", title: "S3-Compatible API" }, ]; -const orchestrators = [{ url: "orchestrator/primary/", title: "Primary (Queue, audio, tracks)" }]; +const orchestrators = [ + { url: "orchestrator/queue-audio/", title: "Queue ⭤ Audio" }, + { url: "orchestrator/queue-tracks/", title: "Queue ⭤ Tracks" }, + { url: "orchestrator/process-tracks/", title: "Process inputs into tracks" }, +]; const output = [ { url: "output/indexed-db/", title: "IndexedDB" }, @@ -145,7 +149,7 @@ const processors = [

There's tradeoffs to both approaches. A particular tradeoff to keep in mind for constituents is that they'll have nested dependencies. So when overriding applets dependencies, the - overrides need to passed down the tree. + overrides need to be passed down the tree.

diff --git a/src/pages/orchestrator/primary/_applet.astro b/src/pages/orchestrator/primary/_applet.astro deleted file mode 100644 index fe88f9c..0000000 --- a/src/pages/orchestrator/primary/_applet.astro +++ /dev/null @@ -1,237 +0,0 @@ - diff --git a/src/pages/orchestrator/primary/_manifest.json b/src/pages/orchestrator/primary/_manifest.json deleted file mode 100644 index b922e3b..0000000 --- a/src/pages/orchestrator/primary/_manifest.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "name": "diffuse/orchestrator/primary", - "title": "Diffuse Orchestrator | Primary", - "entrypoint": "index.html", - "actions": {} -} diff --git a/src/pages/orchestrator/process-tracks/_applet.astro b/src/pages/orchestrator/process-tracks/_applet.astro new file mode 100644 index 0000000..e7a23fb --- /dev/null +++ b/src/pages/orchestrator/process-tracks/_applet.astro @@ -0,0 +1,118 @@ + diff --git a/src/pages/orchestrator/process-tracks/_manifest.json b/src/pages/orchestrator/process-tracks/_manifest.json new file mode 100644 index 0000000..dbbbddf --- /dev/null +++ b/src/pages/orchestrator/process-tracks/_manifest.json @@ -0,0 +1,6 @@ +{ + "name": "diffuse/orchestrator/process-tracks", + "title": "Diffuse Orchestrator | Process Tracks", + "entrypoint": "index.html", + "actions": {} +} diff --git a/src/pages/orchestrator/primary/index.astro b/src/pages/orchestrator/process-tracks/index.astro similarity index 100% rename from src/pages/orchestrator/primary/index.astro rename to src/pages/orchestrator/process-tracks/index.astro diff --git a/src/pages/orchestrator/queue-audio/_applet.astro b/src/pages/orchestrator/queue-audio/_applet.astro new file mode 100644 index 0000000..fbc6a65 --- /dev/null +++ b/src/pages/orchestrator/queue-audio/_applet.astro @@ -0,0 +1,96 @@ + diff --git a/src/pages/orchestrator/queue-audio/_manifest.json b/src/pages/orchestrator/queue-audio/_manifest.json new file mode 100644 index 0000000..abe028f --- /dev/null +++ b/src/pages/orchestrator/queue-audio/_manifest.json @@ -0,0 +1,6 @@ +{ + "name": "diffuse/orchestrator/queue-audio", + "title": "Diffuse Orchestrator | Queue Audio", + "entrypoint": "index.html", + "actions": {} +} diff --git a/src/pages/orchestrator/queue-audio/index.astro b/src/pages/orchestrator/queue-audio/index.astro new file mode 100644 index 0000000..dfb1e1a --- /dev/null +++ b/src/pages/orchestrator/queue-audio/index.astro @@ -0,0 +1,9 @@ +--- +import Layout from "@layouts/applet.astro"; +import Applet from "./_applet.astro"; +import { title } from "./_manifest.json"; +--- + + + + diff --git a/src/pages/orchestrator/queue-tracks/_applet.astro b/src/pages/orchestrator/queue-tracks/_applet.astro new file mode 100644 index 0000000..d659dd6 --- /dev/null +++ b/src/pages/orchestrator/queue-tracks/_applet.astro @@ -0,0 +1,78 @@ + diff --git a/src/pages/orchestrator/queue-tracks/_manifest.json b/src/pages/orchestrator/queue-tracks/_manifest.json new file mode 100644 index 0000000..5e44c91 --- /dev/null +++ b/src/pages/orchestrator/queue-tracks/_manifest.json @@ -0,0 +1,6 @@ +{ + "name": "diffuse/orchestrator/queue-tracks", + "title": "Diffuse Orchestrator | Queue Tracks", + "entrypoint": "index.html", + "actions": {} +} diff --git a/src/pages/orchestrator/queue-tracks/index.astro b/src/pages/orchestrator/queue-tracks/index.astro new file mode 100644 index 0000000..dfb1e1a --- /dev/null +++ b/src/pages/orchestrator/queue-tracks/index.astro @@ -0,0 +1,9 @@ +--- +import Layout from "@layouts/applet.astro"; +import Applet from "./_applet.astro"; +import { title } from "./_manifest.json"; +--- + + + + diff --git a/src/pages/theme/pilot/index.astro b/src/pages/theme/pilot/index.astro index 6b9bf22..157211b 100644 --- a/src/pages/theme/pilot/index.astro +++ b/src/pages/theme/pilot/index.astro @@ -8,7 +8,4 @@ import "@styles/theme/pilot/index.css";
- - - diff --git a/src/scripts/applet/common.ts b/src/scripts/applet/common.ts index 83a400f..0604d85 100644 --- a/src/scripts/applet/common.ts +++ b/src/scripts/applet/common.ts @@ -33,10 +33,7 @@ export async function applet( }`; let query: undefined | Record; - - if (opts.groupId) { - query = { groupId: opts.groupId }; - } + query = { groupId: opts.groupId || "main" }; if (query) { src = QS.stringifyUrl({ url: src, query }); @@ -238,7 +235,7 @@ function broadcastChannel({ originInstanceId: instanceId, }); - if (isMain()) { + if (isMain() && event.data?.isInitialPing === true) { channel.postMessage({ type: "data", data: context.codec.encode(scope.data), @@ -260,7 +257,7 @@ function broadcastChannel({ setTimeout(async () => { const promised = await makeMainPromise(); setIsMain(promised.isMain); - context.unloadHandler?.(); + if (promised.isMain) context.unloadHandler?.(); }, 250); } break; @@ -286,6 +283,8 @@ function broadcastChannel({ }); // Promise that fullfills whenever it figures out its the main instance or not. + let pinged = false; + function makeMainPromise(timeoutDuration: number = 500) { return new Promise<{ isMain: boolean }>((resolve) => { const timeoutId = setTimeout(() => { @@ -308,7 +307,10 @@ function broadcastChannel({ channel.postMessage({ type: "PING", instanceId, + isInitialPing: !pinged, }); + + pinged = true; }); } diff --git a/src/scripts/orchestrator/primary/worker.ts b/src/scripts/orchestrator/primary/worker.ts deleted file mode 100644 index f286231..0000000 --- a/src/scripts/orchestrator/primary/worker.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { provide } from "@scripts/common"; - -//////////////////////////////////////////// -// SETUP -//////////////////////////////////////////// - -const actions = {}; - -const { tasks } = provide({ actions, tasks: actions }); - -export type Actions = typeof actions; -export type Tasks = typeof tasks; - -//////////////////////////////////////////// -// ACTIONS -//////////////////////////////////////////// diff --git a/src/scripts/theme/blur/index.ts b/src/scripts/theme/blur/index.ts index fa7c272..770f897 100644 --- a/src/scripts/theme/blur/index.ts +++ b/src/scripts/theme/blur/index.ts @@ -21,6 +21,7 @@ const _constituent = { b: applet("/constituent/blur/artwork-controller", { container, groupId: labelB }), }; +// TODO: // const _orchestrator = { // primary: applet("/orchestrator/primary", { groupId: labelA }), // }; diff --git a/src/scripts/theme/pilot/index.ts b/src/scripts/theme/pilot/index.ts index bda2bc9..ef53b72 100644 --- a/src/scripts/theme/pilot/index.ts +++ b/src/scripts/theme/pilot/index.ts @@ -14,7 +14,9 @@ const engine = { }; const orchestrator = { - primary: await applet("/orchestrator/primary"), + queueAudio: applet("/orchestrator/queue-audio"), + queueTracks: applet("/orchestrator/queue-tracks"), + processTracks: applet("/orchestrator/process-tracks"), }; const ui = { @@ -47,23 +49,22 @@ reactive( // 🔉 AUDIO //////////////////////////////////////////// +let initialAudioChecked = false; + reactive( ui.audio, (data) => data.isPlaying, - async (isPlaying, setter) => { + async (isPlaying) => { const audioId = engine.queue.data.now?.id; - // Automatically start playing something if nothing is playing yet. - if (!audioId) { - if (isPlaying) { - const now = await engine.queue.sendAction("shift", { groupId: "main" }); - if (!now) { - console.warn("No tracks available yet, try again later."); - await ui.audio.sendAction("modifyIsPlaying", false); - setter(false); - } + // Sync audio state and ui state + // TODO: Figure out a better way to do this + if (!initialAudioChecked) { + if (engine.audio.data.isPlaying && !isPlaying) { + ui.audio.sendAction("modifyIsPlaying", true); + initialAudioChecked = true; + return; } - return; } // Otherwise just control the audio diff --git a/src/scripts/theme/webamp/index.ts b/src/scripts/theme/webamp/index.ts index 010387a..a69afef 100644 --- a/src/scripts/theme/webamp/index.ts +++ b/src/scripts/theme/webamp/index.ts @@ -13,7 +13,9 @@ const configurator = { }; const orchestrator = { - primary: applet("/orchestrator/primary"), + queueAudio: applet("/orchestrator/queue-audio"), + queueTracks: applet("/orchestrator/queue-tracks"), + processTracks: applet("/orchestrator/process-tracks"), }; //////////////////////////////////////////// @@ -48,9 +50,6 @@ configurator.output }) .then(async () => { await loadAndInsert(); - (await orchestrator.primary).sendAction("process_inputs", undefined, { - timeoutDuration: 60000 * 60, - }); }); // Load & insert