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 */
78 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
79 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
84 boost::shared_ptr<Region>
85 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
87 boost::shared_ptr<Region> ret;
88 boost::shared_ptr<const AudioRegion> other_a;
89 boost::shared_ptr<const MidiRegion> other_m;
91 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
93 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
95 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
97 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
100 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
103 return boost::shared_ptr<Region>();
107 ret->apply_changes (plist);
111 CheckNewRegion (ret);
115 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
116 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
121 boost::shared_ptr<Region>
122 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
124 boost::shared_ptr<Region> ret;
125 boost::shared_ptr<const AudioRegion> other_a;
126 boost::shared_ptr<const MidiRegion> other_m;
128 if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
130 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
132 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
134 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
137 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
140 return boost::shared_ptr<Region>();
144 ret->apply_changes (plist);
148 CheckNewRegion (ret);
152 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
153 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
158 boost::shared_ptr<Region>
159 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
161 boost::shared_ptr<Region> ret;
162 boost::shared_ptr<const AudioRegion> other;
164 /* used by AudioFilter when constructing a new region that is intended to have nearly
165 identical settings to an original, but using different sources.
168 if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
170 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
172 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
175 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
181 ret->apply_changes (plist);
185 CheckNewRegion (ret);
189 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
190 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
195 boost::shared_ptr<Region>
196 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
199 srcs.push_back (src);
200 return create (srcs, plist, announce);
203 boost::shared_ptr<Region>
204 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
206 boost::shared_ptr<Region> ret;
207 boost::shared_ptr<AudioSource> as;
208 boost::shared_ptr<MidiSource> ms;
210 if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
212 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
214 } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
216 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
221 ret->apply_changes (plist);
225 CheckNewRegion (ret);
229 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
230 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
235 boost::shared_ptr<Region>
236 RegionFactory::create (Session& session, XMLNode& node, bool yn)
238 return session.XMLRegionFactory (node, yn);
241 boost::shared_ptr<Region>
242 RegionFactory::create (SourceList& srcs, const XMLNode& node)
244 boost::shared_ptr<Region> ret;
250 if (srcs[0]->type() == DataType::AUDIO) {
252 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
254 } else if (srcs[0]->type() == DataType::MIDI) {
256 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
261 if (ret->set_state (node, Stateful::loading_state_version)) {
265 CheckNewRegion (ret);
269 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
270 boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
276 RegionFactory::map_add (boost::shared_ptr<Region> r)
278 pair<ID,boost::shared_ptr<Region> > p;
283 Glib::Mutex::Lock lm (region_map_lock);
284 region_map.insert (p);
287 r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
289 r->PropertyChanged.connect_same_thread (
290 region_list_connections,
291 boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
294 update_region_name_map (r);
298 RegionFactory::map_remove (boost::shared_ptr<Region> r)
300 Glib::Mutex::Lock lm (region_map_lock);
301 RegionMap::iterator i = region_map.find (r->id());
303 if (i != region_map.end()) {
304 region_map.erase (i);
309 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
311 Glib::Mutex::Lock lm (region_map_lock);
313 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
314 RegionMap::iterator tmp = i;
317 if (r->region_list_equivalent (i->second)) {
318 region_map.erase (i);
319 } else if (r == i->second) {
320 region_map.erase (i);
327 boost::shared_ptr<Region>
328 RegionFactory::region_by_id (const PBD::ID& id)
330 RegionMap::iterator i = region_map.find (id);
332 if (i == region_map.end()) {
333 return boost::shared_ptr<Region>();
339 boost::shared_ptr<Region>
340 RegionFactory::wholefile_region_by_name (const std::string& name)
342 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
343 if (i->second->whole_file() && i->second->name() == name) {
347 return boost::shared_ptr<Region>();
350 boost::shared_ptr<Region>
351 RegionFactory::region_by_name (const std::string& name)
353 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
354 if (i->second->name() == name) {
358 return boost::shared_ptr<Region>();
362 RegionFactory::clear_map ()
364 region_list_connections.drop_connections ();
367 Glib::Mutex::Lock lm (region_map_lock);
374 RegionFactory::delete_all_regions ()
378 /* copy region list */
380 Glib::Mutex::Lock lm (region_map_lock);
384 /* clear existing map */
387 /* tell everyone to drop references */
388 for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
389 i->second->drop_references ();
392 /* the copy should now hold the only references, which will
393 vanish as we leave this scope, thus calling all destructors.
398 RegionFactory::nregions ()
400 Glib::Mutex::Lock lm (region_map_lock);
401 return region_map.size ();
405 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
407 string::size_type const last_period = region->name().find_last_of ('.');
409 if (last_period != string::npos && last_period < region->name().length() - 1) {
411 string const base = region->name().substr (0, last_period);
412 string const number = region->name().substr (last_period + 1);
414 /* note that if there is no number, we get zero from atoi,
418 Glib::Mutex::Lock lm (region_name_map_lock);
419 region_name_map[base] = atoi (number.c_str ());
424 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
426 boost::shared_ptr<Region> r = w.lock ();
431 if (what_changed.contains (Properties::name)) {
432 update_region_name_map (r);
437 RegionFactory::region_name (string& result, string base, bool newlevel)
442 if (base.find("/") != string::npos) {
443 base = base.substr(base.find_last_of("/") + 1);
448 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
457 string::size_type pos;
459 pos = base.find_last_of ('.');
461 /* pos may be npos, but then we just use entire base */
463 subbase = base.substr (0, pos);
468 Glib::Mutex::Lock lm (region_name_map_lock);
470 map<string,uint32_t>::iterator x;
474 if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
476 region_name_map[subbase] = 1;
479 snprintf (buf, sizeof (buf), ".%d", x->second);
490 RegionFactory::new_region_name (string old)
492 string::size_type last_period;
494 string::size_type len = old.length() + 64;
497 if ((last_period = old.find_last_of ('.')) == string::npos) {
499 /* no period present - add one explicitly */
502 last_period = old.length() - 1;
507 number = atoi (old.substr (last_period+1).c_str());
511 while (number < (UINT_MAX-1)) {
513 const RegionMap& regions (RegionFactory::regions());
514 RegionMap::const_iterator i;
519 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
522 for (i = regions.begin(); i != regions.end(); ++i) {
523 if (i->second->name() == sbuf) {
528 if (i == regions.end()) {
533 if (number != (UINT_MAX-1)) {
537 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
542 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
544 Glib::Mutex::Lock lm (region_map_lock);
546 for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
547 if (i->second->uses_source (s)) {
548 r.insert (i->second);
554 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
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 (src)) {
560 region_map.erase (i);