PluginInfo::type added to copy constructor. But why is the copy constructor defined...
[ardour.git] / libs / ardour / vst_plugin.cc
1 /*
2   Copyright (C) 2004 Paul Davis 
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <algorithm>
21 #include <vector>
22 #include <string>
23 #include <cctype>
24
25 #include <cstdlib>
26 #include <cstdio> // so libraptor doesn't complain
27 #include <cmath>
28 #include <dirent.h>
29 #include <cstring> // for memmove
30 #include <sys/stat.h>
31 #include <cerrno>
32
33 #include <glibmm/ustring.h>
34 #include <glibmm/miscutils.h>
35
36 #include <lrdf.h>
37 #include <fst.h>
38
39 #include <pbd/compose.h>
40 #include <pbd/error.h>
41 #include <pbd/pathscanner.h>
42 #include <pbd/xml++.h>
43
44 #include <ardour/ardour.h>
45 #include <ardour/session.h>
46 #include <ardour/audioengine.h>
47 #include <ardour/vst_plugin.h>
48
49 #include <pbd/stl_delete.h>
50
51 #include "i18n.h"
52 #include <locale.h>
53
54 using namespace ARDOUR;
55 using namespace PBD;
56 using std::min;
57 using std::max;
58
59 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
60         : Plugin (e, session)
61 {
62         handle = h;
63
64         if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
65                 throw failed_constructor();
66         }
67
68         _plugin = _fst->plugin;
69         _plugin->user = this;
70
71         /* set rate and blocksize */
72
73         //cerr << "Dispatch " <<  "effSetSampleRate" << " for " << name() << endl;
74         _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL, 
75                              (float) session.frame_rate());
76         //cerr << "Dispatch " << "effSetBlockSize" << " for " << name() << endl;
77         _plugin->dispatcher (_plugin, effSetBlockSize, 0, 
78                              session.get_block_size(), NULL, 0.0f);
79         
80         /* set program to zero */
81
82         //cerr << "Dispatch " << "effSetProgram" << " for " << name() << endl;
83         _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
84         
85         Plugin::setup_controls ();
86 }
87
88 VSTPlugin::VSTPlugin (const VSTPlugin &other)
89         : Plugin (other)
90 {
91         handle = other.handle;
92
93         if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
94                 throw failed_constructor();
95         }
96         _plugin = _fst->plugin;
97
98         Plugin::setup_controls ();
99 }
100
101 VSTPlugin::~VSTPlugin ()
102 {
103         deactivate ();
104         GoingAway (); /* EMIT SIGNAL */
105         fst_close (_fst);
106 }
107
108 void
109 VSTPlugin::set_block_size (nframes_t nframes)
110 {
111         deactivate ();
112         //cerr << "Dispatch effSetBlockSize for " << name() << endl;
113         _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
114         activate ();
115 }
116
117 float
118 VSTPlugin::default_value (uint32_t port)
119 {
120         return 0;
121 }       
122
123 void
124 VSTPlugin::set_parameter (uint32_t which, float val)
125 {
126         // cerr << "1SetParameter for " << name() << endl;
127         _plugin->setParameter (_plugin, which, val);
128         // cerr << "signal param change\n";
129         ParameterChanged (which, val); /* EMIT SIGNAL */
130         // cerr << "change done\n";
131 }
132
133 float
134 VSTPlugin::get_parameter (uint32_t which) const
135 {
136         // cerr << "GetParameter for " << name() << endl;
137         return _plugin->getParameter (_plugin, which);
138         
139 }
140
141 uint32_t
142 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
143 {
144         ok = true;
145         return n;
146 }
147
148 XMLNode&
149 VSTPlugin::get_state()
150 {
151         XMLNode *root = new XMLNode (state_node_name());
152         LocaleGuard lg (X_("POSIX"));
153
154         if (_fst->current_program != -1) {
155                 char buf[32];
156                 snprintf (buf, sizeof (buf), "%d", _fst->current_program);
157                 root->add_property ("current-program", buf);
158         }
159
160         if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
161
162                 /* fetch the current chunk */
163                 
164                 guchar* data;
165                 long  data_size;
166                 
167                 //cerr << "Dispatch getChunk for " << name() << endl;
168                 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
169                         return *root;
170                 }
171
172                 /* store information */
173
174                 XMLNode* chunk_node = new XMLNode (X_("chunk"));
175                 gchar * encoded_data = g_base64_encode (data, data_size);
176                 chunk_node->add_content (encoded_data);
177                 g_free (encoded_data);
178
179                 root->add_child_nocopy (*chunk_node);
180         } else {
181
182                 XMLNode* parameters = new XMLNode ("parameters");
183
184                 for (long n = 0; n < _plugin->numParams; ++n) {
185                         char index[64];
186                         char val[32];
187                         snprintf (index, sizeof (index), "param_%ld", n);
188                         snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
189                         parameters->add_property (index, val);
190                 }
191
192                 root->add_child_nocopy (*parameters);
193
194         }
195
196         return *root;
197 }
198
199 int
200 VSTPlugin::set_state(const XMLNode& node)
201 {
202         LocaleGuard lg (X_("POSIX"));
203         const XMLProperty* prop;
204         
205         if (node.name() != state_node_name()) {
206                 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
207                 return 0;
208         }
209
210         if ((prop = node.property ("current-program")) != 0) {
211                 _fst->current_program = atoi (prop->value());
212         }
213
214         XMLNode* child;
215         int ret = -1;
216
217         if ((child = find_named_node (node, X_("chunk"))) != 0) {
218
219                 XMLPropertyList::const_iterator i;
220                 XMLNodeList::const_iterator n;
221                 int ret = -1;
222
223                 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
224                         if ((*n)->is_content ()) {
225                                 gsize chunk_size = 0;
226                                 guchar * data = g_base64_decode ((*n)->content ().c_str (), &chunk_size);
227                                 //cerr << "Dispatch setChunk for " << name() << endl;
228                                 ret = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, 0, chunk_size, data, 0);
229                                 g_free (data);
230                         }
231                 }
232
233         } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
234                 
235                 XMLPropertyList::const_iterator i;
236
237                 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
238                         long param;
239                         float val;
240
241                         sscanf ((*i)->name().c_str(), "param_%ld", &param);
242                         sscanf ((*i)->value().c_str(), "%f", &val);
243
244                         // cerr << "2setParameter for " << name() << endl;
245                         _plugin->setParameter (_plugin, param, val);
246                 }
247
248                 /* program number is not knowable */
249
250                 _fst->current_program = -1;
251
252                 ret = 0;
253         }
254
255         
256
257         return ret;
258 }
259
260 int
261 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
262 {
263         VstParameterProperties prop;
264         
265         desc.min_unbound = false;
266         desc.max_unbound = false;
267         prop.flags = 0;
268
269         //cerr << "Dispatch getParameterProperties for " << name() << endl;
270         if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
271
272                 if (prop.flags & kVstParameterUsesIntegerMinMax) {
273                         desc.lower = prop.minInteger;
274                         desc.upper = prop.maxInteger;
275                 } else {
276                         desc.lower = 0;
277                         desc.upper = 1.0;
278                 }
279                 
280                 if (prop.flags & kVstParameterUsesIntStep) {
281                         
282                         desc.step = prop.stepInteger;
283                         desc.smallstep = prop.stepInteger;
284                         desc.largestep = prop.stepInteger;
285                         
286                 } else if (prop.flags & kVstParameterUsesFloatStep) {
287                         
288                         desc.step = prop.stepFloat;
289                         desc.smallstep = prop.smallStepFloat;
290                         desc.largestep = prop.largeStepFloat;
291                         
292                 } else {
293                         
294                         float range = desc.upper - desc.lower;
295                         
296                         desc.step = range / 100.0f;
297                         desc.smallstep = desc.step / 2.0f;
298                         desc.largestep = desc.step * 10.0f;
299                 }
300                 
301                 desc.toggled = prop.flags & kVstParameterIsSwitch;
302                 // cerr << "parameter " << which << " toggled = " << desc.toggled << " from " << std::hex << prop.flags << " vs. " << kVstParameterIsSwitch << std::dec << endl;
303                 desc.logarithmic = false;
304                 desc.sr_dependent = false;
305                 desc.label = prop.label;
306
307         } else {
308
309                 /* old style */
310
311                 char label[64];
312                 label[0] = '\0';
313
314                 // cerr << "Dispatch paramName for " << name() << endl;
315                 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
316
317                 desc.label = label;
318                 desc.integer_step = false;
319                 desc.lower = 0.0f;
320                 desc.upper = 1.0f;
321                 desc.step = 0.01f;
322                 desc.smallstep = 0.005f;
323                 desc.largestep = 0.1f;
324                 desc.toggled = false;
325                 desc.logarithmic = false;
326                 desc.sr_dependent = false;
327         }
328
329         return 0;
330 }
331
332 bool
333 VSTPlugin::load_preset (string name)
334 {
335
336         if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
337                 error << _("no support for presets using chunks at this time")
338                       << endmsg;
339                 return false;
340         }
341         return Plugin::load_preset (name);
342 }
343
344 bool
345 VSTPlugin::save_preset (string name)
346 {
347         if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
348                 error << _("no support for presets using chunks at this time")
349                       << endmsg;
350                 return false;
351         }
352
353         return Plugin::save_preset (name, "vst");
354 }
355
356 string
357 VSTPlugin::describe_parameter (uint32_t param)
358 {
359         char name[64];
360         // cerr << "Dispatch effGetParamName for " << this->name() << endl;
361         _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
362         return name;
363 }
364
365 nframes_t
366 VSTPlugin::latency () const
367 {
368 #ifdef VESTIGE_HEADER
369         return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
370 #else
371         return 0;
372 #endif
373 }
374
375 set<uint32_t>
376 VSTPlugin::automatable () const
377 {
378         set<uint32_t> ret;
379
380         for (uint32_t i = 0; i < parameter_count(); ++i){
381                 ret.insert (ret.end(), i);
382         }
383
384         return ret;
385 }
386
387 int
388 VSTPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in_index, int32_t& out_index, nframes_t nframes, nframes_t offset)
389 {
390         float *ins[_plugin->numInputs];
391         float *outs[_plugin->numOutputs];
392         int32_t i;
393
394         if (nframes == 0) {
395                 warning << _("VST plugin called with zero frames - please notify Ardour developers") << endmsg;
396                 return 0;
397         }
398
399         for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
400                 ins[i] = bufs[min((uint32_t) in_index,maxbuf - 1)] + offset;
401                 in_index++;
402         }
403
404         for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
405                 outs[i] = bufs[min((uint32_t) out_index,maxbuf - 1)] + offset;
406
407                 /* unbelievably, several VST plugins still rely on Cubase
408                    behaviour and do not silence the buffer in processReplacing 
409                    when they have no output.
410                 */
411                 
412                 // memset (outs[i], 0, sizeof (Sample) * nframes);
413                 out_index++;
414         }
415
416         /* we already know it can support processReplacing */
417
418         // cerr << "!ProcessReplacing for " << name() << endl;
419         _plugin->processReplacing (_plugin, ins, outs, nframes);
420
421         return 0;
422 }
423
424 void
425 VSTPlugin::deactivate ()
426 {
427         //cerr << "Dispatch effMainsChanged for " << name() << endl;
428         _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
429 }
430
431 void
432 VSTPlugin::activate ()
433 {
434         //cerr << "Dispatch effMainsChanged for " << name() << endl;
435         _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
436 }
437
438 string
439 VSTPlugin::unique_id() const
440 {
441         char buf[32];
442 #ifdef VESTIGE_HEADER
443         snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
444 #else
445         snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
446 #endif
447         return string (buf);
448 }
449
450
451 const char *
452 VSTPlugin::name () const
453 {
454         return handle->name;
455 }
456
457 const char *
458 VSTPlugin::maker () const
459 {
460         return "imadeit";
461 }
462
463 const char *
464 VSTPlugin::label () const
465 {
466         return handle->name;
467 }
468
469 uint32_t
470 VSTPlugin::parameter_count() const
471 {
472         return _plugin->numParams;
473 }
474
475 bool
476 VSTPlugin::has_editor () const
477 {
478         return _plugin->flags & effFlagsHasEditor;
479 }
480
481 void
482 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
483 {
484         char *first_nonws;
485
486         //cerr << "Dispatch getParamDisplay for " << name() << endl;
487         _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
488
489         if (buf[0] == '\0') {
490                 return;
491         }
492
493         first_nonws = buf;
494         while (*first_nonws && isspace (*first_nonws)) {
495                 first_nonws++;
496         }
497         if (*first_nonws == '\0') {
498                 return;
499         }
500
501         memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
502 }
503
504 PluginPtr
505 VSTPluginInfo::load (Session& session)
506 {
507         try {
508                 PluginPtr plugin;
509
510                 if (Config->get_use_vst()) {
511                         FSTHandle* handle;
512                         
513                         handle = fst_load(path.c_str());
514         
515                         if ( (int)handle == -1) {
516                                 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
517                         } else {
518                                 plugin.reset (new VSTPlugin (session.engine(), session, handle));
519                         }
520                 } else {
521                         error << _("You asked ardour to not use any VST plugins") << endmsg;
522                         return PluginPtr ((Plugin*) 0);
523                 }
524
525                 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
526                 return plugin;
527         }       
528
529         catch (failed_constructor &err) {
530                 return PluginPtr ((Plugin*) 0);
531         }
532 }