This repository has been archived by the owner on Dec 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdsp.vult
168 lines (134 loc) · 4.38 KB
/
dsp.vult
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//fun process(cv1:real, cv2:real, gate1:bool, gate2:bool, gate3:bool, gate4:bool){
/* Phase distortion oscillator */
// Used to soften the transitions of controls
fun smooth2(input){
mem x;
x = x+(input-x)*0.05;
return x;
}
// Used to soften the transitions of controls
fun smooth(input){
mem x;
x = x+(input-x)*0.005;
return x;
}
// Returns true every time the input value changes
fun change(x):bool {
mem pre_x;
val v:bool = pre_x<>x;
pre_x = x;
return v;
}
// Converts the MIDI note to increment rate at a 44100 sample rate
fun pitchToRate(d) return 8.1758*exp(0.0577623*d)/44100.0;
fun phasor(pitch,reset){
mem rate,phase;
if(change(pitch))
rate = pitchToRate(pitch);
phase = if reset then 0.0 else phase + rate;
phase = if phase > 1.0 then phase - 1.0 else phase;
return phase;
}
fun delay(x:real, time:real, feedback:real) : real {
mem buffer : array(real,32000);
mem write_pos;
// Constraints the parameter values
time = clip(time,0.0,1.0);
feedback = clip(feedback,0.0,1.0);
// Gets the position in the buffer to read
val index_r = real(size(buffer)) * time;
val index_i = int(floor(index_r));
val delta = write_pos - index_i;
val read_pos = if delta < 0 then size(buffer)+delta else delta;
// Gets the decimal part of the position
val decimal = index_r - real(index_i);
// Reads the values in the buffer
val x1 = get(buffer,read_pos);
val x2 = get(buffer,(read_pos+1) % size(buffer));
// Interpolates the value
val ret = (x2-x1)*decimal + x1;
// Write the data to the buffer
write_pos = (write_pos+1) % size(buffer);
_ = set(buffer, write_pos, Saturate.process(x+feedback*ret));
return ret;
}
fun synth_voice(cv, g){
mem prev_phase;
mem delay_time;
val det = Adsr.do(g, 0.0, 0.5, 0.5, 0.5)*2.0;
val real_pitch = smooth2(cv);
// Implements the resonant filter simulation as shown in
// http://en.wikipedia.org/wiki/Phase_distortion_synthesis
val phase1 = phasor(real_pitch, false);
val comp = 1.0 - phase1;
val reset = (prev_phase - phase1) > 0.5;
prev_phase = phase1;
val phase2 = phasor((real_pitch+smooth(det)*64.0),reset);
val sine = sin(2.0*3.14159265359*phase2);
//return (sine*comp);
val env = Adsr.do(g, 0.02,0.5,0.5,0.2);
val sub_osc = Saw_blit.process(Util.pitchToCv(real_pitch-12.0));
val sub_osc_filtered = Svf.process(sub_osc, det*2.0, 0.4, 0);
val dry = env*((sine*comp)+sub_osc_filtered);
val mixed = dry*0.6+(delay(dry, smooth(delay_time), 0.25)*0.4);
return mixed;
}
// Main processing function
fun process(cv1:real, cv2:real, gate1:bool, gate2:bool, gate3:bool, gate4:bool){
/*
mem volume; //,detune; // values set in 'controlChange'
mem pitch;
mem pre_phase1;
mem gate;
mem velo;
mem delay_time;
val det = Adsr.do(gate, 0.0, 0.5, 0.5, 0.5)*2.0;
val real_pitch = smooth2(pitch);
// Implements the resonant filter simulation as shown in
// http://en.wikipedia.org/wiki/Phase_distortion_synthesis
val phase1 = phasor(real_pitch, false);
val comp = 1.0 - phase1;
val reset = (pre_phase1 - phase1) > 0.5;
pre_phase1 = phase1;
val phase2 = phasor(velo*4.0+(real_pitch+smooth(det)*64.0),reset);
val sine = sin(2.0*3.14159265359*phase2);
//return (sine*comp);
val env = Adsr.do(gate, 0.02,0.5,0.5,0.2);
val sub_osc = Saw_blit.process(Util.pitchToCv(pitch-12.0));
val sub_osc_filtered = Svf.process(sub_osc, det*2.0, 0.4, 0);
val dry = env*((sine*comp)+sub_osc_filtered);
val mixed = dry*0.6+(delay(dry, smooth(delay_time), 0.25)*0.4);
*/
mem pitch;
mem gate;
val osc = synth_voice(pitch, gate);
return osc;
}
// Called when a note On is received
and noteOn(note:int,velocity:int,channel:int){
mem gate = 1.0;
mem pitch = real(note);
mem velo = real(velocity)/64.0;
}
// Called when a note Off is received
and noteOff(note:int,channel:int){
mem gate = 0.0;
}
// Called when a control changes
and controlChange(control:int,value:int,channel:int){
mem volume;
mem detune;
mem delay_time;
// Control 30 defines the volume
if(control==30) volume = real(value)/127.0;
if(control==1) delay_time = real(value)/127.0;
}
// Called on initialization to define initial values
and default(){
mem volume = 1.0;
mem pitch = 45.0;
mem detune = 0.0;
mem gate = 0.0;
mem velo = 1.0;
mem delay_time = 0.5;
}