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, bool announce)
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")
75 ret->set_name (new_region_name(ret->name()));
78 /* pure copy constructor - no property list */
79 /* pure copy constructor - no CheckNewRegion emitted */
89 boost::shared_ptr<Region>
90 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
92 return create (region, offset, true, plist, announce);
95 boost::shared_ptr<Region>
96 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
98 return create (region, 0, false, plist, announce);
101 boost::shared_ptr<Region>
102 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, bool offset_relative, const PropertyList& plist, bool announce)
104 boost::shared_ptr<Region> ret;
105 boost::shared_ptr<const AudioRegion> other_a;
106 boost::shared_ptr<const MidiRegion> other_m;
108 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
110 AudioRegion* ar = new AudioRegion (other_a, offset, offset_relative);
111 boost_debug_shared_ptr_mark_interesting (ar, "Region");
113 boost::shared_ptr<AudioRegion> arp (ar);
114 ret = boost::static_pointer_cast<Region> (arp);
116 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
118 MidiRegion* mr = new MidiRegion (other_m, offset, offset_relative);
119 boost::shared_ptr<MidiRegion> mrp (mr);
120 ret = boost::static_pointer_cast<Region> (mrp);
123 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
126 return boost::shared_ptr<Region>();
130 ret->apply_changes (plist);
134 CheckNewRegion (ret);
141 boost::shared_ptr<Region>
142 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
144 boost::shared_ptr<Region> ret;
145 boost::shared_ptr<const AudioRegion> other;
147 /* used by AudioFilter when constructing a new region that is intended to have nearly
148 identical settings to an original, but using different sources.
151 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
153 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
155 AudioRegion* ar = new AudioRegion (other, srcs);
156 boost_debug_shared_ptr_mark_interesting (ar, "Region");
158 boost::shared_ptr<AudioRegion> arp (ar);
159 ret = boost::static_pointer_cast<Region> (arp);
162 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
169 ret->apply_changes (plist);
173 CheckNewRegion (ret);
181 boost::shared_ptr<Region>
182 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
185 srcs.push_back (src);
186 return create (srcs, plist, announce);
189 boost::shared_ptr<Region>
190 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
192 boost::shared_ptr<Region> ret;
193 boost::shared_ptr<AudioSource> as;
194 boost::shared_ptr<MidiSource> ms;
196 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
198 AudioRegion* ar = new AudioRegion (srcs);
199 boost_debug_shared_ptr_mark_interesting (ar, "Region");
201 boost::shared_ptr<AudioRegion> arp (ar);
202 ret = boost::static_pointer_cast<Region> (arp);
204 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
205 MidiRegion* mr = new MidiRegion (srcs);
206 boost_debug_shared_ptr_mark_interesting (mr, "Region");
208 boost::shared_ptr<MidiRegion> mrp (mr);
209 ret = boost::static_pointer_cast<Region> (mrp);
214 ret->apply_changes (plist);
218 CheckNewRegion (ret);
225 boost::shared_ptr<Region>
226 RegionFactory::create (Session& session, XMLNode& node, bool yn)
228 return session.XMLRegionFactory (node, yn);
231 boost::shared_ptr<Region>
232 RegionFactory::create (SourceList& srcs, const XMLNode& node)
234 boost::shared_ptr<Region> ret;
240 if (srcs[0]->type() == DataType::AUDIO) {
242 AudioRegion* ar = new AudioRegion (srcs);
243 boost_debug_shared_ptr_mark_interesting (ar, "Region");
245 boost::shared_ptr<AudioRegion> arp (ar);
246 ret = boost::static_pointer_cast<Region> (arp);
248 } else if (srcs[0]->type() == DataType::MIDI) {
250 MidiRegion* mr = new MidiRegion (srcs);
252 boost::shared_ptr<MidiRegion> mrp (mr);
253 ret = boost::static_pointer_cast<Region> (mrp);
258 if (ret->set_state (node, Stateful::loading_state_version)) {
262 CheckNewRegion (ret);
270 RegionFactory::map_add (boost::shared_ptr<Region> r)
272 pair<ID,boost::shared_ptr<Region> > p;
277 Glib::Mutex::Lock lm (region_map_lock);
278 region_map.insert (p);
281 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
283 r->PropertyChanged.connect_same_thread (
284 region_list_connections,
285 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
288 update_region_name_map (r);
292 RegionFactory::map_remove (boost::shared_ptr<Region> r)
294 Glib::Mutex::Lock lm (region_map_lock);
295 RegionMap::iterator i = region_map.find (r->id());
297 if (i != region_map.end()) {
298 region_map.erase (i);
304 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
306 Glib::Mutex::Lock lm (region_map_lock);
308 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
309 RegionMap::iterator tmp = i;
312 if (r->region_list_equivalent (i->second)) {
313 region_map.erase (i);
314 } else if (r == i->second) {
315 region_map.erase (i);
324 boost::shared_ptr<Region>
325 RegionFactory::region_by_id (const PBD::ID& id)
327 RegionMap::iterator i = region_map.find (id);
329 if (i == region_map.end()) {
330 cerr << "ID " << id << " not found in region map\n";
331 return boost::shared_ptr<Region>();
338 RegionFactory::clear_map ()
340 region_list_connections.drop_connections ();
343 Glib::Mutex::Lock lm (region_map_lock);
350 RegionFactory::delete_all_regions ()
354 /* copy region list */
356 Glib::Mutex::Lock lm (region_map_lock);
360 /* clear existing map */
363 /* tell everyone to drop references */
364 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
365 i->second->drop_references ();
368 /* the copy should now hold the only references, which will
369 vanish as we leave this scope, thus calling all destructors.
374 RegionFactory::nregions ()
376 Glib::Mutex::Lock lm (region_map_lock);
377 return region_map.size ();
381 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
383 string::size_type const last_period = region->name().find_last_of ('.');
385 if (last_period != string::npos && last_period < region->name().length() - 1) {
387 string const base = region->name().substr (0, last_period);
388 string const number = region->name().substr (last_period + 1);
390 /* note that if there is no number, we get zero from atoi,
394 Glib::Mutex::Lock lm (region_name_map_lock);
395 region_name_map[base] = atoi (number.c_str ());
400 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
402 boost::shared_ptr<Region> r = w.lock ();
407 if (what_changed.contains (Properties::name)) {
408 update_region_name_map (r);
413 RegionFactory::region_name (string& result, string base, bool newlevel)
418 if (base.find("/") != string::npos) {
419 base = base.substr(base.find_last_of("/") + 1);
424 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
433 string::size_type pos;
435 pos = base.find_last_of ('.');
437 /* pos may be npos, but then we just use entire base */
439 subbase = base.substr (0, pos);
444 Glib::Mutex::Lock lm (region_name_map_lock);
446 map<string,uint32_t>::iterator x;
450 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
452 region_name_map[subbase] = 1;
455 snprintf (buf, sizeof (buf), ".%d", x->second);
466 RegionFactory::new_region_name (string old)
468 string::size_type last_period;
470 string::size_type len = old.length() + 64;
473 if ((last_period = old.find_last_of ('.')) == string::npos) {
475 /* no period present - add one explicitly */
478 last_period = old.length() - 1;
483 number = atoi (old.substr (last_period+1).c_str());
487 while (number < (UINT_MAX-1)) {
489 const RegionMap& regions (RegionFactory::regions());
490 RegionMap::const_iterator i;
495 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
498 for (i = regions.begin(); i != regions.end(); ++i) {
499 if (i->second->name() == sbuf) {
504 if (i == regions.end()) {
509 if (number != (UINT_MAX-1)) {
513 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
518 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
520 Glib::Mutex::Lock lm (region_map_lock);
522 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
523 if (i->second->uses_source (s)) {
524 r.insert (i->second);