fixes for OS X compile of last commit
[ardour2.git] / libs / ardour / vst_plugin.cc
blob2da534f1782dd88b5b0e23f0413e456a3d51c7f1
1 /*
2 Copyright (C) 2004 Paul Davis
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.
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.
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.
20 #include <algorithm>
21 #include <vector>
22 #include <string>
23 #include <cctype>
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>
33 #include <glibmm/ustring.h>
34 #include <glibmm/miscutils.h>
36 #include <lrdf.h>
37 #include <fst.h>
39 #include <pbd/compose.h>
40 #include <pbd/error.h>
41 #include <pbd/pathscanner.h>
42 #include <pbd/xml++.h>
43 #include <pbd/localeguard.h>
45 #include <ardour/ardour.h>
46 #include <ardour/session.h>
47 #include <ardour/audioengine.h>
48 #include <ardour/vst_plugin.h>
50 #include <pbd/stl_delete.h>
52 #include "i18n.h"
53 #include <locale.h>
55 using namespace ARDOUR;
56 using namespace PBD;
57 using std::min;
58 using std::max;
60 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
61 : Plugin (e, session)
63 handle = h;
65 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
66 throw failed_constructor();
69 _plugin = _fst->plugin;
70 _plugin->user = this;
72 /* set rate and blocksize */
74 //cerr << "Dispatch " << "effSetSampleRate" << " for " << name() << endl;
75 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
76 (float) session.frame_rate());
77 //cerr << "Dispatch " << "effSetBlockSize" << " for " << name() << endl;
78 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
79 session.get_block_size(), NULL, 0.0f);
81 /* set program to zero */
83 //cerr << "Dispatch " << "effSetProgram" << " for " << name() << endl;
84 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
86 Plugin::setup_controls ();
89 VSTPlugin::VSTPlugin (const VSTPlugin &other)
90 : Plugin (other)
92 handle = other.handle;
94 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
95 throw failed_constructor();
97 _plugin = _fst->plugin;
99 Plugin::setup_controls ();
102 VSTPlugin::~VSTPlugin ()
104 deactivate ();
105 GoingAway (); /* EMIT SIGNAL */
106 fst_close (_fst);
110 VSTPlugin::set_block_size (nframes_t nframes)
112 deactivate ();
113 //cerr << "Dispatch effSetBlockSize for " << name() << endl;
114 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
115 activate ();
116 return 0;
119 float
120 VSTPlugin::default_value (uint32_t port)
122 return 0;
125 void
126 VSTPlugin::set_parameter (uint32_t which, float val)
128 // cerr << "1SetParameter for " << name() << endl;
129 _plugin->setParameter (_plugin, which, val);
130 // cerr << "signal param change\n";
131 ParameterChanged (which, val); /* EMIT SIGNAL */
132 // cerr << "change done\n";
135 float
136 VSTPlugin::get_parameter (uint32_t which) const
138 // cerr << "GetParameter for " << name() << endl;
139 return _plugin->getParameter (_plugin, which);
143 uint32_t
144 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
146 ok = true;
147 return n;
150 XMLNode&
151 VSTPlugin::get_state()
153 XMLNode *root = new XMLNode (state_node_name());
154 LocaleGuard lg (X_("POSIX"));
156 if (_fst->current_program != -1) {
157 char buf[32];
158 snprintf (buf, sizeof (buf), "%d", _fst->current_program);
159 root->add_property ("current-program", buf);
162 if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
164 /* fetch the current chunk */
166 guchar* data;
167 long data_size;
169 //cerr << "Dispatch getChunk for " << name() << endl;
170 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
171 return *root;
174 /* store information */
176 XMLNode* chunk_node = new XMLNode (X_("chunk"));
177 gchar * encoded_data = g_base64_encode (data, data_size);
178 chunk_node->add_content (encoded_data);
179 g_free (encoded_data);
181 root->add_child_nocopy (*chunk_node);
182 } else {
184 XMLNode* parameters = new XMLNode ("parameters");
186 for (long n = 0; n < _plugin->numParams; ++n) {
187 char index[64];
188 char val[32];
189 snprintf (index, sizeof (index), "param_%ld", n);
190 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
191 parameters->add_property (index, val);
194 root->add_child_nocopy (*parameters);
198 return *root;
202 VSTPlugin::set_state(const XMLNode& node)
204 LocaleGuard lg (X_("POSIX"));
205 const XMLProperty* prop;
207 if (node.name() != state_node_name()) {
208 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
209 return 0;
212 if ((prop = node.property ("current-program")) != 0) {
213 _fst->current_program = atoi (prop->value());
216 XMLNode* child;
217 int ret = -1;
219 if ((child = find_named_node (node, X_("chunk"))) != 0) {
221 XMLPropertyList::const_iterator i;
222 XMLNodeList::const_iterator n;
223 int ret = -1;
225 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
226 if ((*n)->is_content ()) {
227 gsize chunk_size = 0;
228 guchar * data = g_base64_decode ((*n)->content ().c_str (), &chunk_size);
229 //cerr << "Dispatch setChunk for " << name() << endl;
230 ret = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, 0, chunk_size, data, 0);
231 g_free (data);
235 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
237 XMLPropertyList::const_iterator i;
239 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
240 long param;
241 float val;
243 sscanf ((*i)->name().c_str(), "param_%ld", &param);
244 sscanf ((*i)->value().c_str(), "%f", &val);
246 // cerr << "2setParameter for " << name() << endl;
247 _plugin->setParameter (_plugin, param, val);
250 /* program number is not knowable */
252 _fst->current_program = -1;
254 ret = 0;
259 return ret;
263 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
265 VstParameterProperties prop;
267 desc.min_unbound = false;
268 desc.max_unbound = false;
269 prop.flags = 0;
271 //cerr << "Dispatch getParameterProperties for " << name() << endl;
272 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
274 if (prop.flags & kVstParameterUsesIntegerMinMax) {
275 desc.lower = prop.minInteger;
276 desc.upper = prop.maxInteger;
277 } else {
278 desc.lower = 0;
279 desc.upper = 1.0;
282 if (prop.flags & kVstParameterUsesIntStep) {
284 desc.step = prop.stepInteger;
285 desc.smallstep = prop.stepInteger;
286 desc.largestep = prop.stepInteger;
288 } else if (prop.flags & kVstParameterUsesFloatStep) {
290 desc.step = prop.stepFloat;
291 desc.smallstep = prop.smallStepFloat;
292 desc.largestep = prop.largeStepFloat;
294 } else {
296 float range = desc.upper - desc.lower;
298 desc.step = range / 100.0f;
299 desc.smallstep = desc.step / 2.0f;
300 desc.largestep = desc.step * 10.0f;
303 desc.toggled = prop.flags & kVstParameterIsSwitch;
304 // cerr << "parameter " << which << " toggled = " << desc.toggled << " from " << std::hex << prop.flags << " vs. " << kVstParameterIsSwitch << std::dec << endl;
305 desc.logarithmic = false;
306 desc.sr_dependent = false;
307 desc.label = prop.label;
309 } else {
311 /* old style */
313 char label[64];
314 label[0] = '\0';
316 // cerr << "Dispatch paramName for " << name() << endl;
317 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
319 desc.label = label;
320 desc.integer_step = false;
321 desc.lower = 0.0f;
322 desc.upper = 1.0f;
323 desc.step = 0.01f;
324 desc.smallstep = 0.005f;
325 desc.largestep = 0.1f;
326 desc.toggled = false;
327 desc.logarithmic = false;
328 desc.sr_dependent = false;
331 return 0;
334 bool
335 VSTPlugin::load_preset (string name)
338 if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
339 error << _("no support for presets using chunks at this time")
340 << endmsg;
341 return false;
343 return Plugin::load_preset (name);
346 bool
347 VSTPlugin::save_preset (string name)
349 if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
350 error << _("no support for presets using chunks at this time")
351 << endmsg;
352 return false;
355 return Plugin::save_preset (name, "vst");
358 string
359 VSTPlugin::describe_parameter (uint32_t param)
361 char name[64];
362 // cerr << "Dispatch effGetParamName for " << this->name() << endl;
363 _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
364 return name;
367 nframes_t
368 VSTPlugin::latency () const
370 #ifdef VESTIGE_HEADER
371 return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
372 #else
373 return 0;
374 #endif
377 set<uint32_t>
378 VSTPlugin::automatable () const
380 set<uint32_t> ret;
382 for (uint32_t i = 0; i < parameter_count(); ++i){
383 ret.insert (ret.end(), i);
386 return ret;
390 VSTPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in_index, int32_t& out_index, nframes_t nframes, nframes_t offset)
392 float *ins[_plugin->numInputs];
393 float *outs[_plugin->numOutputs];
394 int32_t i;
396 if (nframes == 0) {
397 warning << _("VST plugin called with zero frames - please notify Ardour developers") << endmsg;
398 return 0;
401 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
402 ins[i] = bufs[min((uint32_t) in_index,maxbuf - 1)] + offset;
403 in_index++;
406 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
407 outs[i] = bufs[min((uint32_t) out_index,maxbuf - 1)] + offset;
409 /* unbelievably, several VST plugins still rely on Cubase
410 behaviour and do not silence the buffer in processReplacing
411 when they have no output.
414 // memset (outs[i], 0, sizeof (Sample) * nframes);
415 out_index++;
418 /* we already know it can support processReplacing */
420 // cerr << "!ProcessReplacing for " << name() << endl;
421 _plugin->processReplacing (_plugin, ins, outs, nframes);
423 return 0;
426 void
427 VSTPlugin::deactivate ()
429 //cerr << "Dispatch effMainsChanged for " << name() << endl;
430 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
433 void
434 VSTPlugin::activate ()
436 //cerr << "Dispatch effMainsChanged for " << name() << endl;
437 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
440 string
441 VSTPlugin::unique_id() const
443 char buf[32];
444 #ifdef VESTIGE_HEADER
445 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
446 #else
447 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
448 #endif
449 return string (buf);
453 const char *
454 VSTPlugin::name () const
456 return handle->name;
459 const char *
460 VSTPlugin::maker () const
462 return "imadeit";
465 const char *
466 VSTPlugin::label () const
468 return handle->name;
471 uint32_t
472 VSTPlugin::parameter_count() const
474 return _plugin->numParams;
477 bool
478 VSTPlugin::has_editor () const
480 return _plugin->flags & effFlagsHasEditor;
483 void
484 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
486 char *first_nonws;
488 //cerr << "Dispatch getParamDisplay for " << name() << endl;
489 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
491 if (buf[0] == '\0') {
492 return;
495 first_nonws = buf;
496 while (*first_nonws && isspace (*first_nonws)) {
497 first_nonws++;
499 if (*first_nonws == '\0') {
500 return;
503 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
506 PluginPtr
507 VSTPluginInfo::load (Session& session)
509 try {
510 PluginPtr plugin;
512 if (Config->get_use_vst()) {
513 FSTHandle* handle;
515 handle = fst_load(path.c_str());
517 if ( (int)handle == -1) {
518 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
519 } else {
520 plugin.reset (new VSTPlugin (session.engine(), session, handle));
522 } else {
523 error << _("You asked ardour to not use any VST plugins") << endmsg;
524 return PluginPtr ((Plugin*) 0);
527 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
528 return plugin;
531 catch (failed_constructor &err) {
532 return PluginPtr ((Plugin*) 0);
536 VSTPluginInfo::VSTPluginInfo()
538 type = ARDOUR::VST;