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