Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / vst_plugin.cc
index 4c09ba34401ed848a7e84e5bd19bf642e39480b0..11169a74de8f02fd78cbe116ec8fd0d24219e7fd 100644 (file)
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <algorithm>
 #include <vector>
 #include <string>
-#include <ctype.h>
+#include <cctype>
 
 #include <cstdlib>
 #include <cstdio> // so libraptor doesn't complain
 #include <cmath>
 #include <dirent.h>
-#include <string.h> // for memmove
+#include <cstring> // for memmove
 #include <sys/stat.h>
 #include <cerrno>
 
+#include <glibmm/ustring.h>
+#include <glibmm/miscutils.h>
+
 #include <lrdf.h>
 #include <fst.h>
 
 
 #include <vst/aeffectx.h>
 
-#include <ardour/ardour.h>
 #include <ardour/session.h>
 #include <ardour/audioengine.h>
+#include <ardour/filesystem_paths.h>
 #include <ardour/vst_plugin.h>
+#include <ardour/buffer_set.h>
 
 #include <pbd/stl_delete.h>
 
@@ -98,28 +101,18 @@ VSTPlugin::VSTPlugin (const VSTPlugin &other)
 VSTPlugin::~VSTPlugin ()
 {
        deactivate ();
-       GoingAway (this); /* EMIT SIGNAL */
+       GoingAway (); /* EMIT SIGNAL */
        fst_close (_fst);
 }
 
 void
-VSTPlugin::set_block_size (jack_nframes_t nframes)
+VSTPlugin::set_block_size (nframes_t nframes)
 {
        deactivate ();
        _plugin->dispatcher (_plugin, effSetBlockSize, 0, nframes, NULL, 0.0f);
        activate ();
 }
 
-void
-VSTPlugin::store_state (PluginState& state)
-{
-}
-
-void
-VSTPlugin::restore_state (PluginState& state)
-{
-}
-
 float
 VSTPlugin::default_value (uint32_t port)
 {
@@ -130,7 +123,7 @@ void
 VSTPlugin::set_parameter (uint32_t which, float val)
 {
        _plugin->setParameter (_plugin, which, val);
-       ParameterChanged (which, val); /* EMIT SIGNAL */
+       //ParameterChanged (which, val); /* EMIT SIGNAL */
 }
 
 float
@@ -152,7 +145,7 @@ VSTPlugin::get_state()
 {
        XMLNode *root = new XMLNode (state_node_name());
        LocaleGuard lg (X_("POSIX"));
-       
+
        if (_plugin->flags & effFlagsProgramChunks) {
 
                /* fetch the current chunk */
@@ -166,26 +159,27 @@ VSTPlugin::get_state()
 
                /* save it to a file */
 
-               string path;
+               Glib::ustring path = Glib::build_filename (get_user_ardour_path (), "vst");
                struct stat sbuf;
 
-               path = getenv ("HOME");
-               path += "/.ardour/vst";
+               sys::path user_vst_directory(user_config_directory());
+       
+               user_vst_directory /= "vst";
+               path = user_vst_directory.to_string();
 
                if (stat (path.c_str(), &sbuf)) {
                        if (errno == ENOENT) {
-                               if (mkdir (path.c_str(), 0600)) {
+                               if (g_mkdir_with_parents (path.c_str(), 0600)) {
                                        error << string_compose (_("cannot create VST chunk directory: %1"),
-                                                         strerror (errno))
+                                                                strerror (errno))
                                              << endmsg;
                                        return *root;
                                }
 
                        } else {
 
-                               error << string_compose (_("cannot check VST chunk directory: %1"),
-                                                 strerror (errno))
-                                     << endmsg;
+                               warning << string_compose (_("cannot check VST chunk directory: %1"), strerror (errno))
+                                       << endmsg;
                                return *root;
                        }
 
@@ -195,7 +189,7 @@ VSTPlugin::get_state()
                        return *root;
                }
                
-               path += "something";
+               path = Glib::build_filename (path, "something");
                
                /* store information */
 
@@ -212,7 +206,7 @@ VSTPlugin::get_state()
                        char index[64];
                        char val[32];
                        snprintf (index, sizeof (index), "param_%ld", n);
-                       snprintf (val, sizeof (val), "%f", _plugin->getParameter (_plugin, n));
+                       snprintf (val, sizeof (val), "%.12g", _plugin->getParameter (_plugin, n));
                        parameters->add_property (index, val);
                }
 
@@ -245,6 +239,7 @@ VSTPlugin::set_state(const XMLNode& node)
                for (i = child->properties().begin(); i != child->properties().end(); ++i) {
                        long param;
                        float val;
+
                        sscanf ((*i)->name().c_str(), "param_%ld", &param);
                        sscanf ((*i)->value().c_str(), "%f", &val);
 
@@ -357,9 +352,13 @@ VSTPlugin::describe_parameter (uint32_t param)
        return name;
 }
 
-jack_nframes_t
-VSTPlugin::latency () const
+nframes_t
+VSTPlugin::signal_latency () const
 {
+       if (_user_latency) {
+               return _user_latency;
+       }
+
        return _plugin->initialDelay;
 }
 
@@ -376,19 +375,21 @@ VSTPlugin::automatable () const
 }
 
 int
-VSTPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in_index, int32_t& out_index, jack_nframes_t nframes, jack_nframes_t offset)
+VSTPlugin::connect_and_run (BufferSet& bufs, uint32_t& in_index, uint32_t& out_index, nframes_t nframes, nframes_t offset)
 {
        float *ins[_plugin->numInputs];
        float *outs[_plugin->numOutputs];
        int32_t i;
 
+       const uint32_t nbufs = bufs.count().n_audio();
+
        for (i = 0; i < (int32_t) _plugin->numInputs; ++i) {
-               ins[i] = bufs[min((uint32_t) in_index,maxbuf - 1)] + offset;
+               ins[i] = bufs.get_audio(min((uint32_t) in_index, nbufs - 1)).data() + offset;
                in_index++;
        }
 
        for (i = 0; i < (int32_t) _plugin->numOutputs; ++i) {
-               outs[i] = bufs[min((uint32_t) out_index,maxbuf - 1)] + offset;
+               outs[i] = bufs.get_audio(min((uint32_t) out_index, nbufs - 1)).data() + offset;
 
                /* unbelievably, several VST plugins still rely on Cubase
                   behaviour and do not silence the buffer in processReplacing 
@@ -419,10 +420,12 @@ VSTPlugin::activate ()
        _plugin->dispatcher (_plugin, effMainsChanged, 0, 1, NULL, 0.0f);
 }
 
-uint32_t 
+string
 VSTPlugin::unique_id() const
 {
-       return _plugin->uniqueID;
+       char buf[32];
+       snprintf (buf, sizeof (buf), "%d", _plugin->uniqueID);
+       return string (buf);
 }