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.
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"
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"
48 DEFINE_ENUM_CONVERT(ARDOUR::Source::Flag);
52 using namespace ARDOUR;
55 Source::Source (Session& s, DataType type, const string& name, Flag flags)
56 : SessionObject(s, name)
59 , _timeline_position(0)
65 fix_writable_flags ();
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)
79 if (set_state (node, Stateful::loading_state_version) || _type == DataType::NIL) {
80 throw failed_constructor();
83 fix_writable_flags ();
88 DEBUG_TRACE (DEBUG::Destruction, string_compose ("Source %1 destructor %2\n", _name, this));
92 Source::fix_writable_flags ()
94 if (!_session.writable()) {
95 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
102 XMLNode *node = new XMLNode ("Source");
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());
109 if (_timestamp != 0) {
110 node->set_property ("timestamp", (int64_t)_timestamp);
117 Source::set_state (const XMLNode& node, int version)
120 if (node.get_property ("name", str)) {
126 if (!set_id (node)) {
130 node.get_property ("type", _type);
133 if (node.get_property ("timestamp", t)) {
137 if (!node.get_property (X_("flags"), _flags)) {
141 /* old style, from the period when we had DestructiveFileSource */
142 if (node.get_property (X_("destructive"), str)) {
143 _flags = Flag (_flags | Destructive);
146 if (Profile->get_trx() && (_flags & Destructive)) {
147 error << string_compose (_("%1: this session uses destructive tracks, which are not supported"), PROGRAM_NAME) << endmsg;
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.
156 if (!(_flags & Destructive)) {
157 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
165 Source::has_been_analysed() const
167 Glib::Threads::Mutex::Lock lm (_analysis_lock);
172 Source::set_been_analysed (bool yn)
175 if (0 == load_transients (get_transients_path())) {
179 if (yn != _analysed) {
180 Glib::Threads::Mutex::Lock lm (_analysis_lock);
183 AnalysisChanged(); // EMIT SIGNAL
187 Source::load_transients (const string& path)
191 if (! (tf = g_fopen (path.c_str (), "rb"))) {
196 while (!feof (tf) && !ferror(tf)) {
198 if (1 != fscanf (tf, "%lf", &val)) {
203 samplepos_t sample = (samplepos_t) floor (val * _session.sample_rate());
204 transients.push_back (sample);
212 Source::get_transients_path () const
214 vector<string> parts;
217 /* old sessions may not have the analysis directory */
219 _session.ensure_subdirs ();
221 s = _session.analysis_dir ();
226 s += TransientDetector::operational_identifier();
229 return Glib::build_filename (parts);
233 Source::check_for_analysis_data_on_disk ()
235 /* looks to see if the analysis files for this source are on disk.
236 if so, mark us already analysed.
239 string path = get_transients_path ();
242 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
246 // XXX add other tests here as appropriate
248 set_been_analysed (ok);
253 Source::mark_for_remove ()
255 // This operation is not allowed for sources for destructive tracks or out-of-session files.
257 /* XXX need a way to detect _within_session() condition here - move it from FileSource?
260 if ((_flags & Destructive)) {
264 _flags = Flag (_flags | Removable | RemoveAtDestroy);
268 Source::set_timeline_position (samplepos_t pos)
270 _timeline_position = pos;
274 Source::set_allow_remove_if_empty (bool yn)
281 _flags = Flag (_flags | RemovableIfEmpty);
283 _flags = Flag (_flags & ~RemovableIfEmpty);
288 Source::inc_use_count ()
290 g_atomic_int_inc (&_use_count);
294 Source::dec_use_count ()
297 gint oldval = g_atomic_int_add (&_use_count, -1);
299 cerr << "Bad use dec for " << name() << endl;
304 g_atomic_int_add (&_use_count, -1);
309 Source::writable () const
311 return (_flags & Writable) && _session.writable();