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