Skip to content

Commit

Permalink
refactor(dsp): update deps, imports, use new Fn types
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 5, 2020
1 parent 486cbcf commit 683b4e9
Show file tree
Hide file tree
Showing 37 changed files with 68 additions and 76 deletions.
2 changes: 1 addition & 1 deletion packages/dsp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.1",
"@microsoft/api-extractor": "^7.9.11",
"@thi.ng/api": "^6.12.3",
"@types/mocha": "^8.0.3",
"@types/node": "^14.6.1",
"mocha": "^8.1.2",
Expand All @@ -49,7 +50,6 @@
"typescript": "^4.0.2"
},
"dependencies": {
"@thi.ng/api": "^6.12.3",
"@thi.ng/checks": "^2.7.7",
"@thi.ng/errors": "^1.2.20",
"@thi.ng/math": "^2.0.4",
Expand Down
4 changes: 2 additions & 2 deletions packages/dsp/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Fn2, IDeref, NumericArray } from "@thi.ng/api";
import type { FnN2, IDeref, NumericArray } from "@thi.ng/api";

export type StatelessOscillator = (
phase: number,
Expand All @@ -10,7 +10,7 @@ export type StatelessOscillator = (

export type ComplexArray = [NumericArray, NumericArray];

export type WindowFn = Fn2<number, number, number>;
export type WindowFn = FnN2;

export interface IGen<T> extends Iterable<T>, IDeref<T> {
next(): T;
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/comp/addg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MapG1 } from "./mapg";
import type { IGen } from "../api";
import { MapG1 } from "./mapg";

/**
* Creates a new {@link IGen} using given `step` gen and `start
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/comp/compp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AProc } from "../proc/aproc";
import type { IProc } from "../api";
import { AProc } from "../proc/aproc";

export function compP<A, B, C>(a: IProc<A, B>, b: IProc<B, C>): IProc<A, C>;
export function compP<A, B, C, D>(
Expand Down
4 changes: 2 additions & 2 deletions packages/dsp/src/comp/mapg.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { illegalArity } from "@thi.ng/errors";
import { AGen } from "../gen/agen";
import type { Fn2, Fn3, Fn4, Fn5, FnAny } from "@thi.ng/api";
import { illegalArity } from "@thi.ng/errors";
import type { IGen } from "../api";
import { AGen } from "../gen/agen";

export function mapG<A, T>(op: Fn2<A, T, T>, a: IGen<A>, init: T): IGen<T>;
export function mapG<A, B, T>(
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/comp/multiplex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AProc } from "../proc/aproc";
import type { IProc } from "../api";
import { AProc } from "../proc/aproc";

export function multiplex<T, A, B>(
a: IProc<T, A>,
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/comp/pipe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MapG1 } from "./mapg";
import type { FnAny } from "@thi.ng/api";
import type { IGen, IProc } from "../api";
import { MapG1 } from "./mapg";

export function pipe<A, B>(src: IGen<A>, proc: IProc<A, B>): IGen<B>;
export function pipe<A, B, C>(
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/comp/product.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MapG2, MapG3 } from "./mapg";
import type { IGen } from "../api";
import { MapG2, MapG3 } from "./mapg";

/**
* Higher order gen. Returns a {@link MapG2} or {@link MapG3} yielding
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/comp/sum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MapG2, MapG3 } from "./mapg";
import type { IGen } from "../api";
import { MapG2, MapG3 } from "./mapg";

/**
* Higher order gen. Returns a {@link MapG2} or {@link MapG3} yielding
Expand Down
8 changes: 4 additions & 4 deletions packages/dsp/src/fft/fft.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FnN3, NumericArray } from "@thi.ng/api";
import { isNumber } from "@thi.ng/checks";
import { magDb } from "../util/convert";
import type { NumericArray } from "@thi.ng/api";
import type { ComplexArray } from "../api";
import { magDb } from "../util/convert";

const PI = Math.PI;

Expand Down Expand Up @@ -333,7 +333,7 @@ export const spectrumPhase = (
* @param fs - sample rate
* @param n - window size
*/
export const freqBin = (f: number, fs: number, n: number) => ((f * n) / fs) | 0;
export const freqBin: FnN3 = (f, fs, n) => ((f * n) / fs) | 0;

/**
* Returns frequency for given FFT bin index, sample rate and window
Expand All @@ -343,7 +343,7 @@ export const freqBin = (f: number, fs: number, n: number) => ((f * n) / fs) | 0;
* @param fs - sample rate
* @param n - window size
*/
export const binFreq = (bin: number, fs: number, n: number) => (bin * fs) / n;
export const binFreq: FnN3 = (bin, fs, n) => (bin * fs) / n;

/**
* Returns array of bin center frequencies for given FFT window size and
Expand Down
18 changes: 5 additions & 13 deletions packages/dsp/src/fft/window.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FloatArray, Fn, FnU3, FnU4 } from "@thi.ng/api";
import { isNumber } from "@thi.ng/checks";
import { PI, TAU } from "@thi.ng/math";
import type { FloatArray } from "@thi.ng/api";
import type { WindowFn } from "../api";

// https://en.wikipedia.org/wiki/Window_function
Expand Down Expand Up @@ -31,33 +31,25 @@ export const windowRect: WindowFn = () => 1;

export const windowSin: WindowFn = (i, n) => sin((PI * i) / n);

export const windowSinPow = (k: number): WindowFn => (i, n) =>
export const windowSinPow: Fn<number, WindowFn> = (k) => (i, n) =>
sin((PI * i) / n) ** k;

export const windowLanczos: WindowFn = (i, n) => {
i = PI * ((2 * i) / n - 1);
return sin(i) / i;
};

const windowCosSum = (k: number): WindowFn => {
const windowCosSum: Fn<number, WindowFn> = (k) => {
let ik = 1 - k;
return (i, n) => k - ik * cos((TAU * i) / n);
};

const windowCosSum3 = (k1: number, k2: number, k3: number): WindowFn => (
i,
n
) => {
const windowCosSum3: FnU3<number, WindowFn> = (k1, k2, k3) => (i, n) => {
i /= n;
return k1 + k2 * cos(TAU * i) + k3 * cos(PI4 * i);
};

const windowCosSum4 = (
k1: number,
k2: number,
k3: number,
k4: number
): WindowFn => (i, n) => {
const windowCosSum4: FnU4<number, WindowFn> = (k1, k2, k3, k4) => (i, n) => {
i /= n;
return k1 + k2 * cos(TAU * i) + k3 * cos(PI4 * i) + k4 * cos(PI6 * i);
};
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/gen/add.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AGen } from "./agen";
import type { IReset } from "@thi.ng/api";
import { AGen } from "./agen";

/**
* Creates a new `Add` gen using given `step` (default: 1) and `start
Expand Down
4 changes: 2 additions & 2 deletions packages/dsp/src/gen/adsr.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { IReset } from "@thi.ng/api";
import { clamp01 } from "@thi.ng/math";
import type { IGen } from "../api";
import { add } from "./add";
import { AGen } from "./agen";
import { curve } from "./curve";
import type { IReset } from "@thi.ng/api";
import type { IGen } from "../api";

const enum EnvPhase {
ATTACK,
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/gen/alt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AGen } from "./agen";
import type { IReset } from "@thi.ng/api";
import { AGen } from "./agen";

export const alt = (n = 1) => new Alt(n, -n);

Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/gen/cosine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IReset } from "@thi.ng/api";
import { TAU } from "@thi.ng/math";
import { AGen } from "./agen";
import type { IReset } from "@thi.ng/api";

/**
* Approximated cosine generator using given normalized `freq` and `amp`
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/gen/impulse-train.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AGen } from "./agen";
import type { IReset } from "@thi.ng/api";
import { AGen } from "./agen";

/**
* https://en.wikipedia.org/wiki/Dirac_comb
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/gen/impulse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AGen } from "./agen";
import type { IReset } from "@thi.ng/api";
import { AGen } from "./agen";

/**
* Numeric version of {@link impulseT}, using given `on` (default: 1) as
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/gen/madd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AGen } from "./agen";
import type { IReset } from "@thi.ng/api";
import { AGen } from "./agen";

/**
* Returns new multiply-add gen producing `y(t) = factor * y(t-1) +
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/gen/mul.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AGen } from "./agen";
import type { IReset } from "@thi.ng/api";
import { AGen } from "./agen";

/**
* Returns new multiply gen, producing `y(t) = factor * y(t-1)`, using
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/gen/osc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { isNumber } from "@thi.ng/checks";
import type { IGen, StatelessOscillator } from "../api";
import { sum } from "../comp/sum";
import { Add, add } from "./add";
import { AGen } from "./agen";
import { Const } from "./const";
import type { IGen, StatelessOscillator } from "../api";

/**
* Higher order oscillator gen, wrapping a {@link StatelessOscillator}
Expand Down
5 changes: 2 additions & 3 deletions packages/dsp/src/gen/pink-noise.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SYSTEM } from "@thi.ng/random";
import { AGen } from "./agen";
import type { IReset, Tuple } from "@thi.ng/api";
import type { IRandom } from "@thi.ng/random";
import { IRandom, SYSTEM } from "@thi.ng/random";
import { AGen } from "./agen";

type PNoiseCoeffs = Tuple<number, 5>;
const AMP = <PNoiseCoeffs>[3.8024, 2.9694, 2.597, 3.087, 3.4006];
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/gen/reciprocal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AGen } from "./agen";
import type { IReset } from "@thi.ng/api";
import { AGen } from "./agen";

/**
* Returns a gen which yield sequence `y(t) = 1 / (y(t - 1) + step)`.
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/gen/sincos.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IReset } from "@thi.ng/api";
import { TAU } from "@thi.ng/math";
import { AGen } from "./agen";
import type { IReset } from "@thi.ng/api";

/**
* Generator of sine & cosine values of given frequency in the form of
Expand Down
5 changes: 2 additions & 3 deletions packages/dsp/src/gen/white-noise.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SYSTEM } from "@thi.ng/random";
import { AGen } from "./agen";
import type { IReset } from "@thi.ng/api";
import type { IRandom } from "@thi.ng/random";
import { IRandom, SYSTEM } from "@thi.ng/random";
import { AGen } from "./agen";

/**
* White noise gen with customizable gain and
Expand Down
4 changes: 2 additions & 2 deletions packages/dsp/src/osc/additive.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { gibbs } from "../util/anti-alias";
import { sin } from "./sin";
import type { Fn } from "@thi.ng/api";
import type { StatelessOscillator } from "../api";
import { gibbs } from "../util/anti-alias";
import { sin } from "./sin";

export const additive = (
osc: StatelessOscillator,
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/osc/wavetable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fract, mix as _mix } from "@thi.ng/math";
import type { Fn3, NumericArray } from "@thi.ng/api";
import { fract, mix as _mix } from "@thi.ng/math";
import type { StatelessOscillator } from "../api";

export const wavetable = (
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/proc/allpass.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { IReset } from "@thi.ng/api";
import { clamp05, PI, QUARTER_PI } from "@thi.ng/math";
import { AProc } from "./aproc";
import type { IReset } from "@thi.ng/api";

/**
* One-pole allpass filter.
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/proc/aproc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { map } from "@thi.ng/transducers";
import type { IXform } from "@thi.ng/transducers";
import { map } from "@thi.ng/transducers";
import type { IProc, IProc2 } from "../api";

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/dsp/src/proc/biquad.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { IReset } from "@thi.ng/api";
import { unsupported } from "@thi.ng/errors";
import { clamp05, PI, SQRT2, SQRT2_2 } from "@thi.ng/math";
import type { FilterConfig, IFilter } from "../api";
import { FilterType } from "../constants";
import { dbMag } from "../util/convert";
import { AProc } from "./aproc";
import type { IReset } from "@thi.ng/api";
import type { FilterConfig, IFilter } from "../api";

type BiquadType =
| FilterType.LP
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/proc/delay.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Fn0, IClear, ILength, IReset } from "@thi.ng/api";
import { isFunction } from "@thi.ng/checks";
import { wrap } from "@thi.ng/math";
import { AProc } from "./aproc";
import type { Fn0, IClear, ILength, IReset } from "@thi.ng/api";

/**
* Delay line of length `n` for numeric values.
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/proc/integrator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AProc } from "./aproc";
import type { IReset } from "@thi.ng/api";
import { AProc } from "./aproc";

/**
* Leaky integrator.
Expand Down
7 changes: 4 additions & 3 deletions packages/dsp/src/proc/onepole.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { IClear, IReset } from "@thi.ng/api";
import { clamp05, TAU } from "@thi.ng/math";
import type { FilterConfig, IFilter } from "../api";
import { FilterType } from "../constants";
import { AProc } from "./aproc";
import type { IClear, IReset } from "@thi.ng/api";
import type { FilterConfig, IFilter } from "../api";

type OnepoleType = FilterType.LP | FilterType.HP;

Expand All @@ -13,7 +13,8 @@ export const onepoleHP = (fc: number) => new OnePole(FilterType.HP, fc);
/**
* https://www.earlevel.com/main/2012/12/15/a-one-pole-filter/
*/
export class OnePole extends AProc<number, number>
export class OnePole
extends AProc<number, number>
implements IClear, IFilter, IReset {
protected _a0!: number;
protected _b1!: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/dsp/src/proc/svf.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IReset } from "@thi.ng/api";
import { clamp05, PI } from "@thi.ng/math";
import { FilterType } from "../constants";
import { AProc } from "./aproc";
import type { IReset } from "@thi.ng/api";

type SVFType =
| FilterType.LP
Expand Down
4 changes: 2 additions & 2 deletions packages/dsp/src/proc/waveshaper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { foldback as _foldback, PI } from "@thi.ng/math";
import { AProc } from "./aproc";
import type { Fn2 } from "@thi.ng/api";
import { PI } from "@thi.ng/math";
import { AProc } from "./aproc";

export type WaveShaperFn = Fn2<number, number, number>;

Expand Down
8 changes: 4 additions & 4 deletions packages/dsp/src/util/anti-alias.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { FnN2 } from "@thi.ng/api";
import { HALF_PI } from "@thi.ng/math";

/**
Expand All @@ -11,8 +12,7 @@ import { HALF_PI } from "@thi.ng/math";
* @param n - number of octaves
* @param i - curr octave [1..n]
*/
export const gibbs = (n: number, i: number) =>
Math.cos(((i - 1) * HALF_PI) / n) ** 2;
export const gibbs: FnN2 = (n, i) => Math.cos(((i - 1) * HALF_PI) / n) ** 2;

/**
* Fejér weight for `k`-th harmonic in a Fourier series of length `n`.
Expand All @@ -25,7 +25,7 @@ export const gibbs = (n: number, i: number) =>
* @param k -
* @param n -
*/
export const fejer = (k: number, n: number) => (n - k) / n;
export const fejer: FnN2 = (k, n) => (n - k) / n;

/**
* Polynomial attenuation to create bandlimited version of a signal.
Expand All @@ -36,7 +36,7 @@ export const fejer = (k: number, n: number) => (n - k) / n;
* @param dt - time step
* @param t - normalized phase
*/
export const polyBLEP = (dt: number, t: number) =>
export const polyBLEP: FnN2 = (dt, t) =>
t < dt
? ((t /= dt), t + t - t * t - 1)
: t > 1 - dt
Expand Down
Loading

0 comments on commit 683b4e9

Please sign in to comment.