summaryrefslogtreecommitdiff
path: root/libs/ardour/region_factory.cc
blob: 9709467281a6c9d2173e370e41ac9c90e6a9e40c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
 * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
 * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
 * Copyright (C) 2014-2017 Robin Gareus <robin@gareus.org>
 * Copyright (C) 2016-2017 Nick Mainsbridge <mainsbridge@gmail.com>
 * Copyright (C) 2018 Ben Loftis <ben@harrisonconsoles.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <inttypes.h>

#include "pbd/basename.h"
#include "pbd/error.h"

#include "ardour/audioregion.h"
#include "ardour/audiosource.h"
#include "ardour/boost_debug.h"
#include "ardour/midi_region.h"
#include "ardour/midi_source.h"
#include "ardour/region.h"
#include "ardour/region_factory.h"
#include "ardour/session.h"

#include "pbd/i18n.h"

using namespace ARDOUR;
using namespace PBD;
using namespace std;

PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
Glib::Threads::Mutex                          RegionFactory::region_map_lock;
RegionFactory::RegionMap                      RegionFactory::region_map;
PBD::ScopedConnectionList*                    RegionFactory::region_list_connections = 0;
Glib::Threads::Mutex                          RegionFactory::region_name_maps_mutex;
std::map<std::string, uint32_t>               RegionFactory::region_name_number_map;
std::map<std::string, PBD::ID>                RegionFactory::region_name_map;
RegionFactory::CompoundAssociations           RegionFactory::_compound_associations;

boost::shared_ptr<Region>
RegionFactory::create (boost::shared_ptr<const Region> region, bool announce, bool fork)
{
	boost::shared_ptr<Region> ret;
	boost::shared_ptr<const AudioRegion> ar;
	boost::shared_ptr<const MidiRegion> mr;

	if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {

		ret = boost::shared_ptr<Region> (new AudioRegion (ar, MusicSample (0, 0)));

	} else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {

		if (mr->session().config.get_midi_copy_is_fork() || fork) {
			/* What we really want to do here is what Editor::fork_region()
			   does via Session::create_midi_source_by_stealing_name(), but we
			   don't have a Track.  We'll just live with the skipped number,
			   and store the ancestral name of sources so multiple clones
			   generates reasonable names that don't have too many suffixes. */
			const std::string ancestor_name = mr->sources().front()->ancestor_name();
			const std::string base          = PBD::basename_nosuffix(ancestor_name);

			boost::shared_ptr<MidiSource> source = mr->session().create_midi_source_for_session(base);
			source->set_ancestor_name(mr->sources().front()->name());
			ret = mr->clone(source);
		} else {
			ret = boost::shared_ptr<Region> (new MidiRegion (mr, MusicSample (0, 0)));
		}

	} else {
		fatal << _("programming error: RegionFactory::create() called with unknown Region type")
		      << endmsg;
		abort(); /*NOTREACHED*/
	}

	if (ret) {
		ret->set_name (new_region_name(ret->name()));

		if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
			ret->set_position_lock_style (MusicTime);
		}

		/* pure copy constructor - no property list */
		if (announce) {
			map_add (ret);
			CheckNewRegion (ret); /* EMIT SIGNAL */
		}
	}

	BOOST_MARK_REGION (ret);
	return ret;
}

boost::shared_ptr<Region>
RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
{
	boost::shared_ptr<Region> ret;
	boost::shared_ptr<const AudioRegion> other_a;
	boost::shared_ptr<const MidiRegion> other_m;

	if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {

		ret = boost::shared_ptr<Region> (new AudioRegion (other_a));

	} else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {

		ret = boost::shared_ptr<Region> (new MidiRegion (other_m));

	} else {
		fatal << _("programming error: RegionFactory::create() called with unknown Region type")
		      << endmsg;
		abort(); /*NOTREACHED*/
		return boost::shared_ptr<Region>();
	}

	if (ret) {
		ret->apply_changes (plist);

		if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
 			ret->set_position_lock_style (MusicTime);
		}

		if (announce) {
			map_add (ret);
			CheckNewRegion (ret); /* EMIT SIGNAL */
		}
	}

	BOOST_MARK_REGION (ret);
	return ret;
}

boost::shared_ptr<Region>
RegionFactory::create (boost::shared_ptr<Region> region, MusicSample offset, const PropertyList& plist, bool announce)
{
	boost::shared_ptr<Region> ret;
	boost::shared_ptr<const AudioRegion> other_a;
	boost::shared_ptr<const MidiRegion> other_m;

	if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {

		ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));

	} else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {

		ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));

	} else {
		fatal << _("programming error: RegionFactory::create() called with unknown Region type")
		      << endmsg;
		abort(); /*NOTREACHED*/
		return boost::shared_ptr<Region>();
	}

	if (ret) {
		ret->apply_changes (plist);

		if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
			ret->set_position_lock_style (MusicTime);
		}

		if (announce) {
			map_add (ret);
			CheckNewRegion (ret); /* EMIT SIGNAL */
		}
	}

	BOOST_MARK_REGION (ret);
	return ret;
}

boost::shared_ptr<Region>
RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
{
	boost::shared_ptr<Region> ret;
	boost::shared_ptr<const AudioRegion> other;

	/* used by AudioFilter when constructing a new region that is intended to have nearly
	   identical settings to an original, but using different sources.
	*/

	if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {

		// XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->natural_position())

		ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));

	} else {
		fatal << _("programming error: RegionFactory::create() called with unknown Region type")
		      << endmsg;
		abort(); /*NOTREACHED*/
	}

	if (ret) {
		ret->apply_changes (plist);

		if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
			ret->set_position_lock_style (MusicTime);
		}

		if (announce) {
			map_add (ret);
			CheckNewRegion (ret); /* EMIT SIGNAL */
		}
	}

	BOOST_MARK_REGION (ret);
	return ret;
}

boost::shared_ptr<Region>
RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
{
	SourceList srcs;
	srcs.push_back (src);
	return create (srcs, plist, announce);
}

boost::shared_ptr<Region>
RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
{
	boost::shared_ptr<Region> ret;
	boost::shared_ptr<AudioSource> as;
	boost::shared_ptr<MidiSource> ms;

	if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {

		ret = boost::shared_ptr<Region> (new AudioRegion (srcs));

	} else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {

		ret = boost::shared_ptr<Region> (new MidiRegion (srcs));

	}

	if (ret) {
		ret->apply_changes (plist);

		if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) {
			ret->set_position_lock_style (MusicTime);
		}

		if (announce) {
			map_add (ret);
			CheckNewRegion (ret); /* EMIT SIGNAL */
		}
	}

	BOOST_MARK_REGION (ret);
	return ret;
}

boost::shared_ptr<Region>
RegionFactory::create (Session& session, XMLNode& node, bool yn)
{
	return session.XMLRegionFactory (node, yn);
}

boost::shared_ptr<Region>
RegionFactory::create (SourceList& srcs, const XMLNode& node)
{
	boost::shared_ptr<Region> ret;

	if (srcs.empty()) {
		return ret;
	}

	if (srcs[0]->type() == DataType::AUDIO) {

		ret = boost::shared_ptr<Region> (new AudioRegion (srcs));

	} else if (srcs[0]->type() == DataType::MIDI) {

		ret = boost::shared_ptr<Region> (new MidiRegion (srcs));

	}

	if (ret) {
		if (ret->set_state (node, Stateful::loading_state_version)) {
			ret.reset ();
		} else {
			map_add (ret);

			/* Don't fiddle with position_lock_style here as the region
			   description is coming from XML.
			*/

			CheckNewRegion (ret); /* EMIT SIGNAL */
		}
	}

	BOOST_MARK_REGION (ret);
	return ret;
}

void
RegionFactory::map_add (boost::shared_ptr<Region> r)
{
	pair<ID,boost::shared_ptr<Region> > p;
	p.first = r->id();
	p.second = r;

	{
		Glib::Threads::Mutex::Lock lm (region_map_lock);
		region_map.insert (p);
	}

	if (!region_list_connections) {
		region_list_connections = new ScopedConnectionList;
	}

	r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
	r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));

	add_to_region_name_maps (r);
}

void
RegionFactory::map_remove (boost::weak_ptr<Region> w)
{
	boost::shared_ptr<Region> r = w.lock ();
	if (!r) {
		return;
	}

	Glib::Threads::Mutex::Lock lm (region_map_lock);
	RegionMap::iterator i = region_map.find (r->id());

	if (i != region_map.end()) {
		remove_from_region_name_map (i->second->name ());
		region_map.erase (i);
	}
}

boost::shared_ptr<Region>
RegionFactory::region_by_id (const PBD::ID& id)
{
	RegionMap::iterator i = region_map.find (id);

	if (i == region_map.end()) {
		return boost::shared_ptr<Region>();
	}

	return i->second;
}

boost::shared_ptr<Region>
RegionFactory::wholefile_region_by_name (const std::string& name)
{
	for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
		if (i->second->whole_file() && i->second->name() == name) {
			return i->second;
		}
	}
	return boost::shared_ptr<Region>();
}

boost::shared_ptr<Region>
RegionFactory::region_by_name (const std::string& name)
{
	for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
		if (i->second->name() == name) {
			return i->second;
		}
	}
	return boost::shared_ptr<Region>();
}

void
RegionFactory::clear_map ()
{
	if (region_list_connections) {
		region_list_connections->drop_connections ();
	}

	{
		Glib::Threads::Mutex::Lock lm (region_map_lock);
		region_map.clear ();
		_compound_associations.clear ();
		region_name_map.clear ();
	}
}

void
RegionFactory::delete_all_regions ()
{
	RegionMap copy;

	/* copy region list */
	{
		Glib::Threads::Mutex::Lock lm (region_map_lock);
		copy = region_map;
	}

	/* clear existing map */
	clear_map ();

	/* tell everyone to drop references */
	for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
		i->second->drop_references ();
	}

	/* the copy should now hold the only references, which will
	   vanish as we leave this scope, thus calling all destructors.
	*/
}

uint32_t
RegionFactory::nregions ()
{
	Glib::Threads::Mutex::Lock lm (region_map_lock);
	return region_map.size ();
}

/** Add a region to the two region name maps */
void
RegionFactory::add_to_region_name_maps (boost::shared_ptr<Region> region)
{
	update_region_name_number_map (region);

	Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
	region_name_map[region->name()] = region->id ();
}

/** Account for a region rename in the two region name maps */
void
RegionFactory::rename_in_region_name_maps (boost::shared_ptr<Region> region)
{
	update_region_name_number_map (region);

	Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);

	map<string, PBD::ID>::iterator i = region_name_map.begin();
	while (i != region_name_map.end() && i->second != region->id ()) {
		++i;
	}

	/* Erase the entry for the old name and put in a new one */
	if (i != region_name_map.end()) {
		region_name_map.erase (i);
		region_name_map[region->name()] = region->id ();
	}
}

/** Remove a region's details from the region_name_map */
void
RegionFactory::remove_from_region_name_map (string n)
{
	map<string, PBD::ID>::iterator i = region_name_map.find (n);
	if (i != region_name_map.end ()) {
		region_name_map.erase (i);
	}
}

/** Update a region's entry in the region_name_number_map */
void
RegionFactory::update_region_name_number_map (boost::shared_ptr<Region> region)
{
	string::size_type const last_period = region->name().find_last_of ('.');

	if (last_period != string::npos && last_period < region->name().length() - 1) {

		string const base = region->name().substr (0, last_period);
		string const number = region->name().substr (last_period + 1);

		/* note that if there is no number, we get zero from atoi,
		   which is just fine
		*/

		Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
		region_name_number_map[base] = atoi (number.c_str ());
	}
}

void
RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
{
	boost::shared_ptr<Region> r = w.lock ();
	if (!r) {
		return;
	}

	if (what_changed.contains (Properties::name)) {
		rename_in_region_name_maps (r);
	}
}

int
RegionFactory::region_name (string& result, string base, bool newlevel)
{
	char buf[16];
	string subbase;

	if (base.find("/") != string::npos) {
		base = base.substr(base.find_last_of("/") + 1);
	}

	if (base == "") {

		snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
		result = "region.";
		result += buf;

	} else {

		if (newlevel) {
			subbase = base;
		} else {
			string::size_type pos;

			pos = base.find_last_of ('.');

			/* pos may be npos, but then we just use entire base */

			subbase = base.substr (0, pos);

		}

		{
			Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);

			map<string,uint32_t>::iterator x;

			result = subbase;

			if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end()) {
				result += ".1";
				region_name_number_map[subbase] = 1;
			} else {
				x->second++;
				snprintf (buf, sizeof (buf), ".%d", x->second);

				result += buf;
			}
		}
	}

	return 0;
}

string
RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
{
	if (whole_source) {
		return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
	} else {
		return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
	}
}

string
RegionFactory::new_region_name (string old)
{
	string::size_type last_period;
	uint32_t number;
	string::size_type len = old.length() + 64;
	string remainder;
	std::vector<char> buf(len);

	if ((last_period = old.find_last_of ('.')) == string::npos) {

		/* no period present - add one explicitly */

		old += '.';
		last_period = old.length() - 1;
		number = 0;

	} else {

		if (last_period < old.length() - 1) {

			string period_to_end = old.substr (last_period+1);

			/* extra material after the period */

			string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");

			number = atoi (period_to_end);

			if (numerals_end < period_to_end.length() - 1) {
				/* extra material after the end of the digits */
				remainder = period_to_end.substr (numerals_end);
			}

		} else {
			last_period = old.length();
			number = 0;
		}
	}

	while (number < (UINT_MAX-1)) {

		string sbuf;

		number++;

		snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
		sbuf = &buf[0];

		if (region_name_map.find (sbuf) == region_name_map.end ()) {
			break;
		}
	}

	if (number != (UINT_MAX-1)) {
		return &buf[0];
	}

	error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
	return old;
}

boost::shared_ptr<Region>
RegionFactory::get_whole_region_for_source (boost::shared_ptr<Source> s)
{
	Glib::Threads::Mutex::Lock lm (region_map_lock);

	for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
		if (i->second->uses_source (s) && i->second->whole_file()) {
			return (i->second);
		}
	}

	return boost::shared_ptr<Region>();
}

void
RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
{
	Glib::Threads::Mutex::Lock lm (region_map_lock);

	for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
		if (i->second->uses_source (s)) {
			r.insert (i->second);
		}
	}
}

void
RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
{
	Glib::Threads::Mutex::Lock lm (region_map_lock);
	RegionList remove_regions;
	for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) {
		if (i->second->uses_source (src)) {
			remove_regions.push_back (i->second);
		}
	}
	lm.release ();

	/* this will call RegionFactory::map_remove () */
	for (RegionList::iterator i = remove_regions.begin(); i != remove_regions.end(); ++i) {
		(*i)->drop_references ();
	}
}

void
RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
{
	Glib::Threads::Mutex::Lock lm (region_map_lock);
	_compound_associations[copy] = orig;
}