Fix more broken whitespace.
[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 void
303 RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
304 {
305         Glib::Mutex::Lock lm (region_map_lock);
306
307         for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
308                 RegionMap::iterator tmp = i;
309                 ++tmp;
310
311                 if (r->region_list_equivalent (i->second)) {
312                         region_map.erase (i);
313                 } else if (r == i->second) {
314                         region_map.erase (i);
315                 } 
316
317                 i = tmp;
318         }
319 }
320
321 boost::shared_ptr<Region>
322 RegionFactory::region_by_id (const PBD::ID& id)
323 {
324         RegionMap::iterator i = region_map.find (id);
325
326         if (i == region_map.end()) {
327                 return boost::shared_ptr<Region>();
328         }
329
330         return i->second;
331 }
332
333 boost::shared_ptr<Region>
334 RegionFactory::wholefile_region_by_name (const std::string& name)
335 {
336         for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
337                 if (i->second->whole_file() && i->second->name() == name) {
338                         return i->second;
339                 }
340         }
341         return boost::shared_ptr<Region>();
342 }       
343
344 boost::shared_ptr<Region>
345 RegionFactory::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->name() == name) {
349                         return i->second;
350                 }
351         }
352         return boost::shared_ptr<Region>();
353 }       
354
355 void
356 RegionFactory::clear_map ()
357 {
358         region_list_connections.drop_connections ();
359
360         {
361                 Glib::Mutex::Lock lm (region_map_lock);
362                 region_map.clear ();
363         }
364
365 }
366
367 void
368 RegionFactory::delete_all_regions ()
369 {
370         RegionMap copy;
371
372         /* copy region list */
373         {
374                 Glib::Mutex::Lock lm (region_map_lock);
375                 copy = region_map;
376         }
377
378         /* clear existing map */
379         clear_map ();
380
381         /* tell everyone to drop references */
382         for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
383                 i->second->drop_references ();
384         }
385
386         /* the copy should now hold the only references, which will
387            vanish as we leave this scope, thus calling all destructors.
388         */
389 }
390         
391 uint32_t
392 RegionFactory::nregions ()
393 {
394         Glib::Mutex::Lock lm (region_map_lock);
395         return region_map.size ();
396 }
397
398 void
399 RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
400 {
401         string::size_type const last_period = region->name().find_last_of ('.');
402
403         if (last_period != string::npos && last_period < region->name().length() - 1) {
404
405                 string const base = region->name().substr (0, last_period);
406                 string const number = region->name().substr (last_period + 1);
407
408                 /* note that if there is no number, we get zero from atoi,
409                    which is just fine
410                 */
411
412                 Glib::Mutex::Lock lm (region_name_map_lock);
413                 region_name_map[base] = atoi (number.c_str ());
414         }
415 }
416
417 void
418 RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
419 {
420         boost::shared_ptr<Region> r = w.lock ();
421         if (!r) {
422                 return;
423         }
424
425         if (what_changed.contains (Properties::name)) {
426                 update_region_name_map (r);
427         }
428 }
429
430 int
431 RegionFactory::region_name (string& result, string base, bool newlevel)
432 {
433         char buf[16];
434         string subbase;
435
436         if (base.find("/") != string::npos) {
437                 base = base.substr(base.find_last_of("/") + 1);
438         }
439
440         if (base == "") {
441
442                 snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
443                 result = "region.";
444                 result += buf;
445
446         } else {
447
448                 if (newlevel) {
449                         subbase = base;
450                 } else {
451                         string::size_type pos;
452
453                         pos = base.find_last_of ('.');
454
455                         /* pos may be npos, but then we just use entire base */
456
457                         subbase = base.substr (0, pos);
458
459                 }
460
461                 {
462                         Glib::Mutex::Lock lm (region_name_map_lock);
463
464                         map<string,uint32_t>::iterator x;
465
466                         result = subbase;
467
468                         if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
469                                 result += ".1";
470                                 region_name_map[subbase] = 1;
471                         } else {
472                                 x->second++;
473                                 snprintf (buf, sizeof (buf), ".%d", x->second);
474
475                                 result += buf;
476                         }
477                 }
478         }
479
480         return 0;
481 }
482
483 string
484 RegionFactory::new_region_name (string old)
485 {
486         string::size_type last_period;
487         uint32_t number;
488         string::size_type len = old.length() + 64;
489         char buf[len];
490
491         if ((last_period = old.find_last_of ('.')) == string::npos) {
492
493                 /* no period present - add one explicitly */
494
495                 old += '.';
496                 last_period = old.length() - 1;
497                 number = 0;
498
499         } else {
500
501                 number = atoi (old.substr (last_period+1).c_str());
502
503         }
504
505         while (number < (UINT_MAX-1)) {
506                 
507                 const RegionMap& regions (RegionFactory::regions());
508                 RegionMap::const_iterator i;
509                 string sbuf;
510
511                 number++;
512
513                 snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
514                 sbuf = buf;
515
516                 for (i = regions.begin(); i != regions.end(); ++i) {
517                         if (i->second->name() == sbuf) {
518                                 break;
519                         }
520                 }
521
522                 if (i == regions.end()) {
523                         break;
524                 }
525         }
526
527         if (number != (UINT_MAX-1)) {
528                 return buf;
529         }
530
531         error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
532         return old;
533 }
534
535 void 
536 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
537 {
538         Glib::Mutex::Lock lm (region_map_lock);
539
540         for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
541                 if (i->second->uses_source (s)) {
542                         r.insert (i->second);
543                 }
544         }
545 }