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