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