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 ret = boost::shared_ptr<Region> (new AudioRegion (ar, 0));
58 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
60 ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
63 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
69 ret->set_name (new_region_name(ret->name()));
72 /* pure copy constructor - no property list */
78 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
82 boost::shared_ptr<Region>
83 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
85 boost::shared_ptr<Region> ret;
86 boost::shared_ptr<const AudioRegion> other_a;
87 boost::shared_ptr<const MidiRegion> other_m;
89 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
91 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
93 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
95 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
98 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
101 return boost::shared_ptr<Region>();
105 ret->apply_changes (plist);
109 CheckNewRegion (ret);
113 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
117 boost::shared_ptr<Region>
118 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
120 boost::shared_ptr<Region> ret;
121 boost::shared_ptr<const AudioRegion> other_a;
122 boost::shared_ptr<const MidiRegion> other_m;
124 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
126 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
128 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
130 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
133 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
136 return boost::shared_ptr<Region>();
140 ret->apply_changes (plist);
144 CheckNewRegion (ret);
148 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
152 boost::shared_ptr<Region>
153 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
155 boost::shared_ptr<Region> ret;
156 boost::shared_ptr<const AudioRegion> other;
158 /* used by AudioFilter when constructing a new region that is intended to have nearly
159 identical settings to an original, but using different sources.
162 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
164 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
166 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
169 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
175 ret->apply_changes (plist);
179 CheckNewRegion (ret);
183 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
187 boost::shared_ptr<Region>
188 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
191 srcs.push_back (src);
192 return create (srcs, plist, announce);
195 boost::shared_ptr<Region>
196 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
198 boost::shared_ptr<Region> ret;
199 boost::shared_ptr<AudioSource> as;
200 boost::shared_ptr<MidiSource> ms;
202 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
204 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
206 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
208 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
213 ret->apply_changes (plist);
217 CheckNewRegion (ret);
221 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
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 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
244 } else if (srcs[0]->type() == DataType::MIDI) {
246 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
251 if (ret->set_state (node, Stateful::loading_state_version)) {
255 CheckNewRegion (ret);
259 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
264 RegionFactory::map_add (boost::shared_ptr<Region> r)
266 pair<ID,boost::shared_ptr<Region> > p;
271 Glib::Mutex::Lock lm (region_map_lock);
272 region_map.insert (p);
275 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
277 r->PropertyChanged.connect_same_thread (
278 region_list_connections,
279 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
282 update_region_name_map (r);
286 RegionFactory::map_remove (boost::shared_ptr<Region> r)
288 Glib::Mutex::Lock lm (region_map_lock);
289 RegionMap::iterator i = region_map.find (r->id());
291 if (i != region_map.end()) {
292 region_map.erase (i);
297 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
299 Glib::Mutex::Lock lm (region_map_lock);
301 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
302 RegionMap::iterator tmp = i;
305 if (r->region_list_equivalent (i->second)) {
306 region_map.erase (i);
307 } else if (r == i->second) {
308 region_map.erase (i);
315 boost::shared_ptr<Region>
316 RegionFactory::region_by_id (const PBD::ID& id)
318 RegionMap::iterator i = region_map.find (id);
320 if (i == region_map.end()) {
321 return boost::shared_ptr<Region>();
327 boost::shared_ptr<Region>
328 RegionFactory::wholefile_region_by_name (const std::string& name)
330 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
331 if (i->second->whole_file() && i->second->name() == name) {
335 return boost::shared_ptr<Region>();
338 boost::shared_ptr<Region>
339 RegionFactory::region_by_name (const std::string& name)
341 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
342 if (i->second->name() == name) {
346 return boost::shared_ptr<Region>();
350 RegionFactory::clear_map ()
352 region_list_connections.drop_connections ();
355 Glib::Mutex::Lock lm (region_map_lock);
362 RegionFactory::delete_all_regions ()
366 /* copy region list */
368 Glib::Mutex::Lock lm (region_map_lock);
372 /* clear existing map */
375 /* tell everyone to drop references */
376 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
377 i->second->drop_references ();
380 /* the copy should now hold the only references, which will
381 vanish as we leave this scope, thus calling all destructors.
386 RegionFactory::nregions ()
388 Glib::Mutex::Lock lm (region_map_lock);
389 return region_map.size ();
393 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
395 string::size_type const last_period = region->name().find_last_of ('.');
397 if (last_period != string::npos && last_period < region->name().length() - 1) {
399 string const base = region->name().substr (0, last_period);
400 string const number = region->name().substr (last_period + 1);
402 /* note that if there is no number, we get zero from atoi,
406 Glib::Mutex::Lock lm (region_name_map_lock);
407 region_name_map[base] = atoi (number.c_str ());
412 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
414 boost::shared_ptr<Region> r = w.lock ();
419 if (what_changed.contains (Properties::name)) {
420 update_region_name_map (r);
425 RegionFactory::region_name (string& result, string base, bool newlevel)
430 if (base.find("/") != string::npos) {
431 base = base.substr(base.find_last_of("/") + 1);
436 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
445 string::size_type pos;
447 pos = base.find_last_of ('.');
449 /* pos may be npos, but then we just use entire base */
451 subbase = base.substr (0, pos);
456 Glib::Mutex::Lock lm (region_name_map_lock);
458 map<string,uint32_t>::iterator x;
462 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
464 region_name_map[subbase] = 1;
467 snprintf (buf, sizeof (buf), ".%d", x->second);
478 RegionFactory::new_region_name (string old)
480 string::size_type last_period;
482 string::size_type len = old.length() + 64;
485 if ((last_period = old.find_last_of ('.')) == string::npos) {
487 /* no period present - add one explicitly */
490 last_period = old.length() - 1;
495 number = atoi (old.substr (last_period+1).c_str());
499 while (number < (UINT_MAX-1)) {
501 const RegionMap& regions (RegionFactory::regions());
502 RegionMap::const_iterator i;
507 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
510 for (i = regions.begin(); i != regions.end(); ++i) {
511 if (i->second->name() == sbuf) {
516 if (i == regions.end()) {
521 if (number != (UINT_MAX-1)) {
525 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
530 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
532 Glib::Mutex::Lock lm (region_map_lock);
534 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
535 if (i->second->uses_source (s)) {
536 r.insert (i->second);