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::notify_gui_about_thread_creation (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 return boost::shared_ptr<Source>();
136 type = DataType (prop->value());
138 if (type == DataType::AUDIO) {
142 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
143 if (setup_peakfile (ret, defer_peaks)) {
144 return boost::shared_ptr<Source>();
146 ret->check_for_analysis_data_on_disk ();
151 catch (failed_constructor& err) {
153 #ifdef USE_COREAUDIO_FOR_FILES
155 /* this is allowed to throw */
157 boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
159 if (setup_peakfile (ret, defer_peaks)) {
160 return boost::shared_ptr<Source>();
163 ret->check_for_analysis_data_on_disk ();
171 } else if (type == DataType::MIDI) {
172 boost::shared_ptr<Source> ret (new SMFSource (s, node));
173 ret->check_for_analysis_data_on_disk ();
178 return boost::shared_ptr<Source>();
181 boost::shared_ptr<Source>
182 SourceFactory::createReadable (DataType type, Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce, bool defer_peaks)
184 if (type == DataType::AUDIO) {
186 if (!(flags & Destructive)) {
190 boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
192 if (setup_peakfile (ret, defer_peaks)) {
193 return boost::shared_ptr<Source>();
196 ret->check_for_analysis_data_on_disk ();
203 catch (failed_constructor& err) {
204 #ifdef USE_COREAUDIO_FOR_FILES
206 boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, chn, flags));
207 if (setup_peakfile (ret, defer_peaks)) {
208 return boost::shared_ptr<Source>();
210 ret->check_for_analysis_data_on_disk ();
225 } else if (type == DataType::MIDI) {
228 boost::shared_ptr<Source> ret (new SMFSource (s, path, SMFSource::Flag(0)));
238 return boost::shared_ptr<Source>();
241 boost::shared_ptr<Source>
242 SourceFactory::createWritable (DataType type, Session& s, std::string path, bool destructive, nframes_t rate, bool announce, bool defer_peaks)
244 /* this might throw failed_constructor(), which is OK */
246 if (type == DataType::AUDIO) {
247 boost::shared_ptr<Source> ret (new SndFileSource
249 Config->get_native_file_data_format(),
250 Config->get_native_file_header_format(),
252 (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
253 SndFileSource::default_writable_flags)));
255 if (setup_peakfile (ret, defer_peaks)) {
256 return boost::shared_ptr<Source>();
259 // no analysis data - this is a new file
266 } else if (type == DataType::MIDI) {
268 boost::shared_ptr<Source> ret (new SMFSource (s, path));
270 // no analysis data - this is a new file
279 return boost::shared_ptr<Source> ();