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