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, framecnt_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) {
223 boost::shared_ptr<SMFSource> src (new SMFSource (s, node));
224 Source::Lock lock(src->mutex());
225 src->load_model (lock, true);
226 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
227 // boost_debug_shared_ptr_mark_interesting (src, "Source");
229 src->check_for_analysis_data_on_disk ();
234 return boost::shared_ptr<Source>();
237 boost::shared_ptr<Source>
238 SourceFactory::createExternal (DataType type, Session& s, const string& path,
239 int chn, Source::Flag flags, bool announce, bool defer_peaks)
241 if (type == DataType::AUDIO) {
243 if (!(flags & Destructive)) {
247 Source* src = new SndFileSource (s, path, chn, flags);
248 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
249 // boost_debug_shared_ptr_mark_interesting (src, "Source");
251 boost::shared_ptr<Source> ret (src);
253 if (setup_peakfile (ret, defer_peaks)) {
254 return boost::shared_ptr<Source>();
257 ret->check_for_analysis_data_on_disk ();
264 catch (failed_constructor& err) {
265 #ifdef HAVE_COREAUDIO
267 Source* src = new CoreAudioSource (s, path, chn, flags);
268 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
269 // boost_debug_shared_ptr_mark_interesting (src, "Source");
271 boost::shared_ptr<Source> ret (src);
272 if (setup_peakfile (ret, defer_peaks)) {
273 return boost::shared_ptr<Source>();
275 ret->check_for_analysis_data_on_disk ();
290 } else if (type == DataType::MIDI) {
292 boost::shared_ptr<SMFSource> src (new SMFSource (s, path));
293 Source::Lock lock(src->mutex());
294 src->load_model (lock, true);
295 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
296 // boost_debug_shared_ptr_mark_interesting (src, "Source");
307 return boost::shared_ptr<Source>();
310 boost::shared_ptr<Source>
311 SourceFactory::createWritable (DataType type, Session& s, const std::string& path,
312 bool destructive, framecnt_t rate, bool announce, bool defer_peaks)
314 /* this might throw failed_constructor(), which is OK */
316 if (type == DataType::AUDIO) {
317 Source* src = new SndFileSource (s, path, string(),
318 s.config.get_native_file_data_format(),
319 s.config.get_native_file_header_format(),
322 ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
323 : SndFileSource::default_writable_flags));
324 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
325 // boost_debug_shared_ptr_mark_interesting (src, "Source");
327 boost::shared_ptr<Source> ret (src);
329 if (setup_peakfile (ret, defer_peaks)) {
330 return boost::shared_ptr<Source>();
333 // no analysis data - this is a new file
340 } else if (type == DataType::MIDI) {
341 // XXX writable flags should belong to MidiSource too
342 boost::shared_ptr<SMFSource> src (new SMFSource (s, path, SndFileSource::default_writable_flags));
343 assert (src->writable ());
345 Source::Lock lock(src->mutex());
346 src->load_model (lock, true);
347 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
348 // boost_debug_shared_ptr_mark_interesting (src, "Source");
351 // no analysis data - this is a new file
360 return boost::shared_ptr<Source> ();
363 boost::shared_ptr<Source>
364 SourceFactory::createForRecovery (DataType type, Session& s, const std::string& path, int chn)
366 /* this might throw failed_constructor(), which is OK */
368 if (type == DataType::AUDIO) {
369 Source* src = new SndFileSource (s, path, chn);
371 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
372 // boost_debug_shared_ptr_mark_interesting (src, "Source");
374 boost::shared_ptr<Source> ret (src);
376 if (setup_peakfile (ret, false)) {
377 return boost::shared_ptr<Source>();
380 // no analysis data - this is still basically a new file (we
381 // crashed while recording.
383 // always announce these files
389 } else if (type == DataType::MIDI) {
390 error << _("Recovery attempted on a MIDI file - not implemented") << endmsg;
393 return boost::shared_ptr<Source> ();
396 boost::shared_ptr<Source>
397 SourceFactory::createFromPlaylist (DataType type, Session& s, boost::shared_ptr<Playlist> p, const PBD::ID& orig, const std::string& name,
398 uint32_t chn, frameoffset_t start, framecnt_t len, bool copy, bool defer_peaks)
400 if (type == DataType::AUDIO) {
403 boost::shared_ptr<AudioPlaylist> ap = boost::dynamic_pointer_cast<AudioPlaylist>(p);
408 ap.reset (new AudioPlaylist (ap, start, len, name, true));
412 Source* src = new AudioPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
413 boost::shared_ptr<Source> ret (src);
415 if (setup_peakfile (ret, defer_peaks)) {
416 return boost::shared_ptr<Source>();
419 ret->check_for_analysis_data_on_disk ();
425 catch (failed_constructor& err) {
426 /* relax - return at function scope */
429 } else if (type == DataType::MIDI) {
433 boost::shared_ptr<MidiPlaylist> ap = boost::dynamic_pointer_cast<MidiPlaylist>(p);
438 ap.reset (new MidiPlaylist (ap, start, len, name, true));
442 Source* src = new MidiPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
443 boost::shared_ptr<Source> ret (src);
450 catch (failed_constructor& err) {
451 /* relax - return at function scope */
456 return boost::shared_ptr<Source>();