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>
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>
55 using namespace ARDOUR
;
60 VSTPlugin::VSTPlugin (AudioEngine
& e
, Session
& session
, FSTHandle
* h
)
65 if ((_fst
= fst_instantiate (handle
, Session::vst_callback
, this)) == 0) {
66 throw failed_constructor();
69 _plugin
= _fst
->plugin
;
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
)
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 ()
105 GoingAway (); /* EMIT SIGNAL */
110 VSTPlugin::set_block_size (nframes_t nframes
)
113 //cerr << "Dispatch effSetBlockSize for " << name() << endl;
114 _plugin
->dispatcher (_plugin
, effSetBlockSize
, 0, nframes
, NULL
, 0.0f
);
120 VSTPlugin::default_value (uint32_t port
)
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";
136 VSTPlugin::get_parameter (uint32_t which
) const
138 // cerr << "GetParameter for " << name() << endl;
139 return _plugin
->getParameter (_plugin
, which
);
144 VSTPlugin::nth_parameter (uint32_t n
, bool& ok
) const
151 VSTPlugin::get_state()
153 XMLNode
*root
= new XMLNode (state_node_name());
154 LocaleGuard
lg (X_("POSIX"));
156 if (_fst
->current_program
!= -1) {
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 */
169 //cerr << "Dispatch getChunk for " << name() << endl;
170 if ((data_size
= _plugin
->dispatcher (_plugin
, 23 /* effGetChunk */, 0, 0, &data
, false)) == 0) {
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
);
184 XMLNode
* parameters
= new XMLNode ("parameters");
186 for (long n
= 0; n
< _plugin
->numParams
; ++n
) {
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
);
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
;
212 if ((prop
= node
.property ("current-program")) != 0) {
213 _fst
->current_program
= atoi (prop
->value());
219 if ((child
= find_named_node (node
, X_("chunk"))) != 0) {
221 XMLPropertyList::const_iterator i
;
222 XMLNodeList::const_iterator n
;
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);
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
) {
243 sscanf ((*i
)->name().c_str(), "param_%ld", ¶m
);
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;
263 VSTPlugin::get_parameter_descriptor (uint32_t which
, ParameterDescriptor
& desc
) const
265 VstParameterProperties prop
;
267 desc
.min_unbound
= false;
268 desc
.max_unbound
= false;
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
;
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
;
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
;
316 // cerr << "Dispatch paramName for " << name() << endl;
317 _plugin
->dispatcher (_plugin
, effGetParamName
, which
, 0, label
, 0);
320 desc
.integer_step
= false;
324 desc
.smallstep
= 0.005f
;
325 desc
.largestep
= 0.1f
;
326 desc
.toggled
= false;
327 desc
.logarithmic
= false;
328 desc
.sr_dependent
= false;
335 VSTPlugin::load_preset (string name
)
338 if (_plugin
->flags
& 32 /* effFlagsProgramChunks */) {
339 error
<< _("no support for presets using chunks at this time")
343 return Plugin::load_preset (name
);
347 VSTPlugin::save_preset (string name
)
349 if (_plugin
->flags
& 32 /* effFlagsProgramChunks */) {
350 error
<< _("no support for presets using chunks at this time")
355 return Plugin::save_preset (name
, "vst");
359 VSTPlugin::describe_parameter (uint32_t param
)
362 // cerr << "Dispatch effGetParamName for " << this->name() << endl;
363 _plugin
->dispatcher (_plugin
, effGetParamName
, param
, 0, name
, 0);
368 VSTPlugin::latency () const
370 #ifdef VESTIGE_HEADER
371 return *((nframes_t
*) (((char *) &_plugin
->flags
) + 12)); /* initialDelay */
378 VSTPlugin::automatable () const
382 for (uint32_t i
= 0; i
< parameter_count(); ++i
){
383 ret
.insert (ret
.end(), i
);
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
];
397 warning
<< _("VST plugin called with zero frames - please notify Ardour developers") << endmsg
;
401 for (i
= 0; i
< (int32_t) _plugin
->numInputs
; ++i
) {
402 ins
[i
] = bufs
[min((uint32_t) in_index
,maxbuf
- 1)] + offset
;
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);
418 /* we already know it can support processReplacing */
420 // cerr << "!ProcessReplacing for " << name() << endl;
421 _plugin
->processReplacing (_plugin
, ins
, outs
, nframes
);
427 VSTPlugin::deactivate ()
429 //cerr << "Dispatch effMainsChanged for " << name() << endl;
430 _plugin
->dispatcher (_plugin
, effMainsChanged
, 0, 0, NULL
, 0.0f
);
434 VSTPlugin::activate ()
436 //cerr << "Dispatch effMainsChanged for " << name() << endl;
437 _plugin
->dispatcher (_plugin
, effMainsChanged
, 0, 1, NULL
, 0.0f
);
441 VSTPlugin::unique_id() const
444 #ifdef VESTIGE_HEADER
445 snprintf (buf
, sizeof (buf
), "%d", *((int32_t*) &_plugin
->unused_id
));
447 snprintf (buf
, sizeof (buf
), "%d", _plugin
->uniqueID
);
454 VSTPlugin::name () const
460 VSTPlugin::maker () const
466 VSTPlugin::label () const
472 VSTPlugin::parameter_count() const
474 return _plugin
->numParams
;
478 VSTPlugin::has_editor () const
480 return _plugin
->flags
& effFlagsHasEditor
;
484 VSTPlugin::print_parameter (uint32_t param
, char *buf
, uint32_t len
) const
488 //cerr << "Dispatch getParamDisplay for " << name() << endl;
489 _plugin
->dispatcher (_plugin
, 7 /* effGetParamDisplay */, param
, 0, buf
, 0);
491 if (buf
[0] == '\0') {
496 while (*first_nonws
&& isspace (*first_nonws
)) {
499 if (*first_nonws
== '\0') {
503 memmove (buf
, first_nonws
, strlen (buf
) - (first_nonws
- buf
) + 1);
507 VSTPluginInfo::load (Session
& session
)
512 if (Config
->get_use_vst()) {
515 handle
= fst_load(path
.c_str());
517 if ( (int)handle
== -1) {
518 error
<< string_compose(_("VST: cannot load module from \"%1\""), path
) << endmsg
;
520 plugin
.reset (new VSTPlugin (session
.engine(), session
, handle
));
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)));
531 catch (failed_constructor
&err
) {
532 return PluginPtr ((Plugin
*) 0);
536 VSTPluginInfo::VSTPluginInfo()