forked from obsproject/obs-vst
-
Notifications
You must be signed in to change notification settings - Fork 3
/
VSTPlugin.cpp
421 lines (328 loc) · 11.8 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
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
417
418
419
420
421
/*****************************************************************************
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 <vector>
#include "headers/VSTPlugin.h"
#include "win/VstWinDefs.h"
#include "headers/grpc_vst_communicatorClient.h"
#define CBASE64_IMPLEMENTATION
#include "cbase64.h"
#ifdef WIN32
#include <cstringt.h>
#endif
#include <functional>
#include <filesystem>
VSTPlugin::VSTPlugin(obs_source_t *sourceContext) : m_sourceContext{sourceContext}, m_effect{nullptr}, m_is_open{false}
{
memset(m_effectName, 0, sizeof(m_effectName));
memset(m_vendorString, 0, sizeof(m_vendorString));
int numChannels = VST_MAX_CHANNELS;
int blocksize = BLOCK_SIZE;
m_inputs = (float **)malloc(sizeof(float **) * numChannels);
m_outputs = (float **)malloc(sizeof(float **) * numChannels);
for (int channel = 0; channel < numChannels; channel++) {
m_inputs[channel] = (float *)malloc(sizeof(float *) * blocksize);
m_outputs[channel] = (float *)malloc(sizeof(float *) * blocksize);
}
}
VSTPlugin::~VSTPlugin()
{
int numChannels = VST_MAX_CHANNELS;
for (int channel = 0; channel < numChannels; channel++) {
if (m_inputs[channel]) {
free(m_inputs[channel]);
m_inputs[channel] = NULL;
}
if (m_outputs[channel]) {
free(m_outputs[channel]);
m_outputs[channel] = NULL;
}
}
if (m_inputs) {
free(m_inputs);
m_inputs = NULL;
}
if (m_outputs) {
free(m_outputs);
m_outputs = NULL;
}
}
void VSTPlugin::loadEffectFromPath(std::string path)
{
if (m_proxyDisconnected || m_effect != nullptr)
return;
std::lock_guard<std::recursive_mutex> grd(m_effectStatusMutex);
blog(LOG_DEBUG, "VST Plug-in: loadEffectFromPath from pluginPath %s ", path.c_str());
m_pluginPath = path;
unloadEffect();
loadEffect();
if (!verifyProxy()) {
blog(LOG_WARNING, "VST Plug-in: loadEffectFromPath 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 (m_effect->magic != kEffectMagic) {
blog(LOG_WARNING, "VST Plug-in: loadEffectFromPath magic number is bad");
return;
}
m_remote->dispatcher(m_effect.get(), effGetEffectName, 0, 0, m_effectName, 0, 64);
m_remote->dispatcher(m_effect.get(), effGetVendorString, 0, 0, m_vendorString, 0, 64);
m_remote->dispatcher(m_effect.get(), effOpen, 0, 0, nullptr, 0.0f, 0);
// Set some default properties
auto sampleRate = audio_output_get_sample_rate(obs_get_audio());
m_remote->dispatcher(m_effect.get(), effSetSampleRate, 0, 0, nullptr, static_cast<float>(sampleRate), 0);
int blocksize = BLOCK_SIZE;
m_remote->dispatcher(m_effect.get(), effSetBlockSize, 0, blocksize, nullptr, 0.0f, 0);
m_remote->dispatcher(m_effect.get(), effMainsChanged, 0, 1, nullptr, 0, 0);
if (!verifyProxy())
return;
if (m_openInterfaceWhenActive)
openEditor();
}
bool VSTPlugin::verifyProxy(const bool notifyAudioPause /*= false*/)
{
if (m_effect == nullptr)
return false;
if (m_proxyDisconnected)
return false;
if (m_remote != nullptr) {
m_proxyDisconnected = !m_remote->m_connected;
if (m_proxyDisconnected) {
std::string msg;
if (notifyAudioPause)
msg = (std::filesystem::path(m_pluginPath).filename().string() +
" has stopped working.\n\nThe audio it modifies has paused and will continue after closing this popup but the filter is now disabled. You may restart the application or recreate the filter to enable it again.");
else
msg = (std::filesystem::path(m_pluginPath).filename().string() +
" has stopped working.\n\nThe filter has been disabled. You may restart the application or recreate the filter to enable it again.");
#ifdef WIN32
::MessageBoxA(GetDesktopWindow(), msg.c_str(), "VST Filter Error", MB_ICONERROR | MB_SYSTEMMODAL);
#endif
stopProxy();
return false;
} else {
return true;
}
}
return false;
}
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 (!m_effectStatusMutex.try_lock())
return audio;
if (m_effect != nullptr && m_remote != nullptr) {
uint32_t passes = (audio->frames + BLOCK_SIZE - 1) / BLOCK_SIZE;
uint32_t extra = audio->frames % BLOCK_SIZE;
for (uint32_t pass = 0; pass < passes; pass++) {
uint32_t frames = pass == passes - 1 && extra ? extra : BLOCK_SIZE;
silenceChannel(m_outputs, VST_MAX_CHANNELS, BLOCK_SIZE);
float *adata[VST_MAX_CHANNELS];
for (size_t d = 0; d < VST_MAX_CHANNELS; d++) {
if (audio->data[d] != nullptr)
adata[d] = ((float *)audio->data[d]) + (pass * BLOCK_SIZE);
else
adata[d] = m_inputs[d];
};
m_remote->processReplacing(m_effect.get(), adata, m_outputs, frames, VST_MAX_CHANNELS);
if (!verifyProxy(true)) {
m_effectStatusMutex.unlock();
return audio;
}
for (size_t c = 0; c < VST_MAX_CHANNELS; c++) {
if (audio->data[c] != nullptr) {
for (size_t i = 0; i < frames; i++)
adata[c][i] = m_outputs[c][i];
}
}
}
}
m_effectStatusMutex.unlock();
return audio;
}
void VSTPlugin::unloadEffect()
{
std::lock_guard<std::recursive_mutex> grd(m_effectStatusMutex);
m_windowCreated = false;
m_proxyDisconnected = false;
if (m_effect != nullptr && m_remote != nullptr) {
m_remote->dispatcher(m_effect.get(), effStopProcess, 0, 0, nullptr, 0, 0);
m_remote->dispatcher(m_effect.get(), effMainsChanged, 0, 0, nullptr, 0, 0);
m_remote->dispatcher(m_effect.get(), effClose, 0, 0, nullptr, 0.0f, 0);
}
stopProxy();
}
bool VSTPlugin::isEditorOpen()
{
return m_is_open;
}
bool VSTPlugin::hasWindowOpen()
{
if (m_windowCreated == false)
return false;
return m_is_open;
}
void VSTPlugin::openEditor()
{
if (isProxyDisconnected())
return;
if (m_effect != nullptr && m_remote != nullptr) {
if (!m_windowCreated) {
m_remote->sendHwndMsg(m_effect.get(), VstProxy::WM_USER_MSG::WM_USER_CREATE_WINDOW);
m_windowCreated = true;
}
m_remote->sendHwndMsg(m_effect.get(), VstProxy::WM_USER_MSG::WM_USER_SHOW);
m_is_open = true;
}
verifyProxy();
}
void VSTPlugin::hideEditor()
{
if (isProxyDisconnected())
return;
if (m_windowCreated && m_effect != nullptr && m_remote != nullptr) {
m_remote->sendHwndMsg(m_effect.get(), VstProxy::WM_USER_MSG::WM_USER_HIDE);
m_is_open = false;
}
verifyProxy();
}
void VSTPlugin::closeEditor()
{
m_is_open = false;
if (m_windowCreated && m_effect != nullptr && m_remote != nullptr) {
m_remote->sendHwndMsg(m_effect.get(), VstProxy::WM_USER_MSG::WM_USER_CLOSE);
m_windowCreated = false;
}
verifyProxy();
}
std::string VSTPlugin::getChunk(VstChunkType type)
{
cbase64_encodestate encoder;
std::string encodedData;
if (m_effect == nullptr || m_remote == nullptr) {
blog(LOG_WARNING, "VST Plug-in: getChunk, no effect loaded");
return "";
}
cbase64_init_encodestate(&encoder);
if (m_effect->flags & effFlagsProgramChunks && type != VstChunkType::Parameter) {
void *buf = nullptr;
intptr_t chunkSize = m_remote->dispatcher(m_effect.get(), effGetChunk, int(type), 0, &buf, 0.0, 0);
if (!verifyProxy())
return "";
if (!buf || chunkSize == 0) {
blog(LOG_WARNING, "VST Plug-in: effGetChunk failed");
return "";
}
encodedData.resize(cbase64_calc_encoded_length(uint32_t(chunkSize)));
int blockEnd = cbase64_encode_block((const unsigned char *)buf, uint32_t(chunkSize), &encodedData[0], &encoder);
cbase64_encode_blockend(&encodedData[blockEnd], &encoder);
return encodedData;
} else if (!(m_effect->flags & effFlagsProgramChunks) && type == VstChunkType::Parameter) {
std::vector<float> params;
for (int i = 0; i < m_effect->numParams; i++) {
float parameter = m_remote->getParameter(m_effect.get(), i);
params.push_back(parameter);
}
if (!verifyProxy())
return "";
if (!params.empty()) {
const char *bytes = reinterpret_cast<const char *>(¶ms[0]);
size_t size = sizeof(float) * params.size();
encodedData.resize(cbase64_calc_encoded_length(uint32_t(size)));
int blockEnd = cbase64_encode_block((const unsigned char *)bytes, uint32_t(size), &encodedData[0], &encoder);
cbase64_encode_blockend(&encodedData[blockEnd], &encoder);
} else {
blog(LOG_WARNING, "VST Plug-in: getChunk params.empty()");
}
return encodedData;
}
// Not every VST uses every type, we don't need to know about that in the logs
//blog(LOG_INFO, "VST Plug-in: getChunk option unavailable");
return "";
}
void VSTPlugin::setChunk(VstChunkType type, std::string &data)
{
if (data.size() == 0) {
blog(LOG_DEBUG, "VST Plug-in: setChunk with empty data chunk ignored");
return;
}
cbase64_decodestate decoder;
cbase64_init_decodestate(&decoder);
std::string decodedData;
if (m_effect == nullptr || m_remote == nullptr) {
blog(LOG_ERROR, "VST Plug-in: setChunk effect is not ready yet");
return;
}
decodedData.resize(cbase64_calc_decoded_length(data.data(), uint32_t(data.size())));
cbase64_decode_block(data.data(), uint32_t(data.size()), (unsigned char *)&decodedData[0], &decoder);
data = "";
if (m_effect->flags & effFlagsProgramChunks && type != VstChunkType::Parameter) {
auto ret = m_remote->dispatcher(m_effect.get(), effSetChunk, type == VstChunkType::Bank ? 0 : 1, decodedData.length(), &decodedData[0], 0.0,
decodedData.length());
} else if (!(m_effect->flags & effFlagsProgramChunks) && type == VstChunkType::Parameter) {
const char *p_chars = &decodedData[0];
const float *p_floats = reinterpret_cast<const float *>(p_chars);
int size = uint32_t(decodedData.length()) / sizeof(float);
std::vector<float> params(p_floats, p_floats + size);
if (params.size() != (size_t)m_effect->numParams) {
blog(LOG_WARNING, "VST Plug-in: setChunk wrong number of params");
return;
}
for (int i = 0; i < m_effect->numParams; i++)
m_remote->setParameter(m_effect.get(), i, params[i]);
}
verifyProxy();
}
void VSTPlugin::setProgram(const int programNumber)
{
if (m_effect == nullptr || m_remote == nullptr) {
blog(LOG_ERROR, "VST Plug-in: setProgram effect is not ready yet");
return;
}
if (programNumber < m_effect->numPrograms) {
intptr_t ret = m_remote->dispatcher(m_effect.get(), effSetProgram, 0, programNumber, nullptr, 0.0f, 0);
blog(LOG_ERROR, "VST Plug-in: setProgram get %lld from effSetProgram", ret);
} else {
blog(LOG_ERROR, "VST Plug-in: setProgram Failed to load program, number was outside possible program range.");
}
verifyProxy();
}
int VSTPlugin::getProgram()
{
if (m_effect == nullptr || m_remote == nullptr) {
blog(LOG_WARNING, "VST Plug-in: getProgram effect is not ready yet");
return 0;
}
intptr_t ret = m_remote->dispatcher(m_effect.get(), effGetProgram, 0, 0, nullptr, 0.0f, 0);
verifyProxy();
return static_cast<int>(ret);
}
void VSTPlugin::getSourceNames()
{
/* Only call inside the vst_filter_audio function! */
m_sourceName = obs_source_get_name(obs_filter_get_target(m_sourceContext));
m_filterName = obs_source_get_name(m_sourceContext);
}
std::string VSTPlugin::getPluginPath()
{
return m_pluginPath;
}