make strip silence work (again?)
[ardour.git] / libs / ardour / strip_silence.cc
1 /*
2     Copyright (C) 2009-2010 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 "pbd/property_list.h"
21
22 #include "ardour/strip_silence.h"
23 #include "ardour/audioregion.h"
24 #include "ardour/region_factory.h"
25 #include "ardour/session.h"
26 #include "ardour/dB.h"
27 #include "ardour/progress.h"
28
29 using namespace ARDOUR;
30
31 /** Construct a StripSilence filter.
32  *  @param s Session.
33  *  @param threshold Threshold below which audio is considered silence, in dBFS.
34  *  @param minimum_length Minimum length of silence period to recognise, in samples.
35  *  @param fade_length Length of fade in/out to apply to trimmed regions, in samples.
36  */
37
38 StripSilence::StripSilence (Session & s, const AudioIntervalMap& sm, framecnt_t fade_length)
39         : Filter (s)
40         , _smap (sm)
41         , _fade_length (fade_length)
42 {
43
44 }
45
46 int
47 StripSilence::run (boost::shared_ptr<Region> r, Progress* progress)
48 {
49         results.clear ();
50
51         /* we only operate on AudioRegions, for now, though this could be adapted to MIDI
52            as well I guess 
53         */
54         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
55         InterThreadInfo itt;
56         AudioIntervalMap::const_iterator sm;
57
58         if (!region) {
59                 results.push_back (r);
60                 return -1;
61         }
62
63         if ((sm = _smap.find (r)) == _smap.end()) {
64                 results.push_back (r);
65                 return -1;
66         }
67
68         const AudioIntervalResult& silence = sm->second;
69
70         if (silence.size () == 1 && silence.front().first == 0 && silence.front().second == region->length() - 1) {
71                 /* the region is all silence, so just return with nothing */
72                 return 0;
73         }
74
75         if (silence.empty()) {
76                 /* no silence in this region */
77                 results.push_back (region);
78                 return 0;
79         }
80
81         AudioIntervalResult::const_iterator s = silence.begin ();
82         PBD::PropertyList plist;
83         framepos_t start;
84         framepos_t end;
85         bool in_silence;
86         boost::shared_ptr<AudioRegion> copy;
87
88         start = r->start();
89
90         if (s->first == start) {
91                 /* segment starting at zero is silent */
92                 end = s->second;
93                 in_silence = true;
94         } else {
95                 /* segment starting at zero is audible, and begins at the start of the region in the source */
96                 end = s->first;
97                 in_silence = false;
98         }
99
100         int n = 0;
101         int const N = silence.size ();
102
103         while (start < r->start() + r->length()) {
104
105                 framecnt_t interval_duration;
106
107                 interval_duration = end - start;
108
109                 if (!in_silence && interval_duration > 0) {
110
111                         plist.clear ();
112                         plist.add (Properties::length, interval_duration);
113                         plist.add (Properties::position, r->position() + (start - r->start()));
114
115                         copy = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create 
116                                                                          (region, (start - r->start()), plist));
117
118                         copy->set_name (RegionFactory::new_region_name (region->name ()));
119
120                         copy->set_fade_in_active (true);
121                         copy->set_fade_in (FadeLinear, _fade_length);
122                         results.push_back (copy);
123                 }
124
125                 start = end;
126                 in_silence = !in_silence;
127                 ++s;
128
129                 if (s == silence.end()) {
130                         end = r->start() + r->length();
131                 } else {
132                         end = s->first;
133                 }
134
135                 ++n;
136
137                 if (progress && (n <= N)) {
138                         progress->set_progress (float (n) / N);
139                 }
140
141         }
142
143         return 0;
144 }