Some improvements to performance with crossfades: don't recompute a whole track's...
[ardour.git] / libs / ardour / amp.cc
index 755dc8b8212d7f197a7a86b9ea22517b92ed5ae9..b8e0ad700f12c3530a05859fb28f5635ab31f4a7 100644 (file)
@@ -16,6 +16,7 @@
     675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
+#include <iostream>
 #include <cstring>
 #include <cmath>
 #include <algorithm>
@@ -27,6 +28,7 @@
 #include "ardour/buffer_set.h"
 #include "ardour/configuration.h"
 #include "ardour/io.h"
+#include "ardour/midi_buffer.h"
 #include "ardour/mute_master.h"
 #include "ardour/session.h"
 
@@ -46,6 +48,12 @@ Amp::Amp(Session& s, boost::shared_ptr<MuteMaster> mm)
        add_control(_gain_control);
 }
 
+std::string
+Amp::display_name() const
+{
+       return _("Fader");
+}
+
 bool
 Amp::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
 {
@@ -64,10 +72,14 @@ Amp::configure_io (ChanCount in, ChanCount out)
 }
 
 void
-Amp::run_in_place (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
+Amp::run (BufferSet& bufs, sframes_t /*start_frame*/, sframes_t /*end_frame*/, nframes_t nframes)
 {
        gain_t mute_gain;
 
+       if (!_active && !_pending_active) {
+               return;
+       }
+
        if (_mute_master) {
                mute_gain = _mute_master->mute_gain_at (MuteMaster::PreFader);
        } else {
@@ -137,27 +149,31 @@ Amp::run_in_place (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame,
                                Amp::apply_gain (bufs, nframes, _current_gain, dg);
                                _current_gain = dg;
                                
-                       } else if ((_current_gain != 0.0f) && (_current_gain != 1.0f)) {
+                       } else if (_current_gain != 1.0f) {
                                
-                               /* gain has not changed, but its non-unity, so apply it unless
-                                  its zero.
+                               /* gain has not changed, but its non-unity
                                */
 
-                               for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
-                                       Sample* const sp = i->data();
-                                       apply_gain_to_buffer(sp, nframes, _current_gain);
-                               }
+                               for (BufferSet::midi_iterator i = bufs.midi_begin(); i != bufs.midi_end(); ++i) {
 
-                       } else if (_current_gain == 0.0f) {
-                               
-                               /* silence! */
+                                       MidiBuffer& mb (*i);
+                                       
+                                       for (MidiBuffer::iterator m = mb.begin(); m != mb.end(); ++m) {
+                                               Evoral::MIDIEvent<MidiBuffer::TimeType> ev = *m;
+                                               if (ev.is_note_on()) {
+                                                       ev.scale_velocity (_current_gain);
+                                               }
+                                       }
+                               }
 
                                for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
-                                       i->clear();
+                                       apply_gain_to_buffer (i->data(), nframes, _current_gain);
                                }
-                       }
+                       } 
                }
        }
+
+       _active = _pending_active;
 }
 
 void
@@ -190,9 +206,29 @@ Amp::apply_gain (BufferSet& bufs, nframes_t nframes, gain_t initial, gain_t targ
                delta = target - initial;
        }
 
+       /* MIDI Gain */
+
+       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;
+
+                       if (ev.is_note_on()) {
+                               gain_t scale = delta * (ev.time()/nframes);
+                               std::cerr << "scale by " << scale << " for " << ev.time() << " of " << nframes << std::endl;
+                               ev.scale_velocity (scale);
+                       }
+               }
+       }
+
+       /* Audio Gain */
+
        for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
                Sample* const buffer = i->data();
-
+               
                fractional_pos = 1.0;
 
                for (nframes_t nx = 0; nx < declick; ++nx) {
@@ -217,10 +253,35 @@ void
 Amp::apply_simple_gain (BufferSet& bufs, nframes_t nframes, gain_t target)
 {
        if (target == 0.0) {
+
+               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;
+                               if (ev.is_note_on()) {
+                                       ev.set_velocity (0);
+                               }
+                       }
+               }
+
                for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
                        memset (i->data(), 0, sizeof (Sample) * nframes);
                }
+
        } else if (target != 1.0) {
+
+               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;
+                               if (ev.is_note_on()) {
+                                       ev.scale_velocity (target);
+                               }
+                       }
+               }
+
                for (BufferSet::audio_iterator i = bufs.audio_begin(); i != bufs.audio_end(); ++i) {
                        apply_gain_to_buffer (i->data(), nframes, target);
                }
@@ -231,6 +292,7 @@ void
 Amp::inc_gain (gain_t factor, void *src)
 {
        float desired_gain = _gain_control->user_float();
+       
        if (desired_gain == 0.0f) {
                set_gain (0.000001f + (0.000001f * factor), src);
        } else {
@@ -249,28 +311,13 @@ Amp::set_gain (gain_t val, void *src)
        //cerr << "set desired gain to " << val << " when curgain = " << _gain_control->get_value () << endl;
 
        if (src != _gain_control.get()) {
-               _gain_control->set_value(val);
+               _gain_control->set_value (val);
                // bit twisty, this will come back and call us again
                // (this keeps control in sync with reality)
                return;
        }
 
-       {
-               // Glib::Mutex::Lock dm (declick_lock);
-               _gain_control->set_float(val, false);
-       }
-
-       if (_session.transport_stopped()) {
-               // _gain = val;
-       }
-       
-       /*
-       if (_session.transport_stopped() && src != 0 && src != this && _gain_control->automation_write()) {
-               _gain_control->list()->add (_session.transport_frame(), val);
-               
-       }
-       */
-
+       _gain_control->set_float(val, false);
        _session.set_dirty();
 }
 
@@ -279,9 +326,33 @@ Amp::state (bool full_state)
 {
        XMLNode& node (Processor::state (full_state));
        node.add_property("type", "amp");
+
+       char buf[32];
+       snprintf (buf, sizeof (buf), "%2.12f", _gain_control->get_value());
+       node.add_property("gain", buf);
+
        return node;
 }
 
+int
+Amp::set_state (const XMLNode& node)
+{
+       const XMLProperty* prop;
+
+       Processor::set_state (node);
+       prop = node.property ("gain");
+
+       if (prop) {
+               gain_t val;
+
+               if (sscanf (prop->value().c_str(), "%f", &val) == 1) {
+                       _gain_control->set_value (val);
+               }
+       }
+
+       return 0;
+}
+
 void
 Amp::GainControl::set_value (float val)
 {
@@ -312,3 +383,9 @@ Amp::setup_gain_automation (sframes_t start_frame, sframes_t end_frame, nframes_
                _apply_gain_automation = false;
        }
 }
+
+bool
+Amp::visible() const
+{
+       return true;
+}