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_debug_shared_ptr_mark_interesting (arn, "Region");
59 boost::shared_ptr<AudioRegion> arp (arn);
60 ret = boost::static_pointer_cast<Region> (arp);
62 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
64 MidiRegion* mrn = new MidiRegion (mr, 0);
65 boost::shared_ptr<MidiRegion> mrp (mrn);
66 ret = boost::static_pointer_cast<Region> (mrp);
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 */
79 /* pure copy constructor - no CheckNewRegion emitted */
89 boost::shared_ptr<Region>
90 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
92 boost::shared_ptr<Region> ret;
93 boost::shared_ptr<const AudioRegion> other_a;
94 boost::shared_ptr<const MidiRegion> other_m;
96 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
98 AudioRegion* ar = new AudioRegion (other_a);
99 boost::shared_ptr<AudioRegion> arp (ar);
100 ret = boost::static_pointer_cast<Region> (arp);
102 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
104 MidiRegion* mr = new MidiRegion (other_m);
105 boost::shared_ptr<MidiRegion> mrp (mr);
106 ret = boost::static_pointer_cast<Region> (mrp);
109 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
112 return boost::shared_ptr<Region>();
116 ret->apply_changes (plist);
120 CheckNewRegion (ret);
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 AudioRegion* ar = new AudioRegion (other_a, offset);
137 boost_debug_shared_ptr_mark_interesting (ar, "Region");
139 boost::shared_ptr<AudioRegion> arp (ar);
140 ret = boost::static_pointer_cast<Region> (arp);
142 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
144 MidiRegion* mr = new MidiRegion (other_m, offset);
145 boost::shared_ptr<MidiRegion> mrp (mr);
146 ret = boost::static_pointer_cast<Region> (mrp);
149 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
152 return boost::shared_ptr<Region>();
156 ret->apply_changes (plist);
160 CheckNewRegion (ret);
167 boost::shared_ptr<Region>
168 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
170 boost::shared_ptr<Region> ret;
171 boost::shared_ptr<const AudioRegion> other;
173 /* used by AudioFilter when constructing a new region that is intended to have nearly
174 identical settings to an original, but using different sources.
177 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
179 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
181 AudioRegion* ar = new AudioRegion (other, srcs);
182 boost_debug_shared_ptr_mark_interesting (ar, "Region");
184 boost::shared_ptr<AudioRegion> arp (ar);
185 ret = boost::static_pointer_cast<Region> (arp);
188 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
195 ret->apply_changes (plist);
199 CheckNewRegion (ret);
207 boost::shared_ptr<Region>
208 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
211 srcs.push_back (src);
212 return create (srcs, plist, announce);
215 boost::shared_ptr<Region>
216 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
218 boost::shared_ptr<Region> ret;
219 boost::shared_ptr<AudioSource> as;
220 boost::shared_ptr<MidiSource> ms;
222 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
224 AudioRegion* ar = new AudioRegion (srcs);
225 boost_debug_shared_ptr_mark_interesting (ar, "Region");
227 boost::shared_ptr<AudioRegion> arp (ar);
228 ret = boost::static_pointer_cast<Region> (arp);
230 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
231 MidiRegion* mr = new MidiRegion (srcs);
232 boost_debug_shared_ptr_mark_interesting (mr, "Region");
234 boost::shared_ptr<MidiRegion> mrp (mr);
235 ret = boost::static_pointer_cast<Region> (mrp);
240 ret->apply_changes (plist);
244 CheckNewRegion (ret);
251 boost::shared_ptr<Region>
252 RegionFactory::create (Session& session, XMLNode& node, bool yn)
254 return session.XMLRegionFactory (node, yn);
257 boost::shared_ptr<Region>
258 RegionFactory::create (SourceList& srcs, const XMLNode& node)
260 boost::shared_ptr<Region> ret;
266 if (srcs[0]->type() == DataType::AUDIO) {
268 AudioRegion* ar = new AudioRegion (srcs);
269 boost_debug_shared_ptr_mark_interesting (ar, "Region");
271 boost::shared_ptr<AudioRegion> arp (ar);
272 ret = boost::static_pointer_cast<Region> (arp);
274 } else if (srcs[0]->type() == DataType::MIDI) {
276 MidiRegion* mr = new MidiRegion (srcs);
278 boost::shared_ptr<MidiRegion> mrp (mr);
279 ret = boost::static_pointer_cast<Region> (mrp);
284 if (ret->set_state (node, Stateful::loading_state_version)) {
288 CheckNewRegion (ret);
296 RegionFactory::map_add (boost::shared_ptr<Region> r)
298 pair<ID,boost::shared_ptr<Region> > p;
303 Glib::Mutex::Lock lm (region_map_lock);
304 region_map.insert (p);
307 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
309 r->PropertyChanged.connect_same_thread (
310 region_list_connections,
311 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
314 update_region_name_map (r);
318 RegionFactory::map_remove (boost::shared_ptr<Region> r)
320 Glib::Mutex::Lock lm (region_map_lock);
321 RegionMap::iterator i = region_map.find (r->id());
323 if (i != region_map.end()) {
324 region_map.erase (i);
329 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
331 Glib::Mutex::Lock lm (region_map_lock);
333 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
334 RegionMap::iterator tmp = i;
337 if (r->region_list_equivalent (i->second)) {
338 region_map.erase (i);
339 } else if (r == i->second) {
340 region_map.erase (i);
347 boost::shared_ptr<Region>
348 RegionFactory::region_by_id (const PBD::ID& id)
350 RegionMap::iterator i = region_map.find (id);
352 if (i == region_map.end()) {
353 return boost::shared_ptr<Region>();
359 boost::shared_ptr<Region>
360 RegionFactory::wholefile_region_by_name (const std::string& name)
362 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
363 if (i->second->whole_file() && i->second->name() == name) {
367 return boost::shared_ptr<Region>();
370 boost::shared_ptr<Region>
371 RegionFactory::region_by_name (const std::string& name)
373 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
374 if (i->second->name() == name) {
378 return boost::shared_ptr<Region>();
382 RegionFactory::clear_map ()
384 region_list_connections.drop_connections ();
387 Glib::Mutex::Lock lm (region_map_lock);
394 RegionFactory::delete_all_regions ()
398 /* copy region list */
400 Glib::Mutex::Lock lm (region_map_lock);
404 /* clear existing map */
407 /* tell everyone to drop references */
408 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
409 i->second->drop_references ();
412 /* the copy should now hold the only references, which will
413 vanish as we leave this scope, thus calling all destructors.
418 RegionFactory::nregions ()
420 Glib::Mutex::Lock lm (region_map_lock);
421 return region_map.size ();
425 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
427 string::size_type const last_period = region->name().find_last_of ('.');
429 if (last_period != string::npos && last_period < region->name().length() - 1) {
431 string const base = region->name().substr (0, last_period);
432 string const number = region->name().substr (last_period + 1);
434 /* note that if there is no number, we get zero from atoi,
438 Glib::Mutex::Lock lm (region_name_map_lock);
439 region_name_map[base] = atoi (number.c_str ());
444 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
446 boost::shared_ptr<Region> r = w.lock ();
451 if (what_changed.contains (Properties::name)) {
452 update_region_name_map (r);
457 RegionFactory::region_name (string& result, string base, bool newlevel)
462 if (base.find("/") != string::npos) {
463 base = base.substr(base.find_last_of("/") + 1);
468 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
477 string::size_type pos;
479 pos = base.find_last_of ('.');
481 /* pos may be npos, but then we just use entire base */
483 subbase = base.substr (0, pos);
488 Glib::Mutex::Lock lm (region_name_map_lock);
490 map<string,uint32_t>::iterator x;
494 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
496 region_name_map[subbase] = 1;
499 snprintf (buf, sizeof (buf), ".%d", x->second);
510 RegionFactory::new_region_name (string old)
512 string::size_type last_period;
514 string::size_type len = old.length() + 64;
517 if ((last_period = old.find_last_of ('.')) == string::npos) {
519 /* no period present - add one explicitly */
522 last_period = old.length() - 1;
527 number = atoi (old.substr (last_period+1).c_str());
531 while (number < (UINT_MAX-1)) {
533 const RegionMap& regions (RegionFactory::regions());
534 RegionMap::const_iterator i;
539 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
542 for (i = regions.begin(); i != regions.end(); ++i) {
543 if (i->second->name() == sbuf) {
548 if (i == regions.end()) {
553 if (number != (UINT_MAX-1)) {
557 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
562 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
564 Glib::Mutex::Lock lm (region_map_lock);
566 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
567 if (i->second->uses_source (s)) {
568 r.insert (i->second);