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

Fix: Implemented STOP button behavior to toggle audio playback #353

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Prev Previous commit
fixed the requested changes. Modified the main.js of hello-audio-work…
…let, one-pole-filter, volume-meter, noise-generator
  • Loading branch information
tarunsinghofficial committed May 26, 2024
commit 00ddbef43e8e5302eec2420bd43a2f7ddfd224a5
2 changes: 1 addition & 1 deletion src/_data/build_info.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"3.1.0","revision":"894b234","lastUpdated":"2024-03-14","copyrightYear":2024}
{"version":"3.1.0","revision":"2df8734","lastUpdated":"2024-03-26","copyrightYear":2024}
9 changes: 7 additions & 2 deletions src/audio-worklet/basic/hello-audio-worklet/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ window.addEventListener('load', async () => {
buttonEl.addEventListener('click', async () => {
if (!oscillatorNode) {
await startAudio(audioContext);
audioContext.resume();
await audioContext.resume();
isPlaying = true;
buttonEl.textContent = 'Playing...';
buttonEl.classList.remove('start-button');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L26-L29 are exactly same with L31-L34. Perhaps this block can be simplified further?

} else if (audioContext.state === 'suspended') {
await audioContext.resume();
isPlaying = true;
buttonEl.textContent = 'Playing...';
buttonEl.classList.remove('start-button');
} else {
audioContext.suspend();
await audioContext.suspend();
isPlaying = false;
buttonEl.textContent = 'START';
buttonEl.classList.add('start-button');
Expand Down
10 changes: 8 additions & 2 deletions src/audio-worklet/basic/noise-generator/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
const audioContext = new AudioContext();
let isPlaying = false;
let noiseGenerator;
let moduleAdded = false;

const startAudio = async (context) => {
await context.audioWorklet.addModule('noise-generator.js');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this empty line.

if(!moduleAdded) {
await context.audioWorklet.addModule('noise-generator.js');
moduleAdded = true;
}

const modulator = new OscillatorNode(context);
const modGain = new GainNode(context);
noiseGenerator = new AudioWorkletNode(context, 'noise-generator');
Expand Down Expand Up @@ -39,7 +45,7 @@ window.addEventListener('load', async () => {
if (!isPlaying) {
// If not playing, start the audio.
await startAudio(audioContext);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will call L10 (addModule) multiple times depending on isPlaying. Have you tested locally? Loading the same script twice should throw an exception.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code to ensure that the module is added only once by using a flag (moduleAdded) to track whether the module has already been loaded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this example is pretty much the same with the other oscillator example. Why do we need a different flag (moduleAdded) only for this example?

audioContext.resume();
await audioContext.resume();
isPlaying = true;
buttonEl.textContent = 'Playing...';
} else { // If playing, stop audio
Expand Down
10 changes: 8 additions & 2 deletions src/audio-worklet/basic/one-pole-filter/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
const audioContext = new AudioContext();
let oscillator;
let isPlaying = false;
let moduleAdded = false;

const startAudio = async (context) => {
await context.audioWorklet.addModule('one-pole-processor.js');

if(!moduleAdded) {
await context.audioWorklet.addModule('one-pole-processor.js');
moduleAdded = true;
}

oscillator = new OscillatorNode(context);
const filter = new AudioWorkletNode(context, 'one-pole-processor');
const frequencyParam = filter.parameters.get('frequency');
Expand Down Expand Up @@ -38,7 +44,7 @@ window.addEventListener('load', async () => {
buttonEl.addEventListener('click', async () => {
if (!isPlaying) {
await startAudio(audioContext);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the flag check here same as above.

audioContext.resume();
await audioContext.resume();
isPlaying = true;
buttonEl.textContent = 'Playing...';
buttonEl.classList.remove('start-button');
Expand Down
10 changes: 8 additions & 2 deletions src/audio-worklet/basic/volume-meter/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
const audioContext = new AudioContext();
let mediaStream;
let volumeMeterNode;
let moduleAdded = false;

const startAudio = async (context, meterElement) => {
await context.audioWorklet.addModule('volume-meter-processor.js');

if (!moduleAdded) {
await context.audioWorklet.addModule('volume-meter-processor.js');
moduleAdded = true;
}

mediaStream = await navigator.mediaDevices.getUserMedia({ audio: true });
const micNode = context.createMediaStreamSource(mediaStream);
volumeMeterNode = new AudioWorkletNode(context, 'volume-meter');
Expand Down Expand Up @@ -39,7 +45,7 @@ window.addEventListener('load', async () => {
buttonEl.addEventListener('click', async () => {
if (!mediaStream) { // If audio is not playing, start audio
await startAudio(audioContext, meterEl);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the requested changes here:

#362

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the flag check here too.

audioContext.resume();
await audioContext.resume();
buttonEl.textContent = 'STOP';
} else { // If audio is playing, stop audio
stopAudio();
Expand Down