Make tempo tests use the api more correctly.
[ardour.git] / libs / ardour / test / framepos_plus_beats_test.cc
1 #include "framepos_plus_beats_test.h"
2 #include "ardour/tempo.h"
3 #include "timecode/bbt_time.h"
4
5 CPPUNIT_TEST_SUITE_REGISTRATION (FrameposPlusBeatsTest);
6
7 using namespace std;
8 using namespace ARDOUR;
9 using namespace Timecode;
10
11 /* Basic tests with no tempo / meter changes */
12 void
13 FrameposPlusBeatsTest::singleTempoTest ()
14 {
15         int const sampling_rate = 48000;
16         int const bpm = 120;
17
18         double const frames_per_beat = (60 / double (bpm)) * double (sampling_rate);
19
20         TempoMap map (sampling_rate);
21         Tempo tempo (bpm);
22         Meter meter (4, 4);
23
24         map.replace_meter (map.first_meter(), meter, BBT_Time (1, 1, 0), (framepos_t) 0, AudioTime);
25         map.replace_tempo (map.first_tempo(), tempo, 0.0, 0, TempoSection::Constant, AudioTime);
26
27         /* Add 1 beat to beat 3 of the first bar */
28         framepos_t r = map.framepos_plus_beats (frames_per_beat * 2, Evoral::Beats(1));
29         CPPUNIT_ASSERT_EQUAL (framepos_t (frames_per_beat * 3), r);
30
31         /* Add 4 beats to a -ve frame of 1 beat before zero */
32         r = map.framepos_plus_beats (-frames_per_beat * 1, Evoral::Beats(4));
33         CPPUNIT_ASSERT_EQUAL (framepos_t (frames_per_beat * 3), r);
34 }
35
36 /* Test adding things that overlap a tempo change */
37 void
38 FrameposPlusBeatsTest::doubleTempoTest ()
39 {
40         int const sampling_rate = 48000;
41
42         TempoMap map (sampling_rate);
43         Meter meter (4, 4);
44         map.replace_meter (map.first_meter(), meter, BBT_Time (1, 1, 0), (framepos_t) 0, AudioTime);
45
46         /*
47           120bpm at bar 1, 240bpm at bar 4
48
49           120bpm = 24e3 samples per beat
50           240bpm = 12e3 samples per beat
51         */
52
53
54         /*
55
56           120bpm                                                240bpm
57           0 beats                                               12 beats
58           0 frames                                              288e3 frames
59           0 pulses                                              3 pulses
60           |                 |                 |                 |                 |
61           | 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 |
62
63         */
64
65         Tempo tempoA (120);
66         map.replace_tempo (map.first_tempo(), tempoA, 0.0, 0, TempoSection::Constant, AudioTime);
67         Tempo tempoB (240);
68         map.add_tempo (tempoB, 12.0 / tempoA.note_type(), 0, TempoSection::Constant, MusicTime);
69
70         /* Now some tests */
71
72         /* Add 1 beat to 1|2 */
73         framepos_t r = map.framepos_plus_beats (24e3, Evoral::Beats(1));
74         CPPUNIT_ASSERT_EQUAL (framepos_t (48e3), r);
75
76         /* Add 2 beats to 3|4 (over the tempo change) */
77         r = map.framepos_plus_beats (264e3, Evoral::Beats(2));
78         CPPUNIT_ASSERT_EQUAL (framepos_t (264e3 + 24e3 + 12e3), r);
79
80         /* Add 2.5 beats to 3|3|960 (over the tempo change) */
81         r = map.framepos_plus_beats (264e3 - 12e3, Evoral::Beats(2.5));
82         CPPUNIT_ASSERT_EQUAL (framepos_t (264e3 + 24e3 + 12e3), r);
83 }
84
85 /* Same as doubleTempoTest () except put a meter change at the same time as the
86    tempo change (which shouldn't affect anything, since we are just dealing with
87    beats)
88 */
89
90 void
91 FrameposPlusBeatsTest::doubleTempoWithMeterTest ()
92 {
93         int const sampling_rate = 48000;
94
95         TempoMap map (sampling_rate);
96         Meter meterA (4, 4);
97         map.replace_meter (map.first_meter(), meterA, BBT_Time (1, 1, 0), (framepos_t) 0, AudioTime);
98
99         /*
100           120bpm at bar 1, 240bpm at bar 4
101
102           120bpm = 24e3 samples per beat
103           240bpm = 12e3 samples per beat
104         */
105
106
107         /*
108
109           120bpm                                                240bpm
110           0 beats                                               12 beats
111           0 frames                                              288e3 frames
112           0 pulses                                              3 pulses
113           |                 |                 |                 |             |
114           | 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 |
115
116         */
117
118         Tempo tempoA (120);
119         map.replace_tempo (map.first_tempo(), tempoA, 0.0, 0, TempoSection::Constant, AudioTime);
120         Tempo tempoB (240);
121         map.add_tempo (tempoB, 12.0 / tempoA.note_type(), 0, TempoSection::Constant, MusicTime);
122         Meter meterB (3, 4);
123         map.add_meter (meterB, 12.0, BBT_Time (4, 1, 0), 0, MusicTime);
124
125         /* Now some tests */
126
127         /* Add 1 beat to 1|2 */
128         framepos_t r = map.framepos_plus_beats (24e3, Evoral::Beats(1));
129         CPPUNIT_ASSERT_EQUAL (framepos_t (48e3), r);
130
131         /* Add 2 beats to 3|4 (over the tempo change) */
132         r = map.framepos_plus_beats (264e3, Evoral::Beats(2));
133         CPPUNIT_ASSERT_EQUAL (framepos_t (264e3 + 24e3 + 12e3), r);
134
135         /* Add 2.5 beats to 3|3|960 (over the tempo change) */
136         r = map.framepos_plus_beats (264e3 - 12e3, Evoral::Beats(2.5));
137         CPPUNIT_ASSERT_EQUAL (framepos_t (264e3 + 24e3 + 12e3), r);
138 }
139
140