MESCLUN: new SAE-specific bindings file; parametized binding files; fix handling...
[ardour.git] / libs / ardour / audiofilter.cc
1 /*
2     Copyright (C) 2004 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/session.h>
26 #include <ardour/audioregion.h>
27 #include <ardour/audiofilter.h>
28 #include <ardour/region_factory.h>
29 #include <ardour/source_factory.h>
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35
36 int
37 AudioFilter::make_new_sources (boost::shared_ptr<AudioRegion> region, SourceList& nsrcs, string suffix)
38 {
39         vector<string> names = region->master_source_names();
40
41         if (names.size() != region->n_channels()) {
42                 warning << _("This is an old Ardour session that does not have\n\
43 sufficient information for rendered FX") << endmsg;
44                 return -1;
45         }
46
47         for (uint32_t i = 0; i < region->n_channels(); ++i) {
48
49                 string name = PBD::basename_nosuffix (names[i]);
50
51                 /* remove any existing version of suffix by assuming it starts
52                    with some kind of "special" character.
53                 */
54
55                 if (!suffix.empty()) {
56                         string::size_type pos = name.find (suffix[0]);
57                         if (pos != string::npos && pos > 2) {
58                                 name = name.substr (0, pos - 1);
59                         }
60                 }
61
62                 string path = session.path_from_region_name (name, suffix);
63
64                 if (path.length() == 0) {
65                         error << string_compose (_("audiofilter: error creating name for new audio file based on %1"), region->name()) 
66                               << endmsg;
67                         return -1;
68                 }
69
70                 try {
71                         nsrcs.push_back (boost::dynamic_pointer_cast<AudioSource> (SourceFactory::createWritable (session, path, false, session.frame_rate())));
72                         nsrcs.back()->prepare_for_peakfile_writes ();
73                 } 
74
75                 catch (failed_constructor& err) {
76                         error << string_compose (_("audiofilter: error creating new audio file %1 (%2)"), path, strerror (errno)) << endmsg;
77                         return -1;
78                 }
79         }
80
81         return 0;
82 }
83
84 int
85 AudioFilter::finish (boost::shared_ptr<AudioRegion> region, SourceList& nsrcs, string region_name)
86 {
87         /* update headers on new sources */
88
89         time_t xnow;
90         struct tm* now;
91
92         time (&xnow);
93         now = localtime (&xnow);
94
95         for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
96                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*si);
97                 boost::shared_ptr<AudioSource> as = boost::dynamic_pointer_cast<AudioSource>(*si);
98
99                 if (as) {
100                         as->done_with_peakfile_writes ();
101                 }
102
103                 if (afs) {
104                         afs->update_header (region->position(), *now, xnow);
105                         afs->mark_immutable ();
106                 }
107         }
108
109         /* create a new region */
110
111         if (region_name.empty()) {
112                 region_name = session.new_region_name (region->name());
113         }
114         results.clear ();
115         results.push_back (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (nsrcs, 0, nsrcs.front()->length(), region_name, 0, 
116                                                                                             Region::Flag (Region::WholeFile|Region::DefaultFlags))));
117         
118         return 0;
119 }