Skip to content

Commit

Permalink
feat(dsp): add ICopy impls for various IGen's
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 26, 2023
1 parent 5c21b00 commit 7894cd4
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 22 deletions.
8 changes: 6 additions & 2 deletions packages/dsp/src/add.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IReset } from "@thi.ng/api";
import type { ICopy, IReset } from "@thi.ng/api";
import { AGen } from "./agen.js";

/**
Expand All @@ -14,7 +14,7 @@ import { AGen } from "./agen.js";
export const add = (step?: number, start?: number, clamp?: number) =>
new Add(step, start, clamp);

export class Add extends AGen<number> implements IReset {
export class Add extends AGen<number> implements ICopy<Add>, IReset {
constructor(
protected _step = 1,
protected _start = 0,
Expand All @@ -24,6 +24,10 @@ export class Add extends AGen<number> implements IReset {
this.reset();
}

copy(): Add {
return new Add(this._step, this._start, this._clamp);
}

reset() {
this._val = this._start - this._step;
return this;
Expand Down
17 changes: 15 additions & 2 deletions packages/dsp/src/adsr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IReset } from "@thi.ng/api";
import type { ICopy, IReset } from "@thi.ng/api";
import { clamp01 } from "@thi.ng/math/interval";
import { add } from "./add.js";
import { AGen } from "./agen.js";
Expand Down Expand Up @@ -78,7 +78,7 @@ export interface ADSROpts {
*/
export const adsr = (opts?: Partial<ADSROpts>) => new ADSR(opts);

export class ADSR extends AGen<number> implements IReset {
export class ADSR extends AGen<number> implements ICopy<ADSR>, IReset {
protected _phase!: EnvPhase;
protected _curve!: IGen<number>;
protected _atime!: number;
Expand Down Expand Up @@ -113,6 +113,19 @@ export class ADSR extends AGen<number> implements IReset {
this.reset();
}

copy() {
return new ADSR({
a: this._atime,
d: this._dtime,
s: this._sustain,
r: this._rtime,
acurve: this._acurve,
dcurve: this._dcurve,
gain: this._gain,
slen: this._speriod,
});
}

reset() {
this._phase = EnvPhase.ATTACK;
this._curve = curve(0, 1, this._atime + 1, this._acurve, true);
Expand Down
8 changes: 6 additions & 2 deletions packages/dsp/src/alt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IReset } from "@thi.ng/api";
import type { ICopy, IReset } from "@thi.ng/api";
import { AGen } from "./agen.js";

export const alt = (n = 1) => new Alt(n, -n);
Expand All @@ -7,13 +7,17 @@ export const altT = <T>(a: T, b: T) => new Alt(a, b);

export const altB = (x = true) => new Alt(x, !x);

export class Alt<T> extends AGen<T> implements IReset {
export class Alt<T> extends AGen<T> implements ICopy<Alt<T>>, IReset {
protected _flip = true;

constructor(protected _a: T, protected _b: T) {
super(_b);
}

copy() {
return new Alt(this._a, this._b);
}

reset() {
this._flip = true;
return this;
Expand Down
7 changes: 6 additions & 1 deletion packages/dsp/src/const.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ICopy } from "@thi.ng/api";
import { AGen } from "./agen.js";

/**
Expand All @@ -7,7 +8,11 @@ import { AGen } from "./agen.js";
*/
export const constant = <T>(x: T) => new Const(x);

export class Const<T> extends AGen<T> {
export class Const<T> extends AGen<T> implements ICopy<Const<T>> {
copy() {
return new Const(this._val);
}

next() {
return this._val;
}
Expand Down
11 changes: 9 additions & 2 deletions packages/dsp/src/delay.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Fn0, IClear, ILength, IReset } from "@thi.ng/api";
import type { Fn0, IClear, ICopy, ILength, IReset } from "@thi.ng/api";
import { isFunction } from "@thi.ng/checks/is-function";
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
import { wrap } from "@thi.ng/math/interval";
Expand All @@ -22,7 +22,10 @@ export const delayT = <T>(n: number, off: T | Fn0<T>) => new Delay<T>(n, off);
* Ring buffer / delay line for arbitrary values w/ support for tapping
* at any delay time (within configured buffer size).
*/
export class Delay<T> extends AProc<T, T> implements IClear, ILength, IReset {
export class Delay<T>
extends AProc<T, T>
implements IClear, ICopy<Delay<T>>, ILength, IReset
{
protected _buf: T[];
protected _rpos: number;
protected _wpos: number;
Expand Down Expand Up @@ -61,6 +64,10 @@ export class Delay<T> extends AProc<T, T> implements IClear, ILength, IReset {
}
}

copy() {
return new Delay<T>(this._buf.length, this._empty);
}

/**
* Alias for {@link Delay.clear}
*/
Expand Down
10 changes: 9 additions & 1 deletion packages/dsp/src/feedback-delay.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { clamp01 } from "@thi.ng/math/interval";
import { Delay } from "./delay.js";
import type { ICopy } from "@thi.ng/api";

/**
* Extension of {@link Delay} which adds sum delayed value multiplied
Expand All @@ -14,12 +15,19 @@ import { Delay } from "./delay.js";
export const feedbackDelay = (n: number, feedback?: number) =>
new FeedbackDelay(n, feedback);

export class FeedbackDelay extends Delay<number> {
export class FeedbackDelay
extends Delay<number>
implements ICopy<FeedbackDelay>
{
constructor(n: number, protected _feedback = 0.5) {
super(n, 0);
this.setFeedback(_feedback);
}

copy() {
return new FeedbackDelay(this._buf.length, this._feedback);
}

next(x: number) {
return super.next(x + this._buf[this._rpos] * this._feedback);
}
Expand Down
16 changes: 14 additions & 2 deletions packages/dsp/src/impulse-train.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IReset } from "@thi.ng/api";
import type { ICopy, IReset } from "@thi.ng/api";
import { AGen } from "./agen.js";

/**
Expand All @@ -20,7 +20,10 @@ export const impulseTrainT = <T>(
export const impulseTrainB = (period: number, start?: number) =>
new ImpulseTrain(true, false, period, start);

export class ImpulseTrain<T> extends AGen<T> implements IReset {
export class ImpulseTrain<T>
extends AGen<T>
implements ICopy<ImpulseTrain<T>>, IReset
{
protected _startpos: number;

constructor(
Expand All @@ -33,6 +36,15 @@ export class ImpulseTrain<T> extends AGen<T> implements IReset {
this._startpos = --this._pos;
}

copy() {
return new ImpulseTrain(
this._on,
this._off,
this._period,
this._startpos + 1
);
}

reset() {
this._val = this._off;
this._pos = this._startpos;
Expand Down
8 changes: 6 additions & 2 deletions packages/dsp/src/impulse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IReset } from "@thi.ng/api";
import type { ICopy, IReset } from "@thi.ng/api";
import { AGen } from "./agen.js";

/**
Expand Down Expand Up @@ -26,11 +26,15 @@ export const impulseT = <T>(on: T, off: T) => new Impulse<T>(on, off);
*/
export const impulseB = (start = true) => new Impulse(start, !start);

export class Impulse<T> extends AGen<T> implements IReset {
export class Impulse<T> extends AGen<T> implements ICopy<Impulse<T>>, IReset {
constructor(protected _on: T, protected _off: T) {
super(_on);
}

copy() {
return new Impulse(this._on, this._off);
}

reset() {
this._val = this._on;
return this;
Expand Down
11 changes: 9 additions & 2 deletions packages/dsp/src/integrator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IReset } from "@thi.ng/api";
import type { ICopy, IReset } from "@thi.ng/api";
import { AProc } from "./aproc.js";

/**
Expand All @@ -11,11 +11,18 @@ import { AProc } from "./aproc.js";
export const integrator = (coeff?: number, start?: number) =>
new Integrator(coeff, start);

export class Integrator extends AProc<number, number> implements IReset {
export class Integrator
extends AProc<number, number>
implements ICopy<Integrator>, IReset
{
constructor(protected _coeff = 1, protected _start = 0) {
super(_start);
}

copy() {
return new Integrator(this._coeff, this._start);
}

reset() {
this._val = this._start;
return this;
Expand Down
8 changes: 6 additions & 2 deletions packages/dsp/src/madd.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IReset } from "@thi.ng/api";
import type { ICopy, IReset } from "@thi.ng/api";
import { AGen } from "./agen.js";

/**
Expand All @@ -18,7 +18,7 @@ export const madd = (
clamp?: number
) => new MAdd(factor, start, offset, clamp);

export class MAdd extends AGen<number> implements IReset {
export class MAdd extends AGen<number> implements ICopy<MAdd>, IReset {
constructor(
protected _factor = 1,
protected _start = 1,
Expand All @@ -29,6 +29,10 @@ export class MAdd extends AGen<number> implements IReset {
this.reset();
}

copy() {
return new MAdd(this._factor, this._start, this._offset, this._clamp);
}

reset() {
this._val = (this._start - this._offset) / this._factor;
return this;
Expand Down
8 changes: 6 additions & 2 deletions packages/dsp/src/mul.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IReset } from "@thi.ng/api";
import type { ICopy, IReset } from "@thi.ng/api";
import { AGen } from "./agen.js";

/**
Expand All @@ -14,7 +14,7 @@ import { AGen } from "./agen.js";
export const mul = (factor?: number, start?: number, clamp?: number) =>
new Mul(factor, start, clamp);

export class Mul extends AGen<number> implements IReset {
export class Mul extends AGen<number> implements ICopy<Mul>, IReset {
constructor(
protected _factor = 1,
protected _start = 1,
Expand All @@ -24,6 +24,10 @@ export class Mul extends AGen<number> implements IReset {
this.reset();
}

copy() {
return new Mul(this._factor, this._start, this._clamp);
}

reset() {
this._val = this._start / this._factor;
return this;
Expand Down
8 changes: 6 additions & 2 deletions packages/dsp/src/sincos.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IReset } from "@thi.ng/api";
import type { ICopy, IReset } from "@thi.ng/api";
import { TAU } from "@thi.ng/math/api";
import { AGen } from "./agen.js";

Expand All @@ -19,7 +19,7 @@ import { AGen } from "./agen.js";
* @param freq - normalized freq
* @param amp - amplitude (default: 1)
*/
export class SinCos extends AGen<number[]> implements IReset {
export class SinCos extends AGen<number[]> implements ICopy<SinCos>, IReset {
protected _f!: number;
protected _s!: number;
protected _c!: number;
Expand All @@ -29,6 +29,10 @@ export class SinCos extends AGen<number[]> implements IReset {
this.calcCoeffs();
}

copy() {
return new SinCos(this._freq, this._amp);
}

reset() {
this.calcCoeffs();
return this;
Expand Down

0 comments on commit 7894cd4

Please sign in to comment.