2 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2009 Taybin Rutkin <taybin@taybin.com>
4 * Copyright (C) 2007-2018 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2014-2015 Robin Gareus <robin@gareus.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "libardour-config.h"
27 #include "pbd/error.h"
28 #include "pbd/convert.h"
29 #include "pbd/pthread_utils.h"
30 #include "pbd/stacktrace.h"
32 #include "ardour/audioplaylist.h"
33 #include "ardour/audio_playlist_source.h"
34 #include "ardour/boost_debug.h"
35 #include "ardour/midi_playlist.h"
36 #include "ardour/midi_playlist_source.h"
37 #include "ardour/source.h"
38 #include "ardour/source_factory.h"
39 #include "ardour/sndfilesource.h"
40 #include "ardour/silentfilesource.h"
41 #include "ardour/smf_source.h"
42 #include "ardour/session.h"
45 #include "ardour/coreaudiosource.h"
51 using namespace ARDOUR;
55 PBD::Signal1<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
56 Glib::Threads::Cond SourceFactory::PeaksToBuild;
57 Glib::Threads::Mutex SourceFactory::peak_building_lock;
58 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
60 static int active_threads = 0;
65 SessionEvent::create_per_thread_pool (X_("PeakFile Builder "), 64);
69 SourceFactory::peak_building_lock.lock ();
72 if (SourceFactory::files_with_peaks.empty()) {
73 SourceFactory::PeaksToBuild.wait (SourceFactory::peak_building_lock);
76 if (SourceFactory::files_with_peaks.empty()) {
80 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
81 SourceFactory::files_with_peaks.pop_front ();
83 SourceFactory::peak_building_lock.unlock ();
89 as->setup_peakfile ();
90 SourceFactory::peak_building_lock.lock ();
92 SourceFactory::peak_building_lock.unlock ();
97 SourceFactory::peak_work_queue_length ()
99 // ideally we'd loop over the queue and check for duplicates
100 // and existing valid peak-files..
101 return SourceFactory::files_with_peaks.size () + active_threads;
105 SourceFactory::init ()
107 for (int n = 0; n < 2; ++n) {
108 Glib::Threads::Thread::create (sigc::ptr_fun (::peak_thread_work));
113 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
115 boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
119 // immediately set 'peakfile-path' for empty and NoPeakFile sources
120 if (async && !as->empty() && !(as->flags() & Source::NoPeakFile)) {
122 Glib::Threads::Mutex::Lock lm (peak_building_lock);
123 files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
124 PeaksToBuild.broadcast ();
128 if (as->setup_peakfile ()) {
129 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
138 boost::shared_ptr<Source>
139 SourceFactory::createSilent (Session& s, const XMLNode& node, samplecnt_t nframes, float sr)
141 Source* src = new SilentFileSource (s, node, nframes, sr);
142 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
143 // boost_debug_shared_ptr_mark_interesting (src, "Source");
145 boost::shared_ptr<Source> ret (src);
146 // no analysis data - the file is non-existent
151 boost::shared_ptr<Source>
152 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
154 DataType type = DataType::AUDIO;
155 XMLProperty const * prop = node.property("type");
158 type = DataType (prop->value());
161 if (type == DataType::AUDIO) {
163 /* it could be nested */
165 if (node.property ("playlist") != 0) {
168 boost::shared_ptr<AudioPlaylistSource> ap (new AudioPlaylistSource (s, node));
170 if (setup_peakfile (ap, true)) {
171 return boost::shared_ptr<Source>();
174 ap->check_for_analysis_data_on_disk ();
179 } catch (failed_constructor&) {
180 /* oh well, so much for that then ... */
187 Source* src = new SndFileSource (s, node);
188 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
189 // boost_debug_shared_ptr_mark_interesting (src, "Source");
191 boost::shared_ptr<Source> ret (src);
192 if (setup_peakfile (ret, defer_peaks)) {
193 return boost::shared_ptr<Source>();
195 ret->check_for_analysis_data_on_disk ();
200 catch (failed_constructor& err) {
202 #ifdef HAVE_COREAUDIO
204 /* this is allowed to throw */
206 Source *src = new CoreAudioSource (s, node);
207 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
208 // boost_debug_shared_ptr_mark_interesting (src, "Source");
210 boost::shared_ptr<Source> ret (src);
212 if (setup_peakfile (ret, defer_peaks)) {
213 return boost::shared_ptr<Source>();
216 ret->check_for_analysis_data_on_disk ();
224 } else if (type == DataType::MIDI) {
226 boost::shared_ptr<SMFSource> src (new SMFSource (s, node));
227 Source::Lock lock(src->mutex());
228 src->load_model (lock, true);
229 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
230 // boost_debug_shared_ptr_mark_interesting (src, "Source");
232 src->check_for_analysis_data_on_disk ();
239 return boost::shared_ptr<Source>();
242 boost::shared_ptr<Source>
243 SourceFactory::createExternal (DataType type, Session& s, const string& path,
244 int chn, Source::Flag flags, bool announce, bool defer_peaks)
246 if (type == DataType::AUDIO) {
248 if (!(flags & Destructive)) {
252 Source* src = new SndFileSource (s, path, chn, flags);
253 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
254 // boost_debug_shared_ptr_mark_interesting (src, "Source");
256 boost::shared_ptr<Source> ret (src);
258 if (setup_peakfile (ret, defer_peaks)) {
259 return boost::shared_ptr<Source>();
262 ret->check_for_analysis_data_on_disk ();
269 catch (failed_constructor& err) {
270 #ifdef HAVE_COREAUDIO
272 Source* src = new CoreAudioSource (s, path, chn, flags);
273 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
274 // boost_debug_shared_ptr_mark_interesting (src, "Source");
276 boost::shared_ptr<Source> ret (src);
277 if (setup_peakfile (ret, defer_peaks)) {
278 return boost::shared_ptr<Source>();
280 ret->check_for_analysis_data_on_disk ();
295 } else if (type == DataType::MIDI) {
298 boost::shared_ptr<SMFSource> src (new SMFSource (s, path));
299 Source::Lock lock(src->mutex());
300 src->load_model (lock, true);
301 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
302 // boost_debug_shared_ptr_mark_interesting (src, "Source");
315 return boost::shared_ptr<Source>();
318 boost::shared_ptr<Source>
319 SourceFactory::createWritable (DataType type, Session& s, const std::string& path,
320 bool destructive, samplecnt_t rate, bool announce, bool defer_peaks)
322 /* this might throw failed_constructor(), which is OK */
324 if (type == DataType::AUDIO) {
325 Source* src = new SndFileSource (s, path, string(),
326 s.config.get_native_file_data_format(),
327 s.config.get_native_file_header_format(),
330 ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
331 : SndFileSource::default_writable_flags));
332 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
333 // boost_debug_shared_ptr_mark_interesting (src, "Source");
335 boost::shared_ptr<Source> ret (src);
337 if (setup_peakfile (ret, defer_peaks)) {
338 return boost::shared_ptr<Source>();
341 // no analysis data - this is a new file
348 } else if (type == DataType::MIDI) {
349 // XXX writable flags should belong to MidiSource too
351 boost::shared_ptr<SMFSource> src (new SMFSource (s, path, SndFileSource::default_writable_flags));
353 assert (src->writable ());
355 Source::Lock lock(src->mutex());
356 src->load_model (lock, true);
357 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
358 // boost_debug_shared_ptr_mark_interesting (src, "Source");
361 // no analysis data - this is a new file
373 return boost::shared_ptr<Source> ();
376 boost::shared_ptr<Source>
377 SourceFactory::createForRecovery (DataType type, Session& s, const std::string& path, int chn)
379 /* this might throw failed_constructor(), which is OK */
381 if (type == DataType::AUDIO) {
382 Source* src = new SndFileSource (s, path, chn);
384 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
385 // boost_debug_shared_ptr_mark_interesting (src, "Source");
387 boost::shared_ptr<Source> ret (src);
389 if (setup_peakfile (ret, false)) {
390 return boost::shared_ptr<Source>();
393 // no analysis data - this is still basically a new file (we
394 // crashed while recording.
396 // always announce these files
402 } else if (type == DataType::MIDI) {
403 error << _("Recovery attempted on a MIDI file - not implemented") << endmsg;
406 return boost::shared_ptr<Source> ();
409 boost::shared_ptr<Source>
410 SourceFactory::createFromPlaylist (DataType type, Session& s, boost::shared_ptr<Playlist> p, const PBD::ID& orig, const std::string& name,
411 uint32_t chn, sampleoffset_t start, samplecnt_t len, bool copy, bool defer_peaks)
413 if (type == DataType::AUDIO) {
416 boost::shared_ptr<AudioPlaylist> ap = boost::dynamic_pointer_cast<AudioPlaylist>(p);
421 ap.reset (new AudioPlaylist (ap, start, len, name, true));
425 Source* src = new AudioPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
426 boost::shared_ptr<Source> ret (src);
428 if (setup_peakfile (ret, defer_peaks)) {
429 return boost::shared_ptr<Source>();
432 ret->check_for_analysis_data_on_disk ();
438 catch (failed_constructor& err) {
439 /* relax - return at function scope */
442 } else if (type == DataType::MIDI) {
446 boost::shared_ptr<MidiPlaylist> ap = boost::dynamic_pointer_cast<MidiPlaylist>(p);
451 ap.reset (new MidiPlaylist (ap, start, len, name, true));
455 Source* src = new MidiPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
456 boost::shared_ptr<Source> ret (src);
463 catch (failed_constructor& err) {
464 /* relax - return at function scope */
469 return boost::shared_ptr<Source>();