-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtestinstrument.class.ts
45 lines (39 loc) · 1.59 KB
/
testinstrument.class.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { SAMPLERATE } from '../environment';
import { SineOscillator } from '../synth/sineoscillator.class';
import { StereoSignal } from '../synth/stereosignal.class';
import { Envelope } from '../synth/envelope.class';
import { DelayLine } from '../fx/delayline';
import { notefreq } from '../synth/note';
export class TestInstrument {
private _note: f32;
readonly envelope: Envelope = new Envelope(0.02, 0.2, 0.2, 0.2);
readonly sineoscillator: SineOscillator = new SineOscillator();
readonly sineoscillator2: SineOscillator = new SineOscillator();
readonly delayline: DelayLine = new DelayLine(SAMPLERATE * 0.5 as usize);
readonly delayline2: DelayLine = new DelayLine(SAMPLERATE * 0.5 as usize);
readonly signal: StereoSignal = new StereoSignal();
set note(note: f32) {
if(note!==this.note && note > 1) {
this.envelope.attack();
this.sineoscillator.frequency = notefreq(note);
this.sineoscillator2.frequency = notefreq(note + 12);
} else if(note===0) {
this.envelope.release();
}
this._note = note;
}
get note(): f32 {
return this._note;
}
next(): void {
let env: f32 = this.envelope.next();
if(env === 0) {
this.signal.clear();
} else {
let osc1 = env * this.sineoscillator.next();
let osc2 = env * this.sineoscillator2.next();
this.signal.left = osc1 * 0.8 + osc2 * 0.2;
this.signal.right = osc1 * 0.2 + osc2 * 0.8;
}
}
}