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/source_factory.h"
31 #include "ardour/sndfilesource.h"
32 #include "ardour/silentfilesource.h"
33 #include "ardour/rc_configuration.h"
34 #include "ardour/smf_source.h"
35 #include "ardour/session.h"
38 #define USE_COREAUDIO_FOR_FILES
41 #ifdef USE_COREAUDIO_FOR_FILES
42 #include "ardour/coreaudiosource.h"
48 using namespace ARDOUR;
52 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
53 Glib::Cond* SourceFactory::PeaksToBuild;
54 Glib::StaticMutex SourceFactory::peak_building_lock = GLIBMM_STATIC_MUTEX_INIT;
55 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
60 PBD::notify_gui_about_thread_creation (pthread_self(), string ("peakbuilder-") + to_string (pthread_self(), std::dec));
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 PeaksToBuild = new Glib::Cond();
92 for (int n = 0; n < 2; ++n) {
93 Glib::Thread::create (sigc::ptr_fun (::peak_thread_work), false);
98 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
100 boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
106 Glib::Mutex::Lock lm (peak_building_lock);
107 files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
108 PeaksToBuild->broadcast ();
112 if (as->setup_peakfile ()) {
113 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
122 boost::shared_ptr<Source>
123 SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
125 boost::shared_ptr<Source> ret (new SilentFileSource (s, node, nframes, sr));
126 // no analysis data - the file is non-existent
131 boost::shared_ptr<Source>
132 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
134 DataType type = DataType::AUDIO;
135 const XMLProperty* prop = node.property("type");
138 type = DataType (prop->value());
141 if (type == DataType::AUDIO) {
145 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
146 if (setup_peakfile (ret, defer_peaks)) {
147 return boost::shared_ptr<Source>();
149 ret->check_for_analysis_data_on_disk ();
154 catch (failed_constructor& err) {
156 #ifdef USE_COREAUDIO_FOR_FILES
158 /* this is allowed to throw */
160 boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
162 if (setup_peakfile (ret, defer_peaks)) {
163 return boost::shared_ptr<Source>();
166 ret->check_for_analysis_data_on_disk ();
174 } else if (type == DataType::MIDI) {
175 boost::shared_ptr<Source> ret (new SMFSource (s, node));
176 ret->check_for_analysis_data_on_disk ();
181 return boost::shared_ptr<Source>();
184 boost::shared_ptr<Source>
185 SourceFactory::createReadable (DataType type, Session& s, const string& path, bool embedded,
186 int chn, Source::Flag flags, bool announce, bool defer_peaks)
188 if (type == DataType::AUDIO) {
190 if (!(flags & Destructive)) {
194 boost::shared_ptr<Source> ret (new SndFileSource (s, path, embedded, chn, flags));
196 if (setup_peakfile (ret, defer_peaks)) {
197 return boost::shared_ptr<Source>();
200 ret->check_for_analysis_data_on_disk ();
207 catch (failed_constructor& err) {
208 #ifdef USE_COREAUDIO_FOR_FILES
210 boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, embedded, chn, flags));
211 if (setup_peakfile (ret, defer_peaks)) {
212 return boost::shared_ptr<Source>();
214 ret->check_for_analysis_data_on_disk ();
229 } else if (type == DataType::MIDI) {
231 boost::shared_ptr<Source> ret (new SMFSource (s, path, embedded, SMFSource::Flag(0)));
241 return boost::shared_ptr<Source>();
244 boost::shared_ptr<Source>
245 SourceFactory::createWritable (DataType type, Session& s, const std::string& path, bool embedded,
246 bool destructive, nframes_t rate, bool announce, bool defer_peaks)
248 /* this might throw failed_constructor(), which is OK */
250 if (type == DataType::AUDIO) {
251 boost::shared_ptr<Source> ret (new SndFileSource (s, path, embedded,
252 s.config.get_native_file_data_format(),
253 s.config.get_native_file_header_format(),
256 ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
257 : SndFileSource::default_writable_flags)));
259 if (setup_peakfile (ret, defer_peaks)) {
260 return boost::shared_ptr<Source>();
263 // no analysis data - this is a new file
270 } else if (type == DataType::MIDI) {
272 boost::shared_ptr<Source> ret (new SMFSource (s, path, embedded, Source::Flag(0)));
274 // no analysis data - this is a new file
283 return boost::shared_ptr<Source> ();