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