Apply VCA master gain automation to Amp (Fader, Trim)
[ardour.git] / libs / ardour / amp.cc
index d5b9f56ca5f932a7ddc67eaeada5668815c1e2e5..d7b901434069f3a221af9ae4c4a1d96b2cb1f6f1 100644 (file)
@@ -16,7 +16,6 @@
     675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
-#include <iostream>
 #include <cstring>
 #include <cmath>
 #include <algorithm>
@@ -31,7 +30,7 @@
 #include "ardour/rc_configuration.h"
 #include "ardour/session.h"
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace ARDOUR;
 using namespace PBD;
@@ -70,8 +69,15 @@ Amp::configure_io (ChanCount in, ChanCount out)
        return Processor::configure_io (in, out);
 }
 
+static void
+scale_midi_velocity(Evoral::Event<MidiBuffer::TimeType>& ev, float factor)
+{
+       factor = std::max(factor, 0.0f);
+       ev.set_velocity(std::min(127L, lrintf(ev.velocity() * factor)));
+}
+
 void
-Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
+Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double /*speed*/, pframes_t nframes, bool)
 {
        if (!_active && !_pending_active) {
                return;
@@ -88,10 +94,10 @@ Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/,
                                for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
                                        MidiBuffer& mb (*i);
                                        for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
-                                               Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
+                                               Evoral::Event<MidiBuffer::TimeType> ev = *m;
                                                if (ev.is_note_on()) {
                                                        assert(ev.time() >= 0 && ev.time() < nframes);
-                                                       ev.scale_velocity (fabsf (gab[ev.time()]));
+                                                       scale_midi_velocity (ev, fabsf (gab[ev.time()]));
                                                }
                                        }
                                }
@@ -118,7 +124,7 @@ Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/,
 
                } else { /* manual (scalar) gain */
 
-                       gain_t const dg = _gain_control->user_double();
+                       gain_t const dg = _gain_control->get_value();
 
                        if (_current_gain != dg) {
 
@@ -135,9 +141,9 @@ Amp::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/,
                                                MidiBuffer& mb (*i);
 
                                                for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
-                                                       Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
+                                                       Evoral::Event<MidiBuffer::TimeType> ev = *m;
                                                        if (ev.is_note_on()) {
-                                                               ev.scale_velocity (fabsf (_current_gain));
+                                                               scale_midi_velocity (ev, fabsf (_current_gain));
                                                        }
                                                }
                                        }
@@ -186,11 +192,11 @@ Amp::apply_gain (BufferSet& bufs, framecnt_t sample_rate, framecnt_t nframes, ga
                        MidiBuffer& mb (*i);
 
                        for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
-                               Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
+                               Evoral::Event<MidiBuffer::TimeType> ev = *m;
 
                                if (ev.is_note_on()) {
                                        const gain_t scale = delta * (ev.time()/(double) nframes);
-                                       ev.scale_velocity (fabsf (initial+scale));
+                                       scale_midi_velocity (ev, fabsf (initial + scale));
                                }
                        }
                }
@@ -304,7 +310,7 @@ Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target, bool
                                MidiBuffer& mb (*i);
 
                                for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
-                                       Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
+                                       Evoral::Event<MidiBuffer::TimeType> ev = *m;
                                        if (ev.is_note_on()) {
                                                ev.set_velocity (0);
                                        }
@@ -324,9 +330,9 @@ Amp::apply_simple_gain (BufferSet& bufs, framecnt_t nframes, gain_t target, bool
                                MidiBuffer& mb (*i);
 
                                for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
-                                       Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
+                                       Evoral::Event<MidiBuffer::TimeType> ev = *m;
                                        if (ev.is_note_on()) {
-                                               ev.scale_velocity (fabsf (target));
+                                               scale_midi_velocity(ev, fabsf (target));
                                        }
                                }
                        }
@@ -352,7 +358,7 @@ XMLNode&
 Amp::state (bool full_state)
 {
        XMLNode& node (Processor::state (full_state));
-       node.add_property("type", _gain_control->parameter().type() == GainAutomation ? "amp" : "trim");
+       node.set_property("type", _gain_control->parameter().type() == GainAutomation ? "amp" : "trim");
        node.add_child_nocopy (_gain_control->get_state());
 
        return node;
@@ -361,13 +367,13 @@ Amp::state (bool full_state)
 int
 Amp::set_state (const XMLNode& node, int version)
 {
-        XMLNode* gain_node;
+       XMLNode* gain_node;
 
        Processor::set_state (node, version);
 
-        if ((gain_node = node.child (Controllable::xml_node_name.c_str())) != 0) {
-                _gain_control->set_state (*gain_node, version);
-        }
+       if ((gain_node = node.child (Controllable::xml_node_name.c_str ())) != 0) {
+               _gain_control->set_state (*gain_node, version);
+       }
 
        return 0;
 }
@@ -386,9 +392,10 @@ Amp::setup_gain_automation (framepos_t start_frame, framepos_t end_frame, framec
            && _gain_control->automation_playback())
        {
                assert (_gain_automation_buffer);
-               _apply_gain_automation = _gain_control->list()->curve().rt_safe_get_vector (
-                       start_frame, end_frame, _gain_automation_buffer, nframes);
-               if (start_frame != _current_automation_frame) {
+
+               _apply_gain_automation = _gain_control->get_masters_curve ( start_frame, end_frame, _gain_automation_buffer, nframes);
+
+               if (start_frame != _current_automation_frame && _session.bounce_processing ()) {
                        _current_gain = _gain_automation_buffer[0];
                }
                _current_automation_frame = end_frame;
@@ -405,7 +412,7 @@ Amp::visible() const
 }
 
 std::string
-Amp::value_as_string (boost::shared_ptr<AutomationControl> ac) const
+Amp::value_as_string (boost::shared_ptr<const AutomationControl> ac) const
 {
        if (ac == _gain_control) {
                char buffer[32];