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