2 Copyright (C) 2000-2006 Paul Davis
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.
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.
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.
22 #include "libardour-config.h"
25 #include "pbd/error.h"
26 #include "pbd/convert.h"
27 #include "pbd/pthread_utils.h"
28 #include "pbd/stacktrace.h"
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"
43 #include "ardour/coreaudiosource.h"
49 using namespace ARDOUR;
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;
58 static int active_threads = 0;
63 SessionEvent::create_per_thread_pool (X_("PeakFile Builder "), 64);
67 SourceFactory::peak_building_lock.lock ();
70 if (SourceFactory::files_with_peaks.empty()) {
71 SourceFactory::PeaksToBuild.wait (SourceFactory::peak_building_lock);
74 if (SourceFactory::files_with_peaks.empty()) {
78 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
79 SourceFactory::files_with_peaks.pop_front ();
81 SourceFactory::peak_building_lock.unlock ();
87 as->setup_peakfile ();
88 SourceFactory::peak_building_lock.lock ();
90 SourceFactory::peak_building_lock.unlock ();
95 SourceFactory::peak_work_queue_length ()
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;
103 SourceFactory::init ()
105 for (int n = 0; n < 2; ++n) {
106 Glib::Threads::Thread::create (sigc::ptr_fun (::peak_thread_work));
111 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
113 boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
117 // immediately set 'peakfile-path' for empty and NoPeakFile sources
118 if (async && !as->empty() && !(as->flags() & Source::NoPeakFile)) {
120 Glib::Threads::Mutex::Lock lm (peak_building_lock);
121 files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
122 PeaksToBuild.broadcast ();
126 if (as->setup_peakfile ()) {
127 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
136 boost::shared_ptr<Source>
137 SourceFactory::createSilent (Session& s, const XMLNode& node, samplecnt_t nframes, float sr)
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");
143 boost::shared_ptr<Source> ret (src);
144 // no analysis data - the file is non-existent
149 boost::shared_ptr<Source>
150 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
152 DataType type = DataType::AUDIO;
153 XMLProperty const * prop = node.property("type");
156 type = DataType (prop->value());
159 if (type == DataType::AUDIO) {
161 /* it could be nested */
163 if (node.property ("playlist") != 0) {
166 boost::shared_ptr<AudioPlaylistSource> ap (new AudioPlaylistSource (s, node));
168 if (setup_peakfile (ap, true)) {
169 return boost::shared_ptr<Source>();
172 ap->check_for_analysis_data_on_disk ();
177 } catch (failed_constructor&) {
178 /* oh well, so much for that then ... */
185 Source* src = new SndFileSource (s, node);
186 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
187 // boost_debug_shared_ptr_mark_interesting (src, "Source");
189 boost::shared_ptr<Source> ret (src);
190 if (setup_peakfile (ret, defer_peaks)) {
191 return boost::shared_ptr<Source>();
193 ret->check_for_analysis_data_on_disk ();
198 catch (failed_constructor& err) {
200 #ifdef HAVE_COREAUDIO
202 /* this is allowed to throw */
204 Source *src = new CoreAudioSource (s, node);
205 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
206 // boost_debug_shared_ptr_mark_interesting (src, "Source");
208 boost::shared_ptr<Source> ret (src);
210 if (setup_peakfile (ret, defer_peaks)) {
211 return boost::shared_ptr<Source>();
214 ret->check_for_analysis_data_on_disk ();
222 } else if (type == DataType::MIDI) {
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");
230 src->check_for_analysis_data_on_disk ();
237 return boost::shared_ptr<Source>();
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)
244 if (type == DataType::AUDIO) {
246 if (!(flags & Destructive)) {
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");
254 boost::shared_ptr<Source> ret (src);
256 if (setup_peakfile (ret, defer_peaks)) {
257 return boost::shared_ptr<Source>();
260 ret->check_for_analysis_data_on_disk ();
267 catch (failed_constructor& err) {
268 #ifdef HAVE_COREAUDIO
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");
274 boost::shared_ptr<Source> ret (src);
275 if (setup_peakfile (ret, defer_peaks)) {
276 return boost::shared_ptr<Source>();
278 ret->check_for_analysis_data_on_disk ();
293 } else if (type == DataType::MIDI) {
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");
313 return boost::shared_ptr<Source>();
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)
320 /* this might throw failed_constructor(), which is OK */
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(),
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");
333 boost::shared_ptr<Source> ret (src);
335 if (setup_peakfile (ret, defer_peaks)) {
336 return boost::shared_ptr<Source>();
339 // no analysis data - this is a new file
346 } else if (type == DataType::MIDI) {
347 // XXX writable flags should belong to MidiSource too
349 boost::shared_ptr<SMFSource> src (new SMFSource (s, path, SndFileSource::default_writable_flags));
351 assert (src->writable ());
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");
359 // no analysis data - this is a new file
371 return boost::shared_ptr<Source> ();
374 boost::shared_ptr<Source>
375 SourceFactory::createForRecovery (DataType type, Session& s, const std::string& path, int chn)
377 /* this might throw failed_constructor(), which is OK */
379 if (type == DataType::AUDIO) {
380 Source* src = new SndFileSource (s, path, chn);
382 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
383 // boost_debug_shared_ptr_mark_interesting (src, "Source");
385 boost::shared_ptr<Source> ret (src);
387 if (setup_peakfile (ret, false)) {
388 return boost::shared_ptr<Source>();
391 // no analysis data - this is still basically a new file (we
392 // crashed while recording.
394 // always announce these files
400 } else if (type == DataType::MIDI) {
401 error << _("Recovery attempted on a MIDI file - not implemented") << endmsg;
404 return boost::shared_ptr<Source> ();
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)
411 if (type == DataType::AUDIO) {
414 boost::shared_ptr<AudioPlaylist> ap = boost::dynamic_pointer_cast<AudioPlaylist>(p);
419 ap.reset (new AudioPlaylist (ap, start, len, name, true));
423 Source* src = new AudioPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
424 boost::shared_ptr<Source> ret (src);
426 if (setup_peakfile (ret, defer_peaks)) {
427 return boost::shared_ptr<Source>();
430 ret->check_for_analysis_data_on_disk ();
436 catch (failed_constructor& err) {
437 /* relax - return at function scope */
440 } else if (type == DataType::MIDI) {
444 boost::shared_ptr<MidiPlaylist> ap = boost::dynamic_pointer_cast<MidiPlaylist>(p);
449 ap.reset (new MidiPlaylist (ap, start, len, name, true));
453 Source* src = new MidiPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
454 boost::shared_ptr<Source> ret (src);
461 catch (failed_constructor& err) {
462 /* relax - return at function scope */
467 return boost::shared_ptr<Source>();