Make audiographer SndfileWriter use PBD::Signal and use it properly. Also make export...
[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/debug.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 using namespace PBD;
49
50 Source::Source (Session& s, DataType type, const string& name, Flag flags)
51         : SessionObject(s, name)
52         , _type(type)
53         , _flags(flags)
54         , _timeline_position(0)
55 {
56         _analysed = false;
57         _timestamp = 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
70         if (set_state (node, Stateful::loading_state_version) || _type == DataType::NIL) {
71                 throw failed_constructor();
72         }
73
74         fix_writable_flags ();
75 }
76
77 Source::~Source ()
78 {
79         DEBUG_TRACE (DEBUG::Destruction, string_compose ("Source %1 destructor %2\n", _name, this));
80 }
81
82 void
83 Source::fix_writable_flags ()
84 {
85         if (!_session.writable()) {
86                 _flags = Flag (_flags & ~(Writable|Removable|RemovableIfEmpty|RemoveAtDestroy|CanRename));
87         }
88 }
89
90 XMLNode&
91 Source::get_state ()
92 {
93         XMLNode *node = new XMLNode ("Source");
94         char buf[64];
95
96         node->add_property ("name", name());
97         node->add_property ("type", _type.to_string());
98         node->add_property (X_("flags"), enum_2_string (_flags));
99         _id.print (buf, sizeof (buf));
100         node->add_property ("id", buf);
101
102         if (_timestamp != 0) {
103                 snprintf (buf, sizeof (buf), "%ld", _timestamp);
104                 node->add_property ("timestamp", buf);
105         }
106
107         return *node;
108 }
109
110 int
111 Source::set_state (const XMLNode& node, int /*version*/)
112 {
113         const XMLProperty* prop;
114
115         if ((prop = node.property ("name")) != 0) {
116                 _name = prop->value();
117         } else {
118                 return -1;
119         }
120
121         if ((prop = node.property ("id")) != 0) {
122                 _id = prop->value ();
123         } else {
124                 return -1;
125         }
126
127         if ((prop = node.property ("type")) != 0) {
128                 _type = DataType(prop->value());
129         }
130
131         if ((prop = node.property ("timestamp")) != 0) {
132                 sscanf (prop->value().c_str(), "%ld", &_timestamp);
133         }
134
135         if ((prop = node.property (X_("flags"))) != 0) {
136                 _flags = Flag (string_2_enum (prop->value(), _flags));
137         } else {
138                 _flags = Flag (0);
139
140         }
141
142         /* old style, from the period when we had DestructiveFileSource */
143         if ((prop = node.property (X_("destructive"))) != 0) {
144                 _flags = Flag (_flags | Destructive);
145         }
146
147         return 0;
148 }
149
150 bool
151 Source::has_been_analysed() const
152 {
153         Glib::Mutex::Lock lm (_analysis_lock);
154         return _analysed;
155 }
156
157 void
158 Source::set_been_analysed (bool yn)
159 {
160         {
161                 Glib::Mutex::Lock lm (_analysis_lock);
162                 _analysed = yn;
163         }
164
165         if (yn) {
166                 load_transients (get_transients_path());
167                 AnalysisChanged(); // EMIT SIGNAL
168         }
169 }
170
171 int
172 Source::load_transients (const string& path)
173 {
174         ifstream file (path.c_str());
175
176         if (!file) {
177                 return -1;
178         }
179
180         transients.clear ();
181
182         stringstream strstr;
183         double val;
184
185         while (file.good()) {
186                 file >> val;
187
188                 if (!file.fail()) {
189                         nframes64_t frame = (nframes64_t) floor (val * _session.frame_rate());
190                         transients.push_back (frame);
191                 }
192         }
193
194         return 0;
195 }
196
197 string
198 Source::get_transients_path () const
199 {
200         vector<string> parts;
201         string s;
202
203         /* old sessions may not have the analysis directory */
204
205         _session.ensure_subdirs ();
206
207         s = _session.analysis_dir ();
208         parts.push_back (s);
209
210         s = _id.to_s();
211         s += '.';
212         s += TransientDetector::operational_identifier();
213         parts.push_back (s);
214
215         return Glib::build_filename (parts);
216 }
217
218 bool
219 Source::check_for_analysis_data_on_disk ()
220 {
221         /* looks to see if the analysis files for this source are on disk.
222            if so, mark us already analysed.
223         */
224
225         string path = get_transients_path ();
226         bool ok = true;
227
228         if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
229                 ok = false;
230         }
231
232         // XXX add other tests here as appropriate
233
234         set_been_analysed (ok);
235         return ok;
236 }
237
238 void
239 Source::mark_for_remove ()
240 {
241         // This operation is not allowed for sources for destructive tracks or out-of-session files.
242
243         /* XXX need a way to detect _within_session() condition here - move it from FileSource? 
244          */
245
246         if ((_flags & Destructive)) { 
247                 return;
248         }
249
250         _flags = Flag (_flags | Removable | RemoveAtDestroy);
251 }
252
253 void
254 Source::set_timeline_position (int64_t pos)
255 {
256         _timeline_position = pos;
257 }
258
259 void
260 Source::set_allow_remove_if_empty (bool yn)
261 {
262         if (!writable()) {
263                 return;
264         }
265
266         if (yn) {
267                 _flags = Flag (_flags | RemovableIfEmpty);
268         } else {
269                 _flags = Flag (_flags & ~RemovableIfEmpty);
270         }
271 }
272