Fix for deadlock in Session::remove_source that was committed in rev 1815
[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)
38 {
39         vector<string> names = region->master_source_names();
40
41         for (uint32_t i = 0; i < region->n_channels(); ++i) {
42
43                 string path = session.path_from_region_name (PBD::basename_nosuffix (names[i]), string (""));
44
45                 if (path.length() == 0) {
46                         error << string_compose (_("audiofilter: error creating name for new audio file based on %1"), region->name()) 
47                               << endmsg;
48                         return -1;
49                 }
50
51                 try {
52                         nsrcs.push_back (boost::dynamic_pointer_cast<AudioSource> (
53                                 SourceFactory::createWritable (DataType::AUDIO, session, path, false, session.frame_rate())));
54                 } 
55
56                 catch (failed_constructor& err) {
57                         error << string_compose (_("audiofilter: error creating new audio file %1 (%2)"), path, strerror (errno)) << endmsg;
58                         return -1;
59                 }
60         }
61
62         return 0;
63 }
64
65 int
66 AudioFilter::finish (boost::shared_ptr<AudioRegion> region, SourceList& nsrcs)
67 {
68         string region_name;
69
70         /* update headers on new sources */
71
72         time_t xnow;
73         struct tm* now;
74
75         time (&xnow);
76         now = localtime (&xnow);
77
78         for (SourceList::iterator si = nsrcs.begin(); si != nsrcs.end(); ++si) {
79                 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(*si);
80                 if (afs) {
81                         afs->update_header (region->position(), *now, xnow);
82                         afs->mark_immutable ();
83                 }
84         }
85
86         /* create a new region */
87
88         region_name = session.new_region_name (region->name());
89         results.clear ();
90         results.push_back (boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create (nsrcs, 0, region->length(), region_name, 0, 
91                                                                                             Region::Flag (Region::WholeFile|Region::DefaultFlags))));
92         
93         return 0;
94 }