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 ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
64 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
70 ret->set_name (new_region_name(ret->name()));
73 /* pure copy constructor - no property list */
79 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
80 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
85 boost::shared_ptr<Region>
86 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
88 boost::shared_ptr<Region> ret;
89 boost::shared_ptr<const AudioRegion> other_a;
90 boost::shared_ptr<const MidiRegion> other_m;
92 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
94 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
96 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
98 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
101 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
104 return boost::shared_ptr<Region>();
108 ret->apply_changes (plist);
112 CheckNewRegion (ret);
116 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
117 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
122 boost::shared_ptr<Region>
123 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
125 boost::shared_ptr<Region> ret;
126 boost::shared_ptr<const AudioRegion> other_a;
127 boost::shared_ptr<const MidiRegion> other_m;
129 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
131 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
133 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
135 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
138 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
141 return boost::shared_ptr<Region>();
145 ret->apply_changes (plist);
149 CheckNewRegion (ret);
153 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
154 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
159 boost::shared_ptr<Region>
160 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
162 boost::shared_ptr<Region> ret;
163 boost::shared_ptr<const AudioRegion> other;
165 /* used by AudioFilter when constructing a new region that is intended to have nearly
166 identical settings to an original, but using different sources.
169 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
171 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
173 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
176 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
182 ret->apply_changes (plist);
186 CheckNewRegion (ret);
190 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
191 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
196 boost::shared_ptr<Region>
197 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
200 srcs.push_back (src);
201 return create (srcs, plist, announce);
204 boost::shared_ptr<Region>
205 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
207 boost::shared_ptr<Region> ret;
208 boost::shared_ptr<AudioSource> as;
209 boost::shared_ptr<MidiSource> ms;
211 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
213 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
215 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
217 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
222 ret->apply_changes (plist);
226 CheckNewRegion (ret);
230 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
231 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
236 boost::shared_ptr<Region>
237 RegionFactory::create (Session& session, XMLNode& node, bool yn)
239 return session.XMLRegionFactory (node, yn);
242 boost::shared_ptr<Region>
243 RegionFactory::create (SourceList& srcs, const XMLNode& node)
245 boost::shared_ptr<Region> ret;
251 if (srcs[0]->type() == DataType::AUDIO) {
253 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
255 } else if (srcs[0]->type() == DataType::MIDI) {
257 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
262 if (ret->set_state (node, Stateful::loading_state_version)) {
266 CheckNewRegion (ret);
270 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
271 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
277 RegionFactory::map_add (boost::shared_ptr<Region> r)
279 pair<ID,boost::shared_ptr<Region> > p;
284 Glib::Mutex::Lock lm (region_map_lock);
285 region_map.insert (p);
288 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
290 r->PropertyChanged.connect_same_thread (
291 region_list_connections,
292 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
295 update_region_name_map (r);
299 RegionFactory::map_remove (boost::shared_ptr<Region> r)
301 Glib::Mutex::Lock lm (region_map_lock);
302 RegionMap::iterator i = region_map.find (r->id());
304 if (i != region_map.end()) {
305 region_map.erase (i);
310 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
312 Glib::Mutex::Lock lm (region_map_lock);
314 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
315 RegionMap::iterator tmp = i;
318 if (r->region_list_equivalent (i->second)) {
319 region_map.erase (i);
320 } else if (r == i->second) {
321 region_map.erase (i);
328 boost::shared_ptr<Region>
329 RegionFactory::region_by_id (const PBD::ID& id)
331 RegionMap::iterator i = region_map.find (id);
333 if (i == region_map.end()) {
334 return boost::shared_ptr<Region>();
340 boost::shared_ptr<Region>
341 RegionFactory::wholefile_region_by_name (const std::string& name)
343 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
344 if (i->second->whole_file() && i->second->name() == name) {
348 return boost::shared_ptr<Region>();
351 boost::shared_ptr<Region>
352 RegionFactory::region_by_name (const std::string& name)
354 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
355 if (i->second->name() == name) {
359 return boost::shared_ptr<Region>();
363 RegionFactory::clear_map ()
365 region_list_connections.drop_connections ();
368 Glib::Mutex::Lock lm (region_map_lock);
370 _compound_associations.clear ();
375 RegionFactory::delete_all_regions ()
379 /* copy region list */
381 Glib::Mutex::Lock lm (region_map_lock);
385 /* clear existing map */
388 /* tell everyone to drop references */
389 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
390 i->second->drop_references ();
393 /* the copy should now hold the only references, which will
394 vanish as we leave this scope, thus calling all destructors.
399 RegionFactory::nregions ()
401 Glib::Mutex::Lock lm (region_map_lock);
402 return region_map.size ();
406 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
408 string::size_type const last_period = region->name().find_last_of ('.');
410 if (last_period != string::npos && last_period < region->name().length() - 1) {
412 string const base = region->name().substr (0, last_period);
413 string const number = region->name().substr (last_period + 1);
415 /* note that if there is no number, we get zero from atoi,
419 Glib::Mutex::Lock lm (region_name_map_lock);
420 region_name_map[base] = atoi (number.c_str ());
425 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
427 boost::shared_ptr<Region> r = w.lock ();
432 if (what_changed.contains (Properties::name)) {
433 update_region_name_map (r);
438 RegionFactory::region_name (string& result, string base, bool newlevel)
443 if (base.find("/") != string::npos) {
444 base = base.substr(base.find_last_of("/") + 1);
449 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
458 string::size_type pos;
460 pos = base.find_last_of ('.');
462 /* pos may be npos, but then we just use entire base */
464 subbase = base.substr (0, pos);
469 Glib::Mutex::Lock lm (region_name_map_lock);
471 map<string,uint32_t>::iterator x;
475 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
477 region_name_map[subbase] = 1;
480 snprintf (buf, sizeof (buf), ".%d", x->second);
491 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
494 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
496 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
501 RegionFactory::new_region_name (string old)
503 string::size_type last_period;
505 string::size_type len = old.length() + 64;
509 if ((last_period = old.find_last_of ('.')) == string::npos) {
511 /* no period present - add one explicitly */
514 last_period = old.length() - 1;
519 if (last_period < old.length() - 1) {
521 string period_to_end = old.substr (last_period+1);
523 /* extra material after the period */
525 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
527 number = atoi (period_to_end);
529 if (numerals_end < period_to_end.length() - 1) {
530 /* extra material after the end of the digits */
531 remainder = period_to_end.substr (numerals_end);
535 last_period = old.length();
540 while (number < (UINT_MAX-1)) {
542 const RegionMap& regions (RegionFactory::regions());
543 RegionMap::const_iterator i;
548 snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
551 for (i = regions.begin(); i != regions.end(); ++i) {
552 if (i->second->name() == sbuf) {
557 if (i == regions.end()) {
562 if (number != (UINT_MAX-1)) {
566 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
571 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
573 Glib::Mutex::Lock lm (region_map_lock);
575 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
576 if (i->second->uses_source (s)) {
577 r.insert (i->second);
583 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
585 Glib::Mutex::Lock lm (region_map_lock);
587 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
588 if (i->second->uses_source (src)) {
589 region_map.erase (i);
595 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
597 Glib::Mutex::Lock lm (region_map_lock);
598 _compound_associations[copy] = orig;