(Hopefully) clarify operator= and copy construction behaviour of the Property hierarc...
[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 <fst.h>
45
46 #include "ardour/session.h"
47 #include "ardour/audioengine.h"
48 #include "ardour/filesystem_paths.h"
49 #include "ardour/vst_plugin.h"
50 #include "ardour/buffer_set.h"
51 #include "ardour/audio_buffer.h"
52 #include "ardour/midi_buffer.h"
53
54 #include "pbd/stl_delete.h"
55
56 #include "i18n.h"
57 #include <locale.h>
58
59 using namespace std;
60 using namespace ARDOUR;
61 using namespace PBD;
62 using std::min;
63 using std::max;
64
65 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
66         : Plugin (e, session)
67 {
68         handle = h;
69
70         if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
71                 throw failed_constructor();
72         }
73
74         _plugin = _fst->plugin;
75         _plugin->user = this;
76
77         /* set rate and blocksize */
78
79         _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
80                              (float) session.frame_rate());
81         _plugin->dispatcher (_plugin, effSetBlockSize, 0,
82                              session.get_block_size(), NULL, 0.0f);
83
84         /* set program to zero */
85
86         _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
87
88         // Plugin::setup_controls ();
89 }
90
91 VSTPlugin::VSTPlugin (const VSTPlugin &other)
92         : Plugin (other)
93 {
94         handle = other.handle;
95
96         if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
97                 throw failed_constructor();
98         }
99         _plugin = _fst->plugin;
100
101         // Plugin::setup_controls ();
102 }
103
104 VSTPlugin::~VSTPlugin ()
105 {
106         deactivate ();
107         fst_close (_fst);
108 }
109
110 int 
111 VSTPlugin::set_block_size (nframes_t nframes)
112 {
113         deactivate ();
114         _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
115         activate ();
116         return 0;
117 }
118
119 float
120 VSTPlugin::default_value (uint32_t port)
121 {
122         return 0;
123 }
124
125 void
126 VSTPlugin::set_parameter (uint32_t which, float val)
127 {
128         _plugin->setParameter (_plugin, which, val);
129         //ParameterChanged (which, val); /* EMIT SIGNAL */
130 }
131
132 float
133 VSTPlugin::get_parameter (uint32_t which) const
134 {
135         return _plugin->getParameter (_plugin, which);
136
137 }
138
139 uint32_t
140 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
141 {
142         ok = true;
143         return n;
144 }
145
146 XMLNode&
147 VSTPlugin::get_state()
148 {
149         XMLNode *root = new XMLNode (state_node_name());
150         LocaleGuard lg (X_("POSIX"));
151
152         if (_fst->current_program != -1) {
153                 char buf[32];
154                 snprintf (buf, sizeof (buf), "%d", _fst->current_program);
155                 root->add_property ("current-program", buf);
156         }
157
158         if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
159
160                 /* fetch the current chunk */
161
162                 guchar* data;
163                 int32_t data_size;
164
165                 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
166                         return *root;
167                 }
168
169                 /* store information */
170
171                 XMLNode* chunk_node = new XMLNode (X_("chunk"));
172
173                 gchar * encoded_data = g_base64_encode (data, data_size);
174                 chunk_node->add_content (encoded_data);
175                 g_free (encoded_data);
176
177                 root->add_child_nocopy (*chunk_node);
178
179         } else {
180
181                 XMLNode* parameters = new XMLNode ("parameters");
182
183                 for (int32_t n = 0; n < _plugin->numParams; ++n) {
184                         char index[64];
185                         char val[32];
186                         snprintf (index, sizeof (index), "param_%ld", n);
187                         snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
188                         parameters->add_property (index, val);
189                 }
190
191                 root->add_child_nocopy (*parameters);
192         }
193
194         return *root;
195 }
196
197 int
198 VSTPlugin::set_state(const XMLNode& node, int)
199 {
200         LocaleGuard lg (X_("POSIX"));
201
202         if (node.name() != state_node_name()) {
203                 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
204                 return 0;
205         }
206
207         const XMLProperty* prop;
208
209         if ((prop = node.property ("current-program")) != 0) {
210                 _fst->current_program = atoi (prop->value().c_str());
211         }
212
213         XMLNode* child;
214         int ret = -1;
215
216         if ((child = find_named_node (node, X_("chunk"))) != 0) {
217
218                 XMLPropertyList::const_iterator i;
219                 XMLNodeList::const_iterator n;
220                 int ret = -1;
221
222                 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
223                         if ((*n)->is_content ()) {
224                                 gsize chunk_size = 0;
225                                 guchar * data = g_base64_decode ((*n)->content ().c_str (), &chunk_size);
226                                 //cerr << "Dispatch setChunk for " << name() << endl;
227                                 ret = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, 0, chunk_size, data, 0);
228                                 g_free (data);
229                         }
230                 }
231
232         } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
233
234                 XMLPropertyList::const_iterator i;
235
236                 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
237                         int32_t param;
238                         float val;
239
240                         sscanf ((*i)->name().c_str(), "param_%d", &param);
241                         sscanf ((*i)->value().c_str(), "%f", &val);
242
243                         _plugin->setParameter (_plugin, param, val);
244                 }
245
246                 /* program number is not knowable */
247
248                 _fst->current_program = -1;
249
250                 ret = 0;
251
252         }
253
254         return ret;
255 }
256
257 int
258 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
259 {
260         VstParameterProperties prop;
261
262         desc.min_unbound = false;
263         desc.max_unbound = false;
264         prop.flags = 0;
265
266         if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
267
268 #ifdef VESTIGE_COMPLETE
269
270                 /* i have yet to find or hear of a VST plugin that uses this */
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                 desc.logarithmic = false;
303                 desc.sr_dependent = false;
304                 desc.label = prop.label;
305 #endif
306
307         } else {
308
309                 /* old style */
310
311                 char label[64];
312                 label[0] = '\0';
313
314                 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
315
316                 desc.label = label;
317                 desc.integer_step = false;
318                 desc.lower = 0.0f;
319                 desc.upper = 1.0f;
320                 desc.step = 0.01f;
321                 desc.smallstep = 0.005f;
322                 desc.largestep = 0.1f;
323                 desc.toggled = false;
324                 desc.logarithmic = false;
325                 desc.sr_dependent = false;
326         }
327
328         return 0;
329 }
330
331 bool
332 VSTPlugin::load_preset (const string& name)
333 {
334         if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
335                 error << _("no support for presets using chunks at this time")
336                       << endmsg;
337                 return false;
338         }
339         return Plugin::load_preset (name);
340 }
341
342 bool
343 VSTPlugin::save_preset (string name)
344 {
345         if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
346                 error << _("no support for presets using chunks at this time")
347                       << endmsg;
348                 return false;
349         }
350         return Plugin::save_preset (name, "vst");
351 }
352
353 string
354 VSTPlugin::describe_parameter (Evoral::Parameter param)
355 {
356         char name[64];
357         _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
358         return name;
359 }
360
361 nframes_t
362 VSTPlugin::signal_latency () const
363 {
364         if (_user_latency) {
365                 return _user_latency;
366         }
367
368 #ifdef VESTIGE_HEADER
369         return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
370 #else
371         return _plugin->initial_delay;
372 #endif
373 }
374
375 set<Evoral::Parameter>
376 VSTPlugin::automatable () const
377 {
378         set<Evoral::Parameter> ret;
379
380         for (uint32_t i = 0; i < parameter_count(); ++i){
381                 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
382         }
383
384         return ret;
385 }
386
387 int
388 VSTPlugin::connect_and_run (BufferSet& bufs,
389                 ChanMapping in_map, ChanMapping out_map,
390                 nframes_t nframes, nframes_t offset)
391 {
392         float *ins[_plugin->numInputs];
393         float *outs[_plugin->numOutputs];
394         int32_t i;
395
396         const uint32_t nbufs = bufs.count().n_audio();
397
398         int in_index = 0;
399         for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
400                 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
401                 in_index++;
402         }
403
404         int out_index = 0;
405         for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
406                 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
407
408                 /* unbelievably, several VST plugins still rely on Cubase
409                    behaviour and do not silence the buffer in processReplacing
410                    when they have no output.
411                 */
412
413                 // memset (outs[i], 0, sizeof (Sample) * nframes);
414                 out_index++;
415         }
416
417
418         if (bufs.count().n_midi() > 0) {
419                 VstEvents* v = bufs.get_vst_midi (0);
420                 _plugin->dispatcher (_plugin, effProcessEvents, 0, 0, v, 0);
421         }
422
423         /* we already know it can support processReplacing */
424
425         _plugin->processReplacing (_plugin, ins, outs, nframes);
426
427         return 0;
428 }
429
430 void
431 VSTPlugin::deactivate ()
432 {
433         _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
434 }
435
436 void
437 VSTPlugin::activate ()
438 {
439         _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
440 }
441
442 string
443 VSTPlugin::unique_id() const
444 {
445         char buf[32];
446
447 #ifdef VESTIGE_HEADER
448        snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
449 #else
450        snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
451 #endif
452        return string (buf);
453 }
454
455
456 const char *
457 VSTPlugin::name () const
458 {
459         return handle->name;
460 }
461
462 const char *
463 VSTPlugin::maker () const
464 {
465         return "imadeit";
466 }
467
468 const char *
469 VSTPlugin::label () const
470 {
471         return handle->name;
472 }
473
474 uint32_t
475 VSTPlugin::parameter_count() const
476 {
477         return _plugin->numParams;
478 }
479
480 bool
481 VSTPlugin::has_editor () const
482 {
483         return _plugin->flags & effFlagsHasEditor;
484 }
485
486 void
487 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
488 {
489         char *first_nonws;
490
491         _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
492
493         if (buf[0] == '\0') {
494                 return;
495         }
496
497         first_nonws = buf;
498         while (*first_nonws && isspace (*first_nonws)) {
499                 first_nonws++;
500         }
501         if (*first_nonws == '\0') {
502                 return;
503         }
504
505         memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
506 }
507
508 PluginPtr
509 VSTPluginInfo::load (Session& session)
510 {
511         try {
512                 PluginPtr plugin;
513
514                 if (Config->get_use_vst()) {
515                         FSTHandle* handle;
516
517                         handle = fst_load(path.c_str());
518
519                         if ( (int)handle == -1) {
520                                 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
521                         } else {
522                                 plugin.reset (new VSTPlugin (session.engine(), session, handle));
523                         }
524                 } else {
525                         error << _("You asked ardour to not use any VST plugins") << endmsg;
526                         return PluginPtr ((Plugin*) 0);
527                 }
528
529                 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
530                 return plugin;
531         }
532
533         catch (failed_constructor &err) {
534                 return PluginPtr ((Plugin*) 0);
535         }
536 }
537
538 VSTPluginInfo::VSTPluginInfo()
539 {
540        type = ARDOUR::VST;
541 }