-
Notifications
You must be signed in to change notification settings - Fork 0
/
ewisynth.cpp
386 lines (347 loc) · 10.8 KB
/
ewisynth.cpp
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
/* include libs */
#include "lv2.h"
#include "curves.h"
#include "variableshapeoscillator.h"
#include "polyfotz.h"
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <lv2/atom/atom.h>
#include <lv2/atom/util.h>
#include <lv2/core/lv2_util.h>
#include <lv2/midi/midi.h>
#include <lv2/urid/urid.h>
#include <math.h>
#include <sys/types.h>
#define MAX_POLYPHONY 16
enum ControlPorts {
CONTROL_TUNE = 0,
CONTROL_OCTAVE = 1,
CONTROL_TRANSPOSE = 2,
CONTROL_GAIN = 3,
CONTROL_LEVEL = 4,
CONTROL_SLEWTIME = 5,
CONTROL_ARPRANGE = 6,
CONTROL_ARPTIME = 7,
CONTROL_POLYPHONY = 8,
CONTROL_DETUNE = 9,
CONTROL_BANK = 10,
CONTROL_VOICING = 11,
CONTROL_ROTATOR = 12,
CONTROL_PHASE = 13,
CONTROL_SHAPE = 14,
CONTROL_NR = 15
};
enum PortGroups {
PORT_MIDI_IN = 0,
PORT_L_AUDIO_OUT = 1,
PORT_R_AUDIO_OUT = 2,
PORT_CONTROL = 3,
PORT_NR = 4
};
struct Urids {
LV2_URID midi_MidiEvent;
};
/* class definition */
class EwiSynth {
private:
const LV2_Atom_Sequence *midi_in_ptr;
float *l_audio_out_ptr;
float *r_audio_out_ptr;
const float *control_ptr[CONTROL_NR];
LV2_URID_Map *map;
Urids urids;
double rate;
float currFrequency = 440.f;
float targetFrequency = 440.f;
float realFrequency = 440.f;
float freqRatio() { return currFrequency / targetFrequency; }
float slewSteps = 0.f;
float slewStepsRemaining = 0.f;
float exponent() { return (slewSteps > 0) ? slewStepsRemaining / slewSteps : 1.f; }
float pitchFactor() { return powf(freqRatio(), exponent()); }
float currBendFactor = 1.f;
float currPulseWidth = .5f;
float currPressure = .0f;
float currShape = 1.f;
float lastPhase = 0.f;
VariableShapeOscillator SAWosc[MAX_POLYPHONY];
VariableShapeOscillator SQRosc[MAX_POLYPHONY];
void handleNoteOn(uint8_t note);
void handlePressure(const uint8_t pressure);
void handlePitchbend(const uint16_t pitchbend);
void handleController(const uint8_t controller, const uint8_t value);
void updateControls();
PolyFotz polyfotz;
Curves curve;
struct StereoPair {
float sqr_l = 0.f;
float saw_r = 0.f;
};
StereoPair sumOscillators();
float waveshaper(float sample) {
return 2/(1+exp(-2*sample))-1;
}
struct Arpeggiator {
bool isActive = false;
uint8_t range = 0;
uint8_t index = 1;
int8_t indexIncrement = 1;
int arpStepsInSamples = 8000;
int arpStepsRemaining = arpStepsInSamples;
void advance(int voicingSize) {
if (!isActive) return;
if (index >= range) {
index = range;
indexIncrement = -1;
}
if (index <= 0) {
index = 0;
indexIncrement = 1;
}
if (arpStepsInSamples > 0) {
if (arpStepsRemaining > 0) {
arpStepsRemaining--;
} else {
arpStepsRemaining = arpStepsInSamples;
index += indexIncrement;
}
}
}
int getIndex(int voicingSize) {
return index % voicingSize;
}
int getOctave(int voicingSize) {
int octave = index / voicingSize;
if (range > 0 || arpStepsInSamples > 0) octave -= 1;
return octave;
}
} arpeggiator;
public:
EwiSynth(const double sample_rate, const LV2_Feature *const *features);
void connectPort(const uint32_t port, void *data_location);
void run(const uint32_t sample_count);
};
EwiSynth::EwiSynth(const double sample_rate, const LV2_Feature *const *features)
: midi_in_ptr(nullptr), l_audio_out_ptr(nullptr), r_audio_out_ptr(nullptr),
control_ptr{nullptr}, map(nullptr), rate(sample_rate) {
const char *missing =
lv2_features_query(features, LV2_URID__map, &map, true, NULL);
if (missing)
throw;
polyfotz.Init(MAX_POLYPHONY);
for (int i = 0; i < MAX_POLYPHONY; i++) {
SAWosc[i].Init(sample_rate);
SQRosc[i].Init(sample_rate);
SAWosc[i].SetWaveshape(0);
SQRosc[i].SetWaveshape(currShape);
}
urids.midi_MidiEvent = map->map(map->handle, LV2_MIDI__MidiEvent);
}
void EwiSynth::connectPort(const uint32_t port, void *data_location) {
switch (port) {
case PORT_MIDI_IN:
midi_in_ptr = (const LV2_Atom_Sequence *)data_location;
break;
case PORT_L_AUDIO_OUT:
l_audio_out_ptr = (float *)data_location;
break;
case PORT_R_AUDIO_OUT:
r_audio_out_ptr = (float *)data_location;
break;
default:
if (port < PORT_CONTROL + CONTROL_NR) {
control_ptr[port - PORT_CONTROL] = (const float *)data_location;
}
break;
}
}
void EwiSynth::run(const uint32_t sample_count) {
// /* check if all ports connected */
// if ((!midi_in_ptr)) return;
//
// for (int i = 0; i < CONTROL_NR; ++i) {
// if (!control_ptr[i])
// return;
// }
updateControls();
uint32_t offset = 0;
/* analyze incomming MIDI data */
LV2_ATOM_SEQUENCE_FOREACH(midi_in_ptr, ev) {
for (int i = offset; i < ev->time.frames; i++) {
const StereoPair outputs = sumOscillators();
l_audio_out_ptr[i] = outputs.sqr_l;
r_audio_out_ptr[i] = outputs.saw_r;
}
if (ev->body.type == urids.midi_MidiEvent) {
const uint8_t *const msg = (const uint8_t *)(ev + 1);
const uint8_t typ = lv2_midi_message_type(msg);
switch (typ) {
case LV2_MIDI_MSG_NOTE_ON:
handleNoteOn(msg[1]);
break;
case LV2_MIDI_MSG_CHANNEL_PRESSURE:
handlePressure(msg[1]);
break;
case LV2_MIDI_MSG_CONTROLLER:
handleController(msg[1], msg[2]);
break;
case LV2_MIDI_MSG_BENDER:
handlePitchbend((msg[2] << 7) | msg[1]);
break;
default:
break;
}
}
offset = (uint32_t)ev->time.frames;
}
for (int i = offset; i < sample_count; i++) {
const StereoPair outputs = sumOscillators();
l_audio_out_ptr[i] = outputs.sqr_l;
r_audio_out_ptr[i] = outputs.saw_r;
}
}
EwiSynth::StereoPair EwiSynth::sumOscillators() {
StereoPair out;
int poly_ = *control_ptr[CONTROL_POLYPHONY];
float delta = 0.f;
if (*control_ptr[CONTROL_PHASE] != lastPhase) {
delta = lastPhase - *control_ptr[CONTROL_PHASE];
lastPhase = *control_ptr[CONTROL_PHASE];
};
int voicingSize = polyfotz.getActiveVoicingSize();
realFrequency = polyfotz.getFrequency(0) * pitchFactor();
for (int i = 0; i < poly_; i++) {
float freq;
if (arpeggiator.isActive) {
freq = polyfotz.getFrequency(arpeggiator.getIndex(voicingSize)) * pow(2, -arpeggiator.getOctave(voicingSize));
for (int j = 0; j < voicingSize; j++) {
SAWosc[j+1].SetFreq(polyfotz.getFrequency(j));
SQRosc[j+1].SetFreq(polyfotz.getFrequency(j));
SAWosc[j+1].SetPW(currPulseWidth);
if (delta != 0.f) SQRosc[j+1].OffsetPhase(delta);
if (currShape == 1.f) {
SQRosc[j+1].SetWaveshape( 1.5f - currPulseWidth );
SQRosc[j+1].SetPW(.5f);
} else {
SQRosc[j+1].SetWaveshape(currShape);
SQRosc[j+1].SetPW(currPulseWidth);
}
out.sqr_l +=
SQRosc[j+1].Process() / voicingSize * *control_ptr[CONTROL_GAIN] * currPressure;
out.saw_r +=
SAWosc[j+1].Process() / voicingSize * *control_ptr[CONTROL_GAIN] * currPressure;
}
} else {
freq = polyfotz.getFrequency(i) * pitchFactor();
}
SAWosc[i].SetFreq(freq);
SQRosc[i].SetFreq(freq);
SAWosc[i].SetPW(currPulseWidth);
if (delta != 0.f) SQRosc[i].OffsetPhase(delta);
if (currShape == 1.f) {
SQRosc[i].SetWaveshape( 1.5f - currPulseWidth );
SQRosc[i].SetPW(.5f);
} else {
SQRosc[i].SetWaveshape(currShape);
SQRosc[i].SetPW(currPulseWidth);
}
out.sqr_l +=
SQRosc[i].Process() / poly_ * *control_ptr[CONTROL_GAIN] * currPressure;
out.saw_r +=
SAWosc[i].Process() / poly_ * *control_ptr[CONTROL_GAIN] * currPressure;
}
out.sqr_l = waveshaper(out.sqr_l) * *control_ptr[CONTROL_LEVEL];
out.saw_r = waveshaper(out.saw_r) * *control_ptr[CONTROL_LEVEL];
(slewStepsRemaining > 0) ? slewStepsRemaining-- : currFrequency = targetFrequency;
arpeggiator.advance(voicingSize);
return out;
}
void EwiSynth::updateControls() {
slewSteps = *control_ptr[CONTROL_SLEWTIME];
if (*control_ptr[CONTROL_POLYPHONY] == 1 && polyfotz.isPitchbendNegative()) {
arpeggiator.range = *control_ptr[CONTROL_ARPRANGE];
arpeggiator.arpStepsInSamples = *control_ptr[CONTROL_ARPTIME];
arpeggiator.isActive = true;
} else {
arpeggiator.range = 0;
arpeggiator.index = 0;
arpeggiator.isActive = false;
}
polyfotz.setTranspose(*control_ptr[CONTROL_TRANSPOSE]);
polyfotz.setOctave(*control_ptr[CONTROL_OCTAVE]);
polyfotz.setTune(*control_ptr[CONTROL_TUNE]);
polyfotz.setBank(*control_ptr[CONTROL_BANK]);
polyfotz.setVoicing(*control_ptr[CONTROL_VOICING]);
polyfotz.setRotator(*control_ptr[CONTROL_ROTATOR]);
polyfotz.setPolyphony(*control_ptr[CONTROL_POLYPHONY]);
polyfotz.setDetune(*control_ptr[CONTROL_DETUNE]);
if (*control_ptr[CONTROL_SHAPE] != currShape) currShape = *control_ptr[CONTROL_SHAPE];
}
void EwiSynth::handleNoteOn(const uint8_t note) {
currFrequency = realFrequency;
polyfotz.setNote(note);
targetFrequency = polyfotz.getFrequency(0);
slewStepsRemaining = slewSteps;
polyfotz.updateRotator();
}
void EwiSynth::handlePressure(const uint8_t pressure) {
currPressure = curve.apply((float)pressure / 128.f);
currPulseWidth = currPressure / 2.f + .5f; // limit pulse width to .5 - 1.
}
void EwiSynth::handlePitchbend(const uint16_t pitchbend) {
polyfotz.setPitchbend(pitchbend); // 2^( ((pitchbend - 8192) / 8192 * bendrange = 2 / max_pitchbend = 16383) / 12 )
}
void EwiSynth::handleController(const uint8_t controller, const uint8_t value) {
switch (controller) {
default:
break;
}
}
/* internal core methods */
static LV2_Handle instantiate(const struct LV2_Descriptor *descriptor,
double sample_rate, const char *bundle_path,
const LV2_Feature *const *features) {
EwiSynth *m = new EwiSynth(sample_rate, features);
return m;
}
static void connect_port(LV2_Handle instance, uint32_t port,
void *data_location) {
EwiSynth *m = (EwiSynth *)instance;
if (m)
m->connectPort(port, data_location);
}
static void activate(LV2_Handle instance) { /* not needed here */
}
static void run(LV2_Handle instance, uint32_t sample_count) {
EwiSynth *m = (EwiSynth *)instance;
if (m)
m->run(sample_count);
}
static void deactivate(LV2_Handle instance) { /* not needed here */
}
static void cleanup(LV2_Handle instance) {
EwiSynth *m = (EwiSynth *)instance;
if (m)
delete m;
}
static const void *extension_data(const char *uri) { return NULL; }
/* descriptor */
static LV2_Descriptor const descriptor = {
"https://github.com/dingodoppelt/ewisynth",
instantiate,
connect_port,
activate,
run,
deactivate /* or NULL */,
cleanup,
extension_data /* or NULL */
};
/* interface */
LV2_SYMBOL_EXPORT const LV2_Descriptor *lv2_descriptor(uint32_t index) {
if (index == 0)
return &descriptor;
else
return NULL;
}