Add option to limit automatable control parmaters
[ardour.git] / libs / ardour / source_factory.cc
1 /*
2     Copyright (C) 2000-2006 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     $Id$
19 */
20
21 #ifdef WAF_BUILD
22 #include "libardour-config.h"
23 #endif
24
25 #include "pbd/error.h"
26 #include "pbd/convert.h"
27 #include "pbd/pthread_utils.h"
28 #include "pbd/stacktrace.h"
29
30 #include "ardour/audioplaylist.h"
31 #include "ardour/audio_playlist_source.h"
32 #include "ardour/boost_debug.h"
33 #include "ardour/midi_playlist.h"
34 #include "ardour/midi_playlist_source.h"
35 #include "ardour/source.h"
36 #include "ardour/source_factory.h"
37 #include "ardour/sndfilesource.h"
38 #include "ardour/silentfilesource.h"
39 #include "ardour/smf_source.h"
40 #include "ardour/session.h"
41
42 #ifdef  HAVE_COREAUDIO
43 #include "ardour/coreaudiosource.h"
44 #endif
45
46
47 #include "pbd/i18n.h"
48
49 using namespace ARDOUR;
50 using namespace std;
51 using namespace PBD;
52
53 PBD::Signal1<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
54 Glib::Threads::Cond SourceFactory::PeaksToBuild;
55 Glib::Threads::Mutex SourceFactory::peak_building_lock;
56 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
57
58 static int active_threads = 0;
59
60 static void
61 peak_thread_work ()
62 {
63         SessionEvent::create_per_thread_pool (X_("PeakFile Builder "), 64);
64
65         while (true) {
66
67                 SourceFactory::peak_building_lock.lock ();
68
69           wait:
70                 if (SourceFactory::files_with_peaks.empty()) {
71                         SourceFactory::PeaksToBuild.wait (SourceFactory::peak_building_lock);
72                 }
73
74                 if (SourceFactory::files_with_peaks.empty()) {
75                         goto wait;
76                 }
77
78                 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
79                 SourceFactory::files_with_peaks.pop_front ();
80                 ++active_threads;
81                 SourceFactory::peak_building_lock.unlock ();
82
83                 if (!as) {
84                         continue;
85                 }
86
87                 as->setup_peakfile ();
88                 SourceFactory::peak_building_lock.lock ();
89                 --active_threads;
90                 SourceFactory::peak_building_lock.unlock ();
91         }
92 }
93
94 int
95 SourceFactory::peak_work_queue_length ()
96 {
97         // ideally we'd loop over the queue and check for duplicates
98         // and existing valid peak-files..
99         return SourceFactory::files_with_peaks.size () + active_threads;
100 }
101
102 void
103 SourceFactory::init ()
104 {
105         for (int n = 0; n < 2; ++n) {
106                 Glib::Threads::Thread::create (sigc::ptr_fun (::peak_thread_work));
107         }
108 }
109
110 int
111 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
112 {
113         boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
114
115         if (as) {
116
117                 // immediately set 'peakfile-path' for empty and NoPeakFile sources
118                 if (async && !as->empty() && !(as->flags() & Source::NoPeakFile)) {
119
120                         Glib::Threads::Mutex::Lock lm (peak_building_lock);
121                         files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
122                         PeaksToBuild.broadcast ();
123
124                 } else {
125
126                         if (as->setup_peakfile ()) {
127                                 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
128                                 return -1;
129                         }
130                 }
131         }
132
133         return 0;
134 }
135
136 boost::shared_ptr<Source>
137 SourceFactory::createSilent (Session& s, const XMLNode& node, samplecnt_t nframes, float sr)
138 {
139         Source* src = new SilentFileSource (s, node, nframes, sr);
140 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
141         // boost_debug_shared_ptr_mark_interesting (src, "Source");
142 #endif
143         boost::shared_ptr<Source> ret (src);
144         // no analysis data - the file is non-existent
145         SourceCreated (ret);
146         return ret;
147 }
148
149 boost::shared_ptr<Source>
150 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
151 {
152         DataType type = DataType::AUDIO;
153         XMLProperty const * prop = node.property("type");
154
155         if (prop) {
156                 type = DataType (prop->value());
157         }
158
159         if (type == DataType::AUDIO) {
160
161                 /* it could be nested */
162
163                 if (node.property ("playlist") != 0) {
164
165                         try {
166                                 boost::shared_ptr<AudioPlaylistSource> ap (new AudioPlaylistSource (s, node));
167
168                                 if (setup_peakfile (ap, true)) {
169                                         return boost::shared_ptr<Source>();
170                                 }
171
172                                 ap->check_for_analysis_data_on_disk ();
173
174                                 SourceCreated (ap);
175                                 return ap;
176
177                         } catch (failed_constructor&) {
178                                 /* oh well, so much for that then ... */
179                         }
180
181                 } else {
182
183
184                         try {
185                                 Source* src = new SndFileSource (s, node);
186 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
187                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
188 #endif
189                                 boost::shared_ptr<Source> ret (src);
190                                 if (setup_peakfile (ret, defer_peaks)) {
191                                         return boost::shared_ptr<Source>();
192                                 }
193                                 ret->check_for_analysis_data_on_disk ();
194                                 SourceCreated (ret);
195                                 return ret;
196                         }
197
198                         catch (failed_constructor& err) {
199
200 #ifdef HAVE_COREAUDIO
201
202                                 /* this is allowed to throw */
203
204                                 Source *src = new CoreAudioSource (s, node);
205 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
206                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
207 #endif
208                                 boost::shared_ptr<Source> ret (src);
209
210                                 if (setup_peakfile (ret, defer_peaks)) {
211                                         return boost::shared_ptr<Source>();
212                                 }
213
214                                 ret->check_for_analysis_data_on_disk ();
215                                 SourceCreated (ret);
216                                 return ret;
217 #else
218                                 throw; // rethrow
219 #endif
220                         }
221                 }
222         } else if (type == DataType::MIDI) {
223                 try {
224                         boost::shared_ptr<SMFSource> src (new SMFSource (s, node));
225                         Source::Lock lock(src->mutex());
226                         src->load_model (lock, true);
227 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
228                         // boost_debug_shared_ptr_mark_interesting (src, "Source");
229 #endif
230                         src->check_for_analysis_data_on_disk ();
231                         SourceCreated (src);
232                         return src;
233                 } catch (...) {
234                 }
235         }
236
237         return boost::shared_ptr<Source>();
238 }
239
240 boost::shared_ptr<Source>
241 SourceFactory::createExternal (DataType type, Session& s, const string& path,
242                                int chn, Source::Flag flags, bool announce, bool defer_peaks)
243 {
244         if (type == DataType::AUDIO) {
245
246                 if (!(flags & Destructive)) {
247
248                         try {
249
250                                 Source* src = new SndFileSource (s, path, chn, flags);
251 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
252                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
253 #endif
254                                 boost::shared_ptr<Source> ret (src);
255
256                                 if (setup_peakfile (ret, defer_peaks)) {
257                                         return boost::shared_ptr<Source>();
258                                 }
259
260                                 ret->check_for_analysis_data_on_disk ();
261                                 if (announce) {
262                                         SourceCreated (ret);
263                                 }
264                                 return ret;
265                         }
266
267                         catch (failed_constructor& err) {
268 #ifdef HAVE_COREAUDIO
269
270                                 Source* src = new CoreAudioSource (s, path, chn, flags);
271 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
272                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
273 #endif
274                                 boost::shared_ptr<Source> ret (src);
275                                 if (setup_peakfile (ret, defer_peaks)) {
276                                         return boost::shared_ptr<Source>();
277                                 }
278                                 ret->check_for_analysis_data_on_disk ();
279                                 if (announce) {
280                                         SourceCreated (ret);
281                                 }
282                                 return ret;
283
284 #else
285                                 throw; // rethrow
286 #endif
287                         }
288
289                 } else {
290                         // eh?
291                 }
292
293         } else if (type == DataType::MIDI) {
294
295                 try {
296                         boost::shared_ptr<SMFSource> src (new SMFSource (s, path));
297                         Source::Lock lock(src->mutex());
298                         src->load_model (lock, true);
299 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
300                         // boost_debug_shared_ptr_mark_interesting (src, "Source");
301 #endif
302
303                         if (announce) {
304                                 SourceCreated (src);
305                         }
306
307                         return src;
308                 } catch (...) {
309                 }
310
311         }
312
313         return boost::shared_ptr<Source>();
314 }
315
316 boost::shared_ptr<Source>
317 SourceFactory::createWritable (DataType type, Session& s, const std::string& path,
318                                bool destructive, samplecnt_t rate, bool announce, bool defer_peaks)
319 {
320         /* this might throw failed_constructor(), which is OK */
321
322         if (type == DataType::AUDIO) {
323                 Source* src = new SndFileSource (s, path, string(),
324                                                  s.config.get_native_file_data_format(),
325                                                  s.config.get_native_file_header_format(),
326                                                  rate,
327                                                  (destructive
328                                                   ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
329                                                   : SndFileSource::default_writable_flags));
330 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
331                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
332 #endif
333                 boost::shared_ptr<Source> ret (src);
334
335                 if (setup_peakfile (ret, defer_peaks)) {
336                         return boost::shared_ptr<Source>();
337                 }
338
339                 // no analysis data - this is a new file
340
341                 if (announce) {
342                         SourceCreated (ret);
343                 }
344                 return ret;
345
346         } else if (type == DataType::MIDI) {
347                 // XXX writable flags should belong to MidiSource too
348                 try {
349                         boost::shared_ptr<SMFSource> src (new SMFSource (s, path, SndFileSource::default_writable_flags));
350
351                         assert (src->writable ());
352
353                         Source::Lock lock(src->mutex());
354                         src->load_model (lock, true);
355 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
356                         // boost_debug_shared_ptr_mark_interesting (src, "Source");
357 #endif
358
359                         // no analysis data - this is a new file
360
361                         if (announce) {
362                                 SourceCreated (src);
363                         }
364
365                         return src;
366
367                 } catch (...) {
368                 }
369         }
370
371         return boost::shared_ptr<Source> ();
372 }
373
374 boost::shared_ptr<Source>
375 SourceFactory::createForRecovery (DataType type, Session& s, const std::string& path, int chn)
376 {
377         /* this might throw failed_constructor(), which is OK */
378
379         if (type == DataType::AUDIO) {
380                 Source* src = new SndFileSource (s, path, chn);
381
382 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
383                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
384 #endif
385                 boost::shared_ptr<Source> ret (src);
386
387                 if (setup_peakfile (ret, false)) {
388                         return boost::shared_ptr<Source>();
389                 }
390
391                 // no analysis data - this is still basically a new file (we
392                 // crashed while recording.
393
394                 // always announce these files
395
396                 SourceCreated (ret);
397
398                 return ret;
399
400         } else if (type == DataType::MIDI) {
401                 error << _("Recovery attempted on a MIDI file - not implemented") << endmsg;
402         }
403
404         return boost::shared_ptr<Source> ();
405 }
406
407 boost::shared_ptr<Source>
408 SourceFactory::createFromPlaylist (DataType type, Session& s, boost::shared_ptr<Playlist> p, const PBD::ID& orig, const std::string& name,
409                                    uint32_t chn, sampleoffset_t start, samplecnt_t len, bool copy, bool defer_peaks)
410 {
411         if (type == DataType::AUDIO) {
412                 try {
413
414                         boost::shared_ptr<AudioPlaylist> ap = boost::dynamic_pointer_cast<AudioPlaylist>(p);
415
416                         if (ap) {
417
418                                 if (copy) {
419                                         ap.reset (new AudioPlaylist (ap, start, len, name, true));
420                                         start = 0;
421                                 }
422
423                                 Source* src = new AudioPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
424                                 boost::shared_ptr<Source> ret (src);
425
426                                 if (setup_peakfile (ret, defer_peaks)) {
427                                         return boost::shared_ptr<Source>();
428                                 }
429
430                                 ret->check_for_analysis_data_on_disk ();
431                                 SourceCreated (ret);
432                                 return ret;
433                         }
434                 }
435
436                 catch (failed_constructor& err) {
437                         /* relax - return at function scope */
438                 }
439
440         } else if (type == DataType::MIDI) {
441
442                 try {
443
444                         boost::shared_ptr<MidiPlaylist> ap = boost::dynamic_pointer_cast<MidiPlaylist>(p);
445
446                         if (ap) {
447
448                                 if (copy) {
449                                         ap.reset (new MidiPlaylist (ap, start, len, name, true));
450                                         start = 0;
451                                 }
452
453                                 Source* src = new MidiPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
454                                 boost::shared_ptr<Source> ret (src);
455
456                                 SourceCreated (ret);
457                                 return ret;
458                         }
459                 }
460
461                 catch (failed_constructor& err) {
462                         /* relax - return at function scope */
463                 }
464
465         }
466
467         return boost::shared_ptr<Source>();
468 }