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