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);
50 results.push_back (r);
54 /* find periods of silence in the region */
55 std::list<std::pair<nframes_t, nframes_t> > const silence =
56 region->find_silence (dB_to_coefficient (_threshold), _minimum_length);
58 if (silence.size () == 1 && silence.front().first == 0 && silence.front().second == region->length() - 1) {
59 /* the region is all silence, so just return with nothing */
63 if (silence.empty()) {
64 /* no silence in this region */
65 results.push_back (region);
69 std::list<std::pair<nframes_t, nframes_t > >::const_iterator s = silence.begin ();
70 nframes_t const pos = region->position ();
71 nframes_t const end = region->start () + region->length() - 1;
72 nframes_t const start = region->start ();
74 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
75 region->set_name (session.new_region_name (region->name ()));
76 boost::shared_ptr<AudioRegion> last_region = region;
77 results.push_back (region);
80 /* the region starts with some silence */
82 /* we must set length to an intermediate value here, otherwise the call
83 ** to set_start will fail */
84 region->set_length (region->length() - s->second + _fade_length, 0);
85 region->set_start (start + s->second - _fade_length, 0);
86 region->set_position (pos + s->second - _fade_length, 0);
87 region->set_fade_in_active (true);
88 region->set_fade_in (AudioRegion::Linear, _fade_length);
92 while (s != silence.end()) {
94 /* trim the end of this region */
95 region->trim_end (pos + s->first + _fade_length, 0);
96 region->set_fade_out_active (true);
97 region->set_fade_out (AudioRegion::Linear, _fade_length);
99 /* make a new region and trim its start */
100 region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
101 region->set_name (session.new_region_name (region->name ()));
102 last_region = region;
104 results.push_back (region);
106 /* set length here for the same reasons as above */
107 region->set_length (region->length() - s->second + _fade_length, 0);
108 region->set_start (start + s->second - _fade_length, 0);
109 region->set_position (pos + s->second - _fade_length, 0);
110 region->set_fade_in_active (true);
111 region->set_fade_in (AudioRegion::Linear, _fade_length);
116 if (silence.back().second == end) {
117 /* the last region we created is zero-sized, so just remove it */
120 /* finish off the last region */
121 last_region->trim_end (end, 0);