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