better comments
[ardour.git] / libs / ardour / strip_silence.cc
1 /*
2  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
4  * Copyright (C) 2010-2017 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include "pbd/property_list.h"
23
24 #include "ardour/strip_silence.h"
25 #include "ardour/audioregion.h"
26 #include "ardour/region_factory.h"
27 #include "ardour/progress.h"
28
29 using namespace ARDOUR;
30
31 /** Construct a StripSilence filter.
32  *  @param s Session.
33  *  @param sm Silences to remove.
34  *  @param fade_length Length of fade in/out to apply to trimmed regions, in samples.
35  */
36
37 StripSilence::StripSilence (Session & s, const AudioIntervalMap& sm, samplecnt_t fade_length)
38         : Filter (s)
39         , _smap (sm)
40         , _fade_length (fade_length)
41 {
42
43 }
44
45 int
46 StripSilence::run (boost::shared_ptr<Region> r, Progress* progress)
47 {
48         results.clear ();
49
50         /* we only operate on AudioRegions, for now, though this could be adapted to MIDI
51            as well I guess
52         */
53         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
54         InterThreadInfo itt;
55         AudioIntervalMap::const_iterator sm;
56
57         if (!region) {
58                 results.push_back (r);
59                 return -1;
60         }
61
62         if ((sm = _smap.find (r)) == _smap.end()) {
63                 results.push_back (r);
64                 return -1;
65         }
66
67         const AudioIntervalResult& silence = sm->second;
68
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 */
71                 return 0;
72         }
73
74         if (silence.empty()) {
75                 /* no silence in this region */
76                 results.push_back (region);
77                 return 0;
78         }
79
80         /* Turn the silence list into an `audible' list */
81         AudioIntervalResult audible;
82
83         /* Add the possible audible section at the start of the region */
84         AudioIntervalResult::const_iterator first_silence = silence.begin ();
85         if (first_silence->first != region->start()) {
86                 audible.push_back (std::make_pair (r->start(), first_silence->first));
87         }
88
89         /* Add audible sections in the middle of the region */
90         for (AudioIntervalResult::const_iterator i = silence.begin (); i != silence.end(); ++i) {
91                 AudioIntervalResult::const_iterator j = i;
92                 ++j;
93
94                 if (j != silence.end ()) {
95                         audible.push_back (std::make_pair (i->second, j->first));
96                 }
97         }
98
99         /* Add the possible audible section at the end of the region */
100         AudioIntervalResult::const_iterator last_silence = silence.end ();
101         --last_silence;
102
103         sampleoffset_t const end_of_region = r->start() + r->length();
104
105         if (last_silence->second < end_of_region - 1) {
106                 audible.push_back (std::make_pair (last_silence->second, end_of_region - 1));
107         }
108
109         int n = 0;
110         int const N = audible.size ();
111
112         for (AudioIntervalResult::const_iterator i = audible.begin(); i != audible.end(); ++i, ++n) {
113
114                 PBD::PropertyList plist;
115                 boost::shared_ptr<AudioRegion> copy;
116
117                 plist.add (Properties::length, i->second - i->first);
118                 plist.add (Properties::position, r->position() + (i->first - r->start()));
119
120                 copy = boost::dynamic_pointer_cast<AudioRegion> (
121                         RegionFactory::create (region, MusicSample (i->first - r->start(), 0), plist)
122                         );
123
124                 copy->set_name (RegionFactory::new_region_name (region->name ()));
125
126                 samplecnt_t const f = std::min (_fade_length, (i->second - i->first) / 2);
127
128                 if (f > 0) {
129                         copy->set_fade_in_active (true);
130                         copy->set_fade_out_active (true);
131                         copy->set_fade_in (FadeLinear, f);
132                         copy->set_fade_out (FadeLinear, f);
133                 } else {
134                         copy->set_fade_in_active (false);
135                         copy->set_fade_out_active (false);
136                 }
137                 results.push_back (copy);
138
139                 if (progress && (n <= N)) {
140                         progress->set_progress (float (n) / N);
141                 }
142         }
143
144         return 0;
145 }