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