summaryrefslogtreecommitdiff
path: root/libs/ardour/test/playlist_read_test.cc
blob: f3003dfec93e5264bf7d5fe234b8006b8b79dd55 (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
/*
    Copyright (C) 2012 Paul Davis

    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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "ardour/playlist.h"
#include "ardour/region.h"
#include "ardour/audioplaylist.h"
#include "ardour/audioregion.h"
#include "ardour/session.h"
#include "playlist_read_test.h"

CPPUNIT_TEST_SUITE_REGISTRATION (PlaylistReadTest);

using namespace std;
using namespace ARDOUR;

void
PlaylistReadTest::setUp ()
{
	AudioRegionTest::setUp ();

	_N = 1024;
	_buf = new Sample[_N];
	_mbuf = new Sample[_N];
	_gbuf = new float[_N];

	//_session->config.set_auto_xfade (false);

	for (int i = 0; i < _N; ++i) {
		_buf[i] = 0;
	}
}

void
PlaylistReadTest::tearDown ()
{
	delete[] _buf;
	delete[] _mbuf;
	delete[] _gbuf;

	AudioRegionTest::tearDown ();
}

void
PlaylistReadTest::singleReadTest ()
{
	/* Single-region read with fades */

	_audio_playlist->add_region (_ar[0], 0);
	_ar[0]->set_default_fade_in ();
	_ar[0]->set_default_fade_out ();
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_in->back()->when);
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_out->back()->when);
	_ar[0]->set_length (1024);
	_audio_playlist->read (_buf, _mbuf, _gbuf, 0, 256, 0);

	for (int i = 0; i < 64; ++i) {
		/* Note: this specific float casting is necessary so that the rounding
		   is done here the same as it is done in AudioPlaylist.
		*/
		CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * float (i / 63.0)), _buf[i], 1e-16);
	}

	for (int i = 64; i < 256; ++i) {
		CPPUNIT_ASSERT_EQUAL (i, int (_buf[i]));
	}
}

void
PlaylistReadTest::overlappingReadTest ()
{
	/* Overlapping read; _ar[0] and _ar[1] are both 1024 samples long, _ar[0] starts at 0,
	   _ar[1] starts at 128.  We test a read from 0 to 256, which should consist
	   of the start of _ar[0], with its fade in, followed by _ar[1]'s fade in (mixed with _ar[0]
	   faded out with the inverse gain), and some more of _ar[1].
	*/

	_audio_playlist->add_region (_ar[0], 0);
	_ar[0]->set_default_fade_in ();
	_ar[0]->set_default_fade_out ();
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_in->back()->when);
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_out->back()->when);
	_ar[0]->set_length (1024);

#if 0
	/* Note: these are ordinary fades, not xfades */
	CPPUNIT_ASSERT_EQUAL (false, _ar[0]->fade_in_is_xfade());
	CPPUNIT_ASSERT_EQUAL (false, _ar[0]->fade_out_is_xfade());
#endif

	_audio_playlist->add_region (_ar[1], 128);
	_ar[1]->set_default_fade_in ();
	_ar[1]->set_default_fade_out ();

#if 0
	/* Note: these are ordinary fades, not xfades */
	CPPUNIT_ASSERT_EQUAL (false, _ar[1]->fade_in_is_xfade());
	CPPUNIT_ASSERT_EQUAL (false, _ar[1]->fade_out_is_xfade());
#endif

	CPPUNIT_ASSERT_EQUAL (double (64), _ar[1]->_fade_in->back()->when);
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[1]->_fade_out->back()->when);

	_ar[1]->set_length (1024);
	_audio_playlist->read (_buf, _mbuf, _gbuf, 0, 256, 0);

	/* _ar[0]'s fade in */
	for (int i = 0; i < 64; ++i) {
		/* Note: this specific float casting is necessary so that the rounding
		   is done here the same as it is done in AudioPlaylist; the gain factor
		   must be computed using double precision, with the result then cast
		   to float.
		*/
		CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * float (i / (double) 63)), _buf[i], 1e-16);
	}

	/* bit of _ar[0] */
	for (int i = 64; i < 128; ++i) {
		CPPUNIT_ASSERT_EQUAL (i, int (_buf[i]));
	}

	/* _ar[1]'s fade in with faded-out _ar[0] */
	for (int i = 0; i < 64; ++i) {
		/* Similar carry-on to above with float rounding */
		float const from_ar0 = (128 + i) * float (1 - (i / (double) 63));
		float const from_ar1 = i * float (i / (double) 63);
		CPPUNIT_ASSERT_DOUBLES_EQUAL (from_ar0 + from_ar1, _buf[i + 128], 1e-16);
	}
}

void
PlaylistReadTest::transparentReadTest ()
{
	_audio_playlist->add_region (_ar[0], 0);
	_ar[0]->set_default_fade_in ();
	_ar[0]->set_default_fade_out ();
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_in->back()->when);
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_out->back()->when);
	_ar[0]->set_length (1024);

	_audio_playlist->add_region (_ar[1], 0);
	_ar[1]->set_default_fade_in ();
	_ar[1]->set_default_fade_out ();
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[1]->_fade_in->back()->when);
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[1]->_fade_out->back()->when);
	_ar[1]->set_length (1024);
	_ar[1]->set_opaque (false);

	_audio_playlist->read (_buf, _mbuf, _gbuf, 0, 1024, 0);

	/* _ar[0] and _ar[1] fade-ins; _ar[1] is on top, but it is transparent, so
	   its fade in will not affect _ar[0]; _ar[0] will just fade in by itself,
	   and the two will be mixed.
	*/
	for (int i = 0; i < 64; ++i) {
		float const fade = i / (double) 63;
		float const ar0 = i * fade;
		float const ar1 = i * fade;
		CPPUNIT_ASSERT_DOUBLES_EQUAL (ar0 + ar1, _buf[i], 1e-16);
	}

	/* _ar[0] and _ar[1] bodies, mixed */
	for (int i = 64; i < (1024 - 64); ++i) {
		CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * 2), _buf[i], 1e-16);
	}

	/* _ar[0] and _ar[1] fade-outs, mixed */
	for (int i = (1024 - 64); i < 1024; ++i) {
		/* Ardour fades out from 1 to VERY_SMALL_SIGNAL, which is 0.0000001,
		   so this fade out expression is a little long-winded.
		*/
		float const fade = (((double) 1 - 0.0000001) / 63) * (1023 - i) + 0.0000001;
		float const ar0 = i * fade;
		float const ar1 = i * fade;
		CPPUNIT_ASSERT_DOUBLES_EQUAL (ar0 + ar1, _buf[i], 1e-16);
	}
}

/* A few tests just to check that nothing nasty is happening with
   memory corruption, really (for running with valgrind).
*/
void
PlaylistReadTest::miscReadTest ()
{
	_audio_playlist->add_region (_ar[0], 0);
	_ar[0]->set_default_fade_in ();
	_ar[0]->set_default_fade_out ();
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_in->back()->when);
	CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_out->back()->when);
	_ar[0]->set_length (128);

	/* Read for just longer than the region */
	_audio_playlist->read (_buf, _mbuf, _gbuf, 0, 129, 0);

	/* Read for much longer than the region */
	_audio_playlist->read (_buf, _mbuf, _gbuf, 0, 1024, 0);

	/* Read one sample */
	_audio_playlist->read (_buf, _mbuf, _gbuf, 53, 54, 0);
}

void
PlaylistReadTest::check_staircase (Sample* b, int offset, int N)
{
	for (int i = 0; i < N; ++i) {
		int const j = i + offset;
		CPPUNIT_ASSERT_EQUAL (j, int (b[i]));
	}
}

/* Check the case where we have
 *    |----------- Region A (transparent) ------------------|
 *                     |---- Region B (opaque) --|
 *
 * The result should be a mix of the two during region B's time.
 */

void
PlaylistReadTest::enclosedTransparentReadTest ()
{
	_audio_playlist->add_region (_ar[0], 256);
	/* These calls will result in a 64-sample fade */
	_ar[0]->set_fade_in_length (0);
	_ar[0]->set_fade_out_length (0);
	_ar[0]->set_length (256);

	_audio_playlist->add_region (_ar[1], 0);
	/* These calls will result in a 64-sample fade */
	_ar[1]->set_fade_in_length (0);
	_ar[1]->set_fade_out_length (0);
	_ar[1]->set_length (1024);
	_ar[1]->set_opaque (false);

	_audio_playlist->read (_buf, _mbuf, _gbuf, 0, 1024, 0);

	/* First 64 samples should just be _ar[1], faded in */
	for (int i = 0; i < 64; ++i) {
		CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * float (i / 63.0)), _buf[i], 1e-16);
	}

	/* Then some of _ar[1] with no fade */
	for (int i = 64; i < 256; ++i) {
		CPPUNIT_ASSERT_DOUBLES_EQUAL (i, _buf[i], 1e-16);
	}

	/* Then _ar[1] + _ar[0] (faded in) for 64 samples */
	for (int i = 256; i < (256 + 64); ++i) {
		CPPUNIT_ASSERT_DOUBLES_EQUAL (i + float ((i - 256) * float ((i - 256) / 63.0)), _buf[i], 1e-16);
	}

	/* Then _ar[1] + _ar[0] for 128 samples */
	for (int i = (256 + 64); i < (256 + 64 + 128); ++i) {
		CPPUNIT_ASSERT_DOUBLES_EQUAL (i + i - (256 + 64) + 64, _buf[i], 1e-16);
	}

	/* Then _ar[1] + _ar[0] (faded out) for 64 samples */
	for (int i = (256 + 64 + 128); i < 512; ++i) {
		float const ar0_without_fade = i - 256;
		/* See above regarding VERY_SMALL_SIGNAL SNAFU */
		float const fade = (((double) 1 - 0.0000001) / 63) * (511 - i) + 0.0000001;
		CPPUNIT_ASSERT_DOUBLES_EQUAL (i + float (ar0_without_fade * fade), _buf[i], 1e-16);
	}

	/* Then just _ar[1] for a while */
	for (int i = 512; i < (1024 - 64); ++i) {
		CPPUNIT_ASSERT_DOUBLES_EQUAL (i, _buf[i], 1e-16);
	}

	/* And finally _ar[1]'s fade out */
	for (int i = (1024 - 64); i < 1024; ++i) {
		/* See above regarding VERY_SMALL_SIGNAL SNAFU */
		float const fade = (((double) 1 - 0.0000001) / 63) * (1023 - i) + 0.0000001;
		CPPUNIT_ASSERT_DOUBLES_EQUAL (i * fade, _buf[i], 1e-16);

	}
}