12a5e7c071461e72c646ee0d68eaac86fc02b654
[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/playlist.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         _length = 0;
58         _in_use = 0;
59 }
60
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)
66 {
67         _timestamp = 0;
68         _length = 0;
69         _analysed = false;
70         _in_use = 0;
71
72         if (set_state (node) || _type == DataType::NIL) {
73                 throw failed_constructor();
74         }
75 }
76
77 Source::~Source ()
78 {
79         notify_callbacks ();
80 }
81
82 XMLNode&
83 Source::get_state ()
84 {
85         XMLNode *node = new XMLNode ("Source");
86         char buf[64];
87
88         node->add_property ("name", name());
89         node->add_property ("type", _type.to_string());
90         node->add_property (X_("flags"), enum_2_string (_flags));
91         _id.print (buf, sizeof (buf));
92         node->add_property ("id", buf);
93
94         if (_timestamp != 0) {
95                 snprintf (buf, sizeof (buf), "%ld", _timestamp);
96                 node->add_property ("timestamp", buf);
97         }
98
99         return *node;
100 }
101
102 int
103 Source::set_state (const XMLNode& node)
104 {
105         const XMLProperty* prop;
106
107         if ((prop = node.property ("name")) != 0) {
108                 _name = prop->value();
109         } else {
110                 return -1;
111         }
112         
113         if ((prop = node.property ("id")) != 0) {
114                 _id = prop->value ();
115         } else {
116                 return -1;
117         }
118
119         if ((prop = node.property ("type")) != 0) {
120                 _type = DataType(prop->value());
121         }
122
123         if ((prop = node.property ("timestamp")) != 0) {
124                 sscanf (prop->value().c_str(), "%ld", &_timestamp);
125         }
126         
127         if ((prop = node.property (X_("flags"))) != 0) {
128                 _flags = Flag (string_2_enum (prop->value(), _flags));
129         } else {
130                 _flags = Flag (0);
131
132         }
133         
134         /* old style, from the period when we had DestructiveFileSource */
135         if ((prop = node.property (X_("destructive"))) != 0) {
136                 _flags = Flag (_flags | Destructive);
137         }
138
139         return 0;
140 }
141
142 void
143 Source::update_length (nframes_t pos, nframes_t cnt)
144 {
145         if (pos + cnt > _length) {
146                 _length = pos + cnt;
147         }
148 }
149
150 void
151 Source::add_playlist (boost::shared_ptr<Playlist> pl)
152 {
153         std::pair<PlaylistMap::iterator,bool> res;
154         std::pair<boost::shared_ptr<Playlist>, uint32_t> newpair (pl, 1);
155         Glib::Mutex::Lock lm (_playlist_lock);
156
157         res = _playlists.insert (newpair);
158
159         if (!res.second) {
160                 /* it already existed, bump count */
161                 res.first->second++;
162         }
163                 
164         pl->GoingAway.connect (bind (
165                         mem_fun (*this, &Source::remove_playlist),
166                         boost::weak_ptr<Playlist> (pl)));
167 }
168
169 void
170 Source::remove_playlist (boost::weak_ptr<Playlist> wpl)
171 {
172         boost::shared_ptr<Playlist> pl (wpl.lock());
173
174         if (!pl) {
175                 return;
176         }
177
178         PlaylistMap::iterator x;
179         Glib::Mutex::Lock lm (_playlist_lock);
180
181         if ((x = _playlists.find (pl)) != _playlists.end()) {
182                 if (x->second > 1) {
183                         x->second--;
184                 } else {
185                         _playlists.erase (x);
186                 }
187         }
188 }
189
190 uint32_t
191 Source::used () const
192 {
193         return _playlists.size();
194 }
195
196 bool
197 Source::has_been_analysed() const
198 {
199         Glib::Mutex::Lock lm (_analysis_lock);
200         return _analysed;
201 }
202
203 void
204 Source::set_been_analysed (bool yn)
205 {
206         {
207                 Glib::Mutex::Lock lm (_analysis_lock);
208                 _analysed = yn;
209         }
210         
211         if (yn) {
212                 load_transients (get_transients_path());
213                 AnalysisChanged(); // EMIT SIGNAL
214         }
215 }
216
217 int
218 Source::load_transients (const string& path)
219 {
220         ifstream file (path.c_str());
221
222         if (!file) {
223                 return -1;
224         }
225         
226         transients.clear ();
227
228         stringstream strstr;
229         double val;
230
231         while (file.good()) {
232                 file >> val;
233
234                 if (!file.fail()) {
235                         nframes64_t frame = (nframes64_t) floor (val * _session.frame_rate());
236                         transients.push_back (frame);
237                 }
238         }
239
240         return 0;
241 }
242
243 string 
244 Source::get_transients_path () const
245 {
246         vector<string> parts;
247         string s;
248
249         /* old sessions may not have the analysis directory */
250         
251         _session.ensure_subdirs ();
252
253         s = _session.analysis_dir ();
254         parts.push_back (s);
255
256         s = _id.to_s();
257         s += '.';
258         s += TransientDetector::operational_identifier();
259         parts.push_back (s);
260         
261         return Glib::build_filename (parts);
262 }
263
264 bool
265 Source::check_for_analysis_data_on_disk () 
266 {
267         /* looks to see if the analysis files for this source are on disk.
268            if so, mark us already analysed.
269         */
270
271         string path = get_transients_path ();
272         bool ok = true;
273
274         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
275                 ok = false;
276         }
277
278         // XXX add other tests here as appropriate
279
280         set_been_analysed (ok);
281         return ok;
282 }
283
284 void
285 Source::mark_for_remove ()
286 {
287         // This operation is not allowed for sources for destructive tracks or embedded files.
288         // Fortunately mark_for_remove() is never called for embedded files. This function
289         // must be fixed if that ever happens.
290         if (_flags & Destructive) {
291                 return;
292         }
293
294         _flags = Flag (_flags | Removable | RemoveAtDestroy);
295 }
296
297 void
298 Source::set_timeline_position (int64_t pos)
299 {
300         _timeline_position = pos;
301 }
302
303 void
304 Source::set_allow_remove_if_empty (bool yn)
305 {
306         if (!writable()) {
307                 return;
308         }
309
310         if (yn) {
311                 _flags = Flag (_flags | RemovableIfEmpty);
312         } else {
313                 _flags = Flag (_flags & ~RemovableIfEmpty);
314         }
315 }
316