substantive changes to the logic and safety for naming of (audio/MIDI) sources, espec...
[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
25 #include "ardour/analyser.h"
26 #include "ardour/audiofilesource.h"
27 #include "ardour/audioregion.h"
28 #include "ardour/filter.h"
29 #include "ardour/region.h"
30 #include "ardour/region_factory.h"
31 #include "ardour/session.h"
32 #include "ardour/smf_source.h"
33 #include "ardour/source_factory.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 int
42 Filter::make_new_sources (boost::shared_ptr<Region> region, SourceList& nsrcs, string suffix)
43 {
44         vector<string> names = region->master_source_names();
45         assert (region->n_channels() <= names.size());
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.new_audio_source_path (name, region->n_channels(), i, false, false);
63
64                 if (path.empty()) {
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, false, session.frame_rate())));
74                 }
75
76                 catch (failed_constructor& err) {
77                         error << string_compose (_("filter: error creating new file %1 (%2)"), path, strerror (errno)) << endmsg;
78                         return -1;
79                 }
80         }
81
82         return 0;
83 }
84
85 int
86 Filter::finish (boost::shared_ptr<Region> 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         /* this is ugly. */
97         for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
98                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*si);
99                 if (afs) {
100                         afs->done_with_peakfile_writes ();
101                         afs->update_header (region->position(), *now, xnow);
102                         afs->mark_immutable ();
103                 }
104
105                 boost::shared_ptr<SMFSource> smfs = boost::dynamic_pointer_cast<SMFSource>(*si);
106                 if (smfs) {
107                         smfs->set_timeline_position (region->position());
108                         smfs->flush ();
109                 }
110
111                 /* now that there is data there, requeue the file for analysis */
112
113                 Analyser::queue_source_for_analysis (*si, false);
114         }
115
116         /* create a new region */
117
118         if (region_name.empty()) {
119                 region_name = RegionFactory::new_region_name (region->name());
120         }
121         results.clear ();
122
123         PropertyList plist;
124
125         plist.add (Properties::start, 0);
126         plist.add (Properties::length, region->length());
127         plist.add (Properties::name, region_name);
128         plist.add (Properties::whole_file, true);
129         plist.add (Properties::position, region->position());
130
131         boost::shared_ptr<Region> r = RegionFactory::create (nsrcs, plist);
132
133         boost::shared_ptr<AudioRegion> audio_region = boost::dynamic_pointer_cast<AudioRegion> (region);
134         boost::shared_ptr<AudioRegion> audio_r = boost::dynamic_pointer_cast<AudioRegion> (r);
135         if (audio_region && audio_r) {
136                 audio_r->set_scale_amplitude (audio_region->scale_amplitude());
137                 audio_r->set_fade_in_active (audio_region->fade_in_active ());
138                 audio_r->set_fade_in (audio_region->fade_in ());
139                 audio_r->set_fade_out_active (audio_region->fade_out_active ());
140                 audio_r->set_fade_out (audio_region->fade_out ());
141                 *(audio_r->envelope()) = *(audio_region->envelope ());
142         }
143         results.push_back (r);
144
145         return 0;
146 }
147
148