globally change all use of "frame" to refer to audio into "sample".
[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 samples_per_beat = (60 / double (bpm)) * double (sampling_rate);
18
19         TempoMap map (sampling_rate);
20         Tempo tempo (bpm, 4.0);
21         Meter meter (4, 4);
22
23         map.replace_meter (map.meter_section_at_sample (0), meter, BBT_Time (1, 1, 0), 0, AudioTime);
24         map.replace_tempo (map.tempo_section_at_sample (0), tempo, 0.0, 0, AudioTime);
25
26         /* Walk 1 beats-worth of samples from beat 3 */
27         double r = map.framewalk_to_qn (samples_per_beat * 2, samples_per_beat * 1).to_double();
28         CPPUNIT_ASSERT_EQUAL (1.0, r);
29
30         /* Walk 6 beats-worth of samples from beat 4 */
31         r = map.framewalk_to_qn (samples_per_beat * 3, samples_per_beat * 6).to_double();
32         CPPUNIT_ASSERT_EQUAL (6.0, r);
33
34         /* Walk 1.5 beats-worth of samples from beat 3 */
35         r = map.framewalk_to_qn (samples_per_beat * 2, samples_per_beat * 1.5).to_double();
36         CPPUNIT_ASSERT_EQUAL (1.5, r);
37
38         /* Walk 1.5 beats-worth of samples from beat 2.5 */
39         r = map.framewalk_to_qn (samples_per_beat * 2.5, samples_per_beat * 1.5).to_double();
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.replace_meter (map.meter_section_at_sample (0), meter, BBT_Time (1, 1, 0), 0, AudioTime);
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 samples                                        288e3 samples
65           24e3 samples per beat                            12e3 samples per beat
66           0 pulses                                        4 pulses
67           |               |               |               |               |
68           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 5.1
69           0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15  16
70
71         */
72
73         Tempo tempoA (120);
74         map.replace_tempo (map.tempo_section_at_sample (0), tempoA, 0.0, 0, AudioTime);
75         Tempo tempoB (240);
76         map.add_tempo (tempoB, 12.0 / tempoB.note_type(), 0, MusicTime);
77
78         /* Now some tests */
79
80         /* Walk 1 beat from 1|2 */
81         double r = map.framewalk_to_qn (24e3, 24e3).to_double();
82         CPPUNIT_ASSERT_EQUAL (1.0, r);
83
84         /* Walk 2 beats from 3|3 to 4|1 (over the tempo change) */
85         r = map.framewalk_to_qn (240e3, (24e3 + 24e3)).to_double();
86         CPPUNIT_ASSERT_EQUAL (2.0, r);
87
88         /* Walk 2.5 beats from 3|3.5 to 4.2 (over the tempo change) */
89         r = map.framewalk_to_qn (264e3 - 12e3, (24e3 + 12e3 + 12e3)).to_double();
90         CPPUNIT_ASSERT_EQUAL (2.5, r);
91         /* Walk 3 beats from 3|4.5 to 4|3.5 (over the tempo change) */
92         r = map.framewalk_to_qn (264e3 - 12e3, (24e3 + 12e3 + 12e3 + 6e3)).to_double();
93         CPPUNIT_ASSERT_EQUAL (3.0, r);
94
95         /* Walk 3.5 beats from 3|4.5 to 4.4 (over the tempo change) */
96         r = map.framewalk_to_qn (264e3 - 12e3, (24e3 + 12e3 + 12e3 + 12e3)).to_double();
97         CPPUNIT_ASSERT_EQUAL (3.5, r);
98 }
99
100 void
101 FramewalkToBeatsTest::tripleTempoTest ()
102 {
103         int const sampling_rate = 48000;
104
105         TempoMap map (sampling_rate);
106         Meter meter (4, 4);
107         map.replace_meter (map.meter_section_at_sample (0), meter, BBT_Time (1, 1, 0), 0, AudioTime);
108
109         /*
110           120bpm at bar 1, 240bpm at bar 2, 160bpm at bar 3
111
112           120bpm = 24e3 samples per beat
113           160bpm = 18e3 samples per beat
114           240bpm = 12e3 samples per beat
115         */
116
117
118         /*
119
120           120bpm            240bpm            160bpm
121           0 beats           4 beats           8 beats
122           0 samples          96e3 samples       144e3 samples
123           0 pulses          1 pulse           2 pulses
124           |                 |                 |                 |                 |
125           | 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 |
126
127         */
128
129         Tempo tempoA (120, 4.0);
130         map.replace_tempo (map.tempo_section_at_sample (0), tempoA, 0.0, 0, AudioTime);
131         Tempo tempoB (240, 4.0);
132         map.add_tempo (tempoB, 4.0 / tempoB.note_type(), 0, MusicTime);
133         Tempo tempoC (160, 4.0);
134         map.add_tempo (tempoC, 8.0 / tempoB.note_type(), 0, MusicTime);
135
136         /* Walk from 1|3 to 4|1 */
137         double r = map.framewalk_to_qn (2 * 24e3, (2 * 24e3) + (4 * 12e3) + (4 * 18e3)).to_double();
138         CPPUNIT_ASSERT_EQUAL (10.0, r);
139 }
140
141 void
142 FramewalkToBeatsTest::singleTempoMeterTest ()
143 {
144         int const sampling_rate = 48000;
145         int const bpm = 120;
146
147         double const samples_per_beat = (60 / double (bpm)) * double (sampling_rate);
148
149         TempoMap map (sampling_rate);
150         Tempo tempo (bpm, 4.0);
151         Meter meter (7, 8);
152
153         map.replace_meter (map.meter_section_at_sample (0), meter, BBT_Time (1, 1, 0), 0, AudioTime);
154         map.replace_tempo (map.tempo_section_at_sample (0), tempo, 0.0, 0, AudioTime);
155
156         /* Walk 1 qn beats-worth of samples from beat 3 */
157         double r = map.framewalk_to_qn (samples_per_beat * 2, samples_per_beat * 1).to_double();
158         CPPUNIT_ASSERT_EQUAL (1.0, r);
159
160         /* Walk 6 qn beats-worth of samples from beat 4 */
161         r = map.framewalk_to_qn (samples_per_beat * 3, samples_per_beat * 6).to_double();
162         CPPUNIT_ASSERT_EQUAL (6.0, r);
163
164         /* Walk 1.5 qn beats-worth of samples from beat 3 */
165         r = map.framewalk_to_qn (samples_per_beat * 2, samples_per_beat * 1.5).to_double();
166         CPPUNIT_ASSERT_EQUAL (1.5, r);
167
168         /* Walk 1.5 qn beats-worth of samples from beat 2.5 */
169         r = map.framewalk_to_qn (samples_per_beat * 2.5, samples_per_beat * 1.5).to_double();
170         CPPUNIT_ASSERT_EQUAL (1.5, r);
171 }