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