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>
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.
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.
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.
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"
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"
54 DEFINE_ENUM_CONVERT(ARDOUR::Source::Flag);
58 using namespace ARDOUR;
61 PBD::Signal1<void,boost::shared_ptr<ARDOUR::Source> > Source::SourcePropertyChanged;
64 Source::Source (Session& s, DataType type, const string& name, Flag flags)
65 : SessionObject(s, name)
68 , _natural_position(0)
69 , _have_natural_position (false)
75 fix_writable_flags ();
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)
90 if (set_state (node, Stateful::loading_state_version) || _type == DataType::NIL) {
91 throw failed_constructor();
94 fix_writable_flags ();
99 DEBUG_TRACE (DEBUG::Destruction, string_compose ("Source %1 destructor %2\n", _name, this));
103 Source::fix_writable_flags ()
105 if (!_session.writable()) {
106 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
113 XMLNode *node = new XMLNode ("Source");
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());
121 if (_timestamp != 0) {
122 int64_t t = _timestamp;
123 node->set_property ("timestamp", t);
126 if (_have_natural_position) {
127 node->set_property ("natural-position", _natural_position);
134 Source::set_state (const XMLNode& node, int version)
137 if (node.get_property ("name", str)) {
143 if (!set_id (node)) {
147 node.get_property ("type", _type);
150 if (node.get_property ("timestamp", t)) {
151 _timestamp = (time_t) t;
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
162 _natural_position = ts;
163 _have_natural_position = true;
166 if (!node.get_property (X_("flags"), _flags)) {
170 if (!node.get_property (X_("take-id"), _take_id)) {
174 /* old style, from the period when we had DestructiveFileSource */
175 if (node.get_property (X_("destructive"), str)) {
176 _flags = Flag (_flags | Destructive);
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.
184 if (!(_flags & Destructive)) {
185 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
193 Source::has_been_analysed() const
195 Glib::Threads::Mutex::Lock lm (_analysis_lock);
200 Source::set_been_analysed (bool yn)
203 if (0 == load_transients (get_transients_path())) {
207 if (yn != _analysed) {
208 Glib::Threads::Mutex::Lock lm (_analysis_lock);
211 AnalysisChanged(); // EMIT SIGNAL
215 Source::load_transients (const string& path)
219 if (! (tf = g_fopen (path.c_str (), "rb"))) {
224 while (!feof (tf) && !ferror(tf)) {
226 if (1 != fscanf (tf, "%lf", &val)) {
231 samplepos_t sample = (samplepos_t) floor (val * _session.sample_rate());
232 transients.push_back (sample);
240 Source::get_transients_path () const
242 vector<string> parts;
245 /* old sessions may not have the analysis directory */
247 _session.ensure_subdirs ();
249 s = _session.analysis_dir ();
254 s += TransientDetector::operational_identifier();
257 return Glib::build_filename (parts);
261 Source::check_for_analysis_data_on_disk ()
263 /* looks to see if the analysis files for this source are on disk.
264 if so, mark us already analysed.
267 string path = get_transients_path ();
270 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
274 // XXX add other tests here as appropriate
276 set_been_analysed (ok);
281 Source::mark_for_remove ()
283 // This operation is not allowed for sources for destructive tracks or out-of-session files.
285 /* XXX need a way to detect _within_session() condition here - move it from FileSource?
288 if ((_flags & Destructive)) {
292 _flags = Flag (_flags | Removable | RemoveAtDestroy);
296 Source::set_natural_position (samplepos_t pos)
298 _natural_position = pos;
299 _have_natural_position = true;
303 Source::set_allow_remove_if_empty (bool yn)
310 _flags = Flag (_flags | RemovableIfEmpty);
312 _flags = Flag (_flags & ~RemovableIfEmpty);
317 Source::inc_use_count ()
319 g_atomic_int_inc (&_use_count);
322 boost::shared_ptr<Source> sptr = shared_from_this();
323 SourcePropertyChanged (sptr);
325 /* no shared_ptr available, relax; */
330 Source::dec_use_count ()
333 gint oldval = g_atomic_int_add (&_use_count, -1);
335 cerr << "Bad use dec for " << name() << endl;
340 g_atomic_int_add (&_use_count, -1);
344 boost::shared_ptr<Source> sptr = shared_from_this();
345 SourcePropertyChanged (sptr);
347 /* no shared_ptr available, relax; */
352 Source::writable () const
354 return (_flags & Writable) && _session.writable();