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()));
78 /* pure copy constructor - no property list */
84 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
85 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
90 boost::shared_ptr<Region>
91 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
93 boost::shared_ptr<Region> ret;
94 boost::shared_ptr<const AudioRegion> other_a;
95 boost::shared_ptr<const MidiRegion> other_m;
97 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
99 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
101 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
103 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
106 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
109 return boost::shared_ptr<Region>();
113 ret->apply_changes (plist);
117 CheckNewRegion (ret);
121 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
122 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
127 boost::shared_ptr<Region>
128 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
130 boost::shared_ptr<Region> ret;
131 boost::shared_ptr<const AudioRegion> other_a;
132 boost::shared_ptr<const MidiRegion> other_m;
134 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
136 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
138 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
140 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
143 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
146 return boost::shared_ptr<Region>();
150 ret->apply_changes (plist);
154 CheckNewRegion (ret);
158 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
159 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
164 boost::shared_ptr<Region>
165 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
167 boost::shared_ptr<Region> ret;
168 boost::shared_ptr<const AudioRegion> other;
170 /* used by AudioFilter when constructing a new region that is intended to have nearly
171 identical settings to an original, but using different sources.
174 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
176 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
178 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
181 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
187 ret->apply_changes (plist);
191 CheckNewRegion (ret);
195 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
196 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
201 boost::shared_ptr<Region>
202 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
205 srcs.push_back (src);
206 return create (srcs, plist, announce);
209 boost::shared_ptr<Region>
210 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
212 boost::shared_ptr<Region> ret;
213 boost::shared_ptr<AudioSource> as;
214 boost::shared_ptr<MidiSource> ms;
216 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
218 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
220 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
222 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
227 ret->apply_changes (plist);
231 CheckNewRegion (ret);
235 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
236 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
241 boost::shared_ptr<Region>
242 RegionFactory::create (Session& session, XMLNode& node, bool yn)
244 return session.XMLRegionFactory (node, yn);
247 boost::shared_ptr<Region>
248 RegionFactory::create (SourceList& srcs, const XMLNode& node)
250 boost::shared_ptr<Region> ret;
256 if (srcs[0]->type() == DataType::AUDIO) {
258 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
260 } else if (srcs[0]->type() == DataType::MIDI) {
262 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
267 if (ret->set_state (node, Stateful::loading_state_version)) {
271 CheckNewRegion (ret);
275 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
276 // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
282 RegionFactory::map_add (boost::shared_ptr<Region> r)
284 pair<ID,boost::shared_ptr<Region> > p;
289 Glib::Mutex::Lock lm (region_map_lock);
290 region_map.insert (p);
293 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
295 r->PropertyChanged.connect_same_thread (
296 region_list_connections,
297 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
300 update_region_name_map (r);
304 RegionFactory::map_remove (boost::shared_ptr<Region> r)
306 Glib::Mutex::Lock lm (region_map_lock);
307 RegionMap::iterator i = region_map.find (r->id());
309 if (i != region_map.end()) {
310 region_map.erase (i);
315 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
317 Glib::Mutex::Lock lm (region_map_lock);
319 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
320 RegionMap::iterator tmp = i;
323 if (r->region_list_equivalent (i->second)) {
324 region_map.erase (i);
325 } else if (r == i->second) {
326 region_map.erase (i);
333 boost::shared_ptr<Region>
334 RegionFactory::region_by_id (const PBD::ID& id)
336 RegionMap::iterator i = region_map.find (id);
338 if (i == region_map.end()) {
339 return boost::shared_ptr<Region>();
345 boost::shared_ptr<Region>
346 RegionFactory::wholefile_region_by_name (const std::string& name)
348 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
349 if (i->second->whole_file() && i->second->name() == name) {
353 return boost::shared_ptr<Region>();
356 boost::shared_ptr<Region>
357 RegionFactory::region_by_name (const std::string& name)
359 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
360 if (i->second->name() == name) {
364 return boost::shared_ptr<Region>();
368 RegionFactory::clear_map ()
370 region_list_connections.drop_connections ();
373 Glib::Mutex::Lock lm (region_map_lock);
375 _compound_associations.clear ();
380 RegionFactory::delete_all_regions ()
384 /* copy region list */
386 Glib::Mutex::Lock lm (region_map_lock);
390 /* clear existing map */
393 /* tell everyone to drop references */
394 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
395 i->second->drop_references ();
398 /* the copy should now hold the only references, which will
399 vanish as we leave this scope, thus calling all destructors.
404 RegionFactory::nregions ()
406 Glib::Mutex::Lock lm (region_map_lock);
407 return region_map.size ();
411 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
413 string::size_type const last_period = region->name().find_last_of ('.');
415 if (last_period != string::npos && last_period < region->name().length() - 1) {
417 string const base = region->name().substr (0, last_period);
418 string const number = region->name().substr (last_period + 1);
420 /* note that if there is no number, we get zero from atoi,
424 Glib::Mutex::Lock lm (region_name_map_lock);
425 region_name_map[base] = atoi (number.c_str ());
430 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
432 boost::shared_ptr<Region> r = w.lock ();
437 if (what_changed.contains (Properties::name)) {
438 update_region_name_map (r);
443 RegionFactory::region_name (string& result, string base, bool newlevel)
448 if (base.find("/") != string::npos) {
449 base = base.substr(base.find_last_of("/") + 1);
454 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
463 string::size_type pos;
465 pos = base.find_last_of ('.');
467 /* pos may be npos, but then we just use entire base */
469 subbase = base.substr (0, pos);
474 Glib::Mutex::Lock lm (region_name_map_lock);
476 map<string,uint32_t>::iterator x;
480 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
482 region_name_map[subbase] = 1;
485 snprintf (buf, sizeof (buf), ".%d", x->second);
496 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
499 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
501 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
506 RegionFactory::new_region_name (string old)
508 string::size_type last_period;
510 string::size_type len = old.length() + 64;
514 if ((last_period = old.find_last_of ('.')) == string::npos) {
516 /* no period present - add one explicitly */
519 last_period = old.length() - 1;
524 if (last_period < old.length() - 1) {
526 string period_to_end = old.substr (last_period+1);
528 /* extra material after the period */
530 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
532 number = atoi (period_to_end);
534 if (numerals_end < period_to_end.length() - 1) {
535 /* extra material after the end of the digits */
536 remainder = period_to_end.substr (numerals_end);
540 last_period = old.length();
545 while (number < (UINT_MAX-1)) {
547 const RegionMap& regions (RegionFactory::regions());
548 RegionMap::const_iterator i;
553 snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
556 for (i = regions.begin(); i != regions.end(); ++i) {
557 if (i->second->name() == sbuf) {
562 if (i == regions.end()) {
567 if (number != (UINT_MAX-1)) {
571 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
576 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
578 Glib::Mutex::Lock lm (region_map_lock);
580 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
581 if (i->second->uses_source (s)) {
582 r.insert (i->second);
588 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
590 Glib::Mutex::Lock lm (region_map_lock);
592 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
593 if (i->second->uses_source (src)) {
594 region_map.erase (i);
600 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
602 Glib::Mutex::Lock lm (region_map_lock);
603 _compound_associations[copy] = orig;