move ownership of an RT MIDI buffer from DiskIO to MidiPlaylist
[ardour.git] / libs / ardour / source.cc
1 /*
2  * Copyright (C) 2000-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2005-2006 Jesse Chappell <jesse@essej.net>
4  * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
5  * Copyright (C) 2006-2011 David Robillard <d@drobilla.net>
6  * Copyright (C) 2007-2016 Tim Mayberry <mojofunk@gmail.com>
7  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
8  * Copyright (C) 2015 Robin Gareus <robin@gareus.org>
9  * Copyright (C) 2018 Ben Loftis <ben@harrisonconsoles.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <float.h>
29 #include <cerrno>
30 #include <ctime>
31 #include <cmath>
32 #include <iomanip>
33 #include <algorithm>
34
35 #include "pbd/gstdio_compat.h"
36 #include <glibmm/threads.h>
37 #include <glibmm/miscutils.h>
38 #include <glibmm/fileutils.h>
39 #include "pbd/xml++.h"
40 #include "pbd/pthread_utils.h"
41 #include "pbd/enumwriter.h"
42 #include "pbd/types_convert.h"
43
44 #include "ardour/debug.h"
45 #include "ardour/profile.h"
46 #include "ardour/session.h"
47 #include "ardour/source.h"
48 #include "ardour/transient_detector.h"
49 #include "ardour/types_convert.h"
50
51 #include "pbd/i18n.h"
52
53 namespace PBD {
54         DEFINE_ENUM_CONVERT(ARDOUR::Source::Flag);
55 }
56
57 using namespace std;
58 using namespace ARDOUR;
59 using namespace PBD;
60
61 PBD::Signal1<void,boost::shared_ptr<ARDOUR::Source> > Source::SourcePropertyChanged;
62
63
64 Source::Source (Session& s, DataType type, const string& name, Flag flags)
65         : SessionObject(s, name)
66         , _type(type)
67         , _flags(flags)
68         , _natural_position(0)
69         , _have_natural_position (false)
70         , _use_count (0)
71         , _level (0)
72 {
73         _analysed = false;
74         _timestamp = 0;
75         fix_writable_flags ();
76 }
77
78 Source::Source (Session& s, const XMLNode& node)
79         : SessionObject(s, "unnamed source")
80         , _type(DataType::AUDIO)
81         , _flags (Flag (Writable|CanRename))
82         , _natural_position(0)
83         , _have_natural_position (false)
84         , _use_count (0)
85         , _level (0)
86 {
87         _timestamp = 0;
88         _analysed = false;
89
90         if (set_state (node, Stateful::loading_state_version) || _type == DataType::NIL) {
91                 throw failed_constructor();
92         }
93
94         fix_writable_flags ();
95 }
96
97 Source::~Source ()
98 {
99         DEBUG_TRACE (DEBUG::Destruction, string_compose ("Source %1 destructor %2\n", _name, this));
100 }
101
102 void
103 Source::fix_writable_flags ()
104 {
105         if (!_session.writable()) {
106                 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
107         }
108 }
109
110 XMLNode&
111 Source::get_state ()
112 {
113         XMLNode *node = new XMLNode ("Source");
114
115         node->set_property ("name", name());
116         node->set_property ("take-id", take_id());
117         node->set_property ("type", _type);
118         node->set_property (X_("flags"), _flags);
119         node->set_property ("id", id());
120
121         if (_timestamp != 0) {
122                 int64_t t = _timestamp;
123                 node->set_property ("timestamp", t);
124         }
125
126         if (_have_natural_position) {
127                 node->set_property ("natural-position", _natural_position);
128         }
129
130         return *node;
131 }
132
133 int
134 Source::set_state (const XMLNode& node, int version)
135 {
136         std::string str;
137         if (node.get_property ("name", str)) {
138                 _name = str;
139         } else {
140                 return -1;
141         }
142
143         if (!set_id (node)) {
144                 return -1;
145         }
146
147         node.get_property ("type", _type);
148
149         int64_t t;
150         if (node.get_property ("timestamp", t)) {
151                 _timestamp = (time_t) t;
152         }
153
154         samplepos_t ts;
155         if (node.get_property ("natural-position", ts)) {
156                 _natural_position = ts;
157                 _have_natural_position = true;
158         } else if (node.get_property ("timeline-position", ts)) {
159                 /* some older versions of ardour might have stored this with
160                    this property name.
161                 */
162                 _natural_position = ts;
163                 _have_natural_position = true;
164         }
165
166         if (!node.get_property (X_("flags"), _flags)) {
167                 _flags = Flag (0);
168         }
169
170         if (!node.get_property (X_("take-id"), _take_id)) {
171                 _take_id = "";
172         }
173
174         /* old style, from the period when we had DestructiveFileSource */
175         if (node.get_property (X_("destructive"), str)) {
176                 _flags = Flag (_flags | Destructive);
177         }
178
179         if (version < 3000) {
180                 /* a source with an XML node must necessarily already exist,
181                    and therefore cannot be removable/writable etc. etc.; 2.X
182                    sometimes marks sources as removable which shouldn't be.
183                 */
184                 if (!(_flags & Destructive)) {
185                         _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
186                 }
187         }
188
189         return 0;
190 }
191
192 bool
193 Source::has_been_analysed() const
194 {
195         Glib::Threads::Mutex::Lock lm (_analysis_lock);
196         return _analysed;
197 }
198
199 void
200 Source::set_been_analysed (bool yn)
201 {
202         if (yn) {
203                 if (0 == load_transients (get_transients_path())) {
204                         yn = false;
205                 }
206         }
207         if (yn != _analysed) {
208                 Glib::Threads::Mutex::Lock lm (_analysis_lock);
209                 _analysed = yn;
210         }
211         AnalysisChanged(); // EMIT SIGNAL
212 }
213
214 int
215 Source::load_transients (const string& path)
216 {
217         int rv = 0;
218         FILE *tf;
219         if (! (tf = g_fopen (path.c_str (), "rb"))) {
220                 return -1;
221         }
222
223         transients.clear ();
224         while (!feof (tf) && !ferror(tf)) {
225                 double val;
226                 if (1 != fscanf (tf, "%lf", &val)) {
227                         rv = -1;
228                         break;
229                 }
230
231                 samplepos_t sample = (samplepos_t) floor (val * _session.sample_rate());
232                 transients.push_back (sample);
233         }
234
235         ::fclose (tf);
236         return rv;
237 }
238
239 string
240 Source::get_transients_path () const
241 {
242         vector<string> parts;
243         string s;
244
245         /* old sessions may not have the analysis directory */
246
247         _session.ensure_subdirs ();
248
249         s = _session.analysis_dir ();
250         parts.push_back (s);
251
252         s = id().to_s();
253         s += '.';
254         s += TransientDetector::operational_identifier();
255         parts.push_back (s);
256
257         return Glib::build_filename (parts);
258 }
259
260 bool
261 Source::check_for_analysis_data_on_disk ()
262 {
263         /* looks to see if the analysis files for this source are on disk.
264            if so, mark us already analysed.
265         */
266
267         string path = get_transients_path ();
268         bool ok = true;
269
270         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
271                 ok = false;
272         }
273
274         // XXX add other tests here as appropriate
275
276         set_been_analysed (ok);
277         return ok;
278 }
279
280 void
281 Source::mark_for_remove ()
282 {
283         // This operation is not allowed for sources for destructive tracks or out-of-session files.
284
285         /* XXX need a way to detect _within_session() condition here - move it from FileSource?
286          */
287
288         if ((_flags & Destructive)) {
289                 return;
290         }
291
292         _flags = Flag (_flags | Removable | RemoveAtDestroy);
293 }
294
295 void
296 Source::set_natural_position (samplepos_t pos)
297 {
298         _natural_position = pos;
299         _have_natural_position = true;
300 }
301
302 void
303 Source::set_allow_remove_if_empty (bool yn)
304 {
305         if (!writable()) {
306                 return;
307         }
308
309         if (yn) {
310                 _flags = Flag (_flags | RemovableIfEmpty);
311         } else {
312                 _flags = Flag (_flags & ~RemovableIfEmpty);
313         }
314 }
315
316 void
317 Source::inc_use_count ()
318 {
319     g_atomic_int_inc (&_use_count);
320
321         try {
322                 boost::shared_ptr<Source> sptr = shared_from_this();
323                 SourcePropertyChanged (sptr);
324         } catch (...) {
325                 /* no shared_ptr available, relax; */
326         }
327 }
328
329 void
330 Source::dec_use_count ()
331 {
332 #ifndef NDEBUG
333         gint oldval = g_atomic_int_add (&_use_count, -1);
334         if (oldval <= 0) {
335                 cerr << "Bad use dec for " << name() << endl;
336                 abort ();
337         }
338         assert (oldval > 0);
339 #else
340         g_atomic_int_add (&_use_count, -1);
341 #endif
342
343         try {
344                 boost::shared_ptr<Source> sptr = shared_from_this();
345                 SourcePropertyChanged (sptr);
346         } catch (...) {
347                 /* no shared_ptr available, relax; */
348         }
349 }
350
351 bool
352 Source::writable () const
353 {
354         return (_flags & Writable) && _session.writable();
355 }