-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathVAx6.cpp
201 lines (161 loc) · 5.78 KB
/
VAx6.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
// Copyright (C)2021 - E.Heidt
//
// Author: E.Heidt ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// See http://creativecommons.org/licenses/MIT/ for more information.
//
#include "../squares-and-circles-api.h"
// #include "../lib/plaits/dsp/engine/virtual_analog_engine.h"
// no virtual functions hack
#define PLAITS_DSP_ENGINE_VIRTUAL_ANALOG_ENGINE_H_
#include "plaits/dsp/engine/engine.h"
#include "plaits/dsp/oscillator/variable_saw_oscillator.h"
#include "plaits/dsp/oscillator/variable_shape_oscillator.h"
#define VA_VARIANT 2
namespace plaits
{
class VirtualAnalogEngine
{
public:
VirtualAnalogEngine() {}
~VirtualAnalogEngine() {}
void Init(stmlib::BufferAllocator *allocator);
void Reset();
void LoadUserData(const uint8_t *user_data) {}
void Render(const EngineParameters ¶meters,
float *out,
float *aux,
size_t size,
bool *already_enveloped);
private:
float ComputeDetuning(float detune) const;
VariableShapeOscillator primary_;
VariableShapeOscillator auxiliary_;
VariableShapeOscillator sync_;
VariableSawOscillator variable_saw_;
float auxiliary_amount_;
float xmod_amount_;
float *temp_buffer_;
DISALLOW_COPY_AND_ASSIGN(VirtualAnalogEngine);
};
} // namespace plaits
#include "../lib/plaits/dsp/envelope.h"
#include "../lib/stmlib/algorithms/voice_allocator.h"
#include "../lib/stmlib/utils/random.h"
#include "stmlib/dsp/units.cc"
#include "stmlib/utils/random.cc"
#include "../lib/plaits/dsp/engine/virtual_analog_engine.cc"
plaits::VirtualAnalogEngine voice[6] = {};
uint8_t buffer[LEN_OF(voice) * plaits::kMaxBlockSize * sizeof(float)] = {};
plaits::EngineParameters parameters[LEN_OF(voice)] = {};
plaits::LPGEnvelope lpg[LEN_OF(voice)] = {};
bool enveloped[LEN_OF(voice)] = {};
stmlib::VoiceAllocator<LEN_OF(voice)> allocator;
float timbre = 0.5f;
float morph = 0.5f;
float harmonics = 0.5f;
float pitch = 0;
float pitch_bend = 0;
float decay = 0.5f;
float hf = 1.f;
float pan[LEN_OF(voice)] = {};
float stereo = 0.5f;
float voiceBuff[FRAME_BUFFER_SIZE] = {};
float dummy[FRAME_BUFFER_SIZE] = {};
void engine::setup()
{
allocator.Init();
allocator.set_size(LEN_OF(voice));
engine::addParam(V_OCT, &pitch);
engine::addParam("Harmo", &harmonics);
engine::addParam("Timbre", &timbre);
engine::addParam("Morph", &morph);
engine::addParam("Decay", &decay);
engine::addParam("Stereo", &stereo);
engine::setMode(ENGINE_MODE_MIDI_IN);
stmlib::BufferAllocator buffAllocator;
buffAllocator.Init(buffer, sizeof(buffer));
for (size_t i = 0; i < LEN_OF(voice); i++)
{
lpg[i].Init();
voice[i].Init(&buffAllocator);
}
}
void engine::process()
{
auto polyBuffL = engine::outputBuffer<0>();
auto polyBuffR = engine::outputBuffer<1>();
std::fill_n(polyBuffL, FRAME_BUFFER_SIZE, 0);
std::fill_n(polyBuffR, FRAME_BUFFER_SIZE, 0);
const float short_decay = (200.0f * FRAME_BUFFER_SIZE) / SAMPLE_RATE *
stmlib::SemitonesToRatio(-96.0f * decay);
const float decay_tail = (20.0f * FRAME_BUFFER_SIZE) / SAMPLE_RATE *
stmlib::SemitonesToRatio(-72.0f * decay + 12.0f * hf) -
short_decay;
for (size_t i = 0; i < LEN_OF(voice); i++)
{
auto p = parameters[i];
p.note += (pitch * 12.f) + pitch_bend;
p.timbre = timbre;
p.morph = morph;
p.harmonics = harmonics;
voice[i].Render(p, voiceBuff, dummy, FRAME_BUFFER_SIZE, &enveloped[i]);
lpg[i].ProcessPing(0.5f, short_decay, decay_tail, hf);
float l = cosf(pan[i] * M_PI / 2);
float r = sinf(pan[i] * M_PI / 2);
for (int s = 0; s < FRAME_BUFFER_SIZE; s++)
{
polyBuffL[s] += (voiceBuff[s] * lpg[i].gain()) * l;
polyBuffR[s] += (voiceBuff[s] * lpg[i].gain()) * r;
}
parameters[i].trigger = plaits::TriggerState::TRIGGER_LOW;
}
}
void engine::draw()
{
}
void engine::onMidiNote(uint8_t key, uint8_t velocity) // NoteOff: velocity == 0
{
if (velocity > 0)
{
auto ni = allocator.NoteOn(key);
parameters[ni].trigger = plaits::TriggerState::TRIGGER_RISING_EDGE;
parameters[ni].note = key;
parameters[ni].accent = velocity > 100;
pan[ni] = 0.5f + stereo * (stmlib::Random::GetFloat() - 0.5f);
lpg[ni].Trigger();
}
else
{
allocator.NoteOff(key);
}
}
void engine::onMidiPitchbend(int16_t pitch)
{
pitch_bend = ((float)pitch / 8192) * 12;
}
void engine::onMidiCC(uint8_t ccc, uint8_t value)
{
// nothing implemented..
}
void engine::onMidiSysex(uint8_t byte)
{
}