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