From 75a802cfb210f70242978c1f2f215648be76f970 Mon Sep 17 00:00:00 2001 From: Christoph Guttandin Date: Mon, 21 Mar 2022 16:34:23 +0100 Subject: [PATCH] Load only a single AudioWorklet --- .../filter/FeedbackCombFilter.test.ts | 16 ++++++++++++++++ Tone/core/context/BaseContext.ts | 3 +-- Tone/core/context/Context.ts | 18 ++++++------------ Tone/core/context/DummyContext.test.ts | 2 +- Tone/core/context/DummyContext.ts | 2 +- Tone/core/worklet/ToneAudioWorklet.ts | 2 +- Tone/effect/BitCrusher.test.ts | 16 ++++++++++++++++ 7 files changed, 42 insertions(+), 17 deletions(-) diff --git a/Tone/component/filter/FeedbackCombFilter.test.ts b/Tone/component/filter/FeedbackCombFilter.test.ts index 7cb73993..1dd94d70 100644 --- a/Tone/component/filter/FeedbackCombFilter.test.ts +++ b/Tone/component/filter/FeedbackCombFilter.test.ts @@ -1,5 +1,6 @@ import { expect } from "chai"; import { FeedbackCombFilter } from "./FeedbackCombFilter"; +import { BitCrusher } from "Tone/effect/BitCrusher"; import { BasicTests } from "test/helper/Basic"; import { PassAudio } from "test/helper/PassAudio"; import { Offline } from "test/helper/Offline"; @@ -76,5 +77,20 @@ describe("FeedbackCombFilter", () => { }); }); }); + + it("should be usable with the BitCrusher", (done) => { + new FeedbackCombFilter(); + new BitCrusher(4); + + const handle = setTimeout(() => { + window.onunhandledrejection = null; + done(); + }, 100); + + window.onunhandledrejection = (event) => { + done(event.reason); + clearTimeout(handle); + }; + }); }); diff --git a/Tone/core/context/BaseContext.ts b/Tone/core/context/BaseContext.ts index d5625d0e..ba831424 100644 --- a/Tone/core/context/BaseContext.ts +++ b/Tone/core/context/BaseContext.ts @@ -105,8 +105,7 @@ export abstract class BaseContext abstract get rawContext(): AnyAudioContext; abstract addAudioWorkletModule( - _url: string, - _name: string + _url: string ): Promise; abstract lookAhead: number; diff --git a/Tone/core/context/Context.ts b/Tone/core/context/Context.ts index 0f417a8b..5cd0308d 100644 --- a/Tone/core/context/Context.ts +++ b/Tone/core/context/Context.ts @@ -340,7 +340,7 @@ export class Context extends BaseContext { /** * Maps a module name to promise of the addModule method */ - private _workletModules: Map> = new Map(); + private _workletPromise: null | Promise = null; /** * Create an audio worklet node from a name and options. The module @@ -356,29 +356,23 @@ export class Context extends BaseContext { /** * Add an AudioWorkletProcessor module * @param url The url of the module - * @param name The name of the module */ - async addAudioWorkletModule(url: string, name: string): Promise { + async addAudioWorkletModule(url: string): Promise { assert( isDefined(this.rawContext.audioWorklet), "AudioWorkletNode is only available in a secure context (https or localhost)" ); - if (!this._workletModules.has(name)) { - this._workletModules.set( - name, - this.rawContext.audioWorklet.addModule(url) - ); + if (!this._workletPromise) { + this._workletPromise = this.rawContext.audioWorklet.addModule(url); } - await this._workletModules.get(name); + await this._workletPromise; } /** * Returns a promise which resolves when all of the worklets have been loaded on this context */ protected async workletsAreReady(): Promise { - const promises: Promise[] = []; - this._workletModules.forEach((promise) => promises.push(promise)); - await Promise.all(promises); + await this._workletPromise ? this._workletPromise : Promise.resolve(); } //--------------------------- diff --git a/Tone/core/context/DummyContext.test.ts b/Tone/core/context/DummyContext.test.ts index a570d828..75dc8e3c 100644 --- a/Tone/core/context/DummyContext.test.ts +++ b/Tone/core/context/DummyContext.test.ts @@ -25,7 +25,7 @@ describe("DummyContext", () => { context.decodeAudioData(new Float32Array(100)); context.createAudioWorkletNode("test.js"); context.rawContext; - context.addAudioWorkletModule("test.js", "test"); + context.addAudioWorkletModule("test.js"); context.resume(); context.setTimeout(() => {}, 1); context.clearTimeout(1); diff --git a/Tone/core/context/DummyContext.ts b/Tone/core/context/DummyContext.ts index 9bf643e2..3af54180 100644 --- a/Tone/core/context/DummyContext.ts +++ b/Tone/core/context/DummyContext.ts @@ -127,7 +127,7 @@ export class DummyContext extends BaseContext { return {} as AnyAudioContext; } - async addAudioWorkletModule(_url: string, _name: string): Promise { + async addAudioWorkletModule(_url: string): Promise { return Promise.resolve(); } diff --git a/Tone/core/worklet/ToneAudioWorklet.ts b/Tone/core/worklet/ToneAudioWorklet.ts index 94a19bcc..8e68f4ac 100644 --- a/Tone/core/worklet/ToneAudioWorklet.ts +++ b/Tone/core/worklet/ToneAudioWorklet.ts @@ -53,7 +53,7 @@ export abstract class ToneAudioWorklet this._dummyParam = this._dummyGain.gain; // Register the processor - this.context.addAudioWorkletModule(blobUrl, name).then(() => { + this.context.addAudioWorkletModule(blobUrl).then(() => { // create the worklet when it's read if (!this.disposed) { this._worklet = this.context.createAudioWorkletNode(name, this.workletOptions); diff --git a/Tone/effect/BitCrusher.test.ts b/Tone/effect/BitCrusher.test.ts index 29789eae..6e68b505 100644 --- a/Tone/effect/BitCrusher.test.ts +++ b/Tone/effect/BitCrusher.test.ts @@ -1,4 +1,5 @@ import { BitCrusher } from "./BitCrusher"; +import { FeedbackCombFilter } from "Tone/component/filter/FeedbackCombFilter"; import { Oscillator } from "Tone/source/oscillator/Oscillator"; import { BasicTests } from "test/helper/Basic"; import { EffectTests } from "test/helper/EffectTests"; @@ -38,5 +39,20 @@ describe("BitCrusher", () => { crusher.dispose(); }); }); + + it("should be usable with the FeedbackCombFilter", (done) => { + new BitCrusher(4); + new FeedbackCombFilter(); + + const handle = setTimeout(() => { + window.onunhandledrejection = null; + done(); + }, 100); + + window.onunhandledrejection = (event) => { + done(event.reason); + clearTimeout(handle); + }; + }); });