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;
47 boost::shared_ptr<Region>
48 RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
50 boost::shared_ptr<Region> ret;
51 boost::shared_ptr<const AudioRegion> ar;
52 boost::shared_ptr<const MidiRegion> mr;
54 if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
56 AudioRegion* arn = new AudioRegion (ar, 0);
57 boost::shared_ptr<AudioRegion> arp (arn);
58 ret = boost::static_pointer_cast<Region> (arp);
60 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
62 MidiRegion* mrn = new MidiRegion (mr, 0);
63 boost::shared_ptr<MidiRegion> mrp (mrn);
64 ret = boost::static_pointer_cast<Region> (mrp);
67 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
73 ret->set_name (new_region_name(ret->name()));
76 /* pure copy constructor - no property list */
77 /* pure copy constructor - no CheckNewRegion emitted */
83 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
87 boost::shared_ptr<Region>
88 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
90 boost::shared_ptr<Region> ret;
91 boost::shared_ptr<const AudioRegion> other_a;
92 boost::shared_ptr<const MidiRegion> other_m;
94 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
96 AudioRegion* ar = new AudioRegion (other_a);
97 boost::shared_ptr<AudioRegion> arp (ar);
98 ret = boost::static_pointer_cast<Region> (arp);
100 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
102 MidiRegion* mr = new MidiRegion (other_m);
103 boost::shared_ptr<MidiRegion> mrp (mr);
104 ret = boost::static_pointer_cast<Region> (mrp);
107 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
110 return boost::shared_ptr<Region>();
114 ret->apply_changes (plist);
118 CheckNewRegion (ret);
122 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 AudioRegion* ar = new AudioRegion (other_a, offset);
136 boost::shared_ptr<AudioRegion> arp (ar);
137 ret = boost::static_pointer_cast<Region> (arp);
139 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
141 MidiRegion* mr = new MidiRegion (other_m, offset);
142 boost::shared_ptr<MidiRegion> mrp (mr);
143 ret = boost::static_pointer_cast<Region> (mrp);
146 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
149 return boost::shared_ptr<Region>();
153 ret->apply_changes (plist);
157 CheckNewRegion (ret);
161 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
165 boost::shared_ptr<Region>
166 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
168 boost::shared_ptr<Region> ret;
169 boost::shared_ptr<const AudioRegion> other;
171 /* used by AudioFilter when constructing a new region that is intended to have nearly
172 identical settings to an original, but using different sources.
175 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
177 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
179 AudioRegion* ar = new AudioRegion (other, srcs);
180 boost::shared_ptr<AudioRegion> arp (ar);
181 ret = boost::static_pointer_cast<Region> (arp);
184 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
190 ret->apply_changes (plist);
194 CheckNewRegion (ret);
198 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
202 boost::shared_ptr<Region>
203 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
206 srcs.push_back (src);
207 return create (srcs, plist, announce);
210 boost::shared_ptr<Region>
211 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
213 boost::shared_ptr<Region> ret;
214 boost::shared_ptr<AudioSource> as;
215 boost::shared_ptr<MidiSource> ms;
217 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
219 AudioRegion* ar = new AudioRegion (srcs);
220 boost::shared_ptr<AudioRegion> arp (ar);
221 ret = boost::static_pointer_cast<Region> (arp);
223 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
225 MidiRegion* mr = new MidiRegion (srcs);
226 boost::shared_ptr<MidiRegion> mrp (mr);
227 ret = boost::static_pointer_cast<Region> (mrp);
232 ret->apply_changes (plist);
236 CheckNewRegion (ret);
240 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
244 boost::shared_ptr<Region>
245 RegionFactory::create (Session& session, XMLNode& node, bool yn)
247 return session.XMLRegionFactory (node, yn);
250 boost::shared_ptr<Region>
251 RegionFactory::create (SourceList& srcs, const XMLNode& node)
253 boost::shared_ptr<Region> ret;
259 if (srcs[0]->type() == DataType::AUDIO) {
261 AudioRegion* ar = new AudioRegion (srcs);
262 boost::shared_ptr<AudioRegion> arp (ar);
263 ret = boost::static_pointer_cast<Region> (arp);
265 } else if (srcs[0]->type() == DataType::MIDI) {
267 MidiRegion* mr = new MidiRegion (srcs);
268 boost::shared_ptr<MidiRegion> mrp (mr);
269 ret = boost::static_pointer_cast<Region> (mrp);
275 if (ret->set_state (node, Stateful::loading_state_version)) {
279 CheckNewRegion (ret);
283 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
288 RegionFactory::map_add (boost::shared_ptr<Region> r)
290 pair<ID,boost::shared_ptr<Region> > p;
295 Glib::Mutex::Lock lm (region_map_lock);
296 region_map.insert (p);
299 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
301 r->PropertyChanged.connect_same_thread (
302 region_list_connections,
303 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
306 update_region_name_map (r);
310 RegionFactory::map_remove (boost::shared_ptr<Region> r)
312 Glib::Mutex::Lock lm (region_map_lock);
313 RegionMap::iterator i = region_map.find (r->id());
315 if (i != region_map.end()) {
316 region_map.erase (i);
321 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
323 Glib::Mutex::Lock lm (region_map_lock);
325 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
326 RegionMap::iterator tmp = i;
329 if (r->region_list_equivalent (i->second)) {
330 region_map.erase (i);
331 } else if (r == i->second) {
332 region_map.erase (i);
339 boost::shared_ptr<Region>
340 RegionFactory::region_by_id (const PBD::ID& id)
342 RegionMap::iterator i = region_map.find (id);
344 if (i == region_map.end()) {
345 return boost::shared_ptr<Region>();
351 boost::shared_ptr<Region>
352 RegionFactory::wholefile_region_by_name (const std::string& name)
354 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
355 if (i->second->whole_file() && i->second->name() == name) {
359 return boost::shared_ptr<Region>();
362 boost::shared_ptr<Region>
363 RegionFactory::region_by_name (const std::string& name)
365 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
366 if (i->second->name() == name) {
370 return boost::shared_ptr<Region>();
374 RegionFactory::clear_map ()
376 region_list_connections.drop_connections ();
379 Glib::Mutex::Lock lm (region_map_lock);
386 RegionFactory::delete_all_regions ()
390 /* copy region list */
392 Glib::Mutex::Lock lm (region_map_lock);
396 /* clear existing map */
399 /* tell everyone to drop references */
400 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
401 i->second->drop_references ();
404 /* the copy should now hold the only references, which will
405 vanish as we leave this scope, thus calling all destructors.
410 RegionFactory::nregions ()
412 Glib::Mutex::Lock lm (region_map_lock);
413 return region_map.size ();
417 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
419 string::size_type const last_period = region->name().find_last_of ('.');
421 if (last_period != string::npos && last_period < region->name().length() - 1) {
423 string const base = region->name().substr (0, last_period);
424 string const number = region->name().substr (last_period + 1);
426 /* note that if there is no number, we get zero from atoi,
430 Glib::Mutex::Lock lm (region_name_map_lock);
431 region_name_map[base] = atoi (number.c_str ());
436 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
438 boost::shared_ptr<Region> r = w.lock ();
443 if (what_changed.contains (Properties::name)) {
444 update_region_name_map (r);
449 RegionFactory::region_name (string& result, string base, bool newlevel)
454 if (base.find("/") != string::npos) {
455 base = base.substr(base.find_last_of("/") + 1);
460 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
469 string::size_type pos;
471 pos = base.find_last_of ('.');
473 /* pos may be npos, but then we just use entire base */
475 subbase = base.substr (0, pos);
480 Glib::Mutex::Lock lm (region_name_map_lock);
482 map<string,uint32_t>::iterator x;
486 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
488 region_name_map[subbase] = 1;
491 snprintf (buf, sizeof (buf), ".%d", x->second);
502 RegionFactory::new_region_name (string old)
504 string::size_type last_period;
506 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 number = atoi (old.substr (last_period+1).c_str());
523 while (number < (UINT_MAX-1)) {
525 const RegionMap& regions (RegionFactory::regions());
526 RegionMap::const_iterator i;
531 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
534 for (i = regions.begin(); i != regions.end(); ++i) {
535 if (i->second->name() == sbuf) {
540 if (i == regions.end()) {
545 if (number != (UINT_MAX-1)) {
549 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
554 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
556 Glib::Mutex::Lock lm (region_map_lock);
558 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
559 if (i->second->uses_source (s)) {
560 r.insert (i->second);