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/configuration.h>
30 #include <ardour/smf_source.h>
33 #define USE_COREAUDIO_FOR_FILES
36 #ifdef USE_COREAUDIO_FOR_FILES
37 #include <ardour/coreaudiosource.h>
43 using namespace ARDOUR;
47 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
48 Glib::Cond* SourceFactory::PeaksToBuild;
49 Glib::StaticMutex SourceFactory::peak_building_lock = GLIBMM_STATIC_MUTEX_INIT;
50 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
55 PBD::ThreadCreated (pthread_self(), string ("peakbuilder-") + to_string (pthread_self(), std::dec));
59 SourceFactory::peak_building_lock.lock ();
62 if (SourceFactory::files_with_peaks.empty()) {
63 SourceFactory::PeaksToBuild->wait (SourceFactory::peak_building_lock);
66 if (SourceFactory::files_with_peaks.empty()) {
70 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
71 SourceFactory::files_with_peaks.pop_front ();
72 SourceFactory::peak_building_lock.unlock ();
78 as->setup_peakfile ();
83 SourceFactory::init ()
85 PeaksToBuild = new Glib::Cond();
87 for (int n = 0; n < 2; ++n) {
88 Glib::Thread::create (sigc::ptr_fun (::peak_thread_work), false);
93 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
95 boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
101 Glib::Mutex::Lock lm (peak_building_lock);
102 files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
103 PeaksToBuild->broadcast ();
107 if (as->setup_peakfile ()) {
108 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
117 boost::shared_ptr<Source>
118 SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
120 boost::shared_ptr<Source> ret (new SilentFileSource (s, node, nframes, sr));
121 // no analysis data - the file is non-existent
126 boost::shared_ptr<Source>
127 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
129 DataType type = DataType::AUDIO;
130 const XMLProperty* prop = node.property("type");
133 type = DataType(prop->value());
136 if (type == DataType::AUDIO) {
140 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
141 if (setup_peakfile (ret, defer_peaks)) {
142 return boost::shared_ptr<Source>();
144 ret->check_for_analysis_data_on_disk ();
149 catch (failed_constructor& err) {
151 #ifdef USE_COREAUDIO_FOR_FILES
153 /* this is allowed to throw */
155 boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
157 if (setup_peakfile (ret, defer_peaks)) {
158 return boost::shared_ptr<Source>();
161 ret->check_for_analysis_data_on_disk ();
169 } else if (type == DataType::MIDI) {
170 boost::shared_ptr<Source> ret (new SMFSource (s, node));
171 ret->check_for_analysis_data_on_disk ();
176 return boost::shared_ptr<Source>();
179 boost::shared_ptr<Source>
180 SourceFactory::createReadable (DataType type, Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce, bool defer_peaks)
182 if (type == DataType::AUDIO) {
184 if (!(flags & Destructive)) {
188 boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
190 if (setup_peakfile (ret, defer_peaks)) {
191 return boost::shared_ptr<Source>();
194 ret->check_for_analysis_data_on_disk ();
201 catch (failed_constructor& err) {
202 #ifdef USE_COREAUDIO_FOR_FILES
204 boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, chn, flags));
205 if (setup_peakfile (ret, defer_peaks)) {
206 return boost::shared_ptr<Source>();
208 ret->check_for_analysis_data_on_disk ();
223 } else if (type == DataType::MIDI) {
226 boost::shared_ptr<Source> ret (new SMFSource (s, path, SMFSource::Flag(0)));
236 return boost::shared_ptr<Source>();
239 boost::shared_ptr<Source>
240 SourceFactory::createWritable (DataType type, Session& s, std::string path, bool destructive, nframes_t rate, bool announce, bool defer_peaks)
242 /* this might throw failed_constructor(), which is OK */
244 if (type == DataType::AUDIO) {
245 boost::shared_ptr<Source> ret (new SndFileSource
247 Config->get_native_file_data_format(),
248 Config->get_native_file_header_format(),
250 (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
251 SndFileSource::default_writable_flags)));
253 if (setup_peakfile (ret, defer_peaks)) {
254 return boost::shared_ptr<Source>();
257 // no analysis data - this is a new file
264 } else if (type == DataType::MIDI) {
266 boost::shared_ptr<Source> ret (new SMFSource (s, path));
268 // no analysis data - this is a new file
277 return boost::shared_ptr<Source> ();