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 <glibmm/thread.h>
23 #include <pbd/error.h>
24 #include <pbd/convert.h>
25 #include <pbd/pthread_utils.h>
26 #include <pbd/stacktrace.h>
28 #include <ardour/source_factory.h>
29 #include <ardour/sndfilesource.h>
30 #include <ardour/silentfilesource.h>
31 #include <ardour/configuration.h>
34 #define USE_COREAUDIO_FOR_FILES
37 #ifdef USE_COREAUDIO_FOR_FILES
38 #include <ardour/coreaudiosource.h>
44 using namespace ARDOUR;
49 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
50 Glib::Cond* SourceFactory::PeaksToBuild;
51 Glib::StaticMutex SourceFactory::peak_building_lock = GLIBMM_STATIC_MUTEX_INIT;
52 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
57 PBD::notify_gui_about_thread_creation (pthread_self(), string ("peakbuilder-") + to_string (pthread_self(), std::dec));
61 SourceFactory::peak_building_lock.lock ();
64 if (SourceFactory::files_with_peaks.empty()) {
65 SourceFactory::PeaksToBuild->wait (SourceFactory::peak_building_lock);
68 if (SourceFactory::files_with_peaks.empty()) {
72 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
73 SourceFactory::files_with_peaks.pop_front ();
74 SourceFactory::peak_building_lock.unlock ();
80 as->setup_peakfile ();
85 SourceFactory::init ()
87 PeaksToBuild = new Glib::Cond();
89 for (int n = 0; n < 2; ++n) {
90 Glib::Thread::create (sigc::ptr_fun (::peak_thread_work), false);
95 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
97 boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
103 Glib::Mutex::Lock lm (peak_building_lock);
104 files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
105 PeaksToBuild->broadcast ();
109 if (as->setup_peakfile ()) {
110 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
119 boost::shared_ptr<Source>
120 SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
122 boost::shared_ptr<Source> ret (new SilentFileSource (s, node, nframes, sr));
123 // no analysis data - the file is non-existent
128 boost::shared_ptr<Source>
129 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
133 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
134 if (setup_peakfile (ret, defer_peaks)) {
135 return boost::shared_ptr<Source>();
137 ret->check_for_analysis_data_on_disk ();
143 catch (failed_constructor& err) {
145 #ifdef USE_COREAUDIO_FOR_FILES
147 /* this is allowed to throw */
149 boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
150 if (setup_peakfile (ret, defer_peaks)) {
151 return boost::shared_ptr<Source>();
153 ret->check_for_analysis_data_on_disk ();
162 return boost::shared_ptr<Source>(); // keep stupid gcc happy
165 boost::shared_ptr<Source>
166 SourceFactory::createReadable (Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce, bool defer_peaks)
168 if (!(flags & Destructive)) {
172 boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
173 if (setup_peakfile (ret, defer_peaks)) {
174 return boost::shared_ptr<Source>();
176 ret->check_for_analysis_data_on_disk ();
183 catch (failed_constructor& err) {
185 /* this is allowed to throw */
187 #ifdef USE_COREAUDIO_FOR_FILES
189 boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, chn, flags));
190 if (setup_peakfile (ret, defer_peaks)) {
191 return boost::shared_ptr<Source>();
193 ret->check_for_analysis_data_on_disk ();
208 return boost::shared_ptr<Source>();
211 boost::shared_ptr<Source>
212 SourceFactory::createWritable (Session& s, std::string path, bool destructive, nframes_t rate, bool announce, bool defer_peaks)
214 /* this might throw failed_constructor(), which is OK */
216 boost::shared_ptr<Source> ret (new SndFileSource
218 Config->get_native_file_data_format(),
219 Config->get_native_file_header_format(),
221 (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
222 SndFileSource::default_writable_flags)));
224 if (setup_peakfile (ret, defer_peaks)) {
225 return boost::shared_ptr<Source>();
228 // no analysis data - this is a new file