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