1 /*
2 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
3 * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
6 * Copyright (C) 2016-2017 Nick Mainsbridge <mainsbridge@gmail.com>
7 * Copyright (C) 2018 Ben Loftis <ben@harrisonconsoles.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23
24 #include <inttypes.h>
25
26 #include "pbd/basename.h"
27 #include "pbd/error.h"
28
29 #include "ardour/audioregion.h"
30 #include "ardour/audiosource.h"
31 #include "ardour/boost_debug.h"
32 #include "ardour/midi_region.h"
33 #include "ardour/midi_source.h"
34 #include "ardour/region.h"
35 #include "ardour/region_factory.h"
36 #include "ardour/session.h"
37 #include "ardour/thawlist.h"
38
39 #include "pbd/i18n.h"
40
41 using namespace ARDOUR;
42 using namespace PBD;
43 using namespace std;
44
45 PBD::Signal1<void, boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
46 Glib::Threads::Mutex RegionFactory::region_map_lock;
47 RegionFactory::RegionMap RegionFactory::region_map;
48 PBD::ScopedConnectionList* RegionFactory::region_list_connections = 0;
49 Glib::Threads::Mutex RegionFactory::region_name_maps_mutex;
50 std::map<std::string, uint32_t> RegionFactory::region_name_number_map;
51 std::map<std::string, PBD::ID> RegionFactory::region_name_map;
52 RegionFactory::CompoundAssociations RegionFactory::_compound_associations;
53
54 boost::shared_ptr<Region>
create(boost::shared_ptr<const Region> region,bool announce,bool fork,ThawList * tl)55 RegionFactory::create (boost::shared_ptr<const Region> region, bool announce, bool fork, ThawList* tl)
56 {
57 boost::shared_ptr<Region> ret;
58 boost::shared_ptr<const AudioRegion> ar;
59 boost::shared_ptr<const MidiRegion> mr;
60
61 if ((ar = boost::dynamic_pointer_cast<const AudioRegion> (region)) != 0) {
62 ret = boost::shared_ptr<Region> (new AudioRegion (ar, MusicSample (0, 0)));
63
64 } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion> (region)) != 0) {
65 if (mr->session ().config.get_midi_copy_is_fork () || fork) {
66 /* What we really want to do here is what Editor::fork_region()
67 does via Session::create_midi_source_by_stealing_name(), but we
68 don't have a Track. We'll just live with the skipped number,
69 and store the ancestral name of sources so multiple clones
70 generates reasonable names that don't have too many suffixes. */
71 const std::string ancestor_name = mr->sources ().front ()->ancestor_name ();
72 const std::string base = PBD::basename_nosuffix (ancestor_name);
73
74 boost::shared_ptr<MidiSource> source = mr->session ().create_midi_source_for_session (base);
75 source->set_ancestor_name (mr->sources ().front ()->name ());
76 ret = mr->clone (source, tl);
77 } else {
78 ret = boost::shared_ptr<Region> (new MidiRegion (mr, MusicSample (0, 0)));
79 }
80
81 } else {
82 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
83 << endmsg;
84 abort (); /*NOTREACHED*/
85 }
86
87 if (ret) {
88 if (tl) {
89 tl->add (ret);
90 }
91
92 ret->set_name (new_region_name (ret->name ()));
93
94 if (ret->session ().config.get_glue_new_regions_to_bars_and_beats () && ret->position_lock_style () != MusicTime) {
95 ret->set_position_lock_style (MusicTime);
96 }
97
98 /* pure copy constructor - no property list */
99 if (announce) {
100 map_add (ret);
101 CheckNewRegion (ret); /* EMIT SIGNAL */
102 }
103 }
104
105 BOOST_MARK_REGION (ret);
106 return ret;
107 }
108
109 boost::shared_ptr<Region>
create(boost::shared_ptr<Region> region,const PropertyList & plist,bool announce,ThawList * tl)110 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce, ThawList* tl)
111 {
112 boost::shared_ptr<Region> ret;
113 boost::shared_ptr<const AudioRegion> other_a;
114 boost::shared_ptr<const MidiRegion> other_m;
115
116 if ((other_a = boost::dynamic_pointer_cast<AudioRegion> (region)) != 0) {
117 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
118
119 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion> (region)) != 0) {
120 ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
121
122 } else {
123 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
124 << endmsg;
125 abort (); /*NOTREACHED*/
126 return boost::shared_ptr<Region> ();
127 }
128
129 if (ret) {
130 if (tl) {
131 tl->add (ret);
132 }
133
134 ret->apply_changes (plist);
135
136 if (ret->session ().config.get_glue_new_regions_to_bars_and_beats () && ret->position_lock_style () != MusicTime) {
137 ret->set_position_lock_style (MusicTime);
138 }
139
140 if (announce) {
141 map_add (ret);
142 CheckNewRegion (ret); /* EMIT SIGNAL */
143 }
144 }
145
146 BOOST_MARK_REGION (ret);
147 return ret;
148 }
149
150 boost::shared_ptr<Region>
create(boost::shared_ptr<Region> region,MusicSample offset,const PropertyList & plist,bool announce,ThawList * tl)151 RegionFactory::create (boost::shared_ptr<Region> region, MusicSample offset, const PropertyList& plist, bool announce, ThawList* tl)
152 {
153 boost::shared_ptr<Region> ret;
154 boost::shared_ptr<const AudioRegion> other_a;
155 boost::shared_ptr<const MidiRegion> other_m;
156
157 if ((other_a = boost::dynamic_pointer_cast<AudioRegion> (region)) != 0) {
158 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
159
160 } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion> (region)) != 0) {
161 ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
162
163 } else {
164 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
165 << endmsg;
166 abort (); /*NOTREACHED*/
167 return boost::shared_ptr<Region> ();
168 }
169
170 if (ret) {
171 if (tl) {
172 tl->add (ret);
173 }
174 ret->apply_changes (plist);
175
176 if (ret->session ().config.get_glue_new_regions_to_bars_and_beats () && ret->position_lock_style () != MusicTime) {
177 ret->set_position_lock_style (MusicTime);
178 }
179
180 if (announce) {
181 map_add (ret);
182 CheckNewRegion (ret); /* EMIT SIGNAL */
183 }
184 }
185
186 BOOST_MARK_REGION (ret);
187 return ret;
188 }
189
190 boost::shared_ptr<Region>
create(boost::shared_ptr<Region> region,const SourceList & srcs,const PropertyList & plist,bool announce,ThawList * tl)191 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce, ThawList* tl)
192 {
193 boost::shared_ptr<Region> ret;
194 boost::shared_ptr<const AudioRegion> other;
195
196 /* used by AudioFilter when constructing a new region that is intended to have nearly
197 identical settings to an original, but using different sources.
198 */
199
200 if ((other = boost::dynamic_pointer_cast<AudioRegion> (region)) != 0) {
201 // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->natural_position())
202
203 ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
204
205 } else {
206 fatal << _("programming error: RegionFactory::create() called with unknown Region type")
207 << endmsg;
208 abort (); /*NOTREACHED*/
209 }
210
211 if (ret) {
212 if (tl) {
213 tl->add (ret);
214 }
215
216 ret->apply_changes (plist);
217
218 if (ret->session ().config.get_glue_new_regions_to_bars_and_beats () && ret->position_lock_style () != MusicTime) {
219 ret->set_position_lock_style (MusicTime);
220 }
221
222 if (announce) {
223 map_add (ret);
224 CheckNewRegion (ret); /* EMIT SIGNAL */
225 }
226 }
227
228 BOOST_MARK_REGION (ret);
229 return ret;
230 }
231
232 boost::shared_ptr<Region>
create(boost::shared_ptr<Source> src,const PropertyList & plist,bool announce,ThawList * tl)233 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce, ThawList* tl)
234 {
235 SourceList srcs;
236 srcs.push_back (src);
237 return create (srcs, plist, announce, tl);
238 }
239
240 boost::shared_ptr<Region>
create(const SourceList & srcs,const PropertyList & plist,bool announce,ThawList * tl)241 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce, ThawList* tl)
242 {
243 boost::shared_ptr<Region> ret;
244 boost::shared_ptr<AudioSource> as;
245 boost::shared_ptr<MidiSource> ms;
246
247 if ((as = boost::dynamic_pointer_cast<AudioSource> (srcs[0])) != 0) {
248 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
249
250 } else if ((ms = boost::dynamic_pointer_cast<MidiSource> (srcs[0])) != 0) {
251 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
252 }
253
254 if (ret) {
255 if (tl) {
256 tl->add (ret);
257 }
258
259 ret->apply_changes (plist);
260
261 if (ret->session ().config.get_glue_new_regions_to_bars_and_beats () && ret->position_lock_style () != MusicTime) {
262 ret->set_position_lock_style (MusicTime);
263 }
264
265 if (announce) {
266 map_add (ret);
267 CheckNewRegion (ret); /* EMIT SIGNAL */
268 }
269 }
270
271 BOOST_MARK_REGION (ret);
272 return ret;
273 }
274
275 boost::shared_ptr<Region>
create(Session & session,XMLNode & node,bool yn)276 RegionFactory::create (Session& session, XMLNode& node, bool yn)
277 {
278 return session.XMLRegionFactory (node, yn);
279 }
280
281 boost::shared_ptr<Region>
create(SourceList & srcs,const XMLNode & node)282 RegionFactory::create (SourceList& srcs, const XMLNode& node)
283 {
284 boost::shared_ptr<Region> ret;
285
286 if (srcs.empty ()) {
287 return ret;
288 }
289
290 if (srcs[0]->type () == DataType::AUDIO) {
291 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
292
293 } else if (srcs[0]->type () == DataType::MIDI) {
294 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
295 }
296
297 if (ret) {
298 if (ret->set_state (node, Stateful::loading_state_version)) {
299 ret.reset ();
300 } else {
301 map_add (ret);
302
303 /* Don't fiddle with position_lock_style here as the region
304 description is coming from XML.
305 */
306
307 CheckNewRegion (ret); /* EMIT SIGNAL */
308 }
309 }
310
311 BOOST_MARK_REGION (ret);
312 return ret;
313 }
314
315 void
map_add(boost::shared_ptr<Region> r)316 RegionFactory::map_add (boost::shared_ptr<Region> r)
317 {
318 pair<ID, boost::shared_ptr<Region> > p;
319 p.first = r->id ();
320 p.second = r;
321
322 {
323 Glib::Threads::Mutex::Lock lm (region_map_lock);
324 region_map.insert (p);
325 }
326
327 if (!region_list_connections) {
328 region_list_connections = new ScopedConnectionList;
329 }
330
331 r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
332 r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
333
334 add_to_region_name_maps (r);
335 }
336
337 void
map_remove(boost::weak_ptr<Region> w)338 RegionFactory::map_remove (boost::weak_ptr<Region> w)
339 {
340 boost::shared_ptr<Region> r = w.lock ();
341 if (!r) {
342 return;
343 }
344
345 Glib::Threads::Mutex::Lock lm (region_map_lock);
346 RegionMap::iterator i = region_map.find (r->id ());
347
348 if (i != region_map.end ()) {
349 remove_from_region_name_map (i->second->name ());
350 region_map.erase (i);
351 }
352 }
353
354 boost::shared_ptr<Region>
region_by_id(const PBD::ID & id)355 RegionFactory::region_by_id (const PBD::ID& id)
356 {
357 RegionMap::iterator i = region_map.find (id);
358
359 if (i == region_map.end ()) {
360 return boost::shared_ptr<Region> ();
361 }
362
363 return i->second;
364 }
365
366 boost::shared_ptr<Region>
wholefile_region_by_name(const std::string & name)367 RegionFactory::wholefile_region_by_name (const std::string& name)
368 {
369 for (RegionMap::iterator i = region_map.begin (); i != region_map.end (); ++i) {
370 if (i->second->whole_file () && i->second->name () == name) {
371 return i->second;
372 }
373 }
374 return boost::shared_ptr<Region> ();
375 }
376
377 boost::shared_ptr<Region>
region_by_name(const std::string & name)378 RegionFactory::region_by_name (const std::string& name)
379 {
380 for (RegionMap::iterator i = region_map.begin (); i != region_map.end (); ++i) {
381 if (i->second->name () == name) {
382 return i->second;
383 }
384 }
385 return boost::shared_ptr<Region> ();
386 }
387
388 void
clear_map()389 RegionFactory::clear_map ()
390 {
391 if (region_list_connections) {
392 region_list_connections->drop_connections ();
393 }
394
395 {
396 Glib::Threads::Mutex::Lock lm (region_map_lock);
397 region_map.clear ();
398 _compound_associations.clear ();
399 region_name_map.clear ();
400 }
401 }
402
403 void
delete_all_regions()404 RegionFactory::delete_all_regions ()
405 {
406 RegionMap copy;
407
408 /* copy region list */
409 {
410 Glib::Threads::Mutex::Lock lm (region_map_lock);
411 copy = region_map;
412 }
413
414 /* clear existing map */
415 clear_map ();
416
417 /* tell everyone to drop references */
418 for (RegionMap::iterator i = copy.begin (); i != copy.end (); ++i) {
419 i->second->drop_references ();
420 }
421
422 /* the copy should now hold the only references, which will
423 vanish as we leave this scope, thus calling all destructors.
424 */
425 }
426
427 uint32_t
nregions()428 RegionFactory::nregions ()
429 {
430 Glib::Threads::Mutex::Lock lm (region_map_lock);
431 return region_map.size ();
432 }
433
434 /** Add a region to the two region name maps */
435 void
add_to_region_name_maps(boost::shared_ptr<Region> region)436 RegionFactory::add_to_region_name_maps (boost::shared_ptr<Region> region)
437 {
438 update_region_name_number_map (region);
439
440 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
441 region_name_map[region->name ()] = region->id ();
442 }
443
444 /** Account for a region rename in the two region name maps */
445 void
rename_in_region_name_maps(boost::shared_ptr<Region> region)446 RegionFactory::rename_in_region_name_maps (boost::shared_ptr<Region> region)
447 {
448 update_region_name_number_map (region);
449
450 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
451
452 map<string, PBD::ID>::iterator i = region_name_map.begin ();
453 while (i != region_name_map.end () && i->second != region->id ()) {
454 ++i;
455 }
456
457 /* Erase the entry for the old name and put in a new one */
458 if (i != region_name_map.end ()) {
459 region_name_map.erase (i);
460 region_name_map[region->name ()] = region->id ();
461 }
462 }
463
464 /** Remove a region's details from the region_name_map */
465 void
remove_from_region_name_map(string n)466 RegionFactory::remove_from_region_name_map (string n)
467 {
468 map<string, PBD::ID>::iterator i = region_name_map.find (n);
469 if (i != region_name_map.end ()) {
470 region_name_map.erase (i);
471 }
472 }
473
474 /** Update a region's entry in the region_name_number_map */
475 void
update_region_name_number_map(boost::shared_ptr<Region> region)476 RegionFactory::update_region_name_number_map (boost::shared_ptr<Region> region)
477 {
478 string::size_type const last_period = region->name ().find_last_of ('.');
479
480 if (last_period != string::npos && last_period < region->name ().length () - 1) {
481 string const base = region->name ().substr (0, last_period);
482 string const number = region->name ().substr (last_period + 1);
483
484 /* note that if there is no number, we get zero from atoi,
485 which is just fine
486 */
487
488 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
489 region_name_number_map[base] = atoi (number.c_str ());
490 }
491 }
492
493 void
region_changed(PropertyChange const & what_changed,boost::weak_ptr<Region> w)494 RegionFactory::region_changed (PropertyChange const& what_changed, boost::weak_ptr<Region> w)
495 {
496 boost::shared_ptr<Region> r = w.lock ();
497 if (!r) {
498 return;
499 }
500
501 if (what_changed.contains (Properties::name)) {
502 rename_in_region_name_maps (r);
503 }
504 }
505
506 int
region_name(string & result,string base,bool newlevel)507 RegionFactory::region_name (string& result, string base, bool newlevel)
508 {
509 char buf[16];
510 string subbase;
511
512 if (base.find ("/") != string::npos) {
513 base = base.substr (base.find_last_of ("/") + 1);
514 }
515
516 if (base == "") {
517 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions () + 1);
518 result = "region.";
519 result += buf;
520
521 } else {
522 if (newlevel) {
523 subbase = base;
524 } else {
525 string::size_type pos;
526
527 pos = base.find_last_of ('.');
528
529 /* pos may be npos, but then we just use entire base */
530
531 subbase = base.substr (0, pos);
532 }
533
534 {
535 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
536
537 map<string, uint32_t>::iterator x;
538
539 result = subbase;
540
541 if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end ()) {
542 result += ".1";
543 region_name_number_map[subbase] = 1;
544 } else {
545 x->second++;
546 snprintf (buf, sizeof (buf), ".%d", x->second);
547
548 result += buf;
549 }
550 }
551 }
552
553 return 0;
554 }
555
556 string
compound_region_name(const string & playlist,uint32_t compound_ops,uint32_t depth,bool whole_source)557 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
558 {
559 if (whole_source) {
560 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops + 1, depth + 1);
561 } else {
562 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops + 1, depth + 1);
563 }
564 }
565
566 string
new_region_name(string old)567 RegionFactory::new_region_name (string old)
568 {
569 string::size_type last_period;
570 uint32_t number;
571 string::size_type len = old.length () + 64;
572 string remainder;
573 std::vector<char> buf (len);
574
575 if ((last_period = old.find_last_of ('.')) == string::npos) {
576 /* no period present - add one explicitly */
577
578 old += '.';
579 last_period = old.length () - 1;
580 number = 0;
581
582 } else {
583 if (last_period < old.length () - 1) {
584 string period_to_end = old.substr (last_period + 1);
585
586 /* extra material after the period */
587
588 string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
589
590 number = atoi (period_to_end);
591
592 if (numerals_end < period_to_end.length () - 1) {
593 /* extra material after the end of the digits */
594 remainder = period_to_end.substr (numerals_end);
595 }
596
597 } else {
598 last_period = old.length ();
599 number = 0;
600 }
601 }
602
603 while (number < (UINT_MAX - 1)) {
604 string sbuf;
605
606 number++;
607
608 snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str (), number, remainder.c_str ());
609 sbuf = &buf[0];
610
611 if (region_name_map.find (sbuf) == region_name_map.end ()) {
612 break;
613 }
614 }
615
616 if (number != (UINT_MAX - 1)) {
617 return &buf[0];
618 }
619
620 error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
621 return old;
622 }
623
624 boost::shared_ptr<Region>
get_whole_region_for_source(boost::shared_ptr<Source> s)625 RegionFactory::get_whole_region_for_source (boost::shared_ptr<Source> s)
626 {
627 Glib::Threads::Mutex::Lock lm (region_map_lock);
628
629 for (RegionMap::const_iterator i = region_map.begin (); i != region_map.end (); ++i) {
630 if (i->second->uses_source (s) && i->second->whole_file ()) {
631 return (i->second);
632 }
633 }
634
635 return boost::shared_ptr<Region> ();
636 }
637
638 void
get_regions_using_source(boost::shared_ptr<Source> s,std::set<boost::shared_ptr<Region>> & r)639 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
640 {
641 Glib::Threads::Mutex::Lock lm (region_map_lock);
642
643 for (RegionMap::const_iterator i = region_map.begin (); i != region_map.end (); ++i) {
644 if (i->second->uses_source (s)) {
645 r.insert (i->second);
646 }
647 }
648 }
649
650 void
remove_regions_using_source(boost::shared_ptr<Source> src)651 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
652 {
653 Glib::Threads::Mutex::Lock lm (region_map_lock);
654 RegionList remove_regions;
655 for (RegionMap::const_iterator i = region_map.begin (); i != region_map.end (); ++i) {
656 if (i->second->uses_source (src)) {
657 remove_regions.push_back (i->second);
658 }
659 }
660 lm.release ();
661
662 /* this will call RegionFactory::map_remove () */
663 for (RegionList::iterator i = remove_regions.begin (); i != remove_regions.end (); ++i) {
664 (*i)->drop_references ();
665 }
666 }
667
668 void
add_compound_association(boost::shared_ptr<Region> orig,boost::shared_ptr<Region> copy)669 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
670 {
671 Glib::Threads::Mutex::Lock lm (region_map_lock);
672 _compound_associations[copy] = orig;
673 }
674