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