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