ardour
region_factory.cc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2000-2006 Paul Davis
3 
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.
8 
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.
13 
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.
17 
18 */
19 
20 #include <inttypes.h>
21 
22 #include "pbd/basename.h"
23 #include "pbd/error.h"
24 
25 #include "ardour/audioregion.h"
26 #include "ardour/audiosource.h"
27 #include "ardour/midi_region.h"
28 #include "ardour/midi_source.h"
29 #include "ardour/region.h"
30 #include "ardour/region_factory.h"
31 #include "ardour/session.h"
32 
33 #include "i18n.h"
34 
35 using namespace ARDOUR;
36 using namespace PBD;
37 using namespace std;
38 
39 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
40 Glib::Threads::Mutex RegionFactory::region_map_lock;
43 Glib::Threads::Mutex RegionFactory::region_name_maps_mutex;
44 std::map<std::string, uint32_t> RegionFactory::region_name_number_map;
45 std::map<std::string, PBD::ID> RegionFactory::region_name_map;
47 
50 {
54 
55  if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
56 
57  ret = boost::shared_ptr<Region> (new AudioRegion (ar, 0));
58 
59  } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
60 
61  if (mr->session().config.get_midi_copy_is_fork()) {
62  /* What we really want to do here is what Editor::fork_region()
63  does via Session::create_midi_source_by_stealing_name(), but we
64  don't have a Track. We'll just live with the skipped number,
65  and store the ancestral name of sources so multiple clones
66  generates reasonable names that don't have too many suffixes. */
67  const std::string ancestor_name = mr->sources().front()->ancestor_name();
68  const std::string base = PBD::basename_nosuffix(ancestor_name);
69 
71  source->set_ancestor_name(mr->sources().front()->name());
72  ret = mr->clone(source);
73  } else {
74  ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
75  }
76 
77  } else {
78  fatal << _("programming error: RegionFactory::create() called with unknown Region type")
79  << endmsg;
80  abort(); /*NOTREACHED*/
81  }
82 
83  if (ret) {
84  ret->set_name (new_region_name(ret->name()));
85  ret->set_position (region->position());
86 
87  if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
89  }
90 
91  /* pure copy constructor - no property list */
92  if (announce) {
93  map_add (ret);
94  CheckNewRegion (ret);
95  }
96  }
97 
98 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
99  // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
100 #endif
101  return ret;
102 }
103 
105 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
106 {
110 
111  if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
112 
113  ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
114 
115  } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
116 
117  ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
118 
119  } else {
120  fatal << _("programming error: RegionFactory::create() called with unknown Region type")
121  << endmsg;
122  abort(); /*NOTREACHED*/
123  return boost::shared_ptr<Region>();
124  }
125 
126  if (ret) {
127  ret->apply_changes (plist);
128 
129  if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
131  }
132 
133  if (announce) {
134  map_add (ret);
135  CheckNewRegion (ret);
136  }
137  }
138 
139 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
140  // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
141 #endif
142  return ret;
143 }
144 
146 RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
147 {
151 
152  if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
153 
154  ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
155 
156  } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
157 
158  ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
159 
160  } else {
161  fatal << _("programming error: RegionFactory::create() called with unknown Region type")
162  << endmsg;
163  abort(); /*NOTREACHED*/
164  return boost::shared_ptr<Region>();
165  }
166 
167  if (ret) {
168  ret->apply_changes (plist);
169 
170  if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
172  }
173 
174  if (announce) {
175  map_add (ret);
176  CheckNewRegion (ret);
177  }
178  }
179 
180 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
181  // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
182 #endif
183  return ret;
184 }
185 
187 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
188 {
191 
192  /* used by AudioFilter when constructing a new region that is intended to have nearly
193  identical settings to an original, but using different sources.
194  */
195 
196  if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
197 
198  // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
199 
200  ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
201 
202  } else {
203  fatal << _("programming error: RegionFactory::create() called with unknown Region type")
204  << endmsg;
205  abort(); /*NOTREACHED*/
206  }
207 
208  if (ret) {
209  ret->apply_changes (plist);
210 
211  if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
213  }
214 
215  if (announce) {
216  map_add (ret);
217  CheckNewRegion (ret);
218  }
219  }
220 
221 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
222  // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
223 #endif
224  return ret;
225 }
226 
229 {
230  SourceList srcs;
231  srcs.push_back (src);
232  return create (srcs, plist, announce);
233 }
234 
236 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
237 {
241 
242  if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
243 
244  ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
245 
246  } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
247 
248  ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
249 
250  }
251 
252  if (ret) {
253  ret->apply_changes (plist);
254 
255  if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
257  }
258 
259  if (announce) {
260  map_add (ret);
261  CheckNewRegion (ret);
262  }
263  }
264 
265 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
266  // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
267 #endif
268  return ret;
269 }
270 
272 RegionFactory::create (Session& session, XMLNode& node, bool yn)
273 {
274  return session.XMLRegionFactory (node, yn);
275 }
276 
279 {
281 
282  if (srcs.empty()) {
283  return ret;
284  }
285 
286  if (srcs[0]->type() == DataType::AUDIO) {
287 
288  ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
289 
290  } else if (srcs[0]->type() == DataType::MIDI) {
291 
292  ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
293 
294  }
295 
296  if (ret) {
297  if (ret->set_state (node, Stateful::loading_state_version)) {
298  ret.reset ();
299  } else {
300  map_add (ret);
301 
302  /* Don't fiddle with position_lock_style here as the region
303  description is coming from XML.
304  */
305 
306  CheckNewRegion (ret);
307  }
308  }
309 
310 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
311  // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
312 #endif
313  return ret;
314 }
315 
316 void
318 {
319  pair<ID,boost::shared_ptr<Region> > p;
320  p.first = r->id();
321  p.second = r;
322 
323  {
324  Glib::Threads::Mutex::Lock lm (region_map_lock);
325  region_map.insert (p);
326  }
327 
328  if (!region_list_connections) {
329  region_list_connections = new ScopedConnectionList;
330  }
331 
332  r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
333  r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
334 
335  add_to_region_name_maps (r);
336 }
337 
338 void
340 {
341  boost::shared_ptr<Region> r = w.lock ();
342  if (!r) {
343  return;
344  }
345 
346  Glib::Threads::Mutex::Lock lm (region_map_lock);
347  RegionMap::iterator i = region_map.find (r->id());
348 
349  if (i != region_map.end()) {
350  remove_from_region_name_map (i->second->name ());
351  region_map.erase (i);
352  }
353 }
354 
357 {
358  RegionMap::iterator i = region_map.find (id);
359 
360  if (i == region_map.end()) {
361  return boost::shared_ptr<Region>();
362  }
363 
364  return i->second;
365 }
366 
369 {
370  for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
371  if (i->second->whole_file() && i->second->name() == name) {
372  return i->second;
373  }
374  }
375  return boost::shared_ptr<Region>();
376 }
377 
380 {
381  for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
382  if (i->second->name() == name) {
383  return i->second;
384  }
385  }
386  return boost::shared_ptr<Region>();
387 }
388 
389 void
391 {
392  if (region_list_connections) {
393  region_list_connections->drop_connections ();
394  }
395 
396  {
397  Glib::Threads::Mutex::Lock lm (region_map_lock);
398  region_map.clear ();
399  _compound_associations.clear ();
400  region_name_map.clear ();
401  }
402 }
403 
404 void
406 {
407  RegionMap copy;
408 
409  /* copy region list */
410  {
411  Glib::Threads::Mutex::Lock lm (region_map_lock);
412  copy = region_map;
413  }
414 
415  /* clear existing map */
416  clear_map ();
417 
418  /* tell everyone to drop references */
419  for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
420  i->second->drop_references ();
421  }
422 
423  /* the copy should now hold the only references, which will
424  vanish as we leave this scope, thus calling all destructors.
425  */
426 }
427 
428 uint32_t
430 {
431  Glib::Threads::Mutex::Lock lm (region_map_lock);
432  return region_map.size ();
433 }
434 
436 void
438 {
439  update_region_name_number_map (region);
440 
441  Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
442  region_name_map[region->name()] = region->id ();
443 }
444 
446 void
448 {
449  update_region_name_number_map (region);
450 
451  Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
452 
453  map<string, PBD::ID>::iterator i = region_name_map.begin();
454  while (i != region_name_map.end() && i->second != region->id ()) {
455  ++i;
456  }
457 
458  /* Erase the entry for the old name and put in a new one */
459  if (i != region_name_map.end()) {
460  region_name_map.erase (i);
461  region_name_map[region->name()] = region->id ();
462  }
463 }
464 
466 void
468 {
469  map<string, PBD::ID>::iterator i = region_name_map.find (n);
470  if (i != region_name_map.end ()) {
471  region_name_map.erase (i);
472  }
473 }
474 
476 void
478 {
479  string::size_type const last_period = region->name().find_last_of ('.');
480 
481  if (last_period != string::npos && last_period < region->name().length() - 1) {
482 
483  string const base = region->name().substr (0, last_period);
484  string const number = region->name().substr (last_period + 1);
485 
486  /* note that if there is no number, we get zero from atoi,
487  which is just fine
488  */
489 
490  Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
491  region_name_number_map[base] = atoi (number.c_str ());
492  }
493 }
494 
495 void
497 {
498  boost::shared_ptr<Region> r = w.lock ();
499  if (!r) {
500  return;
501  }
502 
503  if (what_changed.contains (Properties::name)) {
504  rename_in_region_name_maps (r);
505  }
506 }
507 
508 int
509 RegionFactory::region_name (string& result, string base, bool newlevel)
510 {
511  char buf[16];
512  string subbase;
513 
514  if (base.find("/") != string::npos) {
515  base = base.substr(base.find_last_of("/") + 1);
516  }
517 
518  if (base == "") {
519 
520  snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
521  result = "region.";
522  result += buf;
523 
524  } else {
525 
526  if (newlevel) {
527  subbase = base;
528  } else {
529  string::size_type pos;
530 
531  pos = base.find_last_of ('.');
532 
533  /* pos may be npos, but then we just use entire base */
534 
535  subbase = base.substr (0, pos);
536 
537  }
538 
539  {
540  Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
541 
542  map<string,uint32_t>::iterator x;
543 
544  result = subbase;
545 
546  if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end()) {
547  result += ".1";
548  region_name_number_map[subbase] = 1;
549  } else {
550  x->second++;
551  snprintf (buf, sizeof (buf), ".%d", x->second);
552 
553  result += buf;
554  }
555  }
556  }
557 
558  return 0;
559 }
560 
561 string
562 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
563 {
564  if (whole_source) {
565  return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
566  } else {
567  return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
568  }
569 }
570 
571 string
573 {
574  string::size_type last_period;
575  uint32_t number;
576  string::size_type len = old.length() + 64;
577  string remainder;
578  std::vector<char> buf(len);
579 
580  if ((last_period = old.find_last_of ('.')) == string::npos) {
581 
582  /* no period present - add one explicitly */
583 
584  old += '.';
585  last_period = old.length() - 1;
586  number = 0;
587 
588  } else {
589 
590  if (last_period < old.length() - 1) {
591 
592  string period_to_end = old.substr (last_period+1);
593 
594  /* extra material after the period */
595 
596  string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
597 
598  number = atoi (period_to_end);
599 
600  if (numerals_end < period_to_end.length() - 1) {
601  /* extra material after the end of the digits */
602  remainder = period_to_end.substr (numerals_end);
603  }
604 
605  } else {
606  last_period = old.length();
607  number = 0;
608  }
609  }
610 
611  while (number < (UINT_MAX-1)) {
612 
613  string sbuf;
614 
615  number++;
616 
617  snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
618  sbuf = &buf[0];
619 
620  if (region_name_map.find (sbuf) == region_name_map.end ()) {
621  break;
622  }
623  }
624 
625  if (number != (UINT_MAX-1)) {
626  return &buf[0];
627  }
628 
629  error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
630  return old;
631 }
632 
633 void
635 {
636  Glib::Threads::Mutex::Lock lm (region_map_lock);
637 
638  for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
639  if (i->second->uses_source (s)) {
640  r.insert (i->second);
641  }
642  }
643 }
644 
645 void
647 {
648  Glib::Threads::Mutex::Lock lm (region_map_lock);
649 
650  RegionMap::iterator i = region_map.begin();
651  while (i != region_map.end()) {
652 
653  RegionMap::iterator j = i;
654  ++j;
655 
656  if (i->second->uses_source (src)) {
657  remove_from_region_name_map (i->second->name ());
658  region_map.erase (i);
659  }
660 
661  i = j;
662  }
663 }
664 
665 void
667 {
668  Glib::Threads::Mutex::Lock lm (region_map_lock);
669  _compound_associations[copy] = orig;
670 }
std::map< PBD::ID, boost::shared_ptr< Region > > RegionMap
static std::map< std::string, PBD::ID > region_name_map
LIBPBD_API Transmitter fatal
static int region_name(std::string &, std::string, bool new_level=false)
int atoi(const string &s)
Definition: convert.cc:140
PBD::Signal0< void > DropReferences
Definition: destructible.h:34
PBD::Signal1< void, const PropertyChange & > PropertyChanged
Definition: stateful.h:87
static boost::shared_ptr< Region > region_by_name(const std::string &name)
Session & session() const
static void add_to_region_name_maps(boost::shared_ptr< Region >)
static void map_remove(boost::weak_ptr< Region >)
void set_ancestor_name(const std::string &name)
Definition: source.h:115
std::map< boost::shared_ptr< Region >, boost::shared_ptr< Region > > CompoundAssociations
LIBARDOUR_API PBD::PropertyDescriptor< std::string > name
boost::shared_ptr< MidiSource > create_midi_source_for_session(std::string const &)
Definition: session.cc:4036
static void remove_from_region_name_map(std::string)
static void delete_all_regions()
static void update_region_name_number_map(boost::shared_ptr< Region >)
const SourceList & sources() const
Definition: region.h:261
bool set_name(const std::string &str)
Definition: region.cc:417
static Glib::Threads::Mutex region_name_maps_mutex
boost::shared_ptr< Region > XMLRegionFactory(const XMLNode &, bool full)
static std::map< std::string, uint32_t > region_name_number_map
static void add_compound_association(boost::shared_ptr< Region >, boost::shared_ptr< Region >)
Definition: Beats.hpp:239
LIBPBD_API Transmitter error
boost::shared_ptr< MidiRegion > clone(std::string path=std::string()) const
std::ostream & endmsg(std::ostream &ostr)
Definition: transmitter.h:71
SessionConfiguration config
Definition: session.h:866
Definition: id.h:32
static Glib::Threads::Mutex region_map_lock
static uint32_t nregions()
#define _(Text)
Definition: i18n.h:11
static void map_add(boost::shared_ptr< Region >)
static std::string new_region_name(std::string)
virtual int set_state(const XMLNode &, int version)
Definition: region.cc:1234
virtual bool apply_changes(PropertyBase const &)
Definition: stateful.cc:337
void set_position(framepos_t)
Definition: region.cc:579
static CompoundAssociations _compound_associations
Definition: amp.h:29
static PBD::ScopedConnectionList * region_list_connections
const PBD::ID & id() const
Definition: stateful.h:68
static boost::shared_ptr< Region > wholefile_region_by_name(const std::string &name)
int64_t frameoffset_t
Definition: types.h:71
static PBD::Signal1< void, boost::shared_ptr< Region > > CheckNewRegion
static int loading_state_version
Definition: stateful.h:90
static void remove_regions_using_source(boost::shared_ptr< Source >)
static std::string compound_region_name(const std::string &playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
framepos_t position() const
Definition: region.h:112
LIBPBD_API Glib::ustring basename_nosuffix(Glib::ustring)
static void region_changed(PBD::PropertyChange const &, boost::weak_ptr< Region >)
const char * name
Definition: xml++.h:95
std::string name() const
Definition: debug.h:30
bool contains(PropertyDescriptor< T > p) const
void set_position_lock_style(PositionLockStyle ps)
Definition: region.cc:543
static boost::shared_ptr< Region > region_by_id(const PBD::ID &)
static void rename_in_region_name_maps(boost::shared_ptr< Region >)
static void get_regions_using_source(boost::shared_ptr< Source >, std::set< boost::shared_ptr< Region > > &)
static boost::shared_ptr< Region > create(boost::shared_ptr< const Region > other, bool announce=false)
std::vector< boost::shared_ptr< Source > > SourceList
Definition: types.h:520
static RegionMap region_map
std::string string_compose(const std::string &fmt, const T1 &o1)
Definition: compose.h:208
LIBARDOUR_API PBD::PropertyDescriptor< framecnt_t > length
Definition: region.cc:64