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"
24 #include "ardour/audioregion.h"
25 #include "ardour/audiosource.h"
26 #include "ardour/midi_region.h"
27 #include "ardour/midi_source.h"
28 #include "ardour/region.h"
29 #include "ardour/region_factory.h"
30 #include "ardour/session.h"
34 using namespace ARDOUR;
38 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
39 Glib::Threads::Mutex RegionFactory::region_map_lock;
40 RegionFactory::RegionMap RegionFactory::region_map;
41 PBD::ScopedConnectionList* RegionFactory::region_list_connections = 0;
42 Glib::Threads::Mutex RegionFactory::region_name_maps_mutex;
43 std::map<std::string, uint32_t> RegionFactory::region_name_number_map;
44 std::map<std::string, PBD::ID> RegionFactory::region_name_map;
45 RegionFactory::CompoundAssociations RegionFactory::_compound_associations;
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 if (mr->session().config.get_midi_copy_is_fork()) {
63 ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
67 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
73 ret->set_name (new_region_name(ret->name()));
74 ret->set_position (region->position());
76 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
77 ret->set_position_lock_style (MusicTime);
80 /* pure copy constructor - no property list */
87 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
88 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
93 boost::shared_ptr<Region>
94 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
96 boost::shared_ptr<Region> ret;
97 boost::shared_ptr<const AudioRegion> other_a;
98 boost::shared_ptr<const MidiRegion> other_m;
100 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
102 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
104 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
106 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
109 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
112 return boost::shared_ptr<Region>();
116 ret->apply_changes (plist);
118 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
119 ret->set_position_lock_style (MusicTime);
124 CheckNewRegion (ret);
128 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
129 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
134 boost::shared_ptr<Region>
135 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
137 boost::shared_ptr<Region> ret;
138 boost::shared_ptr<const AudioRegion> other_a;
139 boost::shared_ptr<const MidiRegion> other_m;
141 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
143 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
145 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
147 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
150 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
153 return boost::shared_ptr<Region>();
157 ret->apply_changes (plist);
159 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
160 ret->set_position_lock_style (MusicTime);
165 CheckNewRegion (ret);
169 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
170 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
175 boost::shared_ptr<Region>
176 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
178 boost::shared_ptr<Region> ret;
179 boost::shared_ptr<const AudioRegion> other;
181 /* used by AudioFilter when constructing a new region that is intended to have nearly
182 identical settings to an original, but using different sources.
185 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
187 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
189 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
192 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
198 ret->apply_changes (plist);
200 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
201 ret->set_position_lock_style (MusicTime);
206 CheckNewRegion (ret);
210 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
211 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
216 boost::shared_ptr<Region>
217 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
220 srcs.push_back (src);
221 return create (srcs, plist, announce);
224 boost::shared_ptr<Region>
225 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
227 boost::shared_ptr<Region> ret;
228 boost::shared_ptr<AudioSource> as;
229 boost::shared_ptr<MidiSource> ms;
231 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
233 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
235 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
237 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
242 ret->apply_changes (plist);
244 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
245 ret->set_position_lock_style (MusicTime);
250 CheckNewRegion (ret);
254 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
255 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
260 boost::shared_ptr<Region>
261 RegionFactory::create (Session& session, XMLNode& node, bool yn)
263 return session.XMLRegionFactory (node, yn);
266 boost::shared_ptr<Region>
267 RegionFactory::create (SourceList& srcs, const XMLNode& node)
269 boost::shared_ptr<Region> ret;
275 if (srcs[0]->type() == DataType::AUDIO) {
277 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
279 } else if (srcs[0]->type() == DataType::MIDI) {
281 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
286 if (ret->set_state (node, Stateful::loading_state_version)) {
291 /* Don't fiddle with position_lock_style here as the region
292 description is coming from XML.
295 CheckNewRegion (ret);
299 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
300 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
306 RegionFactory::map_add (boost::shared_ptr<Region> r)
308 pair<ID,boost::shared_ptr<Region> > p;
313 Glib::Threads::Mutex::Lock lm (region_map_lock);
314 region_map.insert (p);
317 if (!region_list_connections) {
318 region_list_connections = new ScopedConnectionList;
321 r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
322 r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
324 add_to_region_name_maps (r);
328 RegionFactory::map_remove (boost::weak_ptr<Region> w)
330 boost::shared_ptr<Region> r = w.lock ();
335 Glib::Threads::Mutex::Lock lm (region_map_lock);
336 RegionMap::iterator i = region_map.find (r->id());
338 if (i != region_map.end()) {
339 remove_from_region_name_map (i->second->name ());
340 region_map.erase (i);
344 boost::shared_ptr<Region>
345 RegionFactory::region_by_id (const PBD::ID& id)
347 RegionMap::iterator i = region_map.find (id);
349 if (i == region_map.end()) {
350 return boost::shared_ptr<Region>();
356 boost::shared_ptr<Region>
357 RegionFactory::wholefile_region_by_name (const std::string& name)
359 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
360 if (i->second->whole_file() && i->second->name() == name) {
364 return boost::shared_ptr<Region>();
367 boost::shared_ptr<Region>
368 RegionFactory::region_by_name (const std::string& name)
370 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
371 if (i->second->name() == name) {
375 return boost::shared_ptr<Region>();
379 RegionFactory::clear_map ()
381 if (region_list_connections) {
382 region_list_connections->drop_connections ();
386 Glib::Threads::Mutex::Lock lm (region_map_lock);
388 _compound_associations.clear ();
389 region_name_map.clear ();
394 RegionFactory::delete_all_regions ()
398 /* copy region list */
400 Glib::Threads::Mutex::Lock lm (region_map_lock);
404 /* clear existing map */
407 /* tell everyone to drop references */
408 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
409 i->second->drop_references ();
412 /* the copy should now hold the only references, which will
413 vanish as we leave this scope, thus calling all destructors.
418 RegionFactory::nregions ()
420 Glib::Threads::Mutex::Lock lm (region_map_lock);
421 return region_map.size ();
424 /** Add a region to the two region name maps */
426 RegionFactory::add_to_region_name_maps (boost::shared_ptr<Region> region)
428 update_region_name_number_map (region);
430 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
431 region_name_map[region->name()] = region->id ();
434 /** Account for a region rename in the two region name maps */
436 RegionFactory::rename_in_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);
442 map<string, PBD::ID>::iterator i = region_name_map.begin();
443 while (i != region_name_map.end() && i->second != region->id ()) {
447 /* Erase the entry for the old name and put in a new one */
448 if (i != region_name_map.end()) {
449 region_name_map.erase (i);
450 region_name_map[region->name()] = region->id ();
454 /** Remove a region's details from the region_name_map */
456 RegionFactory::remove_from_region_name_map (string n)
458 map<string, PBD::ID>::iterator i = region_name_map.find (n);
459 if (i != region_name_map.end ()) {
460 region_name_map.erase (i);
464 /** Update a region's entry in the region_name_number_map */
466 RegionFactory::update_region_name_number_map (boost::shared_ptr<Region> region)
468 string::size_type const last_period = region->name().find_last_of ('.');
470 if (last_period != string::npos && last_period < region->name().length() - 1) {
472 string const base = region->name().substr (0, last_period);
473 string const number = region->name().substr (last_period + 1);
475 /* note that if there is no number, we get zero from atoi,
479 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
480 region_name_number_map[base] = atoi (number.c_str ());
485 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
487 boost::shared_ptr<Region> r = w.lock ();
492 if (what_changed.contains (Properties::name)) {
493 rename_in_region_name_maps (r);
498 RegionFactory::region_name (string& result, string base, bool newlevel)
503 if (base.find("/") != string::npos) {
504 base = base.substr(base.find_last_of("/") + 1);
509 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
518 string::size_type pos;
520 pos = base.find_last_of ('.');
522 /* pos may be npos, but then we just use entire base */
524 subbase = base.substr (0, pos);
529 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
531 map<string,uint32_t>::iterator x;
535 if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end()) {
537 region_name_number_map[subbase] = 1;
540 snprintf (buf, sizeof (buf), ".%d", x->second);
551 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
554 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
556 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
561 RegionFactory::new_region_name (string old)
563 string::size_type last_period;
565 string::size_type len = old.length() + 64;
569 if ((last_period = old.find_last_of ('.')) == string::npos) {
571 /* no period present - add one explicitly */
574 last_period = old.length() - 1;
579 if (last_period < old.length() - 1) {
581 string period_to_end = old.substr (last_period+1);
583 /* extra material after the period */
585 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
587 number = atoi (period_to_end);
589 if (numerals_end < period_to_end.length() - 1) {
590 /* extra material after the end of the digits */
591 remainder = period_to_end.substr (numerals_end);
595 last_period = old.length();
600 while (number < (UINT_MAX-1)) {
606 snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
609 if (region_name_map.find (sbuf) == region_name_map.end ()) {
614 if (number != (UINT_MAX-1)) {
618 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
623 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
625 Glib::Threads::Mutex::Lock lm (region_map_lock);
627 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
628 if (i->second->uses_source (s)) {
629 r.insert (i->second);
635 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
637 Glib::Threads::Mutex::Lock lm (region_map_lock);
639 RegionMap::iterator i = region_map.begin();
640 while (i != region_map.end()) {
642 RegionMap::iterator j = i;
645 if (i->second->uses_source (src)) {
646 remove_from_region_name_map (i->second->name ());
647 region_map.erase (i);
655 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
657 Glib::Threads::Mutex::Lock lm (region_map_lock);
658 _compound_associations[copy] = orig;