Remove all use of nframes_t.
[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 "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, double threshold, framecnt_t minimum_length, framecnt_t fade_length)
39         : Filter (s), _threshold (threshold), _minimum_length (minimum_length), _fade_length (fade_length)
40 {
41
42 }
43
44 int
45 StripSilence::run (boost::shared_ptr<Region> r, Progress* progress)
46 {
47         results.clear ();
48
49         /* we only operate on AudioRegions, for now, though this could be adapted to MIDI
50            as well I guess 
51         */
52         boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
53         InterThreadInfo itt;
54         
55         if (!region) {
56                 results.push_back (r);
57                 return -1;
58         }
59
60         /* find periods of silence in the region */
61         std::list<std::pair<frameoffset_t, framecnt_t> > const silence =
62                 region->find_silence (dB_to_coefficient (_threshold), _minimum_length, itt);
63
64         if (silence.size () == 1 && silence.front().first == 0 && silence.front().second == region->length() - 1) {
65                 /* the region is all silence, so just return with nothing */
66                 return 0;
67         }
68
69         if (silence.empty()) {
70                 /* no silence in this region */
71                 results.push_back (region);
72                 return 0;
73         }
74
75         std::list<std::pair<framepos_t, framecnt_t > >::const_iterator s = silence.begin ();
76         PBD::PropertyList plist;
77         framepos_t start = 0;
78         framepos_t end;
79         bool in_silence;
80         boost::shared_ptr<AudioRegion> copy;
81
82         if (s->first == 0) {
83                 /* initial segment, starting at zero, is silent */
84                 end = s->second;
85                 in_silence = true;
86         } else {
87                 /* initial segment, starting at zero, is audible */
88                 end = s->first;
89                 in_silence = false;
90         }
91
92         int n = 0;
93         int const N = silence.size ();
94
95         while (s != silence.end()) {
96
97                 framecnt_t interval_duration;
98
99                 interval_duration = end - start;
100
101
102                 if (!in_silence && interval_duration > 0) {
103
104                         plist.clear ();
105                         plist.add (Properties::length, interval_duration);
106                         plist.add (Properties::position, region->position() + start);
107
108                         copy = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create 
109                                                                          (region, start, plist));
110
111                         copy->set_name (RegionFactory::new_region_name (region->name ()));
112
113                         std::cerr << "New silent delineated region called " << copy->name()
114                                   << " @ " << copy->start() << " length = " << copy->length() << " pos = " << 
115                                 copy->position() << std::endl;
116
117                         copy->set_fade_in_active (true);
118                         copy->set_fade_in (FadeLinear, _fade_length);
119                         results.push_back (copy);
120                 }
121
122                 start = end;
123                 ++s;
124                 end = s->first;
125                 in_silence = !in_silence;
126
127                 if (progress) {
128                         progress->set_progress (float (n) / N);
129                 }
130                 ++n;
131         }
132
133         return 0;
134 }