Allow to send MIDI data directly to a plugin
[ardour.git] / libs / ardour / plugin.cc
index 4bb5b02c77be15a04d440e136ed2e2d5c834b506..64f00cf1c4768504335452b6d5c9178fe38c2cda 100644 (file)
@@ -65,7 +65,7 @@
 
 #include "pbd/stl_delete.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 #include <locale.h>
 
 using namespace std;
@@ -81,22 +81,29 @@ static bool seen_set_state_message = false;
 
 PBD::Signal2<void, std::string, Plugin*> Plugin::PresetsChanged;
 
+bool
+PluginInfo::needs_midi_input () const
+{
+       return (n_inputs.n_midi() != 0);
+}
+
 bool
 PluginInfo::is_instrument () const
 {
-       return (n_inputs.n_midi() != 0) && (n_outputs.n_audio() > 0);
+       return (n_inputs.n_midi() != 0) && (n_outputs.n_audio() > 0) && (n_inputs.n_audio() == 0);
 }
 
 Plugin::Plugin (AudioEngine& e, Session& s)
        : _engine (e)
        , _session (s)
        , _cycles (0)
+       , _owner (0)
        , _have_presets (false)
        , _have_pending_stop_events (false)
        , _parameter_changed_since_last_preset (false)
+       , _immediate_events(6096) // FIXME: size?
 {
        _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
-       PresetsChanged.connect_same_thread (_preset_connection, boost::bind (&Plugin::update_presets, this, _1 ,_2));
 }
 
 Plugin::Plugin (const Plugin& other)
@@ -106,12 +113,13 @@ Plugin::Plugin (const Plugin& other)
        , _session (other._session)
        , _info (other._info)
        , _cycles (0)
+       , _owner (other._owner)
        , _have_presets (false)
        , _have_pending_stop_events (false)
        , _parameter_changed_since_last_preset (false)
+       , _immediate_events(6096) // FIXME: size?
 {
        _pending_stop_events.ensure_buffers (DataType::MIDI, 1, 4096);
-       PresetsChanged.connect_same_thread (_preset_connection, boost::bind (&Plugin::update_presets, this, _1 ,_2));
 }
 
 Plugin::~Plugin ()
@@ -122,18 +130,23 @@ void
 Plugin::remove_preset (string name)
 {
        Plugin::PresetRecord const * p = preset_by_label (name);
+       if (!p) {
+               PBD::error << _("Trying to remove nonexistent preset.") << endmsg;
+               return;
+       }
        if (!p->user) {
                PBD::error << _("Cannot remove plugin factory preset.") << endmsg;
                return;
        }
 
        do_remove_preset (name);
-       _presets.erase (preset_by_label (name)->uri);
+       _presets.erase (p->uri);
 
        _last_preset.uri = "";
        _parameter_changed_since_last_preset = false;
-       PresetRemoved (); /* EMIT SIGNAL */
+       _have_presets = false;
        PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
+       PresetRemoved (); /* EMIT SIGNAL */
 }
 
 /** @return PresetRecord with empty URI on failure */
@@ -149,8 +162,9 @@ Plugin::save_preset (string name)
 
        if (!uri.empty()) {
                _presets.insert (make_pair (uri, PresetRecord (uri, name)));
-               PresetAdded (); /* EMIT SIGNAL */
+               _have_presets = false;
                PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
+               PresetAdded (); /* EMIT SIGNAL */
        }
 
        return PresetRecord (uri, name);
@@ -164,10 +178,7 @@ ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
 
        switch (type) {
        case ARDOUR::Lua:
-               {
-                       PluginPtr plugin (new LuaProc (session.engine(), session, ""));
-                       return plugin;
-               }
+               plugs = mgr.lua_plugin_info();
                break;
 
        case ARDOUR::LADSPA:
@@ -192,6 +203,12 @@ ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
                break;
 #endif
 
+#ifdef MACVST_SUPPORT
+       case ARDOUR::MacVST:
+               plugs = mgr.mac_vst_plugin_info();
+               break;
+#endif
+
 #ifdef AUDIOUNIT_SUPPORT
        case ARDOUR::AudioUnit:
                plugs = mgr.au_plugin_info();
@@ -257,6 +274,43 @@ Plugin::input_streams () const
        return ChanCount::ZERO;
 }
 
+Plugin::IOPortDescription
+Plugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
+{
+       std::stringstream ss;
+       switch (dt) {
+               case DataType::AUDIO:
+                       ss << _("Audio") << " ";
+                       break;
+               case DataType::MIDI:
+                       ss << _("Midi") << " ";
+                       break;
+               default:
+                       ss << _("?") << " ";
+                       break;
+       }
+       if (input) {
+               ss << _("In") << " ";
+       } else {
+               ss << _("Out") << " ";
+       }
+
+       ss << (id + 1);
+
+       Plugin::IOPortDescription iod (ss.str());
+       return iod;
+}
+
+PluginOutputConfiguration
+Plugin::possible_output () const
+{
+       PluginOutputConfiguration oc;
+       if (_info) {
+               oc.insert (_info->n_outputs.n_audio ());
+       }
+       return oc;
+}
+
 const Plugin::PresetRecord *
 Plugin::preset_by_label (const string& label)
 {
@@ -293,15 +347,28 @@ Plugin::preset_by_uri (const string& uri)
        }
 }
 
+bool
+Plugin::write_immediate_event (size_t size, const uint8_t* buf)
+{
+       if (!Evoral::midi_event_is_valid (buf, size)) {
+               return false;
+       }
+       return (_immediate_events.write (0, Evoral::MIDI_EVENT, size, buf) == size);
+}
+
 int
 Plugin::connect_and_run (BufferSet& bufs,
-                        ChanMapping /*in_map*/, ChanMapping /*out_map*/,
-                        pframes_t /* nframes */, framecnt_t /*offset*/)
+               framepos_t /*start*/, framepos_t /*end*/, double /*speed*/,
+               ChanMapping /*in_map*/, ChanMapping /*out_map*/,
+               pframes_t nframes, framecnt_t /*offset*/)
 {
        if (bufs.count().n_midi() > 0) {
 
-               /* Track notes that we are sending to the plugin */
+               if (_immediate_events.read_space() && nframes > 0) {
+                       _immediate_events.read (bufs.get_midi (0), 0, 1, nframes - 1, true);
+               }
 
+               /* Track notes that we are sending to the plugin */
                const MidiBuffer& b = bufs.get_midi (0);
 
                _tracker.track (b.begin(), b.end());
@@ -342,22 +409,10 @@ Plugin::resolve_midi ()
        */
 
        _pending_stop_events.get_midi(0).clear ();
-       _tracker.resolve_notes (_pending_stop_events.get_midi (0), /* split cycle offset*/ Port::port_offset());
+       _tracker.resolve_notes (_pending_stop_events.get_midi (0), 0);
        _have_pending_stop_events = true;
 }
 
-void
-Plugin::update_presets (std::string src_unique_id, Plugin* src )
-{
-       if (src == this || unique_id() != src_unique_id) {
-               return;
-       }
-       _have_presets = false;
-       // TODO check if a preset was added/removed and emit the proper signal
-       // so far no subscriber distinguishes between PresetAdded and PresetRemoved
-       PresetAdded();
-}
-
 vector<Plugin::PresetRecord>
 Plugin::get_presets ()
 {
@@ -391,6 +446,7 @@ Plugin::load_preset (PresetRecord r)
        _last_preset = r;
        _parameter_changed_since_last_preset = false;
 
+       _session.set_dirty ();
        PresetLoaded (); /* EMIT SIGNAL */
        return true;
 }
@@ -402,6 +458,7 @@ Plugin::clear_preset ()
        _last_preset.label = "";
        _parameter_changed_since_last_preset = false;
 
+       _session.set_dirty ();
        PresetLoaded (); /* EMIT SIGNAL */
 }
 
@@ -409,7 +466,6 @@ void
 Plugin::set_parameter (uint32_t /* which */, float /* value */)
 {
        _parameter_changed_since_last_preset = true;
-       _session.set_dirty ();
        PresetDirty (); /* EMIT SIGNAL */
 }
 
@@ -425,21 +481,9 @@ Plugin::parameter_changed_externally (uint32_t which, float /* value */)
 int
 Plugin::set_state (const XMLNode& node, int /*version*/)
 {
-       XMLProperty const * p = node.property (X_("last-preset-uri"));
-       if (p) {
-               _last_preset.uri = p->value ();
-       }
-
-       p = node.property (X_("last-preset-label"));
-       if (p) {
-               _last_preset.label = p->value ();
-       }
-
-       p = node.property (X_("parameter-changed-since-last-preset"));
-       if (p) {
-               _parameter_changed_since_last_preset = string_is_affirmative (p->value ());
-       }
-
+       node.get_property (X_("last-preset-uri"), _last_preset.uri);
+       node.get_property (X_("last-preset-label"), _last_preset.label);
+       node.get_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset);
        return 0;
 }
 
@@ -447,11 +491,10 @@ XMLNode &
 Plugin::get_state ()
 {
        XMLNode* root = new XMLNode (state_node_name ());
-       LocaleGuard lg (X_("C"));
 
-       root->add_property (X_("last-preset-uri"), _last_preset.uri);
-       root->add_property (X_("last-preset-label"), _last_preset.label);
-       root->add_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset ? X_("yes") : X_("no"));
+       root->set_property (X_("last-preset-uri"), _last_preset.uri);
+       root->set_property (X_("last-preset-label"), _last_preset.label);
+       root->set_property (X_("parameter-changed-since-last-preset"), _parameter_changed_since_last_preset);
 
 #ifndef NO_PLUGIN_STATE
        add_state (root);
@@ -473,4 +516,13 @@ Plugin::set_info (PluginInfoPtr info)
        _info = info;
 }
 
-
+std::string
+Plugin::parameter_label (uint32_t which) const
+{
+       if (which >= parameter_count ()) {
+               return "";
+       }
+       ParameterDescriptor pd;
+       get_parameter_descriptor (which, pd);
+       return pd.label;
+}