Move duplicated AudioFileSource::Flags and SMFSource::Flags into Source.
[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, const string& name, DataType type, Flag flags)
50         : SessionObject(s, name)
51         , _type(type)
52         , _flags(flags)
53 {
54         _analysed = false;
55         _timestamp = 0;
56         _length = 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 {
65         _timestamp = 0;
66         _length = 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         return 0;
133 }
134
135 void
136 Source::update_length (nframes_t pos, nframes_t cnt)
137 {
138         if (pos + cnt > _length) {
139                 _length = pos+cnt;
140         }
141 }
142
143 void
144 Source::add_playlist (boost::shared_ptr<Playlist> pl)
145 {
146         std::pair<PlaylistMap::iterator,bool> res;
147         std::pair<boost::shared_ptr<Playlist>, uint32_t> newpair (pl, 1);
148         Glib::Mutex::Lock lm (_playlist_lock);
149
150         res = _playlists.insert (newpair);
151
152         if (!res.second) {
153                 /* it already existed, bump count */
154                 res.first->second++;
155         }
156                 
157         pl->GoingAway.connect (bind (mem_fun (*this, &Source::remove_playlist), boost::weak_ptr<Playlist> (pl)));
158 }
159
160 void
161 Source::remove_playlist (boost::weak_ptr<Playlist> wpl)
162 {
163         boost::shared_ptr<Playlist> pl (wpl.lock());
164
165         if (!pl) {
166                 return;
167         }
168
169         PlaylistMap::iterator x;
170         Glib::Mutex::Lock lm (_playlist_lock);
171
172         if ((x = _playlists.find (pl)) != _playlists.end()) {
173                 if (x->second > 1) {
174                         x->second--;
175                 } else {
176                         _playlists.erase (x);
177                 }
178         }
179 }
180
181 uint32_t
182 Source::used () const
183 {
184         return _playlists.size();
185 }
186
187 bool
188 Source::has_been_analysed() const
189 {
190         Glib::Mutex::Lock lm (_analysis_lock);
191         return _analysed;
192 }
193
194 void
195 Source::set_been_analysed (bool yn)
196 {
197         {
198                 Glib::Mutex::Lock lm (_analysis_lock);
199                 _analysed = yn;
200         }
201         
202         if (yn) {
203                 load_transients (get_transients_path());
204                 AnalysisChanged(); // EMIT SIGNAL
205         }
206 }
207
208 int
209 Source::load_transients (const string& path)
210 {
211         ifstream file (path.c_str());
212
213         if (!file) {
214                 return -1;
215         }
216         
217         transients.clear ();
218
219         stringstream strstr;
220         double val;
221
222         while (file.good()) {
223                 file >> val;
224
225                 if (!file.fail()) {
226                         nframes64_t frame = (nframes64_t) floor (val * _session.frame_rate());
227                         transients.push_back (frame);
228                 }
229         }
230
231         return 0;
232 }
233
234 string 
235 Source::get_transients_path () const
236 {
237         vector<string> parts;
238         string s;
239
240         /* old sessions may not have the analysis directory */
241         
242         _session.ensure_subdirs ();
243
244         s = _session.analysis_dir ();
245         parts.push_back (s);
246
247         s = _id.to_s();
248         s += '.';
249         s += TransientDetector::operational_identifier();
250         parts.push_back (s);
251         
252         return Glib::build_filename (parts);
253 }
254
255 bool
256 Source::check_for_analysis_data_on_disk () 
257 {
258         /* looks to see if the analysis files for this source are on disk.
259            if so, mark us already analysed.
260         */
261
262         string path = get_transients_path ();
263         bool ok = true;
264
265         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
266                 ok = false;
267         }
268
269         // XXX add other tests here as appropriate
270
271         set_been_analysed (ok);
272         return ok;
273 }
274