summaryrefslogtreecommitdiff
path: root/libs/ardour/strip_silence.cc
blob: 0f58ccf9a3efc81c8eb9b272c53c7dd23bf53179 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
    Copyright (C) 2009 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include "ardour/strip_silence.h"
#include "ardour/audioregion.h"
#include "ardour/region_factory.h"
#include "ardour/session.h"
#include "ardour/dB.h"

using namespace ARDOUR;

/** Construct a StripSilence filter.
 *  @param s Session.
 *  @param threshold Threshold below which audio is considered silence, in dBFS.
 *  @param minimum_length Minimum length of silence period to recognise, in samples.
 *  @param fade_length Length of fade in/out to apply to trimmed regions, in samples.
 */

StripSilence::StripSilence (Session & s, double threshold, nframes_t minimum_length, nframes_t fade_length)
	: Filter (s), _threshold (threshold), _minimum_length (minimum_length), _fade_length (fade_length)
{

}

int
StripSilence::run (boost::shared_ptr<Region> r)
{
	results.clear ();

	/* we only operate on AudioRegions, for now, though this could be adapted to MIDI
	   as well I guess */
	boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
	if (!region) {
		results.push_back (r);
		return -1;
	}

	/* find periods of silence in the region */
	std::list<std::pair<frameoffset_t, framecnt_t> > const silence =
		region->find_silence (dB_to_coefficient (_threshold), _minimum_length);

	if (silence.size () == 1 && silence.front().first == 0 && silence.front().second == region->length() - 1) {
		/* the region is all silence, so just return with nothing */
		return 0;
	}

	if (silence.empty()) {
		/* no silence in this region */
		results.push_back (region);
		return 0;
	}

	std::list<std::pair<framepos_t, framecnt_t > >::const_iterator s = silence.begin ();
	framepos_t const pos = region->position ();
	framepos_t const end = region->start () + region->length() - 1;
	framepos_t const start = region->start ();

	region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
	region->set_name (session.new_region_name (region->name ()));
	boost::shared_ptr<AudioRegion> last_region = region;
	results.push_back (region);

	if (s->first == 0) {
		/* the region starts with some silence */

		/* we must set length to an intermediate value here, otherwise the call
		** to set_start will fail */
		region->set_length (region->length() - s->second + _fade_length, 0);
		region->set_start (start + s->second - _fade_length, 0);
		region->set_position (pos + s->second - _fade_length, 0);
		region->set_fade_in_active (true);
		region->set_fade_in (AudioRegion::Linear, _fade_length);
		s++;
	}

	while (s != silence.end()) {

		/* trim the end of this region */
		region->trim_end (pos + s->first + _fade_length, 0);
		region->set_fade_out_active (true);
		region->set_fade_out (AudioRegion::Linear, _fade_length);

		/* make a new region and trim its start */
		region = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (region));
		region->set_name (session.new_region_name (region->name ()));
		last_region = region;
		assert (region);
		results.push_back (region);

		/* set length here for the same reasons as above */
		region->set_length (region->length() - s->second + _fade_length, 0);
		region->set_start (start + s->second - _fade_length, 0);
		region->set_position (pos + s->second - _fade_length, 0);
		region->set_fade_in_active (true);
		region->set_fade_in (AudioRegion::Linear, _fade_length);

		s++;
	}

	if (silence.back().second == end) {
		/* the last region we created is zero-sized, so just remove it */
		results.pop_back ();
	} else {
		/* finish off the last region */
		last_region->trim_end (end, 0);
	}

	return 0;
}