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