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