Fix erroneous test.
[ardour.git] / libs / ardour / test / framewalk_to_beats_test.cc
1 #include "framewalk_to_beats_test.h"
2 #include "ardour/tempo.h"
3 #include "timecode/bbt_time.h"
4
5 CPPUNIT_TEST_SUITE_REGISTRATION (FramewalkToBeatsTest);
6
7 using namespace std;
8 using namespace ARDOUR;
9 using namespace Timecode;
10
11 void
12 FramewalkToBeatsTest::singleTempoTest ()
13 {
14         int const sampling_rate = 48000;
15         int const bpm = 120;
16
17         double const frames_per_beat = (60 / double (bpm)) * double (sampling_rate);
18         
19         TempoMap map (sampling_rate);
20         Tempo tempo (bpm);
21         Meter meter (4, 4);
22
23         map.add_meter (meter, BBT_Time (1, 1, 0));
24         map.add_tempo (tempo, BBT_Time (1, 1, 0));
25
26         /* Walk 1 beats-worth of frames from beat 3 */
27         double r = map.framewalk_to_beats (frames_per_beat * 2, frames_per_beat * 1);
28         CPPUNIT_ASSERT_EQUAL (1.0, r);
29
30         /* Walk 6 beats-worth of frames from beat 4 */
31         r = map.framewalk_to_beats (frames_per_beat * 3, frames_per_beat * 6);
32         CPPUNIT_ASSERT_EQUAL (6.0, r);
33
34         /* Walk 1.5 beats-worth of frames from beat 3 */
35         r = map.framewalk_to_beats (frames_per_beat * 2, frames_per_beat * 1.5);
36         CPPUNIT_ASSERT_EQUAL (1.5, r);
37
38         /* Walk 1.5 beats-worth of frames from beat 2.5 */
39         r = map.framewalk_to_beats (frames_per_beat * 2.5, frames_per_beat * 1.5);
40         CPPUNIT_ASSERT_EQUAL (1.5, r);
41 }
42
43 void
44 FramewalkToBeatsTest::doubleTempoTest ()
45 {
46         int const sampling_rate = 48000;
47
48         TempoMap map (sampling_rate);
49         Meter meter (4, 4);
50         map.add_meter (meter, BBT_Time (1, 1, 0));
51
52         /*
53           120bpm at bar 1, 240bpm at bar 4
54           
55           120bpm = 24e3 samples per beat
56           240bpm = 12e3 samples per beat
57         */
58         
59
60         /*
61           
62           120bpm                                                240bpm
63           0 beats                                               12 beats
64           0 frames                                              288e3 frames
65           |                 |                 |                 |                 |
66           | 1.1 1.2 1.3 1.4 | 2.1 2.2 2.3.2.4 | 3.1 3.2 3.3 3.4 | 4.1 4.2 4.3 4.4 |
67
68         */
69
70         Tempo tempoA (120);
71         map.add_tempo (tempoA, BBT_Time (1, 1, 0));
72         Tempo tempoB (240);
73         map.add_tempo (tempoB, BBT_Time (4, 1, 0));
74
75         /* Now some tests */
76
77         /* Walk 1 beat from 1|2 */
78         double r = map.framewalk_to_beats (24e3, 24e3);
79         CPPUNIT_ASSERT_EQUAL (1.0, r);
80
81         /* Walk 2 beats from 3|3 to 4|1 (over the tempo change) */
82         r = map.framewalk_to_beats (240e3, (24e3 + 24e3));
83         CPPUNIT_ASSERT_EQUAL (2.0, r);
84
85         /* Walk 2.5 beats from 3|3.5 to 4.2 (over the tempo change) */
86         r = map.framewalk_to_beats (264e3 - 12e3, (24e3 + 12e3 + 12e3));
87         CPPUNIT_ASSERT_EQUAL (2.5, r);
88
89         /* Walk 3 beats from 3|4.5 to 4|3.5 (over the tempo change) */
90         r = map.framewalk_to_beats (264e3 - 12e3, (24e3 + 12e3 + 12e3 + 6e3));
91         CPPUNIT_ASSERT_EQUAL (3.0, r);
92
93         /* Walk 3.5 beats from 3|4.5 to 4.4 (over the tempo change) */
94         r = map.framewalk_to_beats (264e3 - 12e3, (24e3 + 12e3 + 12e3 + 12e3));
95         CPPUNIT_ASSERT_EQUAL (3.5, r);
96 }
97
98 void
99 FramewalkToBeatsTest::tripleTempoTest ()
100 {
101         int const sampling_rate = 48000;
102
103         TempoMap map (sampling_rate);
104         Meter meter (4, 4);
105         map.add_meter (meter, BBT_Time (1, 1, 0));
106
107         /*
108           120bpm at bar 1, 240bpm at bar 2, 160bpm at bar 3
109           
110           120bpm = 24e3 samples per beat
111           160bpm = 18e3 samples per beat
112           240bpm = 12e3 samples per beat
113         */
114         
115
116         /*
117           
118           120bpm            240bpm            160bpm
119           0 beats           4 beats           8 beats
120           0 frames          96e3 frames       144e3 frames
121           |                 |                 |                 |                 |
122           | 1.1 1.2 1.3 1.4 | 2.1 2.2 2.3.2.4 | 3.1 3.2 3.3 3.4 | 4.1 4.2 4.3 4.4 |
123
124         */
125
126         Tempo tempoA (120);
127         map.add_tempo (tempoA, BBT_Time (1, 1, 0));
128         Tempo tempoB (240);
129         map.add_tempo (tempoB, BBT_Time (2, 1, 0));
130         Tempo tempoC (160);
131         map.add_tempo (tempoC, BBT_Time (3, 1, 0));
132
133         /* Walk from 1|3 to 4|1 */
134         double r = map.framewalk_to_beats (2 * 24e3, (2 * 24e3) + (4 * 12e3) + (4 * 18e3));
135         CPPUNIT_ASSERT_EQUAL (10.0, r);
136 }