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