a1b4579e8a42cc21a09440e016dd378e0b5dd725
[ardour.git] / libs / ardour / test / playlist_read_test.cc
1 /*
2     Copyright (C) 2012 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 #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"
26
27 CPPUNIT_TEST_SUITE_REGISTRATION (PlaylistReadTest);
28
29 using namespace std;
30 using namespace ARDOUR;
31
32 void
33 PlaylistReadTest::setUp ()
34 {
35         AudioRegionTest::setUp ();
36
37         _N = 1024;
38         _buf = new Sample[_N];
39         _mbuf = new Sample[_N];
40         _gbuf = new float[_N];
41
42         _session->config.set_auto_xfade (false);
43
44         for (int i = 0; i < _N; ++i) {
45                 _buf[i] = 0;
46         }
47 }
48
49 void
50 PlaylistReadTest::tearDown ()
51 {
52         delete[] _buf;
53         delete[] _mbuf;
54         delete[] _gbuf;
55
56         AudioRegionTest::tearDown ();
57 }
58
59 void
60 PlaylistReadTest::singleReadTest ()
61 {
62         /* Single-region read with fades */
63
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);
71         
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.
75                 */
76                 CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * float (i / 63.0)), _buf[i], 1e-16);
77         }
78         
79         for (int i = 64; i < 256; ++i) {
80                 CPPUNIT_ASSERT_EQUAL (i, int (_buf[i]));
81         }
82 }
83
84 void
85 PlaylistReadTest::overlappingReadTest ()
86 {
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].
91         */
92
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);
99
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());
103         
104         _audio_playlist->add_region (_ar[1], 128);
105         _ar[1]->set_default_fade_in ();
106         _ar[1]->set_default_fade_out ();
107
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());
111         
112         CPPUNIT_ASSERT_EQUAL (double (64), _ar[1]->_fade_in->back()->when);
113         CPPUNIT_ASSERT_EQUAL (double (64), _ar[1]->_fade_out->back()->when);
114         
115         _ar[1]->set_length (1024);
116         _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 256, 0);
117
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
123                    to float.
124                 */
125                 CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * float (i / (double) 63)), _buf[i], 1e-16);
126         }
127
128         /* bit of _ar[0] */
129         for (int i = 64; i < 128; ++i) {
130                 CPPUNIT_ASSERT_EQUAL (i, int (_buf[i]));
131         }
132
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);
139         }
140 }
141
142 void
143 PlaylistReadTest::transparentReadTest ()
144 {
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);
151         
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);
159
160         _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 1024, 0);
161
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.
165         */
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);
171         }
172
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);
176         }
177
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.
182                 */
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);
187         }
188 }
189
190 /* A few tests just to check that nothing nasty is happening with
191    memory corruption, really (for running with valgrind).
192 */
193 void
194 PlaylistReadTest::miscReadTest ()
195 {
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);
202
203         /* Read for just longer than the region */
204         _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 129, 0);
205
206         /* Read for much longer than the region */
207         _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 1024, 0);
208
209         /* Read one sample */
210         _audio_playlist->read (_buf, _mbuf, _gbuf, 53, 54, 0);
211 }
212
213 void
214 PlaylistReadTest::check_staircase (Sample* b, int offset, int N)
215 {
216         for (int i = 0; i < N; ++i) {
217                 int const j = i + offset;
218                 CPPUNIT_ASSERT_EQUAL (j, int (b[i]));
219         }
220 }
221
222 /* Check the case where we have
223  *    |----------- Region A (transparent) ------------------|
224  *                     |---- Region B (opaque) --|
225  *
226  * The result should be a mix of the two during region B's time.
227  */
228
229 void
230 PlaylistReadTest::enclosedTransparentReadTest ()
231 {
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);
237         
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);
244
245         _audio_playlist->read (_buf, _mbuf, _gbuf, 0, 1024, 0);
246
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);
250         }
251
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);
255         }
256
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);
260         }
261
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);
265         }
266         
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);
273         }
274
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);
278         }
279
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);
285
286         }
287 }