when calling Session::engine_halted() after a user-driven engine stop, make sure...
[ardour.git] / libs / ardour / plugin.cc
index 11d859ed8c81fe3558d5effcd200dfb5781b0e1f..2d1ffbed7491a60f6791e2b885d337e63be829c0 100644 (file)
 #include "ardour/chan_count.h"
 #include "ardour/chan_mapping.h"
 #include "ardour/data_type.h"
+#include "ardour/luaproc.h"
 #include "ardour/midi_buffer.h"
 #include "ardour/midi_state_tracker.h"
 #include "ardour/plugin.h"
 #include "ardour/plugin_manager.h"
+#include "ardour/port.h"
 #include "ardour/session.h"
 #include "ardour/types.h"
 
@@ -63,7 +65,7 @@
 
 #include "pbd/stl_delete.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 #include <locale.h>
 
 using namespace std;
@@ -77,19 +79,23 @@ static bool seen_get_state_message = false;
 static bool seen_set_state_message = false;
 #endif
 
+PBD::Signal2<void, std::string, Plugin*> Plugin::PresetsChanged;
+
 bool
-PluginInfo::is_instrument () const
+PluginInfo::needs_midi_input () const
 {
-       return (n_inputs.n_midi() != 0) && (n_outputs.n_audio() > 0);
+       return (n_inputs.n_midi() != 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);
 }
@@ -101,9 +107,11 @@ 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);
 }
@@ -115,11 +123,23 @@ Plugin::~Plugin ()
 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;
+       _have_presets = false;
+       PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
        PresetRemoved (); /* EMIT SIGNAL */
 }
 
@@ -127,10 +147,17 @@ Plugin::remove_preset (string name)
 Plugin::PresetRecord
 Plugin::save_preset (string name)
 {
+       if (preset_by_label (name)) {
+               PBD::error << _("Preset with given name already exists.") << endmsg;
+               return Plugin::PresetRecord ();
+       }
+
        string const uri = do_save_preset (name);
 
        if (!uri.empty()) {
                _presets.insert (make_pair (uri, PresetRecord (uri, name)));
+               _have_presets = false;
+               PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
                PresetAdded (); /* EMIT SIGNAL */
        }
 
@@ -144,6 +171,10 @@ ARDOUR::find_plugin(Session& session, string identifier, PluginType type)
        PluginInfoList plugs;
 
        switch (type) {
+       case ARDOUR::Lua:
+               plugs = mgr.lua_plugin_info();
+               break;
+
        case ARDOUR::LADSPA:
                plugs = mgr.ladspa_plugin_info();
                break;
@@ -166,6 +197,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();
@@ -231,9 +268,52 @@ 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)
 {
+#ifndef NO_PLUGIN_STATE
+       if (!_have_presets) {
+               find_presets ();
+               _have_presets = true;
+       }
+#endif
        // FIXME: O(n)
        for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
                if (i->second.label == label) {
@@ -247,6 +327,12 @@ Plugin::preset_by_label (const string& label)
 const Plugin::PresetRecord *
 Plugin::preset_by_uri (const string& uri)
 {
+#ifndef NO_PLUGIN_STATE
+       if (!_have_presets) {
+               find_presets ();
+               _have_presets = true;
+       }
+#endif
        map<string, PresetRecord>::const_iterator pr = _presets.find (uri);
        if (pr != _presets.end()) {
                return &pr->second;
@@ -255,16 +341,29 @@ 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*/)
+               samplepos_t /*start*/, samplepos_t /*end*/, double /*speed*/,
+               ChanMapping /*in_map*/, ChanMapping /*out_map*/,
+               pframes_t nframes, samplecnt_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);
+               }
 
-               MidiBuffer& b = bufs.get_midi (0);
+               /* Track notes that we are sending to the plugin */
+               const MidiBuffer& b = bufs.get_midi (0);
 
                _tracker.track (b.begin(), b.end());
 
@@ -308,7 +407,6 @@ Plugin::resolve_midi ()
        _have_pending_stop_events = true;
 }
 
-
 vector<Plugin::PresetRecord>
 Plugin::get_presets ()
 {
@@ -328,6 +426,7 @@ Plugin::get_presets ()
                info << string_compose (_("Plugin presets are not supported in this build of %1. Consider paying for a full version"),
                                        PROGRAM_NAME)
                     << endmsg;
+               seen_set_state_message = true;
        }
 #endif
 
@@ -341,6 +440,7 @@ Plugin::load_preset (PresetRecord r)
        _last_preset = r;
        _parameter_changed_since_last_preset = false;
 
+       _session.set_dirty ();
        PresetLoaded (); /* EMIT SIGNAL */
        return true;
 }
@@ -352,36 +452,32 @@ Plugin::clear_preset ()
        _last_preset.label = "";
        _parameter_changed_since_last_preset = false;
 
+       _session.set_dirty ();
        PresetLoaded (); /* EMIT SIGNAL */
 }
 
-/** @param val `plugin' value */
 void
-Plugin::set_parameter (uint32_t which, float)
+Plugin::set_parameter (uint32_t /* which */, float /* value */)
+{
+       _parameter_changed_since_last_preset = true;
+       PresetDirty (); /* EMIT SIGNAL */
+}
+
+void
+Plugin::parameter_changed_externally (uint32_t which, float /* value */)
 {
        _parameter_changed_since_last_preset = true;
        _session.set_dirty ();
-       ParameterChanged (which, get_parameter (which)); /* EMIT SIGNAL */
+       ParameterChangedExternally (which, get_parameter (which)); /* EMIT SIGNAL */
+       PresetDirty (); /* EMIT SIGNAL */
 }
 
 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;
 }
 
@@ -389,13 +485,12 @@ XMLNode &
 Plugin::get_state ()
 {
        XMLNode* root = new XMLNode (state_node_name ());
-       LocaleGuard lg (X_("POSIX"));
 
-       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        
+#ifndef NO_PLUGIN_STATE
        add_state (root);
 #else
        if (!seen_get_state_message) {
@@ -415,4 +510,43 @@ 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;
+}
+
+bool
+PluginInfo::is_effect () const
+{
+       return (!is_instrument () && !is_utility ()  && !is_analyzer ());
+}
+
+bool
+PluginInfo::is_instrument () const
+{
+       if (category == "Instrument") {
+               return true;
+       }
+
+       // second check: if we have  midi input and audio output, we're likely an instrument
+       return (n_inputs.n_midi() != 0) && (n_outputs.n_audio() > 0) && (n_inputs.n_audio() == 0);
+}
+
+bool
+PluginInfo::is_utility () const
+{
+       /* XXX beware of translations, e.g. LV2 categories */
+       return (category == "Utility" || category == "MIDI" || category == "Generator");
+}
 
+bool
+PluginInfo::is_analyzer () const
+{
+       return (category == "Analyser" || category == "Anaylsis" || category == "Analyzer");
+}