summaryrefslogtreecommitdiff
path: root/libs/ardour/test/playlist_layering_test.cc
blob: 23b22f9d90145fe11f79b919944a2ff0f3f2578c (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
#include "pbd/compose.h"
#include "midi++/manager.h"
#include "ardour/playlist_factory.h"
#include "ardour/source_factory.h"
#include "ardour/region.h"
#include "ardour/region_factory.h"
#include "ardour/session.h"
#include "ardour/audiosource.h"
#include "ardour/audioengine.h"
#include "playlist_layering_test.h"
#include "test_receiver.h"

CPPUNIT_TEST_SUITE_REGISTRATION (PlaylistLayeringTest);

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

int const PlaylistLayeringTest::num_regions = 6;

void
PlaylistLayeringTest::setUp ()
{
	TestNeedingSession::setUp ();
	string const test_wav_path = "libs/ardour/test/test.wav";
	
	_playlist = PlaylistFactory::create (DataType::AUDIO, *_session, "test");
	_source = SourceFactory::createWritable (DataType::AUDIO, *_session, test_wav_path, "", false, 44100);

	system ("pwd");
	
	/* Must write some data to our source, otherwise regions which use it will
	   be limited in whether they can be trimmed or not.
	*/
	boost::shared_ptr<AudioSource> a = boost::dynamic_pointer_cast<AudioSource> (_source);
	Sample silence[512];
	memset (silence, 0, 512 * sizeof (Sample));
	a->write (silence, 512);

	_region = new boost::shared_ptr<Region>[num_regions];
}

void
PlaylistLayeringTest::tearDown ()
{
	_playlist.reset ();
	_source.reset ();
	for (int i = 0; i < num_regions; ++i) {
		_region[i].reset ();
	}
	
	delete[] _region;
	
	TestNeedingSession::tearDown ();
}

void
PlaylistLayeringTest::create_short_regions ()
{
	PropertyList plist;
	plist.add (Properties::start, 0);
	plist.add (Properties::length, 100);
	for (int i = 0; i < num_regions; ++i) {
		_region[i] = RegionFactory::create (_source, plist);
		_region[i]->set_name (string_compose ("%1", char (int ('A') + i)));
	}
}

void
PlaylistLayeringTest::laterHigher_relayerOnAll_Test ()
{
	_session->config.set_layer_model (LaterHigher);
	_session->config.set_relayer_on_all_edits (true);
	
	create_short_regions ();

	/* three overlapping regions */
	_playlist->add_region (_region[A], 0);
	_playlist->add_region (_region[B], 10);
	_playlist->add_region (_region[C], 20);
	/* and another non-overlapping one */
	_playlist->add_region (_region[D], 200);

	/* LaterHigher means that they should be arranged thus */
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	_region[A]->set_position (5);

	/* Region move should have no effect in LaterHigher mode */
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* C -> bottom should give C A B, not touching D */
	_region[C]->lower_to_bottom ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* C -> top should go back to A B C, not touching D */
	_region[C]->raise_to_top ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());
}

void
PlaylistLayeringTest::addHigher_relayerOnAll_Test ()
{
	_session->config.set_layer_model (AddHigher);
	_session->config.set_relayer_on_all_edits (true);
	
	create_short_regions ();

	/* three overlapping regions */
	_playlist->add_region (_region[A], 0);
	_playlist->add_region (_region[B], 10);
	_playlist->add_region (_region[C], 20);
	/* and another non-overlapping one */
	_playlist->add_region (_region[D], 200);

	/* AddHigher means that they should be arranged thus */
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	_region[A]->set_position (5);

	/* region move should have no effect in AddHigher mode */
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* C -> bottom should give C A B, not touching D */
	_region[C]->lower_to_bottom ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* C -> top should go back to A B C, not touching D */
	_region[C]->raise_to_top ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());
}

void
PlaylistLayeringTest::addOrBoundsHigher_relayerOnAll_Test ()
{
	_session->config.set_layer_model (AddOrBoundsChangeHigher);
	_session->config.set_relayer_on_all_edits (true);
	
	create_short_regions ();

	/* three overlapping regions */
	_playlist->add_region (_region[A], 0);
	_playlist->add_region (_region[B], 10);
	_playlist->add_region (_region[C], 20);
	/* and another non-overlapping one */
	_playlist->add_region (_region[D], 200);

	/* AddOrBoundsHigher means that they should be arranged thus */
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* region move should put A on top for B C A, not touching D */
	_region[A]->set_position (5);
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* C -> bottom should give C B A, not touching D */
	_region[C]->lower_to_bottom ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* C -> top should go back to B A C, not touching D */
	_region[C]->raise_to_top ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* Put C on the bottom */
	_region[C]->lower_to_bottom ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* Now move it slightly, and it should go back to the top again */
	_region[C]->set_position (21);
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* Put C back on the bottom */
	_region[C]->lower_to_bottom ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* Trim it slightly, and it should go back to the top again */
	_region[C]->trim_front (23);
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* Same with the end */
	_region[C]->lower_to_bottom ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	_region[C]->trim_end (118);
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());
}

void
PlaylistLayeringTest::addOrBoundsHigher_relayerWhenNecessary_Test ()
{
	_session->config.set_layer_model (AddOrBoundsChangeHigher);
	_session->config.set_relayer_on_all_edits (false);
	
	create_short_regions ();

	/* three overlapping regions */
	_playlist->add_region (_region[A], 0);
	_playlist->add_region (_region[B], 10);
	_playlist->add_region (_region[C], 20);
	/* and another non-overlapping one */
	_playlist->add_region (_region[D], 200);

	/* AddOrBoundsHigher means that they should be arranged thus */
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	_region[A]->set_position (5);

	/* region move should not have changed anything, since in
	   this mode we only relayer when there is a new overlap
	*/
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* C -> bottom should give C A B, not touching D */
	_region[C]->lower_to_bottom ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* C -> top should go back to A B C, not touching D */
	_region[C]->raise_to_top ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	/* Put C on the bottom */
	_region[C]->lower_to_bottom ();
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());
	
	/* Now move it slightly, and it should stay where it is */
	_region[C]->set_position (21);
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());
}

void
PlaylistLayeringTest::lastLayerOpTest ()
{
	create_short_regions ();

	_playlist->add_region (_region[A], 0);
	CPPUNIT_ASSERT_EQUAL (_playlist->layer_op_counter, _region[A]->last_layer_op (LayerOpAdd));
	uint64_t const last_add = _region[A]->last_layer_op (LayerOpAdd);
	
	_region[A]->set_position (42);
	CPPUNIT_ASSERT_EQUAL (_playlist->layer_op_counter, _region[A]->last_layer_op (LayerOpBoundsChange));
	CPPUNIT_ASSERT_EQUAL (last_add, _region[A]->last_layer_op (LayerOpAdd));

	_region[A]->trim_front (46);
	CPPUNIT_ASSERT_EQUAL (_playlist->layer_op_counter, _region[A]->last_layer_op (LayerOpBoundsChange));
	CPPUNIT_ASSERT_EQUAL (last_add, _region[A]->last_layer_op (LayerOpAdd));

	_region[A]->trim_end (102);
	CPPUNIT_ASSERT_EQUAL (_playlist->layer_op_counter, _region[A]->last_layer_op (LayerOpBoundsChange));
	CPPUNIT_ASSERT_EQUAL (last_add, _region[A]->last_layer_op (LayerOpAdd));
}

void
PlaylistLayeringTest::recursiveRelayerTest ()
{
	_session->config.set_layer_model (AddOrBoundsChangeHigher);
	_session->config.set_relayer_on_all_edits (false);
	
	create_short_regions ();

	_playlist->add_region (_region[A], 100);
	_playlist->add_region (_region[B], 125);
	_playlist->add_region (_region[C], 50);
	_playlist->add_region (_region[D], 250);

	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[C]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());

	_region[A]->set_position (200);

	CPPUNIT_ASSERT_EQUAL (layer_t (0), _region[D]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (1), _region[A]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (2), _region[B]->layer ());
	CPPUNIT_ASSERT_EQUAL (layer_t (3), _region[C]->layer ());
}