2 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2016-2017 Nick Mainsbridge <mainsbridge@gmail.com>
7 * Copyright (C) 2018 Ben Loftis <ben@harrisonconsoles.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "pbd/basename.h"
27 #include "pbd/error.h"
29 #include "ardour/audioregion.h"
30 #include "ardour/audiosource.h"
31 #include "ardour/midi_region.h"
32 #include "ardour/midi_source.h"
33 #include "ardour/region.h"
34 #include "ardour/region_factory.h"
35 #include "ardour/session.h"
39 using namespace ARDOUR;
43 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
44 Glib::Threads::Mutex RegionFactory::region_map_lock;
45 RegionFactory::RegionMap RegionFactory::region_map;
46 PBD::ScopedConnectionList* RegionFactory::region_list_connections = 0;
47 Glib::Threads::Mutex RegionFactory::region_name_maps_mutex;
48 std::map<std::string, uint32_t> RegionFactory::region_name_number_map;
49 std::map<std::string, PBD::ID> RegionFactory::region_name_map;
50 RegionFactory::CompoundAssociations RegionFactory::_compound_associations;
52 boost::shared_ptr<Region>
53 RegionFactory::create (boost::shared_ptr<const Region> region, bool announce, bool fork)
55 boost::shared_ptr<Region> ret;
56 boost::shared_ptr<const AudioRegion> ar;
57 boost::shared_ptr<const MidiRegion> mr;
59 if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
61 ret = boost::shared_ptr<Region> (new AudioRegion (ar, MusicSample (0, 0)));
63 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
65 if (mr->session().config.get_midi_copy_is_fork() || fork) {
66 /* What we really want to do here is what Editor::fork_region()
67 does via Session::create_midi_source_by_stealing_name(), but we
68 don't have a Track. We'll just live with the skipped number,
69 and store the ancestral name of sources so multiple clones
70 generates reasonable names that don't have too many suffixes. */
71 const std::string ancestor_name = mr->sources().front()->ancestor_name();
72 const std::string base = PBD::basename_nosuffix(ancestor_name);
74 boost::shared_ptr<MidiSource> source = mr->session().create_midi_source_for_session(base);
75 source->set_ancestor_name(mr->sources().front()->name());
76 ret = mr->clone(source);
78 ret = boost::shared_ptr<Region> (new MidiRegion (mr, MusicSample (0, 0)));
82 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
84 abort(); /*NOTREACHED*/
88 ret->set_name (new_region_name(ret->name()));
90 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
91 ret->set_position_lock_style (MusicTime);
94 /* pure copy constructor - no property list */
101 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
102 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
107 boost::shared_ptr<Region>
108 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
110 boost::shared_ptr<Region> ret;
111 boost::shared_ptr<const AudioRegion> other_a;
112 boost::shared_ptr<const MidiRegion> other_m;
114 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
116 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
118 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
120 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
123 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
125 abort(); /*NOTREACHED*/
126 return boost::shared_ptr<Region>();
130 ret->apply_changes (plist);
132 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
133 ret->set_position_lock_style (MusicTime);
138 CheckNewRegion (ret);
142 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
143 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
148 boost::shared_ptr<Region>
149 RegionFactory::create (boost::shared_ptr<Region> region, MusicSample offset, const PropertyList& plist, bool announce)
151 boost::shared_ptr<Region> ret;
152 boost::shared_ptr<const AudioRegion> other_a;
153 boost::shared_ptr<const MidiRegion> other_m;
155 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
157 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
159 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
161 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
164 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
166 abort(); /*NOTREACHED*/
167 return boost::shared_ptr<Region>();
171 ret->apply_changes (plist);
173 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
174 ret->set_position_lock_style (MusicTime);
179 CheckNewRegion (ret);
183 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
184 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
189 boost::shared_ptr<Region>
190 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
192 boost::shared_ptr<Region> ret;
193 boost::shared_ptr<const AudioRegion> other;
195 /* used by AudioFilter when constructing a new region that is intended to have nearly
196 identical settings to an original, but using different sources.
199 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
201 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->natural_position())
203 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
206 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
208 abort(); /*NOTREACHED*/
212 ret->apply_changes (plist);
214 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
215 ret->set_position_lock_style (MusicTime);
220 CheckNewRegion (ret);
224 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
225 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
230 boost::shared_ptr<Region>
231 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
234 srcs.push_back (src);
235 return create (srcs, plist, announce);
238 boost::shared_ptr<Region>
239 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
241 boost::shared_ptr<Region> ret;
242 boost::shared_ptr<AudioSource> as;
243 boost::shared_ptr<MidiSource> ms;
245 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
247 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
249 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
251 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
256 ret->apply_changes (plist);
258 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
259 ret->set_position_lock_style (MusicTime);
264 CheckNewRegion (ret);
268 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
269 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
274 boost::shared_ptr<Region>
275 RegionFactory::create (Session& session, XMLNode& node, bool yn)
277 return session.XMLRegionFactory (node, yn);
280 boost::shared_ptr<Region>
281 RegionFactory::create (SourceList& srcs, const XMLNode& node)
283 boost::shared_ptr<Region> ret;
289 if (srcs[0]->type() == DataType::AUDIO) {
291 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
293 } else if (srcs[0]->type() == DataType::MIDI) {
295 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
300 if (ret->set_state (node, Stateful::loading_state_version)) {
305 /* Don't fiddle with position_lock_style here as the region
306 description is coming from XML.
309 CheckNewRegion (ret);
313 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
314 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
320 RegionFactory::map_add (boost::shared_ptr<Region> r)
322 pair<ID,boost::shared_ptr<Region> > p;
327 Glib::Threads::Mutex::Lock lm (region_map_lock);
328 region_map.insert (p);
331 if (!region_list_connections) {
332 region_list_connections = new ScopedConnectionList;
335 r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
336 r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
338 add_to_region_name_maps (r);
342 RegionFactory::map_remove (boost::weak_ptr<Region> w)
344 boost::shared_ptr<Region> r = w.lock ();
349 Glib::Threads::Mutex::Lock lm (region_map_lock);
350 RegionMap::iterator i = region_map.find (r->id());
352 if (i != region_map.end()) {
353 remove_from_region_name_map (i->second->name ());
354 region_map.erase (i);
358 boost::shared_ptr<Region>
359 RegionFactory::region_by_id (const PBD::ID& id)
361 RegionMap::iterator i = region_map.find (id);
363 if (i == region_map.end()) {
364 return boost::shared_ptr<Region>();
370 boost::shared_ptr<Region>
371 RegionFactory::wholefile_region_by_name (const std::string& name)
373 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
374 if (i->second->whole_file() && i->second->name() == name) {
378 return boost::shared_ptr<Region>();
381 boost::shared_ptr<Region>
382 RegionFactory::region_by_name (const std::string& name)
384 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
385 if (i->second->name() == name) {
389 return boost::shared_ptr<Region>();
393 RegionFactory::clear_map ()
395 if (region_list_connections) {
396 region_list_connections->drop_connections ();
400 Glib::Threads::Mutex::Lock lm (region_map_lock);
402 _compound_associations.clear ();
403 region_name_map.clear ();
408 RegionFactory::delete_all_regions ()
412 /* copy region list */
414 Glib::Threads::Mutex::Lock lm (region_map_lock);
418 /* clear existing map */
421 /* tell everyone to drop references */
422 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
423 i->second->drop_references ();
426 /* the copy should now hold the only references, which will
427 vanish as we leave this scope, thus calling all destructors.
432 RegionFactory::nregions ()
434 Glib::Threads::Mutex::Lock lm (region_map_lock);
435 return region_map.size ();
438 /** Add a region to the two region name maps */
440 RegionFactory::add_to_region_name_maps (boost::shared_ptr<Region> region)
442 update_region_name_number_map (region);
444 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
445 region_name_map[region->name()] = region->id ();
448 /** Account for a region rename in the two region name maps */
450 RegionFactory::rename_in_region_name_maps (boost::shared_ptr<Region> region)
452 update_region_name_number_map (region);
454 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
456 map<string, PBD::ID>::iterator i = region_name_map.begin();
457 while (i != region_name_map.end() && i->second != region->id ()) {
461 /* Erase the entry for the old name and put in a new one */
462 if (i != region_name_map.end()) {
463 region_name_map.erase (i);
464 region_name_map[region->name()] = region->id ();
468 /** Remove a region's details from the region_name_map */
470 RegionFactory::remove_from_region_name_map (string n)
472 map<string, PBD::ID>::iterator i = region_name_map.find (n);
473 if (i != region_name_map.end ()) {
474 region_name_map.erase (i);
478 /** Update a region's entry in the region_name_number_map */
480 RegionFactory::update_region_name_number_map (boost::shared_ptr<Region> region)
482 string::size_type const last_period = region->name().find_last_of ('.');
484 if (last_period != string::npos && last_period < region->name().length() - 1) {
486 string const base = region->name().substr (0, last_period);
487 string const number = region->name().substr (last_period + 1);
489 /* note that if there is no number, we get zero from atoi,
493 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
494 region_name_number_map[base] = atoi (number.c_str ());
499 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
501 boost::shared_ptr<Region> r = w.lock ();
506 if (what_changed.contains (Properties::name)) {
507 rename_in_region_name_maps (r);
512 RegionFactory::region_name (string& result, string base, bool newlevel)
517 if (base.find("/") != string::npos) {
518 base = base.substr(base.find_last_of("/") + 1);
523 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
532 string::size_type pos;
534 pos = base.find_last_of ('.');
536 /* pos may be npos, but then we just use entire base */
538 subbase = base.substr (0, pos);
543 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
545 map<string,uint32_t>::iterator x;
549 if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end()) {
551 region_name_number_map[subbase] = 1;
554 snprintf (buf, sizeof (buf), ".%d", x->second);
565 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
568 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
570 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
575 RegionFactory::new_region_name (string old)
577 string::size_type last_period;
579 string::size_type len = old.length() + 64;
581 std::vector<char> buf(len);
583 if ((last_period = old.find_last_of ('.')) == string::npos) {
585 /* no period present - add one explicitly */
588 last_period = old.length() - 1;
593 if (last_period < old.length() - 1) {
595 string period_to_end = old.substr (last_period+1);
597 /* extra material after the period */
599 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
601 number = atoi (period_to_end);
603 if (numerals_end < period_to_end.length() - 1) {
604 /* extra material after the end of the digits */
605 remainder = period_to_end.substr (numerals_end);
609 last_period = old.length();
614 while (number < (UINT_MAX-1)) {
620 snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
623 if (region_name_map.find (sbuf) == region_name_map.end ()) {
628 if (number != (UINT_MAX-1)) {
632 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
636 boost::shared_ptr<Region>
637 RegionFactory::get_whole_region_for_source (boost::shared_ptr<Source> s)
639 Glib::Threads::Mutex::Lock lm (region_map_lock);
641 for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
642 if (i->second->uses_source (s) && i->second->whole_file()) {
647 return boost::shared_ptr<Region>();
651 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
653 Glib::Threads::Mutex::Lock lm (region_map_lock);
655 for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
656 if (i->second->uses_source (s)) {
657 r.insert (i->second);
663 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
665 Glib::Threads::Mutex::Lock lm (region_map_lock);
667 RegionMap::iterator i = region_map.begin();
668 while (i != region_map.end()) {
670 RegionMap::iterator j = i;
673 if (i->second->uses_source (src)) {
674 remove_from_region_name_map (i->second->name ());
675 region_map.erase (i);
683 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
685 Glib::Threads::Mutex::Lock lm (region_map_lock);
686 _compound_associations[copy] = orig;