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
Source(Session & s,DataType type,const string & name,Flag flags)62 Source::Source (Session& s, DataType type, const string& name, Flag flags)
63 : SessionObject(s, name)
64 , _type(type)
65 , _flags(flags)
66 , _natural_position(0)
67 , _have_natural_position (false)
68 , _level (0)
69 {
70 g_atomic_int_set (&_use_count, 0);
71 _analysed = false;
72 _timestamp = 0;
73
74 fix_writable_flags ();
75 }
76
Source(Session & s,const XMLNode & node)77 Source::Source (Session& s, const XMLNode& node)
78 : SessionObject(s, "unnamed source")
79 , _type(DataType::AUDIO)
80 , _flags (Flag (Writable|CanRename))
81 , _natural_position(0)
82 , _have_natural_position (false)
83 , _level (0)
84 {
85 g_atomic_int_set (&_use_count, 0);
86 _analysed = false;
87 _timestamp = 0;
88
89 if (set_state (node, Stateful::loading_state_version) || _type == DataType::NIL) {
90 throw failed_constructor();
91 }
92
93 fix_writable_flags ();
94 }
95
~Source()96 Source::~Source ()
97 {
98 DEBUG_TRACE (DEBUG::Destruction, string_compose ("Source %1 destructor %2\n", _name, this));
99 }
100
101 void
fix_writable_flags()102 Source::fix_writable_flags ()
103 {
104 if (!_session.writable()) {
105 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
106 }
107 }
108
109 XMLNode&
get_state()110 Source::get_state ()
111 {
112 XMLNode *node = new XMLNode (X_("Source"));
113
114 node->set_property (X_("name"), name());
115 node->set_property (X_("take-id"), take_id());
116 node->set_property (X_("type"), _type);
117 node->set_property (X_(X_("flags")), _flags);
118 node->set_property (X_("id"), id());
119
120 if (_timestamp != 0) {
121 int64_t t = _timestamp;
122 node->set_property (X_("timestamp"), t);
123 }
124
125 if (_have_natural_position) {
126 node->set_property (X_("natural-position"), _natural_position);
127 }
128
129 if (!_xruns.empty ()) {
130 stringstream str;
131 for (XrunPositions::const_iterator xx = _xruns.begin(); xx != _xruns.end(); ++xx) {
132 str << PBD::to_string (*xx) << '\n';
133 }
134 XMLNode* xnode = new XMLNode (X_("xruns"));
135 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
136 content_node->set_content (str.str());
137 xnode->add_child_nocopy (*content_node);
138 node->add_child_nocopy (*xnode);
139 }
140
141 if (!_cue_markers.empty()) {
142 node->add_child_nocopy (get_cue_state());
143 }
144
145 return *node;
146 }
147
148 int
set_state(const XMLNode & node,int version)149 Source::set_state (const XMLNode& node, int version)
150 {
151 std::string str;
152 const CueMarkers old_cues = _cue_markers;
153 XMLNodeList nlist = node.children();
154 int64_t t;
155 samplepos_t ts;
156
157 if (node.name() == X_("Cues")) {
158 /* partial state */
159 int ret = set_cue_state (node, version);
160 if (ret) {
161 return ret;
162 }
163 goto out;
164 }
165
166 if (node.get_property ("name", str)) {
167 _name = str;
168 } else {
169 return -1;
170 }
171
172 if (!set_id (node)) {
173 return -1;
174 }
175
176 node.get_property ("type", _type);
177
178 if (node.get_property ("timestamp", t)) {
179 _timestamp = (time_t) t;
180 }
181
182 if (node.get_property ("natural-position", ts)) {
183 _natural_position = ts;
184 _have_natural_position = true;
185 } else if (node.get_property ("timeline-position", ts)) {
186 /* some older versions of ardour might have stored this with
187 this property name.
188 */
189 _natural_position = ts;
190 _have_natural_position = true;
191 }
192
193 if (!node.get_property (X_("flags"), _flags)) {
194 _flags = Flag (0);
195 }
196
197 _xruns.clear ();
198 for (XMLNodeIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
199
200 if ((*niter)->name() == X_("xruns")) {
201
202 const XMLNode& xruns (*(*niter));
203 if (xruns.children().empty()) {
204 break;
205 }
206 XMLNode* content_node = xruns.children().front();
207 stringstream str (content_node->content());
208 while (str) {
209 samplepos_t x;
210 std::string x_str;
211 str >> x_str;
212 if (!str || !PBD::string_to<samplepos_t> (x_str, x)) {
213 break;
214 }
215 _xruns.push_back (x);
216 }
217
218 } else if ((*niter)->name() == X_("Cues")) {
219 set_cue_state (**niter, version);
220 }
221 }
222
223 /* Destructive is no longer valid */
224
225 if (_flags & Destructive) {
226 _session.set_had_destructive_tracks (true);
227 }
228 _flags = Flag (_flags & ~Destructive);
229
230 if (!node.get_property (X_("take-id"), _take_id)) {
231 _take_id = "";
232 }
233
234 /* old style, from the period when we had DestructiveFileSource */
235 if (node.get_property (X_("destructive"), str)) {
236 _session.set_had_destructive_tracks (true);
237 }
238
239 if (version < 3000) {
240 /* a source with an XML node must necessarily already exist,
241 and therefore cannot be removable/writable etc. etc.; 2.X
242 sometimes marks sources as removable which shouldn't be.
243 */
244 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
245 }
246
247 /* support to make undo/redo actually function. Very few things about
248 * Sources are ever part of undo/redo history, but this can
249 * be. Undo/Redo uses a MementoCommand<> pattern, which will not in
250 * itself notify anyone when the operation changes the cue markers.
251 */
252
253 out:
254 if (old_cues != _cue_markers) {
255 CueMarkersChanged (); /* EMIT SIGNAL */
256 }
257
258 return 0;
259 }
260
261 XMLNode&
get_cue_state() const262 Source::get_cue_state () const
263 {
264 XMLNode* cue_parent = new XMLNode (X_("Cues"));
265
266 for (CueMarkers::const_iterator c = _cue_markers.begin(); c != _cue_markers.end(); ++c) {
267 XMLNode* cue_child = new XMLNode (X_("Cue"));
268 cue_child->set_property ("text", c->text());
269 cue_child->set_property ("position", c->position());
270 cue_parent->add_child_nocopy (*cue_child);
271 }
272
273 return *cue_parent;
274 }
275
276 int
set_cue_state(XMLNode const & cues,int)277 Source::set_cue_state (XMLNode const & cues, int /* version */)
278 {
279 _cue_markers.clear ();
280
281 const XMLNodeList cuelist = cues.children();
282
283 for (XMLNodeConstIterator citer = cuelist.begin(); citer != cuelist.end(); ++citer) {
284 string text;
285 samplepos_t position;
286
287 if (!(*citer)->get_property (X_("text"), text) || !(*citer)->get_property (X_("position"), position)) {
288 continue;
289 }
290
291 _cue_markers.insert (CueMarker (text, position));
292 }
293
294 return 0;
295 }
296
297 bool
has_been_analysed() const298 Source::has_been_analysed() const
299 {
300 Glib::Threads::Mutex::Lock lm (_analysis_lock);
301 return _analysed;
302 }
303
304 void
set_been_analysed(bool yn)305 Source::set_been_analysed (bool yn)
306 {
307 if (yn) {
308 if (0 == load_transients (get_transients_path())) {
309 yn = false;
310 }
311 }
312 if (yn != _analysed) {
313 Glib::Threads::Mutex::Lock lm (_analysis_lock);
314 _analysed = yn;
315 }
316 AnalysisChanged(); // EMIT SIGNAL
317 }
318
319 int
load_transients(const string & path)320 Source::load_transients (const string& path)
321 {
322 int rv = 0;
323 FILE *tf;
324 if (! (tf = g_fopen (path.c_str (), "rb"))) {
325 return -1;
326 }
327
328 transients.clear ();
329 while (!feof (tf) && !ferror(tf)) {
330 double val;
331 if (1 != fscanf (tf, "%lf", &val)) {
332 rv = -1;
333 break;
334 }
335
336 samplepos_t sample = (samplepos_t) floor (val * _session.sample_rate());
337 transients.push_back (sample);
338 }
339
340 ::fclose (tf);
341 return rv;
342 }
343
344 string
get_transients_path() const345 Source::get_transients_path () const
346 {
347 vector<string> parts;
348 string s;
349
350 /* old sessions may not have the analysis directory */
351
352 _session.ensure_subdirs ();
353
354 s = _session.analysis_dir ();
355 parts.push_back (s);
356
357 s = id().to_s();
358 s += '.';
359 s += TransientDetector::operational_identifier();
360 parts.push_back (s);
361
362 return Glib::build_filename (parts);
363 }
364
365 bool
check_for_analysis_data_on_disk()366 Source::check_for_analysis_data_on_disk ()
367 {
368 /* looks to see if the analysis files for this source are on disk.
369 if so, mark us already analysed.
370 */
371
372 string path = get_transients_path ();
373 bool ok = true;
374
375 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
376 ok = false;
377 }
378
379 // XXX add other tests here as appropriate
380
381 set_been_analysed (ok);
382 return ok;
383 }
384
385 void
mark_for_remove()386 Source::mark_for_remove ()
387 {
388 // This operation is not allowed for sources for out-of-session files.
389
390 /* XXX need a way to detect _within_session() condition here - move it from FileSource?
391 */
392
393 _flags = Flag (_flags | Removable | RemoveAtDestroy);
394 }
395
396 void
set_natural_position(samplepos_t pos)397 Source::set_natural_position (samplepos_t pos)
398 {
399 _natural_position = pos;
400 _have_natural_position = true;
401 }
402
403 void
set_allow_remove_if_empty(bool yn)404 Source::set_allow_remove_if_empty (bool yn)
405 {
406 if (!writable()) {
407 return;
408 }
409
410 if (yn) {
411 _flags = Flag (_flags | RemovableIfEmpty);
412 } else {
413 _flags = Flag (_flags & ~RemovableIfEmpty);
414 }
415 }
416
417 void
inc_use_count()418 Source::inc_use_count ()
419 {
420 g_atomic_int_inc (&_use_count);
421 }
422
423 void
dec_use_count()424 Source::dec_use_count ()
425 {
426 #ifndef NDEBUG
427 gint oldval = g_atomic_int_add (&_use_count, -1);
428 if (oldval <= 0) {
429 cerr << "Bad use dec for " << name() << endl;
430 abort ();
431 }
432 assert (oldval > 0);
433 #else
434 g_atomic_int_add (&_use_count, -1);
435 #endif
436
437 try {
438 boost::shared_ptr<Source> sptr = shared_from_this();
439 } catch (...) {
440 /* no shared_ptr available, relax; */
441 }
442 }
443
444 bool
writable() const445 Source::writable () const
446 {
447 return (_flags & Writable) && _session.writable();
448 }
449
450 bool
add_cue_marker(CueMarker const & cm)451 Source::add_cue_marker (CueMarker const & cm)
452 {
453 if (_cue_markers.insert (cm).second) {
454 CueMarkersChanged(); /* EMIT SIGNAL */
455 return true;
456 }
457
458 return false;
459 }
460
461 bool
move_cue_marker(CueMarker const & cm,samplepos_t source_relative_position)462 Source::move_cue_marker (CueMarker const & cm, samplepos_t source_relative_position)
463 {
464 if (source_relative_position > length (0)) {
465 return false;
466 }
467
468 if (remove_cue_marker (cm)) {
469 return add_cue_marker (CueMarker (cm.text(), source_relative_position));
470 }
471
472 return false;
473 }
474
475 bool
rename_cue_marker(CueMarker & cm,std::string const & str)476 Source::rename_cue_marker (CueMarker& cm, std::string const & str)
477 {
478 CueMarkers::iterator m = _cue_markers.find (cm);
479
480 if (m != _cue_markers.end()) {
481 _cue_markers.erase (m);
482 return add_cue_marker (CueMarker (str, cm.position()));
483 }
484
485 return false;
486 }
487
488 bool
remove_cue_marker(CueMarker const & cm)489 Source::remove_cue_marker (CueMarker const & cm)
490 {
491 if (_cue_markers.erase (cm)) {
492 CueMarkersChanged(); /* EMIT SIGNAL */
493 return true;
494 }
495
496 return false;
497 }
498
499 bool
clear_cue_markers()500 Source::clear_cue_markers ()
501 {
502 if (_cue_markers.empty()) {
503 return false;
504 }
505
506 _cue_markers.clear();
507 CueMarkersChanged(); /* EMIT SIGNAL */
508 return true;
509 }
510