Trim include dependency graph, especially for io.h and session.h.
[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
38 #include <ardour/source.h>
39 #include <ardour/playlist.h>
40 #include <ardour/session.h>
41 #include <ardour/transient_detector.h>
42
43 #include "i18n.h"
44
45 using namespace std;
46 using namespace ARDOUR;
47
48 Source::Source (Session& s, const string& name, DataType type)
49         : SessionObject(s, name)
50         , _type(type)
51 {
52         // not true.. is this supposed to be an assertion?
53         //assert(_name.find("/") == string::npos);
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 {
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         _id.print (buf, sizeof (buf));
89         node->add_property ("id", buf);
90
91         if (_timestamp != 0) {
92                 snprintf (buf, sizeof (buf), "%ld", _timestamp);
93                 node->add_property ("timestamp", buf);
94         }
95
96         return *node;
97 }
98
99 int
100 Source::set_state (const XMLNode& node)
101 {
102         const XMLProperty* prop;
103
104         if ((prop = node.property ("name")) != 0) {
105                 _name = prop->value();
106         } else {
107                 return -1;
108         }
109         
110         if ((prop = node.property ("id")) != 0) {
111                 _id = prop->value ();
112         } else {
113                 return -1;
114         }
115
116         if ((prop = node.property ("type")) != 0) {
117                 _type = DataType(prop->value());
118         }
119
120         if ((prop = node.property ("timestamp")) != 0) {
121                 sscanf (prop->value().c_str(), "%ld", &_timestamp);
122         }
123         
124         // Don't think this is valid, absolute paths fail
125         //assert(_name.find("/") == string::npos);
126
127         return 0;
128 }
129
130 void
131 Source::update_length (nframes_t pos, nframes_t cnt)
132 {
133         if (pos + cnt > _length) {
134                 _length = pos+cnt;
135         }
136 }
137
138 void
139 Source::add_playlist (boost::shared_ptr<Playlist> pl)
140 {
141         std::pair<PlaylistMap::iterator,bool> res;
142         std::pair<boost::shared_ptr<Playlist>, uint32_t> newpair (pl, 1);
143         Glib::Mutex::Lock lm (_playlist_lock);
144
145         res = _playlists.insert (newpair);
146
147         if (!res.second) {
148                 /* it already existed, bump count */
149                 res.first->second++;
150         }
151                 
152         pl->GoingAway.connect (bind (mem_fun (*this, &Source::remove_playlist), boost::weak_ptr<Playlist> (pl)));
153 }
154
155 void
156 Source::remove_playlist (boost::weak_ptr<Playlist> wpl)
157 {
158         boost::shared_ptr<Playlist> pl (wpl.lock());
159
160         if (!pl) {
161                 return;
162         }
163
164         PlaylistMap::iterator x;
165         Glib::Mutex::Lock lm (_playlist_lock);
166
167         if ((x = _playlists.find (pl)) != _playlists.end()) {
168                 if (x->second > 1) {
169                         x->second--;
170                 } else {
171                         _playlists.erase (x);
172                 }
173         }
174 }
175
176 uint32_t
177 Source::used () const
178 {
179         return _playlists.size();
180 }
181
182 bool
183 Source::has_been_analysed() const
184 {
185         Glib::Mutex::Lock lm (_analysis_lock);
186         return _analysed;
187 }
188
189 void
190 Source::set_been_analysed (bool yn)
191 {
192         {
193                 Glib::Mutex::Lock lm (_analysis_lock);
194                 _analysed = yn;
195         }
196         
197         if (yn) {
198                 load_transients (get_transients_path());
199                 AnalysisChanged(); // EMIT SIGNAL
200         }
201 }
202
203 int
204 Source::load_transients (const string& path)
205 {
206         ifstream file (path.c_str());
207
208         if (!file) {
209                 return -1;
210         }
211         
212         transients.clear ();
213
214         stringstream strstr;
215         double val;
216
217         while (file.good()) {
218                 file >> val;
219
220                 if (!file.fail()) {
221                         nframes64_t frame = (nframes64_t) floor (val * _session.frame_rate());
222                         transients.push_back (frame);
223                 }
224         }
225
226         return 0;
227 }
228
229 string 
230 Source::get_transients_path () const
231 {
232         vector<string> parts;
233         string s;
234
235         /* old sessions may not have the analysis directory */
236         
237         _session.ensure_subdirs ();
238
239         s = _session.analysis_dir ();
240         parts.push_back (s);
241
242         s = _id.to_s();
243         s += '.';
244         s += TransientDetector::operational_identifier();
245         parts.push_back (s);
246         
247         return Glib::build_filename (parts);
248 }
249
250 bool
251 Source::check_for_analysis_data_on_disk () 
252 {
253         /* looks to see if the analysis files for this source are on disk.
254            if so, mark us already analysed.
255         */
256
257         string path = get_transients_path ();
258         bool ok = true;
259
260         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
261                 ok = false;
262         }
263
264         // XXX add other tests here as appropriate
265
266         set_been_analysed (ok);
267         return ok;
268 }
269