Skip to content

Commit

Permalink
chore: replace more ctors with initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
ascpixi committed Dec 24, 2024
1 parent 78a59f3 commit 423ddb1
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 56 deletions.
6 changes: 1 addition & 5 deletions src/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,7 @@ export class GraphForwarder {
* For each note generator-to-instrument connection, stores the previous
* notes (in MIDI pitches) that were active in the previous pulse.
*/
private prevNoteMap: Map<string, number[]>;

constructor() {
this.prevNoteMap = new Map();
}
private prevNoteMap: Map<string, number[]> = new Map();

/**
* Traces the given graph, forwarding Vestige-generated data to out-of-graph
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/ChorusNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ChorusAudioEffect implements AudioEffect {
wet: 0.4
});

constructor () {
constructor() {
this.chorus.start();
}

Expand Down
16 changes: 6 additions & 10 deletions src/nodes/FilterNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ export class FilterNodeData extends EffectNodeData {
};

export class FilterAudioEffect implements AudioEffect {
filter: tone.Filter;
filter: tone.Filter = new tone.Filter({
frequency: cutoffScalarToHz(0.5),
Q: scalarToResonance(0.5),
rolloff: -24,
type: "lowpass"
});

connectTo(dst: AudioDestination): void {
dst.accept(this.filter);
Expand All @@ -59,15 +64,6 @@ export class FilterAudioEffect implements AudioEffect {
assert(handleId == SIGNAL_INPUT_HID_MAIN, `Unknown signal input handle ID ${handleId}`);
return unaryAudioDestination(this.filter);
}

constructor() {
this.filter = new tone.Filter({
frequency: cutoffScalarToHz(0.5),
Q: scalarToResonance(0.5),
rolloff: -24,
type: "lowpass"
});
}
}

export class FilterNodeSerializer implements NodeDataSerializer<FilterNodeData> {
Expand Down
15 changes: 4 additions & 11 deletions src/nodes/LfoNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export class LfoNodeData extends ValueNodeData {
};

export class LfoValueGenerator implements ValueGenerator {
shape: LfoShape;
frequency: number;
min: number;
max: number;
shape: LfoShape = "sine";
frequency: number = 1;
min: number = 0;
max: number = 1;

generate(time: number): number {
switch (this.shape) {
Expand All @@ -54,13 +54,6 @@ export class LfoValueGenerator implements ValueGenerator {
return scale(biToUni(lfoSaw(time, this.frequency)), this.min, this.max);
}
}

constructor() {
this.shape = "sine";
this.frequency = 1;
this.min = 0;
this.max = 1;
}
}

export class LfoNodeSerializer implements NodeDataSerializer<LfoNodeData> {
Expand Down
13 changes: 2 additions & 11 deletions src/nodes/MixNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,11 @@ const INPUT_A = signalInHandleId("a");
const INPUT_B = signalInHandleId("b");

export class MixNodeData extends EffectNodeData {
effect: MixAudioEffect;

constructor() {
super();
this.effect = new MixAudioEffect();
}
effect = new MixAudioEffect();
};

export class MixAudioEffect implements AudioEffect {
gain: tone.Gain;
gain = new tone.Gain();
inputA?: tone.ToneAudioNode;
inputB?: tone.ToneAudioNode;

Expand All @@ -47,10 +42,6 @@ export class MixAudioEffect implements AudioEffect {
assert(handleId == INPUT_A || handleId == INPUT_B, `Unknown signal input handle ID ${handleId}`);
return unaryAudioDestination(this.gain);
}

constructor() {
this.gain = new tone.Gain();
}
}

export class MixNodeSerializer extends NullNodeDataSerializer<MixNodeData> {
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/PentatonicChordsNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class PentatonicChordsGenerator implements PlainNoteGenerator {
seedOffset: number;
lastNotes: number[] = [];

constructor(
constructor (
public chordLength: number = 6,
public minNotes: number = 4,
public maxNotes: number = 6,
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/PentatonicMelodyNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class PentatonicMelodyGenerator implements PlainNoteGenerator {
offset: number;
lastNotes: number[] = [];

constructor(
constructor (
public density: number = 50,
public octave: number = 4,
public pitchRange: number = 6,
Expand Down
14 changes: 5 additions & 9 deletions src/nodes/ReverbNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export class ReverbNodeData extends EffectNodeData {
};

export class ReverbAudioEffect implements AudioEffect {
reverb: tone.Reverb;
reverb: tone.Reverb = new tone.Reverb({
decay: 4.00,
preDelay: 20 / 1000,
wet: 0.5
});

connectTo(dst: AudioDestination): void {
dst.accept(this.reverb);
Expand All @@ -66,14 +70,6 @@ export class ReverbAudioEffect implements AudioEffect {
assert(handleId == SIGNAL_INPUT_HID_MAIN, `Unknown signal input handle ID ${handleId}`);
return unaryAudioDestination(this.reverb);
}

constructor() {
this.reverb = new tone.Reverb({
decay: 4.00,
preDelay: 20 / 1000,
wet: 0.5
});
}
}

export class ReverbNodeSerializer implements NodeDataSerializer<ReverbNodeData> {
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/SamplerNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class SamplerAudioGenerator implements AudioGenerator {
this.sampler.connect(this.out);
}

constructor () {
constructor() {
this.sampler.connect(this.out);
}

Expand Down
8 changes: 2 additions & 6 deletions src/nodes/SynthNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class SynthNodeData extends InstrumentNodeData {
this.generator.synth.set({ envelope: env });
}

constructor () {
constructor() {
super();

this.generator = new SynthAudioGenerator();
Expand All @@ -84,15 +84,11 @@ export class SynthNodeData extends InstrumentNodeData {
};

export class SynthAudioGenerator implements AudioGenerator {
synth: tone.PolySynth;
synth = new tone.PolySynth(tone.Synth, { oscillator: { type: this.waveform} });

Check failure on line 87 in src/nodes/SynthNode.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

src/tests/serialization.test.ts > deserializes initial project

TypeError: Cannot read properties of undefined (reading 'partialCount') ❯ get partialCount ../../../../../node_modules/.vite/deps/tone.js:16196:29 ❯ ../../../../../node_modules/.vite/deps/tone.js:10508:57 ❯ _OmniOscillator.set ../../../../../node_modules/.vite/deps/tone.js:10507:24 ❯ _OmniOscillator.set ../../../../../node_modules/.vite/deps/tone.js:16207:11 ❯ new _OmniOscillator ../../../../../node_modules/.vite/deps/tone.js:16117:10 ❯ new _Synth ../../../../../node_modules/.vite/deps/tone.js:18317:23 ❯ _PolySynth._getNextAvailableVoice ../../../../../node_modules/.vite/deps/tone.js:19930:21 ❯ new _PolySynth ../../../../../node_modules/.vite/deps/tone.js:19894:29 ❯ <instance_members_initializer> src/nodes/SynthNode.tsx:87:10

Check failure on line 87 in src/nodes/SynthNode.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

src/tests/serialization.test.ts > deserializes test project 1

TypeError: Cannot read properties of undefined (reading 'partialCount') ❯ get partialCount ../../../../../node_modules/.vite/deps/tone.js:16196:29 ❯ ../../../../../node_modules/.vite/deps/tone.js:10508:57 ❯ _OmniOscillator.set ../../../../../node_modules/.vite/deps/tone.js:10507:24 ❯ _OmniOscillator.set ../../../../../node_modules/.vite/deps/tone.js:16207:11 ❯ new _OmniOscillator ../../../../../node_modules/.vite/deps/tone.js:16117:10 ❯ new _Synth ../../../../../node_modules/.vite/deps/tone.js:18317:23 ❯ _PolySynth._getNextAvailableVoice ../../../../../node_modules/.vite/deps/tone.js:19930:21 ❯ new _PolySynth ../../../../../node_modules/.vite/deps/tone.js:19894:29 ❯ <instance_members_initializer> src/nodes/SynthNode.tsx:87:10
private _waveform: Waveform = "sine";
private _unisonSpread: number = 20;
private _unisonCount: number = 1;

constructor() {
this.synth = new tone.PolySynth(tone.Synth, { oscillator: { type: this.waveform} });
}

/** Fully applies oscillator settings, changing its type. May produce audible clicks. */
private fullyApplyOsc() {
if (this._unisonCount > 1) {
Expand Down

0 comments on commit 423ddb1

Please sign in to comment.