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/boost_debug.h"
26 #include "pbd/error.h"
27 #include "pbd/convert.h"
28 #include "pbd/pthread_utils.h"
29 #include "pbd/stacktrace.h"
31 #include "ardour/audioplaylist.h"
32 #include "ardour/audio_playlist_source.h"
33 #include "ardour/midi_playlist.h"
34 #include "ardour/midi_playlist_source.h"
35 #include "ardour/source_factory.h"
36 #include "ardour/sndfilesource.h"
37 #include "ardour/silentfilesource.h"
38 #include "ardour/smf_source.h"
39 #include "ardour/session.h"
42 #include "ardour/coreaudiosource.h"
48 using namespace ARDOUR;
52 PBD::Signal1<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
53 Glib::Threads::Cond SourceFactory::PeaksToBuild;
54 Glib::Threads::Mutex SourceFactory::peak_building_lock;
55 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
60 SessionEvent::create_per_thread_pool (X_("PeakFile Builder "), 64);
64 SourceFactory::peak_building_lock.lock ();
67 if (SourceFactory::files_with_peaks.empty()) {
68 SourceFactory::PeaksToBuild.wait (SourceFactory::peak_building_lock);
71 if (SourceFactory::files_with_peaks.empty()) {
75 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
76 SourceFactory::files_with_peaks.pop_front ();
77 SourceFactory::peak_building_lock.unlock ();
83 as->setup_peakfile ();
88 SourceFactory::init ()
90 for (int n = 0; n < 2; ++n) {
91 Glib::Threads::Thread::create (sigc::ptr_fun (::peak_thread_work));
96 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
98 boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
104 Glib::Threads::Mutex::Lock lm (peak_building_lock);
105 files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
106 PeaksToBuild.broadcast ();
110 if (as->setup_peakfile ()) {
111 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
120 boost::shared_ptr<Source>
121 SourceFactory::createSilent (Session& s, const XMLNode& node, framecnt_t nframes, float sr)
123 Source* src = new SilentFileSource (s, node, nframes, sr);
124 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
125 // boost_debug_shared_ptr_mark_interesting (src, "Source");
127 boost::shared_ptr<Source> ret (src);
128 // no analysis data - the file is non-existent
133 boost::shared_ptr<Source>
134 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
136 DataType type = DataType::AUDIO;
137 const XMLProperty* prop = node.property("type");
140 type = DataType (prop->value());
143 if (type == DataType::AUDIO) {
145 /* it could be nested */
147 if (node.property ("playlist") != 0) {
150 boost::shared_ptr<AudioPlaylistSource> ap (new AudioPlaylistSource (s, node));
152 if (setup_peakfile (ap, true)) {
153 return boost::shared_ptr<Source>();
156 ap->check_for_analysis_data_on_disk ();
161 } catch (failed_constructor&) {
162 /* oh well, so much for that then ... */
169 Source* src = new SndFileSource (s, node);
170 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
171 // boost_debug_shared_ptr_mark_interesting (src, "Source");
173 boost::shared_ptr<Source> ret (src);
174 if (setup_peakfile (ret, defer_peaks)) {
175 return boost::shared_ptr<Source>();
177 ret->check_for_analysis_data_on_disk ();
182 catch (failed_constructor& err) {
184 #ifdef HAVE_COREAUDIO
186 /* this is allowed to throw */
188 Source *src = new CoreAudioSource (s, node);
189 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
190 // boost_debug_shared_ptr_mark_interesting (src, "Source");
192 boost::shared_ptr<Source> ret (src);
194 if (setup_peakfile (ret, defer_peaks)) {
195 return boost::shared_ptr<Source>();
198 ret->check_for_analysis_data_on_disk ();
206 } else if (type == DataType::MIDI) {
207 boost::shared_ptr<SMFSource> src (new SMFSource (s, node));
208 src->load_model (true, true);
209 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
210 // boost_debug_shared_ptr_mark_interesting (src, "Source");
212 src->check_for_analysis_data_on_disk ();
217 return boost::shared_ptr<Source>();
220 boost::shared_ptr<Source>
221 SourceFactory::createExternal (DataType type, Session& s, const string& path,
222 int chn, Source::Flag flags, bool announce, bool defer_peaks)
224 if (type == DataType::AUDIO) {
226 if (!(flags & Destructive)) {
230 Source* src = new SndFileSource (s, path, chn, flags);
231 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
232 // boost_debug_shared_ptr_mark_interesting (src, "Source");
234 boost::shared_ptr<Source> ret (src);
236 if (setup_peakfile (ret, defer_peaks)) {
237 return boost::shared_ptr<Source>();
240 ret->check_for_analysis_data_on_disk ();
247 catch (failed_constructor& err) {
248 #ifdef HAVE_COREAUDIO
250 Source* src = new CoreAudioSource (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);
255 if (setup_peakfile (ret, defer_peaks)) {
256 return boost::shared_ptr<Source>();
258 ret->check_for_analysis_data_on_disk ();
273 } else if (type == DataType::MIDI) {
275 boost::shared_ptr<SMFSource> src (new SMFSource (s, path));
276 src->load_model (true, true);
277 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
278 // boost_debug_shared_ptr_mark_interesting (src, "Source");
289 return boost::shared_ptr<Source>();
292 boost::shared_ptr<Source>
293 SourceFactory::createWritable (DataType type, Session& s, const std::string& path,
294 bool destructive, framecnt_t rate, bool announce, bool defer_peaks)
296 /* this might throw failed_constructor(), which is OK */
298 if (type == DataType::AUDIO) {
299 Source* src = new SndFileSource (s, path, string(),
300 s.config.get_native_file_data_format(),
301 s.config.get_native_file_header_format(),
304 ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
305 : SndFileSource::default_writable_flags));
306 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
307 // boost_debug_shared_ptr_mark_interesting (src, "Source");
309 boost::shared_ptr<Source> ret (src);
311 if (setup_peakfile (ret, defer_peaks)) {
312 return boost::shared_ptr<Source>();
315 // no analysis data - this is a new file
322 } else if (type == DataType::MIDI) {
323 // XXX writable flags should belong to MidiSource too
324 boost::shared_ptr<SMFSource> src (new SMFSource (s, path, SndFileSource::default_writable_flags));
325 assert (src->writable ());
327 src->load_model (true, true);
328 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
329 // boost_debug_shared_ptr_mark_interesting (src, "Source");
332 // no analysis data - this is a new file
341 return boost::shared_ptr<Source> ();
344 boost::shared_ptr<Source>
345 SourceFactory::createForRecovery (DataType type, Session& s, const std::string& path, int chn)
347 /* this might throw failed_constructor(), which is OK */
349 if (type == DataType::AUDIO) {
350 Source* src = new SndFileSource (s, path, chn);
352 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
353 // boost_debug_shared_ptr_mark_interesting (src, "Source");
355 boost::shared_ptr<Source> ret (src);
357 if (setup_peakfile (ret, false)) {
358 return boost::shared_ptr<Source>();
361 // no analysis data - this is still basically a new file (we
362 // crashed while recording.
364 // always announce these files
370 } else if (type == DataType::MIDI) {
371 error << _("Recovery attempted on a MIDI file - not implemented") << endmsg;
374 return boost::shared_ptr<Source> ();
377 boost::shared_ptr<Source>
378 SourceFactory::createFromPlaylist (DataType type, Session& s, boost::shared_ptr<Playlist> p, const PBD::ID& orig, const std::string& name,
379 uint32_t chn, frameoffset_t start, framecnt_t len, bool copy, bool defer_peaks)
381 if (type == DataType::AUDIO) {
384 boost::shared_ptr<AudioPlaylist> ap = boost::dynamic_pointer_cast<AudioPlaylist>(p);
389 ap.reset (new AudioPlaylist (ap, start, len, name, true));
393 Source* src = new AudioPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
394 boost::shared_ptr<Source> ret (src);
396 if (setup_peakfile (ret, defer_peaks)) {
397 return boost::shared_ptr<Source>();
400 ret->check_for_analysis_data_on_disk ();
406 catch (failed_constructor& err) {
407 /* relax - return at function scope */
410 } else if (type == DataType::MIDI) {
414 boost::shared_ptr<MidiPlaylist> ap = boost::dynamic_pointer_cast<MidiPlaylist>(p);
419 ap.reset (new MidiPlaylist (ap, start, len, name, true));
423 Source* src = new MidiPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
424 boost::shared_ptr<Source> ret (src);
431 catch (failed_constructor& err) {
432 /* relax - return at function scope */
437 return boost::shared_ptr<Source>();