Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[ardour.git] / libs / ardour / filter.cc
1 /*
2     Copyright (C) 2004-2007 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 <time.h>
21 #include <cerrno>
22
23 #include "pbd/basename.h"
24 #include "ardour/sndfilesource.h"
25 #include "ardour/smf_source.h"
26 #include "ardour/session.h"
27 #include "ardour/region.h"
28 #include "ardour/filter.h"
29 #include "ardour/region_factory.h"
30 #include "ardour/source_factory.h"
31 #include "ardour/analyser.h"
32 #include "ardour/audioregion.h"
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 int
41 Filter::make_new_sources (boost::shared_ptr<Region> region, SourceList& nsrcs, string suffix)
42 {
43         vector<string> names = region->master_source_names();
44         assert (region->n_channels() <= names.size());
45
46         for (uint32_t i = 0; i < region->n_channels(); ++i) {
47
48                 string name = PBD::basename_nosuffix (names[i]);
49
50                 /* remove any existing version of suffix by assuming it starts
51                    with some kind of "special" character.
52                 */
53
54                 if (!suffix.empty()) {
55                         string::size_type pos = name.find (suffix[0]);
56                         if (pos != string::npos && pos > 2) {
57                                 name = name.substr (0, pos - 1);
58                         }
59                 }
60
61                 string path = session.path_from_region_name (region->data_type(),
62                                 PBD::basename_nosuffix (names[i]), string (""));
63
64                 if (path.length() == 0) {
65                         error << string_compose (_("filter: error creating name for new file based on %1"), region->name())
66                               << endmsg;
67                         return -1;
68                 }
69
70                 try {
71                         nsrcs.push_back (boost::dynamic_pointer_cast<Source> (
72                                 SourceFactory::createWritable (region->data_type(), session,
73                                         path, true,
74                                         false, session.frame_rate())));
75                 }
76
77                 catch (failed_constructor& err) {
78                         error << string_compose (_("filter: error creating new file %1 (%2)"), path, strerror (errno)) << endmsg;
79                         return -1;
80                 }
81         }
82
83         return 0;
84 }
85
86 int
87 Filter::finish (boost::shared_ptr<Region> region, SourceList& nsrcs, string region_name)
88 {
89         /* update headers on new sources */
90
91         time_t xnow;
92         struct tm* now;
93
94         time (&xnow);
95         now = localtime (&xnow);
96
97         /* this is ugly. */
98         for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
99                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*si);
100                 if (afs) {
101                         afs->done_with_peakfile_writes ();
102                         afs->update_header (region->position(), *now, xnow);
103                         afs->mark_immutable ();
104                 }
105
106                 boost::shared_ptr<SMFSource> smfs = boost::dynamic_pointer_cast<SMFSource>(*si);
107                 if (smfs) {
108                         smfs->set_timeline_position (region->position());
109                         smfs->flush ();
110                 }
111
112                 /* now that there is data there, requeue the file for analysis */
113
114                 Analyser::queue_source_for_analysis (*si, false);
115         }
116
117         /* create a new region */
118
119         if (region_name.empty()) {
120                 region_name = session.new_region_name (region->name());
121         }
122         results.clear ();
123
124         boost::shared_ptr<Region> r = RegionFactory::create (nsrcs, 0, region->length(), region_name, 0,
125                                                              Region::Flag (Region::WholeFile|Region::DefaultFlags));
126         r->set_position (region->position(), 0);
127
128         boost::shared_ptr<AudioRegion> audio_region = boost::dynamic_pointer_cast<AudioRegion> (region);
129         boost::shared_ptr<AudioRegion> audio_r = boost::dynamic_pointer_cast<AudioRegion> (r);
130         if (audio_region && audio_r) {
131                 audio_r->set_scale_amplitude (audio_region->scale_amplitude());
132         }
133         results.push_back (r);
134
135         return 0;
136 }
137
138