libs/ardour/wscript: unit tests get a target name, inorder to be able to build them...
[ardour.git] / libs / ardour / audiosource.cc
index 7b56d1891ef156a5ba4270904198a96818e1ea28..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, nframes_t start, nframes_t cnt) 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);
@@ -261,13 +278,17 @@ AudioSource::write (Sample *dst, nframes_t cnt)
 }
 
 int
-AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, nframes_t start, nframes_t cnt, double samples_per_visual_peak) const
+AudioSource::read_peaks (PeakData *peaks, nframes_t npeaks, sframes_t start, nframes_t cnt, double samples_per_visual_peak) const
 {
        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, nframes_t start, nframes_t cnt, 
+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
 {
        Glib::Mutex::Lock lm (_lock);
@@ -308,9 +329,9 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, nframes_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
@@ -426,7 +447,7 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, nframes_t s
                        if (i == stored_peaks_read) {
 
                                uint32_t       start_byte = current_stored_peak * sizeof(PeakData);
-                               tnp = min ((_length/samples_per_file_peak - current_stored_peak), (nframes_t) expected_peaks);
+                               tnp = min ((nframes_t)(_length/samples_per_file_peak - current_stored_peak), (nframes_t) expected_peaks);
                                to_read = min (chunksize, tnp);
                                
 #ifdef DEBUG_READ_PEAKS
@@ -519,14 +540,15 @@ AudioSource::read_peaks_with_fpp (PeakData *peaks, nframes_t npeaks, nframes_t s
                while (nvisual_peaks < npeaks) {
 
                        if (i == frames_read) {
-                               
-                               to_read = min (chunksize, (_length - current_frame));
+
+                               to_read = min (chunksize, nframes_t(_length - current_frame));
 
                                if (to_read == 0) {
                                        /* XXX ARGH .. out by one error ... need to figure out why this happens
                                           and fix it rather than do this band-aid move.
                                        */
                                        zero_fill = npeaks - nvisual_peaks;
+                                       npeaks -= zero_fill;
                                        break;
                                }
 
@@ -681,14 +703,15 @@ AudioSource::done_with_peakfile_writes (bool done)
 }
 
 int
-AudioSource::compute_and_write_peaks (Sample* buf, nframes_t first_frame, nframes_t cnt, bool force, bool intermediate_peaks_ready)
+AudioSource::compute_and_write_peaks (Sample* buf, sframes_t first_frame, nframes_t cnt,
+               bool force, bool intermediate_peaks_ready)
 {
        return compute_and_write_peaks (buf, first_frame, cnt, force, intermediate_peaks_ready, _FPP);
 }
 
 int
-AudioSource::compute_and_write_peaks (Sample* buf, nframes_t first_frame, nframes_t cnt, bool force, 
-                                     bool intermediate_peaks_ready, nframes_t fpp)
+AudioSource::compute_and_write_peaks (Sample* buf, sframes_t first_frame, nframes_t cnt,
+               bool force, bool intermediate_peaks_ready, nframes_t fpp)
 {
        Sample* buf2 = 0;
        nframes_t to_do;
@@ -894,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
@@ -906,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;
 }