lincoln's patch to stop a3 from nuking bounced files created by a2, fixes #3441
[ardour.git] / libs / ardour / source.cc
1 /*
2     Copyright (C) 2000 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 <sys/stat.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <float.h>
25 #include <cerrno>
26 #include <ctime>
27 #include <cmath>
28 #include <iomanip>
29 #include <algorithm>
30 #include <fstream>
31
32 #include <glibmm/thread.h>
33 #include <glibmm/miscutils.h>
34 #include <glibmm/fileutils.h>
35 #include "pbd/xml++.h"
36 #include "pbd/pthread_utils.h"
37 #include "pbd/enumwriter.h"
38
39 #include "ardour/debug.h"
40 #include "ardour/session.h"
41 #include "ardour/source.h"
42 #include "ardour/transient_detector.h"
43
44 #include "i18n.h"
45
46 using namespace std;
47 using namespace ARDOUR;
48 using namespace PBD;
49
50 Source::Source (Session& s, DataType type, const string& name, Flag flags)
51         : SessionObject(s, name)
52         , _type(type)
53         , _flags(flags)
54         , _timeline_position(0)
55         , _use_count (0)
56 {
57         _analysed = false;
58         _timestamp = 0;
59         fix_writable_flags ();
60 }
61
62 Source::Source (Session& s, const XMLNode& node)
63         : SessionObject(s, "unnamed source")
64         , _type(DataType::AUDIO)
65         , _flags (Flag (Writable|CanRename))
66         , _timeline_position(0)
67         , _use_count (0)
68 {
69         _timestamp = 0;
70         _analysed = false;
71
72         if (set_state (node, Stateful::loading_state_version) || _type == DataType::NIL) {
73                 throw failed_constructor();
74         }
75
76         fix_writable_flags ();
77 }
78
79 Source::~Source ()
80 {
81         DEBUG_TRACE (DEBUG::Destruction, string_compose ("Source %1 destructor %2\n", _name, this));
82 }
83
84 void
85 Source::fix_writable_flags ()
86 {
87         if (!_session.writable()) {
88                 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
89         }
90 }
91
92 XMLNode&
93 Source::get_state ()
94 {
95         XMLNode *node = new XMLNode ("Source");
96         char buf[64];
97
98         node->add_property ("name", name());
99         node->add_property ("type", _type.to_string());
100         node->add_property (X_("flags"), enum_2_string (_flags));
101         _id.print (buf, sizeof (buf));
102         node->add_property ("id", buf);
103
104         if (_timestamp != 0) {
105                 snprintf (buf, sizeof (buf), "%ld", _timestamp);
106                 node->add_property ("timestamp", buf);
107         }
108
109         return *node;
110 }
111
112 int
113 Source::set_state (const XMLNode& node, int /*version*/)
114 {
115         const XMLProperty* prop;
116
117         if ((prop = node.property ("name")) != 0) {
118                 _name = prop->value();
119         } else {
120                 return -1;
121         }
122
123         if ((prop = node.property ("id")) != 0) {
124                 _id = prop->value ();
125         } else {
126                 return -1;
127         }
128
129         if ((prop = node.property ("type")) != 0) {
130                 _type = DataType(prop->value());
131         }
132
133         if ((prop = node.property ("timestamp")) != 0) {
134                 sscanf (prop->value().c_str(), "%ld", &_timestamp);
135         }
136
137         if ((prop = node.property (X_("flags"))) != 0) {
138                 _flags = Flag (string_2_enum (prop->value(), _flags));
139         } else {
140                 _flags = Flag (0);
141
142         }
143
144         /* old style, from the period when we had DestructiveFileSource */
145         if ((prop = node.property (X_("destructive"))) != 0) {
146                 _flags = Flag (_flags | Destructive);
147         }
148
149         /* a source with an XML node must necessarily already exist, 
150            and therefore cannot be removable/writable etc. etc.
151         */
152         if (!(_flags & Destructive)) {
153                 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
154         }
155
156         return 0;
157 }
158
159 bool
160 Source::has_been_analysed() const
161 {
162         Glib::Mutex::Lock lm (_analysis_lock);
163         return _analysed;
164 }
165
166 void
167 Source::set_been_analysed (bool yn)
168 {
169         {
170                 Glib::Mutex::Lock lm (_analysis_lock);
171                 _analysed = yn;
172         }
173
174         if (yn) {
175                 load_transients (get_transients_path());
176                 AnalysisChanged(); // EMIT SIGNAL
177         }
178 }
179
180 int
181 Source::load_transients (const string& path)
182 {
183         ifstream file (path.c_str());
184
185         if (!file) {
186                 return -1;
187         }
188
189         transients.clear ();
190
191         stringstream strstr;
192         double val;
193
194         while (file.good()) {
195                 file >> val;
196
197                 if (!file.fail()) {
198                         nframes64_t frame = (nframes64_t) floor (val * _session.frame_rate());
199                         transients.push_back (frame);
200                 }
201         }
202
203         return 0;
204 }
205
206 string
207 Source::get_transients_path () const
208 {
209         vector<string> parts;
210         string s;
211
212         /* old sessions may not have the analysis directory */
213
214         _session.ensure_subdirs ();
215
216         s = _session.analysis_dir ();
217         parts.push_back (s);
218
219         s = _id.to_s();
220         s += '.';
221         s += TransientDetector::operational_identifier();
222         parts.push_back (s);
223
224         return Glib::build_filename (parts);
225 }
226
227 bool
228 Source::check_for_analysis_data_on_disk ()
229 {
230         /* looks to see if the analysis files for this source are on disk.
231            if so, mark us already analysed.
232         */
233
234         string path = get_transients_path ();
235         bool ok = true;
236
237         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
238                 ok = false;
239         }
240
241         // XXX add other tests here as appropriate
242
243         set_been_analysed (ok);
244         return ok;
245 }
246
247 void
248 Source::mark_for_remove ()
249 {
250         // This operation is not allowed for sources for destructive tracks or out-of-session files.
251
252         /* XXX need a way to detect _within_session() condition here - move it from FileSource? 
253          */
254
255         if ((_flags & Destructive)) { 
256                 return;
257         }
258
259         _flags = Flag (_flags | Removable | RemoveAtDestroy);
260 }
261
262 void
263 Source::set_timeline_position (int64_t pos)
264 {
265         _timeline_position = pos;
266 }
267
268 void
269 Source::set_allow_remove_if_empty (bool yn)
270 {
271         if (!writable()) {
272                 return;
273         }
274
275         if (yn) {
276                 _flags = Flag (_flags | RemovableIfEmpty);
277         } else {
278                 _flags = Flag (_flags & ~RemovableIfEmpty);
279         }
280 }
281
282 void
283 Source::inc_use_count ()
284 {
285         g_atomic_int_inc (&_use_count);
286 }
287
288 void
289 Source::dec_use_count ()
290 {
291 #ifndef NDEBUG
292         gint oldval = g_atomic_int_exchange_and_add (&_use_count, -1);
293         if (oldval <= 0) {
294                 cerr << "Bad use dec for " << name() << endl;
295                 abort ();
296         }
297         assert (oldval > 0);
298 #else 
299         g_atomic_int_exchange_and_add (&_use_count, -1);
300 #endif
301 }