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