*** NEW CODING POLICY ***
[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::notify_gui_about_thread_creation (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                 return boost::shared_ptr<Source>();
134         }
135
136         type = DataType (prop->value());
137
138         if (type == DataType::AUDIO) {
139
140                 try {
141                         
142                         boost::shared_ptr<Source> ret (new SndFileSource (s, node));
143                         if (setup_peakfile (ret, defer_peaks)) {
144                                 return boost::shared_ptr<Source>();
145                         }
146                         ret->check_for_analysis_data_on_disk ();
147                         SourceCreated (ret);
148                         return ret;
149                 } 
150                 
151                 catch (failed_constructor& err) {
152
153 #ifdef USE_COREAUDIO_FOR_FILES
154                 
155                         /* this is allowed to throw */
156                         
157                         boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
158                         
159                         if (setup_peakfile (ret, defer_peaks)) {
160                                 return boost::shared_ptr<Source>();
161                         }
162                         
163                         ret->check_for_analysis_data_on_disk ();
164                         SourceCreated (ret);
165                         return ret;
166 #else
167                         throw; // rethrow 
168 #endif
169                 }
170
171         } else if (type == DataType::MIDI) {
172                 boost::shared_ptr<Source> ret (new SMFSource (s, node));
173                 ret->check_for_analysis_data_on_disk ();
174                 SourceCreated (ret);
175                 return ret;
176         }
177
178         return boost::shared_ptr<Source>();
179 }
180
181 boost::shared_ptr<Source>
182 SourceFactory::createReadable (DataType type, Session& s, const string& path, bool embedded,
183                 int chn, Source::Flag flags, bool announce, bool defer_peaks)
184 {
185         if (type == DataType::AUDIO) {
186
187                 if (!(flags & Destructive)) {
188                         
189                         try {
190                                 
191                                 boost::shared_ptr<Source> ret (new SndFileSource (s, path, embedded, chn, flags));
192                                 
193                                 if (setup_peakfile (ret, defer_peaks)) {
194                                         return boost::shared_ptr<Source>();
195                                 }
196                                 
197                                 ret->check_for_analysis_data_on_disk ();
198                                 if (announce) {
199                                         SourceCreated (ret);
200                                 }
201                                 return ret;
202                         }
203                         
204                         catch (failed_constructor& err) {
205 #ifdef USE_COREAUDIO_FOR_FILES
206                                 
207                                 boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, embedded, chn, flags));
208                                 if (setup_peakfile (ret, defer_peaks)) {
209                                         return boost::shared_ptr<Source>();
210                                 }
211                                 ret->check_for_analysis_data_on_disk ();
212                                 if (announce) {
213                                         SourceCreated (ret);
214                                 }
215                                 return ret;
216                                 
217 #else
218                                 throw; // rethrow
219 #endif
220                         }
221
222                 } else {
223                         // eh?
224                 }
225         
226         } else if (type == DataType::MIDI) {
227                 
228                 boost::shared_ptr<Source> ret (new SMFSource (s, path, embedded, SMFSource::Flag(0)));
229                 
230                 if (announce) {
231                         SourceCreated (ret);
232                 }
233
234                 return ret;
235
236         }
237
238         return boost::shared_ptr<Source>();
239 }
240
241 boost::shared_ptr<Source>
242 SourceFactory::createWritable (DataType type, Session& s, const std::string& path, bool embedded,
243                 bool destructive, nframes_t rate, bool announce, bool defer_peaks)
244 {
245         /* this might throw failed_constructor(), which is OK */
246         
247         if (type == DataType::AUDIO) {
248                 boost::shared_ptr<Source> ret (new SndFileSource (s, path, embedded,
249                                 Config->get_native_file_data_format(),
250                                 Config->get_native_file_header_format(),
251                                 rate,
252                                 (destructive
253                                         ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
254                                         : SndFileSource::default_writable_flags)));     
255
256                 if (setup_peakfile (ret, defer_peaks)) {
257                         return boost::shared_ptr<Source>();
258                 }
259                 
260                 // no analysis data - this is a new file
261
262                 if (announce) {
263                         SourceCreated (ret);
264                 }
265                 return ret;
266
267         } else if (type == DataType::MIDI) {
268
269                 boost::shared_ptr<Source> ret (new SMFSource (s, path, embedded, Source::Flag(0)));
270         
271                 // no analysis data - this is a new file
272                 
273                 if (announce) {
274                         SourceCreated (ret);
275                 }
276                 return ret;
277
278         }
279
280         return boost::shared_ptr<Source> ();
281 }
282