-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrender.cpp
More file actions
417 lines (322 loc) · 13 KB
/
render.cpp
File metadata and controls
417 lines (322 loc) · 13 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//This file is an extension of the minimoog_complete example provided by Andrew McPherson as part of ECS7012P//
#include <Bela.h>
#include <libraries/Gui/Gui.h>
#include <libraries/GuiController/GuiController.h>
#include <libraries/Midi/Midi.h>
#include <cmath>
#include "ADSR.h"
#include "sawtooth.h"
#include "sine.h"
#include "filter.h"
#include "moogladder.h"
#include "polyBLEPoscillator.h"
// Browser-based GUI to adjust parameters
Gui gui;
GuiController controller;
// Device for handling MIDI messages
Midi gMidi;
// Name of the MIDI port to use. Run 'amidi -l' on the console to see a list.
// Typical values:
// "hw:0,0,0" for a virtual device (from the computer)
// "hw:1,0,0" for a USB device plugged into the Bela board
const char* gMidiPort0 = "hw:1,0,0";
// Audio oscillator
const int kNumOscillators = 3;
Sawtooth gOscillators[3];
float gOscillatorDetune = 0.0;
float gNoteFrequency = 0.0;
// LFO
Sine gVibratoLFO;
float gVibratoDepth = 0.0;
// ADSR envelope and overall amplitude
ADSR gAmplitudeADSR;
float gAmplitude = 1.0;
// Filter parameters
ADSR gFilterADSR;
float gFilterBaseline = 0.0;
float gFilterContour = 0.0;
float gFilterQ = 0.0;
float gFilterTracking = 0.0;
// Handling for multiple MIDI notes and pitchwheel
float gCurrentPitchBend = 0;
const int kMaxActiveNotes = 16;
int gActiveNotes[kMaxActiveNotes];
int gActiveNoteCount = 0;
//VARIABLES ADDED FOR THIS ADVANCED IMPLEMENTATION//
// Moog ladder filter
MoogLadder gLadderFilter;
// Global variable for the filter drive
float gFilterDrive = 1.0;
//polyBLEP oscillators for quasi-bandlimited waveforms
PolyBLEPOscillator gBLEPOscillators[3];
//global variables for implementing what oscillators to use
int gOscillator1Mode = SAW;
int gOscillator2Mode = SAW;
int gOscillator3Mode = SAW;
// Variables for implementing portamento/glide
float gPortamentoGlobalTime = 0.05;
int gPortamentoCurrentSample = 0;
float gPreviousFrequency = 0;
// Variable to hold the previous q value to check if a slider value has changed.
// To save efficiency on ladder filter as setting Q requires polynomial calculation
float gFilterPreviousQ = 0.0;
bool setup(BelaContext *context, void *userData)
{
// Initialise the MIDI device
if(gMidi.readFrom(gMidiPort0) < 0) {
rt_printf("Unable to read from MIDI port %s\n", gMidiPort0);
return false;
}
gMidi.writeTo(gMidiPort0);
gMidi.enableParser(true);
// Check if analog channels are enabled in 8 channel configuration, which
// produces twice as many audio frames as analog frames
if(context->analogFrames != context->audioFrames / 2) {
rt_printf("Error: this example needs analog enabled with 8 channels\n");
return false;
}
// Initialise the oscillator
for(int i = 0; i < kNumOscillators; i++){
gOscillators[i].setSampleRate(context->audioSampleRate);
gBLEPOscillators[i].setSampleRate(context->audioSampleRate);
}
// Set an inital mode for each oscillator
gBLEPOscillators[0].setMode(gOscillator1Mode);
gBLEPOscillators[1].setMode(gOscillator2Mode);
gBLEPOscillators[2].setMode(gOscillator3Mode);
// Initialise the ADSR, filter and LFO
gAmplitudeADSR.setSampleRate(context->audioSampleRate);
gFilterADSR.setSampleRate(context->audioSampleRate);
gLadderFilter.setSampleRate(context->audioSampleRate);
gVibratoLFO.setSampleRate(context->audioSampleRate);
// Set up the GUI
gui.setup(context->projectName);
controller.setup(&gui, "Controller");
// Arguments: name, minimum, maximum, increment, default value
controller.addSlider("Oscillator detune", 0.002, 0, 0.01, 0);
controller.addSlider("VCA Attack time", 0.01, 0.001, 0.1, 0);
controller.addSlider("VCA Decay time", 0.05, 0.01, 0.3, 0);
controller.addSlider("VCA Sustain level", 0.3, 0, 1, 0);
controller.addSlider("VCA Release time", 0.2, 0.001, 2, 0);
controller.addSlider("Filter cutoff", 1000, 50, 5000, 0);
controller.addSlider("Filter contour", 2000, 0, 5000, 0);
controller.addSlider("Filter Q", 0.0, 0.0, 4.0, 0);
controller.addSlider("Filter tracking", 0.33, 0, 1, 0);
controller.addSlider("Filter Attack time", 0.03, 0.001, 0.2, 0);
controller.addSlider("Filter Decay time", 0.2, 0.01, 1.0, 0);
controller.addSlider("Filter Sustain level", 0.3, 0, 1, 0);
controller.addSlider("Filter Release time", 0.2, 0.001, 2, 0);
controller.addSlider("Vibrato frequency", 5, 1, 10, 0);
controller.addSlider("Filter Drive", 1.0, 0.1, 5.0, 0);
controller.addSlider("Oscillator 1 Mode", 1, 0, 2, 0);
controller.addSlider("Oscillator 2 Mode", 1, 0, 2, 0);
controller.addSlider("Oscillator 3 Mode", 1, 0, 2, 0);
controller.addSlider("Portamento/Glide Time", 0.000, 0.001, 0.3, 0);
return true;
}
// Update the frequency of the detuned oscillators
void updateOscillators(float frequency)
{
gOscillators[0].setFrequency(frequency);
gOscillators[1].setFrequency(frequency * (1.0 + gOscillatorDetune));
gOscillators[2].setFrequency(frequency * (1.0 - gOscillatorDetune));
gBLEPOscillators[0].setFrequency(frequency);
gBLEPOscillators[1].setFrequency(frequency * (1.0 + gOscillatorDetune));
gBLEPOscillators[2].setFrequency(frequency * (1.0 - gOscillatorDetune));
}
// Calculate the frequency based on note and pitch bend
float calculateFrequency(int noteNumber, float pitchBend)
{
float compositeNote = noteNumber + pitchBend;
return powf(2.0, (compositeNote - 69)/12.0) * 440.0;
}
// MIDI note on received
void noteOn(int noteNumber, int velocity)
{
// Check if we have any note slots left
if(gActiveNoteCount < kMaxActiveNotes) {
// Keep track of this note, then play it
gActiveNotes[gActiveNoteCount++] = noteNumber;
//Set the current frequency as the previous frequency for Portamento
gPreviousFrequency = gNoteFrequency;
//Reset the current portamento sample
gPortamentoCurrentSample = 0;
// Map note number to frequency
gNoteFrequency = calculateFrequency(noteNumber, gCurrentPitchBend);
// Map velocity to amplitude on a decibel scale
// float decibels = map(velocity, 1, 127, -40, 0);
// gAmplitude = powf(10.0, decibels / 20.0);
// For this Minimoog-style emulation, we don't have a velociy-sensitive keyboard
gAmplitude = 1.0;
// Start the ADSR, but only if this was the first note
if(gActiveNoteCount == 1) {
gAmplitudeADSR.trigger();
gFilterADSR.trigger();
}
}
}
// MIDI note off received
void noteOff(int noteNumber)
{
bool activeNoteChanged = false;
// Go through all the active notes and remove any with this number
for(int i = gActiveNoteCount - 1; i >= 0; i--) {
if(gActiveNotes[i] == noteNumber) {
// Found a match: is it the most recent note?
if(i == gActiveNoteCount - 1) {
activeNoteChanged = true;
}
// Move all the later notes back in the list
for(int j = i; j < gActiveNoteCount - 1; j++) {
gActiveNotes[j] = gActiveNotes[j + 1];
}
gActiveNoteCount--;
}
}
if(gActiveNoteCount == 0) {
// No notes left: stop the ADSR
gAmplitudeADSR.release();
gFilterADSR.release();
}
else if(activeNoteChanged) {
// Update the frequency but don't retrigger
int mostRecentNote = gActiveNotes[gActiveNoteCount - 1];
//Update the previous note frequency for portamento
gPreviousFrequency = gNoteFrequency;
//Reset the current portamento sample
gPortamentoCurrentSample = 0;
gNoteFrequency = calculateFrequency(mostRecentNote, gCurrentPitchBend);
}
}
// Handle pitch wheel messages
void pitchWheel(int lsb, int msb)
{
// Pitch bend message has two data bytes: first is the least significant
// bits, then the most significant bits. Put these together into a 14-bit
// value.
int wheelValue = lsb + (msb << 7);
// The result is a value between 0 and 16383, with a centre value of
// 8192 corresponding to no bend, and either end of the scale corresponding
// to 2 semitones of difference. Calculate the fractional number of semitones.
float bendSemitones = (wheelValue - 8192) * 2.0 / 8192;
// Update the frequency, if a note is playing
if(gActiveNoteCount > 0) {
gNoteFrequency = calculateFrequency(gActiveNotes[gActiveNoteCount - 1], bendSemitones);
}
// Save the bend value for future notes
gCurrentPitchBend = bendSemitones;
}
void render(BelaContext *context, void *userData)
{
// Get the slider values from the GUI
gOscillatorDetune = controller.getSliderValue(0);
gAmplitudeADSR.attackTime = controller.getSliderValue(1);
gAmplitudeADSR.decayTime = controller.getSliderValue(2);
gAmplitudeADSR.sustainLevel = controller.getSliderValue(3);
gAmplitudeADSR.releaseTime = controller.getSliderValue(4);
gFilterBaseline = controller.getSliderValue(5);
gFilterContour = controller.getSliderValue(6);
gFilterQ = controller.getSliderValue(7);
gFilterTracking = controller.getSliderValue(8);
gFilterADSR.attackTime = controller.getSliderValue(9);
gFilterADSR.decayTime = controller.getSliderValue(10);
gFilterADSR.sustainLevel = controller.getSliderValue(11);
gFilterADSR.releaseTime = controller.getSliderValue(12);
gVibratoLFO.setFrequency(controller.getSliderValue(13));
gFilterDrive = controller.getSliderValue(14);
// Collect slider values for setting oscillator mode
gOscillator1Mode = controller.getSliderValue(15);
gOscillator2Mode = controller.getSliderValue(16);
gOscillator3Mode = controller.getSliderValue(17);
gPortamentoGlobalTime = controller.getSliderValue(18);
// This only needs to be updated when the slider changes
if(gFilterQ != gFilterPreviousQ){
gLadderFilter.setQ(gFilterQ);
}
gLadderFilter.setDrive(gFilterDrive);
gBLEPOscillators[0].setMode(gOscillator1Mode);
gBLEPOscillators[1].setMode(gOscillator2Mode);
gBLEPOscillators[2].setMode(gOscillator3Mode);
// At the beginning of each callback, look for available MIDI
// messages that have come in since the last block
while(gMidi.getParser()->numAvailableMessages() > 0) {
MidiChannelMessage message;
message = gMidi.getParser()->getNextChannelMessage();
message.prettyPrint(); // Print the message data
// A MIDI "note on" message type might actually hold a real
// note onset (e.g. key press), or it might hold a note off (key release).
// The latter is signified by a velocity of 0.
if(message.getType() == kmmNoteOn) {
int noteNumber = message.getDataByte(0);
int velocity = message.getDataByte(1);
// Velocity of 0 is really a note off
if(velocity == 0) {
noteOff(noteNumber);
}
else {
noteOn(noteNumber, velocity);
}
rt_printf("Frequency: %f, Amplitude: %f\n", gNoteFrequency, gAmplitude);
}
else if(message.getType() == kmmNoteOff) {
// We can also encounter the "note off" message type which is the same
// as "note on" with a velocity of 0.
int noteNumber = message.getDataByte(0);
noteOff(noteNumber);
}
else if(message.getType() == kmmPitchBend) {
int lsb = message.getDataByte(0);
int msb = message.getDataByte(1);
pitchWheel(lsb, msb);
}
else if(message.getType() == kmmControlChange) {
int controller = message.getDataByte(0);
int value = message.getDataByte(1);
if(controller == 1) { // CC1 is the mod wheel
gVibratoDepth = map(value, 0, 127, 0, 0.05);
}
}
}
// Now calculate the audio for this block
for(unsigned int n = 0; n < context->audioFrames; n++) {
float value = 0;
// Run the oscillator whenever a note is on
if(gAmplitudeADSR.isActive()) {
// Calculate the oscillator frequencies including the effect of vibrato
//float frequency = gNoteFrequency + gVibratoDepth * gNoteFrequency * gVibratoLFO.nextSample();
// Calculate the start and end frequency for the portamento including the effect of vibrato
float startFrequency = gPreviousFrequency + gVibratoDepth * gPreviousFrequency * gVibratoLFO.nextSample();
float endFrequency = gNoteFrequency + gVibratoDepth * gNoteFrequency * gVibratoLFO.nextSample();
//Portamento Section
float outFrequency = endFrequency*(1-exp(-(gPortamentoCurrentSample)/(gPortamentoGlobalTime*context->audioSampleRate)))
+ startFrequency*exp(-(gPortamentoCurrentSample)/(gPortamentoGlobalTime*context->audioSampleRate));
gPortamentoCurrentSample++;
//Set frequency of oscillators including detune
gBLEPOscillators[0].setFrequency(outFrequency);
gBLEPOscillators[1].setFrequency(outFrequency * (1.0 + gOscillatorDetune));
gBLEPOscillators[2].setFrequency(outFrequency * (1.0 - gOscillatorDetune));
// Calculate the oscillator output
value = 0;
for(int i = 0; i < kNumOscillators; i++){
value += gBLEPOscillators[i].nextSample();
}
value /= (float)kNumOscillators;
// Calculate the filter cutoff frequency based on the
// ADSR and other filter parameters
float filterCutoff = gFilterBaseline + gFilterContour * gFilterADSR.getNextValue();
// Implement filter tracking off the cutoff frequency based on the note frequency
filterCutoff += gFilterTracking * gNoteFrequency;
//Update the filter cutoff
gLadderFilter.setFrequency(filterCutoff);
// Filter the oscillator output
value = gLadderFilter.process(value);
// Now apply the variable gain
value *= 0.5 * gAmplitudeADSR.getNextValue();
}
for(unsigned int ch = 0; ch < context->audioOutChannels; ++ch)
audioWrite(context, n, ch, value);
}
}
void cleanup(BelaContext *context, void *userData)
{
}