2 Copyright (C) 2009 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "pbd/property_list.h"
22 #include "ardour/strip_silence.h"
23 #include "ardour/audioregion.h"
24 #include "ardour/region_factory.h"
25 #include "ardour/session.h"
26 #include "ardour/dB.h"
28 using namespace ARDOUR;
30 /** Construct a StripSilence filter.
32 * @param threshold Threshold below which audio is considered silence, in dBFS.
33 * @param minimum_length Minimum length of silence period to recognise, in samples.
34 * @param fade_length Length of fade in/out to apply to trimmed regions, in samples.
37 StripSilence::StripSilence (Session & s, double threshold, nframes_t minimum_length, nframes_t fade_length)
38 : Filter (s), _threshold (threshold), _minimum_length (minimum_length), _fade_length (fade_length)
44 StripSilence::run (boost::shared_ptr<Region> r)
48 /* we only operate on AudioRegions, for now, though this could be adapted to MIDI
51 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
55 results.push_back (r);
59 /* we don't care about this but we need to fill out the fields
60 anyway. XXX should really be a default constructor for ITT
68 /* find periods of silence in the region */
69 std::list<std::pair<frameoffset_t, framecnt_t> > const silence =
70 region->find_silence (dB_to_coefficient (_threshold), _minimum_length, itt);
72 if (silence.size () == 1 && silence.front().first == 0 && silence.front().second == region->length() - 1) {
73 /* the region is all silence, so just return with nothing */
77 if (silence.empty()) {
78 /* no silence in this region */
79 results.push_back (region);
83 std::list<std::pair<framepos_t, framecnt_t > >::const_iterator s = silence.begin ();
84 PBD::PropertyList plist;
88 boost::shared_ptr<AudioRegion> copy;
91 /* initial segment, starting at zero, is silent */
95 /* initial segment, starting at zero, is audible */
100 while (s != silence.end()) {
102 framecnt_t interval_duration;
104 interval_duration = end - start;
107 if (!in_silence && interval_duration > 0) {
110 plist.add (Properties::length, interval_duration);
111 plist.add (Properties::position, region->position() + start);
113 copy = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create
114 (region, start, plist));
116 copy->set_name (RegionFactory::new_region_name (region->name ()));
118 std::cerr << "New silent delineated region called " << copy->name()
119 << " @ " << copy->start() << " length = " << copy->length() << " pos = " <<
120 copy->position() << std::endl;
122 copy->set_fade_in_active (true);
123 copy->set_fade_in (AudioRegion::Linear, _fade_length);
124 results.push_back (copy);
130 in_silence = !in_silence;