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