Move panner bypass state up to the PannerShell so that it is preserved even when...
[ardour2.git] / libs / ardour / source.cc
blob35e1417fb4c14e92abc5a8af7e7b31199b1653c3
1 /*
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.
20 #include <sys/stat.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <poll.h>
24 #include <float.h>
25 #include <cerrno>
26 #include <ctime>
27 #include <cmath>
28 #include <iomanip>
29 #include <algorithm>
30 #include <fstream>
32 #include <glibmm/thread.h>
33 #include <glibmm/miscutils.h>
34 #include <glibmm/fileutils.h>
35 #include "pbd/xml++.h"
36 #include "pbd/pthread_utils.h"
37 #include "pbd/enumwriter.h"
39 #include "ardour/debug.h"
40 #include "ardour/session.h"
41 #include "ardour/source.h"
42 #include "ardour/transient_detector.h"
44 #include "i18n.h"
46 using namespace std;
47 using namespace ARDOUR;
48 using namespace PBD;
50 Source::Source (Session& s, DataType type, const string& name, Flag flags)
51 : SessionObject(s, name)
52 , _type(type)
53 , _flags(flags)
54 , _timeline_position(0)
55 , _use_count (0)
56 , _level (0)
58 _analysed = false;
59 _timestamp = 0;
60 fix_writable_flags ();
63 Source::Source (Session& s, const XMLNode& node)
64 : SessionObject(s, "unnamed source")
65 , _type(DataType::AUDIO)
66 , _flags (Flag (Writable|CanRename))
67 , _timeline_position(0)
68 , _use_count (0)
69 , _level (0)
71 _timestamp = 0;
72 _analysed = false;
74 if (set_state (node, Stateful::loading_state_version) || _type == DataType::NIL) {
75 throw failed_constructor();
78 fix_writable_flags ();
81 Source::~Source ()
83 DEBUG_TRACE (DEBUG::Destruction, string_compose ("Source %1 destructor %2\n", _name, this));
86 void
87 Source::fix_writable_flags ()
89 if (!_session.writable()) {
90 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
94 XMLNode&
95 Source::get_state ()
97 XMLNode *node = new XMLNode ("Source");
98 char buf[64];
100 node->add_property ("name", name());
101 node->add_property ("type", _type.to_string());
102 node->add_property (X_("flags"), enum_2_string (_flags));
103 _id.print (buf, sizeof (buf));
104 node->add_property ("id", buf);
106 if (_timestamp != 0) {
107 snprintf (buf, sizeof (buf), "%ld", _timestamp);
108 node->add_property ("timestamp", buf);
111 return *node;
115 Source::set_state (const XMLNode& node, int version)
117 const XMLProperty* prop;
119 if ((prop = node.property ("name")) != 0) {
120 _name = prop->value();
121 } else {
122 return -1;
125 if ((prop = node.property ("id")) != 0) {
126 _id = prop->value ();
127 } else {
128 return -1;
131 if ((prop = node.property ("type")) != 0) {
132 _type = DataType(prop->value());
135 if ((prop = node.property ("timestamp")) != 0) {
136 sscanf (prop->value().c_str(), "%ld", &_timestamp);
139 if ((prop = node.property (X_("flags"))) != 0) {
140 _flags = Flag (string_2_enum (prop->value(), _flags));
141 } else {
142 _flags = Flag (0);
146 /* old style, from the period when we had DestructiveFileSource */
147 if ((prop = node.property (X_("destructive"))) != 0) {
148 _flags = Flag (_flags | Destructive);
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));
161 return 0;
164 bool
165 Source::has_been_analysed() const
167 Glib::Mutex::Lock lm (_analysis_lock);
168 return _analysed;
171 void
172 Source::set_been_analysed (bool yn)
175 Glib::Mutex::Lock lm (_analysis_lock);
176 _analysed = yn;
179 if (yn) {
180 load_transients (get_transients_path());
181 AnalysisChanged(); // EMIT SIGNAL
186 Source::load_transients (const string& path)
188 ifstream file (path.c_str());
190 if (!file) {
191 return -1;
194 transients.clear ();
196 stringstream strstr;
197 double val;
199 while (file.good()) {
200 file >> val;
202 if (!file.fail()) {
203 framepos_t frame = (framepos_t) floor (val * _session.frame_rate());
204 transients.push_back (frame);
208 return 0;
211 string
212 Source::get_transients_path () const
214 vector<string> parts;
215 string s;
217 /* old sessions may not have the analysis directory */
219 _session.ensure_subdirs ();
221 s = _session.analysis_dir ();
222 parts.push_back (s);
224 s = _id.to_s();
225 s += '.';
226 s += TransientDetector::operational_identifier();
227 parts.push_back (s);
229 return Glib::build_filename (parts);
232 bool
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 ();
240 bool ok = true;
242 if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
243 ok = false;
246 // XXX add other tests here as appropriate
248 set_been_analysed (ok);
249 return ok;
252 void
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)) {
261 return;
264 _flags = Flag (_flags | Removable | RemoveAtDestroy);
267 void
268 Source::set_timeline_position (int64_t pos)
270 _timeline_position = pos;
273 void
274 Source::set_allow_remove_if_empty (bool yn)
276 if (!writable()) {
277 return;
280 if (yn) {
281 _flags = Flag (_flags | RemovableIfEmpty);
282 } else {
283 _flags = Flag (_flags & ~RemovableIfEmpty);
287 void
288 Source::inc_use_count ()
290 g_atomic_int_inc (&_use_count);
293 void
294 Source::dec_use_count ()
296 #ifndef NDEBUG
297 gint oldval = g_atomic_int_exchange_and_add (&_use_count, -1);
298 if (oldval <= 0) {
299 cerr << "Bad use dec for " << name() << endl;
300 abort ();
302 assert (oldval > 0);
303 #else
304 g_atomic_int_exchange_and_add (&_use_count, -1);
305 #endif
308 bool
309 Source::writable () const
311 return (_flags & Writable) && _session.writable();