-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathsimple.mix.ts
99 lines (78 loc) · 2.85 KB
/
simple.mix.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { SawOscillator, Instrument, Noise, BiQuadFilter, FilterType,
StereoSignal,
Freeverb, SineOscillator, Envelope, notefreq } from "./globalimports";
import { SAMPLERATE } from '../environment';
export const PATTERN_SIZE_SHIFT: usize = 4;
export const BEATS_PER_PATTERN_SHIFT: usize = 2;
const mainline = new StereoSignal();
const reverbline = new StereoSignal();
const freeverb = new Freeverb();
class Kick extends Instrument {
private velocity: f32;
envelope: Envelope = new Envelope(0.0, 0.2, 0, 0.2);
filterenvelope: Envelope = new Envelope(0.0, 0.05, 0.05, 0.1);
sawoscillator: SawOscillator = new SawOscillator();
noise: Noise = new Noise();
filter: BiQuadFilter = new BiQuadFilter();
set note(note: f32) {
if(note > 1) {
this.sawoscillator.frequency = 150;
this.velocity = note / 16;
this.envelope.attack();
this.filterenvelope.attack();
} else {
this.envelope.release();
this.filterenvelope.release();
}
}
next(): void {
let env: f32 = this.envelope.next();
this.sawoscillator.frequency = 20.0 + (env * 150.0);
this.filter.update_coeffecients(FilterType.LowPass, SAMPLERATE,
40 + (this.filterenvelope.next() * 2000), 0.2);
let osc1: f32 = this.envelope.next() * this.velocity * this.sawoscillator.next() * 0.8 + this.noise.next();
osc1 = this.filter.process(osc1);
const val = env * osc1;
mainline.left+=val;
mainline.right+=val;
}
}
class Lead extends Instrument {
osc: SineOscillator = new SineOscillator();
env: Envelope = new Envelope(0.1,0.1,1.0,0.1);
set note(note: f32) {
if(note > 1) {
this.osc.frequency = notefreq(note);
this.env.attack();
} else if (note === 0) {
this.env.release();
}
}
@inline
next(): void {
const val = this.osc.next() * this.env.next() * 0.3;
mainline.left += val;
mainline.right += val;
}
}
const voices: Instrument[] = [
new Kick(),
new Lead(),
new Lead(),
new Lead(),
new Lead()
];
export function setChannelValue(channel: usize, value: f32): void {
voices[channel].note = value;
}
@inline
export function mixernext(leftSampleBufferPtr: usize, rightSampleBufferPtr: usize): void {
mainline.clear();
reverbline.clear();
for(var n=0;n<voices.length;n++) {
voices[n].next();
}
freeverb.tick(reverbline);
store<f32>(leftSampleBufferPtr, mainline.left + reverbline.left);
store<f32>(rightSampleBufferPtr, mainline.right + reverbline.right);
}