Skip to content

Commit

Permalink
feat(dsp): update all gens/procs, housekeeping, docs
Browse files Browse the repository at this point in the history
- add param accessors for all ops
- add IReset & impls for most gens/procs
- add Delay.multiTap()
- replace exp() w/ curve(), add curvature ctrl
- remove lfo(), wrapAround, ATwoPole/AllPass2
  • Loading branch information
postspectacular committed Jan 21, 2020
1 parent 68a88e4 commit e483245
Show file tree
Hide file tree
Showing 25 changed files with 374 additions and 261 deletions.
10 changes: 9 additions & 1 deletion packages/dsp/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type StatelessOscillator = (
freq: number,
amp?: number,
dc?: number,
opts?: any
...opts: any[]
) => number;

export type ComplexArray = [NumericArray, NumericArray];
Expand All @@ -31,6 +31,10 @@ export interface IProc<A, B> extends IDeref<B> {
next(src: A): B;
}

export interface IProc2<A, B, C> extends IDeref<C> {
next(srcA: A, srcB: B): C;
}

export interface FilterConfig {
zeroes: number[];
poles: number[];
Expand All @@ -49,3 +53,7 @@ export interface IFilter {
*/
filterCoeffs(): FilterConfig;
}

export interface IReset {
reset(): void;
}
9 changes: 7 additions & 2 deletions packages/dsp/src/gen/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@ export const line = (start = 0, end = 1, num = 10) =>
new Add((end - start) / num, start);

export class Add extends AGen<number> {
constructor(protected _step = 1, start = 0) {
super(start - _step);
constructor(protected _step = 1, protected _start = 0) {
super(0);
this.reset();
}

reset() {
this._val = this._start - this._step;
}

next() {
Expand Down
4 changes: 2 additions & 2 deletions packages/dsp/src/gen/alt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ 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> {
protected flip = true;
protected _flip = true;

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

next() {
return (this._val = (this.flip = !this.flip) ? this._b : this._a);
return (this._val = (this._flip = !this._flip) ? this._b : this._a);
}
}
37 changes: 28 additions & 9 deletions packages/dsp/src/gen/cosine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { INV_TAU, TAU } from "@thi.ng/math";
import { TAU } from "@thi.ng/math";
import { IReset } from "../api";
import { AGen } from "./agen";

/**
Expand All @@ -10,13 +11,17 @@ import { AGen } from "./agen";
*/
export const cosine = (freq: number, amp?: number) => new Cosine(freq, amp);

export class Cosine extends AGen<number> {
export class Cosine extends AGen<number> implements IReset {
protected _cos!: number;
protected _nxt!: number;

constructor(freq: number, amp = 1) {
constructor(protected _freq: number, protected _amp = 1) {
super(0);
this.set(freq, amp);
this.calcCoeffs();
}

reset() {
this.calcCoeffs();
}

next() {
Expand All @@ -27,12 +32,26 @@ export class Cosine extends AGen<number> {
}

freq() {
return Math.acos(this._cos * 0.5) * INV_TAU;
return this._freq;
}

setFreq(freq: number) {
this._freq = freq;
this.calcCoeffs();
}

amp() {
return this._amp;
}

setAmp(amp: number) {
this._amp = amp;
this.calcCoeffs();
}

set(freq: number, amp: number) {
this._nxt = amp;
this._cos = Math.cos(freq * TAU) * 2;
this._val = this._cos * amp * 0.5;
protected calcCoeffs() {
this._nxt = this._amp;
this._cos = Math.cos(this._freq * TAU) * 2;
this._val = this._cos * this._amp * 0.5;
}
}
12 changes: 10 additions & 2 deletions packages/dsp/src/gen/impulse-train.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IReset } from "../api";
import { AGen } from "./agen";

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

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

constructor(
protected _on: T,
protected _off: T,
protected _period: number,
protected _pos = 0
) {
super(_off);
this._pos--;
this._startpos = --this._pos;
}

reset() {
this._val = this._off;
this._pos = this._startpos;
}

next() {
Expand Down
7 changes: 6 additions & 1 deletion packages/dsp/src/gen/impulse.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IReset } from "../api";
import { AGen } from "./agen";

/**
Expand Down Expand Up @@ -25,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> {
export class Impulse<T> extends AGen<T> implements IReset {
constructor(protected _on: T, protected _off: T) {
super(_on);
}

reset() {
this._val = this._on;
}

next() {
const x = this._val;
this._val = this._off;
Expand Down
67 changes: 64 additions & 3 deletions packages/dsp/src/gen/madd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IReset } from "../api";
import { AGen } from "./agen";

/**
Expand All @@ -10,9 +11,69 @@ import { AGen } from "./agen";
export const madd = (factor?: number, start?: number, offset?: number) =>
new MAdd(factor, start, offset);

export class MAdd extends AGen<number> {
constructor(protected _factor = 1, start = 1, protected _offset = 0) {
super((start - _offset) / _factor);
/**
* Returns new {@link MAdd} gen, producing an exponential curve (with
* adjustable curvature) between `start` and `end` values over `num`
* steps. This is the exponential equivalent of {@link line}.
*
* @remarks
* The `end` value is only reached after `num + 1` steps. The curve will
* NOT stop at `end` but continue indefinitely if more values are
* requested from the generator.
*
* The curvature can be controlled via the logarithmic `rate` param.
* Recommended range [0.0001 - 10000] (curved -> linear). Default: 0.1
*
* Also see {@link madd}.
*
* @example
* ```ts
* curve(-1, 1, 5, 0.1).take(7)
* // [
* // -1,
* // -0.04228753006664476,
* // 0.4786567612639258,
* // 0.7620225554764573,
* // 0.9161583712747458,
* // 1.0000000000000002, // target
* // 1.0456053557111122
* // ]
* ```
*
* @param start - start value
* @param end - end value
* @param num - num steps
* @param rate - curvature
*/
export const curve = (
start: number,
end: number,
steps: number,
rate = 0.1
) => {
const c = Math.exp(
-Math.log((Math.abs(end - start) + rate) / rate) / steps
);
return new MAdd(
c,
start,
(start < end ? end + rate : end - rate) * (1 - c)
);
};

export class MAdd extends AGen<number> implements IReset {
constructor(
protected _factor = 1,
protected _start = 1,
protected _offset = 0
) {
super(0);
this.reset();
}

reset() {
this._val = (this._start - this._offset) / this._factor;
return this;
}

next() {
Expand Down
47 changes: 8 additions & 39 deletions packages/dsp/src/gen/mul.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expFactor } from "@thi.ng/math";
import { IReset } from "../api";
import { AGen } from "./agen";

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

/**
* Returns new `Mul` gen producing an exponential curve between `start`
* and `end` values over `num` steps. Exponential equivalent of
* {@link line}.
*
* @remarks
* Both `start` and `end` MUST be != 0 and of same sign. If `start` <
* `end`, the curve will ease in, if `end` > `start` it will ease out.
*
* The `end` value is only reached after `num + 1` steps. The curve will
* NOT stop at `end` but continue indefinitely if more values are
* requested from the generator.
*
* Also see {@link mul}.
*
* @example
* ```ts
* exp(1, 0.25, 5).take(7)
* // [
* // 1,
* // 0.757858283255199,
* // 0.5743491774985174,
* // 0.435275281648062,
* // 0.3298769776932235,
* // 0.24999999999999994, // target
* // 0.1894645708137997
* // ]
* ```
*
* @param start -
* @param end -
* @param num -
*/
export const exp = (start: number, end: number, num: number) =>
new Mul(expFactor(start, end, num), start);
export class Mul extends AGen<number> implements IReset {
constructor(protected _factor = 1, protected _start = 1) {
super(0);
this.reset();
}

export class Mul extends AGen<number> {
constructor(protected _factor = 1, start = 1) {
super(start / _factor);
reset() {
this._val = this._start / this._factor;
}

next() {
Expand Down
9 changes: 7 additions & 2 deletions packages/dsp/src/gen/reciprocal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IReset } from "../api";
import { AGen } from "./agen";

/**
Expand All @@ -7,11 +8,15 @@ import { AGen } from "./agen";
*/
export const reciprocal = (step?: number) => new Reciprocal(step);

export class Reciprocal extends AGen<number> {
protected _n: number;
export class Reciprocal extends AGen<number> implements IReset {
protected _n!: number;

constructor(protected _step = 1) {
super(1);
this.reset();
}

reset() {
this._n = 1 - this._step;
}

Expand Down
20 changes: 0 additions & 20 deletions packages/dsp/src/gen/wrap-around.ts

This file was deleted.

11 changes: 8 additions & 3 deletions packages/dsp/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export * from "./api";
export * from "./fft";
export * from "./lfo";

export * from "./gen/add";
export * from "./gen/addg";
export * from "./gen/adsr";
export * from "./gen/agen";
export * from "./gen/alt";
export * from "./gen/comp";
Expand All @@ -14,24 +14,29 @@ export * from "./gen/impulse-train";
export * from "./gen/madd";
export * from "./gen/mul";
export * from "./gen/osc";
export * from "./gen/pink-noise";
export * from "./gen/product";
export * from "./gen/reciprocal";
export * from "./gen/sincos";
export * from "./gen/sum";
export * from "./gen/wrap-around";
export * from "./gen/white-noise";

export * from "./proc/allpass";
export * from "./proc/aproc";
export * from "./proc/atwopole";
export * from "./proc/biquad";
export * from "./proc/dcblocker";
export * from "./proc/delay";
export * from "./proc/feedback-delay";
export * from "./proc/foldback";
export * from "./proc/integrator";
export * from "./proc/mix";
export * from "./proc/onepole";
export * from "./proc/svf";
export * from "./proc/waveshaper";

export * from "./stateless/additive";
export * from "./stateless/comb";
export * from "./stateless/dsf";
export * from "./stateless/mix";
export * from "./stateless/rect";
export * from "./stateless/saw";
Expand Down
Loading

0 comments on commit e483245

Please sign in to comment.