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>
25 #include <ardour/source_factory.h>
26 #include <ardour/sndfilesource.h>
27 #include <ardour/silentfilesource.h>
28 #include <ardour/configuration.h>
29 #include <ardour/smf_source.h>
32 #include <ardour/coreaudiosource.h>
37 using namespace ARDOUR;
41 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
42 Glib::Cond* SourceFactory::PeaksToBuild;
43 Glib::StaticMutex SourceFactory::peak_building_lock = GLIBMM_STATIC_MUTEX_INIT;
44 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
49 PBD::ThreadCreated (pthread_self(), string ("peakbuilder-") + to_string (pthread_self(), std::dec));
53 SourceFactory::peak_building_lock.lock ();
56 if (SourceFactory::files_with_peaks.empty()) {
57 SourceFactory::PeaksToBuild->wait (SourceFactory::peak_building_lock);
60 if (SourceFactory::files_with_peaks.empty()) {
64 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
65 SourceFactory::files_with_peaks.pop_front ();
66 SourceFactory::peak_building_lock.unlock ();
71 as->setup_peakfile ();
76 SourceFactory::init ()
78 PeaksToBuild = new Glib::Cond();
80 for (int n = 0; n < 2; ++n) {
81 Glib::Thread::create (sigc::ptr_fun (::peak_thread_work), false);
86 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
88 boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
94 Glib::Mutex::Lock lm (peak_building_lock);
95 files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
96 PeaksToBuild->broadcast ();
100 if (as->setup_peakfile ()) {
101 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
110 boost::shared_ptr<Source>
111 SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
113 boost::shared_ptr<Source> ret (new SilentFileSource (s, node, nframes, sr));
114 // no analysis data - the file is non-existent
119 boost::shared_ptr<Source>
120 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
122 DataType type = DataType::AUDIO;
123 const XMLProperty* prop = node.property("type");
126 type = DataType(prop->value());
129 if (type == DataType::AUDIO) {
131 #ifdef HAVE_COREAUDIO
134 boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
136 if (setup_peakfile (ret, defer_peaks)) {
137 return boost::shared_ptr<Source>();
140 ret->check_for_analysis_data_on_disk ();
146 catch (failed_constructor& err) {
148 /* this is allowed to throw */
150 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
152 if (setup_peakfile (ret, defer_peaks)) {
153 return boost::shared_ptr<Source>();
156 ret->check_for_analysis_data_on_disk ();
161 /* this is allowed to throw */
163 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
165 if (setup_peakfile (ret, defer_peaks)) {
166 return boost::shared_ptr<Source>();
169 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, string path, int chn, AudioFileSource::Flag flags, bool announce, bool defer_peaks)
187 if (type == DataType::AUDIO) {
189 #ifdef HAVE_COREAUDIO
191 boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, chn, flags));
193 if (setup_peakfile (ret, defer_peaks)) {
194 return boost::shared_ptr<Source>();
197 ret->check_for_analysis_data_on_disk ();
204 catch (failed_constructor& err) {
205 boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
206 if (setup_peakfile (ret, defer_peaks)) {
207 return boost::shared_ptr<Source>();
209 ret->check_for_analysis_data_on_disk ();
216 boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
218 if (setup_peakfile (ret, defer_peaks)) {
219 return boost::shared_ptr<Source>();
222 ret->check_for_analysis_data_on_disk ();
230 } else if (type == DataType::MIDI) {
233 boost::shared_ptr<Source> ret (new SMFSource (s, path, SMFSource::Flag(0)));
235 ret->check_for_analysis_data_on_disk ();
243 return boost::shared_ptr<Source>();
246 boost::shared_ptr<Source>
247 SourceFactory::createWritable (DataType type, Session& s, std::string path, bool destructive, nframes_t rate, bool announce, bool defer_peaks)
249 /* this might throw failed_constructor(), which is OK */
251 if (type == DataType::AUDIO) {
252 boost::shared_ptr<Source> ret (new SndFileSource
254 Config->get_native_file_data_format(),
255 Config->get_native_file_header_format(),
257 (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
258 SndFileSource::default_writable_flags)));
260 if (setup_peakfile (ret, defer_peaks)) {
261 return boost::shared_ptr<Source>();
264 // no analysis data - this is a new file
271 } else if (type == DataType::MIDI) {
273 boost::shared_ptr<Source> ret (new SMFSource (s, path));
275 // no analysis data - this is a new file
284 return boost::shared_ptr<Source> ();