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