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