aboutsummaryrefslogtreecommitdiff
path: root/plugin~_vst.c
blob: c8427e5a2599a15f54ed25d78fdb1d9872ea98bf (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
/* plugin~, a Pd tilde object for hosting LADSPA/VST plug-ins
   Copyright (C) 2000 Jarno Seppänen
   $Id: plugin~_vst.c,v 1.1 2002-11-19 09:51:40 ggeiger Exp $

   This file is part of plugin~.

   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, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

#include "config.h"
#if PLUGIN_TILDE_USE_VST

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "plugin~.h"
#include "plugin~_vst.h"
/* VST header */
#include "vst/AEffect.h"
/* VST dll helper functions */
#include "vstutils.h"
#ifdef WIN32
#include "win/vitunmsvc.h" /* strncasecmp() */
#include <windows.h> /* GetForegroundWindow() */
#endif

#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif


const char*
plugin_tilde_vst_search_plugin (Pd_Plugin_Tilde* x,
				const char* name)
{
    /* searching through VST libraries not supported */
    error ("plugin~: warning: searching through VST libraries not supported");
    return NULL;
}

int
plugin_tilde_vst_open_plugin (Pd_Plugin_Tilde* x,
			      const char* name,
			      const char* lib_name,
			      unsigned long sample_rate)
{
    unsigned port_index;

    /* precondition(s) */
    assert (x != NULL);
    /* name is unused */
    assert (lib_name != NULL);
    assert (sample_rate != 0);

    /* Initialize object struct */
    x->plugin.vst.instance = NULL;
    x->plugin.vst.audio_inputs = NULL;
    x->plugin.vst.audio_outputs = NULL;
    x->plugin.vst.num_samples = 0;
    x->plugin.vst.editor_open = 0;

    /* Attempt to load the plugin. */
    x->plugin_library = vstutils_load_vst_plugin_dll (lib_name);
    if (x->plugin_library == NULL)
    {
	error ("plugin~: Unable to load VST plugin library \"%s\"",
	       lib_name);
	return 1;
    }

    /* Construct the plugin.  This is supposed to call
       the AudioEffect::AudioEffect() ctor eventually */
    x->plugin.vst.instance
	= vstutils_init_vst_plugin (x->plugin_library,
				    lib_name,
				    plugin_tilde_vst_audioMaster);
    if (x->plugin.vst.instance == NULL) {
	error ("plugin~: Unable to instantiate VST plugin from library \"%s\"",
	       lib_name);
	return 1;
    }

    /* Stuff Pd_Plugin_Tilde* x into user field of AEffect for audioMaster() to use */
    x->plugin.vst.instance->user = x;

    /* Call plugin open() (through dispatcher()) in order to ensure
       plugin is properly constructed */
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effOpen,
					0, 0, NULL, 0);

#if PLUGIN_TILDE_DEBUG
    error ("DEBUG plugin~: constructed VST plugin \"%s\" successfully",
	   lib_name);
#endif

    /* Check another strange id */
    if (x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					    effIdentify,
					    0, 0, NULL, 0)
	!= 'NvEf') {
	error ("plugin~_vst: warning: VST plugin malfunction (effIdentify != 'NvEf')");
    }

    /* Tell the sample rate and frame length to the VST plug-in */
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effSetSampleRate,
					0, 0, NULL, (float)sample_rate);
    /* FIXME just give some value since it will be changed later */
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effSetBlockSize,
					0, 16, NULL, 0);

    /* Find out the number of inputs and outputs needed (all VST
       parameters can be automated i.e. output if the plug-in so wants
       and the GUI can be operated). */
    x->num_audio_inputs = x->plugin.vst.instance->numInputs;
    x->num_audio_outputs = x->plugin.vst.instance->numOutputs;
    x->num_control_inputs = x->plugin.vst.instance->numParams;
    x->num_control_outputs = x->plugin.vst.instance->numParams;

    /* Make sure that processReplacing() is implemented */
    if (x->plugin.vst.instance->flags & effFlagsCanReplacing == 0) {
	error ("plugin~_vst: sorry, this VST plug-in type isn't supported (processReplacing() not implemented)");
	return 1;
    }

    /* Activate the plugin. */
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effMainsChanged,
					0, 1, NULL, 0);

    /* Finally, attempt open editor GUI if available */
    plugin_tilde_vst_open_editor (x);

    /* Make sure the user data still is there */
    assert (x->plugin.vst.instance->user == x);

    /* success */
    return 0;
}

void
plugin_tilde_vst_close_plugin (Pd_Plugin_Tilde* x)
{
    /* precondition(s) */
    assert (x != NULL);

    /* Attempt to close editor GUI */
    plugin_tilde_vst_close_editor (x);

    if (x->plugin.vst.instance != NULL)
    {
	/* Deactivate the plugin. */
	x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					    effMainsChanged,
					    0, 0, NULL, 0);

	/* Destruct the plugin.  This is supposed to translate to
	   a call to the AudioEffect::~AudioEffect() dtor */
	x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					    effClose,
					    0, 0, NULL, 0);
	x->plugin.vst.instance = NULL;
    }

    if (x->plugin_library != NULL)
    {
	vstutils_unload_vst_plugin_dll (x->plugin_library);
	x->plugin_library = NULL;
    }
}

void
plugin_tilde_vst_apply_plugin (Pd_Plugin_Tilde* x)
{
    /* Run the plugin on Pd's buffers */
    /* FIXME need to implement out-of-place buffers and zero them here
       if processReplacing() isn't implemented */
    x->plugin.vst.instance->processReplacing (x->plugin.vst.instance,
					      x->plugin.vst.audio_inputs,
					      x->plugin.vst.audio_outputs,
					      x->plugin.vst.num_samples);
}

void
plugin_tilde_vst_print (Pd_Plugin_Tilde* x)
{
    unsigned i;

    printf ("control %d in/out; audio %d in/%d out\n"
	    "Loaded from library \"%s\".\n",
	    x->num_control_inputs,
	    x->num_audio_inputs,
	    x->num_audio_outputs,
	    x->plugin_library_filename);

    for (i = 0; i < x->num_control_inputs; i++) {
	/* the Steinberg(tm) way... */
	char name[9];
	char display[25];
	char label[9];
	if (i == 0) {
	    printf ("Control input/output(s):\n");
	}
	memset (name, 0, 9);
	memset (display, 0, 25);
	memset (label, 0, 9);
	x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					    effGetParamName,
					    i, 0, name, 0);
	x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					    effGetParamDisplay,
					    i, 0, display, 0);
	x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					    effGetParamLabel,
					    i, 0, label, 0);
	printf (" #%d \"%s\" (%s %s)\n",
		i + 1, name, display, label);
    }
}

void
plugin_tilde_vst_reset (Pd_Plugin_Tilde* x)
{
    /* precondition(s) */
    assert (x != NULL);
    /* reset plug-in by first deactivating and then re-activating it */
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effMainsChanged,
					0, 0, NULL, 0);
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effMainsChanged,
					0, 1, NULL, 0);
}

void
plugin_tilde_vst_connect_audio (Pd_Plugin_Tilde* x,
				float** audio_inputs,
				float** audio_outputs,
				unsigned long num_samples)
{
    assert (x != NULL);
    assert (audio_inputs != NULL);
    assert (audio_outputs != NULL);
    x->plugin.vst.audio_inputs = audio_inputs;
    x->plugin.vst.audio_outputs = audio_outputs;
    x->plugin.vst.num_samples = num_samples;
    /* Tell the block size to the VST plug-in */
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effMainsChanged,
					0, 0, NULL, 0);
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effSetBlockSize,
					0, num_samples, NULL, 0);
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effMainsChanged,
					0, 1, NULL, 0);
}

void
plugin_tilde_vst_set_control_input_by_name (Pd_Plugin_Tilde* x,
				    const char* name,
				    float value)
{
    unsigned parm_index;
    int found_port; /* boolean */
    char parm_name[9]; /* the Steinberg(tm) way! */

    /* precondition(s) */
    assert (x != NULL);

    /* compare control name to VST parameters' names
       case-insensitively */
    found_port = 0;
    for (parm_index = 0; parm_index < x->num_control_inputs; parm_index++)
    {
	unsigned cmp_start, cmp_length;
	memset (parm_name, 0, 9);
	x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					    effGetParamName,
					    parm_index, 0, parm_name, 0);
	/* skip any initial whitespace */
	cmp_start = 0;
	while (parm_name[cmp_start] != 0
	       && parm_name[cmp_start] == ' ') {
	    cmp_start++;
	}
	cmp_length = MIN (strlen (name), strlen (&parm_name[cmp_start]));
	if (cmp_length != 0
	    && strncasecmp (name, &parm_name[cmp_start], cmp_length) == 0)
	{
	    /* found the first port to match */
	    found_port = 1;
	    break;
	}
    }

    if (!found_port)
    {
	error ("plugin~: VST plugin doesn't have a parameter named \"%s\"",
	       name);
	return;
    }

    plugin_tilde_vst_set_control_input_by_index (x,
						 parm_index,
						 value);
}

void
plugin_tilde_vst_set_control_input_by_index (Pd_Plugin_Tilde* x,
				       unsigned index_,
				       float value)
{
    char name[9]; /* the Steinberg(tm) way! */

    /* precondition(s) */
    assert (x != NULL);
    /* assert (index_ >= 0); causes a warning */
    assert (index_ < x->num_control_inputs);

    /* Limit parameter to range [0, 1] */
    if (value < 0.0 || value > 1.0) {
	if (value < 0.0) {
	    value = 0.0;
	}
	else {
	    value = 1.0;
	}
	error ("plugin~: warning: parameter limited to within [0, 1]");
    }
	
    /* set the appropriate control port value */
    x->plugin.vst.instance->setParameter (x->plugin.vst.instance,
					  index_,
					  value);

#if PLUGIN_TILDE_DEBUG
    memset (name, 0, 9);
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effGetParamName,
					index_, 0, name, 0);
    error ("DEBUG plugin~_vst: control change parameter #%ud: \"%s\" to value %f",
	   index_ + 1, name, value);
#endif
}

static long
plugin_tilde_vst_audioMaster (AEffect* effect,
			      long opcode,
			      long index,
			      long value,
			      void* ptr,
			      float opt)
{
    char param_name[9];
#if 0 /*PLUGIN_TILDE_DEBUG*/
    error ("DEBUG plugin~_vst: audioMaster(0x%p, %ld, %ld, %ld, 0x%p, %f)",
	   effect, opcode, index, value, ptr, opt);
#endif

    switch (opcode) {
	case audioMasterAutomate:
	    assert (effect != NULL);
	    assert (effect->user != NULL); /* this is Pd_Plugin_Tilde* */
	    effect->setParameter (effect, index, opt);
	    /* Send "control" messages from here */
	    memset (param_name, 0, 9);
	    effect->dispatcher (effect, effGetParamName, index, 0, param_name, 0);
	    plugin_tilde_emit_control_output (effect->user, param_name, opt);
	    return 0;
	    break;
	case audioMasterVersion:
	    return 1;
	    break;
	case audioMasterCurrentId:
	    return 0;
	    break;
	case audioMasterIdle:
	    effect->dispatcher (effect, effEditIdle, 0, 0, NULL, 0);
	    return 0;
	    break;
	case audioMasterPinConnected:
	    /* return 0="true" for all inquiries for now */
	    return 0;
	    break;
    }
#if 0 /*PLUGIN_TILDE_DEBUG*/
    error ("DEBUG plugin~_vst: warning: unsupported audioMaster opcode");
#endif
    return 0;
}

static void
plugin_tilde_vst_open_editor (Pd_Plugin_Tilde* x)
{
#if 0 /* FIXME doesn't work */
#ifdef WIN32
    HWND parent;

    /* precondition(s) */
    assert (x != NULL);

#if PLUGIN_TILDE_DEBUG
    error ("DEBUG plugin~_vst: open_editor()");
#endif

    if ((x->plugin.vst.instance->flags & effFlagsHasEditor) == 0
	|| x->plugin.vst.editor_open == 1) {
	/* no editor or editor already open */
	return;
    }

    /* Hmph, don't know about the Pd window, so give the desktop as
       the parent to the plug-in; we could actually use
       GetForegroundWindow(), since the user is currently typing into
       the pd subpatch window */
    /*parent = GetDesktopWindow ();*/
    parent = GetForegroundWindow ();
    /*parent = NULL;*/

    /* Open the editor! */
    if (!x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					     effEditOpen,
					     0, 0, parent, 0)) {
	/* no luck error */
	error ("plugin~_vst: couldn't open editor");
    } else {
	x->plugin.vst.editor_open = 1;
    }
#endif /* WIN32 */
#endif /* FIXME doesn't work */
}

static void
plugin_tilde_vst_close_editor (Pd_Plugin_Tilde* x)
{
#if 0 /* FIXME doesn't work */
#ifdef WIN32

    /* precondition(s) */
    assert (x != NULL);

#if PLUGIN_TILDE_DEBUG
    error ("DEBUG plugin~_vst: close_editor()");
#endif

    if ((x->plugin.vst.instance->flags & effFlagsHasEditor) == 0
	|| x->plugin.vst.editor_open == 0) {
	/* no editor or it isn't open */
	return;
    }

    /* Close the editor */
    x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					effEditClose,
					0, 0, NULL, 0);
    x->plugin.vst.editor_open = 0;

#endif /* WIN32 */
#endif /* FIXME doesn't work */
}

void
plugin_tilde_vst_update_gui (Pd_Plugin_Tilde* x)
{
#if 0 /* FIXME doesn't work */
#ifdef WIN32

    /* precondition(s) */
    assert (x != NULL);

#if PLUGIN_TILDE_DEBUG
    error ("DEBUG plugin~_vst: update_gui()");
#endif
    if (x->plugin.vst.editor_open) {
	x->plugin.vst.instance->dispatcher (x->plugin.vst.instance,
					    effEditIdle,
					    0, 0, NULL, 0);
    }
#endif /* WIN32 */
#endif /* FIXME doesn't work */
}

#endif /* PLUGIN_TILDE_USE_VST */

/* EOF */