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