Fix VST build.
[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 (nframes_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_%ld", 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 string
353 VSTPlugin::describe_parameter (Evoral::Parameter param)
354 {
355         char name[64];
356         _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
357         return name;
358 }
359
360 nframes_t
361 VSTPlugin::signal_latency () const
362 {
363         if (_user_latency) {
364                 return _user_latency;
365         }
366
367 #ifdef VESTIGE_HEADER
368         return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
369 #else
370         return _plugin->initial_delay;
371 #endif
372 }
373
374 set<Evoral::Parameter>
375 VSTPlugin::automatable () const
376 {
377         set<Evoral::Parameter> ret;
378
379         for (uint32_t i = 0; i < parameter_count(); ++i){
380                 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
381         }
382
383         return ret;
384 }
385
386 int
387 VSTPlugin::connect_and_run (BufferSet& bufs,
388                 ChanMapping in_map, ChanMapping out_map,
389                 nframes_t nframes, nframes_t offset)
390 {
391         float *ins[_plugin->numInputs];
392         float *outs[_plugin->numOutputs];
393         int32_t i;
394
395         const uint32_t nbufs = bufs.count().n_audio();
396
397         int in_index = 0;
398         for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
399                 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
400                 in_index++;
401         }
402
403         int out_index = 0;
404         for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
405                 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + 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
417         if (bufs.count().n_midi() > 0) {
418                 VstEvents* v = bufs.get_vst_midi (0);
419                 _plugin->dispatcher (_plugin, effProcessEvents, 0, 0, v, 0);
420         }
421
422         /* we already know it can support processReplacing */
423
424         _plugin->processReplacing (_plugin, ins, outs, nframes);
425
426         return 0;
427 }
428
429 void
430 VSTPlugin::deactivate ()
431 {
432         _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
433 }
434
435 void
436 VSTPlugin::activate ()
437 {
438         _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
439 }
440
441 string
442 VSTPlugin::unique_id() const
443 {
444         char buf[32];
445
446 #ifdef VESTIGE_HEADER
447        snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
448 #else
449        snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
450 #endif
451        return string (buf);
452 }
453
454
455 const char *
456 VSTPlugin::name () const
457 {
458         return handle->name;
459 }
460
461 const char *
462 VSTPlugin::maker () const
463 {
464         return "imadeit";
465 }
466
467 const char *
468 VSTPlugin::label () const
469 {
470         return handle->name;
471 }
472
473 uint32_t
474 VSTPlugin::parameter_count() const
475 {
476         return _plugin->numParams;
477 }
478
479 bool
480 VSTPlugin::has_editor () const
481 {
482         return _plugin->flags & effFlagsHasEditor;
483 }
484
485 void
486 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
487 {
488         char *first_nonws;
489
490         _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
491
492         if (buf[0] == '\0') {
493                 return;
494         }
495
496         first_nonws = buf;
497         while (*first_nonws && isspace (*first_nonws)) {
498                 first_nonws++;
499         }
500         if (*first_nonws == '\0') {
501                 return;
502         }
503
504         memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
505 }
506
507 PluginPtr
508 VSTPluginInfo::load (Session& session)
509 {
510         try {
511                 PluginPtr plugin;
512
513                 if (Config->get_use_vst()) {
514                         FSTHandle* handle;
515
516                         handle = fst_load(path.c_str());
517
518                         if ( (int)handle == -1) {
519                                 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
520                         } else {
521                                 plugin.reset (new VSTPlugin (session.engine(), session, handle));
522                         }
523                 } else {
524                         error << _("You asked ardour to not use any VST plugins") << endmsg;
525                         return PluginPtr ((Plugin*) 0);
526                 }
527
528                 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
529                 return plugin;
530         }
531
532         catch (failed_constructor &err) {
533                 return PluginPtr ((Plugin*) 0);
534         }
535 }
536
537 VSTPluginInfo::VSTPluginInfo()
538 {
539        type = ARDOUR::VST;
540 }