ardour
strip_silence.cc
Go to the documentation of this file.
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/progress.h"
26 
27 using namespace ARDOUR;
28 
36  : Filter (s)
37  , _smap (sm)
38  , _fade_length (fade_length)
39 {
40 
41 }
42 
43 int
45 {
46  results.clear ();
47 
48  /* we only operate on AudioRegions, for now, though this could be adapted to MIDI
49  as well I guess
50  */
52  InterThreadInfo itt;
53  AudioIntervalMap::const_iterator sm;
54 
55  if (!region) {
56  results.push_back (r);
57  return -1;
58  }
59 
60  if ((sm = _smap.find (r)) == _smap.end()) {
61  results.push_back (r);
62  return -1;
63  }
64 
65  const AudioIntervalResult& silence = sm->second;
66 
67  if (silence.size () == 1 && silence.front().first == 0 && silence.front().second == region->length() - 1) {
68  /* the region is all silence, so just return with nothing */
69  return 0;
70  }
71 
72  if (silence.empty()) {
73  /* no silence in this region */
74  results.push_back (region);
75  return 0;
76  }
77 
78  /* Turn the silence list into an `audible' list */
79  AudioIntervalResult audible;
80 
81  /* Add the possible audible section at the start of the region */
82  AudioIntervalResult::const_iterator first_silence = silence.begin ();
83  if (first_silence->first != region->start()) {
84  audible.push_back (std::make_pair (r->start(), first_silence->first));
85  }
86 
87  /* Add audible sections in the middle of the region */
88  for (AudioIntervalResult::const_iterator i = silence.begin (); i != silence.end(); ++i) {
89  AudioIntervalResult::const_iterator j = i;
90  ++j;
91 
92  if (j != silence.end ()) {
93  audible.push_back (std::make_pair (i->second, j->first));
94  }
95  }
96 
97  /* Add the possible audible section at the end of the region */
98  AudioIntervalResult::const_iterator last_silence = silence.end ();
99  --last_silence;
100 
101  frameoffset_t const end_of_region = r->start() + r->length();
102 
103  if (last_silence->second != end_of_region - 1) {
104  audible.push_back (std::make_pair (last_silence->second, end_of_region - 1));
105  }
106 
107  int n = 0;
108  int const N = audible.size ();
109 
110  for (AudioIntervalResult::const_iterator i = audible.begin(); i != audible.end(); ++i) {
111 
112  PBD::PropertyList plist;
114 
115  plist.add (Properties::length, i->second - i->first);
116  plist.add (Properties::position, r->position() + (i->first - r->start()));
117 
119  RegionFactory::create (region, (i->first - r->start()), plist)
120  );
121 
122  copy->set_name (RegionFactory::new_region_name (region->name ()));
123 
124  framecnt_t const f = std::min (_fade_length, (i->second - i->first));
125 
126  copy->set_fade_in_active (true);
127  copy->set_fade_in (FadeLinear, f);
128  copy->set_fade_out (FadeLinear, f);
129  results.push_back (copy);
130 
131  if (progress && (n <= N)) {
132  progress->set_progress (float (n) / N);
133  }
134  }
135 
136  return 0;
137 }
void set_fade_out(FadeShape, framecnt_t)
shared_ptr< T > dynamic_pointer_cast(shared_ptr< U > const &r)
Definition: shared_ptr.hpp:396
std::list< std::pair< frameoffset_t, frameoffset_t > > AudioIntervalResult
Definition: types.h:83
bool set_name(const std::string &str)
Definition: region.cc:417
static int N
Definition: signals_test.cc:27
tuple f
Definition: signals.py:35
std::map< boost::shared_ptr< ARDOUR::Region >, AudioIntervalResult > AudioIntervalMap
Definition: types.h:85
static std::string new_region_name(std::string)
int64_t framecnt_t
Definition: types.h:76
const AudioIntervalMap & _smap
Definition: strip_silence.h:33
int run(boost::shared_ptr< ARDOUR::Region >, Progress *progress=0)
void set_progress(float)
Definition: progress.cc:59
Definition: amp.h:29
void set_fade_in(FadeShape, framecnt_t)
StripSilence(Session &, const AudioIntervalMap &, framecnt_t fade_length)
int64_t frameoffset_t
Definition: types.h:71
void set_fade_in_active(bool yn)
framepos_t position() const
Definition: region.h:112
std::vector< boost::shared_ptr< ARDOUR::Region > > results
Definition: filter.h:41
std::string name() const
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > position
Definition: region.cc:65
framecnt_t length() const
Definition: region.h:114
framepos_t start() const
Definition: region.h:113
framecnt_t _fade_length
fade in/out to use on trimmed regions, in samples
Definition: strip_silence.h:34
static boost::shared_ptr< Region > create(boost::shared_ptr< const Region > other, bool announce=false)
bool add(PropertyBase *prop)
LIBARDOUR_API PBD::PropertyDescriptor< framecnt_t > length
Definition: region.cc:64