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 ret = boost::shared_ptr<Region> (new AudioRegion (ar, 0));
58 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
60 ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
63 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
69 ret->set_name (new_region_name(ret->name()));
72 /* pure copy constructor - no property list */
73 /* pure copy constructor - no CheckNewRegion emitted */
79 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
83 boost::shared_ptr<Region>
84 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
86 boost::shared_ptr<Region> ret;
87 boost::shared_ptr<const AudioRegion> other_a;
88 boost::shared_ptr<const MidiRegion> other_m;
90 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
92 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
94 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
96 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
99 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
102 return boost::shared_ptr<Region>();
106 ret->apply_changes (plist);
110 CheckNewRegion (ret);
114 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
118 boost::shared_ptr<Region>
119 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
121 boost::shared_ptr<Region> ret;
122 boost::shared_ptr<const AudioRegion> other_a;
123 boost::shared_ptr<const MidiRegion> other_m;
125 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
127 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
129 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
131 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
134 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
137 return boost::shared_ptr<Region>();
141 ret->apply_changes (plist);
145 CheckNewRegion (ret);
149 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
153 boost::shared_ptr<Region>
154 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
156 boost::shared_ptr<Region> ret;
157 boost::shared_ptr<const AudioRegion> other;
159 /* used by AudioFilter when constructing a new region that is intended to have nearly
160 identical settings to an original, but using different sources.
163 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
165 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
167 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
170 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
176 ret->apply_changes (plist);
180 CheckNewRegion (ret);
184 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
188 boost::shared_ptr<Region>
189 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
192 srcs.push_back (src);
193 return create (srcs, plist, announce);
196 boost::shared_ptr<Region>
197 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
199 boost::shared_ptr<Region> ret;
200 boost::shared_ptr<AudioSource> as;
201 boost::shared_ptr<MidiSource> ms;
203 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
205 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
207 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
209 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
214 ret->apply_changes (plist);
218 CheckNewRegion (ret);
222 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
226 boost::shared_ptr<Region>
227 RegionFactory::create (Session& session, XMLNode& node, bool yn)
229 return session.XMLRegionFactory (node, yn);
232 boost::shared_ptr<Region>
233 RegionFactory::create (SourceList& srcs, const XMLNode& node)
235 boost::shared_ptr<Region> ret;
241 if (srcs[0]->type() == DataType::AUDIO) {
243 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
245 } else if (srcs[0]->type() == DataType::MIDI) {
247 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
252 if (ret->set_state (node, Stateful::loading_state_version)) {
256 CheckNewRegion (ret);
260 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
265 RegionFactory::map_add (boost::shared_ptr<Region> r)
267 pair<ID,boost::shared_ptr<Region> > p;
272 Glib::Mutex::Lock lm (region_map_lock);
273 region_map.insert (p);
276 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
278 r->PropertyChanged.connect_same_thread (
279 region_list_connections,
280 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
283 update_region_name_map (r);
287 RegionFactory::map_remove (boost::shared_ptr<Region> r)
289 Glib::Mutex::Lock lm (region_map_lock);
290 RegionMap::iterator i = region_map.find (r->id());
292 if (i != region_map.end()) {
293 region_map.erase (i);
298 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
300 Glib::Mutex::Lock lm (region_map_lock);
302 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
303 RegionMap::iterator tmp = i;
306 if (r->region_list_equivalent (i->second)) {
307 region_map.erase (i);
308 } else if (r == i->second) {
309 region_map.erase (i);
316 boost::shared_ptr<Region>
317 RegionFactory::region_by_id (const PBD::ID& id)
319 RegionMap::iterator i = region_map.find (id);
321 if (i == region_map.end()) {
322 return boost::shared_ptr<Region>();
328 boost::shared_ptr<Region>
329 RegionFactory::wholefile_region_by_name (const std::string& name)
331 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
332 if (i->second->whole_file() && i->second->name() == name) {
336 return boost::shared_ptr<Region>();
339 boost::shared_ptr<Region>
340 RegionFactory::region_by_name (const std::string& name)
342 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
343 if (i->second->name() == name) {
347 return boost::shared_ptr<Region>();
351 RegionFactory::clear_map ()
353 region_list_connections.drop_connections ();
356 Glib::Mutex::Lock lm (region_map_lock);
363 RegionFactory::delete_all_regions ()
367 /* copy region list */
369 Glib::Mutex::Lock lm (region_map_lock);
373 /* clear existing map */
376 /* tell everyone to drop references */
377 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
378 i->second->drop_references ();
381 /* the copy should now hold the only references, which will
382 vanish as we leave this scope, thus calling all destructors.
387 RegionFactory::nregions ()
389 Glib::Mutex::Lock lm (region_map_lock);
390 return region_map.size ();
394 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
396 string::size_type const last_period = region->name().find_last_of ('.');
398 if (last_period != string::npos && last_period < region->name().length() - 1) {
400 string const base = region->name().substr (0, last_period);
401 string const number = region->name().substr (last_period + 1);
403 /* note that if there is no number, we get zero from atoi,
407 Glib::Mutex::Lock lm (region_name_map_lock);
408 region_name_map[base] = atoi (number.c_str ());
413 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
415 boost::shared_ptr<Region> r = w.lock ();
420 if (what_changed.contains (Properties::name)) {
421 update_region_name_map (r);
426 RegionFactory::region_name (string& result, string base, bool newlevel)
431 if (base.find("/") != string::npos) {
432 base = base.substr(base.find_last_of("/") + 1);
437 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
446 string::size_type pos;
448 pos = base.find_last_of ('.');
450 /* pos may be npos, but then we just use entire base */
452 subbase = base.substr (0, pos);
457 Glib::Mutex::Lock lm (region_name_map_lock);
459 map<string,uint32_t>::iterator x;
463 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
465 region_name_map[subbase] = 1;
468 snprintf (buf, sizeof (buf), ".%d", x->second);
479 RegionFactory::new_region_name (string old)
481 string::size_type last_period;
483 string::size_type len = old.length() + 64;
486 if ((last_period = old.find_last_of ('.')) == string::npos) {
488 /* no period present - add one explicitly */
491 last_period = old.length() - 1;
496 number = atoi (old.substr (last_period+1).c_str());
500 while (number < (UINT_MAX-1)) {
502 const RegionMap& regions (RegionFactory::regions());
503 RegionMap::const_iterator i;
508 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
511 for (i = regions.begin(); i != regions.end(); ++i) {
512 if (i->second->name() == sbuf) {
517 if (i == regions.end()) {
522 if (number != (UINT_MAX-1)) {
526 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
531 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
533 Glib::Mutex::Lock lm (region_map_lock);
535 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
536 if (i->second->uses_source (s)) {
537 r.insert (i->second);