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)
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, 0));
59 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
61 if (mr->session().config.get_midi_copy_is_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, 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 ()) {
87 ret->set_position_lock_style (MusicTime);
90 ret->set_position (region->position());
92 /* pure copy constructor - no property list */
99 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
100 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
105 boost::shared_ptr<Region>
106 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
108 boost::shared_ptr<Region> ret;
109 boost::shared_ptr<const AudioRegion> other_a;
110 boost::shared_ptr<const MidiRegion> other_m;
112 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
114 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
116 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
118 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
121 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
123 abort(); /*NOTREACHED*/
124 return boost::shared_ptr<Region>();
128 ret->apply_changes (plist);
130 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
131 ret->set_position_lock_style (MusicTime);
136 CheckNewRegion (ret);
140 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
141 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
146 boost::shared_ptr<Region>
147 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
149 boost::shared_ptr<Region> ret;
150 boost::shared_ptr<const AudioRegion> other_a;
151 boost::shared_ptr<const MidiRegion> other_m;
153 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
155 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
157 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
159 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
162 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
164 abort(); /*NOTREACHED*/
165 return boost::shared_ptr<Region>();
169 ret->apply_changes (plist);
171 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
172 ret->set_position_lock_style (MusicTime);
177 CheckNewRegion (ret);
181 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
182 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
187 boost::shared_ptr<Region>
188 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
190 boost::shared_ptr<Region> ret;
191 boost::shared_ptr<const AudioRegion> other;
193 /* used by AudioFilter when constructing a new region that is intended to have nearly
194 identical settings to an original, but using different sources.
197 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
199 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
201 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
204 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
206 abort(); /*NOTREACHED*/
210 ret->apply_changes (plist);
212 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
213 ret->set_position_lock_style (MusicTime);
218 CheckNewRegion (ret);
222 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
223 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
228 boost::shared_ptr<Region>
229 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
232 srcs.push_back (src);
233 return create (srcs, plist, announce);
236 boost::shared_ptr<Region>
237 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
239 boost::shared_ptr<Region> ret;
240 boost::shared_ptr<AudioSource> as;
241 boost::shared_ptr<MidiSource> ms;
243 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
245 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
247 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
249 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
254 ret->apply_changes (plist);
256 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
257 ret->set_position_lock_style (MusicTime);
262 CheckNewRegion (ret);
266 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
267 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
272 boost::shared_ptr<Region>
273 RegionFactory::create (Session& session, XMLNode& node, bool yn)
275 return session.XMLRegionFactory (node, yn);
278 boost::shared_ptr<Region>
279 RegionFactory::create (SourceList& srcs, const XMLNode& node)
281 boost::shared_ptr<Region> ret;
287 if (srcs[0]->type() == DataType::AUDIO) {
289 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
291 } else if (srcs[0]->type() == DataType::MIDI) {
293 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
298 if (ret->set_state (node, Stateful::loading_state_version)) {
303 /* Don't fiddle with position_lock_style here as the region
304 description is coming from XML.
307 CheckNewRegion (ret);
311 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
312 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
318 RegionFactory::map_add (boost::shared_ptr<Region> r)
320 pair<ID,boost::shared_ptr<Region> > p;
325 Glib::Threads::Mutex::Lock lm (region_map_lock);
326 region_map.insert (p);
329 if (!region_list_connections) {
330 region_list_connections = new ScopedConnectionList;
333 r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
334 r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
336 add_to_region_name_maps (r);
340 RegionFactory::map_remove (boost::weak_ptr<Region> w)
342 boost::shared_ptr<Region> r = w.lock ();
347 Glib::Threads::Mutex::Lock lm (region_map_lock);
348 RegionMap::iterator i = region_map.find (r->id());
350 if (i != region_map.end()) {
351 remove_from_region_name_map (i->second->name ());
352 region_map.erase (i);
356 boost::shared_ptr<Region>
357 RegionFactory::region_by_id (const PBD::ID& id)
359 RegionMap::iterator i = region_map.find (id);
361 if (i == region_map.end()) {
362 return boost::shared_ptr<Region>();
368 boost::shared_ptr<Region>
369 RegionFactory::wholefile_region_by_name (const std::string& name)
371 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
372 if (i->second->whole_file() && i->second->name() == name) {
376 return boost::shared_ptr<Region>();
379 boost::shared_ptr<Region>
380 RegionFactory::region_by_name (const std::string& name)
382 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
383 if (i->second->name() == name) {
387 return boost::shared_ptr<Region>();
391 RegionFactory::clear_map ()
393 if (region_list_connections) {
394 region_list_connections->drop_connections ();
398 Glib::Threads::Mutex::Lock lm (region_map_lock);
400 _compound_associations.clear ();
401 region_name_map.clear ();
406 RegionFactory::delete_all_regions ()
410 /* copy region list */
412 Glib::Threads::Mutex::Lock lm (region_map_lock);
416 /* clear existing map */
419 /* tell everyone to drop references */
420 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
421 i->second->drop_references ();
424 /* the copy should now hold the only references, which will
425 vanish as we leave this scope, thus calling all destructors.
430 RegionFactory::nregions ()
432 Glib::Threads::Mutex::Lock lm (region_map_lock);
433 return region_map.size ();
436 /** Add a region to the two region name maps */
438 RegionFactory::add_to_region_name_maps (boost::shared_ptr<Region> region)
440 update_region_name_number_map (region);
442 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
443 region_name_map[region->name()] = region->id ();
446 /** Account for a region rename in the two region name maps */
448 RegionFactory::rename_in_region_name_maps (boost::shared_ptr<Region> region)
450 update_region_name_number_map (region);
452 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
454 map<string, PBD::ID>::iterator i = region_name_map.begin();
455 while (i != region_name_map.end() && i->second != region->id ()) {
459 /* Erase the entry for the old name and put in a new one */
460 if (i != region_name_map.end()) {
461 region_name_map.erase (i);
462 region_name_map[region->name()] = region->id ();
466 /** Remove a region's details from the region_name_map */
468 RegionFactory::remove_from_region_name_map (string n)
470 map<string, PBD::ID>::iterator i = region_name_map.find (n);
471 if (i != region_name_map.end ()) {
472 region_name_map.erase (i);
476 /** Update a region's entry in the region_name_number_map */
478 RegionFactory::update_region_name_number_map (boost::shared_ptr<Region> region)
480 string::size_type const last_period = region->name().find_last_of ('.');
482 if (last_period != string::npos && last_period < region->name().length() - 1) {
484 string const base = region->name().substr (0, last_period);
485 string const number = region->name().substr (last_period + 1);
487 /* note that if there is no number, we get zero from atoi,
491 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
492 region_name_number_map[base] = atoi (number.c_str ());
497 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
499 boost::shared_ptr<Region> r = w.lock ();
504 if (what_changed.contains (Properties::name)) {
505 rename_in_region_name_maps (r);
510 RegionFactory::region_name (string& result, string base, bool newlevel)
515 if (base.find("/") != string::npos) {
516 base = base.substr(base.find_last_of("/") + 1);
521 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
530 string::size_type pos;
532 pos = base.find_last_of ('.');
534 /* pos may be npos, but then we just use entire base */
536 subbase = base.substr (0, pos);
541 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
543 map<string,uint32_t>::iterator x;
547 if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end()) {
549 region_name_number_map[subbase] = 1;
552 snprintf (buf, sizeof (buf), ".%d", x->second);
563 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
566 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
568 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
573 RegionFactory::new_region_name (string old)
575 string::size_type last_period;
577 string::size_type len = old.length() + 64;
579 std::vector<char> buf(len);
581 if ((last_period = old.find_last_of ('.')) == string::npos) {
583 /* no period present - add one explicitly */
586 last_period = old.length() - 1;
591 if (last_period < old.length() - 1) {
593 string period_to_end = old.substr (last_period+1);
595 /* extra material after the period */
597 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
599 number = atoi (period_to_end);
601 if (numerals_end < period_to_end.length() - 1) {
602 /* extra material after the end of the digits */
603 remainder = period_to_end.substr (numerals_end);
607 last_period = old.length();
612 while (number < (UINT_MAX-1)) {
618 snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
621 if (region_name_map.find (sbuf) == region_name_map.end ()) {
626 if (number != (UINT_MAX-1)) {
630 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
635 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
637 Glib::Threads::Mutex::Lock lm (region_map_lock);
639 for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
640 if (i->second->uses_source (s)) {
641 r.insert (i->second);
647 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
649 Glib::Threads::Mutex::Lock lm (region_map_lock);
651 RegionMap::iterator i = region_map.begin();
652 while (i != region_map.end()) {
654 RegionMap::iterator j = i;
657 if (i->second->uses_source (src)) {
658 remove_from_region_name_map (i->second->name ());
659 region_map.erase (i);
667 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
669 Glib::Threads::Mutex::Lock lm (region_map_lock);
670 _compound_associations[copy] = orig;