Merge big changes (mostly Controllable) from trunk
[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     $Id$
19 */
20
21 #include <algorithm>
22 #include <vector>
23 #include <string>
24 #include <ctype.h>
25
26 #include <cstdlib>
27 #include <cstdio> // so libraptor doesn't complain
28 #include <cmath>
29 #include <dirent.h>
30 #include <string.h> // for memmove
31 #include <sys/stat.h>
32 #include <cerrno>
33
34 #include <lrdf.h>
35 #include <fst.h>
36
37 #include <pbd/compose.h>
38 #include <pbd/error.h>
39 #include <pbd/pathscanner.h>
40 #include <pbd/xml++.h>
41
42 #include <vst/aeffectx.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 (this); /* EMIT SIGNAL */
102         fst_close (_fst);
103 }
104
105 void
106 VSTPlugin::set_block_size (jack_nframes_t nframes)
107 {
108         deactivate ();
109         _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
110         activate ();
111 }
112
113 void
114 VSTPlugin::store_state (PluginState& state)
115 {
116 }
117
118 void
119 VSTPlugin::restore_state (PluginState& state)
120 {
121 }
122
123 float
124 VSTPlugin::default_value (uint32_t port)
125 {
126         return 0;
127 }       
128
129 void
130 VSTPlugin::set_parameter (uint32_t which, float val)
131 {
132         _plugin->setParameter (_plugin, which, val);
133         ParameterChanged (which, val); /* EMIT SIGNAL */
134 }
135
136 float
137 VSTPlugin::get_parameter (uint32_t which) const
138 {
139         return _plugin->getParameter (_plugin, which);
140         
141 }
142
143 uint32_t
144 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
145 {
146         ok = true;
147         return n;
148 }
149
150 XMLNode&
151 VSTPlugin::get_state()
152 {
153         XMLNode *root = new XMLNode (state_node_name());
154         LocaleGuard lg (X_("POSIX"));
155         
156         if (_plugin->flags & effFlagsProgramChunks) {
157
158                 /* fetch the current chunk */
159                 
160                 void* data;
161                 long  data_size;
162                 
163                 if ((data_size = _plugin->dispatcher (_plugin, effGetChunk, 0, 0, &data, false)) == 0) {
164                         return *root;
165                 }
166
167                 /* save it to a file */
168
169                 string path;
170                 struct stat sbuf;
171
172                 path = getenv ("HOME");
173                 path += "/.ardour/vst";
174
175                 if (stat (path.c_str(), &sbuf)) {
176                         if (errno == ENOENT) {
177                                 if (mkdir (path.c_str(), 0600)) {
178                                         error << string_compose (_("cannot create VST chunk directory: %1"),
179                                                           strerror (errno))
180                                               << endmsg;
181                                         return *root;
182                                 }
183
184                         } else {
185
186                                 error << string_compose (_("cannot check VST chunk directory: %1"),
187                                                   strerror (errno))
188                                       << endmsg;
189                                 return *root;
190                         }
191
192                 } else if (!S_ISDIR (sbuf.st_mode)) {
193                         error << string_compose (_("%1 exists but is not a directory"), path)
194                               << endmsg;
195                         return *root;
196                 }
197                 
198                 path += "something";
199                 
200                 /* store information */
201
202                 XMLNode* chunk_node = new XMLNode (X_("chunk"));
203                 chunk_node->add_property ("path", path);
204                 
205                 root->add_child_nocopy (*chunk_node);
206                 
207         } else {
208
209                 XMLNode* parameters = new XMLNode ("parameters");
210
211                 for (long n = 0; n < _plugin->numParams; ++n) {
212                         char index[64];
213                         char val[32];
214                         snprintf (index, sizeof (index), "param_%ld", n);
215                         snprintf (val, sizeof (val), "%f", _plugin->getParameter (_plugin, n));
216                         parameters->add_property (index, val);
217                 }
218
219                 root->add_child_nocopy (*parameters);
220         }
221
222         return *root;
223 }
224
225 int
226 VSTPlugin::set_state(const XMLNode& node)
227 {
228         LocaleGuard lg (X_("POSIX"));
229
230         if (node.name() != state_node_name()) {
231                 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
232                 return 0;
233         }
234
235         XMLNode* child;
236
237         if ((child = find_named_node (node, X_("chunks"))) != 0) {
238
239                 return 0;
240
241         } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
242                 
243                 XMLPropertyList::const_iterator i;
244
245                 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
246                         long param;
247                         float val;
248                         sscanf ((*i)->name().c_str(), "param_%ld", &param);
249                         sscanf ((*i)->value().c_str(), "%f", &val);
250
251                         _plugin->setParameter (_plugin, param, val);
252                 }
253
254                 return 0;
255         }
256
257         return -1;
258 }
259
260 int
261 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
262 {
263         VstParameterProperties prop;
264
265         desc.min_unbound = false;
266         desc.max_unbound = false;
267
268         if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
269
270                 /* i have yet to find or hear of a VST plugin that uses this */
271
272                 if (prop.flags & kVstParameterUsesIntegerMinMax) {
273                         desc.lower = prop.minInteger;
274                         desc.upper = prop.maxInteger;
275                 } else {
276                         desc.lower = 0;
277                         desc.upper = 1.0;
278                 }
279                 
280                 if (prop.flags & kVstParameterUsesIntStep) {
281                         
282                         desc.step = prop.stepInteger;
283                         desc.smallstep = prop.stepInteger;
284                         desc.largestep = prop.stepInteger;
285                         
286                 } else if (prop.flags & kVstParameterUsesFloatStep) {
287                         
288                         desc.step = prop.stepFloat;
289                         desc.smallstep = prop.smallStepFloat;
290                         desc.largestep = prop.largeStepFloat;
291                         
292                 } else {
293                         
294                         float range = desc.upper - desc.lower;
295                         
296                         desc.step = range / 100.0f;
297                         desc.smallstep = desc.step / 2.0f;
298                         desc.largestep = desc.step * 10.0f;
299                 }
300                 
301                 desc.toggled = prop.flags & kVstParameterIsSwitch;
302                 desc.logarithmic = false;
303                 desc.sr_dependent = false;
304                 desc.label = prop.label;
305
306         } else {
307
308                 /* old style */
309
310                 char label[64];
311                 label[0] = '\0';
312
313                 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
314
315                 desc.label = label;
316                 desc.integer_step = false;
317                 desc.lower = 0.0f;
318                 desc.upper = 1.0f;
319                 desc.step = 0.01f;
320                 desc.smallstep = 0.005f;
321                 desc.largestep = 0.1f;
322                 desc.toggled = false;
323                 desc.logarithmic = false;
324                 desc.sr_dependent = false;
325         }
326
327         return 0;
328 }
329
330 bool
331 VSTPlugin::load_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::load_preset (name);
339 }
340
341 bool
342 VSTPlugin::save_preset (string name)
343 {
344         if (_plugin->flags & effFlagsProgramChunks) {
345                 error << _("no support for presets using chunks at this time")
346                       << endmsg;
347                 return false;
348         }
349         return Plugin::save_preset (name, "vst");
350 }
351
352 string
353 VSTPlugin::describe_parameter (uint32_t param)
354 {
355         char name[64];
356         _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
357         return name;
358 }
359
360 jack_nframes_t
361 VSTPlugin::latency () const
362 {
363         return _plugin->initialDelay;
364 }
365
366 set<uint32_t>
367 VSTPlugin::automatable () const
368 {
369         set<uint32_t> ret;
370
371         for (uint32_t i = 0; i < parameter_count(); ++i){
372                 ret.insert (ret.end(), i);
373         }
374
375         return ret;
376 }
377
378 int
379 VSTPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in_index, int32_t& out_index, jack_nframes_t nframes, jack_nframes_t offset)
380 {
381         float *ins[_plugin->numInputs];
382         float *outs[_plugin->numOutputs];
383         int32_t i;
384
385         for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
386                 ins[i] = bufs[min((uint32_t) in_index,maxbuf - 1)] + offset;
387                 in_index++;
388         }
389
390         for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
391                 outs[i] = bufs[min((uint32_t) out_index,maxbuf - 1)] + offset;
392
393                 /* unbelievably, several VST plugins still rely on Cubase
394                    behaviour and do not silence the buffer in processReplacing 
395                    when they have no output.
396                 */
397                 
398                 // memset (outs[i], 0, sizeof (Sample) * nframes);
399                 out_index++;
400         }
401
402
403         /* we already know it can support processReplacing */
404
405         _plugin->processReplacing (_plugin, ins, outs, nframes);
406         
407         return 0;
408 }
409
410 void
411 VSTPlugin::deactivate ()
412 {
413         _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
414 }
415
416 void
417 VSTPlugin::activate ()
418 {
419         _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
420 }
421
422 uint32_t 
423 VSTPlugin::unique_id() const
424 {
425         return _plugin->uniqueID;
426 }
427
428
429 const char *
430 VSTPlugin::name () const
431 {
432         return handle->name;
433 }
434
435 const char *
436 VSTPlugin::maker () const
437 {
438         return "imadeit";
439 }
440
441 const char *
442 VSTPlugin::label () const
443 {
444         return handle->name;
445 }
446
447 uint32_t
448 VSTPlugin::parameter_count() const
449 {
450         return _plugin->numParams;
451 }
452
453 bool
454 VSTPlugin::has_editor () const
455 {
456         return _plugin->flags & effFlagsHasEditor;
457 }
458
459 void
460 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
461 {
462         char lab[9];
463         char *first_nonws;
464
465         _plugin->dispatcher (_plugin, effGetParamLabel, param, 0, lab, 0);
466         _plugin->dispatcher (_plugin, 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 }