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