From 0e59bd5badaeb698e00cdc84e33f2a25169d49fa Mon Sep 17 00:00:00 2001 From: duojet2ez Date: Mon, 22 Jul 2024 21:05:11 -0700 Subject: [PATCH 1/5] added stop feature for hello audio worklet example --- src/_data/build_info.json | 2 +- .../basic/hello-audio-worklet/main.js | 46 +++++++++++++++---- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/_data/build_info.json b/src/_data/build_info.json index e27ca5445..9a3607d01 100644 --- a/src/_data/build_info.json +++ b/src/_data/build_info.json @@ -1 +1 @@ -{"version":"3.2.0","revision":"f2acc10","lastUpdated":"2024-07-20","copyrightYear":2024} \ No newline at end of file +{"version":"3.2.0","revision":"0161cce","lastUpdated":"2024-07-22","copyrightYear":2024} \ No newline at end of file diff --git a/src/audio-worklet/basic/hello-audio-worklet/main.js b/src/audio-worklet/basic/hello-audio-worklet/main.js index 5407a2710..fbaac9385 100644 --- a/src/audio-worklet/basic/hello-audio-worklet/main.js +++ b/src/audio-worklet/basic/hello-audio-worklet/main.js @@ -3,13 +3,19 @@ // found in the LICENSE file. const audioContext = new AudioContext(); +let isModuleLoaded = false; +let isPlaying = false; +let oscillatorNode = null; const startAudio = async (context) => { - await context.audioWorklet.addModule('bypass-processor.js'); - const oscillator = new OscillatorNode(context); - const bypasser = new AudioWorkletNode(context, 'bypass-processor'); - oscillator.connect(bypasser).connect(context.destination); - oscillator.start(); + if (!isModuleLoaded) { + await context.audioWorklet.addModule('bypass-processor.js'); + oscillatorNode = new OscillatorNode(context); + const bypasser = new AudioWorkletNode(context, 'bypass-processor'); + oscillatorNode.connect(bypasser).connect(context.destination); + oscillatorNode.start(); + isModuleLoaded = true; + } else context.resume(); }; // A simplem onLoad handler. It also handles user gesture to unlock the audio @@ -17,10 +23,30 @@ const startAudio = async (context) => { window.addEventListener('load', async () => { const buttonEl = document.getElementById('button-start'); buttonEl.disabled = false; + buttonEl.addEventListener('click', async () => { - await startAudio(audioContext); - audioContext.resume(); - buttonEl.disabled = true; - buttonEl.textContent = 'Playing...'; - }, false); + if (!isPlaying) { + await startAudio(audioContext); + isPlaying = true; + buttonEl.textContent = 'Playing...'; + buttonEl.classList.remove('start-button'); + } else { + audioContext.suspend(); + isPlaying = false; + buttonEl.textContent = 'START'; + buttonEl.classList.add('start-button'); + } + }); + + buttonEl.addEventListener('mouseenter', () => { + if (isPlaying) { + buttonEl.textContent = 'STOP'; + } + }); + + buttonEl.addEventListener('mouseleave', () => { + if (isPlaying) { + buttonEl.textContent = 'Playing...'; + } + }); }); From 843739e7a532cd6f06455d01feccc95b9f34b81b Mon Sep 17 00:00:00 2001 From: duojet2ez Date: Mon, 22 Jul 2024 21:12:04 -0700 Subject: [PATCH 2/5] update src/_data/build_info.json --- src/_data/build_info.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_data/build_info.json b/src/_data/build_info.json index 9a3607d01..662382721 100644 --- a/src/_data/build_info.json +++ b/src/_data/build_info.json @@ -1 +1 @@ -{"version":"3.2.0","revision":"0161cce","lastUpdated":"2024-07-22","copyrightYear":2024} \ No newline at end of file +{"version":"3.2.0","revision":"0e59bd5","lastUpdated":"2024-07-22","copyrightYear":2024} \ No newline at end of file From 2e64033a2d99fe3c4018522e4873c261aa9b5dfa Mon Sep 17 00:00:00 2001 From: duojet2ez Date: Wed, 24 Jul 2024 10:06:43 -0700 Subject: [PATCH 3/5] removed hovering lines and audioContext.resume written under buttonE1 addEventListener callback --- src/_data/build_info.json | 2 +- .../basic/hello-audio-worklet/main.js | 15 ++------------- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/src/_data/build_info.json b/src/_data/build_info.json index 662382721..5d9b34981 100644 --- a/src/_data/build_info.json +++ b/src/_data/build_info.json @@ -1 +1 @@ -{"version":"3.2.0","revision":"0e59bd5","lastUpdated":"2024-07-22","copyrightYear":2024} \ No newline at end of file +{"version":"3.2.0","revision":"843739e","lastUpdated":"2024-07-22","copyrightYear":2024} \ No newline at end of file diff --git a/src/audio-worklet/basic/hello-audio-worklet/main.js b/src/audio-worklet/basic/hello-audio-worklet/main.js index fbaac9385..b1bb982eb 100644 --- a/src/audio-worklet/basic/hello-audio-worklet/main.js +++ b/src/audio-worklet/basic/hello-audio-worklet/main.js @@ -15,7 +15,7 @@ const startAudio = async (context) => { oscillatorNode.connect(bypasser).connect(context.destination); oscillatorNode.start(); isModuleLoaded = true; - } else context.resume(); + } }; // A simplem onLoad handler. It also handles user gesture to unlock the audio @@ -30,6 +30,7 @@ window.addEventListener('load', async () => { isPlaying = true; buttonEl.textContent = 'Playing...'; buttonEl.classList.remove('start-button'); + audioContext.resume(); } else { audioContext.suspend(); isPlaying = false; @@ -37,16 +38,4 @@ window.addEventListener('load', async () => { buttonEl.classList.add('start-button'); } }); - - buttonEl.addEventListener('mouseenter', () => { - if (isPlaying) { - buttonEl.textContent = 'STOP'; - } - }); - - buttonEl.addEventListener('mouseleave', () => { - if (isPlaying) { - buttonEl.textContent = 'Playing...'; - } - }); }); From 282a1ba3cf391df2a26579449e2378fb9599067c Mon Sep 17 00:00:00 2001 From: duojet2ez Date: Fri, 26 Jul 2024 15:45:41 -0700 Subject: [PATCH 4/5] added isGraphReady and extracted lines out of startAudio to loadGraph function --- src/_data/build_info.json | 2 +- .../basic/hello-audio-worklet/main.js | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/_data/build_info.json b/src/_data/build_info.json index 5d9b34981..76e82b52f 100644 --- a/src/_data/build_info.json +++ b/src/_data/build_info.json @@ -1 +1 @@ -{"version":"3.2.0","revision":"843739e","lastUpdated":"2024-07-22","copyrightYear":2024} \ No newline at end of file +{"version":"3.2.0","revision":"2e64033","lastUpdated":"2024-07-24","copyrightYear":2024} \ No newline at end of file diff --git a/src/audio-worklet/basic/hello-audio-worklet/main.js b/src/audio-worklet/basic/hello-audio-worklet/main.js index b1bb982eb..43a6b32b4 100644 --- a/src/audio-worklet/basic/hello-audio-worklet/main.js +++ b/src/audio-worklet/basic/hello-audio-worklet/main.js @@ -5,17 +5,26 @@ const audioContext = new AudioContext(); let isModuleLoaded = false; let isPlaying = false; +let isGraphReady = false; let oscillatorNode = null; + +const loadGraph = (context) => { + oscillatorNode = new OscillatorNode(context); + const bypasser = new AudioWorkletNode(context, 'bypass-processor'); + oscillatorNode.connect(bypasser).connect(context.destination); + oscillatorNode.start(); +}; + const startAudio = async (context) => { if (!isModuleLoaded) { await context.audioWorklet.addModule('bypass-processor.js'); - oscillatorNode = new OscillatorNode(context); - const bypasser = new AudioWorkletNode(context, 'bypass-processor'); - oscillatorNode.connect(bypasser).connect(context.destination); - oscillatorNode.start(); isModuleLoaded = true; } + if (!isGraphReady) { + loadGraph(audioContext); + isGraphReady = true; + } }; // A simplem onLoad handler. It also handles user gesture to unlock the audio From 3a0c860d4067115d729c42b9f750f07e8b693428 Mon Sep 17 00:00:00 2001 From: duojet2ez Date: Fri, 26 Jul 2024 21:41:59 -0700 Subject: [PATCH 5/5] load graph function + bool isGraphReady --- src/_data/build_info.json | 2 +- src/audio-worklet/basic/hello-audio-worklet/main.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/_data/build_info.json b/src/_data/build_info.json index 76e82b52f..c74792bcd 100644 --- a/src/_data/build_info.json +++ b/src/_data/build_info.json @@ -1 +1 @@ -{"version":"3.2.0","revision":"2e64033","lastUpdated":"2024-07-24","copyrightYear":2024} \ No newline at end of file +{"version":"3.2.0","revision":"282a1ba","lastUpdated":"2024-07-26","copyrightYear":2024} \ No newline at end of file diff --git a/src/audio-worklet/basic/hello-audio-worklet/main.js b/src/audio-worklet/basic/hello-audio-worklet/main.js index 43a6b32b4..aa2233725 100644 --- a/src/audio-worklet/basic/hello-audio-worklet/main.js +++ b/src/audio-worklet/basic/hello-audio-worklet/main.js @@ -8,7 +8,6 @@ let isPlaying = false; let isGraphReady = false; let oscillatorNode = null; - const loadGraph = (context) => { oscillatorNode = new OscillatorNode(context); const bypasser = new AudioWorkletNode(context, 'bypass-processor');