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
422
423
424
425
426
427
428
429
430
431
432
433
|
//
// Vst2xHostCallback.cpp - MrsWatson
// Created by Nik Reiman on 1/5/12.
// Copyright (c) 2012 Teragon Audio. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// C++ includes
#define VST_FORCE_DEPRECATED 0
#include "aeffectx.h"
#include "plugin/PluginVst2xHostCallback.h"
// C includes
extern "C" {
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "app/BuildInfo.h"
#include "audio/AudioSettings.h"
#include "base/CharString.h"
#include "logging/EventLogger.h"
#include "plugin/PluginChain.h"
#include "plugin/PluginVst2x.h"
#include "plugin/PluginVst2xId.h"
#include "time/AudioClock.h"
void pluginVst2xAudioMasterIOChanged(const Plugin self, AEffect const *const newValues);
}
// Global variables (sigh, unfortunately yes). When plugins ask for the time, they
// expect that the host will pass them a pointer to a VstTimeInfo struct, a pointer
// to which they are not the owner of and are not expected to free afterwards...
// which is actually the correct thing to do, given that if this were the case, a
// huge number of plugins would probably fail to do this and leak memory all over
// the place.
// Anyways, since we cannot scope this variable intelligently, we instead keep one
// instance of it as a static variable, so it is always avaible to plugins when they
// ask for the time.
static VstTimeInfo vstTimeInfo;
extern "C" {
// Current plugin ID, which is mostly used by shell plugins during initialization.
// Instance declared in PluginVst2x.cpp, see explanation for the global-ness and
// need of this variable there.
extern VstInt32 currentPluginUniqueId;
static int _canHostDo(const char *pluginName, const char *canDoString)
{
boolByte supported = false;
logDebug("Plugin '%s' asked if we can do '%s'", pluginName, canDoString);
if (!strcmp(canDoString, EMPTY_STRING)) {
logWarn("Plugin '%s' asked if we can do an empty string. This is probably a bug.", pluginName);
} else if (!strcmp(canDoString, "sendVstEvents")) {
supported = true;
} else if (!strcmp(canDoString, "sendVstMidiEvent")) {
supported = true;
} else if (!strcmp(canDoString, "sendVstTimeInfo")) {
supported = true;
} else if (!strcmp(canDoString, "receiveVstEvents")) {
supported = false;
} else if (!strcmp(canDoString, "receiveVstMidiEvent")) {
supported = false;
} else if (!strcmp(canDoString, "reportConnectionChanges")) {
supported = false;
} else if (!strcmp(canDoString, "acceptIOChanges")) {
supported = false;
} else if (!strcmp(canDoString, "sizeWindow")) {
supported = false;
} else if (!strcmp(canDoString, "offline")) {
supported = false;
} else if (!strcmp(canDoString, "openFileSelector")) {
supported = false;
} else if (!strcmp(canDoString, "closeFileSelector")) {
supported = false;
} else if (!strcmp(canDoString, "startStopProcess")) {
supported = true;
} else if (!strcmp(canDoString, "shellCategory")) {
supported = true;
} else if (!strcmp(canDoString, "sendVstMidiEventFlagIsRealtime")) {
supported = false;
} else {
logInfo("Plugin '%s' asked if host canDo '%s' (unimplemented)", pluginName, canDoString);
}
return supported;
}
VstIntPtr VSTCALLBACK pluginVst2xHostCallback(AEffect *effect, VstInt32 opcode, VstInt32 index, VstIntPtr value, void *dataPtr, float opt)
{
// This string is used in a bunch of logging calls below
PluginVst2xId pluginId;
if (effect != NULL) {
pluginId = newPluginVst2xIdWithId((unsigned long)effect->uniqueID);
} else {
// During plugin initialization, the dispatcher can be called without a
// valid plugin instance, as the AEffect* struct is still not fully constructed
// at that point.
pluginId = newPluginVst2xId();
}
const char *pluginIdString = pluginId->idString->data;
VstIntPtr result = 0;
logDebug("Plugin '%s' called host dispatcher with %d, %d, %d", pluginIdString, opcode, index, value);
switch (opcode) {
case audioMasterAutomate:
// The plugin will call this if a parameter has changed via MIDI or the GUI, so the host can update
// itself accordingly. We don't care about this (for the time being), and as we don't support either
// GUI's or live MIDI, this opcode can be ignored.
break;
case audioMasterVersion:
// We are a VST 2.4 compatible host
result = 2400;
break;
case audioMasterCurrentId:
// Use the current plugin ID, needed by VST shell plugins to determine which sub-plugin to load
result = currentPluginUniqueId;
break;
case audioMasterIdle:
// Ignore
result = 1;
break;
case audioMasterPinConnected:
logDeprecated("audioMasterPinConnected", pluginIdString);
break;
case audioMasterWantMidi:
// This (deprecated) call is sometimes made by VST2.3 instruments to tell
// the host that it is an instrument. We can safely ignore it.
result = 1;
break;
case audioMasterGetTime: {
AudioClock audioClock = getAudioClock();
// These values are always valid
vstTimeInfo.samplePos = audioClock->currentFrame;
vstTimeInfo.sampleRate = getSampleRate();
// Set flags for transport state
vstTimeInfo.flags = 0;
vstTimeInfo.flags |= audioClock->transportChanged ? kVstTransportChanged : 0;
vstTimeInfo.flags |= audioClock->isPlaying ? kVstTransportPlaying : 0;
// Fill values based on other flags which may have been requested
if (value & kVstNanosValid) {
// It doesn't make sense to return this value, as the plugin may try to calculate
// something based on the current system time. As we are running offline, anything
// the plugin calculates here will probably be wrong given the way we are running.
// However, for realtime mode, this flag should be implemented in that case.
logWarn("Plugin '%s' asked for time in nanoseconds (unsupported)", pluginIdString);
}
if (value & kVstPpqPosValid) {
// TODO: Move calculations to AudioClock
double samplesPerBeat = (60.0 / getTempo()) * getSampleRate();
// Musical time starts with 1, not 0
vstTimeInfo.ppqPos = (vstTimeInfo.samplePos / samplesPerBeat) + 1.0;
logDebug("Current PPQ position is %g", vstTimeInfo.ppqPos);
vstTimeInfo.flags |= kVstPpqPosValid;
}
if (value & kVstTempoValid) {
vstTimeInfo.tempo = getTempo();
vstTimeInfo.flags |= kVstTempoValid;
}
if (value & kVstBarsValid) {
if (!(value & kVstPpqPosValid)) {
logError("Plugin requested position in bars, but not PPQ");
}
// TODO: Move calculations to AudioClock
double currentBarPos = floor(vstTimeInfo.ppqPos / (double)getTimeSignatureBeatsPerMeasure());
vstTimeInfo.barStartPos = currentBarPos * (double)getTimeSignatureBeatsPerMeasure() + 1.0;
logDebug("Current bar is %g", vstTimeInfo.barStartPos);
vstTimeInfo.flags |= kVstBarsValid;
}
if (value & kVstCyclePosValid) {
// We don't support cycling, so this is always 0
}
if (value & kVstTimeSigValid) {
vstTimeInfo.timeSigNumerator = getTimeSignatureBeatsPerMeasure();
vstTimeInfo.timeSigDenominator = getTimeSignatureNoteValue();
vstTimeInfo.flags |= kVstTimeSigValid;
}
if (value & kVstSmpteValid) {
logUnsupportedFeature("Current time in SMPTE format");
}
if (value & kVstClockValid) {
logUnsupportedFeature("Sample frames until next clock");
}
result = (VstIntPtr)&vstTimeInfo;
break;
}
case audioMasterProcessEvents:
logUnsupportedFeature("VST master opcode audioMasterProcessEvents");
break;
case audioMasterSetTime:
logDeprecated("audioMasterSetTime", pluginIdString);
break;
case audioMasterTempoAt:
logDeprecated("audioMasterTempoAt", pluginIdString);
break;
case audioMasterGetNumAutomatableParameters:
logDeprecated("audioMasterGetNumAutomatableParameters", pluginIdString);
break;
case audioMasterGetParameterQuantization:
logDeprecated("audioMasterGetParameterQuantization", pluginIdString);
break;
case audioMasterIOChanged: {
if (effect != NULL) {
PluginChain pluginChain = getPluginChain();
logDebug("Number of inputs: %d", effect->numInputs);
logDebug("Number of outputs: %d", effect->numOutputs);
logDebug("Number of parameters: %d", effect->numParams);
logDebug("Initial Delay: %d", effect->initialDelay);
result = -1;
for (unsigned int i = 0; i < pluginChain->numPlugins; ++i) {
if ((unsigned long)effect->uniqueID == pluginVst2xGetUniqueId(pluginChain->plugins[i])) {
logDebug("Updating plugin");
pluginVst2xAudioMasterIOChanged(pluginChain->plugins[i], effect);
result = 0;
break;//Only one plugin will match anyway.
}
}
break;
}
}
case audioMasterNeedIdle:
logDeprecated("audioMasterNeedIdle", pluginIdString);
break;
case audioMasterSizeWindow:
logWarn("Plugin '%s' asked us to resize window (unsupported)", pluginIdString);
break;
case audioMasterGetSampleRate:
result = (int)getSampleRate();
break;
case audioMasterGetBlockSize:
result = (VstIntPtr)getBlocksize();
break;
case audioMasterGetInputLatency:
// Input latency is not used, and is always 0
result = 0;
break;
case audioMasterGetOutputLatency:
// Output latency is not used, and is always 0
result = 0;
break;
case audioMasterGetPreviousPlug:
logDeprecated("audioMasterGetPreviousPlug", pluginIdString);
break;
case audioMasterGetNextPlug:
logDeprecated("audioMasterGetNextPlug", pluginIdString);
break;
case audioMasterWillReplaceOrAccumulate:
logDeprecated("audioMasterWillReplaceOrAccumulate", pluginIdString);
break;
case audioMasterGetCurrentProcessLevel:
// We are not a multithreaded app and have no GUI, so this is unsupported.
result = kVstProcessLevelUnknown;
break;
case audioMasterGetAutomationState:
// Automation is also not supported (for now)
result = kVstAutomationUnsupported;
break;
case audioMasterOfflineStart:
logWarn("Plugin '%s' asked us to start offline processing (unsupported)", pluginIdString);
break;
case audioMasterOfflineRead:
logWarn("Plugin '%s' asked to read offline data (unsupported)", pluginIdString);
break;
case audioMasterOfflineWrite:
logWarn("Plugin '%s' asked to write offline data (unsupported)", pluginIdString);
break;
case audioMasterOfflineGetCurrentPass:
logWarn("Plugin '%s' asked for current offline pass (unsupported)", pluginIdString);
break;
case audioMasterOfflineGetCurrentMetaPass:
logWarn("Plugin '%s' asked for current offline meta pass (unsupported)", pluginIdString);
break;
case audioMasterSetOutputSampleRate:
logDeprecated("audioMasterSetOutputSampleRate", pluginIdString);
break;
case audioMasterGetOutputSpeakerArrangement:
logDeprecated("audioMasterGetOutputSpeakerArrangement", pluginIdString);
break;
case audioMasterGetVendorString:
strncpy((char *)dataPtr, VENDOR_NAME, kVstMaxVendorStrLen);
result = 1;
break;
case audioMasterGetProductString:
strncpy((char *)dataPtr, PROGRAM_NAME, kVstMaxProductStrLen);
result = 1;
break;
case audioMasterGetVendorVersion:
// Return our version as a single string, in the form ABCC, which corresponds to version A.B.C
// Often times the patch can reach double-digits, so it gets two decimal places.
result = VERSION_MAJOR * 1000 + VERSION_MINOR * 100 + VERSION_PATCH;
break;
case audioMasterVendorSpecific:
logWarn("Plugin '%s' made a vendor specific call (unsupported). Arguments: %d, %d, %f", pluginIdString, index, value, opt);
break;
case audioMasterCanDo:
result = _canHostDo(pluginIdString, (char *)dataPtr);
break;
case audioMasterSetIcon:
logDeprecated("audioMasterSetIcon", pluginIdString);
break;
case audioMasterGetLanguage:
result = kVstLangEnglish;
break;
case audioMasterOpenWindow:
logDeprecated("audioMasterOpenWindow", pluginIdString);
break;
case audioMasterCloseWindow:
logDeprecated("audioMasterCloseWindow", pluginIdString);
break;
case audioMasterGetDirectory:
logWarn("Plugin '%s' asked for directory pointer (unsupported)", pluginIdString);
break;
case audioMasterUpdateDisplay:
// Ignore
break;
case audioMasterBeginEdit:
logWarn("Plugin '%s' asked to begin parameter automation (unsupported)", pluginIdString);
break;
case audioMasterEndEdit:
logWarn("Plugin '%s' asked to end parameter automation (unsupported)", pluginIdString);
break;
case audioMasterOpenFileSelector:
logWarn("Plugin '%s' asked us to open file selector (unsupported)", pluginIdString);
break;
case audioMasterCloseFileSelector:
logWarn("Plugin '%s' asked us to close file selector (unsupported)", pluginIdString);
break;
case audioMasterEditFile:
logDeprecated("audioMasterEditFile", pluginIdString);
break;
case audioMasterGetChunkFile:
logDeprecated("audioMasterGetChunkFile", pluginIdString);
break;
case audioMasterGetInputSpeakerArrangement:
logDeprecated("audioMasterGetInputSpeakerArrangement", pluginIdString);
break;
default:
logWarn("Plugin '%s' asked if host can do unknown opcode %d", pluginIdString, opcode);
break;
}
freePluginVst2xId(pluginId);
return result;
}
} // extern "C"
|