enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / libs / ardour / plugin.cc
index 8cdbf98b899258502dc616042f7b51993ff9d328..884ac0d53efbfc8b0f2607a2e55453483045219a 100644 (file)
 #include <cstdlib>
 #include <cstdio> // so libraptor doesn't complain
 #include <cmath>
+#ifndef COMPILER_MSVC
 #include <dirent.h>
+#endif
 #include <sys/stat.h>
 #include <cerrno>
 #include <utility>
 
+#ifdef HAVE_LRDF
 #include <lrdf.h>
+#endif
 
 #include "pbd/compose.h"
 #include "pbd/error.h"
 #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"
 
@@ -59,7 +65,7 @@
 
 #include "pbd/stl_delete.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 #include <locale.h>
 
 using namespace std;
@@ -68,6 +74,13 @@ using namespace PBD;
 
 namespace ARDOUR { class AudioEngine; }
 
+#ifdef NO_PLUGIN_STATE
+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
 {
@@ -83,6 +96,7 @@ Plugin::Plugin (AudioEngine& e, Session& s)
        , _parameter_changed_since_last_preset (false)
 {
        _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)
@@ -97,6 +111,7 @@ Plugin::Plugin (const Plugin& other)
        , _parameter_changed_since_last_preset (false)
 {
        _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 ()
@@ -106,23 +121,36 @@ Plugin::~Plugin ()
 void
 Plugin::remove_preset (string name)
 {
+       Plugin::PresetRecord const * p = preset_by_label (name);
+       if (!p->user) {
+               PBD::error << _("Cannot remove plugin factory preset.") << endmsg;
+               return;
+       }
+
        do_remove_preset (name);
        _presets.erase (preset_by_label (name)->uri);
 
        _last_preset.uri = "";
        _parameter_changed_since_last_preset = false;
        PresetRemoved (); /* EMIT SIGNAL */
+       PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
 }
 
 /** @return PresetRecord with empty URI on failure */
 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)));
                PresetAdded (); /* EMIT SIGNAL */
+               PresetsChanged (unique_id(), this); /* EMIT SIGNAL */
        }
 
        return PresetRecord (uri, name);
@@ -135,6 +163,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;
@@ -222,9 +254,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) {
@@ -238,6 +313,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;
@@ -248,17 +329,17 @@ Plugin::preset_by_uri (const string& uri)
 
 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 */
 
-               MidiBuffer& b = bufs.get_midi (0);
-               bool looped;
+               const MidiBuffer& b = bufs.get_midi (0);
 
-               _tracker.track (b.begin(), b.end(), looped);
+               _tracker.track (b.begin(), b.end());
 
                if (_have_pending_stop_events) {
                        /* Transmit note-offs that are pending from the last transport stop */
@@ -296,22 +377,44 @@ Plugin::resolve_midi ()
        */
 
        _pending_stop_events.get_midi(0).clear ();
-       _tracker.resolve_notes (_pending_stop_events.get_midi (0), 0);
+       _tracker.resolve_notes (_pending_stop_events.get_midi (0), /* split cycle offset*/ Port::port_offset());
        _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 ()
 {
+       vector<PresetRecord> p;
+
+#ifndef NO_PLUGIN_STATE
        if (!_have_presets) {
                find_presets ();
                _have_presets = true;
        }
 
-       vector<PresetRecord> p;
        for (map<string, PresetRecord>::const_iterator i = _presets.begin(); i != _presets.end(); ++i) {
                p.push_back (i->second);
        }
+#else
+       if (!seen_set_state_message) {
+               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
 
        return p;
 }
@@ -337,13 +440,21 @@ Plugin::clear_preset ()
        PresetLoaded (); /* EMIT SIGNAL */
 }
 
-/** @param val `plugin' value */
 void
-Plugin::set_parameter (uint32_t which, float val)
+Plugin::set_parameter (uint32_t /* which */, float /* value */)
 {
        _parameter_changed_since_last_preset = true;
        _session.set_dirty ();
-       ParameterChanged (which, val); /* EMIT SIGNAL */
+       PresetDirty (); /* EMIT SIGNAL */
+}
+
+void
+Plugin::parameter_changed_externally (uint32_t which, float /* value */)
+{
+       _parameter_changed_since_last_preset = true;
+       _session.set_dirty ();
+       ParameterChangedExternally (which, get_parameter (which)); /* EMIT SIGNAL */
+       PresetDirty (); /* EMIT SIGNAL */
 }
 
 int
@@ -371,13 +482,23 @@ XMLNode &
 Plugin::get_state ()
 {
        XMLNode* root = new XMLNode (state_node_name ());
-       LocaleGuard lg (X_("POSIX"));
+       LocaleGuard lg;
 
        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"));
 
+#ifndef NO_PLUGIN_STATE
        add_state (root);
+#else
+       if (!seen_get_state_message) {
+               info << string_compose (_("Saving plugin settings is not supported in this build of %1. Consider paying for the full version"),
+                                       PROGRAM_NAME)
+                    << endmsg;
+               seen_get_state_message = true;
+       }
+#endif
+
        return *root;
 }