remove a bunch of uses of long (mostly replaced by int32_t)
[ardour2.git] / libs / ardour / vst_plugin.cc
blobe3953314b696bb4b90276c559bb8f1ae3cf91bab
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"
44 #include <fst.h>
46 #include "ardour/session.h"
47 #include "ardour/audioengine.h"
48 #include "ardour/filesystem_paths.h"
49 #include "ardour/vst_plugin.h"
50 #include "ardour/buffer_set.h"
51 #include "ardour/audio_buffer.h"
52 #include "ardour/midi_buffer.h"
54 #include "pbd/stl_delete.h"
56 #include "i18n.h"
57 #include <locale.h>
59 using namespace std;
60 using namespace ARDOUR;
61 using namespace PBD;
62 using std::min;
63 using std::max;
65 VSTPlugin::VSTPlugin (AudioEngine& e, Session& session, FSTHandle* h)
66 : Plugin (e, session)
68 handle = h;
70 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
71 throw failed_constructor();
74 _plugin = _fst->plugin;
75 _plugin->user = this;
77 /* set rate and blocksize */
79 _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL,
80 (float) session.frame_rate());
81 _plugin->dispatcher (_plugin, effSetBlockSize, 0,
82 session.get_block_size(), NULL, 0.0f);
84 /* set program to zero */
86 _plugin->dispatcher (_plugin, effSetProgram, 0, 0, NULL, 0.0f);
88 // Plugin::setup_controls ();
91 VSTPlugin::VSTPlugin (const VSTPlugin &other)
92 : Plugin (other)
94 handle = other.handle;
96 if ((_fst = fst_instantiate (handle, Session::vst_callback, this)) == 0) {
97 throw failed_constructor();
99 _plugin = _fst->plugin;
101 // Plugin::setup_controls ();
104 VSTPlugin::~VSTPlugin ()
106 deactivate ();
107 fst_close (_fst);
110 void
111 VSTPlugin::set_block_size (nframes_t nframes)
113 deactivate ();
114 _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
115 activate ();
118 float
119 VSTPlugin::default_value (uint32_t port)
121 return 0;
124 void
125 VSTPlugin::set_parameter (uint32_t which, float val)
127 _plugin->setParameter (_plugin, which, val);
128 //ParameterChanged (which, val); /* EMIT SIGNAL */
131 float
132 VSTPlugin::get_parameter (uint32_t which) const
134 return _plugin->getParameter (_plugin, which);
138 uint32_t
139 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
141 ok = true;
142 return n;
145 XMLNode&
146 VSTPlugin::get_state()
148 XMLNode *root = new XMLNode (state_node_name());
149 LocaleGuard lg (X_("POSIX"));
151 if (_fst->current_program != -1) {
152 char buf[32];
153 snprintf (buf, sizeof (buf), "%d", _fst->current_program);
154 root->add_property ("current-program", buf);
157 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
159 /* fetch the current chunk */
161 guchar* data;
162 int32_t data_size;
164 if ((data_size = _plugin->dispatcher (_plugin, 23 /* effGetChunk */, 0, 0, &data, false)) == 0) {
165 return *root;
168 /* store information */
170 XMLNode* chunk_node = new XMLNode (X_("chunk"));
172 gchar * encoded_data = g_base64_encode (data, data_size);
173 chunk_node->add_content (encoded_data);
174 g_free (encoded_data);
176 root->add_child_nocopy (*chunk_node);
178 } else {
180 XMLNode* parameters = new XMLNode ("parameters");
182 for (int32_t n = 0; n < _plugin->numParams; ++n) {
183 char index[64];
184 char val[32];
185 snprintf (index, sizeof (index), "param_%ld", n);
186 snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
187 parameters->add_property (index, val);
190 root->add_child_nocopy (*parameters);
193 return *root;
197 VSTPlugin::set_state(const XMLNode& node, int)
199 LocaleGuard lg (X_("POSIX"));
201 if (node.name() != state_node_name()) {
202 error << _("Bad node sent to VSTPlugin::set_state") << endmsg;
203 return 0;
206 const XMLProperty* prop;
208 if ((prop = node.property ("current-program")) != 0) {
209 _fst->current_program = atoi (prop->value().c_str());
212 XMLNode* child;
213 int ret = -1;
215 if ((child = find_named_node (node, X_("chunk"))) != 0) {
217 XMLPropertyList::const_iterator i;
218 XMLNodeList::const_iterator n;
219 int ret = -1;
221 for (n = child->children ().begin (); n != child->children ().end (); ++n) {
222 if ((*n)->is_content ()) {
223 gsize chunk_size = 0;
224 guchar * data = g_base64_decode ((*n)->content ().c_str (), &chunk_size);
225 //cerr << "Dispatch setChunk for " << name() << endl;
226 ret = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, 0, chunk_size, data, 0);
227 g_free (data);
231 } else if ((child = find_named_node (node, X_("parameters"))) != 0) {
233 XMLPropertyList::const_iterator i;
235 for (i = child->properties().begin(); i != child->properties().end(); ++i) {
236 int32_t param;
237 float val;
239 sscanf ((*i)->name().c_str(), "param_%d", &param);
240 sscanf ((*i)->value().c_str(), "%f", &val);
242 _plugin->setParameter (_plugin, param, val);
245 /* program number is not knowable */
247 _fst->current_program = -1;
249 ret = 0;
253 return ret;
257 VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc) const
259 VstParameterProperties prop;
261 desc.min_unbound = false;
262 desc.max_unbound = false;
263 prop.flags = 0;
265 if (_plugin->dispatcher (_plugin, effGetParameterProperties, which, 0, &prop, 0)) {
267 #ifdef VESTIGE_COMPLETE
269 /* i have yet to find or hear of a VST plugin that uses this */
271 if (prop.flags & kVstParameterUsesIntegerMinMax) {
272 desc.lower = prop.minInteger;
273 desc.upper = prop.maxInteger;
274 } else {
275 desc.lower = 0;
276 desc.upper = 1.0;
279 if (prop.flags & kVstParameterUsesIntStep) {
281 desc.step = prop.stepInteger;
282 desc.smallstep = prop.stepInteger;
283 desc.largestep = prop.stepInteger;
285 } else if (prop.flags & kVstParameterUsesFloatStep) {
287 desc.step = prop.stepFloat;
288 desc.smallstep = prop.smallStepFloat;
289 desc.largestep = prop.largeStepFloat;
291 } else {
293 float range = desc.upper - desc.lower;
295 desc.step = range / 100.0f;
296 desc.smallstep = desc.step / 2.0f;
297 desc.largestep = desc.step * 10.0f;
300 desc.toggled = prop.flags & kVstParameterIsSwitch;
301 desc.logarithmic = false;
302 desc.sr_dependent = false;
303 desc.label = prop.label;
304 #endif
306 } else {
308 /* old style */
310 char label[64];
311 label[0] = '\0';
313 _plugin->dispatcher (_plugin, effGetParamName, which, 0, label, 0);
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;
327 return 0;
330 bool
331 VSTPlugin::load_preset (const string& name)
333 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
334 error << _("no support for presets using chunks at this time")
335 << endmsg;
336 return false;
338 return Plugin::load_preset (name);
341 bool
342 VSTPlugin::save_preset (string name)
344 if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
345 error << _("no support for presets using chunks at this time")
346 << endmsg;
347 return false;
349 return Plugin::save_preset (name, "vst");
352 string
353 VSTPlugin::describe_parameter (Evoral::Parameter param)
355 char name[64];
356 _plugin->dispatcher (_plugin, effGetParamName, param.id(), 0, name, 0);
357 return name;
360 nframes_t
361 VSTPlugin::signal_latency () const
363 if (_user_latency) {
364 return _user_latency;
367 #ifdef VESTIGE_HEADER
368 return *((nframes_t *) (((char *) &_plugin->flags) + 12)); /* initialDelay */
369 #else
370 return _plugin->initial_delay;
371 #endif
374 set<Evoral::Parameter>
375 VSTPlugin::automatable () const
377 set<Evoral::Parameter> ret;
379 for (uint32_t i = 0; i < parameter_count(); ++i){
380 ret.insert (ret.end(), Evoral::Parameter(PluginAutomation, 0, i));
383 return ret;
387 VSTPlugin::connect_and_run (BufferSet& bufs,
388 ChanMapping in_map, ChanMapping out_map,
389 nframes_t nframes, nframes_t offset)
391 float *ins[_plugin->numInputs];
392 float *outs[_plugin->numOutputs];
393 int32_t i;
395 const uint32_t nbufs = bufs.count().n_audio();
397 int in_index = 0;
398 for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
399 ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
400 in_index++;
403 int out_index = 0;
404 for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
405 outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
407 /* unbelievably, several VST plugins still rely on Cubase
408 behaviour and do not silence the buffer in processReplacing
409 when they have no output.
412 // memset (outs[i], 0, sizeof (Sample) * nframes);
413 out_index++;
417 if (bufs.count().n_midi() > 0) {
418 VstEvents* v = bufs.get_vst_midi (0);
419 _plugin->dispatcher (_plugin, effProcessEvents, 0, 0, v, 0);
422 /* we already know it can support processReplacing */
424 _plugin->processReplacing (_plugin, ins, outs, nframes);
426 return 0;
429 void
430 VSTPlugin::deactivate ()
432 _plugin->dispatcher (_plugin, effMainsChanged, 0, 0, NULL, 0.0f);
435 void
436 VSTPlugin::activate ()
438 _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
441 string
442 VSTPlugin::unique_id() const
444 char buf[32];
446 #ifdef VESTIGE_HEADER
447 snprintf (buf, sizeof (buf), "%d", *((int32_t*) &_plugin->unused_id));
448 #else
449 snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
450 #endif
451 return string (buf);
455 const char *
456 VSTPlugin::name () const
458 return handle->name;
461 const char *
462 VSTPlugin::maker () const
464 return "imadeit";
467 const char *
468 VSTPlugin::label () const
470 return handle->name;
473 uint32_t
474 VSTPlugin::parameter_count() const
476 return _plugin->numParams;
479 bool
480 VSTPlugin::has_editor () const
482 return _plugin->flags & effFlagsHasEditor;
485 void
486 VSTPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
488 char *first_nonws;
490 _plugin->dispatcher (_plugin, 7 /* effGetParamDisplay */, param, 0, buf, 0);
492 if (buf[0] == '\0') {
493 return;
496 first_nonws = buf;
497 while (*first_nonws && isspace (*first_nonws)) {
498 first_nonws++;
500 if (*first_nonws == '\0') {
501 return;
504 memmove (buf, first_nonws, strlen (buf) - (first_nonws - buf) + 1);
507 PluginPtr
508 VSTPluginInfo::load (Session& session)
510 try {
511 PluginPtr plugin;
513 if (Config->get_use_vst()) {
514 FSTHandle* handle;
516 handle = fst_load(path.c_str());
518 if ( (int)handle == -1) {
519 error << string_compose(_("VST: cannot load module from \"%1\""), path) << endmsg;
520 } else {
521 plugin.reset (new VSTPlugin (session.engine(), session, handle));
523 } else {
524 error << _("You asked ardour to not use any VST plugins") << endmsg;
525 return PluginPtr ((Plugin*) 0);
528 plugin->set_info(PluginInfoPtr(new VSTPluginInfo(*this)));
529 return plugin;
532 catch (failed_constructor &err) {
533 return PluginPtr ((Plugin*) 0);
537 VSTPluginInfo::VSTPluginInfo()
539 type = ARDOUR::VST;