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"
23 #include "pbd/boost_debug.h"
25 #include "ardour/session.h"
27 #include "ardour/region_factory.h"
28 #include "ardour/region.h"
29 #include "ardour/audioregion.h"
30 #include "ardour/audiosource.h"
31 #include "ardour/midi_source.h"
32 #include "ardour/midi_region.h"
33 #include "ardour/utils.h"
37 using namespace ARDOUR;
41 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
42 Glib::StaticMutex RegionFactory::region_map_lock;
43 RegionFactory::RegionMap RegionFactory::region_map;
44 PBD::ScopedConnectionList RegionFactory::region_list_connections;
45 Glib::StaticMutex RegionFactory::region_name_map_lock;
46 std::map<std::string, uint32_t> RegionFactory::region_name_map;
47 RegionFactory::CompoundAssociations RegionFactory::_compound_associations;
49 boost::shared_ptr<Region>
50 RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
52 boost::shared_ptr<Region> ret;
53 boost::shared_ptr<const AudioRegion> ar;
54 boost::shared_ptr<const MidiRegion> mr;
56 if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
58 ret = boost::shared_ptr<Region> (new AudioRegion (ar, 0));
60 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
62 if (mr->session().config.get_midi_copy_is_fork()) {
65 ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
69 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
75 ret->set_name (new_region_name(ret->name()));
76 ret->set_position (region->position());
78 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
79 ret->set_position_lock_style (MusicTime);
84 /* pure copy constructor - no property list */
90 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
91 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
96 boost::shared_ptr<Region>
97 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
99 boost::shared_ptr<Region> ret;
100 boost::shared_ptr<const AudioRegion> other_a;
101 boost::shared_ptr<const MidiRegion> other_m;
103 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
105 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
107 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
109 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
112 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
115 return boost::shared_ptr<Region>();
119 ret->apply_changes (plist);
121 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
122 ret->set_position_lock_style (MusicTime);
128 CheckNewRegion (ret);
132 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
133 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
138 boost::shared_ptr<Region>
139 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
141 boost::shared_ptr<Region> ret;
142 boost::shared_ptr<const AudioRegion> other_a;
143 boost::shared_ptr<const MidiRegion> other_m;
145 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
147 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
149 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
151 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
154 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
157 return boost::shared_ptr<Region>();
161 ret->apply_changes (plist);
163 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
164 ret->set_position_lock_style (MusicTime);
170 CheckNewRegion (ret);
174 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
175 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
180 boost::shared_ptr<Region>
181 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
183 boost::shared_ptr<Region> ret;
184 boost::shared_ptr<const AudioRegion> other;
186 /* used by AudioFilter when constructing a new region that is intended to have nearly
187 identical settings to an original, but using different sources.
190 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
192 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
194 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
197 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
203 ret->apply_changes (plist);
205 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
206 ret->set_position_lock_style (MusicTime);
212 CheckNewRegion (ret);
216 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
217 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
222 boost::shared_ptr<Region>
223 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
226 srcs.push_back (src);
227 return create (srcs, plist, announce);
230 boost::shared_ptr<Region>
231 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
233 boost::shared_ptr<Region> ret;
234 boost::shared_ptr<AudioSource> as;
235 boost::shared_ptr<MidiSource> ms;
237 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
239 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
241 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
243 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
248 ret->apply_changes (plist);
250 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
251 ret->set_position_lock_style (MusicTime);
257 CheckNewRegion (ret);
261 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
262 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
267 boost::shared_ptr<Region>
268 RegionFactory::create (Session& session, XMLNode& node, bool yn)
270 return session.XMLRegionFactory (node, yn);
273 boost::shared_ptr<Region>
274 RegionFactory::create (SourceList& srcs, const XMLNode& node)
276 boost::shared_ptr<Region> ret;
282 if (srcs[0]->type() == DataType::AUDIO) {
284 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
286 } else if (srcs[0]->type() == DataType::MIDI) {
288 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
293 if (ret->set_state (node, Stateful::loading_state_version)) {
298 /* Don't fiddle with position_lock_style here as the region
299 description is coming from XML.
302 CheckNewRegion (ret);
306 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
307 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
313 RegionFactory::map_add (boost::shared_ptr<Region> r)
315 pair<ID,boost::shared_ptr<Region> > p;
320 Glib::Mutex::Lock lm (region_map_lock);
321 region_map.insert (p);
324 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
326 r->PropertyChanged.connect_same_thread (
327 region_list_connections,
328 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
331 update_region_name_map (r);
335 RegionFactory::map_remove (boost::shared_ptr<Region> r)
337 Glib::Mutex::Lock lm (region_map_lock);
338 RegionMap::iterator i = region_map.find (r->id());
340 if (i != region_map.end()) {
341 region_map.erase (i);
346 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
348 Glib::Mutex::Lock lm (region_map_lock);
350 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
351 RegionMap::iterator tmp = i;
354 if (r->region_list_equivalent (i->second)) {
355 region_map.erase (i);
356 } else if (r == i->second) {
357 region_map.erase (i);
364 boost::shared_ptr<Region>
365 RegionFactory::region_by_id (const PBD::ID& id)
367 RegionMap::iterator i = region_map.find (id);
369 if (i == region_map.end()) {
370 return boost::shared_ptr<Region>();
376 boost::shared_ptr<Region>
377 RegionFactory::wholefile_region_by_name (const std::string& name)
379 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
380 if (i->second->whole_file() && i->second->name() == name) {
384 return boost::shared_ptr<Region>();
387 boost::shared_ptr<Region>
388 RegionFactory::region_by_name (const std::string& name)
390 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
391 if (i->second->name() == name) {
395 return boost::shared_ptr<Region>();
399 RegionFactory::clear_map ()
401 region_list_connections.drop_connections ();
404 Glib::Mutex::Lock lm (region_map_lock);
406 _compound_associations.clear ();
411 RegionFactory::delete_all_regions ()
415 /* copy region list */
417 Glib::Mutex::Lock lm (region_map_lock);
421 /* clear existing map */
424 /* tell everyone to drop references */
425 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
426 i->second->drop_references ();
429 /* the copy should now hold the only references, which will
430 vanish as we leave this scope, thus calling all destructors.
435 RegionFactory::nregions ()
437 Glib::Mutex::Lock lm (region_map_lock);
438 return region_map.size ();
442 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
444 string::size_type const last_period = region->name().find_last_of ('.');
446 if (last_period != string::npos && last_period < region->name().length() - 1) {
448 string const base = region->name().substr (0, last_period);
449 string const number = region->name().substr (last_period + 1);
451 /* note that if there is no number, we get zero from atoi,
455 Glib::Mutex::Lock lm (region_name_map_lock);
456 region_name_map[base] = atoi (number.c_str ());
461 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
463 boost::shared_ptr<Region> r = w.lock ();
468 if (what_changed.contains (Properties::name)) {
469 update_region_name_map (r);
474 RegionFactory::region_name (string& result, string base, bool newlevel)
479 if (base.find("/") != string::npos) {
480 base = base.substr(base.find_last_of("/") + 1);
485 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
494 string::size_type pos;
496 pos = base.find_last_of ('.');
498 /* pos may be npos, but then we just use entire base */
500 subbase = base.substr (0, pos);
505 Glib::Mutex::Lock lm (region_name_map_lock);
507 map<string,uint32_t>::iterator x;
511 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
513 region_name_map[subbase] = 1;
516 snprintf (buf, sizeof (buf), ".%d", x->second);
527 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
530 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
532 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
537 RegionFactory::new_region_name (string old)
539 string::size_type last_period;
541 string::size_type len = old.length() + 64;
545 if ((last_period = old.find_last_of ('.')) == string::npos) {
547 /* no period present - add one explicitly */
550 last_period = old.length() - 1;
555 if (last_period < old.length() - 1) {
557 string period_to_end = old.substr (last_period+1);
559 /* extra material after the period */
561 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
563 number = atoi (period_to_end);
565 if (numerals_end < period_to_end.length() - 1) {
566 /* extra material after the end of the digits */
567 remainder = period_to_end.substr (numerals_end);
571 last_period = old.length();
576 while (number < (UINT_MAX-1)) {
578 const RegionMap& regions (RegionFactory::regions());
579 RegionMap::const_iterator i;
584 snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
587 for (i = regions.begin(); i != regions.end(); ++i) {
588 if (i->second->name() == sbuf) {
593 if (i == regions.end()) {
598 if (number != (UINT_MAX-1)) {
602 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
607 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
609 Glib::Mutex::Lock lm (region_map_lock);
611 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
612 if (i->second->uses_source (s)) {
613 r.insert (i->second);
619 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
621 Glib::Mutex::Lock lm (region_map_lock);
623 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
624 if (i->second->uses_source (src)) {
625 region_map.erase (i);
631 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
633 Glib::Mutex::Lock lm (region_map_lock);
634 _compound_associations[copy] = orig;