more solo model work, including a GUI fix for mute button state when the route is...
[ardour.git] / libs / ardour / strip_silence.cc
index dbb36735dd0ab7c19f8a1c3e5118f38bb55c8fb7..21faeded6840e16e3b79efc7f2eaaadd8934f00f 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2009 Paul Davis 
+    Copyright (C) 2009 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
@@ -17,6 +17,8 @@
 
 */
 
+#include "pbd/property_list.h"
+
 #include "ardour/strip_silence.h"
 #include "ardour/audioregion.h"
 #include "ardour/region_factory.h"
@@ -31,7 +33,7 @@ using namespace ARDOUR;
  *  @param minimum_length Minimum length of silence period to recognise, in samples.
  *  @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)
 {
@@ -42,18 +44,21 @@ int
 StripSilence::run (boost::shared_ptr<Region> r)
 {
        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;
+        
        if (!region) {
                results.push_back (r);
                return -1;
        }
 
        /* find periods of silence in the region */
-       std::list<std::pair<nframes_t, nframes_t> > const silence =
-               region->find_silence (dB_to_coefficient (_threshold), _minimum_length);
+       std::list<std::pair<frameoffset_t, framecnt_t> > const silence =
+               region->find_silence (dB_to_coefficient (_threshold), _minimum_length, itt);
 
        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 +71,55 @@ StripSilence::run (boost::shared_ptr<Region> r)
                return 0;
        }
 
-       std::list<std::pair<nframes_t, nframes_t > >::const_iterator s = silence.begin ();
-       nframes_t const pos = region->position ();
-       nframes_t const end = region->start () + region->length() - 1;
-       nframes_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++;
-       }
+       std::list<std::pair<framepos_t, framecnt_t > >::const_iterator s = silence.begin ();
+        PBD::PropertyList plist;
+        framepos_t start = 0;
+        framepos_t end;
+        bool in_silence;
+        boost::shared_ptr<AudioRegion> copy;
 
-       while (s != silence.end()) {
+        if (s->first == 0) {
+                /* initial segment, starting at zero, is silent */
+                end = s->second;
+                in_silence = true;
+        } else {
+                /* initial segment, starting at zero, is audible */
+                end = s->first;
+                in_silence = false;
+        }
 
-               /* 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);
+        while (s != silence.end()) {
 
-               /* 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);
+                framecnt_t interval_duration;
 
-               /* 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);
+                interval_duration = end - start;
 
-               s++;
-       }
 
-       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);
-       }
+                if (!in_silence && interval_duration > 0) {
+
+                        plist.clear ();
+                        plist.add (Properties::length, interval_duration);
+                        plist.add (Properties::position, region->position() + start);
+
+                        copy = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create 
+                                                                         (region, start, plist));
+
+                        copy->set_name (RegionFactory::new_region_name (region->name ()));
+
+                        std::cerr << "New silent delineated region called " << copy->name()
+                                  << " @ " << copy->start() << " length = " << copy->length() << " pos = " << 
+                                copy->position() << std::endl;
+
+                        copy->set_fade_in_active (true);
+                        copy->set_fade_in (AudioRegion::Linear, _fade_length);
+                        results.push_back (copy);
+                }
+
+                start = end;
+                ++s;
+                end = s->first;
+                in_silence = !in_silence;
+        }
 
        return 0;
 }