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;
40 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
41 Glib::StaticMutex RegionFactory::region_map_lock;
42 RegionFactory::RegionMap RegionFactory::region_map;
43 PBD::ScopedConnectionList RegionFactory::region_list_connections;
44 Glib::StaticMutex RegionFactory::region_name_map_lock;
45 std::map<std::string, uint32_t> 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()) {
64 ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
68 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
74 ret->set_name (new_region_name(ret->name()));
77 /* pure copy constructor - no property list */
83 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
84 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
89 boost::shared_ptr<Region>
90 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
92 boost::shared_ptr<Region> ret;
93 boost::shared_ptr<const AudioRegion> other_a;
94 boost::shared_ptr<const MidiRegion> other_m;
96 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
98 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
100 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
102 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
105 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
108 return boost::shared_ptr<Region>();
112 ret->apply_changes (plist);
116 CheckNewRegion (ret);
120 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
121 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
126 boost::shared_ptr<Region>
127 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
129 boost::shared_ptr<Region> ret;
130 boost::shared_ptr<const AudioRegion> other_a;
131 boost::shared_ptr<const MidiRegion> other_m;
133 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
135 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
137 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
139 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
142 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
145 return boost::shared_ptr<Region>();
149 ret->apply_changes (plist);
153 CheckNewRegion (ret);
157 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
158 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
163 boost::shared_ptr<Region>
164 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
166 boost::shared_ptr<Region> ret;
167 boost::shared_ptr<const AudioRegion> other;
169 /* used by AudioFilter when constructing a new region that is intended to have nearly
170 identical settings to an original, but using different sources.
173 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
175 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
177 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
180 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
186 ret->apply_changes (plist);
190 CheckNewRegion (ret);
194 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
195 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
200 boost::shared_ptr<Region>
201 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
204 srcs.push_back (src);
205 return create (srcs, plist, announce);
208 boost::shared_ptr<Region>
209 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
211 boost::shared_ptr<Region> ret;
212 boost::shared_ptr<AudioSource> as;
213 boost::shared_ptr<MidiSource> ms;
215 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
217 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
219 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
221 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
226 ret->apply_changes (plist);
230 CheckNewRegion (ret);
234 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
235 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
240 boost::shared_ptr<Region>
241 RegionFactory::create (Session& session, XMLNode& node, bool yn)
243 return session.XMLRegionFactory (node, yn);
246 boost::shared_ptr<Region>
247 RegionFactory::create (SourceList& srcs, const XMLNode& node)
249 boost::shared_ptr<Region> ret;
255 if (srcs[0]->type() == DataType::AUDIO) {
257 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
259 } else if (srcs[0]->type() == DataType::MIDI) {
261 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
266 if (ret->set_state (node, Stateful::loading_state_version)) {
270 CheckNewRegion (ret);
274 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
275 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
281 RegionFactory::map_add (boost::shared_ptr<Region> r)
283 pair<ID,boost::shared_ptr<Region> > p;
288 Glib::Mutex::Lock lm (region_map_lock);
289 region_map.insert (p);
292 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
294 r->PropertyChanged.connect_same_thread (
295 region_list_connections,
296 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
299 update_region_name_map (r);
303 RegionFactory::map_remove (boost::shared_ptr<Region> r)
305 Glib::Mutex::Lock lm (region_map_lock);
306 RegionMap::iterator i = region_map.find (r->id());
308 if (i != region_map.end()) {
309 region_map.erase (i);
314 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
316 Glib::Mutex::Lock lm (region_map_lock);
318 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
319 RegionMap::iterator tmp = i;
322 if (r->region_list_equivalent (i->second)) {
323 region_map.erase (i);
324 } else if (r == i->second) {
325 region_map.erase (i);
332 boost::shared_ptr<Region>
333 RegionFactory::region_by_id (const PBD::ID& id)
335 RegionMap::iterator i = region_map.find (id);
337 if (i == region_map.end()) {
338 return boost::shared_ptr<Region>();
344 boost::shared_ptr<Region>
345 RegionFactory::wholefile_region_by_name (const std::string& name)
347 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
348 if (i->second->whole_file() && i->second->name() == name) {
352 return boost::shared_ptr<Region>();
355 boost::shared_ptr<Region>
356 RegionFactory::region_by_name (const std::string& name)
358 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
359 if (i->second->name() == name) {
363 return boost::shared_ptr<Region>();
367 RegionFactory::clear_map ()
369 region_list_connections.drop_connections ();
372 Glib::Mutex::Lock lm (region_map_lock);
374 _compound_associations.clear ();
379 RegionFactory::delete_all_regions ()
383 /* copy region list */
385 Glib::Mutex::Lock lm (region_map_lock);
389 /* clear existing map */
392 /* tell everyone to drop references */
393 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
394 i->second->drop_references ();
397 /* the copy should now hold the only references, which will
398 vanish as we leave this scope, thus calling all destructors.
403 RegionFactory::nregions ()
405 Glib::Mutex::Lock lm (region_map_lock);
406 return region_map.size ();
410 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
412 string::size_type const last_period = region->name().find_last_of ('.');
414 if (last_period != string::npos && last_period < region->name().length() - 1) {
416 string const base = region->name().substr (0, last_period);
417 string const number = region->name().substr (last_period + 1);
419 /* note that if there is no number, we get zero from atoi,
423 Glib::Mutex::Lock lm (region_name_map_lock);
424 region_name_map[base] = atoi (number.c_str ());
429 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
431 boost::shared_ptr<Region> r = w.lock ();
436 if (what_changed.contains (Properties::name)) {
437 update_region_name_map (r);
442 RegionFactory::region_name (string& result, string base, bool newlevel)
447 if (base.find("/") != string::npos) {
448 base = base.substr(base.find_last_of("/") + 1);
453 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
462 string::size_type pos;
464 pos = base.find_last_of ('.');
466 /* pos may be npos, but then we just use entire base */
468 subbase = base.substr (0, pos);
473 Glib::Mutex::Lock lm (region_name_map_lock);
475 map<string,uint32_t>::iterator x;
479 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
481 region_name_map[subbase] = 1;
484 snprintf (buf, sizeof (buf), ".%d", x->second);
495 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
498 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
500 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
505 RegionFactory::new_region_name (string old)
507 string::size_type last_period;
509 string::size_type len = old.length() + 64;
513 if ((last_period = old.find_last_of ('.')) == string::npos) {
515 /* no period present - add one explicitly */
518 last_period = old.length() - 1;
523 if (last_period < old.length() - 1) {
525 string period_to_end = old.substr (last_period+1);
527 /* extra material after the period */
529 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
531 number = atoi (period_to_end);
533 if (numerals_end < period_to_end.length() - 1) {
534 /* extra material after the end of the digits */
535 remainder = period_to_end.substr (numerals_end);
539 last_period = old.length();
544 while (number < (UINT_MAX-1)) {
546 const RegionMap& regions (RegionFactory::regions());
547 RegionMap::const_iterator i;
552 snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
555 for (i = regions.begin(); i != regions.end(); ++i) {
556 if (i->second->name() == sbuf) {
561 if (i == regions.end()) {
566 if (number != (UINT_MAX-1)) {
570 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
575 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
577 Glib::Mutex::Lock lm (region_map_lock);
579 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
580 if (i->second->uses_source (s)) {
581 r.insert (i->second);
587 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
589 Glib::Mutex::Lock lm (region_map_lock);
591 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
592 if (i->second->uses_source (src)) {
593 region_map.erase (i);
599 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
601 Glib::Mutex::Lock lm (region_map_lock);
602 _compound_associations[copy] = orig;