Various tweaks to the bundle manager.
[ardour.git] / libs / ardour / source.cc
1 /*
2     Copyright (C) 2000 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
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>
31
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"
38
39 #include "ardour/session.h"
40 #include "ardour/source.h"
41 #include "ardour/transient_detector.h"
42
43 #include "i18n.h"
44
45 using namespace std;
46 using namespace ARDOUR;
47
48 Source::Source (Session& s, DataType type, const string& name, Flag flags)
49         : SessionObject(s, name)
50         , _type(type)
51         , _flags(flags)
52         , _timeline_position(0)
53 {
54         _analysed = false;
55         _timestamp = 0;
56         fix_writable_flags ();
57 }
58
59 Source::Source (Session& s, const XMLNode& node)
60         : SessionObject(s, "unnamed source")
61         , _type(DataType::AUDIO)
62         , _flags (Flag (Writable|CanRename))
63         , _timeline_position(0)
64 {
65         _timestamp = 0;
66         _analysed = false;
67
68         if (set_state (node, Stateful::loading_state_version) || _type == DataType::NIL) {
69                 throw failed_constructor();
70         }
71
72         fix_writable_flags ();
73 }
74
75 Source::~Source ()
76 {
77         notify_callbacks ();
78 }
79
80
81 void
82 Source::fix_writable_flags ()
83 {
84         if (!_session.writable()) {
85                 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
86         }
87 }
88
89 XMLNode&
90 Source::get_state ()
91 {
92         XMLNode *node = new XMLNode ("Source");
93         char buf[64];
94
95         node->add_property ("name", name());
96         node->add_property ("type", _type.to_string());
97         node->add_property (X_("flags"), enum_2_string (_flags));
98         _id.print (buf, sizeof (buf));
99         node->add_property ("id", buf);
100
101         if (_timestamp != 0) {
102                 snprintf (buf, sizeof (buf), "%ld", _timestamp);
103                 node->add_property ("timestamp", buf);
104         }
105
106         return *node;
107 }
108
109 int
110 Source::set_state (const XMLNode& node, int /*version*/)
111 {
112         const XMLProperty* prop;
113
114         if ((prop = node.property ("name")) != 0) {
115                 _name = prop->value();
116         } else {
117                 return -1;
118         }
119
120         if ((prop = node.property ("id")) != 0) {
121                 _id = prop->value ();
122         } else {
123                 return -1;
124         }
125
126         if ((prop = node.property ("type")) != 0) {
127                 _type = DataType(prop->value());
128         }
129
130         if ((prop = node.property ("timestamp")) != 0) {
131                 sscanf (prop->value().c_str(), "%ld", &_timestamp);
132         }
133
134         if ((prop = node.property (X_("flags"))) != 0) {
135                 _flags = Flag (string_2_enum (prop->value(), _flags));
136         } else {
137                 _flags = Flag (0);
138
139         }
140
141         /* old style, from the period when we had DestructiveFileSource */
142         if ((prop = node.property (X_("destructive"))) != 0) {
143                 _flags = Flag (_flags | Destructive);
144         }
145
146         return 0;
147 }
148
149 bool
150 Source::has_been_analysed() const
151 {
152         Glib::Mutex::Lock lm (_analysis_lock);
153         return _analysed;
154 }
155
156 void
157 Source::set_been_analysed (bool yn)
158 {
159         {
160                 Glib::Mutex::Lock lm (_analysis_lock);
161                 _analysed = yn;
162         }
163
164         if (yn) {
165                 load_transients (get_transients_path());
166                 AnalysisChanged(); // EMIT SIGNAL
167         }
168 }
169
170 int
171 Source::load_transients (const string& path)
172 {
173         ifstream file (path.c_str());
174
175         if (!file) {
176                 return -1;
177         }
178
179         transients.clear ();
180
181         stringstream strstr;
182         double val;
183
184         while (file.good()) {
185                 file >> val;
186
187                 if (!file.fail()) {
188                         nframes64_t frame = (nframes64_t) floor (val * _session.frame_rate());
189                         transients.push_back (frame);
190                 }
191         }
192
193         return 0;
194 }
195
196 string
197 Source::get_transients_path () const
198 {
199         vector<string> parts;
200         string s;
201
202         /* old sessions may not have the analysis directory */
203
204         _session.ensure_subdirs ();
205
206         s = _session.analysis_dir ();
207         parts.push_back (s);
208
209         s = _id.to_s();
210         s += '.';
211         s += TransientDetector::operational_identifier();
212         parts.push_back (s);
213
214         return Glib::build_filename (parts);
215 }
216
217 bool
218 Source::check_for_analysis_data_on_disk ()
219 {
220         /* looks to see if the analysis files for this source are on disk.
221            if so, mark us already analysed.
222         */
223
224         string path = get_transients_path ();
225         bool ok = true;
226
227         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
228                 ok = false;
229         }
230
231         // XXX add other tests here as appropriate
232
233         set_been_analysed (ok);
234         return ok;
235 }
236
237 void
238 Source::mark_for_remove ()
239 {
240         // This operation is not allowed for sources for destructive tracks or embedded files.
241         // Fortunately mark_for_remove() is never called for embedded files. This function
242         // must be fixed if that ever happens.
243         if (_flags & Destructive) {
244                 return;
245         }
246
247         _flags = Flag (_flags | Removable | RemoveAtDestroy);
248 }
249
250 void
251 Source::set_timeline_position (int64_t pos)
252 {
253         _timeline_position = pos;
254 }
255
256 void
257 Source::set_allow_remove_if_empty (bool yn)
258 {
259         if (!writable()) {
260                 return;
261         }
262
263         if (yn) {
264                 _flags = Flag (_flags | RemovableIfEmpty);
265         } else {
266                 _flags = Flag (_flags & ~RemovableIfEmpty);
267         }
268 }
269