-
Notifications
You must be signed in to change notification settings - Fork 0
/
replicant.cmajor
216 lines (176 loc) · 6.68 KB
/
replicant.cmajor
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
Blade Runner End Titles by Vangelis
Thanks to Mick Grierson... https://github.com/micknoise/Maximilian/blob/master/maximilian_examples/16.Replicant.cpp
cmajor port by Oli Larkin
*/
namespace Replicant
{
let clock = 9; // e.g quarter note in samples = SR/clock
graph Replicant [[ main ]]
{
output stream float out;
node
{
bass = Bass;
lead = Lead;
delay = Delay(0.35f);
}
connection
{
bass.out -> out;
lead.out -> out;
lead.out -> delay.audioIn;
delay.audioOut -> out;
}
}
float pdSawtooth(float phase, float dcw)
{
dcw = clamp(dcw, 0.001f, 0.999f);
dcw = (1.f-dcw) * 0.5f;
let hmdcw = 0.5f - dcw;
let arg1 = phase * (hmdcw/dcw);
let arg2 = (-1.f * phase + 1.f) * (hmdcw / (1.f - dcw));
let pd = phase + min(arg1, arg2);
return -cos(pd * float(twoPi));
}
float pwm(float phase, float pw)
{
pw = clamp(pw, 0.01f, 0.99f);
return ((phase + (wrap(phase + pw, 1.f) * -1.f) + pw) * 2.f) - 1.f;
}
float triangle(float phase)
{
return 1.f - abs((phase * 2.f) - 1.f );
}
float processPhasor(float& phase, float phaseIncr)
{
phase = wrap(phase + phaseIncr, 1.f);
return phase;
}
float processSmoother(float& outm1, float in)
{
outm1 = (in * 0.01f) + (outm1 * 0.99f);
return outm1;
}
processor Delay (float feedbackLevel)
{
input stream float audioIn;
output stream float audioOut;
void main()
{
float[44100] buffer;
wrap<44100> bufferIdx = 0;
float last;
let delayTime = int ((processor.frequency / clock) * 3.98f); //3.98f to reduce comb filtering
loop
{
let out = buffer[wrap<44100>(bufferIdx - delayTime)];
last = out;
buffer[bufferIdx++] = audioIn + (last * feedbackLevel);
audioOut <- out;
advance();
}
}
}
processor Lead
{
output stream float out;
void main()
{
int[] triggerArray =(1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
int[] pitchArray = (69,67,65,64,67,66,64,62,65,64,62,57,55,60,57);
let samplesPerQuarterNote = int (processor.frequency / clock);
wrap<256> playHead;
wrap<15> pitchIdx;
bool trig = true;
float osc1PhaseIncr;
float osc2PhaseIncr;
float osc1Phasor;
float osc2Phasor;
float lfoPhasor;
float envPhasor;
float envPhaseIncr = float (processor.period * 4.f);
float smootherOutm1;
let lfoPhaseIncr = float (processor.period * 0.8);
int basePitch;
float gate; // hack to get it to start on 2nd loop
loop
{
let noteLength = samplesPerQuarterNote * 1;
loop (noteLength)
{
if(triggerArray[playHead] > 0)
{
if(trig)
{
basePitch = pitchArray.at (pitchIdx++);
envPhasor = 0.f;
trig = false;
}
}
envPhasor = envPhasor + envPhaseIncr;
let env = processSmoother(smootherOutm1, clamp(envPhasor, 0.f, 1.f)) * gate;
let lfoMod = (sin(processPhasor(lfoPhasor, lfoPhaseIncr) * float(twoPi)) * 0.1f) * env;
osc1PhaseIncr = float (std::notes::noteToFrequency (basePitch + lfoMod) * processor.period);
osc2PhaseIncr = float (std::notes::noteToFrequency (basePitch + lfoMod + 0.1f) * processor.period);
out <- 0.15f * pdSawtooth(processPhasor(osc1Phasor, osc1PhaseIncr), 0.9f) * env;
out <- 0.15f * pdSawtooth(processPhasor(osc2Phasor, osc2PhaseIncr), 0.91f) * env;
advance();
}
playHead++;
if(playHead == 0)
gate = 1.f;
trig = true;
}
}
}
processor Bass
{
output stream float out;
void main()
{
int[] arp = (57,57,59,60);
int[] rootNotes = (0,0,7,2,5,5,0,0);
let samplesPerQuarterNote = int (processor.frequency / clock);
float osc1Phasor;
float osc2Phasor;
float lfoPhasor;
wrap<8> rootIdx;
wrap<32> playHead;
loop
{
let noteFrequency = std::notes::noteToFrequency (arp.at(playHead % 4) + rootNotes.at(rootIdx) - 12);
let noteLength = samplesPerQuarterNote;
let osc1PhaseIncr = float (noteFrequency * processor.period);
let osc2PhaseIncr = float (noteFrequency * processor.period * 0.5);
let lfoPhaseIncr = float (0.3 * processor.period);
float env = 1.;
loop (noteLength)
{
out <- (0.3f * pwm(processPhasor(osc1Phasor, osc1PhaseIncr), triangle(processPhasor(lfoPhasor, lfoPhaseIncr)))) * env;
out <- (0.3f * pdSawtooth(processPhasor(osc2Phasor, osc2PhaseIncr), 0.9f));
env = env - (1.f/samplesPerQuarterNote);
advance();
}
playHead++;
if(playHead == 0)
rootIdx++;
}
}
}
}