the Properties & 64bit region commit
[ardour.git] / libs / ardour / strip_silence.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
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"
25
26 using namespace ARDOUR;
27
28 /** Construct a StripSilence filter.
29  *  @param s Session.
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.
33  */
34
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)
37 {
38
39 }
40
41 int
42 StripSilence::run (boost::shared_ptr<Region> r)
43 {
44         results.clear ();
45
46         /* we only operate on AudioRegions, for now, though this could be adapted to MIDI
47            as well I guess */
48         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
49         if (!region) {
50                 results.push_back (r);
51                 return -1;
52         }
53
54         /* find periods of silence in the region */
55         std::list<std::pair<frameoffset_t, framecnt_t> > const silence =
56                 region->find_silence (dB_to_coefficient (_threshold), _minimum_length);
57
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 */
60                 return 0;
61         }
62
63         if (silence.empty()) {
64                 /* no silence in this region */
65                 results.push_back (region);
66                 return 0;
67         }
68
69         std::list<std::pair<framepos_t, framecnt_t > >::const_iterator s = silence.begin ();
70         framepos_t const pos = region->position ();
71         framepos_t const end = region->start () + region->length() - 1;
72         framepos_t const start = region->start ();
73
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);
78
79         if (s->first == 0) {
80                 /* the region starts with some silence */
81
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);
89                 s++;
90         }
91
92         while (s != silence.end()) {
93
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);
98
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;
103                 assert (region);
104                 results.push_back (region);
105
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);
112
113                 s++;
114         }
115
116         if (silence.back().second == end) {
117                 /* the last region we created is zero-sized, so just remove it */
118                 results.pop_back ();
119         } else {
120                 /* finish off the last region */
121                 last_region->trim_end (end, 0);
122         }
123
124         return 0;
125 }