changes for OS X support: change waf config define to COREAUDIO_SUPPORT, remove Plugi...
[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/audioplaylist.h"
32 #include "ardour/audio_playlist_source.h"
33 #include "ardour/midi_playlist.h"
34 #include "ardour/midi_playlist_source.h"
35 #include "ardour/source_factory.h"
36 #include "ardour/sndfilesource.h"
37 #include "ardour/silentfilesource.h"
38 #include "ardour/rc_configuration.h"
39 #include "ardour/smf_source.h"
40 #include "ardour/session.h"
41
42 #ifdef  HAVE_COREAUDIO
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 PBD::Signal1<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         SessionEvent::create_per_thread_pool (X_("PeakFile Builder "), 64);
62
63         while (true) {
64
65                 SourceFactory::peak_building_lock.lock ();
66
67           wait:
68                 if (SourceFactory::files_with_peaks.empty()) {
69                         SourceFactory::PeaksToBuild->wait (SourceFactory::peak_building_lock);
70                 }
71
72                 if (SourceFactory::files_with_peaks.empty()) {
73                         goto wait;
74                 }
75
76                 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
77                 SourceFactory::files_with_peaks.pop_front ();
78                 SourceFactory::peak_building_lock.unlock ();
79
80                 if (!as) {
81                         continue;
82                 }
83
84                 as->setup_peakfile ();
85         }
86 }
87
88 void
89 SourceFactory::init ()
90 {
91         PeaksToBuild = new Glib::Cond();
92
93         for (int n = 0; n < 2; ++n) {
94                 Glib::Thread::create (sigc::ptr_fun (::peak_thread_work), false);
95         }
96 }
97
98 int
99 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
100 {
101         boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
102
103         if (as) {
104
105                 if (async) {
106
107                         Glib::Mutex::Lock lm (peak_building_lock);
108                         files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
109                         PeaksToBuild->broadcast ();
110
111                 } else {
112
113                         if (as->setup_peakfile ()) {
114                                 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
115                                 return -1;
116                         }
117                 }
118         }
119
120         return 0;
121 }
122
123 boost::shared_ptr<Source>
124 SourceFactory::createSilent (Session& s, const XMLNode& node, framecnt_t nframes, float sr)
125 {
126         Source* src = new SilentFileSource (s, node, nframes, sr);
127 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
128         // boost_debug_shared_ptr_mark_interesting (src, "Source");
129 #endif
130         boost::shared_ptr<Source> ret (src);
131         // no analysis data - the file is non-existent
132         SourceCreated (ret);
133         return ret;
134 }
135
136 boost::shared_ptr<Source>
137 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
138 {
139         DataType type = DataType::AUDIO;
140         const XMLProperty* prop = node.property("type");
141
142         if (prop) {
143                 type = DataType (prop->value());
144         }
145
146         if (type == DataType::AUDIO) {
147
148                 /* it could be nested */
149
150                 if (node.property ("playlist") != 0) {
151
152                         try {
153                                 boost::shared_ptr<AudioPlaylistSource> ap (new AudioPlaylistSource (s, node));
154                                 
155                                 if (setup_peakfile (ap, true)) {
156                                         return boost::shared_ptr<Source>();
157                                 }
158
159                                 ap->check_for_analysis_data_on_disk ();
160
161                                 SourceCreated (ap);
162                                 return ap;
163
164                         } catch (failed_constructor&) {
165                                 /* oh well, so much for that then ... */
166                         }
167
168                 } else {
169
170
171                         try {
172                                 Source* src = new SndFileSource (s, node);
173 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
174                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
175 #endif
176                                 boost::shared_ptr<Source> ret (src);
177                                 if (setup_peakfile (ret, defer_peaks)) {
178                                         return boost::shared_ptr<Source>();
179                                 }
180                                 ret->check_for_analysis_data_on_disk ();
181                                 SourceCreated (ret);
182                                 return ret;
183                         }
184
185                         catch (failed_constructor& err) {
186
187 #ifdef HAVE_COREAUDIO
188
189                                 /* this is allowed to throw */
190
191                                 Source *src = new CoreAudioSource (s, node);
192 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
193                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
194 #endif
195                                 boost::shared_ptr<Source> ret (src);
196
197                                 if (setup_peakfile (ret, defer_peaks)) {
198                                         return boost::shared_ptr<Source>();
199                                 }
200
201                                 ret->check_for_analysis_data_on_disk ();
202                                 SourceCreated (ret);
203                                 return ret;
204 #else
205                                 throw; // rethrow
206 #endif
207                         }
208                 }
209         } else if (type == DataType::MIDI) {
210                 boost::shared_ptr<SMFSource> src (new SMFSource (s, node));
211                 src->load_model (true, true);
212 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
213                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
214 #endif
215                 src->check_for_analysis_data_on_disk ();
216                 SourceCreated (src);
217                 return src;
218         }
219
220         return boost::shared_ptr<Source>();
221 }
222
223 boost::shared_ptr<Source>
224 SourceFactory::createReadable (DataType type, Session& s, const string& path,
225                                int chn, Source::Flag flags, bool announce, bool defer_peaks)
226 {
227         if (type == DataType::AUDIO) {
228
229                 if (!(flags & Destructive)) {
230
231                         try {
232
233                                 Source* src = new SndFileSource (s, path, chn, flags);
234 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
235                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
236 #endif
237                                 boost::shared_ptr<Source> ret (src);
238
239                                 if (setup_peakfile (ret, defer_peaks)) {
240                                         return boost::shared_ptr<Source>();
241                                 }
242
243                                 ret->check_for_analysis_data_on_disk ();
244                                 if (announce) {
245                                         SourceCreated (ret);
246                                 }
247                                 return ret;
248                         }
249
250                         catch (failed_constructor& err) {
251 #ifdef HAVE_COREAUDIO
252
253                                 Source* src = new CoreAudioSource (s, path, chn, flags);
254 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
255                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
256 #endif
257                                 boost::shared_ptr<Source> ret (src);
258                                 if (setup_peakfile (ret, defer_peaks)) {
259                                         return boost::shared_ptr<Source>();
260                                 }
261                                 ret->check_for_analysis_data_on_disk ();
262                                 if (announce) {
263                                         SourceCreated (ret);
264                                 }
265                                 return ret;
266
267 #else
268                                 throw; // rethrow
269 #endif
270                         }
271
272                 } else {
273                         // eh?
274                 }
275
276         } else if (type == DataType::MIDI) {
277
278                 SMFSource* src = new SMFSource (s, path, SMFSource::Flag(0));
279                 src->load_model (true, true);
280 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
281                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
282 #endif
283                 boost::shared_ptr<Source> ret (src);
284
285                 if (announce) {
286                         SourceCreated (ret);
287                 }
288
289                 return ret;
290
291         }
292
293         return boost::shared_ptr<Source>();
294 }
295
296 boost::shared_ptr<Source>
297 SourceFactory::createWritable (DataType type, Session& s, const std::string& path, const std::string& origin,
298                                bool destructive, framecnt_t rate, bool announce, bool defer_peaks)
299 {
300         /* this might throw failed_constructor(), which is OK */
301
302         if (type == DataType::AUDIO) {
303                 Source* src = new SndFileSource (s, path, origin,
304                                 s.config.get_native_file_data_format(),
305                                 s.config.get_native_file_header_format(),
306                                 rate,
307                                 (destructive
308                                         ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
309                                  : SndFileSource::default_writable_flags));
310 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
311                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
312 #endif
313                 boost::shared_ptr<Source> ret (src);
314
315                 if (setup_peakfile (ret, defer_peaks)) {
316                         return boost::shared_ptr<Source>();
317                 }
318
319                 // no analysis data - this is a new file
320
321                 if (announce) {
322                         SourceCreated (ret);
323                 }
324                 return ret;
325
326         } else if (type == DataType::MIDI) {
327                 // XXX writable flags should belong to MidiSource too
328                 boost::shared_ptr<SMFSource> src (new SMFSource (s, path, SndFileSource::default_writable_flags));
329                 assert (src->writable ());
330
331                 src->load_model (true, true);
332 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
333                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
334 #endif
335
336                 // no analysis data - this is a new file
337
338                 if (announce) {
339                         SourceCreated (src);
340                 }
341                 return src;
342
343         }
344
345         return boost::shared_ptr<Source> ();
346 }
347
348 boost::shared_ptr<Source>
349 SourceFactory::createFromPlaylist (DataType type, Session& s, boost::shared_ptr<Playlist> p, const PBD::ID& orig, const std::string& name,
350                                    uint32_t chn, frameoffset_t start, framecnt_t len, bool copy, bool defer_peaks)
351 {
352         if (type == DataType::AUDIO) {
353                 try {
354
355                         boost::shared_ptr<AudioPlaylist> ap = boost::dynamic_pointer_cast<AudioPlaylist>(p);
356
357                         if (ap) {
358
359                                 if (copy) {
360                                         ap.reset (new AudioPlaylist (ap, start, len, name, true));
361                                         start = 0;
362                                 }
363
364                                 Source* src = new AudioPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
365                                 boost::shared_ptr<Source> ret (src);
366
367                                 if (setup_peakfile (ret, defer_peaks)) {
368                                         return boost::shared_ptr<Source>();
369                                 }
370
371                                 ret->check_for_analysis_data_on_disk ();
372                                 SourceCreated (ret);
373                                 return ret;
374                         }
375                 }
376
377                 catch (failed_constructor& err) {
378                         /* relax - return at function scope */
379                 }
380
381         } else if (type == DataType::MIDI) {
382
383                 try {
384
385                         boost::shared_ptr<MidiPlaylist> ap = boost::dynamic_pointer_cast<MidiPlaylist>(p);
386
387                         if (ap) {
388
389                                 if (copy) {
390                                         ap.reset (new MidiPlaylist (ap, start, len, name, true));
391                                         start = 0;
392                                 }
393
394                                 Source* src = new MidiPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
395                                 boost::shared_ptr<Source> ret (src);
396
397                                 SourceCreated (ret);
398                                 return ret;
399                         }
400                 }
401
402                 catch (failed_constructor& err) {
403                         /* relax - return at function scope */
404                 }
405
406         }
407
408         return boost::shared_ptr<Source>();
409 }
410