changes to help strp silence
[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         InterThreadInfo itt;
50         
51         if (!region) {
52                 results.push_back (r);
53                 return -1;
54         }
55
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
58         */
59
60         itt.done = false;
61         itt.cancel = false;
62         itt.progress = 0.0;
63         itt.thread = 0;
64
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);
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         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 ();
84
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);
89
90         if (s->first == 0) {
91                 /* the region starts with some silence */
92
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);
100                 s++;
101         }
102
103         while (s != silence.end()) {
104
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);
109
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;
114                 assert (region);
115                 results.push_back (region);
116
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);
123
124                 s++;
125         }
126
127         if (silence.back().second == end) {
128                 /* the last region we created is zero-sized, so just remove it */
129                 results.pop_back ();
130         } else {
131                 /* finish off the last region */
132                 last_region->trim_end (end, 0);
133         }
134
135         return 0;
136 }