remove all duplicated _id members from children of PBD::Stateful.
[ardour.git] / libs / ardour / destructive_filesource.cc
index cce757509a48efb592899a02db6351c29bad32ec..1e57d88d709dd4cdcc57ea08ad85a33aab693ea8 100644 (file)
@@ -56,6 +56,7 @@ typedef off_t off64_t;
 
 #include <pbd/error.h>
 #include <ardour/destructive_filesource.h>
+#include <ardour/utils.h>
 
 #include "i18n.h"
 
@@ -67,24 +68,36 @@ gain_t* DestructiveFileSource::out_coefficient = 0;
 gain_t* DestructiveFileSource::in_coefficient = 0;
 jack_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, jack_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 (Session& s, const XMLNode& node)
+       : SndFileSource (s, node)
+{
+       init ();
 }
 
-DestructiveFileSource::DestructiveFileSource (const XMLNode& node)
-       : SndFileSource (node)
+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()
@@ -112,20 +125,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)
 {
-       _capture_start = true;
-       capture_start_frame = pos;
+       if (pos < timeline_position) {
+               _capture_start = false;
+       } else {
+               _capture_start = true;
+               capture_start_frame = pos;
+       }
 }
 
 void
@@ -142,7 +153,7 @@ DestructiveFileSource::clear_capture_marks ()
 }      
 
 jack_nframes_t
-DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in, char * workbuf)
+DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in)
 {
        jack_nframes_t xfade = min (xfade_frames, cnt);
        jack_nframes_t nofade = cnt - xfade;
@@ -179,7 +190,7 @@ 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 */
@@ -197,7 +208,7 @@ DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in,
        }
        
        if (nofade && !fade_in) {
-               if (write_float (data, file_pos, nofade) != nofade) {
+               if (write_float (data, file_pos - timeline_position, nofade) != nofade) {
                        error << string_compose(_("DestructiveFileSource: \"%1\" bad write (%2)"), _path, strerror (errno)) << endmsg;
                        return 0;
                }
@@ -229,24 +240,27 @@ 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]);
+                       xfade_buf[n] = (xfade_buf[n] * out[n]) + (fade_data[n] * in[n]);                
                }
        }
 
        if (xfade) {
-               if (write_float (xfade_buf, fade_position, xfade) != xfade) {
+               if (write_float (xfade_buf, fade_position - timeline_position, xfade) != xfade) {
                        error << string_compose(_("DestructiveFileSource: \"%1\" bad write (%2)"), _path, strerror (errno)) << endmsg;
                        return 0;
                }
        }
        
        if (fade_in && nofade) {
-               if (write_float (data + xfade, file_pos + xfade, nofade) != nofade) {
+               if (write_float (data + xfade, file_pos + xfade - timeline_position, nofade) != nofade) {
                        error << string_compose(_("DestructiveFileSource: \"%1\" bad write (%2)"), _path, strerror (errno)) << endmsg;
                        return 0;
                }
@@ -256,7 +270,7 @@ DestructiveFileSource::crossfade (Sample* data, jack_nframes_t cnt, int fade_in,
 }
 
 jack_nframes_t
-DestructiveFileSource::write_unlocked (Sample* data, jack_nframes_t cnt, char * workbuf)
+DestructiveFileSource::write_unlocked (Sample* data, jack_nframes_t cnt)
 {
        jack_nframes_t old_file_pos;
 
@@ -265,6 +279,11 @@ DestructiveFileSource::write_unlocked (Sample* data, jack_nframes_t cnt, char *
        }
 
        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;
                
@@ -276,7 +295,7 @@ DestructiveFileSource::write_unlocked (Sample* data, jack_nframes_t cnt, char *
                jack_nframes_t ofilepos = file_pos;
                
                // fade in
-               if (crossfade (data, subcnt, 1, workbuf) != subcnt) {
+               if (crossfade (data, subcnt, 1) != subcnt) {
                        return 0;
                }
                
@@ -285,13 +304,17 @@ 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;
@@ -299,22 +322,28 @@ DestructiveFileSource::write_unlocked (Sample* data, jack_nframes_t cnt, char *
                /* move to the correct location place */
                file_pos = capture_start_frame;
                
-               if (crossfade (data, cnt, 1, workbuf) != cnt) {
+               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 {
 
-               if (write_float (data, file_pos, cnt) != cnt) {
+               /* in the middle of recording */
+               
+               if (write_float (data, file_pos - timeline_position, cnt) != cnt) {
                        return 0;
                }
        }
@@ -345,7 +374,7 @@ DestructiveFileSource::write_unlocked (Sample* data, jack_nframes_t cnt, char *
        }
 
        if (_build_peakfiles) {
-               queue_for_peaks (*this);
+               queue_for_peaks (this);
        }
        
        return cnt;
@@ -365,9 +394,20 @@ DestructiveFileSource::get_state ()
        return node;
 }
 
+void
+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 (jack_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
 }