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