major fixes to automation editing
[ardour.git] / libs / ardour / destructive_filesource.cc
index cce757509a48efb592899a02db6351c29bad32ec..2f2e85ed5a088652ad7cd59f3c09de66af79d502 100644 (file)
@@ -55,7 +55,10 @@ typedef off_t off64_t;
 #include <fcntl.h>
 
 #include <pbd/error.h>
+#include <pbd/stacktrace.h>
 #include <ardour/destructive_filesource.h>
+#include <ardour/utils.h>
+#include <ardour/session.h>
 
 #include "i18n.h"
 
@@ -65,26 +68,38 @@ using namespace PBD;
 
 gain_t* DestructiveFileSource::out_coefficient = 0;
 gain_t* DestructiveFileSource::in_coefficient = 0;
-jack_nframes_t DestructiveFileSource::xfade_frames = 64;
+nframes_t DestructiveFileSource::xfade_frames = 64;
 
-DestructiveFileSource::DestructiveFileSource (string path, SampleFormat samp_format, HeaderFormat hdr_format, jack_nframes_t rate, Flag flags)
-       : SndFileSource (path, samp_format, hdr_format, rate, flags)
+DestructiveFileSource::DestructiveFileSource (Session& s, string path, SampleFormat samp_format, HeaderFormat hdr_format, nframes_t rate, Flag flags)
+       : SndFileSource (s, path, samp_format, hdr_format, rate, flags)
 {
-       xfade_buf = new Sample[xfade_frames];
+       init ();
+}
 
-       _capture_start = false;
-       _capture_end = false;
-       file_pos = 0;
+
+DestructiveFileSource::DestructiveFileSource (Session& s, string path, Flag flags)
+       : SndFileSource (s, path, flags)
+{
+       init ();
 }
 
-DestructiveFileSource::DestructiveFileSource (const XMLNode& node)
-       : SndFileSource (node)
+DestructiveFileSource::DestructiveFileSource (Session& s, const XMLNode& node)
+       : SndFileSource (s, node)
+{
+       init ();
+}
+
+void
+DestructiveFileSource::init ()
 {
        xfade_buf = new Sample[xfade_frames];
 
        _capture_start = false;
        _capture_end = false;
        file_pos = 0;
+
+       timeline_position = header_position_offset;
+       AudioFileSource::HeaderPositionOffsetChanged.connect (mem_fun (*this, &DestructiveFileSource::handle_header_position_change));
 }
 
 DestructiveFileSource::~DestructiveFileSource()
@@ -93,13 +108,13 @@ DestructiveFileSource::~DestructiveFileSource()
 }
 
 void
-DestructiveFileSource::setup_standard_crossfades (jack_nframes_t rate)
+DestructiveFileSource::setup_standard_crossfades (nframes_t rate)
 {
        /* This static method is assumed to have been called by the Session
           before any DFS's are created.
        */
 
-       xfade_frames = (jack_nframes_t) floor ((Config->get_destructive_xfade_msecs () / 1000.0) * rate);
+       xfade_frames = (nframes_t) floor ((Config->get_destructive_xfade_msecs () / 1000.0) * rate);
 
        if (out_coefficient) {
                delete [] out_coefficient;
@@ -112,20 +127,18 @@ DestructiveFileSource::setup_standard_crossfades (jack_nframes_t rate)
        out_coefficient = new gain_t[xfade_frames];
        in_coefficient = new gain_t[xfade_frames];
 
-       for (jack_nframes_t n = 0; n < xfade_frames; ++n) {
-
-               /* XXXX THIS IS NOT THE RIGHT XFADE CURVE: USE A PROPER VOLUMETRIC EQUAL POWER CURVE */
-
-               in_coefficient[n] = n/(gain_t) (xfade_frames-1); /* 0 .. 1 */
-               out_coefficient[n] = 1.0 - in_coefficient[n];    /* 1 .. 0 */
-       }
+       compute_equal_power_fades (xfade_frames, in_coefficient, out_coefficient);
 }
 
 void
-DestructiveFileSource::mark_capture_start (jack_nframes_t pos)
+DestructiveFileSource::mark_capture_start (nframes_t pos)
 {
-       _capture_start = true;
-       capture_start_frame = pos;
+       if (pos < timeline_position) {
+               _capture_start = false;
+       } else {
+               _capture_start = true;
+               capture_start_frame = pos;
+       }
 }
 
 void
@@ -141,15 +154,15 @@ DestructiveFileSource::clear_capture_marks ()
        _capture_end = false;
 }      
 
-jack_nframes_t
-DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in, char * workbuf)
+nframes_t
+DestructiveFileSource::crossfade (Sample* data, nframes_t cnt, int fade_in)
 {
-       jack_nframes_t xfade = min (xfade_frames, cnt);
-       jack_nframes_t nofade = cnt - xfade;
+       nframes_t xfade = min (xfade_frames, cnt);
+       nframes_t nofade = cnt - xfade;
        Sample* fade_data = 0;
-       jack_nframes_t fade_position = 0; // in frames
+       nframes_t fade_position = 0; // in frames
        ssize_t retval;
-       jack_nframes_t file_cnt;
+       nframes_t file_cnt;
 
        if (fade_in) {
                fade_position = file_pos;
@@ -179,7 +192,8 @@ DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in,
        }
 
        if (file_cnt) {
-               if ((retval = write_float (xfade_buf, fade_position, file_cnt)) != (ssize_t) file_cnt) {
+               
+               if ((retval = read_unlocked (xfade_buf, fade_position, file_cnt)) != (ssize_t) file_cnt) {
                        if (retval >= 0 && errno == EAGAIN) {
                                /* XXX - can we really trust that errno is meaningful here?  yes POSIX, i'm talking to you.
                                 * short or no data there */
@@ -192,7 +206,7 @@ DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in,
        } 
 
        if (file_cnt != xfade) {
-               jack_nframes_t delta = xfade - file_cnt;
+               nframes_t delta = xfade - file_cnt;
                memset (xfade_buf+file_cnt, 0, sizeof (Sample) * delta);
        }
        
@@ -205,7 +219,7 @@ DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in,
 
        if (xfade == xfade_frames) {
 
-               jack_nframes_t n;
+               nframes_t n;
 
                /* use the standard xfade curve */
                
@@ -229,12 +243,15 @@ DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in,
 
        } else if (xfade) {
 
+               gain_t in[xfade];
+               gain_t out[xfade];
+
                /* short xfade, compute custom curve */
 
-               /* XXX COMPUTE THE CURVE, DAMMIT! */
+               compute_equal_power_fades (xfade, in, out);
 
-               for (jack_nframes_t n = 0; n < xfade; ++n) {
-                       xfade_buf[n] = (xfade_buf[n] * out_coefficient[n]) + (fade_data[n] * in_coefficient[n]);
+               for (nframes_t n = 0; n < xfade; ++n) {
+                       xfade_buf[n] = (xfade_buf[n] * out[n]) + (fade_data[n] * in[n]);                
                }
        }
 
@@ -255,28 +272,33 @@ DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in,
        return cnt;
 }
 
-jack_nframes_t
-DestructiveFileSource::write_unlocked (Sample* data, jack_nframes_t cnt, char * workbuf)
+nframes_t
+DestructiveFileSource::write_unlocked (Sample* data, nframes_t cnt)
 {
-       jack_nframes_t old_file_pos;
+       nframes_t old_file_pos;
 
        if (!writable()) {
                return 0;
        }
 
        if (_capture_start && _capture_end) {
+
+               /* start and end of capture both occur within the data we are writing,
+                  so do both crossfades.
+               */
+
                _capture_start = false;
                _capture_end = false;
                
                /* move to the correct location place */
-               file_pos = capture_start_frame;
+               file_pos = capture_start_frame - timeline_position;
                
                // split cnt in half
-               jack_nframes_t subcnt = cnt / 2;
-               jack_nframes_t ofilepos = file_pos;
+               nframes_t subcnt = cnt / 2;
+               nframes_t ofilepos = file_pos;
                
                // fade in
-               if (crossfade (data, subcnt, 1, workbuf) != subcnt) {
+               if (crossfade (data, subcnt, 1) != subcnt) {
                        return 0;
                }
                
@@ -285,44 +307,54 @@ DestructiveFileSource::write_unlocked (Sample* data, jack_nframes_t cnt, char *
                
                // fade out
                subcnt = cnt - subcnt;
-               if (crossfade (tmpdata, subcnt, 0, workbuf) != subcnt) {
+               if (crossfade (tmpdata, subcnt, 0) != subcnt) {
                        return 0;
                }
                
                file_pos = ofilepos; // adjusted below
-       }
-       else if (_capture_start) {
+
+       } else if (_capture_start) {
+
+               /* start of capture both occur within the data we are writing,
+                  so do the fade in
+               */
 
                _capture_start = false;
                _capture_end = false;
                
                /* move to the correct location place */
-               file_pos = capture_start_frame;
-               
-               if (crossfade (data, cnt, 1, workbuf) != cnt) {
+               file_pos = capture_start_frame - timeline_position;
+
+               if (crossfade (data, cnt, 1) != cnt) {
                        return 0;
                }
                
        } else if (_capture_end) {
 
+               /* end of capture both occur within the data we are writing,
+                  so do the fade out
+               */
+
                _capture_start = false;
                _capture_end = false;
                
-               if (crossfade (data, cnt, 0, workbuf) != cnt) {
+               if (crossfade (data, cnt, 0) != cnt) {
                        return 0;
                }
 
        } else {
 
+               /* in the middle of recording */
+
                if (write_float (data, file_pos, cnt) != cnt) {
                        return 0;
                }
        }
-       
+
        old_file_pos = file_pos;
        update_length (file_pos, cnt);
        file_pos += cnt;
-       
+
        if (_build_peakfiles) {
                PeakBuildRecord *pbr = 0;
                
@@ -345,13 +377,13 @@ DestructiveFileSource::write_unlocked (Sample* data, jack_nframes_t cnt, char *
        }
 
        if (_build_peakfiles) {
-               queue_for_peaks (*this);
+               queue_for_peaks (shared_from_this ());
        }
        
        return cnt;
 }
 
-jack_nframes_t
+nframes_t
 DestructiveFileSource::last_capture_start_frame () const
 {
        return capture_start_frame;
@@ -366,8 +398,27 @@ DestructiveFileSource::get_state ()
 }
 
 void
-DestructiveFileSource::set_timeline_position (jack_nframes_t pos)
+DestructiveFileSource::handle_header_position_change ()
+{
+       if ( _length != 0 ) {
+               error << string_compose(_("Filesource: start time is already set for existing file (%1): Cannot change start time."), _path ) << endmsg;
+               //in the future, pop up a dialog here that allows user to regenerate file with new start offset
+       } else if (writable()) {
+               timeline_position = header_position_offset;
+               set_header_timeline_position ();  //this will get flushed if/when the file is recorded to
+       }
+}
+
+void
+DestructiveFileSource::set_timeline_position (nframes_t pos)
 {
-       /* destructive tracks always start at where our reference frame zero is */
-       timeline_position = 0;
+       //destructive track timeline postion does not change except at instantion or when header_position_offset (session start) changes
 }
+
+int
+DestructiveFileSource::read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_unit) const
+{
+       // cerr << _name << " read peaks at " << start << " for " << cnt << " tpos = " << timeline_position << endl;
+       return AudioFileSource::read_peaks (peaks, npeaks, start, cnt, samples_per_unit);
+}
+