Clean up and hopefully fix handling of logarithmic plugin parameters (fixes #3769).
[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 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                 try {
149                         Source* src = new SndFileSource (s, node);
150 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
151                         // boost_debug_shared_ptr_mark_interesting (src, "Source");
152 #endif
153                         boost::shared_ptr<Source> ret (src);
154                         if (setup_peakfile (ret, defer_peaks)) {
155                                 return boost::shared_ptr<Source>();
156                         }
157                         ret->check_for_analysis_data_on_disk ();
158                         SourceCreated (ret);
159                         return ret;
160                 }
161
162                 catch (failed_constructor& err) {
163
164 #ifdef USE_COREAUDIO_FOR_FILES
165
166                         /* this is allowed to throw */
167
168                         Source *src = new CoreAudioSource (s, node);
169 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
170                         // boost_debug_shared_ptr_mark_interesting (src, "Source");
171 #endif
172                         boost::shared_ptr<Source> ret (src);
173
174                         if (setup_peakfile (ret, defer_peaks)) {
175                                 return boost::shared_ptr<Source>();
176                         }
177
178                         ret->check_for_analysis_data_on_disk ();
179                         SourceCreated (ret);
180                         return ret;
181 #else
182                         throw; // rethrow
183 #endif
184                 }
185
186         } else if (type == DataType::MIDI) {
187                 boost::shared_ptr<SMFSource> src (new SMFSource (s, node));
188                 src->load_model (true, true);
189 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
190                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
191 #endif
192                 src->check_for_analysis_data_on_disk ();
193                 SourceCreated (src);
194                 return src;
195         }
196
197         return boost::shared_ptr<Source>();
198 }
199
200 boost::shared_ptr<Source>
201 SourceFactory::createReadable (DataType type, Session& s, const string& path,
202                                int chn, Source::Flag flags, bool announce, bool defer_peaks)
203 {
204         if (type == DataType::AUDIO) {
205
206                 if (!(flags & Destructive)) {
207
208                         try {
209
210                                 Source* src = new SndFileSource (s, path, chn, flags);
211 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
212                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
213 #endif
214                                 boost::shared_ptr<Source> ret (src);
215                                 
216                                 if (setup_peakfile (ret, defer_peaks)) {
217                                         return boost::shared_ptr<Source>();
218                                 }
219
220                                 ret->check_for_analysis_data_on_disk ();
221                                 if (announce) {
222                                         SourceCreated (ret);
223                                 }
224                                 return ret;
225                         }
226
227                         catch (failed_constructor& err) {
228 #ifdef USE_COREAUDIO_FOR_FILES
229
230                                 Source* src = new CoreAudioSource (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                                 if (setup_peakfile (ret, defer_peaks)) {
236                                         return boost::shared_ptr<Source>();
237                                 }
238                                 ret->check_for_analysis_data_on_disk ();
239                                 if (announce) {
240                                         SourceCreated (ret);
241                                 }
242                                 return ret;
243
244 #else
245                                 throw; // rethrow
246 #endif
247                         }
248
249                 } else {
250                         // eh?
251                 }
252
253         } else if (type == DataType::MIDI) {
254                 
255                 SMFSource* src = new SMFSource (s, path, SMFSource::Flag(0));
256                 src->load_model (true, true);
257 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
258                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
259 #endif
260                 boost::shared_ptr<Source> ret (src);
261
262                 if (announce) {
263                         SourceCreated (ret);
264                 }
265
266                 return ret;
267
268         }
269
270         return boost::shared_ptr<Source>();
271 }
272
273 boost::shared_ptr<Source>
274 SourceFactory::createWritable (DataType type, Session& s, const std::string& path, const std::string& origin,
275                                bool destructive, framecnt_t rate, bool announce, bool defer_peaks)
276 {
277         /* this might throw failed_constructor(), which is OK */
278
279         if (type == DataType::AUDIO) {
280                 Source* src = new SndFileSource (s, path, origin,
281                                 s.config.get_native_file_data_format(),
282                                 s.config.get_native_file_header_format(),
283                                 rate,
284                                 (destructive
285                                         ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
286                                  : SndFileSource::default_writable_flags));
287 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
288                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
289 #endif
290                 boost::shared_ptr<Source> ret (src);
291
292                 if (setup_peakfile (ret, defer_peaks)) {
293                         return boost::shared_ptr<Source>();
294                 }
295
296                 // no analysis data - this is a new file
297
298                 if (announce) {
299                         SourceCreated (ret);
300                 }
301                 return ret;
302
303         } else if (type == DataType::MIDI) {
304                 // XXX writable flags should belong to MidiSource too
305                 boost::shared_ptr<SMFSource> src (new SMFSource (s, path, SndFileSource::default_writable_flags));
306                 src->load_model (true, true);
307 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
308                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
309 #endif
310
311                 // no analysis data - this is a new file
312
313                 if (announce) {
314                         SourceCreated (src);
315                 }
316                 return src;
317
318         }
319
320         return boost::shared_ptr<Source> ();
321 }
322