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>
27 #include <ardour/source_factory.h>
28 #include <ardour/sndfilesource.h>
29 #include <ardour/silentfilesource.h>
30 #include <ardour/configuration.h>
32 #undef USE_COREAUDIO_FOR_FILES
34 #ifdef USE_COREAUDIO_FOR_FILES
35 #include <ardour/coreaudiosource.h>
40 using namespace ARDOUR;
45 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
46 Glib::Cond* SourceFactory::PeaksToBuild;
47 Glib::StaticMutex SourceFactory::peak_building_lock;
48 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
53 PBD::ThreadCreated (pthread_self(), string ("peakbuilder-") + to_string (pthread_self(), std::dec));
57 SourceFactory::peak_building_lock.lock ();
60 if (SourceFactory::files_with_peaks.empty()) {
61 SourceFactory::PeaksToBuild->wait (SourceFactory::peak_building_lock);
64 if (SourceFactory::files_with_peaks.empty()) {
68 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
69 SourceFactory::files_with_peaks.pop_front ();
70 SourceFactory::peak_building_lock.unlock ();
75 as->setup_peakfile ();
80 SourceFactory::init ()
82 PeaksToBuild = new Glib::Cond();
84 for (int n = 0; n < 2; ++n) {
85 Glib::Thread::create (sigc::ptr_fun (::peak_thread_work), false);
90 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
92 boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
98 Glib::Mutex::Lock lm (peak_building_lock);
99 files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
100 PeaksToBuild->broadcast ();
104 if (as->setup_peakfile ()) {
105 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
114 boost::shared_ptr<Source>
115 SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
117 boost::shared_ptr<Source> ret (new SilentFileSource (s, node, nframes, sr));
122 #ifdef USE_COREAUDIO_FOR_FILES
123 boost::shared_ptr<Source>
124 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
127 boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
128 if (setup_peakfile (ret, defer_peaks)) {
129 return boost::shared_ptr<Source>();
136 catch (failed_constructor& err) {
138 /* this is allowed to throw */
140 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
141 if (setup_peakfile (ret, defer_peaks)) {
142 return boost::shared_ptr<Source>();
148 return boost::shared_ptr<Source>();
153 boost::shared_ptr<Source>
154 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
156 /* this is allowed to throw */
158 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
160 if (setup_peakfile (ret, defer_peaks)) {
161 return boost::shared_ptr<Source>();
168 #endif // USE_COREAUDIO_FOR_FILES
170 #ifdef USE_COREAUDIO_FOR_FILES
171 boost::shared_ptr<Source>
172 SourceFactory::createReadable (Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce, bool defer_peaks)
174 if (!(flags & Destructive)) {
177 boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, chn, flags));
178 if (setup_peakfile (ret, defer_peaks)) {
179 return boost::shared_ptr<Source>();
187 catch (failed_constructor& err) {
189 /* this is allowed to throw */
191 boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
192 if (setup_peakfile (ret, defer_peaks)) {
193 return boost::shared_ptr<Source>();
203 boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
204 if (setup_peakfile (ret, defer_peaks)) {
205 return boost::shared_ptr<Source>();
213 return boost::shared_ptr<Source>();
218 boost::shared_ptr<Source>
219 SourceFactory::createReadable (Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce, bool defer_peaks)
221 boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
223 if (setup_peakfile (ret, defer_peaks)) {
224 return boost::shared_ptr<Source>();
234 #endif // USE_COREAUDIO_FOR_FILES
236 boost::shared_ptr<Source>
237 SourceFactory::createWritable (Session& s, std::string path, bool destructive, nframes_t rate, bool announce, bool defer_peaks)
239 /* this might throw failed_constructor(), which is OK */
241 boost::shared_ptr<Source> ret (new SndFileSource
243 Config->get_native_file_data_format(),
244 Config->get_native_file_header_format(),
246 (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
247 SndFileSource::default_writable_flags)));
249 if (setup_peakfile (ret, defer_peaks)) {
250 return boost::shared_ptr<Source>();