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/source_factory.h"
34 #include "ardour/sndfilesource.h"
35 #include "ardour/silentfilesource.h"
36 #include "ardour/rc_configuration.h"
37 #include "ardour/smf_source.h"
38 #include "ardour/session.h"
41 #define USE_COREAUDIO_FOR_FILES
44 #ifdef USE_COREAUDIO_FOR_FILES
45 #include "ardour/coreaudiosource.h"
51 using namespace ARDOUR;
55 PBD::Signal1<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
56 Glib::Cond* SourceFactory::PeaksToBuild;
57 Glib::StaticMutex SourceFactory::peak_building_lock = GLIBMM_STATIC_MUTEX_INIT;
58 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
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 ();
80 SourceFactory::peak_building_lock.unlock ();
86 as->setup_peakfile ();
91 SourceFactory::init ()
93 PeaksToBuild = new Glib::Cond();
95 for (int n = 0; n < 2; ++n) {
96 Glib::Thread::create (sigc::ptr_fun (::peak_thread_work), false);
101 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
103 boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
109 Glib::Mutex::Lock lm (peak_building_lock);
110 files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
111 PeaksToBuild->broadcast ();
115 if (as->setup_peakfile ()) {
116 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
125 boost::shared_ptr<Source>
126 SourceFactory::createSilent (Session& s, const XMLNode& node, framecnt_t nframes, float sr)
128 Source* src = new SilentFileSource (s, node, nframes, sr);
129 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
130 // boost_debug_shared_ptr_mark_interesting (src, "Source");
132 boost::shared_ptr<Source> ret (src);
133 // no analysis data - the file is non-existent
138 boost::shared_ptr<Source>
139 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
141 DataType type = DataType::AUDIO;
142 const XMLProperty* prop = node.property("type");
145 type = DataType (prop->value());
148 if (type == DataType::AUDIO) {
150 /* it could be nested */
152 if (node.property ("playlist") != 0) {
155 boost::shared_ptr<AudioPlaylistSource> ap (new AudioPlaylistSource (s, node));
157 if (setup_peakfile (ap, true)) {
158 return boost::shared_ptr<Source>();
161 ap->check_for_analysis_data_on_disk ();
165 } catch (failed_constructor&) {
166 /* oh well, so much for that then ... */
173 Source* src = new SndFileSource (s, node);
174 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
175 // boost_debug_shared_ptr_mark_interesting (src, "Source");
177 boost::shared_ptr<Source> ret (src);
178 if (setup_peakfile (ret, defer_peaks)) {
179 return boost::shared_ptr<Source>();
181 ret->check_for_analysis_data_on_disk ();
186 catch (failed_constructor& err) {
188 #ifdef USE_COREAUDIO_FOR_FILES
190 /* this is allowed to throw */
192 Source *src = new CoreAudioSource (s, node);
193 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
194 // boost_debug_shared_ptr_mark_interesting (src, "Source");
196 boost::shared_ptr<Source> ret (src);
198 if (setup_peakfile (ret, defer_peaks)) {
199 return boost::shared_ptr<Source>();
202 ret->check_for_analysis_data_on_disk ();
210 } else if (type == DataType::MIDI) {
211 boost::shared_ptr<SMFSource> src (new SMFSource (s, node));
212 src->load_model (true, true);
213 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
214 // boost_debug_shared_ptr_mark_interesting (src, "Source");
216 src->check_for_analysis_data_on_disk ();
221 return boost::shared_ptr<Source>();
224 boost::shared_ptr<Source>
225 SourceFactory::createReadable (DataType type, Session& s, const string& path,
226 int chn, Source::Flag flags, bool announce, bool defer_peaks)
228 if (type == DataType::AUDIO) {
230 if (!(flags & Destructive)) {
234 Source* src = new SndFileSource (s, path, chn, flags);
235 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
236 // boost_debug_shared_ptr_mark_interesting (src, "Source");
238 boost::shared_ptr<Source> ret (src);
240 if (setup_peakfile (ret, defer_peaks)) {
241 return boost::shared_ptr<Source>();
244 ret->check_for_analysis_data_on_disk ();
251 catch (failed_constructor& err) {
252 #ifdef USE_COREAUDIO_FOR_FILES
254 Source* src = new CoreAudioSource (s, path, chn, flags);
255 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
256 // boost_debug_shared_ptr_mark_interesting (src, "Source");
258 boost::shared_ptr<Source> ret (src);
259 if (setup_peakfile (ret, defer_peaks)) {
260 return boost::shared_ptr<Source>();
262 ret->check_for_analysis_data_on_disk ();
277 } else if (type == DataType::MIDI) {
279 SMFSource* src = new SMFSource (s, path, SMFSource::Flag(0));
280 src->load_model (true, true);
281 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
282 // boost_debug_shared_ptr_mark_interesting (src, "Source");
284 boost::shared_ptr<Source> ret (src);
294 return boost::shared_ptr<Source>();
297 boost::shared_ptr<Source>
298 SourceFactory::createWritable (DataType type, Session& s, const std::string& path, const std::string& origin,
299 bool destructive, framecnt_t rate, bool announce, bool defer_peaks)
301 /* this might throw failed_constructor(), which is OK */
303 if (type == DataType::AUDIO) {
304 Source* src = new SndFileSource (s, path, origin,
305 s.config.get_native_file_data_format(),
306 s.config.get_native_file_header_format(),
309 ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
310 : SndFileSource::default_writable_flags));
311 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
312 // boost_debug_shared_ptr_mark_interesting (src, "Source");
314 boost::shared_ptr<Source> ret (src);
316 if (setup_peakfile (ret, defer_peaks)) {
317 return boost::shared_ptr<Source>();
320 // no analysis data - this is a new file
327 } else if (type == DataType::MIDI) {
328 // XXX writable flags should belong to MidiSource too
329 boost::shared_ptr<SMFSource> src (new SMFSource (s, path, SndFileSource::default_writable_flags));
330 assert (src->writable ());
332 src->load_model (true, true);
333 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
334 // boost_debug_shared_ptr_mark_interesting (src, "Source");
337 // no analysis data - this is a new file
346 return boost::shared_ptr<Source> ();
349 boost::shared_ptr<Source>
350 SourceFactory::createFromPlaylist (DataType type, Session& s, boost::shared_ptr<Playlist> p, const std::string& name,
351 uint32_t chn, frameoffset_t start, framecnt_t len, bool copy, bool defer_peaks)
353 if (type == DataType::AUDIO) {
356 boost::shared_ptr<AudioPlaylist> ap = boost::dynamic_pointer_cast<AudioPlaylist>(p);
361 ap.reset (new AudioPlaylist (ap, start, len, name, true));
365 Source* src = new AudioPlaylistSource (s, name, ap, chn, start, len, Source::Flag (0));
366 boost::shared_ptr<Source> ret (src);
368 if (setup_peakfile (ret, defer_peaks)) {
369 return boost::shared_ptr<Source>();
372 ret->check_for_analysis_data_on_disk ();
378 catch (failed_constructor& err) {
379 /* relax - return at function scope */
382 } else if (type == DataType::MIDI) {
386 return boost::shared_ptr<Source>();