From 03e8e2397d91b2c0a493e82e4cb00327875cc748 Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Fri, 31 Mar 2023 18:24:50 +0100 Subject: [PATCH] Added buttons to play audio on the save audio node --- play-audio/playAudio.js | 74 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 play-audio/playAudio.js diff --git a/play-audio/playAudio.js b/play-audio/playAudio.js new file mode 100644 index 0000000..fc1eb6c --- /dev/null +++ b/play-audio/playAudio.js @@ -0,0 +1,74 @@ +import { app } from "/scripts/app.js"; + +app.registerExtension({ + name: "pysssss.PlayAudio", + async beforeRegisterNodeDef(nodeType, nodeData, app) { + if (nodeData.name === "SaveAudioNode") { + const WIDGETS = Symbol(); + nodeType.prototype.onExecuted = function (data) { + if (WIDGETS in this) { + // Clear all other widgets + if (this.widgets) { + this.widgets.length = this[WIDGETS]; + } + if (this.widgets_values) { + this.widgets_values.length = this.widgets.length; + } + } else { + // On first execute store widget count + this[WIDGETS] = this.widgets?.length || 0; + } + + // For each file create a seek bar + play button + for (const file of data) { + let isTick = true; + const audio = new Audio(`/view?filename=${encodeURIComponent(file)}`); + const slider = this.addWidget( + "slider", + "loading", + 0, + (v) => { + if (!isTick) { + audio.currentTime = v; + } + isTick = false; + }, + { + min: 0, + max: 0, + } + ); + + const button = this.addWidget("button", `Play ${file}`, "play", () => { + try { + if (audio.paused) { + audio.play(); + button.name = `Pause ${file}`; + } else { + audio.pause(); + button.name = `Play ${file}`; + } + } catch (error) { + alert(error); + } + app.canvas.setDirty(true); + }); + audio.addEventListener("timeupdate", () => { + isTick = true; + slider.value = audio.currentTime; + app.canvas.setDirty(true); + }); + audio.addEventListener("ended", () => { + button.name = `Play ${file}`; + app.canvas.setDirty(true); + }); + audio.addEventListener("loadedmetadata", () => { + slider.options.max = audio.duration; + slider.name = `(${audio.duration})`; + app.canvas.setDirty(true); + }); + } + }; + } + }, +});