2 Copyright (C) 2000 Paul Davis
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.
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.
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.
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"
37 #include "ardour/debug.h"
38 #include "ardour/session.h"
39 #include "ardour/source.h"
40 #include "ardour/transient_detector.h"
45 using namespace ARDOUR;
48 Source::Source (Session& s, DataType type, const string& name, Flag flags)
49 : SessionObject(s, name)
52 , _timeline_position(0)
58 fix_writable_flags ();
61 Source::Source (Session& s, const XMLNode& node)
62 : SessionObject(s, "unnamed source")
63 , _type(DataType::AUDIO)
64 , _flags (Flag (Writable|CanRename))
65 , _timeline_position(0)
72 if (set_state (node, Stateful::loading_state_version) || _type == DataType::NIL) {
73 throw failed_constructor();
76 fix_writable_flags ();
81 DEBUG_TRACE (DEBUG::Destruction, string_compose ("Source %1 destructor %2\n", _name, this));
85 Source::fix_writable_flags ()
87 if (!_session.writable()) {
88 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
95 XMLNode *node = new XMLNode ("Source");
98 node->add_property ("name", name());
99 node->add_property ("type", _type.to_string());
100 node->add_property (X_("flags"), enum_2_string (_flags));
101 id().print (buf, sizeof (buf));
102 node->add_property ("id", buf);
104 if (_timestamp != 0) {
105 snprintf (buf, sizeof (buf), "%ld", _timestamp);
106 node->add_property ("timestamp", buf);
113 Source::set_state (const XMLNode& node, int version)
115 const XMLProperty* prop;
117 if ((prop = node.property ("name")) != 0) {
118 _name = prop->value();
123 if (!set_id (node)) {
127 if ((prop = node.property ("type")) != 0) {
128 _type = DataType(prop->value());
131 if ((prop = node.property ("timestamp")) != 0) {
132 sscanf (prop->value().c_str(), "%ld", &_timestamp);
135 if ((prop = node.property (X_("flags"))) != 0) {
136 _flags = Flag (string_2_enum (prop->value(), _flags));
142 /* old style, from the period when we had DestructiveFileSource */
143 if ((prop = node.property (X_("destructive"))) != 0) {
144 _flags = Flag (_flags | Destructive);
147 if (version < 3000) {
148 /* a source with an XML node must necessarily already exist,
149 and therefore cannot be removable/writable etc. etc.; 2.X
150 sometimes marks sources as removable which shouldn't be.
152 if (!(_flags & Destructive)) {
153 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
161 Source::has_been_analysed() const
163 Glib::Threads::Mutex::Lock lm (_analysis_lock);
168 Source::set_been_analysed (bool yn)
171 Glib::Threads::Mutex::Lock lm (_analysis_lock);
176 load_transients (get_transients_path());
177 AnalysisChanged(); // EMIT SIGNAL
182 Source::load_transients (const string& path)
184 ifstream file (path.c_str());
195 while (file.good()) {
199 framepos_t frame = (framepos_t) floor (val * _session.frame_rate());
200 transients.push_back (frame);
208 Source::get_transients_path () const
210 vector<string> parts;
213 /* old sessions may not have the analysis directory */
215 _session.ensure_subdirs ();
217 s = _session.analysis_dir ();
222 s += TransientDetector::operational_identifier();
225 return Glib::build_filename (parts);
229 Source::check_for_analysis_data_on_disk ()
231 /* looks to see if the analysis files for this source are on disk.
232 if so, mark us already analysed.
235 string path = get_transients_path ();
238 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
242 // XXX add other tests here as appropriate
244 set_been_analysed (ok);
249 Source::mark_for_remove ()
251 // This operation is not allowed for sources for destructive tracks or out-of-session files.
253 /* XXX need a way to detect _within_session() condition here - move it from FileSource?
256 if ((_flags & Destructive)) {
260 _flags = Flag (_flags | Removable | RemoveAtDestroy);
264 Source::set_timeline_position (framepos_t pos)
266 _timeline_position = pos;
270 Source::set_allow_remove_if_empty (bool yn)
277 _flags = Flag (_flags | RemovableIfEmpty);
279 _flags = Flag (_flags & ~RemovableIfEmpty);
284 Source::inc_use_count ()
286 g_atomic_int_inc (&_use_count);
290 Source::dec_use_count ()
293 gint oldval = g_atomic_int_add (&_use_count, -1);
295 cerr << "Bad use dec for " << name() << endl;
300 g_atomic_int_add (&_use_count, -1);
305 Source::writable () const
307 return (_flags & Writable) && _session.writable();