Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added stop button for hello audio worklet example #387

Merged
merged 6 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/_data/build_info.json
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
{"version":"3.2.0","revision":"0c4b2ce","lastUpdated":"2024-07-22","copyrightYear":2024}
{"version":"3.2.0","revision":"0c4b2ce","lastUpdated":"2024-07-22","copyrightYear":2024}

43 changes: 33 additions & 10 deletions src/audio-worklet/basic/hello-audio-worklet/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,47 @@
// found in the LICENSE file.

const audioContext = new AudioContext();
let isModuleLoaded = false;
duojet2ez marked this conversation as resolved.
Show resolved Hide resolved
let isPlaying = false;
let isGraphReady = false;
let oscillatorNode = null;

const startAudio = async (context) => {
await context.audioWorklet.addModule('bypass-processor.js');
const oscillator = new OscillatorNode(context);
const loadGraph = (context) => {
oscillatorNode = new OscillatorNode(context);
const bypasser = new AudioWorkletNode(context, 'bypass-processor');
oscillator.connect(bypasser).connect(context.destination);
oscillator.start();
oscillatorNode.connect(bypasser).connect(context.destination);
oscillatorNode.start();
};

const startAudio = async (context) => {
if (!isModuleLoaded) {
await context.audioWorklet.addModule('bypass-processor.js');
isModuleLoaded = true;
}
if (!isGraphReady) {
loadGraph(audioContext);
isGraphReady = true;
}
};

// A simplem onLoad handler. It also handles user gesture to unlock the audio
// playback.
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);
duojet2ez marked this conversation as resolved.
Show resolved Hide resolved
isPlaying = true;
buttonEl.textContent = 'Playing...';
buttonEl.classList.remove('start-button');
audioContext.resume();
} else {
audioContext.suspend();
isPlaying = false;
buttonEl.textContent = 'START';
buttonEl.classList.add('start-button');
}
});
});
Loading