fix up reload of existing session by avoiding stupid early return in a RegionFactory...
[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 #include "pbd/boost_debug.h"
22
23 #include "ardour/session.h"
24
25 #include "ardour/region_factory.h"
26 #include "ardour/region.h"
27 #include "ardour/audioregion.h"
28 #include "ardour/audiosource.h"
29 #include "ardour/midi_source.h"
30 #include "ardour/midi_region.h"
31
32 #include "i18n.h"
33
34 using namespace ARDOUR;
35 using namespace PBD;
36
37 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
38 map<PBD::ID,boost::weak_ptr<Region> > RegionFactory::region_map;
39
40 boost::shared_ptr<Region>
41 RegionFactory::create (boost::shared_ptr<const Region> region)
42 {
43         boost::shared_ptr<Region> ret;
44         boost::shared_ptr<const AudioRegion> ar;
45         boost::shared_ptr<const MidiRegion> mr;
46
47         if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
48
49                 AudioRegion* arn = new AudioRegion (ar, 0, true);
50                 boost_debug_shared_ptr_mark_interesting (arn, "Region");
51
52                 boost::shared_ptr<AudioRegion> arp (arn);
53                 ret = boost::static_pointer_cast<Region> (arp);
54
55         } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
56
57                 MidiRegion* mrn = new MidiRegion (mr, 0, true);
58                 boost::shared_ptr<MidiRegion> mrp (mrn);
59                 ret = boost::static_pointer_cast<Region> (mrp);
60
61         } else {
62                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
63                       << endmsg;
64                 /*NOTREACHED*/
65         }
66
67         if (ret) {
68                 ret->unlock_property_changes ();
69                 map_add (ret);
70
71                 /* pure copy constructor - no property list */
72                 /* pure copy constructor - no CheckNewRegion emitted */
73         }
74
75         return ret;
76 }
77
78 boost::shared_ptr<Region>
79 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
80 {
81         boost::shared_ptr<Region> ret;
82         boost::shared_ptr<const AudioRegion> other_a;
83         boost::shared_ptr<const MidiRegion> other_m;
84
85         if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
86
87                 AudioRegion* ar = new AudioRegion (other_a, offset, true);
88                 boost_debug_shared_ptr_mark_interesting (ar, "Region");
89
90                 boost::shared_ptr<AudioRegion> arp (ar);
91                 ret = boost::static_pointer_cast<Region> (arp);
92
93         } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
94
95                 MidiRegion* mr = new MidiRegion (other_m, offset, true);
96                 boost::shared_ptr<MidiRegion> mrp (mr);
97                 ret = boost::static_pointer_cast<Region> (mrp);
98
99         } else {
100                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
101                       << endmsg;
102                 /*NOTREACHED*/
103                 return boost::shared_ptr<Region>();
104         }
105
106         if (ret) {
107
108                 ret->set_properties (plist);
109                 ret->unlock_property_changes ();
110
111                 map_add (ret);
112
113                 if (announce) {
114                         CheckNewRegion (ret);
115                 }
116         }
117
118         return ret;
119 }
120
121 boost::shared_ptr<Region>
122 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
123 {
124         boost::shared_ptr<Region> ret;
125         boost::shared_ptr<const AudioRegion> other_a;
126         boost::shared_ptr<const MidiRegion> other_m;
127
128         if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
129
130                 AudioRegion* ar = new AudioRegion (other_a, 0, false);
131                 boost_debug_shared_ptr_mark_interesting (ar, "Region");
132
133                 boost::shared_ptr<AudioRegion> arp (ar);
134                 ret = boost::static_pointer_cast<Region> (arp);
135
136         } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
137
138                 MidiRegion* mr = new MidiRegion (other_m, 0, false);
139                 boost::shared_ptr<MidiRegion> mrp (mr);
140                 ret = boost::static_pointer_cast<Region> (mrp);
141
142         } else {
143                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
144                       << endmsg;
145                 /*NOTREACHED*/
146                 return boost::shared_ptr<Region>();
147         }
148
149         if (ret) {
150
151                 ret->set_properties (plist);
152                 ret->unlock_property_changes ();
153
154                 map_add (ret);
155
156                 if (announce) {
157                         CheckNewRegion (ret);
158                 }
159         }
160
161         return ret;
162 }
163
164
165
166
167 boost::shared_ptr<Region>
168 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
169 {
170         boost::shared_ptr<Region> ret;
171         boost::shared_ptr<const AudioRegion> other;
172
173         /* used by AudioFilter when constructing a new region that is intended to have nearly
174            identical settings to an original, but using different sources.
175         */
176
177         if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
178
179                 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
180                 
181                 AudioRegion* ar = new AudioRegion (other, srcs);
182                 boost_debug_shared_ptr_mark_interesting (ar, "Region");
183
184                 boost::shared_ptr<AudioRegion> arp (ar);
185                 ret = boost::static_pointer_cast<Region> (arp);
186
187         } else {
188                 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
189                       << endmsg;
190                 /*NOTREACHED*/
191         }
192
193         if (ret) {
194
195                 ret->set_properties (plist);
196                 ret->unlock_property_changes ();
197
198                 map_add (ret);
199
200                 if (announce) {
201                         CheckNewRegion (ret);
202                 }
203         }
204
205         return ret;
206
207 }
208
209 boost::shared_ptr<Region>
210 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
211 {
212         boost::shared_ptr<Region> ret; 
213         boost::shared_ptr<AudioSource> as;
214         boost::shared_ptr<MidiSource> ms;
215
216         if ((as = boost::dynamic_pointer_cast<AudioSource>(src)) != 0) {
217
218                 AudioRegion* ar = new AudioRegion (as);
219                 boost_debug_shared_ptr_mark_interesting (ar, "Region");
220
221                 boost::shared_ptr<AudioRegion> arp (ar);
222                 ret = boost::static_pointer_cast<Region> (arp);
223
224         } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(src)) != 0) {
225                 MidiRegion* mr = new MidiRegion (ms);
226                 boost_debug_shared_ptr_mark_interesting (mr, "Region");
227
228                 boost::shared_ptr<MidiRegion> mrp (mr);
229                 ret = boost::static_pointer_cast<Region> (mrp);
230         }
231
232         if (ret) {
233                 ret->set_properties (plist);
234                 ret->unlock_property_changes ();
235
236                 map_add (ret);
237
238                 if (announce) {
239                         CheckNewRegion (ret);
240                 }
241         }
242
243         return ret;
244 }
245
246 boost::shared_ptr<Region>
247 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
248 {
249         boost::shared_ptr<Region> ret; 
250         boost::shared_ptr<AudioSource> as;
251         boost::shared_ptr<MidiSource> ms;
252
253         if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
254
255                 AudioRegion* ar = new AudioRegion (srcs);
256                 boost_debug_shared_ptr_mark_interesting (ar, "Region");
257
258                 boost::shared_ptr<AudioRegion> arp (ar);
259                 ret = boost::static_pointer_cast<Region> (arp);
260
261         } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
262                 MidiRegion* mr = new MidiRegion (srcs);
263                 boost_debug_shared_ptr_mark_interesting (mr, "Region");
264
265                 boost::shared_ptr<MidiRegion> mrp (mr);
266                 ret = boost::static_pointer_cast<Region> (mrp);
267         }
268
269         if (ret) {
270                 ret->set_properties (plist);
271                 ret->unlock_property_changes ();
272
273                 map_add (ret);
274
275                 if (announce) {
276                         CheckNewRegion (ret);
277                 }
278         }
279
280         return ret;
281 }
282
283 boost::shared_ptr<Region>
284 RegionFactory::create (Session& session, XMLNode& node, bool yn)
285 {
286         boost::shared_ptr<Region> r = session.XMLRegionFactory (node, yn);
287
288         if (r) {
289                 r->unlock_property_changes ();
290                 map_add (r);
291                 CheckNewRegion (r);
292         }
293
294         return r;
295 }
296
297
298 boost::shared_ptr<Region>
299 RegionFactory::create (SourceList& srcs, const XMLNode& node)
300 {
301         boost::shared_ptr<Region> ret;
302
303         if (srcs.empty()) {
304                 return ret;
305         }
306
307         if (srcs[0]->type() == DataType::AUDIO) {
308
309                 AudioRegion* ar = new AudioRegion (srcs, node);
310                 boost_debug_shared_ptr_mark_interesting (ar, "Region");
311
312                 boost::shared_ptr<AudioRegion> arp (ar);
313                 ret = boost::static_pointer_cast<Region> (arp);
314
315         } else if (srcs[0]->type() == DataType::MIDI) {
316                 
317                 MidiRegion* mr = new MidiRegion (srcs, node);
318
319                 boost::shared_ptr<MidiRegion> mrp (mr);
320                 ret = boost::static_pointer_cast<Region> (mrp);
321         }
322
323         if (ret) {
324                 ret->unlock_property_changes ();
325                 map_add (ret);
326                 CheckNewRegion (ret);
327         }
328
329         return ret;
330 }
331
332
333 void
334 RegionFactory::map_add (boost::shared_ptr<Region> r)
335 {
336         pair<ID,boost::weak_ptr<Region> > p;
337         p.first = r->id();
338         p.second = r;
339
340         region_map.insert (p);
341 }
342
343 boost::shared_ptr<Region>
344 RegionFactory::region_by_id (const PBD::ID& id)
345 {
346         map<ID,boost::weak_ptr<Region> >::iterator i = region_map.find (id);
347
348         if (i == region_map.end()) {
349                 return boost::shared_ptr<Region>();
350         }
351
352         return i->second.lock();
353 }
354         
355 void
356 RegionFactory::clear_map ()
357 {
358         region_map.clear ();
359 }