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