Clean up libardour tests a bit.
[ardour.git] / libs / ardour / test / audio_region_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/audioregion.h"
22 #include "audio_region_read_test.h"
23 #include "test_globals.h"
24
25 CPPUNIT_TEST_SUITE_REGISTRATION (AudioRegionReadTest);
26
27 using namespace std;
28 using namespace ARDOUR;
29
30 /** Check some basic reads */
31 void
32 AudioRegionReadTest::readTest ()
33 {
34         int const N = 1024;
35         
36         Sample buf[N];
37         Sample mbuf[N];
38         float gbuf[N];
39
40         int const P = 100;
41
42         /* Simple read: 256 frames from start of region, no fades */
43
44         _ar[0]->set_position (P);
45         _ar[0]->set_length (1024);
46
47         _ar[0]->read_from_sources (_ar[0]->_sources, _ar[0]->_length, buf, P, 256, 0);
48         check_staircase (buf, 0, 256);
49
50         for (int i = 0; i < N; ++i) {
51                 buf[i] = 0;
52         }
53
54         /* Offset read: 256 frames from 128 frames into the region, no fades */
55         _ar[0]->read_from_sources (_ar[0]->_sources, _ar[0]->_length, buf, P + 128, 256, 0);
56         check_staircase (buf, 128, 256);
57
58         /* Simple read with a fade-in: 256 frames from start of region, with fades */
59         _ar[0]->set_default_fade_in ();
60         CPPUNIT_ASSERT_EQUAL (double (64), _ar[0]->_fade_in->back()->when);
61
62         for (int i = 0; i < N; ++i) {
63                 buf[i] = 0;
64         }
65
66         _ar[0]->read_at (buf, mbuf, gbuf, P, 256, 0);
67         for (int i = 0; i < 64; ++i) {
68                 /* XXX: this isn't very accurate, but close enough for now; needs investigation */
69                 CPPUNIT_ASSERT_DOUBLES_EQUAL (float (i * i / 63.0), buf[i], 1e-4);
70         }
71         for (int i = 64; i < P; ++i) {
72                 CPPUNIT_ASSERT_EQUAL (i, int (buf[i]));
73         }
74         
75         /* Offset read: 256 frames from 128 frames into the region, with fades
76            (though the fade should not affect it, as it is finished before the read starts)
77         */
78
79         for (int i = 0; i < N; ++i) {
80                 buf[i] = 0;
81         }
82         
83         _ar[0]->read_at (buf, mbuf, gbuf, P + 128, 256, 0);
84         check_staircase (buf, 128, 256);
85 }
86
87 void
88 AudioRegionReadTest::check_staircase (Sample* b, int offset, int N)
89 {
90         for (int i = 0; i < N; ++i) {
91                 int const j = i + offset;
92                 CPPUNIT_ASSERT_EQUAL (j, int (b[i]));
93         }
94 }