Skip to content

Commit

Permalink
feat: Convolver is just a wrapper around the ConvolverNode, no longer…
Browse files Browse the repository at this point in the history
… an effect

a more basic wrapper around the ConvolverNode which unlike the Convolver effect, does not have a dry/wet knob
  • Loading branch information
tambien committed Oct 7, 2019
1 parent 34f731b commit 1668dec
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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", () => {
Expand Down
37 changes: 22 additions & 15 deletions Tone/effect/Convolver.ts → Tone/component/filter/Convolver.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<ToneConvolverOptions> {
export class Convolver extends ToneAudioNode<ConvolverOptions> {

readonly name: string = "Convolver";

/**
* The native ConvolverNode
*/
Expand All @@ -34,12 +35,15 @@ export class Convolver extends Effect<ToneConvolverOptions> {
*/
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<ToneConvolverOptions>);
constructor(options?: Partial<ConvolverOptions>);
constructor() {

super(optionsFromArguments(Convolver.getDefaults(), arguments, ["url", "onload"]));
Expand All @@ -50,7 +54,10 @@ export class Convolver extends Effect<ToneConvolverOptions> {
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;
}
Expand All @@ -59,11 +66,11 @@ export class Convolver extends Effect<ToneConvolverOptions> {
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,
});
Expand Down Expand Up @@ -96,11 +103,11 @@ export class Convolver extends Effect<ToneConvolverOptions> {
// 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;
Expand Down
1 change: 1 addition & 0 deletions Tone/component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from "./dynamics/Compressor";
export * from "./filter/OnePoleFilter";
export * from "./filter/FeedbackCombFilter";
export * from "./filter/LowpassCombFilter";
export * from "./filter/Convolver";
1 change: 0 additions & 1 deletion Tone/effect/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { FeedbackDelay } from "./FeedbackDelay";
export { Convolver } from "./Convolver";
export { Reverb } from "./Reverb";

0 comments on commit 1668dec

Please sign in to comment.