Skip to content

Commit

Permalink
refactor(dsp): restructure oscillators, fft, window, update gen-diagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 24, 2020
1 parent ff99055 commit 9efd69f
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/dsp/src/fft.ts → packages/dsp/src/fft/fft.ts
@@ -1,7 +1,7 @@
import { NumericArray } from "@thi.ng/api";
import { isNumber } from "@thi.ng/checks";
import { ComplexArray } from "./api";
import { magDb } from "./util/convert";
import { ComplexArray } from "../api";
import { magDb } from "../util/convert";

const PI = Math.PI;

Expand Down
File renamed without changes.
25 changes: 13 additions & 12 deletions packages/dsp/src/index.ts
@@ -1,5 +1,4 @@
export * from "./api";
export * from "./fft";

export * from "./comp/addg";
export * from "./comp/compp";
Expand Down Expand Up @@ -31,7 +30,7 @@ export * from "./gen/white-noise";
export * from "./proc/allpass";
export * from "./proc/aproc";
export * from "./proc/biquad";
export * from "./proc/dcblocker";
export * from "./proc/dcblock";
export * from "./proc/delay";
export * from "./proc/feedback-delay";
export * from "./proc/foldback";
Expand All @@ -41,17 +40,19 @@ export * from "./proc/onepole";
export * from "./proc/svf";
export * from "./proc/waveshaper";

export * from "./stateless/additive";
export * from "./stateless/dsf";
export * from "./stateless/mix";
export * from "./stateless/parabolic";
export * from "./stateless/rect";
export * from "./stateless/saw";
export * from "./stateless/sin";
export * from "./stateless/tri";
export * from "./stateless/wavetable";
export * from "./osc/additive";
export * from "./osc/dsf";
export * from "./osc/mix";
export * from "./osc/parabolic";
export * from "./osc/rect";
export * from "./osc/saw";
export * from "./osc/sin";
export * from "./osc/tri";
export * from "./osc/wavetable";

export * from "./fft/fft";
export * from "./fft/window";

export * from "./util/anti-alias";
export * from "./util/convert";
export * from "./util/filter-response";
export * from "./util/window";
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Expand Up @@ -6,9 +6,9 @@ import { OnePole } from "./onepole";
*
* @param freq
*/
export const dcblocker = (freq: number) => new DCBlocker(FilterType.LP, freq);
export const dcBlock = (freq: number) => new DCBlock(FilterType.LP, freq);

export class DCBlocker extends OnePole {
export class DCBlock extends OnePole {
next(x: number) {
return x - super.next(x);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/dsp/src/proc/foldback.ts
Expand Up @@ -5,6 +5,10 @@ import { AProc } from "./aproc";
* Recursively folds input into `[-thresh .. +thresh]` interval and
* amplifies it with `amp` (default: 1/thresh).
*
* @remarks
* Reference:
* - https://www.desmos.com/calculator/lkyf2ag3ta
*
* @param thresh - fold threshold
* @param amp - post amplifier
*/
Expand Down
7 changes: 3 additions & 4 deletions packages/dsp/src/proc/svf.ts
Expand Up @@ -27,14 +27,13 @@ export const svfAllpass = (fc: number, q?: number) =>
new SVF(FilterType.ALL, fc, q);

/**
* State variable filter w/ trapezoidal integration, after Andrew
* Simper.
* Multi-type state variable filter w/ trapezoidal integration, after
* Andrew Simper.
*
* Reference:
*
* - https://cytomic.com/files/dsp/SvfLinearTrapOptimised2.pdf
* - https://en.wikipedia.org/wiki/Trapezoidal_rule
*
*/
export class SVF extends AProc<number, number> implements IReset {
protected _a1!: number;
Expand All @@ -47,7 +46,7 @@ export class SVF extends AProc<number, number> implements IReset {
constructor(
protected _type: SVFType,
protected _freq: number,
protected _q = 0
protected _q = 0.5
) {
super(0);
this.reset();
Expand Down
29 changes: 23 additions & 6 deletions packages/dsp/tools/generate-diagrams.ts
Expand Up @@ -26,7 +26,6 @@ import {
import { writeFileSync } from "fs";
import {
allpass,
AllPass1,
biquadBP,
biquadHiShelf,
biquadHP,
Expand All @@ -35,9 +34,10 @@ import {
biquadNotch,
biquadPeak,
curve,
dcblocker,
dcBlock,
dsfHOF,
filterResponse,
foldback,
freqMs,
freqRange,
IGen,
Expand All @@ -55,9 +55,12 @@ import {
saw,
sin,
StatelessOscillator,
svfAllpass,
svfBP,
svfHP,
svfLP,
svfNotch,
svfPeak,
tri,
wavetable,
whiteNoise
Expand All @@ -76,7 +79,7 @@ const OSC: IObjectOf<StatelessOscillator> = {
tri,
parabolic,
recttri: mixOscHOF(rect, tri),
dsf: dsfHOF(0.4, 3),
dsf: dsfHOF(0.6, 2.04),
wt: wavetable(curve(1, -1, 127).take(128))
};

Expand Down Expand Up @@ -245,9 +248,15 @@ withFilters((id) => `${id}-svf-hpf.svg`, svfHP);

withFilters((id) => `${id}-svf-bpf.svg`, svfBP);

withFilters((id) => `${id}-svf-notch.svg`, svfNotch);

withFilters((id) => `${id}-svf-peak.svg`, svfPeak);

withFilters((id) => `${id}-svf-all.svg`, svfAllpass);

withFilters((id) => `${id}-1pole-lpf.svg`, onepoleLP);

withFilters((id) => `${id}-dcblock.svg`, dcblocker);
withFilters((id) => `${id}-dcblock.svg`, dcBlock);

withFilters((id) => `${id}-bq-lpf.svg`, biquadLP);

Expand All @@ -263,6 +272,14 @@ withFilters((id) => `${id}-bq-lsh.svg`, biquadLoShelf);

withFilters((id) => `${id}-bq-hsh.svg`, biquadHiShelf);

withFilters(
(id) => `${id}-foldback.svg`,
(t) => foldback(t),
undefined,
[0.5, 0.3, 0.15],
["orig", ...[0.5, 0.3, 0.15].map((t) => `t=${t}`)]
);

withFilters(
(id) => `${id}-allpass1.svg`,
(f) => allpass(f)
Expand All @@ -289,8 +306,8 @@ withFilters(
withFilters(
(id) => `${id}-allpass-high.svg`,
(f) => {
let flt = new AllPass1(f);
return <any>{
let flt = allpass(f);
return <IGen<number>>{
next(x: number) {
return flt.high(x);
}
Expand Down

0 comments on commit 9efd69f

Please sign in to comment.