revert VST debugging hacks
[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         cerr << name() << " dispatch  effSetSampleRate\n";
74         _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL, 
75                              (float) session.frame_rate());
76         cerr << name() << " dispatch  effSetBlockSize\n";
77         _plugin->dispatcher (_plugin, effSetBlockSize, 0, 
78                              session.get_block_size(), NULL, 0.0f);
79         
80         /* set program to zero */
81
82         cerr << name() << " dispatch  effSetProgram\n";
83         _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
84         
85         Plugin::setup_controls ();
86 }
87
88 VSTPlugin::VSTPlugin (const VSTPlugin &other)
89         : Plugin (other)
90 {
91         handle = other.handle;
92
93         if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
94                 throw failed_constructor();
95         }
96         _plugin = _fst->plugin;
97
98         Plugin::setup_controls ();
99 }
100
101 VSTPlugin::~VSTPlugin ()
102 {
103         deactivate ();
104         GoingAway (); /* EMIT SIGNAL */
105         fst_close (_fst);
106 }
107
108 void
109 VSTPlugin::set_block_size (nframes_t nframes)
110 {
111         deactivate ();
112         cerr << name() << " dispatch  effSetBlockSize\n";
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 /* effFlagsProgramChunks */) {
151
152                 /* fetch the current chunk */
153                 
154                 void* data;
155                 long  data_size;
156                 
157                 cerr << name() << " dispatch = GetChunk\n";
158                 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
159                         return *root;
160                 }
161
162                 /* save it to a file */
163
164                 Glib::ustring path = Glib::build_filename (get_user_ardour_path (), "vst");
165                 struct stat sbuf;
166
167                 if (stat (path.c_str(), &sbuf)) {
168                         if (errno == ENOENT) {
169                                 if (g_mkdir_with_parents (path.c_str(), 0600)) {
170                                         error << string_compose (_("cannot create VST chunk directory: %1"),
171                                                                  strerror (errno))
172                                               << endmsg;
173                                         return *root;
174                                 }
175
176                         } else {
177
178                                 warning << string_compose (_("cannot check VST chunk directory: %1"), strerror (errno))
179                                         << endmsg;
180                                 return *root;
181                         }
182
183                 } else if (!S_ISDIR (sbuf.st_mode)) {
184                         error << string_compose (_("%1 exists but is not a directory"), path)
185                               << endmsg;
186                         return *root;
187                 }
188                 
189                 path = Glib::build_filename (path, "something");
190                 
191                 /* store information */
192
193                 XMLNode* chunk_node = new XMLNode (X_("chunk"));
194                 chunk_node->add_property ("path", path);
195                 
196                 root->add_child_nocopy (*chunk_node);
197                 
198         } else {
199
200                 XMLNode* parameters = new XMLNode ("parameters");
201
202                 for (long n = 0; n < _plugin->numParams; ++n) {
203                         char index[64];
204                         char val[32];
205                         snprintf (index, sizeof (index), "param_%ld", n);
206                         snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
207                         parameters->add_property (index, val);
208                 }
209
210                 root->add_child_nocopy (*parameters);
211
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         cerr << name() << " dispatch ParamProps\n";
262         if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
263
264 #ifdef VESTIGE_COMPLETE
265                 /* i have yet to find or hear of a VST plugin that uses this */
266
267                 if (prop.flags & kVstParameterUsesIntegerMinMax) {
268                         desc.lower = prop.minInteger;
269                         desc.upper = prop.maxInteger;
270                 } else {
271                         desc.lower = 0;
272                         desc.upper = 1.0;
273                 }
274                 
275                 if (prop.flags & kVstParameterUsesIntStep) {
276                         
277                         desc.step = prop.stepInteger;
278                         desc.smallstep = prop.stepInteger;
279                         desc.largestep = prop.stepInteger;
280                         
281                 } else if (prop.flags & kVstParameterUsesFloatStep) {
282                         
283                         desc.step = prop.stepFloat;
284                         desc.smallstep = prop.smallStepFloat;
285                         desc.largestep = prop.largeStepFloat;
286                         
287                 } else {
288                         
289                         float range = desc.upper - desc.lower;
290                         
291                         desc.step = range / 100.0f;
292                         desc.smallstep = desc.step / 2.0f;
293                         desc.largestep = desc.step * 10.0f;
294                 }
295                 
296                 desc.toggled = prop.flags & kVstParameterIsSwitch;
297                 desc.logarithmic = false;
298                 desc.sr_dependent = false;
299                 desc.label = prop.label;
300 #endif
301
302         } else {
303
304                 /* old style */
305
306                 char label[64];
307                 label[0] = '\0';
308
309                 cerr << name() << " dispatch  effGetParamName\n";
310                 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
311
312                 desc.label = label;
313                 desc.integer_step = false;
314                 desc.lower = 0.0f;
315                 desc.upper = 1.0f;
316                 desc.step = 0.01f;
317                 desc.smallstep = 0.005f;
318                 desc.largestep = 0.1f;
319                 desc.toggled = false;
320                 desc.logarithmic = false;
321                 desc.sr_dependent = false;
322         }
323
324         return 0;
325 }
326
327 bool
328 VSTPlugin::load_preset (string name)
329 {
330
331         if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
332                 error << _("no support for presets using chunks at this time")
333                       << endmsg;
334                 return false;
335         }
336         return Plugin::load_preset (name);
337 }
338
339 bool
340 VSTPlugin::save_preset (string name)
341 {
342         if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
343                 error << _("no support for presets using chunks at this time")
344                       << endmsg;
345                 return false;
346         }
347
348         return Plugin::save_preset (name, "vst");
349 }
350
351 string
352 VSTPlugin::describe_parameter (uint32_t param)
353 {
354         char name[64];
355         cerr << this->name() << " dispatch  effGetParamName\n";
356         _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
357         return name;
358 }
359
360 nframes_t
361 VSTPlugin::latency () const
362 {
363 #ifdef VESTIGE_HEADER
364         return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
365 #else
366         return 0;
367 #endif
368 }
369
370 set<uint32_t>
371 VSTPlugin::automatable () const
372 {
373         set<uint32_t> ret;
374
375         for (uint32_t i = 0; i < parameter_count(); ++i){
376                 ret.insert (ret.end(), i);
377         }
378
379         return ret;
380 }
381
382 int
383 VSTPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in_index, int32_t& out_index, nframes_t nframes, nframes_t offset)
384 {
385         float *ins[_plugin->numInputs];
386         float *outs[_plugin->numOutputs];
387         int32_t i;
388
389
390         for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
391                 ins[i] = bufs[min((uint32_t) in_index,maxbuf - 1)] + offset;
392                 in_index++;
393         }
394
395         for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
396                 outs[i] = bufs[min((uint32_t) out_index,maxbuf - 1)] + offset;
397
398                 /* unbelievably, several VST plugins still rely on Cubase
399                    behaviour and do not silence the buffer in processReplacing 
400                    when they have no output.
401                 */
402                 
403                 // memset (outs[i], 0, sizeof (Sample) * nframes);
404                 out_index++;
405         }
406
407
408         /* we already know it can support processReplacing */
409
410         cerr << "++++++++++ " << name() << " RUN in " << pthread_self() << endl;
411         _plugin->processReplacing (_plugin, ins, outs, nframes);
412         cerr << "\tdone\n";
413
414         return 0;
415 }
416
417 void
418 VSTPlugin::deactivate ()
419 {
420         cerr << "\n\n\n ************ " << name() << " DEACTIVATE in " << pthread_self() << endl;
421         _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
422 }
423
424 void
425 VSTPlugin::activate ()
426 {
427         cerr << "\n\n\n ************ " << name() << " ACTIVATE in " << pthread_self() << endl;
428         _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
429 }
430
431 string
432 VSTPlugin::unique_id() const
433 {
434         char buf[32];
435 #ifdef VESTIGE_HEADER
436         snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
437 #else
438         snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
439 #endif
440         return string (buf);
441 }
442
443
444 const char *
445 VSTPlugin::name () const
446 {
447         return handle->name;
448 }
449
450 const char *
451 VSTPlugin::maker () const
452 {
453         return "imadeit";
454 }
455
456 const char *
457 VSTPlugin::label () const
458 {
459         return handle->name;
460 }
461
462 uint32_t
463 VSTPlugin::parameter_count() const
464 {
465         return _plugin->numParams;
466 }
467
468 bool
469 VSTPlugin::has_editor () const
470 {
471         return _plugin->flags & effFlagsHasEditor;
472 }
473
474 void
475 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
476 {
477         char *first_nonws;
478
479         cerr << name() << " dispatch  paramDisplay\n";
480         _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
481
482         if (buf[0] == '\0') {
483                 return;
484         }
485
486         first_nonws = buf;
487         while (*first_nonws && isspace (*first_nonws)) {
488                 first_nonws++;
489         }
490         if (*first_nonws == '\0') {
491                 return;
492         }
493
494         memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
495 }
496
497 PluginPtr
498 VSTPluginInfo::load (Session& session)
499 {
500         try {
501                 PluginPtr plugin;
502
503                 if (Config->get_use_vst()) {
504                         FSTHandle* handle;
505                         
506                         handle = fst_load(path.c_str());
507         
508                         if ( (int)handle == -1) {
509                                 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
510                         } else {
511                                 plugin.reset (new VSTPlugin (session.engine(), session, handle));
512                         }
513                 } else {
514                         error << _("You asked ardour to not use any VST plugins") << endmsg;
515                         return PluginPtr ((Plugin*) 0);
516                 }
517
518                 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
519                 return plugin;
520         }       
521
522         catch (failed_constructor &err) {
523                 return PluginPtr ((Plugin*) 0);
524         }
525 }