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