used shared_ptr<Source>, somewhat successfully
[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/destructive_filesource.h>
24 #include <ardour/configuration.h>
25
26 #include "i18n.h"
27
28 using namespace ARDOUR;
29 using namespace std;
30
31 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
32
33 #ifdef HAVE_COREAUDIO
34
35
36 boost::shared_ptr<Source>
37 SourceFactory::create (const XMLNode& node)
38 {
39         if (node.property (X_("destructive")) != 0) {
40                 
41                 boost::shared_ptr<Source> ret (new DestructiveFileSource (node));
42                 SourceCreated (ret);
43                 return ret;
44                 
45         } else {
46                 
47                 try {
48                         boost::shared_ptr<Source> ret (new CoreAudioSource (node));
49                         SourceCreated (ret);
50                         return ret;
51                 } 
52                 
53                 
54                 catch (failed_constructor& err) {
55                         boost::shared_ptr<Source> ret (new SndFileSource (node));
56                         SourceCreated (ret);
57                         return ret;
58                 }
59         }
60         
61         return boost::shared_ptr<Source>();
62 }
63
64 #else
65
66 boost::shared_ptr<Source>
67 SourceFactory::create (const XMLNode& node)
68 {
69         if (node.property (X_("destructive")) != 0) {
70                 
71                 boost::shared_ptr<Source> ret (new DestructiveFileSource (node));
72                 SourceCreated (ret);
73                 return ret;
74                 
75         } else {
76                 
77                 boost::shared_ptr<Source> ret (new SndFileSource (node));
78                 SourceCreated (ret);
79                 return ret;
80         }
81 }
82
83 #endif // HAVE_COREAUDIO
84
85 #ifdef HAVE_COREAUDIO
86 boost::shared_ptr<Source>
87 SourceFactory::createReadable (string idstr, AudioFileSource::Flag flags, bool announce)
88 {
89         if (flags & Destructive) {
90                 boost::shared_ptr<Source> ret (new DestructiveFileSource (idstr, flags));
91                 if (announce) {
92                         SourceCreated (ret);
93                 }
94                 return ret;
95         }
96
97         try {
98                 boost::shared_ptr<Source> ret (new CoreAudioSource (idstr, flags));
99                 if (announce) {
100                         SourceCreated (ret);
101                 }
102                 return ret;
103         }
104
105         catch (failed_constructor& err) {
106                 boost::shared_ptr<Source> ret (new SndFileSource (idstr, flags));
107                 if (announce) {
108                         SourceCreated (ret);
109                 }
110                 return ret;
111         }
112
113         return boost::shared_ptr<Source>();
114 }
115
116 #else
117
118 boost::shared_ptr<Source>
119 SourceFactory::createReadable (string idstr, AudioFileSource::Flag flags, bool announce)
120 {
121         boost::shared_ptr<Source> ret (new SndFileSource (idstr, flags));
122         if (announce) {
123                 SourceCreated (ret);
124         }
125         return ret;
126 }
127
128 #endif // HAVE_COREAUDIO
129
130 boost::shared_ptr<Source>
131 SourceFactory::createWritable (std::string path, bool destructive, jack_nframes_t rate, bool announce)
132 {
133         /* this might throw failed_constructor(), which is OK */
134         
135         if (destructive) {
136                 boost::shared_ptr<Source> ret (new DestructiveFileSource (path,
137                                                                           Config->get_native_file_data_format(),
138                                                                           Config->get_native_file_header_format(),
139                                                                           rate));
140                 if (announce) {
141                         SourceCreated (ret);
142                 }
143                 return ret;
144                 
145         } else {
146                 boost::shared_ptr<Source> ret (new SndFileSource (path, 
147                                                                   Config->get_native_file_data_format(),
148                                                                   Config->get_native_file_header_format(),
149                                                                   rate));
150                 if (announce) {
151                         SourceCreated (ret);
152                 }
153                 return ret;
154         }
155 }