Skip to content

Commit

Permalink
feat: adding AudioWorkletNode constructors to Context
Browse files Browse the repository at this point in the history
  • Loading branch information
tambien committed Sep 27, 2019
1 parent 3b071ed commit f7bdd75
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Tone/core/context/AudioContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {
IAudioWorkletNodeOptions,
AudioContext as stdAudioContext,
AudioWorkletNode as stdAudioWorkletNode,
OfflineAudioContext as stdOfflineAudioContext,
} from "standardized-audio-context";
import { assert } from "../util/Debug";
import { isDefined } from "../util/TypeCheck";

/**
* Create a new AudioContext
Expand Down Expand Up @@ -75,3 +79,9 @@ export function setAudioContext(context: AnyAudioContext): void {
theWindow.TONE_AUDIO_CONTEXT = globalContext;
}
}

export function createAudioWorkletNode(context: AnyAudioContext, name: string, options?: Partial<IAudioWorkletNodeOptions>): AudioWorkletNode {
assert(isDefined(stdAudioWorkletNode), "This node only works in a secure context (https or localhost)");
// @ts-ignore
return new stdAudioWorkletNode(context, name, options);
}
48 changes: 45 additions & 3 deletions Tone/core/context/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Seconds } from "../type/Units";
import { isAudioContext } from "../util/AdvancedTypeCheck";
import { optionsFromArguments } from "../util/Defaults";
import { Emitter } from "../util/Emitter";
import { Omit } from "../util/Interface";
import { noOp, Omit } from "../util/Interface";
import { Timeline } from "../util/Timeline";
import { isString } from "../util/TypeCheck";
import { AnyAudioContext, getAudioContext } from "./AudioContext";
import { isDefined, isString } from "../util/TypeCheck";
import { AnyAudioContext, createAudioWorkletNode, getAudioContext } from "./AudioContext";
import { closeContext, initializeContext } from "./ContextInitialization";

type Transport = import("../clock/Transport").Transport;
Expand Down Expand Up @@ -257,6 +257,48 @@ export class Context extends Emitter<"statechange" | "tick"> implements BaseAudi
this._destination = d;
}

//--------------------------------------------
// AUDIO WORKLET
//--------------------------------------------

/**
* Maps a module name to promise of the addModule method
*/
private _workletModules: Map<string, Promise<void>> = new Map()

/**
* Create an audio worklet node from a name and options. The module
* must first be loaded using [[addAudioWorkletModule]].
*/
createAudioWorkletNode(
name: string,
options?: Partial<AudioWorkletNodeOptions>
): AudioWorkletNode {
return createAudioWorkletNode(this.rawContext, name, options);
}

/**
* 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<void> {
this.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));
}
await this._workletModules.get(name);
}

/**
* Returns a promise which resolves when all of the worklets have been loaded on this context
*/
protected async workletsAreReady(): Promise<void> {
const promises: Promise<void>[] = [];
this._workletModules.forEach(promise => promises.push(promise));
await Promise.all(promises);
}

//---------------------------
// TICKER
//---------------------------
Expand Down
3 changes: 2 additions & 1 deletion Tone/core/context/OfflineContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ export class OfflineContext extends Context {
/**
* Render the output of the OfflineContext
*/
render(): Promise<AudioBuffer> {
async render(): Promise<AudioBuffer> {
await this.workletsAreReady();
this._renderClock();
return this._context.startRendering();
}
Expand Down

0 comments on commit f7bdd75

Please sign in to comment.