X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fstrip_silence.cc;h=8aa703740bdc707705a807134e2c4b9c1f2c9b61;hb=30b087ab3d28f1585987fa3f6ae006562ae192e3;hp=cf03c2df8ba95b8e54bd473d851209f7f4419845;hpb=73192bc1a7ea55fa1864dc3826845b15c00dd2ec;p=ardour.git diff --git a/libs/ardour/strip_silence.cc b/libs/ardour/strip_silence.cc index cf03c2df8b..8aa703740b 100644 --- a/libs/ardour/strip_silence.cc +++ b/libs/ardour/strip_silence.cc @@ -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 @@ -22,21 +22,20 @@ #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; /** Construct a StripSilence filter. * @param s Session. - * @param threshold Threshold below which audio is considered silence, in dBFS. - * @param minimum_length Minimum length of silence period to recognise, in samples. + * @param sm Silences to remove. * @param fade_length Length of fade in/out to apply to trimmed regions, in samples. */ -StripSilence::StripSilence (Session & s, double threshold, framecnt_t minimum_length, framecnt_t fade_length) - : Filter (s), _threshold (threshold), _minimum_length (minimum_length), _fade_length (fade_length) +StripSilence::StripSilence (Session & s, const AudioIntervalMap& sm, samplecnt_t fade_length) + : Filter (s) + , _smap (sm) + , _fade_length (fade_length) { } @@ -47,19 +46,23 @@ StripSilence::run (boost::shared_ptr 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 region = boost::dynamic_pointer_cast (r); InterThreadInfo itt; - + AudioIntervalMap::const_iterator sm; + if (!region) { results.push_back (r); return -1; } - /* find periods of silence in the region */ - std::list > const silence = - region->find_silence (dB_to_coefficient (_threshold), _minimum_length, itt); + 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 */ @@ -72,62 +75,68 @@ StripSilence::run (boost::shared_ptr r, Progress* progress) return 0; } - std::list >::const_iterator s = silence.begin (); - PBD::PropertyList plist; - framepos_t start = 0; - framepos_t end; - bool in_silence; - boost::shared_ptr copy; - - 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; - } + /* Turn the silence list into an `audible' list */ + AudioIntervalResult audible; - int n = 0; - int const N = silence.size (); + /* Add the possible audible section at the start of the region */ + AudioIntervalResult::const_iterator first_silence = silence.begin (); + if (first_silence->first != region->start()) { + audible.push_back (std::make_pair (r->start(), first_silence->first)); + } - while (s != silence.end()) { + /* Add audible sections in the middle of the region */ + for (AudioIntervalResult::const_iterator i = silence.begin (); i != silence.end(); ++i) { + AudioIntervalResult::const_iterator j = i; + ++j; - framecnt_t interval_duration; + if (j != silence.end ()) { + audible.push_back (std::make_pair (i->second, j->first)); + } + } - interval_duration = end - start; + /* Add the possible audible section at the end of the region */ + AudioIntervalResult::const_iterator last_silence = silence.end (); + --last_silence; + sampleoffset_t const end_of_region = r->start() + r->length(); - if (!in_silence && interval_duration > 0) { + if (last_silence->second < end_of_region - 1) { + audible.push_back (std::make_pair (last_silence->second, end_of_region - 1)); + } - plist.clear (); - plist.add (Properties::length, interval_duration); - plist.add (Properties::position, region->position() + start); + int n = 0; + int const N = audible.size (); - copy = boost::dynamic_pointer_cast (RegionFactory::create - (region, start, plist)); + for (AudioIntervalResult::const_iterator i = audible.begin(); i != audible.end(); ++i, ++n) { - copy->set_name (RegionFactory::new_region_name (region->name ())); + PBD::PropertyList plist; + boost::shared_ptr copy; - std::cerr << "New silent delineated region called " << copy->name() - << " @ " << copy->start() << " length = " << copy->length() << " pos = " << - copy->position() << std::endl; + plist.add (Properties::length, i->second - i->first); + plist.add (Properties::position, r->position() + (i->first - r->start())); - copy->set_fade_in_active (true); - copy->set_fade_in (FadeLinear, _fade_length); - results.push_back (copy); - } + copy = boost::dynamic_pointer_cast ( + RegionFactory::create (region, MusicSample (i->first - r->start(), 0), plist) + ); - start = end; - ++s; - end = s->first; - in_silence = !in_silence; + copy->set_name (RegionFactory::new_region_name (region->name ())); + + samplecnt_t const f = std::min (_fade_length, (i->second - i->first) / 2); + + if (f > 0) { + copy->set_fade_in_active (true); + copy->set_fade_out_active (true); + copy->set_fade_in (FadeLinear, f); + copy->set_fade_out (FadeLinear, f); + } else { + copy->set_fade_in_active (false); + copy->set_fade_out_active (false); + } + results.push_back (copy); - if (progress) { + if (progress && (n <= N)) { progress->set_progress (float (n) / N); } - ++n; } return 0;