libs/ardour/wscript: unit tests get a target name, inorder to be able to build them...
[ardour.git] / libs / ardour / audiosource.cc
index 15f35bf1f1cd76a8429161d7766940e2cd6e933f..f44628956a824293494462802fbdaaa401245945 100644 (file)
 #include <glibmm/fileutils.h>
 #include <glibmm/miscutils.h>
 
-#include <pbd/xml++.h>
-#include <pbd/pthread_utils.h>
+#include "pbd/xml++.h"
+#include "pbd/pthread_utils.h"
 
-#include <ardour/audiosource.h>
-#include <ardour/cycle_timer.h>
-#include <ardour/session.h>
-#include <ardour/transient_detector.h>
-#include <ardour/runtime_functions.h>
+#include "ardour/audiosource.h"
+#include "ardour/cycle_timer.h"
+#include "ardour/session.h"
+#include "ardour/transient_detector.h"
+#include "ardour/runtime_functions.h"
 
 #include "i18n.h"
 
@@ -57,6 +57,7 @@ bool AudioSource::_build_peakfiles = false;
 
 AudioSource::AudioSource (Session& s, ustring name)
        : Source (s, DataType::AUDIO, name)
+       , _length (0)
 {
        _peaks_built = false;
        _peak_byte_max = 0;
@@ -70,6 +71,7 @@ AudioSource::AudioSource (Session& s, ustring name)
 
 AudioSource::AudioSource (Session& s, const XMLNode& node) 
        : Source (s, node)
+       , _length (0)
 {
        
        _peaks_built = false;
@@ -125,6 +127,21 @@ AudioSource::set_state (const XMLNode& node)
        return 0;
 }
 
+sframes_t
+AudioSource::length (sframes_t /*pos*/) const
+{
+       return _length;
+}
+
+void
+AudioSource::update_length (sframes_t pos, sframes_t cnt)
+{
+       if (pos + cnt > _length) {
+               _length = pos + cnt;
+       }
+}
+
+
 /***********************************************************************
   PEAK FILE STUFF
  ***********************************************************************/
@@ -211,7 +228,7 @@ AudioSource::initialize_peakfile (bool newfile, ustring audio_path)
                
                /* we found it in the peaks dir, so check it out */
                
-               if (statbuf.st_size == 0 || ((nframes_t) statbuf.st_size < ((length() / _FPP) * sizeof (PeakData)))) {
+               if (statbuf.st_size == 0 || ((nframes_t) statbuf.st_size < ((length(_timeline_position) / _FPP) * sizeof (PeakData)))) {
                        // empty
                        _peaks_built = false;
                } else {
@@ -247,7 +264,7 @@ AudioSource::initialize_peakfile (bool newfile, ustring audio_path)
 }
 
 nframes_t
-AudioSource::read (Sample *dst, sframes_t start, nframes_t cnt, int channel) const
+AudioSource::read (Sample *dst, sframes_t start, nframes_t cnt, int /*channel*/) const
 {
        Glib::Mutex::Lock lm (_lock);
        return read_unlocked (dst, start, cnt);
@@ -266,6 +283,10 @@ AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, sframes_t start, nfr
        return read_peaks_with_fpp (peaks, npeaks, start, cnt, samples_per_visual_peak, _FPP);
 }
 
+/** @param peaks Buffer to write peak data.
+ *  @param npeaks Number of peaks to write.
+ */
+
 int 
 AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, sframes_t start, nframes_t cnt, 
                                  double samples_per_visual_peak, nframes_t samples_per_file_peak) const
@@ -308,9 +329,9 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, sframes_t s
                npeaks = min ((nframes_t) floor (cnt / samples_per_visual_peak), npeaks);
                zero_fill = old - npeaks;
        }
-
-       // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
        
+       // cerr << "actual npeaks = " << npeaks << " zf = " << zero_fill << endl;
+
        if (npeaks == cnt) {
 
 #ifdef DEBUG_READ_PEAKS
@@ -519,7 +540,7 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, sframes_t s
                while (nvisual_peaks < npeaks) {
 
                        if (i == frames_read) {
-                               
+
                                to_read = min (chunksize, nframes_t(_length - current_frame));
 
                                if (to_read == 0) {
@@ -527,6 +548,7 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, sframes_t s
                                           and fix it rather than do this band-aid move.
                                        */
                                        zero_fill = npeaks - nvisual_peaks;
+                                       npeaks -= zero_fill;
                                        break;
                                }
 
@@ -895,10 +917,8 @@ AudioSource::file_changed (ustring path)
 nframes_t
 AudioSource::available_peaks (double zoom_factor) const
 {
-       off_t end;
-
        if (zoom_factor < _FPP) {
-               return length(); // peak data will come from the audio file
+               return length(_timeline_position); // peak data will come from the audio file
        } 
        
        /* peak data comes from peakfile, but the filesize might not represent
@@ -907,7 +927,7 @@ AudioSource::available_peaks (double zoom_factor) const
           but _peak_byte_max only monotonically increases after initialization.
        */
 
-       end = _peak_byte_max;
+       off_t end = _peak_byte_max;
 
        return (end/sizeof(PeakData)) * _FPP;
 }