tweaks to get a VST-supporting 3.0 to build & startup
[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         GoingAway (); /* EMIT SIGNAL */
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 (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
151
152                 /* fetch the current chunk */
153                 
154                 void* data;
155                 long  data_size;
156                 
157                 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
158                         return *root;
159                 }
160
161                 /* save it to a file */
162                 
163                 sys::path user_vst_directory(user_config_directory());
164                 user_vst_directory /= "vst";
165
166                 try {
167                         sys::create_directories (user_vst_directory);   
168                 }
169                 catch (const sys::filesystem_error& ex)
170                 {
171                         error << "Could not create user configuration directory" << endmsg;
172                         return *root;
173                 }
174                 
175                 sys::path file (user_vst_directory);
176                 
177                 file /= "something";
178                 
179                 /* store information */
180
181                 XMLNode* chunk_node = new XMLNode (X_("chunk"));
182                 chunk_node->add_property ("path", file.to_string());
183                 
184                 root->add_child_nocopy (*chunk_node);
185                 
186         } else {
187
188                 XMLNode* parameters = new XMLNode ("parameters");
189
190                 for (long n = 0; n < _plugin->numParams; ++n) {
191                         char index[64];
192                         char val[32];
193                         snprintf (index, sizeof (index), "param_%ld", n);
194                         snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
195                         parameters->add_property (index, val);
196                 }
197
198                 root->add_child_nocopy (*parameters);
199         }
200
201         return *root;
202 }
203
204 int
205 VSTPlugin::set_state(const XMLNode& node)
206 {
207         LocaleGuard lg (X_("POSIX"));
208
209         if (node.name() != state_node_name()) {
210                 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
211                 return 0;
212         }
213
214         XMLNode* child;
215
216         if ((child = find_named_node (node, X_("chunks"))) != 0) {
217
218                 return 0;
219
220         } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
221                 
222                 XMLPropertyList::const_iterator i;
223
224                 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
225                         long param;
226                         float val;
227
228                         sscanf ((*i)->name().c_str(), "param_%ld", &param);
229                         sscanf ((*i)->value().c_str(), "%f", &val);
230
231                         _plugin->setParameter (_plugin, param, val);
232                 }
233
234                 return 0;
235         }
236
237         return -1;
238 }
239
240 int
241 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
242 {
243         VstParameterProperties prop;
244
245         desc.min_unbound = false;
246         desc.max_unbound = false;
247
248         if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
249
250 #ifdef VESTIGE_COMPLETE
251
252                 /* i have yet to find or hear of a VST plugin that uses this */
253
254                 if (prop.flags & kVstParameterUsesIntegerMinMax) {
255                         desc.lower = prop.minInteger;
256                         desc.upper = prop.maxInteger;
257                 } else {
258                         desc.lower = 0;
259                         desc.upper = 1.0;
260                 }
261                 
262                 if (prop.flags & kVstParameterUsesIntStep) {
263                         
264                         desc.step = prop.stepInteger;
265                         desc.smallstep = prop.stepInteger;
266                         desc.largestep = prop.stepInteger;
267                         
268                 } else if (prop.flags & kVstParameterUsesFloatStep) {
269                         
270                         desc.step = prop.stepFloat;
271                         desc.smallstep = prop.smallStepFloat;
272                         desc.largestep = prop.largeStepFloat;
273                         
274                 } else {
275                         
276                         float range = desc.upper - desc.lower;
277                         
278                         desc.step = range / 100.0f;
279                         desc.smallstep = desc.step / 2.0f;
280                         desc.largestep = desc.step * 10.0f;
281                 }
282                 
283                 desc.toggled = prop.flags & kVstParameterIsSwitch;
284                 desc.logarithmic = false;
285                 desc.sr_dependent = false;
286                 desc.label = prop.label;
287 #endif
288
289         } else {
290
291                 /* old style */
292
293                 char label[64];
294                 label[0] = '\0';
295
296                 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
297
298                 desc.label = label;
299                 desc.integer_step = false;
300                 desc.lower = 0.0f;
301                 desc.upper = 1.0f;
302                 desc.step = 0.01f;
303                 desc.smallstep = 0.005f;
304                 desc.largestep = 0.1f;
305                 desc.toggled = false;
306                 desc.logarithmic = false;
307                 desc.sr_dependent = false;
308         }
309
310         return 0;
311 }
312
313 bool
314 VSTPlugin::load_preset (string name)
315 {
316         if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
317                 error << _("no support for presets using chunks at this time")
318                       << endmsg;
319                 return false;
320         }
321         return Plugin::load_preset (name);
322 }
323
324 bool
325 VSTPlugin::save_preset (string name)
326 {
327         if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
328                 error << _("no support for presets using chunks at this time")
329                       << endmsg;
330                 return false;
331         }
332         return Plugin::save_preset (name, "vst");
333 }
334
335 string
336 VSTPlugin::describe_parameter (Evoral::Parameter param)
337 {
338         char name[64];
339         _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
340         return name;
341 }
342
343 nframes_t
344 VSTPlugin::signal_latency () const
345 {
346         if (_user_latency) {
347                 return _user_latency;
348         }
349
350 #ifdef VESTIGE_HEADER
351         return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
352 #else
353         return _plugin->initial_delay;
354 #endif
355 }
356
357 set<Evoral::Parameter>
358 VSTPlugin::automatable () const
359 {
360         set<Evoral::Parameter> ret;
361
362         for (uint32_t i = 0; i < parameter_count(); ++i){
363                 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
364         }
365
366         return ret;
367 }
368
369 int
370 VSTPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, nframes_t nframes, nframes_t offset)
371 {
372         float *ins[_plugin->numInputs];
373         float *outs[_plugin->numOutputs];
374         int32_t i;
375
376         const uint32_t nbufs = bufs.count().n_audio();
377
378         for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
379                 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
380                 in_index++;
381         }
382
383         for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
384                 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
385
386                 /* unbelievably, several VST plugins still rely on Cubase
387                    behaviour and do not silence the buffer in processReplacing 
388                    when they have no output.
389                 */
390                 
391                 // memset (outs[i], 0, sizeof (Sample) * nframes);
392                 out_index++;
393         }
394
395
396         /* we already know it can support processReplacing */
397
398         _plugin->processReplacing (_plugin, ins, outs, nframes);
399         
400         return 0;
401 }
402
403 void
404 VSTPlugin::deactivate ()
405 {
406         _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
407 }
408
409 void
410 VSTPlugin::activate ()
411 {
412         _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
413 }
414
415 string
416 VSTPlugin::unique_id() const
417 {
418         char buf[32];
419
420 #ifdef VESTIGE_HEADER
421        snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
422 #else
423        snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
424 #endif
425        return string (buf);
426 }
427
428
429 const char *
430 VSTPlugin::name () const
431 {
432         return handle->name;
433 }
434
435 const char *
436 VSTPlugin::maker () const
437 {
438         return "imadeit";
439 }
440
441 const char *
442 VSTPlugin::label () const
443 {
444         return handle->name;
445 }
446
447 uint32_t
448 VSTPlugin::parameter_count() const
449 {
450         return _plugin->numParams;
451 }
452
453 bool
454 VSTPlugin::has_editor () const
455 {
456         return _plugin->flags & effFlagsHasEditor;
457 }
458
459 void
460 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
461 {
462         char *first_nonws;
463
464         _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
465
466         if (buf[0] == '\0') {
467                 return;
468         }
469
470         first_nonws = buf;
471         while (*first_nonws && isspace (*first_nonws)) {
472                 first_nonws++;
473         }
474         if (*first_nonws == '\0') {
475                 return;
476         }
477
478         memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
479 }
480
481 PluginPtr
482 VSTPluginInfo::load (Session& session)
483 {
484         try {
485                 PluginPtr plugin;
486
487                 if (Config->get_use_vst()) {
488                         FSTHandle* handle;
489                         
490                         handle = fst_load(path.c_str());
491         
492                         if ( (int)handle == -1) {
493                                 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
494                         } else {
495                                 plugin.reset (new VSTPlugin (session.engine(), session, handle));
496                         }
497                 } else {
498                         error << _("You asked ardour to not use any VST plugins") << endmsg;
499                         return PluginPtr ((Plugin*) 0);
500                 }
501
502                 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
503                 return plugin;
504         }       
505
506         catch (failed_constructor &err) {
507                 return PluginPtr ((Plugin*) 0);
508         }
509 }