2 Copyright (C) 2012 Paul Davis
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.
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.
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.
19 #include "ardour/playlist.h"
20 #include "ardour/region.h"
21 #include "ardour/audioplaylist.h"
22 #include "ardour/audioregion.h"
23 #include "ardour/session.h"
24 #include "playlist_read_test.h"
25 #include "test_globals.h"
27 CPPUNIT_TEST_SUITE_REGISTRATION (PlaylistReadTest);
30 using namespace ARDOUR;
33 PlaylistReadTest::setUp ()
35 AudioRegionTest::setUp ();
38 _buf = new Sample[_N];
39 _mbuf = new Sample[_N];
40 _gbuf = new float[_N];
42 _session->config.set_auto_xfade (false);
44 for (int i = 0; i < _N; ++i) {
50 PlaylistReadTest::tearDown ()
56 AudioRegionTest::tearDown ();
60 PlaylistReadTest::singleReadTest ()
62 /* Single-region read with fades */
64 _audio_playlist->add_region (_ar[0], 0);
65 _ar[0]->set_default_fade_in ();
66 _ar[0]->set_default_fade_out ();
67 CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_in->back()->when);
68 CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_out->back()->when);
69 _ar[0]->set_length (1024);
70 _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 256, 0);
72 for (int i = 0; i < 64; ++i) {
73 /* Note: this specific float casting is necessary so that the rounding
74 is done here the same as it is done in AudioPlaylist.
76 CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * float (i / 63.0)), _buf[i], 1e-16);
79 for (int i = 64; i < 256; ++i) {
80 CPPUNIT_ASSERT_EQUAL (i, int (_buf[i]));
85 PlaylistReadTest::overlappingReadTest ()
87 /* Overlapping read; _ar[0] and _ar[1] are both 1024 frames long, _ar[0] starts at 0,
88 _ar[1] starts at 128. We test a read from 0 to 256, which should consist
89 of the start of _ar[0], with its fade in, followed by _ar[1]'s fade in (mixed with _ar[0]
90 faded out with the inverse gain), and some more of _ar[1].
93 _audio_playlist->add_region (_ar[0], 0);
94 _ar[0]->set_default_fade_in ();
95 _ar[0]->set_default_fade_out ();
96 CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_in->back()->when);
97 CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_out->back()->when);
98 _ar[0]->set_length (1024);
100 /* Note: these are ordinary fades, not xfades */
101 CPPUNIT_ASSERT_EQUAL (false, _ar[0]->fade_in_is_xfade());
102 CPPUNIT_ASSERT_EQUAL (false, _ar[0]->fade_out_is_xfade());
104 _audio_playlist->add_region (_ar[1], 128);
105 _ar[1]->set_default_fade_in ();
106 _ar[1]->set_default_fade_out ();
108 /* Note: these are ordinary fades, not xfades */
109 CPPUNIT_ASSERT_EQUAL (false, _ar[1]->fade_in_is_xfade());
110 CPPUNIT_ASSERT_EQUAL (false, _ar[1]->fade_out_is_xfade());
112 CPPUNIT_ASSERT_EQUAL (double (64), _ar[1]->_fade_in->back()->when);
113 CPPUNIT_ASSERT_EQUAL (double (64), _ar[1]->_fade_out->back()->when);
115 _ar[1]->set_length (1024);
116 _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 256, 0);
118 /* _ar[0]'s fade in */
119 for (int i = 0; i < 64; ++i) {
120 /* Note: this specific float casting is necessary so that the rounding
121 is done here the same as it is done in AudioPlaylist; the gain factor
122 must be computed using double precision, with the result then cast
125 CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * float (i / (double) 63)), _buf[i], 1e-16);
129 for (int i = 64; i < 128; ++i) {
130 CPPUNIT_ASSERT_EQUAL (i, int (_buf[i]));
133 /* _ar[1]'s fade in with faded-out _ar[0] */
134 for (int i = 0; i < 64; ++i) {
135 /* Similar carry-on to above with float rounding */
136 float const from_ar0 = (128 + i) * float (1 - (i / (double) 63));
137 float const from_ar1 = i * float (i / (double) 63);
138 CPPUNIT_ASSERT_DOUBLES_EQUAL (from_ar0 + from_ar1, _buf[i + 128], 1e-16);
143 PlaylistReadTest::transparentReadTest ()
145 _audio_playlist->add_region (_ar[0], 0);
146 _ar[0]->set_default_fade_in ();
147 _ar[0]->set_default_fade_out ();
148 CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_in->back()->when);
149 CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_out->back()->when);
150 _ar[0]->set_length (1024);
152 _audio_playlist->add_region (_ar[1], 0);
153 _ar[1]->set_default_fade_in ();
154 _ar[1]->set_default_fade_out ();
155 CPPUNIT_ASSERT_EQUAL (double (64), _ar[1]->_fade_in->back()->when);
156 CPPUNIT_ASSERT_EQUAL (double (64), _ar[1]->_fade_out->back()->when);
157 _ar[1]->set_length (1024);
158 _ar[1]->set_opaque (false);
160 _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 1024, 0);
162 /* _ar[0] and _ar[1] fade-ins; _ar[1] is on top, but it is transparent, so
163 its fade in will not affect _ar[0]; _ar[0] will just fade in by itself,
164 and the two will be mixed.
166 for (int i = 0; i < 64; ++i) {
167 float const fade = i / (double) 63;
168 float const ar0 = i * fade;
169 float const ar1 = i * fade;
170 CPPUNIT_ASSERT_DOUBLES_EQUAL (ar0 + ar1, _buf[i], 1e-16);
173 /* _ar[0] and _ar[1] bodies, mixed */
174 for (int i = 64; i < (1024 - 64); ++i) {
175 CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * 2), _buf[i], 1e-16);
178 /* _ar[0] and _ar[1] fade-outs, mixed */
179 for (int i = (1024 - 64); i < 1024; ++i) {
180 /* Ardour fades out from 1 to VERY_SMALL_SIGNAL, which is 0.0000001,
181 so this fade out expression is a little long-winded.
183 float const fade = (((double) 1 - 0.0000001) / 63) * (1023 - i) + 0.0000001;
184 float const ar0 = i * fade;
185 float const ar1 = i * fade;
186 CPPUNIT_ASSERT_DOUBLES_EQUAL (ar0 + ar1, _buf[i], 1e-16);
190 /* A few tests just to check that nothing nasty is happening with
191 memory corruption, really (for running with valgrind).
194 PlaylistReadTest::miscReadTest ()
196 _audio_playlist->add_region (_ar[0], 0);
197 _ar[0]->set_default_fade_in ();
198 _ar[0]->set_default_fade_out ();
199 CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_in->back()->when);
200 CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_out->back()->when);
201 _ar[0]->set_length (128);
203 /* Read for just longer than the region */
204 _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 129, 0);
206 /* Read for much longer than the region */
207 _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 1024, 0);
209 /* Read one sample */
210 _audio_playlist->read (_buf, _mbuf, _gbuf, 53, 54, 0);
214 PlaylistReadTest::check_staircase (Sample* b, int offset, int N)
216 for (int i = 0; i < N; ++i) {
217 int const j = i + offset;
218 CPPUNIT_ASSERT_EQUAL (j, int (b[i]));
222 /* Check the case where we have
223 * |----------- Region A (transparent) ------------------|
224 * |---- Region B (opaque) --|
226 * The result should be a mix of the two during region B's time.
230 PlaylistReadTest::enclosedTransparentReadTest ()
232 _audio_playlist->add_region (_ar[0], 256);
233 /* These calls will result in a 64-sample fade */
234 _ar[0]->set_fade_in_length (0);
235 _ar[0]->set_fade_out_length (0);
236 _ar[0]->set_length (256);
238 _audio_playlist->add_region (_ar[1], 0);
239 /* These calls will result in a 64-sample fade */
240 _ar[1]->set_fade_in_length (0);
241 _ar[1]->set_fade_out_length (0);
242 _ar[1]->set_length (1024);
243 _ar[1]->set_opaque (false);
245 _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 1024, 0);
247 /* First 64 samples should just be _ar[1], faded in */
248 for (int i = 0; i < 64; ++i) {
249 CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * float (i / 63.0)), _buf[i], 1e-16);
252 /* Then some of _ar[1] with no fade */
253 for (int i = 64; i < 256; ++i) {
254 CPPUNIT_ASSERT_DOUBLES_EQUAL (i, _buf[i], 1e-16);
257 /* Then _ar[1] + _ar[0] (faded in) for 64 samples */
258 for (int i = 256; i < (256 + 64); ++i) {
259 CPPUNIT_ASSERT_DOUBLES_EQUAL (i + float ((i - 256) * float ((i - 256) / 63.0)), _buf[i], 1e-16);
262 /* Then _ar[1] + _ar[0] for 128 samples */
263 for (int i = (256 + 64); i < (256 + 64 + 128); ++i) {
264 CPPUNIT_ASSERT_DOUBLES_EQUAL (i + i - (256 + 64) + 64, _buf[i], 1e-16);
267 /* Then _ar[1] + _ar[0] (faded out) for 64 samples */
268 for (int i = (256 + 64 + 128); i < 512; ++i) {
269 float const ar0_without_fade = i - 256;
270 /* See above regarding VERY_SMALL_SIGNAL SNAFU */
271 float const fade = (((double) 1 - 0.0000001) / 63) * (511 - i) + 0.0000001;
272 CPPUNIT_ASSERT_DOUBLES_EQUAL (i + float (ar0_without_fade * fade), _buf[i], 1e-16);
275 /* Then just _ar[1] for a while */
276 for (int i = 512; i < (1024 - 64); ++i) {
277 CPPUNIT_ASSERT_DOUBLES_EQUAL (i, _buf[i], 1e-16);
280 /* And finally _ar[1]'s fade out */
281 for (int i = (1024 - 64); i < 1024; ++i) {
282 /* See above regarding VERY_SMALL_SIGNAL SNAFU */
283 float const fade = (((double) 1 - 0.0000001) / 63) * (1023 - i) + 0.0000001;
284 CPPUNIT_ASSERT_DOUBLES_EQUAL (i * fade, _buf[i], 1e-16);