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