Inrease the export "chunk size" to speed it up over 10% at least in some situations
[ardour.git] / libs / ardour / session_click.cc
index d5a3bc5e4213d54ce5e251addac1c8377c0077a6..abfbaecfcd69f03b5da7e9ded5ad2df007722f60 100644 (file)
 #include <list>
 #include <cerrno>
 
-#include "ardour/ardour.h"
+#include "ardour/amp.h"
 #include "ardour/audio_buffer.h"
 #include "ardour/buffer_set.h"
 #include "ardour/click.h"
 #include "ardour/io.h"
 #include "ardour/session.h"
 #include "ardour/tempo.h"
+#include "ardour/types.h"
 
 #include <sndfile.h>
 
@@ -36,7 +37,7 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-Pool Click::pool ("click", sizeof (Click), 128);
+Pool Click::pool ("click", sizeof (Click), 1024);
 
 void
 Session::click (framepos_t start, framecnt_t nframes)
@@ -44,6 +45,7 @@ Session::click (framepos_t start, framecnt_t nframes)
        TempoMap::BBTPointList::const_iterator points_begin;
        TempoMap::BBTPointList::const_iterator points_end;
        Sample *buf;
+       framecnt_t click_distance;
 
        if (_click_io == 0) {
                return;
@@ -51,15 +53,25 @@ Session::click (framepos_t start, framecnt_t nframes)
 
        Glib::RWLock::WriterLock clickm (click_lock, Glib::TRY_LOCK);
 
-       if (!clickm.locked() || _transport_speed != 1.0 || !_clicking || click_data == 0) {
+       /* how far have we moved since the last time the clicks got cleared
+        */
+
+       click_distance = start - _clicks_cleared;
+
+       if (!clickm.locked() || _transport_speed != 1.0 || !_clicking || click_data == 0 || ((click_distance + nframes) < _worst_track_latency)) {
                _click_io->silence (nframes);
                return;
        }
 
+       start -= _worst_track_latency;
+       /* start could be negative at this point */
        const framepos_t end = start + nframes;
+       /* correct start, potentially */
+       start = max (start, (framepos_t) 0);
 
        BufferSet& bufs = get_scratch_buffers(ChanCount(DataType::AUDIO, 1));
        buf = bufs.get_audio(0).data();
+
        _tempo_map->get_grid (points_begin, points_end, start, end);
 
        if (distance (points_begin, points_end) == 0) {
@@ -80,7 +92,6 @@ Session::click (framepos_t start, framecnt_t nframes)
                        }
                        break;
                }
-
        }
 
   run_clicks:
@@ -91,11 +102,8 @@ Session::click (framepos_t start, framecnt_t nframes)
                framecnt_t copy;
                framecnt_t internal_offset;
                Click *clk;
-               list<Click*>::iterator next;
 
                clk = *i;
-               next = i;
-               ++next;
 
                if (clk->start < start) {
                        internal_offset = 0;
@@ -111,20 +119,20 @@ Session::click (framepos_t start, framecnt_t nframes)
                }
 
                copy = min (clk->duration - clk->offset, nframes - internal_offset);
-
+               
                memcpy (buf + internal_offset, &clk->data[clk->offset], copy * sizeof (Sample));
-
+               
                clk->offset += copy;
 
                if (clk->offset >= clk->duration) {
                        delete clk;
-                       clicks.erase (i);
+                       i = clicks.erase (i);
+               } else {
+                       ++i;
                }
-
-
-               i = next;
        }
 
+       _click_gain->run (bufs, 0, 0, nframes, false);
        _click_io->copy_to_outputs (bufs, DataType::AUDIO, nframes, 0);
 }
 
@@ -224,4 +232,5 @@ Session::clear_clicks ()
        }
 
        clicks.clear ();
+       _clicks_cleared = _transport_frame;
 }