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