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