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