Eliminate circular dependency kludge for control_protocol/smpte.o.
[ardour.git] / libs / ardour / strip_silence.cc
index 0f58ccf9a3efc81c8eb9b272c53c7dd23bf53179..cd7ab879e7e6bd4fee9af90d242f28f992b141ad 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2009 Paul Davis
+    Copyright (C) 2009-2010 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
 
 */
 
+#include "pbd/property_list.h"
+
 #include "ardour/strip_silence.h"
 #include "ardour/audioregion.h"
 #include "ardour/region_factory.h"
 #include "ardour/session.h"
 #include "ardour/dB.h"
+#include "ardour/progress.h"
 
 using namespace ARDOUR;
 
@@ -32,28 +35,37 @@ using namespace ARDOUR;
  *  @param fade_length Length of fade in/out to apply to trimmed regions, in samples.
  */
 
-StripSilence::StripSilence (Session & s, double threshold, nframes_t minimum_length, nframes_t fade_length)
-       : Filter (s), _threshold (threshold), _minimum_length (minimum_length), _fade_length (fade_length)
+StripSilence::StripSilence (Session & s, const AudioIntervalMap& sm, framecnt_t fade_length)
+       : Filter (s)
+        , _smap (sm)
+        , _fade_length (fade_length)
 {
 
 }
 
 int
-StripSilence::run (boost::shared_ptr<Region> r)
+StripSilence::run (boost::shared_ptr<Region> r, Progress* progress)
 {
        results.clear ();
 
        /* we only operate on AudioRegions, for now, though this could be adapted to MIDI
-          as well I guess */
+          as well I guess 
+        */
        boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
+        InterThreadInfo itt;
+        AudioIntervalMap::const_iterator sm;
+
        if (!region) {
                results.push_back (r);
                return -1;
        }
 
-       /* find periods of silence in the region */
-       std::list<std::pair<frameoffset_t, framecnt_t> > const silence =
-               region->find_silence (dB_to_coefficient (_threshold), _minimum_length);
+        if ((sm = _smap.find (r)) == _smap.end()) {
+               results.push_back (r);
+               return -1;
+        }
+
+        const AudioIntervalResult& silence = sm->second;
 
        if (silence.size () == 1 && silence.front().first == 0 && silence.front().second == region->length() - 1) {
                /* the region is all silence, so just return with nothing */
@@ -66,60 +78,67 @@ StripSilence::run (boost::shared_ptr<Region> r)
                return 0;
        }
 
-       std::list<std::pair<framepos_t, framecnt_t > >::const_iterator s = silence.begin ();
-       framepos_t const pos = region->position ();
-       framepos_t const end = region->start () + region->length() - 1;
-       framepos_t const start = region->start ();
-
-       region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
-       region->set_name (session.new_region_name (region->name ()));
-       boost::shared_ptr<AudioRegion> last_region = region;
-       results.push_back (region);
-
-       if (s->first == 0) {
-               /* the region starts with some silence */
-
-               /* we must set length to an intermediate value here, otherwise the call
-               ** to set_start will fail */
-               region->set_length (region->length() - s->second + _fade_length, 0);
-               region->set_start (start + s->second - _fade_length, 0);
-               region->set_position (pos + s->second - _fade_length, 0);
-               region->set_fade_in_active (true);
-               region->set_fade_in (AudioRegion::Linear, _fade_length);
-               s++;
-       }
+        AudioIntervalResult::const_iterator s = silence.begin ();
+        PBD::PropertyList plist;
+        framepos_t start;
+        framepos_t end;
+        bool in_silence;
+        boost::shared_ptr<AudioRegion> copy;
 
-       while (s != silence.end()) {
+        start = r->start();
 
-               /* trim the end of this region */
-               region->trim_end (pos + s->first + _fade_length, 0);
-               region->set_fade_out_active (true);
-               region->set_fade_out (AudioRegion::Linear, _fade_length);
+        if (s->first == start) {
+                /* segment starting at zero is silent */
+                end = s->second;
+                in_silence = true;
+        } else {
+                /* segment starting at zero is audible, and begins at the start of the region in the source */
+                end = s->first;
+                in_silence = false;
+        }
 
-               /* make a new region and trim its start */
-               region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
-               region->set_name (session.new_region_name (region->name ()));
-               last_region = region;
-               assert (region);
-               results.push_back (region);
+       int n = 0;
+       int const N = silence.size ();
 
-               /* set length here for the same reasons as above */
-               region->set_length (region->length() - s->second + _fade_length, 0);
-               region->set_start (start + s->second - _fade_length, 0);
-               region->set_position (pos + s->second - _fade_length, 0);
-               region->set_fade_in_active (true);
-               region->set_fade_in (AudioRegion::Linear, _fade_length);
+        while (start < r->start() + r->length()) {
 
-               s++;
-       }
+                framecnt_t interval_duration;
 
-       if (silence.back().second == end) {
-               /* the last region we created is zero-sized, so just remove it */
-               results.pop_back ();
-       } else {
-               /* finish off the last region */
-               last_region->trim_end (end, 0);
-       }
+                interval_duration = end - start;
+
+                if (!in_silence && interval_duration > 0) {
+
+                        plist.clear ();
+                        plist.add (Properties::length, interval_duration);
+                        plist.add (Properties::position, r->position() + (start - r->start()));
+
+                        copy = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create 
+                                                                         (region, (start - r->start()), plist));
+
+                        copy->set_name (RegionFactory::new_region_name (region->name ()));
+
+                        copy->set_fade_in_active (true);
+                        copy->set_fade_in (FadeLinear, _fade_length);
+                        results.push_back (copy);
+                }
+
+                start = end;
+                in_silence = !in_silence;
+                ++s;
+
+                if (s == silence.end()) {
+                        end = r->start() + r->length();
+                } else {
+                        end = s->first;
+                }
+
+               ++n;
+
+               if (progress && (n <= N)) {
+                       progress->set_progress (float (n) / N);
+               }
+
+        }
 
        return 0;
 }