Keep track of MIDI region's start positions in beats, to
[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
11 /* Basic tests with no tempo / meter changes */
12 void
13 FrameposMinusBeatsTest::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.add_meter (meter, BBT_Time (1, 1, 0));
25         map.add_tempo (tempo, BBT_Time (1, 1, 0));
26
27         /* Subtract 1 beat from beat 3 of the first bar */
28         framepos_t r = map.framepos_minus_beats (frames_per_beat * 2, 1);
29         CPPUNIT_ASSERT_EQUAL (r, framepos_t (frames_per_beat * 1));
30 }
31
32 /* Test adding things that overlap a tempo change */
33 void
34 FrameposMinusBeatsTest::doubleTempoTest ()
35 {
36         int const sampling_rate = 48000;
37
38         TempoMap map (sampling_rate);
39         Meter meter (4, 4);
40         map.add_meter (meter, BBT_Time (1, 1, 0));
41
42         /*
43           120bpm at bar 1, 240bpm at bar 4
44           
45           120bpm = 24e3 samples per beat
46           240bpm = 12e3 samples per beat
47         */
48         
49
50         /*
51           
52           120bpm                                                240bpm
53           0 beats                                               12 beats
54           0 frames                                              288e3 frames
55           |                 |                 |                 |                 |
56           | 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 |
57
58         */
59
60         Tempo tempoA (120);
61         map.add_tempo (tempoA, BBT_Time (1, 1, 0));
62         Tempo tempoB (240);
63         map.add_tempo (tempoB, BBT_Time (4, 1, 0));
64
65         /* Now some tests */
66
67         /* Subtract 1 beat from 1|2 */
68         framepos_t r = map.framepos_minus_beats (24e3, 1);
69         CPPUNIT_ASSERT_EQUAL (r, framepos_t (0));
70
71         /* Subtract 2 beats from 4|2 (over the tempo change) */
72         r = map.framepos_minus_beats (288e3 + 12e3, 2);
73         CPPUNIT_ASSERT_EQUAL (r, framepos_t (288e3 - 24e3));
74
75         /* Subtract 2.5 beats from 4|2 (over the tempo change) */
76         r = map.framepos_minus_beats (288e3 + 12e3, 2.5);
77         CPPUNIT_ASSERT_EQUAL (r, framepos_t (288e3 - 24e3 - 12e3));
78 }
79
80 /* Same as doubleTempoTest () except put a meter change at the same time as the
81    tempo change (which shouldn't affect anything, since we are just dealing with
82    beats)
83 */
84    
85 void
86 FrameposMinusBeatsTest::doubleTempoWithMeterTest ()
87 {
88         int const sampling_rate = 48000;
89
90         TempoMap map (sampling_rate);
91         Meter meterA (4, 4);
92         map.add_meter (meterA, BBT_Time (1, 1, 0));
93
94         /*
95           120bpm at bar 1, 240bpm at bar 4
96           
97           120bpm = 24e3 samples per beat
98           240bpm = 12e3 samples per beat
99         */
100         
101
102         /*
103           
104           120bpm                                                240bpm
105           0 beats                                               12 beats
106           0 frames                                              288e3 frames
107           |                 |                 |                 |             |
108           | 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 |
109
110         */
111
112         Tempo tempoA (120);
113         map.add_tempo (tempoA, BBT_Time (1, 1, 0));
114         Tempo tempoB (240);
115         map.add_tempo (tempoB, BBT_Time (4, 1, 0));
116         Meter meterB (3, 4);
117         map.add_meter (meterB, BBT_Time (4, 1, 0));
118
119         /* Now some tests */
120
121         /* Subtract 1 beat from 1|2 */
122         framepos_t r = map.framepos_minus_beats (24e3, 1);
123         CPPUNIT_ASSERT_EQUAL (r, framepos_t (0));
124
125         /* Subtract 2 beats from 4|2 (over the tempo change) */
126         r = map.framepos_minus_beats (288e3 + 12e3, 2);
127         CPPUNIT_ASSERT_EQUAL (r, framepos_t (288e3 - 24e3));
128
129         /* Subtract 2.5 beats from 4|2 (over the tempo change) */
130         r = map.framepos_minus_beats (288e3 + 12e3, 2.5);
131         CPPUNIT_ASSERT_EQUAL (r, framepos_t (288e3 - 24e3 - 12e3));
132 }
133
134