NO-OP: whitespace
[ardour.git] / libs / ardour / region_factory.cc
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/midi_region.h"
32 #include "ardour/midi_source.h"
33 #include "ardour/region.h"
34 #include "ardour/region_factory.h"
35 #include "ardour/session.h"
36
37 #include "pbd/i18n.h"
38
39 using namespace ARDOUR;
40 using namespace PBD;
41 using namespace std;
42
43 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
44 Glib::Threads::Mutex                          RegionFactory::region_map_lock;
45 RegionFactory::RegionMap                      RegionFactory::region_map;
46 PBD::ScopedConnectionList*                    RegionFactory::region_list_connections = 0;
47 Glib::Threads::Mutex                          RegionFactory::region_name_maps_mutex;
48 std::map<std::string, uint32_t>               RegionFactory::region_name_number_map;
49 std::map<std::string, PBD::ID>                RegionFactory::region_name_map;
50 RegionFactory::CompoundAssociations           RegionFactory::_compound_associations;
51
52 boost::shared_ptr<Region>
53 RegionFactory::create (boost::shared_ptr<const Region> region, bool announce, bool fork)
54 {
55         boost::shared_ptr<Region> ret;
56         boost::shared_ptr<const AudioRegion> ar;
57         boost::shared_ptr<const MidiRegion> mr;
58
59         if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
60
61                 ret = boost::shared_ptr<Region> (new AudioRegion (ar, MusicSample (0, 0)));
62
63         } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
64
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);
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                 ret->set_name (new_region_name(ret->name()));
89
90                 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
91                         ret->set_position_lock_style (MusicTime);
92                 }
93
94                 /* pure copy constructor - no property list */
95                 if (announce) {
96                         map_add (ret);
97                         CheckNewRegion (ret);
98                 }
99         }
100
101 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
102         // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
103 #endif
104         return ret;
105 }
106
107 boost::shared_ptr<Region>
108 RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
109 {
110         boost::shared_ptr<Region> ret;
111         boost::shared_ptr<const AudioRegion> other_a;
112         boost::shared_ptr<const MidiRegion> other_m;
113
114         if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
115
116                 ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
117
118         } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
119
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                 ret->apply_changes (plist);
131
132                 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
133                         ret->set_position_lock_style (MusicTime);
134                 }
135
136                 if (announce) {
137                         map_add (ret);
138                         CheckNewRegion (ret);
139                 }
140         }
141
142 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
143         // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
144 #endif
145         return ret;
146 }
147
148 boost::shared_ptr<Region>
149 RegionFactory::create (boost::shared_ptr<Region> region, MusicSample offset, const PropertyList& plist, bool announce)
150 {
151         boost::shared_ptr<Region> ret;
152         boost::shared_ptr<const AudioRegion> other_a;
153         boost::shared_ptr<const MidiRegion> other_m;
154
155         if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
156
157                 ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
158
159         } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
160
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                 ret->apply_changes (plist);
172
173                 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
174                         ret->set_position_lock_style (MusicTime);
175                 }
176
177                 if (announce) {
178                         map_add (ret);
179                         CheckNewRegion (ret);
180                 }
181         }
182
183 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
184         // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
185 #endif
186         return ret;
187 }
188
189 boost::shared_ptr<Region>
190 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
191 {
192         boost::shared_ptr<Region> ret;
193         boost::shared_ptr<const AudioRegion> other;
194
195         /* used by AudioFilter when constructing a new region that is intended to have nearly
196            identical settings to an original, but using different sources.
197         */
198
199         if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
200
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                 ret->apply_changes (plist);
213
214                 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
215                         ret->set_position_lock_style (MusicTime);
216                 }
217
218                 if (announce) {
219                         map_add (ret);
220                         CheckNewRegion (ret);
221                 }
222         }
223
224 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
225         // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
226 #endif
227         return ret;
228 }
229
230 boost::shared_ptr<Region>
231 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
232 {
233         SourceList srcs;
234         srcs.push_back (src);
235         return create (srcs, plist, announce);
236 }
237
238 boost::shared_ptr<Region>
239 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
240 {
241         boost::shared_ptr<Region> ret;
242         boost::shared_ptr<AudioSource> as;
243         boost::shared_ptr<MidiSource> ms;
244
245         if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
246
247                 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
248
249         } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
250
251                 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
252
253         }
254
255         if (ret) {
256                 ret->apply_changes (plist);
257
258                 if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
259                         ret->set_position_lock_style (MusicTime);
260                 }
261
262                 if (announce) {
263                         map_add (ret);
264                         CheckNewRegion (ret);
265                 }
266         }
267
268 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
269         // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
270 #endif
271         return ret;
272 }
273
274 boost::shared_ptr<Region>
275 RegionFactory::create (Session& session, XMLNode& node, bool yn)
276 {
277         return session.XMLRegionFactory (node, yn);
278 }
279
280 boost::shared_ptr<Region>
281 RegionFactory::create (SourceList& srcs, const XMLNode& node)
282 {
283         boost::shared_ptr<Region> ret;
284
285         if (srcs.empty()) {
286                 return ret;
287         }
288
289         if (srcs[0]->type() == DataType::AUDIO) {
290
291                 ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
292
293         } else if (srcs[0]->type() == DataType::MIDI) {
294
295                 ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
296
297         }
298
299         if (ret) {
300                 if (ret->set_state (node, Stateful::loading_state_version)) {
301                         ret.reset ();
302                 } else {
303                         map_add (ret);
304
305                         /* Don't fiddle with position_lock_style here as the region
306                            description is coming from XML.
307                         */
308
309                         CheckNewRegion (ret);
310                 }
311         }
312
313 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
314         // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
315 #endif
316         return ret;
317 }
318
319 void
320 RegionFactory::map_add (boost::shared_ptr<Region> r)
321 {
322         pair<ID,boost::shared_ptr<Region> > p;
323         p.first = r->id();
324         p.second = r;
325
326         {
327                 Glib::Threads::Mutex::Lock lm (region_map_lock);
328                 region_map.insert (p);
329         }
330
331         if (!region_list_connections) {
332                 region_list_connections = new ScopedConnectionList;
333         }
334
335         r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
336         r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
337
338         add_to_region_name_maps (r);
339 }
340
341 void
342 RegionFactory::map_remove (boost::weak_ptr<Region> w)
343 {
344         boost::shared_ptr<Region> r = w.lock ();
345         if (!r) {
346                 return;
347         }
348
349         Glib::Threads::Mutex::Lock lm (region_map_lock);
350         RegionMap::iterator i = region_map.find (r->id());
351
352         if (i != region_map.end()) {
353                 remove_from_region_name_map (i->second->name ());
354                 region_map.erase (i);
355         }
356 }
357
358 boost::shared_ptr<Region>
359 RegionFactory::region_by_id (const PBD::ID& id)
360 {
361         RegionMap::iterator i = region_map.find (id);
362
363         if (i == region_map.end()) {
364                 return boost::shared_ptr<Region>();
365         }
366
367         return i->second;
368 }
369
370 boost::shared_ptr<Region>
371 RegionFactory::wholefile_region_by_name (const std::string& name)
372 {
373         for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
374                 if (i->second->whole_file() && i->second->name() == name) {
375                         return i->second;
376                 }
377         }
378         return boost::shared_ptr<Region>();
379 }
380
381 boost::shared_ptr<Region>
382 RegionFactory::region_by_name (const std::string& name)
383 {
384         for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
385                 if (i->second->name() == name) {
386                         return i->second;
387                 }
388         }
389         return boost::shared_ptr<Region>();
390 }
391
392 void
393 RegionFactory::clear_map ()
394 {
395         if (region_list_connections) {
396                 region_list_connections->drop_connections ();
397         }
398
399         {
400                 Glib::Threads::Mutex::Lock lm (region_map_lock);
401                 region_map.clear ();
402                 _compound_associations.clear ();
403                 region_name_map.clear ();
404         }
405 }
406
407 void
408 RegionFactory::delete_all_regions ()
409 {
410         RegionMap copy;
411
412         /* copy region list */
413         {
414                 Glib::Threads::Mutex::Lock lm (region_map_lock);
415                 copy = region_map;
416         }
417
418         /* clear existing map */
419         clear_map ();
420
421         /* tell everyone to drop references */
422         for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
423                 i->second->drop_references ();
424         }
425
426         /* the copy should now hold the only references, which will
427            vanish as we leave this scope, thus calling all destructors.
428         */
429 }
430
431 uint32_t
432 RegionFactory::nregions ()
433 {
434         Glib::Threads::Mutex::Lock lm (region_map_lock);
435         return region_map.size ();
436 }
437
438 /** Add a region to the two region name maps */
439 void
440 RegionFactory::add_to_region_name_maps (boost::shared_ptr<Region> region)
441 {
442         update_region_name_number_map (region);
443
444         Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
445         region_name_map[region->name()] = region->id ();
446 }
447
448 /** Account for a region rename in the two region name maps */
449 void
450 RegionFactory::rename_in_region_name_maps (boost::shared_ptr<Region> region)
451 {
452         update_region_name_number_map (region);
453
454         Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
455
456         map<string, PBD::ID>::iterator i = region_name_map.begin();
457         while (i != region_name_map.end() && i->second != region->id ()) {
458                 ++i;
459         }
460
461         /* Erase the entry for the old name and put in a new one */
462         if (i != region_name_map.end()) {
463                 region_name_map.erase (i);
464                 region_name_map[region->name()] = region->id ();
465         }
466 }
467
468 /** Remove a region's details from the region_name_map */
469 void
470 RegionFactory::remove_from_region_name_map (string n)
471 {
472         map<string, PBD::ID>::iterator i = region_name_map.find (n);
473         if (i != region_name_map.end ()) {
474                 region_name_map.erase (i);
475         }
476 }
477
478 /** Update a region's entry in the region_name_number_map */
479 void
480 RegionFactory::update_region_name_number_map (boost::shared_ptr<Region> region)
481 {
482         string::size_type const last_period = region->name().find_last_of ('.');
483
484         if (last_period != string::npos && last_period < region->name().length() - 1) {
485
486                 string const base = region->name().substr (0, last_period);
487                 string const number = region->name().substr (last_period + 1);
488
489                 /* note that if there is no number, we get zero from atoi,
490                    which is just fine
491                 */
492
493                 Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
494                 region_name_number_map[base] = atoi (number.c_str ());
495         }
496 }
497
498 void
499 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
500 {
501         boost::shared_ptr<Region> r = w.lock ();
502         if (!r) {
503                 return;
504         }
505
506         if (what_changed.contains (Properties::name)) {
507                 rename_in_region_name_maps (r);
508         }
509 }
510
511 int
512 RegionFactory::region_name (string& result, string base, bool newlevel)
513 {
514         char buf[16];
515         string subbase;
516
517         if (base.find("/") != string::npos) {
518                 base = base.substr(base.find_last_of("/") + 1);
519         }
520
521         if (base == "") {
522
523                 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
524                 result = "region.";
525                 result += buf;
526
527         } else {
528
529                 if (newlevel) {
530                         subbase = base;
531                 } else {
532                         string::size_type pos;
533
534                         pos = base.find_last_of ('.');
535
536                         /* pos may be npos, but then we just use entire base */
537
538                         subbase = base.substr (0, pos);
539
540                 }
541
542                 {
543                         Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
544
545                         map<string,uint32_t>::iterator x;
546
547                         result = subbase;
548
549                         if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end()) {
550                                 result += ".1";
551                                 region_name_number_map[subbase] = 1;
552                         } else {
553                                 x->second++;
554                                 snprintf (buf, sizeof (buf), ".%d", x->second);
555
556                                 result += buf;
557                         }
558                 }
559         }
560
561         return 0;
562 }
563
564 string
565 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
566 {
567         if (whole_source) {
568                 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
569         } else {
570                 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
571         }
572 }
573
574 string
575 RegionFactory::new_region_name (string old)
576 {
577         string::size_type last_period;
578         uint32_t number;
579         string::size_type len = old.length() + 64;
580         string remainder;
581         std::vector<char> buf(len);
582
583         if ((last_period = old.find_last_of ('.')) == string::npos) {
584
585                 /* no period present - add one explicitly */
586
587                 old += '.';
588                 last_period = old.length() - 1;
589                 number = 0;
590
591         } else {
592
593                 if (last_period < old.length() - 1) {
594
595                         string period_to_end = old.substr (last_period+1);
596
597                         /* extra material after the period */
598
599                         string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
600
601                         number = atoi (period_to_end);
602
603                         if (numerals_end < period_to_end.length() - 1) {
604                                 /* extra material after the end of the digits */
605                                 remainder = period_to_end.substr (numerals_end);
606                         }
607
608                 } else {
609                         last_period = old.length();
610                         number = 0;
611                 }
612         }
613
614         while (number < (UINT_MAX-1)) {
615
616                 string sbuf;
617
618                 number++;
619
620                 snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
621                 sbuf = &buf[0];
622
623                 if (region_name_map.find (sbuf) == region_name_map.end ()) {
624                         break;
625                 }
626         }
627
628         if (number != (UINT_MAX-1)) {
629                 return &buf[0];
630         }
631
632         error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
633         return old;
634 }
635
636 boost::shared_ptr<Region>
637 RegionFactory::get_whole_region_for_source (boost::shared_ptr<Source> s)
638 {
639         Glib::Threads::Mutex::Lock lm (region_map_lock);
640
641         for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
642                 if (i->second->uses_source (s) && i->second->whole_file()) {
643                         return (i->second);
644                 }
645         }
646
647         return boost::shared_ptr<Region>();
648 }
649
650 void
651 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
652 {
653         Glib::Threads::Mutex::Lock lm (region_map_lock);
654
655         for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
656                 if (i->second->uses_source (s)) {
657                         r.insert (i->second);
658                 }
659         }
660 }
661
662 void
663 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
664 {
665         Glib::Threads::Mutex::Lock lm (region_map_lock);
666
667         RegionMap::iterator i = region_map.begin();
668         while (i != region_map.end()) {
669
670                 RegionMap::iterator j = i;
671                 ++j;
672
673                 if (i->second->uses_source (src)) {
674                         remove_from_region_name_map (i->second->name ());
675                         region_map.erase (i);
676                 }
677
678                 i = j;
679         }
680 }
681
682 void
683 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
684 {
685         Glib::Threads::Mutex::Lock lm (region_map_lock);
686         _compound_associations[copy] = orig;
687 }