change PropertyChange from a bitfield into a real object, with all the many widesprea...
[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         SourceList srcs;
213         srcs.push_back (src);
214         return create (srcs, plist, announce);
215 }
216
217 boost::shared_ptr<Region>
218 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
219 {
220         boost::shared_ptr<Region> ret; 
221         boost::shared_ptr<AudioSource> as;
222         boost::shared_ptr<MidiSource> ms;
223
224         if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
225
226                 AudioRegion* ar = new AudioRegion (srcs);
227                 boost_debug_shared_ptr_mark_interesting (ar, "Region");
228
229                 boost::shared_ptr<AudioRegion> arp (ar);
230                 ret = boost::static_pointer_cast<Region> (arp);
231
232         } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
233                 MidiRegion* mr = new MidiRegion (srcs);
234                 boost_debug_shared_ptr_mark_interesting (mr, "Region");
235
236                 boost::shared_ptr<MidiRegion> mrp (mr);
237                 ret = boost::static_pointer_cast<Region> (mrp);
238         }
239
240         if (ret) {
241                 ret->set_properties (plist);
242                 ret->unlock_property_changes ();
243
244                 map_add (ret);
245
246                 if (announce) {
247                         CheckNewRegion (ret);
248                 }
249         }
250
251         return ret;
252 }
253
254 boost::shared_ptr<Region>
255 RegionFactory::create (Session& session, XMLNode& node, bool yn)
256 {
257         return session.XMLRegionFactory (node, yn);
258 }
259
260 boost::shared_ptr<Region>
261 RegionFactory::create (SourceList& srcs, const XMLNode& node)
262 {
263         boost::shared_ptr<Region> ret;
264
265         if (srcs.empty()) {
266                 return ret;
267         }
268
269         if (srcs[0]->type() == DataType::AUDIO) {
270
271                 AudioRegion* ar = new AudioRegion (srcs);
272                 boost_debug_shared_ptr_mark_interesting (ar, "Region");
273
274                 boost::shared_ptr<AudioRegion> arp (ar);
275                 ret = boost::static_pointer_cast<Region> (arp);
276
277         } else if (srcs[0]->type() == DataType::MIDI) {
278                 
279                 MidiRegion* mr = new MidiRegion (srcs);
280
281                 boost::shared_ptr<MidiRegion> mrp (mr);
282                 ret = boost::static_pointer_cast<Region> (mrp);
283         }
284
285         if (ret) {
286
287                 if (ret->set_state (node, Stateful::loading_state_version)) {
288                         ret.reset ();
289                 } else {
290                         ret->unlock_property_changes ();
291                         map_add (ret);
292                         CheckNewRegion (ret);
293                 }
294         }
295
296         return ret;
297 }
298
299
300 void
301 RegionFactory::map_add (boost::shared_ptr<Region> r)
302 {
303         pair<ID,boost::weak_ptr<Region> > p;
304         p.first = r->id();
305         p.second = r;
306
307         region_map.insert (p);
308 }
309
310 boost::shared_ptr<Region>
311 RegionFactory::region_by_id (const PBD::ID& id)
312 {
313         map<ID,boost::weak_ptr<Region> >::iterator i = region_map.find (id);
314
315         if (i == region_map.end()) {
316                 return boost::shared_ptr<Region>();
317         }
318
319         return i->second.lock();
320 }
321         
322 void
323 RegionFactory::clear_map ()
324 {
325         region_map.clear ();
326 }