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/basename.h"
23 #include "pbd/error.h"
25 #include "ardour/audioregion.h"
26 #include "ardour/audiosource.h"
27 #include "ardour/midi_region.h"
28 #include "ardour/midi_source.h"
29 #include "ardour/region.h"
30 #include "ardour/region_factory.h"
31 #include "ardour/session.h"
35 using namespace ARDOUR;
39 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
40 Glib::Threads::Mutex RegionFactory::region_map_lock;
41 RegionFactory::RegionMap RegionFactory::region_map;
42 PBD::ScopedConnectionList* RegionFactory::region_list_connections = 0;
43 Glib::Threads::Mutex RegionFactory::region_name_maps_mutex;
44 std::map<std::string, uint32_t> RegionFactory::region_name_number_map;
45 std::map<std::string, PBD::ID> RegionFactory::region_name_map;
46 RegionFactory::CompoundAssociations RegionFactory::_compound_associations;
48 boost::shared_ptr<Region>
49 RegionFactory::create (boost::shared_ptr<const Region> region, bool announce, bool fork)
51 boost::shared_ptr<Region> ret;
52 boost::shared_ptr<const AudioRegion> ar;
53 boost::shared_ptr<const MidiRegion> mr;
55 if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
57 ret = boost::shared_ptr<Region> (new AudioRegion (ar, MusicFrame (0, 0)));
59 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
61 if (mr->session().config.get_midi_copy_is_fork() || fork) {
62 /* What we really want to do here is what Editor::fork_region()
63 does via Session::create_midi_source_by_stealing_name(), but we
64 don't have a Track. We'll just live with the skipped number,
65 and store the ancestral name of sources so multiple clones
66 generates reasonable names that don't have too many suffixes. */
67 const std::string ancestor_name = mr->sources().front()->ancestor_name();
68 const std::string base = PBD::basename_nosuffix(ancestor_name);
70 boost::shared_ptr<MidiSource> source = mr->session().create_midi_source_for_session(base);
71 source->set_ancestor_name(mr->sources().front()->name());
72 ret = mr->clone(source);
74 ret = boost::shared_ptr<Region> (new MidiRegion (mr, MusicFrame (0, 0)));
78 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
80 abort(); /*NOTREACHED*/
84 ret->set_name (new_region_name(ret->name()));
86 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
87 ret->set_position_lock_style (MusicTime);
90 /* pure copy constructor - no property list */
97 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
98 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
103 boost::shared_ptr<Region>
104 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
106 boost::shared_ptr<Region> ret;
107 boost::shared_ptr<const AudioRegion> other_a;
108 boost::shared_ptr<const MidiRegion> other_m;
110 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
112 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
114 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
116 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
119 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
121 abort(); /*NOTREACHED*/
122 return boost::shared_ptr<Region>();
126 ret->apply_changes (plist);
128 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
129 ret->set_position_lock_style (MusicTime);
134 CheckNewRegion (ret);
138 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
139 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
144 boost::shared_ptr<Region>
145 RegionFactory::create (boost::shared_ptr<Region> region, MusicFrame offset, const PropertyList& plist, bool announce)
147 boost::shared_ptr<Region> ret;
148 boost::shared_ptr<const AudioRegion> other_a;
149 boost::shared_ptr<const MidiRegion> other_m;
151 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
153 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
155 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
157 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
160 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
162 abort(); /*NOTREACHED*/
163 return boost::shared_ptr<Region>();
167 ret->apply_changes (plist);
169 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
170 ret->set_position_lock_style (MusicTime);
175 CheckNewRegion (ret);
179 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
180 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
185 boost::shared_ptr<Region>
186 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
188 boost::shared_ptr<Region> ret;
189 boost::shared_ptr<const AudioRegion> other;
191 /* used by AudioFilter when constructing a new region that is intended to have nearly
192 identical settings to an original, but using different sources.
195 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
197 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
199 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
202 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
204 abort(); /*NOTREACHED*/
208 ret->apply_changes (plist);
210 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
211 ret->set_position_lock_style (MusicTime);
216 CheckNewRegion (ret);
220 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
221 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
226 boost::shared_ptr<Region>
227 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
230 srcs.push_back (src);
231 return create (srcs, plist, announce);
234 boost::shared_ptr<Region>
235 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
237 boost::shared_ptr<Region> ret;
238 boost::shared_ptr<AudioSource> as;
239 boost::shared_ptr<MidiSource> ms;
241 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
243 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
245 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
247 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
252 ret->apply_changes (plist);
254 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
255 ret->set_position_lock_style (MusicTime);
260 CheckNewRegion (ret);
264 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
265 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
270 boost::shared_ptr<Region>
271 RegionFactory::create (Session& session, XMLNode& node, bool yn)
273 return session.XMLRegionFactory (node, yn);
276 boost::shared_ptr<Region>
277 RegionFactory::create (SourceList& srcs, const XMLNode& node)
279 boost::shared_ptr<Region> ret;
285 if (srcs[0]->type() == DataType::AUDIO) {
287 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
289 } else if (srcs[0]->type() == DataType::MIDI) {
291 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
296 if (ret->set_state (node, Stateful::loading_state_version)) {
301 /* Don't fiddle with position_lock_style here as the region
302 description is coming from XML.
305 CheckNewRegion (ret);
309 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
310 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
316 RegionFactory::map_add (boost::shared_ptr<Region> r)
318 pair<ID,boost::shared_ptr<Region> > p;
323 Glib::Threads::Mutex::Lock lm (region_map_lock);
324 region_map.insert (p);
327 if (!region_list_connections) {
328 region_list_connections = new ScopedConnectionList;
331 r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
332 r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
334 add_to_region_name_maps (r);
338 RegionFactory::map_remove (boost::weak_ptr<Region> w)
340 boost::shared_ptr<Region> r = w.lock ();
345 Glib::Threads::Mutex::Lock lm (region_map_lock);
346 RegionMap::iterator i = region_map.find (r->id());
348 if (i != region_map.end()) {
349 remove_from_region_name_map (i->second->name ());
350 region_map.erase (i);
354 boost::shared_ptr<Region>
355 RegionFactory::region_by_id (const PBD::ID& id)
357 RegionMap::iterator i = region_map.find (id);
359 if (i == region_map.end()) {
360 return boost::shared_ptr<Region>();
366 boost::shared_ptr<Region>
367 RegionFactory::wholefile_region_by_name (const std::string& name)
369 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
370 if (i->second->whole_file() && i->second->name() == name) {
374 return boost::shared_ptr<Region>();
377 boost::shared_ptr<Region>
378 RegionFactory::region_by_name (const std::string& name)
380 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
381 if (i->second->name() == name) {
385 return boost::shared_ptr<Region>();
389 RegionFactory::clear_map ()
391 if (region_list_connections) {
392 region_list_connections->drop_connections ();
396 Glib::Threads::Mutex::Lock lm (region_map_lock);
398 _compound_associations.clear ();
399 region_name_map.clear ();
404 RegionFactory::delete_all_regions ()
408 /* copy region list */
410 Glib::Threads::Mutex::Lock lm (region_map_lock);
414 /* clear existing map */
417 /* tell everyone to drop references */
418 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
419 i->second->drop_references ();
422 /* the copy should now hold the only references, which will
423 vanish as we leave this scope, thus calling all destructors.
428 RegionFactory::nregions ()
430 Glib::Threads::Mutex::Lock lm (region_map_lock);
431 return region_map.size ();
434 /** Add a region to the two region name maps */
436 RegionFactory::add_to_region_name_maps (boost::shared_ptr<Region> region)
438 update_region_name_number_map (region);
440 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
441 region_name_map[region->name()] = region->id ();
444 /** Account for a region rename in the two region name maps */
446 RegionFactory::rename_in_region_name_maps (boost::shared_ptr<Region> region)
448 update_region_name_number_map (region);
450 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
452 map<string, PBD::ID>::iterator i = region_name_map.begin();
453 while (i != region_name_map.end() && i->second != region->id ()) {
457 /* Erase the entry for the old name and put in a new one */
458 if (i != region_name_map.end()) {
459 region_name_map.erase (i);
460 region_name_map[region->name()] = region->id ();
464 /** Remove a region's details from the region_name_map */
466 RegionFactory::remove_from_region_name_map (string n)
468 map<string, PBD::ID>::iterator i = region_name_map.find (n);
469 if (i != region_name_map.end ()) {
470 region_name_map.erase (i);
474 /** Update a region's entry in the region_name_number_map */
476 RegionFactory::update_region_name_number_map (boost::shared_ptr<Region> region)
478 string::size_type const last_period = region->name().find_last_of ('.');
480 if (last_period != string::npos && last_period < region->name().length() - 1) {
482 string const base = region->name().substr (0, last_period);
483 string const number = region->name().substr (last_period + 1);
485 /* note that if there is no number, we get zero from atoi,
489 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
490 region_name_number_map[base] = atoi (number.c_str ());
495 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
497 boost::shared_ptr<Region> r = w.lock ();
502 if (what_changed.contains (Properties::name)) {
503 rename_in_region_name_maps (r);
508 RegionFactory::region_name (string& result, string base, bool newlevel)
513 if (base.find("/") != string::npos) {
514 base = base.substr(base.find_last_of("/") + 1);
519 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
528 string::size_type pos;
530 pos = base.find_last_of ('.');
532 /* pos may be npos, but then we just use entire base */
534 subbase = base.substr (0, pos);
539 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
541 map<string,uint32_t>::iterator x;
545 if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end()) {
547 region_name_number_map[subbase] = 1;
550 snprintf (buf, sizeof (buf), ".%d", x->second);
561 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
564 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
566 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
571 RegionFactory::new_region_name (string old)
573 string::size_type last_period;
575 string::size_type len = old.length() + 64;
577 std::vector<char> buf(len);
579 if ((last_period = old.find_last_of ('.')) == string::npos) {
581 /* no period present - add one explicitly */
584 last_period = old.length() - 1;
589 if (last_period < old.length() - 1) {
591 string period_to_end = old.substr (last_period+1);
593 /* extra material after the period */
595 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
597 number = atoi (period_to_end);
599 if (numerals_end < period_to_end.length() - 1) {
600 /* extra material after the end of the digits */
601 remainder = period_to_end.substr (numerals_end);
605 last_period = old.length();
610 while (number < (UINT_MAX-1)) {
616 snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
619 if (region_name_map.find (sbuf) == region_name_map.end ()) {
624 if (number != (UINT_MAX-1)) {
628 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
633 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
635 Glib::Threads::Mutex::Lock lm (region_map_lock);
637 for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
638 if (i->second->uses_source (s)) {
639 r.insert (i->second);
645 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
647 Glib::Threads::Mutex::Lock lm (region_map_lock);
649 RegionMap::iterator i = region_map.begin();
650 while (i != region_map.end()) {
652 RegionMap::iterator j = i;
655 if (i->second->uses_source (src)) {
656 remove_from_region_name_map (i->second->name ());
657 region_map.erase (i);
665 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
667 Glib::Threads::Mutex::Lock lm (region_map_lock);
668 _compound_associations[copy] = orig;