Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[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, string path, int chn, AudioFileSource::Flag flags, bool announce, bool defer_peaks)
183 {
184         if (type == DataType::AUDIO) {
185
186                 if (!(flags & Destructive)) {
187                         
188                         try {
189                                 
190                                 boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
191                                 
192                                 if (setup_peakfile (ret, defer_peaks)) {
193                                         return boost::shared_ptr<Source>();
194                                 }
195                                 
196                                 ret->check_for_analysis_data_on_disk ();
197                                 if (announce) {
198                                         SourceCreated (ret);
199                                 }
200                                 return ret;
201                         }
202                         
203                         catch (failed_constructor& err) {
204 #ifdef USE_COREAUDIO_FOR_FILES
205                                 
206                                 boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, chn, flags));
207                                 if (setup_peakfile (ret, defer_peaks)) {
208                                         return boost::shared_ptr<Source>();
209                                 }
210                                 ret->check_for_analysis_data_on_disk ();
211                                 if (announce) {
212                                         SourceCreated (ret);
213                                 }
214                                 return ret;
215                                 
216 #else
217                                 throw; // rethrow
218 #endif
219                         }
220
221                 } else {
222                         // eh?
223                 }
224         
225         } else if (type == DataType::MIDI) {
226                 
227                 // FIXME: flags?
228                 boost::shared_ptr<Source> ret (new SMFSource (s, path, 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, std::string path, bool destructive, nframes_t rate, bool announce, bool defer_peaks)
243 {
244         /* this might throw failed_constructor(), which is OK */
245         
246         if (type == DataType::AUDIO) {
247                 boost::shared_ptr<Source> ret (new SndFileSource 
248                                                (s, path, 
249                                                 Config->get_native_file_data_format(),
250                                                 Config->get_native_file_header_format(),
251                                                 rate,
252                                                 (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
253                                                  SndFileSource::default_writable_flags)));      
254
255                 if (setup_peakfile (ret, defer_peaks)) {
256                         return boost::shared_ptr<Source>();
257                 }
258                 
259                 // no analysis data - this is a new file
260
261                 if (announce) {
262                         SourceCreated (ret);
263                 }
264                 return ret;
265
266         } else if (type == DataType::MIDI) {
267
268                 boost::shared_ptr<Source> ret (new SMFSource (s, path));
269         
270                 // no analysis data - this is a new file
271                 
272                 if (announce) {
273                         SourceCreated (ret);
274                 }
275                 return ret;
276
277         }
278
279         return boost::shared_ptr<Source> ();
280 }