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::StaticMutex RegionFactory::region_map_lock;
40 RegionFactory::RegionMap RegionFactory::region_map;
41 PBD::ScopedConnectionList* RegionFactory::region_list_connections = 0;
42 Glib::StaticMutex RegionFactory::region_name_map_lock;
43 std::map<std::string, uint32_t> RegionFactory::region_name_map;
44 RegionFactory::CompoundAssociations RegionFactory::_compound_associations;
46 boost::shared_ptr<Region>
47 RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
49 boost::shared_ptr<Region> ret;
50 boost::shared_ptr<const AudioRegion> ar;
51 boost::shared_ptr<const MidiRegion> mr;
53 if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
55 ret = boost::shared_ptr<Region> (new AudioRegion (ar, 0));
57 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
59 if (mr->session().config.get_midi_copy_is_fork()) {
62 ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
66 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
72 ret->set_name (new_region_name(ret->name()));
73 ret->set_position (region->position());
75 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
76 ret->set_position_lock_style (MusicTime);
79 /* pure copy constructor - no property list */
86 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
87 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
92 boost::shared_ptr<Region>
93 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
95 boost::shared_ptr<Region> ret;
96 boost::shared_ptr<const AudioRegion> other_a;
97 boost::shared_ptr<const MidiRegion> other_m;
99 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
101 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
103 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
105 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
108 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
111 return boost::shared_ptr<Region>();
115 ret->apply_changes (plist);
117 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
118 ret->set_position_lock_style (MusicTime);
123 CheckNewRegion (ret);
127 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
128 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
133 boost::shared_ptr<Region>
134 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
136 boost::shared_ptr<Region> ret;
137 boost::shared_ptr<const AudioRegion> other_a;
138 boost::shared_ptr<const MidiRegion> other_m;
140 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
142 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
144 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
146 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
149 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
152 return boost::shared_ptr<Region>();
156 ret->apply_changes (plist);
158 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
159 ret->set_position_lock_style (MusicTime);
164 CheckNewRegion (ret);
168 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
169 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
174 boost::shared_ptr<Region>
175 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
177 boost::shared_ptr<Region> ret;
178 boost::shared_ptr<const AudioRegion> other;
180 /* used by AudioFilter when constructing a new region that is intended to have nearly
181 identical settings to an original, but using different sources.
184 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
186 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
188 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
191 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
197 ret->apply_changes (plist);
199 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
200 ret->set_position_lock_style (MusicTime);
205 CheckNewRegion (ret);
209 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
210 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
215 boost::shared_ptr<Region>
216 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
219 srcs.push_back (src);
220 return create (srcs, plist, announce);
223 boost::shared_ptr<Region>
224 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
226 boost::shared_ptr<Region> ret;
227 boost::shared_ptr<AudioSource> as;
228 boost::shared_ptr<MidiSource> ms;
230 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
232 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
234 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
236 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
241 ret->apply_changes (plist);
243 if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
244 ret->set_position_lock_style (MusicTime);
249 CheckNewRegion (ret);
253 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
254 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
259 boost::shared_ptr<Region>
260 RegionFactory::create (Session& session, XMLNode& node, bool yn)
262 return session.XMLRegionFactory (node, yn);
265 boost::shared_ptr<Region>
266 RegionFactory::create (SourceList& srcs, const XMLNode& node)
268 boost::shared_ptr<Region> ret;
274 if (srcs[0]->type() == DataType::AUDIO) {
276 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
278 } else if (srcs[0]->type() == DataType::MIDI) {
280 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
285 if (ret->set_state (node, Stateful::loading_state_version)) {
290 /* Don't fiddle with position_lock_style here as the region
291 description is coming from XML.
294 CheckNewRegion (ret);
298 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
299 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
305 RegionFactory::map_add (boost::shared_ptr<Region> r)
307 pair<ID,boost::shared_ptr<Region> > p;
312 Glib::Mutex::Lock lm (region_map_lock);
313 region_map.insert (p);
316 if (!region_list_connections) {
317 region_list_connections = new ScopedConnectionList;
320 r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
321 r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
323 update_region_name_map (r);
327 RegionFactory::map_remove (boost::weak_ptr<Region> w)
329 boost::shared_ptr<Region> r = w.lock ();
334 Glib::Mutex::Lock lm (region_map_lock);
335 RegionMap::iterator i = region_map.find (r->id());
337 if (i != region_map.end()) {
338 region_map.erase (i);
342 boost::shared_ptr<Region>
343 RegionFactory::region_by_id (const PBD::ID& id)
345 RegionMap::iterator i = region_map.find (id);
347 if (i == region_map.end()) {
348 return boost::shared_ptr<Region>();
354 boost::shared_ptr<Region>
355 RegionFactory::wholefile_region_by_name (const std::string& name)
357 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
358 if (i->second->whole_file() && i->second->name() == name) {
362 return boost::shared_ptr<Region>();
365 boost::shared_ptr<Region>
366 RegionFactory::region_by_name (const std::string& name)
368 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
369 if (i->second->name() == name) {
373 return boost::shared_ptr<Region>();
377 RegionFactory::clear_map ()
379 if (region_list_connections) {
380 region_list_connections->drop_connections ();
384 Glib::Mutex::Lock lm (region_map_lock);
386 _compound_associations.clear ();
391 RegionFactory::delete_all_regions ()
395 /* copy region list */
397 Glib::Mutex::Lock lm (region_map_lock);
401 /* clear existing map */
404 /* tell everyone to drop references */
405 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
406 i->second->drop_references ();
409 /* the copy should now hold the only references, which will
410 vanish as we leave this scope, thus calling all destructors.
415 RegionFactory::nregions ()
417 Glib::Mutex::Lock lm (region_map_lock);
418 return region_map.size ();
422 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
424 string::size_type const last_period = region->name().find_last_of ('.');
426 if (last_period != string::npos && last_period < region->name().length() - 1) {
428 string const base = region->name().substr (0, last_period);
429 string const number = region->name().substr (last_period + 1);
431 /* note that if there is no number, we get zero from atoi,
435 Glib::Mutex::Lock lm (region_name_map_lock);
436 region_name_map[base] = atoi (number.c_str ());
441 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
443 boost::shared_ptr<Region> r = w.lock ();
448 if (what_changed.contains (Properties::name)) {
449 update_region_name_map (r);
454 RegionFactory::region_name (string& result, string base, bool newlevel)
459 if (base.find("/") != string::npos) {
460 base = base.substr(base.find_last_of("/") + 1);
465 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
474 string::size_type pos;
476 pos = base.find_last_of ('.');
478 /* pos may be npos, but then we just use entire base */
480 subbase = base.substr (0, pos);
485 Glib::Mutex::Lock lm (region_name_map_lock);
487 map<string,uint32_t>::iterator x;
491 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
493 region_name_map[subbase] = 1;
496 snprintf (buf, sizeof (buf), ".%d", x->second);
507 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
510 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
512 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
517 RegionFactory::new_region_name (string old)
519 string::size_type last_period;
521 string::size_type len = old.length() + 64;
525 if ((last_period = old.find_last_of ('.')) == string::npos) {
527 /* no period present - add one explicitly */
530 last_period = old.length() - 1;
535 if (last_period < old.length() - 1) {
537 string period_to_end = old.substr (last_period+1);
539 /* extra material after the period */
541 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
543 number = atoi (period_to_end);
545 if (numerals_end < period_to_end.length() - 1) {
546 /* extra material after the end of the digits */
547 remainder = period_to_end.substr (numerals_end);
551 last_period = old.length();
556 while (number < (UINT_MAX-1)) {
558 const RegionMap& regions (RegionFactory::regions());
559 RegionMap::const_iterator i;
564 snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
567 for (i = regions.begin(); i != regions.end(); ++i) {
568 if (i->second->name() == sbuf) {
573 if (i == regions.end()) {
578 if (number != (UINT_MAX-1)) {
582 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
587 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
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 (s)) {
593 r.insert (i->second);
599 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
601 Glib::Mutex::Lock lm (region_map_lock);
603 RegionMap::iterator i = region_map.begin();
604 while (i != region_map.end()) {
606 RegionMap::iterator j = i;
609 if (i->second->uses_source (src)) {
610 region_map.erase (i);
618 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
620 Glib::Mutex::Lock lm (region_map_lock);
621 _compound_associations[copy] = orig;