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.
26 #include <cstdio> // so libraptor doesn't complain
29 #include <cstring> // for memmove
33 #include <glibmm/ustring.h>
34 #include <glibmm/miscutils.h>
39 #include <pbd/compose.h>
40 #include <pbd/error.h>
41 #include <pbd/pathscanner.h>
42 #include <pbd/xml++.h>
44 #include <ardour/ardour.h>
45 #include <ardour/session.h>
46 #include <ardour/audioengine.h>
47 #include <ardour/vst_plugin.h>
49 #include <pbd/stl_delete.h>
54 using namespace ARDOUR;
59 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
64 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
65 throw failed_constructor();
68 _plugin = _fst->plugin;
71 /* set rate and blocksize */
73 //cerr << "Dispatch " << "effSetSampleRate" << " for " << name() << endl;
74 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
75 (float) session.frame_rate());
76 //cerr << "Dispatch " << "effSetBlockSize" << " for " << name() << endl;
77 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
78 session.get_block_size(), NULL, 0.0f);
80 /* set program to zero */
82 //cerr << "Dispatch " << "effSetProgram" << " for " << name() << endl;
83 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
85 Plugin::setup_controls ();
88 VSTPlugin::VSTPlugin (const VSTPlugin &other)
91 handle = other.handle;
93 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
94 throw failed_constructor();
96 _plugin = _fst->plugin;
98 Plugin::setup_controls ();
101 VSTPlugin::~VSTPlugin ()
104 GoingAway (); /* EMIT SIGNAL */
109 VSTPlugin::set_block_size (nframes_t nframes)
112 //cerr << "Dispatch effSetBlockSize for " << name() << endl;
113 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
118 VSTPlugin::default_value (uint32_t port)
124 VSTPlugin::set_parameter (uint32_t which, float val)
126 // cerr << "1SetParameter for " << name() << endl;
127 _plugin->setParameter (_plugin, which, val);
128 // cerr << "signal param change\n";
129 ParameterChanged (which, val); /* EMIT SIGNAL */
130 // cerr << "change done\n";
134 VSTPlugin::get_parameter (uint32_t which) const
136 // cerr << "GetParameter for " << name() << endl;
137 return _plugin->getParameter (_plugin, which);
142 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
149 VSTPlugin::get_state()
151 XMLNode *root = new XMLNode (state_node_name());
152 LocaleGuard lg (X_("POSIX"));
154 if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
156 /* fetch the current chunk */
161 //cerr << "Dispatch getChunk for " << name() << endl;
162 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
166 /* store information */
168 XMLNode* chunk_node = new XMLNode (X_("chunk"));
169 gchar * encoded_data = g_base64_encode (data, data_size);
170 chunk_node->add_content (encoded_data);
171 g_free (encoded_data);
173 root->add_child_nocopy (*chunk_node);
176 XMLNode* parameters = new XMLNode ("parameters");
178 for (long n = 0; n < _plugin->numParams; ++n) {
181 snprintf (index, sizeof (index), "param_%ld", n);
182 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
183 parameters->add_property (index, val);
186 root->add_child_nocopy (*parameters);
194 VSTPlugin::set_state(const XMLNode& node)
196 LocaleGuard lg (X_("POSIX"));
198 if (node.name() != state_node_name()) {
199 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
205 if ((child = find_named_node (node, X_("chunk"))) != 0) {
207 XMLPropertyList::const_iterator i;
209 XMLNodeList::const_iterator n;
210 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
211 if ((*n)->is_content ()) {
212 gsize chunk_size = 0;
213 guchar * data = g_base64_decode ((*n)->content ().c_str (), &chunk_size);
214 //cerr << "Dispatch setChunk for " << name() << endl;
215 if (_plugin->dispatcher (_plugin, 24 /* effSetChunk */, 0, chunk_size, data, 0) == 0) {
226 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
228 XMLPropertyList::const_iterator i;
230 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
234 sscanf ((*i)->name().c_str(), "param_%ld", ¶m);
235 sscanf ((*i)->value().c_str(), "%f", &val);
237 // cerr << "2setParameter for " << name() << endl;
238 _plugin->setParameter (_plugin, param, val);
248 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
250 VstParameterProperties prop;
252 desc.min_unbound = false;
253 desc.max_unbound = false;
256 //cerr << "Dispatch getParameterProperties for " << name() << endl;
257 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
259 if (prop.flags & kVstParameterUsesIntegerMinMax) {
260 desc.lower = prop.minInteger;
261 desc.upper = prop.maxInteger;
267 if (prop.flags & kVstParameterUsesIntStep) {
269 desc.step = prop.stepInteger;
270 desc.smallstep = prop.stepInteger;
271 desc.largestep = prop.stepInteger;
273 } else if (prop.flags & kVstParameterUsesFloatStep) {
275 desc.step = prop.stepFloat;
276 desc.smallstep = prop.smallStepFloat;
277 desc.largestep = prop.largeStepFloat;
281 float range = desc.upper - desc.lower;
283 desc.step = range / 100.0f;
284 desc.smallstep = desc.step / 2.0f;
285 desc.largestep = desc.step * 10.0f;
288 desc.toggled = prop.flags & kVstParameterIsSwitch;
289 // cerr << "parameter " << which << " toggled = " << desc.toggled << " from " << std::hex << prop.flags << " vs. " << kVstParameterIsSwitch << std::dec << endl;
290 desc.logarithmic = false;
291 desc.sr_dependent = false;
292 desc.label = prop.label;
301 // cerr << "Dispatch paramName for " << name() << endl;
302 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
305 desc.integer_step = false;
309 desc.smallstep = 0.005f;
310 desc.largestep = 0.1f;
311 desc.toggled = false;
312 desc.logarithmic = false;
313 desc.sr_dependent = false;
320 VSTPlugin::load_preset (string name)
323 if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
324 error << _("no support for presets using chunks at this time")
328 return Plugin::load_preset (name);
332 VSTPlugin::save_preset (string name)
334 if (_plugin->flags & 32 /* effFlagsProgramChunks */) {
335 error << _("no support for presets using chunks at this time")
340 return Plugin::save_preset (name, "vst");
344 VSTPlugin::describe_parameter (uint32_t param)
347 // cerr << "Dispatch effGetParamName for " << this->name() << endl;
348 _plugin->dispatcher (_plugin, effGetParamName, param, 0, name, 0);
353 VSTPlugin::latency () const
355 #ifdef VESTIGE_HEADER
356 return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
363 VSTPlugin::automatable () const
367 for (uint32_t i = 0; i < parameter_count(); ++i){
368 ret.insert (ret.end(), i);
375 VSTPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in_index, int32_t& out_index, nframes_t nframes, nframes_t offset)
377 float *ins[_plugin->numInputs];
378 float *outs[_plugin->numOutputs];
382 warning << _("VST plugin called with zero frames - please notify Ardour developers") << endmsg;
386 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
387 ins[i] = bufs[min((uint32_t) in_index,maxbuf - 1)] + offset;
391 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
392 outs[i] = bufs[min((uint32_t) out_index,maxbuf - 1)] + offset;
394 /* unbelievably, several VST plugins still rely on Cubase
395 behaviour and do not silence the buffer in processReplacing
396 when they have no output.
399 // memset (outs[i], 0, sizeof (Sample) * nframes);
403 /* we already know it can support processReplacing */
405 // cerr << "!ProcessReplacing for " << name() << endl;
406 _plugin->processReplacing (_plugin, ins, outs, nframes);
412 VSTPlugin::deactivate ()
414 //cerr << "Dispatch effMainsChanged for " << name() << endl;
415 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
419 VSTPlugin::activate ()
421 //cerr << "Dispatch effMainsChanged for " << name() << endl;
422 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
426 VSTPlugin::unique_id() const
429 #ifdef VESTIGE_HEADER
430 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
432 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
439 VSTPlugin::name () const
445 VSTPlugin::maker () const
451 VSTPlugin::label () const
457 VSTPlugin::parameter_count() const
459 return _plugin->numParams;
463 VSTPlugin::has_editor () const
465 return _plugin->flags & effFlagsHasEditor;
469 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
473 //cerr << "Dispatch getParamDisplay for " << name() << endl;
474 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
476 if (buf[0] == '\0') {
481 while (*first_nonws && isspace (*first_nonws)) {
484 if (*first_nonws == '\0') {
488 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
492 VSTPluginInfo::load (Session& session)
497 if (Config->get_use_vst()) {
500 handle = fst_load(path.c_str());
502 if ( (int)handle == -1) {
503 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
505 plugin.reset (new VSTPlugin (session.engine(), session, handle));
508 error << _("You asked ardour to not use any VST plugins") << endmsg;
509 return PluginPtr ((Plugin*) 0);
512 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
516 catch (failed_constructor &err) {
517 return PluginPtr ((Plugin*) 0);