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 "ardour/strip_silence.h"
21 #include "ardour/audioregion.h"
22 #include "ardour/region_factory.h"
23 #include "ardour/session.h"
24 #include "ardour/dB.h"
26 using namespace ARDOUR;
28 /** Construct a StripSilence filter.
30 * @param threshold Threshold below which audio is considered silence, in dBFS.
31 * @param minimum_length Minimum length of silence period to recognise, in samples.
32 * @param fade_length Length of fade in/out to apply to trimmed regions, in samples.
35 StripSilence::StripSilence (Session & s, double threshold, nframes_t minimum_length, nframes_t fade_length)
36 : Filter (s), _threshold (threshold), _minimum_length (minimum_length), _fade_length (fade_length)
42 StripSilence::run (boost::shared_ptr<Region> r)
46 /* we only operate on AudioRegions, for now, though this could be adapted to MIDI
48 boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
52 results.push_back (r);
56 /* we don't care about this but we need to fill out the fields
57 anyway. XXX should really be a default constructor for ITT
65 /* find periods of silence in the region */
66 std::list<std::pair<frameoffset_t, framecnt_t> > const silence =
67 region->find_silence (dB_to_coefficient (_threshold), _minimum_length, itt);
69 if (silence.size () == 1 && silence.front().first == 0 && silence.front().second == region->length() - 1) {
70 /* the region is all silence, so just return with nothing */
74 if (silence.empty()) {
75 /* no silence in this region */
76 results.push_back (region);
80 std::list<std::pair<framepos_t, framecnt_t > >::const_iterator s = silence.begin ();
81 framepos_t const pos = region->position ();
82 framepos_t const end = region->start () + region->length() - 1;
83 framepos_t const start = region->start ();
85 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
86 region->set_name (session.new_region_name (region->name ()));
87 boost::shared_ptr<AudioRegion> last_region = region;
88 results.push_back (region);
91 /* the region starts with some silence */
93 /* we must set length to an intermediate value here, otherwise the call
94 ** to set_start will fail */
95 region->set_length (region->length() - s->second + _fade_length, 0);
96 region->set_start (start + s->second - _fade_length, 0);
97 region->set_position (pos + s->second - _fade_length, 0);
98 region->set_fade_in_active (true);
99 region->set_fade_in (AudioRegion::Linear, _fade_length);
103 while (s != silence.end()) {
105 /* trim the end of this region */
106 region->trim_end (pos + s->first + _fade_length, 0);
107 region->set_fade_out_active (true);
108 region->set_fade_out (AudioRegion::Linear, _fade_length);
110 /* make a new region and trim its start */
111 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
112 region->set_name (session.new_region_name (region->name ()));
113 last_region = region;
115 results.push_back (region);
117 /* set length here for the same reasons as above */
118 region->set_length (region->length() - s->second + _fade_length, 0);
119 region->set_start (start + s->second - _fade_length, 0);
120 region->set_position (pos + s->second - _fade_length, 0);
121 region->set_fade_in_active (true);
122 region->set_fade_in (AudioRegion::Linear, _fade_length);
127 if (silence.back().second == end) {
128 /* the last region we created is zero-sized, so just remove it */
131 /* finish off the last region */
132 last_region->trim_end (end, 0);