Use ID::to_s() in libpbd instead of ID::print()
[ardour.git] / libs / ardour / vst_plugin.cc
index 505a49360ffeff88374b3a7841241de233a59a44..95a661c40c67f0e69a0f93736b9ff8b78707da92 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <glibmm/fileutils.h>
 #include <glibmm/miscutils.h>
+#include <glibmm/convert.h>
 
 #include "pbd/floating.h"
 #include "pbd/locale_guard.h"
@@ -48,6 +49,7 @@ VSTPlugin::VSTPlugin (AudioEngine& engine, Session& session, VSTHandle* handle)
        , _num (0)
        , _transport_frame (0)
        , _transport_speed (0.f)
+       , _eff_bypassed (false)
 {
        memset (&_timeInfo, 0, sizeof(_timeInfo));
 }
@@ -63,6 +65,7 @@ VSTPlugin::VSTPlugin (const VSTPlugin& other)
        , _transport_frame (0)
        , _transport_speed (0.f)
        , _parameter_defaults (other._parameter_defaults)
+       , _eff_bypassed (other._eff_bypassed)
 {
        memset (&_timeInfo, 0, sizeof(_timeInfo));
 }
@@ -73,17 +76,48 @@ VSTPlugin::~VSTPlugin ()
 }
 
 void
-VSTPlugin::set_plugin (AEffect* e)
+VSTPlugin::open_plugin ()
 {
-       _plugin = e;
+       _plugin = _state->plugin;
+       assert (_plugin->user == this); // should have been set by {mac_vst|fst|lxvst}_instantiate
        _plugin->user = this;
+       _state->plugin->dispatcher (_plugin, effOpen, 0, 0, 0, 0);
+       _state->vst_version = _plugin->dispatcher (_plugin, effGetVstVersion, 0, 0, 0, 0);
+}
 
+void
+VSTPlugin::init_plugin ()
+{
        /* set rate and blocksize */
-
        _plugin->dispatcher (_plugin, effSetSampleRate, 0, 0, NULL, (float) _session.frame_rate());
        _plugin->dispatcher (_plugin, effSetBlockSize, 0, _session.get_block_size(), NULL, 0.0f);
 }
 
+
+uint32_t
+VSTPlugin::designated_bypass_port ()
+{
+       if (_plugin->dispatcher (_plugin, effCanDo, 0, 0, const_cast<char*> ("bypass"), 0.0f) != 0) {
+#ifdef ALLOW_VST_BYPASS_TO_FAIL // yet unused, see also plugin_insert.cc
+               return UINT32_MAX - 1; // emulate a port
+#else
+               /* check if plugin actually supports it,
+                * e.g. u-he Presswerk  CanDo "bypass"  but calling effSetBypass is a NO-OP.
+                * (presumably the plugin-author thinks hard-bypassing is a bad idea,
+                * particularly since the plugin itself provides a bypass-port)
+                */
+               intptr_t value = 0; // not bypassed
+               if (0 != _plugin->dispatcher (_plugin, 44 /*effSetBypass*/, 0, value, NULL, 0)) {
+                       cerr << "Emulate VST Bypass Port for " << name() << endl; // XXX DEBUG
+                       return UINT32_MAX - 1; // emulate a port
+               } else {
+                       cerr << "Do *not* Emulate VST Bypass Port for " << name() << endl; // XXX DEBUG
+               }
+#endif
+       }
+       return UINT32_MAX;
+}
+
 void
 VSTPlugin::deactivate ()
 {
@@ -114,12 +148,32 @@ VSTPlugin::default_value (uint32_t which)
 float
 VSTPlugin::get_parameter (uint32_t which) const
 {
+       if (which == UINT32_MAX - 1) {
+               // ardour uses enable-semantics: 1: enabled, 0: bypassed
+               return _eff_bypassed ? 0.f : 1.f;
+       }
        return _plugin->getParameter (_plugin, which);
 }
 
 void
 VSTPlugin::set_parameter (uint32_t which, float newval)
 {
+       if (which == UINT32_MAX - 1) {
+               // ardour uses enable-semantics: 1: enabled, 0: bypassed
+               intptr_t value = (newval <= 0.f) ? 1 : 0;
+               cerr << "effSetBypass " << value << endl; // XXX DEBUG
+               int rv = _plugin->dispatcher (_plugin, 44 /*effSetBypass*/, 0, value, NULL, 0);
+               if (0 != rv) {
+                       _eff_bypassed = (value == 1);
+               } else {
+                       cerr << "effSetBypass failed rv=" << rv << endl; // XXX DEBUG
+#ifdef ALLOW_VST_BYPASS_TO_FAIL // yet unused, see also vst_plugin.cc
+                       // emit signal.. hard un/bypass from here?!
+#endif
+               }
+               return;
+       }
+
        float oldval = get_parameter (which);
 
        if (PBD::floateq (oldval, newval, 1)) {
@@ -136,6 +190,14 @@ VSTPlugin::set_parameter (uint32_t which, float newval)
        }
 }
 
+void
+VSTPlugin::parameter_changed_externally (uint32_t which, float value )
+{
+       ParameterChangedExternally (which, value); /* EMIT SIGNAL */
+       Plugin::set_parameter (which, value);
+}
+
+
 uint32_t
 VSTPlugin::nth_parameter (uint32_t n, bool& ok) const
 {
@@ -171,8 +233,9 @@ VSTPlugin::set_chunk (gchar const * data, bool single)
        int r = 0;
        guchar* raw_data = g_base64_decode (data, &size);
        {
-               Glib::Threads::Mutex::Lock lm (_lock);
+               pthread_mutex_lock (&_state->state_lock);
                r = _plugin->dispatcher (_plugin, 24 /* effSetChunk */, single ? 1 : 0, size, raw_data, 0);
+               pthread_mutex_unlock (&_state->state_lock);
        }
        g_free (raw_data);
        return r;
@@ -343,7 +406,9 @@ VSTPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& desc)
        }
 
        desc.normal = get_parameter (which);
-       _parameter_defaults[which] = desc.normal;
+       if (_parameter_defaults.find (which) == _parameter_defaults.end ()) {
+               _parameter_defaults[which] = desc.normal;
+       }
 
        return 0;
 }
@@ -383,6 +448,7 @@ VSTPlugin::load_plugin_preset (PresetRecord r)
        sscanf (r.uri.c_str(), "VST:%d:%d", &id, &index);
 #endif
        _state->want_program = index;
+       LoadPresetProgram (); /* EMIT SIGNAL */ /* used for macvst */
        return true;
 }
 
@@ -425,6 +491,7 @@ VSTPlugin::load_user_preset (PresetRecord r)
                                        _state->wanted_chunk = raw_data;
                                        _state->wanted_chunk_size = size;
                                        _state->want_chunk = 1;
+                                       LoadPresetProgram (); /* EMIT SIGNAL */ /* used for macvst */
                                        return true;
                                }
                        }
@@ -440,8 +507,10 @@ VSTPlugin::load_user_preset (PresetRecord r)
 
                                                assert (index);
                                                assert (value);
-
-                                               set_parameter (atoi (index->value().c_str()), atof (value->value().c_str ()));
+                                               const uint32_t p = atoi (index->value().c_str());
+                                               const float v = atof (value->value().c_str ());
+                                               set_parameter (p, v);
+                                               PresetPortSetValue (p, v); /* EMIT SIGNAL */
                                }
                        }
                        return true;
@@ -450,6 +519,8 @@ VSTPlugin::load_user_preset (PresetRecord r)
        return false;
 }
 
+#include "sha1.c"
+
 string
 VSTPlugin::do_save_preset (string name)
 {
@@ -458,9 +529,23 @@ VSTPlugin::do_save_preset (string name)
                return "";
        }
 
+       // prevent dups -- just in case
+       t->root()->remove_nodes_and_delete (X_("label"), name);
+
        XMLNode* p = 0;
-       /* XXX: use of _presets.size() + 1 for the unique ID here is dubious at best */
-       string const uri = string_compose (X_("VST:%1:%2"), unique_id (), _presets.size() + 1);
+
+       char tmp[32];
+       snprintf (tmp, 31, "%ld", _presets.size() + 1);
+       tmp[31] = 0;
+
+       char hash[41];
+       Sha1Digest s;
+       sha1_init (&s);
+       sha1_write (&s, (const uint8_t *) name.c_str(), name.size ());
+       sha1_write (&s, (const uint8_t *) tmp, strlen(tmp));
+       sha1_result_hash (&s, hash);
+
+       string const uri = string_compose (X_("VST:%1:x%2"), unique_id (), hash);
 
        if (_plugin->flags & 32 /* effFlagsProgramsChunks */) {
 
@@ -516,6 +601,11 @@ string
 VSTPlugin::describe_parameter (Evoral::Parameter param)
 {
        char name[64];
+       if (param.id() == UINT32_MAX - 1) {
+               strcpy (name, _("Plugin Enable"));
+               return name;
+       }
+
        memset (name, 0, sizeof (name));
 
        /* some VST plugins expect this buffer to be zero-filled */
@@ -563,8 +653,7 @@ VSTPlugin::connect_and_run (BufferSet& bufs,
 {
        Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
 
-       Glib::Threads::Mutex::Lock lm (_state_lock, Glib::Threads::TRY_LOCK);
-       if (!lm.locked()) {
+       if (pthread_mutex_trylock (&_state->state_lock)) {
                /* by convention 'effSetChunk' should not be called while processing
                 * http://www.reaper.fm/sdk/vst/vst_ext.php
                 *
@@ -642,6 +731,7 @@ VSTPlugin::connect_and_run (BufferSet& bufs,
        _plugin->processReplacing (_plugin, &ins[0], &outs[0], nframes);
        _midi_out_buf = 0;
 
+       pthread_mutex_unlock (&_state->state_lock);
        return 0;
 }