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