Merged with trunk R920.
[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 <ardour/source_factory.h>
22 #include <ardour/sndfilesource.h>
23 #include <ardour/smf_source.h>
24 #include <ardour/destructive_filesource.h>
25 #include <ardour/configuration.h>
26
27 #ifdef HAVE_COREAUDIO
28 #include <ardour/coreaudiosource.h>
29 #endif
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace std;
35
36 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
37
38 #ifdef HAVE_COREAUDIO
39 boost::shared_ptr<Source>
40 SourceFactory::create (Session& s, const XMLNode& node)
41 {
42         DataType type = DataType::AUDIO;
43         const XMLProperty* prop = node.property("type");
44         if (prop) {
45                 type = DataType(prop->value());
46         }
47
48         if (type == DataType::AUDIO) {
49                 
50                 if (node.property (X_("destructive")) != 0) {
51                         
52                         boost::shared_ptr<Source> ret (new DestructiveFileSource (s, node));
53                         SourceCreated (ret);
54                         return ret;
55                 
56                 } else {
57                 
58                 try {
59                         boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
60                         SourceCreated (ret);
61                         return ret;
62                 
63                 } catch (failed_constructor& err) {
64
65                         try {
66                                 boost::shared_ptr<Source> ret (new CoreAudioSource (node));
67                                 SourceCreated (ret);
68                                 return ret;
69                         } 
70
71
72                         catch (failed_constructor& err) {
73                                 boost::shared_ptr<Source> ret (new SndFileSource (node));
74                                 SourceCreated (ret);
75                                 return ret;
76                         }
77                 }
78
79         } else if (type == DataType::MIDI) {
80                 
81                 boost::shared_ptr<Source> ret (new SMFSource (node));
82                 SourceCreated (ret);
83                 return ret;
84
85         }
86         
87         return boost::shared_ptr<Source>();
88 }
89
90 #else
91
92 boost::shared_ptr<Source>
93 SourceFactory::create (Session& s, const XMLNode& node)
94 {
95         DataType type = DataType::AUDIO;
96         const XMLProperty* prop = node.property("type");
97         if (prop) {
98                 type = DataType(prop->value());
99         }
100
101         if (type == DataType::AUDIO) {
102                 
103                 if (node.property (X_("destructive")) != 0) {
104
105                         boost::shared_ptr<Source> ret (new DestructiveFileSource (s, node));
106                         SourceCreated (ret);
107                         return ret;
108
109                 } else {
110
111                         boost::shared_ptr<Source> ret (new SndFileSource (s, node));
112                         SourceCreated (ret);
113                         return ret;
114                 }
115
116         } else if (type == DataType::MIDI) {
117                 
118                 boost::shared_ptr<Source> ret (new SMFSource (s, node));
119                 SourceCreated (ret);
120                 return ret;
121
122         }
123         
124         return boost::shared_ptr<Source> ();
125 }
126
127 #endif // HAVE_COREAUDIO
128
129 #ifdef HAVE_COREAUDIO
130 boost::shared_ptr<Source>
131 SourceFactory::createReadable (DataType type, Session& s, string idstr, AudioFileSource::Flag flags, bool announce)
132 {
133         if (type == DataType::AUDIO) {
134                 if (flags & Destructive) {
135                         boost::shared_ptr<Source> ret (new DestructiveFileSource (s, idstr, flags));
136                         if (announce) {
137                                 SourceCreated (ret);
138                         }
139                         return ret;
140         
141                         try {
142                                 boost::shared_ptr<Source> ret (new CoreAudioSource (s, idstr, flags));
143                                 if (announce) {
144                                         SourceCreated (ret);
145                                 }
146                                 return ret;
147                 
148                         } catch (failed_constructor& err) {
149                                 boost::shared_ptr<Source> ret (new SndFileSource (s, idstr, flags));
150                                 if (announce) {
151                                         SourceCreated (ret);
152                                 }
153                                 return ret;
154                         }
155
156         } else if (type == DataType::MIDI) {
157
158                 boost::shared_ptr<Source> ret (new SMFSource (s, node));
159                 SourceCreated (ret);
160                 return ret;
161
162         }
163
164         return boost::shared_ptr<Source>();
165 }
166
167 #else
168
169 boost::shared_ptr<Source>
170 SourceFactory::createReadable (DataType type, Session& s, string idstr, AudioFileSource::Flag flags, bool announce)
171 {
172         if (type == DataType::AUDIO) {
173         
174                 boost::shared_ptr<Source> ret (new SndFileSource (s, idstr, flags));
175                 if (announce) {
176                         SourceCreated (ret);
177                 }
178                 return ret;
179
180         } else if (type == DataType::MIDI) {
181
182                 boost::shared_ptr<Source> ret (new SMFSource (s, idstr, SMFSource::Flag(0))); // FIXME: flags?
183                 if (announce) {
184                         SourceCreated (ret);
185                 }
186                 return ret;
187
188         }
189         
190         return boost::shared_ptr<Source> ();
191 }
192
193 #endif // HAVE_COREAUDIO
194
195 boost::shared_ptr<Source>
196 SourceFactory::createWritable (DataType type, Session& s, std::string path, bool destructive, jack_nframes_t rate, bool announce)
197 {
198         /* this might throw failed_constructor(), which is OK */
199
200         if (type == DataType::AUDIO) {
201                 if (destructive) {
202                         
203                         boost::shared_ptr<Source> ret (new DestructiveFileSource (s, path,
204                                                 Config->get_native_file_data_format(),
205                                                 Config->get_native_file_header_format(),
206                                                 rate));
207                         if (announce) {
208                                 SourceCreated (ret);
209                         }
210                         return ret;
211
212                 } else {
213                         
214                         boost::shared_ptr<Source> ret (new SndFileSource (s, path, 
215                                                 Config->get_native_file_data_format(),
216                                                 Config->get_native_file_header_format(),
217                                                 rate));
218                         if (announce) {
219                                 SourceCreated (ret);
220                         }
221                         return ret;
222
223                 }
224         
225         } else if (type == DataType::MIDI) {
226
227                 boost::shared_ptr<Source> ret (new SMFSource (s, path));
228                 SourceCreated (ret);
229                 return ret;
230         
231         }
232
233         return boost::shared_ptr<Source> ();
234 }