Merged revisions 6293,6296-6306,6308 via svnmerge from
[ardour.git] / libs / ardour / source_factory.cc
1 /*
2     Copyright (C) 2000-2006 Paul Davis
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
21 #ifdef WAF_BUILD
22 #include "libardour-config.h"
23 #endif
24
25 #include "pbd/boost_debug.h"
26 #include "pbd/error.h"
27 #include "pbd/convert.h"
28 #include "pbd/pthread_utils.h"
29 #include "pbd/stacktrace.h"
30
31 #include "ardour/source_factory.h"
32 #include "ardour/sndfilesource.h"
33 #include "ardour/silentfilesource.h"
34 #include "ardour/rc_configuration.h"
35 #include "ardour/smf_source.h"
36 #include "ardour/session.h"
37
38 #ifdef  HAVE_COREAUDIO
39 #define USE_COREAUDIO_FOR_FILES
40 #endif
41
42 #ifdef USE_COREAUDIO_FOR_FILES
43 #include "ardour/coreaudiosource.h"
44 #endif
45
46
47 #include "i18n.h"
48
49 using namespace ARDOUR;
50 using namespace std;
51 using namespace PBD;
52
53 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
54 Glib::Cond* SourceFactory::PeaksToBuild;
55 Glib::StaticMutex SourceFactory::peak_building_lock = GLIBMM_STATIC_MUTEX_INIT;
56 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
57
58 static void
59 peak_thread_work ()
60 {
61         PBD::notify_gui_about_thread_creation (pthread_self(), string ("peakbuilder-") + to_string (pthread_self(), std::dec));
62         SessionEvent::create_per_thread_pool (X_("PeakFile Builder "), 64);
63
64         while (true) {
65
66                 SourceFactory::peak_building_lock.lock ();
67
68           wait:
69                 if (SourceFactory::files_with_peaks.empty()) {
70                         SourceFactory::PeaksToBuild->wait (SourceFactory::peak_building_lock);
71                 }
72
73                 if (SourceFactory::files_with_peaks.empty()) {
74                         goto wait;
75                 }
76
77                 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
78                 SourceFactory::files_with_peaks.pop_front ();
79                 SourceFactory::peak_building_lock.unlock ();
80
81                 if (!as) {
82                         continue;
83                 }
84
85                 as->setup_peakfile ();
86         }
87 }
88
89 void
90 SourceFactory::init ()
91 {
92         PeaksToBuild = new Glib::Cond();
93
94         for (int n = 0; n < 2; ++n) {
95                 Glib::Thread::create (sigc::ptr_fun (::peak_thread_work), false);
96         }
97 }
98
99 int
100 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
101 {
102         boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
103
104         if (as) {
105
106                 if (async) {
107
108                         Glib::Mutex::Lock lm (peak_building_lock);
109                         files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
110                         PeaksToBuild->broadcast ();
111
112                 } else {
113
114                         if (as->setup_peakfile ()) {
115                                 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
116                                 return -1;
117                         }
118                 }
119         }
120
121         return 0;
122 }
123
124 boost::shared_ptr<Source>
125 SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
126 {
127         Source* src = new SilentFileSource (s, node, nframes, sr);
128         // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
129         boost::shared_ptr<Source> ret (src);
130         // no analysis data - the file is non-existent
131         SourceCreated (ret);
132         return ret;
133 }
134
135 boost::shared_ptr<Source>
136 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
137 {
138         DataType type = DataType::AUDIO;
139         const XMLProperty* prop = node.property("type");
140
141         if (prop) {
142                 type = DataType (prop->value());
143         }
144
145         if (type == DataType::AUDIO) {
146
147                 try {
148
149                         Source* src = new SndFileSource (s, node);
150                         // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
151                         boost::shared_ptr<Source> ret (src);
152                         if (setup_peakfile (ret, defer_peaks)) {
153                                 return boost::shared_ptr<Source>();
154                         }
155                         ret->check_for_analysis_data_on_disk ();
156                         SourceCreated (ret);
157                         return ret;
158                 }
159
160                 catch (failed_constructor& err) {
161
162 #ifdef USE_COREAUDIO_FOR_FILES
163
164                         /* this is allowed to throw */
165
166                         Source *src = new CoreAudioSource (s, node);
167                         // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
168                         boost::shared_ptr<Source> ret (src);
169
170                         if (setup_peakfile (ret, defer_peaks)) {
171                                 return boost::shared_ptr<Source>();
172                         }
173
174                         ret->check_for_analysis_data_on_disk ();
175                         SourceCreated (ret);
176                         return ret;
177 #else
178                         throw; // rethrow
179 #endif
180                 }
181
182         } else if (type == DataType::MIDI) {
183                 Source* src = new SMFSource (s, node);
184                 // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
185                 boost::shared_ptr<Source> ret (src);
186                 ret->check_for_analysis_data_on_disk ();
187                 SourceCreated (ret);
188                 return ret;
189         }
190
191         return boost::shared_ptr<Source>();
192 }
193
194 boost::shared_ptr<Source>
195 SourceFactory::createReadable (DataType type, Session& s, const string& path,
196                                int chn, Source::Flag flags, bool announce, bool defer_peaks)
197 {
198         if (type == DataType::AUDIO) {
199
200                 if (!(flags & Destructive)) {
201
202                         try {
203
204                                 Source* src = new SndFileSource (s, path, chn, flags);
205                                 // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
206                                 boost::shared_ptr<Source> ret (src);
207                                 
208                                 if (setup_peakfile (ret, defer_peaks)) {
209                                         return boost::shared_ptr<Source>();
210                                 }
211
212                                 ret->check_for_analysis_data_on_disk ();
213                                 if (announce) {
214                                         SourceCreated (ret);
215                                 }
216                                 return ret;
217                         }
218
219                         catch (failed_constructor& err) {
220 #ifdef USE_COREAUDIO_FOR_FILES
221
222                                 Source* src = new CoreAudioSource (s, path, chn, flags);
223                                 // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
224                                 boost::shared_ptr<Source> ret (src);
225                                 if (setup_peakfile (ret, defer_peaks)) {
226                                         return boost::shared_ptr<Source>();
227                                 }
228                                 ret->check_for_analysis_data_on_disk ();
229                                 if (announce) {
230                                         SourceCreated (ret);
231                                 }
232                                 return ret;
233
234 #else
235                                 throw; // rethrow
236 #endif
237                         }
238
239                 } else {
240                         // eh?
241                 }
242
243         } else if (type == DataType::MIDI) {
244                 
245                 Source* src = new SMFSource (s, path, SMFSource::Flag(0));
246                 // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
247                 boost::shared_ptr<Source> ret (src);
248
249                 if (announce) {
250                         SourceCreated (ret);
251                 }
252
253                 return ret;
254
255         }
256
257         return boost::shared_ptr<Source>();
258 }
259
260 boost::shared_ptr<Source>
261 SourceFactory::createWritable (DataType type, Session& s, const std::string& path, 
262                                bool destructive, nframes_t rate, bool announce, bool defer_peaks)
263 {
264         /* this might throw failed_constructor(), which is OK */
265
266         if (type == DataType::AUDIO) {
267                 Source* src = new SndFileSource (s, path, 
268                                 s.config.get_native_file_data_format(),
269                                 s.config.get_native_file_header_format(),
270                                 rate,
271                                 (destructive
272                                         ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
273                                  : SndFileSource::default_writable_flags));
274                 // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
275                 boost::shared_ptr<Source> ret (src);
276
277                 if (setup_peakfile (ret, defer_peaks)) {
278                         return boost::shared_ptr<Source>();
279                 }
280
281                 // no analysis data - this is a new file
282
283                 if (announce) {
284                         SourceCreated (ret);
285                 }
286                 return ret;
287
288         } else if (type == DataType::MIDI) {
289
290                 Source* src = new SMFSource (s, path, Source::Flag(0));
291                 // boost_debug_shared_ptr_mark_interesting (src, typeid(src).name());
292                 boost::shared_ptr<Source> ret (src);
293
294                 // no analysis data - this is a new file
295
296                 if (announce) {
297                         SourceCreated (ret);
298                 }
299                 return ret;
300
301         }
302
303         return boost::shared_ptr<Source> ();
304 }
305