From 1668decf7019c3bb549ada18b9da9f155f84c98f Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Mon, 7 Oct 2019 15:37:07 -0400 Subject: [PATCH] feat: Convolver is just a wrapper around the ConvolverNode, no longer an effect a more basic wrapper around the ConvolverNode which unlike the Convolver effect, does not have a dry/wet knob --- .../filter}/Convolver.test.ts | 6 --- .../{effect => component/filter}/Convolver.ts | 37 +++++++++++-------- Tone/component/index.ts | 1 + Tone/effect/index.ts | 1 - 4 files changed, 23 insertions(+), 22 deletions(-) rename Tone/{effect => component/filter}/Convolver.test.ts (88%) rename Tone/{effect => component/filter}/Convolver.ts (75%) diff --git a/Tone/effect/Convolver.test.ts b/Tone/component/filter/Convolver.test.ts similarity index 88% rename from Tone/effect/Convolver.test.ts rename to Tone/component/filter/Convolver.test.ts index 4c1a9ea7..f0d4594f 100644 --- a/Tone/effect/Convolver.test.ts +++ b/Tone/component/filter/Convolver.test.ts @@ -1,6 +1,5 @@ import { expect } from "chai"; import { BasicTests } from "test/helper/Basic"; -// import { EffectTests } from "test/helper/EffectTests"; import { ToneAudioBuffer } from "Tone/core/context/ToneAudioBuffer"; import { Convolver } from "./Convolver"; @@ -21,11 +20,6 @@ describe("Convolver", () => { return ir.load(testFile); }); - // the buffers are set to 44.1 Khz, but i always get this error: - // Error: Failed to set the 'buffer' property on 'ConvolverNode': - // The buffer sample rate of 48000 does not match the context rate of 44100 Hz. - // EffectTests(Convolver, ir); - context("API", () => { it("can pass in options in the constructor", () => { diff --git a/Tone/effect/Convolver.ts b/Tone/component/filter/Convolver.ts similarity index 75% rename from Tone/effect/Convolver.ts rename to Tone/component/filter/Convolver.ts index f865e3b7..ed5f0958 100644 --- a/Tone/effect/Convolver.ts +++ b/Tone/component/filter/Convolver.ts @@ -1,9 +1,10 @@ -import { ToneAudioBuffer } from "../core/context/ToneAudioBuffer"; -import { optionsFromArguments } from "../core/util/Defaults"; -import { noOp } from "../core/util/Interface"; -import { Effect, EffectOptions } from "./Effect"; +import { ToneAudioNode, ToneAudioNodeOptions } from "../../core/context/ToneAudioNode"; +import { ToneAudioBuffer } from "../../core/context/ToneAudioBuffer"; +import { optionsFromArguments } from "../../core/util/Defaults"; +import { Gain } from "../../core/context/Gain"; +import { noOp } from "../../core/util/Interface"; -interface ToneConvolverOptions extends EffectOptions { +export interface ConvolverOptions extends ToneAudioNodeOptions { onload: () => void; normalize: boolean; url?: string | AudioBuffer | ToneAudioBuffer; @@ -18,12 +19,12 @@ interface ToneConvolverOptions extends EffectOptions { * @example * //initializing the convolver with an impulse response * var convolver = new Convolver("./path/to/ir.wav").toDestination(); - * @category Effect + * @category Component */ -export class Convolver extends Effect { +export class Convolver extends ToneAudioNode { readonly name: string = "Convolver"; - + /** * The native ConvolverNode */ @@ -34,12 +35,15 @@ export class Convolver extends Effect { */ private _buffer: ToneAudioBuffer; + readonly input: Gain; + readonly output: Gain; + /** * @param url The URL of the impulse response or the ToneAudioBuffer containing the impulse response. * @param onload The callback to invoke when the url is loaded. */ constructor(url?: string | AudioBuffer | ToneAudioBuffer, onload?: () => void); - constructor(options?: Partial); + constructor(options?: Partial); constructor() { super(optionsFromArguments(Convolver.getDefaults(), arguments, ["url", "onload"])); @@ -50,7 +54,10 @@ export class Convolver extends Effect { options.onload(); }); - // set if it's already loaded + this.input = new Gain({ context: this.context }); + this.output = new Gain({ context: this.context }); + + // set if it's already loaded, set it immediately if (this._buffer.loaded) { this.buffer = this._buffer; } @@ -59,11 +66,11 @@ export class Convolver extends Effect { this.normalize = options.normalize; // connect it up - this.connectEffect(this._convolver); + this.input.chain(this._convolver, this.output); } - static getDefaults(): ToneConvolverOptions { - return Object.assign(Effect.getDefaults(), { + static getDefaults(): ConvolverOptions { + return Object.assign(ToneAudioNode.getDefaults(), { normalize: true, onload: noOp, }); @@ -96,11 +103,11 @@ export class Convolver extends Effect { // if it's already got a buffer, create a new one if (this._convolver.buffer) { // disconnect the old one - this.effectSend.disconnect(); + this.input.disconnect(); this._convolver.disconnect(); // create and connect a new one this._convolver = this.context.createConvolver(); - this.connectEffect(this._convolver); + this.input.connect(this._convolver); } const buff = this._buffer.get(); this._convolver.buffer = buff ? buff : null; diff --git a/Tone/component/index.ts b/Tone/component/index.ts index a566d70b..17fa5790 100644 --- a/Tone/component/index.ts +++ b/Tone/component/index.ts @@ -11,3 +11,4 @@ export * from "./dynamics/Compressor"; export * from "./filter/OnePoleFilter"; export * from "./filter/FeedbackCombFilter"; export * from "./filter/LowpassCombFilter"; +export * from "./filter/Convolver"; diff --git a/Tone/effect/index.ts b/Tone/effect/index.ts index 30083e71..9913ca25 100644 --- a/Tone/effect/index.ts +++ b/Tone/effect/index.ts @@ -1,3 +1,2 @@ export { FeedbackDelay } from "./FeedbackDelay"; -export { Convolver } from "./Convolver"; export { Reverb } from "./Reverb";