new tape tracks ended up with non-destructive sources, fixed
[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/destructive_filesource.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 #ifdef HAVE_COREAUDIO
55 boost::shared_ptr<Source>
56 SourceFactory::create (Session& s, const XMLNode& node)
57 {
58         try {
59                 boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
60                 if (setup_peakfile (ret)) {
61                         return boost::shared_ptr<Source>();
62                 }
63                 SourceCreated (ret);
64                 return ret;
65         } 
66         
67         
68         catch (failed_constructor& err) {
69                 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
70                 if (setup_peakfile (ret)) {
71                         return boost::shared_ptr<Source>();
72                 }
73                 SourceCreated (ret);
74                 return ret;
75         }
76
77         return boost::shared_ptr<Source>();
78 }
79
80 #else
81
82 boost::shared_ptr<Source>
83 SourceFactory::create (Session& s, const XMLNode& node)
84 {
85         boost::shared_ptr<Source> ret (new SndFileSource (s, node));
86         if (setup_peakfile (ret)) {
87                 return boost::shared_ptr<Source>();
88         }
89         SourceCreated (ret);
90         return ret;
91 }
92
93 #endif // HAVE_COREAUDIO
94
95 #ifdef HAVE_COREAUDIO
96 boost::shared_ptr<Source>
97 SourceFactory::createReadable (Session& s, string idstr, AudioFileSource::Flag flags, bool announce)
98 {
99         if (!(flags & Destructive)) {
100
101                 try {
102                         boost::shared_ptr<Source> ret (new CoreAudioSource (s, idstr, flags));
103                         if (setup_peakfile (ret)) {
104                                 return boost::shared_ptr<Source>();
105                         }
106                         if (announce) {
107                                 SourceCreated (ret);
108                         }
109                         return ret;
110                 }
111                 
112                 catch (failed_constructor& err) {
113                         boost::shared_ptr<Source> ret (new SndFileSource (s, idstr, flags));
114                         if (setup_peakfile (ret)) {
115                                 return boost::shared_ptr<Source>();
116                         }
117                         if (announce) {
118                                 SourceCreated (ret);
119                         }
120                         return ret;
121                 }
122
123         } else {
124
125                 boost::shared_ptr<Source> ret (new SndFileSource (s, idstr, flags));
126                 if (setup_peakfile (ret)) {
127                         return boost::shared_ptr<Source>();
128                 }
129                 if (announce) {
130                         SourceCreated (ret);
131                 }
132                 return ret;
133         }
134
135         return boost::shared_ptr<Source>();
136 }
137
138 #else
139
140 boost::shared_ptr<Source>
141 SourceFactory::createReadable (Session& s, string idstr, AudioFileSource::Flag flags, bool announce)
142 {
143         boost::shared_ptr<Source> ret (new SndFileSource (s, idstr, 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 #endif // HAVE_COREAUDIO
154
155 boost::shared_ptr<Source>
156 SourceFactory::createWritable (Session& s, std::string path, bool destructive, nframes_t rate, bool announce)
157 {
158         /* this might throw failed_constructor(), which is OK */
159
160         boost::shared_ptr<Source> ret (new SndFileSource 
161                                        (s, path, 
162                                         Config->get_native_file_data_format(),
163                                         Config->get_native_file_header_format(),
164                                         rate,
165                                         (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
166                                          SndFileSource::default_writable_flags)));      
167
168         if (setup_peakfile (ret)) {
169                 return boost::shared_ptr<Source>();
170         }
171         if (announce) {
172                 SourceCreated (ret);
173         }
174         return ret;
175 }