summaryrefslogtreecommitdiff
path: root/src/core/plugin.cpp
blob: d54c7df9ba904cac9dda246687d98c1859aa614a (plain)
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/* ---------------------------------------------------------------------
 *
 * Giada - Your Hardcore Loopmachine
 *
 * ---------------------------------------------------------------------
 *
 * Copyright (C) 2010-2015 Giovanni A. Zuliani | Monocasual
 *
 * This file is part of Giada - Your Hardcore Loopmachine.
 *
 * Giada - Your Hardcore Loopmachine 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 3 of the License, or (at your option) any later version.
 *
 * Giada - Your Hardcore Loopmachine 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 Giada - Your Hardcore Loopmachine. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * ------------------------------------------------------------------ */


#ifdef WITH_VST


#include "../utils/log.h"
#include "plugin.h"


int Plugin::id_generator = 0;


/* ------------------------------------------------------------------ */


Plugin::Plugin()
	: module    (NULL),
	  entryPoint(NULL),
	  plugin    (NULL),
	  id        (id_generator++),
	  program   (-1),
	  bypass    (false),
	  suspended (false)
{}


/* ------------------------------------------------------------------ */


Plugin::~Plugin() {
	unload();
}


/* ------------------------------------------------------------------ */


int Plugin::unload() {

	if (module == NULL)
		return 1;

#if defined(_WIN32)

	FreeLibrary((HMODULE)module); // FIXME - error checking
	return 1;

#elif defined(__linux__)

	return dlclose(module) == 0 ? 1 : 0;

#elif defined(__APPLE__)

	/* we must unload bundles but because bundles may be in use for other
	plug-in types it is important (and mandatory on certain plug-ins,
	e.g. Korg) to do a check on the retain count. */

	CFIndex retainCount = CFGetRetainCount(module);

	if (retainCount == 1) {
		gLog("[plugin] retainCount == 1, can unload dlyb\n");
		CFBundleUnloadExecutable(module);
		CFRelease(module);
	}
	else
		gLog("[plugin] retainCount > 1 (%d), leave dlyb alone\n", (int) retainCount);

	return 1;

#endif
}


/* ------------------------------------------------------------------ */


int Plugin::load(const char *fname) {

	strcpy(pathfile, fname);

#if defined(_WIN32)

	module = LoadLibrary(pathfile);

#elif defined(__linux__)

	module = dlopen(pathfile, RTLD_LAZY);

#elif defined(__APPLE__)

  /* creates the path to the bundle. In OSX vsts are stored inside the
   * so-called bundles, just a directory with '.vst' extension. Finally
   * we open the bundle with CFBundleCreate. */

  CFStringRef pathStr   = CFStringCreateWithCString(NULL, pathfile, kCFStringEncodingASCII);
  CFURLRef    bundleUrl = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,	pathStr, kCFURLPOSIXPathStyle, true);
  if(bundleUrl == NULL) {
    gLog("[plugin] unable to create URL reference for plugin\n");
    status = 0;
    return 0;
  }
  module = CFBundleCreate(kCFAllocatorDefault, bundleUrl);

#endif

	if (module) {

	/* release (free) any old string */

#ifdef __APPLE__
		CFRelease(pathStr);
		CFRelease(bundleUrl);
#endif
		//strcpy(pathfile, fname); ???????????
		status = 1;
		return 1;
	}
	else {

#if defined(_WIN32)

		gLog("[plugin] unable to load %s, error: %d\n", fname, (int) GetLastError());

#elif defined(__linux__)

		gLog("[plugin] unable to load %s, error: %s\n", fname, dlerror());

#elif defined(__APPLE__)

    gLog("[plugin] unable to create bundle reference\n");
    CFRelease(pathStr);
    CFRelease(bundleUrl);

#endif
		status = 0;
		return 0;
	}
}


/* ------------------------------------------------------------------ */


int Plugin::init(VstIntPtr VSTCALLBACK (*HostCallback) (AEffect* effect, VstInt32 opcode, VstInt32 index, VstIntPtr value, void* ptr, float opt)) {

#if defined(_WIN32)

	entryPoint = (vstPluginFuncPtr) GetProcAddress((HMODULE)module, "VSTPluginMain");
	if (!entryPoint)
		entryPoint = (vstPluginFuncPtr) GetProcAddress((HMODULE)module, "main");

#elif defined(__linux__)

	/* bad stuff here: main() is a function pointer, dlsym(module, "main")
	 * returns a pointer to an object (void*) which should be casted to
	 * a pointer to function (main(), precisely). Unfortunately the standard
	 * forbids the conversion from void* to function pointer. So we do a raw
	 * mem copy from tmp to entryPoint. */

	void *tmp;
	tmp = dlsym(module, "VSTPluginMain");
	if (!tmp)
		tmp = dlsym(module, "main");
	memcpy(&entryPoint, &tmp, sizeof(tmp));

#elif defined(__APPLE__)

	/* same also for Unix/OSX. */

	void *tmp = NULL;
	tmp = CFBundleGetFunctionPointerForName(module, CFSTR("VSTPluginMain"));

	if (!tmp) {
		gLog("[plugin] entryPoint 'VSTPluginMain' not found\n");
		tmp = CFBundleGetFunctionPointerForName(module, CFSTR("main_macho"));  // VST SDK < 2.4
	}
	if (!tmp) {
		gLog("[plugin] entryPoint 'main_macho' not found\n");
		tmp = CFBundleGetFunctionPointerForName(module, CFSTR("main"));
	}
	if (tmp)
		memcpy(&entryPoint, &tmp, sizeof(tmp));
	else
		gLog("[plugin] entryPoint 'main' not found\n");

#endif

	/* if entry point is found, add to plugin a pointer to hostCallback. Or
	 * in other words bind the callback to the plugin. */

	if (entryPoint) {
		gLog("[plugin] entryPoint found\n");
		plugin = entryPoint(HostCallback);
		if (!plugin) {
			gLog("[plugin] failed to create effect instance!\n");
			return 0;
		}
	}
	else {
		gLog("[plugin] entryPoint not found, unable to proceed\n");
		return 0;
	}


	/* check the magicNumber */
	/** WARNING: on Windows one can load any DLL! Why!?! */

  if(plugin->magic == kEffectMagic) {
		gLog("[plugin] magic number OK\n");
		return 1;
	}
	else {
    gLog("[plugin] magic number is bad\n");
    return 0;
  }
}


/* ------------------------------------------------------------------ */


int Plugin::setup(int samplerate, int frames) {

  /* init plugin through the dispatcher with some basic infos */

  plugin->dispatcher(plugin, effOpen, 0, 0, 0, 0);
	plugin->dispatcher(plugin, effSetSampleRate, 0, 0, 0, samplerate);
	plugin->dispatcher(plugin, effSetBlockSize, 0, frames, 0, 0);

	/* check SDK compatibility */

	if (getSDKVersion() != kVstVersion)
		gLog("[plugin] warning: different VST version (host: %d, plugin: %d)\n", kVstVersion, getSDKVersion());

	return 1;
}


/* ------------------------------------------------------------------ */


AEffect *Plugin::getPlugin() {
	return plugin;
}


/* ------------------------------------------------------------------ */


int Plugin::getId() { return id; }


/* ------------------------------------------------------------------ */

int Plugin::getSDKVersion() {
	return plugin->dispatcher(plugin, effGetVstVersion, 0, 0, 0, 0);
}


/* ------------------------------------------------------------------ */


void Plugin::getName(char *out) {
	char tmp[128] = "\0";
	plugin->dispatcher(plugin, effGetEffectName, 0, 0, tmp, 0);
	tmp[kVstMaxEffectNameLen-1] = '\0';
	strncpy(out, tmp, kVstMaxEffectNameLen);
}


/* ------------------------------------------------------------------ */


void Plugin::getVendor(char *out) {
	char tmp[128] = "\0";
	plugin->dispatcher(plugin, effGetVendorString, 0, 0, tmp, 0);
	tmp[kVstMaxVendorStrLen-1] = '\0';
	strncpy(out, tmp, kVstMaxVendorStrLen);
}


/* ------------------------------------------------------------------ */


void Plugin::getProduct(char *out) {
	char tmp[128] = "\0";
	plugin->dispatcher(plugin, effGetProductString, 0, 0, tmp, 0);
	tmp[kVstMaxProductStrLen-1] = '\0';
	strncpy(out, tmp, kVstMaxProductStrLen);
}


/* ------------------------------------------------------------------ */


int Plugin::getNumPrograms() { return plugin->numPrograms; }


/* ------------------------------------------------------------------ */


int Plugin::setProgram(int index) {
	plugin->dispatcher(plugin, effBeginSetProgram, 0, 0, 0, 0);
	plugin->dispatcher(plugin, effSetProgram, 0, index, 0, 0);
	gLog("[plugin] program changed, index %d\n", index);
	program = index;
	return plugin->dispatcher(plugin, effEndSetProgram, 0, 0, 0, 0);
}


/* ------------------------------------------------------------------ */


int Plugin::getNumParams() { return plugin->numParams; }


/* ------------------------------------------------------------------ */


int Plugin::getNumInputs() { return plugin->numInputs; }


/* ------------------------------------------------------------------ */


int Plugin::getNumOutputs() {	return plugin->numOutputs; }


/* ------------------------------------------------------------------ */


void Plugin::getProgramName(int index, char *out) {
	char tmp[128] = "\0";
	plugin->dispatcher(plugin, effGetProgramNameIndexed, index, 0, tmp, 0);
	tmp[kVstMaxProgNameLen-1] = '\0';
	strncpy(out, tmp, kVstMaxProgNameLen);
}


/* ------------------------------------------------------------------ */


void Plugin::getParamName(int index, char *out) {
	char tmp[128] = "\0";
	plugin->dispatcher(plugin, effGetParamName, index, 0, tmp, 0);
	tmp[kVstMaxParamStrLen-1] = '\0';
	strncpy(out, tmp, kVstMaxParamStrLen);
}


/* ------------------------------------------------------------------ */


void Plugin::getParamLabel(int index, char *out) {
	char tmp[128] = "\0";
	plugin->dispatcher(plugin, effGetParamLabel, index, 0, tmp, 0);
	tmp[kVstMaxParamStrLen-1] = '\0';
	strncpy(out, tmp, kVstMaxParamStrLen);
}


/* ------------------------------------------------------------------ */


void Plugin::getParamDisplay(int index, char *out) {
	char tmp[128] = "\0";
	plugin->dispatcher(plugin, effGetParamDisplay, index, 0, tmp, 0);
	tmp[kVstMaxParamStrLen-1] = '\0';
	strncpy(out, tmp, kVstMaxParamStrLen);
}


/* ------------------------------------------------------------------ */


float Plugin::getParam(int index) {
	return plugin->getParameter(plugin, index);
}


/* ------------------------------------------------------------------ */


void Plugin::setParam(int index, float value) {
	plugin->setParameter(plugin, index, value);
}


/* ------------------------------------------------------------------ */


bool Plugin::hasGui() {
	return plugin->flags & effFlagsHasEditor;
}


/* ------------------------------------------------------------------ */


void Plugin::openGui(void *w) {
	long val = 0;
#ifdef __linux__
  val = (long) w;
#endif
	plugin->dispatcher(plugin, effEditOpen, 0, val, w, 0);
}


/* ------------------------------------------------------------------ */


void Plugin::closeGui() {
	plugin->dispatcher(plugin, effEditClose, 0, 0, 0, 0);
}


/* ------------------------------------------------------------------ */


int Plugin::getGuiWidth() {
	ERect *pErect = NULL;
	plugin->dispatcher(plugin, effEditGetRect, 0, 0, &pErect, 0);
	return pErect->top + pErect->right;
}


/* ------------------------------------------------------------------ */


int Plugin::getGuiHeight() {
	ERect *pErect = NULL;
	plugin->dispatcher(plugin, effEditGetRect, 0, 0, &pErect, 0);
	return pErect->top + pErect->bottom;
}


/* ------------------------------------------------------------------ */


void Plugin::idle() {
	plugin->dispatcher(plugin, effEditIdle, 0, 0, NULL, 0);
}


/* ------------------------------------------------------------------ */


void Plugin::processAudio(float **in, float **out, long frames) {
	plugin->processReplacing(plugin, in, out, frames);
}


/* ------------------------------------------------------------------ */


void Plugin::processEvents(VstEvents *events) {
	plugin->dispatcher(plugin, effProcessEvents, 0, 0, events, 0.0);
}


/* ------------------------------------------------------------------ */


void Plugin::resume() {
	plugin->dispatcher(plugin, effMainsChanged, 0, 1, 0, 0);
	suspended = false;
}


/* ------------------------------------------------------------------ */


void Plugin::suspend() {
	plugin->dispatcher(plugin, effMainsChanged, 0, 0, 0, 0);
	suspended = true;
}


/* ------------------------------------------------------------------ */


void Plugin::close() {
	plugin->dispatcher(plugin, effClose, 0, 0, 0, 0);
}


/* ------------------------------------------------------------------ */


void Plugin::getRect(ERect **out) {
	plugin->dispatcher(plugin, effEditGetRect, 0, 0, out, 0);
}


#endif