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