mirror of
https://github.com/bendtherules/multimeter-connect-web.git
synced 2026-08-18 13:52:26 +00:00
buzzSound.ts - Defer AudioContext creation
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
// The browser will limit the number of concurrent audio contexts
|
// The browser will limit the number of concurrent audio contexts
|
||||||
// So be sure to re-use them whenever you can
|
// So be sure to re-use them whenever you can
|
||||||
const myAudioContext = new AudioContext();
|
let myAudioContext: AudioContext | undefined = undefined;
|
||||||
let lastOscillatorNode: OscillatorNode | undefined = undefined;
|
let lastOscillatorNode: OscillatorNode | undefined = undefined;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,6 +18,9 @@ export function startBuzz({
|
|||||||
volume = 70,
|
volume = 70,
|
||||||
onEnd = () => {},
|
onEnd = () => {},
|
||||||
} = {}) {
|
} = {}) {
|
||||||
|
if (myAudioContext === undefined) {
|
||||||
|
myAudioContext = new AudioContext();
|
||||||
|
}
|
||||||
if (lastOscillatorNode !== undefined) {
|
if (lastOscillatorNode !== undefined) {
|
||||||
lastOscillatorNode.stop(myAudioContext.currentTime + duration * 0.001);
|
lastOscillatorNode.stop(myAudioContext.currentTime + duration * 0.001);
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user