debugging legacy xfade loading, part 2
[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                         cerr << "set state on region failed\n";
288                 } else {
289                         cerr << "add region " << ret->id() << " to region map\n";
290                         map_add (ret);
291
292                         /* Don't fiddle with position_lock_style here as the region
293                            description is coming from XML.
294                         */
295                         
296                         CheckNewRegion (ret);
297                 }
298         }
299
300 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
301         // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
302 #endif
303         return ret;
304 }
305
306 void
307 RegionFactory::map_add (boost::shared_ptr<Region> r)
308 {
309         pair<ID,boost::shared_ptr<Region> > p;
310         p.first = r->id();
311         p.second = r;
312
313         {
314                 Glib::Mutex::Lock lm (region_map_lock);
315                 region_map.insert (p);
316         }
317
318         if (!region_list_connections) {
319                 region_list_connections = new ScopedConnectionList;
320         }
321
322         r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
323         r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
324
325         update_region_name_map (r);
326 }
327
328 void
329 RegionFactory::map_remove (boost::weak_ptr<Region> w)
330 {
331         boost::shared_ptr<Region> r = w.lock ();
332         if (!r) {
333                 return;
334         }
335         
336         Glib::Mutex::Lock lm (region_map_lock);
337         RegionMap::iterator i = region_map.find (r->id());
338
339         if (i != region_map.end()) {
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::Mutex::Lock lm (region_map_lock);
387                 region_map.clear ();
388                 _compound_associations.clear ();
389         }
390 }
391
392 void
393 RegionFactory::delete_all_regions ()
394 {
395         RegionMap copy;
396
397         /* copy region list */
398         {
399                 Glib::Mutex::Lock lm (region_map_lock);
400                 copy = region_map;
401         }
402
403         /* clear existing map */
404         clear_map ();
405
406         /* tell everyone to drop references */
407         for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
408                 i->second->drop_references ();
409         }
410
411         /* the copy should now hold the only references, which will
412            vanish as we leave this scope, thus calling all destructors.
413         */
414 }
415
416 uint32_t
417 RegionFactory::nregions ()
418 {
419         Glib::Mutex::Lock lm (region_map_lock);
420         return region_map.size ();
421 }
422
423 void
424 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
425 {
426         string::size_type const last_period = region->name().find_last_of ('.');
427
428         if (last_period != string::npos && last_period < region->name().length() - 1) {
429
430                 string const base = region->name().substr (0, last_period);
431                 string const number = region->name().substr (last_period + 1);
432
433                 /* note that if there is no number, we get zero from atoi,
434                    which is just fine
435                 */
436
437                 Glib::Mutex::Lock lm (region_name_map_lock);
438                 region_name_map[base] = atoi (number.c_str ());
439         }
440 }
441
442 void
443 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
444 {
445         boost::shared_ptr<Region> r = w.lock ();
446         if (!r) {
447                 return;
448         }
449
450         if (what_changed.contains (Properties::name)) {
451                 update_region_name_map (r);
452         }
453 }
454
455 int
456 RegionFactory::region_name (string& result, string base, bool newlevel)
457 {
458         char buf[16];
459         string subbase;
460
461         if (base.find("/") != string::npos) {
462                 base = base.substr(base.find_last_of("/") + 1);
463         }
464
465         if (base == "") {
466
467                 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
468                 result = "region.";
469                 result += buf;
470
471         } else {
472
473                 if (newlevel) {
474                         subbase = base;
475                 } else {
476                         string::size_type pos;
477
478                         pos = base.find_last_of ('.');
479
480                         /* pos may be npos, but then we just use entire base */
481
482                         subbase = base.substr (0, pos);
483
484                 }
485
486                 {
487                         Glib::Mutex::Lock lm (region_name_map_lock);
488
489                         map<string,uint32_t>::iterator x;
490
491                         result = subbase;
492
493                         if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
494                                 result += ".1";
495                                 region_name_map[subbase] = 1;
496                         } else {
497                                 x->second++;
498                                 snprintf (buf, sizeof (buf), ".%d", x->second);
499
500                                 result += buf;
501                         }
502                 }
503         }
504
505         return 0;
506 }
507
508 string
509 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
510 {
511         if (whole_source) {
512                 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
513         } else {
514                 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
515         }
516 }
517
518 string
519 RegionFactory::new_region_name (string old)
520 {
521         string::size_type last_period;
522         uint32_t number;
523         string::size_type len = old.length() + 64;
524         string remainder;
525         char buf[len];
526
527         if ((last_period = old.find_last_of ('.')) == string::npos) {
528
529                 /* no period present - add one explicitly */
530
531                 old += '.';
532                 last_period = old.length() - 1;
533                 number = 0;
534
535         } else {
536
537                 if (last_period < old.length() - 1) {
538
539                         string period_to_end = old.substr (last_period+1);
540
541                         /* extra material after the period */
542
543                         string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
544
545                         number = atoi (period_to_end);
546
547                         if (numerals_end < period_to_end.length() - 1) {
548                                 /* extra material after the end of the digits */
549                                 remainder = period_to_end.substr (numerals_end);
550                         }
551
552                 } else {
553                         last_period = old.length();
554                         number = 0;
555                 }
556         }
557
558         while (number < (UINT_MAX-1)) {
559
560                 const RegionMap& regions (RegionFactory::regions());
561                 RegionMap::const_iterator i;
562                 string sbuf;
563
564                 number++;
565
566                 snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
567                 sbuf = buf;
568
569                 for (i = regions.begin(); i != regions.end(); ++i) {
570                         if (i->second->name() == sbuf) {
571                                 break;
572                         }
573                 }
574
575                 if (i == regions.end()) {
576                         break;
577                 }
578         }
579
580         if (number != (UINT_MAX-1)) {
581                 return buf;
582         }
583
584         error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
585         return old;
586 }
587
588 void
589 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
590 {
591         Glib::Mutex::Lock lm (region_map_lock);
592
593         for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
594                 if (i->second->uses_source (s)) {
595                         r.insert (i->second);
596                 }
597         }
598 }
599
600 void
601 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
602 {
603         Glib::Mutex::Lock lm (region_map_lock);
604
605         RegionMap::iterator i = region_map.begin();
606         while (i != region_map.end()) {
607
608                 RegionMap::iterator j = i;
609                 ++j;
610                 
611                 if (i->second->uses_source (src)) {
612                         region_map.erase (i);
613                 }
614
615                 i = j;
616         }
617 }
618
619 void
620 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
621 {
622         Glib::Mutex::Lock lm (region_map_lock);
623         _compound_associations[copy] = orig;
624 }