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 = 0;
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 if (!region_list_connections) {
325 region_list_connections = new ScopedConnectionList;
328 r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
329 r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
331 update_region_name_map (r);
335 RegionFactory::map_remove (boost::weak_ptr<Region> w)
337 boost::shared_ptr<Region> r = w.lock ();
342 Glib::Mutex::Lock lm (region_map_lock);
343 RegionMap::iterator i = region_map.find (r->id());
345 if (i != region_map.end()) {
346 region_map.erase (i);
350 boost::shared_ptr<Region>
351 RegionFactory::region_by_id (const PBD::ID& id)
353 RegionMap::iterator i = region_map.find (id);
355 if (i == region_map.end()) {
356 return boost::shared_ptr<Region>();
362 boost::shared_ptr<Region>
363 RegionFactory::wholefile_region_by_name (const std::string& name)
365 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
366 if (i->second->whole_file() && i->second->name() == name) {
370 return boost::shared_ptr<Region>();
373 boost::shared_ptr<Region>
374 RegionFactory::region_by_name (const std::string& name)
376 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
377 if (i->second->name() == name) {
381 return boost::shared_ptr<Region>();
385 RegionFactory::clear_map ()
387 if (region_list_connections) {
388 region_list_connections->drop_connections ();
392 Glib::Mutex::Lock lm (region_map_lock);
394 _compound_associations.clear ();
399 RegionFactory::delete_all_regions ()
403 /* copy region list */
405 Glib::Mutex::Lock lm (region_map_lock);
409 /* clear existing map */
412 /* tell everyone to drop references */
413 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
414 i->second->drop_references ();
417 /* the copy should now hold the only references, which will
418 vanish as we leave this scope, thus calling all destructors.
423 RegionFactory::nregions ()
425 Glib::Mutex::Lock lm (region_map_lock);
426 return region_map.size ();
430 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
432 string::size_type const last_period = region->name().find_last_of ('.');
434 if (last_period != string::npos && last_period < region->name().length() - 1) {
436 string const base = region->name().substr (0, last_period);
437 string const number = region->name().substr (last_period + 1);
439 /* note that if there is no number, we get zero from atoi,
443 Glib::Mutex::Lock lm (region_name_map_lock);
444 region_name_map[base] = atoi (number.c_str ());
449 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
451 boost::shared_ptr<Region> r = w.lock ();
456 if (what_changed.contains (Properties::name)) {
457 update_region_name_map (r);
462 RegionFactory::region_name (string& result, string base, bool newlevel)
467 if (base.find("/") != string::npos) {
468 base = base.substr(base.find_last_of("/") + 1);
473 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
482 string::size_type pos;
484 pos = base.find_last_of ('.');
486 /* pos may be npos, but then we just use entire base */
488 subbase = base.substr (0, pos);
493 Glib::Mutex::Lock lm (region_name_map_lock);
495 map<string,uint32_t>::iterator x;
499 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
501 region_name_map[subbase] = 1;
504 snprintf (buf, sizeof (buf), ".%d", x->second);
515 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
518 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
520 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
525 RegionFactory::new_region_name (string old)
527 string::size_type last_period;
529 string::size_type len = old.length() + 64;
533 if ((last_period = old.find_last_of ('.')) == string::npos) {
535 /* no period present - add one explicitly */
538 last_period = old.length() - 1;
543 if (last_period < old.length() - 1) {
545 string period_to_end = old.substr (last_period+1);
547 /* extra material after the period */
549 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
551 number = atoi (period_to_end);
553 if (numerals_end < period_to_end.length() - 1) {
554 /* extra material after the end of the digits */
555 remainder = period_to_end.substr (numerals_end);
559 last_period = old.length();
564 while (number < (UINT_MAX-1)) {
566 const RegionMap& regions (RegionFactory::regions());
567 RegionMap::const_iterator i;
572 snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
575 for (i = regions.begin(); i != regions.end(); ++i) {
576 if (i->second->name() == sbuf) {
581 if (i == regions.end()) {
586 if (number != (UINT_MAX-1)) {
590 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
595 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
597 Glib::Mutex::Lock lm (region_map_lock);
599 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
600 if (i->second->uses_source (s)) {
601 r.insert (i->second);
607 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
609 Glib::Mutex::Lock lm (region_map_lock);
611 RegionMap::iterator i = region_map.begin();
612 while (i != region_map.end()) {
614 RegionMap::iterator j = i;
617 if (i->second->uses_source (src)) {
618 region_map.erase (i);
626 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
628 Glib::Mutex::Lock lm (region_map_lock);
629 _compound_associations[copy] = orig;