forked from obsproject/obs-vst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VSTPlugin.cpp
264 lines (214 loc) · 6.82 KB
/
VSTPlugin.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
/*****************************************************************************
Copyright (C) 2016-2017 by Colin Edwards.
Additional Code Copyright (C) 2016-2017 by c3r1c3 <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include "headers/VSTPlugin.h"
VSTPlugin::VSTPlugin(obs_source_t *sourceContext) : sourceContext{sourceContext}
{
int numChannels = VST_MAX_CHANNELS;
int blocksize = BLOCK_SIZE;
inputs = (float **)malloc(sizeof(float **) * numChannels);
outputs = (float **)malloc(sizeof(float **) * numChannels);
for (int channel = 0; channel < numChannels; channel++) {
inputs[channel] = (float *)malloc(sizeof(float *) * blocksize);
outputs[channel] = (float *)malloc(sizeof(float *) * blocksize);
}
}
void VSTPlugin::loadEffectFromPath(std::string path)
{
if (this->pluginPath.compare(path) != 0) {
closeEditor();
unloadEffect();
}
if (!effect) {
pluginPath = path;
effect = loadEffect();
if (!effect) {
// TODO: alert user of error
blog(LOG_WARNING,
"VST Plug-in: Can't load "
"effect!");
return;
}
// Check plug-in's magic number
// If incorrect, then the file either was not loaded properly,
// is not a real VST plug-in, or is otherwise corrupt.
if (effect->magic != kEffectMagic) {
blog(LOG_WARNING, "VST Plug-in's magic number is bad");
return;
}
effect->dispatcher(effect, effGetEffectName, 0, 0, effectName, 0);
effect->dispatcher(effect, effGetVendorString, 0, 0, vendorString, 0);
effect->dispatcher(effect, effOpen, 0, 0, nullptr, 0.0f);
// Set some default properties
size_t sampleRate = audio_output_get_sample_rate(obs_get_audio());
effect->dispatcher(effect, effSetSampleRate, 0, 0, nullptr, sampleRate);
int blocksize = BLOCK_SIZE;
effect->dispatcher(effect, effSetBlockSize, 0, blocksize, nullptr, 0.0f);
effect->dispatcher(effect, effMainsChanged, 0, 1, nullptr, 0);
effectReady = true;
if (openInterfaceWhenActive) {
openEditor();
}
}
}
void silenceChannel(float **channelData, int numChannels, long numFrames)
{
for (int channel = 0; channel < numChannels; ++channel) {
for (long frame = 0; frame < numFrames; ++frame) {
channelData[channel][frame] = 0.0f;
}
}
}
obs_audio_data *VSTPlugin::process(struct obs_audio_data *audio)
{
if (effect && effectReady) {
silenceChannel(outputs, VST_MAX_CHANNELS, audio->frames);
float *adata[VST_MAX_CHANNELS];
for (size_t d = 0; d < VST_MAX_CHANNELS; d++) {
if (audio->data[d] != NULL) {
adata[d] = (float *)audio->data[d];
} else {
adata[d] = inputs[d];
}
};
effect->processReplacing(effect, adata, outputs, audio->frames);
for (size_t c = 0; c < VST_MAX_CHANNELS; c++) {
if (audio->data[c]) {
for (size_t i = 0; i < audio->frames; i++) {
adata[c][i] = outputs[c][i];
}
}
}
}
return audio;
}
void VSTPlugin::unloadEffect()
{
effectReady = false;
if (effect) {
effect->dispatcher(effect, effMainsChanged, 0, 0, nullptr, 0);
effect->dispatcher(effect, effClose, 0, 0, nullptr, 0.0f);
}
effect = nullptr;
unloadLibrary();
}
void VSTPlugin::openEditor()
{
if (effect && !editorWidget) {
editorWidget = new EditorWidget(nullptr, this);
editorWidget->buildEffectContainer(effect);
editorWidget->setWindowTitle(QString("%1 - %2").arg(vendorString, effectName));
editorWidget->show();
}
}
void VSTPlugin::closeEditor()
{
if (effect) {
effect->dispatcher(effect, effEditClose, 0, 0, nullptr, 0);
}
if (editorWidget) {
editorWidget->close();
delete editorWidget;
editorWidget = nullptr;
}
}
intptr_t VSTPlugin::hostCallback(AEffect *effect, int32_t opcode, int32_t index, intptr_t value, void *ptr, float opt)
{
UNUSED_PARAMETER(effect);
UNUSED_PARAMETER(ptr);
UNUSED_PARAMETER(opt);
intptr_t result = 0;
// Filter idle calls...
bool filtered = false;
if (opcode == audioMasterIdle) {
static bool wasIdle = false;
if (wasIdle)
filtered = true;
else {
blog(LOG_WARNING,
"VST Plug-in: Future idle calls "
"will not be displayed!");
wasIdle = true;
}
}
switch (opcode) {
case audioMasterSizeWindow:
// index: width, value: height
if (editorWidget) {
editorWidget->handleResizeRequest(index, value);
}
return 0;
}
return result;
}
std::string VSTPlugin::getChunk()
{
if (!effect) {
return "";
}
if (effect->flags & effFlagsProgramChunks) {
void *buf = nullptr;
intptr_t chunkSize = effect->dispatcher(effect, effGetChunk, 1, 0, &buf, 0.0);
QByteArray data = QByteArray((char *)buf, chunkSize);
return QString(data.toBase64()).toStdString();
} else {
std::vector<float> params;
for (int i = 0; i < effect->numParams; i++) {
float parameter = effect->getParameter(effect, i);
params.push_back(parameter);
}
const char *bytes = reinterpret_cast<const char *>(¶ms[0]);
QByteArray data = QByteArray(bytes, sizeof(float) * params.size());
std::string encoded = QString(data.toBase64()).toStdString();
return encoded;
}
}
void VSTPlugin::setChunk(std::string data)
{
if (!effect) {
return;
}
if (effect->flags & effFlagsProgramChunks) {
QByteArray base64Data = QByteArray(data.c_str(), data.length());
QByteArray chunkData = QByteArray::fromBase64(base64Data);
void * buf = nullptr;
buf = chunkData.data();
effect->dispatcher(effect, effSetChunk, 1, chunkData.length(), buf, 0);
} else {
QByteArray base64Data = QByteArray(data.c_str(), data.length());
QByteArray paramData = QByteArray::fromBase64(base64Data);
const char * p_chars = paramData.data();
const float *p_floats = reinterpret_cast<const float *>(p_chars);
int size = paramData.length() / sizeof(float);
std::vector<float> params(p_floats, p_floats + size);
if (params.size() != effect->numParams) {
return;
}
for (int i = 0; i < effect->numParams; i++) {
effect->setParameter(effect, i, params[i]);
}
}
}
void VSTPlugin::setProgram(const int programNumber)
{
if (programNumber < effect->numPrograms) {
effect->dispatcher(effect, effSetProgram, 0, programNumber, NULL, 0.0f);
} else {
blog(LOG_ERROR, "Failed to load program, number was outside possible program range.");
}
}
int VSTPlugin::getProgram()
{
return effect->dispatcher(effect, effGetProgram, 0, 0, NULL, 0.0f);
}