Remove bind to shared_ptr.
[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, boost::weak_ptr<Region> (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::weak_ptr<Region> w)
336 {
337         boost::shared_ptr<Region> r = w.lock ();
338         if (!r) {
339                 return;
340         }
341         
342         Glib::Mutex::Lock lm (region_map_lock);
343         RegionMap::iterator i = region_map.find (r->id());
344
345         if (i != region_map.end()) {
346                 region_map.erase (i);
347         }
348 }
349
350 void
351 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
352 {
353         Glib::Mutex::Lock lm (region_map_lock);
354
355         for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
356                 RegionMap::iterator tmp = i;
357                 ++tmp;
358
359                 if (r->region_list_equivalent (i->second)) {
360                         region_map.erase (i);
361                 } else if (r == i->second) {
362                         region_map.erase (i);
363                 }
364
365                 i = tmp;
366         }
367 }
368
369 boost::shared_ptr<Region>
370 RegionFactory::region_by_id (const PBD::ID& id)
371 {
372         RegionMap::iterator i = region_map.find (id);
373
374         if (i == region_map.end()) {
375                 return boost::shared_ptr<Region>();
376         }
377
378         return i->second;
379 }
380
381 boost::shared_ptr<Region>
382 RegionFactory::wholefile_region_by_name (const std::string& name)
383 {
384         for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
385                 if (i->second->whole_file() && i->second->name() == name) {
386                         return i->second;
387                 }
388         }
389         return boost::shared_ptr<Region>();
390 }
391
392 boost::shared_ptr<Region>
393 RegionFactory::region_by_name (const std::string& name)
394 {
395         for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
396                 if (i->second->name() == name) {
397                         return i->second;
398                 }
399         }
400         return boost::shared_ptr<Region>();
401 }
402
403 void
404 RegionFactory::clear_map ()
405 {
406         region_list_connections.drop_connections ();
407
408         {
409                 Glib::Mutex::Lock lm (region_map_lock);
410                 region_map.clear ();
411                 _compound_associations.clear ();
412         }
413 }
414
415 void
416 RegionFactory::delete_all_regions ()
417 {
418         RegionMap copy;
419
420         /* copy region list */
421         {
422                 Glib::Mutex::Lock lm (region_map_lock);
423                 copy = region_map;
424         }
425
426         /* clear existing map */
427         clear_map ();
428
429         /* tell everyone to drop references */
430         for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
431                 i->second->drop_references ();
432         }
433
434         /* the copy should now hold the only references, which will
435            vanish as we leave this scope, thus calling all destructors.
436         */
437 }
438
439 uint32_t
440 RegionFactory::nregions ()
441 {
442         Glib::Mutex::Lock lm (region_map_lock);
443         return region_map.size ();
444 }
445
446 void
447 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
448 {
449         string::size_type const last_period = region->name().find_last_of ('.');
450
451         if (last_period != string::npos && last_period < region->name().length() - 1) {
452
453                 string const base = region->name().substr (0, last_period);
454                 string const number = region->name().substr (last_period + 1);
455
456                 /* note that if there is no number, we get zero from atoi,
457                    which is just fine
458                 */
459
460                 Glib::Mutex::Lock lm (region_name_map_lock);
461                 region_name_map[base] = atoi (number.c_str ());
462         }
463 }
464
465 void
466 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
467 {
468         boost::shared_ptr<Region> r = w.lock ();
469         if (!r) {
470                 return;
471         }
472
473         if (what_changed.contains (Properties::name)) {
474                 update_region_name_map (r);
475         }
476 }
477
478 int
479 RegionFactory::region_name (string& result, string base, bool newlevel)
480 {
481         char buf[16];
482         string subbase;
483
484         if (base.find("/") != string::npos) {
485                 base = base.substr(base.find_last_of("/") + 1);
486         }
487
488         if (base == "") {
489
490                 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
491                 result = "region.";
492                 result += buf;
493
494         } else {
495
496                 if (newlevel) {
497                         subbase = base;
498                 } else {
499                         string::size_type pos;
500
501                         pos = base.find_last_of ('.');
502
503                         /* pos may be npos, but then we just use entire base */
504
505                         subbase = base.substr (0, pos);
506
507                 }
508
509                 {
510                         Glib::Mutex::Lock lm (region_name_map_lock);
511
512                         map<string,uint32_t>::iterator x;
513
514                         result = subbase;
515
516                         if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
517                                 result += ".1";
518                                 region_name_map[subbase] = 1;
519                         } else {
520                                 x->second++;
521                                 snprintf (buf, sizeof (buf), ".%d", x->second);
522
523                                 result += buf;
524                         }
525                 }
526         }
527
528         return 0;
529 }
530
531 string
532 RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
533 {
534         if (whole_source) {
535                 return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
536         } else {
537                 return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
538         }
539 }
540
541 string
542 RegionFactory::new_region_name (string old)
543 {
544         string::size_type last_period;
545         uint32_t number;
546         string::size_type len = old.length() + 64;
547         string remainder;
548         char buf[len];
549
550         if ((last_period = old.find_last_of ('.')) == string::npos) {
551
552                 /* no period present - add one explicitly */
553
554                 old += '.';
555                 last_period = old.length() - 1;
556                 number = 0;
557
558         } else {
559
560                 if (last_period < old.length() - 1) {
561
562                         string period_to_end = old.substr (last_period+1);
563
564                         /* extra material after the period */
565
566                         string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
567
568                         number = atoi (period_to_end);
569
570                         if (numerals_end < period_to_end.length() - 1) {
571                                 /* extra material after the end of the digits */
572                                 remainder = period_to_end.substr (numerals_end);
573                         }
574
575                 } else {
576                         last_period = old.length();
577                         number = 0;
578                 }
579         }
580
581         while (number < (UINT_MAX-1)) {
582
583                 const RegionMap& regions (RegionFactory::regions());
584                 RegionMap::const_iterator i;
585                 string sbuf;
586
587                 number++;
588
589                 snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
590                 sbuf = buf;
591
592                 for (i = regions.begin(); i != regions.end(); ++i) {
593                         if (i->second->name() == sbuf) {
594                                 break;
595                         }
596                 }
597
598                 if (i == regions.end()) {
599                         break;
600                 }
601         }
602
603         if (number != (UINT_MAX-1)) {
604                 return buf;
605         }
606
607         error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
608         return old;
609 }
610
611 void
612 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
613 {
614         Glib::Mutex::Lock lm (region_map_lock);
615
616         for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
617                 if (i->second->uses_source (s)) {
618                         r.insert (i->second);
619                 }
620         }
621 }
622
623 void
624 RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
625 {
626         Glib::Mutex::Lock lm (region_map_lock);
627
628         RegionMap::iterator i = region_map.begin();
629         while (i != region_map.end()) {
630
631                 RegionMap::iterator j = i;
632                 ++j;
633                 
634                 if (i->second->uses_source (src)) {
635                         region_map.erase (i);
636                 }
637
638                 i = j;
639         }
640 }
641
642 void
643 RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
644 {
645         Glib::Mutex::Lock lm (region_map_lock);
646         _compound_associations[copy] = orig;
647 }