UNPKG

3.07 kBJavaScriptView Raw
1import { Effect } from "./Effect.js";
2import { optionsFromArguments } from "../core/util/Defaults.js";
3import { WaveShaper } from "../signal/WaveShaper.js";
4import { assert } from "../core/util/Debug.js";
5/**
6 * Chebyshev is a waveshaper which is good
7 * for making different types of distortion sounds.
8 * Note that odd orders sound very different from even ones,
9 * and order = 1 is no change.
10 * Read more at [music.columbia.edu](http://music.columbia.edu/cmc/musicandcomputers/chapter4/04_06.php).
11 * @example
12 * // create a new cheby
13 * const cheby = new Tone.Chebyshev(50).toDestination();
14 * // create a monosynth connected to our cheby
15 * const synth = new Tone.MonoSynth().connect(cheby);
16 * synth.triggerAttackRelease("C2", 0.4);
17 * @category Effect
18 */
19export class Chebyshev extends Effect {
20 constructor() {
21 const options = optionsFromArguments(Chebyshev.getDefaults(), arguments, ["order"]);
22 super(options);
23 this.name = "Chebyshev";
24 this._shaper = new WaveShaper({
25 context: this.context,
26 length: 4096,
27 });
28 this._order = options.order;
29 this.connectEffect(this._shaper);
30 this.order = options.order;
31 this.oversample = options.oversample;
32 }
33 static getDefaults() {
34 return Object.assign(Effect.getDefaults(), {
35 order: 1,
36 oversample: "none",
37 });
38 }
39 /**
40 * get the coefficient for that degree
41 * @param x the x value
42 * @param degree
43 * @param memo memoize the computed value. this speeds up computation greatly.
44 */
45 _getCoefficient(x, degree, memo) {
46 if (memo.has(degree)) {
47 return memo.get(degree);
48 }
49 else if (degree === 0) {
50 memo.set(degree, 0);
51 }
52 else if (degree === 1) {
53 memo.set(degree, x);
54 }
55 else {
56 memo.set(degree, 2 * x * this._getCoefficient(x, degree - 1, memo) -
57 this._getCoefficient(x, degree - 2, memo));
58 }
59 return memo.get(degree);
60 }
61 /**
62 * The order of the Chebyshev polynomial which creates the equation which is applied to the incoming
63 * signal through a Tone.WaveShaper. Must be an integer. The equations are in the form:
64 * ```
65 * order 2: 2x^2 + 1
66 * order 3: 4x^3 + 3x
67 * ```
68 * @min 1
69 * @max 100
70 */
71 get order() {
72 return this._order;
73 }
74 set order(order) {
75 assert(Number.isInteger(order), "'order' must be an integer");
76 this._order = order;
77 this._shaper.setMap((x) => {
78 return this._getCoefficient(x, order, new Map());
79 });
80 }
81 /**
82 * The oversampling of the effect. Can either be "none", "2x" or "4x".
83 */
84 get oversample() {
85 return this._shaper.oversample;
86 }
87 set oversample(oversampling) {
88 this._shaper.oversample = oversampling;
89 }
90 dispose() {
91 super.dispose();
92 this._shaper.dispose();
93 return this;
94 }
95}
96//# sourceMappingURL=Chebyshev.js.map
\No newline at end of file