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
Next Next commit
Include changes made to hello-audio-worklet and audio-worklet-node-op…
…tions files
  • Loading branch information
tarunsinghofficial committed Mar 14, 2024
commit 894b23411cc8275e768c301b9ba750347ddaf81a
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":"0643084","lastUpdated":"2024-03-07","copyrightYear":2024}
{"version":"3.1.0","revision":"bf3ea58","lastUpdated":"2024-03-08","copyrightYear":2024}
31 changes: 13 additions & 18 deletions src/audio-worklet/basic/audio-worklet-node-options/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@ const audioContext = new AudioContext();
let oscillatorProcessor;

const startAudio = async (context, options) => {
await context.audioWorklet.addModule('oscillator-processor.js');
oscillatorProcessor =
new AudioWorkletNode(context, 'oscillator-processor', {
processorOptions: {
waveformType: options.waveformType,
frequency: options.frequency,
}
});
oscillatorProcessor.connect(context.destination);
};

const stopAudio = () => {
if (oscillatorProcessor) {
oscillatorProcessor.disconnect();
oscillatorProcessor = null;
if (!oscillatorProcessor) {
await context.audioWorklet.addModule('oscillator-processor.js');
Copy link
Member

Choose a reason for hiding this comment

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

This can't be called multiple times in any scenario. I think you'll get an error if you do this. Have you checked?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, it can't be called multiple times, so I have modified it. No, I didn't get any error though!

oscillatorProcessor =
new AudioWorkletNode(context, 'oscillator-processor', {
processorOptions: {
waveformType: options.waveformType,
frequency: options.frequency,
}
});
oscillatorProcessor.connect(context.destination);
}
};

Expand All @@ -32,14 +27,14 @@ window.addEventListener('load', async () => {

buttonEl.addEventListener('click', async () => {
if (!oscillatorProcessor) { // If audio is not playing, start audio
Copy link
Member

Choose a reason for hiding this comment

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

nit: Let's move the comment below the if().

Copy link
Author

Choose a reason for hiding this comment

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

Done.

const waveformType =
document.querySelector('#demo-select-waveform-type').value;
const waveformType = document.querySelector('#demo-select-waveform-type').value;
const frequency = document.querySelector('#demo-input-frequency').value;
await startAudio(audioContext, { waveformType, frequency });
hoch marked this conversation as resolved.
Show resolved Hide resolved
audioContext.resume();
buttonEl.textContent = 'STOP';
} else { // If audio is playing, stop audio
Copy link
Member

Choose a reason for hiding this comment

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

nit: Ditto on comments.

Copy link
Author

Choose a reason for hiding this comment

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

done

stopAudio();
oscillatorProcessor.disconnect();
oscillatorProcessor = null;
buttonEl.textContent = 'START';
}
}, false);
Expand Down
19 changes: 10 additions & 9 deletions src/audio-worklet/basic/hello-audio-worklet/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@
// found in the LICENSE file.

const audioContext = new AudioContext();
let oscillator;
let oscillatorNode;
Copy link
Author

Choose a reason for hiding this comment

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

Changed the name to oscillatorNode

let isPlaying = false;

const startAudio = async (context) => {
await context.audioWorklet.addModule('bypass-processor.js');
oscillator = new OscillatorNode(context);
const bypasser = new AudioWorkletNode(context, 'bypass-processor');
oscillator.connect(bypasser).connect(context.destination);
oscillator.start();
if (!oscillatorNode) {
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();
}
};

const stopAudio = () => {
Copy link
Member

Choose a reason for hiding this comment

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

We should leave the rest and simply suspend the context.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion. I've modified the stopAudio() function to suspend the audio context instead of stopping and disconnecting the oscillator node. Please review the changes and let me know if they meet the requirements.

Copy link
Member

Choose a reason for hiding this comment

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

This also has the same problem with src/audio-worklet/basic/audio-worklet-node-options/main.js above. See my comments for the explanation.

if (oscillator) {
oscillator.stop();
oscillator.disconnect();
if (audioContext.state === 'running') {
audioContext.suspend();
}
};

Expand Down