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