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