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.
22 #include "pbd/error.h"
23 #include "pbd/boost_debug.h"
25 #include "ardour/session.h"
27 #include "ardour/region_factory.h"
28 #include "ardour/region.h"
29 #include "ardour/audioregion.h"
30 #include "ardour/audiosource.h"
31 #include "ardour/midi_source.h"
32 #include "ardour/midi_region.h"
33 #include "ardour/utils.h"
37 using namespace ARDOUR;
40 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
41 Glib::StaticMutex RegionFactory::region_map_lock;
42 RegionFactory::RegionMap RegionFactory::region_map;
43 PBD::ScopedConnectionList RegionFactory::region_list_connections;
44 Glib::StaticMutex RegionFactory::region_name_map_lock;
45 std::map<std::string, uint32_t> RegionFactory::region_name_map;
47 boost::shared_ptr<Region>
48 RegionFactory::create (boost::shared_ptr<const Region> region)
50 boost::shared_ptr<Region> ret;
51 boost::shared_ptr<const AudioRegion> ar;
52 boost::shared_ptr<const MidiRegion> mr;
54 if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
56 AudioRegion* arn = new AudioRegion (ar, 0, true);
57 boost_debug_shared_ptr_mark_interesting (arn, "Region");
59 boost::shared_ptr<AudioRegion> arp (arn);
60 ret = boost::static_pointer_cast<Region> (arp);
62 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
64 MidiRegion* mrn = new MidiRegion (mr, 0, true);
65 boost::shared_ptr<MidiRegion> mrp (mrn);
66 ret = boost::static_pointer_cast<Region> (mrp);
69 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
77 /* pure copy constructor - no property list */
78 /* pure copy constructor - no CheckNewRegion emitted */
84 boost::shared_ptr<Region>
85 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
87 return create (region, offset, true, plist, announce);
90 boost::shared_ptr<Region>
91 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
93 return create (region, 0, false, plist, announce);
96 boost::shared_ptr<Region>
97 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, bool offset_relative, const PropertyList& plist, bool announce)
99 boost::shared_ptr<Region> ret;
100 boost::shared_ptr<const AudioRegion> other_a;
101 boost::shared_ptr<const MidiRegion> other_m;
103 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
105 AudioRegion* ar = new AudioRegion (other_a, offset, offset_relative);
106 boost_debug_shared_ptr_mark_interesting (ar, "Region");
108 boost::shared_ptr<AudioRegion> arp (ar);
109 ret = boost::static_pointer_cast<Region> (arp);
111 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
113 MidiRegion* mr = new MidiRegion (other_m, offset, offset_relative);
114 boost::shared_ptr<MidiRegion> mrp (mr);
115 ret = boost::static_pointer_cast<Region> (mrp);
118 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
121 return boost::shared_ptr<Region>();
125 ret->apply_changes (plist);
129 CheckNewRegion (ret);
136 boost::shared_ptr<Region>
137 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
139 boost::shared_ptr<Region> ret;
140 boost::shared_ptr<const AudioRegion> other;
142 /* used by AudioFilter when constructing a new region that is intended to have nearly
143 identical settings to an original, but using different sources.
146 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
148 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
150 AudioRegion* ar = new AudioRegion (other, srcs);
151 boost_debug_shared_ptr_mark_interesting (ar, "Region");
153 boost::shared_ptr<AudioRegion> arp (ar);
154 ret = boost::static_pointer_cast<Region> (arp);
157 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
164 ret->apply_changes (plist);
168 CheckNewRegion (ret);
176 boost::shared_ptr<Region>
177 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
180 srcs.push_back (src);
181 return create (srcs, plist, announce);
184 boost::shared_ptr<Region>
185 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
187 boost::shared_ptr<Region> ret;
188 boost::shared_ptr<AudioSource> as;
189 boost::shared_ptr<MidiSource> ms;
191 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
193 AudioRegion* ar = new AudioRegion (srcs);
194 boost_debug_shared_ptr_mark_interesting (ar, "Region");
196 boost::shared_ptr<AudioRegion> arp (ar);
197 ret = boost::static_pointer_cast<Region> (arp);
199 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
200 MidiRegion* mr = new MidiRegion (srcs);
201 boost_debug_shared_ptr_mark_interesting (mr, "Region");
203 boost::shared_ptr<MidiRegion> mrp (mr);
204 ret = boost::static_pointer_cast<Region> (mrp);
209 ret->apply_changes (plist);
213 CheckNewRegion (ret);
220 boost::shared_ptr<Region>
221 RegionFactory::create (Session& session, XMLNode& node, bool yn)
223 return session.XMLRegionFactory (node, yn);
226 boost::shared_ptr<Region>
227 RegionFactory::create (SourceList& srcs, const XMLNode& node)
229 boost::shared_ptr<Region> ret;
235 if (srcs[0]->type() == DataType::AUDIO) {
237 AudioRegion* ar = new AudioRegion (srcs);
238 boost_debug_shared_ptr_mark_interesting (ar, "Region");
240 boost::shared_ptr<AudioRegion> arp (ar);
241 ret = boost::static_pointer_cast<Region> (arp);
243 } else if (srcs[0]->type() == DataType::MIDI) {
245 MidiRegion* mr = new MidiRegion (srcs);
247 boost::shared_ptr<MidiRegion> mrp (mr);
248 ret = boost::static_pointer_cast<Region> (mrp);
253 if (ret->set_state (node, Stateful::loading_state_version)) {
257 CheckNewRegion (ret);
265 RegionFactory::map_add (boost::shared_ptr<Region> r)
267 pair<ID,boost::shared_ptr<Region> > p;
272 Glib::Mutex::Lock lm (region_map_lock);
273 region_map.insert (p);
276 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
278 r->PropertyChanged.connect_same_thread (
279 region_list_connections,
280 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
283 update_region_name_map (r);
287 RegionFactory::map_remove (boost::shared_ptr<Region> r)
289 Glib::Mutex::Lock lm (region_map_lock);
290 RegionMap::iterator i = region_map.find (r->id());
292 if (i != region_map.end()) {
293 region_map.erase (i);
299 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
301 Glib::Mutex::Lock lm (region_map_lock);
303 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
304 RegionMap::iterator tmp = i;
307 if (r->region_list_equivalent (i->second)) {
308 region_map.erase (i);
309 } else if (r == i->second) {
310 region_map.erase (i);
319 boost::shared_ptr<Region>
320 RegionFactory::region_by_id (const PBD::ID& id)
322 RegionMap::iterator i = region_map.find (id);
324 if (i == region_map.end()) {
325 cerr << "ID " << id << " not found in region map\n";
326 return boost::shared_ptr<Region>();
333 RegionFactory::clear_map ()
335 region_list_connections.drop_connections ();
338 Glib::Mutex::Lock lm (region_map_lock);
345 RegionFactory::delete_all_regions ()
349 /* copy region list */
351 Glib::Mutex::Lock lm (region_map_lock);
355 /* clear existing map */
358 /* tell everyone to drop references */
359 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
360 i->second->drop_references ();
363 /* the copy should now hold the only references, which will
364 vanish as we leave this scope, thus calling all destructors.
369 RegionFactory::nregions ()
371 Glib::Mutex::Lock lm (region_map_lock);
372 return region_map.size ();
376 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
378 string::size_type const last_period = region->name().find_last_of ('.');
380 if (last_period != string::npos && last_period < region->name().length() - 1) {
382 string const base = region->name().substr (0, last_period);
383 string const number = region->name().substr (last_period + 1);
385 /* note that if there is no number, we get zero from atoi,
389 Glib::Mutex::Lock lm (region_name_map_lock);
390 region_name_map[base] = atoi (number.c_str ());
395 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
397 boost::shared_ptr<Region> r = w.lock ();
402 if (what_changed.contains (Properties::name)) {
403 update_region_name_map (r);
408 RegionFactory::region_name (string& result, string base, bool newlevel)
413 if (base.find("/") != string::npos) {
414 base = base.substr(base.find_last_of("/") + 1);
419 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
428 string::size_type pos;
430 pos = base.find_last_of ('.');
432 /* pos may be npos, but then we just use entire base */
434 subbase = base.substr (0, pos);
439 Glib::Mutex::Lock lm (region_name_map_lock);
441 map<string,uint32_t>::iterator x;
445 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
447 region_name_map[subbase] = 1;
450 snprintf (buf, sizeof (buf), ".%d", x->second);
461 RegionFactory::new_region_name (string old)
463 string::size_type last_period;
465 string::size_type len = old.length() + 64;
468 if ((last_period = old.find_last_of ('.')) == string::npos) {
470 /* no period present - add one explicitly */
473 last_period = old.length() - 1;
478 number = atoi (old.substr (last_period+1).c_str());
482 while (number < (UINT_MAX-1)) {
484 const RegionMap& regions (RegionFactory::regions());
485 RegionMap::const_iterator i;
490 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
493 for (i = regions.begin(); i != regions.end(); ++i) {
494 if (i->second->name() == sbuf) {
499 if (i == regions.end()) {
504 if (number != (UINT_MAX-1)) {
508 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
513 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
515 Glib::Mutex::Lock lm (region_map_lock);
517 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
518 if (i->second->uses_source (s)) {
519 r.insert (i->second);