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