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.
21 #include "pbd/error.h"
22 #include "pbd/convert.h"
23 #include "pbd/pthread_utils.h"
24 #include "pbd/stacktrace.h"
26 #include "ardour/source_factory.h"
27 #include "ardour/sndfilesource.h"
28 #include "ardour/silentfilesource.h"
29 #include "ardour/rc_configuration.h"
30 #include "ardour/smf_source.h"
31 #include "ardour/session.h"
34 #define USE_COREAUDIO_FOR_FILES
37 #ifdef USE_COREAUDIO_FOR_FILES
38 #include "ardour/coreaudiosource.h"
44 using namespace ARDOUR;
48 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
49 Glib::Cond* SourceFactory::PeaksToBuild;
50 Glib::StaticMutex SourceFactory::peak_building_lock = GLIBMM_STATIC_MUTEX_INIT;
51 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
56 PBD::notify_gui_about_thread_creation (pthread_self(), string ("peakbuilder-") + to_string (pthread_self(), std::dec));
60 SourceFactory::peak_building_lock.lock ();
63 if (SourceFactory::files_with_peaks.empty()) {
64 SourceFactory::PeaksToBuild->wait (SourceFactory::peak_building_lock);
67 if (SourceFactory::files_with_peaks.empty()) {
71 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
72 SourceFactory::files_with_peaks.pop_front ();
73 SourceFactory::peak_building_lock.unlock ();
79 as->setup_peakfile ();
84 SourceFactory::init ()
86 PeaksToBuild = new Glib::Cond();
88 for (int n = 0; n < 2; ++n) {
89 Glib::Thread::create (sigc::ptr_fun (::peak_thread_work), false);
94 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
96 boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
102 Glib::Mutex::Lock lm (peak_building_lock);
103 files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
104 PeaksToBuild->broadcast ();
108 if (as->setup_peakfile ()) {
109 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
118 boost::shared_ptr<Source>
119 SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
121 boost::shared_ptr<Source> ret (new SilentFileSource (s, node, nframes, sr));
122 // no analysis data - the file is non-existent
127 boost::shared_ptr<Source>
128 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
130 DataType type = DataType::AUDIO;
131 const XMLProperty* prop = node.property("type");
134 return boost::shared_ptr<Source>();
137 type = DataType (prop->value());
139 if (type == DataType::AUDIO) {
143 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
144 if (setup_peakfile (ret, defer_peaks)) {
145 return boost::shared_ptr<Source>();
147 ret->check_for_analysis_data_on_disk ();
152 catch (failed_constructor& err) {
154 #ifdef USE_COREAUDIO_FOR_FILES
156 /* this is allowed to throw */
158 boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
160 if (setup_peakfile (ret, defer_peaks)) {
161 return boost::shared_ptr<Source>();
164 ret->check_for_analysis_data_on_disk ();
172 } else if (type == DataType::MIDI) {
173 boost::shared_ptr<Source> ret (new SMFSource (s, node));
174 ret->check_for_analysis_data_on_disk ();
179 return boost::shared_ptr<Source>();
182 boost::shared_ptr<Source>
183 SourceFactory::createReadable (DataType type, Session& s, const string& path, bool embedded,
184 int chn, Source::Flag flags, bool announce, bool defer_peaks)
186 if (type == DataType::AUDIO) {
188 if (!(flags & Destructive)) {
192 boost::shared_ptr<Source> ret (new SndFileSource (s, path, embedded, chn, flags));
194 if (setup_peakfile (ret, defer_peaks)) {
195 return boost::shared_ptr<Source>();
198 ret->check_for_analysis_data_on_disk ();
205 catch (failed_constructor& err) {
206 #ifdef USE_COREAUDIO_FOR_FILES
208 boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, embedded, chn, flags));
209 if (setup_peakfile (ret, defer_peaks)) {
210 return boost::shared_ptr<Source>();
212 ret->check_for_analysis_data_on_disk ();
227 } else if (type == DataType::MIDI) {
229 boost::shared_ptr<Source> ret (new SMFSource (s, path, embedded, SMFSource::Flag(0)));
239 return boost::shared_ptr<Source>();
242 boost::shared_ptr<Source>
243 SourceFactory::createWritable (DataType type, Session& s, const std::string& path, bool embedded,
244 bool destructive, nframes_t rate, bool announce, bool defer_peaks)
246 /* this might throw failed_constructor(), which is OK */
248 if (type == DataType::AUDIO) {
249 boost::shared_ptr<Source> ret (new SndFileSource (s, path, embedded,
250 s.config.get_native_file_data_format(),
251 s.config.get_native_file_header_format(),
254 ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
255 : SndFileSource::default_writable_flags)));
257 if (setup_peakfile (ret, defer_peaks)) {
258 return boost::shared_ptr<Source>();
261 // no analysis data - this is a new file
268 } else if (type == DataType::MIDI) {
270 boost::shared_ptr<Source> ret (new SMFSource (s, path, embedded, Source::Flag(0)));
272 // no analysis data - this is a new file
281 return boost::shared_ptr<Source> ();