merged with 1697 revision of trunk (which is post-rc1 but pre-rc2
[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
23 #include <ardour/source_factory.h>
24 #include <ardour/sndfilesource.h>
25 #include <ardour/silentfilesource.h>
26 #include <ardour/configuration.h>
27
28 #ifdef HAVE_COREAUDIO
29 #include <ardour/coreaudiosource.h>
30 #endif
31
32 #include "i18n.h"
33
34 using namespace ARDOUR;
35 using namespace std;
36 using namespace PBD;
37
38 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
39
40 int
41 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s)
42 {
43         boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
44         if (as) {
45                 if (as->setup_peakfile ()) {
46                         error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
47                         return -1;
48                 }
49         }
50
51         return 0;
52 }
53
54 boost::shared_ptr<Source>
55 SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
56 {
57         boost::shared_ptr<Source> ret (new SilentFileSource (s, node, nframes, sr));
58         SourceCreated (ret);
59         return ret;
60 }
61
62 #ifdef HAVE_COREAUDIO
63 boost::shared_ptr<Source>
64 SourceFactory::create (Session& s, const XMLNode& node)
65 {
66         try {
67                 boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
68                 if (setup_peakfile (ret)) {
69                         return boost::shared_ptr<Source>();
70                 }
71                 SourceCreated (ret);
72                 return ret;
73         } 
74
75
76         catch (failed_constructor& err) {       
77
78                 /* this is allowed to throw */
79
80                 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
81                 if (setup_peakfile (ret)) {
82                         return boost::shared_ptr<Source>();
83                 }
84                 SourceCreated (ret);
85                 return ret;
86         }
87
88         return boost::shared_ptr<Source>();
89 }
90
91 #else
92
93 boost::shared_ptr<Source>
94 SourceFactory::create (Session& s, const XMLNode& node)
95 {
96         /* this is allowed to throw */
97
98         boost::shared_ptr<Source> ret (new SndFileSource (s, node));
99         
100         if (setup_peakfile (ret)) {
101                 return boost::shared_ptr<Source>();
102         }
103         
104         SourceCreated (ret);
105         return ret;
106 }
107
108 #endif // HAVE_COREAUDIO
109
110 #ifdef HAVE_COREAUDIO
111 boost::shared_ptr<Source>
112 SourceFactory::createReadable (Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce)
113 {
114         if (!(flags & Destructive)) {
115
116                 try {
117                         boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, chn, flags));
118                         if (setup_peakfile (ret)) {
119                                 return boost::shared_ptr<Source>();
120                         }
121                         if (announce) {
122                                 SourceCreated (ret);
123                         }
124                         return ret;
125                 }
126                 
127                 catch (failed_constructor& err) {
128
129                         /* this is allowed to throw */
130
131                         boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
132                         if (setup_peakfile (ret)) {
133                                 return boost::shared_ptr<Source>();
134                         }
135                         if (announce) {
136                                 SourceCreated (ret);
137                         }
138                         return ret;
139                 }
140
141         } else {
142
143                 boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
144                 if (setup_peakfile (ret)) {
145                         return boost::shared_ptr<Source>();
146                 }
147                 if (announce) {
148                         SourceCreated (ret);
149                 }
150                 return ret;
151         }
152
153         return boost::shared_ptr<Source>();
154 }
155
156 #else
157
158 boost::shared_ptr<Source>
159 SourceFactory::createReadable (Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce)
160 {
161         boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
162
163         if (setup_peakfile (ret)) {
164                 return boost::shared_ptr<Source>();
165         }
166
167         if (announce) {
168                 SourceCreated (ret);
169         }
170
171         return ret;
172 }
173
174 #endif // HAVE_COREAUDIO
175
176 boost::shared_ptr<Source>
177 SourceFactory::createWritable (Session& s, std::string path, bool destructive, nframes_t rate, bool announce)
178 {
179         /* this might throw failed_constructor(), which is OK */
180
181         boost::shared_ptr<Source> ret (new SndFileSource 
182                                        (s, path, 
183                                         Config->get_native_file_data_format(),
184                                         Config->get_native_file_header_format(),
185                                         rate,
186                                         (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
187                                          SndFileSource::default_writable_flags)));      
188
189         if (setup_peakfile (ret)) {
190                 return boost::shared_ptr<Source>();
191         }
192         if (announce) {
193                 SourceCreated (ret);
194         }
195         return ret;
196 }