Fix sketchy casts.
[ardour.git] / libs / ardour / region_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 */
19
20 #include "pbd/error.h"
21
22 #include "ardour/session.h"
23
24 #include "ardour/region_factory.h"
25 #include "ardour/region.h"
26 #include "ardour/audioregion.h"
27 #include "ardour/audiosource.h"
28 #include "ardour/midi_source.h"
29 #include "ardour/midi_region.h"
30
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35
36 sigc::signal<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
37
38 boost::shared_ptr<Region>
39 RegionFactory::create (boost::shared_ptr<Region> region, nframes_t start,
40                        nframes_t length, const std::string& name,
41                        layer_t layer, Region::Flag flags, bool announce)
42 {
43         boost::shared_ptr<const AudioRegion> other_a;
44         boost::shared_ptr<const MidiRegion> other_m;
45
46         if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
47                 AudioRegion* ar = new AudioRegion (other_a, start, length, name, layer, flags);
48                 boost::shared_ptr<AudioRegion> arp (ar);
49                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (arp));
50                 ret->unlock_property_changes ();
51                 if (announce) {
52                         CheckNewRegion (ret);
53                 }
54                 return ret;
55         } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
56                 MidiRegion* ar = new MidiRegion (other_m, start, length, name, layer, flags);
57                 boost::shared_ptr<MidiRegion> arp (ar);
58                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (arp));
59                 ret->unlock_property_changes ();
60                 if (announce) {
61                         CheckNewRegion (ret);
62                 }
63                 return ret;
64         } else {
65                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
66                       << endmsg;
67                 /*NOTREACHED*/
68                 return boost::shared_ptr<Region>();
69         }
70 }
71
72 boost::shared_ptr<Region>
73 RegionFactory::create (boost::shared_ptr<const Region> region)
74 {
75         boost::shared_ptr<const AudioRegion> ar;
76         boost::shared_ptr<const MidiRegion> mr;
77
78         if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
79                 boost::shared_ptr<Region> ret (new AudioRegion (ar));
80                 ret->unlock_property_changes ();
81                 /* pure copy constructor - no CheckNewRegion emitted */
82                 return ret;
83         } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
84                 boost::shared_ptr<Region> ret (new MidiRegion (mr));
85                 ret->unlock_property_changes ();
86                 /* pure copy constructor - no CheckNewRegion emitted */
87                 return ret;
88         } else {
89                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
90                       << endmsg;
91                 /*NOTREACHED*/
92                 return boost::shared_ptr<Region>();
93         }
94 }
95
96 boost::shared_ptr<Region>
97 RegionFactory::create (boost::shared_ptr<AudioRegion> region, nframes_t start,
98                        nframes_t length, const std::string& name,
99                        layer_t layer, Region::Flag flags, bool announce)
100 {
101         return create (boost::static_pointer_cast<Region> (region), start, length, name, layer, flags, announce);
102 }
103
104 boost::shared_ptr<Region>
105 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs,
106                        const std::string& name, layer_t layer, Region::Flag flags, bool announce)
107
108 {
109         boost::shared_ptr<const AudioRegion> other;
110
111         /* used by AudioFilter when constructing a new region that is intended to have nearly
112            identical settings to an original, but using different sources.
113         */
114
115         if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
116                 AudioRegion* ar = new AudioRegion (other, srcs, srcs.front()->length(srcs.front()->timeline_position()), name, layer, flags);
117                 boost::shared_ptr<AudioRegion> arp (ar);
118                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (arp));
119                 ret->unlock_property_changes ();
120                 if (announce) {
121                         CheckNewRegion (ret);
122                 }
123                 return ret;
124         } else {
125                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
126                       << endmsg;
127                 /*NOTREACHED*/
128                 return boost::shared_ptr<Region>();
129         }
130 }
131
132 boost::shared_ptr<Region>
133 RegionFactory::create (Session& session, XMLNode& node, bool yn)
134 {
135         boost::shared_ptr<Region> r = session.XMLRegionFactory (node, yn);
136
137         if (r) {
138                 r->unlock_property_changes ();
139                 CheckNewRegion (r);
140         }
141
142         return r;
143 }
144
145 boost::shared_ptr<Region>
146 RegionFactory::create (const SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Region::Flag flags, bool announce)
147 {
148         if (srcs.empty()) {
149                 return boost::shared_ptr<Region>();
150         }
151
152         if (srcs[0]->type() == DataType::AUDIO) {
153
154                 AudioRegion* ar = new AudioRegion (srcs, start, length, name, layer, flags);
155                 boost::shared_ptr<AudioRegion> arp (ar);
156                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (arp));
157                 ret->unlock_property_changes ();
158                 if (announce) {
159                         CheckNewRegion (ret);
160                 }
161                 return ret;
162
163         } else if (srcs[0]->type() == DataType::MIDI) {
164
165                 MidiRegion* ar = new MidiRegion (srcs, start, length, name, layer, flags);
166                 boost::shared_ptr<MidiRegion> mrp (ar);
167                 boost::shared_ptr<Region> ret (boost::static_pointer_cast<Region> (mrp));
168                 ret->unlock_property_changes ();
169                 if (announce) {
170                         CheckNewRegion (ret);
171                 }
172                 return ret;
173
174         }
175
176         return boost::shared_ptr<Region> ();
177 }
178
179 boost::shared_ptr<Region>
180 RegionFactory::create (SourceList& srcs, const XMLNode& node)
181 {
182         if (srcs.empty()) {
183                 return boost::shared_ptr<Region>();
184         }
185
186         if (srcs[0]->type() == DataType::AUDIO) {
187                 boost::shared_ptr<Region> ret (new AudioRegion (srcs, node));
188                 ret->unlock_property_changes ();
189                 CheckNewRegion (ret);
190                 return ret;
191         } else if (srcs[0]->type() == DataType::MIDI) {
192                 boost::shared_ptr<Region> ret (new MidiRegion (srcs, node));
193                 ret->unlock_property_changes ();
194                 CheckNewRegion (ret);
195                 return ret;
196         }
197
198         return boost::shared_ptr<Region> ();
199 }
200
201 boost::shared_ptr<Region>
202 RegionFactory::create (boost::shared_ptr<Source> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Region::Flag flags, bool announce)
203 {
204         boost::shared_ptr<AudioSource> as;
205         boost::shared_ptr<MidiSource> ms;
206
207         if ((as = boost::dynamic_pointer_cast<AudioSource>(src)) != 0) {
208                 boost::shared_ptr<Region> ret (new AudioRegion (as, start, length, name, layer, flags));
209                 ret->unlock_property_changes ();
210                 if (announce) {
211                         CheckNewRegion (ret);
212                 }
213                 return ret;
214         } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(src)) != 0) {
215                 boost::shared_ptr<Region> ret (new MidiRegion (ms, start, length, name, layer, flags));
216                 ret->unlock_property_changes ();
217                 if (announce) {
218                         CheckNewRegion (ret);
219                 }
220                 return ret;
221         }
222
223         return boost::shared_ptr<Region>();
224 }