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