Merge with trunk R2920.
[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
32 #include "i18n.h"
33
34 using namespace ARDOUR;
35 using namespace PBD;
36
37 int
38 Filter::make_new_sources (boost::shared_ptr<Region> region, SourceList& nsrcs, string suffix)
39 {
40         vector<string> names = region->master_source_names();
41
42         for (uint32_t i = 0; i < region->n_channels(); ++i) {
43
44                 string name = PBD::basename_nosuffix (names[i]);
45
46                 /* remove any existing version of suffix by assuming it starts
47                    with some kind of "special" character.
48                 */
49
50                 if (!suffix.empty()) {
51                         string::size_type pos = name.find (suffix[0]);
52                         if (pos != string::npos && pos > 2) {
53                                 name = name.substr (0, pos - 1);
54                         }
55                 }
56
57                 string path = session.path_from_region_name (region->data_type(),
58                                 PBD::basename_nosuffix (names[i]), string (""));
59
60                 if (path.length() == 0) {
61                         error << string_compose (_("filter: error creating name for new file based on %1"), region->name()) 
62                               << endmsg;
63                         return -1;
64                 }
65
66                 try {
67                         nsrcs.push_back (boost::dynamic_pointer_cast<Source> (
68                                 SourceFactory::createWritable (region->data_type(), session, path, false, session.frame_rate())));
69                 } 
70
71                 catch (failed_constructor& err) {
72                         error << string_compose (_("filter: error creating new file %1 (%2)"), path, strerror (errno)) << endmsg;
73                         return -1;
74                 }
75         }
76
77         return 0;
78 }
79
80 int
81 Filter::finish (boost::shared_ptr<Region> region, SourceList& nsrcs, string region_name)
82 {
83         /* update headers on new sources */
84
85         time_t xnow;
86         struct tm* now;
87
88         time (&xnow);
89         now = localtime (&xnow);
90
91         /* this is ugly. */
92         for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
93                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*si);
94                 if (afs) {
95                         afs->update_header (region->position(), *now, xnow);
96                         afs->mark_immutable ();
97                 }
98                 
99                 boost::shared_ptr<SMFSource> smfs = boost::dynamic_pointer_cast<SMFSource>(*si);
100                 if (smfs) {
101                         smfs->set_timeline_position (region->position());
102                         smfs->flush_footer ();
103                 }
104         }
105
106         /* create a new region */
107
108         if (region_name.empty()) {
109                 region_name = session.new_region_name (region->name());
110         }
111         results.clear ();
112         results.push_back (RegionFactory::create (nsrcs, 0, region->length(), region_name, 0, 
113                         Region::Flag (Region::WholeFile|Region::DefaultFlags)));
114         
115         return 0;
116 }
117
118