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()));
85 ret->set_position (region->position());
87 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
88 ret->set_position_lock_style (MusicTime);
91 /* pure copy constructor - no property list */
98 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
99 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
104 boost::shared_ptr<Region>
105 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
107 boost::shared_ptr<Region> ret;
108 boost::shared_ptr<const AudioRegion> other_a;
109 boost::shared_ptr<const MidiRegion> other_m;
111 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
113 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
115 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
117 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
120 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
122 abort(); /*NOTREACHED*/
123 return boost::shared_ptr<Region>();
127 ret->apply_changes (plist);
129 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
130 ret->set_position_lock_style (MusicTime);
135 CheckNewRegion (ret);
139 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
140 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
145 boost::shared_ptr<Region>
146 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
148 boost::shared_ptr<Region> ret;
149 boost::shared_ptr<const AudioRegion> other_a;
150 boost::shared_ptr<const MidiRegion> other_m;
152 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
154 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
156 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
158 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
161 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
163 abort(); /*NOTREACHED*/
164 return boost::shared_ptr<Region>();
168 ret->apply_changes (plist);
170 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
171 ret->set_position_lock_style (MusicTime);
176 CheckNewRegion (ret);
180 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
181 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
186 boost::shared_ptr<Region>
187 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
189 boost::shared_ptr<Region> ret;
190 boost::shared_ptr<const AudioRegion> other;
192 /* used by AudioFilter when constructing a new region that is intended to have nearly
193 identical settings to an original, but using different sources.
196 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
198 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
200 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
203 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
205 abort(); /*NOTREACHED*/
209 ret->apply_changes (plist);
211 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
212 ret->set_position_lock_style (MusicTime);
217 CheckNewRegion (ret);
221 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
222 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
227 boost::shared_ptr<Region>
228 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
231 srcs.push_back (src);
232 return create (srcs, plist, announce);
235 boost::shared_ptr<Region>
236 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
238 boost::shared_ptr<Region> ret;
239 boost::shared_ptr<AudioSource> as;
240 boost::shared_ptr<MidiSource> ms;
242 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
244 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
246 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
248 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
253 ret->apply_changes (plist);
255 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
256 ret->set_position_lock_style (MusicTime);
261 CheckNewRegion (ret);
265 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
266 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
271 boost::shared_ptr<Region>
272 RegionFactory::create (Session& session, XMLNode& node, bool yn)
274 return session.XMLRegionFactory (node, yn);
277 boost::shared_ptr<Region>
278 RegionFactory::create (SourceList& srcs, const XMLNode& node)
280 boost::shared_ptr<Region> ret;
286 if (srcs[0]->type() == DataType::AUDIO) {
288 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
290 } else if (srcs[0]->type() == DataType::MIDI) {
292 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
297 if (ret->set_state (node, Stateful::loading_state_version)) {
302 /* Don't fiddle with position_lock_style here as the region
303 description is coming from XML.
306 CheckNewRegion (ret);
310 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
311 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
317 RegionFactory::map_add (boost::shared_ptr<Region> r)
319 pair<ID,boost::shared_ptr<Region> > p;
324 Glib::Threads::Mutex::Lock lm (region_map_lock);
325 region_map.insert (p);
328 if (!region_list_connections) {
329 region_list_connections = new ScopedConnectionList;
332 r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
333 r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
335 add_to_region_name_maps (r);
339 RegionFactory::map_remove (boost::weak_ptr<Region> w)
341 boost::shared_ptr<Region> r = w.lock ();
346 Glib::Threads::Mutex::Lock lm (region_map_lock);
347 RegionMap::iterator i = region_map.find (r->id());
349 if (i != region_map.end()) {
350 remove_from_region_name_map (i->second->name ());
351 region_map.erase (i);
355 boost::shared_ptr<Region>
356 RegionFactory::region_by_id (const PBD::ID& id)
358 RegionMap::iterator i = region_map.find (id);
360 if (i == region_map.end()) {
361 return boost::shared_ptr<Region>();
367 boost::shared_ptr<Region>
368 RegionFactory::wholefile_region_by_name (const std::string& name)
370 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
371 if (i->second->whole_file() && i->second->name() == name) {
375 return boost::shared_ptr<Region>();
378 boost::shared_ptr<Region>
379 RegionFactory::region_by_name (const std::string& name)
381 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
382 if (i->second->name() == name) {
386 return boost::shared_ptr<Region>();
390 RegionFactory::clear_map ()
392 if (region_list_connections) {
393 region_list_connections->drop_connections ();
397 Glib::Threads::Mutex::Lock lm (region_map_lock);
399 _compound_associations.clear ();
400 region_name_map.clear ();
405 RegionFactory::delete_all_regions ()
409 /* copy region list */
411 Glib::Threads::Mutex::Lock lm (region_map_lock);
415 /* clear existing map */
418 /* tell everyone to drop references */
419 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
420 i->second->drop_references ();
423 /* the copy should now hold the only references, which will
424 vanish as we leave this scope, thus calling all destructors.
429 RegionFactory::nregions ()
431 Glib::Threads::Mutex::Lock lm (region_map_lock);
432 return region_map.size ();
435 /** Add a region to the two region name maps */
437 RegionFactory::add_to_region_name_maps (boost::shared_ptr<Region> region)
439 update_region_name_number_map (region);
441 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
442 region_name_map[region->name()] = region->id ();
445 /** Account for a region rename in the two region name maps */
447 RegionFactory::rename_in_region_name_maps (boost::shared_ptr<Region> region)
449 update_region_name_number_map (region);
451 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
453 map<string, PBD::ID>::iterator i = region_name_map.begin();
454 while (i != region_name_map.end() && i->second != region->id ()) {
458 /* Erase the entry for the old name and put in a new one */
459 if (i != region_name_map.end()) {
460 region_name_map.erase (i);
461 region_name_map[region->name()] = region->id ();
465 /** Remove a region's details from the region_name_map */
467 RegionFactory::remove_from_region_name_map (string n)
469 map<string, PBD::ID>::iterator i = region_name_map.find (n);
470 if (i != region_name_map.end ()) {
471 region_name_map.erase (i);
475 /** Update a region's entry in the region_name_number_map */
477 RegionFactory::update_region_name_number_map (boost::shared_ptr<Region> region)
479 string::size_type const last_period = region->name().find_last_of ('.');
481 if (last_period != string::npos && last_period < region->name().length() - 1) {
483 string const base = region->name().substr (0, last_period);
484 string const number = region->name().substr (last_period + 1);
486 /* note that if there is no number, we get zero from atoi,
490 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
491 region_name_number_map[base] = atoi (number.c_str ());
496 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
498 boost::shared_ptr<Region> r = w.lock ();
503 if (what_changed.contains (Properties::name)) {
504 rename_in_region_name_maps (r);
509 RegionFactory::region_name (string& result, string base, bool newlevel)
514 if (base.find("/") != string::npos) {
515 base = base.substr(base.find_last_of("/") + 1);
520 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
529 string::size_type pos;
531 pos = base.find_last_of ('.');
533 /* pos may be npos, but then we just use entire base */
535 subbase = base.substr (0, pos);
540 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
542 map<string,uint32_t>::iterator x;
546 if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end()) {
548 region_name_number_map[subbase] = 1;
551 snprintf (buf, sizeof (buf), ".%d", x->second);
562 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
565 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
567 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
572 RegionFactory::new_region_name (string old)
574 string::size_type last_period;
576 string::size_type len = old.length() + 64;
578 std::vector<char> buf(len);
580 if ((last_period = old.find_last_of ('.')) == string::npos) {
582 /* no period present - add one explicitly */
585 last_period = old.length() - 1;
590 if (last_period < old.length() - 1) {
592 string period_to_end = old.substr (last_period+1);
594 /* extra material after the period */
596 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
598 number = atoi (period_to_end);
600 if (numerals_end < period_to_end.length() - 1) {
601 /* extra material after the end of the digits */
602 remainder = period_to_end.substr (numerals_end);
606 last_period = old.length();
611 while (number < (UINT_MAX-1)) {
617 snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
620 if (region_name_map.find (sbuf) == region_name_map.end ()) {
625 if (number != (UINT_MAX-1)) {
629 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
634 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
636 Glib::Threads::Mutex::Lock lm (region_map_lock);
638 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
639 if (i->second->uses_source (s)) {
640 r.insert (i->second);
646 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
648 Glib::Threads::Mutex::Lock lm (region_map_lock);
650 RegionMap::iterator i = region_map.begin();
651 while (i != region_map.end()) {
653 RegionMap::iterator j = i;
656 if (i->second->uses_source (src)) {
657 remove_from_region_name_map (i->second->name ());
658 region_map.erase (i);
666 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
668 Glib::Threads::Mutex::Lock lm (region_map_lock);
669 _compound_associations[copy] = orig;