2 Copyright (C) 2000-2006 Paul Davis
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.
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.
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.
20 #include "pbd/error.h"
21 #include "pbd/boost_debug.h"
23 #include "ardour/session.h"
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"
34 using namespace ARDOUR;
37 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
38 map<PBD::ID,boost::weak_ptr<Region> > RegionFactory::region_map;
40 boost::shared_ptr<Region>
41 RegionFactory::create (boost::shared_ptr<const Region> region)
43 boost::shared_ptr<Region> ret;
44 boost::shared_ptr<const AudioRegion> ar;
45 boost::shared_ptr<const MidiRegion> mr;
47 if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
49 AudioRegion* arn = new AudioRegion (ar, 0, true);
50 boost_debug_shared_ptr_mark_interesting (arn, "Region");
52 boost::shared_ptr<AudioRegion> arp (arn);
53 ret = boost::static_pointer_cast<Region> (arp);
55 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
57 MidiRegion* mrn = new MidiRegion (mr, 0, true);
58 boost::shared_ptr<MidiRegion> mrp (mrn);
59 ret = boost::static_pointer_cast<Region> (mrp);
62 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
68 ret->unlock_property_changes ();
71 /* pure copy constructor - no property list */
72 /* pure copy constructor - no CheckNewRegion emitted */
78 boost::shared_ptr<Region>
79 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
81 boost::shared_ptr<Region> ret;
82 boost::shared_ptr<const AudioRegion> other_a;
83 boost::shared_ptr<const MidiRegion> other_m;
85 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
87 AudioRegion* ar = new AudioRegion (other_a, offset, true);
88 boost_debug_shared_ptr_mark_interesting (ar, "Region");
90 boost::shared_ptr<AudioRegion> arp (ar);
91 ret = boost::static_pointer_cast<Region> (arp);
93 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
95 MidiRegion* mr = new MidiRegion (other_m, offset, true);
96 boost::shared_ptr<MidiRegion> mrp (mr);
97 ret = boost::static_pointer_cast<Region> (mrp);
100 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
103 return boost::shared_ptr<Region>();
108 ret->set_properties (plist);
109 ret->unlock_property_changes ();
114 CheckNewRegion (ret);
121 boost::shared_ptr<Region>
122 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
124 boost::shared_ptr<Region> ret;
125 boost::shared_ptr<const AudioRegion> other_a;
126 boost::shared_ptr<const MidiRegion> other_m;
128 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
130 AudioRegion* ar = new AudioRegion (other_a, 0, false);
131 boost_debug_shared_ptr_mark_interesting (ar, "Region");
133 boost::shared_ptr<AudioRegion> arp (ar);
134 ret = boost::static_pointer_cast<Region> (arp);
136 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
138 MidiRegion* mr = new MidiRegion (other_m, 0, false);
139 boost::shared_ptr<MidiRegion> mrp (mr);
140 ret = boost::static_pointer_cast<Region> (mrp);
143 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
146 return boost::shared_ptr<Region>();
151 ret->set_properties (plist);
152 ret->unlock_property_changes ();
157 CheckNewRegion (ret);
167 boost::shared_ptr<Region>
168 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
170 boost::shared_ptr<Region> ret;
171 boost::shared_ptr<const AudioRegion> other;
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.
177 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
179 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
181 AudioRegion* ar = new AudioRegion (other, srcs);
182 boost_debug_shared_ptr_mark_interesting (ar, "Region");
184 boost::shared_ptr<AudioRegion> arp (ar);
185 ret = boost::static_pointer_cast<Region> (arp);
188 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
195 ret->set_properties (plist);
196 ret->unlock_property_changes ();
201 CheckNewRegion (ret);
209 boost::shared_ptr<Region>
210 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
212 boost::shared_ptr<Region> ret;
213 boost::shared_ptr<AudioSource> as;
214 boost::shared_ptr<MidiSource> ms;
216 if ((as = boost::dynamic_pointer_cast<AudioSource>(src)) != 0) {
218 AudioRegion* ar = new AudioRegion (as);
219 boost_debug_shared_ptr_mark_interesting (ar, "Region");
221 boost::shared_ptr<AudioRegion> arp (ar);
222 ret = boost::static_pointer_cast<Region> (arp);
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");
228 boost::shared_ptr<MidiRegion> mrp (mr);
229 ret = boost::static_pointer_cast<Region> (mrp);
233 ret->set_properties (plist);
234 ret->unlock_property_changes ();
239 CheckNewRegion (ret);
246 boost::shared_ptr<Region>
247 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
249 boost::shared_ptr<Region> ret;
250 boost::shared_ptr<AudioSource> as;
251 boost::shared_ptr<MidiSource> ms;
253 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
255 AudioRegion* ar = new AudioRegion (srcs);
256 boost_debug_shared_ptr_mark_interesting (ar, "Region");
258 boost::shared_ptr<AudioRegion> arp (ar);
259 ret = boost::static_pointer_cast<Region> (arp);
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");
265 boost::shared_ptr<MidiRegion> mrp (mr);
266 ret = boost::static_pointer_cast<Region> (mrp);
270 ret->set_properties (plist);
271 ret->unlock_property_changes ();
276 CheckNewRegion (ret);
283 boost::shared_ptr<Region>
284 RegionFactory::create (Session& session, XMLNode& node, bool yn)
286 boost::shared_ptr<Region> r = session.XMLRegionFactory (node, yn);
289 r->unlock_property_changes ();
298 boost::shared_ptr<Region>
299 RegionFactory::create (SourceList& srcs, const XMLNode& node)
301 boost::shared_ptr<Region> ret;
307 if (srcs[0]->type() == DataType::AUDIO) {
309 AudioRegion* ar = new AudioRegion (srcs, node);
310 boost_debug_shared_ptr_mark_interesting (ar, "Region");
312 boost::shared_ptr<AudioRegion> arp (ar);
313 ret = boost::static_pointer_cast<Region> (arp);
315 } else if (srcs[0]->type() == DataType::MIDI) {
317 MidiRegion* mr = new MidiRegion (srcs, node);
319 boost::shared_ptr<MidiRegion> mrp (mr);
320 ret = boost::static_pointer_cast<Region> (mrp);
324 ret->unlock_property_changes ();
326 CheckNewRegion (ret);
334 RegionFactory::map_add (boost::shared_ptr<Region> r)
336 pair<ID,boost::weak_ptr<Region> > p;
340 region_map.insert (p);
343 boost::shared_ptr<Region>
344 RegionFactory::region_by_id (const PBD::ID& id)
346 map<ID,boost::weak_ptr<Region> >::iterator i = region_map.find (id);
348 if (i == region_map.end()) {
349 return boost::shared_ptr<Region>();
352 return i->second.lock();
356 RegionFactory::clear_map ()