Another not-quite-there-but-better commit.
[ardour.git] / libs / ardour / panner.cc
index 563d4e90d41da2ebca6584bfde602a4f85e4252f..e0dcf206345febb2efad2d256edd162382c47c20 100644 (file)
@@ -17,6 +17,9 @@
 
 */
 
+#define __STDC_FORMAT_MACROS 1
+#include <inttypes.h>
+
 #include <cmath>
 #include <cerrno>
 #include <fstream>
@@ -38,7 +41,8 @@
 #include <ardour/panner.h>
 #include <ardour/utils.h>
 
-#include <ardour/mix.h>
+#include <ardour/runtime_functions.h>
+#include <ardour/buffer_set.h>
 
 #include "i18n.h"
 
@@ -67,11 +71,11 @@ static double direct_pan_to_control (pan_t val) {
 
 StreamPanner::StreamPanner (Panner& p)
        : parent (p),
-         _control (X_("panner"), *this)
+         _control (new PanControllable(p.session(), X_("panner"), *this))
 {
        _muted = false;
 
-       parent.session().add_controllable (&_control);
+       parent.session().add_controllable (_control);
 
        x = 0.5;
        y = 0.5;
@@ -128,7 +132,7 @@ StreamPanner::set_position (float xpos, bool link_call)
                x = xpos;
                update ();
                Changed ();
-               _control.Changed ();
+               _control->Changed ();
        }
 }
 
@@ -186,7 +190,7 @@ StreamPanner::add_state (XMLNode& node)
 /*---------------------------------------------------------------------- */
 
 BaseStereoPanner::BaseStereoPanner (Panner& p)
-       : StreamPanner (p), _automation (0.0, 1.0, 0.5)
+       : StreamPanner (p)
 {
 }
 
@@ -194,53 +198,16 @@ BaseStereoPanner::~BaseStereoPanner ()
 {
 }
 
-void
-BaseStereoPanner::snapshot (nframes_t now)
-{
-       if (_automation.automation_state() == Write || _automation.automation_state() == Touch) {
-               _automation.rt_add (now, x);
-       }
-}
-
-void
-BaseStereoPanner::transport_stopped (nframes_t frame)
-{
-       _automation.reposition_for_rt_add (frame);
-
-       if (_automation.automation_state() != Off) {
-               set_position (_automation.eval (frame));
-       }
-}
-
-void
-BaseStereoPanner::set_automation_style (AutoStyle style)
-{
-       _automation.set_automation_style (style);
-}
-
-void
-BaseStereoPanner::set_automation_state (AutoState state)
-{
-       if (state != _automation.automation_state()) {
-
-               _automation.set_automation_state (state);
-               
-               if (state != Off) {
-                       set_position (_automation.eval (parent.session().transport_frame()));
-               }
-       }
-}
-
 int
 BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
 {
        char line[128];
        LocaleGuard lg (X_("POSIX"));
        
-       _automation.clear ();
+       _control->list()->clear ();
 
        while (in.getline (line, sizeof (line), '\n')) {
-               jack_nframes_t when;
+               nframes_t when;
                double value;
 
                ++linecnt;
@@ -254,19 +221,21 @@ BaseStereoPanner::load (istream& in, string path, uint32_t& linecnt)
                        continue;
                }
 
-               _automation.fast_simple_add (when, value);
+               _control->list()->fast_simple_add (when, value);
        }
 
        /* now that we are done loading */
 
-       _automation.StateChanged ();
+       _control->list()->StateChanged ();
 
        return 0;
 }
 
 void
-BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nframes_t nframes)
+BaseStereoPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
 {
+       assert(obufs.count().n_audio() == 2);
+
        pan_t delta;
        Sample* dst;
        pan_t pan;
@@ -274,10 +243,12 @@ BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nf
        if (_muted) {
                return;
        }
+       
+       Sample* const src = srcbuf.data();
 
        /* LEFT */
 
-       dst = obufs[0];
+       dst = obufs.get_audio(0).data();
 
        if (fabsf ((delta = (left - desired_left))) > 0.002) { // about 1 degree of arc 
                
@@ -296,7 +267,7 @@ BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nf
                
                pan = left * gain_coeff;
 
-               Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
+               mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
                
        } else {
                
@@ -307,7 +278,7 @@ BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nf
                        
                        if (pan != 0.0f) {
                                
-                               Session::mix_buffers_with_gain(dst,src,nframes,pan);
+                               mix_buffers_with_gain(dst,src,nframes,pan);
 
                                /* mark that we wrote into the buffer */
 
@@ -317,7 +288,7 @@ BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nf
                        
                } else {
                        
-                       Session::mix_buffers_no_gain(dst,src,nframes);
+                       mix_buffers_no_gain(dst,src,nframes);
                        
                        /* mark that we wrote into the buffer */
                        
@@ -327,7 +298,7 @@ BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nf
 
        /* RIGHT */
 
-       dst = obufs[1];
+       dst = obufs.get_audio(1).data();
        
        if (fabsf ((delta = (right - desired_right))) > 0.002) { // about 1 degree of arc 
                
@@ -346,7 +317,7 @@ BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nf
                
                pan = right * gain_coeff;
                
-               Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
+               mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
                
                /* XXX it would be nice to mark the buffer as written to */
 
@@ -359,14 +330,14 @@ BaseStereoPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nf
                        
                        if (pan != 0.0f) {
                                
-                               Session::mix_buffers_with_gain(dst,src,nframes,pan);
+                               mix_buffers_with_gain(dst,src,nframes,pan);
                                
                                /* XXX it would be nice to mark the buffer as written to */
                        }
                        
                } else {
                        
-                       Session::mix_buffers_no_gain(dst,src,nframes);
+                       mix_buffers_no_gain(dst,src,nframes);
                        
                        /* XXX it would be nice to mark the buffer as written to */
                }
@@ -418,19 +389,22 @@ EqualPowerStereoPanner::update ()
 }
 
 void
-EqualPowerStereoPanner::distribute_automated (Sample* src, Sample** obufs, 
+EqualPowerStereoPanner::distribute_automated (AudioBuffer& srcbuf, BufferSet& obufs, 
                                              nframes_t start, nframes_t end, nframes_t nframes,
                                              pan_t** buffers)
 {
+       assert(obufs.count().n_audio() == 2);
+
        Sample* dst;
        pan_t* pbuf;
+       Sample* const src = srcbuf.data();
 
        /* fetch positional data */
 
-       if (!_automation.rt_safe_get_vector (start, end, buffers[0], nframes)) {
+       if (!_control->list()->curve().rt_safe_get_vector (start, end, buffers[0], nframes)) {
                /* fallback */
                if (!_muted) {
-                       distribute (src, obufs, 1.0, nframes);
+                       distribute (srcbuf, obufs, 1.0, nframes);
                }
                return;
        }
@@ -462,25 +436,25 @@ EqualPowerStereoPanner::distribute_automated (Sample* src, Sample** obufs,
 
        /* LEFT */
 
-       dst = obufs[0];
+       dst = obufs.get_audio(0).data();
        pbuf = buffers[0];
        
        for (nframes_t n = 0; n < nframes; ++n) {
                dst[n] += src[n] * pbuf[n];
        }       
 
-        /* XXX it would be nice to mark the buffer as written to */
+       /* XXX it would be nice to mark the buffer as written to */
 
        /* RIGHT */
 
-       dst = obufs[1];
+       dst = obufs.get_audio(1).data();
        pbuf = buffers[1];
 
        for (nframes_t n = 0; n < nframes; ++n) {
                dst[n] += src[n] * pbuf[n];
        }       
        
-        /* XXX it would be nice to mark the buffer as written to */
+       /* XXX it would be nice to mark the buffer as written to */
 }
 
 StreamPanner*
@@ -507,12 +481,12 @@ EqualPowerStereoPanner::state (bool full_state)
        root->add_property (X_("type"), EqualPowerStereoPanner::name);
 
        XMLNode* autonode = new XMLNode (X_("Automation"));
-       autonode->add_child_nocopy (_automation.state (full_state));
+       autonode->add_child_nocopy (_control->list()->state (full_state));
        root->add_child_nocopy (*autonode);
 
        StreamPanner::add_state (*root);
 
-       root->add_child_nocopy (_control.get_state ());
+       root->add_child_nocopy (_control->get_state ());
 
        return *root;
 }
@@ -535,15 +509,15 @@ EqualPowerStereoPanner::set_state (const XMLNode& node)
 
                if ((*iter)->name() == X_("controllable")) {
                        if ((prop = (*iter)->property("name")) != 0 && prop->value() == "panner") {
-                               _control.set_state (**iter);
+                               _control->set_state (**iter);
                        }
 
                } else if ((*iter)->name() == X_("Automation")) {
 
-                       _automation.set_state (*((*iter)->children().front()));
+                       _control->list()->set_state (*((*iter)->children().front()));
 
-                       if (_automation.automation_state() != Off) {
-                               set_position (_automation.eval (parent.session().transport_frame()));
+                       if (_control->list()->automation_state() != Off) {
+                               set_position (_control->list()->eval (parent.session().transport_frame()));
                        }
                }
        }
@@ -554,7 +528,7 @@ EqualPowerStereoPanner::set_state (const XMLNode& node)
 /*----------------------------------------------------------------------*/
 
 Multi2dPanner::Multi2dPanner (Panner& p)
-       : StreamPanner (p), _automation (0.0, 1.0, 0.5) // XXX useless
+       : StreamPanner (p)
 {
        update ();
 }
@@ -563,30 +537,6 @@ Multi2dPanner::~Multi2dPanner ()
 {
 }
 
-void
-Multi2dPanner::snapshot (nframes_t now)
-{
-       // how?
-}
-
-void
-Multi2dPanner::transport_stopped (nframes_t frame)
-{
-       //what?
-}
-
-void
-Multi2dPanner::set_automation_style (AutoStyle style)
-{
-       //what?
-}
-
-void
-Multi2dPanner::set_automation_state (AutoState state)
-{
-       // what?
-}
-
 void
 Multi2dPanner::update ()
 {
@@ -620,7 +570,7 @@ Multi2dPanner::update ()
 }
 
 void
-Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nframes_t nframes)
+Multi2dPanner::distribute (AudioBuffer& srcbuf, BufferSet& obufs, gain_t gain_coeff, nframes_t nframes)
 {
        Sample* dst;
        pan_t pan;
@@ -630,11 +580,13 @@ Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nfram
        if (_muted) {
                return;
        }
+       
+       Sample* const src = srcbuf.data();
 
 
        for (n = 0, o = parent.outputs.begin(); o != parent.outputs.end(); ++o, ++n) {
 
-               dst = obufs[n];
+               dst = obufs.get_audio(n).data();
        
 #ifdef CAN_INTERP
                if (fabsf ((delta = (left_interp - desired_left))) > 0.002) { // about 1 degree of arc 
@@ -653,7 +605,7 @@ Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nfram
                        }
                        
                        pan = left * gain_coeff;
-                       Session::mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
+                       mix_buffers_with_gain(dst+n,src+n,nframes-n,pan);
                        
                } else {
 
@@ -663,10 +615,10 @@ Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nfram
                        if ((pan *= gain_coeff) != 1.0f) {
                                
                                if (pan != 0.0f) {
-                                       Session::mix_buffers_with_gain(dst,src,nframes,pan);
+                                       mix_buffers_with_gain(dst,src,nframes,pan);
                                } 
                        } else {
-                                       Session::mix_buffers_no_gain(dst,src,nframes);
+                                       mix_buffers_no_gain(dst,src,nframes);
                        }
 #endif
 #ifdef CAN_INTERP
@@ -678,7 +630,7 @@ Multi2dPanner::distribute (Sample* src, Sample** obufs, gain_t gain_coeff, nfram
 }
 
 void
-Multi2dPanner::distribute_automated (Sample* src, Sample** obufs, 
+Multi2dPanner::distribute_automated (AudioBuffer& src, BufferSet& obufs, 
                                     nframes_t start, nframes_t end, nframes_t nframes,
                                     pan_t** buffers)
 {
@@ -917,10 +869,10 @@ Panner::reset (uint32_t nouts, uint32_t npans)
                if (changed || ((left == 0.5) && (right == 0.5))) {
                
                        front()->set_position (0.0);
-                       front()->automation().reset_default (0.0);
+                       front()->pan_control()->list()->reset_default (0.0);
                        
                        back()->set_position (1.0);
-                       back()->automation().reset_default (1.0);
+                       back()->pan_control()->list()->reset_default (1.0);
                        
                        changed = true;
                }
@@ -959,7 +911,7 @@ void
 Panner::set_automation_style (AutoStyle style)
 {
        for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
-               (*i)->set_automation_style (style);
+               (*i)->pan_control()->list()->set_automation_style (style);
        }
        _session.set_dirty ();
 }      
@@ -968,7 +920,7 @@ void
 Panner::set_automation_state (AutoState state)
 {
        for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
-               (*i)->set_automation_state (state);
+               (*i)->pan_control()->list()->set_automation_state (state);
        }
        _session.set_dirty ();
 }      
@@ -977,7 +929,7 @@ AutoState
 Panner::automation_state () const
 {
        if (!empty()) {
-               return front()->automation().automation_state ();
+               return front()->pan_control()->list()->automation_state ();
        } else {
                return Off;
        }
@@ -987,7 +939,7 @@ AutoStyle
 Panner::automation_style () const
 {
        if (!empty()) {
-               return front()->automation().automation_style ();
+               return front()->pan_control()->list()->automation_style ();
        } else {
                return Absolute;
        }
@@ -997,7 +949,7 @@ void
 Panner::transport_stopped (nframes_t frame)
 {
        for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
-               (*i)->transport_stopped (frame);
+               (*i)->pan_control()->list()->reposition_for_rt_add (frame);
        }
 }      
 
@@ -1005,7 +957,9 @@ void
 Panner::snapshot (nframes_t now)
 {
        for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
-               (*i)->snapshot (now);
+               boost::shared_ptr<AutomationList> list = (*i)->pan_control()->list();
+               if (list->automation_write())
+                       list->rt_add(now, (*i)->pan_control()->get_value());
        }
 }      
 
@@ -1013,7 +967,7 @@ void
 Panner::clear_automation ()
 {
        for (vector<StreamPanner*>::iterator i = begin(); i != end(); ++i) {
-               (*i)->automation().clear ();
+               (*i)->pan_control()->list()->clear ();
        }
        _session.set_dirty ();
 }      
@@ -1168,7 +1122,7 @@ bool
 Panner::touching () const
 {
        for (vector<StreamPanner*>::const_iterator i = begin(); i != end(); ++i) {
-               if ((*i)->automation().touching ()) {
+               if ((*i)->pan_control()->list()->touching ()) {
                        return true;
                }
        }
@@ -1319,6 +1273,134 @@ Panner::set_position (float xpos, float ypos, float zpos, StreamPanner& orig)
        }
 }
 
+void
+Panner::distribute_no_automation (BufferSet& inbufs, BufferSet& outbufs, nframes_t nframes, nframes_t offset, gain_t gain_coeff)
+{
+       if (outbufs.count().n_audio() == 0) {
+               // Don't want to lose audio...
+               assert(inbufs.count().n_audio() == 0);
+               return;
+       }
+
+       // We shouldn't be called in the first place...
+       assert(!bypassed());
+       assert(!empty());
+
+       
+       if (outbufs.count().n_audio() == 1) {
+
+               AudioBuffer& dst = outbufs.get_audio(0);
+
+               if (gain_coeff == 0.0f) {
+
+                       /* only one output, and gain was zero, so make it silent */
+
+                       dst.silence(offset);
+                       
+               } else if (gain_coeff == 1.0f){
+
+                       /* mix all buffers into the output */
+
+                       // copy the first
+                       dst.read_from(inbufs.get_audio(0), nframes, offset);
+                       
+                       // accumulate starting with the second
+                       BufferSet::audio_iterator i = inbufs.audio_begin();
+                       for (++i; i != inbufs.audio_end(); ++i) {
+                               dst.accumulate_from(*i, nframes, offset);
+                       }
+
+               } else {
+
+                       /* mix all buffers into the output, scaling them all by the gain */
+
+                       // copy the first
+                       dst.read_from(inbufs.get_audio(0), nframes, offset);
+                       
+                       // accumulate (with gain) starting with the second
+                       BufferSet::audio_iterator i = inbufs.audio_begin();
+                       for (++i; i != inbufs.audio_end(); ++i) {
+                               dst.accumulate_with_gain_from(*i, nframes, offset, gain_coeff);
+                       }
+
+               }
+
+               return;
+       }
+       
+       /* the terrible silence ... */
+       for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
+               i->silence(nframes, offset);
+       }
+
+       BufferSet::audio_iterator i = inbufs.audio_begin();
+
+       for (iterator pan = begin(); pan != end() && i != inbufs.audio_end(); ++pan, ++i) {
+               (*pan)->distribute (*i, outbufs, gain_coeff, nframes);
+       }
+}
+
+void
+Panner::distribute (BufferSet& inbufs, BufferSet& outbufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
+{      
+       if (outbufs.count().n_audio() == 0) {
+               // Failing to deliver audio we were asked to deliver is a bug
+               assert(inbufs.count().n_audio() == 0);
+               return;
+       }
+
+       // We shouldn't be called in the first place...
+       assert(!bypassed());
+       assert(!empty());
+
+       // If we shouldn't play automation defer to distribute_no_automation
+       if ( !( automation_state() & Play ||
+                        ((automation_state() & Touch) && !touching()) ) ) {
+
+               // Speed quietning
+               gain_t gain_coeff = 1.0;
+               if (fabsf(_session.transport_speed()) > 1.5f) {
+                       gain_coeff = speed_quietning;
+               }
+
+               distribute_no_automation(inbufs, outbufs, nframes, offset, gain_coeff);
+               return;
+       }
+
+       // Otherwise.. let the automation flow, baby
+       
+       if (outbufs.count().n_audio() == 1) {
+
+               AudioBuffer& dst = outbufs.get_audio(0);
+
+               // FIXME: apply gain automation?
+
+               // copy the first
+               dst.read_from(inbufs.get_audio(0), nframes, offset);
+
+               // accumulate starting with the second
+               BufferSet::audio_iterator i = inbufs.audio_begin();
+               for (++i; i != inbufs.audio_end(); ++i) {
+                       dst.accumulate_from(*i, nframes, offset);
+               }
+
+               return;
+       }
+
+       // More than 1 output, we should have 1 panner for each input
+       assert(size() == inbufs.count().n_audio());
+       
+       /* the terrible silence ... */
+       for (BufferSet::audio_iterator i = outbufs.audio_begin(); i != outbufs.audio_end(); ++i) {
+               i->silence(nframes, offset);
+       }
+
+       BufferSet::audio_iterator i = inbufs.audio_begin();
+       for (iterator pan = begin(); pan != end(); ++pan, ++i) {
+               (*pan)->distribute_automated (*i, outbufs, start_frame, end_frame, nframes, _session.pan_automation_buffer());
+       }
+}
+
 /* old school automation handling */
 
 void